Skip to main content

onetaskgraph_github_projects/
lib.rs

1//! A stateless onetaskgraph source over one GitHub Projects v2 board.
2//!
3//! **A write held to a version** (`--if-version`) is checked immediately before its first
4//! mutation and not as one step with it: GitHub takes no conditional write, so the engine reads
5//! the task through this source just before writing it, and a change landing between that read
6//! and the mutation — the time one request takes — is GitHub's residual race.
7//!
8//! **A board is a container of projects, not a project.** Its own `title`,
9//! `shortDescription` and `readme` are never read as an item's fields and are never
10//! written: nothing in this source can rename the board a user configured.
11//!
12//! **A project is an issue and its tasks are that issue's sub-issues.** GitHub's schema
13//! decides that: `Issue` exposes `parent`, `subIssues` and `subIssuesSummary`, and
14//! `DraftIssue` exposes none of them. Creating an issue needs a `repositoryId`, and a
15//! board has none, so a write without [`GitHubProjectsConfig::repository`] is refused
16//! naming the field — but that repository is the *fallback*, not the home of every item.
17//!
18//! <!-- llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] The rule's one
19//! executable source is `GitHubProjectsSource::creation_target`; this is where a reader of
20//! the module meets it, and `tests/plugin.rs` drives every arm below against the loopback
21//! board and asserts on `createIssue`'s own `repositoryId`, so the prose cannot outlive a
22//! change to the rule. -->
23//! **Which repository an issue is created in is decided by the item's own `repositories`
24//! field, under one rule.** Exactly one entry names the repository the issue is created in:
25//! a task issue is where a person finds the work from the repository it changes, and one
26//! filed in a board's nominated repository is invisible from every other. Zero entries, or
27//! two or more, name none, so a task's or a document's issue is created in the repository
28//! its parent project's issue lives in — read from the board, or from this process's own
29//! record of a project it created earlier in the same command — and a project's issue, or
30//! a task or document written with no parent, is created in the configured `repository:`.
31//! What that rule refuses, it refuses before `createIssue`, so no issue is half-created. An
32//! existing issue is never moved: the update path leaves the issue where it is and records
33//! the list in the metadata slot when it differs, so the read side's derivation and the
34//! creation rule agree by construction.
35//! <!-- llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate] -->
36//!
37//! **A document is an ordinary issue whose title begins [`DESIGN_TITLE_PREFIX`].** A
38//! board has no document type and nothing but issues to hold one in, so the title is the
39//! discriminator and it is the whole of it. The title this source *reports* is the one a
40//! person wrote, with the prefix taken off — the same way the metadata slot is taken off
41//! the body so `content` is what the person wrote — and writing a document puts the prefix
42//! back, so a round trip returns the title that went in.
43//!
44//! **Telling a document from a project from a task.** The design prefix is read **first**:
45//! a document is never a project and never a task, whatever sub-issues it has or does not
46//! have. Only then does the rest apply — a board issue is a project when *either* it has
47//! sub-issues *or* it carries [`ItemKind::METADATA_KEY`]; otherwise it is a task. A
48//! sub-issue is always a task, whatever it carries. The marker is sufficient and never
49//! necessary: it is what makes an *empty* project — the state a project copy passes
50//! through between creating the project and filing its first task — readable as a
51//! project, while the sub-issue arm lets a person author a project on the board by hand
52//! with no knowledge of this product's metadata at all. Reading the prefix later than the
53//! sub-issue rule would make a design issue with no sub-issues an empty project, which is
54//! exactly the state that rule exists to catch. Pull requests are neither a project nor a
55//! task nor a document and are ignored.
56//!
57//! **A task's comments are its issue's comments.** They are read off `Issue.comments` and
58//! written with `addComment`, `updateIssueComment` and `deleteIssueComment`, and a comment's
59//! id is GitHub's own node id for the `IssueComment`. Two things GitHub decides are refused
60//! rather than papered over: a board **draft** is not an issue and has no comments at all, so
61//! a comment call on one is refused rather than answered with an empty page; and GitHub signs
62//! every comment as the account the token belongs to, so a comment handed an author of its
63//! own is refused rather than posted under another name. GitHub's comment mutations take the
64//! comment's id and nothing else, so an edit or a delete first reads which issue that comment
65//! is on, and a comment on some other issue is one this task does not have.
66//!
67//! **Where an entity is, is a link.** Every project, task and document this source reports
68//! carries a [`Location::Url`] naming the issue's own web address — the same address the
69//! `url` field already reports, in the shape that says a reader can open it. That is the
70//! contrast the location contract exists for: a reader holding an entity from this source
71//! is handed something to link to and one holding an entity from a folder of Markdown is
72//! handed a path, and neither has to know which plugin answered. It does not replace or
73//! derive from `url`; that field goes on reporting what it always reported.
74//!
75//! **Where metadata lives.** Short typed things go to typed fields and native relations:
76//! status to the board's `Status` single-select and the issue's own state, the copy
77//! origin to a source-owned `onetaskgraph.origin` text field, and dependencies to
78//! `blockedBy` and to sub-issue links. Unbounded caller JSON goes in a trailing
79//! `<!-- onetaskgraph.metadata ... -->` comment at the end of the issue body — the same
80//! encoding `docs/metadata.md` settles for Linear, not a second one. A ProjectV2 text
81//! field is length-bounded and `shortDescription` is capped at 300 characters, which is
82//! why neither can hold a caller's own prose. Setting one caller key on its own — on a task,
83//! a project or a document alike — is one update of the issue body that changes that slot
84//! and not one byte outside it, and it is not sent at all when the key already holds the
85//! value. The link a copy records on an item it copied, `onetaskgraph.copies`, is small and
86//! is kept in that same slot, written by that same update.
87//!
88//! **A caller's value can also be projected onto a board text field**, so the board can filter
89//! on it: each [`GitHubProjectsConfig::metadata_fields`] entry names a text field, a top-level
90//! metadata key and a path inside its value. The slot stays the value's one home — every read
91//! reports metadata from it alone — and the field follows it on every write of an item, a
92//! create, an update and a copy alike: a string is written, and not sent again while the field
93//! holds it — an empty string included, which is written as itself; nothing there or `null`
94//! clears a field holding a value; any other
95//! JSON type is refused before any mutation. It rides the ordered [`graphql::UPDATE_FIELDS`]
96//! write beside `Status`, `Priority` and the origin, so it adds no request; a key set on its
97//! own moves its field first and the body last, as every write of an existing item does. A
98//! board without the field, or with a non-text field of that name, is refused before any
99//! mutation pointing at `sources fields`, whose [`GitHubProjectsSource::fields`] creates it as
100//! a text field and never changes a field that is there.
101//!
102// llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] This public module documentation is a required user-facing description; the loopback plugin tests and shared live journey drive StatusMapping resolution, both mutations, and observed read-back together.
103//! **Status.** `status_mapping` is per-instance configuration, in the shared grammar
104//! [`onetaskgraph_plugin_api::StatusMapping`] documents, from a status category to an
105//! option of the board's one `Status` field for a task and for a project: a bare name is
106//! the option for both kinds, `null` disables the category for both, and `{task, project}`
107//! names it per kind. A category the mapping does not mention keeps its shipped default for
108//! both kinds; one it mentions is exactly what it configures, so a per-kind object no longer
109//! gets the shipped default for the kind it leaves out. Two categories one kind would read
110//! back from one option are refused as the configuration is read, ignoring case, while one
111//! option may stand for different categories of the two kinds. Writes go by the kind of the
112//! item written: a status that kind has no option for, or whose option the board lacks, is
113//! refused before any mutation, naming the source, the kind, the category and the key
114//! `status_mapping.<category>.<kind>` — there is no fallback. `done` selects its mapped
115//! option and closes the issue as `COMPLETED`; `cancelled` selects its mapped option and
116//! closes it as `NOT_PLANNED`, for either kind. Every open category reopens a closed issue
117//! before selecting its option. Reads give a closed issue's reason precedence over its
118//! option, while an open issue's option decides its category through its own kind's
119//! mapping, and an option that mapping does not name reads as `unknown` under its own name.
120//! The guarded [`GitHubProjectsSource::status_options`] and
121//! [`GitHubProjectsSource::fields`] operations are the one path here that calls
122//! `updateProjectV2Field`: GitHub replaces the whole option list, so they preserve every
123//! existing option id and verify the field and item assignments immediately afterwards.
124//! They ask for both kinds' options, counting a terminal category's mapped option as
125//! configured because a terminal write refuses without it. No ordinary source read or
126//! write calls that mutation, whose
127//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
128//! and a mistake destroys every item's status. A status this board cannot represent is a
129//! refusal naming the status and the instance instead.
130//!
131//! `unknown` has no shipped option because this source cannot preserve an open-ended
132//! status word: it writes an existing board option and never
133//! creates an option. An operator may map `unknown` to one existing option, in which case
134//! every unknown word lands on that option and reads back as `unknown` under the option's
135//! name. This differs from `local-md`, which writes and reads the original word itself.
136//!
137//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
138//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
139//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
140//! finished tasks were only moved to a "Done" column would read 0% complete forever.
141// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
142//!
143//! # What this source declares, field by field
144//!
145//! One verdict per field of [`Capabilities`], and what `Native` means when this source
146//! says it. *Proven* means a shared journey drives it against the real
147//! binary over this source's own row in `crates/onetaskgraph-e2e-support/src/fixtures.rs`, and
148//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
149//! [`capabilities`](TaskSource::capabilities) from parting.
150//!
151//! | Field | Verdict |
152//! | --- | --- |
153//! | `projects` | **Supported and proven,** and the one predicate here that is pushed down rather than applied in process: a task's project is the issue it is a sub-issue of, so a listing scoped to one *asks that issue* for its own sub-issues. This is the field that was declared and then not applied, which silently returned another project's tasks. |
154//! | `documents` | **Supported and proven.** A board holds issues, so a document is one: the issue whose title begins [`DESIGN_TITLE_PREFIX`]. Reads, filters and paging answer on exactly the terms a task read does, and a write puts the prefix back. |
155//! | `comments` | **Supported and proven,** over the task issue's own comment connection, oldest first and paged by GitHub's own cursor; added, edited and removed through GitHub's comment mutations, paced as every other mutation is. A draft item has no comments on GitHub and is refused, and so is an author, because GitHub records the signed-in account as every comment's author. |
156//! | `assets` | **Supported and native.** Image references travel as GitHub user attachments in the repository the task or document issue lives in. Uploads are authenticated and verified before the issue body is written. |
157//! | `priority` | **Supported and proven** by an instance configured with `priority_mapping`, and declared unsupported by one without it, which reports every task's priority as `none` and sends exactly the requests it sent before priorities existed. The priority is the board's single-select `Priority` field: no value is `none`, a mapped option is its level, matched case-insensitively, and an option the mapping does not name fails the read of that task, naming the option. A write selects the mapped option, or clears the value for `none`; a board without the field or the option is refused, pointing at `sources fields`, which is the one thing that creates either. |
158//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
159//! | `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. |
160//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
161//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
162//! | `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. |
163//! | `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. |
164//! | `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. |
165//! | `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. |
166//! | `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. |
167//! | `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. |
168//! | `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. |
169//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
170//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
171//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
172//!
173//! Image writes use `POST https://uploads.github.com/user-attachments/assets` with `name`,
174//! `content_type` and the issue repository's numeric `repository_id` as query parameters,
175//! the source token as `Authorization: Bearer`, the image content type as `Content-Type`,
176//! and the raw bytes as the request body. The numeric repository id is read at most once
177//! per repository per source instance. A loopback API endpoint also selects the loopback
178//! uploads host; production needs no assets repository, commits or extra configuration.
179//! Every returned JSON `url` is read with the same token before a body is written: only
180//! a 2xx verification succeeds. A refusal names the asset, URL and HTTP status; an
181//! anonymous 404 does not invalidate an authenticated 200. An upload refusal names the
182//! asset and HTTP status and says the token type may not be accepted by the upload
183//! endpoint. A classic PAT was measured accepted; no acceptance claim is made about
184//! fine-grained PATs or OAuth tokens.
185//! The body keeps the authored alt text while `./<name>` becomes the attachment URL,
186//! and `onetaskgraph.assets` records `{sha256, url}` by name. A matching destination
187//! SHA-256 reuses its URL without an upload or verifying read; changed bytes are uploaded
188//! and verified afresh, in the repository the issue already lives in on an update.
189//! An uploaded image renders for the viewers GitHub lets read it, like the issue text beside it.
190//! Uploads take the same mutation spacing as content writes. Both uploads and verification
191//! reads wait out classified rate limits within the configured per-call retry budget,
192//! on the supplied clock; verifying reads take no mutation slot.
193//!
194//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
195//! source has documents and that its tasks have comments, both of which hold — and the three
196//! facts behind the uniform `Native` on the
197//! predicates beside it are recorded below rather than re-derived, because a reader who
198//! takes `Native` to mean *the remote service filters* will read that uniformity as a
199//! lie.
200//!
201//! First, the plugin contract defines `Support::Native` as *the source applies this
202//! predicate itself*, and says nothing about where it applies it. What the declaration
203//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
204//! — so that the engine may push it down and apply nothing of its own.
205//!
206//! Second, this source can keep that promise for every predicate at no additional API
207//! cost, because whichever of the reads below answers a query has already read every
208//! candidate that query will return before it filters anything. Filtering those items is
209//! in-process work over data already in hand.
210//!
211//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
212//! applied in process over what that question returned. A project filter has a relationship — a
213//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
214//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
215//! a search for metadata values, is the board-scoped issue search carrying the text and each
216//! value as quoted phrases; an origin is the board's own field filter over its origin field
217//! beside the same search for the id. **The text search narrows, and that is this source's
218//! declared semantics:** GitHub matches whole words where the substring rule this source and
219//! the local Markdown source confirm with would match inside one, so an item holding the text
220//! only inside a longer word is never a candidate. Every item returned does contain the text.
221//! A project query's text, and a document query's scoped to no project, is that same search
222//! and narrows on the same terms, its candidates confirmed by their kind as well.
223//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
224//! so those three are applied in process over the candidates, and a query carrying none of
225//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
226//! engine compensate for work this source has already done, and declaring `projects` native
227//! while ignoring the filter (which this source once did) silently returns another project's
228//! tasks, because the engine trusts the declaration and applies nothing locally.
229//!
230//! # The three ways this source reaches an item, and what each costs
231//!
232//! A board read is charged for what its *nested* connections could return rather than for
233//! what was asked, so one whole-board read costs the same whether the question was about
234//! one project or about all of them. That is why a question about one project is never
235//! answered by reading the board:
236//!
237//! | The question | What is sent | What it costs |
238//! | --- | --- | --- |
239//! | 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 |
240//! | 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 |
241//! | one board page with each task's first comments, for `TaskSource::query_task_details` | [`graphql::BOARD_WITH_COMMENTS`] — the board's own `items`, without search completion | tasks and first comment pages in one request; board consistency, with comment-activity filtering refused |
242//! | 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 |
243//! | 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 |
244//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
245//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
246//! | 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 |
247//! | 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 |
248//! | 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 |
249//! | 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 |
250//! | 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 |
251//! | 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 |
252//!
253//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
254//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
255//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
256//! equal to declared points equal to the row. They include the origin lookup and the
257//! field/repository discovery a create needs. A bound re-copy changes status, priority,
258//! content and metadata; comment recount means a subsequent detail read. Each request here
259//! costs one declared point. A membership beyond the embedded page can additionally require
260//! the one-point membership recovery described above. A bound re-copy of a task filed under a
261//! project adds one read, the engine confirming that project's link by its own id once per
262//! command; and the same-source far ends a write newly names — those that do not already block
263//! the item, whose own read answered for them — are read together by their own ids,
264//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
265//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
266//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
267//!
268//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
269//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
270//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
271//! holds both halves.
272//!
273//! **An existing item is written body last.** A bound re-copy and a `task update` send its
274//! board fields first — the `Status` option and the `Priority` together, in one request — then
275//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
276//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
277//! undoing an earlier field when a later one fails, so that order is what makes a write
278//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
279//! the one piece of metadata written before the body, an origin a copy re-points, is put back
280//! when a later write is refused — and when putting it back is refused too, the write's own
281//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
282//! of those writes in turn, whole and as one aliased field failing after the one before it.
283//!
284//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
285//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
286//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
287//!
288//! - **A board is accepted at creation but its item is not answered, so a create still files
289//!   the issue itself: a new copy is 5 requests, and 4 with `--create`.**
290//!   `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
291//!   Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
292//!   The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
293//!   against a real board on 2026-10-01 with a create sending the board there and reading the
294//!   item off the payload's `Issue.projectItems`: every one of its four creates answered with
295//!   no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
296//!   refused "Content already exists in this project" — GitHub had filed the issue after
297//!   answering, and refuses a second filing rather than answering with the item it holds. A
298//!   create therefore sends no `projectV2Ids` and files the issue with
299//!   `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
300//!   real is the read before it: the board's fields and the repository's id together, in
301//!   [`graphql::CREATION_CONTEXT`], at the point the repository is known.
302//! - **A comment still reads its target first, so a comment is 2 requests.**
303//!   `AddCommentInput.subjectId: ID!` is declared there with
304//!   `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
305//!   "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
306//!   project's issue, a document's issue, an issue on no board of this source and a pull
307//!   request all are: GitHub writes the comment, so there is no refusal to map into "that is
308//!   not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
309//!   refuses those by name.
310//!
311//! | Verb | Requests / points | Documents |
312//! | --- | --- | --- |
313//! | 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 |
314//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, CREATED_BOARD_ITEMS, WRITE_FIELD_VALUES: the new copy with no origin to ask about |
315//! | 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 |
316//! | 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 |
317//! | 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 |
318//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
319//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
320//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
321//! | 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 |
322//! | recount | 1 | ISSUE_DETAIL |
323//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
324//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
325//! | content | 2 | ISSUE, UPDATE_ISSUE |
326//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
327//! | 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 |
328//! | 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 |
329//! | record only | 1 | ISSUE |
330//!
331//! <!-- github-search-paging:start -->
332//! Board-scoped text, metadata, project-name and comment-activity searches send every
333//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
334//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
335//! needs rows. A page is never resized to the rows still needed: GitHub orders one
336//! search differently at different page sizes, so one fixed size makes a paged walk
337//! send exactly the requests one whole read sends, and the answer's order is the order
338//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
339//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
340//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
341//! returned and fetched pages: a limit is sliced from the pages it needs, and local
342//! confirmation can require more candidates than matching rows. Walking all pages
343//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
344//! cursor and how far into that page the last answer stopped, and resumes in the same
345//! process or a new one, without duplicates or gaps. It carries no rows: one process
346//! sends each page's search once, and a new process re-reads only the page it resumes
347//! in, then sends a further page once, never as a re-read, only when its limit still
348//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
349//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
350//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
351//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
352//! not required to include the original process's writes still omitted by the index.
353//! <!-- github-search-paging:end -->
354//!
355//! The board half of an issue — its board item's id, its `Status` option and this
356//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
357//! an item reached any of those ways resolves through the same
358//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
359//! same status, the same labels and the same qualified id. That connection comes back a
360//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
361//! on the page in hand and — only if that page reports more of the connection — in the
362//! last row's read of that one issue's memberships, resumed from the page's own cursor and
363//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
364//! report, which is what keeps an id naming another repository's issue from being answered
365//! as an item of this board; and because the page is where the search starts rather than
366//! where it ends, that answer is one about a connection read to exhaustion and never about
367//! an unread page. Nothing costs the extra read but an issue on more boards than a page
368//! holds: an issue this board really does not hold reports no next page, so its
369//! memberships are already exhausted where they arrived.
370//!
371//! **No document here selects the board's own `Labels` field, and nothing is lost by
372//! that.** An item's labels are read from its content alone, wherever that content is
373//! reached: the three documents above select `Issue.labels` on the fragment, and
374//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
375//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
376//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
377//! create one, and `ProjectV2FieldValue` — the whole of what
378//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
379//! it from the content, for every content type it exists on, and there is nothing it can
380//! hold that the content does not already say: for an `Issue` it *is* that issue's own
381//! labels, so selecting it beside them unions a set with itself.
382//!
383//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
384//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
385//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
386//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
387//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
388//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
389//! label set, and that set is the fixture issue's own, by
390//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
391//! item whose content is a draft reports an empty set, by
392//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
393//! selection is held over [`graphql::DOCUMENTS`] by
394//! `no_document_selects_the_boards_own_labels_field`.
395//!
396//! The whole-board row is still the board's own item connection, and deliberately: a
397//! **draft** board item is not an issue, so no search can list one, and the reads that have
398//! to answer for the whole board are the ones whose cost is the board's size anyway.
399//!
400//! **A question about one item this source already names by id never lists the board.**
401//! Whether that item is on this board, and what its board fields are, is answered by reading
402//! that item — its own `Issue.projectItems`, walked to exhaustion by
403//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
404//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
405//! holds. That covers a write's destination, the project a new item is filed under, a
406//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
407//! and the delete that takes back an item a copy made. What such a write needs of the board
408//! and the item does not carry — the board's id, the `Status` and origin field definitions —
409//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
410//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
411//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
412//! minutes. Scanning this host's 842-item board has refused a document copy and an update
413//! even though the items' own reads named that board. A scan there gives the wrong answer
414//! as well as paying for every page. So a `board.items` lookup does not belong on any of
415//! those paths.
416//!
417//! **What a read may return is capped too, and that cap is on the document rather than on
418//! the board.** GitHub limits the number of nodes **one query may return** to
419//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
420//! an error naming the connection the count crossed at, not a slow or a partial result.
421//! Every board this source reads is refused the same way, so no board is too big for these
422//! documents and none is small enough to save one that is over.
423//!
424//! The count is arithmetic over the document's own text: each connection contributes the
425//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
426//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
427//! restate them — `github-graphql-node-count` implements them, and
428//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
429//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
430//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
431//! same text on every run and fails naming any that reaches the limit — so a connection
432//! added to a shared fragment is caught there rather than by GitHub.
433//!
434//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
435//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
436//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
437//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
438//! effectively squared there, which is why it is the one the limit is most sensitive to.
439//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
440//! page of memberships misses is recovered by one further read rather than refused, so it
441//! buys a bound every read pays for at the price of a request only a multi-board issue
442//! pays.
443//!
444//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
445//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
446//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
447//! `cost` is rate-limit points, metered per hour across everything one credential does; it
448//! is what the two limiters [`Limiter`] tells apart meter, and a document under
449//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
450//! that second number, and `tests/point_cost.rs` pins every document in
451//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
452//! one under, the pin itself is the check. The credentialed lane reconciles both figures
453//! against GitHub's own, off a probe it already sends.
454//!
455//! **What is pinned that way is a per-document price and never a session's.** The record in
456//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
457//! **requests** and **worst-case nodes** — and neither is points. What one whole session
458//! consumes of the hourly point allowance is observable only from a credentialed run's own
459//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
460//! and what `tests/live.rs` prints at the end of every run.
461//!
462//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
463//!
464//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
465//! enumerations of a board can supply one alone.** Resolving a node id is strongly
466//! consistent, so a read by id and a project's own sub-issues are already current. The
467//! other two are not, and they are behind by different amounts and in different directions:
468//!
469//! - GitHub's **issue search** is an index and answers a write made moments ago with the
470//!   value from before it — usually for a second or two.
471//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
472//!   on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
473//!   content withheld, absent, with the connection walked to its own `hasNextPage: false` —
474//!   for *minutes*, while `Issue.projectItems` names the same membership at once.
475//!
476//! That second one is a measurement rather than a caution. This repository's own
477//! credentialed journey writes a project and waits for the board to report it, then writes a
478//! task and waits for the same thing seconds later on the same board: the project wait is
479//! answered through the search and converged in two or three attempts in each of three runs,
480//! and the task wait is answered through `ProjectV2.items` and converged in none of them
481//! inside thirty. Separately, an item added to a second and larger board was read back by
482//! `Issue.projectItems` on that board's own id while every one of that connection's nine
483//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
484//! through the lagging one alone is what had a board read deny an issue that had certainly
485//! landed on it.
486//!
487//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
488//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
489//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
490//! search reports what the projection is behind on. What closes the last
491//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
492//! source answers is completed with what this process itself wrote, so an item created
493//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
494//! nothing is written down, and the record dies with the process. **A wait that has to
495//! observe GitHub's own data cannot be answered from that record** — which is why the
496//! credentialed journey asks through a source built afresh, and why the union above rather
497//! than a longer wait is what makes such a wait converge.
498//!
499//! **A narrowed read is the same bargain, stated for each of the three predicates it
500//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
501//! than walking the board, and every such answer is completed with what this process wrote —
502//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
503//! each filtered by the same predicates as the rest — so an item this command wrote a moment
504//! ago is returned by a query that matches it whether or not the index has caught up. An item
505//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
506//! consistent. What is left is stated rather than papered over:
507//!
508//! | Read | Finds | Behind by |
509//! | --- | --- | --- |
510//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
511//! | 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 |
512//! | 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 |
513//! | origin, third read | this process's own writes | nothing |
514//!
515//! So an origin carrier another process added within the last second or two, before either
516//! index has it, can be missing from an origin query, and one written by the release before
517//! this one — its origin in the field alone — can be missing for as long as the board's own
518//! item connection is behind on it. A copy that must not duplicate its own earlier write
519//! relies on the link it records, not on either index. **A board draft is not an issue**, so
520//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
521//! lists one, the origin lookup drops any the board's own field filter names, and one this
522//! process wrote is not added back either.
523//!
524//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
525//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
526//! body's metadata slot, so the issue search can find it in seconds. The field is
527//! authoritative: this source reads an item's origin from the field alone, so a slot that
528//! disagrees with it, or holds one where the field holds none, is never read as a second
529//! origin — and the release before this one reads the slot, drops that key's copy for the
530//! field's, and sees the same one origin.
531//!
532//! Filtering happens before paging, so a page of a filtered result is a page of the
533//! survivors rather than the survivors of a page. Label matching and the substring rule a
534//! text candidate is confirmed by answer the same question the same way the local Markdown
535//! source's do; which candidates a text search has to confirm is GitHub's word match, which
536//! is the one place the two sources can answer the same text differently.
537//!
538//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
539//! has one source, `capabilities`, and the note above is the reasoning behind it rather
540//! than a second copy of it: without the three facts recorded here a reader takes the
541//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
542//! crate's own capabilities test, which pins every field of it against a fully spelled-out
543//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
544//! fails to compile there rather than going unasserted. -->
545//! The fixture-server tests above run wherever this crate is selected; the credentialed
546//! lane runs in the same required check, beside them, and can fail it — it verifies the
547//! 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,
548//! one filed under neither, a label on one of the three and a closed status on another —
549//! because that shape is what tells an honoured predicate from an ignored one: a board
550//! holding a single project answers a project filter the same way whether or not this
551//! source applies it, which is exactly how the defect above went unseen.
552//!
553//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
554//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
555//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
556//! session or request, independently of the live demand. Fix that variable in the machine's
557//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
558//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
559//! policy crate; absent credentials or nominations otherwise skip.
560//!
561//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
562//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
563//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
564//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
565//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
566//! after its run reads back as `completed`, immediately before the listed-artifact batch.
567//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
568//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
569//! and the board's `onetaskgraph.origin` field untouched.
570//!
571//! # What a session of requests costs, and where the report is
572//!
573//! This source records **every** request it sends into [`accounting::Accounting`], at
574//! `send_once` — the one place a request leaves this crate, which is why a read path added
575//! later is counted without anybody remembering to count it. That is the whole of what this
576//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
577//! session's spend is arrived at, and what it deliberately does not know are set out.
578//!
579//! What one whole session of the live journey costs, counted that way against this crate's
580//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
581//! reduction it came out of, and with what it does and does not say about rate-limit points.
582//!
583//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
584//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
585//! code path — no environment variable, no feature, no build configuration — because an
586//! instrument nobody switches on measures nothing, and
587//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
588//! source's counts the whole session rather than this source's share. The credentialed lane
589//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
590//! passed or failed.
591//!
592//! **A live session refuses to start unless the account can afford it.** Before it does any
593//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
594//! GitHub documents as not counting against the REST rate limit and which answers both of
595//! its budgets at once — and starts only if, for each of them, what remains minus this
596//! session's estimated cost is still at least
597//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
598//! allowance. A session that cannot **declines**: it did not run, so it is
599//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
600//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
601//! waiting for the budget to come back. The estimate is derived offline from
602//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
603//! which is also where the published rule that model rests on is cited; the accounting
604//! above records the gate's own read like any other request, and
605//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
606//!
607//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
608//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
609//! text, which is what lets it run on every platform and on a pull request from a fork with
610//! no credential — and that is what actually stops a regression merging. But an offline
611//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
612//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
613//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
614//! number of nodes this query may return"* and whose `cost` is what that document would
615//! spend, both for a document **without executing it**, and the lane asks it for every query
616//! document this source sends, under the largest bindings this source sends, and fails when
617//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
618//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
619//! one; the offline pins still cover it. It records what those calls reported about the
620//! account's own allowance, because whether asking is free is a thing to observe rather than
621//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
622//! and `cost` is metered against an hourly allowance the accounting above reads off a
623//! credentialed run's own response headers.
624//!
625//! **GitHub has two rate limiters and this source is refused by both, so nothing here
626//! treats them as one thing.** The primary budget is the hourly allowance `gh api
627//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
628//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
629//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
630//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
631//! one part of.
632#![deny(missing_docs)]
633
634use std::collections::BTreeMap;
635use std::sync::{Arc, Mutex};
636use std::time::Duration;
637
638use chrono::{DateTime, Utc};
639use onetaskgraph_plugin_api::{
640    Capabilities, Classification, ClosedAs, Comment, CommentBody, CommentOrigin, Completion,
641    ContradictedItem, Coverage, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
642    DependencySupport, Designator, Direction, Document, DocumentQuery, DocumentVersionCoverage,
643    Health, HeldTask, IssueHolding, IssueRef, ItemKind, ItemWrite, Label, LabelFilter, Location,
644    MetadataKey, Metering, NativeId, NewComment, Page, PageRequest, Priority, Project,
645    ProjectFilter, ProjectQuery, ProjectVersionCoverage, Repository, SecretResolver, SharedClock,
646    SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusContradiction,
647    StatusMapping, Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate,
648    TaskUpdateOutcome, TextFields, TextQuery, UnmappedStatus, UpdatedField, VersionCoverage,
649    WaitCause, WriteJournal, WriteSupport, system_clock,
650};
651use reqwest::{Client, StatusCode, Url};
652use schemars::{Schema, schema_for};
653use secrecy::{ExposeSecret, SecretString};
654use serde::{Deserialize, Serialize};
655use serde_json::{Value, json};
656
657pub mod accounting;
658mod assets;
659pub mod change_token;
660mod expected;
661pub use expected::{CONTEXT_VERSION, WHOLE_MEMBERS, context_shapes};
662mod pacing;
663mod projection;
664mod visibility;
665
666use accounting::Accounting;
667
668/// The registry name for this plugin.
669pub const KIND: &str = "github-projects";
670/// GitHub's maximum connection page size.
671pub const MAX_PAGE_SIZE: u32 = 100;
672/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
673/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
674/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
675pub const SEARCH_PAGE_SIZE: u32 = 20;
676/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
677/// its comments: the largest batch the node-count model prices at one point.
678///
679/// Each aliased item is resolved once, and what GitHub charges for it is the connections
680/// under it — its labels, its page of board memberships, the field values of each of those
681/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
682/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
683/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
684/// one point and fails if one item more would still be priced at one.
685pub const DETAIL_BATCH: usize = 24;
686/// How many issue references one [`graphql::ISSUES_BY_REFERENCE`] request reads.
687///
688/// Each reference asks GitHub for one connection — a page of its board memberships, each with
689/// its `Status` value read by name rather than through a connection of field values — so fifty
690/// come to fifty requests' worth of the aggregate GitHub divides by a hundred, which is its
691/// one-point minimum, and the batch a landing's closures are read in costs one point per fifty
692/// issues per source. `tests/point_cost.rs` prices the document at one point.
693pub const REFERENCE_BATCH: usize = 50;
694/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
695/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
696/// point.
697///
698/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
699/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
700/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
701/// if one item more would still be priced at one.
702pub const COPY_DETAIL_BATCH: usize = 21;
703/// How many mutations one aliased request of a copy's creates, closes and updates carries:
704/// [`graphql::CREATE_ISSUES`], [`graphql::CLOSE_ISSUES`] and [`graphql::UPDATE_ISSUES`].
705///
706/// Eleven is what GitHub was seen to accept in one request — the timing spike
707/// (`board-copy-speed-2026-10-18-spike-copy-timing-report`) sent eleven aliased mutations of four
708/// kinds, three `createIssue`, three `addProjectV2ItemById`, three field updates, an
709/// `addSubIssue` and an `addBlockedBy`, and every one ran — and no more was established.
710pub const MUTATION_BATCH: usize = 11;
711/// How many aliased writes one request of a copy's board field values and blocked-by edges
712/// carries: [`graphql::WRITE_FIELD_VALUES`], [`graphql::ADD_BLOCKED_BYS`] and, for a request
713/// holding both kinds, [`graphql::WRITE_FIELDS_AND_EDGES`].
714///
715/// Nineteen is the most aliases the feasibility spike
716/// (`board-copy-gaps-2026-10-24-spike-feasibility-report`) sent in one mutation request and
717/// saw GitHub price at the one point every mutation request costs; no larger request was
718/// established, so a copy holding more is split.
719pub const WRITE_BATCH: usize = 19;
720/// How many issues one [`graphql::DELETE_ISSUES`] request deletes: one request of eleven
721/// aliased `deleteIssue` met `RESOURCE_LIMITS_EXCEEDED` from the eighth alias on, and batches
722/// of five succeeded (the feasibility spike's undo-batching run).
723pub const DELETE_BATCH: usize = 5;
724/// How many origins one [`graphql::ORIGIN_LOOKUPS`] request asks about: the nine records of a
725/// plan and the project's document, and no more, since each carries a page of the board's field
726/// filter and one of the issue search.
727pub const ORIGIN_BATCH: usize = 10;
728
729/// The most nodes any one document this source sends may be asked to return.
730///
731/// GitHub's own published per-query ceiling, taken from
732/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
733/// workspace cannot hold a stale copy of somebody else's number. A query above it is
734/// **refused before it is executed**, whoever is asking and whatever board they are
735/// asking about — so this is a bound on the documents rather than a budget that runs out.
736///
737/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
738/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
739/// everything the credential does — two numbers against two limits, and this constant
740/// bounds only the first. The second is computed offline too, per document:
741/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
742/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
743/// lane. There is no constant like this one to hold a price under, because points are an
744/// hourly allowance rather than a per-call bound.
745///
746/// Neither is a session's price. What `session-cost.md` records of a whole session is its
747/// **requests** and its **worst-case nodes**; what a whole session spends in points is
748/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
749/// [`accounting`]. The module section on the three ways this source reaches an item says how
750/// the count is arrived at, and which of the page sizes below decide it.
751pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
752
753/// Nested connection size for the connections that hang off one item.
754///
755/// It multiplies through every document that reaches an item under a page — the count
756/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
757/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
758/// every document under these constants and fails naming any that reaches the limit, so
759/// raising this is caught there rather than by GitHub.
760const NESTED_PAGE_SIZE: u32 = 50;
761/// How many of one issue's board memberships are read when an issue is reached directly.
762///
763/// An issue reached through a search or through its own node id carries its board half in
764/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
765/// under a page of issues, so every point of it multiplies through the whole document and
766/// is paid for whether or not any issue is on a second board — which is why it is
767/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
768///
769/// **Three, because what a page misses is now recovered rather than refused**, and the
770/// recovery is what the value is chosen against. An issue whose entry for this board sits
771/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
772/// that page's own cursor — so the value trades a bound every read pays for a request only
773/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
774/// boards would pay that request *per issue*, which is order N against the one page per
775/// hundred issues a read costs today. At three it is only reached by an issue on four or
776/// more boards at once, which keeps the recovery path exceptional rather than routine for
777/// a plausible deployment.
778const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
779/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
780/// its two connections for.
781///
782/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
783/// second is a duplicate a copy already takes the first of. Both connections are walked to
784/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
785/// never what the answer is. It is small because every point of it is paid on every lookup,
786/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
787/// worst-case nodes than the one whole-board read they replaced.
788const ORIGIN_PAGE_SIZE: u32 = 3;
789/// How many carriers of each origin the batched [`graphql::ORIGIN_LOOKUPS`] asks each of its two
790/// connections for: one, the one carrier an origin names.
791///
792/// A second carrier still shows: the page then says there is more, and that origin is left to
793/// its own [`graphql::ORIGIN_LOOKUP`], which walks every page at `ORIGIN_PAGE_SIZE` — so a
794/// duplicate is found exactly as before, by the lookup that reads all of them. The feasibility
795/// spike (`board-copy-gaps-2026-10-24-spike-feasibility-report`) priced the batch of ten
796/// origins at two points with a first page of three and at one with a first page of one.
797const ORIGIN_LOOKUPS_PAGE_SIZE: u32 = 1;
798
799/// How many of one project's sub-issues one [`graphql::SUB_ISSUES`] page asks for: the most
800/// one point buys, since every row carries six connections GitHub charges for once per row.
801/// `session-cost.md` derives it; `tests/point_cost.rs` prices the document at one point and
802/// fails if one row more would still be priced at one.
803pub const MEMBER_PAGE_SIZE: u32 = 23;
804
805pub use github_graphql_node_count::{NodeCountError, Variables};
806
807/// The largest value this source can bind to each page-size variable its documents name.
808///
809/// Every `first:` in [`graphql`] reads one of these seven, and each is capped at the
810/// constant above it wherever a caller's own limit could reach it — `$first` at
811/// [`MAX_PAGE_SIZE`], `$commentFirst` at [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
812/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`, `$lookupFirst` at
813/// `ORIGIN_LOOKUPS_PAGE_SIZE`, `$memberFirst` at [`MEMBER_PAGE_SIZE`]. So this is the worst case a caller can drive this source to,
814/// not one configuration of it, which is what makes a bound computed under it a bound on
815/// every read.
816pub fn largest_page_sizes() -> Variables {
817    Variables::from([
818        ("first".to_owned(), MAX_PAGE_SIZE),
819        ("commentFirst".to_owned(), MAX_PAGE_SIZE),
820        ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
821        ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
822        ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
823        ("lookupFirst".to_owned(), ORIGIN_LOOKUPS_PAGE_SIZE),
824        ("memberFirst".to_owned(), MEMBER_PAGE_SIZE),
825    ])
826}
827
828/// The most nodes `document` could be asked to return, by GitHub's published rules.
829///
830/// Computed offline from the document's own text under [`largest_page_sizes`] — no
831/// network, no credential and no schema — by
832/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
833/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
834/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
835///
836/// # Errors
837///
838/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
839/// no single operation, or binds a page size this source does not name — each of which is
840/// a defect in the document rather than a number.
841pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
842    node_count(document, &largest_page_sizes())
843}
844
845/// The most rate-limit points one call of `document` could spend, by GitHub's published
846/// rules.
847///
848/// Computed offline from the document's own text under [`largest_page_sizes`] — no
849/// network, no credential and no schema — by
850/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
851/// This is `cost`, metered **per hour** against the allowance one credential shares across
852/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
853/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
854/// under, so what `tests/point_cost.rs` does with it is pin every document in
855/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
856/// figures against GitHub's own reported `cost`.
857///
858/// # Errors
859///
860/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
861/// no single operation, or binds a page size this source does not name — each of which is
862/// a defect in the document rather than a number.
863pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
864    github_graphql_node_count::point_cost(document, &largest_page_sizes())
865}
866
867/// The most nodes `document` could be asked to return under `variables`.
868///
869/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
870/// [`accounting`] is this under the bindings one request really sent — one spelling of the
871/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
872/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
873///
874/// # Errors
875///
876/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
877/// no single operation, or binds a page size `variables` does not name.
878pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
879    github_graphql_node_count::node_count(document, variables)
880}
881
882/// The issue-title prefix that makes a board issue a document.
883///
884/// A GitHub Projects board has no document type — it holds issues — so the discriminator
885/// is the title, and this is the whole of it: an issue whose title begins with these bytes
886/// is a document and every other issue is the task or project the sub-issue rule makes it.
887///
888/// It is spelled **once**, here, and read rather than restated everywhere else — including
889/// by the shared journeys, which take it from this constant so a board fixture cannot
890/// drift from what this source reads. `docs/metadata.md` records the two consequences that
891/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
892/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
893/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
894pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
895
896/// Exact GraphQL query documents issued by this plugin.
897///
898/// Keeping the production documents here lets the pinned-schema test validate the same
899/// bytes that are sent to GitHub, rather than a test-only copy which could drift
900/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
901/// field, and its guarded caller always supplies the complete existing option set with ids.
902pub mod graphql {
903    /// The board half of one item: the field values every document here reads it from.
904    ///
905    /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
906    /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
907    /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
908    /// *the same value*, because
909    /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
910    /// one path. Three spellings of it is what would drift, so there is one.
911    ///
912    /// The `Status` option and this source's own origin text field are the whole of it. It
913    /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
914    /// content, so it holds nothing the content's own `labels` do not already say, and it
915    /// would sit a label connection two page sizes deep.
916    macro_rules! board_item_values {
917        () => {
918            r#"fieldValues(first:$nestedFirst){nodes{
919          ... on ProjectV2ItemFieldSingleSelectValue{name field{
920            ... on ProjectV2SingleSelectField{id name options{id name}}
921          }}
922          ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
923        }pageInfo{hasNextPage}}"#
924        };
925    }
926
927    /// The variable every document reading an issue declares for the relationship events its
928    /// change token counts, with its value as the default, so no caller binds it.
929    ///
930    /// A variable rather than a literal list because every argument a production document
931    /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
932    /// its default to the pinned enum. Its default is the six event types GitHub records a
933    /// relationship added or removed with: a dependency on either end, as `blockedBy` and
934    /// `blocking`, so one a person makes from the far end moves this end's count too; and a
935    /// project membership on the task, as its parent, which GitHub records there whichever
936    /// issue the move was made from. A sub-issue's events on its parent are not counted: a
937    /// project carries no task token, and a copy writes nothing of a task its own sub-issues
938    /// decide. [`super::change_token`] says why the count is in the token.
939    macro_rules! relationship_events_variable {
940        () => {
941            ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
942        };
943    }
944
945    /// Everything this source reads about one issue, wherever it reaches that issue.
946    ///
947    /// A macro rather than a constant so the three documents below can `concat!` it: one
948    /// spelling of these fields is what makes an issue read through the board-scoped
949    /// search, through its own node id, and through its project's sub-issue relationship
950    /// resolve to *the same* item, which is the whole of what
951    /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
952    ///
953    /// `projectItems` is what carries the board half of an issue: the board item's own id
954    /// and the [`board_item_values!`] above — the `Status` option and this source's origin
955    /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
956    /// issue rather than on the board, which is what makes the cost of a read proportional
957    /// to what was asked for instead of to the board's size.
958    ///
959    /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
960    /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
961    /// not on that page: a page here is where the search for the entry starts rather than
962    /// where it ends.
963    ///
964    /// It does **not** select the board's `Labels` field value, and that is the whole of
965    /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
966    /// a label connection there sits under `fieldValues` under `projectItems` under a page
967    /// of issues, spending `$nestedFirst` twice down one path, and took
968    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
969    /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
970    /// above, and that connection is where every label this source reports comes from. No
971    /// document in this module selects the board field any longer, [`BOARD`] included; the
972    /// module documentation records why nothing it could have held is lost.
973    macro_rules! board_issue {
974        () => {
975            concat!(
976                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}
977      labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
978      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
979        "#,
980                board_item_values!(),
981                r#"}pageInfo{hasNextPage endCursor}}}"#
982            )
983        };
984    }
985
986    /// One page of a repository's issues, newest first, each with when it was created and its
987    /// body: what a retried copy reads to find the issue its earlier attempt created, by the
988    /// create key in that body's metadata slot — never through search, whose index lags.
989    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}}}}";
990    /// Every issue of one board, found by a search scoped to that board.
991    ///
992    /// This is how the projects a board holds are listed, and it selects no `items`
993    /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
994    /// container walked page by page, so nothing nested inside a board item is paid for.
995    /// Which of the issues it returns is a project is then read off `parent` — GitHub
996    /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
997    /// discriminator has to be applied to the field, which is a scalar on the issue and
998    /// costs nothing.
999    pub const SEARCH_ISSUES: &str = concat!(
1000        r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1001        relationship_events_variable!(),
1002        r#"){
1003      search(query:$search,type:$type,first:$first,after:$after){
1004        pageInfo{hasNextPage endCursor}
1005        nodes{__typename ...BoardIssue}
1006      }
1007    }"#,
1008        board_issue!()
1009    );
1010
1011    /// What a dependency read selects of each far end: enough to say which kind of item it
1012    /// is, its body included for the kind marker.
1013    macro_rules! related_issue {
1014        () => {
1015            " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
1016        };
1017    }
1018
1019    /// One issue by its own node id, which is what a qualified id names here — with what a
1020    /// write of it needs and the issue does not carry in `board_issue!`: the field
1021    /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
1022    ///
1023    /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
1024    /// answers a write made moments ago with the value from before it, and resolving a node
1025    /// id does not.
1026    ///
1027    /// **Why those two ride here and not on the fragment.** A copy or an update of an item
1028    /// reads it by its own id, and with them that one read answers everything the write
1029    /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
1030    /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
1031    /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
1032    /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
1033    /// they sit under one item, and this read is still one point.
1034    pub const ISSUE: &str = concat!(
1035        r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1036        relationship_events_variable!(),
1037        r#"){
1038      node(id:$id){__typename ...BoardIssue ... on Issue{
1039        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1040          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1041          ... on ProjectV2Field{__typename id name dataType}
1042        }pageInfo{hasNextPage}}}}}
1043        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1044      }}
1045    }"#,
1046        board_issue!(),
1047        related_issue!()
1048    );
1049
1050    /// One document by its own node id, as [`ISSUE`] reads one, with the project it is filed
1051    /// under and that project's members beside it: the project issue whole, and a page of its
1052    /// sub-issues, each as [`SUB_ISSUES`] carries one but with no page of what blocks it.
1053    ///
1054    /// What a guarded write of a document's metadata reads, and what reading a document by its
1055    /// id is: a write to a board the boundary screens is held to who may read the document,
1056    /// which is the project's and every member's classification as much as its own, and here
1057    /// the one request that holds the version the write is compared against answers all of it.
1058    /// Its members carry no edges, which nothing classifying them reads.
1059    pub const DOCUMENT_IN_PROJECT: &str = concat!(
1060        r#"query($id:ID!,$first:Int!,$memberFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1061        relationship_events_variable!(),
1062        r#"){
1063      node(id:$id){__typename ...BoardIssue ... on Issue{
1064        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1065          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1066          ... on ProjectV2Field{__typename id name dataType}
1067        }pageInfo{hasNextPage}}}}}
1068        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1069        project:parent{__typename ...BoardIssue
1070          subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}
1071        }
1072      }}
1073    }"#,
1074        board_issue!(),
1075        related_issue!()
1076    );
1077
1078    /// One project's tasks: the sub-issues of the issue that project is, each with a page of
1079    /// what blocks it.
1080    ///
1081    /// Paged at `$memberFirst` — [`MEMBER_PAGE_SIZE`](super::MEMBER_PAGE_SIZE), the most rows
1082    /// one point buys — rather than at `$first`, because every row carries six connections and
1083    /// a hundred-row page cost six points to list a project of ten.
1084    ///
1085    /// The work this costs is the project's own size. Nothing about it grows as the board
1086    /// gains projects, or as those projects gain tasks.
1087    ///
1088    /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
1089    /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
1090    /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
1091    /// costs grow with its tasks rather than with the pages of them. Carried here, a task
1092    /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
1093    /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
1094    /// It adds a connection under each issue of the page — one rate-limit point per page,
1095    /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
1096    /// answer questions that never read an edge.
1097    ///
1098    /// **The project issue itself rides here too**, with the page of what blocks it, because
1099    /// the reader of a project's tasks reads the project next: a copy writing the project reads
1100    /// it and its edges before it writes them. Carried on the request that already names the
1101    /// project, the first page answers that read and the dependency read after it, at the
1102    /// state GitHub answered in this command, rather than spending one more read on an item
1103    /// the cache's token cannot vouch for whole. Each sub-issue is kept the same way, so a
1104    /// copy's read of a task this listing carried is answered by it rather than sent again.
1105    /// Only a copy's read-before-write takes them; every other read by id reads GitHub.
1106    pub const SUB_ISSUES: &str = concat!(
1107        r#"query($id:ID!,$memberFirst:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1108        relationship_events_variable!(),
1109        r#"){
1110      node(id:$id){__typename ...BoardIssue
1111        ... on Issue{blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1112          subIssues(first:$memberFirst,after:$after){
1113          pageInfo{hasNextPage endCursor}
1114          nodes{__typename ...BoardIssue ... on Issue{
1115            blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1116          }}
1117        }}}
1118    }"#,
1119        board_issue!(),
1120        related_issue!()
1121    );
1122
1123    /// Everything this source reads about one issue comment, wherever it reaches one.
1124    ///
1125    /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1126    /// and a comment just edited are handed to one mapper, so they are selected by one
1127    /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1128    /// longer exists, and `login` is the one member every kind of actor carries; `__typename`
1129    /// says whether it is a `Bot`, whose `login` GitHub's own pages spell with `[bot]` after it.
1130    macro_rules! issue_comment {
1131        () => {
1132            "id author{__typename login} createdAt updatedAt body url"
1133        };
1134    }
1135
1136    /// What a read of the board's own `items` selects of each item's content.
1137    ///
1138    /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1139    /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1140    /// content by one spelling.
1141    macro_rules! board_item_content {
1142        () => { board_item_content!("") };
1143        ($comments:expr) => {
1144            concat!(r#" content{
1145        ... on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount} labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}"#, $comments, r#"}
1146        ... on PullRequest{__typename id}
1147        ... on DraftIssue{__typename id title body createdAt updatedAt}
1148      }"#)
1149        };
1150    }
1151
1152    macro_rules! board_listing {
1153        ($variables:expr, $comments:expr) => { concat!(
1154        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1155        relationship_events_variable!(), $variables,
1156        r#"){
1157      owner:repositoryOwner(login:$owner){
1158        ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1159      }
1160    } fragment Board on ProjectV2 { id title
1161      fields(first:$nestedFirst){nodes{
1162        ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1163        ... on ProjectV2Field{__typename id name dataType}
1164      }pageInfo{hasNextPage}}
1165      items(first:$first,after:$after){nodes{id updatedAt "#,
1166        board_item_values!(),
1167        board_item_content!($comments),
1168        r#"} pageInfo{hasNextPage endCursor}}
1169    }"#
1170    ) };
1171    }
1172
1173    /// Reads the board's fields and one page of its items.
1174    pub const BOARD: &str = board_listing!("", "");
1175
1176    /// A board page carrying each issue's first comment page in the same request.
1177    pub const BOARD_WITH_COMMENTS: &str = concat!(
1178        board_listing!(
1179            ",$commentFirst:Int!",
1180            concat!(
1181                "comments(first:$commentFirst){nodes{",
1182                issue_comment!(),
1183                "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}"
1184            )
1185        ),
1186        related_issue!()
1187    );
1188
1189    /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1190    /// the board.
1191    ///
1192    /// **`originItems`** is the board's own items narrowed by its own field filter —
1193    /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1194    /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1195    /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1196    /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1197    /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1198    /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1199    /// fresh `addProjectV2ItemById` the way that connection does.
1200    ///
1201    /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1202    /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1203    /// indexes that comment, and the index catches up with a write in a second or two rather
1204    /// than in minutes, so it finds a carrier another process wrote that the first read is
1205    /// still behind on.
1206    ///
1207    /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1208    /// — and resumes from its own cursor; a connection already walked to its end is resumed
1209    /// from its last cursor, which answers an empty page. Every candidate either read returns
1210    /// is confirmed against its own origin field before it is reported, so a token match of
1211    /// the search or anything else the filter admits never is.
1212    ///
1213    /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1214    /// own whole reads counts this one among them.
1215    pub const ORIGIN_LOOKUP: &str = concat!(
1216        r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1217        relationship_events_variable!(),
1218        r#"){
1219      originItems:repositoryOwner(login:$owner){
1220        ... on ProjectV2Owner{projectV2(number:$number){
1221          items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1222        board_item_values!(),
1223        board_item_content!(),
1224        r#"} pageInfo{hasNextPage endCursor}}
1225        }}
1226      }
1227      search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1228        pageInfo{hasNextPage endCursor}
1229        nodes{__typename ...BoardIssue}
1230      }
1231    }"#,
1232        board_issue!()
1233    );
1234
1235    /// Every closed item of the board, by the board's own field filter — what
1236    /// `task status reconcile` reads, and nothing else of the board.
1237    ///
1238    /// **`ProjectV2.items(query:)` narrowed by `$filter`**, which this source binds to
1239    /// `is:closed` alone, so a board whose open items are most of it is not read whole to find
1240    /// the closed ones. It is narrowed rather than trusted: every item it answers is confirmed
1241    /// closed, with a reason and a card that contradict each other, from the item node it
1242    /// already carries, so a filter that also let open items through would cost reads and
1243    /// write nothing wrong.
1244    ///
1245    /// The board's own id and field definitions ride along, as [`BOARD`] carries them, so the
1246    /// option writes that follow need no read of the board. The root is aliased `closedItems`
1247    /// rather than `owner`, so nothing counting the board's own whole reads counts this one
1248    /// among them.
1249    pub const CLOSED_ITEMS: &str = concat!(
1250        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$filter:String!,$nestedFirst:Int!,$duplicates:Boolean!"#,
1251        relationship_events_variable!(),
1252        r#"){
1253      closedItems:repositoryOwner(login:$owner){
1254        ... on ProjectV2Owner{projectV2(number:$number){id
1255          fields(first:$nestedFirst){nodes{
1256            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1257            ... on ProjectV2Field{__typename id name dataType}
1258          }pageInfo{hasNextPage}}
1259          items(first:$first,after:$after,query:$filter){nodes{id updatedAt "#,
1260        board_item_values!(),
1261        board_item_content!(),
1262        r#"} pageInfo{hasNextPage endCursor}}
1263        }}
1264      }
1265    }"#
1266    );
1267
1268    /// One alias of [`ISSUES_BY_REFERENCE`]: the issue `$owner<n>/$name<n>#$number<n>` names.
1269    macro_rules! reference_alias {
1270        ($n:literal) => {
1271            concat!(
1272                "\n      r",
1273                stringify!($n),
1274                ":repository(owner:$owner",
1275                stringify!($n),
1276                ",name:$name",
1277                stringify!($n),
1278                "){issue(number:$number",
1279                stringify!($n),
1280                "){...HeldIssue}}"
1281            )
1282        };
1283    }
1284
1285    /// [`REFERENCE_BATCH`](super::REFERENCE_BATCH) issues at once, each by its repository and
1286    /// number — the one read `task status complete` makes of a source, and nothing else of
1287    /// the board: no `items`, no search and no project read.
1288    ///
1289    /// **Its own fragment, lighter than `board_issue!`, and that is what makes fifty
1290    /// references one point.** It selects no labels, and of each board membership only the
1291    /// `Status` value, by name, with `fieldValueByName` — so each alias asks GitHub for one
1292    /// connection, its page of memberships, where the whole fragment asks for five. What a
1293    /// completion writes is a card and, at most, the issue's state, so the labels and the other
1294    /// field values it leaves out are never what it needs; an item read this way is held for
1295    /// that write alone and never answers another read. An issue whose entry for this board is
1296    /// past its first page of memberships is walked with [`ISSUE_BOARD_ITEMS`], exactly as
1297    /// every other read of an issue recovers one.
1298    ///
1299    /// Fixed-size, as [`ISSUE_DETAILS`] is, so there is one document to price: a batch of
1300    /// fewer references binds the slots it has none for to its last one.
1301    pub const ISSUES_BY_REFERENCE: &str = concat!(
1302        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!"#,
1303        relationship_events_variable!(),
1304        r#"){"#,
1305        reference_alias!(0),
1306        reference_alias!(1),
1307        reference_alias!(2),
1308        reference_alias!(3),
1309        reference_alias!(4),
1310        reference_alias!(5),
1311        reference_alias!(6),
1312        reference_alias!(7),
1313        reference_alias!(8),
1314        reference_alias!(9),
1315        reference_alias!(10),
1316        reference_alias!(11),
1317        reference_alias!(12),
1318        reference_alias!(13),
1319        reference_alias!(14),
1320        reference_alias!(15),
1321        reference_alias!(16),
1322        reference_alias!(17),
1323        reference_alias!(18),
1324        reference_alias!(19),
1325        reference_alias!(20),
1326        reference_alias!(21),
1327        reference_alias!(22),
1328        reference_alias!(23),
1329        reference_alias!(24),
1330        reference_alias!(25),
1331        reference_alias!(26),
1332        reference_alias!(27),
1333        reference_alias!(28),
1334        reference_alias!(29),
1335        reference_alias!(30),
1336        reference_alias!(31),
1337        reference_alias!(32),
1338        reference_alias!(33),
1339        reference_alias!(34),
1340        reference_alias!(35),
1341        reference_alias!(36),
1342        reference_alias!(37),
1343        reference_alias!(38),
1344        reference_alias!(39),
1345        reference_alias!(40),
1346        reference_alias!(41),
1347        reference_alias!(42),
1348        reference_alias!(43),
1349        reference_alias!(44),
1350        reference_alias!(45),
1351        reference_alias!(46),
1352        reference_alias!(47),
1353        reference_alias!(48),
1354        reference_alias!(49),
1355        "\n    }",
1356        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}
1357      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1358        status:fieldValueByName(name:$statusField){... on ProjectV2ItemFieldSingleSelectValue{name field{
1359          ... on ProjectV2SingleSelectField{id name options{id name}}
1360        }}}
1361      }pageInfo{hasNextPage endCursor}}}"#
1362    );
1363
1364    /// The board's own id and field definitions, and not one of its items.
1365    ///
1366    /// What a write needs of the board when the item it writes does not say: the id a field
1367    /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1368    /// origin fields. It selects no `items`, so what it costs is the board's field list
1369    /// however many items the board holds — and it decides nothing about which items those
1370    /// are, which is the question a read of one item by its own id answers instead.
1371    ///
1372    /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1373    /// board's item reads by their root counts this one among them.
1374    pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1375      boardFields:repositoryOwner(login:$owner){
1376        ... on ProjectV2Owner{projectV2(number:$number){id
1377          fields(first:$nestedFirst){nodes{
1378            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1379            ... on ProjectV2Field{__typename id name dataType}
1380          }pageInfo{hasNextPage}}
1381        }}
1382      }
1383    }"#;
1384
1385    /// One board draft by its own node id, with the board item it sits in.
1386    ///
1387    /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1388    /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1389    /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1390    /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1391    /// board listing hands it to, and nothing has to list the board to find one.
1392    pub const DRAFT: &str = concat!(
1393        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1394      node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1395        projectV2Items(first:$boardItems){nodes{id project{id number}
1396        "#,
1397        board_item_values!(),
1398        r#"}pageInfo{hasNextPage endCursor}}}}
1399    }"#
1400    );
1401
1402    /// One issue's board memberships alone, walked past the page a read of it carried.
1403    ///
1404    /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1405    /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1406    /// boards than that page holds may have this board's entry past its end. This asks that
1407    /// one issue for its memberships and nothing else — the caller already holds the issue —
1408    /// so an answer of "this board does not hold it" is only ever given about a connection
1409    /// read to exhaustion.
1410    ///
1411    /// It selects the board item's id, its project number and the same
1412    /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1413    /// very same resolver: an issue recovered this way reports the same title, the same
1414    /// status, the same labels and the same qualified id as one whose entry was on the
1415    /// page.
1416    ///
1417    /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1418    /// multiplies through it and the membership connection can be walked at
1419    /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1420    /// further request for any issue a person really keeps.
1421    pub const ISSUE_BOARD_ITEMS: &str = concat!(
1422        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1423      node(id:$id){
1424        ... on Issue{projectItems(first:$first,after:$after){
1425          nodes{id updatedAt project{id number}
1426        "#,
1427        board_item_values!(),
1428        r#"}
1429          pageInfo{hasNextPage endCursor}}}
1430      }
1431    }"#
1432    );
1433    /// Whether the board's Project is public — half of what decides whether a write here can
1434    /// be read by anybody. Needs the `read:project` scope.
1435    pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1436    /// Resolves the configured repository's node id, which creating an issue requires.
1437    pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1438    /// What creating an issue needs and has not read yet: the board's own id and field
1439    /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1440    /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1441    ///
1442    /// Sent at the point a create knows which repository it is for, when neither half is
1443    /// already known to this process; a create needing only one of them sends that one's own
1444    /// document. Neither half is kept past the process: a field's option ids are re-minted by
1445    /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1446    /// status.
1447    pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1448      boardFields:repositoryOwner(login:$owner){
1449        ... on ProjectV2Owner{projectV2(number:$number){id
1450          fields(first:$nestedFirst){nodes{
1451            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1452            ... on ProjectV2Field{__typename id name dataType}
1453          }pageInfo{hasNextPage}}
1454        }}
1455      }
1456      repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1457    }"#;
1458    /// Reads both dependency directions for one issue, with each far end's own kind — and
1459    /// the issue's own body, which is where an edge to another source is recorded, so that
1460    /// half of a dependency read needs no second read of the issue or of the board.
1461    pub const ISSUE_DEPENDENCIES: &str = concat!(
1462        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1463      ... on Issue{body
1464        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1465        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1466      }}}"#,
1467        related_issue!()
1468    );
1469    /// What one item depends on: [`ISSUE_DEPENDENCIES`] narrowed to `blockedBy`, so a
1470    /// dependency read asks GitHub for the one direction it answers. The issue's `body` rides
1471    /// along because its metadata slot is where an edge to another source is recorded — and a
1472    /// board draft's `body` too, with the one board item GitHub links a draft to and, aliased
1473    /// `board`, this board's own id, so a draft's recorded edges are read off this same answer
1474    /// and only when that item is this board's: no draft read and no board read follow it.
1475    ///
1476    /// The draft's board item is asked for under a root of its own, aliased `draft`, rather
1477    /// than beside its `body` under `node`. GitHub counts the type-conditional fragments of
1478    /// one selection as the largest of them — `blockedBy`'s 100 alone, where this workspace's
1479    /// calculation adds the draft's 3 — while separate roots are summed by both, so this is
1480    /// the spelling whose node count GitHub and the calculation agree on. Both price it at
1481    /// one point; the credentialed lane's reconciliation is what holds the two to that.
1482    pub const ISSUE_DEPENDS_ON: &str = concat!(
1483        r#"query($id:ID!,$first:Int!,$after:String,$owner:String!,$number:Int!,$boardItems:Int!){
1484      node(id:$id){__typename
1485      ... on Issue{body
1486        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1487      }
1488      ... on DraftIssue{id body}}
1489      draft:node(id:$id){... on DraftIssue{projectV2Items(first:$boardItems){nodes{project{id number}}pageInfo{hasNextPage}}}}
1490      board:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id}}}}"#,
1491        related_issue!()
1492    );
1493    /// What depends on one issue: [`ISSUE_DEPENDENCIES`] with its `blocking` alone. No `body`:
1494    /// the reverse of a recorded edge is derived from the far end, never written down here.
1495    pub const ISSUE_DEPENDED_ON_BY: &str = concat!(
1496        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1497      ... on Issue{
1498        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1499      }}}"#,
1500        related_issue!()
1501    );
1502    /// Creates one issue. Outside a copy it is created on no board, and [`ADD_TO_BOARD`] files
1503    /// it; inside one, its input names the board in `projectV2Ids` and its project in
1504    /// `parentIssueId`, so this one request files it under both — see
1505    /// [`GitHubProjectsSource::create_filed_issue`](super::GitHubProjectsSource).
1506    pub const CREATE_ISSUE: &str =
1507        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1508    /// Puts an existing issue on the configured board.
1509    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1510    /// Updates an issue's visible fields and its open or closed state in one call.
1511    pub const UPDATE_ISSUE: &str =
1512        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1513    /// Updates an existing draft's user-visible fields.
1514    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1515    /// Updates a text or single-select value on one project item.
1516    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}}}}}}}}"#;
1517    /// Writes up to six board fields and clears up to three in one ordered mutation: every
1518    /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1519    /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1520    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}}}"#;
1521    /// Clears one project item's value of one field, which is what a `none` priority is.
1522    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}}}}}}}}"#;
1523    /// Creates one field: a single-select one with its options, or a text one for a projected
1524    /// metadata value. Only the guarded field setup may use this document, and only for a
1525    /// field the board lacks.
1526    pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1527    /// Replaces a single-select field's options. Only the guarded field setup — the
1528    /// `status-options` and `fields` operations — may use this document, because GitHub
1529    /// treats the input as the complete option list.
1530    pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1531    /// A fresh snapshot of the Status field and every board item's assignment.
1532    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}}}}}}"#;
1533    /// Files one issue under another as a sub-issue, which is what project membership is.
1534    pub const ADD_SUB_ISSUE: &str =
1535        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1536    /// Takes one issue back out of its parent.
1537    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1538    /// Adds GitHub's native issue blocked-by relationship.
1539    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1540    /// One origin of [`ORIGIN_LOOKUPS`]: [`ORIGIN_LOOKUP`]'s two connections for the origin the
1541    /// `$filter<n>` and `$search<n>` of slot `<n>` name, sent only while `$ask<n>` holds.
1542    macro_rules! origin_lookup_alias {
1543        ($n:literal) => {
1544            concat!(
1545                "\n      o",
1546                stringify!($n),
1547                ":repositoryOwner(login:$owner) @include(if:$ask",
1548                stringify!($n),
1549                "){... on ProjectV2Owner{projectV2(number:$number){items(first:$lookupFirst,query:$filter",
1550                stringify!($n),
1551                "){nodes{id updatedAt ",
1552                board_item_values!(),
1553                board_item_content!(),
1554                "} pageInfo{hasNextPage endCursor}}}}}\n      s",
1555                stringify!($n),
1556                ":search(query:$search",
1557                stringify!($n),
1558                ",type:$type,first:$lookupFirst) @include(if:$ask",
1559                stringify!($n),
1560                "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}"
1561            )
1562        };
1563    }
1564
1565    /// [`ORIGIN_LOOKUPS`], with `$extra_variables` declared after its own and `$extra_fields`
1566    /// selected beside them: one spelling of the batched lookup for the documents that send it.
1567    macro_rules! origin_lookups {
1568        ($extra_variables:literal, $extra_fields:literal) => {
1569            concat!(
1570                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!"#,
1571                relationship_events_variable!(),
1572                $extra_variables,
1573                r#"){
1574      boardFields:repositoryOwner(login:$owner){
1575        ... on ProjectV2Owner{projectV2(number:$number){id
1576          fields(first:$nestedFirst){nodes{
1577            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1578            ... on ProjectV2Field{__typename id name dataType}
1579          }pageInfo{hasNextPage}}
1580        }}
1581      }
1582      repository(owner:$repositoryOwner,name:$repositoryName){id databaseId nameWithOwner}"#,
1583                $extra_fields,
1584                origin_lookup_alias!(0),
1585                origin_lookup_alias!(1),
1586                origin_lookup_alias!(2),
1587                origin_lookup_alias!(3),
1588                origin_lookup_alias!(4),
1589                origin_lookup_alias!(5),
1590                origin_lookup_alias!(6),
1591                origin_lookup_alias!(7),
1592                origin_lookup_alias!(8),
1593                origin_lookup_alias!(9),
1594                "\n    }",
1595                board_issue!()
1596            )
1597        };
1598    }
1599
1600    /// The first page of [`ORIGIN_LOOKUP`] for up to [`ORIGIN_BATCH`](super::ORIGIN_BATCH)
1601    /// origins a copy is about to look for, and everything creating an item then reads —
1602    /// the board's own id and field definitions, as [`CREATION_CONTEXT`] reads them, and the node
1603    /// id and the numeric id of the repository an issue is created in — in one request.
1604    ///
1605    /// What a copy reads before it writes anything, so that finding none of its records on the
1606    /// board and creating every one costs this read and the writes. An origin whose page says
1607    /// there is more is left to its own [`ORIGIN_LOOKUP`]. The repository's numeric id is what an
1608    /// image upload names, so a copy carrying assets needs no `GET /repos/{owner}/{repo}` either.
1609    pub const ORIGIN_LOOKUPS: &str = origin_lookups!("", "");
1610
1611    /// [`ORIGIN_LOOKUPS`], with a page of the members of the project `$project` beside it, each
1612    /// trimmed to what classifying it reads — its id, its parent, its body and its repository,
1613    /// and no connection under it.
1614    ///
1615    /// What a document copy sends where the public boundary screens its write: the document is
1616    /// held to who may read its project, which is its members' classification as much as the
1617    /// project's own, and this request carries both the project — among the origins it asks —
1618    /// and its members. GitHub's own dry run prices it at the one point [`ORIGIN_LOOKUPS`]
1619    /// costs alone (`docs/measurements/board-copy-gaps-2026-10-24/coalesced-members-dry-run`).
1620    pub const ORIGIN_LOOKUPS_WITH_MEMBERS: &str = origin_lookups!(
1621        ",$project:ID!,$memberFirst:Int!",
1622        "\n      members:node(id:$project){... on Issue{subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{id body parent{id} repository{nameWithOwner}}}}}"
1623    );
1624
1625    /// One slot `<n>` of a copy's aliased writes: `$<prefix><n>` is its input and `$send<n>`
1626    /// whether it is sent.
1627    macro_rules! write_slot {
1628        ($prefix:literal, $field:literal, $selection:literal, $n:literal) => {
1629            concat!(
1630                " ",
1631                $prefix,
1632                stringify!($n),
1633                ":",
1634                $field,
1635                "(input:$",
1636                $prefix,
1637                stringify!($n),
1638                ") @include(if:$send",
1639                stringify!($n),
1640                "){",
1641                $selection,
1642                "}"
1643            )
1644        };
1645    }
1646
1647    /// The variables of a copy's aliased writes: eleven inputs of `$type` named `$<prefix><n>`,
1648    /// and eleven `$send<n>`.
1649    macro_rules! write_variables {
1650        ($prefix:literal, $type:literal) => {
1651            concat!(
1652                "mutation(",
1653                "$",
1654                $prefix,
1655                "0:",
1656                $type,
1657                "!,$",
1658                $prefix,
1659                "1:",
1660                $type,
1661                "!,$",
1662                $prefix,
1663                "2:",
1664                $type,
1665                "!,$",
1666                $prefix,
1667                "3:",
1668                $type,
1669                "!,$",
1670                $prefix,
1671                "4:",
1672                $type,
1673                "!,$",
1674                $prefix,
1675                "5:",
1676                $type,
1677                "!,$",
1678                $prefix,
1679                "6:",
1680                $type,
1681                "!,$",
1682                $prefix,
1683                "7:",
1684                $type,
1685                "!,$",
1686                $prefix,
1687                "8:",
1688                $type,
1689                "!,$",
1690                $prefix,
1691                "9:",
1692                $type,
1693                "!,$",
1694                $prefix,
1695                "10:",
1696                $type,
1697                "!,",
1698                "$send0:Boolean!,$send1:Boolean!,$send2:Boolean!,$send3:Boolean!,$send4:Boolean!,",
1699                "$send5:Boolean!,$send6:Boolean!,$send7:Boolean!,$send8:Boolean!,$send9:Boolean!,",
1700                "$send10:Boolean!){"
1701            )
1702        };
1703    }
1704
1705    /// A copy's aliased writes of one kind: [`MUTATION_BATCH`](super::MUTATION_BATCH) slots of
1706    /// `$field`, each sent only while its `$send<n>` holds.
1707    macro_rules! write_batch {
1708        ($prefix:literal, $type:literal, $field:literal, $selection:literal) => {
1709            concat!(
1710                write_variables!($prefix, $type),
1711                write_slot!($prefix, $field, $selection, 0),
1712                write_slot!($prefix, $field, $selection, 1),
1713                write_slot!($prefix, $field, $selection, 2),
1714                write_slot!($prefix, $field, $selection, 3),
1715                write_slot!($prefix, $field, $selection, 4),
1716                write_slot!($prefix, $field, $selection, 5),
1717                write_slot!($prefix, $field, $selection, 6),
1718                write_slot!($prefix, $field, $selection, 7),
1719                write_slot!($prefix, $field, $selection, 8),
1720                write_slot!($prefix, $field, $selection, 9),
1721                write_slot!($prefix, $field, $selection, 10),
1722                "}"
1723            )
1724        };
1725    }
1726
1727    /// Creates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues in one request, each filed on
1728    /// the board and under its project by the same `createIssue` — its `projectV2Ids` and
1729    /// `parentIssueId` — as [`CREATE_ISSUE`] creates one.
1730    pub const CREATE_ISSUES: &str = write_batch!(
1731        "create",
1732        "CreateIssueInput",
1733        "createIssue",
1734        "issue{id number url}"
1735    );
1736
1737    /// One issue of [`CREATED_BOARD_ITEMS`]: the board items the issue `$id<n>` names is on, with
1738    /// the `Status` value of each, read only while `$read<n>` holds.
1739    macro_rules! created_item_alias {
1740        ($n:literal) => {
1741            concat!(
1742                "\n      i",
1743                stringify!($n),
1744                ":node(id:$id",
1745                stringify!($n),
1746                ") @include(if:$read",
1747                stringify!($n),
1748                "){... on Issue{id projectItems(first:$boardItems){nodes{id project{id} status:fieldValueByName(name:$statusName){... on ProjectV2ItemFieldSingleSelectValue{name}}}}}}"
1749            )
1750        };
1751    }
1752
1753    /// The board item of each issue a copy created, up to
1754    /// [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH) at a time, with its `Status` as the board
1755    /// set it: what `createIssue`'s `projectV2Ids` files the issue as, which GitHub adds after
1756    /// answering the create, so its id comes from a read made once every create has been sent.
1757    pub const CREATED_BOARD_ITEMS: &str = concat!(
1758        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!){"#,
1759        created_item_alias!(0),
1760        created_item_alias!(1),
1761        created_item_alias!(2),
1762        created_item_alias!(3),
1763        created_item_alias!(4),
1764        created_item_alias!(5),
1765        created_item_alias!(6),
1766        created_item_alias!(7),
1767        created_item_alias!(8),
1768        created_item_alias!(9),
1769        created_item_alias!(10),
1770        created_item_alias!(11),
1771        created_item_alias!(12),
1772        created_item_alias!(13),
1773        created_item_alias!(14),
1774        created_item_alias!(15),
1775        created_item_alias!(16),
1776        created_item_alias!(17),
1777        created_item_alias!(18),
1778        created_item_alias!(19),
1779        created_item_alias!(20),
1780        "\n    }"
1781    );
1782
1783    /// The variables and the aliases of [`WRITE_BATCH`](super::WRITE_BATCH) slots: `$<prefix><n>`
1784    /// is a slot's input and `$<send><n>` whether it is sent, for the slot numbers given.
1785    macro_rules! wide_variables {
1786        ($prefix:literal, $type:literal, $send:literal; $($n:literal)+) => {
1787            concat!($("$", $prefix, stringify!($n), ":", $type, "!,$", $send, stringify!($n), ":Boolean!,",)+)
1788        };
1789    }
1790    macro_rules! wide_slots {
1791        ($prefix:literal, $field:literal, $selection:literal, $send:literal; $($n:literal)+) => {
1792            concat!($(
1793                " ", $prefix, stringify!($n), ":", $field, "(input:$", $prefix, stringify!($n),
1794                ") @include(if:$", $send, stringify!($n), "){", $selection, "}",
1795            )+)
1796        };
1797    }
1798
1799    /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) board field values a copy writes on the items it
1800    /// created, each on its own item, in one ordered request.
1801    pub const WRITE_FIELD_VALUES: &str = concat!(
1802        "mutation(",
1803        wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1804        "){",
1805        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),
1806        "}"
1807    );
1808
1809    /// A copy's board field values and its blocked-by edges together, up to
1810    /// [`WRITE_BATCH`](super::WRITE_BATCH) of the two kinds between them, in one ordered request:
1811    /// every field value its `$sendField<n>` sends, then every edge its `$sendBlock<n>` sends.
1812    /// What a copy sends when one request of its writes holds both kinds; one holding a single
1813    /// kind is [`WRITE_FIELD_VALUES`] or [`ADD_BLOCKED_BYS`].
1814    pub const WRITE_FIELDS_AND_EDGES: &str = concat!(
1815        "mutation(",
1816        wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1817        wide_variables!("block", "AddBlockedByInput", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1818        "){",
1819        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),
1820        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),
1821        "}"
1822    );
1823
1824    /// Closes up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues a copy created at a closing
1825    /// status, each with its own `stateInput` and nothing else, once its option is selected.
1826    pub const CLOSE_ISSUES: &str =
1827        write_batch!("close", "UpdateIssueInput", "updateIssue", "issue{id}");
1828
1829    /// Updates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) existing issues a copy rewrites,
1830    /// each with the members its own [`UPDATE_ISSUE`] would have sent, in one request.
1831    pub const UPDATE_ISSUES: &str =
1832        write_batch!("update", "UpdateIssueInput", "updateIssue", "issue{id}");
1833
1834    /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) native blocked-by relationships a copy adds to the
1835    /// issues it created, in one request.
1836    pub const ADD_BLOCKED_BYS: &str = concat!(
1837        "mutation(",
1838        wide_variables!("block", "AddBlockedByInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1839        "){",
1840        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),
1841        "}"
1842    );
1843
1844    /// Removes one native issue blocked-by relationship.
1845    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1846    /// Deletes one issue, which takes its board item with it.
1847    ///
1848    /// The engine sends this in one situation only: undoing a copy that could not finish,
1849    /// over the items that same copy created. Deleting the issue removes the board item
1850    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1851    pub const DELETE_ISSUE: &str =
1852        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1853    /// Deletes up to [`DELETE_BATCH`](super::DELETE_BATCH) issues in one request, each as
1854    /// [`DELETE_ISSUE`] deletes one: what a write that created several issues and was refused
1855    /// part way sends to take them back.
1856    pub const DELETE_ISSUES: &str = concat!(
1857        "mutation(",
1858        wide_variables!("delete", "DeleteIssueInput", "send"; 0 1 2 3 4),
1859        "){",
1860        wide_slots!("delete", "deleteIssue", "repository{id}", "send"; 0 1 2 3 4),
1861        "}"
1862    );
1863
1864    /// One task's comments: a page of its issue's own `comments` connection.
1865    ///
1866    /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1867    /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1868    /// list every time somebody edited it; left unordered the connection answers in the order
1869    /// the comments were written, which is the order GitHub documents for the same collection
1870    /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1871    /// node count and the caller's own page size is pushed straight down.
1872    pub const ISSUE_COMMENTS: &str = concat!(
1873        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1874        issue_comment!(),
1875        r#"}pageInfo{hasNextPage endCursor}}}}}"#
1876    );
1877    /// One issue by its own node id, with a page of its comments: what `task show` and a
1878    /// comment listing read, in one request.
1879    ///
1880    /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1881    /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1882    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1883    /// connection there would multiply through both of those documents' price, and neither
1884    /// needs one.
1885    pub const ISSUE_DETAIL: &str = concat!(
1886        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1887        relationship_events_variable!(),
1888        r#"){
1889      node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1890        issue_comment!(),
1891        r#"}pageInfo{hasNextPage endCursor}}}}
1892    }"#,
1893        board_issue!()
1894    );
1895
1896    /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1897    /// page of its comments when `$comments` asks for them.
1898    macro_rules! issue_details_alias {
1899        ($n:literal) => {
1900            concat!(
1901                "\n      i",
1902                stringify!($n),
1903                ":node(id:$id",
1904                stringify!($n),
1905                "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1906                issue_comment!(),
1907                "}pageInfo{hasNextPage endCursor}}}}"
1908            )
1909        };
1910    }
1911
1912    /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1913    /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1914    ///
1915    /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1916    /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1917    /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1918    /// neither — so every connection under it would be priced at nothing and the pin in
1919    /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1920    /// one-item read the model already prices, so the batch costs what its aliases cost.
1921    ///
1922    /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1923    /// slots it has no item for to the last item it does, and reads that item again; the
1924    /// price is the document's, whatever its variables, so a short batch costs what a full
1925    /// one does and nothing more.
1926    pub const ISSUE_DETAILS: &str = concat!(
1927        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!"#,
1928        relationship_events_variable!(),
1929        r#"){"#,
1930        issue_details_alias!(0),
1931        issue_details_alias!(1),
1932        issue_details_alias!(2),
1933        issue_details_alias!(3),
1934        issue_details_alias!(4),
1935        issue_details_alias!(5),
1936        issue_details_alias!(6),
1937        issue_details_alias!(7),
1938        issue_details_alias!(8),
1939        issue_details_alias!(9),
1940        issue_details_alias!(10),
1941        issue_details_alias!(11),
1942        issue_details_alias!(12),
1943        issue_details_alias!(13),
1944        issue_details_alias!(14),
1945        issue_details_alias!(15),
1946        issue_details_alias!(16),
1947        issue_details_alias!(17),
1948        issue_details_alias!(18),
1949        issue_details_alias!(19),
1950        issue_details_alias!(20),
1951        issue_details_alias!(21),
1952        issue_details_alias!(22),
1953        issue_details_alias!(23),
1954        "\n    }",
1955        board_issue!()
1956    );
1957
1958    /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1959    /// page of its comments and the issues it is blocked by.
1960    macro_rules! issue_copy_details_alias {
1961        ($n:literal) => {
1962            concat!(
1963                "\n      i",
1964                stringify!($n),
1965                ":node(id:$id",
1966                stringify!($n),
1967                "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1968                issue_comment!(),
1969                "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1970            )
1971        };
1972    }
1973
1974    /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1975    /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1976    /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1977    /// at a time, so a copy of a changed task costs this read and nothing else.
1978    ///
1979    /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1980    /// what `task show-many` is priced by and which reads no edge.
1981    ///
1982    /// `children`, read only while `$children` holds, is which issues the issue `$project` has
1983    /// as sub-issues — each one's id and title, the page `$nestedFirst` allows — so that a
1984    /// project copy, which reads its project and every task at once, knows from this same read
1985    /// which tasks the project holds at the destination, and its orphan check is answered by it
1986    /// rather than by a listing of the project.
1987    pub const ISSUE_COPY_DETAILS: &str = concat!(
1988        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!"#,
1989        relationship_events_variable!(),
1990        r#"){"#,
1991        issue_copy_details_alias!(0),
1992        issue_copy_details_alias!(1),
1993        issue_copy_details_alias!(2),
1994        issue_copy_details_alias!(3),
1995        issue_copy_details_alias!(4),
1996        issue_copy_details_alias!(5),
1997        issue_copy_details_alias!(6),
1998        issue_copy_details_alias!(7),
1999        issue_copy_details_alias!(8),
2000        issue_copy_details_alias!(9),
2001        issue_copy_details_alias!(10),
2002        issue_copy_details_alias!(11),
2003        issue_copy_details_alias!(12),
2004        issue_copy_details_alias!(13),
2005        issue_copy_details_alias!(14),
2006        issue_copy_details_alias!(15),
2007        issue_copy_details_alias!(16),
2008        issue_copy_details_alias!(17),
2009        issue_copy_details_alias!(18),
2010        issue_copy_details_alias!(19),
2011        issue_copy_details_alias!(20),
2012        "\n      children:node(id:$project) @include(if:$children){... on Issue{id subIssues(first:$nestedFirst){nodes{id title} pageInfo{hasNextPage}}}}",
2013        "\n    }",
2014        board_issue!(),
2015        related_issue!()
2016    );
2017
2018    /// Which issue one comment is on, read before that comment is edited or removed.
2019    ///
2020    /// GitHub's comment mutations take the comment's id and nothing else, so without this a
2021    /// comment id given against the wrong task would change a comment on another issue.
2022    pub const COMMENT_ISSUE: &str =
2023        r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
2024    /// Adds one comment to an issue, signed as the account the token belongs to.
2025    pub const ADD_COMMENT: &str = concat!(
2026        r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
2027        issue_comment!(),
2028        r#"}}}}"#
2029    );
2030    /// Replaces the body of one issue comment.
2031    pub const UPDATE_COMMENT: &str = concat!(
2032        r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
2033        issue_comment!(),
2034        r#"}}}"#
2035    );
2036    /// Removes one issue comment. Its payload carries nothing about the comment it removed.
2037    pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
2038
2039    /// What a mutation of a write the store's cache expects selects of the issue it wrote:
2040    /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
2041    /// change token's every part and the first page of its comments.
2042    ///
2043    /// Sent **only** while that write runs, in place of the document named beside each
2044    /// below, so every other write's documents, prices and request counts are what they
2045    /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
2046    /// mutation's response is stamped seconds after the change it made, so against GitHub
2047    /// itself a response rarely determines a version; the cache is then left unserveable
2048    /// for the task, and its next read refreshes it.
2049    macro_rules! whole_issue {
2050        () => {
2051            concat!(
2052                r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
2053                issue_comment!(),
2054                r#"}pageInfo{hasNextPage endCursor}}}"#
2055            )
2056        };
2057    }
2058
2059    /// The variables every document of `whole_issue!` declares beside its own.
2060    macro_rules! whole_variables {
2061        () => {
2062            concat!(
2063                r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2064                relationship_events_variable!()
2065            )
2066        };
2067    }
2068
2069    /// [`ADD_COMMENT`], selecting the whole issue commented on.
2070    pub const ADD_COMMENT_WHOLE: &str = concat!(
2071        r#"mutation($input:AddCommentInput!"#,
2072        whole_variables!(),
2073        r#"){addComment(input:$input){subject{id "#,
2074        whole_issue!(),
2075        r#"} commentEdge{node{"#,
2076        issue_comment!(),
2077        r#"}}}}"#,
2078        board_issue!()
2079    );
2080    /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
2081    pub const UPDATE_COMMENT_WHOLE: &str = concat!(
2082        r#"mutation($input:UpdateIssueCommentInput!"#,
2083        whole_variables!(),
2084        r#"){updateIssueComment(input:$input){issueComment{"#,
2085        issue_comment!(),
2086        r#" issue{"#,
2087        whole_issue!(),
2088        r#"}}}}"#,
2089        board_issue!()
2090    );
2091    /// [`UPDATE_ISSUE`], selecting the whole issue updated.
2092    pub const UPDATE_ISSUE_WHOLE: &str = concat!(
2093        r#"mutation($input:UpdateIssueInput!"#,
2094        whole_variables!(),
2095        r#"){updateIssue(input:$input){issue{id "#,
2096        whole_issue!(),
2097        r#"}}}"#,
2098        board_issue!()
2099    );
2100    /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
2101    pub const UPDATE_FIELD_WHOLE: &str = concat!(
2102        r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2103        whole_variables!(),
2104        r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2105        whole_issue!(),
2106        r#"}}}}"#,
2107        board_issue!()
2108    );
2109    /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
2110    pub const CLEAR_FIELD_WHOLE: &str = concat!(
2111        r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2112        whole_variables!(),
2113        r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2114        whole_issue!(),
2115        r#"}}}}"#,
2116        board_issue!()
2117    );
2118
2119    /// The document sent in `document`'s place while a write the store's cache expects runs,
2120    /// when it has one.
2121    #[must_use]
2122    pub fn whole_twin(document: &str) -> Option<&'static str> {
2123        [
2124            (ADD_COMMENT, ADD_COMMENT_WHOLE),
2125            (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
2126            (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
2127            (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
2128            (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
2129        ]
2130        .into_iter()
2131        .find(|(plain, _)| *plain == document)
2132        .map(|(_, whole)| whole)
2133    }
2134
2135    /// Every document above, with what this source is doing when it sends one.
2136    ///
2137    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
2138    /// name the call that was refused, and a `match` with a catch-all arm would answer a
2139    /// document added later with "talking to GitHub" and never say so.
2140    ///
2141    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
2142    /// const` here that this list omits, so the two cannot part — which is the same guard
2143    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
2144    pub const DOCUMENTS: [(&str, &str); 57] = [
2145        (
2146            ISSUES_BY_CREATION,
2147            "listing a repository's issues newest first for a create key",
2148        ),
2149        (SEARCH_ISSUES, "searching this board's issues"),
2150        (ISSUE, "reading one issue"),
2151        (
2152            DOCUMENT_IN_PROJECT,
2153            "reading one document with its project and the project's members",
2154        ),
2155        (
2156            ISSUE_BOARD_ITEMS,
2157            "reading one issue's board memberships past the page it came with",
2158        ),
2159        (SUB_ISSUES, "reading a project's tasks"),
2160        (BOARD, "reading the board"),
2161        (
2162            BOARD_WITH_COMMENTS,
2163            "reading a board page with first comments",
2164        ),
2165        (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
2166        (CLOSED_ITEMS, "reading the board's closed items"),
2167        (
2168            ISSUES_BY_REFERENCE,
2169            "reading a batch of issues by their references",
2170        ),
2171        (BOARD_FIELDS, "reading the board's fields"),
2172        (DRAFT, "reading one draft"),
2173        (REPOSITORY, "reading the destination repository"),
2174        (
2175            PROJECT_VISIBILITY,
2176            "reading whether the board's project is public",
2177        ),
2178        (
2179            CREATION_CONTEXT,
2180            "reading the board's fields and the destination repository",
2181        ),
2182        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
2183        (ISSUE_DEPENDS_ON, "reading what an item depends on"),
2184        (ISSUE_DEPENDED_ON_BY, "reading what depends on an issue"),
2185        (CREATE_ISSUE, "creating an issue"),
2186        (
2187            ORIGIN_LOOKUPS,
2188            "looking up the items copied from several origins, and what creating one reads",
2189        ),
2190        (
2191            ORIGIN_LOOKUPS_WITH_MEMBERS,
2192            "looking up the items copied from several origins, with a project's members",
2193        ),
2194        (CREATE_ISSUES, "creating several issues together"),
2195        (
2196            CREATED_BOARD_ITEMS,
2197            "reading the board items of the issues a copy created",
2198        ),
2199        (
2200            WRITE_FIELD_VALUES,
2201            "writing board fields of the items a copy created",
2202        ),
2203        (
2204            WRITE_FIELDS_AND_EDGES,
2205            "writing board fields and dependencies of the items a copy created",
2206        ),
2207        (CLOSE_ISSUES, "closing the issues a copy created"),
2208        (UPDATE_ISSUES, "updating several issues together"),
2209        (ADD_BLOCKED_BYS, "recording several dependencies together"),
2210        (ADD_TO_BOARD, "adding an issue to the board"),
2211        (UPDATE_ISSUE, "updating an issue"),
2212        (UPDATE_DRAFT, "updating a draft item"),
2213        (UPDATE_FIELD, "writing a board field"),
2214        (UPDATE_FIELDS, "writing board fields together"),
2215        (CLEAR_FIELD, "clearing a board field"),
2216        (
2217            CREATE_FIELD,
2218            "creating a board single-select field with its options",
2219        ),
2220        (
2221            STATUS_OPTIONS_SNAPSHOT,
2222            "snapshotting board Status options and assignments",
2223        ),
2224        (
2225            STATUS_OPTIONS_UPDATE,
2226            "safely replacing the board Status option list",
2227        ),
2228        (ADD_SUB_ISSUE, "filing an issue under its project"),
2229        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
2230        (ADD_BLOCKED_BY, "recording a dependency"),
2231        (REMOVE_BLOCKED_BY, "removing a dependency"),
2232        (DELETE_ISSUE, "deleting an issue"),
2233        (DELETE_ISSUES, "deleting several issues together"),
2234        (ISSUE_COMMENTS, "reading a task's comments"),
2235        (ISSUE_DETAIL, "reading one issue with its comments"),
2236        (
2237            ISSUE_DETAILS,
2238            "reading a batch of issues with their comments",
2239        ),
2240        (
2241            ISSUE_COPY_DETAILS,
2242            "reading a batch of issues with their comments and dependencies for a copy",
2243        ),
2244        (COMMENT_ISSUE, "reading which issue a comment is on"),
2245        (ADD_COMMENT, "adding a comment"),
2246        (UPDATE_COMMENT, "editing a comment"),
2247        (DELETE_COMMENT, "deleting a comment"),
2248        (
2249            ADD_COMMENT_WHOLE,
2250            "adding a comment, answered with the whole issue",
2251        ),
2252        (
2253            UPDATE_COMMENT_WHOLE,
2254            "editing a comment, answered with the whole issue",
2255        ),
2256        (
2257            UPDATE_ISSUE_WHOLE,
2258            "updating an issue, answered with it whole",
2259        ),
2260        (
2261            UPDATE_FIELD_WHOLE,
2262            "writing a board field, answered with the whole issue",
2263        ),
2264        (
2265            CLEAR_FIELD_WHOLE,
2266            "clearing a board field, answered with the whole issue",
2267        ),
2268    ];
2269}
2270
2271/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
2272/// under, the variable carrying its input, and the boolean variable that includes it.
2273#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2274pub struct FieldSlot {
2275    /// The key of the answer's `data` this field's payload is under.
2276    pub alias: &'static str,
2277    /// The variable carrying this field's input.
2278    pub variable: &'static str,
2279    /// The boolean variable whose `true` sends this field.
2280    pub include: &'static str,
2281}
2282
2283/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
2284pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
2285    FieldSlot {
2286        alias: "updateProjectV2ItemFieldValue",
2287        variable: "input",
2288        include: "writeFirst",
2289    },
2290    FieldSlot {
2291        alias: "second",
2292        variable: "second",
2293        include: "writeSecond",
2294    },
2295    FieldSlot {
2296        alias: "third",
2297        variable: "third",
2298        include: "writeThird",
2299    },
2300    FieldSlot {
2301        alias: "fourth",
2302        variable: "fourth",
2303        include: "writeFourth",
2304    },
2305    FieldSlot {
2306        alias: "fifth",
2307        variable: "fifth",
2308        include: "writeFifth",
2309    },
2310    FieldSlot {
2311        alias: "sixth",
2312        variable: "sixth",
2313        include: "writeSixth",
2314    },
2315];
2316
2317/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
2318pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
2319    FieldSlot {
2320        alias: "cleared",
2321        variable: "clear",
2322        include: "writeClear",
2323    },
2324    FieldSlot {
2325        alias: "clearedSecond",
2326        variable: "clearSecond",
2327        include: "writeClearSecond",
2328    },
2329    FieldSlot {
2330        alias: "clearedThird",
2331        variable: "clearThird",
2332        include: "writeClearThird",
2333    },
2334];
2335
2336/// Which of GitHub's two rate limiters refused a request.
2337///
2338/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
2339/// secondary one — so an operator told the wrong one takes the wrong next step, which is
2340/// the whole reason this is carried rather than collapsed into "rate limited".
2341#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2342enum Limiter {
2343    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
2344    Primary,
2345    /// The burst limiter over content-generating requests, which nothing reports.
2346    Secondary,
2347}
2348
2349/// The wordings GitHub answers a secondary rate limit with.
2350///
2351/// It sends them under a forbidden status, under a too-many-requests status, and inside
2352/// the `errors` of a *successful* response, which is why the text is what this matches on
2353/// rather than the status. `abuse detection` is the wording GitHub used before the
2354/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
2355/// what a burst of content creation is refused with.
2356///
2357/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
2358/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
2359/// when, and the drift gate reconciles the two lists both ways. Public for that gate
2360/// alone — a caller has no use for it, and matching on a refusal is this source's job.
2361pub const SECONDARY_WORDINGS: [&str; 5] = [
2362    "secondary rate limit",
2363    "temporarily blocked from content creation",
2364    "abuse detection",
2365    "submitted too quickly",
2366    "exceeded a secondary",
2367];
2368
2369/// The wordings GitHub answers an exhausted primary budget with.
2370///
2371/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
2372/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
2373/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
2374/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
2375/// two phrases is a substring of it, so without it that answer read as a refusal that will
2376/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
2377/// one reason.
2378pub const PRIMARY_WORDINGS: [&str; 4] = [
2379    "api rate limit exceeded",
2380    "api rate limit already exceeded",
2381    "rate limit exceeded",
2382    "rate_limited",
2383];
2384
2385/// The wordings GitHub answers a write that collided with another change to the same
2386/// ordering with, in the `errors` of an HTTP 200: "Your attempt to move this item created a
2387/// temporary conflict. Please try again." The write did not take effect and GitHub asks for it
2388/// again, so it is retried on the same growing schedule a rate limit without a hint is, within
2389/// the same retry budget — except a `createIssue`, whose answer cannot say the issue was not
2390/// made, and which is therefore reported rather than replayed into a duplicate.
2391///
2392/// Observed rather than documented, so it is pinned exactly as [`SECONDARY_WORDINGS`] is:
2393/// `tests/fixtures/rate-limits.json` records where it was read under `conflict.observed`, and
2394/// the same drift gate reconciles the two both ways. Public for that gate alone.
2395pub const CONFLICT_WORDINGS: [&str; 1] = ["created a temporary conflict"];
2396
2397/// What a response *says about itself*, which is the only place a refusal can be read.
2398///
2399/// Deliberately not the whole response body. A board is a place people write about their
2400/// own work, and a task on it titled "the secondary rate limit" would, matched across the
2401/// raw text, turn a perfectly good answer into a refusal this source then waited out and
2402/// reported. So the item data is never read: what is read is GitHub's own REST-style
2403/// `message` envelope, which is what a forbidden status carries, and the `message` and
2404/// `type` of each GraphQL error, which is where a *successful* response says it.
2405///
2406/// A body that is not JSON at all has nothing structured to read, so only a failing
2407/// response's own text is taken — a successful response that is not JSON is malformed
2408/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
2409fn refusal_wording(status: StatusCode, body: &str) -> String {
2410    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
2411        return if status.is_success() {
2412            String::new()
2413        } else {
2414            body.to_owned()
2415        };
2416    };
2417    let mut said: Vec<&str> = parsed
2418        .get("message")
2419        .and_then(Value::as_str)
2420        .into_iter()
2421        .collect();
2422    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
2423        for error in errors {
2424            said.extend(
2425                ["message", "type"]
2426                    .into_iter()
2427                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
2428            );
2429        }
2430    }
2431    said.join("; ")
2432}
2433
2434impl Limiter {
2435    /// Which limiter refused this response, or `None` when none of them did.
2436    ///
2437    /// The wording is read first and the status only decides what carries none of it,
2438    /// because GitHub answers a secondary limit with a forbidden status far more often
2439    /// than with too-many-requests — while a forbidden status saying nothing about a limit
2440    /// really is a credential this token lacks.
2441    ///
2442    /// A response is a refusal because of its status or its own wording. A spent budget
2443    /// only ever explains one; it never turns an answer into a refusal.
2444    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
2445        let normalized = refusal_wording(status, body).to_ascii_lowercase();
2446        if SECONDARY_WORDINGS
2447            .iter()
2448            .any(|wording| normalized.contains(wording))
2449        {
2450            return Some(Self::Secondary);
2451        }
2452        if status == StatusCode::TOO_MANY_REQUESTS {
2453            return Some(Self::Primary);
2454        }
2455        // An exhausted budget *explains* a response that failed; it does not make one that
2456        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
2457        // request the budget allowed as well as on the ones it then refuses, so reading
2458        // the header alone threw away a good answer — and, once refusals were retried,
2459        // replayed a request that had already taken effect.
2460        if !status.is_success() && budget_exhausted {
2461            return Some(Self::Primary);
2462        }
2463        // A successful response saying it: GitHub reports a GraphQL rate limit in the
2464        // `errors` of an HTTP 200, where nothing about the status says so at all.
2465        if status.is_success()
2466            && PRIMARY_WORDINGS
2467                .iter()
2468                .any(|wording| normalized.contains(wording))
2469        {
2470            return Some(Self::Primary);
2471        }
2472        None
2473    }
2474
2475    /// What this limiter is called where an operator can look it up.
2476    const fn name(self) -> &'static str {
2477        match self {
2478            Self::Primary => "GitHub's primary API rate limit",
2479            Self::Secondary => "GitHub's secondary rate limit",
2480        }
2481    }
2482
2483    /// What the endpoint an operator would go and check says about this limiter.
2484    const fn where_to_look(self) -> &'static str {
2485        match self {
2486            Self::Primary => {
2487                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
2488                 comes back."
2489            }
2490            Self::Secondary => {
2491                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
2492                 primary budget and does not report this one, so budget showing there says \
2493                 nothing about this refusal, and every further attempt extends it."
2494            }
2495        }
2496    }
2497
2498    /// The next step this limiter actually calls for.
2499    const fn what_to_do(self) -> &'static str {
2500        match self {
2501            Self::Primary => {
2502                "wait for the reset `gh api rate_limit` reports, then run the command again."
2503            }
2504            Self::Secondary => {
2505                "leave this board alone for a few minutes, then run the command again — or \
2506                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
2507            }
2508        }
2509    }
2510}
2511
2512/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
2513#[derive(Debug, Clone, Copy)]
2514struct Limited {
2515    limiter: Limiter,
2516    hint: Option<u64>,
2517    /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
2518    /// `x-ratelimit-reset`.
2519    hinted_by: Hint,
2520}
2521
2522/// Where a refusal's hint came from, which is what its wait is named after.
2523#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2524enum Hint {
2525    /// `retry-after`, or no hint at all.
2526    RetryAfter,
2527    /// An exhausted primary budget's `x-ratelimit-reset`.
2528    PrimaryReset,
2529}
2530
2531impl Limited {
2532    /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
2533    /// and the cause it is named by: the hint, raised to the schedule's own next wait —
2534    /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
2535    /// extends a secondary limit — or the schedule alone when there is none.
2536    fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
2537        match (self.hint, self.hinted_by) {
2538            (Some(hint), Hint::RetryAfter) => (
2539                Duration::from_secs(hint).max(backoff),
2540                pacing::cause::RETRY_AFTER,
2541            ),
2542            (Some(hint), Hint::PrimaryReset) => (
2543                Duration::from_secs(hint).max(backoff),
2544                pacing::cause::PRIMARY_RESET,
2545            ),
2546            (None, _) => (backoff, pacing::cause::BACKOFF),
2547        }
2548    }
2549
2550    /// What the caller is told once this source has waited as long as it may.
2551    ///
2552    /// Both limiters report as [`SourceError::RateLimited`], because that is what
2553    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2554    /// about *which* limiter it was makes it a different kind of failure. What differs is
2555    /// the operator's next step, and that is what the message carries — a secondary
2556    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2557    /// budget looks fine, and then back to retry the very burst that was refused.
2558    fn exhausted(
2559        self,
2560        doing: &str,
2561        waits: u32,
2562        waited: Duration,
2563        needed: Duration,
2564        budget: Duration,
2565    ) -> SourceError {
2566        SourceError::RateLimited {
2567            retry_after_seconds: self.hint,
2568            message: Some(format!(
2569                "{} refused this source while {doing}; it waited {} out over {} and was refused \
2570                 again, and the next wait of {} would take it past the {} one call may spend \
2571                 waiting. {} next: {}",
2572                self.limiter.name(),
2573                plural(waits, "refusal"),
2574                seconds(waited),
2575                seconds(needed),
2576                seconds(budget),
2577                self.limiter.where_to_look(),
2578                self.limiter.what_to_do(),
2579            )),
2580        }
2581    }
2582}
2583
2584/// One HTTP attempt's result, with what its response said about the rate limit.
2585///
2586/// The two travel together so the record and the outcome are written from the same place:
2587/// what a response said about the budget is only readable while that response is in hand,
2588/// and what the attempt *meant* is only decidable once its body has been read.
2589struct Attempted {
2590    result: Result<Value, Attempt>,
2591    limits: accounting::RateLimit,
2592    /// GitHub's own reported cost for this call, for a document that asked for it.
2593    reported_cost: Option<u64>,
2594    response_outcome: accounting::ResponseOutcome,
2595}
2596
2597/// One attempt's outcome: an error to report, or a rate limit to wait out.
2598enum Attempt {
2599    Failed(SourceError),
2600    Limited(Limited),
2601    /// GitHub refused a write as a temporary conflict and asked for it again — see
2602    /// [`CONFLICT_WORDINGS`]. Carries what GitHub said, for the diagnostic once retrying ends.
2603    Conflicted(String),
2604}
2605
2606fn plural(count: u32, thing: &str) -> String {
2607    if count == 1 {
2608        format!("{count} {thing}")
2609    } else {
2610        format!("{count} {thing}s")
2611    }
2612}
2613
2614fn seconds(duration: Duration) -> String {
2615    format!("{:.1}s", duration.as_secs_f64())
2616}
2617
2618/// The member [`stamp_served`] writes onto every node of a response that says when it last
2619/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2620const SERVED_AT: &str = "onetaskgraphServedAt";
2621
2622/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2623/// node's time later knows the server's clock on the response that read it — which is what
2624/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2625fn stamp_served(data: &mut Value, served: &str) {
2626    match data {
2627        Value::Object(members) => {
2628            if members.contains_key("updatedAt") {
2629                members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2630            }
2631            for member in members.values_mut() {
2632                stamp_served(member, served);
2633            }
2634        }
2635        Value::Array(items) => {
2636            for item in items {
2637                stamp_served(item, served);
2638            }
2639        }
2640        _ => {}
2641    }
2642}
2643
2644/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2645///
2646/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2647/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2648/// header, and neither is what makes a response a refusal — so the whole cost of one this
2649/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2650/// instead. Refusing the response over the header would turn a readable refusal into an
2651/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2652fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2653    value
2654        .and_then(|value| value.to_str().ok())
2655        .and_then(|value| value.trim().parse::<u64>().ok())
2656}
2657
2658/// A transport failure with every cause beneath it, outermost first.
2659///
2660/// The client's own text for a request it could not complete is `error sending request for
2661/// url (…)`, and the socket error that says why — a refused or reset connection, an exhausted
2662/// port range, a connection closed before its answer — is only in the chain of sources below
2663/// it. Those differ in what to do next, so all of them are kept.
2664pub(crate) fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
2665    let mut text = error.to_string();
2666    let mut cause = error.source();
2667    while let Some(next) = cause {
2668        let said = next.to_string();
2669        if !text.ends_with(&said) {
2670            text = format!("{text}: {said}");
2671        }
2672        cause = next.source();
2673    }
2674    text
2675}
2676
2677/// Every mutation this source sends creates content — an issue, a board item, a field of
2678/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2679/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2680/// and what the keyword says are the same set. That is what makes the keyword a sound test
2681/// rather than a convenient one: counting an edit or a removal the limiter might not have
2682/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
2683pub(crate) fn is_mutation(query: &str) -> bool {
2684    query.trim_start().starts_with("mutation")
2685}
2686
2687/// What this source was doing, for a diagnostic that has to say so.
2688///
2689/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2690/// a document added without a description is caught by that list's own gate instead of
2691/// falling through to the vague arm below.
2692fn operation_description(query: &str) -> &'static str {
2693    graphql::DOCUMENTS
2694        .iter()
2695        .find(|(document, _)| *document == query)
2696        .map_or("talking to GitHub", |(_, doing)| *doing)
2697}
2698
2699/// GitHub's published ceiling on content-generating requests in any one minute.
2700///
2701/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2702/// GitHub's number rather than this source's. This source paces at it over its window: a
2703/// content-creating request goes out at once while the minute's creations leave room for it,
2704/// and waits until they do otherwise — see the `pacing` module.
2705pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2706/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
2707/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
2708pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2709/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
2710/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
2711/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
2712pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
2713/// The secondary points one GraphQL request carrying a mutation is charged.
2714pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
2715/// The secondary points one GraphQL request carrying no mutation is charged.
2716pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
2717/// The fastest *steady* spacing of content-creating mutations that cannot exceed
2718/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
2719///
2720/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
2721/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
2722/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
2723/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
2724/// value that holds a long sustained burst under the per-minute bound by spacing alone.
2725pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2726/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2727///
2728/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2729/// own advice for a secondary limit — wait, and wait longer each time — without spending
2730/// the first minute of a transient refusal doing nothing.
2731pub const RETRY_BACKOFF_MS: u64 = 1_000;
2732/// Total time one call may spend waiting out rate limits before it reports a failure.
2733///
2734/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2735/// short enough that a command an operator is watching returns. The bound is what makes
2736/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2737/// the limiter, not in a process nobody can tell from a wedged one.
2738pub const RETRY_BUDGET_MS: u64 = 120_000;
2739
2740fn default_token_env() -> String {
2741    "GH_PROJECTS_TOKEN".to_owned()
2742}
2743fn default_endpoint() -> String {
2744    "https://api.github.com/graphql".to_owned()
2745}
2746
2747/// The name of a `Status` single-select option on the board.
2748///
2749/// Validated on the way in rather than checked later, so a blank option name — which
2750/// nothing on a board can be — is a state this type cannot hold.
2751#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2752#[serde(try_from = "String")]
2753#[schemars(extend("minLength" = 1))]
2754pub struct ColumnName(String);
2755
2756impl ColumnName {
2757    /// The option name, as the board spells it.
2758    fn as_str(&self) -> &str {
2759        &self.0
2760    }
2761}
2762
2763impl TryFrom<String> for ColumnName {
2764    type Error = String;
2765
2766    fn try_from(name: String) -> Result<Self, Self::Error> {
2767        if name.trim().is_empty() {
2768            return Err("a status_mapping option name cannot be blank".to_owned());
2769        }
2770        Ok(Self(name))
2771    }
2772}
2773
2774/// The two closed states this product can mean.
2775///
2776/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2777/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2778/// product never decides on a person's behalf that two issues are the same work.
2779#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2780#[serde(rename_all = "kebab-case")]
2781pub enum ClosedState {
2782    /// `COMPLETED` — precisely done.
2783    Completed,
2784    /// `NOT_PLANNED` — precisely cancelled.
2785    NotPlanned,
2786}
2787
2788impl ClosedState {
2789    const fn reason(self) -> &'static str {
2790        match self {
2791            Self::Completed => "COMPLETED",
2792            Self::NotPlanned => "NOT_PLANNED",
2793        }
2794    }
2795}
2796
2797/// Configuration for one GitHub Projects v2 board.
2798///
2799/// It serializes to a document this same type reads back as itself, so a caller writing a
2800/// configuration — a consumer filing onto a board it configures — can build one here rather
2801/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2802#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2803#[serde(default, deny_unknown_fields)]
2804pub struct GitHubProjectsConfig {
2805    /// Login of the user or organization which owns the board.
2806    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2807    /// The project number shown in the board's GitHub URL.
2808    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2809    // 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.
2810    /// `owner/name` of the repository this source creates an issue in when the item's own
2811    /// `repositories` field does not decide it.
2812    ///
2813    /// An item naming exactly one repository is created there — on a source declared
2814    /// `visibility: private`, only when that repository reads private, and otherwise as an
2815    /// item naming none is; a task or a document naming none or several is created in its
2816    /// parent project's repository; and a project, or a task or document with no parent,
2817    /// naming none or several is created here. A board
2818    /// has no repository of its own and `createIssue` requires one, so a write without
2819    /// this is refused naming the field. Reads never need it.
2820    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2821    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2822    /// GitHub logins whose repositories this source may create an issue in.
2823    ///
2824    /// Absent or empty, the default, restricts nothing. Otherwise a create whose issue the
2825    /// rule `repository` describes would file in a repository owned by anyone else is refused
2826    /// before anything of it is written — by `task copy`, `task copy --create`, a member
2827    /// create of a project copy and a dry run of each — with the error kind `creation-owner`,
2828    /// naming the repository and these owners. Logins compare case-insensitively, as GitHub
2829    /// compares them. An update of an issue that already exists is never refused by it.
2830    /// `task locate` reports such a refusal as `creates_in.refused` rather than failing.
2831    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2832    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2833    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2834    #[schemars(!skip_serializing_if)]
2835    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.
2836    /// Environment variable containing a fine-grained token with Projects and Issues
2837    /// read/write plus Pull requests read-only access for every repository represented on
2838    /// the board.
2839    #[serde(default = "default_token_env")]
2840    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2841    /// GraphQL endpoint. GitHub Enterprise installations may override it.
2842    #[serde(default = "default_endpoint")]
2843    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2844    /// Per-instance mapping from a status category to the option of the board's one
2845    /// `Status` field it lands on, for a task and for a project.
2846    ///
2847    /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2848    /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2849    /// where a kind left out leaves the category unmapped for that kind. A category this
2850    /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2851    /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2852    /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2853    /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2854    /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2855    /// either kind. No two categories may name one option for the same kind, ignoring case.
2856    /// `unknown` may name one existing option; every unknown word then lands on it and
2857    /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2858    /// each unknown word because it never creates board options.
2859    #[serde(default)]
2860    pub status_mapping: StatusMapping,
2861    /// Per-instance mapping from a task's priority to an option of this board's
2862    /// single-select field named `Priority`.
2863    ///
2864    /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2865    /// other priority is refused before it reaches this board. Present, each of `urgent`,
2866    /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2867    /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2868    /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2869    /// no two levels may name one option. Reads and writes never create the field or an
2870    /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2871    /// the board lacks is refused pointing there.
2872    #[serde(default)]
2873    pub priority_mapping: Option<PriorityMappingConfig>,
2874    /// Caller metadata values this source also writes to board text fields, so the board can
2875    /// filter on them.
2876    ///
2877    /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2878    /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2879    /// value. On every item write — create, update and copy, of a task, a project or a
2880    /// document — a string found there is written to the field, and nothing is sent when the
2881    /// field already holds it; nothing there, or `null`, clears the field when it holds a
2882    /// value; any other JSON type is refused before any mutation. The metadata comment in the
2883    /// issue body stays the one home of the value, and every read reports metadata from it.
2884    /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2885    /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2886    /// non-text field of that name, is refused pointing there.
2887    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2888    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2889    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2890    #[schemars(!skip_serializing_if)]
2891    pub metadata_fields: Vec<MetadataFieldConfig>,
2892    /// How fast this source writes, and how long it waits out a rate-limit refusal.
2893    ///
2894    /// Every field keeps its shipped default when it is absent, and the defaults are
2895    /// GitHub's own published limits rather than taste. See [`Pacing`].
2896    #[serde(default)]
2897    pub pacing: PacingConfig,
2898}
2899
2900/// Which option of the board's `Priority` field each priority lands on.
2901///
2902/// One member per level rather than a map, so a key that is not a level is refused where
2903/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2904/// value in the field, not an option of it.
2905#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2906#[serde(default, deny_unknown_fields)]
2907pub struct PriorityMappingConfig {
2908    /// The option `urgent` lands on; `Urgent` when absent.
2909    pub urgent: Option<PriorityOptionName>,
2910    /// The option `high` lands on; `High` when absent.
2911    pub high: Option<PriorityOptionName>,
2912    /// The option `medium` lands on; `Medium` when absent.
2913    pub medium: Option<PriorityOptionName>,
2914    /// The option `low` lands on; `Low` when absent.
2915    pub low: Option<PriorityOptionName>,
2916}
2917
2918/// The name of an option of the board's `Priority` single-select field.
2919///
2920/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2921/// blank name.
2922#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2923#[serde(try_from = "String")]
2924#[schemars(extend("minLength" = 1))]
2925pub struct PriorityOptionName(String);
2926
2927impl PriorityOptionName {
2928    /// The option name, as the board spells it.
2929    fn as_str(&self) -> &str {
2930        &self.0
2931    }
2932}
2933
2934impl TryFrom<String> for PriorityOptionName {
2935    type Error = String;
2936
2937    fn try_from(name: String) -> Result<Self, Self::Error> {
2938        if name.trim().is_empty() {
2939            return Err("a priority_mapping option name cannot be blank".to_owned());
2940        }
2941        Ok(Self(name))
2942    }
2943}
2944
2945/// The name of the board field a priority is held in.
2946pub const PRIORITY_FIELD: &str = "Priority";
2947
2948/// The four priorities a board option can hold, in the order a new `Priority` field lists
2949/// them. `none` is not among them: it is the field holding no value.
2950///
2951/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2952/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2953/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2954/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2955pub const PRIORITY_LEVELS: [Priority; 4] = [
2956    Priority::Urgent,
2957    Priority::High,
2958    Priority::Medium,
2959    Priority::Low,
2960];
2961
2962/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2963/// see that list for what this pins.
2964#[must_use]
2965pub const fn level_position(priority: Priority) -> Option<usize> {
2966    match priority {
2967        Priority::None => None,
2968        Priority::Urgent => Some(0),
2969        Priority::High => Some(1),
2970        Priority::Medium => Some(2),
2971        Priority::Low => Some(3),
2972    }
2973}
2974
2975/// This instance's complete priority-to-option mapping, read in both directions.
2976///
2977/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2978/// two levels name one option.
2979#[derive(Debug, Clone)]
2980struct PriorityMapping {
2981    options: [PriorityOptionName; 4],
2982}
2983
2984impl PriorityMapping {
2985    fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2986        let shipped = |name: &str| PriorityOptionName(name.to_owned());
2987        let mapping = Self {
2988            options: [
2989                config.urgent.unwrap_or_else(|| shipped("Urgent")),
2990                config.high.unwrap_or_else(|| shipped("High")),
2991                config.medium.unwrap_or_else(|| shipped("Medium")),
2992                config.low.unwrap_or_else(|| shipped("Low")),
2993            ],
2994        };
2995        for (index, option) in mapping.options.iter().enumerate() {
2996            if let Some(earlier) = mapping.options[..index]
2997                .iter()
2998                .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2999            {
3000                return Err(SourceError::Config {
3001                    message: format!(
3002                        "priority_mapping of source {instance} sends both {} and {} to the board \
3003                         option {:?}; one option cannot read back as two priorities",
3004                        PRIORITY_LEVELS[earlier],
3005                        PRIORITY_LEVELS[index],
3006                        option.as_str()
3007                    ),
3008                });
3009            }
3010        }
3011        Ok(mapping)
3012    }
3013
3014    /// The option `priority` lands on, or `None` for `none`, which is no option at all.
3015    fn option(&self, priority: Priority) -> Option<&str> {
3016        level_position(priority).map(|index| self.options[index].as_str())
3017    }
3018
3019    /// The priority a board option name reports, or `None` when nothing maps to it.
3020    fn priority_of(&self, option: &str) -> Option<Priority> {
3021        self.options
3022            .iter()
3023            .position(|name| name.as_str().eq_ignore_ascii_case(option))
3024            .map(|index| PRIORITY_LEVELS[index])
3025    }
3026
3027    /// Every mapped option name, in the order a new `Priority` field lists them.
3028    fn names(&self) -> impl Iterator<Item = &str> {
3029        self.options.iter().map(PriorityOptionName::as_str)
3030    }
3031}
3032
3033/// One metadata value this source projects onto a board text field.
3034#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3035#[serde(deny_unknown_fields)]
3036pub struct MetadataFieldConfig {
3037    /// The name of the board text field the value is written to.
3038    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.
3039    /// The top-level metadata key the value is read from. A key under the reserved
3040    /// `onetaskgraph.` prefix is refused.
3041    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.
3042    /// Object keys walked inside that key's value, outermost first. Absent or empty, the
3043    /// key's own value is the one projected. Left out of a serialized entry when empty.
3044    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3045    // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
3046    #[schemars(!skip_serializing_if)]
3047    pub path: Vec<String>,
3048}
3049
3050/// The board fields no metadata value may be projected onto, compared ignoring case: the
3051/// three this source writes itself, and those GitHub owns on every board.
3052// 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.
3053const UNPROJECTABLE_FIELDS: [&str; 13] = [
3054    STATUS_FIELD,
3055    PRIORITY_FIELD,
3056    ORIGIN_FIELD,
3057    "Title",
3058    "Assignees",
3059    "Labels",
3060    "Linked pull requests",
3061    "Milestone",
3062    "Repository",
3063    "Reviewers",
3064    "Parent issue",
3065    "Sub-issues progress",
3066    "Type",
3067];
3068
3069/// One validated [`MetadataFieldConfig`] entry.
3070#[derive(Debug, Clone)]
3071struct MetadataField {
3072    field: ProjectedField,
3073    key: ProjectedKey,
3074    path: Vec<String>,
3075}
3076
3077/// The name of a board text field a metadata value is projected onto: never blank, never one
3078/// this source or GitHub writes, and never one another entry of the same source names.
3079#[derive(Debug, Clone, PartialEq, Eq)]
3080struct ProjectedField(String);
3081
3082/// The top-level metadata key a projected value is read from: never blank, and never under
3083/// the namespace this product reserves.
3084#[derive(Debug, Clone, PartialEq, Eq)]
3085struct ProjectedKey(String);
3086
3087impl std::ops::Deref for ProjectedField {
3088    type Target = str;
3089    fn deref(&self) -> &str {
3090        &self.0
3091    }
3092}
3093
3094impl std::ops::Deref for ProjectedKey {
3095    type Target = str;
3096    fn deref(&self) -> &str {
3097        &self.0
3098    }
3099}
3100
3101/// What one item's metadata says one projected field should hold.
3102#[derive(Debug, Clone, PartialEq, Eq)]
3103enum Projected {
3104    /// A string to hold.
3105    Text(String),
3106    /// Nothing: the key or a step of the path is absent, or the value is `null`.
3107    Nothing,
3108}
3109
3110impl MetadataField {
3111    /// Validate every entry of one instance's `metadata_fields`, refusing the first that
3112    /// cannot stand, naming the source and the entry.
3113    fn resolve(
3114        configured: Vec<MetadataFieldConfig>,
3115        instance: &SourceName,
3116    ) -> Result<Vec<Self>, SourceError> {
3117        let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
3118        for (index, entry) in configured.into_iter().enumerate() {
3119            let refuse = |why: String| SourceError::Config {
3120                message: format!(
3121                    "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
3122                    entry.field, entry.key
3123                ),
3124            };
3125            if entry.field.trim().is_empty() {
3126                return Err(refuse("names a blank field".to_owned()));
3127            }
3128            if entry.key.trim().is_empty() {
3129                return Err(refuse("names a blank key".to_owned()));
3130            }
3131            if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
3132                return Err(refuse(format!(
3133                    "names a key under the reserved \"{}.\" prefix, which this product keeps \
3134                     for its own keys; next: project a caller key instead",
3135                    MetadataKey::RESERVED_NAMESPACE
3136                )));
3137            }
3138            if let Some(owned) = UNPROJECTABLE_FIELDS
3139                .iter()
3140                .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
3141            {
3142                return Err(refuse(format!(
3143                    "names the board field {owned:?}, which this source or GitHub already \
3144                     writes; next: name a text field of your own"
3145                )));
3146            }
3147            if let Some(earlier) = resolved
3148                .iter()
3149                .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
3150            {
3151                return Err(refuse(format!(
3152                    "names the same board field as metadata_fields[{earlier}]; one field \
3153                     cannot hold two values"
3154                )));
3155            }
3156            resolved.push(Self {
3157                field: ProjectedField(entry.field),
3158                key: ProjectedKey(entry.key),
3159                path: entry.path,
3160            });
3161        }
3162        Ok(resolved)
3163    }
3164
3165    /// The path as a refusal spells it: `[]` for the key's own value.
3166    fn spelled_path(&self) -> String {
3167        serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
3168    }
3169
3170    /// What `metadata` says this field should hold, or the refusal for a value no text field
3171    /// can hold.
3172    fn projected(
3173        &self,
3174        metadata: &BTreeMap<String, Value>,
3175        instance: &SourceName,
3176    ) -> Result<Projected, SourceError> {
3177        let mut value = metadata.get(&*self.key);
3178        for step in &self.path {
3179            value = value.and_then(|held| held.get(step.as_str()));
3180        }
3181        let found = match value {
3182            None | Some(Value::Null) => return Ok(Projected::Nothing),
3183            Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
3184            Some(Value::Bool(_)) => "a boolean",
3185            Some(Value::Number(_)) => "a number",
3186            Some(Value::Array(_)) => "an array",
3187            Some(Value::Object(_)) => "an object",
3188        };
3189        Err(SourceError::Refused {
3190            message: format!(
3191                "source {instance} projects metadata key {:?} at path {} onto its board text \
3192                 field {:?}, and this item holds {found} there; only a string, null or nothing \
3193                 can be written to a text field; next: store a string there, or remove the \
3194                 value",
3195                &*self.key,
3196                self.spelled_path(),
3197                &*self.field
3198            ),
3199        })
3200    }
3201}
3202
3203/// What one item's `Priority` field says, read through this instance's mapping.
3204#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
3205enum HeldPriority {
3206    /// A priority this source reports: an option the mapping names, or no value (`none`).
3207    Read(Priority),
3208    /// An option the mapping does not name, which is never read as a level or as `none`.
3209    Unmapped(String),
3210}
3211
3212/// How fast this source writes, and how long it waits out a rate-limit refusal.
3213///
3214/// Configurable because a GitHub Enterprise installation sets its own limits and an
3215/// operator who has already been refused may want to go slower still — not because the
3216/// defaults are guesses.
3217#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3218#[serde(default, deny_unknown_fields)]
3219pub struct PacingConfig {
3220    /// Shortest interval between two content-creating mutations, in milliseconds.
3221    ///
3222    /// Zero sends them as fast as they are asked for, which is what a fixture server on
3223    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
3224    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.
3225    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
3226    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
3227    /// zero while there is a budget to spend, because a schedule of zero-length waits
3228    /// consumes none of it and so never ends.
3229    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.
3230    /// Total time one call may spend waiting out rate limits, in milliseconds.
3231    ///
3232    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
3233    /// the bound is what makes this a wait rather than a hang.
3234    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.
3235}
3236
3237/// The largest any pacing setting may be, in milliseconds.
3238///
3239/// One hour. GitHub's own harshest published bound on content-generating requests works
3240/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
3241/// anything a real limit asks for, and past it the settings stop describing pacing at all:
3242/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
3243/// and an interval beyond it is a command that never sends its second mutation. It also
3244/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
3245/// what a `Duration` can hold on every platform.
3246pub const MAX_PACING_MS: u64 = 3_600_000;
3247
3248/// [`PacingConfig`] with every default resolved and every value checked, which is what the
3249/// source holds.
3250///
3251/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
3252/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
3253/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
3254/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
3255/// is asked for, windows included.
3256#[derive(Debug, Clone, Copy)]
3257struct Pacing {
3258    /// Whether requests are held for GitHub's secondary windows: always, unless
3259    /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
3260    /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
3261    windows: bool,
3262    min_mutation_interval: Duration,
3263    retry_backoff: Duration,
3264    retry_budget: Duration,
3265}
3266
3267impl Pacing {
3268    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
3269    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3270        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
3271            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
3272                message: format!(
3273                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3274                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3275                     GitHub's own harshest published limit"
3276                ),
3277            }),
3278            Some(value) => Ok(Duration::from_millis(value)),
3279            None => Ok(Duration::from_millis(default)),
3280        };
3281        let retry_backoff = bounded(
3282            config.retry_backoff_ms,
3283            RETRY_BACKOFF_MS,
3284            "retry_backoff_ms",
3285        )?;
3286        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3287        if retry_backoff.is_zero() && !retry_budget.is_zero() {
3288            return Err(SourceError::Config {
3289                message: format!(
3290                    "pacing.retry_backoff_ms of source {instance} is 0 while \
3291                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3292                     none of that budget, so it would retry a refusal forever. Set a backoff of \
3293                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3294                     waiting at all",
3295                    retry_budget.as_millis()
3296                ),
3297            });
3298        }
3299        Ok(Self {
3300            windows: config.min_mutation_interval_ms != Some(0),
3301            min_mutation_interval: bounded(
3302                config.min_mutation_interval_ms,
3303                0,
3304                "min_mutation_interval_ms",
3305            )?,
3306            retry_backoff,
3307            retry_budget,
3308        })
3309    }
3310}
3311
3312/// Factory for [`GitHubProjectsSource`].
3313#[derive(Debug, Clone, Copy, Default)]
3314pub struct Plugin;
3315
3316impl SourcePlugin for Plugin {
3317    fn kind(&self) -> &'static str {
3318        KIND
3319    }
3320    /// The endpoint, the owner and the board's number: what names one board.
3321    fn remote_identity(&self, config: &Value) -> Option<String> {
3322        let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
3323        Some(format!(
3324            "{}\n{}\n{}",
3325            config.endpoint, config.owner, config.project_number
3326        ))
3327    }
3328    fn config_schema(&self) -> Schema {
3329        schema_for!(GitHubProjectsConfig)
3330    }
3331    fn build(
3332        &self,
3333        name: &SourceName,
3334        config: &Value,
3335        secrets: &dyn SecretResolver,
3336    ) -> Result<Box<dyn TaskSource>, SourceError> {
3337        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
3338    }
3339
3340    fn build_with_clock(
3341        &self,
3342        name: &SourceName,
3343        config: &Value,
3344        secrets: &dyn SecretResolver,
3345        clock: SharedClock,
3346    ) -> Result<Box<dyn TaskSource>, SourceError> {
3347        self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
3348    }
3349}
3350
3351/// The client `cell` holds, building it with `build` the first time it is asked for.
3352///
3353/// A failed build is the request's failure, with the message the source's construction gave it
3354/// before clients were deferred.
3355// 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`.
3356fn built(
3357    cell: &std::sync::OnceLock<Client>,
3358    build: impl FnOnce() -> Result<Client, SourceError>,
3359) -> Result<&Client, SourceError> {
3360    if let Some(client) = cell.get() {
3361        return Ok(client);
3362    }
3363    let client = build()?;
3364    Ok(cell.get_or_init(|| client))
3365}
3366
3367impl Plugin {
3368    /// Build a source recording every request it sends into an accounting the caller holds.
3369    ///
3370    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
3371    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
3372    /// session total rather than two — see [`accounting`] and
3373    /// [`GitHubProjectsSource::recording_into`].
3374    ///
3375    /// # Errors
3376    ///
3377    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
3378    /// [`SourceError::Config`] for configuration this plugin cannot use and
3379    /// [`SourceError::Auth`] for a credential it cannot find.
3380    pub fn build_recording_into(
3381        &self,
3382        name: &SourceName,
3383        config: &Value,
3384        secrets: &dyn SecretResolver,
3385        ledger: Arc<Accounting>,
3386    ) -> Result<Box<dyn TaskSource>, SourceError> {
3387        self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
3388    }
3389
3390    fn build_recording_with_clock(
3391        &self,
3392        name: &SourceName,
3393        config: &Value,
3394        secrets: &dyn SecretResolver,
3395        ledger: Arc<Accounting>,
3396        clock: SharedClock,
3397    ) -> Result<Box<dyn TaskSource>, SourceError> {
3398        let config: GitHubProjectsConfig =
3399            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
3400                message: format!("source {name}: {e}"),
3401            })?;
3402        let prefix = format!("source {name}: ");
3403        let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
3404            .map_err(|error| match error {
3405                // The shared `StatusMapping::distinct` names the source itself.
3406                SourceError::Config { message } if message.starts_with(&prefix) => {
3407                    SourceError::Config { message }
3408                }
3409                SourceError::Config { message } => SourceError::Config {
3410                    message: format!("{prefix}{message}"),
3411                },
3412                SourceError::Auth { message } => SourceError::Auth {
3413                    message: format!("source {name}: {message}"),
3414                },
3415                other => other,
3416            })?;
3417        source.clock = clock;
3418        Ok(Box::new(source))
3419    }
3420}
3421
3422/// Where a status category lands on this board, once configuration is resolved.
3423#[derive(Debug, Clone, PartialEq, Eq)]
3424enum StatusTarget {
3425    /// Not usable against this instance for this kind, and why.
3426    Disabled(UnmappedStatus),
3427    /// The board's `Status` option of this name.
3428    Column(ColumnName),
3429    /// A closed issue, with both its board option and the reason that says which closed it means.
3430    // 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.
3431    Terminal(ColumnName, ClosedState),
3432}
3433
3434/// Every status category, in the order the vocabulary declares them.
3435///
3436/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
3437/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
3438/// added to the shared vocabulary fails to compile until it is named there, and this
3439/// crate's suite reconciles this list against that enum's own derived schema, which is
3440/// generated from the variants rather than written beside them. The schema is what
3441/// catches a list left one short — a list checking only the positions it already holds
3442/// would pass while every mapping indexed by the new position panicked.
3443pub const CATEGORIES: [StatusCategory; 8] = [
3444    StatusCategory::Draft,
3445    StatusCategory::Backlog,
3446    StatusCategory::Todo,
3447    StatusCategory::Queued,
3448    StatusCategory::InProgress,
3449    StatusCategory::Done,
3450    StatusCategory::Cancelled,
3451    StatusCategory::Unknown,
3452];
3453
3454/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
3455#[must_use]
3456pub const fn category_position(category: StatusCategory) -> usize {
3457    match category {
3458        StatusCategory::Draft => 0,
3459        StatusCategory::Backlog => 1,
3460        StatusCategory::Todo => 2,
3461        StatusCategory::Queued => 3,
3462        StatusCategory::InProgress => 4,
3463        StatusCategory::Done => 5,
3464        StatusCategory::Cancelled => 6,
3465        StatusCategory::Unknown => 7,
3466    }
3467}
3468
3469/// The spelling a status category is configured and reported under.
3470fn category_name(category: StatusCategory) -> &'static str {
3471    match category {
3472        StatusCategory::Draft => "draft",
3473        StatusCategory::Backlog => "backlog",
3474        StatusCategory::Todo => "todo",
3475        StatusCategory::Queued => "queued",
3476        StatusCategory::InProgress => "in-progress",
3477        StatusCategory::Done => "done",
3478        StatusCategory::Cancelled => "cancelled",
3479        StatusCategory::Unknown => "unknown",
3480    }
3481}
3482
3483/// A shipped default's option name.
3484///
3485/// The literals below are this file's own and non-blank, and they are validated by the
3486/// one constructor a configured name goes through rather than beside it.
3487fn shipped_column(name: &'static str) -> ColumnName {
3488    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
3489}
3490
3491/// The shipped default for one category this instance's `status_mapping` does not mention,
3492/// for either kind.
3493fn shipped_default(category: StatusCategory) -> StatusTarget {
3494    match category {
3495        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
3496        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
3497        StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
3498        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
3499        StatusCategory::Done => {
3500            StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
3501        }
3502        StatusCategory::Cancelled => {
3503            StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
3504        }
3505        StatusCategory::Draft | StatusCategory::Unknown => {
3506            StatusTarget::Disabled(UnmappedStatus::Unconfigured)
3507        }
3508    }
3509}
3510
3511/// The two kinds a status is written and read for, each with its own half of the mapping.
3512const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
3513
3514/// This instance's complete category-to-target mapping for each kind, read in both
3515/// directions.
3516///
3517/// One target per category per kind, held at that category's own [`category_position`], so
3518/// a category missing from the mapping, named twice in it, or filed out of order is a state
3519/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
3520/// targets are options of the board's one `Status` field.
3521#[derive(Debug, Clone)]
3522struct BoardStatuses {
3523    tasks: [StatusTarget; CATEGORIES.len()],
3524    projects: [StatusTarget; CATEGORIES.len()],
3525}
3526
3527impl BoardStatuses {
3528    /// Resolve `configured` against the shipped defaults, refusing two categories one kind
3529    /// would read back from one option.
3530    ///
3531    /// A category the mapping does not mention keeps its shipped default for both kinds; one
3532    /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
3533    /// omits unmapped rather than defaulted.
3534    fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
3535        let resolve_kind =
3536            |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
3537                // `CATEGORIES[position] == category` for every category — the crate's suite
3538                // asserts it — so mapping the list in order fills each category's own slot.
3539                let mut targets = CATEGORIES.map(shipped_default);
3540                for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
3541                    if !configured.mentions(category) {
3542                        continue;
3543                    }
3544                    *slot = match configured.name_for(category, kind) {
3545                        Err(why) => StatusTarget::Disabled(why),
3546                        Ok(name) => {
3547                            let option = ColumnName::try_from(name.as_str().to_owned())
3548                                .map_err(|message| SourceError::Config { message })?;
3549                            match category {
3550                                StatusCategory::Done => {
3551                                    StatusTarget::Terminal(option, ClosedState::Completed)
3552                                }
3553                                StatusCategory::Cancelled => {
3554                                    StatusTarget::Terminal(option, ClosedState::NotPlanned)
3555                                }
3556                                _ => StatusTarget::Column(option),
3557                            }
3558                        }
3559                    };
3560                }
3561                StatusMapping::distinct(
3562                    instance,
3563                    kind,
3564                    CATEGORIES
3565                        .iter()
3566                        .zip(&targets)
3567                        .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
3568                )?;
3569                Ok(targets)
3570            };
3571        Ok(Self {
3572            tasks: resolve_kind(ItemKind::Task)?,
3573            projects: resolve_kind(ItemKind::Project)?,
3574        })
3575    }
3576
3577    /// Every category's target for `kind`, in category order.
3578    const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
3579        match kind {
3580            ItemKind::Task => &self.tasks,
3581            ItemKind::Project => &self.projects,
3582        }
3583    }
3584
3585    fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
3586        &self.targets(kind)[category_position(category)]
3587    }
3588
3589    /// The category a board option name reports for `kind`, or `None` when nothing of that
3590    /// kind maps to it.
3591    fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
3592        CATEGORIES.into_iter().find(|category| {
3593            self.target(kind, *category)
3594                .option()
3595                .is_some_and(|name| name.eq_ignore_ascii_case(option))
3596        })
3597    }
3598
3599    /// Every option name either kind maps a category to, each once ignoring case, in
3600    /// category order with a task's name before a project's — what the guarded setup asks
3601    /// the `Status` field to hold.
3602    fn wanted(&self) -> Vec<String> {
3603        let mut wanted: Vec<String> = Vec::new();
3604        for category in CATEGORIES {
3605            for kind in STATUS_KINDS {
3606                if let Some(name) = self.target(kind, category).option()
3607                    && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
3608                {
3609                    wanted.push(name.to_owned());
3610                }
3611            }
3612        }
3613        wanted
3614    }
3615
3616    /// The status an item of `kind` reports, from the three things a read of it says: its
3617    /// board `Status` option, whether its issue is closed, and the reason it was closed with.
3618    ///
3619    /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
3620    /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
3621    /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
3622    /// produce, so it is read permissively rather than refused — reads are faithful, and
3623    /// refusals belong on writes.
3624    ///
3625    /// **Its name follows the closure too, unless the option agrees with it.** The option's
3626    /// own name is kept when this kind's mapping reads that option as the same category, or
3627    /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3628    /// named `Shipped`. An option the mapping reads as another category contradicts the
3629    /// closure, and so does no option at all: the name is then the option the mapping gives
3630    /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3631    /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3632    /// it. An `unknown` closure keeps the option, or `Closed` without one.
3633    ///
3634    /// An open item's option reads through its own kind's mapping, and an option that mapping
3635    /// does not name reads as `Unknown` under its own name.
3636    ///
3637    /// One function of those three rather than of a response, so a narrow status write can
3638    /// answer what a re-read would report by applying it to the state it has just written.
3639    fn status(
3640        &self,
3641        kind: ItemKind,
3642        option: Option<&str>,
3643        closed: bool,
3644        reason: Option<&str>,
3645    ) -> Status {
3646        if closed {
3647            let category = closed_category(reason);
3648            let agrees = option.filter(|option| {
3649                category == StatusCategory::Unknown
3650                    || self
3651                        .category_of(kind, option)
3652                        .is_none_or(|read| read == category)
3653            });
3654            let name = agrees.or_else(|| self.target(kind, category).option());
3655            let fallback = match category {
3656                StatusCategory::Done => "Done",
3657                StatusCategory::Cancelled => "Cancelled",
3658                _ => "Closed",
3659            };
3660            return Status {
3661                category,
3662                name: name.or(option).unwrap_or(fallback).to_owned(),
3663            };
3664        }
3665        let name = option.unwrap_or("Open").to_owned();
3666        Status {
3667            category: self
3668                .category_of(kind, &name)
3669                .unwrap_or(StatusCategory::Unknown),
3670            name,
3671        }
3672    }
3673
3674    /// The option a closed item's `Status` has to hold for its card to agree with its
3675    /// closure, and the category that closure reads as — or `None` when the item is open,
3676    /// its closure reads `unknown`, or its option already agrees.
3677    ///
3678    /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3679    /// in line, each to the one option the mapping gives its category — an item with no
3680    /// option, or at one the mapping does not name, included.
3681    fn contradicted(
3682        &self,
3683        kind: ItemKind,
3684        option: Option<&str>,
3685        closed: bool,
3686        reason: Option<&str>,
3687    ) -> Option<(ClosedAs, &str)> {
3688        if !closed {
3689            return None;
3690        }
3691        let closed_as = match closed_category(reason) {
3692            StatusCategory::Done => ClosedAs::Done,
3693            StatusCategory::Cancelled => ClosedAs::Cancelled,
3694            _ => return None,
3695        };
3696        let wanted = self.target(kind, closed_as.category()).option()?;
3697        if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3698            return None;
3699        }
3700        Some((closed_as, wanted))
3701    }
3702}
3703
3704/// The category a closed issue reads as, from the reason it was closed with.
3705///
3706/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3707/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3708/// is one this source cannot read a category from.
3709fn closed_category(reason: Option<&str>) -> StatusCategory {
3710    match reason {
3711        None | Some("COMPLETED") => StatusCategory::Done,
3712        Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3713        Some(_) => StatusCategory::Unknown,
3714    }
3715}
3716
3717impl BoardStatuses {
3718    /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3719    /// case; a kind lacking none is left out.
3720    fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3721        STATUS_KINDS
3722            .into_iter()
3723            .filter_map(|kind| {
3724                let missing: Vec<String> = self
3725                    .targets(kind)
3726                    .iter()
3727                    .filter_map(StatusTarget::option)
3728                    .filter(|wanted| {
3729                        !existing
3730                            .iter()
3731                            .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3732                    })
3733                    .map(str::to_owned)
3734                    .collect();
3735                (!missing.is_empty()).then_some(KindMissing { kind, missing })
3736            })
3737            .collect()
3738    }
3739}
3740
3741impl StatusTarget {
3742    /// The board option this target selects, or `None` for an unmapped one.
3743    fn option(&self) -> Option<&str> {
3744        match self {
3745            Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3746            Self::Disabled(_) => None,
3747        }
3748    }
3749}
3750
3751// 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.
3752/// One repository this source can create an issue in, as `owner/name`.
3753///
3754/// Every `createIssue` this source sends names one of these: the item's own single
3755/// `repositories` entry — on a source declared private, only when it reads private — else
3756/// its parent project issue's repository, else the configured
3757/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3758/// that choice and says what it refuses before `createIssue`.
3759// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3760#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
3761#[serde(try_from = "String", into = "String")]
3762struct RepositoryTarget {
3763    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3764    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3765}
3766
3767impl RepositoryTarget {
3768    fn parse(value: &str) -> Result<Self, SourceError> {
3769        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3770            message: format!(
3771                "repository must be spelled owner/name; {value:?} names no repository"
3772            ),
3773        })?;
3774        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3775            return Err(SourceError::Config {
3776                message: format!(
3777                    "repository must be spelled owner/name with a GitHub login and one \
3778                     repository name; {value:?} is not"
3779                ),
3780            });
3781        }
3782        Ok(Self {
3783            owner: owner.to_owned(),
3784            name: name.to_owned(),
3785        })
3786    }
3787
3788    /// The one host whose repositories this source creates issues in, spelled once: it is
3789    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3790    const HOST: &str = "github.com";
3791
3792    fn origin(&self) -> String {
3793        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3794    }
3795
3796    /// The repository a normalized origin names, or why it is none this source can create
3797    /// an issue in: another host, or more or fewer than `owner/name` under this one.
3798    fn from_origin(origin: &Repository) -> Result<Self, String> {
3799        let not_here = || {
3800            format!(
3801                "{} is not a {}/owner/name repository",
3802                origin.as_str(),
3803                Self::HOST
3804            )
3805        };
3806        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3807        if host != Self::HOST {
3808            return Err(not_here());
3809        }
3810        Self::parse(rest).map_err(|_| not_here())
3811    }
3812
3813    fn slug(&self) -> String {
3814        format!("{}/{}", self.owner, self.name)
3815    }
3816}
3817
3818/// Read back from a kept context as the `owner/name` it was written as, refused unless
3819/// [`RepositoryTarget::parse`] takes it.
3820impl TryFrom<String> for RepositoryTarget {
3821    type Error = String;
3822
3823    fn try_from(slug: String) -> Result<Self, Self::Error> {
3824        Self::parse(&slug).map_err(|error| error.to_string())
3825    }
3826}
3827
3828impl From<RepositoryTarget> for String {
3829    fn from(target: RepositoryTarget) -> Self {
3830        target.slug()
3831    }
3832}
3833
3834/// How long GitHub's issue search may go on missing a comment after it was made or edited,
3835/// which a read of the comments made since an instant sets its watermark back by: four times the
3836/// thirty seconds the credentialed lane has watched it lag.
3837pub const COMMENT_SEARCH_LAG: std::time::Duration = std::time::Duration::from_secs(120);
3838
3839/// A source which reads GitHub afresh for every operation.
3840pub struct GitHubProjectsSource {
3841    /// This source's configured name, used both to tell a far end naming this source
3842    /// from one naming a system it knows nothing about, and to name the instance a
3843    /// status refusal is about.
3844    name: SourceName,
3845    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3846    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3847    repository: Option<RepositoryTarget>,
3848    /// The logins whose repositories a create may file in; empty restricts nothing.
3849    creation_owners: Vec<String>, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after each entry passes GitHub's login grammar.
3850    endpoint: Url,
3851    token: SecretString,
3852    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3853    statuses: BoardStatuses,
3854    /// Where each priority lands on this board, or `None` when this instance holds none.
3855    priorities: Option<PriorityMapping>,
3856    /// The metadata values this instance projects onto board text fields, in configured order.
3857    metadata_fields: Vec<MetadataField>,
3858    /// Built on the first request rather than with the source, as [`Self::client`] says.
3859    client: std::sync::OnceLock<Client>,
3860    /// Built on the first attachment request, as [`Self::asset_client`] says.
3861    asset_client: std::sync::OnceLock<Client>,
3862    /// The markup the engine handed this source for its bodies' headings, applied to every
3863    /// visible body it composes and stripped from every one it reads back: see
3864    /// [`TaskSource::attach_body_markup`].
3865    body_markup: std::sync::OnceLock<onetaskgraph_plugin_api::SharedBodyMarkup>,
3866    /// Every item this source has created in this command, in the order it created them —
3867    /// dropped by [`TaskSource::end_command`].
3868    ///
3869    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3870    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3871    /// a copy resolving a dependency on an item it had just created refused it as not
3872    /// found. A board read is completed from this — an item remembered here and absent from
3873    /// the read is added back, because the board really does hold it and only the read is
3874    /// behind.
3875    ///
3876    /// It is not a cache of a user's work: nothing is remembered that this process did not
3877    /// itself just write, it lives and dies with the process, and it is never consulted for
3878    /// an item this source did not create.
3879    created: Mutex<Vec<Resolved>>,
3880    /// Every item that already existed and that this source has written in this command, as
3881    /// it wrote it — dropped by [`TaskSource::end_command`].
3882    ///
3883    /// The other half of [`Self::created`], held on the same terms and for the reason a
3884    /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3885    /// filter is an index behind a write this process made moments ago, so a query matching
3886    /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3887    /// is remembered that this process did not itself just write.
3888    updated: Mutex<Vec<Resolved>>,
3889    /// Every issue this source has added a comment to or edited a comment of in this command
3890    /// — dropped by [`TaskSource::end_command`].
3891    ///
3892    /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3893    /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3894    /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3895    /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3896    /// from before — until the index caught up. Each id here is a candidate of every such
3897    /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3898    /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3899    /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3900    /// process wrote is still found only once the index has it.
3901    commented: Mutex<Vec<NativeId>>,
3902    /// How fast this source writes, and how long it waits out a refusal.
3903    pacing: Pacing,
3904    /// When the last content-creating mutation finished, or the moment the furthest-out
3905    /// reserved slot releases the next one, whichever is later — so the one after it can be
3906    /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
3907    /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
3908    /// measured from.
3909    last_mutation: Mutex<Option<Duration>>,
3910    /// The `Date` GitHub stated on the last answer this source read: its own time, which the
3911    /// watermark of a comment read is set back from. See [`TaskSource::served_at`].
3912    last_served: Mutex<Option<DateTime<Utc>>>,
3913    /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
3914    windows: Mutex<pacing::Windows>,
3915    /// The tasks a project holds, as a copy's batched read of the project and every one of them
3916    /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
3917    /// a listing of that project's tasks is answered without a read of its own.
3918    task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
3919    /// The web address of each issue this source created in the current command, as its
3920    /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
3921    /// request.
3922    written: Mutex<BTreeMap<NativeId, String>>,
3923    /// What a copy has held back of the items it created, between
3924    /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
3925    /// nothing is being held back, when every write lands whole as it is made.
3926    deferred: Mutex<Option<Deferred>>,
3927    /// Each existing issue's update held back while writes are, in the order it was made: sent
3928    /// together by [`Self::send_held_updates`] before any other request goes out, so nothing
3929    /// this source sends or reads ever sees the board without them.
3930    held_updates: Mutex<Vec<HeldUpdate>>,
3931    clock: SharedClock,
3932    /// Whether the configuration declares this source private, as
3933    /// [`TaskSource::declare_visibility`] told it: what decides where a new issue whose one
3934    /// named repository reads public is created — see [`Self::creation_rule`].
3935    declared_private: std::sync::atomic::AtomicBool,
3936    numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3937    /// The board as this process last read it, for the length of one command — dropped by
3938    /// [`TaskSource::end_command`].
3939    ///
3940    /// A copy of a project used to re-read the whole board, paged, before writing each of
3941    /// its items, which is by far the largest part of a copy's request count and none of
3942    /// its work. Nothing else changes this board while a command runs — this source's own
3943    /// writes are the only writer — so one read answers them all.
3944    ///
3945    /// It is not a store of a user's work and it is not the cache the no-persistence
3946    /// invariant forbids: it lives and dies with the process exactly as `created` does,
3947    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3948    /// an item this command created and then depends on resolves whether or not GitHub's
3949    /// own eventually-consistent read has caught up. A write to an item already on the
3950    /// board updates the entry here too, so what this holds is the last read plus this
3951    /// process's own writes rather than a snapshot taken before them.
3952    board_cache: Mutex<Option<Board>>,
3953    /// Every issue this board's own search reported, for the length of one command — dropped
3954    /// by [`TaskSource::end_command`].
3955    ///
3956    /// The second half of a board read, and cached for the same reason and on the same
3957    /// terms as the first: it lives and dies with the process, nothing is written down, and
3958    /// a write this process makes updates the entry here exactly as it updates the one in
3959    /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3960    /// that lists this board's projects and its tasks pays for one search rather than two.
3961    search_cache: Mutex<Option<Vec<Resolved>>>,
3962    /// What each narrowed question GitHub was asked answered, keyed by that question, for
3963    /// the length of one command — dropped by [`TaskSource::end_command`].
3964    ///
3965    /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3966    /// and dies with the process, nothing is written down, a write this process makes
3967    /// updates the entry here as it updates the other two, and every answer is completed
3968    /// with this process's own writes each time it is given. A command that asks the same
3969    /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3970    /// write — pays for it once, which is what the whole-board read it replaced gave it.
3971    narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3972    search_next: Mutex<BTreeMap<String, Option<String>>>,
3973    /// Records already resolved in this command, reused by writes and for comment identity.
3974    /// Explicit item reads still reach GitHub. Nothing is persisted, and
3975    /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3976    /// its item as a person has since left it.
3977    resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3978    /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
3979    /// answered as holding nothing — each taken by the next read of that item by id and by
3980    /// nothing else, dropped by any mutation naming it, and cleared by
3981    /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
3982    /// read-before-write, made in the same request as the others' rather than on its own.
3983    read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
3984    /// The origins [`TaskSource::read_ahead_origins_for_copy`] was asked about and has not sent
3985    /// yet: held for the [`TaskSource::read_ahead_for_copy`] that follows it, which sends them —
3986    /// beside the members of the one project it names, when it names one — or for the next
3987    /// request this source makes, whichever comes first.
3988    pending_origins: Mutex<Vec<String>>,
3989    /// Each project's members as a lookup carrying them read them in this command, trimmed to
3990    /// their classification and repositories, taken by [`TaskSource::member_classifications`]
3991    /// of that project and by nothing else, and cleared by [`TaskSource::end_command`].
3992    member_classes: Mutex<BTreeMap<NativeId, Vec<MemberClass>>>,
3993    /// Every item a listing of one project's members carried whole in this command — the
3994    /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
3995    /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
3996    /// it again. No other read is answered from it, so a read by id outside a copy's
3997    /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
3998    /// item, and cleared by [`TaskSource::end_command`].
3999    carried: Mutex<BTreeMap<NativeId, Resolved>>,
4000    /// Items an issue read by reference reached, held for the one status write each was read
4001    /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
4002    /// what such a write needs. Dropped by [`TaskSource::end_command`].
4003    held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
4004    /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
4005    /// asked for under, with the project that selector named — for the length of one command,
4006    /// dropped by [`TaskSource::end_command`].
4007    ///
4008    /// A caller pages through a project's tasks one engine page at a time, and every page
4009    /// is cut from the whole list of them, so without this each page walked every
4010    /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
4011    /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
4012    /// dies with the process, nothing is written down, a write this process makes updates or
4013    /// removes the entry here as it does there, and every answer is completed with this
4014    /// process's own writes each time it is given.
4015    children_cache: Mutex<ProjectChildren>,
4016    /// The board's own id and field definitions as this process last read them on their
4017    /// own, for the length of one command — dropped by [`TaskSource::end_command`].
4018    ///
4019    /// What a write needs of the board and its item does not say, read once per command
4020    /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
4021    /// lives and dies with the process and nothing is written down. It holds no item and so
4022    /// can answer no question about one — see [`Self::board_fields`].
4023    fields_cache: Mutex<Option<BoardFields>>,
4024    /// Each destination repository's node id, resolved once per repository
4025    /// rather than per issue created.
4026    ///
4027    /// A repository's node id does not change, and re-reading it for every issue of a copy
4028    /// spent one request per item on an answer this source already had. It is a map rather
4029    /// than one entry because a copy files each item in the repository its own
4030    /// `repositories` field names, so a plan across five repositories asks GitHub five
4031    /// times and not once per item.
4032    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
4033    /// What every request this source sends is recorded into.
4034    ///
4035    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
4036    /// a request leaves this crate, so nothing has to be switched on for a session to be
4037    /// counted. It is shared rather than owned so a caller accounting for a whole session —
4038    /// its own schema verification, board lookups, residue sweep and cleanup beside this
4039    /// source's reads and writes — adds up one accounting instead of two. See
4040    /// [`accounting`] for what a record carries and what a session's spend is and is not.
4041    ledger: Arc<Accounting>,
4042    /// What [`expected`] holds for a write the store's cache expects: the journal while one is
4043    /// open, the issues primed from the cache, and the comments known on each.
4044    expected: expected::Held,
4045    /// What [`projection`] holds for a producer's projection: a kept placement awaiting its
4046    /// setup-context token, and where the last create was placed.
4047    projection: projection::Held,
4048}
4049
4050/// The board item a single field update's answer names, refused unless it is `item_id`.
4051fn field_update_item<'a>(data: &'a Value, item_id: &str) -> Result<&'a Value, SourceError> {
4052    let returned = data
4053        .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
4054        .ok_or_else(|| SourceError::Malformed {
4055            message: "GitHub field update returned no project item".into(),
4056        })?;
4057    if required_str(returned, "id")? != item_id {
4058        return Err(SourceError::Malformed {
4059            message: "GitHub field update returned the wrong project item".into(),
4060        });
4061    }
4062    Ok(returned)
4063}
4064
4065/// GitHub's closed single-select color vocabulary.
4066#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
4067#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
4068pub enum StatusOptionColor {
4069    /// Gray.
4070    Gray,
4071    /// Blue.
4072    Blue,
4073    /// Green.
4074    Green,
4075    /// Yellow.
4076    Yellow,
4077    /// Purple.
4078    Purple,
4079    /// Red.
4080    Red,
4081    /// Orange.
4082    Orange,
4083    /// Pink.
4084    Pink,
4085}
4086
4087/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
4088/// applies its additions.
4089#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4090pub enum SetupMode {
4091    /// Read without mutation.
4092    Plan,
4093    /// Apply and verify.
4094    Apply,
4095}
4096
4097/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
4098/// against it goes on compiling.
4099pub type StatusOptionsMode = SetupMode;
4100
4101/// The explicit result of the requested operation.
4102#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4103#[serde(rename_all = "kebab-case")]
4104pub enum StatusOptionsOutcome {
4105    /// A read-only plan.
4106    Planned,
4107    /// Apply found nothing missing.
4108    Unchanged,
4109    /// Additions were applied and verified.
4110    Applied,
4111}
4112
4113/// A GitHub single-select option's opaque GraphQL node identifier.
4114#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4115#[serde(transparent)]
4116pub struct StatusOptionId(#[schemars(length(min = 1))] String);
4117
4118impl TryFrom<String> for StatusOptionId {
4119    type Error = String;
4120
4121    fn try_from(id: String) -> Result<Self, Self::Error> {
4122        if id.trim().is_empty() {
4123            return Err("a GitHub Status option id cannot be blank".to_owned());
4124        }
4125        Ok(Self(id))
4126    }
4127}
4128
4129/// One existing or proposed option in a guarded Status-field update.
4130#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4131pub struct StatusOption {
4132    /// GitHub's stable id.
4133    pub id: StatusOptionId,
4134    /// The visible option name.
4135    pub name: ColumnName,
4136    /// GitHub's single-select color token.
4137    pub color: StatusOptionColor,
4138    /// The option description, including an empty one.
4139    pub description: String,
4140}
4141
4142/// One board item's Status assignment, retained as recovery data.
4143#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4144pub struct StatusAssignment {
4145    /// The project item id whose assignment this is.
4146    // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
4147    // carried verbatim as operator recovery data; introducing a semantic type would claim
4148    // validation rules GitHub does not publish and no operation here interprets.
4149    pub item_id: String,
4150    /// The selected option, absent when the item has no status.
4151    #[serde(skip_serializing_if = "Option::is_none")]
4152    pub option: Option<AssignedStatusOption>,
4153}
4154
4155/// The inseparable id and name of an assigned option.
4156#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4157pub struct AssignedStatusOption {
4158    /// GitHub's stable id.
4159    pub id: StatusOptionId,
4160    /// The visible name.
4161    pub name: ColumnName,
4162}
4163
4164/// The plan and verified outcome of reconciling configured Status options.
4165#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4166pub struct StatusOptionsReport {
4167    /// The configured source name.
4168    pub source: SourceName,
4169    /// Configured option names absent before the operation.
4170    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
4171    // `ColumnName` and has therefore already passed its nonblank validation; retaining the
4172    // serialized string here preserves the report's intentionally simple public contract.
4173    pub missing: Vec<String>,
4174    /// What the requested operation did.
4175    pub outcome: StatusOptionsOutcome,
4176    /// The complete option list observed before any mutation.
4177    pub existing: Vec<StatusOption>,
4178}
4179
4180#[derive(Debug, Clone, PartialEq, Eq)]
4181struct StatusSnapshot {
4182    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4183    // passed back as the mutation's project identity; a newtype could enforce no stronger
4184    // invariant because GitHub publishes no grammar for it.
4185    board_id: String,
4186    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4187    // passed back as the mutation's field identity; a newtype could enforce no stronger
4188    // invariant because GitHub publishes no grammar for it.
4189    field_id: String,
4190    options: Vec<StatusOption>,
4191    assignments: Vec<StatusAssignment>,
4192}
4193
4194/// The name of the board field a status is held in.
4195const STATUS_FIELD: &str = "Status";
4196
4197/// Every item's value of each field `report` names, as it stood before the setup wrote
4198/// anything — what a person puts back when the setup is refused part way.
4199fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
4200    let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
4201        .fields
4202        .iter()
4203        .map(|field| (field.field.name(), before.assignments(field.field)))
4204        .collect();
4205    serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
4206        message: format!("cannot render the pre-write field recovery snapshot: {error}"),
4207    })
4208}
4209
4210/// One board field the guarded setup reads and writes — every one it reads, and the only
4211/// ones it writes.
4212#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
4213pub enum BoardField {
4214    /// The single-select `Status` field every instance's `status_mapping` resolves into.
4215    Status,
4216    /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
4217    Priority,
4218}
4219
4220impl BoardField {
4221    /// The field's name on the board.
4222    #[must_use]
4223    pub const fn name(self) -> &'static str {
4224        match self {
4225            Self::Status => STATUS_FIELD,
4226            Self::Priority => PRIORITY_FIELD,
4227        }
4228    }
4229
4230    /// The field a board calls `name`, or `None` for one this setup does not own.
4231    fn named(name: &str) -> Option<Self> {
4232        [Self::Status, Self::Priority]
4233            .into_iter()
4234            .find(|field| field.name() == name)
4235    }
4236}
4237
4238/// What the guarded setup did to one field.
4239#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4240#[serde(rename_all = "kebab-case")]
4241pub enum FieldOutcome {
4242    /// A read-only plan.
4243    Planned,
4244    /// Apply found the field there with every configured option.
4245    Unchanged,
4246    /// Missing options were added to the field that was there, and verified.
4247    Applied,
4248    /// The field was not there; it was created holding the configured options, and verified.
4249    Created,
4250}
4251
4252/// One field's plan, or its verified outcome.
4253#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4254pub struct FieldReport {
4255    /// Which field.
4256    pub field: BoardField,
4257    /// Whether the board had the field before the operation.
4258    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
4259    // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
4260    // "outcome", "existing"}` — so folding one into the other would change a published JSON
4261    // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
4262    // one constructor, and it derives `outcome` from `exists` in one match.
4263    pub exists: bool,
4264    /// Configured option names the field lacked before the operation — every one of them,
4265    /// in the order a new field lists them, when the field was not there at all.
4266    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4267    // mapping name and has therefore already passed its nonblank validation; the serialized
4268    // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
4269    pub missing: Vec<String>,
4270    /// For the `Status` field, which item kind each missing name is configured for: one
4271    /// entry per kind `status_mapping` names a missing option for, task before project, each
4272    /// listing that kind's missing names in category order. A name both kinds use is in
4273    /// both. Empty — and left out of the JSON — when nothing is missing, and always for
4274    /// `Priority`, which only a task holds.
4275    #[serde(default, skip_serializing_if = "Vec::is_empty")]
4276    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4277    // both SDKs model an absent `kinds` as an empty list rather than as `null`.
4278    #[schemars(!skip_serializing_if)]
4279    pub kinds: Vec<KindMissing>,
4280    /// What the requested operation did.
4281    pub outcome: FieldOutcome,
4282    /// The field's complete option list observed before any mutation; empty when the field
4283    /// was not there.
4284    pub existing: Vec<StatusOption>,
4285}
4286
4287/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
4288#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4289pub struct KindMissing {
4290    /// The kind these names are configured for.
4291    pub kind: ItemKind,
4292    /// The names that kind maps a category to and the field lacked, in category order.
4293    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4294    // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
4295    // intentionally simple public contract.
4296    pub missing: Vec<String>,
4297}
4298
4299/// The plan and verified outcome of setting up every field a source's configuration names.
4300#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4301pub struct FieldsReport {
4302    /// The configured source name.
4303    pub source: SourceName,
4304    /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
4305    // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
4306    // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
4307    // per field would change a published JSON shape. The states the list could hold and the
4308    // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
4309    // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
4310    pub fields: Vec<FieldReport>,
4311    /// Each board text field the source's `metadata_fields` projects a value onto, in
4312    /// configured order. Empty — and left out of the JSON — when it projects none.
4313    #[serde(default, skip_serializing_if = "Vec::is_empty")]
4314    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4315    // both SDKs model an absent list as an empty one rather than as `null`.
4316    #[schemars(!skip_serializing_if)]
4317    pub metadata_fields: Vec<MetadataFieldReport>,
4318}
4319
4320/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
4321/// its GraphQL type when it has none — which is what a projected field's conflict reports.
4322///
4323/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
4324/// no conflict.
4325// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
4326// refuses exactly what `try_from` below does.
4327#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4328#[serde(transparent)]
4329pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
4330
4331/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
4332/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
4333/// neither SDK's validator can be relied on to support.
4334const NON_TEXT_FIELD_TYPE_PATTERN: &str =
4335    r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
4336
4337impl TryFrom<String> for NonTextFieldType {
4338    type Error = String;
4339
4340    fn try_from(kind: String) -> Result<Self, Self::Error> {
4341        if kind.is_empty() || kind.starts_with(char::is_whitespace) {
4342            return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
4343        }
4344        if kind == "TEXT" {
4345            return Err("a TEXT field is no conflict for a projected text field".to_owned());
4346        }
4347        Ok(Self(kind))
4348    }
4349}
4350
4351impl std::fmt::Display for NonTextFieldType {
4352    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4353        formatter.write_str(&self.0)
4354    }
4355}
4356
4357/// One projected metadata text field's plan, or its verified outcome.
4358///
4359/// The setup creates a missing one as a text field and never alters a field that is there:
4360/// a same-named field of another type is reported in `conflict` and left as it is.
4361#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4362pub struct MetadataFieldReport {
4363    /// The board field's name.
4364    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.
4365    /// The metadata key the value is read from.
4366    pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
4367    /// The object keys walked inside that key's value; empty for the key's own value.
4368    pub path: Vec<String>,
4369    /// Whether the board had a text field of that name before the operation.
4370    // 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.
4371    pub exists: bool,
4372    /// The type of a field of that name that is not a text field, which the setup leaves as
4373    /// it is; absent — and left out of the JSON — when there is none.
4374    #[serde(default, skip_serializing_if = "Option::is_none")]
4375    pub conflict: Option<NonTextFieldType>,
4376    /// What the requested operation did: `planned`; `created` for a field that was not
4377    /// there; `unchanged` for one that was, and for a conflict, which is never changed.
4378    pub outcome: FieldOutcome,
4379}
4380
4381/// Which options one field is configured with, in the order a new field would list them.
4382struct FieldPlan {
4383    field: BoardField,
4384    wanted: Vec<String>,
4385}
4386
4387/// One single-select field as the guarded setup snapshots it.
4388#[derive(Debug, Clone, PartialEq, Eq)]
4389struct SnapshotField {
4390    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4391    // passed back as the mutation's field identity; a newtype could enforce no stronger
4392    // invariant because GitHub publishes no grammar for it.
4393    field_id: String,
4394    options: Vec<StatusOption>,
4395}
4396
4397/// Every single-select field of a board and every item's value of each.
4398#[derive(Debug, Clone, PartialEq, Eq)]
4399struct BoardSnapshot {
4400    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4401    // passed back as the mutation's project identity; a newtype could enforce no stronger
4402    // invariant because GitHub publishes no grammar for it.
4403    board_id: String,
4404    fields: BTreeMap<BoardField, SnapshotField>,
4405    /// Each board item's id, and its value of each field this setup owns that it holds one of.
4406    items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
4407}
4408
4409impl BoardSnapshot {
4410    /// Every item's value of `field`, in board order — the recovery data a drift refusal
4411    /// carries.
4412    fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
4413        self.items
4414            .iter()
4415            .map(|(item_id, values)| StatusAssignment {
4416                item_id: item_id.clone(),
4417                option: values.get(&field).cloned(),
4418            })
4419            .collect()
4420    }
4421}
4422
4423impl GitHubProjectsSource {
4424    /// `content` as this source sends it, with the markup the engine handed it applied.
4425    fn marked(&self, content: &str) -> String {
4426        match self.body_markup.get() {
4427            Some(markup) if !content.is_empty() => markup.written(content),
4428            _ => content.to_owned(),
4429        }
4430    }
4431
4432    /// The visible body and the metadata slot of `body` as [`metadata_body`] splits them,
4433    /// the visible body without the markup [`Self::marked`] applies.
4434    fn visible(
4435        &self,
4436        body: Option<String>,
4437    ) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
4438        let (visible, slot) = metadata_body(body)?;
4439        Ok((
4440            match (visible, self.body_markup.get()) {
4441                (Some(visible), Some(markup)) => {
4442                    Some(markup.read(&visible)).filter(|read| !read.is_empty())
4443                }
4444                (visible, _) => visible,
4445            },
4446            slot,
4447        ))
4448    }
4449
4450    /// Report missing configured Status options and, when `apply` is true, add them with
4451    /// a whole-list mutation that preserves every existing id and verifies the result.
4452    ///
4453    /// # Errors
4454    ///
4455    /// Refuses a board without a single-select `Status` field. A post-write difference in
4456    /// any pre-existing option id or item assignment is refused with the complete pre-write
4457    /// assignment snapshot in the diagnostic for recovery.
4458    // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
4459    // successful mutation, both drift refusals, source selection, missing Status, casing,
4460    // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
4461    // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
4462    // responses from entering the defensive malformed-response branches below.
4463    pub async fn status_options(
4464        &self,
4465        mode: StatusOptionsMode,
4466    ) -> Result<StatusOptionsReport, SourceError> {
4467        let before = self.status_snapshot().await?;
4468        // A terminal category's option is as configured as an open one's: a terminal
4469        // write validates it before closing and refuses when the board lacks it. Both
4470        // kinds' names are options of the one field, so both are asked for.
4471        let missing = self
4472            .statuses
4473            .wanted()
4474            .into_iter()
4475            .filter(|wanted| {
4476                !before
4477                    .options
4478                    .iter()
4479                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4480            })
4481            .collect::<Vec<_>>();
4482        let report = StatusOptionsReport {
4483            source: self.name.clone(),
4484            missing: missing.clone(),
4485            outcome: match (mode, missing.is_empty()) {
4486                (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
4487                (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
4488                (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
4489            },
4490            existing: before.options.clone(),
4491        };
4492        if mode == StatusOptionsMode::Plan || missing.is_empty() {
4493            return Ok(report);
4494        }
4495        let mut options = before
4496            .options
4497            .iter()
4498            .map(|option| {
4499                json!({
4500                    "id": option.id, "name": option.name, "color": option.color,
4501                    "description": option.description,
4502                })
4503            })
4504            .collect::<Vec<_>>();
4505        options.extend(missing.iter().map(|name| {
4506            json!({
4507                "name": name, "color": "GRAY", "description": ""
4508            })
4509        }));
4510        self.graphql(
4511            graphql::STATUS_OPTIONS_UPDATE,
4512            json!({"input": {
4513                "fieldId": before.field_id,
4514                "singleSelectOptions": options,
4515            }}),
4516        )
4517        .await?;
4518        let after = self.status_snapshot().await?;
4519        let options_preserved = before
4520            .options
4521            .iter()
4522            .all(|old| after.options.iter().any(|new| new == old));
4523        let additions_present = missing.iter().all(|wanted| {
4524            after
4525                .options
4526                .iter()
4527                .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4528        });
4529        if !options_preserved || !additions_present || after.assignments != before.assignments {
4530            let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
4531                SourceError::Malformed {
4532                    message: format!("cannot render pre-write Status recovery snapshot: {error}"),
4533                }
4534            })?;
4535            return Err(SourceError::Refused {
4536                message: format!(
4537                    "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}"
4538                ),
4539            });
4540        }
4541        Ok(report)
4542    }
4543
4544    /// A fresh snapshot of the Status field and every board item's assignment of it.
4545    ///
4546    /// # Errors
4547    ///
4548    /// Refuses a board without a single-select `Status` field, and one the token cannot see.
4549    async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
4550        // Status alone, as this operation has always read it: a `Priority` field is another
4551        // operation's, so nothing about it can refuse this one.
4552        let mut board = self.board_snapshot(&[BoardField::Status]).await?;
4553        let field = board
4554            .fields
4555            .remove(&BoardField::Status)
4556            .ok_or_else(|| self.no_status_field())?;
4557        Ok(StatusSnapshot {
4558            assignments: board.assignments(BoardField::Status),
4559            board_id: board.board_id,
4560            field_id: field.field_id,
4561            options: field.options,
4562        })
4563    }
4564
4565    /// The refusal a board with no `Status` field is answered with by the guarded setup.
4566    fn no_status_field(&self) -> SourceError {
4567        SourceError::Refused {
4568            message: format!("source {} board has no Status field", self.name),
4569        }
4570    }
4571
4572    // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
4573    // the real CLI loopback journey, including pagination. The individual malformed guards
4574    // are defensive validation of a schema-pinned third-party response, not separate user
4575    // journeys; drift and missing-field failures cover the operation's recovery behavior.
4576    /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
4577    /// every board item's value of each, walked to the end of the board's items. A field not
4578    /// in `owned` is read past whatever it holds.
4579    async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
4580        let mut after: Option<String> = None;
4581        let mut snapshot: Option<BoardSnapshot> = None;
4582        loop {
4583            let data = self
4584                .graphql(
4585                    graphql::STATUS_OPTIONS_SNAPSHOT,
4586                    json!({
4587                        "owner": self.owner, "number": self.project_number,
4588                        "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
4589                    }),
4590                )
4591                .await?;
4592            let board = data
4593                .pointer("/owner/projectV2")
4594                .filter(|board| board.is_object())
4595                .ok_or_else(|| SourceError::Refused {
4596                    message: format!(
4597                        "source {} has no accessible GitHub Projects board",
4598                        self.name
4599                    ),
4600                })?;
4601            if board
4602                .pointer("/fields/pageInfo/hasNextPage")
4603                .and_then(Value::as_bool)
4604                != Some(false)
4605            {
4606                return Err(SourceError::Malformed {
4607                    message:
4608                        "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
4609                            .into(),
4610                });
4611            }
4612            let mut fields = BTreeMap::new();
4613            // Only the fields this setup owns, by name: a node the single-select fragment did not
4614            // match carries no name, and a person's own single-select field — a `Size`, a
4615            // `Team` — is none of this setup's business, so nothing about it can refuse one. A
4616            // `Status` or `Priority` field without its options is malformed, not absent.
4617            // 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.
4618            for (owned, field) in board
4619                .pointer("/fields/nodes")
4620                .and_then(Value::as_array)
4621                .ok_or_else(|| SourceError::Malformed {
4622                    message: "GitHub project fields.nodes is not an array".into(),
4623                })?
4624                .iter()
4625                .filter_map(|field| {
4626                    let named = BoardField::named(field.get("name")?.as_str()?)?;
4627                    owned.contains(&named).then_some((named, field))
4628                })
4629            {
4630                let options = field
4631                    .get("options")
4632                    .and_then(Value::as_array)
4633                    .ok_or_else(|| SourceError::Malformed {
4634                        message: "GitHub single-select field options is not an array".into(),
4635                    })?
4636                    .iter()
4637                    .map(|option| {
4638                        Ok(StatusOption {
4639                            id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
4640                                .map_err(|message| SourceError::Malformed { message })?,
4641                            name: ColumnName::try_from(required_str(option, "name")?.to_owned())
4642                                .map_err(|message| SourceError::Malformed {
4643                                    message: format!(
4644                                        "GitHub single-select option name is invalid: {message}"
4645                                    ),
4646                                })?,
4647                            color: serde_json::from_value(
4648                                option.get("color").cloned().unwrap_or(Value::Null),
4649                            )
4650                            .map_err(|error| {
4651                                SourceError::Malformed {
4652                                    message: format!(
4653                                        "GitHub single-select option color is invalid: {error}"
4654                                    ),
4655                                }
4656                            })?,
4657                            description: optional_str(option, "description")?
4658                                .unwrap_or_default()
4659                                .to_owned(),
4660                        })
4661                    })
4662                    .collect::<Result<Vec<_>, SourceError>>()?;
4663                let snapshot = SnapshotField {
4664                    field_id: required_nonblank_str(field, "id")?.to_owned(),
4665                    options,
4666                };
4667                // A board's field names are unique, so a second one is an answer that cannot
4668                // say which field the setup would act on — refused rather than one chosen.
4669                if fields.insert(owned, snapshot).is_some() {
4670                    return Err(SourceError::Malformed {
4671                        message: format!(
4672                            "GitHub answered two {} fields for this board",
4673                            owned.name()
4674                        ),
4675                    });
4676                }
4677            }
4678            let board_id = required_nonblank_str(board, "id")?.to_owned();
4679            let current = snapshot.get_or_insert_with(|| BoardSnapshot {
4680                board_id,
4681                fields,
4682                items: Vec::new(),
4683            });
4684            let items = board
4685                .pointer("/items/nodes")
4686                .and_then(Value::as_array)
4687                .ok_or_else(|| SourceError::Malformed {
4688                    message: "GitHub project items.nodes is not an array".into(),
4689                })?;
4690            for item in items {
4691                let field_values =
4692                    item.get("fieldValues")
4693                        .ok_or_else(|| SourceError::Malformed {
4694                            message: "GitHub project item is missing fieldValues".into(),
4695                        })?;
4696                if field_values
4697                    .pointer("/pageInfo/hasNextPage")
4698                    .and_then(Value::as_bool)
4699                    != Some(false)
4700                {
4701                    return Err(SourceError::Malformed {
4702                        message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
4703                    });
4704                }
4705                let values = item
4706                    .pointer("/fieldValues/nodes")
4707                    .and_then(Value::as_array)
4708                    .ok_or_else(|| SourceError::Malformed {
4709                        message: "GitHub project item fieldValues.nodes is not an array".into(),
4710                    })?;
4711                let item_id = required_nonblank_str(item, "id")?;
4712                let mut assigned = BTreeMap::new();
4713                for value in values {
4714                    let Some(field) = value
4715                        .pointer("/field/name")
4716                        .and_then(Value::as_str)
4717                        .and_then(BoardField::named)
4718                        .filter(|field| owned.contains(field))
4719                    else {
4720                        continue;
4721                    };
4722                    let held = assigned.insert(
4723                        field,
4724                        AssignedStatusOption {
4725                            id: StatusOptionId::try_from(
4726                                required_str(value, "optionId")?.to_owned(),
4727                            )
4728                            .map_err(|message| SourceError::Malformed { message })?,
4729                            name: ColumnName::try_from(required_str(value, "name")?.to_owned())
4730                                .map_err(|message| SourceError::Malformed {
4731                                    message: format!(
4732                                        "GitHub assigned {} name is invalid: {message}",
4733                                        field.name()
4734                                    ),
4735                                })?,
4736                        },
4737                    );
4738                    // An item holds one value of a field, so a second one leaves no way to
4739                    // tell which it holds — and a verification or recovery built on either
4740                    // could restore the wrong one.
4741                    if held.is_some() {
4742                        return Err(SourceError::Malformed {
4743                            message: format!(
4744                                "GitHub answered two {} values for board item {item_id}",
4745                                field.name()
4746                            ),
4747                        });
4748                    }
4749                }
4750                current.items.push((item_id.to_owned(), assigned));
4751            }
4752            let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4753                message: "GitHub project is missing items".into(),
4754            })?;
4755            let has_next = page
4756                .pointer("/pageInfo/hasNextPage")
4757                .and_then(Value::as_bool)
4758                .ok_or_else(|| SourceError::Malformed {
4759                    message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4760                })?;
4761            if !has_next {
4762                break;
4763            }
4764            let next =
4765                required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4766            validate_cursor_progress(after.as_deref(), next)?;
4767            after = Some(next.to_owned());
4768        }
4769        snapshot.ok_or_else(|| SourceError::Malformed {
4770            message: "GitHub returned no board field snapshot".into(),
4771        })
4772    }
4773    // llmlint: ignore-end[changed_behavior_has_e2e]
4774
4775    /// Report every board field this source's configuration names and, with
4776    /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4777    /// the `Priority` field when the board has none.
4778    ///
4779    /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4780    /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4781    /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4782    /// color and description: the whole option list goes back with every existing id, because
4783    /// a re-minted id clears every item's value.
4784    ///
4785    /// # Errors
4786    ///
4787    /// Refuses a board without a single-select `Status` field. After an apply the board is
4788    /// read again, and a pre-existing option or any item's value of either field that moved is
4789    /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4790    // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4791    // unchanged apply, a created field, an added option to each field, drift refusal, a board
4792    // with no Status field and a non-github-projects source through the compiled CLI against
4793    // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4794    pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4795        let owned: Vec<BoardField> = if self.priorities.is_some() {
4796            vec![BoardField::Status, BoardField::Priority]
4797        } else {
4798            vec![BoardField::Status]
4799        };
4800        let before = self.board_snapshot(&owned).await?;
4801        let mut plans = vec![FieldPlan {
4802            field: BoardField::Status,
4803            wanted: self.statuses.wanted(),
4804        }];
4805        if !before.fields.contains_key(&BoardField::Status) {
4806            return Err(self.no_status_field());
4807        }
4808        if let Some(mapping) = &self.priorities {
4809            plans.push(FieldPlan {
4810                field: BoardField::Priority,
4811                wanted: mapping.names().map(str::to_owned).collect(),
4812            });
4813        }
4814        // The snapshot reads single-select fields alone, so a field it did not find may still
4815        // be on the board under the name, of another type: creating one beside it would fail
4816        // part way, or leave two fields of one name. Asked of the board's own field list, and
4817        // only when a field is missing.
4818        if plans
4819            .iter()
4820            .any(|plan| !before.fields.contains_key(&plan.field))
4821        {
4822            let board = self.board_fields().await?;
4823            for plan in plans
4824                .iter()
4825                .filter(|plan| !before.fields.contains_key(&plan.field))
4826            {
4827                if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4828                    return Err(SourceError::Refused {
4829                        message: format!(
4830                            "source {}'s board has a {} field that is not a single-select field \
4831                             (it is a {}), so it cannot hold this source's options; next: rename \
4832                             or remove that field, then run this again",
4833                            self.name,
4834                            plan.field.name(),
4835                            optional_str(field, "__typename")?.unwrap_or("field of another type")
4836                        ),
4837                    });
4838                }
4839            }
4840        }
4841        let mut reports = Vec::new();
4842        for plan in &plans {
4843            let held = before.fields.get(&plan.field);
4844            let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4845            let mut missing: Vec<String> = Vec::new();
4846            for wanted in &plan.wanted {
4847                let present = existing
4848                    .iter()
4849                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4850                    || missing
4851                        .iter()
4852                        .any(|named| named.eq_ignore_ascii_case(wanted));
4853                if !present {
4854                    missing.push(wanted.clone());
4855                }
4856            }
4857            let kinds = match plan.field {
4858                BoardField::Status => self.statuses.missing_by_kind(&existing),
4859                BoardField::Priority => Vec::new(),
4860            };
4861            reports.push(FieldReport {
4862                field: plan.field,
4863                exists: held.is_some(),
4864                kinds,
4865                outcome: match (mode, held.is_some(), missing.is_empty()) {
4866                    (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4867                    (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4868                    (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4869                    (SetupMode::Apply, false, _) => FieldOutcome::Created,
4870                },
4871                missing,
4872                existing,
4873            });
4874        }
4875        // A projected field is a text field, which the single-select snapshot cannot see, so
4876        // it is asked of the board's own field list — and only when one is configured.
4877        let mut metadata_fields = Vec::new();
4878        if !self.metadata_fields.is_empty() {
4879            let board = self.board_fields().await?;
4880            for projection in &self.metadata_fields {
4881                let conflict = match Board::field(&board.fields, &projection.field)? {
4882                    None => None,
4883                    Some(field) => {
4884                        let typename = optional_str(field, "__typename")?;
4885                        let data_type = optional_str(field, "dataType")?;
4886                        if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4887                            None
4888                        } else {
4889                            let named = data_type
4890                                .filter(|kind| *kind != "TEXT")
4891                                .or(typename)
4892                                .unwrap_or("field of another type");
4893                            Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4894                                |message| SourceError::Malformed {
4895                                    message: format!(
4896                                        "GitHub answered the board's {} field with a type this \
4897                                         setup cannot read: {message}",
4898                                        &*projection.field
4899                                    ),
4900                                },
4901                            )?)
4902                        }
4903                    }
4904                };
4905                let exists =
4906                    conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4907                metadata_fields.push(MetadataFieldReport {
4908                    field: projection.field.to_string(),
4909                    key: projection.key.to_string(),
4910                    path: projection.path.clone(),
4911                    exists,
4912                    outcome: match (mode, exists || conflict.is_some()) {
4913                        (SetupMode::Plan, _) => FieldOutcome::Planned,
4914                        (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4915                        (SetupMode::Apply, false) => FieldOutcome::Created,
4916                    },
4917                    conflict,
4918                });
4919            }
4920        }
4921        let report = FieldsReport {
4922            source: self.name.clone(),
4923            fields: reports,
4924            metadata_fields,
4925        };
4926        let writes: Vec<&FieldReport> = report
4927            .fields
4928            .iter()
4929            .filter(|field| !field.missing.is_empty() || !field.exists)
4930            .collect();
4931        let creates: Vec<&MetadataFieldReport> = report
4932            .metadata_fields
4933            .iter()
4934            .filter(|field| field.outcome == FieldOutcome::Created)
4935            .collect();
4936        if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4937            return Ok(report);
4938        }
4939        let mut landed: Vec<&str> = Vec::new();
4940        for field in &writes {
4941            let added = field
4942                .missing
4943                .iter()
4944                .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4945            let sent = match before.fields.get(&field.field) {
4946                Some(held) => {
4947                    let mut options = held
4948                        .options
4949                        .iter()
4950                        .map(|option| {
4951                            json!({
4952                                "id": option.id, "name": option.name, "color": option.color,
4953                                "description": option.description,
4954                            })
4955                        })
4956                        .collect::<Vec<_>>();
4957                    options.extend(added);
4958                    self.graphql(
4959                        graphql::STATUS_OPTIONS_UPDATE,
4960                        json!({"input": {
4961                            "fieldId": held.field_id,
4962                            "singleSelectOptions": options,
4963                        }}),
4964                    )
4965                    .await
4966                }
4967                None => {
4968                    self.graphql(
4969                        graphql::CREATE_FIELD,
4970                        json!({"input": {
4971                            "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4972                            "name": field.field.name(),
4973                            "singleSelectOptions": added.collect::<Vec<_>>(),
4974                        }}),
4975                    )
4976                    .await
4977                }
4978            };
4979            // A mutation that failed does not establish that GitHub left its field as it was,
4980            // so every failure from here on carries the recovery data a drift refusal does.
4981            match sent {
4982                Ok(_) => landed.push(field.field.name()),
4983                Err(error) => {
4984                    let changed = if landed.is_empty() {
4985                        String::new()
4986                    } else {
4987                        format!("changed the {} field and then ", landed.join(" and "))
4988                    };
4989                    return Err(SourceError::Refused {
4990                        message: format!(
4991                            "the guarded field setup {changed}failed on the {} field, which it may \
4992                             have changed part way: {error}; the pre-write item assignments \
4993                             are:\n{}",
4994                            field.field.name(),
4995                            recovery(&report, &before)?
4996                        ),
4997                    });
4998                }
4999            }
5000        }
5001        for field in &creates {
5002            let sent = self
5003                .graphql(
5004                    graphql::CREATE_FIELD,
5005                    json!({"input": {
5006                        "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
5007                    }}),
5008                )
5009                .await;
5010            if let Err(error) = sent {
5011                let changed = if landed.is_empty() {
5012                    String::new()
5013                } else {
5014                    format!("changed the {} field and then ", landed.join(" and "))
5015                };
5016                return Err(SourceError::Refused {
5017                    message: format!(
5018                        "the guarded field setup {changed}failed creating the {} text field: \
5019                         {error}; run it again to create what is still missing",
5020                        field.field
5021                    ),
5022                });
5023            }
5024            landed.push(&field.field);
5025        }
5026        // The board has been written, so a verification read that fails leaves it unverified
5027        // rather than unchanged, and says what to put back.
5028        let after = match self.board_snapshot(&owned).await {
5029            Ok(after) => after,
5030            Err(error) => {
5031                return Err(SourceError::Refused {
5032                    message: format!(
5033                        "the guarded field setup changed the {} field and then could not read the \
5034                         board back to verify it: {error}; the pre-write item assignments are:\n{}",
5035                        landed.join(" and "),
5036                        recovery(&report, &before)?
5037                    ),
5038                });
5039            }
5040        };
5041        let mut moved = Vec::new();
5042        for field in &report.fields {
5043            let name = field.field.name();
5044            let now = after
5045                .fields
5046                .get(&field.field)
5047                .map(|held| held.options.as_slice())
5048                .unwrap_or_default();
5049            if !field.existing.iter().all(|old| now.contains(old)) {
5050                moved.push(format!(
5051                    "a pre-existing {name} option id, name, color or description"
5052                ));
5053            }
5054            if !field.missing.iter().all(|wanted| {
5055                now.iter()
5056                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
5057            }) {
5058                moved.push(format!("an added {name} option"));
5059            }
5060            if after.assignments(field.field) != before.assignments(field.field) {
5061                moved.push(format!("an item's {name} value"));
5062            }
5063        }
5064        if !creates.is_empty() {
5065            // Read afresh: the view held for this command is the one from before the creates.
5066            *self.fields_cache()? = None;
5067            let board = match self.board_fields().await {
5068                Ok(board) => board,
5069                Err(error) => {
5070                    return Err(SourceError::Refused {
5071                        message: format!(
5072                            "the guarded field setup changed the {} field and then could not \
5073                             read the board back to verify it: {error}; run it again to verify \
5074                             it, which creates nothing a second time",
5075                            landed.join(" and ")
5076                        ),
5077                    });
5078                }
5079            };
5080            for field in &creates {
5081                let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
5082                    held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
5083                        && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
5084                });
5085                if !text {
5086                    moved.push(format!("the created {} text field", field.field));
5087                }
5088            }
5089        }
5090        if !moved.is_empty() {
5091            return Err(SourceError::Refused {
5092                message: format!(
5093                    "GitHub changed {} after the guarded field setup; the pre-write item \
5094                     assignments are:\n{}",
5095                    moved.join(", "),
5096                    recovery(&report, &before)?
5097                ),
5098            });
5099        }
5100        Ok(report)
5101    }
5102
5103    /// Validate configuration and capture the named credential without exposing it.
5104    ///
5105    /// # Errors
5106    ///
5107    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
5108    /// [`SourceError::Auth`] when the named credential is missing or empty.
5109    pub fn new(
5110        name: &SourceName,
5111        config: GitHubProjectsConfig,
5112        secrets: &dyn SecretResolver,
5113    ) -> Result<Self, SourceError> {
5114        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
5115    }
5116
5117    /// The same, recording every request it sends into an accounting the caller holds too.
5118    ///
5119    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
5120    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
5121    /// up — passes the one it records those into, so the session total accounts for the
5122    /// whole session rather than for this source's share of it.
5123    ///
5124    /// # Errors
5125    ///
5126    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
5127    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
5128    pub fn recording_into(
5129        name: &SourceName,
5130        config: GitHubProjectsConfig,
5131        secrets: &dyn SecretResolver,
5132        ledger: Arc<Accounting>,
5133    ) -> Result<Self, SourceError> {
5134        if !valid_github_owner(&config.owner) {
5135            return Err(SourceError::Config {
5136                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
5137            });
5138        }
5139        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
5140            return Err(SourceError::Config {
5141                message: format!("project_number must be between 1 and {}", i32::MAX),
5142            });
5143        }
5144        if !valid_environment_name(&config.token_env) {
5145            return Err(SourceError::Config {
5146                message: "token_env must be a valid environment-variable name".into(),
5147            });
5148        }
5149        let repository = config
5150            .repository
5151            .as_deref()
5152            .map(RepositoryTarget::parse)
5153            .transpose()?;
5154        if let Some(entry) = config
5155            .creation_owners
5156            .iter()
5157            .find(|entry| !valid_github_owner(entry))
5158        {
5159            return Err(SourceError::Config {
5160                message: format!(
5161                    "creation_owners entry {entry:?} is not a GitHub login: a login is 1-39 ASCII \
5162                     letters, digits, or single hyphens, and cannot start or end with a hyphen"
5163                ),
5164            });
5165        }
5166        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
5167            message: format!("endpoint is not a valid URL: {e}"),
5168        })?;
5169        if endpoint.scheme() != "https"
5170            && !(endpoint.scheme() == "http"
5171                && endpoint
5172                    .host_str()
5173                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
5174        {
5175            return Err(SourceError::Config {
5176                message:
5177                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
5178                        .into(),
5179            });
5180        }
5181        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
5182            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),
5183        })?;
5184        Ok(Self {
5185            name: name.clone(),
5186            owner: config.owner,
5187            project_number: config.project_number,
5188            repository,
5189            creation_owners: config.creation_owners,
5190            asset_client: std::sync::OnceLock::new(),
5191            body_markup: std::sync::OnceLock::new(),
5192            endpoint,
5193            token,
5194            credential_name: config.token_env,
5195            statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
5196            priorities: config
5197                .priority_mapping
5198                .map(|mapping| PriorityMapping::resolve(mapping, name))
5199                .transpose()?,
5200            metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
5201            client: std::sync::OnceLock::new(),
5202            created: Mutex::new(Vec::new()),
5203            updated: Mutex::new(Vec::new()),
5204            commented: Mutex::new(Vec::new()),
5205            pacing: Pacing::resolve(config.pacing, name)?,
5206            last_mutation: Mutex::new(None),
5207            last_served: Mutex::new(None),
5208            windows: Mutex::new(pacing::Windows::default()),
5209            task_children: Mutex::new(BTreeMap::new()),
5210            written: Mutex::new(BTreeMap::new()),
5211            deferred: Mutex::new(None),
5212            held_updates: Mutex::new(Vec::new()),
5213            clock: system_clock(),
5214            declared_private: std::sync::atomic::AtomicBool::new(false),
5215            numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
5216            board_cache: Mutex::new(None),
5217            search_cache: Mutex::new(None),
5218            narrowed_cache: Mutex::new(BTreeMap::new()),
5219            resolved_cache: Mutex::new(BTreeMap::new()),
5220            read_ahead: Mutex::new(BTreeMap::new()),
5221            pending_origins: Mutex::new(Vec::new()),
5222            member_classes: Mutex::new(BTreeMap::new()),
5223            carried: Mutex::new(BTreeMap::new()),
5224            held_cache: Mutex::new(BTreeMap::new()),
5225            children_cache: Mutex::new(BTreeMap::new()),
5226            search_next: Mutex::new(BTreeMap::new()),
5227            fields_cache: Mutex::new(None),
5228            repository_cache: Mutex::new(BTreeMap::new()),
5229            ledger,
5230            expected: expected::Held::default(),
5231            projection: projection::Held::default(),
5232        })
5233    }
5234
5235    /// The client every API request goes through, built on the first one.
5236    ///
5237    /// Not with the source: building a client loads the system's certificate store, which
5238    /// costs tens of milliseconds, and a command that configures this board but never sends to
5239    /// it — a read of another source — should not pay that in its process.
5240    fn client(&self) -> Result<&Client, SourceError> {
5241        built(&self.client, || {
5242            Client::builder()
5243                .user_agent("onetaskgraph")
5244                .build()
5245                .map_err(|e| SourceError::Config {
5246                    message: format!("cannot build HTTP client: {e}"),
5247                })
5248        })
5249    }
5250
5251    /// The client attachment requests go through, with its own redirect policy, built on the
5252    /// first one for the reason [`Self::client`] gives.
5253    fn asset_client(&self) -> Result<&Client, SourceError> {
5254        built(&self.asset_client, || assets::client(&self.endpoint))
5255    }
5256
5257    /// A snapshot of every request this source has sent, and what each cost.
5258    ///
5259    /// A value to hold and compare rather than a borrow of the accounting itself, so two
5260    /// of them can sit side by side. When this source was built with
5261    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
5262    /// point of building it that way.
5263    #[must_use]
5264    pub fn accounting(&self) -> accounting::Session {
5265        self.ledger.snapshot()
5266    }
5267
5268    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
5269    /// rate limit rather than handing it straight back as an error.
5270    ///
5271    /// Retrying is safe for every document here, including the mutations, and the reason
5272    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
5273    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
5274    /// this replays has already taken effect. An outcome this source cannot know — the
5275    /// send failed, or the body could not be read, so the mutation may well have landed —
5276    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
5277    /// attempt. A duplicate write would come from replaying one of those, and none is
5278    /// replayed.
5279    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5280        // Updates held back go out before any other request, so whatever it reads or writes
5281        // sees the board as though each had been sent when it was made.
5282        Box::pin(self.send_held_updates()).await?;
5283        // Origins held back for a read ahead that never came are sent before anything else.
5284        if query != graphql::ORIGIN_LOOKUPS && query != graphql::ORIGIN_LOOKUPS_WITH_MEMBERS {
5285            Box::pin(self.send_pending_origins(None)).await?;
5286        }
5287        if !self.expected.journaling() {
5288            return self.graphql_sent(query, variables).await;
5289        }
5290        // While a write the store's cache expects runs, its mutations select the whole issue
5291        // they leave behind, so the response vouches for what the cache keeps — and every
5292        // request is recorded, so the write's parts are reported from their own responses.
5293        let (query, variables) = match graphql::whole_twin(query) {
5294            Some(twin) => (twin, expected::whole_variables(variables)),
5295            None => (query, variables),
5296        };
5297        let sent = self.graphql_sent(query, variables.clone()).await;
5298        self.journal(query, &variables, &sent).await;
5299        sent
5300    }
5301
5302    /// One document sent, retried while GitHub asks for a wait, exactly as asked.
5303    async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5304        if is_mutation(query)
5305            && ![
5306                graphql::ADD_COMMENT,
5307                graphql::UPDATE_COMMENT,
5308                graphql::DELETE_COMMENT,
5309                graphql::ADD_COMMENT_WHOLE,
5310                graphql::UPDATE_COMMENT_WHOLE,
5311            ]
5312            .contains(&query)
5313        {
5314            let mut cache = self.resolved_cache()?;
5315            let mut ahead = self.read_ahead()?;
5316            let mut carried = self.carried()?;
5317            for argument in ["input", "second", "third", "clear"] {
5318                if let Some(input) = variables.get(argument) {
5319                    let names = |id: &NativeId, item_id: Option<&str>| {
5320                        ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
5321                            input
5322                                .get(key)
5323                                .and_then(Value::as_str)
5324                                .is_some_and(|value| value == id.0 || Some(value) == item_id)
5325                        })
5326                    };
5327                    cache.retain(|id, item| !names(id, Some(&item.item_id)));
5328                    ahead.retain(|id, item| {
5329                        !names(id, item.as_ref().map(|item| item.item_id.as_str()))
5330                    });
5331                    carried.retain(|id, item| !names(id, Some(&item.item_id)));
5332                }
5333            }
5334        }
5335        let doing = operation_description(query);
5336        let mutation = is_mutation(query);
5337        let charge = pacing::Charge::graphql(query, &variables);
5338        let mut waited = Duration::ZERO;
5339        let mut waits = 0_u32;
5340        let mut backoff = self.pacing.retry_backoff;
5341        loop {
5342            let ticket = self.pace(charge, mutation).await;
5343            // Every write but a create is asked for immediately before it is sent — a create's
5344            // fields, its relationships, its content and the delete that takes it back included
5345            // — so a copy that owns the create renews its hold first, and one that has lost it
5346            // sends nothing more. Asked again before a retry, which is sent later.
5347            if mutation
5348                && !query.contains("createIssue(")
5349                && let Err(refused) = <dyn onetaskgraph_plugin_api::WriteGuard>::check().await
5350            {
5351                self.paced(ticket, mutation);
5352                return Err(refused);
5353            }
5354            let attempt = self.send_once(query, &variables, waits + 1).await;
5355            self.paced(ticket, mutation);
5356            let remaining = self.pacing.retry_budget.saturating_sub(waited);
5357            let (wait, cause) = match attempt {
5358                Ok(data) => return Ok(data),
5359                Err(Attempt::Failed(error)) => return Err(error),
5360                Err(Attempt::Conflicted(said)) => {
5361                    // A create is never replayed: see `CONFLICT_WORDINGS`.
5362                    if query.contains("createIssue(") || backoff.is_zero() || backoff > remaining {
5363                        return Err(SourceError::Unavailable {
5364                            message: format!(
5365                                "GitHub refused this source while {doing} as a temporary \
5366                                 conflict ({said}); it retried {} over {} and the conflict \
5367                                 did not clear within the {} one call may spend waiting. \
5368                                 next: run the command again once nothing else is writing to \
5369                                 the same items",
5370                                plural(waits, "time"),
5371                                seconds(waited),
5372                                seconds(self.pacing.retry_budget),
5373                            ),
5374                        });
5375                    }
5376                    (backoff, pacing::cause::BACKOFF)
5377                }
5378                Err(Attempt::Limited(limited)) => {
5379                    let (wait, cause) = limited.wait(backoff);
5380                    // A wait of nothing spends none of the budget, so it is exhaustion rather
5381                    // than a retry. `Pacing::resolve` rules out every way of configuring one
5382                    // except a budget of zero, where reporting the first refusal is the ask.
5383                    if wait.is_zero() || wait > remaining {
5384                        return Err(limited.exhausted(
5385                            doing,
5386                            waits,
5387                            waited,
5388                            wait,
5389                            self.pacing.retry_budget,
5390                        ));
5391                    }
5392                    (wait, cause)
5393                }
5394            };
5395            self.clock.sleep_because(wait, cause).await;
5396            waited += wait;
5397            waits += 1;
5398            backoff = backoff.saturating_mul(2);
5399        }
5400    }
5401
5402    /// Hold one request charging `charge` until every secondary window it charges has room for
5403    /// the whole of it — and, for a mutation of an instance configured with
5404    /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
5405    /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
5406    ///
5407    /// The moment is decided and counted under the lock and waited out outside it, so two
5408    /// callers take two places rather than the same one, and no lock is held across an await.
5409    /// A request with room goes out at once: nothing here waits that a window does not ask for.
5410    async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
5411        let (wait, cause, ticket) = {
5412            // A poisoned lock here costs pacing, not correctness, and refusing the write over
5413            // it would turn an earlier failure into a second one for no gain.
5414            let mut windows = self
5415                .windows
5416                .lock()
5417                .unwrap_or_else(std::sync::PoisonError::into_inner);
5418            let now = self.clock.now();
5419            let (mut at, mut cause) = if self.pacing.windows {
5420                windows.room(now, charge)
5421            } else {
5422                (now, None)
5423            };
5424            if mutation && !self.pacing.min_mutation_interval.is_zero() {
5425                let mut last = self
5426                    .last_mutation
5427                    .lock()
5428                    .unwrap_or_else(std::sync::PoisonError::into_inner);
5429                // `checked_add` rather than `+`: adding durations can panic on overflow, and
5430                // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
5431                if let Some(earliest) = last
5432                    .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
5433                    && earliest > at
5434                {
5435                    at = earliest;
5436                    cause = Some(pacing::cause::MIN_INTERVAL);
5437                }
5438                *last = Some(at);
5439            }
5440            let ticket = windows.charge(at, charge);
5441            (at.saturating_sub(now), cause, ticket)
5442        };
5443        if let (false, Some(cause)) = (wait.is_zero(), cause) {
5444            self.clock.sleep_because(wait, cause).await;
5445        }
5446        ticket
5447    }
5448
5449    /// Record that the request `ticket` was charged for has finished, so each window — and
5450    /// the configured spacing — counts it from here rather than from the moment it left.
5451    ///
5452    /// This source can only choose when a request *departs*; GitHub's limiter counts when it
5453    /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
5454    /// departure would hand the limiter a window that looked roomier than it is, so a source
5455    /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
5456    /// paced well inside a board's threshold was once refused by it on a slower machine while
5457    /// passing on a quick one. The previous request had arrived before its response came back,
5458    /// so counting from here is never earlier than the limiter does, whatever transit costs
5459    /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
5460    /// reads for the next fifty minutes.
5461    ///
5462    /// A failed attempt is finished too: a request refused by the limiter still arrived, and
5463    /// one that never left costs only room nobody needed.
5464    fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
5465        let now = self.clock.now();
5466        self.windows
5467            .lock()
5468            .unwrap_or_else(std::sync::PoisonError::into_inner)
5469            .finish(ticket, now);
5470        if mutation && !self.pacing.min_mutation_interval.is_zero() {
5471            let mut last = self
5472                .last_mutation
5473                .lock()
5474                .unwrap_or_else(std::sync::PoisonError::into_inner);
5475            // `max` rather than an assignment: a concurrent caller may already have reserved a
5476            // slot further out, and completing this request must never pull that slot back in.
5477            *last = Some(last.map_or(now, |reserved| reserved.max(now)));
5478        }
5479    }
5480
5481    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
5482    /// failure that waiting cannot help — and recorded, whichever of the three it was.
5483    ///
5484    /// This is the one place a request leaves this crate, which is why the accounting is
5485    /// here rather than at each of the callers: a read path added later is counted without
5486    /// anybody remembering to count it, and
5487    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
5488    /// when one is not.
5489    async fn send_once(
5490        &self,
5491        query: &str,
5492        variables: &Value,
5493        number: u32,
5494    ) -> Result<Value, Attempt> {
5495        let executed = self.ledger.priced_document(query);
5496        let Attempted {
5497            result,
5498            limits,
5499            reported_cost,
5500            response_outcome,
5501        } = self.attempt(executed.as_ref(), variables).await;
5502        // No `otherwise` name: every document this source sends is one of its own, and the
5503        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
5504        let sending = accounting::Request::graphql(query, variables, None, reported_cost);
5505        let outcome = match &result {
5506            Ok(_) => accounting::Outcome::Answered,
5507            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
5508            Err(Attempt::Failed(_) | Attempt::Conflicted(_)) => accounting::Outcome::Refused,
5509        };
5510        self.ledger
5511            .record(sending.finished(outcome, limits).with_execution(
5512                Some(executed.into_owned()),
5513                response_outcome,
5514                number,
5515            ));
5516        result
5517    }
5518
5519    /// The attempt itself, with what its response said about the rate limit alongside.
5520    ///
5521    /// The two are returned together rather than recorded here because every one of the
5522    /// early exits below is a different outcome, and a record written at each of them is a
5523    /// record one of them can be added without.
5524    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
5525        let mut limits = accounting::RateLimit::default();
5526        let mut reported_cost = None;
5527        let mut response_outcome = accounting::ResponseOutcome::TransportError;
5528        let result = self
5529            .attempted(
5530                query,
5531                variables,
5532                &mut limits,
5533                &mut reported_cost,
5534                &mut response_outcome,
5535            )
5536            .await;
5537        Attempted {
5538            result,
5539            limits,
5540            reported_cost,
5541            response_outcome,
5542        }
5543    }
5544
5545    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
5546    async fn attempted(
5547        &self,
5548        query: &str,
5549        variables: &Value,
5550        limits: &mut accounting::RateLimit,
5551        reported_cost: &mut Option<u64>,
5552        response_outcome: &mut accounting::ResponseOutcome,
5553    ) -> Result<Value, Attempt> {
5554        let response = self
5555            .client()
5556            .map_err(Attempt::Failed)?
5557            .post(self.endpoint.clone())
5558            .bearer_auth(self.token.expose_secret())
5559            .json(&json!({"query": query, "variables": variables}))
5560            .send()
5561            .await
5562            .map_err(|e| {
5563                Attempt::Failed(SourceError::Unavailable {
5564                    message: format!("GitHub GraphQL request failed: {}", transport_error(&e)),
5565                })
5566            })?;
5567        let status = response.status();
5568        if !status.is_success() {
5569            *response_outcome = accounting::ResponseOutcome::HttpError;
5570        }
5571        let header = |name: &str| whole_seconds(response.headers().get(name));
5572        let served = response
5573            .headers()
5574            .get(reqwest::header::DATE)
5575            .and_then(|value| value.to_str().ok())
5576            .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
5577            .map(|at| at.with_timezone(&Utc));
5578        *limits = accounting::RateLimit::read(|name| {
5579            response
5580                .headers()
5581                .get(name)
5582                .and_then(|value| value.to_str().ok())
5583                .map(str::to_owned)
5584        });
5585        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
5586        // that are not text at all — is "not known to be exhausted". This never makes a
5587        // response a refusal on its own: it says which limiter a refusal is attributed to
5588        // and where its hint comes from, so a value this cannot read costs a hint rather
5589        // than an answer.
5590        let exhausted = response
5591            .headers()
5592            .get("x-ratelimit-remaining")
5593            .and_then(|value| value.to_str().ok())
5594            == Some("0");
5595        // `retry-after` is what GitHub asks for when it asks; when it does not and the
5596        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
5597        // which is the same question answered as an absolute time. Nothing else here is a
5598        // hint, and a schedule is what answers a refusal that carries none.
5599        let retry_after = header("retry-after");
5600        let hinted_by = if retry_after.is_some() || !exhausted {
5601            Hint::RetryAfter
5602        } else {
5603            Hint::PrimaryReset
5604        };
5605        let hint = retry_after.or_else(|| {
5606            exhausted
5607                .then(|| header("x-ratelimit-reset"))
5608                .flatten()
5609                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
5610        });
5611        // Read before it is parsed, because the evidence which tells a secondary rate
5612        // limit from a rejected credential is in the body of a response whose status says
5613        // only "forbidden" — and a non-success response was never parsed at all.
5614        let body = response.text().await.map_err(|e| {
5615            Attempt::Failed(SourceError::Unavailable {
5616                message: format!(
5617                    "GitHub GraphQL response could not be read: {}",
5618                    transport_error(&e)
5619                ),
5620            })
5621        })?;
5622        *reported_cost = serde_json::from_str::<Value>(&body)
5623            .ok()
5624            .as_ref()
5625            .and_then(|body| body.pointer("/data/rateLimit/cost"))
5626            .and_then(Value::as_u64);
5627        let parsed = serde_json::from_str::<Value>(&body).ok();
5628        *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
5629        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
5630            return Err(Attempt::Limited(Limited {
5631                limiter,
5632                hint,
5633                hinted_by,
5634            }));
5635        }
5636        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
5637            return Err(Attempt::Failed(SourceError::Auth {
5638                message: format!(
5639                    "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"
5640                ),
5641            }));
5642        }
5643        if !status.is_success() {
5644            return Err(Attempt::Failed(SourceError::Unavailable {
5645                message: format!("GitHub GraphQL returned HTTP {status}"),
5646            }));
5647        }
5648        let said = refusal_wording(status, &body);
5649        let normalized = said.to_ascii_lowercase();
5650        if CONFLICT_WORDINGS
5651            .iter()
5652            .any(|wording| normalized.contains(wording))
5653        {
5654            return Err(Attempt::Conflicted(said));
5655        }
5656        let answered = if query == graphql::ISSUES_BY_REFERENCE {
5657            self.answer_unresolved(&body)
5658        } else if query == graphql::CREATE_ISSUES {
5659            answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
5660        } else {
5661            self.answer(&body)
5662        };
5663        let mut data = answered.map_err(Attempt::Failed)?;
5664        if let Some(served) = served {
5665            *self
5666                .last_served
5667                .lock()
5668                .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(served);
5669            stamp_served(&mut data, &served.to_rfc3339());
5670        }
5671        Ok(data)
5672    }
5673
5674    /// What one successful HTTP response says, once its GraphQL errors are read.
5675    /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
5676    /// for this credential — a repository it cannot see, an issue number it does not hold —
5677    /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
5678    /// that reference rather than a failure of the others. Any other error refuses the read,
5679    /// as there.
5680    fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
5681        let parsed: Option<Value> = serde_json::from_str(body).ok();
5682        // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
5683        // `issue`, and the answer holds `null` at exactly that path: anything else — another
5684        // kind of error, one naming no slot, or one beside a value it says is not there — is a
5685        // refusal of the whole read.
5686        let unresolved = |data: &Value, error: &Value| {
5687            error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
5688                && error
5689                    .get("path")
5690                    .and_then(Value::as_array)
5691                    .is_some_and(|path| {
5692                        let alias = path.first().and_then(Value::as_str).and_then(|alias| {
5693                            alias
5694                                .strip_prefix('r')
5695                                .and_then(|slot| slot.parse::<usize>().ok())
5696                                .filter(|slot| *slot < REFERENCE_BATCH)
5697                                .map(|_| alias)
5698                        });
5699                        match (alias, path.get(1).and_then(Value::as_str), path.len()) {
5700                            (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
5701                            (Some(alias), Some("issue"), 2) => data
5702                                .get(alias)
5703                                .and_then(|repository| repository.get("issue"))
5704                                .is_some_and(Value::is_null),
5705                            _ => false,
5706                        }
5707                    })
5708        };
5709        let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
5710            parsed.get("data").is_some_and(|data| {
5711                data.is_object()
5712                    && parsed
5713                        .get("errors")
5714                        .and_then(Value::as_array)
5715                        .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
5716            })
5717        });
5718        match parsed {
5719            Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
5720            _ => self.answer(body),
5721        }
5722    }
5723
5724    fn answer(&self, body: &str) -> Result<Value, SourceError> {
5725        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
5726            message: format!("GitHub returned invalid JSON: {e}"),
5727        })?;
5728        let errors = body
5729            .get("errors")
5730            .map(|value| {
5731                value.as_array().ok_or_else(|| SourceError::Malformed {
5732                    message: "GitHub response errors is not an array".into(),
5733                })
5734            })
5735            .transpose()?;
5736        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
5737            let messages = errors
5738                .iter()
5739                .filter_map(|e| e.get("message").and_then(Value::as_str))
5740                .collect::<Vec<_>>()
5741                .join("; ");
5742            let message = if messages.is_empty() {
5743                "GitHub returned GraphQL errors".into()
5744            } else {
5745                messages
5746            };
5747            let normalized = message.to_ascii_lowercase();
5748            if normalized.contains("resource not accessible") || normalized.contains("scope") {
5749                return Err(SourceError::Auth {
5750                    message: format!(
5751                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
5752                        self.credential_name
5753                    ),
5754                });
5755            }
5756            return Err(SourceError::Refused { message });
5757        }
5758        body.get("data")
5759            .filter(|data| data.is_object())
5760            .cloned()
5761            .ok_or_else(|| SourceError::Malformed {
5762                message: "GitHub response has no data object".into(),
5763            })
5764    }
5765
5766    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
5767    // GraphQL cannot independently page them inside the outer item page. This source page is
5768    // deliberately bounded at that published maximum; the live drift journey exercises it.
5769    async fn board_page(
5770        &self,
5771        items_after: Option<&str>,
5772        items_first: u32,
5773    ) -> Result<Value, SourceError> {
5774        let data = self
5775            .graphql(
5776                graphql::BOARD,
5777                json!({"owner":self.owner,"number":self.project_number,
5778                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
5779                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
5780            )
5781            .await?;
5782        data.pointer("/owner/projectV2")
5783            .filter(|v| !v.is_null())
5784            .cloned()
5785            .ok_or_else(|| SourceError::Refused {
5786                message: format!(
5787                    "GitHub project {}/{} was not found or is not visible to the token",
5788                    self.owner, self.project_number
5789                ),
5790            })
5791    }
5792
5793    /// The search that finds the issues of this board, narrowed by `also` when it is
5794    /// given.
5795    ///
5796    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
5797    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
5798    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
5799    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
5800    /// from a task by the `parent` field each issue carries rather than by the search.
5801    fn board_search(&self, also: Option<&str>) -> String {
5802        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
5803        match also {
5804            Some(also) => format!("{scope} {also}"),
5805            None => scope,
5806        }
5807    }
5808
5809    /// One issue this source reached directly, as the board item a read of the board would
5810    /// have produced — or `None` when this board does not hold it.
5811    ///
5812    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
5813    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
5814    /// item's own id, that item's field values, and the issue as its content. One resolver
5815    /// for both routes is what makes an issue read through a search, through its own node
5816    /// id, or through its project's sub-issues report the same title, the same status, the
5817    /// same labels and the same qualified id.
5818    ///
5819    /// An issue with no entry for *this* board is not this source's to report, which is
5820    /// what keeps an id naming some other repository's issue from being answered as an item
5821    /// of this board. That answer is given about an **exhausted** connection and never
5822    /// about an unread page: the entry is looked for on the page in hand, and only if that
5823    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
5824    /// rest of it.
5825    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
5826        self.resolve_issue_as(issue, true).await
5827    }
5828
5829    /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
5830    /// and writes only when `remember` says to.
5831    async fn resolve_issue_as(
5832        &self,
5833        issue: &Value,
5834        remember: bool,
5835    ) -> Result<Option<Resolved>, SourceError> {
5836        if optional_str(issue, "__typename")? != Some("Issue") {
5837            return Ok(None);
5838        }
5839        let memberships = issue
5840            .get("projectItems")
5841            .ok_or_else(|| SourceError::Malformed {
5842                message: "GitHub issue is missing projectItems".into(),
5843            })?;
5844        let nodes = memberships
5845            .get("nodes")
5846            .and_then(Value::as_array)
5847            .ok_or_else(|| SourceError::Malformed {
5848                message: "GitHub issue projectItems.nodes is not an array".into(),
5849            })?;
5850        let held = match self.board_entry(nodes) {
5851            Some(held) => held.clone(),
5852            None => {
5853                let info = memberships
5854                    .get("pageInfo")
5855                    .ok_or_else(|| SourceError::Malformed {
5856                        message: "GitHub issue projectItems has no pageInfo".into(),
5857                    })?;
5858                // The page held no entry for this board. Whether that means the issue is
5859                // not on it is a question about the rest of the connection, and only a
5860                // connection with no rest answers it here.
5861                if !required_bool(info, "hasNextPage")? {
5862                    return Ok(None);
5863                }
5864                let cursor = required_str(info, "endCursor")?;
5865                validate_cursor_progress(None, cursor)?;
5866                let issue_id = required_str(issue, "id")?;
5867                match self.board_membership(issue_id, cursor).await? {
5868                    Some(held) => held,
5869                    None => return Ok(None),
5870                }
5871            }
5872        };
5873        // The membership's own `updatedAt` rides along: it is the board half of the item's change
5874        // token, which a field change moves where the issue's own time does not.
5875        let item = json!({
5876            "id": required_str(&held, "id")?,
5877            "updatedAt": held.get("updatedAt"),
5878            "project": held.get("project"),
5879            "fieldValues": held.get("fieldValues"),
5880            "content": issue,
5881        });
5882        if remember {
5883            self.resolve(&item)
5884        } else {
5885            self.resolve_unremembered(&item)
5886        }
5887    }
5888
5889    /// This board's own entry among one page of an issue's `Issue.projectItems`.
5890    ///
5891    /// One spelling of *which membership is this board's*, so the page a read carries and
5892    /// the pages [`Self::board_membership`] walks are searched by the same rule.
5893    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5894        nodes.iter().find(|node| {
5895            node.pointer("/project/number").and_then(Value::as_u64)
5896                == Some(u64::from(self.project_number))
5897        })
5898    }
5899
5900    /// The rest of one issue's board memberships, from `after`, for this board's entry.
5901    ///
5902    /// The recovery read: a page of memberships that holds no entry for this board says
5903    /// nothing about the memberships past it, so the connection is walked to exhaustion
5904    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5905    /// positive answer — the whole connection was read and no entry named this board —
5906    /// rather than a failure, and the walk is held to
5907    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5908    /// with a cursor that does not advance is refused instead of spun on.
5909    async fn board_membership(
5910        &self,
5911        issue: &str,
5912        after: &str,
5913    ) -> Result<Option<Value>, SourceError> {
5914        let mut after = after.to_owned();
5915        loop {
5916            let data = self
5917                .graphql(
5918                    graphql::ISSUE_BOARD_ITEMS,
5919                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5920                           "nestedFirst":NESTED_PAGE_SIZE}),
5921                )
5922                .await?;
5923            let Some(connection) = data
5924                .pointer("/node/projectItems")
5925                .filter(|value| !value.is_null())
5926            else {
5927                // The id resolved to nothing, or to something with no memberships to walk —
5928                // which is the same answer as a connection holding no entry for this board.
5929                return Ok(None);
5930            };
5931            let nodes = connection
5932                .get("nodes")
5933                .and_then(Value::as_array)
5934                .ok_or_else(|| SourceError::Malformed {
5935                    message: "GitHub issue projectItems.nodes is not an array".into(),
5936                })?;
5937            if let Some(held) = self.board_entry(nodes) {
5938                return Ok(Some(held.clone()));
5939            }
5940            let info = connection
5941                .get("pageInfo")
5942                .ok_or_else(|| SourceError::Malformed {
5943                    message: "GitHub issue projectItems has no pageInfo".into(),
5944                })?;
5945            let next = required_bool(info, "hasNextPage")?
5946                .then(|| required_str(info, "endCursor"))
5947                .transpose()?;
5948            match next {
5949                Some(next) => {
5950                    validate_cursor_progress(Some(&after), next)?;
5951                    after = next.to_owned();
5952                }
5953                None => return Ok(None),
5954            }
5955        }
5956    }
5957
5958    /// One page of a board-scoped issue search, and where the next page resumes.
5959    async fn search_page(
5960        &self,
5961        search: &str,
5962        first: u32,
5963        after: Option<&str>,
5964    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5965        let data = self
5966            .graphql(
5967                graphql::SEARCH_ISSUES,
5968                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5969                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5970                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5971            )
5972            .await?;
5973        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5974            message: "GitHub search response has no search connection".into(),
5975        })?;
5976        let mut found = Vec::new();
5977        for node in connection
5978            .get("nodes")
5979            .and_then(Value::as_array)
5980            .ok_or_else(|| SourceError::Malformed {
5981                message: "GitHub search nodes is not an array".into(),
5982            })?
5983        {
5984            if let Some(resolved) = self.resolve_issue(node).await? {
5985                found.push(resolved);
5986            }
5987        }
5988        let info = connection
5989            .get("pageInfo")
5990            .ok_or_else(|| SourceError::Malformed {
5991                message: "GitHub search connection has no pageInfo".into(),
5992            })?;
5993        let next = required_bool(info, "hasNextPage")?
5994            .then(|| required_str(info, "endCursor"))
5995            .transpose()?
5996            .map(str::to_owned);
5997        if let Some(next) = &next {
5998            validate_cursor_progress(after, next)?;
5999        }
6000        Ok((found, next))
6001    }
6002
6003    /// Every issue this board holds, completed with what this run wrote.
6004    ///
6005    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
6006    /// is an index and is eventually consistent, so an issue this run created seconds ago
6007    /// can be absent from it, and a project listed straight after being written would
6008    /// otherwise be missing from its own board. What is added back is only what this
6009    /// process itself wrote, out of [`Self::created`], which lives and dies with the
6010    /// process.
6011    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
6012        let found = self.searched_issues().await?;
6013        self.completed_with_written(found, |_| true)
6014    }
6015
6016    /// Every issue this board's own search reports, walked to exhaustion, read once per
6017    /// source.
6018    ///
6019    /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
6020    /// needs it too and the two would otherwise walk the same search twice in one command.
6021    /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
6022    /// is.
6023    async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
6024        let cached = self.search_cache()?.clone();
6025        if let Some(held) = cached {
6026            return Ok(held);
6027        }
6028        let mut after: Option<String> = None;
6029        let mut found = Vec::new();
6030        let search = self.board_search(None);
6031        loop {
6032            let (page, next) = self
6033                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
6034                .await?;
6035            found.extend(page);
6036            match next {
6037                Some(next) => after = Some(next),
6038                None => break,
6039            }
6040        }
6041        *self.search_cache()? = Some(found.clone());
6042        Ok(found)
6043    }
6044
6045    /// Every issue this board's own search reports with `also` beside its scope, walked to
6046    /// exhaustion — held for no later read, which may not be narrowed alike.
6047    async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6048        let mut after: Option<String> = None;
6049        let mut found = Vec::new();
6050        let search = self.board_search(Some(also));
6051        loop {
6052            let (page, next) = self
6053                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
6054                .await?;
6055            found.extend(page);
6056            match next {
6057                Some(next) => after = Some(next),
6058                None => return Ok(found),
6059            }
6060        }
6061    }
6062
6063    /// This process's own view of the board's issues, or the refusal a poisoned lock is.
6064    fn search_cache(
6065        &self,
6066    ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
6067        self.search_cache
6068            .lock()
6069            .map_err(|_| SourceError::Unavailable {
6070                message: "this source's view of the board's issues was left inconsistent by an \
6071                      earlier failure; next: run the command again"
6072                    .into(),
6073            })
6074    }
6075
6076    /// `found`, with everything this run wrote that `keep` accepts and the read did not
6077    /// report.
6078    ///
6079    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
6080    /// at all: the search index is behind, and a node read of an item filed moments ago can
6081    /// be too.
6082    fn completed_with_written(
6083        &self,
6084        mut found: Vec<Resolved>,
6085        keep: impl Fn(&Resolved) -> bool,
6086    ) -> Result<Vec<Resolved>, SourceError> {
6087        for own in self.created()?.iter().filter(|own| keep(own)) {
6088            if !found.iter().any(|item| item.id == own.id) {
6089                found.push(own.clone());
6090            }
6091        }
6092        Ok(found)
6093    }
6094
6095    /// What resolving one node id reached.
6096    ///
6097    /// Three answers rather than an `Option`, because a board *draft* is none of the other
6098    /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
6099    /// is completed by a read of the draft itself rather than reported as nothing.
6100    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
6101        let asked = self
6102            .graphql(
6103                graphql::ISSUE,
6104                json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
6105                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6106            )
6107            .await;
6108        let data = match asked {
6109            Ok(data) => data,
6110            // A string that is not a node id at all is not a failure to report: it is an id
6111            // this board does not hold, which is what every read of one already answers.
6112            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
6113            Err(error) => return Err(error),
6114        };
6115        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
6116            return Ok(Reached::Nothing);
6117        };
6118        if optional_str(node, "__typename")? == Some("DraftIssue") {
6119            return Ok(Reached::Draft);
6120        }
6121        Ok(match self.resolve_issue(node).await? {
6122            Some(item) => Reached::Held(Box::new(item)),
6123            None => Reached::Nothing,
6124        })
6125    }
6126
6127    /// One item of this board by its own id, whatever kind it is.
6128    ///
6129    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
6130    /// run wrote is read first, because a node read of an item created moments ago can
6131    /// still be behind the board field values written onto it — see [`Self::created`].
6132    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6133        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
6134            return Ok(Some(own.clone()));
6135        }
6136        if let Some(held) = self.read_ahead()?.remove(id) {
6137            return Ok(held);
6138        }
6139        match self.reach(id).await? {
6140            Reached::Held(item) => Ok(Some(*item)),
6141            Reached::Nothing => Ok(None),
6142            Reached::Draft => self.draft_by_id(id).await,
6143        }
6144    }
6145
6146    /// [`Self::item_by_id`] for a document: one [`graphql::DOCUMENT_IN_PROJECT`] read, which
6147    /// answers the project it is filed under and that project's members beside it.
6148    ///
6149    /// Each is held for this command as a read of it would have been: the project for the
6150    /// next read of it by its id, and — when its page of members is the whole of them — the
6151    /// members for the next listing of that project. So a write held to who may read the
6152    /// document, which reads all three, is answered by this one request.
6153    async fn document_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6154        if self.created()?.iter().any(|own| own.id == *id) || self.read_ahead()?.contains_key(id) {
6155            return self.item_by_id(id).await;
6156        }
6157        let asked = self
6158            .graphql(
6159                graphql::DOCUMENT_IN_PROJECT,
6160                json!({"id":id.0,"first":MAX_PAGE_SIZE,"memberFirst":MEMBER_PAGE_SIZE,
6161                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6162                       "duplicates":true}),
6163            )
6164            .await;
6165        let data = match asked {
6166            Ok(data) => data,
6167            Err(error) if unresolvable_node(&error) => return Ok(None),
6168            Err(error) => return Err(error),
6169        };
6170        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
6171            return Ok(None);
6172        };
6173        if optional_str(node, "__typename")? == Some("DraftIssue") {
6174            return self.draft_by_id(id).await;
6175        }
6176        let Some(item) = self.resolve_issue(node).await? else {
6177            return Ok(None);
6178        };
6179        // An answer for another issue than the one asked about says nothing of that one.
6180        if item.id != *id {
6181            return Err(SourceError::Malformed {
6182                message: format!(
6183                    "GitHub answered issue {} when asked for document {}",
6184                    item.id.0, id.0
6185                ),
6186            });
6187        }
6188        if let Some(project) = node.get("project").filter(|value| !value.is_null())
6189            && let Some(parent) = self
6190                .resolve_issue(project)
6191                .await?
6192                .filter(|parent| Some(&parent.id) == item.parent.as_ref())
6193        {
6194            let members = project.get("subIssues").filter(|value| !value.is_null());
6195            let whole = members
6196                .and_then(|members| members.pointer("/pageInfo/hasNextPage"))
6197                .and_then(Value::as_bool)
6198                == Some(false);
6199            if whole {
6200                let nodes = members
6201                    .and_then(|members| members.get("nodes"))
6202                    .and_then(Value::as_array)
6203                    .ok_or_else(|| SourceError::Malformed {
6204                        message: "GitHub subIssues.nodes is not an array".into(),
6205                    })?;
6206                let mut children = Vec::new();
6207                for member in nodes {
6208                    if let Some(resolved) = self.resolve_issue(member).await? {
6209                        children.push(resolved);
6210                    }
6211                }
6212                // Held as the project's members only when every one resolved and says it is
6213                // filed under it: a page with any other answers nothing of all the members.
6214                if children.len() == nodes.len()
6215                    && children
6216                        .iter()
6217                        .all(|child| child.parent.as_ref() == Some(&parent.id))
6218                {
6219                    self.children_cache()?
6220                        .insert(parent.id.clone(), (parent.id.clone(), children));
6221                }
6222            }
6223            self.read_ahead()?.insert(parent.id.clone(), Some(parent));
6224        }
6225        Ok(Some(item))
6226    }
6227
6228    /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
6229    /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
6230    /// than one request per id.
6231    ///
6232    /// What this run wrote answers first, as it does there, and only the rest is read. One id
6233    /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
6234    /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
6235    /// id at a time, so that id is answered as not held and the others as themselves; a draft
6236    /// is completed by a read of the draft, exactly as there.
6237    async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
6238        let mut found: Vec<Option<Option<Resolved>>> = {
6239            let created = self.created()?;
6240            ids.iter()
6241                .map(|id| {
6242                    created
6243                        .iter()
6244                        .find(|own| own.id == *id)
6245                        .map(|own| Some(own.clone()))
6246                })
6247                .collect()
6248        };
6249        let unread: Vec<NativeId> = ids
6250            .iter()
6251            .zip(&found)
6252            .filter(|(_, found)| found.is_none())
6253            .map(|(id, _)| id.clone())
6254            .collect();
6255        let mut read = Vec::with_capacity(unread.len());
6256        if let [one] = unread.as_slice() {
6257            read.push(self.item_by_id(one).await?);
6258        } else {
6259            for batch in unread.chunks(DETAIL_BATCH) {
6260                let data = match self
6261                    .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
6262                    .await
6263                {
6264                    Ok(data) => data,
6265                    Err(error) if unresolvable_node(&error) => {
6266                        for id in batch {
6267                            read.push(self.item_by_id(id).await?);
6268                        }
6269                        continue;
6270                    }
6271                    Err(error) => return Err(error),
6272                };
6273                for (slot, id) in batch.iter().enumerate() {
6274                    let node =
6275                        data.get(format!("i{slot}"))
6276                            .ok_or_else(|| SourceError::Malformed {
6277                                message: format!(
6278                                    "GitHub answered a batch read with no item for {}",
6279                                    id.0
6280                                ),
6281                            })?;
6282                    read.push(if node.is_null() {
6283                        None
6284                    } else if optional_str(node, "__typename")? == Some("DraftIssue") {
6285                        self.draft_by_id(id).await?
6286                    } else {
6287                        if optional_str(node, "__typename")? == Some("Issue")
6288                            && required_str(node, "id")? != id.0
6289                        {
6290                            return Err(SourceError::Malformed {
6291                                message: format!(
6292                                    "GitHub answered the read of {} with issue {}",
6293                                    id.0,
6294                                    required_str(node, "id")?
6295                                ),
6296                            });
6297                        }
6298                        self.resolve_issue(node).await?
6299                    });
6300                }
6301            }
6302        }
6303        let mut read = read.into_iter();
6304        Ok(found
6305            .iter_mut()
6306            .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
6307            .collect())
6308    }
6309
6310    fn read_ahead(
6311        &self,
6312    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
6313        self.read_ahead
6314            .lock()
6315            .map_err(|_| SourceError::Unavailable {
6316                message:
6317                    "items read ahead for a copy were left inconsistent; run the command again"
6318                        .into(),
6319            })
6320    }
6321
6322    fn resolved_cache(
6323        &self,
6324    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6325        self.resolved_cache
6326            .lock()
6327            .map_err(|_| SourceError::Unavailable {
6328                message: "resolved item records were left inconsistent; run the command again"
6329                    .into(),
6330            })
6331    }
6332
6333    /// Reuse a record this invocation already resolved. The mutation sender invalidates
6334    /// it before writing, so a partial failure cannot leave a pre-write binding behind.
6335    async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6336        let cached = self.resolved_cache()?.get(id).cloned();
6337        if let Some(item) = cached {
6338            return Ok(Some(item));
6339        }
6340        self.item_by_id(id).await
6341    }
6342
6343    /// Take out the item an issue read by reference reached for `id`, when one is held: the
6344    /// status write it was read for is the one caller, so no other read or write is ever
6345    /// answered from a read that selected only what that write needs.
6346    fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6347        Ok(self
6348            .held_cache
6349            .lock()
6350            .map_err(|_| SourceError::Unavailable {
6351                message: "items read by issue reference were left inconsistent; run the \
6352                          command again"
6353                    .into(),
6354            })?
6355            .remove(id))
6356    }
6357
6358    /// One board draft by its own id, with the board item it sits in — or `None` when no
6359    /// item of this board is that draft's.
6360    ///
6361    /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
6362    /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
6363    /// links a draft to one board item, so the page this read carries is the whole of that
6364    /// connection, and a page that reports more than it holds is refused rather than read
6365    /// as an answer about memberships nobody read.
6366    async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6367        let data = self
6368            .graphql(
6369                graphql::DRAFT,
6370                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
6371                       "boardItems":BOARD_ITEMS_PAGE_SIZE}),
6372            )
6373            .await?;
6374        // Gone between the two reads is an answer — the draft is no longer there. Anything
6375        // else than the draft [`Self::reach`] was just told this id is, is not one.
6376        let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
6377            return Ok(None);
6378        };
6379        if optional_str(draft, "__typename")? != Some("DraftIssue") {
6380            return Err(SourceError::Malformed {
6381                message: format!(
6382                    "GitHub answered {} as a draft and then as something else",
6383                    id.0
6384                ),
6385            });
6386        }
6387        if required_str(draft, "id")? != id.0 {
6388            return Err(SourceError::Malformed {
6389                message: format!("GitHub answered a different draft for {}", id.0),
6390            });
6391        }
6392        let memberships = draft
6393            .get("projectV2Items")
6394            .ok_or_else(|| SourceError::Malformed {
6395                message: format!("GitHub draft {} is missing projectV2Items", id.0),
6396            })?;
6397        let nodes = memberships
6398            .get("nodes")
6399            .and_then(Value::as_array)
6400            .ok_or_else(|| SourceError::Malformed {
6401                message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
6402            })?;
6403        let info = memberships
6404            .get("pageInfo")
6405            .ok_or_else(|| SourceError::Malformed {
6406                message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
6407            })?;
6408        // Read whether or not this board's entry is on the page: a page claiming more than
6409        // the one item GitHub links a draft to is a malformed answer either way.
6410        if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
6411            return Err(SourceError::Malformed {
6412                message: format!(
6413                    "GitHub draft {} reports more board items than the one GitHub links a draft \
6414                     to",
6415                    id.0
6416                ),
6417            });
6418        }
6419        if let Some(node) = nodes.first()
6420            && node
6421                .pointer("/project/number")
6422                .and_then(Value::as_u64)
6423                .is_none()
6424        {
6425            return Err(SourceError::Malformed {
6426                message: format!(
6427                    "GitHub draft {} board item has no numeric project number",
6428                    id.0
6429                ),
6430            });
6431        }
6432        let Some(held) = self.board_entry(nodes) else {
6433            return Ok(None);
6434        };
6435        if required_str(
6436            held.get("project").ok_or_else(|| SourceError::Malformed {
6437                message: format!("GitHub draft {} board item has no project", id.0),
6438            })?,
6439            "id",
6440        )? != self.board_fields().await?.id.as_str()
6441        {
6442            return Ok(None);
6443        }
6444        let item = json!({
6445            "id": required_str(held, "id")?,
6446            "project": held.get("project"),
6447            "fieldValues": held.get("fieldValues"),
6448            "content": draft,
6449        });
6450        self.resolve(&item)
6451    }
6452
6453    /// The board's own id and field definitions, for a write whose item does not carry
6454    /// them — never its items.
6455    ///
6456    /// A board this command has already listed supplies them, since it read them beside its
6457    /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
6458    /// is consulted about which items the board holds: see the module documentation for
6459    /// why a question about one known item is answered by reading that item.
6460    async fn board_fields(&self) -> Result<BoardFields, SourceError> {
6461        if let Some(board) = self.board_cache()?.as_ref() {
6462            return Ok(BoardFields {
6463                id: BoardId::parse(&board.id)?,
6464                fields: board.fields.clone(),
6465            });
6466        }
6467        if let Some(held) = self.fields_cache()?.clone() {
6468            return Ok(held);
6469        }
6470        let data = self
6471            .graphql(
6472                graphql::BOARD_FIELDS,
6473                json!({"owner":self.owner,"number":self.project_number,
6474                       "nestedFirst":NESTED_PAGE_SIZE}),
6475            )
6476            .await?;
6477        self.fields_read(&data)
6478    }
6479
6480    /// The board's id and fields out of an answer carrying the `boardFields` root, held for
6481    /// the rest of this command.
6482    fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
6483        let board = data
6484            .pointer("/boardFields/projectV2")
6485            .filter(|value| !value.is_null())
6486            .ok_or_else(|| SourceError::Refused {
6487                message: format!(
6488                    "GitHub project {}/{} was not found or is not visible to the token",
6489                    self.owner, self.project_number
6490                ),
6491            })?;
6492        let read = BoardFields {
6493            id: BoardId::parse(required_str(board, "id")?)?,
6494            fields: board.get("fields").cloned().unwrap_or(Value::Null),
6495        };
6496        *self.fields_cache()? = Some(read.clone());
6497        Ok(read)
6498    }
6499
6500    /// Read what creating an issue in `repository` needs and this command has not read yet —
6501    /// the board's fields and the repository's node id — in one request when it needs both.
6502    ///
6503    /// When either is already known this sends nothing, and the other is read by its own
6504    /// document where it is asked for, so no create reads anything twice.
6505    async fn creation_context(
6506        &self,
6507        repository: &RepositoryTarget,
6508        incoming: &Incoming<'_>,
6509    ) -> Result<(), SourceError> {
6510        let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
6511        if fields_known || self.repository_cache()?.contains_key(repository) {
6512            return Ok(());
6513        }
6514        // A placement an earlier process kept is taken only while the project issue this create
6515        // has just read shows its setup-context token unchanged; see `projection`.
6516        if self.placed_by_kept(repository, incoming.parent) {
6517            return Ok(());
6518        }
6519        let data = self
6520            .graphql(
6521                graphql::CREATION_CONTEXT,
6522                json!({"owner":self.owner,"number":self.project_number,
6523                       "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
6524                       "repositoryName":repository.name}),
6525            )
6526            .await?;
6527        self.fields_read(&data)?;
6528        self.repository_read(&data, repository, incoming)?;
6529        Ok(())
6530    }
6531
6532    /// This process's own view of the board's fields, or the refusal a poisoned lock is.
6533    fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
6534        self.fields_cache
6535            .lock()
6536            .map_err(|_| SourceError::Unavailable {
6537                message: "this source's view of the board's fields was left inconsistent by an \
6538                      earlier failure; next: run the command again"
6539                    .into(),
6540            })
6541    }
6542
6543    /// What a write to `item` needs of the board, read off that item when it says enough and
6544    /// off [`Self::board_fields`] when it does not.
6545    ///
6546    /// A node read of an item names its board and carries the definition of every field it
6547    /// holds a value of — so an item naming its board, holding a value of the origin field
6548    /// when the write carries an origin, and, when the write carries a status, holding a
6549    /// `Status` value, needs no read of the board at all. **Nothing the item does not say is
6550    /// guessed:** a field it holds no value of may still be on the board, and a view reading it
6551    /// as absent would refuse a write the board can take or skip a field write the board
6552    /// needs, so such an item — and a create, which has no item yet — takes the board's fields
6553    /// from their own read instead. A write carrying no origin to an item holding none — every
6554    /// write out of a source declared private — writes no origin whatever the board defines,
6555    /// so it needs no definition of that field.
6556    async fn fields_for(
6557        &self,
6558        item: Option<&Resolved>,
6559        writes_origin: bool,
6560        writes_status: bool,
6561        selects_priority: bool,
6562    ) -> Result<BoardFields, SourceError> {
6563        if let Some(board) = item.and_then(Resolved::carried_board) {
6564            return Ok(board);
6565        }
6566        if let Some(item) = item
6567            && let Some(board_id) = item.named_board()
6568            && (!writes_origin || item.defines(ORIGIN_FIELD))
6569            && (!writes_status || item.defines("Status"))
6570            && (!selects_priority || item.defines(PRIORITY_FIELD))
6571            && self
6572                .metadata_fields
6573                .iter()
6574                .all(|projection| item.defines(&projection.field))
6575        {
6576            return Ok(BoardFields {
6577                id: board_id,
6578                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6579            });
6580        }
6581        self.board_fields().await
6582    }
6583
6584    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
6585    /// when that id names nothing here with a sub-issue relationship to walk.
6586    ///
6587    /// `None` and an empty answer are different: `None` is *this is not an issue of this
6588    /// GitHub*, which is what sends a project selector on to be read as a name, and an
6589    /// empty vector is a project that holds nothing.
6590    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6591        let mut after: Option<String> = None;
6592        let mut children = Vec::new();
6593        // The project issue as the first page carried it, held once the walk has ended.
6594        let mut parent: Option<Resolved> = None;
6595        loop {
6596            let asked = self
6597                .graphql(
6598                    graphql::SUB_ISSUES,
6599                    json!({"id":id.0,"memberFirst":MEMBER_PAGE_SIZE,"after":after,
6600                           "nestedFirst":NESTED_PAGE_SIZE,
6601                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6602                )
6603                .await;
6604            let data = match asked {
6605                Ok(data) => data,
6606                // A string that is not a node id at all is not a failure to report: it is
6607                // the ordinary answer to a selector naming a project by its name.
6608                Err(error) if unresolvable_node(&error) => return Ok(None),
6609                Err(error) => return Err(error),
6610            };
6611            let Some(connection) = data
6612                .pointer("/node/subIssues")
6613                .filter(|value| !value.is_null())
6614            else {
6615                // No such node, or one with no sub-issue relationship — a board draft is
6616                // the one this board can really hold.
6617                return Ok(None);
6618            };
6619            if after.is_none()
6620                && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
6621            {
6622                parent = self
6623                    .resolve_issue(node)
6624                    .await?
6625                    .filter(|resolved| resolved.id == *id);
6626            }
6627            for node in connection
6628                .get("nodes")
6629                .and_then(Value::as_array)
6630                .ok_or_else(|| SourceError::Malformed {
6631                    message: "GitHub subIssues.nodes is not an array".into(),
6632                })?
6633            {
6634                if let Some(resolved) = self.resolve_issue(node).await? {
6635                    children.push(resolved);
6636                }
6637            }
6638            let info = connection
6639                .get("pageInfo")
6640                .ok_or_else(|| SourceError::Malformed {
6641                    message: "GitHub subIssues connection has no pageInfo".into(),
6642                })?;
6643            let next = required_bool(info, "hasNextPage")?
6644                .then(|| required_str(info, "endCursor"))
6645                .transpose()?;
6646            match next {
6647                Some(next) => {
6648                    validate_cursor_progress(after.as_deref(), next)?;
6649                    after = Some(next.to_owned());
6650                }
6651                None => {
6652                    // Held only once the walk has ended, so one failing part way holds
6653                    // nothing and every read it would have answered is made.
6654                    // The project issue the first page carried answers the next read of the
6655                    // project by its id in this command, as a read of it made now would: what
6656                    // classifying a project for a write reads straight after its members.
6657                    if let Some(parent) = &parent {
6658                        self.read_ahead()?
6659                            .entry(parent.id.clone())
6660                            .or_insert_with(|| Some(parent.clone()));
6661                    }
6662                    let mut carried = self.carried()?;
6663                    for item in parent.into_iter().chain(children.iter().cloned()) {
6664                        carried.insert(item.id.clone(), item);
6665                    }
6666                    return Ok(Some(children));
6667                }
6668            }
6669        }
6670    }
6671
6672    fn carried(
6673        &self,
6674    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6675        self.carried.lock().map_err(|_| SourceError::Unavailable {
6676            message: "items a project's listing carried were left inconsistent; run the \
6677                      command again"
6678                .into(),
6679        })
6680    }
6681
6682    /// Which issue of this board a project *name* is, or `None` when none is.
6683    ///
6684    /// One bounded query which filters on that name at the server, rather than a walk of
6685    /// every issue the board holds. The name is compared again here: the qualifier narrows
6686    /// what GitHub sends, and this source decides what it names.
6687    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
6688        let search = self.board_search(Some(&title_qualifier(name)));
6689        let mut after = None;
6690        loop {
6691            let (candidates, next) = self
6692                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6693                .await?;
6694            if let Some(item) = candidates.into_iter().find(|item| {
6695                item.kind == BoardKind::Work(ItemKind::Project)
6696                    && item.title.eq_ignore_ascii_case(name)
6697            }) {
6698                return Ok(Some(item.id));
6699            }
6700            match next {
6701                Some(next) => after = Some(next),
6702                None => return Ok(None),
6703            }
6704        }
6705    }
6706
6707    /// Everything filed under one project of this board: the sub-issues of the issue that
6708    /// project is.
6709    ///
6710    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
6711    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
6712    /// gains projects, or as another project gains tasks.
6713    ///
6714    /// A qualified id names the issue and is asked for its sub-issues directly: one
6715    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
6716    /// read as a project *name*, which costs the one bounded search
6717    /// [`Self::project_by_name`] makes.
6718    ///
6719    /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
6720    /// — and each answer is still cut to the items whose parent is this project, so one this
6721    /// process has since filed elsewhere is not reported here, and completed with what this
6722    /// process filed under it.
6723    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
6724        let held = self.children_cache()?.get(selector).cloned();
6725        let (project, children) = match held {
6726            Some(held) => held,
6727            None => {
6728                let answered = match self.sub_issues(selector).await? {
6729                    Some(children) => (selector.clone(), children),
6730                    None => match self.project_by_name(&selector.0).await? {
6731                        Some(project) => {
6732                            let children = self.sub_issues(&project).await?.unwrap_or_default();
6733                            (project, children)
6734                        }
6735                        None => return Ok(Vec::new()),
6736                    },
6737                };
6738                self.children_cache()?
6739                    .insert(selector.clone(), answered.clone());
6740                answered
6741            }
6742        };
6743        let mut children: Vec<Resolved> = children
6744            .into_iter()
6745            .filter(|child| child.parent.as_ref() == Some(&project))
6746            .collect();
6747        for own in self.updated()?.iter() {
6748            if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
6749                children.push(own.clone());
6750            }
6751        }
6752        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
6753    }
6754
6755    /// Every issue of this board GitHub's issue search reports updated at or after `since`,
6756    /// completed with what this run wrote — the candidates a comment-activity read confirms.
6757    ///
6758    /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
6759    /// on the board whatever repository, and whatever owner, it lives in, so no repository or
6760    /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
6761    /// sufficient is a fact about GitHub rather than about this source: a comment written on an
6762    /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
6763    /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
6764    /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
6765    /// rather than silently narrowing a caller's answer.
6766    ///
6767    /// The instant is written to the second, rounded down, which can only widen what the
6768    /// search returns; confirmation against each candidate's own comments is what makes the
6769    /// answer exact. The search is an index that lags a write by a second or two — the module
6770    /// documentation records it — so a caller that asks again from its last instant should
6771    /// overlap the two by more than that.
6772    async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
6773        let found = self.searched(&updated_qualifier(since)).await?;
6774        self.completed_with_written(found, |_| true)
6775    }
6776
6777    /// Every issue of this board GitHub's issue search reports for the board-scoped search
6778    /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
6779    ///
6780    /// Uncompleted: what this process wrote is added by the caller, which knows whether its
6781    /// own record is the fresher of the two.
6782    async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6783        let search = self.board_search(Some(also));
6784        let mut after: Option<String> = None;
6785        let mut found = Vec::new();
6786        loop {
6787            let (page, next) = self
6788                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6789                .await?;
6790            found.extend(page);
6791            match next {
6792                Some(next) => after = Some(next),
6793                None => return Ok(found),
6794            }
6795        }
6796    }
6797
6798    /// A bounded task answer; the versioned cursor carries the connection position, how
6799    /// many rows of the page starting there were already handed out, and the own-write ids
6800    /// already observed, including across a new source instance.
6801    ///
6802    /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
6803    /// sliced from the pages it needs; why is the module documentation's paging contract.
6804    async fn search_tasks(
6805        &self,
6806        query: &TaskQuery,
6807        page: &PageRequest,
6808        also: &str,
6809    ) -> Result<Page<Task>, SourceError> {
6810        let mut position = match &page.cursor {
6811            None => SearchPosition::default(),
6812            Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
6813                .ok()
6814                .filter(|position| {
6815                    position.version == SEARCH_CURSOR_VERSION
6816                        && position.connection.valid_resume(position.offset)
6817                })
6818                .ok_or_else(|| SourceError::Config {
6819                    message: "page cursor is invalid".into(),
6820                })?,
6821        };
6822        let search = self.board_search(Some(also));
6823        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6824        let own = self.with_own_writes(Vec::new())?;
6825        // An issue this process commented on is a candidate of a comment-activity read
6826        // whether or not the search has caught up with the comment; see `Self::commented`.
6827        let commented = match query.commented_since {
6828            Some(_) => self.commented()?.clone(),
6829            None => Vec::new(),
6830        };
6831        for id in own.iter().map(|item| &item.id).chain(&commented) {
6832            if !position.own.contains(id) {
6833                position.own.push(id.clone());
6834            }
6835        }
6836        let mut tasks = Vec::new();
6837        while !position.connection.exhausted() && tasks.len() < limit {
6838            let first = SEARCH_PAGE_SIZE;
6839            // Page size is part of the key: a short cached answer cannot answer a wider ask.
6840            let key =
6841                serde_json::to_string(&("page", &search, &position.connection.after(), first))
6842                    .expect("search page key is serializable");
6843            let cached = if query.commented_since.is_none() {
6844                self.narrowed_cache()?.get(&key).cloned()
6845            } else {
6846                None
6847            };
6848            let (found, next) = match cached {
6849                Some(found) => {
6850                    let next = self
6851                        .search_next
6852                        .lock()
6853                        .map_err(|_| SourceError::Unavailable {
6854                            message:
6855                                "search pagination was left inconsistent; run the command again"
6856                                    .into(),
6857                        })?
6858                        .get(&key)
6859                        .cloned()
6860                        .flatten();
6861                    (found, next)
6862                }
6863                None => {
6864                    let (found, next) = self
6865                        .search_page(&search, first, position.connection.after())
6866                        .await?;
6867                    if query.commented_since.is_none() {
6868                        self.search_next
6869                            .lock()
6870                            .map_err(|_| SourceError::Unavailable {
6871                                message:
6872                                    "search pagination was left inconsistent; run the command again"
6873                                        .into(),
6874                            })?
6875                            .insert(key.clone(), next.clone());
6876                        self.narrowed_cache()?.insert(key, found.clone());
6877                    }
6878                    (found, next)
6879                }
6880            };
6881            let rows = found.len();
6882            for mut item in found.into_iter().skip(position.offset) {
6883                if tasks.len() == limit {
6884                    break;
6885                }
6886                position.offset += 1;
6887                if position.own.contains(&item.id) {
6888                    if position.seen.contains(&item.id) {
6889                        continue;
6890                    }
6891                    position.seen.push(item.id.clone());
6892                    // The search's own copy of an issue this process only commented on is as
6893                    // good as a node read of it, since its comments are read either way.
6894                    let only_commented = commented.contains(&item.id)
6895                        && !own.iter().any(|written| written.id == item.id);
6896                    if !only_commented {
6897                        let updated_at = item.updated_at;
6898                        let Some(written) = self.search_written(&own, &item.id).await? else {
6899                            continue;
6900                        };
6901                        item = written;
6902                        item.updated_at = item.updated_at.max(updated_at);
6903                        self.resolved_cache()?.insert(item.id.clone(), item.clone());
6904                    }
6905                }
6906                if item.kind == BoardKind::Work(ItemKind::Task) {
6907                    let task = item.task()?;
6908                    if task_matches(&task, query, &query.project)
6909                        && self.commented_since(&item, query.commented_since).await?
6910                    {
6911                        tasks.push(task);
6912                    }
6913                }
6914            }
6915            if position.offset < rows {
6916                continue;
6917            }
6918            position.offset = 0;
6919            position.connection = match next {
6920                Some(after) => SearchConnection::Continuing {
6921                    after: Cursor(after),
6922                },
6923                None => SearchConnection::Exhausted {},
6924            };
6925        }
6926        if position.connection.exhausted() {
6927            for id in position.own.clone() {
6928                if position.seen.contains(&id) {
6929                    continue;
6930                }
6931                if tasks.len() == limit {
6932                    break;
6933                }
6934                position.seen.push(id.clone());
6935                let Some(item) = self.search_written(&own, &id).await? else {
6936                    continue;
6937                };
6938                if item.kind == BoardKind::Work(ItemKind::Task) {
6939                    let task = item.task()?;
6940                    if task_matches(&task, query, &query.project)
6941                        && self.commented_since(&item, query.commented_since).await?
6942                    {
6943                        tasks.push(task);
6944                    }
6945                }
6946            }
6947        }
6948        let more = !position.connection.exhausted()
6949            || position.own.iter().any(|id| !position.seen.contains(id));
6950        Ok(Page {
6951            items: tasks,
6952            next: more.then(|| {
6953                Cursor(serde_json::to_string(&position).expect("search position is serializable"))
6954            }),
6955        })
6956    }
6957
6958    /// A resumed process has the ids but no write records; resolve only a record the
6959    /// current page needs, by its uncached node read rather than the lagging search index.
6960    async fn search_written(
6961        &self,
6962        own: &[Resolved],
6963        id: &NativeId,
6964    ) -> Result<Option<Resolved>, SourceError> {
6965        match own.iter().find(|item| item.id == *id) {
6966            Some(item) => Ok(Some(item.clone())),
6967            None => self.item_by_id(id).await,
6968        }
6969    }
6970
6971    /// The candidates for a task query carrying a text, metadata or origin predicate, read
6972    /// without enumerating the board — or `None` for a query carrying none of the three, which
6973    /// keeps the reads it always had.
6974    ///
6975    /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
6976    /// because it names at most a handful of items. Text and metadata are answered by one
6977    /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
6978    /// further by `updated:>=` when the query also asks for comment activity, since both
6979    /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
6980    /// process afterwards by the same predicates [`task_matches`] applies to every read.
6981    ///
6982    /// Completed with what this process wrote, its own record winning over the index's copy
6983    /// of the same item: see [`Self::with_own_writes`].
6984    async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
6985        self.send_pending_origins(None).await?;
6986        let asked = match (&query.origin, narrowing_qualifiers(query)) {
6987            (Some(origin), _) => Narrowing::Origin(origin.clone()),
6988            (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
6989                Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
6990                None => qualifiers,
6991            }),
6992            (None, None) => return Ok(None),
6993        };
6994        // A question about comment activity is asked afresh every time, as it always was: it
6995        // is the one a caller polls from one source while waiting for the index, and an
6996        // answer held from the first poll would be the answer to every later one.
6997        let key = query.commented_since.is_none().then(|| asked.key());
6998        let cached = match &key {
6999            Some(key) => self.narrowed_cache()?.get(key).cloned(),
7000            None => None,
7001        };
7002        let found = match cached {
7003            Some(found) => found,
7004            None => {
7005                let found = match &asked {
7006                    Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
7007                    Narrowing::Search(also) => self.searched(also).await?,
7008                };
7009                if let Some(key) = key {
7010                    self.narrowed_cache()?.insert(key, found.clone());
7011                }
7012                found
7013            }
7014        };
7015        self.with_own_writes(found).map(Some)
7016    }
7017
7018    /// The candidates for a project or unscoped document query carrying a searchable text,
7019    /// read without enumerating the board — or `None` for a query with no text or a blank one,
7020    /// which keeps the read it always had.
7021    ///
7022    /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
7023    /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
7024    /// costs is the issues that match and never the board. Its answer is held for the command
7025    /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
7026    /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
7027    /// substring rule, exactly as an item of the wider read was, and is completed with what this
7028    /// process wrote: see [`Self::with_own_writes`].
7029    async fn text_searched(
7030        &self,
7031        text: Option<&TextQuery>,
7032    ) -> Result<Option<Vec<Resolved>>, SourceError> {
7033        let Some(also) = text_qualifiers(text) else {
7034            return Ok(None);
7035        };
7036        let key = Narrowing::Search(also.clone()).key();
7037        let cached = self.narrowed_cache()?.get(&key).cloned();
7038        let found = match cached {
7039            Some(found) => found,
7040            None => {
7041                let found = self.searched(&also).await?;
7042                self.narrowed_cache()?.insert(key, found.clone());
7043                found
7044            }
7045        };
7046        self.with_own_writes(found).map(Some)
7047    }
7048
7049    /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
7050    /// [`Self::narrowed`] reads for a task query carrying that origin — one
7051    /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
7052    /// what this process wrote — each confirmed against its own origin entry.
7053    ///
7054    /// What answers a project or document listing by origin, so finding every copy of one plan
7055    /// costs the items that carry its origin and never a page of the board.
7056    async fn copied_from(
7057        &self,
7058        origin: &str,
7059        kind: BoardKind,
7060    ) -> Result<Vec<Resolved>, SourceError> {
7061        let wanted = TaskQuery {
7062            origin: Some(origin.to_owned()),
7063            ..TaskQuery::default()
7064        };
7065        Ok(self
7066            .narrowed(&wanted)
7067            .await?
7068            .unwrap_or_default()
7069            .into_iter()
7070            .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
7071            .collect())
7072    }
7073
7074    /// Every item of this board that may carry `origin` — a superset of those that do — found
7075    /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
7076    ///
7077    /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
7078    /// which reads the field every carrier holds, whichever release wrote it — and the
7079    /// board-scoped issue search for the same id as a phrase in the body, where this source
7080    /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
7081    /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
7082    /// query's, exactly.
7083    ///
7084    /// Both connections are walked to exhaustion, each from its own cursor. One that has
7085    /// already ended is sent its last cursor again, which answers an empty page, so the one
7086    /// document serves every page of either. What the two leave is stated in the module
7087    /// documentation: a carrier another process added within the last second or two, before
7088    /// either index has it.
7089    async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
7090        let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
7091        let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
7092        let mut items_after: Option<String> = None;
7093        let mut search_after: Option<String> = None;
7094        let mut found: Vec<Resolved> = Vec::new();
7095        let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
7096            if !found.iter().any(|held| held.id == resolved.id) {
7097                found.push(resolved);
7098            }
7099        };
7100        loop {
7101            let data = self
7102                .graphql(
7103                    graphql::ORIGIN_LOOKUP,
7104                    json!({"owner":self.owner,"number":self.project_number,"filter":filter,
7105                           "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
7106                           "itemsAfter":items_after,"searchAfter":search_after,
7107                           "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
7108                           "duplicates":true}),
7109                )
7110                .await?;
7111            let items = data
7112                .pointer("/originItems/projectV2/items")
7113                .filter(|value| !value.is_null())
7114                .ok_or_else(|| SourceError::Refused {
7115                    message: format!(
7116                        "GitHub project {}/{} was not found or is not visible to the token",
7117                        self.owner, self.project_number
7118                    ),
7119                })?;
7120            for item in optional_nodes(Some(items), "project items")?
7121                .into_iter()
7122                .flatten()
7123            {
7124                // The board's own items list its drafts too, and a draft is not an issue: no
7125                // narrowed read answers with one, whatever its origin field holds.
7126                if let Some(resolved) = self.resolve(item)?
7127                    && resolved.content_kind == ContentKind::Issue
7128                {
7129                    keep(resolved, &mut found);
7130                }
7131            }
7132            let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
7133                message: "GitHub search response has no search connection".into(),
7134            })?;
7135            for node in optional_nodes(Some(searched), "search")?
7136                .into_iter()
7137                .flatten()
7138            {
7139                if let Some(resolved) = self.resolve_issue(node).await? {
7140                    keep(resolved, &mut found);
7141                }
7142            }
7143            let items_next = resumed(items, items_after.as_deref())?;
7144            let search_next = resumed(searched, search_after.as_deref())?;
7145            if !items_next.has_more() && !search_next.has_more() {
7146                return Ok(found);
7147            }
7148            items_after = items_next.cursor();
7149            search_after = search_next.cursor();
7150        }
7151    }
7152
7153    /// `found`, with every item this process created or wrote in its place, and every one of
7154    /// them the read did not report added.
7155    ///
7156    /// This process's own record wins over the read's copy of the same item, because a read
7157    /// of an item written moments ago can still be behind what was written onto it — the
7158    /// origin field included, which is the one a narrowed read is confirmed against — and a
7159    /// read that still names an item under a predicate this process's write moved it out of
7160    /// must not return it. The one thing the read knows that the record cannot is when GitHub
7161    /// last saw the item change, which is what a comment-activity read rules a candidate out
7162    /// by, so the read's `updatedAt` is kept when the record has none of its own. See
7163    /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
7164    fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
7165        // A board draft is not an issue, so no narrowed read returns one, and this process
7166        // having written one does not make it an answer either.
7167        let own: Vec<Resolved> = self
7168            .created()?
7169            .iter()
7170            .chain(self.updated()?.iter())
7171            .filter(|own| own.content_kind == ContentKind::Issue)
7172            .cloned()
7173            .collect();
7174        for mut own in own {
7175            self.resolved_cache()?.insert(own.id.clone(), own.clone());
7176            match found.iter_mut().find(|read| read.id == own.id) {
7177                Some(read) => {
7178                    own.updated_at = own.updated_at.max(read.updated_at);
7179                    *read = own;
7180                }
7181                None => found.push(own),
7182            }
7183        }
7184        Ok(found)
7185    }
7186
7187    /// Whether `item` has a comment created or last edited at or after `since` — always, when
7188    /// there is no instant to hold it to.
7189    ///
7190    /// The candidate's own `updatedAt` is read first, because a comment written or edited at
7191    /// or after the instant moved it there: an issue not updated since holds no such comment,
7192    /// and its comments are never asked for — unless this process commented on it in this
7193    /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
7194    /// only as far as the first that matches. A board draft is not an issue and has no
7195    /// comments, so it never matches.
7196    async fn commented_since(
7197        &self,
7198        item: &Resolved,
7199        since: Option<DateTime<Utc>>,
7200    ) -> Result<bool, SourceError> {
7201        let Some(since) = since else {
7202            return Ok(true);
7203        };
7204        if item.content_kind == ContentKind::DraftIssue {
7205            return Ok(false);
7206        }
7207        // An `updatedAt` this process's own record or a lagging index holds can predate a
7208        // comment this process wrote since, so only an issue it did not comment on is ruled
7209        // out by one.
7210        if item.updated_at.is_some_and(|updated| updated < since)
7211            && !self.commented()?.contains(&item.id)
7212        {
7213            return Ok(false);
7214        }
7215        let query = TaskQuery {
7216            commented_since: Some(since),
7217            ..TaskQuery::default()
7218        };
7219        let mut after: Option<String> = None;
7220        loop {
7221            let data = self
7222                .graphql(
7223                    graphql::ISSUE_COMMENTS,
7224                    json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
7225                )
7226                .await?;
7227            let Some(connection) = data
7228                .get("node")
7229                .filter(|value| !value.is_null())
7230                .and_then(|node| node.get("comments"))
7231                .filter(|value| !value.is_null())
7232            else {
7233                // Removed since the search reported it: no longer an issue with comments.
7234                return Ok(false);
7235            };
7236            let comments = optional_nodes(Some(connection), "issue comments")?
7237                .into_iter()
7238                .flatten()
7239                .map(comment_from)
7240                .collect::<Result<Vec<_>, _>>()?;
7241            if query.comments_match(&comments) {
7242                return Ok(true);
7243            }
7244            match next_cursor(connection)? {
7245                Some(next) => {
7246                    validate_cursor_progress(after.as_deref(), &next.0)?;
7247                    after = Some(next.0);
7248                }
7249                None => return Ok(false),
7250            }
7251        }
7252    }
7253
7254    /// Every item on the board: the union of both enumerations GitHub offers of one.
7255    ///
7256    /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
7257    /// board **draft** and reads the board's own fields beside its items, and only the search
7258    /// reports an item that connection is behind on. The module documentation is where the lag and the
7259    /// measurements behind it are written down.
7260    ///
7261    /// A search result is admitted on the same terms as any other issue this source reaches
7262    /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
7263    /// names *this* board — so an issue the index still believes is here after it was taken
7264    /// off is refused rather than reported.
7265    ///
7266    /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
7267    /// which is what the cache could otherwise have broken.
7268    async fn board(&self) -> Result<Board, SourceError> {
7269        self.board_completed_by(None).await
7270    }
7271
7272    /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
7273    /// that half could add is then answered only if it holds.
7274    async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
7275        let cached = self.board_cache()?.clone();
7276        let mut board = match cached {
7277            Some(board) => board,
7278            None => {
7279                let read = self.read_board().await?;
7280                *self.board_cache()? = Some(read.clone());
7281                read
7282            }
7283        };
7284        let searched = match also {
7285            None => self.searched_issues().await?,
7286            Some(also) => self.searched_narrowly(also).await?,
7287        };
7288        // A complete sweep asks for every page of this view. Reconcile ids once per
7289        // view, rather than scanning the board for every search result on every page.
7290        let mut ids: std::collections::HashSet<_> =
7291            board.items.iter().map(|item| item.id.clone()).collect();
7292        for held in searched {
7293            if ids.insert(held.id.clone()) {
7294                board.items.push(held);
7295            }
7296        }
7297        for own in self.created()?.iter() {
7298            if ids.insert(own.id.clone()) {
7299                board.items.push(own.clone());
7300            }
7301        }
7302        Ok(board)
7303    }
7304
7305    /// This process's own view of the board, or the refusal a poisoned lock is.
7306    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
7307        self.board_cache
7308            .lock()
7309            .map_err(|_| SourceError::Unavailable {
7310                message: "this source's view of the board was left inconsistent by an earlier \
7311                      failure; next: run the command again"
7312                    .into(),
7313            })
7314    }
7315
7316    /// Bring this process's own view of the board up to an item it has just written.
7317    ///
7318    /// A created item goes to `created`, which is what completes a board read GitHub's own
7319    /// eventual consistency has left behind. An item that was already there is replaced
7320    /// where it sits, so a second write of it in the same command reads its real parent
7321    /// rather than the one it had before the first write.
7322    ///
7323    /// "Where it sits" is three places, and missing an earlier one leaves a stale record
7324    /// that wins: an item this same run created is held in `created` and not in the cached
7325    /// board, and `board` completes the cached board *from* `created`, so replacing only
7326    /// the cached copy of such an item replaces nothing and the read still reports the
7327    /// title it was created with. The search is the third, and it is the one an item the
7328    /// board's own projection is behind on sits in *alone* — which is exactly the item this
7329    /// source is least able to re-read, so leaving it out would put the stale title back on
7330    /// the only items the completion in [`Self::board`] exists for.
7331    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
7332        self.resolved_cache()?.insert(item.id.clone(), item.clone());
7333        if created {
7334            self.created()?.push(item);
7335            return Ok(());
7336        }
7337        {
7338            let mut own = self.created()?;
7339            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
7340                *held = item;
7341                return Ok(());
7342            }
7343        }
7344        {
7345            let mut own = self.updated()?;
7346            match own.iter_mut().find(|held| held.id == item.id) {
7347                Some(held) => *held = item.clone(),
7348                None => own.push(item.clone()),
7349            }
7350        }
7351        if let Some(board) = self.board_cache()?.as_mut()
7352            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
7353        {
7354            *held = item.clone();
7355        }
7356        if let Some(found) = self.search_cache()?.as_mut()
7357            && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
7358        {
7359            *held = item.clone();
7360        }
7361        for found in self.narrowed_cache()?.values_mut() {
7362            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7363                *held = item.clone();
7364            }
7365        }
7366        for (_, found) in self.children_cache()?.values_mut() {
7367            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7368                *held = item.clone();
7369            }
7370        }
7371        Ok(())
7372    }
7373
7374    /// Forget one item this process has just deleted, from every half of its own view.
7375    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
7376        if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
7377            created.retain(|held| held.content != *id);
7378        }
7379        self.resolved_cache()?.remove(id);
7380        self.read_ahead()?.remove(id);
7381        self.carried()?.remove(id);
7382        self.created()?.retain(|own| own.id != *id);
7383        self.updated()?.retain(|own| own.id != *id);
7384        self.commented()?.retain(|own| own != id);
7385        if let Some(board) = self.board_cache()?.as_mut() {
7386            board.items.retain(|item| item.id != *id);
7387        }
7388        if let Some(found) = self.search_cache()?.as_mut() {
7389            found.retain(|item| item.id != *id);
7390        }
7391        for found in self.narrowed_cache()?.values_mut() {
7392            found.retain(|item| item.id != *id);
7393        }
7394        for (_, found) in self.children_cache()?.values_mut() {
7395            found.retain(|item| item.id != *id);
7396        }
7397        Ok(())
7398    }
7399
7400    /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
7401    fn narrowed_cache(
7402        &self,
7403    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
7404        self.narrowed_cache
7405            .lock()
7406            .map_err(|_| SourceError::Unavailable {
7407                message: "this source's view of a narrowed read was left inconsistent by an \
7408                      earlier failure; next: run the command again"
7409                    .into(),
7410            })
7411    }
7412
7413    /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
7414    /// is.
7415    /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
7416    fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
7417        Ok(self.task_children()?.get(project).cloned())
7418    }
7419
7420    fn task_children(
7421        &self,
7422    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
7423        self.task_children
7424            .lock()
7425            .map_err(|_| SourceError::Unavailable {
7426                message: "this source's record of a project's tasks was left inconsistent by an \
7427                          earlier failure; next: run the command again"
7428                    .into(),
7429            })
7430    }
7431
7432    fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
7433        self.children_cache
7434            .lock()
7435            .map_err(|_| SourceError::Unavailable {
7436                message: "this source's view of a project's tasks was left inconsistent by an \
7437                      earlier failure; next: run the command again"
7438                    .into(),
7439            })
7440    }
7441
7442    /// Every page of the board, read from GitHub.
7443    async fn read_board(&self) -> Result<Board, SourceError> {
7444        let mut after: Option<String> = None;
7445        let mut items = Vec::new();
7446        let mut board;
7447        loop {
7448            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
7449            for item in page
7450                .pointer("/items/nodes")
7451                .and_then(Value::as_array)
7452                .ok_or_else(|| SourceError::Malformed {
7453                    message: "GitHub project items.nodes is not an array".into(),
7454                })?
7455            {
7456                if let Some(resolved) = self.resolve(item)? {
7457                    items.push(resolved);
7458                }
7459            }
7460            let info = page
7461                .pointer("/items/pageInfo")
7462                .ok_or_else(|| SourceError::Malformed {
7463                    message: "GitHub project items have no pageInfo".into(),
7464                })?;
7465            let has_next = required_bool(info, "hasNextPage")?;
7466            let next = has_next
7467                .then(|| required_str(info, "endCursor"))
7468                .transpose()?;
7469            board = page.clone();
7470            match next {
7471                Some(next) => {
7472                    validate_cursor_progress(after.as_deref(), next)?;
7473                    after = Some(next.to_owned());
7474                }
7475                None => break,
7476            }
7477        }
7478        Ok(Board {
7479            id: required_str(&board, "id")?.to_owned(),
7480            fields: board.get("fields").cloned().unwrap_or(Value::Null),
7481            items,
7482        })
7483    }
7484
7485    /// The existing items this source has written, for completing a narrowed read that is
7486    /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
7487    fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7488        self.updated.lock().map_err(|_| SourceError::Unavailable {
7489            message: "this source's record of what it wrote in this run was left inconsistent \
7490                      by an earlier failure; next: run the command again"
7491                .into(),
7492        })
7493    }
7494
7495    /// The issues this source has commented on in this command; see
7496    /// [`Self::commented`](GitHubProjectsSource::commented).
7497    fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
7498        self.commented.lock().map_err(|_| SourceError::Unavailable {
7499            message: "this source's record of what it commented on in this run was left \
7500                      inconsistent by an earlier failure; next: run the command again"
7501                .into(),
7502        })
7503    }
7504
7505    /// Called only once GitHub has answered the comment write, so an issue whose comment
7506    /// failed is never made a candidate a later read would pay a node read for.
7507    fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
7508        let mut commented = self.commented()?;
7509        if !commented.contains(issue) {
7510            commented.push(issue.clone());
7511        }
7512        Ok(())
7513    }
7514
7515    /// The items this source has created, for completing a board read that is behind.
7516    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7517        self.created.lock().map_err(|_| SourceError::Unavailable {
7518            message: "this source's record of what it created in this run was left \
7519                      inconsistent by an earlier failure; next: run the command again"
7520                .into(),
7521        })
7522    }
7523
7524    /// One board item as this source reports it, or `None` for content it ignores.
7525    ///
7526    /// A pull request is neither a project nor a task — it is somebody's change, not a
7527    /// unit of plan — and an item whose content the token cannot see has nothing to
7528    /// report at all.
7529    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7530        let resolved = self.resolve_unremembered(item)?;
7531        if let Some(resolved) = &resolved {
7532            self.resolved_cache()?
7533                .insert(resolved.id.clone(), resolved.clone());
7534        }
7535        Ok(resolved)
7536    }
7537
7538    /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
7539    /// write's own response needs, which says nothing this process should answer a later read
7540    /// with.
7541    fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7542        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
7543            message: "GitHub project item is missing content".into(),
7544        })?;
7545        if content.is_null() {
7546            return Ok(None);
7547        }
7548        let content_kind = match required_str(content, "__typename")? {
7549            "Issue" => ContentKind::Issue,
7550            "DraftIssue" => ContentKind::DraftIssue,
7551            _ => return Ok(None),
7552        };
7553        let field_values = item
7554            .get("fieldValues")
7555            .ok_or_else(|| SourceError::Malformed {
7556                message: "GitHub project item is missing fieldValues".into(),
7557            })?;
7558        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
7559        let nodes = field_values
7560            .get("nodes")
7561            .and_then(Value::as_array)
7562            .ok_or_else(|| SourceError::Malformed {
7563                message: "GitHub project item fieldValues.nodes is not an array".into(),
7564            })?;
7565        if let Some(labels) = content.get("labels") {
7566            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
7567        }
7568        let raw_body = optional_str(content, "body")?.map(str::to_owned);
7569        let (body, slot) = self.visible(raw_body.clone())?;
7570        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
7571            .map(|id| NativeId(id.to_owned()));
7572        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
7573        // to read one from; it is a task, and never a project.
7574        let sub_issues = match content_kind {
7575            ContentKind::Issue => sub_issue_total(content)?,
7576            ContentKind::DraftIssue => 0,
7577        };
7578        let content_id = required_str(content, "id")?;
7579        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7580            message: format!("GitHub issue {content_id}: {message}"),
7581        })?;
7582        let raw_title = required_str(content, "title")?;
7583        let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
7584        // The title a person wrote, which for a document is the one without the prefix —
7585        // the same way `content` above is the body without this source's metadata slot.
7586        let title = match kind {
7587            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
7588            BoardKind::Work(_) => raw_title.to_owned(),
7589        };
7590        let own_repository = content
7591            .pointer("/repository/nameWithOwner")
7592            .and_then(Value::as_str)
7593            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
7594            .transpose()
7595            .map_err(|message| SourceError::Malformed { message })?;
7596        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
7597            Repository::from_metadata(&slot)
7598                .map_err(|message| SourceError::Malformed { message })?
7599        } else {
7600            own_repository.clone().into_iter().collect()
7601        };
7602        let classification = Classification::from_metadata(&slot)
7603            .map_err(|message| SourceError::Malformed { message })?;
7604        let id = NativeId(content_id.to_owned());
7605        // Read only for a task, because only a task has either list: a project or a
7606        // document holding one of these keys holds nothing this source reports, and the
7607        // keys are left out of its caller-visible metadata all the same.
7608        let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
7609            let listed = |key: &str| {
7610                TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
7611                    .map_err(|message| SourceError::Malformed { message })
7612            };
7613            (
7614                listed(TaskRef::DELIVERS_KEY)?,
7615                listed(TaskRef::DELIVERED_BY_KEY)?,
7616            )
7617        } else {
7618            (Vec::new(), Vec::new())
7619        };
7620        let (option, closed, reason) = Self::status_parts(nodes, content)?;
7621        let priority = self.held_priority(nodes)?;
7622        let item_labels = labels(content)?;
7623        // Present when the item was reached through its own issue, whose board entry
7624        // names the board; a read of the board's own items has the board already. An
7625        // empty id names nothing a field write could address, so it is read as absent and
7626        // the write goes back to reading the board.
7627        let board_id = item
7628            .pointer("/project/id")
7629            .and_then(Value::as_str)
7630            .filter(|id| !id.is_empty());
7631        let resolved = Resolved {
7632            item_id: required_str(item, "id")?.to_owned(),
7633            id,
7634            content_kind,
7635            kind,
7636            title,
7637            body: body.filter(|value| !value.is_empty()),
7638            raw_body,
7639            status: self
7640                .statuses
7641                .status(kind.status_kind(), option, closed, reason),
7642            option: option.map(str::to_owned),
7643            priority,
7644            closed,
7645            reason: closed.then(|| reason.map(str::to_owned)).flatten(),
7646            delivers,
7647            delivered_by,
7648            labels: item_labels.clone(),
7649            parent,
7650            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
7651            projected: self.held_projections(nodes)?,
7652            number: match content_kind {
7653                ContentKind::Issue => Some(issue_number(content)?),
7654                // A draft is filed in no repository, so nothing ever numbered it:
7655                // `DraftIssue` declares no `number` at all, exactly as it declares no
7656                // `subIssuesSummary` the branch above reads.
7657                ContentKind::DraftIssue => None,
7658            },
7659            url: optional_str(content, "url")?.map(str::to_owned),
7660            created_at: optional_time(content, "createdAt")?,
7661            updated_at: optional_time(content, "updatedAt")?,
7662            item_updated_at: optional_time(item, "updatedAt")?,
7663            comment_count: content
7664                .pointer("/commentCount/totalCount")
7665                .and_then(Value::as_u64),
7666            relationship_events: content
7667                .pointer("/relationshipEvents/totalCount")
7668                .and_then(Value::as_u64),
7669            // The issue node is stamped wherever it was read; a board item a read of the issue
7670            // put together from its memberships is not.
7671            served_at: match optional_time(content, SERVED_AT)? {
7672                Some(served) => Some(served),
7673                None => optional_time(item, SERVED_AT)?,
7674            },
7675            // Both connections were read to their end above — one reporting a further page is
7676            // refused — so these are every field value and every label the item holds. A
7677            // response that did not select the labels at all says nothing of them, and gives
7678            // no facets.
7679            facets: content.get("labels").is_some().then(|| {
7680                change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
7681            }),
7682            own_repository,
7683            repositories,
7684            classification,
7685            slot,
7686            board_id: board_id.map(str::to_owned),
7687            fields: field_definitions(nodes),
7688            board_fields: Self::carried_board_fields(content, board_id)?,
7689            blocked_by: carried_blocked_by(content)?,
7690        };
7691        Ok(Some(resolved))
7692    }
7693
7694    /// The field definitions of the board `board_id` names — the project this issue's own
7695    /// board item is on — off the `boards` page a read of an issue by its own id carries, or
7696    /// `None` when the read carried none, carried no entry for that board, or the board item
7697    /// named no board, which a write then answers by reading the board's fields itself.
7698    ///
7699    /// Matched by the board's node id and never by its number alone: a project number is
7700    /// unique only within its owner, so another owner's board numbered alike can sit on the
7701    /// same page, and its field and option ids address nothing on this one.
7702    fn carried_board_fields(
7703        content: &Value,
7704        board_id: Option<&str>,
7705    ) -> Result<Option<Value>, SourceError> {
7706        let (Some(nodes), Some(board_id)) = (
7707            content.pointer("/boards/nodes").and_then(Value::as_array),
7708            board_id,
7709        ) else {
7710            return Ok(None);
7711        };
7712        let Some(board) = nodes.iter().find_map(|node| {
7713            let project = node.get("project")?;
7714            (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
7715        }) else {
7716            return Ok(None);
7717        };
7718        let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
7719            return Ok(None);
7720        };
7721        complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
7722        Ok(Some(fields.clone()))
7723    }
7724
7725    /// What one board item's `Priority` field says, through this instance's mapping.
7726    ///
7727    /// An instance with no mapping holds no priority, so every item reads as `none` whatever
7728    /// its board holds. With one, no value is `none`, a mapped option is its level, and an
7729    /// option the mapping does not name is kept as itself — never read as a level or as
7730    /// `none` — for a read of the task to report by name.
7731    fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
7732        let Some(mapping) = &self.priorities else {
7733            return Ok(HeldPriority::Read(Priority::None));
7734        };
7735        // A value of the field that names no option — a text field someone called `Priority` —
7736        // is malformed rather than `none`: reading it as no priority would let the next copy
7737        // clear one a person set.
7738        let Some(option) = field_values
7739            .iter()
7740            .find(|value| {
7741                value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
7742            })
7743            .map(|value| required_str(value, "name"))
7744            .transpose()?
7745        else {
7746            return Ok(HeldPriority::Read(Priority::None));
7747        };
7748        Ok(mapping.priority_of(option).map_or_else(
7749            || HeldPriority::Unmapped(option.to_owned()),
7750            HeldPriority::Read,
7751        ))
7752    }
7753
7754    /// What each projected metadata field holds for one board item, by field name — every text
7755    /// value GitHub answers, an empty one included.
7756    fn held_projections(
7757        &self,
7758        field_values: &[Value],
7759    ) -> Result<BTreeMap<String, String>, SourceError> {
7760        let mut held = BTreeMap::new();
7761        for projection in &self.metadata_fields {
7762            if let Some(text) = text_field(field_values, &projection.field)? {
7763                held.insert(projection.field.to_string(), text);
7764            }
7765        }
7766        Ok(held)
7767    }
7768
7769    /// What one board item's status is read from: its `Status` option, whether its issue
7770    /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
7771    /// three into the status it reports.
7772    fn status_parts<'a>(
7773        field_values: &'a [Value],
7774        content: &'a Value,
7775    ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
7776        let option = field_values
7777            .iter()
7778            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
7779            .map(|value| required_str(value, "name"))
7780            .transpose()?;
7781        let closed = optional_str(content, "state")? == Some("CLOSED");
7782        Ok((option, closed, optional_str(content, "stateReason")?))
7783    }
7784
7785    /// The board Status option this write selects, or the refusal that says why not.
7786    ///
7787    /// The mapped option is required for both open and terminal targets. A terminal write
7788    /// validates it before changing either representation, so it can never fall back to
7789    /// closing an issue whose board cannot display the matching status.
7790    ///
7791    /// Answers the field's id, the option's id, and the option's name as the board spells
7792    /// it — which is the name a read of the item reports once it sits there.
7793    fn column_for(
7794        &self,
7795        fields: &Value,
7796        kind: ItemKind,
7797        category: StatusCategory,
7798        target: &StatusTarget,
7799    ) -> Result<Option<(String, String, String)>, SourceError> {
7800        let Some(wanted) = target.option() else {
7801            return Ok(None);
7802        };
7803        let missing = |detail: &str| SourceError::Refused {
7804            message: format!(
7805                "{} status {} of source {} needs the board Status option {wanted:?}, and \
7806                 {detail}; next: add that option to the board, which `onetaskgraph sources \
7807                 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
7808                 it has",
7809                kind.marker(),
7810                category_name(category),
7811                self.name,
7812                self.name,
7813                category_name(category),
7814                kind.marker()
7815            ),
7816        };
7817        let Some(field) = Board::field(fields, "Status")? else {
7818            return Err(missing("this board has no Status field"));
7819        };
7820        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7821            return Err(missing(
7822                "this board's Status field is not a single-select field",
7823            ));
7824        }
7825        let option = field
7826            .get("options")
7827            .and_then(Value::as_array)
7828            .and_then(|options| {
7829                options.iter().find(|option| {
7830                    option
7831                        .get("name")
7832                        .and_then(Value::as_str)
7833                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7834                })
7835            });
7836        match option {
7837            None => Err(missing("this board does not have it")),
7838            Some(option) => Ok(Some((
7839                required_str(field, "id")?.to_owned(),
7840                required_str(option, "id")?.to_owned(),
7841                required_str(option, "name")?.to_owned(),
7842            ))),
7843        }
7844    }
7845
7846    /// The refusal a status that closes an issue is answered with over a board draft.
7847    fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
7848        SourceError::Refused {
7849            message: format!(
7850                "status {} of source {} closes the item's issue, and GitHub draft items have \
7851                 no open or closed state",
7852                category_name(category),
7853                self.name
7854            ),
7855        }
7856    }
7857
7858    /// What a status write to one item needs of the board: the board's id and the
7859    /// definition of its `Status` field, read off the item when the item says both.
7860    ///
7861    /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
7862    /// and its `Status` value carries that field's definition, options and all. An item that
7863    /// does not say — no board id, or no `Status` value to read the field off — takes them
7864    /// from [`Self::board_fields`], which reads no item.
7865    async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7866        if let Some(board) = item.carried_board() {
7867            return Ok(board);
7868        }
7869        if item.defines("Status")
7870            && let Some(board_id) = item.named_board()
7871        {
7872            return Ok(BoardFields {
7873                id: board_id,
7874                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7875            });
7876        }
7877        self.board_fields().await
7878    }
7879
7880    /// Set one task's or one project's status and nothing else; see
7881    /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
7882    async fn set_status(
7883        &self,
7884        id: &NativeId,
7885        kind: ItemKind,
7886        category: StatusCategory,
7887    ) -> Result<Option<Status>, SourceError> {
7888        // Refused before anything is read, in the words a write of the same status is.
7889        let target = self.resolved_target(kind, category)?;
7890        // An item read by issue reference carries only what this write needs, so what it
7891        // becomes is never remembered for a later read to be answered from.
7892        let held = self.take_held(id)?;
7893        let by_reference = held.is_some();
7894        let bound = match held {
7895            Some(item) => Some(item),
7896            None => self.bound_item(id).await?,
7897        };
7898        let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
7899            return Ok(None);
7900        };
7901        let board = self.status_board(&item).await?;
7902        let (field, option, name) = self
7903            .column_for(&board.fields, kind, category, &target)?
7904            .ok_or_else(|| SourceError::Malformed {
7905                message: format!(
7906                    "status {} of source {} names no board Status option",
7907                    category_name(category),
7908                    self.name
7909                ),
7910            })?;
7911        if item.status.category == category && item.option.as_deref() == Some(&name) {
7912            return Ok(Some(item.status));
7913        }
7914        match &target {
7915            StatusTarget::Terminal(_, reason) => {
7916                if item.content_kind == ContentKind::DraftIssue {
7917                    return Err(self.closes_a_draft(category));
7918                }
7919                // The option first, so a refused selection leaves an open issue open rather
7920                // than closed behind a card that still reads open.
7921                self.set_item_field(
7922                    board.id.as_str(),
7923                    &item.item_id,
7924                    &field,
7925                    json!({"singleSelectOptionId": option}),
7926                )
7927                .await?;
7928                // An issue already closed in a way that reads as this category keeps its state
7929                // and its reason: only its card was out of line, and closing it again would
7930                // overwrite a reason — a duplicate's — that says more than this write does.
7931                let already = item.closed && item.status.category == category;
7932                if !already {
7933                    self.update_content(
7934                        ContentKind::Issue,
7935                        &item.id,
7936                        json!({"stateInput": state_input(Some(&target))}),
7937                    )
7938                    .await?;
7939                }
7940                let reason = if already {
7941                    item.reason.clone()
7942                } else {
7943                    Some(reason.reason().to_owned())
7944                };
7945                item.closed = true;
7946                item.status = self
7947                    .statuses
7948                    .status(kind, Some(&name), true, reason.as_deref());
7949                item.option = Some(name);
7950                item.reason = reason;
7951            }
7952            StatusTarget::Column(_) => {
7953                // An option is what an open item's status is, so a closed issue is reopened
7954                // first — sitting closed in the column, it would read back as closed. A draft has
7955                // no state to reopen.
7956                if item.content_kind == ContentKind::Issue && item.closed {
7957                    self.update_content(
7958                        ContentKind::Issue,
7959                        &item.id,
7960                        json!({"stateInput": state_input(Some(&target))}),
7961                    )
7962                    .await?;
7963                    item.closed = false;
7964                }
7965                self.set_item_field(
7966                    board.id.as_str(),
7967                    &item.item_id,
7968                    &field,
7969                    json!({"singleSelectOptionId": option}),
7970                )
7971                .await?;
7972                item.status = self.statuses.status(kind, Some(&name), false, None);
7973                item.option = Some(name);
7974            }
7975            StatusTarget::Disabled(_) => {
7976                unreachable!("resolved_target refused a disabled status")
7977            }
7978        }
7979        let status = item.status.clone();
7980        if !by_reference {
7981            self.remember_written(item, false)?;
7982        }
7983        Ok(Some(status))
7984    }
7985
7986    /// Replace one task's `delivered_by` and nothing else; see
7987    /// [`TaskSource::set_delivered_by`].
7988    ///
7989    /// One update of the body, which differs from the body GitHub holds only inside the
7990    /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
7991    async fn replace_delivered_by(
7992        &self,
7993        id: &NativeId,
7994        delivered_by: &[TaskRef],
7995    ) -> Result<Option<()>, SourceError> {
7996        let entries = TaskRef::listed(
7997            TaskRef::DELIVERED_BY_KEY,
7998            id,
7999            Some(&self.name),
8000            delivered_by.to_vec(),
8001        )
8002        .map_err(|message| SourceError::Refused { message })?;
8003        let Some(mut item) = self
8004            .bound_item(id)
8005            .await?
8006            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8007        else {
8008            return Ok(None);
8009        };
8010        let mut slot = item.slot.clone();
8011        set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
8012        self.write_slot(&mut item, &slot).await?;
8013        item.delivered_by = entries;
8014        self.remember_written(item, false)?;
8015        Ok(Some(()))
8016    }
8017
8018    /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
8019    /// see [`TaskSource::set_task_metadata`].
8020    ///
8021    /// `None` when this board holds no item by that id, or holds one of another kind. The
8022    /// answer is the item as this source now reads it, so what a caller is told the key
8023    /// holds is what the slot holds.
8024    ///
8025    /// A key already holding the value is answered without a write, compared as JSON rather
8026    /// than as the body's bytes: a slot a person spelled with other whitespace would
8027    /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
8028    async fn set_slot_key(
8029        &self,
8030        id: &NativeId,
8031        kind: BoardKind,
8032        key: &MetadataKey,
8033        value: &Value,
8034    ) -> Result<Option<Resolved>, SourceError> {
8035        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8036            return Ok(None);
8037        };
8038        if item.slot.get(key.as_str()) == Some(value) {
8039            return Ok(Some(item));
8040        }
8041        let mut slot = item.slot.clone();
8042        slot.insert(key.as_str().to_owned(), value.clone());
8043        // A key a board text field projects moves that field first, so the body — the value's
8044        // one authoritative home — is written last, as every write of an existing item is.
8045        if self
8046            .metadata_fields
8047            .iter()
8048            .any(|projection| &*projection.key == key.as_str())
8049        {
8050            let board = self.projection_board(&item).await?;
8051            let (writes, projected) = self.projection_writes(
8052                &board.fields,
8053                Some(&item.projected),
8054                &slot,
8055                Some(&[key.as_str()]),
8056            )?;
8057            let (mut values, mut clears) = (Vec::new(), Vec::new());
8058            for write in &writes {
8059                write.join(&mut values, &mut clears);
8060            }
8061            self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
8062                .await?;
8063            item.projected = projected;
8064        }
8065        self.write_slot(&mut item, &slot).await?;
8066        self.remember_written(item.clone(), false)?;
8067        Ok(Some(item))
8068    }
8069
8070    /// What a projection write to `item` needs of the board, read off the item when it says
8071    /// enough, as [`Self::fields_for`] reads it.
8072    async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
8073        if let Some(board) = item.carried_board() {
8074            return Ok(board);
8075        }
8076        if let Some(board_id) = item.named_board()
8077            && self
8078                .metadata_fields
8079                .iter()
8080                .all(|projection| item.defines(&projection.field))
8081        {
8082            return Ok(BoardFields {
8083                id: board_id,
8084                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8085            });
8086        }
8087        self.board_fields().await
8088    }
8089
8090    /// Put `slot` in one item's metadata slot with a single update of its body, and bring
8091    /// `item` up to what that write left.
8092    ///
8093    /// The body sent differs from the body GitHub holds only inside the slot — see
8094    /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
8095    /// the mutation the item's content takes, so a board draft's body is written with
8096    /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
8097    async fn write_slot(
8098        &self,
8099        item: &mut Resolved,
8100        slot: &BTreeMap<String, Value>,
8101    ) -> Result<(), SourceError> {
8102        let held = item.raw_body.clone().unwrap_or_default();
8103        let body = with_slot(&held, slot)?;
8104        if body != held {
8105            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8106                .await?;
8107        }
8108        let (visible, slot) = self.visible(Some(body.clone()))?;
8109        item.body = visible.filter(|value| !value.is_empty());
8110        item.raw_body = Some(body);
8111        item.slot = slot;
8112        Ok(())
8113    }
8114
8115    /// This instance's target for a category written to an item of `kind`, refusing one
8116    /// that kind has no option for — before anything is read or written.
8117    ///
8118    /// Nothing here mutates the board's option set to make room for a status. GitHub
8119    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
8120    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
8121    /// field and every item's status.
8122    fn resolved_target(
8123        &self,
8124        kind: ItemKind,
8125        category: StatusCategory,
8126    ) -> Result<StatusTarget, SourceError> {
8127        let target = self.statuses.target(kind, category).clone();
8128        let StatusTarget::Disabled(why) = target else {
8129            return Ok(target);
8130        };
8131        let refusal = why.refusal(&self.name, category, kind);
8132        // Why there is no shipped default, which is the question a person meeting this
8133        // refusal on a source that never mentioned the category asks.
8134        let shipped_none = match category {
8135            StatusCategory::Draft => Some(
8136                "draft has no shipped default because GitHub draft issues cannot have \
8137                 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
8138            ),
8139            StatusCategory::Unknown => Some(
8140                "unknown has no shipped default because this board keeps no open-ended status \
8141                 word: every word classified unknown is written to the one board Status option \
8142                 status_mapping.unknown names",
8143            ),
8144            _ => None,
8145        };
8146        Err(match (refusal, shipped_none, why) {
8147            (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
8148                SourceError::Refused {
8149                    message: format!("{message}; {note}"),
8150                }
8151            }
8152            (refusal, _, _) => refusal,
8153        })
8154    }
8155
8156    /// What writing `priority` does to one item's `Priority` field on this board, or the
8157    /// refusal naming what the board lacks.
8158    ///
8159    /// `none` is no value, so it clears the field — and asks nothing of an item that holds
8160    /// none already, or of an item not created yet. Every other priority selects the option
8161    /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
8162    /// without that option, is refused rather than given one: reads and writes never create
8163    /// a field or an option.
8164    fn priority_write(
8165        &self,
8166        fields: &Value,
8167        existing: Option<&Resolved>,
8168        priority: Priority,
8169    ) -> Result<Option<PriorityWrite>, SourceError> {
8170        let Some(mapping) = &self.priorities else {
8171            return Err(self.holds_no_priority());
8172        };
8173        let Some(wanted) = mapping.option(priority) else {
8174            if !existing.is_some_and(Resolved::holds_priority) {
8175                return Ok(None);
8176            }
8177            let field =
8178                Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
8179                    message: format!(
8180                        "an item holding a {PRIORITY_FIELD} value was read without that field"
8181                    ),
8182                })?;
8183            return Ok(Some(PriorityWrite::Clear {
8184                field: required_str(field, "id")?.to_owned(),
8185            }));
8186        };
8187        let missing = |detail: &str| SourceError::Refused {
8188            message: format!(
8189                "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
8190                 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
8191                 it, or point priority_mapping.{priority} of this source at an option the board \
8192                 has",
8193                self.name, self.name
8194            ),
8195        };
8196        let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
8197            return Err(missing(&format!(
8198                "this board has no {PRIORITY_FIELD} field"
8199            )));
8200        };
8201        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
8202            return Err(missing(&format!(
8203                "this board's {PRIORITY_FIELD} field is not a single-select field"
8204            )));
8205        }
8206        // An options list that is absent or not a list is an answer this source cannot read,
8207        // not a board lacking the option: `sources fields --apply` is no remedy for it.
8208        let option = field
8209            .get("options")
8210            .and_then(Value::as_array)
8211            .ok_or_else(|| SourceError::Malformed {
8212                message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
8213            })?
8214            .iter()
8215            .find(|option| {
8216                option
8217                    .get("name")
8218                    .and_then(Value::as_str)
8219                    .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
8220            })
8221            .ok_or_else(|| missing("this board does not have it"))?;
8222        Ok(Some(PriorityWrite::Select {
8223            field: required_str(field, "id")?.to_owned(),
8224            option: required_str(option, "id")?.to_owned(),
8225        }))
8226    }
8227
8228    /// What writing `metadata` does to each projected text field of one item, and what those
8229    /// fields hold after it — or the refusal, before anything is sent.
8230    ///
8231    /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
8232    /// narrows the work to the entries projecting one of those metadata keys, for a write that
8233    /// touches only them; `None` is every entry. A string the field already holds, and nothing
8234    /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
8235    /// and so is a board without the field, or with a field of that name that is not a text
8236    /// field: reads and writes never create a field.
8237    fn projection_writes(
8238        &self,
8239        fields: &Value,
8240        held: Option<&BTreeMap<String, String>>,
8241        metadata: &BTreeMap<String, Value>,
8242        keys: Option<&[&str]>,
8243    ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
8244        let mut projected = held.cloned().unwrap_or_default();
8245        let mut writes = Vec::new();
8246        for projection in self
8247            .metadata_fields
8248            .iter()
8249            .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
8250        {
8251            let wanted = projection.projected(metadata, &self.name)?;
8252            let field = self.projection_field(fields, projection)?;
8253            let holding = projected.get(&*projection.field);
8254            match wanted {
8255                Projected::Text(text) if holding != Some(&text) => {
8256                    writes.push(ProjectionWrite::Set {
8257                        field,
8258                        text: text.clone(),
8259                    });
8260                    projected.insert(projection.field.to_string(), text);
8261                }
8262                Projected::Nothing if holding.is_some() => {
8263                    writes.push(ProjectionWrite::Clear { field });
8264                    projected.remove(&*projection.field);
8265                }
8266                Projected::Text(_) | Projected::Nothing => {}
8267            }
8268        }
8269        Ok((writes, projected))
8270    }
8271
8272    /// The id of the board text field one projection writes, or the refusal naming what the
8273    /// board has instead.
8274    fn projection_field(
8275        &self,
8276        fields: &Value,
8277        projection: &MetadataField,
8278    ) -> Result<String, SourceError> {
8279        let refuse = |detail: String| SourceError::Refused {
8280            message: format!(
8281                "source {} projects metadata key {:?} onto the board text field {:?}, and \
8282                 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
8283                 text field, or name another field in metadata_fields",
8284                self.name, &*projection.key, &*projection.field, self.name
8285            ),
8286        };
8287        let Some(field) = Board::field(fields, &projection.field)? else {
8288            return Err(refuse("this board has no field of that name".to_owned()));
8289        };
8290        let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
8291        let data_type = optional_str(field, "dataType")?;
8292        if typename != "ProjectV2Field" || data_type != Some("TEXT") {
8293            return Err(refuse(format!(
8294                "this board's field of that name is not a text field (it is a {}), so it is \
8295                 left as it is; rename or remove that field first",
8296                data_type.unwrap_or(typename)
8297            )));
8298        }
8299        Ok(required_str(field, "id")?.to_owned())
8300    }
8301
8302    /// Apply one priority write to one board item.
8303    async fn write_priority(
8304        &self,
8305        board_id: &str,
8306        item_id: &str,
8307        write: &PriorityWrite,
8308    ) -> Result<(), SourceError> {
8309        match write {
8310            PriorityWrite::Select { field, option } => {
8311                self.set_item_field(
8312                    board_id,
8313                    item_id,
8314                    field,
8315                    json!({"singleSelectOptionId": option}),
8316                )
8317                .await
8318            }
8319            PriorityWrite::Clear { field } => {
8320                let data = self
8321                    .graphql(
8322                        graphql::CLEAR_FIELD,
8323                        json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
8324                            "readPriority":false,"priorityName":PRIORITY_FIELD}),
8325                    )
8326                    .await?;
8327                let returned = data
8328                    .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
8329                    .ok_or_else(|| SourceError::Malformed {
8330                        message: "GitHub field clear returned no project item".into(),
8331                    })?;
8332                if required_str(returned, "id")? != item_id {
8333                    return Err(SourceError::Malformed {
8334                        message: "GitHub field clear returned the wrong project item".into(),
8335                    });
8336                }
8337                Ok(())
8338            }
8339        }
8340    }
8341
8342    /// The refusal a priority is answered with by an instance configured with no
8343    /// `priority_mapping`, which holds none.
8344    fn holds_no_priority(&self) -> SourceError {
8345        SourceError::Refused {
8346            message: format!(
8347                "source {} holds no task priority: its configuration sets no priority_mapping; \
8348                 next: set priority_mapping on this source, then run `onetaskgraph sources \
8349                 fields {} --apply` to set its board up",
8350                self.name, self.name
8351            ),
8352        }
8353    }
8354
8355    /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
8356    ///
8357    /// One field write — a select, or a clear for `none` — and no title, body, label, state
8358    /// or `Status` request. Clearing a priority an item does not hold sends nothing.
8359    async fn set_priority(
8360        &self,
8361        id: &NativeId,
8362        priority: Priority,
8363    ) -> Result<Option<Priority>, SourceError> {
8364        if self.priorities.is_none() {
8365            return Err(self.holds_no_priority());
8366        }
8367        let Some(mut item) = self
8368            .bound_item(id)
8369            .await?
8370            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8371        else {
8372            return Ok(None);
8373        };
8374        if priority == Priority::None && !item.holds_priority() {
8375            return Ok(Some(priority));
8376        }
8377        // The item's own read carries the field's definition whenever it holds a value of
8378        // it, which a clear always does; a select onto an item holding none reads the board.
8379        let board = match (item.carried_board(), item.named_board()) {
8380            (Some(board), _) => board,
8381            (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
8382                id,
8383                fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
8384            },
8385            _ => self.board_fields().await?,
8386        };
8387        let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
8388            return Ok(Some(priority));
8389        };
8390        let (document, root, input) = match write {
8391            PriorityWrite::Select { field, option } => (
8392                graphql::UPDATE_FIELD,
8393                "updateProjectV2ItemFieldValue",
8394                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
8395            ),
8396            PriorityWrite::Clear { field } => (
8397                graphql::CLEAR_FIELD,
8398                "clearProjectV2ItemFieldValue",
8399                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
8400            ),
8401        };
8402        let data = self
8403            .graphql(
8404                document,
8405                json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
8406            )
8407            .await?;
8408        let returned = data
8409            .get(root)
8410            .and_then(|value| value.get("projectV2Item"))
8411            .ok_or_else(|| SourceError::Malformed {
8412                message: "GitHub priority write returned no project item".into(),
8413            })?;
8414        if required_str(returned, "id")? != item.item_id {
8415            return Err(SourceError::Malformed {
8416                message: "GitHub priority write returned the wrong project item".into(),
8417            });
8418        }
8419        let value = returned
8420            .get("fieldValueByName")
8421            .ok_or_else(|| SourceError::Malformed {
8422                message: "GitHub priority write returned no priority read-back".into(),
8423            })?;
8424        if !value.is_null()
8425            && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
8426        {
8427            return Err(SourceError::Malformed {
8428                message: "GitHub priority read-back is not a Priority field value".into(),
8429            });
8430        }
8431        let values = if value.is_null() {
8432            Vec::new()
8433        } else {
8434            vec![value.clone()]
8435        };
8436        item.priority = self.held_priority(&values)?;
8437        let answer = item.task()?.priority;
8438        self.remember_written(item, false)?;
8439        Ok(Some(answer))
8440    }
8441
8442    /// Replace one task's visible body and nothing else; see
8443    /// [`TaskSource::set_task_content`].
8444    ///
8445    /// One update of the body, which differs from the body GitHub holds only outside the
8446    /// metadata slot — the slot is kept byte for byte, so every caller key and every list
8447    /// this source keeps there reads back as it was. A body that would not change is not
8448    /// sent at all.
8449    async fn replace_content(
8450        &self,
8451        id: &NativeId,
8452        content: &str,
8453    ) -> Result<Option<()>, SourceError> {
8454        let Some(mut item) = self
8455            .bound_item(id)
8456            .await?
8457            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8458        else {
8459            return Ok(None);
8460        };
8461        let held = item.raw_body.clone().unwrap_or_default();
8462        let body = with_content(&held, &self.marked(content))?;
8463        // Checked before anything is sent: content ending in what this source reads as its own
8464        // metadata slot would read back as metadata rather than as the content it was.
8465        let (visible, slot) = self.visible(Some(body.clone()))?;
8466        if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
8467            return Err(SourceError::Refused {
8468                message: format!(
8469                    "this content ends in what source {} reads as its own metadata slot \
8470                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8471                     as content; next: remove that trailing block from the content",
8472                    self.name
8473                ),
8474            });
8475        }
8476        if body != held {
8477            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8478                .await?;
8479        }
8480        item.body = visible.filter(|value| !value.is_empty());
8481        item.raw_body = Some(body);
8482        item.slot = slot;
8483        self.remember_written(item, false)?;
8484        Ok(Some(()))
8485    }
8486
8487    /// Apply one targeted update to one task; see [`TaskSource::update_task`].
8488    ///
8489    /// One read of the item — which carries the board's field definitions and the issue's
8490    /// `blockedBy`, so neither is read again — and then only what differs from it: the
8491    /// `Status` option and the `Priority` field together in one request, the `blockedBy`
8492    /// additions and removals the named edges differ by, and last one `updateIssue` carrying
8493    /// the title, the body — visible content and metadata slot together — and a state change.
8494    /// So an update naming any of title, body, metadata, status and priority is one read and
8495    /// at most two writes. The body goes last so that a write refused part-way leaves it, and
8496    /// the metadata in it, as it stood. A terminal status selects its option and then closes,
8497    /// as a whole write does; an open one selects its option and then reopens. The origin
8498    /// field is never written: an update is of an item that already exists, whose origin is
8499    /// what it is.
8500    ///
8501    /// The task answered is the item as those writes left it, built from the read and what was
8502    /// sent rather than read again — the same record a later read in this run answers from.
8503    async fn targeted_update(
8504        &self,
8505        id: &NativeId,
8506        update: &TaskUpdate,
8507    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
8508        // Everything this source can refuse without reading the item is refused first, in the
8509        // words a whole write of the same fields is refused with.
8510        update.consistent()?;
8511        if update
8512            .title
8513            .as_deref()
8514            .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
8515        {
8516            return Err(SourceError::Refused {
8517                message: format!(
8518                    "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8519                     spells a document, so it would read back as one rather than as a task; \
8520                     retitle it",
8521                    self.name
8522                ),
8523            });
8524        }
8525        if let Some(delivers) = &update.delivers {
8526            TaskRef::listed(
8527                TaskRef::DELIVERS_KEY,
8528                id,
8529                Some(&self.name),
8530                delivers.clone(),
8531            )
8532            .map_err(|message| SourceError::Refused { message })?;
8533        }
8534        if self.priorities.is_none()
8535            && update
8536                .priority
8537                .is_some_and(|priority| priority != Priority::None)
8538        {
8539            return Err(self.holds_no_priority());
8540        }
8541        let target = update
8542            .status
8543            .as_ref()
8544            .map(|status| self.resolved_target(ItemKind::Task, status.category))
8545            .transpose()?;
8546        let Some(mut item) = self
8547            .bound_item(id)
8548            .await?
8549            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8550        else {
8551            return Ok(None);
8552        };
8553        let before = item.task()?;
8554
8555        let mut status_move = None;
8556        if let (Some(status), Some(target)) = (&update.status, target) {
8557            let board = self.status_board(&item).await?;
8558            let (field, option, name) = self
8559                .column_for(&board.fields, ItemKind::Task, status.category, &target)?
8560                .ok_or_else(|| SourceError::Malformed {
8561                    message: format!(
8562                        "status {} of source {} names no board Status option",
8563                        category_name(status.category),
8564                        self.name
8565                    ),
8566                })?;
8567            let terminal = matches!(target, StatusTarget::Terminal(_, _));
8568            if terminal && item.content_kind == ContentKind::DraftIssue {
8569                return Err(self.closes_a_draft(status.category));
8570            }
8571            let landed = match &target {
8572                StatusTarget::Terminal(_, reason) => {
8573                    self.statuses
8574                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
8575                }
8576                _ => self
8577                    .statuses
8578                    .status(ItemKind::Task, Some(&name), false, None),
8579            };
8580            let option_moves = item
8581                .option
8582                .as_deref()
8583                .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
8584            let state_moves = item.content_kind == ContentKind::Issue
8585                && (item.closed != terminal || (terminal && item.status != landed));
8586            if let Some(moves) = Moves::of(option_moves, state_moves) {
8587                status_move = Some(StatusMove {
8588                    board: board.id,
8589                    field,
8590                    option,
8591                    name,
8592                    target,
8593                    landed,
8594                    moves,
8595                });
8596            }
8597        }
8598
8599        let mut priority_move = None;
8600        if let Some(priority) = update.priority
8601            && self.priorities.is_some()
8602            && item.priority != HeldPriority::Read(priority)
8603        {
8604            let board = match (item.carried_board(), item.named_board()) {
8605                (Some(board), _) => board,
8606                (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
8607                    id: board,
8608                    fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8609                },
8610                _ => self.board_fields().await?,
8611            };
8612            if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
8613                priority_move = Some((board.id, write, priority));
8614            }
8615        }
8616
8617        // The metadata keys this update names that a board text field projects, and what
8618        // that does to those fields — refused, as a whole write refuses it, before anything
8619        // is sent.
8620        let touched: Vec<&str> = update
8621            .metadata_set
8622            .keys()
8623            .chain(update.metadata_remove.iter())
8624            .map(MetadataKey::as_str)
8625            .collect();
8626        let mut projection_move = None;
8627        if self
8628            .metadata_fields
8629            .iter()
8630            .any(|projection| touched.contains(&&*projection.key))
8631        {
8632            let mut metadata = item.slot.clone();
8633            for (key, value) in &update.metadata_set {
8634                metadata.insert(key.as_str().to_owned(), value.clone());
8635            }
8636            for key in &update.metadata_remove {
8637                metadata.remove(key.as_str());
8638            }
8639            let board = self.projection_board(&item).await?;
8640            let (writes, projected) = self.projection_writes(
8641                &board.fields,
8642                Some(&item.projected),
8643                &metadata,
8644                Some(&touched),
8645            )?;
8646            projection_move = Some((board.id, writes, projected));
8647        }
8648
8649        // Resolved before the body is composed, because a far end `blockedBy` cannot name is
8650        // recorded in the slot, and the slot travels in the one body update below.
8651        let edges = match &update.depends_on {
8652            Some(edges) => Some(
8653                self.partition_edges(
8654                    BoardKind::Work(ItemKind::Task),
8655                    item.content_kind,
8656                    item.blocked_by.as_deref(),
8657                    edges,
8658                )
8659                .await?,
8660            ),
8661            None => None,
8662        };
8663
8664        // A record primed from what a producer wrote holds no body anybody read, so an update
8665        // changing the body reads it first: by the `Status` write's own answer when the update
8666        // moves the option — the mutation answering with the issue whole — and otherwise by
8667        // reading the issue. The body is then composed against what GitHub holds now.
8668        let mut option_selected = false;
8669        // The mark stays until a read lands, so an update that writes no body, or one whose
8670        // read fails, leaves the next update of it to read the body too.
8671        if (update.content.is_some()
8672            || !update.metadata_set.is_empty()
8673            || !update.metadata_remove.is_empty()
8674            || update.delivers.is_some()
8675            || update.repositories.is_some()
8676            || edges.is_some())
8677            && self.is_unread(id)
8678        {
8679            let live = match status_move.as_ref().filter(|moving| moving.moves.option()) {
8680                Some(moving) => {
8681                    option_selected = true;
8682                    Some(
8683                        self.select_reading_body(
8684                            moving.board.as_str(),
8685                            &item.item_id,
8686                            &moving.field,
8687                            &moving.option,
8688                        )
8689                        .await?,
8690                    )
8691                }
8692                None => {
8693                    self.forget(id)?;
8694                    let Some(read) = self.item_by_id(id).await? else {
8695                        return Ok(None);
8696                    };
8697                    read.raw_body
8698                }
8699            };
8700            let (visible, read_slot) = self.visible(live.clone())?;
8701            self.mark_read(id);
8702            item.raw_body = live;
8703            item.body = visible;
8704            item.slot = read_slot;
8705        }
8706
8707        let mut slot = item.slot.clone();
8708        for (key, value) in &update.metadata_set {
8709            slot.insert(key.as_str().to_owned(), value.clone());
8710        }
8711        for key in &update.metadata_remove {
8712            slot.remove(key.as_str());
8713        }
8714        if let Some(delivers) = &update.delivers {
8715            set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
8716        }
8717        // The list a whole write records, by the same rule: none when it is exactly the issue's
8718        // own repository, or empty with no issue repository to derive, and otherwise the list.
8719        if let Some(repositories) = &update.repositories {
8720            let derivable = item
8721                .own_repository
8722                .as_ref()
8723                .map_or(repositories.is_empty(), |own| {
8724                    repositories.as_slice() == std::slice::from_ref(own)
8725                });
8726            if derivable {
8727                slot.remove(Repository::METADATA_KEY);
8728            } else {
8729                slot.insert(
8730                    Repository::METADATA_KEY.to_owned(),
8731                    Value::Array(
8732                        repositories
8733                            .iter()
8734                            .map(|repository| Value::String(repository.as_str().to_owned()))
8735                            .collect(),
8736                    ),
8737                );
8738            }
8739        }
8740        if let Some((_, recorded)) = &edges {
8741            record_edges(&mut slot, recorded);
8742        }
8743        let held = item.raw_body.clone().unwrap_or_default();
8744        let content = match &update.content {
8745            Some(content) => with_content(&held, &self.marked(content))?,
8746            None => held.clone(),
8747        };
8748        // A slot holding what it held is kept byte for byte, compared as JSON rather than as
8749        // the body's bytes, as a metadata write compares it: a slot a person spelled with
8750        // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
8751        let body = if slot == item.slot {
8752            content
8753        } else {
8754            with_slot(&content, &slot)?
8755        };
8756        // Checked before anything is sent, as a content write checks it: content ending in
8757        // what this source reads as its own slot would read back as metadata.
8758        let (visible, read) = self.visible(Some(body.clone()))?;
8759        let wanted = update.content.as_deref().or(item.body.as_deref());
8760        if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
8761            return Err(SourceError::Refused {
8762                message: format!(
8763                    "this content ends in what source {} reads as its own metadata slot \
8764                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8765                     as content; next: remove that trailing block from the content",
8766                    self.name
8767                ),
8768            });
8769        }
8770        let recorded_moves =
8771            slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
8772
8773        // One `updateIssue` carries all three, because every mutation spends the secondary
8774        // limiter and the title, body and state are one mutation's inputs.
8775        let mut fields = serde_json::Map::new();
8776        if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
8777            fields.insert("title".to_owned(), json!(title));
8778        }
8779        if body != held {
8780            fields.insert("body".to_owned(), json!(body));
8781        }
8782        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
8783            fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
8784        }
8785        // **The body is written last, and that is the guarantee a refusal part-way keeps.**
8786        // GitHub runs no two requests as one, and runs one document's mutation fields in order
8787        // without undoing an earlier field when a later one fails — so a body written before a
8788        // board field the board then refused would be left changed. Written after every other
8789        // write has landed, a refusal anywhere leaves the item's body, and every metadata key
8790        // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
8791        // first, together in one request — a terminal option selected before the issue
8792        // closes, as a whole write does — then the `blockedBy` difference, then the body.
8793        let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
8794        let mut clears: Vec<(&BoardId, &str)> = Vec::new();
8795        if let Some(moving) = status_move
8796            .as_ref()
8797            .filter(|moving| moving.moves.option() && !option_selected)
8798        {
8799            board_writes.push((
8800                &moving.board,
8801                (
8802                    moving.field.clone(),
8803                    json!({"singleSelectOptionId": moving.option}),
8804                ),
8805            ));
8806        }
8807        match &priority_move {
8808            Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
8809                board,
8810                (field.clone(), json!({"singleSelectOptionId": option})),
8811            )),
8812            Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
8813            None => {}
8814        }
8815        if let Some((board, writes, _)) = &projection_move {
8816            for write in writes {
8817                let (mut values, mut cleared) = (Vec::new(), Vec::new());
8818                write.join(&mut values, &mut cleared);
8819                board_writes.extend(values.into_iter().map(|value| (board, value)));
8820                clears.extend(cleared.into_iter().map(|field| (board, field)));
8821            }
8822        }
8823        let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
8824        boards.extend(clears.iter().map(|(board, _)| *board));
8825        boards.dedup_by(|one, other| one.as_str() == other.as_str());
8826        for board in boards {
8827            let writes = board_writes
8828                .iter()
8829                .filter(|(on, _)| on.as_str() == board.as_str())
8830                .map(|(_, write)| write.clone())
8831                .collect::<Vec<_>>();
8832            let cleared = clears
8833                .iter()
8834                .filter(|(on, _)| on.as_str() == board.as_str())
8835                .map(|(_, field)| *field)
8836                .collect::<Vec<_>>();
8837            self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
8838                .await?;
8839        }
8840        let mut blocked_by_moved = false;
8841        if let Some((native, _)) = &edges
8842            && item.content_kind == ContentKind::Issue
8843        {
8844            blocked_by_moved = self
8845                .reconcile_blocked_by(
8846                    &item.id,
8847                    native,
8848                    Issue::Existing(item.blocked_by.as_deref()),
8849                )
8850                .await?;
8851        }
8852        if !fields.is_empty() {
8853            self.update_content(item.content_kind, &item.id, Value::Object(fields))
8854                .await?;
8855        }
8856
8857        // The `blockedBy` this item was read with is what the write just changed, so it is no
8858        // answer any more: a later write in this command would reconcile against it — re-adding
8859        // what it kept and never removing what it dropped — and a forward-edge read would
8860        // answer with it. Left unknown, each asks GitHub instead.
8861        if blocked_by_moved {
8862            item.blocked_by = None;
8863        }
8864        if let Some(title) = &update.title {
8865            item.title.clone_from(title);
8866        }
8867        item.body = visible.filter(|value| !value.is_empty());
8868        item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
8869        item.slot = slot;
8870        if let Some(delivers) = &update.delivers {
8871            item.delivers.clone_from(delivers);
8872        }
8873        if let Some(repositories) = &update.repositories {
8874            item.repositories.clone_from(repositories);
8875        }
8876        if let Some(moving) = status_move {
8877            item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
8878                && item.content_kind == ContentKind::Issue;
8879            item.status = moving.landed;
8880            item.option = Some(moving.name);
8881        }
8882        if let Some((_, _, priority)) = priority_move {
8883            item.priority = HeldPriority::Read(priority);
8884        }
8885        if let Some((_, _, projected)) = projection_move {
8886            item.projected = projected;
8887        }
8888        let task = item.task()?;
8889        let mut written = update.changed(&before, &task);
8890        if blocked_by_moved || recorded_moves {
8891            written.insert(UpdatedField::DependsOn);
8892        }
8893        self.remember_written(item, false)?;
8894        Ok(Some(TaskUpdateOutcome {
8895            task,
8896            written,
8897            delivers_before: before.delivers,
8898        }))
8899    }
8900
8901    /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
8902    /// together, and nothing else; see [`TaskSource::set_task_rendering`].
8903    ///
8904    /// One update of the body: the content outside the slot, and inside it that one entry,
8905    /// every other entry kept as it was. This source keeps no template answers — an issue has
8906    /// no room beside itself that is not its body, and answers written there would duplicate
8907    /// what the content already says and count against GitHub's body limit — so `answers`
8908    /// reaches nothing here. A body that would not change is not sent at all.
8909    async fn replace_rendering(
8910        &self,
8911        id: &NativeId,
8912        kind: BoardKind,
8913        content: &str,
8914        provenance: &Value,
8915        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
8916    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
8917        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8918            return Ok(None);
8919        };
8920        let held = item.raw_body.clone().unwrap_or_default();
8921        let mut slot = item.slot.clone();
8922        slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
8923        let rewritten;
8924        let content = if let Some(assets) = assets {
8925            let uploads = self
8926                .upload_assets(item.own_repository.as_ref(), assets)
8927                .await?;
8928            rewritten =
8929                onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
8930            rewritten.as_str()
8931        } else {
8932            content
8933        };
8934        let body = with_slot(&with_content(&held, &self.marked(content))?, &slot)?;
8935        // Checked before anything is sent, as a content write checks it.
8936        let (visible, read) = self.visible(Some(body.clone()))?;
8937        if visible.as_deref().unwrap_or_default() != content || read != slot {
8938            return Err(SourceError::Refused {
8939                message: format!(
8940                    "this content ends in what source {} reads as its own metadata slot \
8941                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8942                     as content; next: remove that trailing block from the template",
8943                    self.name
8944                ),
8945            });
8946        }
8947        if body != held {
8948            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8949                .await?;
8950        }
8951        item.body = visible.filter(|value| !value.is_empty());
8952        item.raw_body = Some(body);
8953        item.slot = read;
8954        self.remember_written(item, false)?;
8955        Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
8956            id: id.clone(),
8957            content: Some(content.to_owned()),
8958        }))
8959    }
8960
8961    async fn set_item_field(
8962        &self,
8963        board_id: &str,
8964        item_id: &str,
8965        field_id: &str,
8966        value: Value,
8967    ) -> Result<(), SourceError> {
8968        let data = self
8969            .graphql(
8970                graphql::UPDATE_FIELD,
8971                json!({"input":{
8972                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
8973                },"readPriority":false,"priorityName":PRIORITY_FIELD}),
8974            )
8975            .await?;
8976        field_update_item(&data, item_id).map(|_| ())
8977    }
8978
8979    /// GitHub accepts one value per field mutation; aliases combine those mutations in
8980    /// one request — writes in the order given, then clears. A lone write is
8981    /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
8982    /// request's slots hold go in further requests of the same document, in order. Every
8983    /// returned item id is checked, including optional aliases.
8984    async fn set_item_fields(
8985        &self,
8986        board: &str,
8987        item: &str,
8988        fields: &[(String, Value)],
8989        clears: &[&str],
8990    ) -> Result<(), SourceError> {
8991        match (fields, clears) {
8992            ([], []) => return Ok(()),
8993            ([(field, value)], []) => {
8994                return self.set_item_field(board, item, field, value.clone()).await;
8995            }
8996            ([], [field]) => {
8997                return self
8998                    .write_priority(
8999                        board,
9000                        item,
9001                        &PriorityWrite::Clear {
9002                            field: (*field).to_owned(),
9003                        },
9004                    )
9005                    .await;
9006            }
9007            _ => {}
9008        }
9009        // Any field of the request stands in for an input a slot leaves out, which GitHub
9010        // still requires to be well formed although it never runs it.
9011        let filler = fields
9012            .first()
9013            .map_or_else(|| clears[0], |(field, _)| field.as_str());
9014        let mut writes = fields.iter();
9015        let mut cleared = clears.iter();
9016        loop {
9017            let batch: Vec<&(String, Value)> =
9018                writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
9019            let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
9020            if batch.is_empty() && batch_clears.is_empty() {
9021                return Ok(());
9022            }
9023            let mut variables = serde_json::Map::new();
9024            for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
9025                let input = match batch.get(index) {
9026                    Some((field, value)) => {
9027                        json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
9028                    }
9029                    None => json!({"projectId":board,"itemId":item,"fieldId":filler,
9030                                   "value":{"text":""}}),
9031                };
9032                variables.insert(slot.variable.to_owned(), input);
9033                variables.insert(slot.include.to_owned(), json!(index < batch.len()));
9034            }
9035            for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
9036                let field = batch_clears.get(index).map_or(filler, |field| **field);
9037                variables.insert(
9038                    slot.variable.to_owned(),
9039                    json!({"projectId":board,"itemId":item,"fieldId":field}),
9040                );
9041                variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
9042            }
9043            let data = self
9044                .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
9045                .await?;
9046            let sent = FIELD_WRITE_SLOTS[..batch.len()]
9047                .iter()
9048                .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
9049            for slot in sent {
9050                let alias = slot.alias;
9051                let returned = data
9052                    .get(alias)
9053                    .and_then(|value| value.get("projectV2Item"))
9054                    .ok_or_else(|| SourceError::Malformed {
9055                        message: format!("GitHub field update {alias} returned no project item"),
9056                    })?;
9057                if required_str(returned, "id")? != item {
9058                    return Err(SourceError::Malformed {
9059                        message: format!(
9060                            "GitHub field update {alias} returned the wrong project item"
9061                        ),
9062                    });
9063                }
9064            }
9065        }
9066    }
9067
9068    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
9069        let mut after: Option<String> = None;
9070        let mut ids = Vec::new();
9071        loop {
9072            let data = self
9073                .graphql(
9074                    graphql::ISSUE_DEPENDENCIES,
9075                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
9076                )
9077                .await?;
9078            let connection =
9079                data.pointer("/node/blockedBy")
9080                    .ok_or_else(|| SourceError::Malformed {
9081                        message: "GitHub dependency response has no blockedBy connection".into(),
9082                    })?;
9083            ids.extend(
9084                connection
9085                    .get("nodes")
9086                    .and_then(Value::as_array)
9087                    .ok_or_else(|| SourceError::Malformed {
9088                        message: "GitHub dependency response nodes is not an array".into(),
9089                    })?
9090                    .iter()
9091                    .map(|value| required_str(value, "id").map(str::to_owned))
9092                    .collect::<Result<Vec<_>, _>>()?,
9093            );
9094            let next = next_cursor(connection)?;
9095            if let Some(next) = &next {
9096                validate_cursor_progress(after.as_deref(), &next.0)?;
9097            }
9098            after = next.map(|cursor| cursor.0);
9099            if after.is_none() {
9100                return Ok(ids);
9101            }
9102        }
9103    }
9104
9105    async fn dependencies(
9106        &self,
9107        id: &NativeId,
9108        near_kind: ItemKind,
9109        direction: Direction,
9110        page: &PageRequest,
9111    ) -> Result<Page<DependencyEdge>, SourceError> {
9112        validate_page(page)?;
9113        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
9114        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
9115        let recorded = recorded_offset(cursor, direction)?;
9116        // What this issue is blocked by, when a read of it by its own id in this command
9117        // already carried the whole connection — a copy reads the item it writes before it
9118        // reads its edges — and the page asked for is the whole of it, or the recorded tail
9119        // after it. Answered from that read, in the shape the dependency read answers in;
9120        // anything else is asked of GitHub.
9121        let carried = match direction {
9122            Direction::DependsOn => self
9123                .resolved_cache()?
9124                .get(id)
9125                .filter(|item| item.content_kind == ContentKind::Issue)
9126                .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
9127            Direction::DependedOnBy => None,
9128        }
9129        .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
9130        // Asked for even in the recorded phase, whose page reads nothing from the
9131        // connection: `__typename` is what says whether this item has a native
9132        // relationship at all, and that is what decides which far ends the reserved key is
9133        // allowed to hold.
9134        let data = match carried {
9135            Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
9136                "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
9137            None => {
9138                let after = if recorded.is_some() { None } else { cursor };
9139                let first = page.limit.min(MAX_PAGE_SIZE);
9140                match direction {
9141                    Direction::DependsOn => {
9142                        self.graphql(
9143                            graphql::ISSUE_DEPENDS_ON,
9144                            json!({"id":id.0,"first":first,"after":after,"owner":self.owner,
9145                                   "number":self.project_number,
9146                                   "boardItems":BOARD_ITEMS_PAGE_SIZE}),
9147                        )
9148                        .await?
9149                    }
9150                    Direction::DependedOnBy => {
9151                        self.graphql(
9152                            graphql::ISSUE_DEPENDED_ON_BY,
9153                            json!({"id":id.0,"first":first,"after":after}),
9154                        )
9155                        .await?
9156                    }
9157                }
9158            }
9159        };
9160        let draft_slot = draft_slot(&data, id, self.project_number)?;
9161        let node =
9162            data.get("node")
9163                .filter(|v| !v.is_null())
9164                .ok_or_else(|| SourceError::Refused {
9165                    message: format!(
9166                        "GitHub item {} was not found or does not support dependencies",
9167                        id.0
9168                    ),
9169                })?;
9170        let connection_name = match direction {
9171            Direction::DependsOn => "blockedBy",
9172            Direction::DependedOnBy => "blocking",
9173        };
9174        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
9175        // named natively and the reserved key may hold any far end. An issue's connections
9176        // hold issues, and this source reads them at the near item's own level.
9177        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
9178        if let Some(offset) = recorded {
9179            return Ok(recorded_page(
9180                self.recorded_edges(
9181                    id,
9182                    near_kind,
9183                    direction,
9184                    natively_names,
9185                    node,
9186                    draft_slot.as_ref(),
9187                )
9188                .await?,
9189                offset,
9190                limit,
9191            ));
9192        }
9193        if natively_names.is_none() {
9194            return Ok(recorded_page(
9195                self.recorded_edges(
9196                    id,
9197                    near_kind,
9198                    direction,
9199                    natively_names,
9200                    node,
9201                    draft_slot.as_ref(),
9202                )
9203                .await?,
9204                0,
9205                limit,
9206            ));
9207        }
9208        let connection = node
9209            .get(connection_name)
9210            .ok_or_else(|| SourceError::Malformed {
9211                message: "GitHub dependency response is missing its connection".into(),
9212            })?;
9213        let nodes = connection
9214            .get("nodes")
9215            .and_then(Value::as_array)
9216            .ok_or_else(|| SourceError::Malformed {
9217                message: "GitHub dependency response nodes is not an array".into(),
9218            })?;
9219        // `from` depends on `to`, always. GitHub spells the same relationship from either
9220        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
9221        // it — so the near item is `from` in one direction and `to` in the other.
9222        let items = nodes
9223            .iter()
9224            .map(|value| {
9225                let related = NativeId(required_str(value, "id")?.into());
9226                let related_kind = related_kind(value)?;
9227                let (from, to) = match direction {
9228                    Direction::DependsOn => (
9229                        DependencyEndpoint::from_native(id.clone(), near_kind),
9230                        DependencyEndpoint::from_native(related, related_kind),
9231                    ),
9232                    Direction::DependedOnBy => (
9233                        DependencyEndpoint::from_native(related, related_kind),
9234                        DependencyEndpoint::from_native(id.clone(), near_kind),
9235                    ),
9236                };
9237                Ok(DependencyEdge {
9238                    from,
9239                    to,
9240                    kind: DependencyKind::Blocks,
9241                })
9242            })
9243            .collect::<Result<Vec<_>, SourceError>>()?;
9244        let mut next = next_cursor(connection)?;
9245        if let Some(next) = &next {
9246            validate_cursor_progress(cursor, &next.0)?;
9247        }
9248        // The recorded far ends ride on the last page of `blockedBy`, whose answer carried the
9249        // body they are recorded in, as far as the page has room — so they cost no request of
9250        // their own — and only what does not fit is resumed by a cursor.
9251        let mut items = items;
9252        if next.is_none() {
9253            let recorded = self
9254                .recorded_edges(
9255                    id,
9256                    near_kind,
9257                    direction,
9258                    natively_names,
9259                    node,
9260                    draft_slot.as_ref(),
9261                )
9262                .await?;
9263            let room = limit.saturating_sub(items.len());
9264            let total = recorded.len();
9265            items.extend(recorded.into_iter().take(room));
9266            if total > room {
9267                next = Some(Cursor(format!("{RECORDED_CURSOR}{room}")));
9268            }
9269        }
9270        Ok(Page { items, next })
9271    }
9272
9273    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
9274    /// a far end in another source has to live: no GitHub issue relationship can name one.
9275    ///
9276    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
9277    /// source never writes one down.
9278    ///
9279    /// The metadata lives in the item's own body slot, and `node` is the dependency read's
9280    /// own answer, which carries an issue's body — so an issue's recorded edges cost no
9281    /// request beyond the read already made, and reading the board for them would be a
9282    /// walk of every item for one field of one. A draft has no body in that answer, because
9283    /// a draft is not an issue, so a draft's are read off its own read by id — never off a
9284    /// listing of the board, which can be behind on the very item asked about.
9285    async fn recorded_edges(
9286        &self,
9287        id: &NativeId,
9288        near_kind: ItemKind,
9289        direction: Direction,
9290        natively_names: Option<ItemKind>,
9291        node: &Value,
9292        draft_slot: Option<&Option<BTreeMap<String, Value>>>,
9293    ) -> Result<Vec<DependencyEdge>, SourceError> {
9294        if direction != Direction::DependsOn {
9295            return Ok(Vec::new());
9296        }
9297        let slot = match (node.get("body"), draft_slot) {
9298            (Some(body), _) if natively_names.is_some() => {
9299                metadata_body(body.as_str().map(str::to_owned))?.1
9300            }
9301            // A draft the dependency read itself answered: this board's, or none of its.
9302            (_, Some(Some(slot))) => slot.clone(),
9303            (_, Some(None)) => return Ok(Vec::new()),
9304            _ => {
9305                let Some(item) = self.bound_item(id).await? else {
9306                    return Ok(Vec::new());
9307                };
9308                item.slot
9309            }
9310        };
9311        DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
9312            .map_err(|message| SourceError::Malformed { message })
9313    }
9314
9315    /// Why `creation_owners` refuses a create filed in `target`, or `None` when it allows it —
9316    /// every create when the list is empty.
9317    fn owner_refusal(&self, target: &RepositoryTarget) -> Option<String> {
9318        if self.creation_owners.is_empty()
9319            || self
9320                .creation_owners
9321                .iter()
9322                .any(|owner| owner.eq_ignore_ascii_case(&target.owner))
9323        {
9324            return None;
9325        }
9326        Some(format!(
9327            "source {} would create the issue in repository {}, owned by {}, and its \
9328             creation_owners allows only repositories of {}",
9329            self.name,
9330            target.slug(),
9331            target.owner,
9332            self.creation_owners.join(", ")
9333        ))
9334    }
9335
9336    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
9337        self.repository
9338            .as_ref()
9339            .ok_or_else(|| SourceError::Refused {
9340                message: format!(
9341                    "source {} has no repository configured, and a GitHub Projects board has no \
9342                 repository of its own to create an issue in; set repository: owner/name on \
9343                 this source",
9344                    self.name
9345                ),
9346            })
9347    }
9348
9349    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
9350    /// states.
9351    ///
9352    /// The fallback is demanded first, whichever arm answers: a write without a configured
9353    /// repository is refused naming the field exactly as it was before the rule existed,
9354    /// so a source that could not write before cannot write now, rather than writing for
9355    /// the one item whose own field happens to decide it.
9356    ///
9357    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
9358    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
9359    /// entry owned by someone other than the owner of the parent issue's repository —
9360    /// GitHub accepts a sub-issue from another repository of the same owner and from no
9361    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
9362    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
9363    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
9364    /// and is visible to the token is checked where its node id is resolved, still before
9365    /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
9366    /// looked up in a listing of the board, which can be minutes behind an issue its own
9367    /// `projectItems` already places on it — and that read answers first from this process's
9368    /// own record, so a project created moments ago in this command answers though GitHub
9369    /// has not caught up.
9370    async fn creation_target(
9371        &self,
9372        incoming: &Incoming<'_>,
9373    ) -> Result<RepositoryTarget, SourceError> {
9374        let what = format!(
9375            "{} {:?}",
9376            incoming.written.kind().describes(),
9377            incoming.title
9378        );
9379        let target = self
9380            .creation_rule(incoming.repositories, incoming.parent, &what)
9381            .await?;
9382        // The engine asks `creation_refusal` before anything of a copy is written; this holds
9383        // every other create to the same restriction, before `createIssue`.
9384        match self.owner_refusal(&target) {
9385            Some(message) => Err(SourceError::Refused { message }),
9386            None => Ok(target),
9387        }
9388    }
9389
9390    /// [`Self::creation_target`]'s rule over what it reads of the item — its `repositories`
9391    /// and the project issue it is filed under — with `what` naming the item in a refusal: the
9392    /// one place the rule is, so the create, [`TaskSource::creation_repository`] and the
9393    /// write-time read of who can read the issue ([`TaskSource::visibility`]) cannot answer
9394    /// differently.
9395    ///
9396    /// On a source declared private, an item's one named repository is read for its
9397    /// visibility first — once per engine hold, see the `visibility` module — and one that
9398    /// does not read private decides nothing: the issue is created in the parent's
9399    /// repository, else the configured one. A read that cannot be answered refuses.
9400    async fn creation_rule(
9401        &self,
9402        repositories: &[Repository],
9403        parent: Option<&NativeId>,
9404        what: &str,
9405    ) -> Result<RepositoryTarget, SourceError> {
9406        let parent = match parent {
9407            Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
9408                SourceError::Refused {
9409                    message: format!(
9410                        "GitHub project issue {} was not found on the board of source {}, so {} \
9411                         cannot be filed under it",
9412                        parent.0, self.name, what
9413                    ),
9414                }
9415            })?),
9416            None => None,
9417        };
9418        let parents_repository = parent
9419            .as_ref()
9420            .map(|parent| {
9421                // A draft is on the board and so is found, but it has no repository to
9422                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
9423                // would refuse the task only once `createIssue` had made it.
9424                if parent.content_kind == ContentKind::DraftIssue {
9425                    return Err(SourceError::Refused {
9426                        message: format!(
9427                            "GitHub project item {} on the board of source {} is a draft, \
9428                             which cannot have sub-issues, so {} cannot be filed under it",
9429                            parent.id.0, self.name, what
9430                        ),
9431                    });
9432                }
9433                // An issue's repository is where a sub-issue is placed and whose owner it
9434                // is compared against, so a parent whose repository this source cannot
9435                // spell as `owner/name` — GitHub's login grammar is wider than this
9436                // source's floor — is one nothing can be filed under.
9437                parent
9438                    .own_repository
9439                    .as_ref()
9440                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
9441                    .ok_or_else(|| SourceError::Malformed {
9442                        message: format!(
9443                            "GitHub project issue {} on the board of source {} is in {}, which \
9444                             is not a {}/owner/name repository this source can place {} in",
9445                            parent.id.0,
9446                            self.name,
9447                            parent
9448                                .own_repository
9449                                .as_ref()
9450                                .map_or("no repository", Repository::as_str),
9451                            RepositoryTarget::HOST,
9452                            what
9453                        ),
9454                    })
9455            })
9456            .transpose()?;
9457        self.creation_rule_in_repository(repositories, parents_repository, what)
9458            .await
9459    }
9460
9461    async fn creation_rule_in_repository(
9462        &self,
9463        repositories: &[Repository],
9464        parents_repository: Option<RepositoryTarget>,
9465        what: &str,
9466    ) -> Result<RepositoryTarget, SourceError> {
9467        let fallback = self.configured_repository()?;
9468        match repositories {
9469            [named] => {
9470                let target =
9471                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
9472                        message: format!(
9473                            "{} names repository {}, which is not a {}/owner/name repository \
9474                             source {} can create an issue in; name one that is, or name none",
9475                            what,
9476                            named.as_str(),
9477                            RepositoryTarget::HOST,
9478                            self.name
9479                        ),
9480                    })?;
9481                // On a source declared private, an issue whose one named repository does not
9482                // read private is created where an item naming none is: the item still names
9483                // that repository in its metadata slot, and its issue stays private.
9484                if self.is_declared_private()
9485                    && self.repository_visibility(&target).await?
9486                        != onetaskgraph_plugin_api::Visibility::Private
9487                {
9488                    return Ok(parents_repository.unwrap_or_else(|| fallback.clone()));
9489                }
9490                if let Some(parents) = &parents_repository
9491                    && parents.owner != target.owner
9492                {
9493                    return Err(SourceError::Refused {
9494                        message: format!(
9495                            "{} names repository {}, owned by {}, but its project's issue is in \
9496                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
9497                             of the same owner as its parent issue; name a repository of {}, or \
9498                             name none",
9499                            what,
9500                            target.slug(),
9501                            target.owner,
9502                            parents.slug(),
9503                            parents.owner,
9504                            parents.owner
9505                        ),
9506                    });
9507                }
9508                Ok(target)
9509            }
9510            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
9511        }
9512    }
9513
9514    /// The node id of the repository `incoming` is being created in, or the refusal naming
9515    /// the item and the repository the token cannot see.
9516    ///
9517    /// Resolved once per command per repository; see [`Self::repository_cache`].
9518    async fn repository_id(
9519        &self,
9520        repository: &RepositoryTarget,
9521        incoming: &Incoming<'_>,
9522    ) -> Result<String, SourceError> {
9523        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
9524            return Ok(id);
9525        }
9526        let data = self
9527            .graphql(
9528                graphql::REPOSITORY,
9529                json!({"owner":repository.owner,"name":repository.name}),
9530            )
9531            .await?;
9532        self.repository_read(&data, repository, incoming)
9533    }
9534
9535    /// The repository's node id out of an answer carrying the `repository` root, held for
9536    /// the rest of this command, or the refusal naming the item that cannot be created in it.
9537    fn repository_read(
9538        &self,
9539        data: &Value,
9540        repository: &RepositoryTarget,
9541        incoming: &Incoming<'_>,
9542    ) -> Result<String, SourceError> {
9543        let node = data
9544            .get("repository")
9545            .filter(|value| !value.is_null())
9546            .ok_or_else(|| SourceError::Refused {
9547                message: format!(
9548                    "GitHub repository {} was not found or is not visible to the token, so {} \
9549                     {:?} cannot be created in it",
9550                    repository.slug(),
9551                    incoming.written.kind().describes(),
9552                    incoming.title
9553                ),
9554            })?;
9555        let id = required_str(node, "id")?.to_owned();
9556        self.repository_cache()?
9557            .insert(repository.clone(), id.clone());
9558        Ok(id)
9559    }
9560
9561    fn repository_cache(
9562        &self,
9563    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
9564        self.repository_cache
9565            .lock()
9566            .map_err(|_| SourceError::Unavailable {
9567                message: "this source's record of the destination repository was left \
9568                          inconsistent by an earlier failure; next: run the command again"
9569                    .into(),
9570            })
9571    }
9572
9573    /// Create or update one board item, whichever kind it is.
9574    async fn write_item(
9575        &self,
9576        incoming: &Incoming<'_>,
9577        target: Option<&NativeId>,
9578        depends_on: &[DependencyEdge],
9579    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9580        let prepared = self.prepare_write(incoming, target, depends_on).await?;
9581        let landed = match &prepared.destination {
9582            // Its content is written last, by `complete_write`, once everything else has landed.
9583            WriteDestination::Existing(item) => Landed {
9584                content_id: item.id.clone(),
9585                item_id: Some(item.item_id.clone()),
9586                url: item.url.clone(),
9587                number: item.number,
9588            },
9589            WriteDestination::New(target) => {
9590                if self.deferring()? {
9591                    self.create_filed_issue(
9592                        prepared.board.id.as_str(),
9593                        target,
9594                        incoming,
9595                        &prepared.body,
9596                    )
9597                    .await?
9598                } else {
9599                    self.create_and_file_issue(
9600                        prepared.board.id.as_str(),
9601                        target,
9602                        incoming,
9603                        &prepared.body,
9604                    )
9605                    .await?
9606                }
9607            }
9608        };
9609        self.complete_write(incoming, prepared, landed).await
9610    }
9611
9612    /// Everything one write of `incoming` reads and decides before it sends anything: what is
9613    /// there now, where a new item is created, the board's fields, and every value refused while
9614    /// nothing has been written — the item's body included.
9615    async fn prepare_write(
9616        &self,
9617        incoming: &Incoming<'_>,
9618        target: Option<&NativeId>,
9619        depends_on: &[DependencyEdge],
9620    ) -> Result<Prepared, SourceError> {
9621        // Refused before anything is read or written: a task or a project titled the way
9622        // this board spells a document would land as an issue this same source reads back
9623        // as a document, so the field this destination cannot carry is named rather than
9624        // written and silently reclassified.
9625        if let Written::Work(kind, _) = incoming.written
9626            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
9627        {
9628            return Err(SourceError::Refused {
9629                message: format!(
9630                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
9631                     spells a document, so it would read back as one rather than as a {}; \
9632                     retitle it, or copy it as a document",
9633                    kind.marker(),
9634                    self.name,
9635                    kind.marker()
9636                ),
9637            });
9638        }
9639        // The destination is read by its own id, and whether this board holds it is decided
9640        // by that read — its own `projectItems` — rather than by whether a listing of the
9641        // board happens to include it yet. See the module documentation.
9642        // An existing issue is never moved; a new one is created where the rule says — and
9643        // knowing where is what lets the board's fields and that repository's id be read
9644        // together, before anything below needs either.
9645        let destination = match target {
9646            Some(target) => {
9647                WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
9648                    || SourceError::Refused {
9649                        message: format!("GitHub destination item {} was not found", target.0),
9650                    },
9651                )?))
9652            }
9653            None => {
9654                let target = self.creation_target(incoming).await?;
9655                self.creation_context(&target, incoming).await?;
9656                WriteDestination::New(target)
9657            }
9658        };
9659        let existing = destination.existing();
9660        let board = self
9661            .fields_for(
9662                existing,
9663                incoming.metadata.contains_key(ORIGIN_KEY),
9664                incoming.written.status().is_some(),
9665                incoming
9666                    .priority
9667                    .is_some_and(|priority| priority != Priority::None),
9668            )
9669            .await?;
9670        let status_target = incoming
9671            .written
9672            .work_status()
9673            .map(|(kind, status)| self.resolved_target(kind, status.category))
9674            .transpose()?;
9675        let column = match (incoming.written.work_status(), status_target.as_ref()) {
9676            (Some((kind, status)), Some(target)) => {
9677                self.column_for(&board.fields, kind, status.category, target)?
9678            }
9679            _ => None,
9680        };
9681        // Resolved before anything is created, for the reason the column above is: a
9682        // priority this board has no option for is refused while nothing has been written.
9683        let priority_write = match incoming.priority {
9684            Some(priority) => self.priority_write(&board.fields, existing, priority)?,
9685            None => None,
9686        };
9687        // And for the reason the priority is: a value no projected field can hold, or a board
9688        // that cannot hold the field, is refused while nothing has been written.
9689        let (projections, projected) = self.projection_writes(
9690            &board.fields,
9691            existing.map(|item| &item.projected),
9692            incoming.metadata,
9693            None,
9694        )?;
9695        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
9696        if content_kind == ContentKind::DraftIssue {
9697            if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
9698                (status_target.as_ref(), incoming.written.status())
9699            {
9700                return Err(self.closes_a_draft(status.category));
9701            }
9702            if incoming.parent.is_some() {
9703                return Err(SourceError::Refused {
9704                    message: "GitHub draft items cannot be a project's sub-issue".into(),
9705                });
9706            }
9707        }
9708        match existing {
9709            Some(item) if content_kind == ContentKind::Issue => {
9710                if item.labels != incoming.labels {
9711                    return Err(SourceError::Refused {
9712                        message: "GitHub issue labels differ from the labels being written".into(),
9713                    });
9714                }
9715            }
9716            _ => {
9717                if !incoming.labels.is_empty() {
9718                    return Err(SourceError::Refused {
9719                        message: "GitHub items created by this destination carry no labels".into(),
9720                    });
9721                }
9722            }
9723        }
9724
9725        // The repository the issue really lives in is what the slot below is written against,
9726        // so a single entry that is where the issue is created travels as no key at all, and
9727        // the read side derives it back from the issue.
9728        let own_repository = match &destination {
9729            WriteDestination::Existing(item) => item.own_repository.clone(),
9730            WriteDestination::New(target) => Some(
9731                Repository::try_from(target.origin())
9732                    .map_err(|message| SourceError::Config { message })?,
9733            ),
9734        };
9735        let (native, fallback) = self
9736            .partition_edges(
9737                incoming.written.kind(),
9738                content_kind,
9739                existing.and_then(|item| item.blocked_by.as_deref()),
9740                depends_on,
9741            )
9742            .await?;
9743        let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
9744        let content = match incoming.assets {
9745            Some(assets) => {
9746                let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
9747                let rewritten = onetaskgraph_plugin_api::serve_asset_references(
9748                    incoming.content.unwrap_or_default(),
9749                    &mut slot,
9750                    &uploads,
9751                );
9752                incoming.content.map(|_| rewritten)
9753            }
9754            None => incoming.content.map(str::to_owned),
9755        };
9756        let marked = content.as_deref().map(|content| self.marked(content));
9757        let body = compose_body(marked.as_deref(), &slot)?;
9758        // Read before anything is created, for the reason the field below is: a value
9759        // this destination cannot store has to refuse, and refusing after `createIssue`
9760        // would leave an issue behind that nothing asked for. The engine writes a
9761        // qualified id here; a caller handing this key anything else is told so rather
9762        // than having it silently stored as no origin at all.
9763        // 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.
9764        let origin = match incoming.metadata.get(ORIGIN_KEY) {
9765            None => "",
9766            Some(Value::String(origin)) => origin.as_str(),
9767            Some(other) => {
9768                return Err(SourceError::Refused {
9769                    message: format!(
9770                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
9771                         is {other}"
9772                    ),
9773                });
9774            }
9775        };
9776        // Resolved before anything is created: a board that cannot carry the copy origin
9777        // has to refuse the write, and refusing it after `createIssue` would leave an
9778        // issue behind that nothing asked for.
9779        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
9780            Some(field) => {
9781                if required_str(field, "__typename")? != "ProjectV2Field" {
9782                    return Err(SourceError::Refused {
9783                        message: format!(
9784                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
9785                        ),
9786                    });
9787                }
9788                Some(required_str(field, "id")?.to_owned())
9789            }
9790            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
9791                return Err(SourceError::Refused {
9792                    message: format!(
9793                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
9794                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
9795                         the board"
9796                    ),
9797                });
9798            }
9799            None => None,
9800        };
9801
9802        Ok(Prepared {
9803            origin: origin.to_owned(),
9804            destination,
9805            board,
9806            status_target,
9807            column,
9808            priority_write,
9809            projections,
9810            projected,
9811            content_kind,
9812            own_repository,
9813            native,
9814            slot,
9815            content,
9816            body,
9817            origin_field,
9818        })
9819    }
9820
9821    /// Everything one write does once the item it writes exists: its board fields, its
9822    /// relationships and — for an item that was already there — its content last, then what this
9823    /// process remembers of it.
9824    ///
9825    /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
9826    /// item this command created has its fields, its closing and its edges held for
9827    /// [`TaskSource::finish_writes`] rather than sent here.
9828    async fn complete_write(
9829        &self,
9830        incoming: &Incoming<'_>,
9831        prepared: Prepared,
9832        landed: Landed,
9833    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9834        let Prepared {
9835            destination,
9836            board,
9837            status_target,
9838            column,
9839            priority_write,
9840            projections,
9841            projected,
9842            content_kind,
9843            own_repository,
9844            native,
9845            slot,
9846            content,
9847            body,
9848            origin,
9849            origin_field,
9850        } = prepared;
9851        let existing = destination.existing();
9852        let origin = origin.as_str();
9853        let Landed {
9854            content_id,
9855            item_id,
9856            url,
9857            number,
9858        } = landed;
9859
9860        // Only what differs from the item as this write read it is sent: a priority the item
9861        // already holds is not, so an unchanged re-copy writes no field and a person's edit of
9862        // it since that read is left alone.
9863        let priority_write = priority_write.filter(|_| {
9864            !existing.is_some_and(|item| {
9865                incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
9866            })
9867        });
9868        let written_option = column.as_ref().map(|(_, _, name)| name.clone());
9869        let column = column
9870            .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
9871            .map(|(field, option, _)| (field, option));
9872        // Creating an item here is several calls — `createIssue`, which files it on the
9873        // board, then its board fields, the parent and the dependencies — and GitHub can fail
9874        // at any of them. Everything this source can refuse *before* the first of those is
9875        // already checked above, so what is left is GitHub itself failing part way. When it
9876        // does over an item this call created, the issue is taken back: a write that
9877        // refused must not leave an item behind that nobody asked for, and one that does
9878        // makes the retry create a second.
9879        // Whether the board-field write carrying a moved origin was answered as landing whole.
9880        // When it was refused, GitHub does not say which of its fields ran before the one that
9881        // failed, so the origin may or may not have moved.
9882        let mut origin_landed = false;
9883        // An item this command created while writes are held back has every part that can land
9884        // later held for `finish_writes` — its own create filed it on the board and under its
9885        // project already — and so does a later write of it in the same command.
9886        let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
9887        let landed = if holding {
9888            self.hold_back(
9889                &board.id,
9890                &content_id,
9891                existing,
9892                origin_field.as_deref(),
9893                origin,
9894                column,
9895                status_target.as_ref(),
9896                priority_write.as_ref(),
9897                &projections,
9898                &native,
9899            )
9900        } else if let Some(item_id) = item_id.as_deref() {
9901            self.finish_write(
9902                board.id.as_str(),
9903                incoming,
9904                &content_id,
9905                item_id,
9906                content_kind,
9907                existing,
9908                origin_field.as_deref(),
9909                origin,
9910                column,
9911                status_target.as_ref(),
9912                priority_write.as_ref(),
9913                &projections,
9914                &native,
9915                &mut origin_landed,
9916            )
9917            .await
9918        } else {
9919            // An issue a create filed on the board itself has no board item this process
9920            // knows until `finish_writes` reads it, so nothing but a held-back write may land
9921            // on it before then.
9922            Err(SourceError::Malformed {
9923                message: format!(
9924                    "issue {} was written before the board item GitHub filed it as was read",
9925                    content_id.0
9926                ),
9927            })
9928        };
9929        // An existing item's title, body and state go last, in one `updateIssue`, once its board
9930        // fields and its relationships have landed: a refusal of any of those then leaves its
9931        // body — and the metadata slot inside it — exactly as it stood.
9932        let landed = match (landed, existing) {
9933            (Ok(()), Some(item)) => {
9934                self.update_existing(item, incoming, &body, status_target.as_ref())
9935                    .await
9936            }
9937            (landed, _) => landed,
9938        };
9939        if let Err(error) = landed {
9940            match existing {
9941                // Best effort, and the write's own failure is what the caller is told: a
9942                // refusal naming the tidy-up would hide why the write failed at all.
9943                None => {
9944                    let _ = self.delete_issue(&content_id).await;
9945                }
9946                // The origin field is the one piece of an existing item's metadata written
9947                // before its body, so a write refused after it puts it back as it was. When
9948                // that is refused too, the write's own failure is still what the caller is
9949                // told — with what it left behind added, because the item's metadata is then
9950                // not as it stood and a caller retrying has to know which key moved.
9951                Some(item) => {
9952                    let before = item.origin.as_deref().unwrap_or("");
9953                    if let Some(field) = origin_field.as_deref()
9954                        && before != origin
9955                        && let Err(restore) = self
9956                            .set_item_field(
9957                                board.id.as_str(),
9958                                &item.item_id,
9959                                field,
9960                                json!({"text": before}),
9961                            )
9962                            .await
9963                    {
9964                        let left = if origin_landed {
9965                            format!(
9966                                "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
9967                                 not be put back to {before:?} ({restore}), so item {} still \
9968                                 holds {origin:?} there",
9969                                item.id.0
9970                            )
9971                        } else {
9972                            format!(
9973                                "the refused write carried its {ORIGIN_KEY} from {before:?} to \
9974                                 {origin:?}, GitHub does not say whether that part of it ran, \
9975                                 and putting it back to {before:?} was refused ({restore}), so \
9976                                 item {} holds {origin:?} or {before:?} there",
9977                                item.id.0
9978                            )
9979                        };
9980                        return Err(noting(
9981                            error,
9982                            &format!(
9983                                "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
9984                                 run the write again"
9985                            ),
9986                        ));
9987                    }
9988                }
9989            }
9990            return Err(error);
9991        }
9992
9993        let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
9994            (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
9995                self.statuses
9996                    .status(kind, written_option.as_deref(), true, Some(reason.reason()))
9997            }
9998            (Some((kind, _)), Some(StatusTarget::Column(_))) => {
9999                self.statuses
10000                    .status(kind, written_option.as_deref(), false, None)
10001            }
10002            (Some((_, status)), _) => status.clone(),
10003            (None, _) => Status {
10004                category: StatusCategory::Unknown,
10005                name: "Open".to_owned(),
10006            },
10007        };
10008
10009        // So the rest of this command reads what it just did rather than what the board
10010        // said before it. See `remember_written` for which half takes it.
10011        let remembered = Resolved {
10012            // Empty for an issue whose board item is not read yet: `finish_writes` reads it
10013            // and `learn_item` puts it here.
10014            // 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.
10015            item_id: item_id.unwrap_or_default(),
10016            id: content_id.clone(),
10017            content_kind,
10018            kind: incoming.written.kind(),
10019            title: incoming.title.to_owned(),
10020            // The visible half of the body this write composed, split back off it the
10021            // way a read splits it — so what this record reports is what a read of the
10022            // same issue reports, rather than the person's text with the metadata slot
10023            // still on the end of it.
10024            body: self.visible(body.clone())?.0,
10025            raw_body: body.clone(),
10026            // A document has no status of its own; what it reads back as is whatever
10027            // the issue's own state says, which is what a re-read reports.
10028            status: written_status,
10029            option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
10030            priority: match incoming.priority {
10031                Some(priority) => HeldPriority::Read(priority),
10032                None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
10033                    item.priority.clone()
10034                }),
10035            },
10036            // What `state_input` asked for: closed for a terminal target, open for any other
10037            // status, and the issue's own state left as it was by a document write.
10038            closed: content_kind == ContentKind::Issue
10039                && match status_target.as_ref() {
10040                    Some(StatusTarget::Terminal(_, _)) => true,
10041                    Some(_) => false,
10042                    None => existing.is_some_and(|item| item.closed),
10043                },
10044            reason: match status_target.as_ref() {
10045                Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
10046                    Some(reason.reason().to_owned())
10047                }
10048                Some(_) => None,
10049                None => existing.and_then(|item| item.reason.clone()),
10050            },
10051            delivers: incoming.delivers.to_vec(),
10052            delivered_by: incoming.delivered_by.to_vec(),
10053            labels: incoming.labels.to_vec(),
10054            parent: incoming.parent.cloned(),
10055            origin: (!origin.is_empty()).then(|| origin.to_owned()),
10056            projected,
10057            number,
10058            // In the update path this is the item's own url, read off `existing` where the
10059            // record above was bound, so one expression serves both halves.
10060            url,
10061            created_at: existing.and_then(|item| item.created_at),
10062            updated_at: existing.and_then(|item| item.updated_at),
10063            // What this write moved is GitHub's to say, so a record of it gives no token until
10064            // it is read again.
10065            item_updated_at: None,
10066            comment_count: None,
10067            relationship_events: None,
10068            served_at: None,
10069            facets: None,
10070            own_repository,
10071            repositories: incoming.repositories.to_vec(),
10072            classification: incoming.classification,
10073            slot,
10074            board_id: Some(board.id.as_str().to_owned()),
10075            fields: board
10076                .fields
10077                .get("nodes")
10078                .and_then(Value::as_array)
10079                .cloned()
10080                .unwrap_or_default(),
10081            board_fields: Some(board.fields.clone()),
10082            // What this write left the relationship holding is known by id alone, and a
10083            // later read of its edges needs each far end's kind, so it reads them again.
10084            blocked_by: None,
10085        };
10086        self.remember_written(remembered, existing.is_none())?;
10087        Ok(onetaskgraph_plugin_api::AssetsWritten {
10088            id: content_id,
10089            content,
10090        })
10091    }
10092
10093    /// Everything a write does after the item exists: its board fields, its parent, and
10094    /// its dependencies.
10095    ///
10096    /// Split out of `write_item` so there is one place a failure past the point of no
10097    /// return is caught, rather than a tidy-up repeated at each `?` above.
10098    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
10099    // so there is one place a failure past the point of no return is caught, and its
10100    // arguments are exactly the values that tail already had in scope. Bundling them into a
10101    // struct would describe no concept — it would be "the arguments of this function" — and
10102    // would put the whole of `write_item`'s locals behind one more indirection.
10103    #[allow(clippy::too_many_arguments)]
10104    async fn finish_write(
10105        &self,
10106        board_id: &str,
10107        incoming: &Incoming<'_>,
10108        content_id: &NativeId,
10109        item_id: &str,
10110        content_kind: ContentKind,
10111        existing: Option<&Resolved>,
10112        origin_field: Option<&str>,
10113        origin: &str,
10114        column: Option<(String, String)>,
10115        status_target: Option<&StatusTarget>,
10116        priority: Option<&PriorityWrite>,
10117        projections: &[ProjectionWrite],
10118        native: &[String],
10119        origin_landed: &mut bool,
10120    ) -> Result<(), SourceError> {
10121        let (fields, clears) = field_writes(
10122            existing,
10123            origin_field,
10124            origin,
10125            column,
10126            priority,
10127            projections,
10128        );
10129        self.set_item_fields(board_id, item_id, &fields, &clears)
10130            .await?;
10131        *origin_landed = true;
10132
10133        // An existing issue closes in the `updateIssue` its write ends with; one created just
10134        // now closes here, once its option is selected.
10135        if existing.is_none()
10136            && content_kind == ContentKind::Issue
10137            && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
10138        {
10139            self.update_content(
10140                ContentKind::Issue,
10141                content_id,
10142                json!({"stateInput":state_input(status_target)}),
10143            )
10144            .await?;
10145        }
10146
10147        if content_kind == ContentKind::Issue {
10148            self.reparent(
10149                existing.and_then(|item| item.parent.clone()),
10150                content_id,
10151                incoming.parent,
10152            )
10153            .await?;
10154            // A document takes part in no dependency graph, so writing one neither reads
10155            // nor changes the issue's own `blockedBy` relationships. Reconciling them
10156            // against the empty list a document write carries would *delete* whatever
10157            // relationships a person had made on that issue, which is a write nobody
10158            // asked for.
10159            if incoming.written.kind() != BoardKind::Document {
10160                let issue = match existing {
10161                    Some(item) => Issue::Existing(item.blocked_by.as_deref()),
10162                    None => Issue::Created,
10163                };
10164                self.reconcile_blocked_by(content_id, native, issue).await?;
10165            }
10166        }
10167        Ok(())
10168    }
10169
10170    /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
10171    fn deferring(&self) -> Result<bool, SourceError> {
10172        Ok(self.deferred()?.is_some())
10173    }
10174
10175    /// Whether `content` is an item this command created whose parts are held back.
10176    fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
10177        Ok(self
10178            .deferred()?
10179            .as_ref()
10180            .is_some_and(|deferred| deferred.holds(content)))
10181    }
10182
10183    fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
10184        self.deferred.lock().map_err(|_| SourceError::Unavailable {
10185            message: "this source's record of the writes it is holding back was left \
10186                      inconsistent by an earlier failure; next: run the command again"
10187                .into(),
10188        })
10189    }
10190
10191    /// Creates one issue filed on the board and under its project by the `createIssue` itself —
10192    /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
10193    /// creates with: one request where adding it to the board and filing it under its project
10194    /// were two more. GitHub adds the board item after answering, so its id is not known here;
10195    /// [`TaskSource::finish_writes`] reads it.
10196    async fn create_filed_issue(
10197        &self,
10198        board_id: &str,
10199        repository: &RepositoryTarget,
10200        incoming: &Incoming<'_>,
10201        body: &Option<String>,
10202    ) -> Result<Landed, SourceError> {
10203        let repository_id = self.repository_id(repository, incoming).await?;
10204        let data = self
10205            .graphql(
10206                graphql::CREATE_ISSUE,
10207                json!({"input": filed_input(&repository_id, board_id, incoming, body)}),
10208            )
10209            .await?;
10210        let created = data
10211            .pointer("/createIssue/issue")
10212            .filter(|value| !value.is_null())
10213            .ok_or_else(|| SourceError::Malformed {
10214                message: "GitHub issue creation returned no issue".into(),
10215            })?;
10216        self.created_landed(created).await
10217    }
10218
10219    /// Keep the address `createIssue` answered for the issue `content`, for
10220    /// [`TaskSource::written_location`].
10221    fn note_written(&self, content: &NativeId, url: Option<&str>) {
10222        if let Some(url) = url {
10223            self.written
10224                .lock()
10225                .unwrap_or_else(std::sync::PoisonError::into_inner)
10226                .insert(content.clone(), url.to_owned());
10227        }
10228    }
10229
10230    /// What one `createIssue` answered, as the item it created — or, when its number cannot be
10231    /// read, the refusal, after taking the issue back.
10232    async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
10233        let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
10234        let url = optional_str(created, "url")?.map(str::to_owned);
10235        let number = match created_issue_number(created) {
10236            Ok(number) => number,
10237            Err(error) => {
10238                let _ = self.delete_issue(&content_id).await;
10239                return Err(error);
10240            }
10241        };
10242        self.note_written(&content_id, url.as_deref());
10243        Ok(Landed {
10244            content_id,
10245            item_id: None,
10246            url,
10247            number,
10248        })
10249    }
10250
10251    /// Hold back everything a write of the created item `content` would send after creating it,
10252    /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
10253    /// `finish_write` sends, the parent excepted, which its create already named. A later write
10254    /// of the same item replaces what it changes.
10255    // 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.
10256    #[allow(clippy::too_many_arguments)]
10257    fn hold_back(
10258        &self,
10259        board_id: &BoardId,
10260        content: &NativeId,
10261        existing: Option<&Resolved>,
10262        origin_field: Option<&str>,
10263        origin: &str,
10264        column: Option<(String, String)>,
10265        status_target: Option<&StatusTarget>,
10266        priority: Option<&PriorityWrite>,
10267        projections: &[ProjectionWrite],
10268        native: &[String],
10269    ) -> Result<(), SourceError> {
10270        let status = column.map(|(field, option)| {
10271            let name = self
10272                .fields_cache()
10273                .ok()
10274                .and_then(|fields| {
10275                    fields.as_ref().and_then(|board| {
10276                        option_name(&board.fields, &field, &option).map(str::to_owned)
10277                    })
10278                })
10279                .unwrap_or_default();
10280            (field, option, name)
10281        });
10282        let (fields, clears) =
10283            field_writes(existing, origin_field, origin, None, priority, projections);
10284        let close = match status_target {
10285            Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
10286            _ => None,
10287        };
10288        let mut deferred = self.deferred()?;
10289        let deferred = deferred.get_or_insert_with(Deferred::default);
10290        if let Deferred::Nothing = deferred {
10291            *deferred = Deferred::Held {
10292                board: board_id.clone(),
10293                created: Vec::new(),
10294            };
10295        }
10296        let Deferred::Held { created, .. } = deferred else {
10297            unreachable!("held back on the line above");
10298        };
10299        let held = match created.iter().position(|held| held.content == *content) {
10300            Some(at) => &mut created[at],
10301            None => {
10302                created.push(HeldBack {
10303                    content: content.clone(),
10304                    fields: Vec::new(),
10305                    status: None,
10306                    clears: Vec::new(),
10307                    close: None,
10308                    edges: Vec::new(),
10309                });
10310                created.last_mut().expect("just held")
10311            }
10312        };
10313        for (field, value) in fields {
10314            held.fields.retain(|(held, _)| *held != field);
10315            held.clears.retain(|held| *held != field);
10316            held.fields.push((field, value));
10317        }
10318        for field in clears {
10319            held.fields.retain(|(held, _)| held != field);
10320            if !held.clears.iter().any(|held| held == field) {
10321                held.clears.push(field.to_owned());
10322            }
10323        }
10324        if status.is_some() {
10325            held.status = status;
10326        }
10327        if close.is_some() {
10328            held.close = close;
10329        }
10330        // Nothing of an item created in this command has been sent but its create, so the edges
10331        // it is to have are exactly the ones its latest write names.
10332        held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
10333        Ok(())
10334    }
10335
10336    /// Create every task of `writes`, together, filed on the board and under each one's
10337    /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
10338    ///
10339    /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
10340    /// before the first request — then created, then completed with its parts held back. A
10341    /// request refused part way leaves the issues it did create, which are taken back, with the
10342    /// rest of this call's, before the refusal is answered.
10343    async fn create_together(
10344        &self,
10345        writes: &[ItemWrite<Task>],
10346    ) -> Result<Vec<NativeId>, SourceError> {
10347        let incomings: Vec<Incoming<'_>> = writes
10348            .iter()
10349            .map(|write| self.task_incoming(write, None))
10350            .collect::<Result<_, _>>()?;
10351        let mut prepared = Vec::with_capacity(writes.len());
10352        for (incoming, write) in incomings.iter().zip(writes) {
10353            if write.target.is_some() {
10354                return Err(SourceError::Malformed {
10355                    message: "a write naming its target was handed to a batch of creates".into(),
10356                });
10357            }
10358            prepared.push(
10359                self.prepare_write(incoming, None, &write.depends_on)
10360                    .await?,
10361            );
10362        }
10363        let mut inputs = Vec::with_capacity(prepared.len());
10364        for (incoming, prepared) in incomings.iter().zip(&prepared) {
10365            let WriteDestination::New(target) = &prepared.destination else {
10366                return Err(SourceError::Malformed {
10367                    message: "an update was decided for a write handed to a batch of creates"
10368                        .into(),
10369                });
10370            };
10371            let repository_id = self.repository_id(target, incoming).await?;
10372            inputs.push(filed_input(
10373                &repository_id,
10374                prepared.board.id.as_str(),
10375                incoming,
10376                &prepared.body,
10377            ));
10378        }
10379        let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
10380        let refused = 'create: {
10381            for chunk in inputs.chunks(MUTATION_BATCH) {
10382                let mut variables = serde_json::Map::new();
10383                for slot in 0..MUTATION_BATCH {
10384                    variables.insert(
10385                        format!("create{slot}"),
10386                        chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10387                    );
10388                    variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10389                }
10390                let data = match self
10391                    .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
10392                    .await
10393                {
10394                    Ok(data) => data,
10395                    Err(error) => break 'create Some(error),
10396                };
10397                let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
10398                    let said: Vec<&str> = errors
10399                        .as_array()
10400                        .into_iter()
10401                        .flatten()
10402                        .filter_map(|error| error.get("message").and_then(Value::as_str))
10403                        .collect();
10404                    SourceError::Refused {
10405                        message: if said.is_empty() {
10406                            "GitHub returned GraphQL errors".to_owned()
10407                        } else {
10408                            said.join("; ")
10409                        },
10410                    }
10411                });
10412                for slot in 0..chunk.len() {
10413                    match data
10414                        .pointer(&format!("/create{slot}/issue"))
10415                        .filter(|value| !value.is_null())
10416                    {
10417                        Some(created) => match self.created_landed(created).await {
10418                            Ok(created) => landed.push(created),
10419                            Err(error) => {
10420                                refusal.get_or_insert(error);
10421                            }
10422                        },
10423                        // An alias GitHub refused answers nothing, and the refusal says why.
10424                        None => {
10425                            refusal.get_or_insert(SourceError::Malformed {
10426                                message: "GitHub issue creation returned no issue".into(),
10427                            });
10428                        }
10429                    }
10430                }
10431                if let Some(error) = refusal {
10432                    break 'create Some(error);
10433                }
10434            }
10435            None
10436        };
10437        if let Some(error) = refused {
10438            // None of these ids reaches the caller, so an issue GitHub will not take back is
10439            // named here or by nobody.
10440            let ids: Vec<&NativeId> = landed.iter().map(|created| &created.content_id).collect();
10441            let left = self.delete_issues(&ids).await;
10442            let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10443            return Err(left_behind(error, &left));
10444        }
10445        let created: Vec<NativeId> = landed
10446            .iter()
10447            .map(|landed| landed.content_id.clone())
10448            .collect();
10449        let mut ids = Vec::with_capacity(landed.len());
10450        for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
10451            match self.complete_write(incoming, prepared, landed).await {
10452                Ok(written) => ids.push(written.id),
10453                // 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.
10454                Err(error) => {
10455                    let ids: Vec<&NativeId> = created.iter().collect();
10456                    let left = self.delete_issues(&ids).await;
10457                    let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10458                    return Err(left_behind(error, &left));
10459                }
10460            }
10461        }
10462        Ok(ids)
10463    }
10464
10465    /// Hold back the update of the existing issue `issue` — `members`, what its own
10466    /// [`graphql::UPDATE_ISSUE`] would send beside its id — while writes are being held back — so the updates a copy makes one after
10467    /// another go to GitHub as one request. Whatever this source held of the issue is dropped
10468    /// as the mutation itself would drop it. Answers whether it was held: never while a write
10469    /// the store's cache expects runs, which records each mutation's own answer, and never for
10470    /// an issue an update is already held for, which that one is sent ahead of.
10471    fn hold_update(&self, issue: &NativeId, members: &Value) -> Result<bool, SourceError> {
10472        if self.expected.journaling() || self.deferred()?.is_none() {
10473            return Ok(false);
10474        }
10475        let mut held = self
10476            .held_updates
10477            .lock()
10478            .map_err(|_| SourceError::Unavailable {
10479                message: "issue updates held back were left inconsistent; run the command again"
10480                    .into(),
10481            })?;
10482        if held.iter().any(|earlier| earlier.issue == *issue) {
10483            return Ok(false);
10484        }
10485        let named = |value: &str| issue.0 == value;
10486        self.resolved_cache()?
10487            .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10488        self.read_ahead()?.retain(|id, item| {
10489            !named(&id.0) && !item.as_ref().is_some_and(|item| named(&item.item_id))
10490        });
10491        self.carried()?
10492            .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10493        held.push(HeldUpdate::new(issue, members));
10494        Ok(true)
10495    }
10496
10497    /// Send every update [`Self::hold_update`] held back: one alone as [`graphql::UPDATE_ISSUE`]
10498    /// sends it, several as aliased `updateIssue`s of [`graphql::UPDATE_ISSUES`],
10499    /// [`MUTATION_BATCH`] to a request. Each counts one write, as it would alone.
10500    async fn send_held_updates(&self) -> Result<(), SourceError> {
10501        let held = std::mem::take(&mut *self.held_updates.lock().map_err(|_| {
10502            SourceError::Unavailable {
10503                message: "issue updates held back were left inconsistent; run the command again"
10504                    .into(),
10505            }
10506        })?);
10507        if let [one] = held.as_slice() {
10508            return self
10509                .send_update(
10510                    graphql::UPDATE_ISSUE,
10511                    "/updateIssue/issue",
10512                    &one.issue,
10513                    one.input.clone(),
10514                )
10515                .await;
10516        }
10517        for chunk in held.chunks(MUTATION_BATCH) {
10518            let mut variables = serde_json::Map::new();
10519            for slot in 0..MUTATION_BATCH {
10520                variables.insert(
10521                    format!("update{slot}"),
10522                    chunk.get(slot).unwrap_or(&chunk[0]).input.clone(),
10523                );
10524                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10525            }
10526            let data = self
10527                .graphql(graphql::UPDATE_ISSUES, Value::Object(variables))
10528                .await?;
10529            let inputs: Vec<&Value> = chunk.iter().map(|held| &held.input).collect();
10530            check_aliased_answers(
10531                graphql::UPDATE_ISSUES,
10532                &data,
10533                "update",
10534                &inputs,
10535                &[("issue", "id")],
10536            )?;
10537        }
10538        Ok(())
10539    }
10540
10541    /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
10542    /// created items' board items, then their field values and edges together in requests of
10543    /// [`WRITE_BATCH`], then their closes in requests of [`MUTATION_BATCH`]. Anything refused
10544    /// takes back every issue held, [`DELETE_BATCH`] to a request, and answers the refusal.
10545    async fn send_held_back(&self) -> Result<(), SourceError> {
10546        let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
10547            return Ok(());
10548        };
10549        if created.is_empty() {
10550            return Ok(());
10551        }
10552        let sent = self.send_deferred(board.as_str(), &created).await;
10553        if let Err(error) = sent {
10554            // Best effort, and the failure is what the caller is told, as a write of one item
10555            // that fails part way takes back what it created: the copy that held these back
10556            // journalled every one, so one this cannot take back its undo tries again, and says
10557            // so when it cannot either.
10558            let ids: Vec<&NativeId> = created.iter().map(|held| &held.content).collect();
10559            let _ = self.delete_issues(&ids).await;
10560            return Err(error);
10561        }
10562        Ok(())
10563    }
10564
10565    /// The requests [`Self::send_held_back`] makes.
10566    async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
10567        let items = self.created_board_items(board, created).await?;
10568        let mut fields: Vec<Value> = Vec::new();
10569        let mut closes: Vec<Value> = Vec::new();
10570        let mut edges: Vec<Value> = Vec::new();
10571        for held in created {
10572            let (item, status) = &items[&held.content];
10573            let mut values = held.fields.clone();
10574            // A status the board already shows — set by a workflow a person turned on to file
10575            // new items at it — is not written again.
10576            if let Some((field, option, name)) = &held.status
10577                && status.as_deref() != Some(name.as_str())
10578            {
10579                values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
10580            }
10581            for (field, value) in values {
10582                fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
10583            }
10584            if let Some(reason) = held.close {
10585                closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
10586            }
10587            for far in &held.edges {
10588                edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
10589            }
10590            if !held.clears.is_empty() {
10591                let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
10592                self.set_item_fields(board, item, &[], &clears).await?;
10593            }
10594        }
10595        // Each alias answers with what it wrote, which is what its input named: the board item a
10596        // field value is set on, the issue closed, the issue blocked and the one blocking it.
10597        let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
10598        let close_answer: &[(&str, &str)] = &[("issue", "id")];
10599        let block_answer: &[(&str, &str)] =
10600            &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
10601        // Every field value, then every edge, as one ordered list sent `WRITE_BATCH` aliases to a
10602        // request: a request holding both kinds is the one document carrying both, so a plan's
10603        // values and edges cost one request where they fit in one.
10604        let writes: Vec<HeldWrite<'_>> = fields
10605            .iter()
10606            .map(HeldWrite::Field)
10607            .chain(edges.iter().map(HeldWrite::Edge))
10608            .collect();
10609        for chunk in writes.chunks(WRITE_BATCH) {
10610            let values: Vec<&Value> = chunk
10611                .iter()
10612                .filter_map(|write| match write {
10613                    HeldWrite::Field(input) => Some(*input),
10614                    HeldWrite::Edge(_) => None,
10615                })
10616                .collect();
10617            let blocks: Vec<&Value> = chunk
10618                .iter()
10619                .filter_map(|write| match write {
10620                    HeldWrite::Edge(input) => Some(*input),
10621                    HeldWrite::Field(_) => None,
10622                })
10623                .collect();
10624            let (document, sends) = match (values.is_empty(), blocks.is_empty()) {
10625                (false, false) => (graphql::WRITE_FIELDS_AND_EDGES, ["sendField", "sendBlock"]),
10626                (false, true) => (graphql::WRITE_FIELD_VALUES, ["send", "send"]),
10627                _ => (graphql::ADD_BLOCKED_BYS, ["send", "send"]),
10628            };
10629            let mut variables = serde_json::Map::new();
10630            let kinds = [
10631                ("field", &values, sends[0], field_answer),
10632                ("block", &blocks, sends[1], block_answer),
10633            ];
10634            for (prefix, inputs, send, _) in &kinds {
10635                if inputs.is_empty() {
10636                    continue;
10637                }
10638                for slot in 0..WRITE_BATCH {
10639                    variables.insert(
10640                        format!("{prefix}{slot}"),
10641                        (*inputs.get(slot).unwrap_or(&inputs[0])).clone(),
10642                    );
10643                    variables.insert(format!("{send}{slot}"), json!(slot < inputs.len()));
10644                }
10645            }
10646            let data = self.graphql(document, Value::Object(variables)).await?;
10647            for (prefix, inputs, _, answers) in kinds {
10648                check_aliased_answers(document, &data, prefix, inputs, answers)?;
10649            }
10650        }
10651        for chunk in closes.chunks(MUTATION_BATCH) {
10652            let mut variables = serde_json::Map::new();
10653            for slot in 0..MUTATION_BATCH {
10654                variables.insert(
10655                    format!("close{slot}"),
10656                    chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10657                );
10658                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10659            }
10660            let data = self
10661                .graphql(graphql::CLOSE_ISSUES, Value::Object(variables))
10662                .await?;
10663            let inputs: Vec<&Value> = chunk.iter().collect();
10664            check_aliased_answers(graphql::CLOSE_ISSUES, &data, "close", &inputs, close_answer)?;
10665        }
10666        for held in created {
10667            self.learn_item(&held.content, &items[&held.content].0)?;
10668        }
10669        Ok(())
10670    }
10671
10672    /// The board item each of `created` is on this board, with its `Status` as the board set it:
10673    /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
10674    /// eight and sixteen — for only those GitHub has not filed yet.
10675    ///
10676    /// **Membership is never assumed.** `createIssue`'s `projectV2Ids` files an issue some time
10677    /// after GitHub answered the create — the feasibility spike saw 0.45 to 1.84 seconds, and no
10678    /// create answer carrying the item — so only a read says it is there. One still absent once
10679    /// those reads are spent is filed with [`graphql::ADD_TO_BOARD`], one write each; GitHub
10680    /// filing it meanwhile refuses that "Content already exists in this project", which says it
10681    /// is placed, and one more read finds the item rather than a second one being made.
10682    async fn created_board_items(
10683        &self,
10684        board: &str,
10685        created: &[HeldBack],
10686    ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
10687        let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
10688        let unfiled = |found: &BTreeMap<NativeId, (String, Option<String>)>| -> Vec<NativeId> {
10689            created
10690                .iter()
10691                .map(|held| held.content.clone())
10692                .filter(|id| !found.contains_key(id))
10693                .collect()
10694        };
10695        let mut wait = Duration::from_secs(1);
10696        for attempt in 0..6 {
10697            let absent = unfiled(&found);
10698            if absent.is_empty() {
10699                return Ok(found);
10700            }
10701            if attempt > 0 {
10702                self.clock
10703                    .sleep_because(wait, pacing::cause::BOARD_FILING)
10704                    .await;
10705                wait = wait.saturating_mul(2);
10706            }
10707            self.read_created_items(board, &absent, &mut found).await?;
10708        }
10709        let mut placed_meanwhile = Vec::new();
10710        for id in unfiled(&found) {
10711            match self
10712                .graphql(
10713                    graphql::ADD_TO_BOARD,
10714                    json!({"input":{"projectId":board,"contentId":id.0}}),
10715                )
10716                .await
10717            {
10718                Ok(added) => {
10719                    let item = added
10720                        .pointer("/addProjectV2ItemById/item")
10721                        .filter(|value| !value.is_null())
10722                        .ok_or_else(|| SourceError::Malformed {
10723                            message: "GitHub board addition returned no project item".into(),
10724                        })?;
10725                    found.insert(id, (required_nonblank_str(item, "id")?.to_owned(), None));
10726                }
10727                Err(error) if already_on_the_board(&error) => placed_meanwhile.push(id),
10728                Err(error) => return Err(error),
10729            }
10730        }
10731        if !placed_meanwhile.is_empty() {
10732            self.read_created_items(board, &placed_meanwhile, &mut found)
10733                .await?;
10734        }
10735        let absent = unfiled(&found);
10736        if absent.is_empty() {
10737            return Ok(found);
10738        }
10739        let absent: Vec<&str> = absent.iter().map(|id| id.0.as_str()).collect();
10740        Err(SourceError::Unavailable {
10741            message: format!(
10742                "GitHub created {} filed on board {board} and, after 31 seconds and adding {} \
10743                 to it, still answers {} on no item of it: {}; next: run the copy again once \
10744                 GitHub has caught up",
10745                if absent.len() == 1 {
10746                    "an issue"
10747                } else {
10748                    "issues"
10749                },
10750                if absent.len() == 1 { "it" } else { "them" },
10751                if absent.len() == 1 { "it" } else { "them" },
10752                absent.join(", ")
10753            ),
10754        })
10755    }
10756
10757    /// One [`graphql::CREATED_BOARD_ITEMS`] read of each of `ids`, [`COPY_DETAIL_BATCH`] to a
10758    /// request, adding to `found` the board item of every one GitHub has filed on `board`.
10759    async fn read_created_items(
10760        &self,
10761        board: &str,
10762        ids: &[NativeId],
10763        found: &mut BTreeMap<NativeId, (String, Option<String>)>,
10764    ) -> Result<(), SourceError> {
10765        for chunk in ids.chunks(COPY_DETAIL_BATCH) {
10766            let mut variables = serde_json::Map::new();
10767            for slot in 0..COPY_DETAIL_BATCH {
10768                variables.insert(
10769                    format!("id{slot}"),
10770                    json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
10771                );
10772                variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
10773            }
10774            variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
10775            variables.insert("statusName".into(), json!(STATUS_FIELD));
10776            let data = self
10777                .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
10778                .await?;
10779            for (slot, id) in chunk.iter().enumerate() {
10780                // An answer for another issue than the one asked about says nothing of
10781                // where that one is filed, and its board item is not this one's.
10782                let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
10783                let named = node.map(|node| node.get("id").and_then(Value::as_str));
10784                if let Some(named) = named
10785                    && named != Some(id.0.as_str())
10786                {
10787                    return Err(SourceError::Malformed {
10788                        message: format!(
10789                            "GitHub answered the board items of {} when asked for those of {}",
10790                            named.unwrap_or("an issue it did not name"),
10791                            id.0
10792                        ),
10793                    });
10794                }
10795                // An issue GitHub does not answer yet is not filed yet; one it answers is
10796                // read whole, its connection, each board item's board and its status, and
10797                // any of them unreadable is refused rather than read as not filed.
10798                let Some(node) = node else { continue };
10799                let malformed = |what: &str| SourceError::Malformed {
10800                    message: format!("GitHub answered the board items of {} with {what}", id.0),
10801                };
10802                let nodes = node
10803                    .pointer("/projectItems/nodes")
10804                    .and_then(Value::as_array)
10805                    .ok_or_else(|| malformed("no list of board items"))?;
10806                let mut item = None;
10807                for node in nodes {
10808                    let on = node
10809                        .pointer("/project/id")
10810                        .and_then(Value::as_str)
10811                        .ok_or_else(|| malformed("a board item naming no board"))?;
10812                    if on == board {
10813                        item = Some(node);
10814                    }
10815                }
10816                if let Some(item) = item {
10817                    let status = match item.get("status") {
10818                        None | Some(Value::Null) => None,
10819                        Some(status) => Some(
10820                            status
10821                                .get("name")
10822                                .and_then(Value::as_str)
10823                                .ok_or_else(|| malformed("a status that names no option"))?
10824                                .to_owned(),
10825                        ),
10826                    };
10827                    found.insert(
10828                        id.clone(),
10829                        (required_nonblank_str(item, "id")?.to_owned(), status),
10830                    );
10831                }
10832            }
10833        }
10834        Ok(())
10835    }
10836
10837    /// The board item id of `content`, which this process created and remembered without one,
10838    /// in every half of its own view.
10839    fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
10840        if let Some(held) = self.resolved_cache()?.get_mut(content) {
10841            held.item_id = item.to_owned();
10842        }
10843        if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
10844            held.item_id = item.to_owned();
10845        }
10846        Ok(())
10847    }
10848
10849    /// Ask together which items each of `origins` names, and what creating an item reads; see
10850    /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
10851    /// page is left to its own lookup.
10852    async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
10853        if self.repository.is_none() {
10854            return Ok(());
10855        }
10856        let unasked: Vec<&String> = {
10857            let held = self.narrowed_cache()?;
10858            let mut unasked: Vec<&String> = Vec::new();
10859            for origin in origins {
10860                if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
10861                    && !unasked.contains(&origin)
10862                {
10863                    unasked.push(origin);
10864                }
10865            }
10866            unasked
10867        };
10868        if unasked.is_empty() {
10869            return Ok(());
10870        }
10871        // One batch is held back for the read ahead that follows, which may carry a project's
10872        // members beside it in the same request.
10873        if unasked.len() <= ORIGIN_BATCH {
10874            let mut pending =
10875                self.pending_origins
10876                    .lock()
10877                    .map_err(|_| SourceError::Unavailable {
10878                        message:
10879                            "origins held for a read ahead were left inconsistent; run the command \
10880                          again"
10881                                .into(),
10882                    })?;
10883            for origin in unasked {
10884                if !pending.contains(origin) {
10885                    pending.push(origin.clone());
10886                }
10887            }
10888            return Ok(());
10889        }
10890        let unasked: Vec<String> = unasked.into_iter().cloned().collect();
10891        self.send_origin_lookups(&unasked, None).await
10892    }
10893
10894    /// Hold, for [`TaskSource::member_classifications`] of `project`, each member's
10895    /// classification and repositories as the members page `data` carries them — when that page
10896    /// is every member, each one names `project` as its parent, and every record it holds is
10897    /// readable. Any other page holds nothing, so the members are read as they always were.
10898    fn hold_member_classes(&self, project: &NativeId, data: &Value) -> Result<(), SourceError> {
10899        let Some(page) = data
10900            .pointer("/members/subIssues")
10901            .filter(|page| !page.is_null())
10902        else {
10903            return Ok(());
10904        };
10905        if page
10906            .pointer("/pageInfo/hasNextPage")
10907            .and_then(Value::as_bool)
10908            != Some(false)
10909        {
10910            return Ok(());
10911        }
10912        let Some(nodes) = page.get("nodes").and_then(Value::as_array) else {
10913            return Ok(());
10914        };
10915        let mut members = Vec::with_capacity(nodes.len());
10916        for node in nodes {
10917            if node.pointer("/parent/id").and_then(Value::as_str) != Some(project.0.as_str()) {
10918                return Ok(());
10919            }
10920            let Some(body) = node.get("body").and_then(Value::as_str) else {
10921                return Ok(());
10922            };
10923            // Every node here is filed under `project`, which `BoardKind::of` reads as one of its
10924            // tasks whatever its marker says, so each is a member the full read would classify.
10925            let Ok((_, slot)) = metadata_body(Some(body.to_owned())) else {
10926                return Ok(());
10927            };
10928            let Ok(classification) = Classification::from_metadata(&slot) else {
10929                return Ok(());
10930            };
10931            let repositories = if slot.contains_key(Repository::METADATA_KEY) {
10932                let Ok(repositories) = Repository::from_metadata(&slot) else {
10933                    return Ok(());
10934                };
10935                repositories
10936            } else {
10937                let Some(origin) = node
10938                    .pointer("/repository/nameWithOwner")
10939                    .and_then(Value::as_str)
10940                else {
10941                    return Ok(());
10942                };
10943                let Ok(own) = Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST))
10944                else {
10945                    return Ok(());
10946                };
10947                vec![own]
10948            };
10949            members.push((classification, repositories));
10950        }
10951        self.member_classes
10952            .lock()
10953            .map_err(|_| SourceError::Unavailable {
10954                message: "a project's members held for its classification were left \
10955                          inconsistent; run the command again"
10956                    .into(),
10957            })?
10958            .insert(project.clone(), members);
10959        Ok(())
10960    }
10961
10962    /// Send every origin held back for the read ahead, beside the members of `members` when it
10963    /// names a project — or nothing, when none is held.
10964    async fn send_pending_origins(&self, members: Option<&NativeId>) -> Result<(), SourceError> {
10965        let pending = std::mem::take(&mut *self.pending_origins.lock().map_err(|_| {
10966            SourceError::Unavailable {
10967                message: "origins held for a read ahead were left inconsistent; run the command \
10968                          again"
10969                    .into(),
10970            }
10971        })?);
10972        if pending.is_empty() {
10973            return Ok(());
10974        }
10975        self.send_origin_lookups(&pending, members).await
10976    }
10977
10978    /// [`graphql::ORIGIN_LOOKUPS`] of `unasked`, [`ORIGIN_BATCH`] to a request — the first one
10979    /// [`graphql::ORIGIN_LOOKUPS_WITH_MEMBERS`], carrying the members of `members`, when it names
10980    /// a project — holding each origin's answer and, from a members page that is the whole of
10981    /// them, each member's classification.
10982    async fn send_origin_lookups(
10983        &self,
10984        unasked: &[String],
10985        members: Option<&NativeId>,
10986    ) -> Result<(), SourceError> {
10987        let Some(repository) = self.repository.clone() else {
10988            return Ok(());
10989        };
10990        let mut members = members;
10991        for chunk in unasked.chunks(ORIGIN_BATCH) {
10992            let mut variables = json!({"owner":self.owner,"number":self.project_number,
10993                "type":"ISSUE","lookupFirst":ORIGIN_LOOKUPS_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
10994                "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
10995                "repositoryOwner":repository.owner,"repositoryName":repository.name});
10996            for slot in 0..ORIGIN_BATCH {
10997                let origin = chunk.get(slot).unwrap_or(&chunk[0]);
10998                variables[format!("filter{slot}")] =
10999                    json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
11000                variables[format!("search{slot}")] = json!(
11001                    self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
11002                );
11003                variables[format!("ask{slot}")] = json!(slot < chunk.len());
11004            }
11005            let project = members.take();
11006            let document = match project {
11007                Some(project) => {
11008                    variables["project"] = json!(project.0);
11009                    variables["memberFirst"] = json!(MEMBER_PAGE_SIZE);
11010                    graphql::ORIGIN_LOOKUPS_WITH_MEMBERS
11011                }
11012                None => graphql::ORIGIN_LOOKUPS,
11013            };
11014            let data = self.graphql(document, variables).await?;
11015            self.fields_read(&data)?;
11016            if let Some(project) = project {
11017                self.hold_member_classes(project, &data)?;
11018            }
11019            if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
11020                let id = required_nonblank_str(node, "id")?.to_owned();
11021                self.repository_cache()?.insert(repository.clone(), id);
11022                if let Some(numeric) = node
11023                    .get("databaseId")
11024                    .and_then(Value::as_u64)
11025                    .and_then(std::num::NonZeroU64::new)
11026                {
11027                    self.numeric_repositories
11028                        .lock()
11029                        .await
11030                        .insert(repository.clone(), numeric);
11031                }
11032            }
11033            for (slot, origin) in chunk.iter().enumerate() {
11034                let items = data
11035                    .pointer(&format!("/o{slot}/projectV2/items"))
11036                    .filter(|value| !value.is_null())
11037                    .ok_or_else(|| SourceError::Refused {
11038                        message: format!(
11039                            "GitHub project {}/{} was not found or is not visible to the token",
11040                            self.owner, self.project_number
11041                        ),
11042                    })?;
11043                let searched =
11044                    data.get(format!("s{slot}"))
11045                        .ok_or_else(|| SourceError::Malformed {
11046                            message: "GitHub search response has no search connection".into(),
11047                        })?;
11048                if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
11049                    continue;
11050                }
11051                let mut found: Vec<Resolved> = Vec::new();
11052                for item in optional_nodes(Some(items), "project items")?
11053                    .into_iter()
11054                    .flatten()
11055                {
11056                    if let Some(resolved) = self.resolve(item)?
11057                        && resolved.content_kind == ContentKind::Issue
11058                        && !found.iter().any(|held| held.id == resolved.id)
11059                    {
11060                        found.push(resolved);
11061                    }
11062                }
11063                for node in optional_nodes(Some(searched), "search")?
11064                    .into_iter()
11065                    .flatten()
11066                {
11067                    if let Some(resolved) = self.resolve_issue(node).await?
11068                        && !found.iter().any(|held| held.id == resolved.id)
11069                    {
11070                        found.push(resolved);
11071                    }
11072                }
11073                self.narrowed_cache()?
11074                    .insert(Narrowing::Origin((*origin).clone()).key(), found);
11075            }
11076        }
11077        Ok(())
11078    }
11079
11080    /// Delete every issue of `ids`, [`DELETE_BATCH`] to a request with
11081    /// [`graphql::DELETE_ISSUES`], and answer those still there: each a request's answer does not
11082    /// confirm, every one of a request refused whole included.
11083    async fn delete_issues(&self, ids: &[&NativeId]) -> Vec<NativeId> {
11084        let mut left = Vec::new();
11085        for chunk in ids.chunks(DELETE_BATCH) {
11086            let mut variables = serde_json::Map::new();
11087            for slot in 0..DELETE_BATCH {
11088                let id = chunk.get(slot).unwrap_or(&chunk[0]);
11089                variables.insert(format!("delete{slot}"), json!({"issueId": id.0}));
11090                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
11091            }
11092            let data = self
11093                .graphql(graphql::DELETE_ISSUES, Value::Object(variables))
11094                .await
11095                .unwrap_or(Value::Null);
11096            for (slot, id) in chunk.iter().enumerate() {
11097                // Confirmed only by the repository the deleted issue was in, named by a
11098                // non-blank id: anything else says nothing of whether the issue is gone.
11099                let confirmed = data
11100                    .pointer(&format!("/delete{slot}/repository/id"))
11101                    .and_then(Value::as_str)
11102                    .is_some_and(|id| !id.trim().is_empty());
11103                if !confirmed {
11104                    left.push((*id).clone());
11105                }
11106            }
11107        }
11108        left
11109    }
11110
11111    /// Delete one issue, which takes its board item with it.
11112    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
11113        let data = self
11114            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
11115            .await?;
11116        data.pointer("/deleteIssue/repository")
11117            .filter(|value| !value.is_null())
11118            .ok_or_else(|| SourceError::Malformed {
11119                message: "GitHub issue deletion returned no repository".into(),
11120            })?;
11121        self.forget(id)?;
11122        Ok(())
11123    }
11124
11125    /// Remove one item this copy created, so a copy that could not finish leaves the board
11126    /// as it found it.
11127    ///
11128    /// Deleting the issue takes its board item with it, so there is no second mutation to
11129    /// keep in step. An id the board does not hold is not an error: the item is already
11130    /// gone, which is the state this asks for. Which that is, is decided by reading the item
11131    /// by its own id — a listing of the board can still be missing an item it holds, and
11132    /// reading that as *already gone* would leave behind the very item this was asked to
11133    /// take back.
11134    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
11135        let Some(item) = self.bound_item(id).await? else {
11136            return Ok(());
11137        };
11138        if item.content_kind == ContentKind::DraftIssue {
11139            return Err(SourceError::Refused {
11140                message: format!(
11141                    "GitHub item {} is a draft, and this source removes an item by deleting \
11142                     its issue; next: remove it from the board by hand",
11143                    id.0
11144                ),
11145            });
11146        }
11147        let data = self
11148            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
11149            .await?;
11150        data.pointer("/deleteIssue/repository")
11151            .filter(|value| !value.is_null())
11152            .ok_or_else(|| SourceError::Malformed {
11153                message: "GitHub issue deletion returned no repository".into(),
11154            })?;
11155        self.forget(id)?;
11156        Ok(())
11157    }
11158
11159    /// The issue a comment call on `task` is about, or `None` when this board holds no such
11160    /// task.
11161    ///
11162    /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
11163    /// read of the task cannot disagree about which ids name one: a project or a document of
11164    /// this board is not a task here either.
11165    ///
11166    /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
11167    /// issues and a draft is not one. It is refused rather than answered with an empty page,
11168    /// which would read as a task nobody has commented on yet.
11169    async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
11170        let cached = self.resolved_cache()?.get(task).cloned();
11171        let Some(item) = (match cached {
11172            Some(item) => Some(item),
11173            None => self.item_by_id(task).await?,
11174        })
11175        .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
11176            return Ok(None);
11177        };
11178        if item.content_kind == ContentKind::DraftIssue {
11179            return Err(self.draft_has_no_comments(task));
11180        }
11181        Ok(Some(item.id))
11182    }
11183
11184    /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
11185    /// issues, and a draft is not one.
11186    fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
11187        SourceError::Refused {
11188            message: format!(
11189                "task {} of source {} is a draft item on the board, and GitHub keeps \
11190                 comments on issues alone, so a draft has none to read or write; next: \
11191                 convert the draft to an issue on the board, then comment on the issue it \
11192                 becomes",
11193                task.0, self.name
11194            ),
11195        }
11196    }
11197
11198    /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
11199    /// request — or `None` when this board holds no task by that id.
11200    ///
11201    /// What `task show` and a comment listing read. A draft is a task with no comments, so it
11202    /// is answered with the draft and the refusal, at the price of the draft's own read.
11203    async fn issue_detail(
11204        &self,
11205        id: &NativeId,
11206        page: &PageRequest,
11207    ) -> Result<Option<TaskDetailRead>, SourceError> {
11208        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
11209        let asked = self
11210            .graphql(
11211                graphql::ISSUE_DETAIL,
11212                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
11213                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
11214                       "duplicates":true}),
11215            )
11216            .await;
11217        let data = match asked {
11218            Ok(data) => data,
11219            Err(error) if unresolvable_node(&error) => return Ok(None),
11220            Err(error) => return Err(error),
11221        };
11222        // `node` is null for an id that names nothing, and absent only from an answer this
11223        // source cannot read — never the same thing.
11224        let node = data.get("node").ok_or_else(|| SourceError::Malformed {
11225            message: format!("GitHub answered the read of {} with no node", id.0),
11226        })?;
11227        self.detail_of(id, node, true, after).await
11228    }
11229
11230    /// Several tasks, each with the first page of its comments when `comments` is set, read
11231    /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
11232    /// order.
11233    ///
11234    /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
11235    /// one item at a time, so that id is answered as missing and the others as themselves; any
11236    /// other refusal is every id of that batch's answer.
11237    async fn issue_details(
11238        &self,
11239        ids: &[NativeId],
11240        comments: Option<&PageRequest>,
11241    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
11242        let mut read = Vec::with_capacity(ids.len());
11243        for batch in ids.chunks(DETAIL_BATCH) {
11244            match self
11245                .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
11246                .await
11247            {
11248                Ok(data) => {
11249                    for (slot, id) in batch.iter().enumerate() {
11250                        // Every alias asked for is answered, null for an id naming nothing;
11251                        // one missing is an answer this source cannot read.
11252                        let read_one = match data.get(format!("i{slot}")) {
11253                            Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
11254                            None => Err(SourceError::Malformed {
11255                                message: format!(
11256                                    "GitHub answered a batch read with no item for {}",
11257                                    id.0
11258                                ),
11259                            }),
11260                        };
11261                        read.push(read_one);
11262                    }
11263                }
11264                Err(error) if unresolvable_node(&error) => {
11265                    for id in batch {
11266                        read.push(match comments {
11267                            Some(page) => self.issue_detail(id, page).await,
11268                            None => self.task_read(id).await,
11269                        });
11270                    }
11271                }
11272                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
11273            }
11274        }
11275        read
11276    }
11277
11278    /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
11279    async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
11280        Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
11281            task,
11282            comments: None,
11283        }))
11284    }
11285
11286    /// What one node a detail read reached says: the task this board holds by `id`, with the
11287    /// page of comments the node carries when `commented` — or `None` for a node that is no
11288    /// task of this board.
11289    ///
11290    /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
11291    /// and an item this process created answers from this process's own record, which a node
11292    /// read taken moments after the write can still be behind.
11293    async fn detail_of(
11294        &self,
11295        id: &NativeId,
11296        node: &Value,
11297        commented: bool,
11298        after: Option<&str>,
11299    ) -> Result<Option<TaskDetailRead>, SourceError> {
11300        if node.is_null() {
11301            return Ok(None);
11302        }
11303        let draft = optional_str(node, "__typename")? == Some("DraftIssue");
11304        // An issue answered under one id is that id's, or the answer is not one this source
11305        // can report: reporting another issue's task and comments under the qualified id asked
11306        // for would be the one wrong answer here. A draft's own read checks the same.
11307        if !draft
11308            && optional_str(node, "__typename")? == Some("Issue")
11309            && required_str(node, "id")? != id.0
11310        {
11311            return Err(SourceError::Malformed {
11312                message: format!(
11313                    "GitHub answered the read of {} with issue {}",
11314                    id.0,
11315                    required_str(node, "id")?
11316                ),
11317            });
11318        }
11319        let item = if draft {
11320            self.draft_by_id(id).await?
11321        } else {
11322            self.resolve_issue(node).await?
11323        };
11324        // A read that carried the whole of what the issue is blocked by — a copy's — is kept
11325        // for this command, so the dependency read the copy makes next is answered from it.
11326        if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
11327            self.resolved_cache()?.insert(read.id.clone(), read.clone());
11328        }
11329        let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
11330            return Ok(None);
11331        };
11332        let own = self.created()?.iter().find(|own| own.id == *id).cloned();
11333        let task = own.unwrap_or(item).task()?;
11334        let comments = match (commented, draft) {
11335            (false, _) => None,
11336            (true, true) => Some(Err(self.draft_has_no_comments(id))),
11337            (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
11338        };
11339        if commented && !draft && after.is_none() {
11340            self.expected.learn_comments(id, node);
11341        }
11342        Ok(Some(TaskDetailRead { task, comments }))
11343    }
11344
11345    /// Whether the comment `comment` is one of `issue`'s own.
11346    ///
11347    /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
11348    /// comment's id and nothing else: a comment id given against the wrong task would
11349    /// otherwise change a comment on some other issue entirely. An id that names nothing, or
11350    /// names something that is not an issue comment, is a comment this task does not have —
11351    /// which is what GitHub refusing to resolve it means too.
11352    async fn comment_is_on(
11353        &self,
11354        issue: &NativeId,
11355        comment: &NativeId,
11356    ) -> Result<bool, SourceError> {
11357        Ok(self.comment_on(issue, comment).await?.is_some())
11358    }
11359
11360    /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
11361    /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
11362    async fn comment_on(
11363        &self,
11364        issue: &NativeId,
11365        comment: &NativeId,
11366    ) -> Result<Option<String>, SourceError> {
11367        // A write the store's cache expects knows which comments its issue held, and their
11368        // bodies, at the version it expects: what this read would answer.
11369        if let Some(primed) = self.expected.primed_comment(issue, comment) {
11370            return Ok(primed);
11371        }
11372        let asked = self
11373            .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
11374            .await;
11375        let data = match asked {
11376            Ok(data) => data,
11377            Err(error) if unresolvable_node(&error) => return Ok(None),
11378            Err(error) => return Err(error),
11379        };
11380        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
11381            return Ok(None);
11382        };
11383        if optional_str(node, "__typename")? != Some("IssueComment") {
11384            return Ok(None);
11385        }
11386        let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
11387            message: format!("GitHub issue comment {} names no issue", comment.0),
11388        })?;
11389        if required_str(on, "id")? != issue.0 {
11390            return Ok(None);
11391        }
11392        Ok(Some(required_str(node, "body")?.to_owned()))
11393    }
11394
11395    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
11396    async fn partition_edges(
11397        &self,
11398        near_kind: BoardKind,
11399        near_content: ContentKind,
11400        carried: Option<&[Value]>,
11401        depends_on: &[DependencyEdge],
11402    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
11403        let mut native = Vec::new();
11404        let mut fallback = Vec::new();
11405        let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
11406            .iter()
11407            .map(|edge| {
11408                // An opaque far end names another tracker, so it is never this board's own.
11409                let same_source = edge.to.designator().is_none()
11410                    && edge
11411                        .to
11412                        .source()
11413                        .is_none_or(|source| source == self.name.as_str());
11414                // A qualified id's source segment runs to its *first* colon — `GlobalId` and
11415                // `DependencyEndpoint::source` both read it that way — and a native id may hold
11416                // colons of its own, so the far end is everything after that one separator.
11417                // Splitting at the last would truncate `work:urn:task:7` to `7`.
11418                let far_id = if edge.to.is_qualified() {
11419                    edge.to
11420                        .id()
11421                        .split_once(':')
11422                        .map_or(edge.to.id(), |(_, native)| native)
11423                } else {
11424                    edge.to.id()
11425                };
11426                // One that already blocks the near issue was answered by that issue's own
11427                // read, which carried each of its blockers' kinds — an issue every one — so it
11428                // is not read again.
11429                let blocking = carried.and_then(|nodes| {
11430                    nodes
11431                        .iter()
11432                        .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
11433                });
11434                (edge, far_id, same_source, blocking)
11435            })
11436            .collect();
11437        // Every other same-source far end is read by its own id, exactly as the item it is a
11438        // far end of is: whether this board holds it is that read's answer, never a listing's.
11439        // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
11440        let mut unread: Vec<NativeId> = Vec::new();
11441        for (_, far_id, same_source, blocking) in &far_ends {
11442            let id = NativeId((*far_id).to_owned());
11443            if *same_source && blocking.is_none() && !unread.contains(&id) {
11444                unread.push(id);
11445            }
11446        }
11447        let read: BTreeMap<NativeId, Option<Resolved>> = unread
11448            .iter()
11449            .cloned()
11450            .zip(self.items_by_ids(&unread).await?)
11451            .collect();
11452        for (edge, far_id, same_source, blocking) in far_ends {
11453            let far = match (same_source, blocking) {
11454                (false, _) => None,
11455                (true, Some(node)) => Some(FarEnd {
11456                    kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
11457                        BoardKind::Document
11458                    } else {
11459                        BoardKind::Work(related_kind(node)?)
11460                    },
11461                    content_kind: ContentKind::Issue,
11462                }),
11463                (true, None) => {
11464                    let read = read
11465                        .get(&NativeId(far_id.to_owned()))
11466                        .cloned()
11467                        .flatten()
11468                        .ok_or_else(|| SourceError::Refused {
11469                            message: format!("GitHub dependency item {far_id} was not found"),
11470                        })?;
11471                    Some(FarEnd {
11472                        kind: read.kind,
11473                        content_kind: read.content_kind,
11474                    })
11475                }
11476            };
11477            let far = far.as_ref();
11478            // The caller says which kind the far end is, and this board holds the far end
11479            // itself, so a disagreement is settled here rather than stored: recorded, the
11480            // wrong kind would read back as a cross-level edge that never existed; written
11481            // natively, it would name a relationship of a different level than the caller
11482            // asked for.
11483            //
11484            // A far end this board holds as a *document* fails the same comparison and is
11485            // refused by the same sentence: `ItemKind` has no document variant because
11486            // nothing may point at one, so no caller can name it correctly and the refusal
11487            // is the only honest answer.
11488            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
11489                return Err(SourceError::Refused {
11490                    message: format!(
11491                        "GitHub dependency item {far_id} is a {} of this board, and this item \
11492                         names it as a {}; record the kind it is",
11493                        disagreeing.kind.describes(),
11494                        edge.to.kind.marker()
11495                    ),
11496                });
11497            }
11498            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
11499            // however the far end is spelled — and one classified native here would be
11500            // written nowhere at all, because a draft's native reconciliation never runs.
11501            let native_here = near_content == ContentKind::Issue
11502                && far.is_some_and(|far| {
11503                    far.content_kind == ContentKind::Issue
11504                        && BoardKind::Work(edge.to.kind) == near_kind
11505                });
11506            if native_here {
11507                native.push(far_id.to_owned());
11508            } else {
11509                fallback.push(edge.clone());
11510            }
11511        }
11512        Ok((native, fallback))
11513    }
11514
11515    async fn update_existing(
11516        &self,
11517        item: &Resolved,
11518        incoming: &Incoming<'_>,
11519        body: &Option<String>,
11520        status_target: Option<&StatusTarget>,
11521    ) -> Result<(), SourceError> {
11522        let title = incoming.written_title();
11523        // A terminal status closes the issue here, in the same mutation as its body: its board
11524        // option was selected before this, so a close never lands on an item whose board cannot
11525        // show it.
11526        let mut fields = match item.content_kind {
11527            ContentKind::DraftIssue => json!({"title":title,"body":body}),
11528            ContentKind::Issue => json!({"title":title,"body":body,
11529                                         "stateInput":state_input(status_target)}),
11530        };
11531        // Only the members whose value differs from the item as this write read it are sent,
11532        // and nothing at all when none does — whether or not the store's cache expects a
11533        // version of it. A member left out is one GitHub leaves as it is, so a person's edit
11534        // of it since that read is kept rather than overwritten with what it already held,
11535        // and a re-copy of an unchanged item costs no mutation.
11536        if let Some(members) = fields.as_object_mut() {
11537            if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
11538                members.remove("title");
11539            }
11540            let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
11541                (Some(Value::String(sent)), Some(held)) => sent == held,
11542                (Some(Value::Null), held) => held.is_none_or(str::is_empty),
11543                _ => false,
11544            };
11545            if body_unchanged {
11546                members.remove("body");
11547            }
11548            // An issue already in the state the target asks for keeps it: an open one stays
11549            // open, and a closed one whose closure reads as the target's category keeps its
11550            // reason — a duplicate's says more than this write would.
11551            let unchanged_state = match status_target {
11552                Some(StatusTarget::Terminal(_, reason)) => {
11553                    item.closed
11554                        && closed_category(item.reason.as_deref())
11555                            == closed_category(Some(reason.reason()))
11556                }
11557                Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
11558                None => true,
11559            };
11560            if unchanged_state {
11561                members.remove("stateInput");
11562            }
11563            if members.is_empty() {
11564                return Ok(());
11565            }
11566        }
11567        self.update_content(item.content_kind, &item.id, fields)
11568            .await
11569    }
11570
11571    /// Update one board item's content with exactly `fields` beside its id, through the
11572    /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
11573    /// a draft.
11574    ///
11575    /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
11576    /// is what lets a narrow write carry the one thing it changes and nothing else.
11577    async fn update_content(
11578        &self,
11579        kind: ContentKind,
11580        id: &NativeId,
11581        fields: Value,
11582    ) -> Result<(), SourceError> {
11583        let (operation, id_key, pointer) = match kind {
11584            ContentKind::DraftIssue => (
11585                graphql::UPDATE_DRAFT,
11586                "draftIssueId",
11587                "/updateProjectV2DraftIssue/draftIssue",
11588            ),
11589            ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
11590        };
11591        if kind == ContentKind::Issue && self.hold_update(id, &fields)? {
11592            return Ok(());
11593        }
11594        let mut input = fields;
11595        input[id_key] = json!(id.0);
11596        self.send_update(operation, pointer, id, input).await
11597    }
11598
11599    /// Send one item's update, `input`, as `operation`, and hold GitHub's answer at `pointer`
11600    /// to naming that item, `id`.
11601    async fn send_update(
11602        &self,
11603        operation: &str,
11604        pointer: &str,
11605        id: &NativeId,
11606        input: Value,
11607    ) -> Result<(), SourceError> {
11608        let data = self.graphql(operation, json!({"input":input})).await?;
11609        let returned = data
11610            .pointer(pointer)
11611            .ok_or_else(|| SourceError::Malformed {
11612                message: "GitHub item update returned no item".into(),
11613            })?;
11614        if required_str(returned, "id")? != id.0 {
11615            return Err(SourceError::Malformed {
11616                message: "GitHub item update returned the wrong item".into(),
11617            });
11618        }
11619        Ok(())
11620    }
11621
11622    /// Creates one issue, files it on the board, and reports what a read of it would say:
11623    /// its content id, its board item id, and the web address GitHub gave it.
11624    ///
11625    /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
11626    /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
11627    /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
11628    /// board item, and the `addProjectV2ItemById` that then had to follow was refused
11629    /// "Content already exists in this project". A terminal status is not written here:
11630    /// `finish_write` selects its option first and closes the issue after, so a close never
11631    /// lands on an item whose board cannot show it.
11632    ///
11633    /// The address and the number come back here because this is the only place either is
11634    /// known before GitHub's own board read catches up — an item this run created answers
11635    /// the reads that follow it out of the record below, and one remembered without them
11636    /// would report no location and no key for the rest of the run.
11637    async fn create_and_file_issue(
11638        &self,
11639        board_id: &str,
11640        repository: &RepositoryTarget,
11641        incoming: &Incoming<'_>,
11642        body: &Option<String>,
11643    ) -> Result<Landed, SourceError> {
11644        let repository_id = self.repository_id(repository, incoming).await?;
11645        let data = self
11646            .graphql(
11647                graphql::CREATE_ISSUE,
11648                json!({"input":{
11649                    "repositoryId":repository_id,"title":incoming.written_title(),"body":body
11650                }}),
11651            )
11652            .await?;
11653        let created = data
11654            .pointer("/createIssue/issue")
11655            .filter(|value| !value.is_null())
11656            .ok_or_else(|| SourceError::Malformed {
11657                message: "GitHub issue creation returned no issue".into(),
11658            })?;
11659        let content_id = NativeId(required_str(created, "id")?.to_owned());
11660        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
11661        // a response without it is not worth failing a landed write over — the item simply
11662        // reports no location until the board read catches up, which is what it did before.
11663        let url = optional_str(created, "url")?.map(str::to_owned);
11664        // The issue exists from here on, so an unreadable number and a refused board
11665        // filing below each try, best effort, to take it back: an issue in the repository
11666        // that is on no board is an item nobody asked for and nothing here would find again.
11667        //
11668        // Its number is optional on the same terms its address is — a landed write is not
11669        // worth failing over a member that came back missing, and such an item reports no
11670        // handle until a board read catches up. A number that is *present* and is not an
11671        // unsigned integer is still a response this source cannot read.
11672        let number = match created_issue_number(created) {
11673            Ok(number) => number,
11674            Err(error) => {
11675                let _ = self.delete_issue(&content_id).await;
11676                return Err(error);
11677            }
11678        };
11679        let added = match self
11680            .graphql(
11681                graphql::ADD_TO_BOARD,
11682                json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
11683            )
11684            .await
11685        {
11686            Ok(added) => added,
11687            Err(error) => {
11688                let _ = self.delete_issue(&content_id).await;
11689                return Err(error);
11690            }
11691        };
11692        let item = added
11693            .pointer("/addProjectV2ItemById/item")
11694            .filter(|value| !value.is_null())
11695            .ok_or_else(|| SourceError::Malformed {
11696                message: "GitHub board addition returned no project item".into(),
11697            })?;
11698        self.note_written(&content_id, url.as_deref());
11699        Ok(Landed {
11700            content_id,
11701            item_id: Some(required_str(item, "id")?.to_owned()),
11702            url,
11703            number,
11704        })
11705    }
11706
11707    /// Move one issue under the project it now belongs to, or out of the one it left.
11708    async fn reparent(
11709        &self,
11710        held: Option<NativeId>,
11711        child: &NativeId,
11712        wanted: Option<&NativeId>,
11713    ) -> Result<(), SourceError> {
11714        if held.as_ref() == wanted {
11715            return Ok(());
11716        }
11717        if let Some(held) = &held {
11718            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
11719                .await?;
11720        }
11721        if let Some(wanted) = wanted {
11722            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
11723                .await?;
11724        }
11725        Ok(())
11726    }
11727
11728    async fn sub_issue(
11729        &self,
11730        operation: &str,
11731        parent: &NativeId,
11732        child: &NativeId,
11733        root: &str,
11734    ) -> Result<(), SourceError> {
11735        let data = self
11736            .graphql(
11737                operation,
11738                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
11739            )
11740            .await?;
11741        let issue =
11742            data.pointer(&format!("/{root}/issue"))
11743                .ok_or_else(|| SourceError::Malformed {
11744                    message: "GitHub sub-issue update returned no issue".into(),
11745                })?;
11746        let sub =
11747            data.pointer(&format!("/{root}/subIssue"))
11748                .ok_or_else(|| SourceError::Malformed {
11749                    message: "GitHub sub-issue update returned no sub-issue".into(),
11750                })?;
11751        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
11752            return Err(SourceError::Malformed {
11753                message: "GitHub sub-issue update returned the wrong issues".into(),
11754            });
11755        }
11756        Ok(())
11757    }
11758
11759    /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
11760    /// whether there was one.
11761    ///
11762    /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
11763    /// relationships are not read: there is nothing a read of them could find.
11764    async fn reconcile_blocked_by(
11765        &self,
11766        content_id: &NativeId,
11767        native: &[String],
11768        issue: Issue<'_>,
11769    ) -> Result<bool, SourceError> {
11770        let current = match issue {
11771            Issue::Created => Vec::new(),
11772            Issue::Existing(Some(held)) => held
11773                .iter()
11774                .map(|far| required_str(far, "id").map(str::to_owned))
11775                .collect::<Result<Vec<_>, _>>()?,
11776            Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
11777        };
11778        let mut changed = false;
11779        for (operation, far_id) in current
11780            .iter()
11781            .filter(|id| !native.contains(id))
11782            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
11783            .chain(
11784                native
11785                    .iter()
11786                    .filter(|id| !current.contains(id))
11787                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
11788            )
11789        {
11790            let data = self
11791                .graphql(
11792                    operation,
11793                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
11794                )
11795                .await?;
11796            let root = if operation == graphql::ADD_BLOCKED_BY {
11797                "addBlockedBy"
11798            } else {
11799                "removeBlockedBy"
11800            };
11801            let issue =
11802                data.pointer(&format!("/{root}/issue"))
11803                    .ok_or_else(|| SourceError::Malformed {
11804                        message: "GitHub dependency update returned no issue".into(),
11805                    })?;
11806            let blocker = data
11807                .pointer(&format!("/{root}/blockingIssue"))
11808                .ok_or_else(|| SourceError::Malformed {
11809                    message: "GitHub dependency update returned no blocking issue".into(),
11810                })?;
11811            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
11812            {
11813                return Err(SourceError::Malformed {
11814                    message: "GitHub dependency update returned the wrong issues".into(),
11815                });
11816            }
11817            changed = true;
11818        }
11819        Ok(changed)
11820    }
11821}
11822
11823/// The board field values one write sends, in the order GitHub runs them, and the fields it
11824/// clears after them: the origin field when it moves, the `Status` option `column` names, the
11825/// priority, then each projected field.
11826fn field_writes<'a>(
11827    existing: Option<&Resolved>,
11828    origin_field: Option<&str>,
11829    origin: &str,
11830    column: Option<(String, String)>,
11831    priority: Option<&'a PriorityWrite>,
11832    projections: &'a [ProjectionWrite],
11833) -> (Vec<(String, Value)>, Vec<&'a str>) {
11834    let mut fields = Vec::new();
11835    if let Some(field_id) = origin_field
11836        && existing.map_or(!origin.is_empty(), |item| {
11837            item.origin.as_deref().unwrap_or("") != origin
11838        })
11839    {
11840        fields.push((field_id.to_owned(), json!({"text":origin})));
11841    }
11842    if let Some((field_id, option_id)) = column {
11843        fields.push((field_id, json!({"singleSelectOptionId":option_id})));
11844    }
11845    let mut clears = Vec::new();
11846    match priority {
11847        Some(PriorityWrite::Select { field, option }) => {
11848            fields.push((field.clone(), json!({"singleSelectOptionId":option})));
11849        }
11850        Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
11851        None => {}
11852    }
11853    for projection in projections {
11854        projection.join(&mut fields, &mut clears);
11855    }
11856    (fields, clears)
11857}
11858
11859/// Everything one write decided before sending anything: see
11860/// [`GitHubProjectsSource::prepare_write`].
11861struct Prepared {
11862    /// What the write lands on.
11863    destination: WriteDestination,
11864    board: BoardFields,
11865    status_target: Option<StatusTarget>,
11866    /// The `Status` field, the option and the option's name the status lands on.
11867    // 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.
11868    column: Option<(String, String, String)>,
11869    priority_write: Option<PriorityWrite>,
11870    projections: Vec<ProjectionWrite>,
11871    projected: BTreeMap<String, String>,
11872    content_kind: ContentKind,
11873    own_repository: Option<Repository>,
11874    /// The far ends of the item's native blocked-by relationships.
11875    // 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.
11876    native: Vec<String>,
11877    slot: BTreeMap<String, Value>,
11878    content: Option<String>,
11879    body: Option<String>,
11880    /// The copy origin the item records, empty for none.
11881    // 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.
11882    origin: String,
11883    origin_field: Option<String>,
11884}
11885
11886/// One write a copy held back of the kinds sent together: a board field value, or a blocked-by
11887/// edge, each its input.
11888enum HeldWrite<'a> {
11889    Field(&'a Value),
11890    Edge(&'a Value),
11891}
11892
11893/// Whether `error` is GitHub refusing to file an issue on a board because it is filed there
11894/// already — what `addProjectV2ItemById` answers once `createIssue`'s `projectV2Ids` has placed
11895/// it, rather than answering with the item it holds.
11896fn already_on_the_board(error: &SourceError) -> bool {
11897    error
11898        .to_string()
11899        .contains("Content already exists in this project")
11900}
11901
11902/// Hold every alias `inputs` sent under `prefix` in `data` to answering with what its input
11903/// named — each `(root, named)` of `answers` — and refuse one that answered nothing or another
11904/// item.
11905fn check_aliased_answers(
11906    document: &str,
11907    data: &Value,
11908    prefix: &str,
11909    inputs: &[&Value],
11910    answers: &[(&str, &str)],
11911) -> Result<(), SourceError> {
11912    for (slot, input) in inputs.iter().enumerate() {
11913        for (root, named) in answers {
11914            let answered = data
11915                .pointer(&format!("/{prefix}{slot}/{root}/id"))
11916                .and_then(Value::as_str)
11917                .ok_or_else(|| SourceError::Malformed {
11918                    message: format!(
11919                        "GitHub answered {} with nothing at {prefix}{slot}",
11920                        operation_description(document)
11921                    ),
11922                })?;
11923            let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
11924            if answered != asked {
11925                return Err(SourceError::Malformed {
11926                    message: format!(
11927                        "GitHub answered {} at {prefix}{slot} with {answered}, which is not \
11928                         {asked}, the item it was asked to write",
11929                        operation_description(document)
11930                    ),
11931                });
11932            }
11933        }
11934    }
11935    Ok(())
11936}
11937
11938/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
11939/// back — each issue of `left` it created that is still in the repository, which its caller,
11940/// never handed their ids, could not name.
11941fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
11942    if left.is_empty() {
11943        return error;
11944    }
11945    SourceError::Refused {
11946        message: format!(
11947            "{error}; GitHub then refused to take back {}, which this write created and which \
11948             are still in the repository; next: delete {} before writing again",
11949            left.join(", "),
11950            if left.len() == 1 { "it" } else { "them" }
11951        ),
11952    }
11953}
11954
11955/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
11956enum WriteDestination {
11957    /// The item as it stood, for an update.
11958    Existing(Box<Resolved>),
11959    /// Where a new item is created.
11960    New(RepositoryTarget),
11961}
11962
11963impl WriteDestination {
11964    /// The item an update writes over, as it stood; `None` for a create.
11965    fn existing(&self) -> Option<&Resolved> {
11966        match self {
11967            Self::Existing(item) => Some(item),
11968            Self::New(_) => None,
11969        }
11970    }
11971}
11972
11973/// One existing issue's update held back by [`GitHubProjectsSource::hold_update`]: the issue,
11974/// and the `UpdateIssueInput` its own [`graphql::UPDATE_ISSUE`] would have sent — the members
11975/// it writes beside that issue's id, which only [`Self::new`] sets.
11976struct HeldUpdate {
11977    issue: NativeId,
11978    // 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.
11979    input: Value,
11980}
11981
11982impl HeldUpdate {
11983    /// The update writing `members` — an `UpdateIssueInput` without its id — to `issue`.
11984    fn new(issue: &NativeId, members: &Value) -> Self {
11985        let mut input = members.clone();
11986        input["id"] = json!(issue.0);
11987        Self {
11988            issue: issue.clone(),
11989            input,
11990        }
11991    }
11992}
11993
11994/// What a copy has held back of the items it created; see
11995/// [`GitHubProjectsSource::hold_back`].
11996#[derive(Debug, Default)]
11997enum Deferred {
11998    /// Writes are being held back, and nothing has been held yet.
11999    #[default]
12000    Nothing,
12001    /// The items held back, every one of them on `board`.
12002    Held {
12003        /// The board every one of them is on.
12004        board: BoardId,
12005        /// Each created item, in the order it was created.
12006        created: Vec<HeldBack>,
12007    },
12008}
12009
12010impl Deferred {
12011    /// Whether `content` is one of the items held back.
12012    fn holds(&self, content: &NativeId) -> bool {
12013        match self {
12014            Self::Nothing => false,
12015            Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
12016        }
12017    }
12018}
12019
12020/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
12021#[derive(Debug)]
12022struct HeldBack {
12023    /// The issue.
12024    content: NativeId,
12025    /// Its board field values other than its `Status`, in the order they are written.
12026    fields: Vec<(String, Value)>,
12027    /// The `Status` field, and the option and its name that the item's status lands on.
12028    // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
12029    status: Option<(String, String, String)>,
12030    /// The fields it clears after those are written.
12031    clears: Vec<String>,
12032    /// The reason it is closed for, once its option is selected; `None` leaves it open.
12033    close: Option<ClosedState>,
12034    /// The issues it is blocked by.
12035    edges: Vec<NativeId>,
12036}
12037
12038/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
12039/// no GitHub type has a field of this name.
12040const ANSWER_ERRORS: &str = "onetaskgraphErrors";
12041
12042/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
12043/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
12044///
12045/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
12046/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
12047/// created named in the data beside it — the ids a copy needs to take them back.
12048fn answer_with_errors(body: &str) -> Option<Value> {
12049    let parsed: Value = serde_json::from_str(body).ok()?;
12050    let errors = parsed
12051        .get("errors")?
12052        .as_array()
12053        .filter(|errors| !errors.is_empty())?;
12054    let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
12055    data[ANSWER_ERRORS] = Value::Array(errors.clone());
12056    Some(data)
12057}
12058
12059/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
12060/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
12061fn filed_input(
12062    repository: &str,
12063    board: &str,
12064    incoming: &Incoming<'_>,
12065    body: &Option<String>,
12066) -> Value {
12067    let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
12068                           "body":body,"projectV2Ids":[board]});
12069    if let Some(parent) = incoming.parent {
12070        input["parentIssueId"] = json!(parent.0);
12071    }
12072    input
12073}
12074
12075/// The name of option `option` of field `field` among a board's `fields`.
12076fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
12077    fields
12078        .get("nodes")?
12079        .as_array()?
12080        .iter()
12081        .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
12082        .get("options")?
12083        .as_array()?
12084        .iter()
12085        .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
12086        .get("name")?
12087        .as_str()
12088}
12089
12090/// What a write needs to know of one far end it names: which kind of item it is, and whether
12091/// it is an issue a native relationship can name.
12092struct FarEnd {
12093    kind: BoardKind,
12094    content_kind: ContentKind,
12095}
12096
12097/// Whether the issue one write reconciles was created by that write or was already there.
12098#[derive(Clone, Copy, PartialEq, Eq)]
12099enum Issue<'a> {
12100    /// Created by this write, so it holds no relationships yet.
12101    Created,
12102    /// On the board before this write, holding whatever relationships it holds — the far
12103    /// ends of its whole `blockedBy`, when the read that reached it carried them.
12104    Existing(Option<&'a [Value]>),
12105}
12106
12107/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
12108enum Reached {
12109    /// An issue this board holds, resolved into everything this source reports about it.
12110    Held(Box<Resolved>),
12111    /// Nothing this board holds: no such node, or a node on some other board.
12112    Nothing,
12113    /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
12114    /// again by [`GitHubProjectsSource::draft_by_id`].
12115    Draft,
12116}
12117
12118/// What GitHub says when a string is not a node id it can resolve.
12119///
12120/// Matched because it is the ordinary answer to a project selector naming a project by its
12121/// *name*, and reporting that as a failure would make naming one impossible. It is read
12122/// off the refusal GitHub sent, never guessed from the shape of the string: this source
12123/// does not define the syntax of a GitHub node id and would be wrong about it.
12124const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
12125
12126/// `error` with `note` added to the end of what it says, its kind and every other member
12127/// unchanged — so a caller still branches on the failure that happened, and reads beside it
12128/// what that failure left behind.
12129fn noting(error: SourceError, note: &str) -> SourceError {
12130    match error {
12131        SourceError::Config { message } => SourceError::Config {
12132            message: message + note,
12133        },
12134        SourceError::Auth { message } => SourceError::Auth {
12135            message: message + note,
12136        },
12137        SourceError::Refused { message } => SourceError::Refused {
12138            message: message + note,
12139        },
12140        SourceError::RateLimited {
12141            retry_after_seconds,
12142            message,
12143        } => SourceError::RateLimited {
12144            retry_after_seconds,
12145            message: Some(message.unwrap_or_default() + note),
12146        },
12147        SourceError::Unavailable { message } => SourceError::Unavailable {
12148            message: message + note,
12149        },
12150        SourceError::Malformed { message } => SourceError::Malformed {
12151            message: message + note,
12152        },
12153    }
12154}
12155
12156/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
12157/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
12158fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
12159    let mut variables = serde_json::Map::new();
12160    for slot in 0..COPY_DETAIL_BATCH {
12161        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
12162        variables.insert(format!("id{slot}"), json!(id));
12163    }
12164    variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
12165    variables.insert("children".to_owned(), json!(children));
12166    variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
12167    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
12168    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12169    variables.insert("duplicates".to_owned(), json!(true));
12170    Value::Object(variables)
12171}
12172
12173/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
12174/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
12175/// for them.
12176///
12177/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
12178/// is read again at no added price.
12179fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
12180    let mut variables = serde_json::Map::new();
12181    for slot in 0..DETAIL_BATCH {
12182        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
12183        variables.insert(format!("id{slot}"), json!(id));
12184    }
12185    variables.insert(
12186        "first".to_owned(),
12187        json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
12188    );
12189    variables.insert("comments".to_owned(), json!(comments.is_some()));
12190    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
12191    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12192    variables.insert("duplicates".to_owned(), json!(true));
12193    Value::Object(variables)
12194}
12195
12196/// Whether this refusal is GitHub saying the id names no node at all.
12197fn unresolvable_node(error: &SourceError) -> bool {
12198    matches!(error, SourceError::Refused { message }
12199        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
12200}
12201
12202/// One project name, as a search qualifier which filters on it at the server.
12203///
12204/// Quoted so the whole title is one phrase rather than a bag of words, with the two
12205/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
12206/// the way it documents. A title matched here is still compared for equality afterwards:
12207/// the qualifier narrows what the server sends, and this source decides what it names.
12208fn title_qualifier(name: &str) -> String {
12209    format!("in:title {}", quoted(name))
12210}
12211
12212/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
12213/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
12214/// it documents — so a value holding a qualifier's spelling is searched for rather than
12215/// obeyed.
12216fn quoted(value: &str) -> String {
12217    let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
12218    format!("\"{escaped}\"")
12219}
12220
12221/// The search qualifier for the issues updated at or after `since`.
12222///
12223/// Written to the second, rounded down, which can only widen what the search returns.
12224fn updated_qualifier(since: DateTime<Utc>) -> String {
12225    format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
12226}
12227
12228/// The search terms that narrow a board-scoped issue search to a task query's text and
12229/// metadata predicates, or `None` when it carries neither.
12230///
12231/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
12232/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
12233/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
12234/// matches each in any field the `in:` qualifier names, so a query naming a title search and
12235/// a metadata value searches both fields for both — wider than asked, never narrower, and
12236/// every candidate is confirmed in process afterwards.
12237///
12238/// **This narrows a text search, and that is this source's declared semantics.** GitHub
12239/// matches whole tokens where a substring rule would match inside a word, so an item holding
12240/// the text only inside a longer word is not returned. A text of nothing but whitespace
12241/// matches every item, so it narrows nothing and is not sent.
12242fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
12243    let text = query
12244        .text
12245        .as_ref()
12246        .filter(|text| !text.terms.trim().is_empty());
12247    if text.is_none() && query.metadata.is_empty() {
12248        return None;
12249    }
12250    let (title, body) = match text.map(|text| text.fields) {
12251        None => (false, true),
12252        Some(TextFields::Title) => (true, !query.metadata.is_empty()),
12253        Some(TextFields::Content) => (false, true),
12254        Some(TextFields::TitleOrContent) => (true, true),
12255    };
12256    let fields = match (title, body) {
12257        (true, true) => "in:title,body",
12258        (true, false) => "in:title",
12259        _ => "in:body",
12260    };
12261    let phrases = text
12262        .map(|text| text.terms.clone())
12263        .into_iter()
12264        .chain(
12265            query
12266                .metadata
12267                .iter()
12268                .map(|wanted| as_stored(wanted.value())),
12269        )
12270        .map(|phrase| quoted(&phrase))
12271        .collect::<Vec<_>>();
12272    Some(format!("{fields} {}", phrases.join(" ")))
12273}
12274
12275/// The search terms that narrow a board-scoped issue search to a project or document query's
12276/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
12277/// carrying that text alone is sent as by [`narrowing_qualifiers`].
12278fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
12279    narrowing_qualifiers(&TaskQuery {
12280        text: text.cloned(),
12281        ..TaskQuery::default()
12282    })
12283}
12284
12285/// Refuses a project or document query's text GitHub's issue search cannot find, before
12286/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
12287/// query's.
12288fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
12289    refuse_unsearchable(&TaskQuery {
12290        text: text.cloned(),
12291        ..TaskQuery::default()
12292    })
12293}
12294
12295/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
12296/// before anything is asked of GitHub.
12297///
12298/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
12299/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
12300/// left out, the search is every issue of the board. So this source says it cannot answer
12301/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
12302/// nothing GitHub could search for, and keeps the board read it always had.
12303fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
12304    const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
12305                       letter or digit with a bounded query";
12306    if let Some(text) = &query.text
12307        && !text.terms.trim().is_empty()
12308        && !has_words(&text.terms)
12309    {
12310        return Err(SourceError::Refused {
12311            message: format!(
12312                "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
12313                text.terms
12314            ),
12315        });
12316    }
12317    if let Some(wanted) = query
12318        .metadata
12319        .iter()
12320        .find(|wanted| !has_words(wanted.value()))
12321    {
12322        return Err(SourceError::Refused {
12323            message: format!(
12324                "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
12325                wanted.value(),
12326                std::iter::once(wanted.key())
12327                    .chain(wanted.path().iter().map(String::as_str))
12328                    .collect::<Vec<_>>()
12329                    .join("/"),
12330            ),
12331        });
12332    }
12333    Ok(())
12334}
12335
12336/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
12337fn has_words(phrase: &str) -> bool {
12338    phrase.chars().any(char::is_alphanumeric)
12339}
12340
12341/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
12342///
12343/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
12344/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
12345/// which GitHub's word match would read as different words.
12346fn as_stored(value: &str) -> String {
12347    let encoded = Value::String(value.to_owned()).to_string();
12348    encoded[1..encoded.len() - 1].to_owned()
12349}
12350
12351/// The one narrower question a task query carrying a text, metadata or origin predicate is
12352/// sent as.
12353enum Narrowing {
12354    /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
12355    Origin(String),
12356    /// The board-scoped issue search narrowed by these qualifiers.
12357    Search(String),
12358}
12359
12360impl Narrowing {
12361    /// What this question is remembered under for the length of one command.
12362    fn key(&self) -> String {
12363        match self {
12364            Self::Origin(origin) => format!("origin {origin}"),
12365            Self::Search(also) => format!("search {also}"),
12366        }
12367    }
12368}
12369
12370/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
12371enum Resumed {
12372    /// It reported another page, which starts after this cursor.
12373    More(String),
12374    /// It has ended. Sending this cursor again — the page's own end when it had one, and
12375    /// otherwise the cursor it was reached from — answers an empty page, so the one document
12376    /// can go on walking the other connection.
12377    Ended(Option<String>),
12378}
12379
12380impl Resumed {
12381    /// Whether the connection has another page.
12382    const fn has_more(&self) -> bool {
12383        matches!(self, Self::More(_))
12384    }
12385
12386    /// The cursor to send this connection next.
12387    fn cursor(self) -> Option<String> {
12388        match self {
12389            Self::More(next) => Some(next),
12390            Self::Ended(last) => last,
12391        }
12392    }
12393}
12394
12395/// Where `connection`, reached from `after`, resumes — refused when it reports another page
12396/// with no cursor to it, or from a cursor that does not advance.
12397fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
12398    let info = connection
12399        .get("pageInfo")
12400        .ok_or_else(|| SourceError::Malformed {
12401            message: "GitHub connection has no pageInfo".into(),
12402        })?;
12403    let end = optional_str(info, "endCursor")?;
12404    if required_bool(info, "hasNextPage")? {
12405        let next = end.ok_or_else(|| SourceError::Malformed {
12406            message: "GitHub connection reports another page and no endCursor".into(),
12407        })?;
12408        validate_cursor_progress(after, next)?;
12409        return Ok(Resumed::More(next.to_owned()));
12410    }
12411    Ok(Resumed::Ended(
12412        end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
12413    ))
12414}
12415
12416/// The board, and every item on it this source reports.
12417#[derive(Clone)]
12418struct Board {
12419    id: String,
12420    fields: Value,
12421    items: Vec<Resolved>,
12422}
12423
12424/// What a write needs of the board and nothing more: its node id and its field
12425/// definitions, in the shape a read of the board's own `fields` gives them.
12426///
12427/// Deliberately no items. A write decides which item it writes, which parent it files
12428/// under and which far ends it names by reading each of them by its own id; this is the
12429/// half of the board those reads cannot carry, and holding no item is what keeps it from
12430/// ever being asked whether an item is there.
12431#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12432struct BoardFields {
12433    id: BoardId,
12434    fields: Value,
12435}
12436
12437/// A board's node id: what a field write and `addProjectV2ItemById` address.
12438///
12439/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
12440/// refused where it is read, and one an item names blank is read as not named at all.
12441#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
12442#[serde(try_from = "String")]
12443struct BoardId(String);
12444
12445impl TryFrom<String> for BoardId {
12446    type Error = String;
12447
12448    /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
12449    fn try_from(id: String) -> Result<Self, Self::Error> {
12450        Self::parse(&id).map_err(|error| error.to_string())
12451    }
12452}
12453
12454/// Where one write left its item, for the record the rest of the command reads it out of.
12455///
12456/// A named record rather than a tuple because the update arm and the create arm each fill
12457/// all four, and two `Option`s of different meaning side by side in a tuple are two
12458/// positions a reader has to count.
12459struct Landed {
12460    /// The issue's own node id, which is the [`NativeId`] this source reports.
12461    content_id: NativeId,
12462    /// The board item's id, which is what a field write addresses — `None` for an issue its
12463    /// create filed on the board itself, whose board item GitHub adds after answering and
12464    /// [`TaskSource::finish_writes`] reads.
12465    // 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.
12466    item_id: Option<String>,
12467    /// The web address GitHub gave the issue, when it gave one.
12468    // 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.
12469    url: Option<String>,
12470    /// The issue's number on its repository, when GitHub reported one.
12471    number: Option<u64>,
12472}
12473
12474impl BoardId {
12475    fn parse(id: &str) -> Result<Self, SourceError> {
12476        if id.trim().is_empty() {
12477            return Err(SourceError::Malformed {
12478                message: "GitHub named a board with a blank node id".into(),
12479            });
12480        }
12481        Ok(Self(id.to_owned()))
12482    }
12483
12484    fn as_str(&self) -> &str {
12485        &self.0
12486    }
12487}
12488
12489impl Board {
12490    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
12491        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
12492        let nodes = fields
12493            .get("nodes")
12494            .and_then(Value::as_array)
12495            .ok_or_else(|| SourceError::Malformed {
12496                message: "GitHub project fields.nodes is not an array".into(),
12497            })?;
12498        Ok(nodes
12499            .iter()
12500            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
12501    }
12502}
12503
12504/// One board item, resolved into everything this source reports about it.
12505///
12506/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
12507/// [`TaskSource::prime_write`] takes back — see [`expected`].
12508#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12509struct Resolved {
12510    item_id: String,
12511    id: NativeId,
12512    content_kind: ContentKind,
12513    kind: BoardKind,
12514    title: String,
12515    body: Option<String>,
12516    /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
12517    /// that changes the slot alone has to keep byte for byte outside it.
12518    raw_body: Option<String>,
12519    status: Status,
12520    /// The name of the board `Status` option this item sits in, as the board spells it.
12521    option: Option<String>,
12522    /// What its `Priority` field says, read through this instance's mapping.
12523    priority: HeldPriority,
12524    /// Whether this item's issue is closed. A draft has no such state and is never closed.
12525    closed: bool,
12526    /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
12527    /// issue, a draft, and an issue closed before GitHub recorded reasons.
12528    #[serde(default)]
12529    reason: Option<String>,
12530    /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
12531    delivers: Vec<TaskRef>,
12532    /// Every task that delivers this one, read out of its slot. Empty for anything not a
12533    /// task.
12534    delivered_by: Vec<TaskRef>,
12535    labels: Vec<Label>,
12536    parent: Option<NativeId>,
12537    // 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.
12538    origin: Option<String>,
12539    /// What each board text field this instance projects metadata onto holds for this item,
12540    /// by field name — only the fields holding a value, an empty text included. What a write compares the
12541    /// item's metadata against, so a field already holding its value is not written again;
12542    /// never read back as metadata, which the slot alone reports.
12543    projected: BTreeMap<String, String>,
12544    /// The issue's own number on its repository, as GitHub reports it.
12545    ///
12546    /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
12547    /// declares none, and a draft is not filed in a repository to be numbered by one — and
12548    /// an issue this run created whose creating mutation answered without one, which is a
12549    /// response GitHub's own schema says cannot happen and which a landed write is not
12550    /// worth failing over. An `Issue` read off the board always has one.
12551    number: Option<u64>,
12552    url: Option<String>,
12553    created_at: Option<DateTime<Utc>>,
12554    updated_at: Option<DateTime<Utc>>,
12555    /// When the board item itself last changed — what a board field write moves — when the
12556    /// read that reached it said.
12557    item_updated_at: Option<DateTime<Utc>>,
12558    /// How many comments the issue holds, when the read that reached it said.
12559    comment_count: Option<u64>,
12560    /// How many relationship events the issue's timeline holds — every dependency, as
12561    /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
12562    /// removed — when the read that reached it said.
12563    relationship_events: Option<u64>,
12564    /// The server's clock on the response that read it — its `Date` header — when the
12565    /// response carried one.
12566    served_at: Option<DateTime<Utc>>,
12567    /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
12568    /// its labels — when the read that reached it carried all three whole.
12569    #[serde(default)]
12570    facets: Option<change_token::FacetsDigest>,
12571    own_repository: Option<Repository>,
12572    repositories: Vec<Repository>,
12573    classification: Classification,
12574    slot: BTreeMap<String, Value>,
12575    /// The node id of the board this item sits on, when the read that reached it said.
12576    board_id: Option<String>,
12577    /// The definition of every board field this item holds a value of, in the shape a read
12578    /// of the board's own `fields` gives one.
12579    ///
12580    /// Only the fields this item has a value in: a field it holds nothing of is not here,
12581    /// which says nothing about whether the board has it.
12582    fields: Vec<Value>,
12583    /// Every field the board this item sits on defines, as its own read of the board's
12584    /// `fields` gives them — when the read that reached the item carried them, which a read
12585    /// of it by its own id does. What a write of it needs of the board, then, needs no read
12586    /// of the board.
12587    board_fields: Option<Value>,
12588    /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
12589    /// selects one — when the read that reached it carried the connection to its end, which a
12590    /// read of it by its own id does for any issue blocked by no more than a page. What a
12591    /// write reconciles that relationship against, and what a read of its forward edges in
12592    /// the same command answers with.
12593    blocked_by: Option<Vec<Value>>,
12594}
12595
12596impl Resolved {
12597    /// The board this item's own read names it on, when that read named one this source can
12598    /// address.
12599    fn named_board(&self) -> Option<BoardId> {
12600        self.board_id
12601            .as_deref()
12602            .and_then(|id| BoardId::parse(id).ok())
12603    }
12604
12605    /// The board's id and every field it defines, when the read that reached this item
12606    /// carried both — which a read of it by its own id does.
12607    fn carried_board(&self) -> Option<BoardFields> {
12608        Some(BoardFields {
12609            id: self.named_board()?,
12610            fields: self.board_fields.clone()?,
12611        })
12612    }
12613
12614    /// Whether this item holds a value of the board field called `name`, and so carries
12615    /// that field's definition. `false` says nothing about whether the board has the field.
12616    fn defines(&self, name: &str) -> bool {
12617        self.fields
12618            .iter()
12619            .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
12620    }
12621
12622    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
12623    /// in a field of its own, and none of the five keys that are only an encoding.
12624    ///
12625    /// The two delivery keys are left out for every kind, not only for a task: they are
12626    /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
12627    /// document carrying one holds nothing a caller's own metadata could mean by it.
12628    fn metadata(&self) -> BTreeMap<String, Value> {
12629        let mut metadata = self.slot.clone();
12630        metadata.remove(Repository::METADATA_KEY);
12631        metadata.remove(DependencyEdge::RECORDED_KEY);
12632        metadata.remove(ItemKind::METADATA_KEY);
12633        metadata.remove(TaskRef::DELIVERS_KEY);
12634        metadata.remove(TaskRef::DELIVERED_BY_KEY);
12635        metadata.remove(Classification::METADATA_KEY);
12636        // The board field is the origin, and the body's copy of it is only a mirror for the
12637        // issue search to find: an item whose field holds none has none, whatever its body
12638        // says, so no reader ever sees two answers.
12639        metadata.remove(ORIGIN_KEY);
12640        if let Some(origin) = &self.origin {
12641            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
12642        }
12643        metadata
12644    }
12645
12646    /// Where this item is, as a link a reader can open.
12647    ///
12648    /// A board is a hosted place and every issue on it has a web address, so that address
12649    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
12650    /// of place it is, so a reader knows to open it rather than to read a file out. It
12651    /// does not replace or derive from `url`: the field goes on reporting exactly what it
12652    /// reported before, and this says what that address *is*.
12653    ///
12654    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
12655    /// rather than a third variant, which is the contract's "the source did not say". An
12656    /// issue this run created is not one of those: its address comes back from the
12657    /// creating mutation, so it is somewhere a reader can open from the moment it exists
12658    /// rather than from whenever the board read catches up.
12659    fn location(&self) -> Option<Location> {
12660        self.url.clone().map(Location::Url)
12661    }
12662
12663    /// The short handle this board's backend shows people for a task: the issue's number
12664    /// alone, as a decimal string.
12665    ///
12666    /// The number alone rather than `owner/repo#1043`, because that is the contract's
12667    /// value for this backend. A draft has no number and so no handle, which is the
12668    /// contract's *absent* rather than a handle of some other shape — and the native
12669    /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
12670    /// derives from.
12671    fn key(&self) -> Option<String> {
12672        self.number.map(|number| number.to_string())
12673    }
12674
12675    /// This item's change token — see [`change_token`] — or `None` when the read that
12676    /// reached it did not carry all six of its parts, which a draft never does, did not say
12677    /// when GitHub served it, or found it changed too recently to have settled.
12678    fn version(&self) -> Option<String> {
12679        match self.content_kind {
12680            ContentKind::Issue => {
12681                let (issue, item) = (self.updated_at?, self.item_updated_at?);
12682                let parts = change_token::Parts {
12683                    issue,
12684                    item,
12685                    comments: self.comment_count?,
12686                    relationships: self.relationship_events?,
12687                    facets: self.facets.clone()?,
12688                    repository: self.own_repository.clone()?,
12689                };
12690                change_token::settled(issue, item, self.served_at?)
12691                    .then(|| change_token::spell(&parts))
12692            }
12693            ContentKind::DraftIssue => None,
12694        }
12695    }
12696
12697    /// Whether its `Priority` field holds a value at all, mapped or not.
12698    fn holds_priority(&self) -> bool {
12699        self.priority != HeldPriority::Read(Priority::None)
12700    }
12701
12702    /// The task this item is.
12703    ///
12704    /// Fails for an item whose `Priority` field holds an option the mapping does not name:
12705    /// reading that as a level would be a guess, and reading it as `none` would let the next
12706    /// copy clear a priority a person set.
12707    fn task(&self) -> Result<Task, SourceError> {
12708        let priority = match &self.priority {
12709            HeldPriority::Read(priority) => *priority,
12710            HeldPriority::Unmapped(option) => {
12711                return Err(SourceError::Malformed {
12712                    message: format!(
12713                        "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
12714                         this source's priority_mapping does not name, so its priority cannot be \
12715                         read; next: name {option:?} under priority_mapping, or move the item to \
12716                         a mapped option",
12717                        self.id,
12718                        self.number
12719                            .map(|number| format!(" (#{number})"))
12720                            .unwrap_or_default()
12721                    ),
12722                });
12723            }
12724        };
12725        Ok(Task {
12726            version: self.version(),
12727            id: self.id.clone(),
12728            key: self.key(),
12729            title: self.title.clone(),
12730            content: self.body.clone(),
12731            status: self.status.clone(),
12732            priority,
12733            labels: self.labels.clone(),
12734            project: self.parent.clone(),
12735            url: self.url.clone(),
12736            location: self.location(),
12737            created_at: self.created_at,
12738            updated_at: self.updated_at,
12739            metadata: self.metadata(),
12740            repositories: self.repositories.clone(),
12741            delivers: self.delivers.clone(),
12742            delivered_by: self.delivered_by.clone(),
12743            classification: self.classification,
12744        })
12745    }
12746
12747    fn project(&self) -> Project {
12748        Project {
12749            version: self.version(),
12750            id: self.id.clone(),
12751            title: self.title.clone(),
12752            content: self.body.clone(),
12753            status: self.status.clone(),
12754            labels: self.labels.clone(),
12755            url: self.url.clone(),
12756            location: self.location(),
12757            created_at: self.created_at,
12758            updated_at: self.updated_at,
12759            metadata: self.metadata(),
12760            repositories: self.repositories.clone(),
12761            classification: self.classification,
12762        }
12763    }
12764
12765    /// The same issue as a document: the project it is filed under, and no status and no
12766    /// dependencies, because a document is not work.
12767    fn document(&self) -> Document {
12768        Document {
12769            version: self.version(),
12770            id: self.id.clone(),
12771            title: self.title.clone(),
12772            content: self.body.clone(),
12773            project: self.parent.clone(),
12774            labels: self.labels.clone(),
12775            url: self.url.clone(),
12776            location: self.location(),
12777            created_at: self.created_at,
12778            updated_at: self.updated_at,
12779            metadata: self.metadata(),
12780            repositories: self.repositories.clone(),
12781            classification: self.classification,
12782        }
12783    }
12784}
12785
12786/// Where one targeted update moves an item's status, and which of its two halves move.
12787struct StatusMove {
12788    /// The board the item's `Status` field is on.
12789    board: BoardId,
12790    /// The `Status` field's id.
12791    field: String,
12792    /// The option's id.
12793    option: String,
12794    /// The option's name, as the board spells it.
12795    name: String,
12796    /// What the status asks of the issue's state.
12797    target: StatusTarget,
12798    /// The status the item reads as once it is there.
12799    landed: Status,
12800    /// Which of the status's two halves differ from what the item holds.
12801    moves: Moves,
12802}
12803
12804/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
12805/// closed state of its issue, or both. A status neither half of which differs is no move at all,
12806/// and is not a value of this type.
12807#[derive(Clone, Copy, PartialEq, Eq)]
12808enum Moves {
12809    /// The option alone.
12810    Option,
12811    /// The issue's state alone: open, closed, or closed with another reason.
12812    State,
12813    /// Both.
12814    Both,
12815}
12816
12817impl Moves {
12818    /// What differs, or `None` when nothing does.
12819    const fn of(option: bool, state: bool) -> Option<Self> {
12820        match (option, state) {
12821            (true, true) => Some(Self::Both),
12822            (true, false) => Some(Self::Option),
12823            (false, true) => Some(Self::State),
12824            (false, false) => None,
12825        }
12826    }
12827
12828    /// Whether the option moves.
12829    const fn option(self) -> bool {
12830        matches!(self, Self::Option | Self::Both)
12831    }
12832
12833    /// Whether the issue's state moves.
12834    const fn state(self) -> bool {
12835        matches!(self, Self::State | Self::Both)
12836    }
12837}
12838
12839/// What one write is, and the status that comes with being it.
12840///
12841/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
12842/// status and a task or a project always has one, so "a document carrying a status" and
12843/// "a task carrying none" are states a write cannot be in rather than states every use
12844/// site below has to defend against.
12845enum Written<'a> {
12846    /// A document, which is not work and so has no status at all.
12847    Document,
12848    /// A task or a project, and the status it is being written with.
12849    Work(ItemKind, &'a Status),
12850}
12851
12852impl Written<'_> {
12853    /// Which of the board's three kinds this write is.
12854    const fn kind(&self) -> BoardKind {
12855        match self {
12856            Self::Document => BoardKind::Document,
12857            Self::Work(kind, _) => BoardKind::Work(*kind),
12858        }
12859    }
12860
12861    /// The status this write carries. A document carries none, so a write of one says
12862    /// nothing about the issue's open or closed state and selects no board `Status`
12863    /// option.
12864    const fn status(&self) -> Option<&Status> {
12865        match self {
12866            Self::Document => None,
12867            Self::Work(_, status) => Some(status),
12868        }
12869    }
12870
12871    /// The status this write carries with the kind whose half of `status_mapping` it is
12872    /// written through.
12873    const fn work_status(&self) -> Option<(ItemKind, &Status)> {
12874        match self {
12875            Self::Document => None,
12876            Self::Work(kind, status) => Some((*kind, status)),
12877        }
12878    }
12879}
12880
12881/// The item being written, in the one shape all three write methods reach.
12882struct Incoming<'a> {
12883    written: Written<'a>,
12884    /// The title a person wrote. A document's goes onto the issue with
12885    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
12886    title: &'a str,
12887    content: Option<&'a str>,
12888    assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
12889    labels: &'a [Label],
12890    metadata: &'a BTreeMap<String, Value>,
12891    repositories: &'a [Repository],
12892    /// Recorded in the slot while private, and nowhere while public.
12893    classification: Classification,
12894    parent: Option<&'a NativeId>,
12895    /// [`Task::delivers`], already checked. Empty for a project or a document, which is
12896    /// what keeps either key out of their slot.
12897    delivers: &'a [TaskRef],
12898    /// [`Task::delivered_by`], already checked. Empty for a project or a document.
12899    delivered_by: &'a [TaskRef],
12900    /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
12901    /// project, a document, and every write to an instance with no `priority_mapping` —
12902    /// which is what keeps such a write's requests exactly what they were before.
12903    priority: Option<Priority>,
12904}
12905
12906/// What one write does to one projected metadata text field, by the field's id.
12907#[derive(Debug, Clone)]
12908enum ProjectionWrite {
12909    /// Write this text.
12910    Set {
12911        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.
12912        text: String,
12913    },
12914    /// Clear the field's value.
12915    Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
12916}
12917
12918impl ProjectionWrite {
12919    /// Add this write to a field write's list of values and list of clears.
12920    fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
12921        match self {
12922            Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
12923            Self::Clear { field } => clears.push(field),
12924        }
12925    }
12926}
12927
12928/// What one write does to an item's `Priority` field.
12929enum PriorityWrite {
12930    /// Select this option of this field.
12931    Select {
12932        /// The `Priority` field's id.
12933        field: String,
12934        /// The mapped option's id.
12935        option: String,
12936    },
12937    /// Clear the field's value, which is what `none` is.
12938    Clear {
12939        /// The `Priority` field's id.
12940        field: String,
12941    },
12942}
12943
12944impl Incoming<'_> {
12945    /// The title this write puts on the issue.
12946    fn written_title(&self) -> String {
12947        match self.written {
12948            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
12949            Written::Work(..) => self.title.to_owned(),
12950        }
12951    }
12952}
12953
12954#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12955enum ContentKind {
12956    DraftIssue,
12957    Issue,
12958}
12959
12960/// What one board issue is: a document, or the work an [`ItemKind`] names.
12961///
12962/// A type of this source's own rather than an `ItemKind` with a third variant, because
12963/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
12964/// document — the contract keeps a document out of that enum deliberately. Holding the
12965/// board's three answers in one value is what makes every place that asks "which is this?"
12966/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
12967/// two thirds of the board.
12968#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12969enum BoardKind {
12970    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
12971    Document,
12972    /// Every other issue, and every draft.
12973    Work(ItemKind),
12974}
12975
12976impl BoardKind {
12977    /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
12978    /// sub-issues and carrying the kind marker `marked` in its slot.
12979    ///
12980    /// The design prefix is read *first*, before either of the two rules that separate a
12981    /// project from a task. A document is not work whatever sub-issues it has and whatever
12982    /// marker it carries, and reading the prefix later would make a design issue with none of
12983    /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
12984    /// overrides it: an issue filed under a project is that project's task even when it has
12985    /// sub-issues of its own.
12986    fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
12987        if title.starts_with(DESIGN_TITLE_PREFIX) {
12988            Self::Document
12989        } else if filed {
12990            Self::Work(ItemKind::Task)
12991        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
12992            Self::Work(ItemKind::Project)
12993        } else {
12994            Self::Work(ItemKind::Task)
12995        }
12996    }
12997
12998    /// What the change token's facets call this kind.
12999    const fn facet(self) -> change_token::FacetKind {
13000        match self {
13001            Self::Document => change_token::FacetKind::Document,
13002            Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
13003            Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
13004        }
13005    }
13006
13007    /// Whose half of `status_mapping` an item of this kind reads its status through. A
13008    /// document has no status of its own, so the task half stands in for whatever the issue
13009    /// holds; nothing reports it.
13010    const fn status_kind(self) -> ItemKind {
13011        match self {
13012            Self::Document => ItemKind::Task,
13013            Self::Work(kind) => kind,
13014        }
13015    }
13016
13017    /// How a refusal names this kind to the person reading it.
13018    const fn describes(self) -> &'static str {
13019        match self {
13020            Self::Document => "document",
13021            Self::Work(kind) => kind.marker(),
13022        }
13023    }
13024}
13025
13026/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
13027///
13028/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
13029/// the shared cross-source journeys assert one answer to one question, so two sources
13030/// that disagree about what "carries the label bug" means fail them.
13031fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
13032    let holds = |name: &String| {
13033        labels
13034            .iter()
13035            .any(|label| label.name.eq_ignore_ascii_case(name))
13036    };
13037    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
13038        && filter.all_of.iter().all(holds)
13039        && !filter.none_of.iter().any(holds)
13040}
13041
13042/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
13043/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
13044fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
13045    statuses.is_empty() || statuses.contains(&category)
13046}
13047
13048/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
13049///
13050/// `content` is the item's own prose — the body with this source's trailing metadata
13051/// comment already taken off — so a search never matches an encoding the author of the
13052/// issue never wrote.
13053fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
13054    let terms = query.terms.to_lowercase();
13055    let in_title = title.to_lowercase().contains(&terms);
13056    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
13057    match query.fields {
13058        TextFields::Title => in_title,
13059        TextFields::Content => in_content,
13060        TextFields::TitleOrContent => in_title || in_content,
13061    }
13062}
13063
13064/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
13065///
13066/// The project predicate is passed separately because a read narrowed to one project has
13067/// already answered it by asking *that project* for its own items — and re-applying it
13068/// there would compare the caller's selector, which may be a project's **name**, against
13069/// the id of the project that name resolved to, and keep nothing. Every other read passes
13070/// `query.project` and applies it here, which is what keeps `projects` a predicate this
13071/// source really does apply.
13072fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
13073    labels_match(&task.labels, &query.labels)
13074        && status_matches(task.status.category, &query.statuses)
13075        && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
13076        && match project {
13077            ProjectFilter::Any => true,
13078            ProjectFilter::Orphans => task.project.is_none(),
13079            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
13080        }
13081        && query
13082            .text
13083            .as_ref()
13084            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
13085        // Against the parsed metadata slot, and against the origin field, which is where
13086        // `Resolved::metadata` reads each of them from.
13087        && query.metadata_matches(&task.metadata)
13088        && query.origin_matches(&task.metadata)
13089}
13090
13091fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
13092    labels_match(&project.labels, &query.labels)
13093        && status_matches(project.status.category, &query.statuses)
13094        && query
13095            .text
13096            .as_ref()
13097            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
13098}
13099
13100/// The same three predicates a task query carries, minus the status filter.
13101///
13102/// A document is not work, so it has no status for one to compare against and the query
13103/// type carries none. The project predicate is the same one — a design issue filed under a
13104/// project issue is in that project, and one filed under nothing is in none — so it is
13105/// spelled the same way here rather than answered differently.
13106fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
13107    labels_match(&document.labels, &query.labels)
13108        && match project {
13109            ProjectFilter::Any => true,
13110            ProjectFilter::Orphans => document.project.is_none(),
13111            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
13112        }
13113        && query
13114            .text
13115            .as_ref()
13116            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
13117}
13118
13119/// Why a GitHub item's change token does not vouch for its forward edges.
13120///
13121/// A forward edge names its far end and that far end's kind, and the near item's token moves
13122/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
13123/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
13124/// nothing the near token reads, so an edge kept at the near version could name a far end that
13125/// is no longer what it said.
13126const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
13127     whether it became a document, or its deletion, so its forward edges are always read from \
13128     github";
13129
13130/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
13131/// categories an open issue reads as narrows a board-scoped search with.
13132const OPEN_QUALIFIER: &str = "is:open";
13133
13134/// The board filter `task status reconcile` reads the board through: closed items alone.
13135const CLOSED_FILTER: &str = "is:closed";
13136
13137/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
13138/// one reference, and a slot the batch has none for names its last one again.
13139fn reference_batch(batch: &[IssueRef]) -> Value {
13140    let mut variables = serde_json::Map::new();
13141    for slot in 0..REFERENCE_BATCH {
13142        let issue = batch.get(slot).or(batch.last());
13143        variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
13144        variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
13145        variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
13146    }
13147    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
13148    variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
13149    variables.insert("duplicates".to_owned(), json!(true));
13150    Value::Object(variables)
13151}
13152
13153/// One issue an issue read by reference answered, in the shape every other read of an issue
13154/// hands its resolver: each board membership's `Status` value, read by name, as the one field
13155/// value that membership carries.
13156///
13157/// A membership's `status` is `null` when its card sits at no option — or on a board with no
13158/// field of that name — and otherwise an option value naming its option; anything else is not
13159/// an answer to this read, and is refused rather than read as a card at no option.
13160fn held_issue(issue: &Value) -> Result<Value, SourceError> {
13161    let malformed = |what: &str| SourceError::Malformed {
13162        message: format!("GitHub answered an issue read by reference with {what}"),
13163    };
13164    let mut issue = issue.clone();
13165    let nodes = issue
13166        .pointer_mut("/projectItems/nodes")
13167        .and_then(Value::as_array_mut)
13168        .ok_or_else(|| malformed("no list of board memberships"))?;
13169    for node in nodes {
13170        let node = node
13171            .as_object_mut()
13172            .ok_or_else(|| malformed("a board membership that is not an object"))?;
13173        let status = match node.get("status") {
13174            None => return Err(malformed("a board membership with no status member")),
13175            Some(Value::Null) => None,
13176            Some(status) if status.get("name").is_some_and(Value::is_string) => {
13177                Some(status.clone())
13178            }
13179            // A single-select value of another field type answers `{}`; only an option value
13180            // carries a name.
13181            Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
13182            Some(_) => return Err(malformed("a membership status that names no option")),
13183        };
13184        node.insert(
13185            "fieldValues".to_owned(),
13186            json!({"nodes": status.into_iter().collect::<Vec<_>>(),
13187                   "pageInfo": {"hasNextPage": false}}),
13188        );
13189    }
13190    Ok(issue)
13191}
13192
13193/// The issue one slot of a read by reference answered, `None` when GitHub answered that
13194/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
13195/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
13196/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
13197/// than a reference nothing holds.
13198fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
13199    let alias = format!("r{slot}");
13200    let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
13201        message: format!("GitHub answered a read by reference with no {alias}"),
13202    })?;
13203    if repository.is_null() {
13204        return Ok(None);
13205    }
13206    let issue = repository
13207        .get("issue")
13208        .ok_or_else(|| SourceError::Malformed {
13209            message: format!("GitHub answered {alias} of a read by reference with no issue"),
13210        })?;
13211    if issue.is_null() {
13212        return Ok(None);
13213    }
13214    // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
13215    if optional_str(issue, "__typename")? != Some("Issue") {
13216        return Err(SourceError::Malformed {
13217            message: format!(
13218                "GitHub answered {alias} of a read by reference with an issue whose __typename \
13219                 is not Issue"
13220            ),
13221        });
13222    }
13223    Ok(Some(issue))
13224}
13225
13226/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
13227fn spoken_reason(reason: &str) -> String {
13228    reason.to_ascii_lowercase().replace('_', " ")
13229}
13230
13231impl GitHubProjectsSource {
13232    /// Every task and project of this board whose card contradicts its issue's closure, each
13233    /// judged against its own kind's half of `status_mapping`, read through the board's own
13234    /// `is:closed` filter, page by page — never the board's unfiltered `items`.
13235    ///
13236    /// Each item the filter answers is confirmed from its own node: an open one, one whose
13237    /// closure reads `unknown`, and one whose card is already in line are left out whatever
13238    /// the filter let through. The board's fields ride along and are held for the option
13239    /// writes that follow, so those need no read of the board.
13240    async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
13241        let mut after: Option<String> = None;
13242        let mut found = Vec::new();
13243        loop {
13244            let data = self
13245                .graphql(
13246                    graphql::CLOSED_ITEMS,
13247                    json!({"owner":self.owner,"number":self.project_number,
13248                           "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
13249                           "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
13250                )
13251                .await?;
13252            let board = data
13253                .pointer("/closedItems/projectV2")
13254                .filter(|value| !value.is_null())
13255                .ok_or_else(|| SourceError::Refused {
13256                    message: format!(
13257                        "GitHub project {}/{} was not found or is not visible to the token",
13258                        self.owner, self.project_number
13259                    ),
13260                })?;
13261            *self.fields_cache()? = Some(BoardFields {
13262                id: BoardId::parse(required_str(board, "id")?)?,
13263                fields: board.get("fields").cloned().unwrap_or(Value::Null),
13264            });
13265            let items = board.get("items").ok_or_else(|| SourceError::Malformed {
13266                message: "GitHub project closed items are missing".into(),
13267            })?;
13268            for item in items
13269                .get("nodes")
13270                .and_then(Value::as_array)
13271                .ok_or_else(|| SourceError::Malformed {
13272                    message: "GitHub project items.nodes is not an array".into(),
13273                })?
13274            {
13275                let Some(resolved) = self.resolve(item)? else {
13276                    continue;
13277                };
13278                let BoardKind::Work(kind) = resolved.kind else {
13279                    continue;
13280                };
13281                let Some((closed_as, name)) = self.statuses.contradicted(
13282                    kind,
13283                    resolved.option.as_deref(),
13284                    resolved.closed,
13285                    resolved.reason.as_deref(),
13286                ) else {
13287                    continue;
13288                };
13289                found.push(StatusContradiction {
13290                    closed_as,
13291                    to: name.to_owned(),
13292                    option: resolved.option.clone(),
13293                    item: match kind {
13294                        ItemKind::Task => ContradictedItem::Task(resolved.task()?),
13295                        ItemKind::Project => ContradictedItem::Project(resolved.project()),
13296                    },
13297                });
13298            }
13299            let info = items
13300                .get("pageInfo")
13301                .ok_or_else(|| SourceError::Malformed {
13302                    message: "GitHub project items have no pageInfo".into(),
13303                })?;
13304            if !required_bool(info, "hasNextPage")? {
13305                return Ok(found);
13306            }
13307            let next = required_str(info, "endCursor")?;
13308            validate_cursor_progress(after.as_deref(), next)?;
13309            after = Some(next.to_owned());
13310        }
13311    }
13312
13313    /// What this board holds for each issue in `issues`, once each in the order given, read
13314    /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
13315    ///
13316    /// An issue GitHub cannot find for this source's credential, and one with no item on this
13317    /// board, are both absent. A closure that already says the work will not be done here —
13318    /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
13319    /// completed whose card sits at its kind's `done` option is unchanged, and every other item
13320    /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
13321    /// tasks as sub-issues — is held under exactly these rules, judged against the project
13322    /// kind's half of `status_mapping`.
13323    async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
13324        let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
13325        for issue in issues {
13326            if !distinct.contains(issue) {
13327                distinct.push(issue.clone());
13328            }
13329        }
13330        let done = |kind: ItemKind| {
13331            self.statuses
13332                .target(kind, StatusCategory::Done)
13333                .option()
13334                .unwrap_or("Done")
13335                .to_owned()
13336        };
13337        // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
13338        // its range names no issue any repository can hold: it is answered absent and never sent,
13339        // where binding it would have GitHub refuse the whole batch.
13340        let readable: Vec<IssueRef> = distinct
13341            .iter()
13342            .filter(|issue| i32::try_from(issue.number).is_ok())
13343            .cloned()
13344            .collect();
13345        let mut holdings = Vec::with_capacity(readable.len());
13346        for batch in readable.chunks(REFERENCE_BATCH) {
13347            let data = self
13348                .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
13349                .await?;
13350            for (slot, issue) in batch.iter().enumerate() {
13351                let node = referenced_issue(&data, slot)?;
13352                let resolved = match node {
13353                    Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
13354                    None => None,
13355                };
13356                // A task and a project are both held; a document is not work, so nothing is.
13357                let Some((item, kind)) = resolved.and_then(|item| match item.kind {
13358                    BoardKind::Work(kind) => Some((item, kind)),
13359                    BoardKind::Document => None,
13360                }) else {
13361                    holdings.push(IssueHolding {
13362                        issue: issue.clone(),
13363                        completion: Completion::Absent,
13364                    });
13365                    continue;
13366                };
13367                let task = match kind {
13368                    ItemKind::Task => {
13369                        let held = item.task()?;
13370                        HeldTask {
13371                            id: held.id,
13372                            kind,
13373                            option: item.option.clone(),
13374                            classification: held.classification,
13375                            repositories: held.repositories,
13376                            delivers: held.delivers,
13377                        }
13378                    }
13379                    // A project delivers no task, so what its write keeps in step is nothing.
13380                    ItemKind::Project => {
13381                        let held = item.project();
13382                        HeldTask {
13383                            id: held.id,
13384                            kind,
13385                            option: item.option.clone(),
13386                            classification: held.classification,
13387                            repositories: held.repositories,
13388                            delivers: Vec::new(),
13389                        }
13390                    }
13391                };
13392                let done = done(kind);
13393                let completion = match (item.closed, item.reason.as_deref()) {
13394                    (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
13395                        task,
13396                        reason: format!("closed as {}", spoken_reason(reason)),
13397                    },
13398                    (true, None | Some("COMPLETED"))
13399                        if item
13400                            .option
13401                            .as_deref()
13402                            .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
13403                    {
13404                        Completion::Unchanged { task }
13405                    }
13406                    _ => {
13407                        let kept = item.clone();
13408                        self.held_cache
13409                            .lock()
13410                            .map_err(|_| SourceError::Unavailable {
13411                                message: "items read by issue reference were left \
13412                                          inconsistent; run the command again"
13413                                    .into(),
13414                            })?
13415                            .insert(kept.id.clone(), kept);
13416                        Completion::Due {
13417                            task,
13418                            to: done.clone(),
13419                        }
13420                    }
13421                };
13422                holdings.push(IssueHolding {
13423                    issue: issue.clone(),
13424                    completion,
13425                });
13426            }
13427        }
13428        let mut read = holdings.into_iter();
13429        let mut answered = Vec::with_capacity(distinct.len());
13430        for issue in distinct {
13431            if i32::try_from(issue.number).is_ok() {
13432                answered.push(read.next().ok_or_else(|| SourceError::Malformed {
13433                    message: format!("no holding was read for {issue}"),
13434                })?);
13435            } else {
13436                answered.push(IssueHolding {
13437                    issue,
13438                    completion: Completion::Absent,
13439                });
13440            }
13441        }
13442        Ok(answered)
13443    }
13444}
13445
13446#[async_trait::async_trait]
13447impl TaskSource for GitHubProjectsSource {
13448    fn kind(&self) -> &'static str {
13449        KIND
13450    }
13451    /// GitHub's issue search, which narrows a read of the comments made since an instant, is
13452    /// an index behind the writes it indexes — the credentialed lane has watched it miss a newly
13453    /// commented issue for thirty seconds — so a watermark is set back by
13454    /// [`COMMENT_SEARCH_LAG`], four times that.
13455    fn comment_consistency_window(&self) -> std::time::Duration {
13456        COMMENT_SEARCH_LAG
13457    }
13458    /// The `Date` of the last answer GitHub gave this source.
13459    fn served_at(&self) -> Option<DateTime<Utc>> {
13460        *self
13461            .last_served
13462            .lock()
13463            .unwrap_or_else(std::sync::PoisonError::into_inner)
13464    }
13465    fn attach_body_markup(&self, markup: onetaskgraph_plugin_api::SharedBodyMarkup) {
13466        let _ = self.body_markup.set(markup);
13467    }
13468    fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
13469        change_token::order(a, b)
13470    }
13471    /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
13472    /// member, the change each part of the token moves for. Not its forward edges — see
13473    /// [`FAR_ENDS_UNCOVERED`].
13474    fn version_coverage(&self) -> VersionCoverage {
13475        VersionCoverage {
13476            forward_edges: Coverage::Uncovered {
13477                reason: FAR_ENDS_UNCOVERED.to_owned(),
13478            },
13479            ..VersionCoverage::whole()
13480        }
13481    }
13482    /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
13483    /// and for a far end deleted, so the token sees every change to a task's edges but one — a
13484    /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
13485    /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
13486    /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
13487    // 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.
13488    fn copy_version_coverage(&self) -> VersionCoverage {
13489        VersionCoverage::whole()
13490    }
13491    /// A project's fields, and not its forward edges, for the same reason a task's are not.
13492    fn project_version_coverage(&self) -> ProjectVersionCoverage {
13493        ProjectVersionCoverage {
13494            fields: Coverage::Covered,
13495            forward_edges: Coverage::Uncovered {
13496                reason: FAR_ENDS_UNCOVERED.to_owned(),
13497            },
13498        }
13499    }
13500    /// A document's fields and the manifest of its image assets, which its metadata slot
13501    /// records: both are the issue's body, whose every edit moves the token's issue time.
13502    fn document_version_coverage(&self) -> DocumentVersionCoverage {
13503        DocumentVersionCoverage::whole()
13504    }
13505    fn write_context(&self, id: &NativeId) -> Option<Value> {
13506        self.context_of(id)
13507    }
13508    fn board_context(&self) -> Option<Value> {
13509        self.board_context_held()
13510    }
13511    fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
13512        self.prime(id, context, board)
13513    }
13514    fn unprime(&self, id: &NativeId) {
13515        self.unprime_held(id);
13516    }
13517    fn placement_context(&self) -> Option<Value> {
13518        self.placement_held()
13519    }
13520    fn prime_placement(&self, context: &Value) -> bool {
13521        self.prime_placement_held(context)
13522    }
13523    fn written_context(&self, id: &NativeId) -> Option<Value> {
13524        self.written_held(id)
13525    }
13526    fn prime_written(&self, id: &NativeId, context: &Value, task: &Task) -> bool {
13527        self.prime_written_held(id, context, task)
13528    }
13529    fn begin_journal(&self) {
13530        self.expected.begin();
13531    }
13532    fn end_journal(&self) -> Option<WriteJournal> {
13533        Some(self.expected.end())
13534    }
13535    fn capabilities(&self) -> Capabilities {
13536        Capabilities {
13537            projects: Support::Native,
13538            documents: Support::Native,
13539            comments: Support::Native,
13540            assets: Support::Native,
13541            priority: if self.priorities.is_some() {
13542                Support::Native
13543            } else {
13544                Support::Unsupported
13545            },
13546            filter_by_priority: Support::Native,
13547            filter_by_comment_activity: Support::Native,
13548            filter_by_metadata: Support::Native,
13549            filter_by_origin: Support::Native,
13550            reconcile_status: Support::Native,
13551            complete_by_issue: Support::Native,
13552            orphan_tasks: Support::Native,
13553            filter_by_label: Support::Native,
13554            filter_by_status: Support::Native,
13555            search_title: Support::Native,
13556            search_content: Support::Native,
13557            task_dependencies: DependencySupport::BothDirections,
13558            project_dependencies: DependencySupport::BothDirections,
13559            max_page_size: MAX_PAGE_SIZE,
13560        }
13561    }
13562    async fn visibility(
13563        &self,
13564        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13565    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13566        self.write_visibility(target).await
13567    }
13568    async fn visibility_with_project(
13569        &self,
13570        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13571        project: &onetaskgraph_plugin_api::WriteTarget<'_>,
13572    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13573        self.write_visibility_with_project(target, project).await
13574    }
13575    fn declare_visibility(&self, declared: onetaskgraph_plugin_api::Visibility) {
13576        self.declared_private.store(
13577            declared == onetaskgraph_plugin_api::Visibility::Private,
13578            std::sync::atomic::Ordering::SeqCst,
13579        );
13580    }
13581    fn scope_visibility<'a>(
13582        &'a self,
13583        operation: std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>>,
13584    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>> {
13585        self.scope_visibility_reads(operation)
13586    }
13587
13588    /// Where a create of `target` would file its issue, by [`Self::creation_rule`] — the rule
13589    /// the create applies — reading the parent project's issue only when `target` names one.
13590    async fn creation_repository(
13591        &self,
13592        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13593    ) -> Result<Option<Repository>, SourceError> {
13594        let onetaskgraph_plugin_api::WriteTarget::New {
13595            repositories,
13596            project,
13597        } = target
13598        else {
13599            return Ok(None);
13600        };
13601        let target = self
13602            .creation_rule(repositories, *project, "a new item")
13603            .await?;
13604        Repository::try_from(target.origin())
13605            .map(Some)
13606            .map_err(|message| SourceError::Malformed { message })
13607    }
13608    /// Every issue of the repository a create of `target` files in whose body carries `key` in
13609    /// its metadata slot, read off that repository's issues newest first back to `since` —
13610    /// never through search, whose index lags a fresh issue by seconds.
13611    async fn created_with_key(
13612        &self,
13613        record: onetaskgraph_plugin_api::MetadataRecord,
13614        key: &str,
13615        since: Option<DateTime<Utc>>,
13616        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13617    ) -> Result<Vec<NativeId>, SourceError> {
13618        let _ = record;
13619        let onetaskgraph_plugin_api::WriteTarget::New {
13620            repositories,
13621            project,
13622        } = target
13623        else {
13624            return Ok(Vec::new());
13625        };
13626        let repository = self
13627            .creation_rule(repositories, *project, "a new item")
13628            .await?;
13629        let wanted = Value::String(key.to_owned());
13630        let mut found = Vec::new();
13631        let mut after: Option<String> = None;
13632        let mut followed = std::collections::HashSet::new();
13633        loop {
13634            let data = self
13635                .graphql(
13636                    graphql::ISSUES_BY_CREATION,
13637                    json!({"owner": repository.owner, "name": repository.name,
13638                           "first": MAX_PAGE_SIZE, "after": after}),
13639                )
13640                .await?;
13641            let issues =
13642                data.pointer("/repository/issues")
13643                    .ok_or_else(|| SourceError::Malformed {
13644                        message: format!(
13645                            "GitHub answered no issues for repository {}",
13646                            repository.slug()
13647                        ),
13648                    })?;
13649            let malformed = |what: &str| SourceError::Malformed {
13650                message: format!(
13651                    "GitHub answered a page of repository {}'s issues {what}",
13652                    repository.slug()
13653                ),
13654            };
13655            let nodes = issues
13656                .get("nodes")
13657                .and_then(Value::as_array)
13658                .ok_or_else(|| malformed("with no list of issues"))?;
13659            let mut older = false;
13660            for node in nodes {
13661                let id = node
13662                    .get("id")
13663                    .and_then(Value::as_str)
13664                    .filter(|id| !id.is_empty())
13665                    .ok_or_else(|| malformed("holding an issue with no id"))?;
13666                let created = optional_time(node, "createdAt")?
13667                    .ok_or_else(|| malformed("holding an issue with no creation time"))?;
13668                if since.is_some_and(|since| created < since) {
13669                    older = true;
13670                    break;
13671                }
13672                let body = match node.get("body") {
13673                    Some(Value::String(body)) => Some(body.clone()),
13674                    Some(Value::Null) => None,
13675                    _ => return Err(malformed("holding an issue with no body member")),
13676                };
13677                let (_, metadata) = metadata_body(body)?;
13678                if metadata.get(MetadataKey::CREATE_KEY) == Some(&wanted) {
13679                    found.push(NativeId(id.to_owned()));
13680                }
13681            }
13682            let next = issues
13683                .pointer("/pageInfo/hasNextPage")
13684                .and_then(Value::as_bool)
13685                .ok_or_else(|| malformed("that does not say whether another follows"))?;
13686            if older || !next {
13687                return Ok(found);
13688            }
13689            // A page that says another follows names where it starts, and never a page already
13690            // read: anything else would ask for the same pages again for ever.
13691            let cursor = issues
13692                .pointer("/pageInfo/endCursor")
13693                .and_then(Value::as_str)
13694                .filter(|cursor| !cursor.is_empty())
13695                .map(str::to_owned);
13696            if cursor
13697                .as_ref()
13698                .is_none_or(|cursor| !followed.insert(cursor.clone()))
13699            {
13700                return Err(SourceError::Malformed {
13701                    message: format!(
13702                        "GitHub answered a page of repository {}'s issues that says another \
13703                         follows without a new cursor to it",
13704                        repository.slug()
13705                    ),
13706                });
13707            }
13708            after = cursor;
13709        }
13710    }
13711
13712    /// `Repository.issues` lists an issue GitHub committed only once its listing catches up.
13713    fn create_key_lags(&self) -> bool {
13714        true
13715    }
13716
13717    /// The task as this process's own latest write of it left it, from the item it recorded
13718    /// that write as — never a request. Its `version` is always `None`: a board's change token
13719    /// is given only once an item has gone unchanged for [`change_token::SETTLED`], which an
13720    /// item this command has just written has not, so no write's own answer can vouch for one.
13721    async fn written_task(&self, id: &NativeId) -> Option<Task> {
13722        let held = self
13723            .created()
13724            .ok()?
13725            .iter()
13726            .find(|held| held.id == *id)
13727            .cloned()
13728            .or_else(|| {
13729                self.updated()
13730                    .ok()?
13731                    .iter()
13732                    .find(|held| held.id == *id)
13733                    .cloned()
13734            })?;
13735        let mut task = held.task().ok()?;
13736        task.version = None;
13737        Some(task)
13738    }
13739
13740    /// Why `creation_owners` refuses a create of `target`, by [`Self::creation_rule`] — read
13741    /// only when the list is not empty, so a source restricting nothing reads nothing.
13742    async fn creation_refusal(
13743        &self,
13744        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13745    ) -> Result<Option<String>, SourceError> {
13746        let onetaskgraph_plugin_api::WriteTarget::New {
13747            repositories,
13748            project,
13749        } = target
13750        else {
13751            return Ok(None);
13752        };
13753        if self.creation_owners.is_empty() {
13754            return Ok(None);
13755        }
13756        let target = self
13757            .creation_rule(repositories, *project, "a new item")
13758            .await?;
13759        Ok(self.owner_refusal(&target))
13760    }
13761
13762    async fn health(&self) -> Result<Health, SourceError> {
13763        let board = self.board_page(None, 1).await?;
13764        Ok(Health {
13765            reachable: true,
13766            detail: Some(format!(
13767                "reading GitHub project {}/{} ({})",
13768                self.owner,
13769                self.project_number,
13770                required_str(&board, "title")?
13771            )),
13772        })
13773    }
13774    /// The board's own node id, digested: the one request reading its fields, or none when
13775    /// this command has read them already.
13776    async fn designator(&self) -> Result<Option<Designator>, SourceError> {
13777        let board = self.board_fields().await?;
13778        Designator::of_backend(KIND, board.id.as_str())
13779            .map(Some)
13780            .map_err(|message| SourceError::Malformed { message })
13781    }
13782    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
13783        self.item_by_id(id)
13784            .await?
13785            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13786            .map(|item| item.task())
13787            .transpose()
13788    }
13789    async fn task_assets(
13790        &self,
13791        id: &NativeId,
13792    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13793        self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
13794    }
13795    async fn task_asset(
13796        &self,
13797        id: &NativeId,
13798        name: &onetaskgraph_plugin_api::AssetName,
13799    ) -> Result<Option<Vec<u8>>, SourceError> {
13800        self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
13801            .await
13802    }
13803    async fn set_task_rendering_with_assets(
13804        &self,
13805        id: &NativeId,
13806        content: &str,
13807        provenance: &Value,
13808        _answers: &BTreeMap<String, Value>,
13809        assets: &onetaskgraph_plugin_api::AssetWrite,
13810    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13811        self.replace_rendering(
13812            id,
13813            BoardKind::Work(ItemKind::Task),
13814            content,
13815            provenance,
13816            Some(assets),
13817        )
13818        .await
13819    }
13820    async fn document_assets(
13821        &self,
13822        id: &NativeId,
13823    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13824        self.held_assets(id, BoardKind::Document).await
13825    }
13826    async fn document_asset(
13827        &self,
13828        id: &NativeId,
13829        name: &onetaskgraph_plugin_api::AssetName,
13830    ) -> Result<Option<Vec<u8>>, SourceError> {
13831        self.held_asset(id, BoardKind::Document, name).await
13832    }
13833    async fn set_document_rendering_with_assets(
13834        &self,
13835        id: &NativeId,
13836        content: &str,
13837        provenance: &Value,
13838        _answers: &BTreeMap<String, Value>,
13839        assets: &onetaskgraph_plugin_api::AssetWrite,
13840    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13841        self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
13842            .await
13843    }
13844    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
13845        Ok(self
13846            .item_by_id(id)
13847            .await?
13848            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13849            .map(|item| item.project()))
13850    }
13851    async fn query_tasks(
13852        &self,
13853        query: &TaskQuery,
13854        page: &PageRequest,
13855    ) -> Result<Page<Task>, SourceError> {
13856        validate_page(page)?;
13857        refuse_unsearchable(query)?;
13858        // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
13859        // category — `unknown`; every other category reads only off an open issue or a draft. So
13860        // a status filter naming none of those three asks every issue search it sends for open
13861        // issues alone; each candidate's category is still confirmed from its own item below.
13862        let open_only = !query.statuses.is_empty()
13863            && query.statuses.iter().all(|category| {
13864                !matches!(
13865                    category,
13866                    StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
13867                )
13868            });
13869        if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
13870            let mut qualifiers: Vec<String> = [
13871                query.commented_since.map(updated_qualifier),
13872                narrowing_qualifiers(query),
13873            ]
13874            .into_iter()
13875            .flatten()
13876            .collect();
13877            // A narrowed search never answers a draft, which is no issue, so narrowing it to
13878            // open issues too leaves out nothing such a search could have answered.
13879            if !qualifiers.is_empty() {
13880                if open_only {
13881                    qualifiers.push(OPEN_QUALIFIER.to_owned());
13882                }
13883                return self.search_tasks(query, page, &qualifiers.join(" ")).await;
13884            }
13885        }
13886
13887        // A read narrowed to one project asks that project for its own tasks, so nothing
13888        // about it costs what the rest of the board holds. A read carrying a text, metadata
13889        // or origin predicate asks GitHub the narrower question those predicates are, and a
13890        // read narrowed to comment activity alone asks the board's own issue search for the
13891        // issues updated since, which is every issue a comment could have been written or
13892        // edited on since. Every other task read is a question about the whole board and is
13893        // answered by reading it.
13894        let (held, membership) = match (&query.project, query.commented_since) {
13895            (ProjectFilter::Is(project), _) => (
13896                match self.held_task_children(project)? {
13897                    // Taken as `project_children` takes the project's own sub-issue read: with
13898                    // every item this process wrote under it since, and none it moved away.
13899                    Some(held) => self.completed_with_written(
13900                        held.into_iter()
13901                            .filter(|child| child.parent.as_ref() == Some(project))
13902                            .collect(),
13903                        |own| own.parent.as_ref() == Some(project),
13904                    )?,
13905                    None => self.project_children(project).await?,
13906                },
13907                // Answered by where these items came from; see `task_matches`.
13908                &ProjectFilter::Any,
13909            ),
13910            (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
13911                match (self.narrowed(query).await?, since) {
13912                    (Some(narrowed), _) => (narrowed, &query.project),
13913                    (None, Some(since)) => (self.updated_since(since).await?, &query.project),
13914                    // The board's own items list every draft; only the search half, which
13915                    // completes them with issues they lag behind on, is narrowed — a closed
13916                    // issue that half leaves out is at no category the filter keeps.
13917                    (None, None) if open_only => (
13918                        self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
13919                        &query.project,
13920                    ),
13921                    (None, None) => (self.board().await?.items, &query.project),
13922                }
13923            }
13924        };
13925        // Filtered before paged: a page of a filtered result is a page of the survivors,
13926        // never the survivors of a page.
13927        let mut tasks = Vec::new();
13928        for item in held
13929            .iter()
13930            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13931        {
13932            let task = item.task()?;
13933            if task_matches(&task, query, membership)
13934                && self.commented_since(item, query.commented_since).await?
13935            {
13936                tasks.push(task);
13937            }
13938        }
13939        Ok(offset_page(
13940            tasks,
13941            numeric_cursor(page.cursor.as_ref())?,
13942            page.limit.min(MAX_PAGE_SIZE) as usize,
13943        ))
13944    }
13945    /// Read board pages directly, at the board connection's consistency. Unlike
13946    /// `query_tasks`, this does not complete a lagging board with issue search: a complete
13947    /// sweep needs one request per listing page, including the first comments.
13948    async fn query_task_details(
13949        &self,
13950        query: &TaskQuery,
13951        page: &PageRequest,
13952        comments: Option<&PageRequest>,
13953    ) -> Result<Page<TaskDetailRead>, SourceError> {
13954        let Some(comments) = comments else {
13955            let tasks = self.query_tasks(query, page).await?;
13956            return Ok(Page {
13957                items: tasks
13958                    .items
13959                    .into_iter()
13960                    .map(|task| TaskDetailRead {
13961                        task,
13962                        comments: None,
13963                    })
13964                    .collect(),
13965                next: tasks.next,
13966            });
13967        };
13968        validate_page(page)?;
13969        validate_page(comments)?;
13970        refuse_unsearchable(query)?;
13971        if comments.cursor.is_some() || query.commented_since.is_some() {
13972            return Err(SourceError::Refused {
13973                message: "a board listing with comments requires a first comment page and cannot filter by comment activity".into(),
13974            });
13975        }
13976        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
13977        let data = self
13978            .graphql(
13979                graphql::BOARD_WITH_COMMENTS,
13980                json!({
13981                    "owner":self.owner,"number":self.project_number,
13982                    "first":page.limit.min(MAX_PAGE_SIZE),"after":after,
13983                    "commentFirst":comments.limit.min(MAX_PAGE_SIZE),
13984                    "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true,
13985                }),
13986            )
13987            .await?;
13988        let connection = data
13989            .pointer("/owner/projectV2/items")
13990            .filter(|value| !value.is_null())
13991            .ok_or_else(|| SourceError::Refused {
13992                message: "GitHub project was not found or is not visible to the token".into(),
13993            })?;
13994        let mut items = Vec::new();
13995        for node in optional_nodes(Some(connection), "board items")?
13996            .into_iter()
13997            .flatten()
13998        {
13999            let Some(item) = self.resolve(node)? else {
14000                continue;
14001            };
14002            if item.kind != BoardKind::Work(ItemKind::Task) {
14003                continue;
14004            }
14005            let task = item.task()?;
14006            if !task_matches(&task, query, &query.project) {
14007                continue;
14008            }
14009            let read = if item.content_kind == ContentKind::DraftIssue {
14010                Err(self.draft_has_no_comments(&task.id))
14011            } else {
14012                self.expected.learn_comments(&task.id, &node["content"]);
14013                comment_page(&node["content"], &task.id.0, None).map(Some)
14014            };
14015            items.push(TaskDetailRead {
14016                task,
14017                comments: Some(read),
14018            });
14019        }
14020        let next = next_cursor(connection)?;
14021        if let Some(next) = &next {
14022            validate_cursor_progress(after, &next.0)?;
14023        }
14024        Ok(Page { items, next })
14025    }
14026
14027    async fn query_projects(
14028        &self,
14029        query: &ProjectQuery,
14030        page: &PageRequest,
14031    ) -> Result<Page<Project>, SourceError> {
14032        validate_page(page)?;
14033        refuse_unsearchable_text(query.text.as_ref())?;
14034        // The projects a board holds are found by an issue search scoped to that board,
14035        // never by walking the board's own item connection: what tells a project from a
14036        // task is the `parent` each issue carries, which costs nothing to read. A query
14037        // carrying a text asks that search for the text too, so it reads the issues that
14038        // hold it rather than every issue of the board.
14039        let held = match self.text_searched(query.text.as_ref()).await? {
14040            Some(searched) => searched,
14041            None => self.board_issues().await?,
14042        };
14043        let projects = held
14044            .iter()
14045            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
14046            .map(Resolved::project)
14047            .filter(|project| project_matches(project, query))
14048            .collect();
14049        Ok(offset_page(
14050            projects,
14051            numeric_cursor(page.cursor.as_ref())?,
14052            page.limit.min(MAX_PAGE_SIZE) as usize,
14053        ))
14054    }
14055    async fn query_projects_by_origin(
14056        &self,
14057        origin: &str,
14058        page: &PageRequest,
14059    ) -> Result<Page<Project>, SourceError> {
14060        validate_page(page)?;
14061        let projects = self
14062            .copied_from(origin, BoardKind::Work(ItemKind::Project))
14063            .await?
14064            .iter()
14065            .map(Resolved::project)
14066            .collect();
14067        Ok(offset_page(
14068            projects,
14069            numeric_cursor(page.cursor.as_ref())?,
14070            page.limit.min(MAX_PAGE_SIZE) as usize,
14071        ))
14072    }
14073    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
14074        let Some(item) = self
14075            .document_by_id(id)
14076            .await?
14077            .filter(|item| item.kind == BoardKind::Document)
14078        else {
14079            return Ok(None);
14080        };
14081        self.expected.read_document(&item);
14082        Ok(Some(item.document()))
14083    }
14084    async fn query_documents_by_origin(
14085        &self,
14086        origin: &str,
14087        page: &PageRequest,
14088    ) -> Result<Page<Document>, SourceError> {
14089        validate_page(page)?;
14090        let documents = self
14091            .copied_from(origin, BoardKind::Document)
14092            .await?
14093            .iter()
14094            .map(Resolved::document)
14095            .collect();
14096        Ok(offset_page(
14097            documents,
14098            numeric_cursor(page.cursor.as_ref())?,
14099            page.limit.min(MAX_PAGE_SIZE) as usize,
14100        ))
14101    }
14102    async fn query_documents(
14103        &self,
14104        query: &DocumentQuery,
14105        page: &PageRequest,
14106    ) -> Result<Page<Document>, SourceError> {
14107        validate_page(page)?;
14108        // Narrowed to one project, this is the same sub-issue read a task list scoped to
14109        // that project makes — a document filed under a project is a sub-issue of it too,
14110        // and which of them come back is the kind this caller asked for. Unscoped, a query
14111        // carrying a text asks the board-scoped issue search for it, as a task query does,
14112        // and only one carrying none reads the board.
14113        let (held, membership) = match &query.project {
14114            ProjectFilter::Is(project) => (
14115                self.project_children(project).await?,
14116                // Answered by where these items came from; see `task_matches`.
14117                &ProjectFilter::Any,
14118            ),
14119            ProjectFilter::Any | ProjectFilter::Orphans => {
14120                refuse_unsearchable_text(query.text.as_ref())?;
14121                match self.text_searched(query.text.as_ref()).await? {
14122                    Some(searched) => (searched, &query.project),
14123                    None => (self.board().await?.items, &query.project),
14124                }
14125            }
14126        };
14127        // Filtered before paged, exactly as a task read is: a page of a filtered result is
14128        // a page of the survivors, never the survivors of a page.
14129        let documents = held
14130            .iter()
14131            .filter(|item| item.kind == BoardKind::Document)
14132            .map(Resolved::document)
14133            .filter(|document| document_matches(document, query, membership))
14134            .collect();
14135        Ok(offset_page(
14136            documents,
14137            numeric_cursor(page.cursor.as_ref())?,
14138            page.limit.min(MAX_PAGE_SIZE) as usize,
14139        ))
14140    }
14141    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
14142        validate_page(page)?;
14143        let offset = numeric_cursor(page.cursor.as_ref())?;
14144        let mut labels = self
14145            .board()
14146            .await?
14147            .items
14148            .into_iter()
14149            .flat_map(|item| item.labels)
14150            .fold(Vec::new(), |mut all, label| {
14151                if !all.iter().any(|x: &Label| x.id == label.id) {
14152                    all.push(label);
14153                }
14154                all
14155            });
14156        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
14157        Ok(offset_page(
14158            labels,
14159            offset,
14160            page.limit.min(MAX_PAGE_SIZE) as usize,
14161        ))
14162    }
14163    async fn task_dependencies(
14164        &self,
14165        id: &NativeId,
14166        direction: Direction,
14167        page: &PageRequest,
14168    ) -> Result<Page<DependencyEdge>, SourceError> {
14169        self.dependencies(id, ItemKind::Task, direction, page).await
14170    }
14171    async fn project_dependencies(
14172        &self,
14173        id: &NativeId,
14174        direction: Direction,
14175        page: &PageRequest,
14176    ) -> Result<Page<DependencyEdge>, SourceError> {
14177        self.dependencies(id, ItemKind::Project, direction, page)
14178            .await
14179    }
14180
14181    fn writes(&self) -> WriteSupport {
14182        WriteSupport::Supported
14183    }
14184
14185    /// Create or update one task.
14186    ///
14187    /// Its `delivers` and `delivered_by` are checked before anything is read or written —
14188    /// neither may name the task itself or name one task twice — and land in the body's
14189    /// metadata slot under their reserved keys, in place of any caller metadata of those
14190    /// names.
14191    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
14192        self.write_task_assets(write, None)
14193            .await
14194            .map(|written| written.id)
14195    }
14196
14197    async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
14198        self.origins_ahead(origins).await
14199    }
14200
14201    async fn member_classifications(
14202        &self,
14203        project: &NativeId,
14204    ) -> Result<Option<Vec<(Classification, Vec<Repository>)>>, SourceError> {
14205        Ok(self
14206            .member_classes
14207            .lock()
14208            .map_err(|_| SourceError::Unavailable {
14209                message: "a project's members held for its classification were left \
14210                          inconsistent; run the command again"
14211                    .into(),
14212            })?
14213            .remove(project))
14214    }
14215
14216    /// The issue's own web address, as the `createIssue` that made it in this command answered
14217    /// it: a task, a project and a document are each an issue here.
14218    fn written_location(
14219        &self,
14220        _record: onetaskgraph_plugin_api::MetadataRecord,
14221        id: &NativeId,
14222    ) -> Option<Location> {
14223        self.written
14224            .lock()
14225            .unwrap_or_else(std::sync::PoisonError::into_inner)
14226            .get(id)
14227            .cloned()
14228            .map(Location::Url)
14229    }
14230
14231    /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
14232    /// in one request, and a copy's tasks are created together only then, since their fields
14233    /// and edges are what is held back.
14234    fn create_batch_limit(&self) -> usize {
14235        if self.deferring().unwrap_or(false) {
14236            MUTATION_BATCH
14237        } else {
14238            1
14239        }
14240    }
14241
14242    // 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.
14243    async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
14244        if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
14245            return Err(SourceError::Refused {
14246                message: format!(
14247                    "write_tasks creates tasks, and the write of {} names the existing task {}; \
14248                     next: update it with write_task",
14249                    write.item.id,
14250                    write.target.as_ref().map_or("", |target| target.0.as_str())
14251                ),
14252            });
14253        }
14254        if !self.deferring()? {
14255            let mut created = Vec::with_capacity(writes.len());
14256            for write in writes {
14257                match self.write_task(write).await {
14258                    Ok(id) => created.push(id),
14259                    Err(error) => {
14260                        let mut left: Vec<&str> = Vec::new();
14261                        for id in &created {
14262                            if self.delete_issue(id).await.is_err() {
14263                                left.push(id.0.as_str());
14264                            }
14265                        }
14266                        return Err(left_behind(error, &left));
14267                    }
14268                }
14269            }
14270            return Ok(created);
14271        }
14272        self.create_together(writes).await
14273    }
14274
14275    fn begin_deferred_writes(&self) {
14276        if let Ok(mut deferred) = self.deferred.lock() {
14277            deferred.get_or_insert_with(Deferred::default);
14278        }
14279    }
14280
14281    async fn finish_writes(&self) -> Result<(), SourceError> {
14282        self.send_held_updates().await?;
14283        self.send_held_back().await
14284    }
14285
14286    async fn write_task_with_assets(
14287        &self,
14288        write: &ItemWrite<Task>,
14289        _answers: Option<&BTreeMap<String, Value>>,
14290        assets: &onetaskgraph_plugin_api::AssetWrite,
14291    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14292        self.write_task_assets(write, Some(assets)).await
14293    }
14294
14295    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
14296        self.write_item(
14297            &Incoming {
14298                written: Written::Work(ItemKind::Project, &write.item.status),
14299                title: &write.item.title,
14300                content: write.item.content.as_deref(),
14301                assets: None,
14302                labels: &write.item.labels,
14303                metadata: &write.item.metadata,
14304                repositories: &write.item.repositories,
14305                classification: write.item.classification,
14306                parent: None,
14307                delivers: &[],
14308                delivered_by: &[],
14309                priority: None,
14310            },
14311            write.target.as_ref(),
14312            &write.depends_on,
14313        )
14314        .await
14315        .map(|written| written.id)
14316    }
14317
14318    /// Create or update one document, which is one issue titled the way this board spells
14319    /// a document.
14320    ///
14321    /// Everything else is exactly a task write: caller metadata goes to the same canonical
14322    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
14323    /// or a field this board cannot carry is refused by name rather than dropped, a target
14324    /// naming an issue this board does not hold is refused rather than created, and an
14325    /// issue this call created is taken back when the rest of the write fails.
14326    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
14327        self.write_document_assets(write, None)
14328            .await
14329            .map(|written| written.id)
14330    }
14331
14332    async fn write_document_with_assets(
14333        &self,
14334        write: &ItemWrite<Document>,
14335        _answers: Option<&BTreeMap<String, Value>>,
14336        assets: &onetaskgraph_plugin_api::AssetWrite,
14337    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14338        self.write_document_assets(write, Some(assets)).await
14339    }
14340
14341    /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
14342    /// which reads nothing; then the board's `Status` option. Over an existing item that is
14343    /// read off the item, as the write reads it, and the item is held among this command's
14344    /// resolved records so the write that follows reuses that read rather than repeating it;
14345    /// an item that does not carry the field takes the board's fields, which are held once
14346    /// read. A create is checked against the board's fields only when this command already
14347    /// holds them, because a create reads them together with its repository, in one request,
14348    /// and refuses a missing option before it writes anything.
14349    async fn check_status_write(
14350        &self,
14351        kind: ItemKind,
14352        category: StatusCategory,
14353        target: Option<&NativeId>,
14354    ) -> Result<(), SourceError> {
14355        let status = self.resolved_target(kind, category)?;
14356        if status.option().is_none() {
14357            return Ok(());
14358        }
14359        let fields = match target {
14360            Some(target) => {
14361                // A target this board does not hold is the write's own refusal to make.
14362                let Some(item) = self.bound_item(target).await? else {
14363                    return Ok(());
14364                };
14365                self.resolved_cache()?.insert(target.clone(), item.clone());
14366                // Only the `Status` field decides whether the write can land.
14367                self.fields_for(Some(&item), false, true, false)
14368                    .await?
14369                    .fields
14370            }
14371            None => {
14372                let held = self
14373                    .board_cache()?
14374                    .as_ref()
14375                    .map(|board| board.fields.clone());
14376                match held.or_else(|| {
14377                    self.fields_cache()
14378                        .ok()
14379                        .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
14380                }) {
14381                    Some(fields) => fields,
14382                    None => return Ok(()),
14383                }
14384            }
14385        };
14386        self.column_for(&fields, kind, category, &status)
14387            .map(|_| ())
14388    }
14389
14390    /// What a write of `status` reads back as: the `Status` option its category maps to for
14391    /// `kind`, and for a closing category the closure that option and its fixed reason read
14392    /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
14393    /// this instance has no option for is answered as given, since writing it is refused.
14394    fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
14395        match self.statuses.target(kind, status.category) {
14396            StatusTarget::Column(name) => {
14397                self.statuses.status(kind, Some(name.as_str()), false, None)
14398            }
14399            StatusTarget::Terminal(name, reason) => {
14400                self.statuses
14401                    .status(kind, Some(name.as_str()), true, Some(reason.reason()))
14402            }
14403            StatusTarget::Disabled(_) => status.clone(),
14404        }
14405    }
14406
14407    /// Set one task's status alone.
14408    ///
14409    /// An open target reopens a closed issue with an `updateIssue` carrying only its
14410    /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
14411    /// terminal target selects its mapped option, then closes with its fixed reason. No
14412    /// request carries a title, a body or a label. The status
14413    /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
14414    /// what a re-read reports.
14415    async fn set_task_status(
14416        &self,
14417        id: &NativeId,
14418        category: StatusCategory,
14419    ) -> Result<Option<Status>, SourceError> {
14420        self.set_status(id, ItemKind::Task, category).await
14421    }
14422
14423    /// Set one task's status and answer the task with it, reading the item once: the read the
14424    /// task is answered from is handed to the status write, which would otherwise read the
14425    /// same issue again — two requests on a fresh process, the read and the field write,
14426    /// where the default's `get_task` before `set_task_status` sent three.
14427    async fn set_task_status_reading(
14428        &self,
14429        id: &NativeId,
14430        category: StatusCategory,
14431    ) -> Result<Option<Task>, SourceError> {
14432        // Refused before anything is read, in the words the write itself refuses it.
14433        self.resolved_target(ItemKind::Task, category)?;
14434        let Some(item) = self
14435            .bound_item(id)
14436            .await?
14437            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
14438        else {
14439            return Ok(None);
14440        };
14441        let mut task = item.task()?;
14442        self.held_cache
14443            .lock()
14444            .map_err(|_| SourceError::Unavailable {
14445                message: "items read by issue reference were left inconsistent; run the \
14446                          command again"
14447                    .into(),
14448            })?
14449            .insert(id.clone(), item);
14450        let Some(status) = self.set_status(id, ItemKind::Task, category).await? else {
14451            return Ok(None);
14452        };
14453        task.status = status;
14454        Ok(Some(task))
14455    }
14456
14457    /// Set one project's status alone, by exactly the paired write a task's status takes, for
14458    /// the project kind: a terminal category selects the project kind's mapped option and then
14459    /// closes the issue with the category's fixed reason — an issue already closed with a
14460    /// reason that reads that category is left as it is — and an open category reopens a closed
14461    /// issue and then selects its option. A board draft is refused a terminal category as a
14462    /// task's is.
14463    async fn set_project_status(
14464        &self,
14465        id: &NativeId,
14466        category: StatusCategory,
14467    ) -> Result<Option<Status>, SourceError> {
14468        self.set_status(id, ItemKind::Project, category).await
14469    }
14470
14471    /// Every task and project whose card contradicts its issue's closure, read through the
14472    /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
14473    async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
14474        self.contradictions().await
14475    }
14476
14477    /// What this board holds for each issue reference, one request per fifty of them; see
14478    /// [`TaskSource::issue_holdings`].
14479    async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
14480        self.holdings(issues).await
14481    }
14482
14483    /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
14484    /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
14485    /// for `none`. Refused by an instance with no `priority_mapping`.
14486    async fn set_task_priority(
14487        &self,
14488        id: &NativeId,
14489        priority: Priority,
14490    ) -> Result<Option<Priority>, SourceError> {
14491        self.set_priority(id, priority).await
14492    }
14493
14494    /// Replace one task's content with a single body update that keeps the metadata slot
14495    /// byte for byte.
14496    async fn set_task_content(
14497        &self,
14498        id: &NativeId,
14499        content: &str,
14500    ) -> Result<Option<()>, SourceError> {
14501        self.replace_content(id, content).await
14502    }
14503
14504    /// Replace one task issue's content and its provenance slot entry with a single body
14505    /// update. The answers are not kept: see `replace_rendering`.
14506    async fn set_task_rendering(
14507        &self,
14508        id: &NativeId,
14509        content: &str,
14510        provenance: &Value,
14511        _answers: &BTreeMap<String, Value>,
14512    ) -> Result<Option<()>, SourceError> {
14513        self.replace_rendering(
14514            id,
14515            BoardKind::Work(ItemKind::Task),
14516            content,
14517            provenance,
14518            None,
14519        )
14520        .await
14521        .map(|written| written.map(|_| ()))
14522    }
14523
14524    /// Replace one design-document issue's content and its provenance slot entry, on exactly
14525    /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
14526    async fn set_document_rendering(
14527        &self,
14528        id: &NativeId,
14529        content: &str,
14530        provenance: &Value,
14531        _answers: &BTreeMap<String, Value>,
14532    ) -> Result<Option<()>, SourceError> {
14533        self.replace_rendering(id, BoardKind::Document, content, provenance, None)
14534            .await
14535            .map(|written| written.map(|_| ()))
14536    }
14537
14538    /// Replace one project issue's content and its provenance slot entry, on exactly the
14539    /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
14540    async fn set_project_rendering(
14541        &self,
14542        id: &NativeId,
14543        content: &str,
14544        provenance: &Value,
14545        _answers: &BTreeMap<String, Value>,
14546    ) -> Result<Option<()>, SourceError> {
14547        self.replace_rendering(
14548            id,
14549            BoardKind::Work(ItemKind::Project),
14550            content,
14551            provenance,
14552            None,
14553        )
14554        .await
14555        .map(|written| written.map(|_| ()))
14556    }
14557
14558    /// Apply a targeted update with one read of the item and a write only for what differs:
14559    /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
14560    /// and last one `updateIssue` for title, body and state. See `targeted_update`.
14561    async fn update_task(
14562        &self,
14563        id: &NativeId,
14564        update: &TaskUpdate,
14565    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
14566        self.targeted_update(id, update).await
14567    }
14568
14569    /// Replace one task's `delivered_by` with a single body update that changes the
14570    /// metadata slot and nothing outside it.
14571    async fn set_delivered_by(
14572        &self,
14573        id: &NativeId,
14574        delivered_by: &[TaskRef],
14575    ) -> Result<Option<()>, SourceError> {
14576        self.replace_delivered_by(id, delivered_by).await
14577    }
14578
14579    /// Set one key of one task issue's metadata with a single body update that changes the
14580    /// metadata slot and nothing outside it — no title, label, state or board field request —
14581    /// and sends nothing when the task already holds that value under the key.
14582    async fn set_task_metadata(
14583        &self,
14584        id: &NativeId,
14585        key: &MetadataKey,
14586        value: &Value,
14587    ) -> Result<Option<Task>, SourceError> {
14588        Ok(self
14589            .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
14590            .await?
14591            .map(|item| item.task())
14592            .transpose()?)
14593    }
14594
14595    /// Set one key of one project issue's metadata, on exactly the terms of
14596    /// [`set_task_metadata`](TaskSource::set_task_metadata).
14597    async fn set_project_metadata(
14598        &self,
14599        id: &NativeId,
14600        key: &MetadataKey,
14601        value: &Value,
14602    ) -> Result<Option<Project>, SourceError> {
14603        Ok(self
14604            .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
14605            .await?
14606            .map(|item| item.project()))
14607    }
14608
14609    /// Set one key of one design-document issue's metadata, on exactly the terms of
14610    /// [`set_task_metadata`](TaskSource::set_task_metadata).
14611    async fn set_document_metadata(
14612        &self,
14613        id: &NativeId,
14614        key: &MetadataKey,
14615        value: &Value,
14616    ) -> Result<Option<Document>, SourceError> {
14617        Ok(self
14618            .set_slot_key(id, BoardKind::Document, key, value)
14619            .await?
14620            .map(|item| item.document()))
14621    }
14622
14623    /// Take one key out of one issue's metadata slot with a single body update that changes the
14624    /// slot and nothing outside it, sending nothing when the slot does not hold the key.
14625    async fn remove_record_metadata(
14626        &self,
14627        record: onetaskgraph_plugin_api::MetadataRecord,
14628        id: &NativeId,
14629        key: &MetadataKey,
14630    ) -> Result<Option<Option<String>>, SourceError> {
14631        let kind = match record {
14632            onetaskgraph_plugin_api::MetadataRecord::Task => BoardKind::Work(ItemKind::Task),
14633            onetaskgraph_plugin_api::MetadataRecord::Project => BoardKind::Work(ItemKind::Project),
14634            onetaskgraph_plugin_api::MetadataRecord::Document => BoardKind::Document,
14635        };
14636        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
14637            return Ok(None);
14638        };
14639        if item.slot.contains_key(key.as_str()) {
14640            let mut slot = item.slot.clone();
14641            slot.remove(key.as_str());
14642            self.write_slot(&mut item, &slot).await?;
14643            self.remember_written(item.clone(), false)?;
14644        }
14645        Ok(Some(match record {
14646            onetaskgraph_plugin_api::MetadataRecord::Task => item.task()?.version,
14647            onetaskgraph_plugin_api::MetadataRecord::Project => item.project().version,
14648            onetaskgraph_plugin_api::MetadataRecord::Document => item.document().version,
14649        }))
14650    }
14651
14652    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
14653        self.delete_item(id).await
14654    }
14655
14656    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
14657        self.delete_item(id).await
14658    }
14659
14660    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
14661        self.delete_item(id).await
14662    }
14663
14664    /// One page of the task issue's own comments, walked by GitHub's own cursor.
14665    ///
14666    /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
14667    /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
14668    ///
14669    /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
14670    /// board is the read of its comments. A draft this process already resolved is refused
14671    /// without one.
14672    async fn task_comments(
14673        &self,
14674        task: &NativeId,
14675        page: &PageRequest,
14676    ) -> Result<Option<Page<Comment>>, SourceError> {
14677        validate_page(page)?;
14678        let cached = self.resolved_cache()?.get(task).cloned();
14679        if let Some(item) = cached {
14680            if item.kind != BoardKind::Work(ItemKind::Task) {
14681                return Ok(None);
14682            }
14683            if item.content_kind == ContentKind::DraftIssue {
14684                return Err(self.draft_has_no_comments(task));
14685            }
14686        }
14687        match self.issue_detail(task, page).await? {
14688            Some(TaskDetailRead {
14689                comments: Some(comments),
14690                ..
14691            }) => comments,
14692            _ => Ok(None),
14693        }
14694    }
14695
14696    /// Every id's task, with the first page of its comments when `comments` names it:
14697    /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
14698    /// comments in one [`graphql::ISSUE_DETAIL`] request.
14699    async fn get_task_details(
14700        &self,
14701        ids: &[NativeId],
14702        comments: Option<&PageRequest>,
14703    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14704        if let Some(page) = comments
14705            && let Err(error) = validate_page(page)
14706        {
14707            return ids.iter().map(|_| Err(error.clone())).collect();
14708        }
14709        match (ids, comments) {
14710            ([id], Some(page)) => vec![self.issue_detail(id, page).await],
14711            ([id], None) => vec![self.task_read(id).await],
14712            _ => self.issue_details(ids, comments).await,
14713        }
14714    }
14715
14716    /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
14717    /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
14718    /// request, so the dependency read a copy makes of each next is answered from this one.
14719    async fn get_task_details_for_copy(
14720        &self,
14721        ids: &[NativeId],
14722        comments: &PageRequest,
14723    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14724        // A page past the first is a page of one task's comments, which the detail read of it
14725        // alone answers.
14726        if comments.cursor.is_some() {
14727            return self.get_task_details(ids, Some(comments)).await;
14728        }
14729        if let Err(error) = validate_page(comments) {
14730            return ids.iter().map(|_| Err(error.clone())).collect();
14731        }
14732        let mut read = Vec::with_capacity(ids.len());
14733        for batch in ids.chunks(COPY_DETAIL_BATCH) {
14734            match self
14735                .graphql(
14736                    graphql::ISSUE_COPY_DETAILS,
14737                    copy_detail_batch(batch, comments, false),
14738                )
14739                .await
14740            {
14741                Ok(data) => {
14742                    for (slot, id) in batch.iter().enumerate() {
14743                        let read_one = match data.get(format!("i{slot}")) {
14744                            Some(node) => self.detail_of(id, node, true, None).await,
14745                            None => Err(SourceError::Malformed {
14746                                message: format!(
14747                                    "GitHub answered a batch read with no item for {}",
14748                                    id.0
14749                                ),
14750                            }),
14751                        };
14752                        read.push(read_one);
14753                    }
14754                }
14755                Err(error) if unresolvable_node(&error) => {
14756                    for id in batch {
14757                        read.push(self.issue_detail(id, comments).await);
14758                    }
14759                }
14760                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
14761            }
14762        }
14763        read
14764    }
14765
14766    /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
14767    /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
14768    /// and held for the next read of each by id; see `read_ahead`.
14769    ///
14770    /// What this run wrote is never read: its own record answers, as it does for a read by id.
14771    /// Nor is an item a listing of its project carried whole earlier in this command — see
14772    /// `carried` — which is held in place of reading it.
14773    /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
14774    /// so each of its ids is read on its own and answered as that read answers; a draft, and an
14775    /// answer naming another issue than the one asked for, are left to their own reads too.
14776    async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
14777        // The origins asked just before go out now, beside the members of the one project this
14778        // names when it names one; a project those origins answered whole is held for its next
14779        // read by id rather than read again here.
14780        let hinted = match ids {
14781            [one] => Some(one),
14782            _ => None,
14783        };
14784        self.send_pending_origins(hinted).await?;
14785        if let Some(project) = hinted {
14786            let answered = self.resolved_cache()?.get(project).cloned();
14787            if let Some(answered) = answered
14788                && answered.kind == BoardKind::Work(ItemKind::Project)
14789            {
14790                self.read_ahead()?.insert(project.clone(), Some(answered));
14791                return Ok(());
14792            }
14793        }
14794        let mut unread: Vec<NativeId> = Vec::new();
14795        // Read whole by a listing of its project in this command, which a mutation naming it
14796        // would have dropped: held as a batch would have held it, once every batch is answered.
14797        let mut listed: Vec<NativeId> = Vec::new();
14798        {
14799            let created = self.created()?;
14800            let held = self.read_ahead()?;
14801            let carried = self.carried()?;
14802            for id in ids {
14803                if unread.contains(id)
14804                    || listed.contains(id)
14805                    || held.contains_key(id)
14806                    || created.iter().any(|own| own.id == *id)
14807                {
14808                    continue;
14809                }
14810                // An issue a listing carried without the whole of what blocks it is read in a
14811                // batch too, which carries that, rather than having its dependency read sent
14812                // on its own once the copy reconciles its edges.
14813                let whole = carried.get(id).is_some_and(|item| {
14814                    item.content_kind != ContentKind::Issue || item.blocked_by.is_some()
14815                });
14816                if whole {
14817                    listed.push(id.clone());
14818                } else {
14819                    unread.push(id.clone());
14820                }
14821            }
14822        }
14823        // Nothing of an item's comments is wanted here: the smallest page keeps the read to
14824        // what the item itself costs.
14825        let comments = PageRequest {
14826            cursor: None,
14827            limit: 1,
14828        };
14829        // Held only once every batch has been answered, so a read-ahead that fails part way
14830        // holds nothing — what a listing carried included, which stays carried — and every
14831        // item is read on its own, as the method promises.
14832        let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
14833        // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
14834        // answered them: what a project copy's orphan check asks, when that first id is its
14835        // project.
14836        let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
14837        for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
14838            let data = match self
14839                .graphql(
14840                    graphql::ISSUE_COPY_DETAILS,
14841                    copy_detail_batch(batch, &comments, index == 0),
14842                )
14843                .await
14844            {
14845                Ok(data) => data,
14846                Err(error) if unresolvable_node(&error) => continue,
14847                Err(error) => return Err(error),
14848            };
14849            for (slot, id) in batch.iter().enumerate() {
14850                let Some(node) = data.get(format!("i{slot}")) else {
14851                    continue;
14852                };
14853                let held = if node.is_null() {
14854                    None
14855                } else {
14856                    match optional_str(node, "__typename")? {
14857                        Some("DraftIssue") => continue,
14858                        Some("Issue") if required_str(node, "id")? != id.0 => continue,
14859                        _ => {}
14860                    }
14861                    // Remembered below with the rest, so a later batch refused leaves no
14862                    // dependency read answered from this one either.
14863                    self.resolve_issue_as(node, false).await?
14864                };
14865                read.push((id.clone(), held));
14866            }
14867            if index == 0
14868                && let Some(node) = data.get("children").filter(|node| !node.is_null())
14869                && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
14870                && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14871            {
14872                // A list GitHub did not answer is not an empty one: it would answer the project's
14873                // task listing with nothing, and the orphan check with no orphans.
14874                let listed = node
14875                    .pointer("/subIssues/nodes")
14876                    .and_then(Value::as_array)
14877                    .ok_or_else(|| SourceError::Malformed {
14878                        message: format!(
14879                            "GitHub answered the sub-issues of project {} with no list of them",
14880                            batch[0].0
14881                        ),
14882                    })?
14883                    .iter()
14884                    .map(|child| {
14885                        Ok((
14886                            required_str(child, "id")?.to_owned(),
14887                            required_str(child, "title")?.to_owned(),
14888                        ))
14889                    })
14890                    .collect::<Result<Vec<_>, SourceError>>()?;
14891                children = Some((batch[0].clone(), listed));
14892            }
14893        }
14894        // The project's tasks are known from this read alone when every child it named is held
14895        // from it, but for a document, which its title says is one and which a task listing
14896        // never answers: then a listing of the project's tasks is answered from them.
14897        if let Some((project, listed)) = children
14898            && read.iter().any(|(id, held)| {
14899                *id == project
14900                    && held
14901                        .as_ref()
14902                        .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
14903            })
14904        {
14905            let mut tasks = Vec::new();
14906            let mut whole = true;
14907            for (child, title) in &listed {
14908                match read.iter().find(|(id, _)| id.0 == *child) {
14909                    Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
14910                        if held.kind == BoardKind::Work(ItemKind::Task) {
14911                            tasks.push(held.clone());
14912                        }
14913                    }
14914                    _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
14915                    _ => whole = false,
14916                }
14917            }
14918            if whole {
14919                self.task_children()?.insert(project, tasks);
14920            }
14921        }
14922        let mut resolved = self.resolved_cache()?;
14923        let mut ahead = self.read_ahead()?;
14924        let mut carried = self.carried()?;
14925        for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
14926            resolved.insert(item.id.clone(), item.clone());
14927        }
14928        // Taken only if still carried: a mutation naming it while the batches ran dropped it,
14929        // and its next read is then its own.
14930        for id in &listed {
14931            let Some(item) = carried.remove(id) else {
14932                continue;
14933            };
14934            // The whole of what it is blocked by came with it, so the dependency read a copy
14935            // makes next is answered from this one, as a copy's detail read is.
14936            if item.blocked_by.is_some() {
14937                resolved.insert(item.id.clone(), item.clone());
14938            }
14939            read.push((id.clone(), Some(item)));
14940        }
14941        for (id, held) in read {
14942            ahead.insert(id, held);
14943        }
14944        Ok(())
14945    }
14946
14947    /// Add one comment to the task's issue, as the account the token belongs to.
14948    ///
14949    /// The author is refused before anything is sent — not even the task is read — because
14950    /// no answer GitHub could give would make posting under another name than the one asked
14951    /// for the right outcome.
14952    async fn add_comment(
14953        &self,
14954        task: &NativeId,
14955        comment: &NewComment,
14956    ) -> Result<Option<Comment>, SourceError> {
14957        if let Some(author) = &comment.author {
14958            return Err(SourceError::Refused {
14959                message: format!(
14960                    "source {} cannot post a comment as {author:?}: GitHub records the account \
14961                     the token signs in as the author of every comment; next: leave --author \
14962                     out, and the comment is posted as that account",
14963                    self.name
14964                ),
14965            });
14966        }
14967        let Some(issue) = self.commented_issue(task).await? else {
14968            return Ok(None);
14969        };
14970        let data = self
14971            .graphql(
14972                graphql::ADD_COMMENT,
14973                json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
14974            )
14975            .await?;
14976        let subject = data
14977            .pointer("/addComment/subject")
14978            .filter(|value| !value.is_null())
14979            .ok_or_else(|| SourceError::Malformed {
14980                message: "GitHub comment addition returned no subject".into(),
14981            })?;
14982        if required_str(subject, "id")? != issue.0 {
14983            return Err(SourceError::Malformed {
14984                message: "GitHub comment addition answered about another issue".into(),
14985            });
14986        }
14987        let added = data
14988            .pointer("/addComment/commentEdge/node")
14989            .filter(|value| !value.is_null())
14990            .ok_or_else(|| SourceError::Malformed {
14991                message: "GitHub comment addition returned no comment".into(),
14992            })?;
14993        let added = comment_from(added)?;
14994        self.remember_commented(&issue)?;
14995        Ok(Some(added))
14996    }
14997
14998    async fn edit_comment(
14999        &self,
15000        task: &NativeId,
15001        comment: &NativeId,
15002        body: &CommentBody,
15003    ) -> Result<Option<Comment>, SourceError> {
15004        let Some(issue) = self.commented_issue(task).await? else {
15005            return Ok(None);
15006        };
15007        let Some(held) = self.comment_on(&issue, comment).await? else {
15008            return Ok(None);
15009        };
15010        // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
15011        let body = CommentOrigin::kept(&held, body)?;
15012        let data = self
15013            .graphql(
15014                graphql::UPDATE_COMMENT,
15015                json!({"input":{"id":comment.0,"body":body.as_str()}}),
15016            )
15017            .await?;
15018        let edited = data
15019            .pointer("/updateIssueComment/issueComment")
15020            .filter(|value| !value.is_null())
15021            .ok_or_else(|| SourceError::Malformed {
15022                message: "GitHub comment update returned no comment".into(),
15023            })?;
15024        let edited = comment_from(edited)?;
15025        if edited.id != *comment {
15026            return Err(SourceError::Malformed {
15027                message: "GitHub comment update returned the wrong comment".into(),
15028            });
15029        }
15030        self.remember_commented(&issue)?;
15031        Ok(Some(edited))
15032    }
15033
15034    async fn delete_comment(
15035        &self,
15036        task: &NativeId,
15037        comment: &NativeId,
15038    ) -> Result<Option<NativeId>, SourceError> {
15039        let Some(issue) = self.commented_issue(task).await? else {
15040            return Ok(None);
15041        };
15042        if !self.comment_is_on(&issue, comment).await? {
15043            return Ok(None);
15044        }
15045        let data = self
15046            .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
15047            .await?;
15048        // The payload says nothing about the comment it removed, so what is checked is that
15049        // GitHub answered the mutation at all rather than leaving it unanswered.
15050        data.get("deleteIssueComment")
15051            .filter(|value| !value.is_null())
15052            .ok_or_else(|| SourceError::Malformed {
15053                message: "GitHub comment deletion returned no payload".into(),
15054            })?;
15055        Ok(Some(comment.clone()))
15056    }
15057
15058    /// Every request this source has recorded, and what each of GitHub's two budgets was
15059    /// attributed — read off the same accounting the session report is rendered from, so
15060    /// the two cannot count one request two ways.
15061    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
15062        Ok(Some(self.ledger.snapshot().metering()))
15063    }
15064
15065    /// Drop every item, search answer and board read this source holds, so the next command
15066    /// reads the board as a person has since left it.
15067    ///
15068    /// Every one of those is held on the assumption that nothing but this source writes the
15069    /// board while a command runs, which stops being true the moment the command is over: a
15070    /// body a person edited would be overwritten from the record held here, and a card they
15071    /// moved would be read as still where this source left it. The board's own field
15072    /// definitions go too, because a person can add or delete a `Status` option and a write
15073    /// resolved against the held list would not re-read on a miss. What stays is what stays
15074    /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
15075    /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
15076    /// accounting [`metering`](TaskSource::metering) answers from.
15077    ///
15078    /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
15079    /// refused, because clearing it is what puts it right.
15080    async fn end_command(&self) -> Result<(), SourceError> {
15081        fn clear<T: Default>(held: &Mutex<T>) {
15082            *held
15083                .lock()
15084                .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
15085            held.clear_poison();
15086        }
15087        clear(&self.created);
15088        clear(&self.updated);
15089        clear(&self.commented);
15090        clear(&self.board_cache);
15091        clear(&self.search_cache);
15092        clear(&self.narrowed_cache);
15093        clear(&self.search_next);
15094        clear(&self.resolved_cache);
15095        clear(&self.read_ahead);
15096        clear(&self.pending_origins);
15097        clear(&self.member_classes);
15098        clear(&self.carried);
15099        clear(&self.held_cache);
15100        clear(&self.children_cache);
15101        clear(&self.task_children);
15102        clear(&self.written);
15103        clear(&self.fields_cache);
15104        self.expected.end_command();
15105        Ok(())
15106    }
15107}
15108
15109/// One member of a project as a classification-only read answers it: its classification and
15110/// the repositories its work changes.
15111type MemberClass = (Classification, Vec<Repository>);
15112
15113/// Each project's sub-issues as one command read them, keyed by the selector they were asked
15114/// for under, beside the project that selector named; see `children_cache`.
15115type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
15116
15117/// One issue comment as the contract carries it.
15118///
15119/// `author` is absent both when GitHub answers `null` for an account that no longer exists
15120/// and when it answers an actor with no login, because either way the source did not say who
15121/// wrote it — which is what an absent author means, rather than an author called nothing.
15122///
15123/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
15124/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
15125/// and is read back with the marker taken out and reported as its origin.
15126fn comment_from(value: &Value) -> Result<Comment, SourceError> {
15127    let actor = value.get("author").unwrap_or(&Value::Null);
15128    // A bot's login is spelled as GitHub's own pages spell it, `<login>[bot]`, so a reader —
15129    // and every copy of the comment — can tell it from a person's.
15130    let bot = actor.get("__typename").and_then(Value::as_str) == Some("Bot");
15131    Ok(Comment {
15132        origin: None,
15133        id: NativeId(required_str(value, "id")?.to_owned()),
15134        author: optional_str(actor, "login")?.map(|login| {
15135            if bot && !login.ends_with("[bot]") {
15136                format!("{login}[bot]")
15137            } else {
15138                login.to_owned()
15139            }
15140        }),
15141        created_at: optional_time(value, "createdAt")?,
15142        updated_at: optional_time(value, "updatedAt")?,
15143        body: required_str(value, "body")?.to_owned(),
15144        url: optional_str(value, "url")?.map(str::to_owned),
15145    }
15146    .unmarked())
15147}
15148
15149/// The page of comments one issue node carries, resumed from `after`.
15150fn comment_page(
15151    node: &Value,
15152    issue: &str,
15153    after: Option<&str>,
15154) -> Result<Page<Comment>, SourceError> {
15155    let connection = node
15156        .get("comments")
15157        .filter(|value| !value.is_null())
15158        .ok_or_else(|| SourceError::Malformed {
15159            message: format!("GitHub issue {issue} answered with no comments connection"),
15160        })?;
15161    let items = optional_nodes(Some(connection), "issue comments")?
15162        .into_iter()
15163        .flatten()
15164        .map(comment_from)
15165        .collect::<Result<Vec<_>, _>>()?;
15166    let next = next_cursor(connection)?;
15167    if let Some(next) = &next {
15168        validate_cursor_progress(after, &next.0)?;
15169    }
15170    Ok(Page { items, next })
15171}
15172
15173/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
15174/// end — `None` when it carried none, or a page with more past it.
15175fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
15176    let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
15177        return Ok(None);
15178    };
15179    if next_cursor(connection)?.is_some() {
15180        return Ok(None);
15181    }
15182    Ok(Some(
15183        optional_nodes(Some(connection), "blocked-by issues")?
15184            .into_iter()
15185            .flatten()
15186            .cloned()
15187            .collect(),
15188    ))
15189}
15190
15191/// Where the recorded tail of a dependency walk resumes; see
15192/// [`GitHubProjectsSource::recorded_edges`].
15193const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
15194
15195/// The board text field this source keeps a copy's origin in.
15196///
15197/// Named after the key it holds, and held to that name by the guard below rather than by
15198/// a reader noticing.
15199const ORIGIN_FIELD: &str = "onetaskgraph.origin";
15200
15201/// The metadata key that field holds.
15202///
15203/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
15204/// constructs or interprets the qualified id it carries. This source names it only to
15205/// route it — a short, typed value belongs in a typed field rather than in the body slot
15206/// a caller's own prose shares.
15207///
15208/// Restated rather than imported, because no plugin crate may depend on the engine. What
15209/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
15210/// target in `check`: it reads the engine's own literal and fails naming the file and the
15211/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
15212/// that creates a second item every run instead of finding the one it wrote — and that is
15213/// too late to learn it.
15214const ORIGIN_KEY: &str = "onetaskgraph.origin";
15215
15216/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
15217///
15218/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
15219/// is derived from the far end, never written down on the near item — so only a forward
15220/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
15221/// it did not come from, and it is told so rather than answered with an empty page that
15222/// reads as a walk which ended.
15223fn recorded_offset(
15224    cursor: Option<&str>,
15225    direction: Direction,
15226) -> Result<Option<usize>, SourceError> {
15227    cursor
15228        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
15229        .map(|offset| {
15230            if direction != Direction::DependsOn {
15231                return Err(SourceError::Config {
15232                    message: format!(
15233                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
15234                         reverse dependency read never issues; resume it in the direction \
15235                         that reported it"
15236                    ),
15237                });
15238            }
15239            offset.parse().map_err(|_| SourceError::Config {
15240                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
15241            })
15242        })
15243        .transpose()
15244}
15245
15246/// What a dependency read's own answer says of a board draft: `None` when the item is not a
15247/// draft, or the answer did not carry its board item; `Some(None)` for a draft whose one board
15248/// item is not on this board, which this source does not hold; and `Some(Some(slot))`, the
15249/// draft's metadata, for one it does — its board item naming this board by its number and by
15250/// the id the same answer read of the board, as [`GitHubProjectsSource::draft_by_id`] decides.
15251fn draft_slot(
15252    data: &Value,
15253    id: &NativeId,
15254    project_number: u32,
15255) -> Result<Option<Option<BTreeMap<String, Value>>>, SourceError> {
15256    let Some(draft) = data
15257        .get("node")
15258        .filter(|node| node.get("__typename").and_then(Value::as_str) == Some("DraftIssue"))
15259    else {
15260        return Ok(None);
15261    };
15262    let Some(items) = data.pointer("/draft/projectV2Items") else {
15263        return Ok(None);
15264    };
15265    if required_str(draft, "id")? != id.0 {
15266        return Err(SourceError::Malformed {
15267            message: format!("GitHub answered a different draft for {}", id.0),
15268        });
15269    }
15270    let nodes = items
15271        .get("nodes")
15272        .and_then(Value::as_array)
15273        .ok_or_else(|| SourceError::Malformed {
15274            message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
15275        })?;
15276    let info = items
15277        .get("pageInfo")
15278        .ok_or_else(|| SourceError::Malformed {
15279            message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
15280        })?;
15281    if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
15282        return Err(SourceError::Malformed {
15283            message: format!(
15284                "GitHub draft {} reports more board items than the one GitHub links a draft to",
15285                id.0
15286            ),
15287        });
15288    }
15289    let board = data
15290        .pointer("/board/projectV2/id")
15291        .and_then(Value::as_str)
15292        .ok_or_else(|| SourceError::Malformed {
15293            message: format!(
15294                "GitHub answered draft {} without this board's own id beside it",
15295                id.0
15296            ),
15297        })?;
15298    let mut held = false;
15299    for node in nodes {
15300        let project = node.get("project").ok_or_else(|| SourceError::Malformed {
15301            message: format!("GitHub draft {} board item has no project", id.0),
15302        })?;
15303        let number = project
15304            .get("number")
15305            .and_then(Value::as_u64)
15306            .ok_or_else(|| SourceError::Malformed {
15307                message: format!(
15308                    "GitHub draft {} board item has no numeric project number",
15309                    id.0
15310                ),
15311            })?;
15312        let project_id = required_str(project, "id")?;
15313        held |= number == u64::from(project_number) && project_id == board;
15314    }
15315    if !held {
15316        return Ok(Some(None));
15317    }
15318    Ok(Some(Some(
15319        metadata_body(optional_str(draft, "body")?.map(str::to_owned))?.1,
15320    )))
15321}
15322
15323fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
15324    let mut page = offset_page(edges, offset, limit.max(1));
15325    page.next = page
15326        .next
15327        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
15328    page
15329}
15330
15331/// The kind of one issue reached through a dependency connection.
15332///
15333/// The same questions the board scan asks, over the fields the dependency document
15334/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
15335/// then anything with sub-issues or the marker is a project.
15336///
15337/// # Errors
15338///
15339/// A far end this board holds as a document is refused rather than reported. The two
15340/// answers that are not refusals would both be wrong: reporting it as a task names an id
15341/// no task read of this source can find, and reporting it as a project names one no
15342/// project read can. There is no third value to return — `ItemKind` has no document
15343/// variant, because nothing may point at a document — so the relationship itself is what
15344/// the person is told about.
15345fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
15346    let id = required_str(value, "id")?;
15347    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
15348        return Err(SourceError::Refused {
15349            message: format!(
15350                "GitHub issue {id} is a document of this board — its title begins \
15351                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
15352                 on by one; next: remove that issue's blocking relationship on this board"
15353            ),
15354        });
15355    }
15356    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
15357    if parent.is_some() {
15358        return Ok(ItemKind::Task);
15359    }
15360    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
15361    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
15362        message: format!("GitHub issue {id}: {message}"),
15363    })?;
15364    let sub_issues = sub_issue_total(value)?;
15365    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
15366        ItemKind::Project
15367    } else {
15368        ItemKind::Task
15369    })
15370}
15371
15372/// The `stateInput` that closes an issue for `reason`.
15373fn closed_input(reason: ClosedState) -> Value {
15374    json!({"value":"CLOSED","stateReason":reason.reason()})
15375}
15376
15377/// The `IssueStateUpdateInput` one status target asks for.
15378///
15379/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
15380/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
15381/// a currently-closed issue: without that the item would read back `Unknown` and a copy
15382/// would report a change forever. A document has no status at all, and asks for neither.
15383fn state_input(target: Option<&StatusTarget>) -> Value {
15384    match target {
15385        Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
15386        Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
15387        // A document has no status, so a write of one says nothing about the issue's open
15388        // or closed state rather than forcing it open: `stateInput` is what carries that
15389        // instruction, and an explicit null asks for no change to it.
15390        None => Value::Null,
15391    }
15392}
15393
15394/// The metadata one write stores in the item's body slot.
15395///
15396/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
15397/// rather than carried: the kind marker so an empty project stays readable, the
15398/// repository list only when it is not exactly the issue's own repository, and the far
15399/// ends no relationship here can name.
15400///
15401/// The copy origin is the one typed field that is also mirrored here, and only as a
15402/// mirror: it lands in the board's origin field as well, which stays the one every reader
15403/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
15404/// and catches up with a write in seconds rather than minutes — can find the item by it.
15405/// A reader of the release before this one drops the slot's copy and reads the field, so an
15406/// item written here still reads with exactly one origin there.
15407fn slot_metadata(
15408    incoming: &Incoming<'_>,
15409    own_repository: Option<&Repository>,
15410    fallback: &[DependencyEdge],
15411) -> BTreeMap<String, Value> {
15412    let mut metadata = incoming.metadata.clone();
15413    match metadata.remove(ORIGIN_KEY) {
15414        Some(Value::String(origin)) if !origin.is_empty() => {
15415            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
15416        }
15417        _ => {}
15418    }
15419    match incoming.written.kind() {
15420        BoardKind::Work(kind) => metadata.insert(
15421            ItemKind::METADATA_KEY.to_owned(),
15422            Value::String(kind.marker().to_owned()),
15423        ),
15424        // A document is told by its title, so it carries no kind marker: that key names
15425        // what a dependency endpoint points at, and nothing may point at a document.
15426        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
15427    };
15428    incoming.classification.record(&mut metadata);
15429    let derivable = own_repository
15430        .map(|own| incoming.repositories == [own.clone()])
15431        .unwrap_or(incoming.repositories.is_empty());
15432    if derivable {
15433        metadata.remove(Repository::METADATA_KEY);
15434    } else {
15435        metadata.insert(
15436            Repository::METADATA_KEY.to_owned(),
15437            Value::Array(
15438                incoming
15439                    .repositories
15440                    .iter()
15441                    .map(|repository| Value::String(repository.as_str().to_owned()))
15442                    .collect(),
15443            ),
15444        );
15445    }
15446    // The typed lists are what land, whatever the caller's own metadata held under their
15447    // keys: a key of either name travelling beside the field would otherwise be a second
15448    // answer to the same question, and the field is the one the contract names.
15449    for (key, entries) in [
15450        (TaskRef::DELIVERS_KEY, incoming.delivers),
15451        (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
15452    ] {
15453        set_task_list(&mut metadata, key, entries);
15454    }
15455    record_edges(&mut metadata, fallback);
15456    metadata
15457}
15458
15459/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
15460/// one slot's metadata, or no such key when there are none.
15461fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
15462    if fallback.is_empty() {
15463        metadata.remove(DependencyEdge::RECORDED_KEY);
15464    } else {
15465        metadata.insert(
15466            DependencyEdge::RECORDED_KEY.to_owned(),
15467            Value::Array(
15468                fallback
15469                    .iter()
15470                    .map(|edge| {
15471                        serde_json::to_value(&edge.to).expect("an endpoint renders as JSON")
15472                    })
15473                    .collect(),
15474            ),
15475        );
15476    }
15477}
15478
15479/// Every label one item carries, from its content's own connection and nowhere else.
15480///
15481/// There is no second place to read one from: no document this source sends selects the
15482/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
15483/// cannot carry one at all. The module documentation records the three schema facts that
15484/// settle it.
15485fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
15486    optional_nodes(content.get("labels"), "content labels")?
15487        .into_iter()
15488        .flatten()
15489        .map(|v| {
15490            Ok(Label {
15491                id: NativeId(required_str(v, "id")?.to_owned()),
15492                name: required_str(v, "name")?.to_owned(),
15493                color: optional_str(v, "color")?.map(str::to_owned),
15494            })
15495        })
15496        .collect()
15497}
15498
15499/// Each board field value one item holds, as the change token's facets name it: the field's
15500/// name, with the single-select option's name or the text. A value of a type no read here
15501/// selects, and one naming no field, says nothing a person can rename and is left out.
15502fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
15503    field_values
15504        .iter()
15505        .filter_map(|value| {
15506            let field = value.pointer("/field/name")?.as_str()?;
15507            let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
15508            Some((field.to_owned(), held.to_owned()))
15509        })
15510        .collect()
15511}
15512
15513/// The digest of the facets of the issue `content`, whose board item's values are
15514/// `field_values` — or `None` when the response did not carry, whole, every member they are read
15515/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
15516/// label, and every field value.
15517///
15518/// What a write's own response spells its token from, by exactly the rules a read takes them by.
15519fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
15520    let ended = |connection: &Value| {
15521        connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
15522    };
15523    let labels = content.get("labels").filter(|labels| ended(labels))?;
15524    if !ended(field_values) {
15525        return None;
15526    }
15527    let parent = content.get("parent")?;
15528    let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
15529    let kind = BoardKind::of(
15530        content.get("title")?.as_str()?,
15531        !parent.is_null(),
15532        sub_issue_total(content).ok()?,
15533        ItemKind::from_metadata(&slot).ok()?,
15534    );
15535    let labels = self::labels(&json!({"labels": labels})).ok()?;
15536    let nodes = field_values.get("nodes")?.as_array()?;
15537    Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
15538}
15539
15540/// The definition of each board field one item's values are values of, in the shape a read
15541/// of the board's own `fields` gives one.
15542///
15543/// A value names its field through a fragment on that field's own type, so the type is
15544/// known from which kind of value it is: a single-select value's field is a
15545/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
15546/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
15547fn field_definitions(field_values: &[Value]) -> Vec<Value> {
15548    field_values
15549        .iter()
15550        .filter_map(|value| {
15551            let field = value.get("field")?.as_object()?;
15552            field.get("id")?.as_str().filter(|id| !id.is_empty())?;
15553            let typename = if value.get("text").is_some() {
15554                "ProjectV2Field"
15555            } else if value.get("name").is_some() {
15556                "ProjectV2SingleSelectField"
15557            } else {
15558                return None;
15559            };
15560            let mut defined = field.clone();
15561            defined.insert("__typename".to_owned(), json!(typename));
15562            // Only a text field holds a text value, so the value says the field's type too.
15563            if typename == "ProjectV2Field" {
15564                defined.insert("dataType".to_owned(), json!("TEXT"));
15565            }
15566            Some(Value::Object(defined))
15567        })
15568        .collect()
15569}
15570
15571fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
15572    let Some(node) = field_values
15573        .iter()
15574        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
15575    else {
15576        return Ok(None);
15577    };
15578    Ok(optional_str(node, "text")?.map(str::to_owned))
15579}
15580
15581fn valid_github_owner(owner: &str) -> bool {
15582    !owner.is_empty()
15583        && owner.len() <= 39
15584        && !owner.starts_with('-')
15585        && !owner.ends_with('-')
15586        && !owner.contains("--")
15587        && owner
15588            .bytes()
15589            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
15590}
15591
15592/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
15593/// neither of the two names a path segment already means.
15594fn valid_github_repository_name(name: &str) -> bool {
15595    !name.is_empty()
15596        && name.len() <= 100
15597        && name != "."
15598        && name != ".."
15599        && name
15600            .bytes()
15601            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
15602}
15603
15604fn valid_environment_name(name: &str) -> bool {
15605    let mut bytes = name.bytes();
15606    bytes
15607        .next()
15608        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
15609        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
15610}
15611
15612/// How many sub-issues one issue has.
15613///
15614/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
15615/// absent or non-integer one is a response this source cannot read — and reading it as
15616/// zero would classify a project as a task, which is exactly the mistake the marker
15617/// exists to keep from happening quietly.
15618fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
15619    let summary = issue
15620        .get("subIssuesSummary")
15621        .ok_or_else(|| SourceError::Malformed {
15622            message: "GitHub issue is missing subIssuesSummary".into(),
15623        })?;
15624    summary
15625        .get("total")
15626        .and_then(Value::as_u64)
15627        .ok_or_else(|| SourceError::Malformed {
15628            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
15629        })
15630}
15631
15632/// One issue's own `number`.
15633///
15634/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
15635/// an issue in this module asks for it. So a read of one that comes back without it, or
15636/// with something that is not an unsigned integer, is a response this source cannot read —
15637/// absence here is **not** "this issue has no number". A draft is the content that has
15638/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
15639/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
15640fn issue_number(issue: &Value) -> Result<u64, SourceError> {
15641    issue
15642        .get("number")
15643        .and_then(Value::as_u64)
15644        .ok_or_else(|| SourceError::Malformed {
15645            message: "GitHub issue number is missing or is not an unsigned integer".into(),
15646        })
15647}
15648
15649/// The `number` a creating mutation answered with, and `None` when it answered without one;
15650/// why a missing one is tolerated is at the call in `create_and_file_issue`.
15651fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
15652    match created.get("number") {
15653        None | Some(Value::Null) => Ok(None),
15654        Some(value) => value
15655            .as_u64()
15656            .map(Some)
15657            .ok_or_else(|| SourceError::Malformed {
15658                message: "GitHub created issue number is not an unsigned integer".into(),
15659            }),
15660    }
15661}
15662
15663fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15664    value
15665        .get(field)
15666        .and_then(Value::as_str)
15667        .ok_or_else(|| SourceError::Malformed {
15668            message: format!("GitHub response is missing string field {field}"),
15669        })
15670}
15671
15672fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
15673    let found = required_str(value, field)?;
15674    if found.trim().is_empty() {
15675        return Err(SourceError::Malformed {
15676            message: format!("GitHub response has blank string field {field}"),
15677        });
15678    }
15679    Ok(found)
15680}
15681
15682/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
15683/// needs one — Linear spells them too, in its own description field.
15684///
15685/// Restated rather than shared, because a plugin crate depends on the contract crate and
15686/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
15687/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
15688/// source round-trips its own writes perfectly well under its own spelling.
15689const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
15690const METADATA_CLOSE: &str = "\n-->";
15691
15692/// What the composer puts between a non-empty visible body and the slot, and the one thing
15693/// the parser takes off the visible body when it takes the slot off — exactly once, so every
15694/// other trailing byte of the body comes back as it was written.
15695// 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.
15696const METADATA_SEPARATOR: &str = "\n\n";
15697
15698/// The visible body and the metadata slot at the end of it.
15699///
15700/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
15701/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
15702/// own content and is left alone. The visible body is everything before the slot less the
15703/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
15704fn metadata_body(
15705    body: Option<String>,
15706) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
15707    let Some(body) = body else {
15708        return Ok((None, BTreeMap::new()));
15709    };
15710    let Some(slot) = slot_span(&body)? else {
15711        return Ok((Some(body), BTreeMap::new()));
15712    };
15713    let metadata =
15714        serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
15715            SourceError::Malformed {
15716                message: format!(
15717                    "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
15718                ),
15719            }
15720        })?;
15721    let before = &body[..slot.start];
15722    let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
15723    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
15724}
15725
15726/// Where the metadata slot sits in one body, as byte offsets into it.
15727struct SlotSpan {
15728    /// Where [`METADATA_OPEN`] begins.
15729    start: usize,
15730    /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
15731    encoded_start: usize,
15732    /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
15733    encoded_end: usize,
15734    /// Just past [`METADATA_CLOSE`].
15735    end: usize,
15736}
15737
15738/// The slot at the very end of `body`, or `None` when it has none.
15739///
15740/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
15741/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
15742/// slot.
15743fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
15744    let Some(start) = body.rfind(METADATA_OPEN) else {
15745        return Ok(None);
15746    };
15747    let encoded_start = start + METADATA_OPEN.len();
15748    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
15749        return Err(SourceError::Malformed {
15750            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
15751        });
15752    };
15753    let encoded_end = encoded_start + relative_end;
15754    let end = encoded_end + METADATA_CLOSE.len();
15755    if !body[end..].trim().is_empty() {
15756        return Ok(None);
15757    }
15758    Ok(Some(SlotSpan {
15759        start,
15760        encoded_start,
15761        encoded_end,
15762        end,
15763    }))
15764}
15765
15766/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
15767/// slot as it was.
15768///
15769/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
15770/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
15771/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
15772/// or alone in an empty body — and a body with no slot that is given no metadata is
15773/// returned as it is.
15774fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
15775    let encoded = if metadata.is_empty() {
15776        None
15777    } else {
15778        Some(
15779            serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15780                message: error.to_string(),
15781            })?,
15782        )
15783    };
15784    Ok(match (slot_span(body)?, encoded) {
15785        (Some(slot), Some(encoded)) => format!(
15786            "{}{encoded}{}",
15787            &body[..slot.encoded_start],
15788            &body[slot.encoded_end..]
15789        ),
15790        (Some(slot), None) => {
15791            let before = &body[..slot.start];
15792            format!(
15793                "{}{}",
15794                before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
15795                &body[slot.end..]
15796            )
15797        }
15798        (None, None) => body.to_owned(),
15799        (None, Some(encoded)) if body.is_empty() => {
15800            format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15801        }
15802        (None, Some(encoded)) => {
15803            format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15804        }
15805    })
15806}
15807
15808/// `body` with everything before its metadata slot replaced by `content`, and the slot
15809/// itself kept byte for byte.
15810///
15811/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
15812/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
15813/// `content` is empty — so a read of the result reports `content` as the visible body and
15814/// the slot's metadata exactly as it was.
15815fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
15816    let Some(slot) = slot_span(body)? else {
15817        return Ok(content.to_owned());
15818    };
15819    let kept = &body[slot.start..];
15820    Ok(if content.is_empty() {
15821        kept.to_owned()
15822    } else {
15823        format!("{content}{METADATA_SEPARATOR}{kept}")
15824    })
15825}
15826
15827/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
15828fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
15829    if entries.is_empty() {
15830        metadata.remove(key);
15831    } else {
15832        metadata.insert(
15833            key.to_owned(),
15834            Value::Array(
15835                entries
15836                    .iter()
15837                    .map(|entry| Value::String(entry.as_str().to_owned()))
15838                    .collect(),
15839            ),
15840        );
15841    }
15842}
15843
15844fn compose_body(
15845    content: Option<&str>,
15846    metadata: &BTreeMap<String, Value>,
15847) -> Result<Option<String>, SourceError> {
15848    let visible = content.unwrap_or_default();
15849    if metadata.is_empty() {
15850        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
15851    }
15852    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15853        message: error.to_string(),
15854    })?;
15855    Ok(Some(if visible.is_empty() {
15856        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15857    } else {
15858        format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15859    }))
15860}
15861
15862fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
15863    value
15864        .get(field)
15865        .and_then(Value::as_bool)
15866        .ok_or_else(|| SourceError::Malformed {
15867            message: format!("GitHub response is missing boolean field {field}"),
15868        })
15869}
15870fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
15871    match value.get(field) {
15872        None | Some(Value::Null) => Ok(None),
15873        Some(value) => value
15874            .as_str()
15875            .map(Some)
15876            .ok_or_else(|| SourceError::Malformed {
15877                message: format!("GitHub response field {field} is not a string or null"),
15878            }),
15879    }
15880}
15881fn optional_nodes<'a>(
15882    connection: Option<&'a Value>,
15883    name: &str,
15884) -> Result<Option<&'a Vec<Value>>, SourceError> {
15885    match connection {
15886        None | Some(Value::Null) => Ok(None),
15887        Some(value) => value
15888            .get("nodes")
15889            .and_then(Value::as_array)
15890            .map(Some)
15891            .ok_or_else(|| SourceError::Malformed {
15892                message: format!("GitHub {name}.nodes is not an array"),
15893            }),
15894    }
15895}
15896fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
15897    let page_info = connection
15898        .get("pageInfo")
15899        .ok_or_else(|| SourceError::Malformed {
15900            message: format!("GitHub {name} has no pageInfo"),
15901        })?;
15902    if required_bool(page_info, "hasNextPage")? {
15903        return Err(SourceError::Malformed {
15904            message: format!(
15905                "GitHub {name} exceeds the supported nested connection size of {size}"
15906            ),
15907        });
15908    }
15909    Ok(())
15910}
15911fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
15912    optional_str(value, field)?
15913        .map(|timestamp| {
15914            timestamp.parse().map_err(|error| SourceError::Malformed {
15915                message: format!("GitHub response field {field} is not a timestamp: {error}"),
15916            })
15917        })
15918        .transpose()
15919}
15920fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
15921    if page.limit == 0 {
15922        Err(SourceError::Config {
15923            message: "page limit must be at least 1".into(),
15924        })
15925    } else {
15926        Ok(())
15927    }
15928}
15929fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
15930    let page = connection
15931        .get("pageInfo")
15932        .filter(|value| value.is_object())
15933        .ok_or_else(|| SourceError::Malformed {
15934            message: "GitHub connection is missing pageInfo".into(),
15935        })?;
15936    if required_bool(page, "hasNextPage")? {
15937        let cursor = required_str(page, "endCursor")?;
15938        validate_cursor_progress(None, cursor)?;
15939        Ok(Some(Cursor(cursor.into())))
15940    } else {
15941        Ok(None)
15942    }
15943}
15944fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
15945    if next.is_empty() || previous == Some(next) {
15946        Err(SourceError::Malformed {
15947            message: "GitHub pagination cursor is empty or did not advance".into(),
15948        })
15949    } else {
15950        Ok(())
15951    }
15952}
15953/// The version of this plugin's opaque narrowing-search cursor.
15954pub const SEARCH_CURSOR_VERSION: u32 = 4;
15955
15956#[derive(Serialize, Deserialize)]
15957#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
15958enum SearchConnection {
15959    Initial {},
15960    Continuing { after: Cursor },
15961    Exhausted {},
15962}
15963impl SearchConnection {
15964    fn after(&self) -> Option<&str> {
15965        match self {
15966            Self::Continuing { after } => Some(&after.0),
15967            _ => None,
15968        }
15969    }
15970    fn exhausted(&self) -> bool {
15971        matches!(self, Self::Exhausted { .. })
15972    }
15973    /// Whether a cursor naming this position, `offset` rows into its page, is one this
15974    /// plugin could have handed out: a page is resumed only part of the way through it — an
15975    /// offset of a whole page or more would skip rows nobody was given — an initial page
15976    /// only once some of it was handed out, and an exhausted connection has no page to be
15977    /// part of the way through.
15978    fn valid_resume(&self, offset: usize) -> bool {
15979        let within = offset < SEARCH_PAGE_SIZE as usize;
15980        match self {
15981            Self::Initial { .. } => offset > 0 && within,
15982            Self::Continuing { after } => !after.0.is_empty() && within,
15983            Self::Exhausted { .. } => offset == 0,
15984        }
15985    }
15986}
15987
15988/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
15989#[derive(Serialize, Deserialize)]
15990#[serde(deny_unknown_fields)]
15991struct SearchPosition {
15992    version: u32,
15993    connection: SearchConnection,
15994    /// How many rows of the page `connection` starts were already handed out.
15995    #[serde(default, skip_serializing_if = "is_zero")]
15996    offset: usize,
15997    #[serde(default, skip_serializing_if = "Vec::is_empty")]
15998    seen: Vec<NativeId>,
15999    #[serde(default, skip_serializing_if = "Vec::is_empty")]
16000    own: Vec<NativeId>,
16001}
16002impl Default for SearchPosition {
16003    fn default() -> Self {
16004        Self {
16005            version: SEARCH_CURSOR_VERSION,
16006            connection: SearchConnection::Initial {},
16007            offset: 0,
16008            seen: Vec::new(),
16009            own: Vec::new(),
16010        }
16011    }
16012}
16013
16014fn is_zero(offset: &usize) -> bool {
16015    *offset == 0
16016}
16017
16018fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
16019    cursor.map_or(Ok(0), |c| {
16020        c.0.parse().map_err(|_| SourceError::Config {
16021            message: "page cursor is invalid".into(),
16022        })
16023    })
16024}
16025fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
16026    if offset > items.len() {
16027        return Page::last(vec![]);
16028    }
16029    let tail = items.split_off(offset);
16030    let mut selected = tail;
16031    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
16032    selected.truncate(limit);
16033    Page {
16034        items: selected,
16035        next,
16036    }
16037}
16038
16039impl GitHubProjectsSource {
16040    async fn write_task_assets(
16041        &self,
16042        write: &ItemWrite<Task>,
16043        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
16044    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
16045        self.write_item(
16046            &self.task_incoming(write, assets)?,
16047            write.target.as_ref(),
16048            &write.depends_on,
16049        )
16050        .await
16051    }
16052
16053    /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
16054    /// are known to be ones this source can write.
16055    fn task_incoming<'a>(
16056        &self,
16057        write: &'a ItemWrite<Task>,
16058        assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
16059    ) -> Result<Incoming<'a>, SourceError> {
16060        let near = write.target.as_ref().unwrap_or(&write.item.id);
16061        for (key, entries) in [
16062            (TaskRef::DELIVERS_KEY, &write.item.delivers),
16063            (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
16064        ] {
16065            TaskRef::listed(key, near, Some(&self.name), entries.clone())
16066                .map_err(|message| SourceError::Refused { message })?;
16067        }
16068        if self.priorities.is_none() && write.item.priority != Priority::None {
16069            return Err(self.holds_no_priority());
16070        }
16071        Ok(Incoming {
16072            written: Written::Work(ItemKind::Task, &write.item.status),
16073            title: &write.item.title,
16074            content: write.item.content.as_deref(),
16075            assets,
16076            labels: &write.item.labels,
16077            metadata: &write.item.metadata,
16078            repositories: &write.item.repositories,
16079            classification: write.item.classification,
16080            parent: write.item.project.as_ref(),
16081            delivers: &write.item.delivers,
16082            delivered_by: &write.item.delivered_by,
16083            priority: self.priorities.as_ref().map(|_| write.item.priority),
16084        })
16085    }
16086    async fn write_document_assets(
16087        &self,
16088        write: &ItemWrite<Document>,
16089        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
16090    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
16091        // A document takes part in no dependency graph, so there is no far end to write
16092        // natively and none to record: a caller naming one is told so rather than having it
16093        // stored under the reserved key, where a later read would report an edge the
16094        // contract says cannot exist.
16095        if !write.depends_on.is_empty() {
16096            return Err(SourceError::Refused {
16097                message: format!(
16098                    "this write names {} dependencies for a document, and a document takes \
16099                     part in no dependency graph; next: put the dependency on the task or \
16100                     project the document is about",
16101                    write.depends_on.len()
16102                ),
16103            });
16104        }
16105        self.write_item(
16106            &Incoming {
16107                written: Written::Document,
16108                title: &write.item.title,
16109                content: write.item.content.as_deref(),
16110                assets,
16111                labels: &write.item.labels,
16112                metadata: &write.item.metadata,
16113                repositories: &write.item.repositories,
16114                classification: write.item.classification,
16115                parent: write.item.project.as_ref(),
16116                delivers: &[],
16117                delivered_by: &[],
16118                priority: None,
16119            },
16120            write.target.as_ref(),
16121            &[],
16122        )
16123        .await
16124    }
16125}
16126
16127/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
16128/// itself, for the two things no journey can observe.
16129///
16130/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
16131/// with and without the call, that a settlement, a board listing and a metadata search each
16132/// read afresh after it — the resolved records, the written-item overlay, the board and its
16133/// search, and the narrowed searches. What they cannot reach is the held field definitions,
16134/// because a status write naming an option a person deleted is refused the same whether or
16135/// not the list is held, and a poisoned lock, because nothing outside the source can panic
16136/// while one of its locks is held. So these assert those directly, and every other holder
16137/// beside them so a holder added later without a clear in the call fails here.
16138#[cfg(test)]
16139mod end_command_tests {
16140    use super::*;
16141
16142    struct Token;
16143
16144    impl SecretResolver for Token {
16145        fn get(&self, var: &str) -> Option<SecretString> {
16146            (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
16147        }
16148    }
16149
16150    fn source() -> GitHubProjectsSource {
16151        let config = serde_json::from_value(json!({
16152            "owner": "octo-org", "project_number": 7, "repository": "acme/work",
16153            // Nothing here is sent: the source is only built and its state inspected.
16154            "endpoint": "http://127.0.0.1:9/graphql",
16155        }))
16156        .expect("a usable configuration");
16157        GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
16158            .expect("the source builds")
16159    }
16160
16161    /// One issue as a board read answers it.
16162    fn resolved(source: &GitHubProjectsSource) -> Resolved {
16163        source
16164            .resolve(&json!({
16165                "id": "ITEM-1",
16166                "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
16167                            "body": "what a person may since have edited", "state": "OPEN",
16168                            "stateReason": null, "url": null, "number": 1,
16169                            "subIssuesSummary": {"total": 0},
16170                            "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
16171                "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
16172            }))
16173            .expect("the item reads")
16174            .expect("an issue")
16175    }
16176
16177    /// Hold something in every holder the call clears, and the repository id it keeps.
16178    fn fill(source: &GitHubProjectsSource) {
16179        let item = resolved(source);
16180        source.created.lock().unwrap().push(item.clone());
16181        source.updated.lock().unwrap().push(item.clone());
16182        *source.board_cache.lock().unwrap() = Some(Board {
16183            id: "PVT-board".into(),
16184            fields: json!({"nodes": []}),
16185            items: vec![item.clone()],
16186        });
16187        *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
16188        source
16189            .narrowed_cache
16190            .lock()
16191            .unwrap()
16192            .insert("status:todo".into(), vec![item.clone()]);
16193        source.children_cache.lock().unwrap().insert(
16194            NativeId("P-1".into()),
16195            (NativeId("P-1".into()), vec![item.clone()]),
16196        );
16197        source
16198            .search_next
16199            .lock()
16200            .unwrap()
16201            .insert("status:todo".into(), Some("cursor".into()));
16202        source
16203            .resolved_cache
16204            .lock()
16205            .unwrap()
16206            .insert(item.id.clone(), item);
16207        *source.fields_cache.lock().unwrap() = Some(BoardFields {
16208            id: BoardId::parse("PVT-board").unwrap(),
16209            fields: json!({"nodes": []}),
16210        });
16211        source
16212            .repository_cache
16213            .lock()
16214            .unwrap()
16215            .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
16216    }
16217
16218    fn assert_dropped(source: &GitHubProjectsSource) {
16219        assert!(source.created().unwrap().is_empty(), "created");
16220        assert!(source.updated().unwrap().is_empty(), "updated");
16221        assert!(source.board_cache().unwrap().is_none(), "board");
16222        assert!(source.search_cache.lock().unwrap().is_none(), "search");
16223        assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
16224        assert!(
16225            source.children_cache.lock().unwrap().is_empty(),
16226            "project children"
16227        );
16228        assert!(
16229            source.search_next.lock().unwrap().is_empty(),
16230            "search paging"
16231        );
16232        assert!(
16233            source.resolved_cache().unwrap().is_empty(),
16234            "resolved records"
16235        );
16236        assert!(source.fields_cache().unwrap().is_none(), "board fields");
16237        assert_eq!(
16238            source.repository_cache().unwrap().len(),
16239            1,
16240            "a repository's node id stays valid and is kept"
16241        );
16242    }
16243
16244    fn end(source: &GitHubProjectsSource) {
16245        tokio::runtime::Builder::new_current_thread()
16246            .build()
16247            .unwrap()
16248            .block_on(source.end_command())
16249            .expect("the command ends");
16250    }
16251
16252    #[test]
16253    fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
16254        let source = source();
16255        fill(&source);
16256        end(&source);
16257        assert_dropped(&source);
16258    }
16259
16260    #[test]
16261    fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
16262        fn poison<T: Send>(held: &Mutex<T>) {
16263            std::thread::scope(|scope| {
16264                let _ = scope
16265                    .spawn(|| {
16266                        let _guard = held.lock().unwrap();
16267                        panic!("a failure while the lock is held");
16268                    })
16269                    .join();
16270            });
16271            assert!(held.is_poisoned());
16272        }
16273        let source = source();
16274        fill(&source);
16275        poison(&source.created);
16276        poison(&source.updated);
16277        poison(&source.board_cache);
16278        poison(&source.search_cache);
16279        poison(&source.narrowed_cache);
16280        poison(&source.search_next);
16281        poison(&source.resolved_cache);
16282        poison(&source.fields_cache);
16283        assert!(
16284            source.resolved_cache().is_err(),
16285            "a poisoned lock is refused before the call"
16286        );
16287        end(&source);
16288        assert_dropped(&source);
16289    }
16290}
16291
16292#[cfg(test)]
16293mod unprojectable_fields_tests {
16294    use super::*;
16295
16296    /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
16297    fn documented() -> std::collections::BTreeSet<String> {
16298        let document = std::fs::read_to_string(
16299            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
16300        )
16301        .expect("docs/metadata.md is readable");
16302        let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
16303        let lead = "a `field` equal, ignoring case, to ";
16304        let start = document.find(lead).expect("the refused-field sentence") + lead.len();
16305        let sentence = &document[start..];
16306        let sentence = &sentence[..sentence
16307            .find("; and two entries naming one field")
16308            .expect("the refused-field sentence ends at the duplicate rule")];
16309        let (own, github) = sentence
16310            .split_once(" or a field GitHub owns on every board (")
16311            .expect("the sentence names GitHub's own fields");
16312        own.split('`')
16313            .skip(1)
16314            .step_by(2)
16315            .chain(
16316                github
16317                    .strip_suffix(')')
16318                    .expect("GitHub's own fields close the sentence")
16319                    .split(", "),
16320            )
16321            .map(str::to_owned)
16322            .collect()
16323    }
16324
16325    #[test]
16326    fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
16327        let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
16328            .iter()
16329            .map(|&name| name.to_owned())
16330            .collect();
16331        assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
16332        let instance = SourceName::new("work").unwrap();
16333        for name in UNPROJECTABLE_FIELDS {
16334            for spelled in [name.to_owned(), name.to_uppercase()] {
16335                let entry = MetadataFieldConfig {
16336                    field: spelled.clone(),
16337                    key: "team.machine".to_owned(),
16338                    path: Vec::new(),
16339                };
16340                let refused = MetadataField::resolve(vec![entry], &instance)
16341                    .expect_err("an unprojectable field is refused");
16342                assert!(
16343                    refused.to_string().contains(&format!("{name:?}")),
16344                    "{spelled}: {refused}"
16345                );
16346            }
16347        }
16348    }
16349}