Skip to main content

onetaskgraph_github_projects/
lib.rs

1//! A stateless onetaskgraph source over one GitHub Projects v2 board.
2//!
3//! **A board is a container of projects, not a project.** Its own `title`,
4//! `shortDescription` and `readme` are never read as an item's fields and are never
5//! written: nothing in this source can rename the board a user configured.
6//!
7//! **A project is an issue and its tasks are that issue's sub-issues.** GitHub's schema
8//! decides that: `Issue` exposes `parent`, `subIssues` and `subIssuesSummary`, and
9//! `DraftIssue` exposes none of them. Creating an issue needs a `repositoryId`, and a
10//! board has none, so a write without [`GitHubProjectsConfig::repository`] is refused
11//! naming the field — but that repository is the *fallback*, not the home of every item.
12//!
13//! <!-- llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] The rule's one
14//! executable source is `GitHubProjectsSource::creation_target`; this is where a reader of
15//! the module meets it, and `tests/plugin.rs` drives every arm below against the loopback
16//! board and asserts on `createIssue`'s own `repositoryId`, so the prose cannot outlive a
17//! change to the rule. -->
18//! **Which repository an issue is created in is decided by the item's own `repositories`
19//! field, under one rule.** Exactly one entry names the repository the issue is created in:
20//! a task issue is where a person finds the work from the repository it changes, and one
21//! filed in a board's nominated repository is invisible from every other. Zero entries, or
22//! two or more, name none, so a task's or a document's issue is created in the repository
23//! its parent project's issue lives in — read from the board, or from this process's own
24//! record of a project it created earlier in the same command — and a project's issue, or
25//! a task or document written with no parent, is created in the configured `repository:`.
26//! What that rule refuses, it refuses before `createIssue`, so no issue is half-created. An
27//! existing issue is never moved: the update path leaves the issue where it is and records
28//! the list in the metadata slot when it differs, so the read side's derivation and the
29//! creation rule agree by construction.
30//! <!-- llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate] -->
31//!
32//! **A document is an ordinary issue whose title begins [`DESIGN_TITLE_PREFIX`].** A
33//! board has no document type and nothing but issues to hold one in, so the title is the
34//! discriminator and it is the whole of it. The title this source *reports* is the one a
35//! person wrote, with the prefix taken off — the same way the metadata slot is taken off
36//! the body so `content` is what the person wrote — and writing a document puts the prefix
37//! back, so a round trip returns the title that went in.
38//!
39//! **Telling a document from a project from a task.** The design prefix is read **first**:
40//! a document is never a project and never a task, whatever sub-issues it has or does not
41//! have. Only then does the rest apply — a board issue is a project when *either* it has
42//! sub-issues *or* it carries [`ItemKind::METADATA_KEY`]; otherwise it is a task. A
43//! sub-issue is always a task, whatever it carries. The marker is sufficient and never
44//! necessary: it is what makes an *empty* project — the state a project copy passes
45//! through between creating the project and filing its first task — readable as a
46//! project, while the sub-issue arm lets a person author a project on the board by hand
47//! with no knowledge of this product's metadata at all. Reading the prefix later than the
48//! sub-issue rule would make a design issue with no sub-issues an empty project, which is
49//! exactly the state that rule exists to catch. Pull requests are neither a project nor a
50//! task nor a document and are ignored.
51//!
52//! **A task's comments are its issue's comments.** They are read off `Issue.comments` and
53//! written with `addComment`, `updateIssueComment` and `deleteIssueComment`, and a comment's
54//! id is GitHub's own node id for the `IssueComment`. Two things GitHub decides are refused
55//! rather than papered over: a board **draft** is not an issue and has no comments at all, so
56//! a comment call on one is refused rather than answered with an empty page; and GitHub signs
57//! every comment as the account the token belongs to, so a comment handed an author of its
58//! own is refused rather than posted under another name. GitHub's comment mutations take the
59//! comment's id and nothing else, so an edit or a delete first reads which issue that comment
60//! is on, and a comment on some other issue is one this task does not have.
61//!
62//! **Where an entity is, is a link.** Every project, task and document this source reports
63//! carries a [`Location::Url`] naming the issue's own web address — the same address the
64//! `url` field already reports, in the shape that says a reader can open it. That is the
65//! contrast the location contract exists for: a reader holding an entity from this source
66//! is handed something to link to and one holding an entity from a folder of Markdown is
67//! handed a path, and neither has to know which plugin answered. It does not replace or
68//! derive from `url`; that field goes on reporting what it always reported.
69//!
70//! **Where metadata lives.** Short typed things go to typed fields and native relations:
71//! status to the board's `Status` single-select and the issue's own state, the copy
72//! origin to a source-owned `onetaskgraph.origin` text field, and dependencies to
73//! `blockedBy` and to sub-issue links. Unbounded caller JSON goes in a trailing
74//! `<!-- onetaskgraph.metadata ... -->` comment at the end of the issue body — the same
75//! encoding `docs/metadata.md` settles for Linear, not a second one. A ProjectV2 text
76//! field is length-bounded and `shortDescription` is capped at 300 characters, which is
77//! why neither can hold a caller's own prose. Setting one caller key on its own — on a task,
78//! a project or a document alike — is one update of the issue body that changes that slot
79//! and not one byte outside it, and it is not sent at all when the key already holds the
80//! value. The link a copy records on an item it copied, `onetaskgraph.copies`, is small and
81//! is kept in that same slot, written by that same update.
82//!
83//! **A caller's value can also be projected onto a board text field**, so the board can filter
84//! on it: each [`GitHubProjectsConfig::metadata_fields`] entry names a text field, a top-level
85//! metadata key and a path inside its value. The slot stays the value's one home — every read
86//! reports metadata from it alone — and the field follows it on every write of an item, a
87//! create, an update and a copy alike: a string is written, and not sent again while the field
88//! holds it — an empty string included, which is written as itself; nothing there or `null`
89//! clears a field holding a value; any other
90//! JSON type is refused before any mutation. It rides the ordered [`graphql::UPDATE_FIELDS`]
91//! write beside `Status`, `Priority` and the origin, so it adds no request; a key set on its
92//! own moves its field first and the body last, as every write of an existing item does. A
93//! board without the field, or with a non-text field of that name, is refused before any
94//! mutation pointing at `sources fields`, whose [`GitHubProjectsSource::fields`] creates it as
95//! a text field and never changes a field that is there.
96//!
97// llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] This public module documentation is a required user-facing description; the loopback plugin tests and shared live journey drive StatusMapping resolution, both mutations, and observed read-back together.
98//! **Status.** `status_mapping` is per-instance configuration, in the shared grammar
99//! [`onetaskgraph_plugin_api::StatusMapping`] documents, from a status category to an
100//! option of the board's one `Status` field for a task and for a project: a bare name is
101//! the option for both kinds, `null` disables the category for both, and `{task, project}`
102//! names it per kind. A category the mapping does not mention keeps its shipped default for
103//! both kinds; one it mentions is exactly what it configures, so a per-kind object no longer
104//! gets the shipped default for the kind it leaves out. Two categories one kind would read
105//! back from one option are refused as the configuration is read, ignoring case, while one
106//! option may stand for different categories of the two kinds. Writes go by the kind of the
107//! item written: a status that kind has no option for, or whose option the board lacks, is
108//! refused before any mutation, naming the source, the kind, the category and the key
109//! `status_mapping.<category>.<kind>` — there is no fallback. `done` selects its mapped
110//! option and closes the issue as `COMPLETED`; `cancelled` selects its mapped option and
111//! closes it as `NOT_PLANNED`, for either kind. Every open category reopens a closed issue
112//! before selecting its option. Reads give a closed issue's reason precedence over its
113//! option, while an open issue's option decides its category through its own kind's
114//! mapping, and an option that mapping does not name reads as `unknown` under its own name.
115//! The guarded [`GitHubProjectsSource::status_options`] and
116//! [`GitHubProjectsSource::fields`] operations are the one path here that calls
117//! `updateProjectV2Field`: GitHub replaces the whole option list, so they preserve every
118//! existing option id and verify the field and item assignments immediately afterwards.
119//! They ask for both kinds' options, counting a terminal category's mapped option as
120//! configured because a terminal write refuses without it. No ordinary source read or
121//! write calls that mutation, whose
122//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
123//! and a mistake destroys every item's status. A status this board cannot represent is a
124//! refusal naming the status and the instance instead.
125//!
126//! `unknown` has no shipped option because this source cannot preserve an open-ended
127//! status word: it writes an existing board option and never
128//! creates an option. An operator may map `unknown` to one existing option, in which case
129//! every unknown word lands on that option and reads back as `unknown` under the option's
130//! name. This differs from `local-md`, which writes and reads the original word itself.
131//!
132//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
133//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
134//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
135//! finished tasks were only moved to a "Done" column would read 0% complete forever.
136// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
137//!
138//! # What this source declares, field by field
139//!
140//! One verdict per field of [`Capabilities`], and what `Native` means when this source
141//! says it. *Proven* means a shared journey drives it against the real
142//! binary over this source's own row in `crates/onetaskgraph-e2e-support/src/fixtures.rs`, and
143//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
144//! [`capabilities`](TaskSource::capabilities) from parting.
145//!
146//! | Field | Verdict |
147//! | --- | --- |
148//! | `projects` | **Supported and proven,** and the one predicate here that is pushed down rather than applied in process: a task's project is the issue it is a sub-issue of, so a listing scoped to one *asks that issue* for its own sub-issues. This is the field that was declared and then not applied, which silently returned another project's tasks. |
149//! | `documents` | **Supported and proven.** A board holds issues, so a document is one: the issue whose title begins [`DESIGN_TITLE_PREFIX`]. Reads, filters and paging answer on exactly the terms a task read does, and a write puts the prefix back. |
150//! | `comments` | **Supported and proven,** over the task issue's own comment connection, oldest first and paged by GitHub's own cursor; added, edited and removed through GitHub's comment mutations, paced as every other mutation is. A draft item has no comments on GitHub and is refused, and so is an author, because GitHub records the signed-in account as every comment's author. |
151//! | `assets` | **Supported and native.** Image references travel as GitHub user attachments in the repository the task or document issue lives in. Uploads are authenticated and verified before the issue body is written. |
152//! | `priority` | **Supported and proven** by an instance configured with `priority_mapping`, and declared unsupported by one without it, which reports every task's priority as `none` and sends exactly the requests it sent before priorities existed. The priority is the board's single-select `Priority` field: no value is `none`, a mapped option is its level, matched case-insensitively, and an option the mapping does not name fails the read of that task, naming the option. A write selects the mapped option, or clears the value for `none`; a board without the field or the option is refused, pointing at `sources fields`, which is the one thing that creates either. |
153//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
154//! | `filter_by_comment_activity` | **Supported, and exact** for comments created and for comments edited at or after `commented_since`, in every repository — of any owner — the board's items live in. Applied by asking a narrower question rather than by reading the board: GitHub's issue search scoped by `project:<owner>/<number>` alone, with an `updated:>=` qualifier, names the candidates, and each candidate's own comments confirm it, so `ProjectV2.items` is never read. That rests on GitHub moving an issue's `updatedAt` when a comment on it is added **or edited**, which the credentialed journey `an_edited_comment_moves_its_issue_and_is_selected_since` re-takes on every run of this lane. The search is an index that lags a write — the credentialed lane has watched it miss a newly commented issue for thirty seconds — so an issue this process itself commented on in the same command is a candidate whatever the search says, read by its own node if the search did not name it, and has its comments read rather than being ruled out by an `updatedAt` from before the comment. A comment another process wrote is found only once the index has it, so a caller asking again from its last instant should overlap the two generously. |
155//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
156//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
157//! | `filter_by_status` | **Supported and proven,** over the board's `Status` option and the issue's open or closed state, through this instance's own `status_mapping` for the item's kind — a task query by the task half, a project query by the project half, `unknown` included. |
158//! | `filter_by_metadata` | **Supported, and asked of GitHub** — through the body and never through a projected `metadata_fields` text field, which is the board's to filter on and not this source's to read. A query naming metadata values is one board-scoped issue search with each value a quoted phrase `in:body` — GitHub's index covers the metadata comment at the end of the body, which is where caller metadata lives — and every candidate is confirmed against its own parsed metadata comment, so only an item holding that string at that key and path is returned. **A value with no letter or digit is refused** — the empty string, whitespace or punctuation alone — before any request, as a `SourceError::Refused` (wire kind `refused`) naming the value: GitHub's index holds words, so no bounded query can find such a value, and this source neither reads the whole board for it nor answers it as empty. |
159//! | `filter_by_origin` | **Supported, and asked of GitHub without enumerating the board.** The union of three reads, each confirmed by an exact match against the item's own origin field: the board's field filter over the `onetaskgraph.origin` text field, the issue search for the id as a phrase in the body where a write of this release mirrors it, and this process's own writes. See *Where a read-after-write guarantee comes from* for the window the three leave. |
160//! | `reconcile_status` | **Supported,** and proven by `onetaskgraph-github-projects-e2e`'s `status_reconcile.rs`: the board's own `ProjectV2.items(query:)` filter, `is:closed`, read page by page and never the unfiltered items, each candidate confirmed from its own item node — a closed task whose card is not at the option the mapping gives its closure's category. |
161//! | `complete_by_issue` | **Supported,** and proven by `status_complete.rs` there and in the engine's suite: each issue a reference names read in one aliased request per fifty — its repository, its number, its first page of board memberships with each card's `Status` read by name — and nothing else of the board. |
162//! | `search_title` | **Supported, and asked of GitHub for a task,** over `Issue.title`: a task query's text is one board-scoped issue search for it as a phrase `in:title`, every candidate confirmed by the case-insensitive substring rule. GitHub matches whole words, so a task holding the text only inside a longer word is not returned — a narrowing this source declares rather than hides. **A text with no letter or digit that is not blank is refused** — `--` for one — before any request, as the same `refused` error naming the text, for the reason a metadata value like it is; a blank text is not refused, and keeps the board read it always had, confirmed by the same substring rule. A project query's text, and a document query's text when the query is scoped to no project, is that same board-scoped search for the same phrase in the same fields, refused on the same terms, every candidate confirmed by its kind and by the same substring rule, so it narrows exactly as a task's does; a document query scoped to one project sends no search, reads that project's sub-issues and confirms its text over them by the substring rule alone, so it is neither narrowed to whole words nor refused for a text with no letter or digit. A board draft is not an issue, so no text search lists one, a draft titled as a document included. |
163//! | `search_content` | **Supported,** on the same terms, `in:body`, over the visible body — the trailing metadata comment is not part of what the substring rule confirms. |
164//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
165//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
166//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
167//!
168//! Image writes use `POST https://uploads.github.com/user-attachments/assets` with `name`,
169//! `content_type` and the issue repository's numeric `repository_id` as query parameters,
170//! the source token as `Authorization: Bearer`, the image content type as `Content-Type`,
171//! and the raw bytes as the request body. The numeric repository id is read at most once
172//! per repository per source instance. A loopback API endpoint also selects the loopback
173//! uploads host; production needs no assets repository, commits or extra configuration.
174//! Every returned JSON `url` is read with the same token before a body is written: only
175//! a 2xx verification succeeds. A refusal names the asset, URL and HTTP status; an
176//! anonymous 404 does not invalidate an authenticated 200. An upload refusal names the
177//! asset and HTTP status and says the token type may not be accepted by the upload
178//! endpoint. A classic PAT was measured accepted; no acceptance claim is made about
179//! fine-grained PATs or OAuth tokens.
180//! The body keeps the authored alt text while `./<name>` becomes the attachment URL,
181//! and `onetaskgraph.assets` records `{sha256, url}` by name. A matching destination
182//! SHA-256 reuses its URL without an upload or verifying read; changed bytes are uploaded
183//! and verified afresh, in the repository the issue already lives in on an update.
184//! An uploaded image renders for the viewers GitHub lets read it, like the issue text beside it.
185//! Uploads take the same mutation spacing as content writes. Both uploads and verification
186//! reads wait out classified rate limits within the configured per-call retry budget,
187//! on the supplied clock; verifying reads take no mutation slot.
188//!
189//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
190//! source has documents and that its tasks have comments, both of which hold — and the three
191//! facts behind the uniform `Native` on the
192//! predicates beside it are recorded below rather than re-derived, because a reader who
193//! takes `Native` to mean *the remote service filters* will read that uniformity as a
194//! lie.
195//!
196//! First, the plugin contract defines `Support::Native` as *the source applies this
197//! predicate itself*, and says nothing about where it applies it. What the declaration
198//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
199//! — so that the engine may push it down and apply nothing of its own.
200//!
201//! Second, this source can keep that promise for every predicate at no additional API
202//! cost, because whichever of the reads below answers a query has already read every
203//! candidate that query will return before it filters anything. Filtering those items is
204//! in-process work over data already in hand.
205//!
206//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
207//! applied in process over what that question returned. A project filter has a relationship — a
208//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
209//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
210//! a search for metadata values, is the board-scoped issue search carrying the text and each
211//! value as quoted phrases; an origin is the board's own field filter over its origin field
212//! beside the same search for the id. **The text search narrows, and that is this source's
213//! declared semantics:** GitHub matches whole words where the substring rule this source and
214//! the local Markdown source confirm with would match inside one, so an item holding the text
215//! only inside a longer word is never a candidate. Every item returned does contain the text.
216//! A project query's text, and a document query's scoped to no project, is that same search
217//! and narrows on the same terms, its candidates confirmed by their kind as well.
218//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
219//! so those three are applied in process over the candidates, and a query carrying none of
220//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
221//! engine compensate for work this source has already done, and declaring `projects` native
222//! while ignoring the filter (which this source once did) silently returns another project's
223//! tasks, because the engine trusts the declaration and applies nothing locally.
224//!
225//! # The three ways this source reaches an item, and what each costs
226//!
227//! A board read is charged for what its *nested* connections could return rather than for
228//! what was asked, so one whole-board read costs the same whether the question was about
229//! one project or about all of them. That is why a question about one project is never
230//! answered by reading the board:
231//!
232//! | The question | What is sent | What it costs |
233//! | --- | --- | --- |
234//! | one item, by its own id | [`graphql::ISSUE`] — `node(id:)`, carrying the field definitions of the boards it sits on and the far ends of its `blockedBy`, which is what a write of it needs — and, when that node is a board draft, [`graphql::DRAFT`] — the draft and the one board item it is | the item |
235//! | one task with its first page of comments, for `task show` and a comment listing | [`graphql::ISSUE_DETAIL`] — the same `node(id:)` read with the issue's `comments` | the item and a page of its comments |
236//! | several tasks with their comments, for `task show-many` | [`graphql::ISSUE_DETAILS`] — [`DETAIL_BATCH`] aliased `node(id:)` fields per request | each item and a page of its comments |
237//! | the board's own id and field definitions, for a write whose item does not carry them | [`graphql::BOARD_FIELDS`] — the board's `id` and `fields`, and no `items` — or, for a create that needs the repository's id too, [`graphql::CREATION_CONTEXT`], both in one request | the board's fields |
238//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
239//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
240//! | which projects hold a text, or which documents do when no project narrows the question | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text as one quoted phrase, `in:title`, `in:body` or both, as a task's text is sent — walked to its end in pages of twenty | the issues that match |
241//! | which tasks were commented on since an instant | [`graphql::SEARCH_ISSUES`] — the same board-scoped search with an `updated:>=` qualifier — then [`graphql::ISSUE_COMMENTS`] for each candidate it names | the issues updated since, and their comments |
242//! | which tasks hold a text, or a metadata value | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text and each value as quoted phrases, `in:title`, `in:body` or both, and an `updated:>=` qualifier too when comment activity is asked for — in pages of twenty, only as many as the caller's rows need | the issues that match |
243//! | which tasks were copied from one origin | [`graphql::ORIGIN_LOOKUP`] — the board's own `items` under its field filter on the origin field, and the same board-scoped search for the id `in:body`, in one request, each paged at three | the carriers of that origin, which is one item |
244//! | every task, every document, every label, when nothing above narrows the question | [`graphql::BOARD`] — the board's own `items` — **and** [`graphql::SEARCH_ISSUES`], because neither enumeration of a board is complete alone; see [`GitHubProjectsSource::board`] | the board, twice over |
245//! | which board item one issue is, past the page that came with it | [`graphql::ISSUE_BOARD_ITEMS`] — that issue's own `projectItems` | one issue's memberships |
246//!
247//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
248//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
249//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
250//! equal to declared points equal to the row. They include the origin lookup and the
251//! field/repository discovery a create needs. A bound re-copy changes status, priority,
252//! content and metadata; comment recount means a subsequent detail read. Each request here
253//! costs one declared point. A membership beyond the embedded page can additionally require
254//! the one-point membership recovery described above. A bound re-copy of a task filed under a
255//! project adds one read, the engine confirming that project's link by its own id once per
256//! command; and the same-source far ends a write newly names — those that do not already block
257//! the item, whose own read answered for them — are read together by their own ids,
258//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
259//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
260//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
261//!
262//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
263//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
264//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
265//! holds both halves.
266//!
267//! **An existing item is written body last.** A bound re-copy and a `task update` send its
268//! board fields first — the `Status` option and the `Priority` together, in one request — then
269//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
270//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
271//! undoing an earlier field when a later one fails, so that order is what makes a write
272//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
273//! the one piece of metadata written before the body, an origin a copy re-points, is put back
274//! when a later write is refused — and when putting it back is refused too, the write's own
275//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
276//! of those writes in turn, whole and as one aliased field failing after the one before it.
277//!
278//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
279//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
280//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
281//!
282//! - **A board is accepted at creation but its item is not answered, so a create still files
283//!   the issue itself: a new copy is 5 requests, and 4 with `--create`.**
284//!   `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
285//!   Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
286//!   The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
287//!   against a real board on 2026-10-01 with a create sending the board there and reading the
288//!   item off the payload's `Issue.projectItems`: every one of its four creates answered with
289//!   no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
290//!   refused "Content already exists in this project" — GitHub had filed the issue after
291//!   answering, and refuses a second filing rather than answering with the item it holds. A
292//!   create therefore sends no `projectV2Ids` and files the issue with
293//!   `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
294//!   real is the read before it: the board's fields and the repository's id together, in
295//!   [`graphql::CREATION_CONTEXT`], at the point the repository is known.
296//! - **A comment still reads its target first, so a comment is 2 requests.**
297//!   `AddCommentInput.subjectId: ID!` is declared there with
298//!   `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
299//!   "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
300//!   project's issue, a document's issue, an issue on no board of this source and a pull
301//!   request all are: GitHub writes the comment, so there is no refusal to map into "that is
302//!   not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
303//!   refuses those by name.
304//!
305//! | Verb | Requests / points | Documents |
306//! | --- | --- | --- |
307//! | new copy | 4 | ORIGIN_LOOKUPS (the origin, the board's fields and the repository's node and numeric ids together, a page of one carrier for each origin), CREATE_ISSUE filing the issue on the board and under its project, CREATED_BOARD_ITEMS once GitHub has added it, WRITE_FIELD_VALUES |
308//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, CREATED_BOARD_ITEMS, WRITE_FIELD_VALUES: the new copy with no origin to ask about |
309//! | bound copy | 3 | ISSUE (with the board's fields and the issue's `blockedBy`, so no BOARD_FIELDS or ISSUE_DEPENDENCIES), UPDATE_FIELDS when a board field differs, then UPDATE_ISSUE last carrying only the title, body or state that differs — none, when nothing does |
310//! | bound copy, filed under a project | 4 | the bound copy's three, and one ISSUE of the destination project its link names, read once per command |
311//! | bound copy, newly naming n dependencies | + ceil(n / DETAIL_BATCH) + n | ISSUE_DETAILS for the far ends that do not already block the item, DETAIL_BATCH (24) to a request (one alone is ISSUE), then one ADD_BLOCKED_BY each; a far end already blocking it is answered by its own read and costs nothing |
312//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
313//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
314//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
315//! | copy --comments, n changed tasks | ceil(n / COPY_DETAIL_BATCH) | ISSUE_COPY_DETAILS: each task with its first page of comments and its `blockedBy`, COPY_DETAIL_BATCH (21) at a time; a task whose comments run past that page adds one ISSUE_DETAIL per further page, and a task skipped from its listing costs nothing beyond it |
316//! | recount | 1 | ISSUE_DETAIL |
317//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
318//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
319//! | content | 2 | ISSUE, UPDATE_ISSUE |
320//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
321//! | document metadata, `--expect` a version | 2 | ISSUE, then UPDATE_ISSUE only when the issue is still at that version — the precondition GitHub's own update cannot take — primed with what the read answered |
322//! | update | 3 | `task update` naming any of title, body, metadata, status and priority — all five included: ISSUE, UPDATE_FIELDS (the status option and the priority together), UPDATE_ISSUE (title, body with its metadata slot, and state) last |
323//! | record only | 1 | ISSUE |
324//!
325//! <!-- github-search-paging:start -->
326//! Board-scoped text, metadata, project-name and comment-activity searches send every
327//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
328//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
329//! needs rows. A page is never resized to the rows still needed: GitHub orders one
330//! search differently at different page sizes, so one fixed size makes a paged walk
331//! send exactly the requests one whole read sends, and the answer's order is the order
332//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
333//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
334//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
335//! returned and fetched pages: a limit is sliced from the pages it needs, and local
336//! confirmation can require more candidates than matching rows. Walking all pages
337//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
338//! cursor and how far into that page the last answer stopped, and resumes in the same
339//! process or a new one, without duplicates or gaps. It carries no rows: one process
340//! sends each page's search once, and a new process re-reads only the page it resumes
341//! in, then sends a further page once, never as a re-read, only when its limit still
342//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
343//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
344//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
345//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
346//! not required to include the original process's writes still omitted by the index.
347//! <!-- github-search-paging:end -->
348//!
349//! The board half of an issue — its board item's id, its `Status` option and this
350//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
351//! an item reached any of those ways resolves through the same
352//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
353//! same status, the same labels and the same qualified id. That connection comes back a
354//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
355//! on the page in hand and — only if that page reports more of the connection — in the
356//! last row's read of that one issue's memberships, resumed from the page's own cursor and
357//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
358//! report, which is what keeps an id naming another repository's issue from being answered
359//! as an item of this board; and because the page is where the search starts rather than
360//! where it ends, that answer is one about a connection read to exhaustion and never about
361//! an unread page. Nothing costs the extra read but an issue on more boards than a page
362//! holds: an issue this board really does not hold reports no next page, so its
363//! memberships are already exhausted where they arrived.
364//!
365//! **No document here selects the board's own `Labels` field, and nothing is lost by
366//! that.** An item's labels are read from its content alone, wherever that content is
367//! reached: the three documents above select `Issue.labels` on the fragment, and
368//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
369//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
370//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
371//! create one, and `ProjectV2FieldValue` — the whole of what
372//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
373//! it from the content, for every content type it exists on, and there is nothing it can
374//! hold that the content does not already say: for an `Issue` it *is* that issue's own
375//! labels, so selecting it beside them unions a set with itself.
376//!
377//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
378//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
379//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
380//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
381//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
382//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
383//! label set, and that set is the fixture issue's own, by
384//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
385//! item whose content is a draft reports an empty set, by
386//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
387//! selection is held over [`graphql::DOCUMENTS`] by
388//! `no_document_selects_the_boards_own_labels_field`.
389//!
390//! The whole-board row is still the board's own item connection, and deliberately: a
391//! **draft** board item is not an issue, so no search can list one, and the reads that have
392//! to answer for the whole board are the ones whose cost is the board's size anyway.
393//!
394//! **A question about one item this source already names by id never lists the board.**
395//! Whether that item is on this board, and what its board fields are, is answered by reading
396//! that item — its own `Issue.projectItems`, walked to exhaustion by
397//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
398//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
399//! holds. That covers a write's destination, the project a new item is filed under, a
400//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
401//! and the delete that takes back an item a copy made. What such a write needs of the board
402//! and the item does not carry — the board's id, the `Status` and origin field definitions —
403//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
404//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
405//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
406//! minutes. Scanning this host's 842-item board has refused a document copy and an update
407//! even though the items' own reads named that board. A scan there gives the wrong answer
408//! as well as paying for every page. So a `board.items` lookup does not belong on any of
409//! those paths.
410//!
411//! **What a read may return is capped too, and that cap is on the document rather than on
412//! the board.** GitHub limits the number of nodes **one query may return** to
413//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
414//! an error naming the connection the count crossed at, not a slow or a partial result.
415//! Every board this source reads is refused the same way, so no board is too big for these
416//! documents and none is small enough to save one that is over.
417//!
418//! The count is arithmetic over the document's own text: each connection contributes the
419//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
420//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
421//! restate them — `github-graphql-node-count` implements them, and
422//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
423//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
424//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
425//! same text on every run and fails naming any that reaches the limit — so a connection
426//! added to a shared fragment is caught there rather than by GitHub.
427//!
428//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
429//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
430//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
431//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
432//! effectively squared there, which is why it is the one the limit is most sensitive to.
433//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
434//! page of memberships misses is recovered by one further read rather than refused, so it
435//! buys a bound every read pays for at the price of a request only a multi-board issue
436//! pays.
437//!
438//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
439//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
440//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
441//! `cost` is rate-limit points, metered per hour across everything one credential does; it
442//! is what the two limiters [`Limiter`] tells apart meter, and a document under
443//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
444//! that second number, and `tests/point_cost.rs` pins every document in
445//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
446//! one under, the pin itself is the check. The credentialed lane reconciles both figures
447//! against GitHub's own, off a probe it already sends.
448//!
449//! **What is pinned that way is a per-document price and never a session's.** The record in
450//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
451//! **requests** and **worst-case nodes** — and neither is points. What one whole session
452//! consumes of the hourly point allowance is observable only from a credentialed run's own
453//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
454//! and what `tests/live.rs` prints at the end of every run.
455//!
456//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
457//!
458//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
459//! enumerations of a board can supply one alone.** Resolving a node id is strongly
460//! consistent, so a read by id and a project's own sub-issues are already current. The
461//! other two are not, and they are behind by different amounts and in different directions:
462//!
463//! - GitHub's **issue search** is an index and answers a write made moments ago with the
464//!   value from before it — usually for a second or two.
465//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
466//!   on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
467//!   content withheld, absent, with the connection walked to its own `hasNextPage: false` —
468//!   for *minutes*, while `Issue.projectItems` names the same membership at once.
469//!
470//! That second one is a measurement rather than a caution. This repository's own
471//! credentialed journey writes a project and waits for the board to report it, then writes a
472//! task and waits for the same thing seconds later on the same board: the project wait is
473//! answered through the search and converged in two or three attempts in each of three runs,
474//! and the task wait is answered through `ProjectV2.items` and converged in none of them
475//! inside thirty. Separately, an item added to a second and larger board was read back by
476//! `Issue.projectItems` on that board's own id while every one of that connection's nine
477//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
478//! through the lagging one alone is what had a board read deny an issue that had certainly
479//! landed on it.
480//!
481//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
482//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
483//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
484//! search reports what the projection is behind on. What closes the last
485//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
486//! source answers is completed with what this process itself wrote, so an item created
487//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
488//! nothing is written down, and the record dies with the process. **A wait that has to
489//! observe GitHub's own data cannot be answered from that record** — which is why the
490//! credentialed journey asks through a source built afresh, and why the union above rather
491//! than a longer wait is what makes such a wait converge.
492//!
493//! **A narrowed read is the same bargain, stated for each of the three predicates it
494//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
495//! than walking the board, and every such answer is completed with what this process wrote —
496//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
497//! each filtered by the same predicates as the rest — so an item this command wrote a moment
498//! ago is returned by a query that matches it whether or not the index has caught up. An item
499//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
500//! consistent. What is left is stated rather than papered over:
501//!
502//! | Read | Finds | Behind by |
503//! | --- | --- | --- |
504//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
505//! | origin, first read | the board's field filter over the origin field — every carrier, whichever release wrote it | what `ProjectV2.items` is behind on, which the measurements above put in minutes |
506//! | origin, second read | the issue search for the id in the body, where a write of this release mirrors it | a second or two, as any search |
507//! | origin, third read | this process's own writes | nothing |
508//!
509//! So an origin carrier another process added within the last second or two, before either
510//! index has it, can be missing from an origin query, and one written by the release before
511//! this one — its origin in the field alone — can be missing for as long as the board's own
512//! item connection is behind on it. A copy that must not duplicate its own earlier write
513//! relies on the link it records, not on either index. **A board draft is not an issue**, so
514//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
515//! lists one, the origin lookup drops any the board's own field filter names, and one this
516//! process wrote is not added back either.
517//!
518//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
519//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
520//! body's metadata slot, so the issue search can find it in seconds. The field is
521//! authoritative: this source reads an item's origin from the field alone, so a slot that
522//! disagrees with it, or holds one where the field holds none, is never read as a second
523//! origin — and the release before this one reads the slot, drops that key's copy for the
524//! field's, and sees the same one origin.
525//!
526//! Filtering happens before paging, so a page of a filtered result is a page of the
527//! survivors rather than the survivors of a page. Label matching and the substring rule a
528//! text candidate is confirmed by answer the same question the same way the local Markdown
529//! source's do; which candidates a text search has to confirm is GitHub's word match, which
530//! is the one place the two sources can answer the same text differently.
531//!
532//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
533//! has one source, `capabilities`, and the note above is the reasoning behind it rather
534//! than a second copy of it: without the three facts recorded here a reader takes the
535//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
536//! crate's own capabilities test, which pins every field of it against a fully spelled-out
537//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
538//! fails to compile there rather than going unasserted. -->
539//! The fixture-server tests above run wherever this crate is selected; the credentialed
540//! lane runs in the same required check, beside them, and can fail it — it verifies the
541//! current schema, then drives every field of the table above against the real board. It builds its own fixture there — two projects, one task filed under each,
542//! one filed under neither, a label on one of the three and a closed status on another —
543//! because that shape is what tells an honoured predicate from an ignored one: a board
544//! holding a single project answers a project filter the same way whether or not this
545//! source applies it, which is exactly how the defect above went unseen.
546//!
547//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
548//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
549//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
550//! session or request, independently of the live demand. Fix that variable in the machine's
551//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
552//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
553//! policy crate; absent credentials or nominations otherwise skip.
554//!
555//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
556//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
557//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
558//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
559//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
560//! after its run reads back as `completed`, immediately before the listed-artifact batch.
561//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
562//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
563//! and the board's `onetaskgraph.origin` field untouched.
564//!
565//! # What a session of requests costs, and where the report is
566//!
567//! This source records **every** request it sends into [`accounting::Accounting`], at
568//! `send_once` — the one place a request leaves this crate, which is why a read path added
569//! later is counted without anybody remembering to count it. That is the whole of what this
570//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
571//! session's spend is arrived at, and what it deliberately does not know are set out.
572//!
573//! What one whole session of the live journey costs, counted that way against this crate's
574//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
575//! reduction it came out of, and with what it does and does not say about rate-limit points.
576//!
577//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
578//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
579//! code path — no environment variable, no feature, no build configuration — because an
580//! instrument nobody switches on measures nothing, and
581//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
582//! source's counts the whole session rather than this source's share. The credentialed lane
583//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
584//! passed or failed.
585//!
586//! **A live session refuses to start unless the account can afford it.** Before it does any
587//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
588//! GitHub documents as not counting against the REST rate limit and which answers both of
589//! its budgets at once — and starts only if, for each of them, what remains minus this
590//! session's estimated cost is still at least
591//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
592//! allowance. A session that cannot **declines**: it did not run, so it is
593//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
594//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
595//! waiting for the budget to come back. The estimate is derived offline from
596//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
597//! which is also where the published rule that model rests on is cited; the accounting
598//! above records the gate's own read like any other request, and
599//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
600//!
601//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
602//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
603//! text, which is what lets it run on every platform and on a pull request from a fork with
604//! no credential — and that is what actually stops a regression merging. But an offline
605//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
606//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
607//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
608//! number of nodes this query may return"* and whose `cost` is what that document would
609//! spend, both for a document **without executing it**, and the lane asks it for every query
610//! document this source sends, under the largest bindings this source sends, and fails when
611//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
612//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
613//! one; the offline pins still cover it. It records what those calls reported about the
614//! account's own allowance, because whether asking is free is a thing to observe rather than
615//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
616//! and `cost` is metered against an hourly allowance the accounting above reads off a
617//! credentialed run's own response headers.
618//!
619//! **GitHub has two rate limiters and this source is refused by both, so nothing here
620//! treats them as one thing.** The primary budget is the hourly allowance `gh api
621//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
622//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
623//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
624//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
625//! one part of.
626#![deny(missing_docs)]
627
628use std::collections::BTreeMap;
629use std::sync::{Arc, Mutex};
630use std::time::Duration;
631
632use chrono::{DateTime, Utc};
633use onetaskgraph_plugin_api::{
634    Capabilities, Classification, ClosedAs, Comment, CommentBody, CommentOrigin, Completion,
635    ContradictedItem, Coverage, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
636    DependencySupport, Designator, Direction, Document, DocumentQuery, DocumentVersionCoverage,
637    Health, HeldTask, IssueHolding, IssueRef, ItemKind, ItemWrite, Label, LabelFilter, Location,
638    MetadataKey, Metering, NativeId, NewComment, Page, PageRequest, Priority, Project,
639    ProjectFilter, ProjectQuery, ProjectVersionCoverage, Repository, SecretResolver, SharedClock,
640    SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusContradiction,
641    StatusMapping, Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate,
642    TaskUpdateOutcome, TextFields, TextQuery, UnmappedStatus, UpdatedField, VersionCoverage,
643    WaitCause, WriteJournal, WriteSupport, system_clock,
644};
645use reqwest::{Client, StatusCode, Url};
646use schemars::{Schema, schema_for};
647use secrecy::{ExposeSecret, SecretString};
648use serde::{Deserialize, Serialize};
649use serde_json::{Value, json};
650
651pub mod accounting;
652mod assets;
653pub mod change_token;
654mod expected;
655pub use expected::{CONTEXT_VERSION, WHOLE_MEMBERS, context_shapes};
656mod pacing;
657mod visibility;
658
659use accounting::Accounting;
660
661/// The registry name for this plugin.
662pub const KIND: &str = "github-projects";
663/// GitHub's maximum connection page size.
664pub const MAX_PAGE_SIZE: u32 = 100;
665/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
666/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
667/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
668pub const SEARCH_PAGE_SIZE: u32 = 20;
669/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
670/// its comments: the largest batch the node-count model prices at one point.
671///
672/// Each aliased item is resolved once, and what GitHub charges for it is the connections
673/// under it — its labels, its page of board memberships, the field values of each of those
674/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
675/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
676/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
677/// one point and fails if one item more would still be priced at one.
678pub const DETAIL_BATCH: usize = 24;
679/// How many issue references one [`graphql::ISSUES_BY_REFERENCE`] request reads.
680///
681/// Each reference asks GitHub for one connection — a page of its board memberships, each with
682/// its `Status` value read by name rather than through a connection of field values — so fifty
683/// come to fifty requests' worth of the aggregate GitHub divides by a hundred, which is its
684/// one-point minimum, and the batch a landing's closures are read in costs one point per fifty
685/// issues per source. `tests/point_cost.rs` prices the document at one point.
686pub const REFERENCE_BATCH: usize = 50;
687/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
688/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
689/// point.
690///
691/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
692/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
693/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
694/// if one item more would still be priced at one.
695pub const COPY_DETAIL_BATCH: usize = 21;
696/// How many mutations one aliased request of a copy's creates, closes and updates carries:
697/// [`graphql::CREATE_ISSUES`], [`graphql::CLOSE_ISSUES`] and [`graphql::UPDATE_ISSUES`].
698///
699/// Eleven is what GitHub was seen to accept in one request — the timing spike
700/// (`board-copy-speed-2026-10-18-spike-copy-timing-report`) sent eleven aliased mutations of four
701/// kinds, three `createIssue`, three `addProjectV2ItemById`, three field updates, an
702/// `addSubIssue` and an `addBlockedBy`, and every one ran — and no more was established.
703pub const MUTATION_BATCH: usize = 11;
704/// How many aliased writes one request of a copy's board field values and blocked-by edges
705/// carries: [`graphql::WRITE_FIELD_VALUES`], [`graphql::ADD_BLOCKED_BYS`] and, for a request
706/// holding both kinds, [`graphql::WRITE_FIELDS_AND_EDGES`].
707///
708/// Nineteen is the most aliases the feasibility spike
709/// (`board-copy-gaps-2026-10-24-spike-feasibility-report`) sent in one mutation request and
710/// saw GitHub price at the one point every mutation request costs; no larger request was
711/// established, so a copy holding more is split.
712pub const WRITE_BATCH: usize = 19;
713/// How many issues one [`graphql::DELETE_ISSUES`] request deletes: one request of eleven
714/// aliased `deleteIssue` met `RESOURCE_LIMITS_EXCEEDED` from the eighth alias on, and batches
715/// of five succeeded (the feasibility spike's undo-batching run).
716pub const DELETE_BATCH: usize = 5;
717/// How many origins one [`graphql::ORIGIN_LOOKUPS`] request asks about: the nine records of a
718/// plan and the project's document, and no more, since each carries a page of the board's field
719/// filter and one of the issue search.
720pub const ORIGIN_BATCH: usize = 10;
721
722/// The most nodes any one document this source sends may be asked to return.
723///
724/// GitHub's own published per-query ceiling, taken from
725/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
726/// workspace cannot hold a stale copy of somebody else's number. A query above it is
727/// **refused before it is executed**, whoever is asking and whatever board they are
728/// asking about — so this is a bound on the documents rather than a budget that runs out.
729///
730/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
731/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
732/// everything the credential does — two numbers against two limits, and this constant
733/// bounds only the first. The second is computed offline too, per document:
734/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
735/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
736/// lane. There is no constant like this one to hold a price under, because points are an
737/// hourly allowance rather than a per-call bound.
738///
739/// Neither is a session's price. What `session-cost.md` records of a whole session is its
740/// **requests** and its **worst-case nodes**; what a whole session spends in points is
741/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
742/// [`accounting`]. The module section on the three ways this source reaches an item says how
743/// the count is arrived at, and which of the page sizes below decide it.
744pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
745
746/// Nested connection size for the connections that hang off one item.
747///
748/// It multiplies through every document that reaches an item under a page — the count
749/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
750/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
751/// every document under these constants and fails naming any that reaches the limit, so
752/// raising this is caught there rather than by GitHub.
753const NESTED_PAGE_SIZE: u32 = 50;
754/// How many of one issue's board memberships are read when an issue is reached directly.
755///
756/// An issue reached through a search or through its own node id carries its board half in
757/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
758/// under a page of issues, so every point of it multiplies through the whole document and
759/// is paid for whether or not any issue is on a second board — which is why it is
760/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
761///
762/// **Three, because what a page misses is now recovered rather than refused**, and the
763/// recovery is what the value is chosen against. An issue whose entry for this board sits
764/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
765/// that page's own cursor — so the value trades a bound every read pays for a request only
766/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
767/// boards would pay that request *per issue*, which is order N against the one page per
768/// hundred issues a read costs today. At three it is only reached by an issue on four or
769/// more boards at once, which keeps the recovery path exceptional rather than routine for
770/// a plausible deployment.
771const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
772/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
773/// its two connections for.
774///
775/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
776/// second is a duplicate a copy already takes the first of. Both connections are walked to
777/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
778/// never what the answer is. It is small because every point of it is paid on every lookup,
779/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
780/// worst-case nodes than the one whole-board read they replaced.
781const ORIGIN_PAGE_SIZE: u32 = 3;
782/// How many carriers of each origin the batched [`graphql::ORIGIN_LOOKUPS`] asks each of its two
783/// connections for: one, the one carrier an origin names.
784///
785/// A second carrier still shows: the page then says there is more, and that origin is left to
786/// its own [`graphql::ORIGIN_LOOKUP`], which walks every page at `ORIGIN_PAGE_SIZE` — so a
787/// duplicate is found exactly as before, by the lookup that reads all of them. The feasibility
788/// spike (`board-copy-gaps-2026-10-24-spike-feasibility-report`) priced the batch of ten
789/// origins at two points with a first page of three and at one with a first page of one.
790const ORIGIN_LOOKUPS_PAGE_SIZE: u32 = 1;
791
792/// How many of one project's sub-issues one [`graphql::SUB_ISSUES`] page asks for: the most
793/// one point buys, since every row carries six connections GitHub charges for once per row.
794/// `session-cost.md` derives it; `tests/point_cost.rs` prices the document at one point and
795/// fails if one row more would still be priced at one.
796pub const MEMBER_PAGE_SIZE: u32 = 23;
797
798pub use github_graphql_node_count::{NodeCountError, Variables};
799
800/// The largest value this source can bind to each page-size variable its documents name.
801///
802/// Every `first:` in [`graphql`] reads one of these six, and each is capped at the
803/// constant above it wherever a caller's own limit could reach it — `$first` at
804/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
805/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`, `$lookupFirst` at
806/// `ORIGIN_LOOKUPS_PAGE_SIZE`, `$memberFirst` at [`MEMBER_PAGE_SIZE`]. So this is the worst case a caller can drive this source to,
807/// not one configuration of it, which is what makes a bound computed under it a bound on
808/// every read.
809pub fn largest_page_sizes() -> Variables {
810    Variables::from([
811        ("first".to_owned(), MAX_PAGE_SIZE),
812        ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
813        ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
814        ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
815        ("lookupFirst".to_owned(), ORIGIN_LOOKUPS_PAGE_SIZE),
816        ("memberFirst".to_owned(), MEMBER_PAGE_SIZE),
817    ])
818}
819
820/// The most nodes `document` could be asked to return, by GitHub's published rules.
821///
822/// Computed offline from the document's own text under [`largest_page_sizes`] — no
823/// network, no credential and no schema — by
824/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
825/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
826/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
827///
828/// # Errors
829///
830/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
831/// no single operation, or binds a page size this source does not name — each of which is
832/// a defect in the document rather than a number.
833pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
834    node_count(document, &largest_page_sizes())
835}
836
837/// The most rate-limit points one call of `document` could spend, by GitHub's published
838/// rules.
839///
840/// Computed offline from the document's own text under [`largest_page_sizes`] — no
841/// network, no credential and no schema — by
842/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
843/// This is `cost`, metered **per hour** against the allowance one credential shares across
844/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
845/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
846/// under, so what `tests/point_cost.rs` does with it is pin every document in
847/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
848/// figures against GitHub's own reported `cost`.
849///
850/// # Errors
851///
852/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
853/// no single operation, or binds a page size this source does not name — each of which is
854/// a defect in the document rather than a number.
855pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
856    github_graphql_node_count::point_cost(document, &largest_page_sizes())
857}
858
859/// The most nodes `document` could be asked to return under `variables`.
860///
861/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
862/// [`accounting`] is this under the bindings one request really sent — one spelling of the
863/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
864/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
865///
866/// # Errors
867///
868/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
869/// no single operation, or binds a page size `variables` does not name.
870pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
871    github_graphql_node_count::node_count(document, variables)
872}
873
874/// The issue-title prefix that makes a board issue a document.
875///
876/// A GitHub Projects board has no document type — it holds issues — so the discriminator
877/// is the title, and this is the whole of it: an issue whose title begins with these bytes
878/// is a document and every other issue is the task or project the sub-issue rule makes it.
879///
880/// It is spelled **once**, here, and read rather than restated everywhere else — including
881/// by the shared journeys, which take it from this constant so a board fixture cannot
882/// drift from what this source reads. `docs/metadata.md` records the two consequences that
883/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
884/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
885/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
886pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
887
888/// Exact GraphQL query documents issued by this plugin.
889///
890/// Keeping the production documents here lets the pinned-schema test validate the same
891/// bytes that are sent to GitHub, rather than a test-only copy which could drift
892/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
893/// field, and its guarded caller always supplies the complete existing option set with ids.
894pub mod graphql {
895    /// The board half of one item: the field values every document here reads it from.
896    ///
897    /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
898    /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
899    /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
900    /// *the same value*, because
901    /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
902    /// one path. Three spellings of it is what would drift, so there is one.
903    ///
904    /// The `Status` option and this source's own origin text field are the whole of it. It
905    /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
906    /// content, so it holds nothing the content's own `labels` do not already say, and it
907    /// would sit a label connection two page sizes deep.
908    macro_rules! board_item_values {
909        () => {
910            r#"fieldValues(first:$nestedFirst){nodes{
911          ... on ProjectV2ItemFieldSingleSelectValue{name field{
912            ... on ProjectV2SingleSelectField{id name options{id name}}
913          }}
914          ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
915        }pageInfo{hasNextPage}}"#
916        };
917    }
918
919    /// The variable every document reading an issue declares for the relationship events its
920    /// change token counts, with its value as the default, so no caller binds it.
921    ///
922    /// A variable rather than a literal list because every argument a production document
923    /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
924    /// its default to the pinned enum. Its default is the six event types GitHub records a
925    /// relationship added or removed with: a dependency on either end, as `blockedBy` and
926    /// `blocking`, so one a person makes from the far end moves this end's count too; and a
927    /// project membership on the task, as its parent, which GitHub records there whichever
928    /// issue the move was made from. A sub-issue's events on its parent are not counted: a
929    /// project carries no task token, and a copy writes nothing of a task its own sub-issues
930    /// decide. [`super::change_token`] says why the count is in the token.
931    macro_rules! relationship_events_variable {
932        () => {
933            ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
934        };
935    }
936
937    /// Everything this source reads about one issue, wherever it reaches that issue.
938    ///
939    /// A macro rather than a constant so the three documents below can `concat!` it: one
940    /// spelling of these fields is what makes an issue read through the board-scoped
941    /// search, through its own node id, and through its project's sub-issue relationship
942    /// resolve to *the same* item, which is the whole of what
943    /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
944    ///
945    /// `projectItems` is what carries the board half of an issue: the board item's own id
946    /// and the [`board_item_values!`] above — the `Status` option and this source's origin
947    /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
948    /// issue rather than on the board, which is what makes the cost of a read proportional
949    /// to what was asked for instead of to the board's size.
950    ///
951    /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
952    /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
953    /// not on that page: a page here is where the search for the entry starts rather than
954    /// where it ends.
955    ///
956    /// It does **not** select the board's `Labels` field value, and that is the whole of
957    /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
958    /// a label connection there sits under `fieldValues` under `projectItems` under a page
959    /// of issues, spending `$nestedFirst` twice down one path, and took
960    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
961    /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
962    /// above, and that connection is where every label this source reports comes from. No
963    /// document in this module selects the board field any longer, [`BOARD`] included; the
964    /// module documentation records why nothing it could have held is lost.
965    macro_rules! board_issue {
966        () => {
967            concat!(
968                r#" fragment BoardIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount}
969      labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
970      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
971        "#,
972                board_item_values!(),
973                r#"}pageInfo{hasNextPage endCursor}}}"#
974            )
975        };
976    }
977
978    /// Every issue of one board, found by a search scoped to that board.
979    ///
980    /// This is how the projects a board holds are listed, and it selects no `items`
981    /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
982    /// container walked page by page, so nothing nested inside a board item is paid for.
983    /// Which of the issues it returns is a project is then read off `parent` — GitHub
984    /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
985    /// discriminator has to be applied to the field, which is a scalar on the issue and
986    /// costs nothing.
987    pub const SEARCH_ISSUES: &str = concat!(
988        r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
989        relationship_events_variable!(),
990        r#"){
991      search(query:$search,type:$type,first:$first,after:$after){
992        pageInfo{hasNextPage endCursor}
993        nodes{__typename ...BoardIssue}
994      }
995    }"#,
996        board_issue!()
997    );
998
999    /// What a dependency read selects of each far end: enough to say which kind of item it
1000    /// is, its body included for the kind marker.
1001    macro_rules! related_issue {
1002        () => {
1003            " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
1004        };
1005    }
1006
1007    /// One issue by its own node id, which is what a qualified id names here — with what a
1008    /// write of it needs and the issue does not carry in `board_issue!`: the field
1009    /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
1010    ///
1011    /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
1012    /// answers a write made moments ago with the value from before it, and resolving a node
1013    /// id does not.
1014    ///
1015    /// **Why those two ride here and not on the fragment.** A copy or an update of an item
1016    /// reads it by its own id, and with them that one read answers everything the write
1017    /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
1018    /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
1019    /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
1020    /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
1021    /// they sit under one item, and this read is still one point.
1022    pub const ISSUE: &str = concat!(
1023        r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1024        relationship_events_variable!(),
1025        r#"){
1026      node(id:$id){__typename ...BoardIssue ... on Issue{
1027        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1028          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1029          ... on ProjectV2Field{__typename id name dataType}
1030        }pageInfo{hasNextPage}}}}}
1031        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1032      }}
1033    }"#,
1034        board_issue!(),
1035        related_issue!()
1036    );
1037
1038    /// One document by its own node id, as [`ISSUE`] reads one, with the project it is filed
1039    /// under and that project's members beside it: the project issue whole, and a page of its
1040    /// sub-issues, each as [`SUB_ISSUES`] carries one but with no page of what blocks it.
1041    ///
1042    /// What a guarded write of a document's metadata reads, and what reading a document by its
1043    /// id is: a write to a board the boundary screens is held to who may read the document,
1044    /// which is the project's and every member's classification as much as its own, and here
1045    /// the one request that holds the version the write is compared against answers all of it.
1046    /// Its members carry no edges, which nothing classifying them reads.
1047    pub const DOCUMENT_IN_PROJECT: &str = concat!(
1048        r#"query($id:ID!,$first:Int!,$memberFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1049        relationship_events_variable!(),
1050        r#"){
1051      node(id:$id){__typename ...BoardIssue ... on Issue{
1052        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1053          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1054          ... on ProjectV2Field{__typename id name dataType}
1055        }pageInfo{hasNextPage}}}}}
1056        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1057        project:parent{__typename ...BoardIssue
1058          subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}
1059        }
1060      }}
1061    }"#,
1062        board_issue!(),
1063        related_issue!()
1064    );
1065
1066    /// One project's tasks: the sub-issues of the issue that project is, each with a page of
1067    /// what blocks it.
1068    ///
1069    /// Paged at `$memberFirst` — [`MEMBER_PAGE_SIZE`](super::MEMBER_PAGE_SIZE), the most rows
1070    /// one point buys — rather than at `$first`, because every row carries six connections and
1071    /// a hundred-row page cost six points to list a project of ten.
1072    ///
1073    /// The work this costs is the project's own size. Nothing about it grows as the board
1074    /// gains projects, or as those projects gain tasks.
1075    ///
1076    /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
1077    /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
1078    /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
1079    /// costs grow with its tasks rather than with the pages of them. Carried here, a task
1080    /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
1081    /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
1082    /// It adds a connection under each issue of the page — one rate-limit point per page,
1083    /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
1084    /// answer questions that never read an edge.
1085    ///
1086    /// **The project issue itself rides here too**, with the page of what blocks it, because
1087    /// the reader of a project's tasks reads the project next: a copy writing the project reads
1088    /// it and its edges before it writes them. Carried on the request that already names the
1089    /// project, the first page answers that read and the dependency read after it, at the
1090    /// state GitHub answered in this command, rather than spending one more read on an item
1091    /// the cache's token cannot vouch for whole. Each sub-issue is kept the same way, so a
1092    /// copy's read of a task this listing carried is answered by it rather than sent again.
1093    /// Only a copy's read-before-write takes them; every other read by id reads GitHub.
1094    pub const SUB_ISSUES: &str = concat!(
1095        r#"query($id:ID!,$memberFirst:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1096        relationship_events_variable!(),
1097        r#"){
1098      node(id:$id){__typename ...BoardIssue
1099        ... on Issue{blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1100          subIssues(first:$memberFirst,after:$after){
1101          pageInfo{hasNextPage endCursor}
1102          nodes{__typename ...BoardIssue ... on Issue{
1103            blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1104          }}
1105        }}}
1106    }"#,
1107        board_issue!(),
1108        related_issue!()
1109    );
1110
1111    /// What a read of the board's own `items` selects of each item's content.
1112    ///
1113    /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1114    /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1115    /// content by one spelling.
1116    macro_rules! board_item_content {
1117        () => {
1118            r#" content{
1119        ... on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount} labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}}
1120        ... on PullRequest{__typename id}
1121        ... on DraftIssue{__typename id title body createdAt updatedAt}
1122      }"#
1123        };
1124    }
1125
1126    /// Reads the board's fields and one page of its items.
1127    pub const BOARD: &str = concat!(
1128        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1129        relationship_events_variable!(),
1130        r#"){
1131      owner:repositoryOwner(login:$owner){
1132        ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1133      }
1134    } fragment Board on ProjectV2 { id title
1135      fields(first:$nestedFirst){nodes{
1136        ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1137        ... on ProjectV2Field{__typename id name dataType}
1138      }pageInfo{hasNextPage}}
1139      items(first:$first,after:$after){nodes{id updatedAt "#,
1140        board_item_values!(),
1141        board_item_content!(),
1142        r#"} pageInfo{hasNextPage endCursor}}
1143    }"#
1144    );
1145
1146    /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1147    /// the board.
1148    ///
1149    /// **`originItems`** is the board's own items narrowed by its own field filter —
1150    /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1151    /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1152    /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1153    /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1154    /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1155    /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1156    /// fresh `addProjectV2ItemById` the way that connection does.
1157    ///
1158    /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1159    /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1160    /// indexes that comment, and the index catches up with a write in a second or two rather
1161    /// than in minutes, so it finds a carrier another process wrote that the first read is
1162    /// still behind on.
1163    ///
1164    /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1165    /// — and resumes from its own cursor; a connection already walked to its end is resumed
1166    /// from its last cursor, which answers an empty page. Every candidate either read returns
1167    /// is confirmed against its own origin field before it is reported, so a token match of
1168    /// the search or anything else the filter admits never is.
1169    ///
1170    /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1171    /// own whole reads counts this one among them.
1172    pub const ORIGIN_LOOKUP: &str = concat!(
1173        r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1174        relationship_events_variable!(),
1175        r#"){
1176      originItems:repositoryOwner(login:$owner){
1177        ... on ProjectV2Owner{projectV2(number:$number){
1178          items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1179        board_item_values!(),
1180        board_item_content!(),
1181        r#"} pageInfo{hasNextPage endCursor}}
1182        }}
1183      }
1184      search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1185        pageInfo{hasNextPage endCursor}
1186        nodes{__typename ...BoardIssue}
1187      }
1188    }"#,
1189        board_issue!()
1190    );
1191
1192    /// Every closed item of the board, by the board's own field filter — what
1193    /// `task status reconcile` reads, and nothing else of the board.
1194    ///
1195    /// **`ProjectV2.items(query:)` narrowed by `$filter`**, which this source binds to
1196    /// `is:closed` alone, so a board whose open items are most of it is not read whole to find
1197    /// the closed ones. It is narrowed rather than trusted: every item it answers is confirmed
1198    /// closed, with a reason and a card that contradict each other, from the item node it
1199    /// already carries, so a filter that also let open items through would cost reads and
1200    /// write nothing wrong.
1201    ///
1202    /// The board's own id and field definitions ride along, as [`BOARD`] carries them, so the
1203    /// option writes that follow need no read of the board. The root is aliased `closedItems`
1204    /// rather than `owner`, so nothing counting the board's own whole reads counts this one
1205    /// among them.
1206    pub const CLOSED_ITEMS: &str = concat!(
1207        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$filter:String!,$nestedFirst:Int!,$duplicates:Boolean!"#,
1208        relationship_events_variable!(),
1209        r#"){
1210      closedItems:repositoryOwner(login:$owner){
1211        ... on ProjectV2Owner{projectV2(number:$number){id
1212          fields(first:$nestedFirst){nodes{
1213            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1214            ... on ProjectV2Field{__typename id name dataType}
1215          }pageInfo{hasNextPage}}
1216          items(first:$first,after:$after,query:$filter){nodes{id updatedAt "#,
1217        board_item_values!(),
1218        board_item_content!(),
1219        r#"} pageInfo{hasNextPage endCursor}}
1220        }}
1221      }
1222    }"#
1223    );
1224
1225    /// One alias of [`ISSUES_BY_REFERENCE`]: the issue `$owner<n>/$name<n>#$number<n>` names.
1226    macro_rules! reference_alias {
1227        ($n:literal) => {
1228            concat!(
1229                "\n      r",
1230                stringify!($n),
1231                ":repository(owner:$owner",
1232                stringify!($n),
1233                ",name:$name",
1234                stringify!($n),
1235                "){issue(number:$number",
1236                stringify!($n),
1237                "){...HeldIssue}}"
1238            )
1239        };
1240    }
1241
1242    /// [`REFERENCE_BATCH`](super::REFERENCE_BATCH) issues at once, each by its repository and
1243    /// number — the one read `task status complete` makes of a source, and nothing else of
1244    /// the board: no `items`, no search and no project read.
1245    ///
1246    /// **Its own fragment, lighter than `board_issue!`, and that is what makes fifty
1247    /// references one point.** It selects no labels, and of each board membership only the
1248    /// `Status` value, by name, with `fieldValueByName` — so each alias asks GitHub for one
1249    /// connection, its page of memberships, where the whole fragment asks for five. What a
1250    /// completion writes is a card and, at most, the issue's state, so the labels and the other
1251    /// field values it leaves out are never what it needs; an item read this way is held for
1252    /// that write alone and never answers another read. An issue whose entry for this board is
1253    /// past its first page of memberships is walked with [`ISSUE_BOARD_ITEMS`], exactly as
1254    /// every other read of an issue recovers one.
1255    ///
1256    /// Fixed-size, as [`ISSUE_DETAILS`] is, so there is one document to price: a batch of
1257    /// fewer references binds the slots it has none for to its last one.
1258    pub const ISSUES_BY_REFERENCE: &str = concat!(
1259        r#"query($owner0:String!,$name0:String!,$number0:Int!,$owner1:String!,$name1:String!,$number1:Int!,$owner2:String!,$name2:String!,$number2:Int!,$owner3:String!,$name3:String!,$number3:Int!,$owner4:String!,$name4:String!,$number4:Int!,$owner5:String!,$name5:String!,$number5:Int!,$owner6:String!,$name6:String!,$number6:Int!,$owner7:String!,$name7:String!,$number7:Int!,$owner8:String!,$name8:String!,$number8:Int!,$owner9:String!,$name9:String!,$number9:Int!,$owner10:String!,$name10:String!,$number10:Int!,$owner11:String!,$name11:String!,$number11:Int!,$owner12:String!,$name12:String!,$number12:Int!,$owner13:String!,$name13:String!,$number13:Int!,$owner14:String!,$name14:String!,$number14:Int!,$owner15:String!,$name15:String!,$number15:Int!,$owner16:String!,$name16:String!,$number16:Int!,$owner17:String!,$name17:String!,$number17:Int!,$owner18:String!,$name18:String!,$number18:Int!,$owner19:String!,$name19:String!,$number19:Int!,$owner20:String!,$name20:String!,$number20:Int!,$owner21:String!,$name21:String!,$number21:Int!,$owner22:String!,$name22:String!,$number22:Int!,$owner23:String!,$name23:String!,$number23:Int!,$owner24:String!,$name24:String!,$number24:Int!,$owner25:String!,$name25:String!,$number25:Int!,$owner26:String!,$name26:String!,$number26:Int!,$owner27:String!,$name27:String!,$number27:Int!,$owner28:String!,$name28:String!,$number28:Int!,$owner29:String!,$name29:String!,$number29:Int!,$owner30:String!,$name30:String!,$number30:Int!,$owner31:String!,$name31:String!,$number31:Int!,$owner32:String!,$name32:String!,$number32:Int!,$owner33:String!,$name33:String!,$number33:Int!,$owner34:String!,$name34:String!,$number34:Int!,$owner35:String!,$name35:String!,$number35:Int!,$owner36:String!,$name36:String!,$number36:Int!,$owner37:String!,$name37:String!,$number37:Int!,$owner38:String!,$name38:String!,$number38:Int!,$owner39:String!,$name39:String!,$number39:Int!,$owner40:String!,$name40:String!,$number40:Int!,$owner41:String!,$name41:String!,$number41:Int!,$owner42:String!,$name42:String!,$number42:Int!,$owner43:String!,$name43:String!,$number43:Int!,$owner44:String!,$name44:String!,$number44:Int!,$owner45:String!,$name45:String!,$number45:Int!,$owner46:String!,$name46:String!,$number46:Int!,$owner47:String!,$name47:String!,$number47:Int!,$owner48:String!,$name48:String!,$number48:Int!,$owner49:String!,$name49:String!,$number49:Int!,$boardItems:Int!,$statusField:String!,$duplicates:Boolean!"#,
1260        relationship_events_variable!(),
1261        r#"){"#,
1262        reference_alias!(0),
1263        reference_alias!(1),
1264        reference_alias!(2),
1265        reference_alias!(3),
1266        reference_alias!(4),
1267        reference_alias!(5),
1268        reference_alias!(6),
1269        reference_alias!(7),
1270        reference_alias!(8),
1271        reference_alias!(9),
1272        reference_alias!(10),
1273        reference_alias!(11),
1274        reference_alias!(12),
1275        reference_alias!(13),
1276        reference_alias!(14),
1277        reference_alias!(15),
1278        reference_alias!(16),
1279        reference_alias!(17),
1280        reference_alias!(18),
1281        reference_alias!(19),
1282        reference_alias!(20),
1283        reference_alias!(21),
1284        reference_alias!(22),
1285        reference_alias!(23),
1286        reference_alias!(24),
1287        reference_alias!(25),
1288        reference_alias!(26),
1289        reference_alias!(27),
1290        reference_alias!(28),
1291        reference_alias!(29),
1292        reference_alias!(30),
1293        reference_alias!(31),
1294        reference_alias!(32),
1295        reference_alias!(33),
1296        reference_alias!(34),
1297        reference_alias!(35),
1298        reference_alias!(36),
1299        reference_alias!(37),
1300        reference_alias!(38),
1301        reference_alias!(39),
1302        reference_alias!(40),
1303        reference_alias!(41),
1304        reference_alias!(42),
1305        reference_alias!(43),
1306        reference_alias!(44),
1307        reference_alias!(45),
1308        reference_alias!(46),
1309        reference_alias!(47),
1310        reference_alias!(48),
1311        reference_alias!(49),
1312        "\n    }",
1313        r#" fragment HeldIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount}
1314      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1315        status:fieldValueByName(name:$statusField){... on ProjectV2ItemFieldSingleSelectValue{name field{
1316          ... on ProjectV2SingleSelectField{id name options{id name}}
1317        }}}
1318      }pageInfo{hasNextPage endCursor}}}"#
1319    );
1320
1321    /// The board's own id and field definitions, and not one of its items.
1322    ///
1323    /// What a write needs of the board when the item it writes does not say: the id a field
1324    /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1325    /// origin fields. It selects no `items`, so what it costs is the board's field list
1326    /// however many items the board holds — and it decides nothing about which items those
1327    /// are, which is the question a read of one item by its own id answers instead.
1328    ///
1329    /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1330    /// board's item reads by their root counts this one among them.
1331    pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1332      boardFields:repositoryOwner(login:$owner){
1333        ... on ProjectV2Owner{projectV2(number:$number){id
1334          fields(first:$nestedFirst){nodes{
1335            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1336            ... on ProjectV2Field{__typename id name dataType}
1337          }pageInfo{hasNextPage}}
1338        }}
1339      }
1340    }"#;
1341
1342    /// One board draft by its own node id, with the board item it sits in.
1343    ///
1344    /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1345    /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1346    /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1347    /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1348    /// board listing hands it to, and nothing has to list the board to find one.
1349    pub const DRAFT: &str = concat!(
1350        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1351      node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1352        projectV2Items(first:$boardItems){nodes{id project{id number}
1353        "#,
1354        board_item_values!(),
1355        r#"}pageInfo{hasNextPage endCursor}}}}
1356    }"#
1357    );
1358
1359    /// One issue's board memberships alone, walked past the page a read of it carried.
1360    ///
1361    /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1362    /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1363    /// boards than that page holds may have this board's entry past its end. This asks that
1364    /// one issue for its memberships and nothing else — the caller already holds the issue —
1365    /// so an answer of "this board does not hold it" is only ever given about a connection
1366    /// read to exhaustion.
1367    ///
1368    /// It selects the board item's id, its project number and the same
1369    /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1370    /// very same resolver: an issue recovered this way reports the same title, the same
1371    /// status, the same labels and the same qualified id as one whose entry was on the
1372    /// page.
1373    ///
1374    /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1375    /// multiplies through it and the membership connection can be walked at
1376    /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1377    /// further request for any issue a person really keeps.
1378    pub const ISSUE_BOARD_ITEMS: &str = concat!(
1379        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1380      node(id:$id){
1381        ... on Issue{projectItems(first:$first,after:$after){
1382          nodes{id updatedAt project{id number}
1383        "#,
1384        board_item_values!(),
1385        r#"}
1386          pageInfo{hasNextPage endCursor}}}
1387      }
1388    }"#
1389    );
1390    /// Whether the board's Project is public — half of what decides whether a write here can
1391    /// be read by anybody. Needs the `read:project` scope.
1392    pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1393    /// Resolves the configured repository's node id, which creating an issue requires.
1394    pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1395    /// What creating an issue needs and has not read yet: the board's own id and field
1396    /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1397    /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1398    ///
1399    /// Sent at the point a create knows which repository it is for, when neither half is
1400    /// already known to this process; a create needing only one of them sends that one's own
1401    /// document. Neither half is kept past the process: a field's option ids are re-minted by
1402    /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1403    /// status.
1404    pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1405      boardFields:repositoryOwner(login:$owner){
1406        ... on ProjectV2Owner{projectV2(number:$number){id
1407          fields(first:$nestedFirst){nodes{
1408            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1409            ... on ProjectV2Field{__typename id name dataType}
1410          }pageInfo{hasNextPage}}
1411        }}
1412      }
1413      repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1414    }"#;
1415    /// Reads both dependency directions for one issue, with each far end's own kind — and
1416    /// the issue's own body, which is where an edge to another source is recorded, so that
1417    /// half of a dependency read needs no second read of the issue or of the board.
1418    pub const ISSUE_DEPENDENCIES: &str = concat!(
1419        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1420      ... on Issue{body
1421        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1422        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1423      }}}"#,
1424        related_issue!()
1425    );
1426    /// What one item depends on: [`ISSUE_DEPENDENCIES`] narrowed to `blockedBy`, so a
1427    /// dependency read asks GitHub for the one direction it answers. The issue's `body` rides
1428    /// along because its metadata slot is where an edge to another source is recorded — and a
1429    /// board draft's `body` too, with the one board item GitHub links a draft to and, aliased
1430    /// `board`, this board's own id, so a draft's recorded edges are read off this same answer
1431    /// and only when that item is this board's: no draft read and no board read follow it.
1432    ///
1433    /// The draft's board item is asked for under a root of its own, aliased `draft`, rather
1434    /// than beside its `body` under `node`. GitHub counts the type-conditional fragments of
1435    /// one selection as the largest of them — `blockedBy`'s 100 alone, where this workspace's
1436    /// calculation adds the draft's 3 — while separate roots are summed by both, so this is
1437    /// the spelling whose node count GitHub and the calculation agree on. Both price it at
1438    /// one point; the credentialed lane's reconciliation is what holds the two to that.
1439    pub const ISSUE_DEPENDS_ON: &str = concat!(
1440        r#"query($id:ID!,$first:Int!,$after:String,$owner:String!,$number:Int!,$boardItems:Int!){
1441      node(id:$id){__typename
1442      ... on Issue{body
1443        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1444      }
1445      ... on DraftIssue{id body}}
1446      draft:node(id:$id){... on DraftIssue{projectV2Items(first:$boardItems){nodes{project{id number}}pageInfo{hasNextPage}}}}
1447      board:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id}}}}"#,
1448        related_issue!()
1449    );
1450    /// What depends on one issue: [`ISSUE_DEPENDENCIES`] with its `blocking` alone. No `body`:
1451    /// the reverse of a recorded edge is derived from the far end, never written down here.
1452    pub const ISSUE_DEPENDED_ON_BY: &str = concat!(
1453        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1454      ... on Issue{
1455        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1456      }}}"#,
1457        related_issue!()
1458    );
1459    /// Creates one issue. Outside a copy it is created on no board, and [`ADD_TO_BOARD`] files
1460    /// it; inside one, its input names the board in `projectV2Ids` and its project in
1461    /// `parentIssueId`, so this one request files it under both — see
1462    /// [`GitHubProjectsSource::create_filed_issue`](super::GitHubProjectsSource).
1463    pub const CREATE_ISSUE: &str =
1464        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1465    /// Puts an existing issue on the configured board.
1466    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1467    /// Updates an issue's visible fields and its open or closed state in one call.
1468    pub const UPDATE_ISSUE: &str =
1469        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1470    /// Updates an existing draft's user-visible fields.
1471    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1472    /// Updates a text or single-select value on one project item.
1473    pub const UPDATE_FIELD: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}}}}}"#;
1474    /// Writes up to six board fields and clears up to three in one ordered mutation: every
1475    /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1476    /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1477    pub const UPDATE_FIELDS: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$second:UpdateProjectV2ItemFieldValueInput!,$third:UpdateProjectV2ItemFieldValueInput!,$fourth:UpdateProjectV2ItemFieldValueInput!,$fifth:UpdateProjectV2ItemFieldValueInput!,$sixth:UpdateProjectV2ItemFieldValueInput!,$clear:ClearProjectV2ItemFieldValueInput!,$clearSecond:ClearProjectV2ItemFieldValueInput!,$clearThird:ClearProjectV2ItemFieldValueInput!,$writeFirst:Boolean!,$writeSecond:Boolean!,$writeThird:Boolean!,$writeFourth:Boolean!,$writeFifth:Boolean!,$writeSixth:Boolean!,$writeClear:Boolean!,$writeClearSecond:Boolean!,$writeClearThird:Boolean!){updateProjectV2ItemFieldValue(input:$input) @include(if:$writeFirst){projectV2Item{id}} second:updateProjectV2ItemFieldValue(input:$second) @include(if:$writeSecond){projectV2Item{id}} third:updateProjectV2ItemFieldValue(input:$third) @include(if:$writeThird){projectV2Item{id}} fourth:updateProjectV2ItemFieldValue(input:$fourth) @include(if:$writeFourth){projectV2Item{id}} fifth:updateProjectV2ItemFieldValue(input:$fifth) @include(if:$writeFifth){projectV2Item{id}} sixth:updateProjectV2ItemFieldValue(input:$sixth) @include(if:$writeSixth){projectV2Item{id}} cleared:clearProjectV2ItemFieldValue(input:$clear) @include(if:$writeClear){projectV2Item{id}} clearedSecond:clearProjectV2ItemFieldValue(input:$clearSecond) @include(if:$writeClearSecond){projectV2Item{id}} clearedThird:clearProjectV2ItemFieldValue(input:$clearThird) @include(if:$writeClearThird){projectV2Item{id}}}"#;
1478    /// Clears one project item's value of one field, which is what a `none` priority is.
1479    pub const CLEAR_FIELD: &str = r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}}}}}"#;
1480    /// Creates one field: a single-select one with its options, or a text one for a projected
1481    /// metadata value. Only the guarded field setup may use this document, and only for a
1482    /// field the board lacks.
1483    pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1484    /// Replaces a single-select field's options. Only the guarded field setup — the
1485    /// `status-options` and `fields` operations — may use this document, because GitHub
1486    /// treats the input as the complete option list.
1487    pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1488    /// A fresh snapshot of the Status field and every board item's assignment.
1489    pub const STATUS_OPTIONS_SNAPSHOT: &str = r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!){owner:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id fields(first:$nestedFirst){nodes{... on ProjectV2SingleSelectField{id name options{id name color description}}}pageInfo{hasNextPage}} items(first:$first,after:$after){nodes{id fieldValues(first:$nestedFirst){nodes{... on ProjectV2ItemFieldSingleSelectValue{name optionId field{... on ProjectV2SingleSelectField{id name}}}}pageInfo{hasNextPage}}}pageInfo{hasNextPage endCursor}}}}}}"#;
1490    /// Files one issue under another as a sub-issue, which is what project membership is.
1491    pub const ADD_SUB_ISSUE: &str =
1492        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1493    /// Takes one issue back out of its parent.
1494    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1495    /// Adds GitHub's native issue blocked-by relationship.
1496    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1497    /// One origin of [`ORIGIN_LOOKUPS`]: [`ORIGIN_LOOKUP`]'s two connections for the origin the
1498    /// `$filter<n>` and `$search<n>` of slot `<n>` name, sent only while `$ask<n>` holds.
1499    macro_rules! origin_lookup_alias {
1500        ($n:literal) => {
1501            concat!(
1502                "\n      o",
1503                stringify!($n),
1504                ":repositoryOwner(login:$owner) @include(if:$ask",
1505                stringify!($n),
1506                "){... on ProjectV2Owner{projectV2(number:$number){items(first:$lookupFirst,query:$filter",
1507                stringify!($n),
1508                "){nodes{id updatedAt ",
1509                board_item_values!(),
1510                board_item_content!(),
1511                "} pageInfo{hasNextPage endCursor}}}}}\n      s",
1512                stringify!($n),
1513                ":search(query:$search",
1514                stringify!($n),
1515                ",type:$type,first:$lookupFirst) @include(if:$ask",
1516                stringify!($n),
1517                "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}"
1518            )
1519        };
1520    }
1521
1522    /// [`ORIGIN_LOOKUPS`], with `$extra_variables` declared after its own and `$extra_fields`
1523    /// selected beside them: one spelling of the batched lookup for the documents that send it.
1524    macro_rules! origin_lookups {
1525        ($extra_variables:literal, $extra_fields:literal) => {
1526            concat!(
1527                r#"query($owner:String!,$number:Int!,$type:SearchType!,$lookupFirst:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!,$repositoryOwner:String!,$repositoryName:String!,$filter0:String!,$filter1:String!,$filter2:String!,$filter3:String!,$filter4:String!,$filter5:String!,$filter6:String!,$filter7:String!,$filter8:String!,$filter9:String!,$search0:String!,$search1:String!,$search2:String!,$search3:String!,$search4:String!,$search5:String!,$search6:String!,$search7:String!,$search8:String!,$search9:String!,$ask0:Boolean!,$ask1:Boolean!,$ask2:Boolean!,$ask3:Boolean!,$ask4:Boolean!,$ask5:Boolean!,$ask6:Boolean!,$ask7:Boolean!,$ask8:Boolean!,$ask9:Boolean!"#,
1528                relationship_events_variable!(),
1529                $extra_variables,
1530                r#"){
1531      boardFields:repositoryOwner(login:$owner){
1532        ... on ProjectV2Owner{projectV2(number:$number){id
1533          fields(first:$nestedFirst){nodes{
1534            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1535            ... on ProjectV2Field{__typename id name dataType}
1536          }pageInfo{hasNextPage}}
1537        }}
1538      }
1539      repository(owner:$repositoryOwner,name:$repositoryName){id databaseId nameWithOwner}"#,
1540                $extra_fields,
1541                origin_lookup_alias!(0),
1542                origin_lookup_alias!(1),
1543                origin_lookup_alias!(2),
1544                origin_lookup_alias!(3),
1545                origin_lookup_alias!(4),
1546                origin_lookup_alias!(5),
1547                origin_lookup_alias!(6),
1548                origin_lookup_alias!(7),
1549                origin_lookup_alias!(8),
1550                origin_lookup_alias!(9),
1551                "\n    }",
1552                board_issue!()
1553            )
1554        };
1555    }
1556
1557    /// The first page of [`ORIGIN_LOOKUP`] for up to [`ORIGIN_BATCH`](super::ORIGIN_BATCH)
1558    /// origins a copy is about to look for, and everything creating an item then reads —
1559    /// the board's own id and field definitions, as [`CREATION_CONTEXT`] reads them, and the node
1560    /// id and the numeric id of the repository an issue is created in — in one request.
1561    ///
1562    /// What a copy reads before it writes anything, so that finding none of its records on the
1563    /// board and creating every one costs this read and the writes. An origin whose page says
1564    /// there is more is left to its own [`ORIGIN_LOOKUP`]. The repository's numeric id is what an
1565    /// image upload names, so a copy carrying assets needs no `GET /repos/{owner}/{repo}` either.
1566    pub const ORIGIN_LOOKUPS: &str = origin_lookups!("", "");
1567
1568    /// [`ORIGIN_LOOKUPS`], with a page of the members of the project `$project` beside it, each
1569    /// trimmed to what classifying it reads — its id, its parent, its body and its repository,
1570    /// and no connection under it.
1571    ///
1572    /// What a document copy sends where the public boundary screens its write: the document is
1573    /// held to who may read its project, which is its members' classification as much as the
1574    /// project's own, and this request carries both the project — among the origins it asks —
1575    /// and its members. GitHub's own dry run prices it at the one point [`ORIGIN_LOOKUPS`]
1576    /// costs alone (`docs/measurements/board-copy-gaps-2026-10-24/coalesced-members-dry-run`).
1577    pub const ORIGIN_LOOKUPS_WITH_MEMBERS: &str = origin_lookups!(
1578        ",$project:ID!,$memberFirst:Int!",
1579        "\n      members:node(id:$project){... on Issue{subIssues(first:$memberFirst){pageInfo{hasNextPage endCursor} nodes{id body parent{id} repository{nameWithOwner}}}}}"
1580    );
1581
1582    /// One slot `<n>` of a copy's aliased writes: `$<prefix><n>` is its input and `$send<n>`
1583    /// whether it is sent.
1584    macro_rules! write_slot {
1585        ($prefix:literal, $field:literal, $selection:literal, $n:literal) => {
1586            concat!(
1587                " ",
1588                $prefix,
1589                stringify!($n),
1590                ":",
1591                $field,
1592                "(input:$",
1593                $prefix,
1594                stringify!($n),
1595                ") @include(if:$send",
1596                stringify!($n),
1597                "){",
1598                $selection,
1599                "}"
1600            )
1601        };
1602    }
1603
1604    /// The variables of a copy's aliased writes: eleven inputs of `$type` named `$<prefix><n>`,
1605    /// and eleven `$send<n>`.
1606    macro_rules! write_variables {
1607        ($prefix:literal, $type:literal) => {
1608            concat!(
1609                "mutation(",
1610                "$",
1611                $prefix,
1612                "0:",
1613                $type,
1614                "!,$",
1615                $prefix,
1616                "1:",
1617                $type,
1618                "!,$",
1619                $prefix,
1620                "2:",
1621                $type,
1622                "!,$",
1623                $prefix,
1624                "3:",
1625                $type,
1626                "!,$",
1627                $prefix,
1628                "4:",
1629                $type,
1630                "!,$",
1631                $prefix,
1632                "5:",
1633                $type,
1634                "!,$",
1635                $prefix,
1636                "6:",
1637                $type,
1638                "!,$",
1639                $prefix,
1640                "7:",
1641                $type,
1642                "!,$",
1643                $prefix,
1644                "8:",
1645                $type,
1646                "!,$",
1647                $prefix,
1648                "9:",
1649                $type,
1650                "!,$",
1651                $prefix,
1652                "10:",
1653                $type,
1654                "!,",
1655                "$send0:Boolean!,$send1:Boolean!,$send2:Boolean!,$send3:Boolean!,$send4:Boolean!,",
1656                "$send5:Boolean!,$send6:Boolean!,$send7:Boolean!,$send8:Boolean!,$send9:Boolean!,",
1657                "$send10:Boolean!){"
1658            )
1659        };
1660    }
1661
1662    /// A copy's aliased writes of one kind: [`MUTATION_BATCH`](super::MUTATION_BATCH) slots of
1663    /// `$field`, each sent only while its `$send<n>` holds.
1664    macro_rules! write_batch {
1665        ($prefix:literal, $type:literal, $field:literal, $selection:literal) => {
1666            concat!(
1667                write_variables!($prefix, $type),
1668                write_slot!($prefix, $field, $selection, 0),
1669                write_slot!($prefix, $field, $selection, 1),
1670                write_slot!($prefix, $field, $selection, 2),
1671                write_slot!($prefix, $field, $selection, 3),
1672                write_slot!($prefix, $field, $selection, 4),
1673                write_slot!($prefix, $field, $selection, 5),
1674                write_slot!($prefix, $field, $selection, 6),
1675                write_slot!($prefix, $field, $selection, 7),
1676                write_slot!($prefix, $field, $selection, 8),
1677                write_slot!($prefix, $field, $selection, 9),
1678                write_slot!($prefix, $field, $selection, 10),
1679                "}"
1680            )
1681        };
1682    }
1683
1684    /// Creates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues in one request, each filed on
1685    /// the board and under its project by the same `createIssue` — its `projectV2Ids` and
1686    /// `parentIssueId` — as [`CREATE_ISSUE`] creates one.
1687    pub const CREATE_ISSUES: &str = write_batch!(
1688        "create",
1689        "CreateIssueInput",
1690        "createIssue",
1691        "issue{id number url}"
1692    );
1693
1694    /// One issue of [`CREATED_BOARD_ITEMS`]: the board items the issue `$id<n>` names is on, with
1695    /// the `Status` value of each, read only while `$read<n>` holds.
1696    macro_rules! created_item_alias {
1697        ($n:literal) => {
1698            concat!(
1699                "\n      i",
1700                stringify!($n),
1701                ":node(id:$id",
1702                stringify!($n),
1703                ") @include(if:$read",
1704                stringify!($n),
1705                "){... on Issue{id projectItems(first:$boardItems){nodes{id project{id} status:fieldValueByName(name:$statusName){... on ProjectV2ItemFieldSingleSelectValue{name}}}}}}"
1706            )
1707        };
1708    }
1709
1710    /// The board item of each issue a copy created, up to
1711    /// [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH) at a time, with its `Status` as the board
1712    /// set it: what `createIssue`'s `projectV2Ids` files the issue as, which GitHub adds after
1713    /// answering the create, so its id comes from a read made once every create has been sent.
1714    pub const CREATED_BOARD_ITEMS: &str = concat!(
1715        r#"query($id0:ID!,$id1:ID!,$id2:ID!,$id3:ID!,$id4:ID!,$id5:ID!,$id6:ID!,$id7:ID!,$id8:ID!,$id9:ID!,$id10:ID!,$id11:ID!,$id12:ID!,$id13:ID!,$id14:ID!,$id15:ID!,$id16:ID!,$id17:ID!,$id18:ID!,$id19:ID!,$id20:ID!,$read0:Boolean!,$read1:Boolean!,$read2:Boolean!,$read3:Boolean!,$read4:Boolean!,$read5:Boolean!,$read6:Boolean!,$read7:Boolean!,$read8:Boolean!,$read9:Boolean!,$read10:Boolean!,$read11:Boolean!,$read12:Boolean!,$read13:Boolean!,$read14:Boolean!,$read15:Boolean!,$read16:Boolean!,$read17:Boolean!,$read18:Boolean!,$read19:Boolean!,$read20:Boolean!,$boardItems:Int!,$statusName:String!){"#,
1716        created_item_alias!(0),
1717        created_item_alias!(1),
1718        created_item_alias!(2),
1719        created_item_alias!(3),
1720        created_item_alias!(4),
1721        created_item_alias!(5),
1722        created_item_alias!(6),
1723        created_item_alias!(7),
1724        created_item_alias!(8),
1725        created_item_alias!(9),
1726        created_item_alias!(10),
1727        created_item_alias!(11),
1728        created_item_alias!(12),
1729        created_item_alias!(13),
1730        created_item_alias!(14),
1731        created_item_alias!(15),
1732        created_item_alias!(16),
1733        created_item_alias!(17),
1734        created_item_alias!(18),
1735        created_item_alias!(19),
1736        created_item_alias!(20),
1737        "\n    }"
1738    );
1739
1740    /// The variables and the aliases of [`WRITE_BATCH`](super::WRITE_BATCH) slots: `$<prefix><n>`
1741    /// is a slot's input and `$<send><n>` whether it is sent, for the slot numbers given.
1742    macro_rules! wide_variables {
1743        ($prefix:literal, $type:literal, $send:literal; $($n:literal)+) => {
1744            concat!($("$", $prefix, stringify!($n), ":", $type, "!,$", $send, stringify!($n), ":Boolean!,",)+)
1745        };
1746    }
1747    macro_rules! wide_slots {
1748        ($prefix:literal, $field:literal, $selection:literal, $send:literal; $($n:literal)+) => {
1749            concat!($(
1750                " ", $prefix, stringify!($n), ":", $field, "(input:$", $prefix, stringify!($n),
1751                ") @include(if:$", $send, stringify!($n), "){", $selection, "}",
1752            )+)
1753        };
1754    }
1755
1756    /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) board field values a copy writes on the items it
1757    /// created, each on its own item, in one ordered request.
1758    pub const WRITE_FIELD_VALUES: &str = concat!(
1759        "mutation(",
1760        wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1761        "){",
1762        wide_slots!("field", "updateProjectV2ItemFieldValue", "projectV2Item{id}", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1763        "}"
1764    );
1765
1766    /// A copy's board field values and its blocked-by edges together, up to
1767    /// [`WRITE_BATCH`](super::WRITE_BATCH) of the two kinds between them, in one ordered request:
1768    /// every field value its `$sendField<n>` sends, then every edge its `$sendBlock<n>` sends.
1769    /// What a copy sends when one request of its writes holds both kinds; one holding a single
1770    /// kind is [`WRITE_FIELD_VALUES`] or [`ADD_BLOCKED_BYS`].
1771    pub const WRITE_FIELDS_AND_EDGES: &str = concat!(
1772        "mutation(",
1773        wide_variables!("field", "UpdateProjectV2ItemFieldValueInput", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1774        wide_variables!("block", "AddBlockedByInput", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1775        "){",
1776        wide_slots!("field", "updateProjectV2ItemFieldValue", "projectV2Item{id}", "sendField"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1777        wide_slots!("block", "addBlockedBy", "issue{id} blockingIssue{id}", "sendBlock"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1778        "}"
1779    );
1780
1781    /// Closes up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues a copy created at a closing
1782    /// status, each with its own `stateInput` and nothing else, once its option is selected.
1783    pub const CLOSE_ISSUES: &str =
1784        write_batch!("close", "UpdateIssueInput", "updateIssue", "issue{id}");
1785
1786    /// Updates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) existing issues a copy rewrites,
1787    /// each with the members its own [`UPDATE_ISSUE`] would have sent, in one request.
1788    pub const UPDATE_ISSUES: &str =
1789        write_batch!("update", "UpdateIssueInput", "updateIssue", "issue{id}");
1790
1791    /// Up to [`WRITE_BATCH`](super::WRITE_BATCH) native blocked-by relationships a copy adds to the
1792    /// issues it created, in one request.
1793    pub const ADD_BLOCKED_BYS: &str = concat!(
1794        "mutation(",
1795        wide_variables!("block", "AddBlockedByInput", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1796        "){",
1797        wide_slots!("block", "addBlockedBy", "issue{id} blockingIssue{id}", "send"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18),
1798        "}"
1799    );
1800
1801    /// Removes one native issue blocked-by relationship.
1802    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1803    /// Deletes one issue, which takes its board item with it.
1804    ///
1805    /// The engine sends this in one situation only: undoing a copy that could not finish,
1806    /// over the items that same copy created. Deleting the issue removes the board item
1807    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1808    pub const DELETE_ISSUE: &str =
1809        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1810    /// Deletes up to [`DELETE_BATCH`](super::DELETE_BATCH) issues in one request, each as
1811    /// [`DELETE_ISSUE`] deletes one: what a write that created several issues and was refused
1812    /// part way sends to take them back.
1813    pub const DELETE_ISSUES: &str = concat!(
1814        "mutation(",
1815        wide_variables!("delete", "DeleteIssueInput", "send"; 0 1 2 3 4),
1816        "){",
1817        wide_slots!("delete", "deleteIssue", "repository{id}", "send"; 0 1 2 3 4),
1818        "}"
1819    );
1820
1821    /// Everything this source reads about one issue comment, wherever it reaches one.
1822    ///
1823    /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1824    /// and a comment just edited are handed to one mapper, so they are selected by one
1825    /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1826    /// longer exists, and `login` is the one member every kind of actor carries.
1827    macro_rules! issue_comment {
1828        () => {
1829            "id author{login} createdAt updatedAt body url"
1830        };
1831    }
1832
1833    /// One task's comments: a page of its issue's own `comments` connection.
1834    ///
1835    /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1836    /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1837    /// list every time somebody edited it; left unordered the connection answers in the order
1838    /// the comments were written, which is the order GitHub documents for the same collection
1839    /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1840    /// node count and the caller's own page size is pushed straight down.
1841    pub const ISSUE_COMMENTS: &str = concat!(
1842        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1843        issue_comment!(),
1844        r#"}pageInfo{hasNextPage endCursor}}}}}"#
1845    );
1846    /// One issue by its own node id, with a page of its comments: what `task show` and a
1847    /// comment listing read, in one request.
1848    ///
1849    /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1850    /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1851    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1852    /// connection there would multiply through both of those documents' price, and neither
1853    /// needs one.
1854    pub const ISSUE_DETAIL: &str = concat!(
1855        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1856        relationship_events_variable!(),
1857        r#"){
1858      node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1859        issue_comment!(),
1860        r#"}pageInfo{hasNextPage endCursor}}}}
1861    }"#,
1862        board_issue!()
1863    );
1864
1865    /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1866    /// page of its comments when `$comments` asks for them.
1867    macro_rules! issue_details_alias {
1868        ($n:literal) => {
1869            concat!(
1870                "\n      i",
1871                stringify!($n),
1872                ":node(id:$id",
1873                stringify!($n),
1874                "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1875                issue_comment!(),
1876                "}pageInfo{hasNextPage endCursor}}}}"
1877            )
1878        };
1879    }
1880
1881    /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1882    /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1883    ///
1884    /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1885    /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1886    /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1887    /// neither — so every connection under it would be priced at nothing and the pin in
1888    /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1889    /// one-item read the model already prices, so the batch costs what its aliases cost.
1890    ///
1891    /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1892    /// slots it has no item for to the last item it does, and reads that item again; the
1893    /// price is the document's, whatever its variables, so a short batch costs what a full
1894    /// one does and nothing more.
1895    pub const ISSUE_DETAILS: &str = concat!(
1896        r#"query($id0:ID!,$id1:ID!,$id2:ID!,$id3:ID!,$id4:ID!,$id5:ID!,$id6:ID!,$id7:ID!,$id8:ID!,$id9:ID!,$id10:ID!,$id11:ID!,$id12:ID!,$id13:ID!,$id14:ID!,$id15:ID!,$id16:ID!,$id17:ID!,$id18:ID!,$id19:ID!,$id20:ID!,$id21:ID!,$id22:ID!,$id23:ID!,$first:Int!,$comments:Boolean!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1897        relationship_events_variable!(),
1898        r#"){"#,
1899        issue_details_alias!(0),
1900        issue_details_alias!(1),
1901        issue_details_alias!(2),
1902        issue_details_alias!(3),
1903        issue_details_alias!(4),
1904        issue_details_alias!(5),
1905        issue_details_alias!(6),
1906        issue_details_alias!(7),
1907        issue_details_alias!(8),
1908        issue_details_alias!(9),
1909        issue_details_alias!(10),
1910        issue_details_alias!(11),
1911        issue_details_alias!(12),
1912        issue_details_alias!(13),
1913        issue_details_alias!(14),
1914        issue_details_alias!(15),
1915        issue_details_alias!(16),
1916        issue_details_alias!(17),
1917        issue_details_alias!(18),
1918        issue_details_alias!(19),
1919        issue_details_alias!(20),
1920        issue_details_alias!(21),
1921        issue_details_alias!(22),
1922        issue_details_alias!(23),
1923        "\n    }",
1924        board_issue!()
1925    );
1926
1927    /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1928    /// page of its comments and the issues it is blocked by.
1929    macro_rules! issue_copy_details_alias {
1930        ($n:literal) => {
1931            concat!(
1932                "\n      i",
1933                stringify!($n),
1934                ":node(id:$id",
1935                stringify!($n),
1936                "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1937                issue_comment!(),
1938                "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1939            )
1940        };
1941    }
1942
1943    /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1944    /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1945    /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1946    /// at a time, so a copy of a changed task costs this read and nothing else.
1947    ///
1948    /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1949    /// what `task show-many` is priced by and which reads no edge.
1950    ///
1951    /// `children`, read only while `$children` holds, is which issues the issue `$project` has
1952    /// as sub-issues — each one's id and title, the page `$nestedFirst` allows — so that a
1953    /// project copy, which reads its project and every task at once, knows from this same read
1954    /// which tasks the project holds at the destination, and its orphan check is answered by it
1955    /// rather than by a listing of the project.
1956    pub const ISSUE_COPY_DETAILS: &str = concat!(
1957        r#"query($id0:ID!,$id1:ID!,$id2:ID!,$id3:ID!,$id4:ID!,$id5:ID!,$id6:ID!,$id7:ID!,$id8:ID!,$id9:ID!,$id10:ID!,$id11:ID!,$id12:ID!,$id13:ID!,$id14:ID!,$id15:ID!,$id16:ID!,$id17:ID!,$id18:ID!,$id19:ID!,$id20:ID!,$project:ID!,$children:Boolean!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1958        relationship_events_variable!(),
1959        r#"){"#,
1960        issue_copy_details_alias!(0),
1961        issue_copy_details_alias!(1),
1962        issue_copy_details_alias!(2),
1963        issue_copy_details_alias!(3),
1964        issue_copy_details_alias!(4),
1965        issue_copy_details_alias!(5),
1966        issue_copy_details_alias!(6),
1967        issue_copy_details_alias!(7),
1968        issue_copy_details_alias!(8),
1969        issue_copy_details_alias!(9),
1970        issue_copy_details_alias!(10),
1971        issue_copy_details_alias!(11),
1972        issue_copy_details_alias!(12),
1973        issue_copy_details_alias!(13),
1974        issue_copy_details_alias!(14),
1975        issue_copy_details_alias!(15),
1976        issue_copy_details_alias!(16),
1977        issue_copy_details_alias!(17),
1978        issue_copy_details_alias!(18),
1979        issue_copy_details_alias!(19),
1980        issue_copy_details_alias!(20),
1981        "\n      children:node(id:$project) @include(if:$children){... on Issue{id subIssues(first:$nestedFirst){nodes{id title} pageInfo{hasNextPage}}}}",
1982        "\n    }",
1983        board_issue!(),
1984        related_issue!()
1985    );
1986
1987    /// Which issue one comment is on, read before that comment is edited or removed.
1988    ///
1989    /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1990    /// comment id given against the wrong task would change a comment on another issue.
1991    pub const COMMENT_ISSUE: &str =
1992        r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1993    /// Adds one comment to an issue, signed as the account the token belongs to.
1994    pub const ADD_COMMENT: &str = concat!(
1995        r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1996        issue_comment!(),
1997        r#"}}}}"#
1998    );
1999    /// Replaces the body of one issue comment.
2000    pub const UPDATE_COMMENT: &str = concat!(
2001        r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
2002        issue_comment!(),
2003        r#"}}}"#
2004    );
2005    /// Removes one issue comment. Its payload carries nothing about the comment it removed.
2006    pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
2007
2008    /// What a mutation of a write the store's cache expects selects of the issue it wrote:
2009    /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
2010    /// change token's every part and the first page of its comments.
2011    ///
2012    /// Sent **only** while that write runs, in place of the document named beside each
2013    /// below, so every other write's documents, prices and request counts are what they
2014    /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
2015    /// mutation's response is stamped seconds after the change it made, so against GitHub
2016    /// itself a response rarely determines a version; the cache is then left unserveable
2017    /// for the task, and its next read refreshes it.
2018    macro_rules! whole_issue {
2019        () => {
2020            concat!(
2021                r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
2022                issue_comment!(),
2023                r#"}pageInfo{hasNextPage endCursor}}}"#
2024            )
2025        };
2026    }
2027
2028    /// The variables every document of `whole_issue!` declares beside its own.
2029    macro_rules! whole_variables {
2030        () => {
2031            concat!(
2032                r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
2033                relationship_events_variable!()
2034            )
2035        };
2036    }
2037
2038    /// [`ADD_COMMENT`], selecting the whole issue commented on.
2039    pub const ADD_COMMENT_WHOLE: &str = concat!(
2040        r#"mutation($input:AddCommentInput!"#,
2041        whole_variables!(),
2042        r#"){addComment(input:$input){subject{id "#,
2043        whole_issue!(),
2044        r#"} commentEdge{node{"#,
2045        issue_comment!(),
2046        r#"}}}}"#,
2047        board_issue!()
2048    );
2049    /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
2050    pub const UPDATE_COMMENT_WHOLE: &str = concat!(
2051        r#"mutation($input:UpdateIssueCommentInput!"#,
2052        whole_variables!(),
2053        r#"){updateIssueComment(input:$input){issueComment{"#,
2054        issue_comment!(),
2055        r#" issue{"#,
2056        whole_issue!(),
2057        r#"}}}}"#,
2058        board_issue!()
2059    );
2060    /// [`UPDATE_ISSUE`], selecting the whole issue updated.
2061    pub const UPDATE_ISSUE_WHOLE: &str = concat!(
2062        r#"mutation($input:UpdateIssueInput!"#,
2063        whole_variables!(),
2064        r#"){updateIssue(input:$input){issue{id "#,
2065        whole_issue!(),
2066        r#"}}}"#,
2067        board_issue!()
2068    );
2069    /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
2070    pub const UPDATE_FIELD_WHOLE: &str = concat!(
2071        r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2072        whole_variables!(),
2073        r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2074        whole_issue!(),
2075        r#"}}}}"#,
2076        board_issue!()
2077    );
2078    /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
2079    pub const CLEAR_FIELD_WHOLE: &str = concat!(
2080        r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
2081        whole_variables!(),
2082        r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
2083        whole_issue!(),
2084        r#"}}}}"#,
2085        board_issue!()
2086    );
2087
2088    /// The document sent in `document`'s place while a write the store's cache expects runs,
2089    /// when it has one.
2090    #[must_use]
2091    pub fn whole_twin(document: &str) -> Option<&'static str> {
2092        [
2093            (ADD_COMMENT, ADD_COMMENT_WHOLE),
2094            (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
2095            (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
2096            (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
2097            (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
2098        ]
2099        .into_iter()
2100        .find(|(plain, _)| *plain == document)
2101        .map(|(_, whole)| whole)
2102    }
2103
2104    /// Every document above, with what this source is doing when it sends one.
2105    ///
2106    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
2107    /// name the call that was refused, and a `match` with a catch-all arm would answer a
2108    /// document added later with "talking to GitHub" and never say so.
2109    ///
2110    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
2111    /// const` here that this list omits, so the two cannot part — which is the same guard
2112    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
2113    pub const DOCUMENTS: [(&str, &str); 55] = [
2114        (SEARCH_ISSUES, "searching this board's issues"),
2115        (ISSUE, "reading one issue"),
2116        (
2117            DOCUMENT_IN_PROJECT,
2118            "reading one document with its project and the project's members",
2119        ),
2120        (
2121            ISSUE_BOARD_ITEMS,
2122            "reading one issue's board memberships past the page it came with",
2123        ),
2124        (SUB_ISSUES, "reading a project's tasks"),
2125        (BOARD, "reading the board"),
2126        (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
2127        (CLOSED_ITEMS, "reading the board's closed items"),
2128        (
2129            ISSUES_BY_REFERENCE,
2130            "reading a batch of issues by their references",
2131        ),
2132        (BOARD_FIELDS, "reading the board's fields"),
2133        (DRAFT, "reading one draft"),
2134        (REPOSITORY, "reading the destination repository"),
2135        (
2136            PROJECT_VISIBILITY,
2137            "reading whether the board's project is public",
2138        ),
2139        (
2140            CREATION_CONTEXT,
2141            "reading the board's fields and the destination repository",
2142        ),
2143        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
2144        (ISSUE_DEPENDS_ON, "reading what an item depends on"),
2145        (ISSUE_DEPENDED_ON_BY, "reading what depends on an issue"),
2146        (CREATE_ISSUE, "creating an issue"),
2147        (
2148            ORIGIN_LOOKUPS,
2149            "looking up the items copied from several origins, and what creating one reads",
2150        ),
2151        (
2152            ORIGIN_LOOKUPS_WITH_MEMBERS,
2153            "looking up the items copied from several origins, with a project's members",
2154        ),
2155        (CREATE_ISSUES, "creating several issues together"),
2156        (
2157            CREATED_BOARD_ITEMS,
2158            "reading the board items of the issues a copy created",
2159        ),
2160        (
2161            WRITE_FIELD_VALUES,
2162            "writing board fields of the items a copy created",
2163        ),
2164        (
2165            WRITE_FIELDS_AND_EDGES,
2166            "writing board fields and dependencies of the items a copy created",
2167        ),
2168        (CLOSE_ISSUES, "closing the issues a copy created"),
2169        (UPDATE_ISSUES, "updating several issues together"),
2170        (ADD_BLOCKED_BYS, "recording several dependencies together"),
2171        (ADD_TO_BOARD, "adding an issue to the board"),
2172        (UPDATE_ISSUE, "updating an issue"),
2173        (UPDATE_DRAFT, "updating a draft item"),
2174        (UPDATE_FIELD, "writing a board field"),
2175        (UPDATE_FIELDS, "writing board fields together"),
2176        (CLEAR_FIELD, "clearing a board field"),
2177        (
2178            CREATE_FIELD,
2179            "creating a board single-select field with its options",
2180        ),
2181        (
2182            STATUS_OPTIONS_SNAPSHOT,
2183            "snapshotting board Status options and assignments",
2184        ),
2185        (
2186            STATUS_OPTIONS_UPDATE,
2187            "safely replacing the board Status option list",
2188        ),
2189        (ADD_SUB_ISSUE, "filing an issue under its project"),
2190        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
2191        (ADD_BLOCKED_BY, "recording a dependency"),
2192        (REMOVE_BLOCKED_BY, "removing a dependency"),
2193        (DELETE_ISSUE, "deleting an issue"),
2194        (DELETE_ISSUES, "deleting several issues together"),
2195        (ISSUE_COMMENTS, "reading a task's comments"),
2196        (ISSUE_DETAIL, "reading one issue with its comments"),
2197        (
2198            ISSUE_DETAILS,
2199            "reading a batch of issues with their comments",
2200        ),
2201        (
2202            ISSUE_COPY_DETAILS,
2203            "reading a batch of issues with their comments and dependencies for a copy",
2204        ),
2205        (COMMENT_ISSUE, "reading which issue a comment is on"),
2206        (ADD_COMMENT, "adding a comment"),
2207        (UPDATE_COMMENT, "editing a comment"),
2208        (DELETE_COMMENT, "deleting a comment"),
2209        (
2210            ADD_COMMENT_WHOLE,
2211            "adding a comment, answered with the whole issue",
2212        ),
2213        (
2214            UPDATE_COMMENT_WHOLE,
2215            "editing a comment, answered with the whole issue",
2216        ),
2217        (
2218            UPDATE_ISSUE_WHOLE,
2219            "updating an issue, answered with it whole",
2220        ),
2221        (
2222            UPDATE_FIELD_WHOLE,
2223            "writing a board field, answered with the whole issue",
2224        ),
2225        (
2226            CLEAR_FIELD_WHOLE,
2227            "clearing a board field, answered with the whole issue",
2228        ),
2229    ];
2230}
2231
2232/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
2233/// under, the variable carrying its input, and the boolean variable that includes it.
2234#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2235pub struct FieldSlot {
2236    /// The key of the answer's `data` this field's payload is under.
2237    pub alias: &'static str,
2238    /// The variable carrying this field's input.
2239    pub variable: &'static str,
2240    /// The boolean variable whose `true` sends this field.
2241    pub include: &'static str,
2242}
2243
2244/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
2245pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
2246    FieldSlot {
2247        alias: "updateProjectV2ItemFieldValue",
2248        variable: "input",
2249        include: "writeFirst",
2250    },
2251    FieldSlot {
2252        alias: "second",
2253        variable: "second",
2254        include: "writeSecond",
2255    },
2256    FieldSlot {
2257        alias: "third",
2258        variable: "third",
2259        include: "writeThird",
2260    },
2261    FieldSlot {
2262        alias: "fourth",
2263        variable: "fourth",
2264        include: "writeFourth",
2265    },
2266    FieldSlot {
2267        alias: "fifth",
2268        variable: "fifth",
2269        include: "writeFifth",
2270    },
2271    FieldSlot {
2272        alias: "sixth",
2273        variable: "sixth",
2274        include: "writeSixth",
2275    },
2276];
2277
2278/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
2279pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
2280    FieldSlot {
2281        alias: "cleared",
2282        variable: "clear",
2283        include: "writeClear",
2284    },
2285    FieldSlot {
2286        alias: "clearedSecond",
2287        variable: "clearSecond",
2288        include: "writeClearSecond",
2289    },
2290    FieldSlot {
2291        alias: "clearedThird",
2292        variable: "clearThird",
2293        include: "writeClearThird",
2294    },
2295];
2296
2297/// Which of GitHub's two rate limiters refused a request.
2298///
2299/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
2300/// secondary one — so an operator told the wrong one takes the wrong next step, which is
2301/// the whole reason this is carried rather than collapsed into "rate limited".
2302#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2303enum Limiter {
2304    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
2305    Primary,
2306    /// The burst limiter over content-generating requests, which nothing reports.
2307    Secondary,
2308}
2309
2310/// The wordings GitHub answers a secondary rate limit with.
2311///
2312/// It sends them under a forbidden status, under a too-many-requests status, and inside
2313/// the `errors` of a *successful* response, which is why the text is what this matches on
2314/// rather than the status. `abuse detection` is the wording GitHub used before the
2315/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
2316/// what a burst of content creation is refused with.
2317///
2318/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
2319/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
2320/// when, and the drift gate reconciles the two lists both ways. Public for that gate
2321/// alone — a caller has no use for it, and matching on a refusal is this source's job.
2322pub const SECONDARY_WORDINGS: [&str; 5] = [
2323    "secondary rate limit",
2324    "temporarily blocked from content creation",
2325    "abuse detection",
2326    "submitted too quickly",
2327    "exceeded a secondary",
2328];
2329
2330/// The wordings GitHub answers an exhausted primary budget with.
2331///
2332/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
2333/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
2334/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
2335/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
2336/// two phrases is a substring of it, so without it that answer read as a refusal that will
2337/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
2338/// one reason.
2339pub const PRIMARY_WORDINGS: [&str; 4] = [
2340    "api rate limit exceeded",
2341    "api rate limit already exceeded",
2342    "rate limit exceeded",
2343    "rate_limited",
2344];
2345
2346/// What a response *says about itself*, which is the only place a refusal can be read.
2347///
2348/// Deliberately not the whole response body. A board is a place people write about their
2349/// own work, and a task on it titled "the secondary rate limit" would, matched across the
2350/// raw text, turn a perfectly good answer into a refusal this source then waited out and
2351/// reported. So the item data is never read: what is read is GitHub's own REST-style
2352/// `message` envelope, which is what a forbidden status carries, and the `message` and
2353/// `type` of each GraphQL error, which is where a *successful* response says it.
2354///
2355/// A body that is not JSON at all has nothing structured to read, so only a failing
2356/// response's own text is taken — a successful response that is not JSON is malformed
2357/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
2358fn refusal_wording(status: StatusCode, body: &str) -> String {
2359    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
2360        return if status.is_success() {
2361            String::new()
2362        } else {
2363            body.to_owned()
2364        };
2365    };
2366    let mut said: Vec<&str> = parsed
2367        .get("message")
2368        .and_then(Value::as_str)
2369        .into_iter()
2370        .collect();
2371    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
2372        for error in errors {
2373            said.extend(
2374                ["message", "type"]
2375                    .into_iter()
2376                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
2377            );
2378        }
2379    }
2380    said.join("; ")
2381}
2382
2383impl Limiter {
2384    /// Which limiter refused this response, or `None` when none of them did.
2385    ///
2386    /// The wording is read first and the status only decides what carries none of it,
2387    /// because GitHub answers a secondary limit with a forbidden status far more often
2388    /// than with too-many-requests — while a forbidden status saying nothing about a limit
2389    /// really is a credential this token lacks.
2390    ///
2391    /// A response is a refusal because of its status or its own wording. A spent budget
2392    /// only ever explains one; it never turns an answer into a refusal.
2393    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
2394        let normalized = refusal_wording(status, body).to_ascii_lowercase();
2395        if SECONDARY_WORDINGS
2396            .iter()
2397            .any(|wording| normalized.contains(wording))
2398        {
2399            return Some(Self::Secondary);
2400        }
2401        if status == StatusCode::TOO_MANY_REQUESTS {
2402            return Some(Self::Primary);
2403        }
2404        // An exhausted budget *explains* a response that failed; it does not make one that
2405        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
2406        // request the budget allowed as well as on the ones it then refuses, so reading
2407        // the header alone threw away a good answer — and, once refusals were retried,
2408        // replayed a request that had already taken effect.
2409        if !status.is_success() && budget_exhausted {
2410            return Some(Self::Primary);
2411        }
2412        // A successful response saying it: GitHub reports a GraphQL rate limit in the
2413        // `errors` of an HTTP 200, where nothing about the status says so at all.
2414        if status.is_success()
2415            && PRIMARY_WORDINGS
2416                .iter()
2417                .any(|wording| normalized.contains(wording))
2418        {
2419            return Some(Self::Primary);
2420        }
2421        None
2422    }
2423
2424    /// What this limiter is called where an operator can look it up.
2425    const fn name(self) -> &'static str {
2426        match self {
2427            Self::Primary => "GitHub's primary API rate limit",
2428            Self::Secondary => "GitHub's secondary rate limit",
2429        }
2430    }
2431
2432    /// What the endpoint an operator would go and check says about this limiter.
2433    const fn where_to_look(self) -> &'static str {
2434        match self {
2435            Self::Primary => {
2436                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
2437                 comes back."
2438            }
2439            Self::Secondary => {
2440                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
2441                 primary budget and does not report this one, so budget showing there says \
2442                 nothing about this refusal, and every further attempt extends it."
2443            }
2444        }
2445    }
2446
2447    /// The next step this limiter actually calls for.
2448    const fn what_to_do(self) -> &'static str {
2449        match self {
2450            Self::Primary => {
2451                "wait for the reset `gh api rate_limit` reports, then run the command again."
2452            }
2453            Self::Secondary => {
2454                "leave this board alone for a few minutes, then run the command again — or \
2455                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
2456            }
2457        }
2458    }
2459}
2460
2461/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
2462#[derive(Debug, Clone, Copy)]
2463struct Limited {
2464    limiter: Limiter,
2465    hint: Option<u64>,
2466    /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
2467    /// `x-ratelimit-reset`.
2468    hinted_by: Hint,
2469}
2470
2471/// Where a refusal's hint came from, which is what its wait is named after.
2472#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2473enum Hint {
2474    /// `retry-after`, or no hint at all.
2475    RetryAfter,
2476    /// An exhausted primary budget's `x-ratelimit-reset`.
2477    PrimaryReset,
2478}
2479
2480impl Limited {
2481    /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
2482    /// and the cause it is named by: the hint, raised to the schedule's own next wait —
2483    /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
2484    /// extends a secondary limit — or the schedule alone when there is none.
2485    fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
2486        match (self.hint, self.hinted_by) {
2487            (Some(hint), Hint::RetryAfter) => (
2488                Duration::from_secs(hint).max(backoff),
2489                pacing::cause::RETRY_AFTER,
2490            ),
2491            (Some(hint), Hint::PrimaryReset) => (
2492                Duration::from_secs(hint).max(backoff),
2493                pacing::cause::PRIMARY_RESET,
2494            ),
2495            (None, _) => (backoff, pacing::cause::BACKOFF),
2496        }
2497    }
2498
2499    /// What the caller is told once this source has waited as long as it may.
2500    ///
2501    /// Both limiters report as [`SourceError::RateLimited`], because that is what
2502    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2503    /// about *which* limiter it was makes it a different kind of failure. What differs is
2504    /// the operator's next step, and that is what the message carries — a secondary
2505    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2506    /// budget looks fine, and then back to retry the very burst that was refused.
2507    fn exhausted(
2508        self,
2509        doing: &str,
2510        waits: u32,
2511        waited: Duration,
2512        needed: Duration,
2513        budget: Duration,
2514    ) -> SourceError {
2515        SourceError::RateLimited {
2516            retry_after_seconds: self.hint,
2517            message: Some(format!(
2518                "{} refused this source while {doing}; it waited {} out over {} and was refused \
2519                 again, and the next wait of {} would take it past the {} one call may spend \
2520                 waiting. {} next: {}",
2521                self.limiter.name(),
2522                plural(waits, "refusal"),
2523                seconds(waited),
2524                seconds(needed),
2525                seconds(budget),
2526                self.limiter.where_to_look(),
2527                self.limiter.what_to_do(),
2528            )),
2529        }
2530    }
2531}
2532
2533/// One HTTP attempt's result, with what its response said about the rate limit.
2534///
2535/// The two travel together so the record and the outcome are written from the same place:
2536/// what a response said about the budget is only readable while that response is in hand,
2537/// and what the attempt *meant* is only decidable once its body has been read.
2538struct Attempted {
2539    result: Result<Value, Attempt>,
2540    limits: accounting::RateLimit,
2541    /// GitHub's own reported cost for this call, for a document that asked for it.
2542    reported_cost: Option<u64>,
2543    response_outcome: accounting::ResponseOutcome,
2544}
2545
2546/// One attempt's outcome: an error to report, or a rate limit to wait out.
2547enum Attempt {
2548    Failed(SourceError),
2549    Limited(Limited),
2550}
2551
2552fn plural(count: u32, thing: &str) -> String {
2553    if count == 1 {
2554        format!("{count} {thing}")
2555    } else {
2556        format!("{count} {thing}s")
2557    }
2558}
2559
2560fn seconds(duration: Duration) -> String {
2561    format!("{:.1}s", duration.as_secs_f64())
2562}
2563
2564/// The member [`stamp_served`] writes onto every node of a response that says when it last
2565/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2566const SERVED_AT: &str = "onetaskgraphServedAt";
2567
2568/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2569/// node's time later knows the server's clock on the response that read it — which is what
2570/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2571fn stamp_served(data: &mut Value, served: &str) {
2572    match data {
2573        Value::Object(members) => {
2574            if members.contains_key("updatedAt") {
2575                members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2576            }
2577            for member in members.values_mut() {
2578                stamp_served(member, served);
2579            }
2580        }
2581        Value::Array(items) => {
2582            for item in items {
2583                stamp_served(item, served);
2584            }
2585        }
2586        _ => {}
2587    }
2588}
2589
2590/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2591///
2592/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2593/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2594/// header, and neither is what makes a response a refusal — so the whole cost of one this
2595/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2596/// instead. Refusing the response over the header would turn a readable refusal into an
2597/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2598fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2599    value
2600        .and_then(|value| value.to_str().ok())
2601        .and_then(|value| value.trim().parse::<u64>().ok())
2602}
2603
2604/// A transport failure with every cause beneath it, outermost first.
2605///
2606/// The client's own text for a request it could not complete is `error sending request for
2607/// url (…)`, and the socket error that says why — a refused or reset connection, an exhausted
2608/// port range, a connection closed before its answer — is only in the chain of sources below
2609/// it. Those differ in what to do next, so all of them are kept.
2610pub(crate) fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
2611    let mut text = error.to_string();
2612    let mut cause = error.source();
2613    while let Some(next) = cause {
2614        let said = next.to_string();
2615        if !text.ends_with(&said) {
2616            text = format!("{text}: {said}");
2617        }
2618        cause = next.source();
2619    }
2620    text
2621}
2622
2623/// Every mutation this source sends creates content — an issue, a board item, a field of
2624/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2625/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2626/// and what the keyword says are the same set. That is what makes the keyword a sound test
2627/// rather than a convenient one: counting an edit or a removal the limiter might not have
2628/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
2629pub(crate) fn is_mutation(query: &str) -> bool {
2630    query.trim_start().starts_with("mutation")
2631}
2632
2633/// What this source was doing, for a diagnostic that has to say so.
2634///
2635/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2636/// a document added without a description is caught by that list's own gate instead of
2637/// falling through to the vague arm below.
2638fn operation_description(query: &str) -> &'static str {
2639    graphql::DOCUMENTS
2640        .iter()
2641        .find(|(document, _)| *document == query)
2642        .map_or("talking to GitHub", |(_, doing)| *doing)
2643}
2644
2645/// GitHub's published ceiling on content-generating requests in any one minute.
2646///
2647/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2648/// GitHub's number rather than this source's. This source paces at it over its window: a
2649/// content-creating request goes out at once while the minute's creations leave room for it,
2650/// and waits until they do otherwise — see the `pacing` module.
2651pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2652/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
2653/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
2654pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2655/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
2656/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
2657/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
2658pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
2659/// The secondary points one GraphQL request carrying a mutation is charged.
2660pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
2661/// The secondary points one GraphQL request carrying no mutation is charged.
2662pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
2663/// The fastest *steady* spacing of content-creating mutations that cannot exceed
2664/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
2665///
2666/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
2667/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
2668/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
2669/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
2670/// value that holds a long sustained burst under the per-minute bound by spacing alone.
2671pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2672/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2673///
2674/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2675/// own advice for a secondary limit — wait, and wait longer each time — without spending
2676/// the first minute of a transient refusal doing nothing.
2677pub const RETRY_BACKOFF_MS: u64 = 1_000;
2678/// Total time one call may spend waiting out rate limits before it reports a failure.
2679///
2680/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2681/// short enough that a command an operator is watching returns. The bound is what makes
2682/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2683/// the limiter, not in a process nobody can tell from a wedged one.
2684pub const RETRY_BUDGET_MS: u64 = 120_000;
2685
2686fn default_token_env() -> String {
2687    "GH_PROJECTS_TOKEN".to_owned()
2688}
2689fn default_endpoint() -> String {
2690    "https://api.github.com/graphql".to_owned()
2691}
2692
2693/// The name of a `Status` single-select option on the board.
2694///
2695/// Validated on the way in rather than checked later, so a blank option name — which
2696/// nothing on a board can be — is a state this type cannot hold.
2697#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2698#[serde(try_from = "String")]
2699#[schemars(extend("minLength" = 1))]
2700pub struct ColumnName(String);
2701
2702impl ColumnName {
2703    /// The option name, as the board spells it.
2704    fn as_str(&self) -> &str {
2705        &self.0
2706    }
2707}
2708
2709impl TryFrom<String> for ColumnName {
2710    type Error = String;
2711
2712    fn try_from(name: String) -> Result<Self, Self::Error> {
2713        if name.trim().is_empty() {
2714            return Err("a status_mapping option name cannot be blank".to_owned());
2715        }
2716        Ok(Self(name))
2717    }
2718}
2719
2720/// The two closed states this product can mean.
2721///
2722/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2723/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2724/// product never decides on a person's behalf that two issues are the same work.
2725#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2726#[serde(rename_all = "kebab-case")]
2727pub enum ClosedState {
2728    /// `COMPLETED` — precisely done.
2729    Completed,
2730    /// `NOT_PLANNED` — precisely cancelled.
2731    NotPlanned,
2732}
2733
2734impl ClosedState {
2735    const fn reason(self) -> &'static str {
2736        match self {
2737            Self::Completed => "COMPLETED",
2738            Self::NotPlanned => "NOT_PLANNED",
2739        }
2740    }
2741}
2742
2743/// Configuration for one GitHub Projects v2 board.
2744///
2745/// It serializes to a document this same type reads back as itself, so a caller writing a
2746/// configuration — a consumer filing onto a board it configures — can build one here rather
2747/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2748#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2749#[serde(default, deny_unknown_fields)]
2750pub struct GitHubProjectsConfig {
2751    /// Login of the user or organization which owns the board.
2752    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2753    /// The project number shown in the board's GitHub URL.
2754    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2755    // llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] This doc is the field's schema description, which is what a person configuring the source reads, so it has to say when the field decides an issue's repository and when the item's own field does; the rule's one executable source is `GitHubProjectsSource::creation_target`, and `tests/plugin.rs` drives each case named here against the loopback board.
2756    /// `owner/name` of the repository this source creates an issue in when the item's own
2757    /// `repositories` field does not decide it.
2758    ///
2759    /// An item naming exactly one repository is created there; a task or a document naming
2760    /// none or several is created in its parent project's repository; and a project, or a
2761    /// task or document with no parent, naming none or several is created here. A board
2762    /// has no repository of its own and `createIssue` requires one, so a write without
2763    /// this is refused naming the field. Reads never need it.
2764    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2765    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2766    /// Environment variable containing a fine-grained token with Projects and Issues
2767    /// read/write plus Pull requests read-only access for every repository represented on
2768    /// the board.
2769    #[serde(default = "default_token_env")]
2770    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2771    /// GraphQL endpoint. GitHub Enterprise installations may override it.
2772    #[serde(default = "default_endpoint")]
2773    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2774    /// Per-instance mapping from a status category to the option of the board's one
2775    /// `Status` field it lands on, for a task and for a project.
2776    ///
2777    /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2778    /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2779    /// where a kind left out leaves the category unmapped for that kind. A category this
2780    /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2781    /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2782    /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2783    /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2784    /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2785    /// either kind. No two categories may name one option for the same kind, ignoring case.
2786    /// `unknown` may name one existing option; every unknown word then lands on it and
2787    /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2788    /// each unknown word because it never creates board options.
2789    #[serde(default)]
2790    pub status_mapping: StatusMapping,
2791    /// Per-instance mapping from a task's priority to an option of this board's
2792    /// single-select field named `Priority`.
2793    ///
2794    /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2795    /// other priority is refused before it reaches this board. Present, each of `urgent`,
2796    /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2797    /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2798    /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2799    /// no two levels may name one option. Reads and writes never create the field or an
2800    /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2801    /// the board lacks is refused pointing there.
2802    #[serde(default)]
2803    pub priority_mapping: Option<PriorityMappingConfig>,
2804    /// Caller metadata values this source also writes to board text fields, so the board can
2805    /// filter on them.
2806    ///
2807    /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2808    /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2809    /// value. On every item write — create, update and copy, of a task, a project or a
2810    /// document — a string found there is written to the field, and nothing is sent when the
2811    /// field already holds it; nothing there, or `null`, clears the field when it holds a
2812    /// value; any other JSON type is refused before any mutation. The metadata comment in the
2813    /// issue body stays the one home of the value, and every read reports metadata from it.
2814    /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2815    /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2816    /// non-text field of that name, is refused pointing there.
2817    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2818    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2819    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2820    #[schemars(!skip_serializing_if)]
2821    pub metadata_fields: Vec<MetadataFieldConfig>,
2822    /// How fast this source writes, and how long it waits out a rate-limit refusal.
2823    ///
2824    /// Every field keeps its shipped default when it is absent, and the defaults are
2825    /// GitHub's own published limits rather than taste. See [`Pacing`].
2826    #[serde(default)]
2827    pub pacing: PacingConfig,
2828}
2829
2830/// Which option of the board's `Priority` field each priority lands on.
2831///
2832/// One member per level rather than a map, so a key that is not a level is refused where
2833/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2834/// value in the field, not an option of it.
2835#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2836#[serde(default, deny_unknown_fields)]
2837pub struct PriorityMappingConfig {
2838    /// The option `urgent` lands on; `Urgent` when absent.
2839    pub urgent: Option<PriorityOptionName>,
2840    /// The option `high` lands on; `High` when absent.
2841    pub high: Option<PriorityOptionName>,
2842    /// The option `medium` lands on; `Medium` when absent.
2843    pub medium: Option<PriorityOptionName>,
2844    /// The option `low` lands on; `Low` when absent.
2845    pub low: Option<PriorityOptionName>,
2846}
2847
2848/// The name of an option of the board's `Priority` single-select field.
2849///
2850/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2851/// blank name.
2852#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2853#[serde(try_from = "String")]
2854#[schemars(extend("minLength" = 1))]
2855pub struct PriorityOptionName(String);
2856
2857impl PriorityOptionName {
2858    /// The option name, as the board spells it.
2859    fn as_str(&self) -> &str {
2860        &self.0
2861    }
2862}
2863
2864impl TryFrom<String> for PriorityOptionName {
2865    type Error = String;
2866
2867    fn try_from(name: String) -> Result<Self, Self::Error> {
2868        if name.trim().is_empty() {
2869            return Err("a priority_mapping option name cannot be blank".to_owned());
2870        }
2871        Ok(Self(name))
2872    }
2873}
2874
2875/// The name of the board field a priority is held in.
2876pub const PRIORITY_FIELD: &str = "Priority";
2877
2878/// The four priorities a board option can hold, in the order a new `Priority` field lists
2879/// them. `none` is not among them: it is the field holding no value.
2880///
2881/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2882/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2883/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2884/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2885pub const PRIORITY_LEVELS: [Priority; 4] = [
2886    Priority::Urgent,
2887    Priority::High,
2888    Priority::Medium,
2889    Priority::Low,
2890];
2891
2892/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2893/// see that list for what this pins.
2894#[must_use]
2895pub const fn level_position(priority: Priority) -> Option<usize> {
2896    match priority {
2897        Priority::None => None,
2898        Priority::Urgent => Some(0),
2899        Priority::High => Some(1),
2900        Priority::Medium => Some(2),
2901        Priority::Low => Some(3),
2902    }
2903}
2904
2905/// This instance's complete priority-to-option mapping, read in both directions.
2906///
2907/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2908/// two levels name one option.
2909#[derive(Debug, Clone)]
2910struct PriorityMapping {
2911    options: [PriorityOptionName; 4],
2912}
2913
2914impl PriorityMapping {
2915    fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2916        let shipped = |name: &str| PriorityOptionName(name.to_owned());
2917        let mapping = Self {
2918            options: [
2919                config.urgent.unwrap_or_else(|| shipped("Urgent")),
2920                config.high.unwrap_or_else(|| shipped("High")),
2921                config.medium.unwrap_or_else(|| shipped("Medium")),
2922                config.low.unwrap_or_else(|| shipped("Low")),
2923            ],
2924        };
2925        for (index, option) in mapping.options.iter().enumerate() {
2926            if let Some(earlier) = mapping.options[..index]
2927                .iter()
2928                .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2929            {
2930                return Err(SourceError::Config {
2931                    message: format!(
2932                        "priority_mapping of source {instance} sends both {} and {} to the board \
2933                         option {:?}; one option cannot read back as two priorities",
2934                        PRIORITY_LEVELS[earlier],
2935                        PRIORITY_LEVELS[index],
2936                        option.as_str()
2937                    ),
2938                });
2939            }
2940        }
2941        Ok(mapping)
2942    }
2943
2944    /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2945    fn option(&self, priority: Priority) -> Option<&str> {
2946        level_position(priority).map(|index| self.options[index].as_str())
2947    }
2948
2949    /// The priority a board option name reports, or `None` when nothing maps to it.
2950    fn priority_of(&self, option: &str) -> Option<Priority> {
2951        self.options
2952            .iter()
2953            .position(|name| name.as_str().eq_ignore_ascii_case(option))
2954            .map(|index| PRIORITY_LEVELS[index])
2955    }
2956
2957    /// Every mapped option name, in the order a new `Priority` field lists them.
2958    fn names(&self) -> impl Iterator<Item = &str> {
2959        self.options.iter().map(PriorityOptionName::as_str)
2960    }
2961}
2962
2963/// One metadata value this source projects onto a board text field.
2964#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2965#[serde(deny_unknown_fields)]
2966pub struct MetadataFieldConfig {
2967    /// The name of the board text field the value is written to.
2968    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.
2969    /// The top-level metadata key the value is read from. A key under the reserved
2970    /// `onetaskgraph.` prefix is refused.
2971    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.
2972    /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2973    /// key's own value is the one projected. Left out of a serialized entry when empty.
2974    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2975    // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
2976    #[schemars(!skip_serializing_if)]
2977    pub path: Vec<String>,
2978}
2979
2980/// The board fields no metadata value may be projected onto, compared ignoring case: the
2981/// three this source writes itself, and those GitHub owns on every board.
2982// 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.
2983const UNPROJECTABLE_FIELDS: [&str; 13] = [
2984    STATUS_FIELD,
2985    PRIORITY_FIELD,
2986    ORIGIN_FIELD,
2987    "Title",
2988    "Assignees",
2989    "Labels",
2990    "Linked pull requests",
2991    "Milestone",
2992    "Repository",
2993    "Reviewers",
2994    "Parent issue",
2995    "Sub-issues progress",
2996    "Type",
2997];
2998
2999/// One validated [`MetadataFieldConfig`] entry.
3000#[derive(Debug, Clone)]
3001struct MetadataField {
3002    field: ProjectedField,
3003    key: ProjectedKey,
3004    path: Vec<String>,
3005}
3006
3007/// The name of a board text field a metadata value is projected onto: never blank, never one
3008/// this source or GitHub writes, and never one another entry of the same source names.
3009#[derive(Debug, Clone, PartialEq, Eq)]
3010struct ProjectedField(String);
3011
3012/// The top-level metadata key a projected value is read from: never blank, and never under
3013/// the namespace this product reserves.
3014#[derive(Debug, Clone, PartialEq, Eq)]
3015struct ProjectedKey(String);
3016
3017impl std::ops::Deref for ProjectedField {
3018    type Target = str;
3019    fn deref(&self) -> &str {
3020        &self.0
3021    }
3022}
3023
3024impl std::ops::Deref for ProjectedKey {
3025    type Target = str;
3026    fn deref(&self) -> &str {
3027        &self.0
3028    }
3029}
3030
3031/// What one item's metadata says one projected field should hold.
3032#[derive(Debug, Clone, PartialEq, Eq)]
3033enum Projected {
3034    /// A string to hold.
3035    Text(String),
3036    /// Nothing: the key or a step of the path is absent, or the value is `null`.
3037    Nothing,
3038}
3039
3040impl MetadataField {
3041    /// Validate every entry of one instance's `metadata_fields`, refusing the first that
3042    /// cannot stand, naming the source and the entry.
3043    fn resolve(
3044        configured: Vec<MetadataFieldConfig>,
3045        instance: &SourceName,
3046    ) -> Result<Vec<Self>, SourceError> {
3047        let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
3048        for (index, entry) in configured.into_iter().enumerate() {
3049            let refuse = |why: String| SourceError::Config {
3050                message: format!(
3051                    "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
3052                    entry.field, entry.key
3053                ),
3054            };
3055            if entry.field.trim().is_empty() {
3056                return Err(refuse("names a blank field".to_owned()));
3057            }
3058            if entry.key.trim().is_empty() {
3059                return Err(refuse("names a blank key".to_owned()));
3060            }
3061            if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
3062                return Err(refuse(format!(
3063                    "names a key under the reserved \"{}.\" prefix, which this product keeps \
3064                     for its own keys; next: project a caller key instead",
3065                    MetadataKey::RESERVED_NAMESPACE
3066                )));
3067            }
3068            if let Some(owned) = UNPROJECTABLE_FIELDS
3069                .iter()
3070                .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
3071            {
3072                return Err(refuse(format!(
3073                    "names the board field {owned:?}, which this source or GitHub already \
3074                     writes; next: name a text field of your own"
3075                )));
3076            }
3077            if let Some(earlier) = resolved
3078                .iter()
3079                .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
3080            {
3081                return Err(refuse(format!(
3082                    "names the same board field as metadata_fields[{earlier}]; one field \
3083                     cannot hold two values"
3084                )));
3085            }
3086            resolved.push(Self {
3087                field: ProjectedField(entry.field),
3088                key: ProjectedKey(entry.key),
3089                path: entry.path,
3090            });
3091        }
3092        Ok(resolved)
3093    }
3094
3095    /// The path as a refusal spells it: `[]` for the key's own value.
3096    fn spelled_path(&self) -> String {
3097        serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
3098    }
3099
3100    /// What `metadata` says this field should hold, or the refusal for a value no text field
3101    /// can hold.
3102    fn projected(
3103        &self,
3104        metadata: &BTreeMap<String, Value>,
3105        instance: &SourceName,
3106    ) -> Result<Projected, SourceError> {
3107        let mut value = metadata.get(&*self.key);
3108        for step in &self.path {
3109            value = value.and_then(|held| held.get(step.as_str()));
3110        }
3111        let found = match value {
3112            None | Some(Value::Null) => return Ok(Projected::Nothing),
3113            Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
3114            Some(Value::Bool(_)) => "a boolean",
3115            Some(Value::Number(_)) => "a number",
3116            Some(Value::Array(_)) => "an array",
3117            Some(Value::Object(_)) => "an object",
3118        };
3119        Err(SourceError::Refused {
3120            message: format!(
3121                "source {instance} projects metadata key {:?} at path {} onto its board text \
3122                 field {:?}, and this item holds {found} there; only a string, null or nothing \
3123                 can be written to a text field; next: store a string there, or remove the \
3124                 value",
3125                &*self.key,
3126                self.spelled_path(),
3127                &*self.field
3128            ),
3129        })
3130    }
3131}
3132
3133/// What one item's `Priority` field says, read through this instance's mapping.
3134#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
3135enum HeldPriority {
3136    /// A priority this source reports: an option the mapping names, or no value (`none`).
3137    Read(Priority),
3138    /// An option the mapping does not name, which is never read as a level or as `none`.
3139    Unmapped(String),
3140}
3141
3142/// How fast this source writes, and how long it waits out a rate-limit refusal.
3143///
3144/// Configurable because a GitHub Enterprise installation sets its own limits and an
3145/// operator who has already been refused may want to go slower still — not because the
3146/// defaults are guesses.
3147#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3148#[serde(default, deny_unknown_fields)]
3149pub struct PacingConfig {
3150    /// Shortest interval between two content-creating mutations, in milliseconds.
3151    ///
3152    /// Zero sends them as fast as they are asked for, which is what a fixture server on
3153    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
3154    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.
3155    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
3156    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
3157    /// zero while there is a budget to spend, because a schedule of zero-length waits
3158    /// consumes none of it and so never ends.
3159    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.
3160    /// Total time one call may spend waiting out rate limits, in milliseconds.
3161    ///
3162    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
3163    /// the bound is what makes this a wait rather than a hang.
3164    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.
3165}
3166
3167/// The largest any pacing setting may be, in milliseconds.
3168///
3169/// One hour. GitHub's own harshest published bound on content-generating requests works
3170/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
3171/// anything a real limit asks for, and past it the settings stop describing pacing at all:
3172/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
3173/// and an interval beyond it is a command that never sends its second mutation. It also
3174/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
3175/// what a `Duration` can hold on every platform.
3176pub const MAX_PACING_MS: u64 = 3_600_000;
3177
3178/// [`PacingConfig`] with every default resolved and every value checked, which is what the
3179/// source holds.
3180///
3181/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
3182/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
3183/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
3184/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
3185/// is asked for, windows included.
3186#[derive(Debug, Clone, Copy)]
3187struct Pacing {
3188    /// Whether requests are held for GitHub's secondary windows: always, unless
3189    /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
3190    /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
3191    windows: bool,
3192    min_mutation_interval: Duration,
3193    retry_backoff: Duration,
3194    retry_budget: Duration,
3195}
3196
3197impl Pacing {
3198    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
3199    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3200        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
3201            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
3202                message: format!(
3203                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3204                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3205                     GitHub's own harshest published limit"
3206                ),
3207            }),
3208            Some(value) => Ok(Duration::from_millis(value)),
3209            None => Ok(Duration::from_millis(default)),
3210        };
3211        let retry_backoff = bounded(
3212            config.retry_backoff_ms,
3213            RETRY_BACKOFF_MS,
3214            "retry_backoff_ms",
3215        )?;
3216        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3217        if retry_backoff.is_zero() && !retry_budget.is_zero() {
3218            return Err(SourceError::Config {
3219                message: format!(
3220                    "pacing.retry_backoff_ms of source {instance} is 0 while \
3221                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3222                     none of that budget, so it would retry a refusal forever. Set a backoff of \
3223                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3224                     waiting at all",
3225                    retry_budget.as_millis()
3226                ),
3227            });
3228        }
3229        Ok(Self {
3230            windows: config.min_mutation_interval_ms != Some(0),
3231            min_mutation_interval: bounded(
3232                config.min_mutation_interval_ms,
3233                0,
3234                "min_mutation_interval_ms",
3235            )?,
3236            retry_backoff,
3237            retry_budget,
3238        })
3239    }
3240}
3241
3242/// Factory for [`GitHubProjectsSource`].
3243#[derive(Debug, Clone, Copy, Default)]
3244pub struct Plugin;
3245
3246impl SourcePlugin for Plugin {
3247    fn kind(&self) -> &'static str {
3248        KIND
3249    }
3250    /// The endpoint, the owner and the board's number: what names one board.
3251    fn remote_identity(&self, config: &Value) -> Option<String> {
3252        let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
3253        Some(format!(
3254            "{}\n{}\n{}",
3255            config.endpoint, config.owner, config.project_number
3256        ))
3257    }
3258    fn config_schema(&self) -> Schema {
3259        schema_for!(GitHubProjectsConfig)
3260    }
3261    fn build(
3262        &self,
3263        name: &SourceName,
3264        config: &Value,
3265        secrets: &dyn SecretResolver,
3266    ) -> Result<Box<dyn TaskSource>, SourceError> {
3267        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
3268    }
3269
3270    fn build_with_clock(
3271        &self,
3272        name: &SourceName,
3273        config: &Value,
3274        secrets: &dyn SecretResolver,
3275        clock: SharedClock,
3276    ) -> Result<Box<dyn TaskSource>, SourceError> {
3277        self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
3278    }
3279}
3280
3281/// The client `cell` holds, building it with `build` the first time it is asked for.
3282///
3283/// A failed build is the request's failure, with the message the source's construction gave it
3284/// before clients were deferred.
3285// 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`.
3286fn built(
3287    cell: &std::sync::OnceLock<Client>,
3288    build: impl FnOnce() -> Result<Client, SourceError>,
3289) -> Result<&Client, SourceError> {
3290    if let Some(client) = cell.get() {
3291        return Ok(client);
3292    }
3293    let client = build()?;
3294    Ok(cell.get_or_init(|| client))
3295}
3296
3297impl Plugin {
3298    /// Build a source recording every request it sends into an accounting the caller holds.
3299    ///
3300    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
3301    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
3302    /// session total rather than two — see [`accounting`] and
3303    /// [`GitHubProjectsSource::recording_into`].
3304    ///
3305    /// # Errors
3306    ///
3307    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
3308    /// [`SourceError::Config`] for configuration this plugin cannot use and
3309    /// [`SourceError::Auth`] for a credential it cannot find.
3310    pub fn build_recording_into(
3311        &self,
3312        name: &SourceName,
3313        config: &Value,
3314        secrets: &dyn SecretResolver,
3315        ledger: Arc<Accounting>,
3316    ) -> Result<Box<dyn TaskSource>, SourceError> {
3317        self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
3318    }
3319
3320    fn build_recording_with_clock(
3321        &self,
3322        name: &SourceName,
3323        config: &Value,
3324        secrets: &dyn SecretResolver,
3325        ledger: Arc<Accounting>,
3326        clock: SharedClock,
3327    ) -> Result<Box<dyn TaskSource>, SourceError> {
3328        let config: GitHubProjectsConfig =
3329            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
3330                message: format!("source {name}: {e}"),
3331            })?;
3332        let prefix = format!("source {name}: ");
3333        let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
3334            .map_err(|error| match error {
3335                // The shared `StatusMapping::distinct` names the source itself.
3336                SourceError::Config { message } if message.starts_with(&prefix) => {
3337                    SourceError::Config { message }
3338                }
3339                SourceError::Config { message } => SourceError::Config {
3340                    message: format!("{prefix}{message}"),
3341                },
3342                SourceError::Auth { message } => SourceError::Auth {
3343                    message: format!("source {name}: {message}"),
3344                },
3345                other => other,
3346            })?;
3347        source.clock = clock;
3348        Ok(Box::new(source))
3349    }
3350}
3351
3352/// Where a status category lands on this board, once configuration is resolved.
3353#[derive(Debug, Clone, PartialEq, Eq)]
3354enum StatusTarget {
3355    /// Not usable against this instance for this kind, and why.
3356    Disabled(UnmappedStatus),
3357    /// The board's `Status` option of this name.
3358    Column(ColumnName),
3359    /// A closed issue, with both its board option and the reason that says which closed it means.
3360    // 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.
3361    Terminal(ColumnName, ClosedState),
3362}
3363
3364/// Every status category, in the order the vocabulary declares them.
3365///
3366/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
3367/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
3368/// added to the shared vocabulary fails to compile until it is named there, and this
3369/// crate's suite reconciles this list against that enum's own derived schema, which is
3370/// generated from the variants rather than written beside them. The schema is what
3371/// catches a list left one short — a list checking only the positions it already holds
3372/// would pass while every mapping indexed by the new position panicked.
3373pub const CATEGORIES: [StatusCategory; 8] = [
3374    StatusCategory::Draft,
3375    StatusCategory::Backlog,
3376    StatusCategory::Todo,
3377    StatusCategory::Queued,
3378    StatusCategory::InProgress,
3379    StatusCategory::Done,
3380    StatusCategory::Cancelled,
3381    StatusCategory::Unknown,
3382];
3383
3384/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
3385#[must_use]
3386pub const fn category_position(category: StatusCategory) -> usize {
3387    match category {
3388        StatusCategory::Draft => 0,
3389        StatusCategory::Backlog => 1,
3390        StatusCategory::Todo => 2,
3391        StatusCategory::Queued => 3,
3392        StatusCategory::InProgress => 4,
3393        StatusCategory::Done => 5,
3394        StatusCategory::Cancelled => 6,
3395        StatusCategory::Unknown => 7,
3396    }
3397}
3398
3399/// The spelling a status category is configured and reported under.
3400fn category_name(category: StatusCategory) -> &'static str {
3401    match category {
3402        StatusCategory::Draft => "draft",
3403        StatusCategory::Backlog => "backlog",
3404        StatusCategory::Todo => "todo",
3405        StatusCategory::Queued => "queued",
3406        StatusCategory::InProgress => "in-progress",
3407        StatusCategory::Done => "done",
3408        StatusCategory::Cancelled => "cancelled",
3409        StatusCategory::Unknown => "unknown",
3410    }
3411}
3412
3413/// A shipped default's option name.
3414///
3415/// The literals below are this file's own and non-blank, and they are validated by the
3416/// one constructor a configured name goes through rather than beside it.
3417fn shipped_column(name: &'static str) -> ColumnName {
3418    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
3419}
3420
3421/// The shipped default for one category this instance's `status_mapping` does not mention,
3422/// for either kind.
3423fn shipped_default(category: StatusCategory) -> StatusTarget {
3424    match category {
3425        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
3426        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
3427        StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
3428        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
3429        StatusCategory::Done => {
3430            StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
3431        }
3432        StatusCategory::Cancelled => {
3433            StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
3434        }
3435        StatusCategory::Draft | StatusCategory::Unknown => {
3436            StatusTarget::Disabled(UnmappedStatus::Unconfigured)
3437        }
3438    }
3439}
3440
3441/// The two kinds a status is written and read for, each with its own half of the mapping.
3442const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
3443
3444/// This instance's complete category-to-target mapping for each kind, read in both
3445/// directions.
3446///
3447/// One target per category per kind, held at that category's own [`category_position`], so
3448/// a category missing from the mapping, named twice in it, or filed out of order is a state
3449/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
3450/// targets are options of the board's one `Status` field.
3451#[derive(Debug, Clone)]
3452struct BoardStatuses {
3453    tasks: [StatusTarget; CATEGORIES.len()],
3454    projects: [StatusTarget; CATEGORIES.len()],
3455}
3456
3457impl BoardStatuses {
3458    /// Resolve `configured` against the shipped defaults, refusing two categories one kind
3459    /// would read back from one option.
3460    ///
3461    /// A category the mapping does not mention keeps its shipped default for both kinds; one
3462    /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
3463    /// omits unmapped rather than defaulted.
3464    fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
3465        let resolve_kind =
3466            |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
3467                // `CATEGORIES[position] == category` for every category — the crate's suite
3468                // asserts it — so mapping the list in order fills each category's own slot.
3469                let mut targets = CATEGORIES.map(shipped_default);
3470                for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
3471                    if !configured.mentions(category) {
3472                        continue;
3473                    }
3474                    *slot = match configured.name_for(category, kind) {
3475                        Err(why) => StatusTarget::Disabled(why),
3476                        Ok(name) => {
3477                            let option = ColumnName::try_from(name.as_str().to_owned())
3478                                .map_err(|message| SourceError::Config { message })?;
3479                            match category {
3480                                StatusCategory::Done => {
3481                                    StatusTarget::Terminal(option, ClosedState::Completed)
3482                                }
3483                                StatusCategory::Cancelled => {
3484                                    StatusTarget::Terminal(option, ClosedState::NotPlanned)
3485                                }
3486                                _ => StatusTarget::Column(option),
3487                            }
3488                        }
3489                    };
3490                }
3491                StatusMapping::distinct(
3492                    instance,
3493                    kind,
3494                    CATEGORIES
3495                        .iter()
3496                        .zip(&targets)
3497                        .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
3498                )?;
3499                Ok(targets)
3500            };
3501        Ok(Self {
3502            tasks: resolve_kind(ItemKind::Task)?,
3503            projects: resolve_kind(ItemKind::Project)?,
3504        })
3505    }
3506
3507    /// Every category's target for `kind`, in category order.
3508    const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
3509        match kind {
3510            ItemKind::Task => &self.tasks,
3511            ItemKind::Project => &self.projects,
3512        }
3513    }
3514
3515    fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
3516        &self.targets(kind)[category_position(category)]
3517    }
3518
3519    /// The category a board option name reports for `kind`, or `None` when nothing of that
3520    /// kind maps to it.
3521    fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
3522        CATEGORIES.into_iter().find(|category| {
3523            self.target(kind, *category)
3524                .option()
3525                .is_some_and(|name| name.eq_ignore_ascii_case(option))
3526        })
3527    }
3528
3529    /// Every option name either kind maps a category to, each once ignoring case, in
3530    /// category order with a task's name before a project's — what the guarded setup asks
3531    /// the `Status` field to hold.
3532    fn wanted(&self) -> Vec<String> {
3533        let mut wanted: Vec<String> = Vec::new();
3534        for category in CATEGORIES {
3535            for kind in STATUS_KINDS {
3536                if let Some(name) = self.target(kind, category).option()
3537                    && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
3538                {
3539                    wanted.push(name.to_owned());
3540                }
3541            }
3542        }
3543        wanted
3544    }
3545
3546    /// The status an item of `kind` reports, from the three things a read of it says: its
3547    /// board `Status` option, whether its issue is closed, and the reason it was closed with.
3548    ///
3549    /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
3550    /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
3551    /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
3552    /// produce, so it is read permissively rather than refused — reads are faithful, and
3553    /// refusals belong on writes.
3554    ///
3555    /// **Its name follows the closure too, unless the option agrees with it.** The option's
3556    /// own name is kept when this kind's mapping reads that option as the same category, or
3557    /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3558    /// named `Shipped`. An option the mapping reads as another category contradicts the
3559    /// closure, and so does no option at all: the name is then the option the mapping gives
3560    /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3561    /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3562    /// it. An `unknown` closure keeps the option, or `Closed` without one.
3563    ///
3564    /// An open item's option reads through its own kind's mapping, and an option that mapping
3565    /// does not name reads as `Unknown` under its own name.
3566    ///
3567    /// One function of those three rather than of a response, so a narrow status write can
3568    /// answer what a re-read would report by applying it to the state it has just written.
3569    fn status(
3570        &self,
3571        kind: ItemKind,
3572        option: Option<&str>,
3573        closed: bool,
3574        reason: Option<&str>,
3575    ) -> Status {
3576        if closed {
3577            let category = closed_category(reason);
3578            let agrees = option.filter(|option| {
3579                category == StatusCategory::Unknown
3580                    || self
3581                        .category_of(kind, option)
3582                        .is_none_or(|read| read == category)
3583            });
3584            let name = agrees.or_else(|| self.target(kind, category).option());
3585            let fallback = match category {
3586                StatusCategory::Done => "Done",
3587                StatusCategory::Cancelled => "Cancelled",
3588                _ => "Closed",
3589            };
3590            return Status {
3591                category,
3592                name: name.or(option).unwrap_or(fallback).to_owned(),
3593            };
3594        }
3595        let name = option.unwrap_or("Open").to_owned();
3596        Status {
3597            category: self
3598                .category_of(kind, &name)
3599                .unwrap_or(StatusCategory::Unknown),
3600            name,
3601        }
3602    }
3603
3604    /// The option a closed item's `Status` has to hold for its card to agree with its
3605    /// closure, and the category that closure reads as — or `None` when the item is open,
3606    /// its closure reads `unknown`, or its option already agrees.
3607    ///
3608    /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3609    /// in line, each to the one option the mapping gives its category — an item with no
3610    /// option, or at one the mapping does not name, included.
3611    fn contradicted(
3612        &self,
3613        kind: ItemKind,
3614        option: Option<&str>,
3615        closed: bool,
3616        reason: Option<&str>,
3617    ) -> Option<(ClosedAs, &str)> {
3618        if !closed {
3619            return None;
3620        }
3621        let closed_as = match closed_category(reason) {
3622            StatusCategory::Done => ClosedAs::Done,
3623            StatusCategory::Cancelled => ClosedAs::Cancelled,
3624            _ => return None,
3625        };
3626        let wanted = self.target(kind, closed_as.category()).option()?;
3627        if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3628            return None;
3629        }
3630        Some((closed_as, wanted))
3631    }
3632}
3633
3634/// The category a closed issue reads as, from the reason it was closed with.
3635///
3636/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3637/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3638/// is one this source cannot read a category from.
3639fn closed_category(reason: Option<&str>) -> StatusCategory {
3640    match reason {
3641        None | Some("COMPLETED") => StatusCategory::Done,
3642        Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3643        Some(_) => StatusCategory::Unknown,
3644    }
3645}
3646
3647impl BoardStatuses {
3648    /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3649    /// case; a kind lacking none is left out.
3650    fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3651        STATUS_KINDS
3652            .into_iter()
3653            .filter_map(|kind| {
3654                let missing: Vec<String> = self
3655                    .targets(kind)
3656                    .iter()
3657                    .filter_map(StatusTarget::option)
3658                    .filter(|wanted| {
3659                        !existing
3660                            .iter()
3661                            .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3662                    })
3663                    .map(str::to_owned)
3664                    .collect();
3665                (!missing.is_empty()).then_some(KindMissing { kind, missing })
3666            })
3667            .collect()
3668    }
3669}
3670
3671impl StatusTarget {
3672    /// The board option this target selects, or `None` for an unmapped one.
3673    fn option(&self) -> Option<&str> {
3674        match self {
3675            Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3676            Self::Disabled(_) => None,
3677        }
3678    }
3679}
3680
3681// 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.
3682/// One repository this source can create an issue in, as `owner/name`.
3683///
3684/// Every `createIssue` this source sends names one of these: the item's own single
3685/// `repositories` entry, else its parent project issue's repository, else the configured
3686/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3687/// that choice and says what it refuses before `createIssue`.
3688// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3689#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
3690struct RepositoryTarget {
3691    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3692    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3693}
3694
3695impl RepositoryTarget {
3696    fn parse(value: &str) -> Result<Self, SourceError> {
3697        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3698            message: format!(
3699                "repository must be spelled owner/name; {value:?} names no repository"
3700            ),
3701        })?;
3702        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3703            return Err(SourceError::Config {
3704                message: format!(
3705                    "repository must be spelled owner/name with a GitHub login and one \
3706                     repository name; {value:?} is not"
3707                ),
3708            });
3709        }
3710        Ok(Self {
3711            owner: owner.to_owned(),
3712            name: name.to_owned(),
3713        })
3714    }
3715
3716    /// The one host whose repositories this source creates issues in, spelled once: it is
3717    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3718    const HOST: &str = "github.com";
3719
3720    fn origin(&self) -> String {
3721        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3722    }
3723
3724    /// The repository a normalized origin names, or why it is none this source can create
3725    /// an issue in: another host, or more or fewer than `owner/name` under this one.
3726    fn from_origin(origin: &Repository) -> Result<Self, String> {
3727        let not_here = || {
3728            format!(
3729                "{} is not a {}/owner/name repository",
3730                origin.as_str(),
3731                Self::HOST
3732            )
3733        };
3734        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3735        if host != Self::HOST {
3736            return Err(not_here());
3737        }
3738        Self::parse(rest).map_err(|_| not_here())
3739    }
3740
3741    fn slug(&self) -> String {
3742        format!("{}/{}", self.owner, self.name)
3743    }
3744}
3745
3746/// A source which reads GitHub afresh for every operation.
3747pub struct GitHubProjectsSource {
3748    /// This source's configured name, used both to tell a far end naming this source
3749    /// from one naming a system it knows nothing about, and to name the instance a
3750    /// status refusal is about.
3751    name: SourceName,
3752    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3753    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3754    repository: Option<RepositoryTarget>,
3755    endpoint: Url,
3756    token: SecretString,
3757    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3758    statuses: BoardStatuses,
3759    /// Where each priority lands on this board, or `None` when this instance holds none.
3760    priorities: Option<PriorityMapping>,
3761    /// The metadata values this instance projects onto board text fields, in configured order.
3762    metadata_fields: Vec<MetadataField>,
3763    /// Built on the first request rather than with the source, as [`Self::client`] says.
3764    client: std::sync::OnceLock<Client>,
3765    /// Built on the first attachment request, as [`Self::asset_client`] says.
3766    asset_client: std::sync::OnceLock<Client>,
3767    /// Every item this source has created in this command, in the order it created them —
3768    /// dropped by [`TaskSource::end_command`].
3769    ///
3770    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3771    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3772    /// a copy resolving a dependency on an item it had just created refused it as not
3773    /// found. A board read is completed from this — an item remembered here and absent from
3774    /// the read is added back, because the board really does hold it and only the read is
3775    /// behind.
3776    ///
3777    /// It is not a cache of a user's work: nothing is remembered that this process did not
3778    /// itself just write, it lives and dies with the process, and it is never consulted for
3779    /// an item this source did not create.
3780    created: Mutex<Vec<Resolved>>,
3781    /// Every item that already existed and that this source has written in this command, as
3782    /// it wrote it — dropped by [`TaskSource::end_command`].
3783    ///
3784    /// The other half of [`Self::created`], held on the same terms and for the reason a
3785    /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3786    /// filter is an index behind a write this process made moments ago, so a query matching
3787    /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3788    /// is remembered that this process did not itself just write.
3789    updated: Mutex<Vec<Resolved>>,
3790    /// Every issue this source has added a comment to or edited a comment of in this command
3791    /// — dropped by [`TaskSource::end_command`].
3792    ///
3793    /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3794    /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3795    /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3796    /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3797    /// from before — until the index caught up. Each id here is a candidate of every such
3798    /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3799    /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3800    /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3801    /// process wrote is still found only once the index has it.
3802    commented: Mutex<Vec<NativeId>>,
3803    /// How fast this source writes, and how long it waits out a refusal.
3804    pacing: Pacing,
3805    /// When the last content-creating mutation finished, or the moment the furthest-out
3806    /// reserved slot releases the next one, whichever is later — so the one after it can be
3807    /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
3808    /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
3809    /// measured from.
3810    last_mutation: Mutex<Option<Duration>>,
3811    /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
3812    windows: Mutex<pacing::Windows>,
3813    /// The tasks a project holds, as a copy's batched read of the project and every one of them
3814    /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
3815    /// a listing of that project's tasks is answered without a read of its own.
3816    task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
3817    /// The web address of each issue this source created in the current command, as its
3818    /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
3819    /// request.
3820    written: Mutex<BTreeMap<NativeId, String>>,
3821    /// What a copy has held back of the items it created, between
3822    /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
3823    /// nothing is being held back, when every write lands whole as it is made.
3824    deferred: Mutex<Option<Deferred>>,
3825    /// Each existing issue's update held back while writes are, in the order it was made: sent
3826    /// together by [`Self::send_held_updates`] before any other request goes out, so nothing
3827    /// this source sends or reads ever sees the board without them.
3828    held_updates: Mutex<Vec<HeldUpdate>>,
3829    clock: SharedClock,
3830    numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3831    /// The board as this process last read it, for the length of one command — dropped by
3832    /// [`TaskSource::end_command`].
3833    ///
3834    /// A copy of a project used to re-read the whole board, paged, before writing each of
3835    /// its items, which is by far the largest part of a copy's request count and none of
3836    /// its work. Nothing else changes this board while a command runs — this source's own
3837    /// writes are the only writer — so one read answers them all.
3838    ///
3839    /// It is not a store of a user's work and it is not the cache the no-persistence
3840    /// invariant forbids: it lives and dies with the process exactly as `created` does,
3841    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3842    /// an item this command created and then depends on resolves whether or not GitHub's
3843    /// own eventually-consistent read has caught up. A write to an item already on the
3844    /// board updates the entry here too, so what this holds is the last read plus this
3845    /// process's own writes rather than a snapshot taken before them.
3846    board_cache: Mutex<Option<Board>>,
3847    /// Every issue this board's own search reported, for the length of one command — dropped
3848    /// by [`TaskSource::end_command`].
3849    ///
3850    /// The second half of a board read, and cached for the same reason and on the same
3851    /// terms as the first: it lives and dies with the process, nothing is written down, and
3852    /// a write this process makes updates the entry here exactly as it updates the one in
3853    /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3854    /// that lists this board's projects and its tasks pays for one search rather than two.
3855    search_cache: Mutex<Option<Vec<Resolved>>>,
3856    /// What each narrowed question GitHub was asked answered, keyed by that question, for
3857    /// the length of one command — dropped by [`TaskSource::end_command`].
3858    ///
3859    /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3860    /// and dies with the process, nothing is written down, a write this process makes
3861    /// updates the entry here as it updates the other two, and every answer is completed
3862    /// with this process's own writes each time it is given. A command that asks the same
3863    /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3864    /// write — pays for it once, which is what the whole-board read it replaced gave it.
3865    narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3866    search_next: Mutex<BTreeMap<String, Option<String>>>,
3867    /// Records already resolved in this command, reused by writes and for comment identity.
3868    /// Explicit item reads still reach GitHub. Nothing is persisted, and
3869    /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3870    /// its item as a person has since left it.
3871    resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3872    /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
3873    /// answered as holding nothing — each taken by the next read of that item by id and by
3874    /// nothing else, dropped by any mutation naming it, and cleared by
3875    /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
3876    /// read-before-write, made in the same request as the others' rather than on its own.
3877    read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
3878    /// The origins [`TaskSource::read_ahead_origins_for_copy`] was asked about and has not sent
3879    /// yet: held for the [`TaskSource::read_ahead_for_copy`] that follows it, which sends them —
3880    /// beside the members of the one project it names, when it names one — or for the next
3881    /// request this source makes, whichever comes first.
3882    pending_origins: Mutex<Vec<String>>,
3883    /// Each project's members as a lookup carrying them read them in this command, trimmed to
3884    /// their classification and repositories, taken by [`TaskSource::member_classifications`]
3885    /// of that project and by nothing else, and cleared by [`TaskSource::end_command`].
3886    member_classes: Mutex<BTreeMap<NativeId, Vec<MemberClass>>>,
3887    /// Every item a listing of one project's members carried whole in this command — the
3888    /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
3889    /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
3890    /// it again. No other read is answered from it, so a read by id outside a copy's
3891    /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
3892    /// item, and cleared by [`TaskSource::end_command`].
3893    carried: Mutex<BTreeMap<NativeId, Resolved>>,
3894    /// Items an issue read by reference reached, held for the one status write each was read
3895    /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
3896    /// what such a write needs. Dropped by [`TaskSource::end_command`].
3897    held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3898    /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3899    /// asked for under, with the project that selector named — for the length of one command,
3900    /// dropped by [`TaskSource::end_command`].
3901    ///
3902    /// A caller pages through a project's tasks one engine page at a time, and every page
3903    /// is cut from the whole list of them, so without this each page walked every
3904    /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3905    /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3906    /// dies with the process, nothing is written down, a write this process makes updates or
3907    /// removes the entry here as it does there, and every answer is completed with this
3908    /// process's own writes each time it is given.
3909    children_cache: Mutex<ProjectChildren>,
3910    /// The board's own id and field definitions as this process last read them on their
3911    /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3912    ///
3913    /// What a write needs of the board and its item does not say, read once per command
3914    /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3915    /// lives and dies with the process and nothing is written down. It holds no item and so
3916    /// can answer no question about one — see [`Self::board_fields`].
3917    fields_cache: Mutex<Option<BoardFields>>,
3918    /// Each destination repository's node id, resolved once per repository
3919    /// rather than per issue created.
3920    ///
3921    /// A repository's node id does not change, and re-reading it for every issue of a copy
3922    /// spent one request per item on an answer this source already had. It is a map rather
3923    /// than one entry because a copy files each item in the repository its own
3924    /// `repositories` field names, so a plan across five repositories asks GitHub five
3925    /// times and not once per item.
3926    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3927    /// What every request this source sends is recorded into.
3928    ///
3929    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3930    /// a request leaves this crate, so nothing has to be switched on for a session to be
3931    /// counted. It is shared rather than owned so a caller accounting for a whole session —
3932    /// its own schema verification, board lookups, residue sweep and cleanup beside this
3933    /// source's reads and writes — adds up one accounting instead of two. See
3934    /// [`accounting`] for what a record carries and what a session's spend is and is not.
3935    ledger: Arc<Accounting>,
3936    /// What [`expected`] holds for a write the store's cache expects: the journal while one is
3937    /// open, the issues primed from the cache, and the comments known on each.
3938    expected: expected::Held,
3939}
3940
3941/// GitHub's closed single-select color vocabulary.
3942#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3943#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
3944pub enum StatusOptionColor {
3945    /// Gray.
3946    Gray,
3947    /// Blue.
3948    Blue,
3949    /// Green.
3950    Green,
3951    /// Yellow.
3952    Yellow,
3953    /// Purple.
3954    Purple,
3955    /// Red.
3956    Red,
3957    /// Orange.
3958    Orange,
3959    /// Pink.
3960    Pink,
3961}
3962
3963/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
3964/// applies its additions.
3965#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3966pub enum SetupMode {
3967    /// Read without mutation.
3968    Plan,
3969    /// Apply and verify.
3970    Apply,
3971}
3972
3973/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
3974/// against it goes on compiling.
3975pub type StatusOptionsMode = SetupMode;
3976
3977/// The explicit result of the requested operation.
3978#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3979#[serde(rename_all = "kebab-case")]
3980pub enum StatusOptionsOutcome {
3981    /// A read-only plan.
3982    Planned,
3983    /// Apply found nothing missing.
3984    Unchanged,
3985    /// Additions were applied and verified.
3986    Applied,
3987}
3988
3989/// A GitHub single-select option's opaque GraphQL node identifier.
3990#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3991#[serde(transparent)]
3992pub struct StatusOptionId(#[schemars(length(min = 1))] String);
3993
3994impl TryFrom<String> for StatusOptionId {
3995    type Error = String;
3996
3997    fn try_from(id: String) -> Result<Self, Self::Error> {
3998        if id.trim().is_empty() {
3999            return Err("a GitHub Status option id cannot be blank".to_owned());
4000        }
4001        Ok(Self(id))
4002    }
4003}
4004
4005/// One existing or proposed option in a guarded Status-field update.
4006#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4007pub struct StatusOption {
4008    /// GitHub's stable id.
4009    pub id: StatusOptionId,
4010    /// The visible option name.
4011    pub name: ColumnName,
4012    /// GitHub's single-select color token.
4013    pub color: StatusOptionColor,
4014    /// The option description, including an empty one.
4015    pub description: String,
4016}
4017
4018/// One board item's Status assignment, retained as recovery data.
4019#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4020pub struct StatusAssignment {
4021    /// The project item id whose assignment this is.
4022    // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
4023    // carried verbatim as operator recovery data; introducing a semantic type would claim
4024    // validation rules GitHub does not publish and no operation here interprets.
4025    pub item_id: String,
4026    /// The selected option, absent when the item has no status.
4027    #[serde(skip_serializing_if = "Option::is_none")]
4028    pub option: Option<AssignedStatusOption>,
4029}
4030
4031/// The inseparable id and name of an assigned option.
4032#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4033pub struct AssignedStatusOption {
4034    /// GitHub's stable id.
4035    pub id: StatusOptionId,
4036    /// The visible name.
4037    pub name: ColumnName,
4038}
4039
4040/// The plan and verified outcome of reconciling configured Status options.
4041#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4042pub struct StatusOptionsReport {
4043    /// The configured source name.
4044    pub source: SourceName,
4045    /// Configured option names absent before the operation.
4046    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
4047    // `ColumnName` and has therefore already passed its nonblank validation; retaining the
4048    // serialized string here preserves the report's intentionally simple public contract.
4049    pub missing: Vec<String>,
4050    /// What the requested operation did.
4051    pub outcome: StatusOptionsOutcome,
4052    /// The complete option list observed before any mutation.
4053    pub existing: Vec<StatusOption>,
4054}
4055
4056#[derive(Debug, Clone, PartialEq, Eq)]
4057struct StatusSnapshot {
4058    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4059    // passed back as the mutation's project identity; a newtype could enforce no stronger
4060    // invariant because GitHub publishes no grammar for it.
4061    board_id: String,
4062    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4063    // passed back as the mutation's field identity; a newtype could enforce no stronger
4064    // invariant because GitHub publishes no grammar for it.
4065    field_id: String,
4066    options: Vec<StatusOption>,
4067    assignments: Vec<StatusAssignment>,
4068}
4069
4070/// The name of the board field a status is held in.
4071const STATUS_FIELD: &str = "Status";
4072
4073/// Every item's value of each field `report` names, as it stood before the setup wrote
4074/// anything — what a person puts back when the setup is refused part way.
4075fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
4076    let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
4077        .fields
4078        .iter()
4079        .map(|field| (field.field.name(), before.assignments(field.field)))
4080        .collect();
4081    serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
4082        message: format!("cannot render the pre-write field recovery snapshot: {error}"),
4083    })
4084}
4085
4086/// One board field the guarded setup reads and writes — every one it reads, and the only
4087/// ones it writes.
4088#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
4089pub enum BoardField {
4090    /// The single-select `Status` field every instance's `status_mapping` resolves into.
4091    Status,
4092    /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
4093    Priority,
4094}
4095
4096impl BoardField {
4097    /// The field's name on the board.
4098    #[must_use]
4099    pub const fn name(self) -> &'static str {
4100        match self {
4101            Self::Status => STATUS_FIELD,
4102            Self::Priority => PRIORITY_FIELD,
4103        }
4104    }
4105
4106    /// The field a board calls `name`, or `None` for one this setup does not own.
4107    fn named(name: &str) -> Option<Self> {
4108        [Self::Status, Self::Priority]
4109            .into_iter()
4110            .find(|field| field.name() == name)
4111    }
4112}
4113
4114/// What the guarded setup did to one field.
4115#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4116#[serde(rename_all = "kebab-case")]
4117pub enum FieldOutcome {
4118    /// A read-only plan.
4119    Planned,
4120    /// Apply found the field there with every configured option.
4121    Unchanged,
4122    /// Missing options were added to the field that was there, and verified.
4123    Applied,
4124    /// The field was not there; it was created holding the configured options, and verified.
4125    Created,
4126}
4127
4128/// One field's plan, or its verified outcome.
4129#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4130pub struct FieldReport {
4131    /// Which field.
4132    pub field: BoardField,
4133    /// Whether the board had the field before the operation.
4134    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
4135    // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
4136    // "outcome", "existing"}` — so folding one into the other would change a published JSON
4137    // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
4138    // one constructor, and it derives `outcome` from `exists` in one match.
4139    pub exists: bool,
4140    /// Configured option names the field lacked before the operation — every one of them,
4141    /// in the order a new field lists them, when the field was not there at all.
4142    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4143    // mapping name and has therefore already passed its nonblank validation; the serialized
4144    // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
4145    pub missing: Vec<String>,
4146    /// For the `Status` field, which item kind each missing name is configured for: one
4147    /// entry per kind `status_mapping` names a missing option for, task before project, each
4148    /// listing that kind's missing names in category order. A name both kinds use is in
4149    /// both. Empty — and left out of the JSON — when nothing is missing, and always for
4150    /// `Priority`, which only a task holds.
4151    #[serde(default, skip_serializing_if = "Vec::is_empty")]
4152    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4153    // both SDKs model an absent `kinds` as an empty list rather than as `null`.
4154    #[schemars(!skip_serializing_if)]
4155    pub kinds: Vec<KindMissing>,
4156    /// What the requested operation did.
4157    pub outcome: FieldOutcome,
4158    /// The field's complete option list observed before any mutation; empty when the field
4159    /// was not there.
4160    pub existing: Vec<StatusOption>,
4161}
4162
4163/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
4164#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4165pub struct KindMissing {
4166    /// The kind these names are configured for.
4167    pub kind: ItemKind,
4168    /// The names that kind maps a category to and the field lacked, in category order.
4169    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4170    // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
4171    // intentionally simple public contract.
4172    pub missing: Vec<String>,
4173}
4174
4175/// The plan and verified outcome of setting up every field a source's configuration names.
4176#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4177pub struct FieldsReport {
4178    /// The configured source name.
4179    pub source: SourceName,
4180    /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
4181    // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
4182    // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
4183    // per field would change a published JSON shape. The states the list could hold and the
4184    // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
4185    // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
4186    pub fields: Vec<FieldReport>,
4187    /// Each board text field the source's `metadata_fields` projects a value onto, in
4188    /// configured order. Empty — and left out of the JSON — when it projects none.
4189    #[serde(default, skip_serializing_if = "Vec::is_empty")]
4190    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4191    // both SDKs model an absent list as an empty one rather than as `null`.
4192    #[schemars(!skip_serializing_if)]
4193    pub metadata_fields: Vec<MetadataFieldReport>,
4194}
4195
4196/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
4197/// its GraphQL type when it has none — which is what a projected field's conflict reports.
4198///
4199/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
4200/// no conflict.
4201// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
4202// refuses exactly what `try_from` below does.
4203#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4204#[serde(transparent)]
4205pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
4206
4207/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
4208/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
4209/// neither SDK's validator can be relied on to support.
4210const NON_TEXT_FIELD_TYPE_PATTERN: &str =
4211    r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
4212
4213impl TryFrom<String> for NonTextFieldType {
4214    type Error = String;
4215
4216    fn try_from(kind: String) -> Result<Self, Self::Error> {
4217        if kind.is_empty() || kind.starts_with(char::is_whitespace) {
4218            return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
4219        }
4220        if kind == "TEXT" {
4221            return Err("a TEXT field is no conflict for a projected text field".to_owned());
4222        }
4223        Ok(Self(kind))
4224    }
4225}
4226
4227impl std::fmt::Display for NonTextFieldType {
4228    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4229        formatter.write_str(&self.0)
4230    }
4231}
4232
4233/// One projected metadata text field's plan, or its verified outcome.
4234///
4235/// The setup creates a missing one as a text field and never alters a field that is there:
4236/// a same-named field of another type is reported in `conflict` and left as it is.
4237#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4238pub struct MetadataFieldReport {
4239    /// The board field's name.
4240    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.
4241    /// The metadata key the value is read from.
4242    pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
4243    /// The object keys walked inside that key's value; empty for the key's own value.
4244    pub path: Vec<String>,
4245    /// Whether the board had a text field of that name before the operation.
4246    // 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.
4247    pub exists: bool,
4248    /// The type of a field of that name that is not a text field, which the setup leaves as
4249    /// it is; absent — and left out of the JSON — when there is none.
4250    #[serde(default, skip_serializing_if = "Option::is_none")]
4251    pub conflict: Option<NonTextFieldType>,
4252    /// What the requested operation did: `planned`; `created` for a field that was not
4253    /// there; `unchanged` for one that was, and for a conflict, which is never changed.
4254    pub outcome: FieldOutcome,
4255}
4256
4257/// Which options one field is configured with, in the order a new field would list them.
4258struct FieldPlan {
4259    field: BoardField,
4260    wanted: Vec<String>,
4261}
4262
4263/// One single-select field as the guarded setup snapshots it.
4264#[derive(Debug, Clone, PartialEq, Eq)]
4265struct SnapshotField {
4266    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4267    // passed back as the mutation's field identity; a newtype could enforce no stronger
4268    // invariant because GitHub publishes no grammar for it.
4269    field_id: String,
4270    options: Vec<StatusOption>,
4271}
4272
4273/// Every single-select field of a board and every item's value of each.
4274#[derive(Debug, Clone, PartialEq, Eq)]
4275struct BoardSnapshot {
4276    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4277    // passed back as the mutation's project identity; a newtype could enforce no stronger
4278    // invariant because GitHub publishes no grammar for it.
4279    board_id: String,
4280    fields: BTreeMap<BoardField, SnapshotField>,
4281    /// Each board item's id, and its value of each field this setup owns that it holds one of.
4282    items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
4283}
4284
4285impl BoardSnapshot {
4286    /// Every item's value of `field`, in board order — the recovery data a drift refusal
4287    /// carries.
4288    fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
4289        self.items
4290            .iter()
4291            .map(|(item_id, values)| StatusAssignment {
4292                item_id: item_id.clone(),
4293                option: values.get(&field).cloned(),
4294            })
4295            .collect()
4296    }
4297}
4298
4299impl GitHubProjectsSource {
4300    /// Report missing configured Status options and, when `apply` is true, add them with
4301    /// a whole-list mutation that preserves every existing id and verifies the result.
4302    ///
4303    /// # Errors
4304    ///
4305    /// Refuses a board without a single-select `Status` field. A post-write difference in
4306    /// any pre-existing option id or item assignment is refused with the complete pre-write
4307    /// assignment snapshot in the diagnostic for recovery.
4308    // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
4309    // successful mutation, both drift refusals, source selection, missing Status, casing,
4310    // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
4311    // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
4312    // responses from entering the defensive malformed-response branches below.
4313    pub async fn status_options(
4314        &self,
4315        mode: StatusOptionsMode,
4316    ) -> Result<StatusOptionsReport, SourceError> {
4317        let before = self.status_snapshot().await?;
4318        // A terminal category's option is as configured as an open one's: a terminal
4319        // write validates it before closing and refuses when the board lacks it. Both
4320        // kinds' names are options of the one field, so both are asked for.
4321        let missing = self
4322            .statuses
4323            .wanted()
4324            .into_iter()
4325            .filter(|wanted| {
4326                !before
4327                    .options
4328                    .iter()
4329                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4330            })
4331            .collect::<Vec<_>>();
4332        let report = StatusOptionsReport {
4333            source: self.name.clone(),
4334            missing: missing.clone(),
4335            outcome: match (mode, missing.is_empty()) {
4336                (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
4337                (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
4338                (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
4339            },
4340            existing: before.options.clone(),
4341        };
4342        if mode == StatusOptionsMode::Plan || missing.is_empty() {
4343            return Ok(report);
4344        }
4345        let mut options = before
4346            .options
4347            .iter()
4348            .map(|option| {
4349                json!({
4350                    "id": option.id, "name": option.name, "color": option.color,
4351                    "description": option.description,
4352                })
4353            })
4354            .collect::<Vec<_>>();
4355        options.extend(missing.iter().map(|name| {
4356            json!({
4357                "name": name, "color": "GRAY", "description": ""
4358            })
4359        }));
4360        self.graphql(
4361            graphql::STATUS_OPTIONS_UPDATE,
4362            json!({"input": {
4363                "projectId": before.board_id, "fieldId": before.field_id,
4364                "singleSelectOptions": options,
4365            }}),
4366        )
4367        .await?;
4368        let after = self.status_snapshot().await?;
4369        let options_preserved = before
4370            .options
4371            .iter()
4372            .all(|old| after.options.iter().any(|new| new == old));
4373        let additions_present = missing.iter().all(|wanted| {
4374            after
4375                .options
4376                .iter()
4377                .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4378        });
4379        if !options_preserved || !additions_present || after.assignments != before.assignments {
4380            let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
4381                SourceError::Malformed {
4382                    message: format!("cannot render pre-write Status recovery snapshot: {error}"),
4383                }
4384            })?;
4385            return Err(SourceError::Refused {
4386                message: format!(
4387                    "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}"
4388                ),
4389            });
4390        }
4391        Ok(report)
4392    }
4393
4394    /// A fresh snapshot of the Status field and every board item's assignment of it.
4395    ///
4396    /// # Errors
4397    ///
4398    /// Refuses a board without a single-select `Status` field, and one the token cannot see.
4399    async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
4400        // Status alone, as this operation has always read it: a `Priority` field is another
4401        // operation's, so nothing about it can refuse this one.
4402        let mut board = self.board_snapshot(&[BoardField::Status]).await?;
4403        let field = board
4404            .fields
4405            .remove(&BoardField::Status)
4406            .ok_or_else(|| self.no_status_field())?;
4407        Ok(StatusSnapshot {
4408            assignments: board.assignments(BoardField::Status),
4409            board_id: board.board_id,
4410            field_id: field.field_id,
4411            options: field.options,
4412        })
4413    }
4414
4415    /// The refusal a board with no `Status` field is answered with by the guarded setup.
4416    fn no_status_field(&self) -> SourceError {
4417        SourceError::Refused {
4418            message: format!("source {} board has no Status field", self.name),
4419        }
4420    }
4421
4422    // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
4423    // the real CLI loopback journey, including pagination. The individual malformed guards
4424    // are defensive validation of a schema-pinned third-party response, not separate user
4425    // journeys; drift and missing-field failures cover the operation's recovery behavior.
4426    /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
4427    /// every board item's value of each, walked to the end of the board's items. A field not
4428    /// in `owned` is read past whatever it holds.
4429    async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
4430        let mut after: Option<String> = None;
4431        let mut snapshot: Option<BoardSnapshot> = None;
4432        loop {
4433            let data = self
4434                .graphql(
4435                    graphql::STATUS_OPTIONS_SNAPSHOT,
4436                    json!({
4437                        "owner": self.owner, "number": self.project_number,
4438                        "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
4439                    }),
4440                )
4441                .await?;
4442            let board = data
4443                .pointer("/owner/projectV2")
4444                .filter(|board| board.is_object())
4445                .ok_or_else(|| SourceError::Refused {
4446                    message: format!(
4447                        "source {} has no accessible GitHub Projects board",
4448                        self.name
4449                    ),
4450                })?;
4451            if board
4452                .pointer("/fields/pageInfo/hasNextPage")
4453                .and_then(Value::as_bool)
4454                != Some(false)
4455            {
4456                return Err(SourceError::Malformed {
4457                    message:
4458                        "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
4459                            .into(),
4460                });
4461            }
4462            let mut fields = BTreeMap::new();
4463            // Only the fields this setup owns, by name: a node the single-select fragment did not
4464            // match carries no name, and a person's own single-select field — a `Size`, a
4465            // `Team` — is none of this setup's business, so nothing about it can refuse one. A
4466            // `Status` or `Priority` field without its options is malformed, not absent.
4467            // 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.
4468            for (owned, field) in board
4469                .pointer("/fields/nodes")
4470                .and_then(Value::as_array)
4471                .ok_or_else(|| SourceError::Malformed {
4472                    message: "GitHub project fields.nodes is not an array".into(),
4473                })?
4474                .iter()
4475                .filter_map(|field| {
4476                    let named = BoardField::named(field.get("name")?.as_str()?)?;
4477                    owned.contains(&named).then_some((named, field))
4478                })
4479            {
4480                let options = field
4481                    .get("options")
4482                    .and_then(Value::as_array)
4483                    .ok_or_else(|| SourceError::Malformed {
4484                        message: "GitHub single-select field options is not an array".into(),
4485                    })?
4486                    .iter()
4487                    .map(|option| {
4488                        Ok(StatusOption {
4489                            id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
4490                                .map_err(|message| SourceError::Malformed { message })?,
4491                            name: ColumnName::try_from(required_str(option, "name")?.to_owned())
4492                                .map_err(|message| SourceError::Malformed {
4493                                    message: format!(
4494                                        "GitHub single-select option name is invalid: {message}"
4495                                    ),
4496                                })?,
4497                            color: serde_json::from_value(
4498                                option.get("color").cloned().unwrap_or(Value::Null),
4499                            )
4500                            .map_err(|error| {
4501                                SourceError::Malformed {
4502                                    message: format!(
4503                                        "GitHub single-select option color is invalid: {error}"
4504                                    ),
4505                                }
4506                            })?,
4507                            description: optional_str(option, "description")?
4508                                .unwrap_or_default()
4509                                .to_owned(),
4510                        })
4511                    })
4512                    .collect::<Result<Vec<_>, SourceError>>()?;
4513                let snapshot = SnapshotField {
4514                    field_id: required_nonblank_str(field, "id")?.to_owned(),
4515                    options,
4516                };
4517                // A board's field names are unique, so a second one is an answer that cannot
4518                // say which field the setup would act on — refused rather than one chosen.
4519                if fields.insert(owned, snapshot).is_some() {
4520                    return Err(SourceError::Malformed {
4521                        message: format!(
4522                            "GitHub answered two {} fields for this board",
4523                            owned.name()
4524                        ),
4525                    });
4526                }
4527            }
4528            let board_id = required_nonblank_str(board, "id")?.to_owned();
4529            let current = snapshot.get_or_insert_with(|| BoardSnapshot {
4530                board_id,
4531                fields,
4532                items: Vec::new(),
4533            });
4534            let items = board
4535                .pointer("/items/nodes")
4536                .and_then(Value::as_array)
4537                .ok_or_else(|| SourceError::Malformed {
4538                    message: "GitHub project items.nodes is not an array".into(),
4539                })?;
4540            for item in items {
4541                let field_values =
4542                    item.get("fieldValues")
4543                        .ok_or_else(|| SourceError::Malformed {
4544                            message: "GitHub project item is missing fieldValues".into(),
4545                        })?;
4546                if field_values
4547                    .pointer("/pageInfo/hasNextPage")
4548                    .and_then(Value::as_bool)
4549                    != Some(false)
4550                {
4551                    return Err(SourceError::Malformed {
4552                        message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
4553                    });
4554                }
4555                let values = item
4556                    .pointer("/fieldValues/nodes")
4557                    .and_then(Value::as_array)
4558                    .ok_or_else(|| SourceError::Malformed {
4559                        message: "GitHub project item fieldValues.nodes is not an array".into(),
4560                    })?;
4561                let item_id = required_nonblank_str(item, "id")?;
4562                let mut assigned = BTreeMap::new();
4563                for value in values {
4564                    let Some(field) = value
4565                        .pointer("/field/name")
4566                        .and_then(Value::as_str)
4567                        .and_then(BoardField::named)
4568                        .filter(|field| owned.contains(field))
4569                    else {
4570                        continue;
4571                    };
4572                    let held = assigned.insert(
4573                        field,
4574                        AssignedStatusOption {
4575                            id: StatusOptionId::try_from(
4576                                required_str(value, "optionId")?.to_owned(),
4577                            )
4578                            .map_err(|message| SourceError::Malformed { message })?,
4579                            name: ColumnName::try_from(required_str(value, "name")?.to_owned())
4580                                .map_err(|message| SourceError::Malformed {
4581                                    message: format!(
4582                                        "GitHub assigned {} name is invalid: {message}",
4583                                        field.name()
4584                                    ),
4585                                })?,
4586                        },
4587                    );
4588                    // An item holds one value of a field, so a second one leaves no way to
4589                    // tell which it holds — and a verification or recovery built on either
4590                    // could restore the wrong one.
4591                    if held.is_some() {
4592                        return Err(SourceError::Malformed {
4593                            message: format!(
4594                                "GitHub answered two {} values for board item {item_id}",
4595                                field.name()
4596                            ),
4597                        });
4598                    }
4599                }
4600                current.items.push((item_id.to_owned(), assigned));
4601            }
4602            let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4603                message: "GitHub project is missing items".into(),
4604            })?;
4605            let has_next = page
4606                .pointer("/pageInfo/hasNextPage")
4607                .and_then(Value::as_bool)
4608                .ok_or_else(|| SourceError::Malformed {
4609                    message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4610                })?;
4611            if !has_next {
4612                break;
4613            }
4614            let next =
4615                required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4616            validate_cursor_progress(after.as_deref(), next)?;
4617            after = Some(next.to_owned());
4618        }
4619        snapshot.ok_or_else(|| SourceError::Malformed {
4620            message: "GitHub returned no board field snapshot".into(),
4621        })
4622    }
4623    // llmlint: ignore-end[changed_behavior_has_e2e]
4624
4625    /// Report every board field this source's configuration names and, with
4626    /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4627    /// the `Priority` field when the board has none.
4628    ///
4629    /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4630    /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4631    /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4632    /// color and description: the whole option list goes back with every existing id, because
4633    /// a re-minted id clears every item's value.
4634    ///
4635    /// # Errors
4636    ///
4637    /// Refuses a board without a single-select `Status` field. After an apply the board is
4638    /// read again, and a pre-existing option or any item's value of either field that moved is
4639    /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4640    // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4641    // unchanged apply, a created field, an added option to each field, drift refusal, a board
4642    // with no Status field and a non-github-projects source through the compiled CLI against
4643    // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4644    pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4645        let owned: Vec<BoardField> = if self.priorities.is_some() {
4646            vec![BoardField::Status, BoardField::Priority]
4647        } else {
4648            vec![BoardField::Status]
4649        };
4650        let before = self.board_snapshot(&owned).await?;
4651        let mut plans = vec![FieldPlan {
4652            field: BoardField::Status,
4653            wanted: self.statuses.wanted(),
4654        }];
4655        if !before.fields.contains_key(&BoardField::Status) {
4656            return Err(self.no_status_field());
4657        }
4658        if let Some(mapping) = &self.priorities {
4659            plans.push(FieldPlan {
4660                field: BoardField::Priority,
4661                wanted: mapping.names().map(str::to_owned).collect(),
4662            });
4663        }
4664        // The snapshot reads single-select fields alone, so a field it did not find may still
4665        // be on the board under the name, of another type: creating one beside it would fail
4666        // part way, or leave two fields of one name. Asked of the board's own field list, and
4667        // only when a field is missing.
4668        if plans
4669            .iter()
4670            .any(|plan| !before.fields.contains_key(&plan.field))
4671        {
4672            let board = self.board_fields().await?;
4673            for plan in plans
4674                .iter()
4675                .filter(|plan| !before.fields.contains_key(&plan.field))
4676            {
4677                if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4678                    return Err(SourceError::Refused {
4679                        message: format!(
4680                            "source {}'s board has a {} field that is not a single-select field \
4681                             (it is a {}), so it cannot hold this source's options; next: rename \
4682                             or remove that field, then run this again",
4683                            self.name,
4684                            plan.field.name(),
4685                            optional_str(field, "__typename")?.unwrap_or("field of another type")
4686                        ),
4687                    });
4688                }
4689            }
4690        }
4691        let mut reports = Vec::new();
4692        for plan in &plans {
4693            let held = before.fields.get(&plan.field);
4694            let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4695            let mut missing: Vec<String> = Vec::new();
4696            for wanted in &plan.wanted {
4697                let present = existing
4698                    .iter()
4699                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4700                    || missing
4701                        .iter()
4702                        .any(|named| named.eq_ignore_ascii_case(wanted));
4703                if !present {
4704                    missing.push(wanted.clone());
4705                }
4706            }
4707            let kinds = match plan.field {
4708                BoardField::Status => self.statuses.missing_by_kind(&existing),
4709                BoardField::Priority => Vec::new(),
4710            };
4711            reports.push(FieldReport {
4712                field: plan.field,
4713                exists: held.is_some(),
4714                kinds,
4715                outcome: match (mode, held.is_some(), missing.is_empty()) {
4716                    (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4717                    (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4718                    (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4719                    (SetupMode::Apply, false, _) => FieldOutcome::Created,
4720                },
4721                missing,
4722                existing,
4723            });
4724        }
4725        // A projected field is a text field, which the single-select snapshot cannot see, so
4726        // it is asked of the board's own field list — and only when one is configured.
4727        let mut metadata_fields = Vec::new();
4728        if !self.metadata_fields.is_empty() {
4729            let board = self.board_fields().await?;
4730            for projection in &self.metadata_fields {
4731                let conflict = match Board::field(&board.fields, &projection.field)? {
4732                    None => None,
4733                    Some(field) => {
4734                        let typename = optional_str(field, "__typename")?;
4735                        let data_type = optional_str(field, "dataType")?;
4736                        if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4737                            None
4738                        } else {
4739                            let named = data_type
4740                                .filter(|kind| *kind != "TEXT")
4741                                .or(typename)
4742                                .unwrap_or("field of another type");
4743                            Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4744                                |message| SourceError::Malformed {
4745                                    message: format!(
4746                                        "GitHub answered the board's {} field with a type this \
4747                                         setup cannot read: {message}",
4748                                        &*projection.field
4749                                    ),
4750                                },
4751                            )?)
4752                        }
4753                    }
4754                };
4755                let exists =
4756                    conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4757                metadata_fields.push(MetadataFieldReport {
4758                    field: projection.field.to_string(),
4759                    key: projection.key.to_string(),
4760                    path: projection.path.clone(),
4761                    exists,
4762                    outcome: match (mode, exists || conflict.is_some()) {
4763                        (SetupMode::Plan, _) => FieldOutcome::Planned,
4764                        (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4765                        (SetupMode::Apply, false) => FieldOutcome::Created,
4766                    },
4767                    conflict,
4768                });
4769            }
4770        }
4771        let report = FieldsReport {
4772            source: self.name.clone(),
4773            fields: reports,
4774            metadata_fields,
4775        };
4776        let writes: Vec<&FieldReport> = report
4777            .fields
4778            .iter()
4779            .filter(|field| !field.missing.is_empty() || !field.exists)
4780            .collect();
4781        let creates: Vec<&MetadataFieldReport> = report
4782            .metadata_fields
4783            .iter()
4784            .filter(|field| field.outcome == FieldOutcome::Created)
4785            .collect();
4786        if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4787            return Ok(report);
4788        }
4789        let mut landed: Vec<&str> = Vec::new();
4790        for field in &writes {
4791            let added = field
4792                .missing
4793                .iter()
4794                .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4795            let sent = match before.fields.get(&field.field) {
4796                Some(held) => {
4797                    let mut options = held
4798                        .options
4799                        .iter()
4800                        .map(|option| {
4801                            json!({
4802                                "id": option.id, "name": option.name, "color": option.color,
4803                                "description": option.description,
4804                            })
4805                        })
4806                        .collect::<Vec<_>>();
4807                    options.extend(added);
4808                    self.graphql(
4809                        graphql::STATUS_OPTIONS_UPDATE,
4810                        json!({"input": {
4811                            "projectId": before.board_id, "fieldId": held.field_id,
4812                            "singleSelectOptions": options,
4813                        }}),
4814                    )
4815                    .await
4816                }
4817                None => {
4818                    self.graphql(
4819                        graphql::CREATE_FIELD,
4820                        json!({"input": {
4821                            "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4822                            "name": field.field.name(),
4823                            "singleSelectOptions": added.collect::<Vec<_>>(),
4824                        }}),
4825                    )
4826                    .await
4827                }
4828            };
4829            // A mutation that failed does not establish that GitHub left its field as it was,
4830            // so every failure from here on carries the recovery data a drift refusal does.
4831            match sent {
4832                Ok(_) => landed.push(field.field.name()),
4833                Err(error) => {
4834                    let changed = if landed.is_empty() {
4835                        String::new()
4836                    } else {
4837                        format!("changed the {} field and then ", landed.join(" and "))
4838                    };
4839                    return Err(SourceError::Refused {
4840                        message: format!(
4841                            "the guarded field setup {changed}failed on the {} field, which it may \
4842                             have changed part way: {error}; the pre-write item assignments \
4843                             are:\n{}",
4844                            field.field.name(),
4845                            recovery(&report, &before)?
4846                        ),
4847                    });
4848                }
4849            }
4850        }
4851        for field in &creates {
4852            let sent = self
4853                .graphql(
4854                    graphql::CREATE_FIELD,
4855                    json!({"input": {
4856                        "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4857                    }}),
4858                )
4859                .await;
4860            if let Err(error) = sent {
4861                let changed = if landed.is_empty() {
4862                    String::new()
4863                } else {
4864                    format!("changed the {} field and then ", landed.join(" and "))
4865                };
4866                return Err(SourceError::Refused {
4867                    message: format!(
4868                        "the guarded field setup {changed}failed creating the {} text field: \
4869                         {error}; run it again to create what is still missing",
4870                        field.field
4871                    ),
4872                });
4873            }
4874            landed.push(&field.field);
4875        }
4876        // The board has been written, so a verification read that fails leaves it unverified
4877        // rather than unchanged, and says what to put back.
4878        let after = match self.board_snapshot(&owned).await {
4879            Ok(after) => after,
4880            Err(error) => {
4881                return Err(SourceError::Refused {
4882                    message: format!(
4883                        "the guarded field setup changed the {} field and then could not read the \
4884                         board back to verify it: {error}; the pre-write item assignments are:\n{}",
4885                        landed.join(" and "),
4886                        recovery(&report, &before)?
4887                    ),
4888                });
4889            }
4890        };
4891        let mut moved = Vec::new();
4892        for field in &report.fields {
4893            let name = field.field.name();
4894            let now = after
4895                .fields
4896                .get(&field.field)
4897                .map(|held| held.options.as_slice())
4898                .unwrap_or_default();
4899            if !field.existing.iter().all(|old| now.contains(old)) {
4900                moved.push(format!(
4901                    "a pre-existing {name} option id, name, color or description"
4902                ));
4903            }
4904            if !field.missing.iter().all(|wanted| {
4905                now.iter()
4906                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4907            }) {
4908                moved.push(format!("an added {name} option"));
4909            }
4910            if after.assignments(field.field) != before.assignments(field.field) {
4911                moved.push(format!("an item's {name} value"));
4912            }
4913        }
4914        if !creates.is_empty() {
4915            // Read afresh: the view held for this command is the one from before the creates.
4916            *self.fields_cache()? = None;
4917            let board = match self.board_fields().await {
4918                Ok(board) => board,
4919                Err(error) => {
4920                    return Err(SourceError::Refused {
4921                        message: format!(
4922                            "the guarded field setup changed the {} field and then could not \
4923                             read the board back to verify it: {error}; run it again to verify \
4924                             it, which creates nothing a second time",
4925                            landed.join(" and ")
4926                        ),
4927                    });
4928                }
4929            };
4930            for field in &creates {
4931                let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
4932                    held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
4933                        && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
4934                });
4935                if !text {
4936                    moved.push(format!("the created {} text field", field.field));
4937                }
4938            }
4939        }
4940        if !moved.is_empty() {
4941            return Err(SourceError::Refused {
4942                message: format!(
4943                    "GitHub changed {} after the guarded field setup; the pre-write item \
4944                     assignments are:\n{}",
4945                    moved.join(", "),
4946                    recovery(&report, &before)?
4947                ),
4948            });
4949        }
4950        Ok(report)
4951    }
4952
4953    /// Validate configuration and capture the named credential without exposing it.
4954    ///
4955    /// # Errors
4956    ///
4957    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
4958    /// [`SourceError::Auth`] when the named credential is missing or empty.
4959    pub fn new(
4960        name: &SourceName,
4961        config: GitHubProjectsConfig,
4962        secrets: &dyn SecretResolver,
4963    ) -> Result<Self, SourceError> {
4964        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
4965    }
4966
4967    /// The same, recording every request it sends into an accounting the caller holds too.
4968    ///
4969    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
4970    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
4971    /// up — passes the one it records those into, so the session total accounts for the
4972    /// whole session rather than for this source's share of it.
4973    ///
4974    /// # Errors
4975    ///
4976    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
4977    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
4978    pub fn recording_into(
4979        name: &SourceName,
4980        config: GitHubProjectsConfig,
4981        secrets: &dyn SecretResolver,
4982        ledger: Arc<Accounting>,
4983    ) -> Result<Self, SourceError> {
4984        if !valid_github_owner(&config.owner) {
4985            return Err(SourceError::Config {
4986                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
4987            });
4988        }
4989        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
4990            return Err(SourceError::Config {
4991                message: format!("project_number must be between 1 and {}", i32::MAX),
4992            });
4993        }
4994        if !valid_environment_name(&config.token_env) {
4995            return Err(SourceError::Config {
4996                message: "token_env must be a valid environment-variable name".into(),
4997            });
4998        }
4999        let repository = config
5000            .repository
5001            .as_deref()
5002            .map(RepositoryTarget::parse)
5003            .transpose()?;
5004        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
5005            message: format!("endpoint is not a valid URL: {e}"),
5006        })?;
5007        if endpoint.scheme() != "https"
5008            && !(endpoint.scheme() == "http"
5009                && endpoint
5010                    .host_str()
5011                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
5012        {
5013            return Err(SourceError::Config {
5014                message:
5015                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
5016                        .into(),
5017            });
5018        }
5019        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
5020            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),
5021        })?;
5022        Ok(Self {
5023            name: name.clone(),
5024            owner: config.owner,
5025            project_number: config.project_number,
5026            repository,
5027            asset_client: std::sync::OnceLock::new(),
5028            endpoint,
5029            token,
5030            credential_name: config.token_env,
5031            statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
5032            priorities: config
5033                .priority_mapping
5034                .map(|mapping| PriorityMapping::resolve(mapping, name))
5035                .transpose()?,
5036            metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
5037            client: std::sync::OnceLock::new(),
5038            created: Mutex::new(Vec::new()),
5039            updated: Mutex::new(Vec::new()),
5040            commented: Mutex::new(Vec::new()),
5041            pacing: Pacing::resolve(config.pacing, name)?,
5042            last_mutation: Mutex::new(None),
5043            windows: Mutex::new(pacing::Windows::default()),
5044            task_children: Mutex::new(BTreeMap::new()),
5045            written: Mutex::new(BTreeMap::new()),
5046            deferred: Mutex::new(None),
5047            held_updates: Mutex::new(Vec::new()),
5048            clock: system_clock(),
5049            numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
5050            board_cache: Mutex::new(None),
5051            search_cache: Mutex::new(None),
5052            narrowed_cache: Mutex::new(BTreeMap::new()),
5053            resolved_cache: Mutex::new(BTreeMap::new()),
5054            read_ahead: Mutex::new(BTreeMap::new()),
5055            pending_origins: Mutex::new(Vec::new()),
5056            member_classes: Mutex::new(BTreeMap::new()),
5057            carried: Mutex::new(BTreeMap::new()),
5058            held_cache: Mutex::new(BTreeMap::new()),
5059            children_cache: Mutex::new(BTreeMap::new()),
5060            search_next: Mutex::new(BTreeMap::new()),
5061            fields_cache: Mutex::new(None),
5062            repository_cache: Mutex::new(BTreeMap::new()),
5063            ledger,
5064            expected: expected::Held::default(),
5065        })
5066    }
5067
5068    /// The client every API request goes through, built on the first one.
5069    ///
5070    /// Not with the source: building a client loads the system's certificate store, which
5071    /// costs tens of milliseconds, and a command that configures this board but never sends to
5072    /// it — a read of another source — should not pay that in its process.
5073    fn client(&self) -> Result<&Client, SourceError> {
5074        built(&self.client, || {
5075            Client::builder()
5076                .user_agent("onetaskgraph")
5077                .build()
5078                .map_err(|e| SourceError::Config {
5079                    message: format!("cannot build HTTP client: {e}"),
5080                })
5081        })
5082    }
5083
5084    /// The client attachment requests go through, with its own redirect policy, built on the
5085    /// first one for the reason [`Self::client`] gives.
5086    fn asset_client(&self) -> Result<&Client, SourceError> {
5087        built(&self.asset_client, || assets::client(&self.endpoint))
5088    }
5089
5090    /// A snapshot of every request this source has sent, and what each cost.
5091    ///
5092    /// A value to hold and compare rather than a borrow of the accounting itself, so two
5093    /// of them can sit side by side. When this source was built with
5094    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
5095    /// point of building it that way.
5096    #[must_use]
5097    pub fn accounting(&self) -> accounting::Session {
5098        self.ledger.snapshot()
5099    }
5100
5101    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
5102    /// rate limit rather than handing it straight back as an error.
5103    ///
5104    /// Retrying is safe for every document here, including the mutations, and the reason
5105    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
5106    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
5107    /// this replays has already taken effect. An outcome this source cannot know — the
5108    /// send failed, or the body could not be read, so the mutation may well have landed —
5109    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
5110    /// attempt. A duplicate write would come from replaying one of those, and none is
5111    /// replayed.
5112    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5113        // Updates held back go out before any other request, so whatever it reads or writes
5114        // sees the board as though each had been sent when it was made.
5115        Box::pin(self.send_held_updates()).await?;
5116        // Origins held back for a read ahead that never came are sent before anything else.
5117        if query != graphql::ORIGIN_LOOKUPS && query != graphql::ORIGIN_LOOKUPS_WITH_MEMBERS {
5118            Box::pin(self.send_pending_origins(None)).await?;
5119        }
5120        if !self.expected.journaling() {
5121            return self.graphql_sent(query, variables).await;
5122        }
5123        // While a write the store's cache expects runs, its mutations select the whole issue
5124        // they leave behind, so the response vouches for what the cache keeps — and every
5125        // request is recorded, so the write's parts are reported from their own responses.
5126        let (query, variables) = match graphql::whole_twin(query) {
5127            Some(twin) => (twin, expected::whole_variables(variables)),
5128            None => (query, variables),
5129        };
5130        let sent = self.graphql_sent(query, variables.clone()).await;
5131        self.journal(query, &variables, &sent).await;
5132        sent
5133    }
5134
5135    /// One document sent, retried while GitHub asks for a wait, exactly as asked.
5136    async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
5137        if is_mutation(query)
5138            && ![
5139                graphql::ADD_COMMENT,
5140                graphql::UPDATE_COMMENT,
5141                graphql::DELETE_COMMENT,
5142                graphql::ADD_COMMENT_WHOLE,
5143                graphql::UPDATE_COMMENT_WHOLE,
5144            ]
5145            .contains(&query)
5146        {
5147            let mut cache = self.resolved_cache()?;
5148            let mut ahead = self.read_ahead()?;
5149            let mut carried = self.carried()?;
5150            for argument in ["input", "second", "third", "clear"] {
5151                if let Some(input) = variables.get(argument) {
5152                    let names = |id: &NativeId, item_id: Option<&str>| {
5153                        ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
5154                            input
5155                                .get(key)
5156                                .and_then(Value::as_str)
5157                                .is_some_and(|value| value == id.0 || Some(value) == item_id)
5158                        })
5159                    };
5160                    cache.retain(|id, item| !names(id, Some(&item.item_id)));
5161                    ahead.retain(|id, item| {
5162                        !names(id, item.as_ref().map(|item| item.item_id.as_str()))
5163                    });
5164                    carried.retain(|id, item| !names(id, Some(&item.item_id)));
5165                }
5166            }
5167        }
5168        let doing = operation_description(query);
5169        let mutation = is_mutation(query);
5170        let charge = pacing::Charge::graphql(query, &variables);
5171        let mut waited = Duration::ZERO;
5172        let mut waits = 0_u32;
5173        let mut backoff = self.pacing.retry_backoff;
5174        loop {
5175            let ticket = self.pace(charge, mutation).await;
5176            let attempt = self.send_once(query, &variables, waits + 1).await;
5177            self.paced(ticket, mutation);
5178            let limited = match attempt {
5179                Ok(data) => return Ok(data),
5180                Err(Attempt::Failed(error)) => return Err(error),
5181                Err(Attempt::Limited(limited)) => limited,
5182            };
5183            let (wait, cause) = limited.wait(backoff);
5184            let remaining = self.pacing.retry_budget.saturating_sub(waited);
5185            // A wait of nothing spends none of the budget, so it is exhaustion rather
5186            // than a retry. `Pacing::resolve` rules out every way of configuring one
5187            // except a budget of zero, where reporting the first refusal is the ask.
5188            if wait.is_zero() || wait > remaining {
5189                return Err(limited.exhausted(
5190                    doing,
5191                    waits,
5192                    waited,
5193                    wait,
5194                    self.pacing.retry_budget,
5195                ));
5196            }
5197            self.clock.sleep_because(wait, cause).await;
5198            waited += wait;
5199            waits += 1;
5200            backoff = backoff.saturating_mul(2);
5201        }
5202    }
5203
5204    /// Hold one request charging `charge` until every secondary window it charges has room for
5205    /// the whole of it — and, for a mutation of an instance configured with
5206    /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
5207    /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
5208    ///
5209    /// The moment is decided and counted under the lock and waited out outside it, so two
5210    /// callers take two places rather than the same one, and no lock is held across an await.
5211    /// A request with room goes out at once: nothing here waits that a window does not ask for.
5212    async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
5213        let (wait, cause, ticket) = {
5214            // A poisoned lock here costs pacing, not correctness, and refusing the write over
5215            // it would turn an earlier failure into a second one for no gain.
5216            let mut windows = self
5217                .windows
5218                .lock()
5219                .unwrap_or_else(std::sync::PoisonError::into_inner);
5220            let now = self.clock.now();
5221            let (mut at, mut cause) = if self.pacing.windows {
5222                windows.room(now, charge)
5223            } else {
5224                (now, None)
5225            };
5226            if mutation && !self.pacing.min_mutation_interval.is_zero() {
5227                let mut last = self
5228                    .last_mutation
5229                    .lock()
5230                    .unwrap_or_else(std::sync::PoisonError::into_inner);
5231                // `checked_add` rather than `+`: adding durations can panic on overflow, and
5232                // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
5233                if let Some(earliest) = last
5234                    .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
5235                    && earliest > at
5236                {
5237                    at = earliest;
5238                    cause = Some(pacing::cause::MIN_INTERVAL);
5239                }
5240                *last = Some(at);
5241            }
5242            let ticket = windows.charge(at, charge);
5243            (at.saturating_sub(now), cause, ticket)
5244        };
5245        if let (false, Some(cause)) = (wait.is_zero(), cause) {
5246            self.clock.sleep_because(wait, cause).await;
5247        }
5248        ticket
5249    }
5250
5251    /// Record that the request `ticket` was charged for has finished, so each window — and
5252    /// the configured spacing — counts it from here rather than from the moment it left.
5253    ///
5254    /// This source can only choose when a request *departs*; GitHub's limiter counts when it
5255    /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
5256    /// departure would hand the limiter a window that looked roomier than it is, so a source
5257    /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
5258    /// paced well inside a board's threshold was once refused by it on a slower machine while
5259    /// passing on a quick one. The previous request had arrived before its response came back,
5260    /// so counting from here is never earlier than the limiter does, whatever transit costs
5261    /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
5262    /// reads for the next fifty minutes.
5263    ///
5264    /// A failed attempt is finished too: a request refused by the limiter still arrived, and
5265    /// one that never left costs only room nobody needed.
5266    fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
5267        let now = self.clock.now();
5268        self.windows
5269            .lock()
5270            .unwrap_or_else(std::sync::PoisonError::into_inner)
5271            .finish(ticket, now);
5272        if mutation && !self.pacing.min_mutation_interval.is_zero() {
5273            let mut last = self
5274                .last_mutation
5275                .lock()
5276                .unwrap_or_else(std::sync::PoisonError::into_inner);
5277            // `max` rather than an assignment: a concurrent caller may already have reserved a
5278            // slot further out, and completing this request must never pull that slot back in.
5279            *last = Some(last.map_or(now, |reserved| reserved.max(now)));
5280        }
5281    }
5282
5283    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
5284    /// failure that waiting cannot help — and recorded, whichever of the three it was.
5285    ///
5286    /// This is the one place a request leaves this crate, which is why the accounting is
5287    /// here rather than at each of the callers: a read path added later is counted without
5288    /// anybody remembering to count it, and
5289    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
5290    /// when one is not.
5291    async fn send_once(
5292        &self,
5293        query: &str,
5294        variables: &Value,
5295        number: u32,
5296    ) -> Result<Value, Attempt> {
5297        let executed = self.ledger.priced_document(query);
5298        let Attempted {
5299            result,
5300            limits,
5301            reported_cost,
5302            response_outcome,
5303        } = self.attempt(executed.as_ref(), variables).await;
5304        // No `otherwise` name: every document this source sends is one of its own, and the
5305        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
5306        let sending = accounting::Request::graphql(query, variables, None, reported_cost);
5307        let outcome = match &result {
5308            Ok(_) => accounting::Outcome::Answered,
5309            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
5310            Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
5311        };
5312        self.ledger
5313            .record(sending.finished(outcome, limits).with_execution(
5314                Some(executed.into_owned()),
5315                response_outcome,
5316                number,
5317            ));
5318        result
5319    }
5320
5321    /// The attempt itself, with what its response said about the rate limit alongside.
5322    ///
5323    /// The two are returned together rather than recorded here because every one of the
5324    /// early exits below is a different outcome, and a record written at each of them is a
5325    /// record one of them can be added without.
5326    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
5327        let mut limits = accounting::RateLimit::default();
5328        let mut reported_cost = None;
5329        let mut response_outcome = accounting::ResponseOutcome::TransportError;
5330        let result = self
5331            .attempted(
5332                query,
5333                variables,
5334                &mut limits,
5335                &mut reported_cost,
5336                &mut response_outcome,
5337            )
5338            .await;
5339        Attempted {
5340            result,
5341            limits,
5342            reported_cost,
5343            response_outcome,
5344        }
5345    }
5346
5347    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
5348    async fn attempted(
5349        &self,
5350        query: &str,
5351        variables: &Value,
5352        limits: &mut accounting::RateLimit,
5353        reported_cost: &mut Option<u64>,
5354        response_outcome: &mut accounting::ResponseOutcome,
5355    ) -> Result<Value, Attempt> {
5356        let response = self
5357            .client()
5358            .map_err(Attempt::Failed)?
5359            .post(self.endpoint.clone())
5360            .bearer_auth(self.token.expose_secret())
5361            .json(&json!({"query": query, "variables": variables}))
5362            .send()
5363            .await
5364            .map_err(|e| {
5365                Attempt::Failed(SourceError::Unavailable {
5366                    message: format!("GitHub GraphQL request failed: {}", transport_error(&e)),
5367                })
5368            })?;
5369        let status = response.status();
5370        if !status.is_success() {
5371            *response_outcome = accounting::ResponseOutcome::HttpError;
5372        }
5373        let header = |name: &str| whole_seconds(response.headers().get(name));
5374        let served = response
5375            .headers()
5376            .get(reqwest::header::DATE)
5377            .and_then(|value| value.to_str().ok())
5378            .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
5379            .map(|at| at.with_timezone(&Utc));
5380        *limits = accounting::RateLimit::read(|name| {
5381            response
5382                .headers()
5383                .get(name)
5384                .and_then(|value| value.to_str().ok())
5385                .map(str::to_owned)
5386        });
5387        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
5388        // that are not text at all — is "not known to be exhausted". This never makes a
5389        // response a refusal on its own: it says which limiter a refusal is attributed to
5390        // and where its hint comes from, so a value this cannot read costs a hint rather
5391        // than an answer.
5392        let exhausted = response
5393            .headers()
5394            .get("x-ratelimit-remaining")
5395            .and_then(|value| value.to_str().ok())
5396            == Some("0");
5397        // `retry-after` is what GitHub asks for when it asks; when it does not and the
5398        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
5399        // which is the same question answered as an absolute time. Nothing else here is a
5400        // hint, and a schedule is what answers a refusal that carries none.
5401        let retry_after = header("retry-after");
5402        let hinted_by = if retry_after.is_some() || !exhausted {
5403            Hint::RetryAfter
5404        } else {
5405            Hint::PrimaryReset
5406        };
5407        let hint = retry_after.or_else(|| {
5408            exhausted
5409                .then(|| header("x-ratelimit-reset"))
5410                .flatten()
5411                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
5412        });
5413        // Read before it is parsed, because the evidence which tells a secondary rate
5414        // limit from a rejected credential is in the body of a response whose status says
5415        // only "forbidden" — and a non-success response was never parsed at all.
5416        let body = response.text().await.map_err(|e| {
5417            Attempt::Failed(SourceError::Unavailable {
5418                message: format!(
5419                    "GitHub GraphQL response could not be read: {}",
5420                    transport_error(&e)
5421                ),
5422            })
5423        })?;
5424        *reported_cost = serde_json::from_str::<Value>(&body)
5425            .ok()
5426            .as_ref()
5427            .and_then(|body| body.pointer("/data/rateLimit/cost"))
5428            .and_then(Value::as_u64);
5429        let parsed = serde_json::from_str::<Value>(&body).ok();
5430        *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
5431        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
5432            return Err(Attempt::Limited(Limited {
5433                limiter,
5434                hint,
5435                hinted_by,
5436            }));
5437        }
5438        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
5439            return Err(Attempt::Failed(SourceError::Auth {
5440                message: format!(
5441                    "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"
5442                ),
5443            }));
5444        }
5445        if !status.is_success() {
5446            return Err(Attempt::Failed(SourceError::Unavailable {
5447                message: format!("GitHub GraphQL returned HTTP {status}"),
5448            }));
5449        }
5450        let answered = if query == graphql::ISSUES_BY_REFERENCE {
5451            self.answer_unresolved(&body)
5452        } else if query == graphql::CREATE_ISSUES {
5453            answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
5454        } else {
5455            self.answer(&body)
5456        };
5457        let mut data = answered.map_err(Attempt::Failed)?;
5458        if let Some(served) = served {
5459            stamp_served(&mut data, &served.to_rfc3339());
5460        }
5461        Ok(data)
5462    }
5463
5464    /// What one successful HTTP response says, once its GraphQL errors are read.
5465    /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
5466    /// for this credential — a repository it cannot see, an issue number it does not hold —
5467    /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
5468    /// that reference rather than a failure of the others. Any other error refuses the read,
5469    /// as there.
5470    fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
5471        let parsed: Option<Value> = serde_json::from_str(body).ok();
5472        // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
5473        // `issue`, and the answer holds `null` at exactly that path: anything else — another
5474        // kind of error, one naming no slot, or one beside a value it says is not there — is a
5475        // refusal of the whole read.
5476        let unresolved = |data: &Value, error: &Value| {
5477            error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
5478                && error
5479                    .get("path")
5480                    .and_then(Value::as_array)
5481                    .is_some_and(|path| {
5482                        let alias = path.first().and_then(Value::as_str).and_then(|alias| {
5483                            alias
5484                                .strip_prefix('r')
5485                                .and_then(|slot| slot.parse::<usize>().ok())
5486                                .filter(|slot| *slot < REFERENCE_BATCH)
5487                                .map(|_| alias)
5488                        });
5489                        match (alias, path.get(1).and_then(Value::as_str), path.len()) {
5490                            (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
5491                            (Some(alias), Some("issue"), 2) => data
5492                                .get(alias)
5493                                .and_then(|repository| repository.get("issue"))
5494                                .is_some_and(Value::is_null),
5495                            _ => false,
5496                        }
5497                    })
5498        };
5499        let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
5500            parsed.get("data").is_some_and(|data| {
5501                data.is_object()
5502                    && parsed
5503                        .get("errors")
5504                        .and_then(Value::as_array)
5505                        .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
5506            })
5507        });
5508        match parsed {
5509            Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
5510            _ => self.answer(body),
5511        }
5512    }
5513
5514    fn answer(&self, body: &str) -> Result<Value, SourceError> {
5515        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
5516            message: format!("GitHub returned invalid JSON: {e}"),
5517        })?;
5518        let errors = body
5519            .get("errors")
5520            .map(|value| {
5521                value.as_array().ok_or_else(|| SourceError::Malformed {
5522                    message: "GitHub response errors is not an array".into(),
5523                })
5524            })
5525            .transpose()?;
5526        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
5527            let messages = errors
5528                .iter()
5529                .filter_map(|e| e.get("message").and_then(Value::as_str))
5530                .collect::<Vec<_>>()
5531                .join("; ");
5532            let message = if messages.is_empty() {
5533                "GitHub returned GraphQL errors".into()
5534            } else {
5535                messages
5536            };
5537            let normalized = message.to_ascii_lowercase();
5538            if normalized.contains("resource not accessible") || normalized.contains("scope") {
5539                return Err(SourceError::Auth {
5540                    message: format!(
5541                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
5542                        self.credential_name
5543                    ),
5544                });
5545            }
5546            return Err(SourceError::Refused { message });
5547        }
5548        body.get("data")
5549            .filter(|data| data.is_object())
5550            .cloned()
5551            .ok_or_else(|| SourceError::Malformed {
5552                message: "GitHub response has no data object".into(),
5553            })
5554    }
5555
5556    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
5557    // GraphQL cannot independently page them inside the outer item page. This source page is
5558    // deliberately bounded at that published maximum; the live drift journey exercises it.
5559    async fn board_page(
5560        &self,
5561        items_after: Option<&str>,
5562        items_first: u32,
5563    ) -> Result<Value, SourceError> {
5564        let data = self
5565            .graphql(
5566                graphql::BOARD,
5567                json!({"owner":self.owner,"number":self.project_number,
5568                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
5569                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
5570            )
5571            .await?;
5572        data.pointer("/owner/projectV2")
5573            .filter(|v| !v.is_null())
5574            .cloned()
5575            .ok_or_else(|| SourceError::Refused {
5576                message: format!(
5577                    "GitHub project {}/{} was not found or is not visible to the token",
5578                    self.owner, self.project_number
5579                ),
5580            })
5581    }
5582
5583    /// The search that finds the issues of this board, narrowed by `also` when it is
5584    /// given.
5585    ///
5586    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
5587    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
5588    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
5589    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
5590    /// from a task by the `parent` field each issue carries rather than by the search.
5591    fn board_search(&self, also: Option<&str>) -> String {
5592        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
5593        match also {
5594            Some(also) => format!("{scope} {also}"),
5595            None => scope,
5596        }
5597    }
5598
5599    /// One issue this source reached directly, as the board item a read of the board would
5600    /// have produced — or `None` when this board does not hold it.
5601    ///
5602    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
5603    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
5604    /// item's own id, that item's field values, and the issue as its content. One resolver
5605    /// for both routes is what makes an issue read through a search, through its own node
5606    /// id, or through its project's sub-issues report the same title, the same status, the
5607    /// same labels and the same qualified id.
5608    ///
5609    /// An issue with no entry for *this* board is not this source's to report, which is
5610    /// what keeps an id naming some other repository's issue from being answered as an item
5611    /// of this board. That answer is given about an **exhausted** connection and never
5612    /// about an unread page: the entry is looked for on the page in hand, and only if that
5613    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
5614    /// rest of it.
5615    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
5616        self.resolve_issue_as(issue, true).await
5617    }
5618
5619    /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
5620    /// and writes only when `remember` says to.
5621    async fn resolve_issue_as(
5622        &self,
5623        issue: &Value,
5624        remember: bool,
5625    ) -> Result<Option<Resolved>, SourceError> {
5626        if optional_str(issue, "__typename")? != Some("Issue") {
5627            return Ok(None);
5628        }
5629        let memberships = issue
5630            .get("projectItems")
5631            .ok_or_else(|| SourceError::Malformed {
5632                message: "GitHub issue is missing projectItems".into(),
5633            })?;
5634        let nodes = memberships
5635            .get("nodes")
5636            .and_then(Value::as_array)
5637            .ok_or_else(|| SourceError::Malformed {
5638                message: "GitHub issue projectItems.nodes is not an array".into(),
5639            })?;
5640        let held = match self.board_entry(nodes) {
5641            Some(held) => held.clone(),
5642            None => {
5643                let info = memberships
5644                    .get("pageInfo")
5645                    .ok_or_else(|| SourceError::Malformed {
5646                        message: "GitHub issue projectItems has no pageInfo".into(),
5647                    })?;
5648                // The page held no entry for this board. Whether that means the issue is
5649                // not on it is a question about the rest of the connection, and only a
5650                // connection with no rest answers it here.
5651                if !required_bool(info, "hasNextPage")? {
5652                    return Ok(None);
5653                }
5654                let cursor = required_str(info, "endCursor")?;
5655                validate_cursor_progress(None, cursor)?;
5656                let issue_id = required_str(issue, "id")?;
5657                match self.board_membership(issue_id, cursor).await? {
5658                    Some(held) => held,
5659                    None => return Ok(None),
5660                }
5661            }
5662        };
5663        // The membership's own `updatedAt` rides along: it is the board half of the item's change
5664        // token, which a field change moves where the issue's own time does not.
5665        let item = json!({
5666            "id": required_str(&held, "id")?,
5667            "updatedAt": held.get("updatedAt"),
5668            "project": held.get("project"),
5669            "fieldValues": held.get("fieldValues"),
5670            "content": issue,
5671        });
5672        if remember {
5673            self.resolve(&item)
5674        } else {
5675            self.resolve_unremembered(&item)
5676        }
5677    }
5678
5679    /// This board's own entry among one page of an issue's `Issue.projectItems`.
5680    ///
5681    /// One spelling of *which membership is this board's*, so the page a read carries and
5682    /// the pages [`Self::board_membership`] walks are searched by the same rule.
5683    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5684        nodes.iter().find(|node| {
5685            node.pointer("/project/number").and_then(Value::as_u64)
5686                == Some(u64::from(self.project_number))
5687        })
5688    }
5689
5690    /// The rest of one issue's board memberships, from `after`, for this board's entry.
5691    ///
5692    /// The recovery read: a page of memberships that holds no entry for this board says
5693    /// nothing about the memberships past it, so the connection is walked to exhaustion
5694    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5695    /// positive answer — the whole connection was read and no entry named this board —
5696    /// rather than a failure, and the walk is held to
5697    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5698    /// with a cursor that does not advance is refused instead of spun on.
5699    async fn board_membership(
5700        &self,
5701        issue: &str,
5702        after: &str,
5703    ) -> Result<Option<Value>, SourceError> {
5704        let mut after = after.to_owned();
5705        loop {
5706            let data = self
5707                .graphql(
5708                    graphql::ISSUE_BOARD_ITEMS,
5709                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5710                           "nestedFirst":NESTED_PAGE_SIZE}),
5711                )
5712                .await?;
5713            let Some(connection) = data
5714                .pointer("/node/projectItems")
5715                .filter(|value| !value.is_null())
5716            else {
5717                // The id resolved to nothing, or to something with no memberships to walk —
5718                // which is the same answer as a connection holding no entry for this board.
5719                return Ok(None);
5720            };
5721            let nodes = connection
5722                .get("nodes")
5723                .and_then(Value::as_array)
5724                .ok_or_else(|| SourceError::Malformed {
5725                    message: "GitHub issue projectItems.nodes is not an array".into(),
5726                })?;
5727            if let Some(held) = self.board_entry(nodes) {
5728                return Ok(Some(held.clone()));
5729            }
5730            let info = connection
5731                .get("pageInfo")
5732                .ok_or_else(|| SourceError::Malformed {
5733                    message: "GitHub issue projectItems has no pageInfo".into(),
5734                })?;
5735            let next = required_bool(info, "hasNextPage")?
5736                .then(|| required_str(info, "endCursor"))
5737                .transpose()?;
5738            match next {
5739                Some(next) => {
5740                    validate_cursor_progress(Some(&after), next)?;
5741                    after = next.to_owned();
5742                }
5743                None => return Ok(None),
5744            }
5745        }
5746    }
5747
5748    /// One page of a board-scoped issue search, and where the next page resumes.
5749    async fn search_page(
5750        &self,
5751        search: &str,
5752        first: u32,
5753        after: Option<&str>,
5754    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5755        let data = self
5756            .graphql(
5757                graphql::SEARCH_ISSUES,
5758                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5759                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5760                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5761            )
5762            .await?;
5763        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5764            message: "GitHub search response has no search connection".into(),
5765        })?;
5766        let mut found = Vec::new();
5767        for node in connection
5768            .get("nodes")
5769            .and_then(Value::as_array)
5770            .ok_or_else(|| SourceError::Malformed {
5771                message: "GitHub search nodes is not an array".into(),
5772            })?
5773        {
5774            if let Some(resolved) = self.resolve_issue(node).await? {
5775                found.push(resolved);
5776            }
5777        }
5778        let info = connection
5779            .get("pageInfo")
5780            .ok_or_else(|| SourceError::Malformed {
5781                message: "GitHub search connection has no pageInfo".into(),
5782            })?;
5783        let next = required_bool(info, "hasNextPage")?
5784            .then(|| required_str(info, "endCursor"))
5785            .transpose()?
5786            .map(str::to_owned);
5787        if let Some(next) = &next {
5788            validate_cursor_progress(after, next)?;
5789        }
5790        Ok((found, next))
5791    }
5792
5793    /// Every issue this board holds, completed with what this run wrote.
5794    ///
5795    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
5796    /// is an index and is eventually consistent, so an issue this run created seconds ago
5797    /// can be absent from it, and a project listed straight after being written would
5798    /// otherwise be missing from its own board. What is added back is only what this
5799    /// process itself wrote, out of [`Self::created`], which lives and dies with the
5800    /// process.
5801    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5802        let found = self.searched_issues().await?;
5803        self.completed_with_written(found, |_| true)
5804    }
5805
5806    /// Every issue this board's own search reports, walked to exhaustion, read once per
5807    /// source.
5808    ///
5809    /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
5810    /// needs it too and the two would otherwise walk the same search twice in one command.
5811    /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
5812    /// is.
5813    async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5814        let cached = self.search_cache()?.clone();
5815        if let Some(held) = cached {
5816            return Ok(held);
5817        }
5818        let mut after: Option<String> = None;
5819        let mut found = Vec::new();
5820        let search = self.board_search(None);
5821        loop {
5822            let (page, next) = self
5823                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5824                .await?;
5825            found.extend(page);
5826            match next {
5827                Some(next) => after = Some(next),
5828                None => break,
5829            }
5830        }
5831        *self.search_cache()? = Some(found.clone());
5832        Ok(found)
5833    }
5834
5835    /// Every issue this board's own search reports with `also` beside its scope, walked to
5836    /// exhaustion — held for no later read, which may not be narrowed alike.
5837    async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5838        let mut after: Option<String> = None;
5839        let mut found = Vec::new();
5840        let search = self.board_search(Some(also));
5841        loop {
5842            let (page, next) = self
5843                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5844                .await?;
5845            found.extend(page);
5846            match next {
5847                Some(next) => after = Some(next),
5848                None => return Ok(found),
5849            }
5850        }
5851    }
5852
5853    /// This process's own view of the board's issues, or the refusal a poisoned lock is.
5854    fn search_cache(
5855        &self,
5856    ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
5857        self.search_cache
5858            .lock()
5859            .map_err(|_| SourceError::Unavailable {
5860                message: "this source's view of the board's issues was left inconsistent by an \
5861                      earlier failure; next: run the command again"
5862                    .into(),
5863            })
5864    }
5865
5866    /// `found`, with everything this run wrote that `keep` accepts and the read did not
5867    /// report.
5868    ///
5869    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
5870    /// at all: the search index is behind, and a node read of an item filed moments ago can
5871    /// be too.
5872    fn completed_with_written(
5873        &self,
5874        mut found: Vec<Resolved>,
5875        keep: impl Fn(&Resolved) -> bool,
5876    ) -> Result<Vec<Resolved>, SourceError> {
5877        for own in self.created()?.iter().filter(|own| keep(own)) {
5878            if !found.iter().any(|item| item.id == own.id) {
5879                found.push(own.clone());
5880            }
5881        }
5882        Ok(found)
5883    }
5884
5885    /// What resolving one node id reached.
5886    ///
5887    /// Three answers rather than an `Option`, because a board *draft* is none of the other
5888    /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
5889    /// is completed by a read of the draft itself rather than reported as nothing.
5890    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
5891        let asked = self
5892            .graphql(
5893                graphql::ISSUE,
5894                json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
5895                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5896            )
5897            .await;
5898        let data = match asked {
5899            Ok(data) => data,
5900            // A string that is not a node id at all is not a failure to report: it is an id
5901            // this board does not hold, which is what every read of one already answers.
5902            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
5903            Err(error) => return Err(error),
5904        };
5905        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5906            return Ok(Reached::Nothing);
5907        };
5908        if optional_str(node, "__typename")? == Some("DraftIssue") {
5909            return Ok(Reached::Draft);
5910        }
5911        Ok(match self.resolve_issue(node).await? {
5912            Some(item) => Reached::Held(Box::new(item)),
5913            None => Reached::Nothing,
5914        })
5915    }
5916
5917    /// One item of this board by its own id, whatever kind it is.
5918    ///
5919    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
5920    /// run wrote is read first, because a node read of an item created moments ago can
5921    /// still be behind the board field values written onto it — see [`Self::created`].
5922    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5923        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
5924            return Ok(Some(own.clone()));
5925        }
5926        if let Some(held) = self.read_ahead()?.remove(id) {
5927            return Ok(held);
5928        }
5929        match self.reach(id).await? {
5930            Reached::Held(item) => Ok(Some(*item)),
5931            Reached::Nothing => Ok(None),
5932            Reached::Draft => self.draft_by_id(id).await,
5933        }
5934    }
5935
5936    /// [`Self::item_by_id`] for a document: one [`graphql::DOCUMENT_IN_PROJECT`] read, which
5937    /// answers the project it is filed under and that project's members beside it.
5938    ///
5939    /// Each is held for this command as a read of it would have been: the project for the
5940    /// next read of it by its id, and — when its page of members is the whole of them — the
5941    /// members for the next listing of that project. So a write held to who may read the
5942    /// document, which reads all three, is answered by this one request.
5943    async fn document_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5944        if self.created()?.iter().any(|own| own.id == *id) || self.read_ahead()?.contains_key(id) {
5945            return self.item_by_id(id).await;
5946        }
5947        let asked = self
5948            .graphql(
5949                graphql::DOCUMENT_IN_PROJECT,
5950                json!({"id":id.0,"first":MAX_PAGE_SIZE,"memberFirst":MEMBER_PAGE_SIZE,
5951                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
5952                       "duplicates":true}),
5953            )
5954            .await;
5955        let data = match asked {
5956            Ok(data) => data,
5957            Err(error) if unresolvable_node(&error) => return Ok(None),
5958            Err(error) => return Err(error),
5959        };
5960        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5961            return Ok(None);
5962        };
5963        if optional_str(node, "__typename")? == Some("DraftIssue") {
5964            return self.draft_by_id(id).await;
5965        }
5966        let Some(item) = self.resolve_issue(node).await? else {
5967            return Ok(None);
5968        };
5969        // An answer for another issue than the one asked about says nothing of that one.
5970        if item.id != *id {
5971            return Err(SourceError::Malformed {
5972                message: format!(
5973                    "GitHub answered issue {} when asked for document {}",
5974                    item.id.0, id.0
5975                ),
5976            });
5977        }
5978        if let Some(project) = node.get("project").filter(|value| !value.is_null())
5979            && let Some(parent) = self
5980                .resolve_issue(project)
5981                .await?
5982                .filter(|parent| Some(&parent.id) == item.parent.as_ref())
5983        {
5984            let members = project.get("subIssues").filter(|value| !value.is_null());
5985            let whole = members
5986                .and_then(|members| members.pointer("/pageInfo/hasNextPage"))
5987                .and_then(Value::as_bool)
5988                == Some(false);
5989            if whole {
5990                let nodes = members
5991                    .and_then(|members| members.get("nodes"))
5992                    .and_then(Value::as_array)
5993                    .ok_or_else(|| SourceError::Malformed {
5994                        message: "GitHub subIssues.nodes is not an array".into(),
5995                    })?;
5996                let mut children = Vec::new();
5997                for member in nodes {
5998                    if let Some(resolved) = self.resolve_issue(member).await? {
5999                        children.push(resolved);
6000                    }
6001                }
6002                // Held as the project's members only when every one resolved and says it is
6003                // filed under it: a page with any other answers nothing of all the members.
6004                if children.len() == nodes.len()
6005                    && children
6006                        .iter()
6007                        .all(|child| child.parent.as_ref() == Some(&parent.id))
6008                {
6009                    self.children_cache()?
6010                        .insert(parent.id.clone(), (parent.id.clone(), children));
6011                }
6012            }
6013            self.read_ahead()?.insert(parent.id.clone(), Some(parent));
6014        }
6015        Ok(Some(item))
6016    }
6017
6018    /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
6019    /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
6020    /// than one request per id.
6021    ///
6022    /// What this run wrote answers first, as it does there, and only the rest is read. One id
6023    /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
6024    /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
6025    /// id at a time, so that id is answered as not held and the others as themselves; a draft
6026    /// is completed by a read of the draft, exactly as there.
6027    async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
6028        let mut found: Vec<Option<Option<Resolved>>> = {
6029            let created = self.created()?;
6030            ids.iter()
6031                .map(|id| {
6032                    created
6033                        .iter()
6034                        .find(|own| own.id == *id)
6035                        .map(|own| Some(own.clone()))
6036                })
6037                .collect()
6038        };
6039        let unread: Vec<NativeId> = ids
6040            .iter()
6041            .zip(&found)
6042            .filter(|(_, found)| found.is_none())
6043            .map(|(id, _)| id.clone())
6044            .collect();
6045        let mut read = Vec::with_capacity(unread.len());
6046        if let [one] = unread.as_slice() {
6047            read.push(self.item_by_id(one).await?);
6048        } else {
6049            for batch in unread.chunks(DETAIL_BATCH) {
6050                let data = match self
6051                    .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
6052                    .await
6053                {
6054                    Ok(data) => data,
6055                    Err(error) if unresolvable_node(&error) => {
6056                        for id in batch {
6057                            read.push(self.item_by_id(id).await?);
6058                        }
6059                        continue;
6060                    }
6061                    Err(error) => return Err(error),
6062                };
6063                for (slot, id) in batch.iter().enumerate() {
6064                    let node =
6065                        data.get(format!("i{slot}"))
6066                            .ok_or_else(|| SourceError::Malformed {
6067                                message: format!(
6068                                    "GitHub answered a batch read with no item for {}",
6069                                    id.0
6070                                ),
6071                            })?;
6072                    read.push(if node.is_null() {
6073                        None
6074                    } else if optional_str(node, "__typename")? == Some("DraftIssue") {
6075                        self.draft_by_id(id).await?
6076                    } else {
6077                        if optional_str(node, "__typename")? == Some("Issue")
6078                            && required_str(node, "id")? != id.0
6079                        {
6080                            return Err(SourceError::Malformed {
6081                                message: format!(
6082                                    "GitHub answered the read of {} with issue {}",
6083                                    id.0,
6084                                    required_str(node, "id")?
6085                                ),
6086                            });
6087                        }
6088                        self.resolve_issue(node).await?
6089                    });
6090                }
6091            }
6092        }
6093        let mut read = read.into_iter();
6094        Ok(found
6095            .iter_mut()
6096            .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
6097            .collect())
6098    }
6099
6100    fn read_ahead(
6101        &self,
6102    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
6103        self.read_ahead
6104            .lock()
6105            .map_err(|_| SourceError::Unavailable {
6106                message:
6107                    "items read ahead for a copy were left inconsistent; run the command again"
6108                        .into(),
6109            })
6110    }
6111
6112    fn resolved_cache(
6113        &self,
6114    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6115        self.resolved_cache
6116            .lock()
6117            .map_err(|_| SourceError::Unavailable {
6118                message: "resolved item records were left inconsistent; run the command again"
6119                    .into(),
6120            })
6121    }
6122
6123    /// Reuse a record this invocation already resolved. The mutation sender invalidates
6124    /// it before writing, so a partial failure cannot leave a pre-write binding behind.
6125    async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6126        let cached = self.resolved_cache()?.get(id).cloned();
6127        if let Some(item) = cached {
6128            return Ok(Some(item));
6129        }
6130        self.item_by_id(id).await
6131    }
6132
6133    /// Take out the item an issue read by reference reached for `id`, when one is held: the
6134    /// status write it was read for is the one caller, so no other read or write is ever
6135    /// answered from a read that selected only what that write needs.
6136    fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6137        Ok(self
6138            .held_cache
6139            .lock()
6140            .map_err(|_| SourceError::Unavailable {
6141                message: "items read by issue reference were left inconsistent; run the \
6142                          command again"
6143                    .into(),
6144            })?
6145            .remove(id))
6146    }
6147
6148    /// One board draft by its own id, with the board item it sits in — or `None` when no
6149    /// item of this board is that draft's.
6150    ///
6151    /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
6152    /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
6153    /// links a draft to one board item, so the page this read carries is the whole of that
6154    /// connection, and a page that reports more than it holds is refused rather than read
6155    /// as an answer about memberships nobody read.
6156    async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
6157        let data = self
6158            .graphql(
6159                graphql::DRAFT,
6160                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
6161                       "boardItems":BOARD_ITEMS_PAGE_SIZE}),
6162            )
6163            .await?;
6164        // Gone between the two reads is an answer — the draft is no longer there. Anything
6165        // else than the draft [`Self::reach`] was just told this id is, is not one.
6166        let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
6167            return Ok(None);
6168        };
6169        if optional_str(draft, "__typename")? != Some("DraftIssue") {
6170            return Err(SourceError::Malformed {
6171                message: format!(
6172                    "GitHub answered {} as a draft and then as something else",
6173                    id.0
6174                ),
6175            });
6176        }
6177        if required_str(draft, "id")? != id.0 {
6178            return Err(SourceError::Malformed {
6179                message: format!("GitHub answered a different draft for {}", id.0),
6180            });
6181        }
6182        let memberships = draft
6183            .get("projectV2Items")
6184            .ok_or_else(|| SourceError::Malformed {
6185                message: format!("GitHub draft {} is missing projectV2Items", id.0),
6186            })?;
6187        let nodes = memberships
6188            .get("nodes")
6189            .and_then(Value::as_array)
6190            .ok_or_else(|| SourceError::Malformed {
6191                message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
6192            })?;
6193        let info = memberships
6194            .get("pageInfo")
6195            .ok_or_else(|| SourceError::Malformed {
6196                message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
6197            })?;
6198        // Read whether or not this board's entry is on the page: a page claiming more than
6199        // the one item GitHub links a draft to is a malformed answer either way.
6200        if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
6201            return Err(SourceError::Malformed {
6202                message: format!(
6203                    "GitHub draft {} reports more board items than the one GitHub links a draft \
6204                     to",
6205                    id.0
6206                ),
6207            });
6208        }
6209        if let Some(node) = nodes.first()
6210            && node
6211                .pointer("/project/number")
6212                .and_then(Value::as_u64)
6213                .is_none()
6214        {
6215            return Err(SourceError::Malformed {
6216                message: format!(
6217                    "GitHub draft {} board item has no numeric project number",
6218                    id.0
6219                ),
6220            });
6221        }
6222        let Some(held) = self.board_entry(nodes) else {
6223            return Ok(None);
6224        };
6225        if required_str(
6226            held.get("project").ok_or_else(|| SourceError::Malformed {
6227                message: format!("GitHub draft {} board item has no project", id.0),
6228            })?,
6229            "id",
6230        )? != self.board_fields().await?.id.as_str()
6231        {
6232            return Ok(None);
6233        }
6234        let item = json!({
6235            "id": required_str(held, "id")?,
6236            "project": held.get("project"),
6237            "fieldValues": held.get("fieldValues"),
6238            "content": draft,
6239        });
6240        self.resolve(&item)
6241    }
6242
6243    /// The board's own id and field definitions, for a write whose item does not carry
6244    /// them — never its items.
6245    ///
6246    /// A board this command has already listed supplies them, since it read them beside its
6247    /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
6248    /// is consulted about which items the board holds: see the module documentation for
6249    /// why a question about one known item is answered by reading that item.
6250    async fn board_fields(&self) -> Result<BoardFields, SourceError> {
6251        if let Some(board) = self.board_cache()?.as_ref() {
6252            return Ok(BoardFields {
6253                id: BoardId::parse(&board.id)?,
6254                fields: board.fields.clone(),
6255            });
6256        }
6257        if let Some(held) = self.fields_cache()?.clone() {
6258            return Ok(held);
6259        }
6260        let data = self
6261            .graphql(
6262                graphql::BOARD_FIELDS,
6263                json!({"owner":self.owner,"number":self.project_number,
6264                       "nestedFirst":NESTED_PAGE_SIZE}),
6265            )
6266            .await?;
6267        self.fields_read(&data)
6268    }
6269
6270    /// The board's id and fields out of an answer carrying the `boardFields` root, held for
6271    /// the rest of this command.
6272    fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
6273        let board = data
6274            .pointer("/boardFields/projectV2")
6275            .filter(|value| !value.is_null())
6276            .ok_or_else(|| SourceError::Refused {
6277                message: format!(
6278                    "GitHub project {}/{} was not found or is not visible to the token",
6279                    self.owner, self.project_number
6280                ),
6281            })?;
6282        let read = BoardFields {
6283            id: BoardId::parse(required_str(board, "id")?)?,
6284            fields: board.get("fields").cloned().unwrap_or(Value::Null),
6285        };
6286        *self.fields_cache()? = Some(read.clone());
6287        Ok(read)
6288    }
6289
6290    /// Read what creating an issue in `repository` needs and this command has not read yet —
6291    /// the board's fields and the repository's node id — in one request when it needs both.
6292    ///
6293    /// When either is already known this sends nothing, and the other is read by its own
6294    /// document where it is asked for, so no create reads anything twice.
6295    async fn creation_context(
6296        &self,
6297        repository: &RepositoryTarget,
6298        incoming: &Incoming<'_>,
6299    ) -> Result<(), SourceError> {
6300        let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
6301        if fields_known || self.repository_cache()?.contains_key(repository) {
6302            return Ok(());
6303        }
6304        let data = self
6305            .graphql(
6306                graphql::CREATION_CONTEXT,
6307                json!({"owner":self.owner,"number":self.project_number,
6308                       "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
6309                       "repositoryName":repository.name}),
6310            )
6311            .await?;
6312        self.fields_read(&data)?;
6313        self.repository_read(&data, repository, incoming)?;
6314        Ok(())
6315    }
6316
6317    /// This process's own view of the board's fields, or the refusal a poisoned lock is.
6318    fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
6319        self.fields_cache
6320            .lock()
6321            .map_err(|_| SourceError::Unavailable {
6322                message: "this source's view of the board's fields was left inconsistent by an \
6323                      earlier failure; next: run the command again"
6324                    .into(),
6325            })
6326    }
6327
6328    /// What a write to `item` needs of the board, read off that item when it says enough and
6329    /// off [`Self::board_fields`] when it does not.
6330    ///
6331    /// A node read of an item names its board and carries the definition of every field it
6332    /// holds a value of — so an item naming its board, holding a value of the origin field
6333    /// when the write carries an origin, and, when the write carries a status, holding a
6334    /// `Status` value, needs no read of the board at all. **Nothing the item does not say is
6335    /// guessed:** a field it holds no value of may still be on the board, and a view reading it
6336    /// as absent would refuse a write the board can take or skip a field write the board
6337    /// needs, so such an item — and a create, which has no item yet — takes the board's fields
6338    /// from their own read instead. A write carrying no origin to an item holding none — every
6339    /// write out of a source declared private — writes no origin whatever the board defines,
6340    /// so it needs no definition of that field.
6341    async fn fields_for(
6342        &self,
6343        item: Option<&Resolved>,
6344        writes_origin: bool,
6345        writes_status: bool,
6346        selects_priority: bool,
6347    ) -> Result<BoardFields, SourceError> {
6348        if let Some(board) = item.and_then(Resolved::carried_board) {
6349            return Ok(board);
6350        }
6351        if let Some(item) = item
6352            && let Some(board_id) = item.named_board()
6353            && (!writes_origin || item.defines(ORIGIN_FIELD))
6354            && (!writes_status || item.defines("Status"))
6355            && (!selects_priority || item.defines(PRIORITY_FIELD))
6356            && self
6357                .metadata_fields
6358                .iter()
6359                .all(|projection| item.defines(&projection.field))
6360        {
6361            return Ok(BoardFields {
6362                id: board_id,
6363                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6364            });
6365        }
6366        self.board_fields().await
6367    }
6368
6369    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
6370    /// when that id names nothing here with a sub-issue relationship to walk.
6371    ///
6372    /// `None` and an empty answer are different: `None` is *this is not an issue of this
6373    /// GitHub*, which is what sends a project selector on to be read as a name, and an
6374    /// empty vector is a project that holds nothing.
6375    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6376        let mut after: Option<String> = None;
6377        let mut children = Vec::new();
6378        // The project issue as the first page carried it, held once the walk has ended.
6379        let mut parent: Option<Resolved> = None;
6380        loop {
6381            let asked = self
6382                .graphql(
6383                    graphql::SUB_ISSUES,
6384                    json!({"id":id.0,"memberFirst":MEMBER_PAGE_SIZE,"after":after,
6385                           "nestedFirst":NESTED_PAGE_SIZE,
6386                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6387                )
6388                .await;
6389            let data = match asked {
6390                Ok(data) => data,
6391                // A string that is not a node id at all is not a failure to report: it is
6392                // the ordinary answer to a selector naming a project by its name.
6393                Err(error) if unresolvable_node(&error) => return Ok(None),
6394                Err(error) => return Err(error),
6395            };
6396            let Some(connection) = data
6397                .pointer("/node/subIssues")
6398                .filter(|value| !value.is_null())
6399            else {
6400                // No such node, or one with no sub-issue relationship — a board draft is
6401                // the one this board can really hold.
6402                return Ok(None);
6403            };
6404            if after.is_none()
6405                && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
6406            {
6407                parent = self
6408                    .resolve_issue(node)
6409                    .await?
6410                    .filter(|resolved| resolved.id == *id);
6411            }
6412            for node in connection
6413                .get("nodes")
6414                .and_then(Value::as_array)
6415                .ok_or_else(|| SourceError::Malformed {
6416                    message: "GitHub subIssues.nodes is not an array".into(),
6417                })?
6418            {
6419                if let Some(resolved) = self.resolve_issue(node).await? {
6420                    children.push(resolved);
6421                }
6422            }
6423            let info = connection
6424                .get("pageInfo")
6425                .ok_or_else(|| SourceError::Malformed {
6426                    message: "GitHub subIssues connection has no pageInfo".into(),
6427                })?;
6428            let next = required_bool(info, "hasNextPage")?
6429                .then(|| required_str(info, "endCursor"))
6430                .transpose()?;
6431            match next {
6432                Some(next) => {
6433                    validate_cursor_progress(after.as_deref(), next)?;
6434                    after = Some(next.to_owned());
6435                }
6436                None => {
6437                    // Held only once the walk has ended, so one failing part way holds
6438                    // nothing and every read it would have answered is made.
6439                    // The project issue the first page carried answers the next read of the
6440                    // project by its id in this command, as a read of it made now would: what
6441                    // classifying a project for a write reads straight after its members.
6442                    if let Some(parent) = &parent {
6443                        self.read_ahead()?
6444                            .entry(parent.id.clone())
6445                            .or_insert_with(|| Some(parent.clone()));
6446                    }
6447                    let mut carried = self.carried()?;
6448                    for item in parent.into_iter().chain(children.iter().cloned()) {
6449                        carried.insert(item.id.clone(), item);
6450                    }
6451                    return Ok(Some(children));
6452                }
6453            }
6454        }
6455    }
6456
6457    fn carried(
6458        &self,
6459    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6460        self.carried.lock().map_err(|_| SourceError::Unavailable {
6461            message: "items a project's listing carried were left inconsistent; run the \
6462                      command again"
6463                .into(),
6464        })
6465    }
6466
6467    /// Which issue of this board a project *name* is, or `None` when none is.
6468    ///
6469    /// One bounded query which filters on that name at the server, rather than a walk of
6470    /// every issue the board holds. The name is compared again here: the qualifier narrows
6471    /// what GitHub sends, and this source decides what it names.
6472    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
6473        let search = self.board_search(Some(&title_qualifier(name)));
6474        let mut after = None;
6475        loop {
6476            let (candidates, next) = self
6477                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6478                .await?;
6479            if let Some(item) = candidates.into_iter().find(|item| {
6480                item.kind == BoardKind::Work(ItemKind::Project)
6481                    && item.title.eq_ignore_ascii_case(name)
6482            }) {
6483                return Ok(Some(item.id));
6484            }
6485            match next {
6486                Some(next) => after = Some(next),
6487                None => return Ok(None),
6488            }
6489        }
6490    }
6491
6492    /// Everything filed under one project of this board: the sub-issues of the issue that
6493    /// project is.
6494    ///
6495    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
6496    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
6497    /// gains projects, or as another project gains tasks.
6498    ///
6499    /// A qualified id names the issue and is asked for its sub-issues directly: one
6500    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
6501    /// read as a project *name*, which costs the one bounded search
6502    /// [`Self::project_by_name`] makes.
6503    ///
6504    /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
6505    /// — and each answer is still cut to the items whose parent is this project, so one this
6506    /// process has since filed elsewhere is not reported here, and completed with what this
6507    /// process filed under it.
6508    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
6509        let held = self.children_cache()?.get(selector).cloned();
6510        let (project, children) = match held {
6511            Some(held) => held,
6512            None => {
6513                let answered = match self.sub_issues(selector).await? {
6514                    Some(children) => (selector.clone(), children),
6515                    None => match self.project_by_name(&selector.0).await? {
6516                        Some(project) => {
6517                            let children = self.sub_issues(&project).await?.unwrap_or_default();
6518                            (project, children)
6519                        }
6520                        None => return Ok(Vec::new()),
6521                    },
6522                };
6523                self.children_cache()?
6524                    .insert(selector.clone(), answered.clone());
6525                answered
6526            }
6527        };
6528        let mut children: Vec<Resolved> = children
6529            .into_iter()
6530            .filter(|child| child.parent.as_ref() == Some(&project))
6531            .collect();
6532        for own in self.updated()?.iter() {
6533            if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
6534                children.push(own.clone());
6535            }
6536        }
6537        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
6538    }
6539
6540    /// Every issue of this board GitHub's issue search reports updated at or after `since`,
6541    /// completed with what this run wrote — the candidates a comment-activity read confirms.
6542    ///
6543    /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
6544    /// on the board whatever repository, and whatever owner, it lives in, so no repository or
6545    /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
6546    /// sufficient is a fact about GitHub rather than about this source: a comment written on an
6547    /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
6548    /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
6549    /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
6550    /// rather than silently narrowing a caller's answer.
6551    ///
6552    /// The instant is written to the second, rounded down, which can only widen what the
6553    /// search returns; confirmation against each candidate's own comments is what makes the
6554    /// answer exact. The search is an index that lags a write by a second or two — the module
6555    /// documentation records it — so a caller that asks again from its last instant should
6556    /// overlap the two by more than that.
6557    async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
6558        let found = self.searched(&updated_qualifier(since)).await?;
6559        self.completed_with_written(found, |_| true)
6560    }
6561
6562    /// Every issue of this board GitHub's issue search reports for the board-scoped search
6563    /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
6564    ///
6565    /// Uncompleted: what this process wrote is added by the caller, which knows whether its
6566    /// own record is the fresher of the two.
6567    async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6568        let search = self.board_search(Some(also));
6569        let mut after: Option<String> = None;
6570        let mut found = Vec::new();
6571        loop {
6572            let (page, next) = self
6573                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6574                .await?;
6575            found.extend(page);
6576            match next {
6577                Some(next) => after = Some(next),
6578                None => return Ok(found),
6579            }
6580        }
6581    }
6582
6583    /// A bounded task answer; the versioned cursor carries the connection position, how
6584    /// many rows of the page starting there were already handed out, and the own-write ids
6585    /// already observed, including across a new source instance.
6586    ///
6587    /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
6588    /// sliced from the pages it needs; why is the module documentation's paging contract.
6589    async fn search_tasks(
6590        &self,
6591        query: &TaskQuery,
6592        page: &PageRequest,
6593        also: &str,
6594    ) -> Result<Page<Task>, SourceError> {
6595        let mut position = match &page.cursor {
6596            None => SearchPosition::default(),
6597            Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
6598                .ok()
6599                .filter(|position| {
6600                    position.version == SEARCH_CURSOR_VERSION
6601                        && position.connection.valid_resume(position.offset)
6602                })
6603                .ok_or_else(|| SourceError::Config {
6604                    message: "page cursor is invalid".into(),
6605                })?,
6606        };
6607        let search = self.board_search(Some(also));
6608        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6609        let own = self.with_own_writes(Vec::new())?;
6610        // An issue this process commented on is a candidate of a comment-activity read
6611        // whether or not the search has caught up with the comment; see `Self::commented`.
6612        let commented = match query.commented_since {
6613            Some(_) => self.commented()?.clone(),
6614            None => Vec::new(),
6615        };
6616        for id in own.iter().map(|item| &item.id).chain(&commented) {
6617            if !position.own.contains(id) {
6618                position.own.push(id.clone());
6619            }
6620        }
6621        let mut tasks = Vec::new();
6622        while !position.connection.exhausted() && tasks.len() < limit {
6623            let first = SEARCH_PAGE_SIZE;
6624            // Page size is part of the key: a short cached answer cannot answer a wider ask.
6625            let key =
6626                serde_json::to_string(&("page", &search, &position.connection.after(), first))
6627                    .expect("search page key is serializable");
6628            let cached = if query.commented_since.is_none() {
6629                self.narrowed_cache()?.get(&key).cloned()
6630            } else {
6631                None
6632            };
6633            let (found, next) = match cached {
6634                Some(found) => {
6635                    let next = self
6636                        .search_next
6637                        .lock()
6638                        .map_err(|_| SourceError::Unavailable {
6639                            message:
6640                                "search pagination was left inconsistent; run the command again"
6641                                    .into(),
6642                        })?
6643                        .get(&key)
6644                        .cloned()
6645                        .flatten();
6646                    (found, next)
6647                }
6648                None => {
6649                    let (found, next) = self
6650                        .search_page(&search, first, position.connection.after())
6651                        .await?;
6652                    if query.commented_since.is_none() {
6653                        self.search_next
6654                            .lock()
6655                            .map_err(|_| SourceError::Unavailable {
6656                                message:
6657                                    "search pagination was left inconsistent; run the command again"
6658                                        .into(),
6659                            })?
6660                            .insert(key.clone(), next.clone());
6661                        self.narrowed_cache()?.insert(key, found.clone());
6662                    }
6663                    (found, next)
6664                }
6665            };
6666            let rows = found.len();
6667            for mut item in found.into_iter().skip(position.offset) {
6668                if tasks.len() == limit {
6669                    break;
6670                }
6671                position.offset += 1;
6672                if position.own.contains(&item.id) {
6673                    if position.seen.contains(&item.id) {
6674                        continue;
6675                    }
6676                    position.seen.push(item.id.clone());
6677                    // The search's own copy of an issue this process only commented on is as
6678                    // good as a node read of it, since its comments are read either way.
6679                    let only_commented = commented.contains(&item.id)
6680                        && !own.iter().any(|written| written.id == item.id);
6681                    if !only_commented {
6682                        let updated_at = item.updated_at;
6683                        let Some(written) = self.search_written(&own, &item.id).await? else {
6684                            continue;
6685                        };
6686                        item = written;
6687                        item.updated_at = item.updated_at.max(updated_at);
6688                        self.resolved_cache()?.insert(item.id.clone(), item.clone());
6689                    }
6690                }
6691                if item.kind == BoardKind::Work(ItemKind::Task) {
6692                    let task = item.task()?;
6693                    if task_matches(&task, query, &query.project)
6694                        && self.commented_since(&item, query.commented_since).await?
6695                    {
6696                        tasks.push(task);
6697                    }
6698                }
6699            }
6700            if position.offset < rows {
6701                continue;
6702            }
6703            position.offset = 0;
6704            position.connection = match next {
6705                Some(after) => SearchConnection::Continuing {
6706                    after: Cursor(after),
6707                },
6708                None => SearchConnection::Exhausted {},
6709            };
6710        }
6711        if position.connection.exhausted() {
6712            for id in position.own.clone() {
6713                if position.seen.contains(&id) {
6714                    continue;
6715                }
6716                if tasks.len() == limit {
6717                    break;
6718                }
6719                position.seen.push(id.clone());
6720                let Some(item) = self.search_written(&own, &id).await? else {
6721                    continue;
6722                };
6723                if item.kind == BoardKind::Work(ItemKind::Task) {
6724                    let task = item.task()?;
6725                    if task_matches(&task, query, &query.project)
6726                        && self.commented_since(&item, query.commented_since).await?
6727                    {
6728                        tasks.push(task);
6729                    }
6730                }
6731            }
6732        }
6733        let more = !position.connection.exhausted()
6734            || position.own.iter().any(|id| !position.seen.contains(id));
6735        Ok(Page {
6736            items: tasks,
6737            next: more.then(|| {
6738                Cursor(serde_json::to_string(&position).expect("search position is serializable"))
6739            }),
6740        })
6741    }
6742
6743    /// A resumed process has the ids but no write records; resolve only a record the
6744    /// current page needs, by its uncached node read rather than the lagging search index.
6745    async fn search_written(
6746        &self,
6747        own: &[Resolved],
6748        id: &NativeId,
6749    ) -> Result<Option<Resolved>, SourceError> {
6750        match own.iter().find(|item| item.id == *id) {
6751            Some(item) => Ok(Some(item.clone())),
6752            None => self.item_by_id(id).await,
6753        }
6754    }
6755
6756    /// The candidates for a task query carrying a text, metadata or origin predicate, read
6757    /// without enumerating the board — or `None` for a query carrying none of the three, which
6758    /// keeps the reads it always had.
6759    ///
6760    /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
6761    /// because it names at most a handful of items. Text and metadata are answered by one
6762    /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
6763    /// further by `updated:>=` when the query also asks for comment activity, since both
6764    /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
6765    /// process afterwards by the same predicates [`task_matches`] applies to every read.
6766    ///
6767    /// Completed with what this process wrote, its own record winning over the index's copy
6768    /// of the same item: see [`Self::with_own_writes`].
6769    async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
6770        self.send_pending_origins(None).await?;
6771        let asked = match (&query.origin, narrowing_qualifiers(query)) {
6772            (Some(origin), _) => Narrowing::Origin(origin.clone()),
6773            (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
6774                Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
6775                None => qualifiers,
6776            }),
6777            (None, None) => return Ok(None),
6778        };
6779        // A question about comment activity is asked afresh every time, as it always was: it
6780        // is the one a caller polls from one source while waiting for the index, and an
6781        // answer held from the first poll would be the answer to every later one.
6782        let key = query.commented_since.is_none().then(|| asked.key());
6783        let cached = match &key {
6784            Some(key) => self.narrowed_cache()?.get(key).cloned(),
6785            None => None,
6786        };
6787        let found = match cached {
6788            Some(found) => found,
6789            None => {
6790                let found = match &asked {
6791                    Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
6792                    Narrowing::Search(also) => self.searched(also).await?,
6793                };
6794                if let Some(key) = key {
6795                    self.narrowed_cache()?.insert(key, found.clone());
6796                }
6797                found
6798            }
6799        };
6800        self.with_own_writes(found).map(Some)
6801    }
6802
6803    /// The candidates for a project or unscoped document query carrying a searchable text,
6804    /// read without enumerating the board — or `None` for a query with no text or a blank one,
6805    /// which keeps the read it always had.
6806    ///
6807    /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
6808    /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
6809    /// costs is the issues that match and never the board. Its answer is held for the command
6810    /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
6811    /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
6812    /// substring rule, exactly as an item of the wider read was, and is completed with what this
6813    /// process wrote: see [`Self::with_own_writes`].
6814    async fn text_searched(
6815        &self,
6816        text: Option<&TextQuery>,
6817    ) -> Result<Option<Vec<Resolved>>, SourceError> {
6818        let Some(also) = text_qualifiers(text) else {
6819            return Ok(None);
6820        };
6821        let key = Narrowing::Search(also.clone()).key();
6822        let cached = self.narrowed_cache()?.get(&key).cloned();
6823        let found = match cached {
6824            Some(found) => found,
6825            None => {
6826                let found = self.searched(&also).await?;
6827                self.narrowed_cache()?.insert(key, found.clone());
6828                found
6829            }
6830        };
6831        self.with_own_writes(found).map(Some)
6832    }
6833
6834    /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
6835    /// [`Self::narrowed`] reads for a task query carrying that origin — one
6836    /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
6837    /// what this process wrote — each confirmed against its own origin entry.
6838    ///
6839    /// What answers a project or document listing by origin, so finding every copy of one plan
6840    /// costs the items that carry its origin and never a page of the board.
6841    async fn copied_from(
6842        &self,
6843        origin: &str,
6844        kind: BoardKind,
6845    ) -> Result<Vec<Resolved>, SourceError> {
6846        let wanted = TaskQuery {
6847            origin: Some(origin.to_owned()),
6848            ..TaskQuery::default()
6849        };
6850        Ok(self
6851            .narrowed(&wanted)
6852            .await?
6853            .unwrap_or_default()
6854            .into_iter()
6855            .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
6856            .collect())
6857    }
6858
6859    /// Every item of this board that may carry `origin` — a superset of those that do — found
6860    /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
6861    ///
6862    /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
6863    /// which reads the field every carrier holds, whichever release wrote it — and the
6864    /// board-scoped issue search for the same id as a phrase in the body, where this source
6865    /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
6866    /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
6867    /// query's, exactly.
6868    ///
6869    /// Both connections are walked to exhaustion, each from its own cursor. One that has
6870    /// already ended is sent its last cursor again, which answers an empty page, so the one
6871    /// document serves every page of either. What the two leave is stated in the module
6872    /// documentation: a carrier another process added within the last second or two, before
6873    /// either index has it.
6874    async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
6875        let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
6876        let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
6877        let mut items_after: Option<String> = None;
6878        let mut search_after: Option<String> = None;
6879        let mut found: Vec<Resolved> = Vec::new();
6880        let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
6881            if !found.iter().any(|held| held.id == resolved.id) {
6882                found.push(resolved);
6883            }
6884        };
6885        loop {
6886            let data = self
6887                .graphql(
6888                    graphql::ORIGIN_LOOKUP,
6889                    json!({"owner":self.owner,"number":self.project_number,"filter":filter,
6890                           "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
6891                           "itemsAfter":items_after,"searchAfter":search_after,
6892                           "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6893                           "duplicates":true}),
6894                )
6895                .await?;
6896            let items = data
6897                .pointer("/originItems/projectV2/items")
6898                .filter(|value| !value.is_null())
6899                .ok_or_else(|| SourceError::Refused {
6900                    message: format!(
6901                        "GitHub project {}/{} was not found or is not visible to the token",
6902                        self.owner, self.project_number
6903                    ),
6904                })?;
6905            for item in optional_nodes(Some(items), "project items")?
6906                .into_iter()
6907                .flatten()
6908            {
6909                // The board's own items list its drafts too, and a draft is not an issue: no
6910                // narrowed read answers with one, whatever its origin field holds.
6911                if let Some(resolved) = self.resolve(item)?
6912                    && resolved.content_kind == ContentKind::Issue
6913                {
6914                    keep(resolved, &mut found);
6915                }
6916            }
6917            let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
6918                message: "GitHub search response has no search connection".into(),
6919            })?;
6920            for node in optional_nodes(Some(searched), "search")?
6921                .into_iter()
6922                .flatten()
6923            {
6924                if let Some(resolved) = self.resolve_issue(node).await? {
6925                    keep(resolved, &mut found);
6926                }
6927            }
6928            let items_next = resumed(items, items_after.as_deref())?;
6929            let search_next = resumed(searched, search_after.as_deref())?;
6930            if !items_next.has_more() && !search_next.has_more() {
6931                return Ok(found);
6932            }
6933            items_after = items_next.cursor();
6934            search_after = search_next.cursor();
6935        }
6936    }
6937
6938    /// `found`, with every item this process created or wrote in its place, and every one of
6939    /// them the read did not report added.
6940    ///
6941    /// This process's own record wins over the read's copy of the same item, because a read
6942    /// of an item written moments ago can still be behind what was written onto it — the
6943    /// origin field included, which is the one a narrowed read is confirmed against — and a
6944    /// read that still names an item under a predicate this process's write moved it out of
6945    /// must not return it. The one thing the read knows that the record cannot is when GitHub
6946    /// last saw the item change, which is what a comment-activity read rules a candidate out
6947    /// by, so the read's `updatedAt` is kept when the record has none of its own. See
6948    /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
6949    fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
6950        // A board draft is not an issue, so no narrowed read returns one, and this process
6951        // having written one does not make it an answer either.
6952        let own: Vec<Resolved> = self
6953            .created()?
6954            .iter()
6955            .chain(self.updated()?.iter())
6956            .filter(|own| own.content_kind == ContentKind::Issue)
6957            .cloned()
6958            .collect();
6959        for mut own in own {
6960            self.resolved_cache()?.insert(own.id.clone(), own.clone());
6961            match found.iter_mut().find(|read| read.id == own.id) {
6962                Some(read) => {
6963                    own.updated_at = own.updated_at.max(read.updated_at);
6964                    *read = own;
6965                }
6966                None => found.push(own),
6967            }
6968        }
6969        Ok(found)
6970    }
6971
6972    /// Whether `item` has a comment created or last edited at or after `since` — always, when
6973    /// there is no instant to hold it to.
6974    ///
6975    /// The candidate's own `updatedAt` is read first, because a comment written or edited at
6976    /// or after the instant moved it there: an issue not updated since holds no such comment,
6977    /// and its comments are never asked for — unless this process commented on it in this
6978    /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
6979    /// only as far as the first that matches. A board draft is not an issue and has no
6980    /// comments, so it never matches.
6981    async fn commented_since(
6982        &self,
6983        item: &Resolved,
6984        since: Option<DateTime<Utc>>,
6985    ) -> Result<bool, SourceError> {
6986        let Some(since) = since else {
6987            return Ok(true);
6988        };
6989        if item.content_kind == ContentKind::DraftIssue {
6990            return Ok(false);
6991        }
6992        // An `updatedAt` this process's own record or a lagging index holds can predate a
6993        // comment this process wrote since, so only an issue it did not comment on is ruled
6994        // out by one.
6995        if item.updated_at.is_some_and(|updated| updated < since)
6996            && !self.commented()?.contains(&item.id)
6997        {
6998            return Ok(false);
6999        }
7000        let query = TaskQuery {
7001            commented_since: Some(since),
7002            ..TaskQuery::default()
7003        };
7004        let mut after: Option<String> = None;
7005        loop {
7006            let data = self
7007                .graphql(
7008                    graphql::ISSUE_COMMENTS,
7009                    json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
7010                )
7011                .await?;
7012            let Some(connection) = data
7013                .get("node")
7014                .filter(|value| !value.is_null())
7015                .and_then(|node| node.get("comments"))
7016                .filter(|value| !value.is_null())
7017            else {
7018                // Removed since the search reported it: no longer an issue with comments.
7019                return Ok(false);
7020            };
7021            let comments = optional_nodes(Some(connection), "issue comments")?
7022                .into_iter()
7023                .flatten()
7024                .map(comment_from)
7025                .collect::<Result<Vec<_>, _>>()?;
7026            if query.comments_match(&comments) {
7027                return Ok(true);
7028            }
7029            match next_cursor(connection)? {
7030                Some(next) => {
7031                    validate_cursor_progress(after.as_deref(), &next.0)?;
7032                    after = Some(next.0);
7033                }
7034                None => return Ok(false),
7035            }
7036        }
7037    }
7038
7039    /// Every item on the board: the union of both enumerations GitHub offers of one.
7040    ///
7041    /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
7042    /// board **draft** and reads the board's own fields beside its items, and only the search
7043    /// reports an item that connection is behind on. The module documentation is where the lag and the
7044    /// measurements behind it are written down.
7045    ///
7046    /// A search result is admitted on the same terms as any other issue this source reaches
7047    /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
7048    /// names *this* board — so an issue the index still believes is here after it was taken
7049    /// off is refused rather than reported.
7050    ///
7051    /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
7052    /// which is what the cache could otherwise have broken.
7053    async fn board(&self) -> Result<Board, SourceError> {
7054        self.board_completed_by(None).await
7055    }
7056
7057    /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
7058    /// that half could add is then answered only if it holds.
7059    async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
7060        let cached = self.board_cache()?.clone();
7061        let mut board = match cached {
7062            Some(board) => board,
7063            None => {
7064                let read = self.read_board().await?;
7065                *self.board_cache()? = Some(read.clone());
7066                read
7067            }
7068        };
7069        let searched = match also {
7070            None => self.searched_issues().await?,
7071            Some(also) => self.searched_narrowly(also).await?,
7072        };
7073        for held in searched {
7074            if !board.items.iter().any(|item| item.id == held.id) {
7075                board.items.push(held);
7076            }
7077        }
7078        for own in self.created()?.iter() {
7079            if !board.items.iter().any(|item| item.id == own.id) {
7080                board.items.push(own.clone());
7081            }
7082        }
7083        Ok(board)
7084    }
7085
7086    /// This process's own view of the board, or the refusal a poisoned lock is.
7087    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
7088        self.board_cache
7089            .lock()
7090            .map_err(|_| SourceError::Unavailable {
7091                message: "this source's view of the board was left inconsistent by an earlier \
7092                      failure; next: run the command again"
7093                    .into(),
7094            })
7095    }
7096
7097    /// Bring this process's own view of the board up to an item it has just written.
7098    ///
7099    /// A created item goes to `created`, which is what completes a board read GitHub's own
7100    /// eventual consistency has left behind. An item that was already there is replaced
7101    /// where it sits, so a second write of it in the same command reads its real parent
7102    /// rather than the one it had before the first write.
7103    ///
7104    /// "Where it sits" is three places, and missing an earlier one leaves a stale record
7105    /// that wins: an item this same run created is held in `created` and not in the cached
7106    /// board, and `board` completes the cached board *from* `created`, so replacing only
7107    /// the cached copy of such an item replaces nothing and the read still reports the
7108    /// title it was created with. The search is the third, and it is the one an item the
7109    /// board's own projection is behind on sits in *alone* — which is exactly the item this
7110    /// source is least able to re-read, so leaving it out would put the stale title back on
7111    /// the only items the completion in [`Self::board`] exists for.
7112    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
7113        self.resolved_cache()?.insert(item.id.clone(), item.clone());
7114        if created {
7115            self.created()?.push(item);
7116            return Ok(());
7117        }
7118        {
7119            let mut own = self.created()?;
7120            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
7121                *held = item;
7122                return Ok(());
7123            }
7124        }
7125        {
7126            let mut own = self.updated()?;
7127            match own.iter_mut().find(|held| held.id == item.id) {
7128                Some(held) => *held = item.clone(),
7129                None => own.push(item.clone()),
7130            }
7131        }
7132        if let Some(board) = self.board_cache()?.as_mut()
7133            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
7134        {
7135            *held = item.clone();
7136        }
7137        if let Some(found) = self.search_cache()?.as_mut()
7138            && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
7139        {
7140            *held = item.clone();
7141        }
7142        for found in self.narrowed_cache()?.values_mut() {
7143            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7144                *held = item.clone();
7145            }
7146        }
7147        for (_, found) in self.children_cache()?.values_mut() {
7148            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
7149                *held = item.clone();
7150            }
7151        }
7152        Ok(())
7153    }
7154
7155    /// Forget one item this process has just deleted, from every half of its own view.
7156    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
7157        if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
7158            created.retain(|held| held.content != *id);
7159        }
7160        self.resolved_cache()?.remove(id);
7161        self.read_ahead()?.remove(id);
7162        self.carried()?.remove(id);
7163        self.created()?.retain(|own| own.id != *id);
7164        self.updated()?.retain(|own| own.id != *id);
7165        self.commented()?.retain(|own| own != id);
7166        if let Some(board) = self.board_cache()?.as_mut() {
7167            board.items.retain(|item| item.id != *id);
7168        }
7169        if let Some(found) = self.search_cache()?.as_mut() {
7170            found.retain(|item| item.id != *id);
7171        }
7172        for found in self.narrowed_cache()?.values_mut() {
7173            found.retain(|item| item.id != *id);
7174        }
7175        for (_, found) in self.children_cache()?.values_mut() {
7176            found.retain(|item| item.id != *id);
7177        }
7178        Ok(())
7179    }
7180
7181    /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
7182    fn narrowed_cache(
7183        &self,
7184    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
7185        self.narrowed_cache
7186            .lock()
7187            .map_err(|_| SourceError::Unavailable {
7188                message: "this source's view of a narrowed read was left inconsistent by an \
7189                      earlier failure; next: run the command again"
7190                    .into(),
7191            })
7192    }
7193
7194    /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
7195    /// is.
7196    /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
7197    fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
7198        Ok(self.task_children()?.get(project).cloned())
7199    }
7200
7201    fn task_children(
7202        &self,
7203    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
7204        self.task_children
7205            .lock()
7206            .map_err(|_| SourceError::Unavailable {
7207                message: "this source's record of a project's tasks was left inconsistent by an \
7208                          earlier failure; next: run the command again"
7209                    .into(),
7210            })
7211    }
7212
7213    fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
7214        self.children_cache
7215            .lock()
7216            .map_err(|_| SourceError::Unavailable {
7217                message: "this source's view of a project's tasks was left inconsistent by an \
7218                      earlier failure; next: run the command again"
7219                    .into(),
7220            })
7221    }
7222
7223    /// Every page of the board, read from GitHub.
7224    async fn read_board(&self) -> Result<Board, SourceError> {
7225        let mut after: Option<String> = None;
7226        let mut items = Vec::new();
7227        let mut board;
7228        loop {
7229            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
7230            for item in page
7231                .pointer("/items/nodes")
7232                .and_then(Value::as_array)
7233                .ok_or_else(|| SourceError::Malformed {
7234                    message: "GitHub project items.nodes is not an array".into(),
7235                })?
7236            {
7237                if let Some(resolved) = self.resolve(item)? {
7238                    items.push(resolved);
7239                }
7240            }
7241            let info = page
7242                .pointer("/items/pageInfo")
7243                .ok_or_else(|| SourceError::Malformed {
7244                    message: "GitHub project items have no pageInfo".into(),
7245                })?;
7246            let has_next = required_bool(info, "hasNextPage")?;
7247            let next = has_next
7248                .then(|| required_str(info, "endCursor"))
7249                .transpose()?;
7250            board = page.clone();
7251            match next {
7252                Some(next) => {
7253                    validate_cursor_progress(after.as_deref(), next)?;
7254                    after = Some(next.to_owned());
7255                }
7256                None => break,
7257            }
7258        }
7259        Ok(Board {
7260            id: required_str(&board, "id")?.to_owned(),
7261            fields: board.get("fields").cloned().unwrap_or(Value::Null),
7262            items,
7263        })
7264    }
7265
7266    /// The existing items this source has written, for completing a narrowed read that is
7267    /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
7268    fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7269        self.updated.lock().map_err(|_| SourceError::Unavailable {
7270            message: "this source's record of what it wrote in this run was left inconsistent \
7271                      by an earlier failure; next: run the command again"
7272                .into(),
7273        })
7274    }
7275
7276    /// The issues this source has commented on in this command; see
7277    /// [`Self::commented`](GitHubProjectsSource::commented).
7278    fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
7279        self.commented.lock().map_err(|_| SourceError::Unavailable {
7280            message: "this source's record of what it commented on in this run was left \
7281                      inconsistent by an earlier failure; next: run the command again"
7282                .into(),
7283        })
7284    }
7285
7286    /// Called only once GitHub has answered the comment write, so an issue whose comment
7287    /// failed is never made a candidate a later read would pay a node read for.
7288    fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
7289        let mut commented = self.commented()?;
7290        if !commented.contains(issue) {
7291            commented.push(issue.clone());
7292        }
7293        Ok(())
7294    }
7295
7296    /// The items this source has created, for completing a board read that is behind.
7297    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7298        self.created.lock().map_err(|_| SourceError::Unavailable {
7299            message: "this source's record of what it created in this run was left \
7300                      inconsistent by an earlier failure; next: run the command again"
7301                .into(),
7302        })
7303    }
7304
7305    /// One board item as this source reports it, or `None` for content it ignores.
7306    ///
7307    /// A pull request is neither a project nor a task — it is somebody's change, not a
7308    /// unit of plan — and an item whose content the token cannot see has nothing to
7309    /// report at all.
7310    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7311        let resolved = self.resolve_unremembered(item)?;
7312        if let Some(resolved) = &resolved {
7313            self.resolved_cache()?
7314                .insert(resolved.id.clone(), resolved.clone());
7315        }
7316        Ok(resolved)
7317    }
7318
7319    /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
7320    /// write's own response needs, which says nothing this process should answer a later read
7321    /// with.
7322    fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7323        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
7324            message: "GitHub project item is missing content".into(),
7325        })?;
7326        if content.is_null() {
7327            return Ok(None);
7328        }
7329        let content_kind = match required_str(content, "__typename")? {
7330            "Issue" => ContentKind::Issue,
7331            "DraftIssue" => ContentKind::DraftIssue,
7332            _ => return Ok(None),
7333        };
7334        let field_values = item
7335            .get("fieldValues")
7336            .ok_or_else(|| SourceError::Malformed {
7337                message: "GitHub project item is missing fieldValues".into(),
7338            })?;
7339        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
7340        let nodes = field_values
7341            .get("nodes")
7342            .and_then(Value::as_array)
7343            .ok_or_else(|| SourceError::Malformed {
7344                message: "GitHub project item fieldValues.nodes is not an array".into(),
7345            })?;
7346        if let Some(labels) = content.get("labels") {
7347            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
7348        }
7349        let raw_body = optional_str(content, "body")?.map(str::to_owned);
7350        let (body, slot) = metadata_body(raw_body.clone())?;
7351        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
7352            .map(|id| NativeId(id.to_owned()));
7353        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
7354        // to read one from; it is a task, and never a project.
7355        let sub_issues = match content_kind {
7356            ContentKind::Issue => sub_issue_total(content)?,
7357            ContentKind::DraftIssue => 0,
7358        };
7359        let content_id = required_str(content, "id")?;
7360        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7361            message: format!("GitHub issue {content_id}: {message}"),
7362        })?;
7363        let raw_title = required_str(content, "title")?;
7364        let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
7365        // The title a person wrote, which for a document is the one without the prefix —
7366        // the same way `content` above is the body without this source's metadata slot.
7367        let title = match kind {
7368            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
7369            BoardKind::Work(_) => raw_title.to_owned(),
7370        };
7371        let own_repository = content
7372            .pointer("/repository/nameWithOwner")
7373            .and_then(Value::as_str)
7374            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
7375            .transpose()
7376            .map_err(|message| SourceError::Malformed { message })?;
7377        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
7378            Repository::from_metadata(&slot)
7379                .map_err(|message| SourceError::Malformed { message })?
7380        } else {
7381            own_repository.clone().into_iter().collect()
7382        };
7383        let classification = Classification::from_metadata(&slot)
7384            .map_err(|message| SourceError::Malformed { message })?;
7385        let id = NativeId(content_id.to_owned());
7386        // Read only for a task, because only a task has either list: a project or a
7387        // document holding one of these keys holds nothing this source reports, and the
7388        // keys are left out of its caller-visible metadata all the same.
7389        let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
7390            let listed = |key: &str| {
7391                TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
7392                    .map_err(|message| SourceError::Malformed { message })
7393            };
7394            (
7395                listed(TaskRef::DELIVERS_KEY)?,
7396                listed(TaskRef::DELIVERED_BY_KEY)?,
7397            )
7398        } else {
7399            (Vec::new(), Vec::new())
7400        };
7401        let (option, closed, reason) = Self::status_parts(nodes, content)?;
7402        let priority = self.held_priority(nodes)?;
7403        let item_labels = labels(content)?;
7404        // Present when the item was reached through its own issue, whose board entry
7405        // names the board; a read of the board's own items has the board already. An
7406        // empty id names nothing a field write could address, so it is read as absent and
7407        // the write goes back to reading the board.
7408        let board_id = item
7409            .pointer("/project/id")
7410            .and_then(Value::as_str)
7411            .filter(|id| !id.is_empty());
7412        let resolved = Resolved {
7413            item_id: required_str(item, "id")?.to_owned(),
7414            id,
7415            content_kind,
7416            kind,
7417            title,
7418            body: body.filter(|value| !value.is_empty()),
7419            raw_body,
7420            status: self
7421                .statuses
7422                .status(kind.status_kind(), option, closed, reason),
7423            option: option.map(str::to_owned),
7424            priority,
7425            closed,
7426            reason: closed.then(|| reason.map(str::to_owned)).flatten(),
7427            delivers,
7428            delivered_by,
7429            labels: item_labels.clone(),
7430            parent,
7431            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
7432            projected: self.held_projections(nodes)?,
7433            number: match content_kind {
7434                ContentKind::Issue => Some(issue_number(content)?),
7435                // A draft is filed in no repository, so nothing ever numbered it:
7436                // `DraftIssue` declares no `number` at all, exactly as it declares no
7437                // `subIssuesSummary` the branch above reads.
7438                ContentKind::DraftIssue => None,
7439            },
7440            url: optional_str(content, "url")?.map(str::to_owned),
7441            created_at: optional_time(content, "createdAt")?,
7442            updated_at: optional_time(content, "updatedAt")?,
7443            item_updated_at: optional_time(item, "updatedAt")?,
7444            comment_count: content
7445                .pointer("/commentCount/totalCount")
7446                .and_then(Value::as_u64),
7447            relationship_events: content
7448                .pointer("/relationshipEvents/totalCount")
7449                .and_then(Value::as_u64),
7450            // The issue node is stamped wherever it was read; a board item a read of the issue
7451            // put together from its memberships is not.
7452            served_at: match optional_time(content, SERVED_AT)? {
7453                Some(served) => Some(served),
7454                None => optional_time(item, SERVED_AT)?,
7455            },
7456            // Both connections were read to their end above — one reporting a further page is
7457            // refused — so these are every field value and every label the item holds. A
7458            // response that did not select the labels at all says nothing of them, and gives
7459            // no facets.
7460            facets: content.get("labels").is_some().then(|| {
7461                change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
7462            }),
7463            own_repository,
7464            repositories,
7465            classification,
7466            slot,
7467            board_id: board_id.map(str::to_owned),
7468            fields: field_definitions(nodes),
7469            board_fields: Self::carried_board_fields(content, board_id)?,
7470            blocked_by: carried_blocked_by(content)?,
7471        };
7472        Ok(Some(resolved))
7473    }
7474
7475    /// The field definitions of the board `board_id` names — the project this issue's own
7476    /// board item is on — off the `boards` page a read of an issue by its own id carries, or
7477    /// `None` when the read carried none, carried no entry for that board, or the board item
7478    /// named no board, which a write then answers by reading the board's fields itself.
7479    ///
7480    /// Matched by the board's node id and never by its number alone: a project number is
7481    /// unique only within its owner, so another owner's board numbered alike can sit on the
7482    /// same page, and its field and option ids address nothing on this one.
7483    fn carried_board_fields(
7484        content: &Value,
7485        board_id: Option<&str>,
7486    ) -> Result<Option<Value>, SourceError> {
7487        let (Some(nodes), Some(board_id)) = (
7488            content.pointer("/boards/nodes").and_then(Value::as_array),
7489            board_id,
7490        ) else {
7491            return Ok(None);
7492        };
7493        let Some(board) = nodes.iter().find_map(|node| {
7494            let project = node.get("project")?;
7495            (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
7496        }) else {
7497            return Ok(None);
7498        };
7499        let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
7500            return Ok(None);
7501        };
7502        complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
7503        Ok(Some(fields.clone()))
7504    }
7505
7506    /// What one board item's `Priority` field says, through this instance's mapping.
7507    ///
7508    /// An instance with no mapping holds no priority, so every item reads as `none` whatever
7509    /// its board holds. With one, no value is `none`, a mapped option is its level, and an
7510    /// option the mapping does not name is kept as itself — never read as a level or as
7511    /// `none` — for a read of the task to report by name.
7512    fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
7513        let Some(mapping) = &self.priorities else {
7514            return Ok(HeldPriority::Read(Priority::None));
7515        };
7516        // A value of the field that names no option — a text field someone called `Priority` —
7517        // is malformed rather than `none`: reading it as no priority would let the next copy
7518        // clear one a person set.
7519        let Some(option) = field_values
7520            .iter()
7521            .find(|value| {
7522                value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
7523            })
7524            .map(|value| required_str(value, "name"))
7525            .transpose()?
7526        else {
7527            return Ok(HeldPriority::Read(Priority::None));
7528        };
7529        Ok(mapping.priority_of(option).map_or_else(
7530            || HeldPriority::Unmapped(option.to_owned()),
7531            HeldPriority::Read,
7532        ))
7533    }
7534
7535    /// What each projected metadata field holds for one board item, by field name — every text
7536    /// value GitHub answers, an empty one included.
7537    fn held_projections(
7538        &self,
7539        field_values: &[Value],
7540    ) -> Result<BTreeMap<String, String>, SourceError> {
7541        let mut held = BTreeMap::new();
7542        for projection in &self.metadata_fields {
7543            if let Some(text) = text_field(field_values, &projection.field)? {
7544                held.insert(projection.field.to_string(), text);
7545            }
7546        }
7547        Ok(held)
7548    }
7549
7550    /// What one board item's status is read from: its `Status` option, whether its issue
7551    /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
7552    /// three into the status it reports.
7553    fn status_parts<'a>(
7554        field_values: &'a [Value],
7555        content: &'a Value,
7556    ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
7557        let option = field_values
7558            .iter()
7559            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
7560            .map(|value| required_str(value, "name"))
7561            .transpose()?;
7562        let closed = optional_str(content, "state")? == Some("CLOSED");
7563        Ok((option, closed, optional_str(content, "stateReason")?))
7564    }
7565
7566    /// The board Status option this write selects, or the refusal that says why not.
7567    ///
7568    /// The mapped option is required for both open and terminal targets. A terminal write
7569    /// validates it before changing either representation, so it can never fall back to
7570    /// closing an issue whose board cannot display the matching status.
7571    ///
7572    /// Answers the field's id, the option's id, and the option's name as the board spells
7573    /// it — which is the name a read of the item reports once it sits there.
7574    fn column_for(
7575        &self,
7576        fields: &Value,
7577        kind: ItemKind,
7578        category: StatusCategory,
7579        target: &StatusTarget,
7580    ) -> Result<Option<(String, String, String)>, SourceError> {
7581        let Some(wanted) = target.option() else {
7582            return Ok(None);
7583        };
7584        let missing = |detail: &str| SourceError::Refused {
7585            message: format!(
7586                "{} status {} of source {} needs the board Status option {wanted:?}, and \
7587                 {detail}; next: add that option to the board, which `onetaskgraph sources \
7588                 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
7589                 it has",
7590                kind.marker(),
7591                category_name(category),
7592                self.name,
7593                self.name,
7594                category_name(category),
7595                kind.marker()
7596            ),
7597        };
7598        let Some(field) = Board::field(fields, "Status")? else {
7599            return Err(missing("this board has no Status field"));
7600        };
7601        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7602            return Err(missing(
7603                "this board's Status field is not a single-select field",
7604            ));
7605        }
7606        let option = field
7607            .get("options")
7608            .and_then(Value::as_array)
7609            .and_then(|options| {
7610                options.iter().find(|option| {
7611                    option
7612                        .get("name")
7613                        .and_then(Value::as_str)
7614                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7615                })
7616            });
7617        match option {
7618            None => Err(missing("this board does not have it")),
7619            Some(option) => Ok(Some((
7620                required_str(field, "id")?.to_owned(),
7621                required_str(option, "id")?.to_owned(),
7622                required_str(option, "name")?.to_owned(),
7623            ))),
7624        }
7625    }
7626
7627    /// The refusal a status that closes an issue is answered with over a board draft.
7628    fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
7629        SourceError::Refused {
7630            message: format!(
7631                "status {} of source {} closes the item's issue, and GitHub draft items have \
7632                 no open or closed state",
7633                category_name(category),
7634                self.name
7635            ),
7636        }
7637    }
7638
7639    /// What a status write to one item needs of the board: the board's id and the
7640    /// definition of its `Status` field, read off the item when the item says both.
7641    ///
7642    /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
7643    /// and its `Status` value carries that field's definition, options and all. An item that
7644    /// does not say — no board id, or no `Status` value to read the field off — takes them
7645    /// from [`Self::board_fields`], which reads no item.
7646    async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7647        if let Some(board) = item.carried_board() {
7648            return Ok(board);
7649        }
7650        if item.defines("Status")
7651            && let Some(board_id) = item.named_board()
7652        {
7653            return Ok(BoardFields {
7654                id: board_id,
7655                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7656            });
7657        }
7658        self.board_fields().await
7659    }
7660
7661    /// Set one task's or one project's status and nothing else; see
7662    /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
7663    async fn set_status(
7664        &self,
7665        id: &NativeId,
7666        kind: ItemKind,
7667        category: StatusCategory,
7668    ) -> Result<Option<Status>, SourceError> {
7669        // Refused before anything is read, in the words a write of the same status is.
7670        let target = self.resolved_target(kind, category)?;
7671        // An item read by issue reference carries only what this write needs, so what it
7672        // becomes is never remembered for a later read to be answered from.
7673        let held = self.take_held(id)?;
7674        let by_reference = held.is_some();
7675        let bound = match held {
7676            Some(item) => Some(item),
7677            None => self.bound_item(id).await?,
7678        };
7679        let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
7680            return Ok(None);
7681        };
7682        let board = self.status_board(&item).await?;
7683        let (field, option, name) = self
7684            .column_for(&board.fields, kind, category, &target)?
7685            .ok_or_else(|| SourceError::Malformed {
7686                message: format!(
7687                    "status {} of source {} names no board Status option",
7688                    category_name(category),
7689                    self.name
7690                ),
7691            })?;
7692        if item.status.category == category && item.option.as_deref() == Some(&name) {
7693            return Ok(Some(item.status));
7694        }
7695        match &target {
7696            StatusTarget::Terminal(_, reason) => {
7697                if item.content_kind == ContentKind::DraftIssue {
7698                    return Err(self.closes_a_draft(category));
7699                }
7700                // The option first, so a refused selection leaves an open issue open rather
7701                // than closed behind a card that still reads open.
7702                self.set_item_field(
7703                    board.id.as_str(),
7704                    &item.item_id,
7705                    &field,
7706                    json!({"singleSelectOptionId": option}),
7707                )
7708                .await?;
7709                // An issue already closed in a way that reads as this category keeps its state
7710                // and its reason: only its card was out of line, and closing it again would
7711                // overwrite a reason — a duplicate's — that says more than this write does.
7712                let already = item.closed && item.status.category == category;
7713                if !already {
7714                    self.update_content(
7715                        ContentKind::Issue,
7716                        &item.id,
7717                        json!({"stateInput": state_input(Some(&target))}),
7718                    )
7719                    .await?;
7720                }
7721                let reason = if already {
7722                    item.reason.clone()
7723                } else {
7724                    Some(reason.reason().to_owned())
7725                };
7726                item.closed = true;
7727                item.status = self
7728                    .statuses
7729                    .status(kind, Some(&name), true, reason.as_deref());
7730                item.option = Some(name);
7731                item.reason = reason;
7732            }
7733            StatusTarget::Column(_) => {
7734                // An option is what an open item's status is, so a closed issue is reopened
7735                // first — sitting closed in the column, it would read back as closed. A draft has
7736                // no state to reopen.
7737                if item.content_kind == ContentKind::Issue && item.closed {
7738                    self.update_content(
7739                        ContentKind::Issue,
7740                        &item.id,
7741                        json!({"stateInput": state_input(Some(&target))}),
7742                    )
7743                    .await?;
7744                    item.closed = false;
7745                }
7746                self.set_item_field(
7747                    board.id.as_str(),
7748                    &item.item_id,
7749                    &field,
7750                    json!({"singleSelectOptionId": option}),
7751                )
7752                .await?;
7753                item.status = self.statuses.status(kind, Some(&name), false, None);
7754                item.option = Some(name);
7755            }
7756            StatusTarget::Disabled(_) => {
7757                unreachable!("resolved_target refused a disabled status")
7758            }
7759        }
7760        let status = item.status.clone();
7761        if !by_reference {
7762            self.remember_written(item, false)?;
7763        }
7764        Ok(Some(status))
7765    }
7766
7767    /// Replace one task's `delivered_by` and nothing else; see
7768    /// [`TaskSource::set_delivered_by`].
7769    ///
7770    /// One update of the body, which differs from the body GitHub holds only inside the
7771    /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
7772    async fn replace_delivered_by(
7773        &self,
7774        id: &NativeId,
7775        delivered_by: &[TaskRef],
7776    ) -> Result<Option<()>, SourceError> {
7777        let entries = TaskRef::listed(
7778            TaskRef::DELIVERED_BY_KEY,
7779            id,
7780            Some(&self.name),
7781            delivered_by.to_vec(),
7782        )
7783        .map_err(|message| SourceError::Refused { message })?;
7784        let Some(mut item) = self
7785            .bound_item(id)
7786            .await?
7787            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7788        else {
7789            return Ok(None);
7790        };
7791        let mut slot = item.slot.clone();
7792        set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
7793        self.write_slot(&mut item, &slot).await?;
7794        item.delivered_by = entries;
7795        self.remember_written(item, false)?;
7796        Ok(Some(()))
7797    }
7798
7799    /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
7800    /// see [`TaskSource::set_task_metadata`].
7801    ///
7802    /// `None` when this board holds no item by that id, or holds one of another kind. The
7803    /// answer is the item as this source now reads it, so what a caller is told the key
7804    /// holds is what the slot holds.
7805    ///
7806    /// A key already holding the value is answered without a write, compared as JSON rather
7807    /// than as the body's bytes: a slot a person spelled with other whitespace would
7808    /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
7809    async fn set_slot_key(
7810        &self,
7811        id: &NativeId,
7812        kind: BoardKind,
7813        key: &MetadataKey,
7814        value: &Value,
7815    ) -> Result<Option<Resolved>, SourceError> {
7816        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7817            return Ok(None);
7818        };
7819        if item.slot.get(key.as_str()) == Some(value) {
7820            return Ok(Some(item));
7821        }
7822        let mut slot = item.slot.clone();
7823        slot.insert(key.as_str().to_owned(), value.clone());
7824        // A key a board text field projects moves that field first, so the body — the value's
7825        // one authoritative home — is written last, as every write of an existing item is.
7826        if self
7827            .metadata_fields
7828            .iter()
7829            .any(|projection| &*projection.key == key.as_str())
7830        {
7831            let board = self.projection_board(&item).await?;
7832            let (writes, projected) = self.projection_writes(
7833                &board.fields,
7834                Some(&item.projected),
7835                &slot,
7836                Some(&[key.as_str()]),
7837            )?;
7838            let (mut values, mut clears) = (Vec::new(), Vec::new());
7839            for write in &writes {
7840                write.join(&mut values, &mut clears);
7841            }
7842            self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
7843                .await?;
7844            item.projected = projected;
7845        }
7846        self.write_slot(&mut item, &slot).await?;
7847        self.remember_written(item.clone(), false)?;
7848        Ok(Some(item))
7849    }
7850
7851    /// What a projection write to `item` needs of the board, read off the item when it says
7852    /// enough, as [`Self::fields_for`] reads it.
7853    async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7854        if let Some(board) = item.carried_board() {
7855            return Ok(board);
7856        }
7857        if let Some(board_id) = item.named_board()
7858            && self
7859                .metadata_fields
7860                .iter()
7861                .all(|projection| item.defines(&projection.field))
7862        {
7863            return Ok(BoardFields {
7864                id: board_id,
7865                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7866            });
7867        }
7868        self.board_fields().await
7869    }
7870
7871    /// Put `slot` in one item's metadata slot with a single update of its body, and bring
7872    /// `item` up to what that write left.
7873    ///
7874    /// The body sent differs from the body GitHub holds only inside the slot — see
7875    /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
7876    /// the mutation the item's content takes, so a board draft's body is written with
7877    /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
7878    async fn write_slot(
7879        &self,
7880        item: &mut Resolved,
7881        slot: &BTreeMap<String, Value>,
7882    ) -> Result<(), SourceError> {
7883        let held = item.raw_body.clone().unwrap_or_default();
7884        let body = with_slot(&held, slot)?;
7885        if body != held {
7886            self.update_content(item.content_kind, &item.id, json!({"body": body}))
7887                .await?;
7888        }
7889        let (visible, slot) = metadata_body(Some(body.clone()))?;
7890        item.body = visible.filter(|value| !value.is_empty());
7891        item.raw_body = Some(body);
7892        item.slot = slot;
7893        Ok(())
7894    }
7895
7896    /// This instance's target for a category written to an item of `kind`, refusing one
7897    /// that kind has no option for — before anything is read or written.
7898    ///
7899    /// Nothing here mutates the board's option set to make room for a status. GitHub
7900    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
7901    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
7902    /// field and every item's status.
7903    fn resolved_target(
7904        &self,
7905        kind: ItemKind,
7906        category: StatusCategory,
7907    ) -> Result<StatusTarget, SourceError> {
7908        let target = self.statuses.target(kind, category).clone();
7909        let StatusTarget::Disabled(why) = target else {
7910            return Ok(target);
7911        };
7912        let refusal = why.refusal(&self.name, category, kind);
7913        // Why there is no shipped default, which is the question a person meeting this
7914        // refusal on a source that never mentioned the category asks.
7915        let shipped_none = match category {
7916            StatusCategory::Draft => Some(
7917                "draft has no shipped default because GitHub draft issues cannot have \
7918                 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
7919            ),
7920            StatusCategory::Unknown => Some(
7921                "unknown has no shipped default because this board keeps no open-ended status \
7922                 word: every word classified unknown is written to the one board Status option \
7923                 status_mapping.unknown names",
7924            ),
7925            _ => None,
7926        };
7927        Err(match (refusal, shipped_none, why) {
7928            (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
7929                SourceError::Refused {
7930                    message: format!("{message}; {note}"),
7931                }
7932            }
7933            (refusal, _, _) => refusal,
7934        })
7935    }
7936
7937    /// What writing `priority` does to one item's `Priority` field on this board, or the
7938    /// refusal naming what the board lacks.
7939    ///
7940    /// `none` is no value, so it clears the field — and asks nothing of an item that holds
7941    /// none already, or of an item not created yet. Every other priority selects the option
7942    /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
7943    /// without that option, is refused rather than given one: reads and writes never create
7944    /// a field or an option.
7945    fn priority_write(
7946        &self,
7947        fields: &Value,
7948        existing: Option<&Resolved>,
7949        priority: Priority,
7950    ) -> Result<Option<PriorityWrite>, SourceError> {
7951        let Some(mapping) = &self.priorities else {
7952            return Err(self.holds_no_priority());
7953        };
7954        let Some(wanted) = mapping.option(priority) else {
7955            if !existing.is_some_and(Resolved::holds_priority) {
7956                return Ok(None);
7957            }
7958            let field =
7959                Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
7960                    message: format!(
7961                        "an item holding a {PRIORITY_FIELD} value was read without that field"
7962                    ),
7963                })?;
7964            return Ok(Some(PriorityWrite::Clear {
7965                field: required_str(field, "id")?.to_owned(),
7966            }));
7967        };
7968        let missing = |detail: &str| SourceError::Refused {
7969            message: format!(
7970                "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
7971                 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
7972                 it, or point priority_mapping.{priority} of this source at an option the board \
7973                 has",
7974                self.name, self.name
7975            ),
7976        };
7977        let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
7978            return Err(missing(&format!(
7979                "this board has no {PRIORITY_FIELD} field"
7980            )));
7981        };
7982        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7983            return Err(missing(&format!(
7984                "this board's {PRIORITY_FIELD} field is not a single-select field"
7985            )));
7986        }
7987        // An options list that is absent or not a list is an answer this source cannot read,
7988        // not a board lacking the option: `sources fields --apply` is no remedy for it.
7989        let option = field
7990            .get("options")
7991            .and_then(Value::as_array)
7992            .ok_or_else(|| SourceError::Malformed {
7993                message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
7994            })?
7995            .iter()
7996            .find(|option| {
7997                option
7998                    .get("name")
7999                    .and_then(Value::as_str)
8000                    .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
8001            })
8002            .ok_or_else(|| missing("this board does not have it"))?;
8003        Ok(Some(PriorityWrite::Select {
8004            field: required_str(field, "id")?.to_owned(),
8005            option: required_str(option, "id")?.to_owned(),
8006        }))
8007    }
8008
8009    /// What writing `metadata` does to each projected text field of one item, and what those
8010    /// fields hold after it — or the refusal, before anything is sent.
8011    ///
8012    /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
8013    /// narrows the work to the entries projecting one of those metadata keys, for a write that
8014    /// touches only them; `None` is every entry. A string the field already holds, and nothing
8015    /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
8016    /// and so is a board without the field, or with a field of that name that is not a text
8017    /// field: reads and writes never create a field.
8018    fn projection_writes(
8019        &self,
8020        fields: &Value,
8021        held: Option<&BTreeMap<String, String>>,
8022        metadata: &BTreeMap<String, Value>,
8023        keys: Option<&[&str]>,
8024    ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
8025        let mut projected = held.cloned().unwrap_or_default();
8026        let mut writes = Vec::new();
8027        for projection in self
8028            .metadata_fields
8029            .iter()
8030            .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
8031        {
8032            let wanted = projection.projected(metadata, &self.name)?;
8033            let field = self.projection_field(fields, projection)?;
8034            let holding = projected.get(&*projection.field);
8035            match wanted {
8036                Projected::Text(text) if holding != Some(&text) => {
8037                    writes.push(ProjectionWrite::Set {
8038                        field,
8039                        text: text.clone(),
8040                    });
8041                    projected.insert(projection.field.to_string(), text);
8042                }
8043                Projected::Nothing if holding.is_some() => {
8044                    writes.push(ProjectionWrite::Clear { field });
8045                    projected.remove(&*projection.field);
8046                }
8047                Projected::Text(_) | Projected::Nothing => {}
8048            }
8049        }
8050        Ok((writes, projected))
8051    }
8052
8053    /// The id of the board text field one projection writes, or the refusal naming what the
8054    /// board has instead.
8055    fn projection_field(
8056        &self,
8057        fields: &Value,
8058        projection: &MetadataField,
8059    ) -> Result<String, SourceError> {
8060        let refuse = |detail: String| SourceError::Refused {
8061            message: format!(
8062                "source {} projects metadata key {:?} onto the board text field {:?}, and \
8063                 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
8064                 text field, or name another field in metadata_fields",
8065                self.name, &*projection.key, &*projection.field, self.name
8066            ),
8067        };
8068        let Some(field) = Board::field(fields, &projection.field)? else {
8069            return Err(refuse("this board has no field of that name".to_owned()));
8070        };
8071        let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
8072        let data_type = optional_str(field, "dataType")?;
8073        if typename != "ProjectV2Field" || data_type != Some("TEXT") {
8074            return Err(refuse(format!(
8075                "this board's field of that name is not a text field (it is a {}), so it is \
8076                 left as it is; rename or remove that field first",
8077                data_type.unwrap_or(typename)
8078            )));
8079        }
8080        Ok(required_str(field, "id")?.to_owned())
8081    }
8082
8083    /// Apply one priority write to one board item.
8084    async fn write_priority(
8085        &self,
8086        board_id: &str,
8087        item_id: &str,
8088        write: &PriorityWrite,
8089    ) -> Result<(), SourceError> {
8090        match write {
8091            PriorityWrite::Select { field, option } => {
8092                self.set_item_field(
8093                    board_id,
8094                    item_id,
8095                    field,
8096                    json!({"singleSelectOptionId": option}),
8097                )
8098                .await
8099            }
8100            PriorityWrite::Clear { field } => {
8101                let data = self
8102                    .graphql(
8103                        graphql::CLEAR_FIELD,
8104                        json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
8105                            "readPriority":false,"priorityName":PRIORITY_FIELD}),
8106                    )
8107                    .await?;
8108                let returned = data
8109                    .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
8110                    .ok_or_else(|| SourceError::Malformed {
8111                        message: "GitHub field clear returned no project item".into(),
8112                    })?;
8113                if required_str(returned, "id")? != item_id {
8114                    return Err(SourceError::Malformed {
8115                        message: "GitHub field clear returned the wrong project item".into(),
8116                    });
8117                }
8118                Ok(())
8119            }
8120        }
8121    }
8122
8123    /// The refusal a priority is answered with by an instance configured with no
8124    /// `priority_mapping`, which holds none.
8125    fn holds_no_priority(&self) -> SourceError {
8126        SourceError::Refused {
8127            message: format!(
8128                "source {} holds no task priority: its configuration sets no priority_mapping; \
8129                 next: set priority_mapping on this source, then run `onetaskgraph sources \
8130                 fields {} --apply` to set its board up",
8131                self.name, self.name
8132            ),
8133        }
8134    }
8135
8136    /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
8137    ///
8138    /// One field write — a select, or a clear for `none` — and no title, body, label, state
8139    /// or `Status` request. Clearing a priority an item does not hold sends nothing.
8140    async fn set_priority(
8141        &self,
8142        id: &NativeId,
8143        priority: Priority,
8144    ) -> Result<Option<Priority>, SourceError> {
8145        if self.priorities.is_none() {
8146            return Err(self.holds_no_priority());
8147        }
8148        let Some(mut item) = self
8149            .bound_item(id)
8150            .await?
8151            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8152        else {
8153            return Ok(None);
8154        };
8155        if priority == Priority::None && !item.holds_priority() {
8156            return Ok(Some(priority));
8157        }
8158        // The item's own read carries the field's definition whenever it holds a value of
8159        // it, which a clear always does; a select onto an item holding none reads the board.
8160        let board = match (item.carried_board(), item.named_board()) {
8161            (Some(board), _) => board,
8162            (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
8163                id,
8164                fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
8165            },
8166            _ => self.board_fields().await?,
8167        };
8168        let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
8169            return Ok(Some(priority));
8170        };
8171        let (document, root, input) = match write {
8172            PriorityWrite::Select { field, option } => (
8173                graphql::UPDATE_FIELD,
8174                "updateProjectV2ItemFieldValue",
8175                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
8176            ),
8177            PriorityWrite::Clear { field } => (
8178                graphql::CLEAR_FIELD,
8179                "clearProjectV2ItemFieldValue",
8180                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
8181            ),
8182        };
8183        let data = self
8184            .graphql(
8185                document,
8186                json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
8187            )
8188            .await?;
8189        let returned = data
8190            .get(root)
8191            .and_then(|value| value.get("projectV2Item"))
8192            .ok_or_else(|| SourceError::Malformed {
8193                message: "GitHub priority write returned no project item".into(),
8194            })?;
8195        if required_str(returned, "id")? != item.item_id {
8196            return Err(SourceError::Malformed {
8197                message: "GitHub priority write returned the wrong project item".into(),
8198            });
8199        }
8200        let value = returned
8201            .get("fieldValueByName")
8202            .ok_or_else(|| SourceError::Malformed {
8203                message: "GitHub priority write returned no priority read-back".into(),
8204            })?;
8205        if !value.is_null()
8206            && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
8207        {
8208            return Err(SourceError::Malformed {
8209                message: "GitHub priority read-back is not a Priority field value".into(),
8210            });
8211        }
8212        let values = if value.is_null() {
8213            Vec::new()
8214        } else {
8215            vec![value.clone()]
8216        };
8217        item.priority = self.held_priority(&values)?;
8218        let answer = item.task()?.priority;
8219        self.remember_written(item, false)?;
8220        Ok(Some(answer))
8221    }
8222
8223    /// Replace one task's visible body and nothing else; see
8224    /// [`TaskSource::set_task_content`].
8225    ///
8226    /// One update of the body, which differs from the body GitHub holds only outside the
8227    /// metadata slot — the slot is kept byte for byte, so every caller key and every list
8228    /// this source keeps there reads back as it was. A body that would not change is not
8229    /// sent at all.
8230    async fn replace_content(
8231        &self,
8232        id: &NativeId,
8233        content: &str,
8234    ) -> Result<Option<()>, SourceError> {
8235        let Some(mut item) = self
8236            .bound_item(id)
8237            .await?
8238            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8239        else {
8240            return Ok(None);
8241        };
8242        let held = item.raw_body.clone().unwrap_or_default();
8243        let body = with_content(&held, content)?;
8244        // Checked before anything is sent: content ending in what this source reads as its own
8245        // metadata slot would read back as metadata rather than as the content it was.
8246        let (visible, slot) = metadata_body(Some(body.clone()))?;
8247        if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
8248            return Err(SourceError::Refused {
8249                message: format!(
8250                    "this content ends in what source {} reads as its own metadata slot \
8251                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8252                     as content; next: remove that trailing block from the content",
8253                    self.name
8254                ),
8255            });
8256        }
8257        if body != held {
8258            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8259                .await?;
8260        }
8261        item.body = visible.filter(|value| !value.is_empty());
8262        item.raw_body = Some(body);
8263        item.slot = slot;
8264        self.remember_written(item, false)?;
8265        Ok(Some(()))
8266    }
8267
8268    /// Apply one targeted update to one task; see [`TaskSource::update_task`].
8269    ///
8270    /// One read of the item — which carries the board's field definitions and the issue's
8271    /// `blockedBy`, so neither is read again — and then only what differs from it: the
8272    /// `Status` option and the `Priority` field together in one request, the `blockedBy`
8273    /// additions and removals the named edges differ by, and last one `updateIssue` carrying
8274    /// the title, the body — visible content and metadata slot together — and a state change.
8275    /// So an update naming any of title, body, metadata, status and priority is one read and
8276    /// at most two writes. The body goes last so that a write refused part-way leaves it, and
8277    /// the metadata in it, as it stood. A terminal status selects its option and then closes,
8278    /// as a whole write does; an open one selects its option and then reopens. The origin
8279    /// field is never written: an update is of an item that already exists, whose origin is
8280    /// what it is.
8281    ///
8282    /// The task answered is the item as those writes left it, built from the read and what was
8283    /// sent rather than read again — the same record a later read in this run answers from.
8284    async fn targeted_update(
8285        &self,
8286        id: &NativeId,
8287        update: &TaskUpdate,
8288    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
8289        // Everything this source can refuse without reading the item is refused first, in the
8290        // words a whole write of the same fields is refused with.
8291        update.consistent()?;
8292        if update
8293            .title
8294            .as_deref()
8295            .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
8296        {
8297            return Err(SourceError::Refused {
8298                message: format!(
8299                    "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8300                     spells a document, so it would read back as one rather than as a task; \
8301                     retitle it",
8302                    self.name
8303                ),
8304            });
8305        }
8306        if let Some(delivers) = &update.delivers {
8307            TaskRef::listed(
8308                TaskRef::DELIVERS_KEY,
8309                id,
8310                Some(&self.name),
8311                delivers.clone(),
8312            )
8313            .map_err(|message| SourceError::Refused { message })?;
8314        }
8315        if self.priorities.is_none()
8316            && update
8317                .priority
8318                .is_some_and(|priority| priority != Priority::None)
8319        {
8320            return Err(self.holds_no_priority());
8321        }
8322        let target = update
8323            .status
8324            .as_ref()
8325            .map(|status| self.resolved_target(ItemKind::Task, status.category))
8326            .transpose()?;
8327        let Some(mut item) = self
8328            .bound_item(id)
8329            .await?
8330            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8331        else {
8332            return Ok(None);
8333        };
8334        let before = item.task()?;
8335
8336        let mut status_move = None;
8337        if let (Some(status), Some(target)) = (&update.status, target) {
8338            let board = self.status_board(&item).await?;
8339            let (field, option, name) = self
8340                .column_for(&board.fields, ItemKind::Task, status.category, &target)?
8341                .ok_or_else(|| SourceError::Malformed {
8342                    message: format!(
8343                        "status {} of source {} names no board Status option",
8344                        category_name(status.category),
8345                        self.name
8346                    ),
8347                })?;
8348            let terminal = matches!(target, StatusTarget::Terminal(_, _));
8349            if terminal && item.content_kind == ContentKind::DraftIssue {
8350                return Err(self.closes_a_draft(status.category));
8351            }
8352            let landed = match &target {
8353                StatusTarget::Terminal(_, reason) => {
8354                    self.statuses
8355                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
8356                }
8357                _ => self
8358                    .statuses
8359                    .status(ItemKind::Task, Some(&name), false, None),
8360            };
8361            let option_moves = item
8362                .option
8363                .as_deref()
8364                .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
8365            let state_moves = item.content_kind == ContentKind::Issue
8366                && (item.closed != terminal || (terminal && item.status != landed));
8367            if let Some(moves) = Moves::of(option_moves, state_moves) {
8368                status_move = Some(StatusMove {
8369                    board: board.id,
8370                    field,
8371                    option,
8372                    name,
8373                    target,
8374                    landed,
8375                    moves,
8376                });
8377            }
8378        }
8379
8380        let mut priority_move = None;
8381        if let Some(priority) = update.priority
8382            && self.priorities.is_some()
8383            && item.priority != HeldPriority::Read(priority)
8384        {
8385            let board = match (item.carried_board(), item.named_board()) {
8386                (Some(board), _) => board,
8387                (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
8388                    id: board,
8389                    fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8390                },
8391                _ => self.board_fields().await?,
8392            };
8393            if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
8394                priority_move = Some((board.id, write, priority));
8395            }
8396        }
8397
8398        // The metadata keys this update names that a board text field projects, and what
8399        // that does to those fields — refused, as a whole write refuses it, before anything
8400        // is sent.
8401        let touched: Vec<&str> = update
8402            .metadata_set
8403            .keys()
8404            .chain(update.metadata_remove.iter())
8405            .map(MetadataKey::as_str)
8406            .collect();
8407        let mut projection_move = None;
8408        if self
8409            .metadata_fields
8410            .iter()
8411            .any(|projection| touched.contains(&&*projection.key))
8412        {
8413            let mut metadata = item.slot.clone();
8414            for (key, value) in &update.metadata_set {
8415                metadata.insert(key.as_str().to_owned(), value.clone());
8416            }
8417            for key in &update.metadata_remove {
8418                metadata.remove(key.as_str());
8419            }
8420            let board = self.projection_board(&item).await?;
8421            let (writes, projected) = self.projection_writes(
8422                &board.fields,
8423                Some(&item.projected),
8424                &metadata,
8425                Some(&touched),
8426            )?;
8427            projection_move = Some((board.id, writes, projected));
8428        }
8429
8430        // Resolved before the body is composed, because a far end `blockedBy` cannot name is
8431        // recorded in the slot, and the slot travels in the one body update below.
8432        let edges = match &update.depends_on {
8433            Some(edges) => Some(
8434                self.partition_edges(
8435                    BoardKind::Work(ItemKind::Task),
8436                    item.content_kind,
8437                    item.blocked_by.as_deref(),
8438                    edges,
8439                )
8440                .await?,
8441            ),
8442            None => None,
8443        };
8444
8445        let mut slot = item.slot.clone();
8446        for (key, value) in &update.metadata_set {
8447            slot.insert(key.as_str().to_owned(), value.clone());
8448        }
8449        for key in &update.metadata_remove {
8450            slot.remove(key.as_str());
8451        }
8452        if let Some(delivers) = &update.delivers {
8453            set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
8454        }
8455        if let Some((_, recorded)) = &edges {
8456            record_edges(&mut slot, recorded);
8457        }
8458        let held = item.raw_body.clone().unwrap_or_default();
8459        let content = match &update.content {
8460            Some(content) => with_content(&held, content)?,
8461            None => held.clone(),
8462        };
8463        // A slot holding what it held is kept byte for byte, compared as JSON rather than as
8464        // the body's bytes, as a metadata write compares it: a slot a person spelled with
8465        // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
8466        let body = if slot == item.slot {
8467            content
8468        } else {
8469            with_slot(&content, &slot)?
8470        };
8471        // Checked before anything is sent, as a content write checks it: content ending in
8472        // what this source reads as its own slot would read back as metadata.
8473        let (visible, read) = metadata_body(Some(body.clone()))?;
8474        let wanted = update.content.as_deref().or(item.body.as_deref());
8475        if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
8476            return Err(SourceError::Refused {
8477                message: format!(
8478                    "this content ends in what source {} reads as its own metadata slot \
8479                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8480                     as content; next: remove that trailing block from the content",
8481                    self.name
8482                ),
8483            });
8484        }
8485        let recorded_moves =
8486            slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
8487
8488        // One `updateIssue` carries all three, because every mutation spends the secondary
8489        // limiter and the title, body and state are one mutation's inputs.
8490        let mut fields = serde_json::Map::new();
8491        if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
8492            fields.insert("title".to_owned(), json!(title));
8493        }
8494        if body != held {
8495            fields.insert("body".to_owned(), json!(body));
8496        }
8497        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
8498            fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
8499        }
8500        // **The body is written last, and that is the guarantee a refusal part-way keeps.**
8501        // GitHub runs no two requests as one, and runs one document's mutation fields in order
8502        // without undoing an earlier field when a later one fails — so a body written before a
8503        // board field the board then refused would be left changed. Written after every other
8504        // write has landed, a refusal anywhere leaves the item's body, and every metadata key
8505        // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
8506        // first, together in one request — a terminal option selected before the issue
8507        // closes, as a whole write does — then the `blockedBy` difference, then the body.
8508        let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
8509        let mut clears: Vec<(&BoardId, &str)> = Vec::new();
8510        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
8511            board_writes.push((
8512                &moving.board,
8513                (
8514                    moving.field.clone(),
8515                    json!({"singleSelectOptionId": moving.option}),
8516                ),
8517            ));
8518        }
8519        match &priority_move {
8520            Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
8521                board,
8522                (field.clone(), json!({"singleSelectOptionId": option})),
8523            )),
8524            Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
8525            None => {}
8526        }
8527        if let Some((board, writes, _)) = &projection_move {
8528            for write in writes {
8529                let (mut values, mut cleared) = (Vec::new(), Vec::new());
8530                write.join(&mut values, &mut cleared);
8531                board_writes.extend(values.into_iter().map(|value| (board, value)));
8532                clears.extend(cleared.into_iter().map(|field| (board, field)));
8533            }
8534        }
8535        let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
8536        boards.extend(clears.iter().map(|(board, _)| *board));
8537        boards.dedup_by(|one, other| one.as_str() == other.as_str());
8538        for board in boards {
8539            let writes = board_writes
8540                .iter()
8541                .filter(|(on, _)| on.as_str() == board.as_str())
8542                .map(|(_, write)| write.clone())
8543                .collect::<Vec<_>>();
8544            let cleared = clears
8545                .iter()
8546                .filter(|(on, _)| on.as_str() == board.as_str())
8547                .map(|(_, field)| *field)
8548                .collect::<Vec<_>>();
8549            self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
8550                .await?;
8551        }
8552        let mut blocked_by_moved = false;
8553        if let Some((native, _)) = &edges
8554            && item.content_kind == ContentKind::Issue
8555        {
8556            blocked_by_moved = self
8557                .reconcile_blocked_by(
8558                    &item.id,
8559                    native,
8560                    Issue::Existing(item.blocked_by.as_deref()),
8561                )
8562                .await?;
8563        }
8564        if !fields.is_empty() {
8565            self.update_content(item.content_kind, &item.id, Value::Object(fields))
8566                .await?;
8567        }
8568
8569        // The `blockedBy` this item was read with is what the write just changed, so it is no
8570        // answer any more: a later write in this command would reconcile against it — re-adding
8571        // what it kept and never removing what it dropped — and a forward-edge read would
8572        // answer with it. Left unknown, each asks GitHub instead.
8573        if blocked_by_moved {
8574            item.blocked_by = None;
8575        }
8576        if let Some(title) = &update.title {
8577            item.title.clone_from(title);
8578        }
8579        item.body = visible.filter(|value| !value.is_empty());
8580        item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
8581        item.slot = slot;
8582        if let Some(delivers) = &update.delivers {
8583            item.delivers.clone_from(delivers);
8584        }
8585        if let Some(moving) = status_move {
8586            item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
8587                && item.content_kind == ContentKind::Issue;
8588            item.status = moving.landed;
8589            item.option = Some(moving.name);
8590        }
8591        if let Some((_, _, priority)) = priority_move {
8592            item.priority = HeldPriority::Read(priority);
8593        }
8594        if let Some((_, _, projected)) = projection_move {
8595            item.projected = projected;
8596        }
8597        let task = item.task()?;
8598        let mut written = update.changed(&before, &task);
8599        if blocked_by_moved || recorded_moves {
8600            written.insert(UpdatedField::DependsOn);
8601        }
8602        self.remember_written(item, false)?;
8603        Ok(Some(TaskUpdateOutcome {
8604            task,
8605            written,
8606            delivers_before: before.delivers,
8607        }))
8608    }
8609
8610    /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
8611    /// together, and nothing else; see [`TaskSource::set_task_rendering`].
8612    ///
8613    /// One update of the body: the content outside the slot, and inside it that one entry,
8614    /// every other entry kept as it was. This source keeps no template answers — an issue has
8615    /// no room beside itself that is not its body, and answers written there would duplicate
8616    /// what the content already says and count against GitHub's body limit — so `answers`
8617    /// reaches nothing here. A body that would not change is not sent at all.
8618    async fn replace_rendering(
8619        &self,
8620        id: &NativeId,
8621        kind: BoardKind,
8622        content: &str,
8623        provenance: &Value,
8624        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
8625    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
8626        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8627            return Ok(None);
8628        };
8629        let held = item.raw_body.clone().unwrap_or_default();
8630        let mut slot = item.slot.clone();
8631        slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
8632        let rewritten;
8633        let content = if let Some(assets) = assets {
8634            let uploads = self
8635                .upload_assets(item.own_repository.as_ref(), assets)
8636                .await?;
8637            rewritten =
8638                onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
8639            rewritten.as_str()
8640        } else {
8641            content
8642        };
8643        let body = with_slot(&with_content(&held, content)?, &slot)?;
8644        // Checked before anything is sent, as a content write checks it.
8645        let (visible, read) = metadata_body(Some(body.clone()))?;
8646        if visible.as_deref().unwrap_or_default() != content || read != slot {
8647            return Err(SourceError::Refused {
8648                message: format!(
8649                    "this content ends in what source {} reads as its own metadata slot \
8650                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8651                     as content; next: remove that trailing block from the template",
8652                    self.name
8653                ),
8654            });
8655        }
8656        if body != held {
8657            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8658                .await?;
8659        }
8660        item.body = visible.filter(|value| !value.is_empty());
8661        item.raw_body = Some(body);
8662        item.slot = read;
8663        self.remember_written(item, false)?;
8664        Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
8665            id: id.clone(),
8666            content: Some(content.to_owned()),
8667        }))
8668    }
8669
8670    async fn set_item_field(
8671        &self,
8672        board_id: &str,
8673        item_id: &str,
8674        field_id: &str,
8675        value: Value,
8676    ) -> Result<(), SourceError> {
8677        let data = self
8678            .graphql(
8679                graphql::UPDATE_FIELD,
8680                json!({"input":{
8681                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
8682                },"readPriority":false,"priorityName":PRIORITY_FIELD}),
8683            )
8684            .await?;
8685        let returned = data
8686            .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
8687            .ok_or_else(|| SourceError::Malformed {
8688                message: "GitHub field update returned no project item".into(),
8689            })?;
8690        if required_str(returned, "id")? != item_id {
8691            return Err(SourceError::Malformed {
8692                message: "GitHub field update returned the wrong project item".into(),
8693            });
8694        }
8695        Ok(())
8696    }
8697
8698    /// GitHub accepts one value per field mutation; aliases combine those mutations in
8699    /// one request — writes in the order given, then clears. A lone write is
8700    /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
8701    /// request's slots hold go in further requests of the same document, in order. Every
8702    /// returned item id is checked, including optional aliases.
8703    async fn set_item_fields(
8704        &self,
8705        board: &str,
8706        item: &str,
8707        fields: &[(String, Value)],
8708        clears: &[&str],
8709    ) -> Result<(), SourceError> {
8710        match (fields, clears) {
8711            ([], []) => return Ok(()),
8712            ([(field, value)], []) => {
8713                return self.set_item_field(board, item, field, value.clone()).await;
8714            }
8715            ([], [field]) => {
8716                return self
8717                    .write_priority(
8718                        board,
8719                        item,
8720                        &PriorityWrite::Clear {
8721                            field: (*field).to_owned(),
8722                        },
8723                    )
8724                    .await;
8725            }
8726            _ => {}
8727        }
8728        // Any field of the request stands in for an input a slot leaves out, which GitHub
8729        // still requires to be well formed although it never runs it.
8730        let filler = fields
8731            .first()
8732            .map_or_else(|| clears[0], |(field, _)| field.as_str());
8733        let mut writes = fields.iter();
8734        let mut cleared = clears.iter();
8735        loop {
8736            let batch: Vec<&(String, Value)> =
8737                writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
8738            let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
8739            if batch.is_empty() && batch_clears.is_empty() {
8740                return Ok(());
8741            }
8742            let mut variables = serde_json::Map::new();
8743            for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
8744                let input = match batch.get(index) {
8745                    Some((field, value)) => {
8746                        json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
8747                    }
8748                    None => json!({"projectId":board,"itemId":item,"fieldId":filler,
8749                                   "value":{"text":""}}),
8750                };
8751                variables.insert(slot.variable.to_owned(), input);
8752                variables.insert(slot.include.to_owned(), json!(index < batch.len()));
8753            }
8754            for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
8755                let field = batch_clears.get(index).map_or(filler, |field| **field);
8756                variables.insert(
8757                    slot.variable.to_owned(),
8758                    json!({"projectId":board,"itemId":item,"fieldId":field}),
8759                );
8760                variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
8761            }
8762            let data = self
8763                .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
8764                .await?;
8765            let sent = FIELD_WRITE_SLOTS[..batch.len()]
8766                .iter()
8767                .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
8768            for slot in sent {
8769                let alias = slot.alias;
8770                let returned = data
8771                    .get(alias)
8772                    .and_then(|value| value.get("projectV2Item"))
8773                    .ok_or_else(|| SourceError::Malformed {
8774                        message: format!("GitHub field update {alias} returned no project item"),
8775                    })?;
8776                if required_str(returned, "id")? != item {
8777                    return Err(SourceError::Malformed {
8778                        message: format!(
8779                            "GitHub field update {alias} returned the wrong project item"
8780                        ),
8781                    });
8782                }
8783            }
8784        }
8785    }
8786
8787    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
8788        let mut after: Option<String> = None;
8789        let mut ids = Vec::new();
8790        loop {
8791            let data = self
8792                .graphql(
8793                    graphql::ISSUE_DEPENDENCIES,
8794                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
8795                )
8796                .await?;
8797            let connection =
8798                data.pointer("/node/blockedBy")
8799                    .ok_or_else(|| SourceError::Malformed {
8800                        message: "GitHub dependency response has no blockedBy connection".into(),
8801                    })?;
8802            ids.extend(
8803                connection
8804                    .get("nodes")
8805                    .and_then(Value::as_array)
8806                    .ok_or_else(|| SourceError::Malformed {
8807                        message: "GitHub dependency response nodes is not an array".into(),
8808                    })?
8809                    .iter()
8810                    .map(|value| required_str(value, "id").map(str::to_owned))
8811                    .collect::<Result<Vec<_>, _>>()?,
8812            );
8813            let next = next_cursor(connection)?;
8814            if let Some(next) = &next {
8815                validate_cursor_progress(after.as_deref(), &next.0)?;
8816            }
8817            after = next.map(|cursor| cursor.0);
8818            if after.is_none() {
8819                return Ok(ids);
8820            }
8821        }
8822    }
8823
8824    async fn dependencies(
8825        &self,
8826        id: &NativeId,
8827        near_kind: ItemKind,
8828        direction: Direction,
8829        page: &PageRequest,
8830    ) -> Result<Page<DependencyEdge>, SourceError> {
8831        validate_page(page)?;
8832        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
8833        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
8834        let recorded = recorded_offset(cursor, direction)?;
8835        // What this issue is blocked by, when a read of it by its own id in this command
8836        // already carried the whole connection — a copy reads the item it writes before it
8837        // reads its edges — and the page asked for is the whole of it, or the recorded tail
8838        // after it. Answered from that read, in the shape the dependency read answers in;
8839        // anything else is asked of GitHub.
8840        let carried = match direction {
8841            Direction::DependsOn => self
8842                .resolved_cache()?
8843                .get(id)
8844                .filter(|item| item.content_kind == ContentKind::Issue)
8845                .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
8846            Direction::DependedOnBy => None,
8847        }
8848        .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
8849        // Asked for even in the recorded phase, whose page reads nothing from the
8850        // connection: `__typename` is what says whether this item has a native
8851        // relationship at all, and that is what decides which far ends the reserved key is
8852        // allowed to hold.
8853        let data = match carried {
8854            Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
8855                "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
8856            None => {
8857                let after = if recorded.is_some() { None } else { cursor };
8858                let first = page.limit.min(MAX_PAGE_SIZE);
8859                match direction {
8860                    Direction::DependsOn => {
8861                        self.graphql(
8862                            graphql::ISSUE_DEPENDS_ON,
8863                            json!({"id":id.0,"first":first,"after":after,"owner":self.owner,
8864                                   "number":self.project_number,
8865                                   "boardItems":BOARD_ITEMS_PAGE_SIZE}),
8866                        )
8867                        .await?
8868                    }
8869                    Direction::DependedOnBy => {
8870                        self.graphql(
8871                            graphql::ISSUE_DEPENDED_ON_BY,
8872                            json!({"id":id.0,"first":first,"after":after}),
8873                        )
8874                        .await?
8875                    }
8876                }
8877            }
8878        };
8879        let draft_slot = draft_slot(&data, id, self.project_number)?;
8880        let node =
8881            data.get("node")
8882                .filter(|v| !v.is_null())
8883                .ok_or_else(|| SourceError::Refused {
8884                    message: format!(
8885                        "GitHub item {} was not found or does not support dependencies",
8886                        id.0
8887                    ),
8888                })?;
8889        let connection_name = match direction {
8890            Direction::DependsOn => "blockedBy",
8891            Direction::DependedOnBy => "blocking",
8892        };
8893        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
8894        // named natively and the reserved key may hold any far end. An issue's connections
8895        // hold issues, and this source reads them at the near item's own level.
8896        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
8897        if let Some(offset) = recorded {
8898            return Ok(recorded_page(
8899                self.recorded_edges(
8900                    id,
8901                    near_kind,
8902                    direction,
8903                    natively_names,
8904                    node,
8905                    draft_slot.as_ref(),
8906                )
8907                .await?,
8908                offset,
8909                limit,
8910            ));
8911        }
8912        if natively_names.is_none() {
8913            return Ok(recorded_page(
8914                self.recorded_edges(
8915                    id,
8916                    near_kind,
8917                    direction,
8918                    natively_names,
8919                    node,
8920                    draft_slot.as_ref(),
8921                )
8922                .await?,
8923                0,
8924                limit,
8925            ));
8926        }
8927        let connection = node
8928            .get(connection_name)
8929            .ok_or_else(|| SourceError::Malformed {
8930                message: "GitHub dependency response is missing its connection".into(),
8931            })?;
8932        let nodes = connection
8933            .get("nodes")
8934            .and_then(Value::as_array)
8935            .ok_or_else(|| SourceError::Malformed {
8936                message: "GitHub dependency response nodes is not an array".into(),
8937            })?;
8938        // `from` depends on `to`, always. GitHub spells the same relationship from either
8939        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
8940        // it — so the near item is `from` in one direction and `to` in the other.
8941        let items = nodes
8942            .iter()
8943            .map(|value| {
8944                let related = NativeId(required_str(value, "id")?.into());
8945                let related_kind = related_kind(value)?;
8946                let (from, to) = match direction {
8947                    Direction::DependsOn => (
8948                        DependencyEndpoint::from_native(id.clone(), near_kind),
8949                        DependencyEndpoint::from_native(related, related_kind),
8950                    ),
8951                    Direction::DependedOnBy => (
8952                        DependencyEndpoint::from_native(related, related_kind),
8953                        DependencyEndpoint::from_native(id.clone(), near_kind),
8954                    ),
8955                };
8956                Ok(DependencyEdge {
8957                    from,
8958                    to,
8959                    kind: DependencyKind::Blocks,
8960                })
8961            })
8962            .collect::<Result<Vec<_>, SourceError>>()?;
8963        let mut next = next_cursor(connection)?;
8964        if let Some(next) = &next {
8965            validate_cursor_progress(cursor, &next.0)?;
8966        }
8967        // The recorded far ends ride on the last page of `blockedBy`, whose answer carried the
8968        // body they are recorded in, as far as the page has room — so they cost no request of
8969        // their own — and only what does not fit is resumed by a cursor.
8970        let mut items = items;
8971        if next.is_none() {
8972            let recorded = self
8973                .recorded_edges(
8974                    id,
8975                    near_kind,
8976                    direction,
8977                    natively_names,
8978                    node,
8979                    draft_slot.as_ref(),
8980                )
8981                .await?;
8982            let room = limit.saturating_sub(items.len());
8983            let total = recorded.len();
8984            items.extend(recorded.into_iter().take(room));
8985            if total > room {
8986                next = Some(Cursor(format!("{RECORDED_CURSOR}{room}")));
8987            }
8988        }
8989        Ok(Page { items, next })
8990    }
8991
8992    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
8993    /// a far end in another source has to live: no GitHub issue relationship can name one.
8994    ///
8995    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
8996    /// source never writes one down.
8997    ///
8998    /// The metadata lives in the item's own body slot, and `node` is the dependency read's
8999    /// own answer, which carries an issue's body — so an issue's recorded edges cost no
9000    /// request beyond the read already made, and reading the board for them would be a
9001    /// walk of every item for one field of one. A draft has no body in that answer, because
9002    /// a draft is not an issue, so a draft's are read off its own read by id — never off a
9003    /// listing of the board, which can be behind on the very item asked about.
9004    async fn recorded_edges(
9005        &self,
9006        id: &NativeId,
9007        near_kind: ItemKind,
9008        direction: Direction,
9009        natively_names: Option<ItemKind>,
9010        node: &Value,
9011        draft_slot: Option<&Option<BTreeMap<String, Value>>>,
9012    ) -> Result<Vec<DependencyEdge>, SourceError> {
9013        if direction != Direction::DependsOn {
9014            return Ok(Vec::new());
9015        }
9016        let slot = match (node.get("body"), draft_slot) {
9017            (Some(body), _) if natively_names.is_some() => {
9018                metadata_body(body.as_str().map(str::to_owned))?.1
9019            }
9020            // A draft the dependency read itself answered: this board's, or none of its.
9021            (_, Some(Some(slot))) => slot.clone(),
9022            (_, Some(None)) => return Ok(Vec::new()),
9023            _ => {
9024                let Some(item) = self.bound_item(id).await? else {
9025                    return Ok(Vec::new());
9026                };
9027                item.slot
9028            }
9029        };
9030        DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
9031            .map_err(|message| SourceError::Malformed { message })
9032    }
9033
9034    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
9035        self.repository
9036            .as_ref()
9037            .ok_or_else(|| SourceError::Refused {
9038                message: format!(
9039                    "source {} has no repository configured, and a GitHub Projects board has no \
9040                 repository of its own to create an issue in; set repository: owner/name on \
9041                 this source",
9042                    self.name
9043                ),
9044            })
9045    }
9046
9047    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
9048    /// states.
9049    ///
9050    /// The fallback is demanded first, whichever arm answers: a write without a configured
9051    /// repository is refused naming the field exactly as it was before the rule existed,
9052    /// so a source that could not write before cannot write now, rather than writing for
9053    /// the one item whose own field happens to decide it.
9054    ///
9055    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
9056    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
9057    /// entry owned by someone other than the owner of the parent issue's repository —
9058    /// GitHub accepts a sub-issue from another repository of the same owner and from no
9059    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
9060    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
9061    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
9062    /// and is visible to the token is checked where its node id is resolved, still before
9063    /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
9064    /// looked up in a listing of the board, which can be minutes behind an issue its own
9065    /// `projectItems` already places on it — and that read answers first from this process's
9066    /// own record, so a project created moments ago in this command answers though GitHub
9067    /// has not caught up.
9068    async fn creation_target(
9069        &self,
9070        incoming: &Incoming<'_>,
9071    ) -> Result<RepositoryTarget, SourceError> {
9072        let fallback = self.configured_repository()?;
9073        let what = |incoming: &Incoming<'_>| {
9074            format!(
9075                "{} {:?}",
9076                incoming.written.kind().describes(),
9077                incoming.title
9078            )
9079        };
9080        let parent = match incoming.parent {
9081            Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
9082                SourceError::Refused {
9083                    message: format!(
9084                        "GitHub project issue {} was not found on the board of source {}, so {} \
9085                         cannot be filed under it",
9086                        parent.0,
9087                        self.name,
9088                        what(incoming)
9089                    ),
9090                }
9091            })?),
9092            None => None,
9093        };
9094        let parents_repository = parent
9095            .as_ref()
9096            .map(|parent| {
9097                // A draft is on the board and so is found, but it has no repository to
9098                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
9099                // would refuse the task only once `createIssue` had made it.
9100                if parent.content_kind == ContentKind::DraftIssue {
9101                    return Err(SourceError::Refused {
9102                        message: format!(
9103                            "GitHub project item {} on the board of source {} is a draft, \
9104                             which cannot have sub-issues, so {} cannot be filed under it",
9105                            parent.id.0,
9106                            self.name,
9107                            what(incoming)
9108                        ),
9109                    });
9110                }
9111                // An issue's repository is where a sub-issue is placed and whose owner it
9112                // is compared against, so a parent whose repository this source cannot
9113                // spell as `owner/name` — GitHub's login grammar is wider than this
9114                // source's floor — is one nothing can be filed under.
9115                parent
9116                    .own_repository
9117                    .as_ref()
9118                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
9119                    .ok_or_else(|| SourceError::Malformed {
9120                        message: format!(
9121                            "GitHub project issue {} on the board of source {} is in {}, which \
9122                             is not a {}/owner/name repository this source can place {} in",
9123                            parent.id.0,
9124                            self.name,
9125                            parent
9126                                .own_repository
9127                                .as_ref()
9128                                .map_or("no repository", Repository::as_str),
9129                            RepositoryTarget::HOST,
9130                            what(incoming)
9131                        ),
9132                    })
9133            })
9134            .transpose()?;
9135        match incoming.repositories {
9136            [named] => {
9137                let target =
9138                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
9139                        message: format!(
9140                            "{} names repository {}, which is not a {}/owner/name repository \
9141                             source {} can create an issue in; name one that is, or name none",
9142                            what(incoming),
9143                            named.as_str(),
9144                            RepositoryTarget::HOST,
9145                            self.name
9146                        ),
9147                    })?;
9148                if let Some(parents) = &parents_repository
9149                    && parents.owner != target.owner
9150                {
9151                    return Err(SourceError::Refused {
9152                        message: format!(
9153                            "{} names repository {}, owned by {}, but its project's issue is in \
9154                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
9155                             of the same owner as its parent issue; name a repository of {}, or \
9156                             name none",
9157                            what(incoming),
9158                            target.slug(),
9159                            target.owner,
9160                            parents.slug(),
9161                            parents.owner,
9162                            parents.owner
9163                        ),
9164                    });
9165                }
9166                Ok(target)
9167            }
9168            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
9169        }
9170    }
9171
9172    /// The node id of the repository `incoming` is being created in, or the refusal naming
9173    /// the item and the repository the token cannot see.
9174    ///
9175    /// Resolved once per command per repository; see [`Self::repository_cache`].
9176    async fn repository_id(
9177        &self,
9178        repository: &RepositoryTarget,
9179        incoming: &Incoming<'_>,
9180    ) -> Result<String, SourceError> {
9181        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
9182            return Ok(id);
9183        }
9184        let data = self
9185            .graphql(
9186                graphql::REPOSITORY,
9187                json!({"owner":repository.owner,"name":repository.name}),
9188            )
9189            .await?;
9190        self.repository_read(&data, repository, incoming)
9191    }
9192
9193    /// The repository's node id out of an answer carrying the `repository` root, held for
9194    /// the rest of this command, or the refusal naming the item that cannot be created in it.
9195    fn repository_read(
9196        &self,
9197        data: &Value,
9198        repository: &RepositoryTarget,
9199        incoming: &Incoming<'_>,
9200    ) -> Result<String, SourceError> {
9201        let node = data
9202            .get("repository")
9203            .filter(|value| !value.is_null())
9204            .ok_or_else(|| SourceError::Refused {
9205                message: format!(
9206                    "GitHub repository {} was not found or is not visible to the token, so {} \
9207                     {:?} cannot be created in it",
9208                    repository.slug(),
9209                    incoming.written.kind().describes(),
9210                    incoming.title
9211                ),
9212            })?;
9213        let id = required_str(node, "id")?.to_owned();
9214        self.repository_cache()?
9215            .insert(repository.clone(), id.clone());
9216        Ok(id)
9217    }
9218
9219    fn repository_cache(
9220        &self,
9221    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
9222        self.repository_cache
9223            .lock()
9224            .map_err(|_| SourceError::Unavailable {
9225                message: "this source's record of the destination repository was left \
9226                          inconsistent by an earlier failure; next: run the command again"
9227                    .into(),
9228            })
9229    }
9230
9231    /// Create or update one board item, whichever kind it is.
9232    async fn write_item(
9233        &self,
9234        incoming: &Incoming<'_>,
9235        target: Option<&NativeId>,
9236        depends_on: &[DependencyEdge],
9237    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9238        let prepared = self.prepare_write(incoming, target, depends_on).await?;
9239        let landed = match &prepared.destination {
9240            // Its content is written last, by `complete_write`, once everything else has landed.
9241            WriteDestination::Existing(item) => Landed {
9242                content_id: item.id.clone(),
9243                item_id: Some(item.item_id.clone()),
9244                url: item.url.clone(),
9245                number: item.number,
9246            },
9247            WriteDestination::New(target) => {
9248                if self.deferring()? {
9249                    self.create_filed_issue(
9250                        prepared.board.id.as_str(),
9251                        target,
9252                        incoming,
9253                        &prepared.body,
9254                    )
9255                    .await?
9256                } else {
9257                    self.create_and_file_issue(
9258                        prepared.board.id.as_str(),
9259                        target,
9260                        incoming,
9261                        &prepared.body,
9262                    )
9263                    .await?
9264                }
9265            }
9266        };
9267        self.complete_write(incoming, prepared, landed).await
9268    }
9269
9270    /// Everything one write of `incoming` reads and decides before it sends anything: what is
9271    /// there now, where a new item is created, the board's fields, and every value refused while
9272    /// nothing has been written — the item's body included.
9273    async fn prepare_write(
9274        &self,
9275        incoming: &Incoming<'_>,
9276        target: Option<&NativeId>,
9277        depends_on: &[DependencyEdge],
9278    ) -> Result<Prepared, SourceError> {
9279        // Refused before anything is read or written: a task or a project titled the way
9280        // this board spells a document would land as an issue this same source reads back
9281        // as a document, so the field this destination cannot carry is named rather than
9282        // written and silently reclassified.
9283        if let Written::Work(kind, _) = incoming.written
9284            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
9285        {
9286            return Err(SourceError::Refused {
9287                message: format!(
9288                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
9289                     spells a document, so it would read back as one rather than as a {}; \
9290                     retitle it, or copy it as a document",
9291                    kind.marker(),
9292                    self.name,
9293                    kind.marker()
9294                ),
9295            });
9296        }
9297        // The destination is read by its own id, and whether this board holds it is decided
9298        // by that read — its own `projectItems` — rather than by whether a listing of the
9299        // board happens to include it yet. See the module documentation.
9300        // An existing issue is never moved; a new one is created where the rule says — and
9301        // knowing where is what lets the board's fields and that repository's id be read
9302        // together, before anything below needs either.
9303        let destination = match target {
9304            Some(target) => {
9305                WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
9306                    || SourceError::Refused {
9307                        message: format!("GitHub destination item {} was not found", target.0),
9308                    },
9309                )?))
9310            }
9311            None => {
9312                let target = self.creation_target(incoming).await?;
9313                self.creation_context(&target, incoming).await?;
9314                WriteDestination::New(target)
9315            }
9316        };
9317        let existing = destination.existing();
9318        let board = self
9319            .fields_for(
9320                existing,
9321                incoming.metadata.contains_key(ORIGIN_KEY),
9322                incoming.written.status().is_some(),
9323                incoming
9324                    .priority
9325                    .is_some_and(|priority| priority != Priority::None),
9326            )
9327            .await?;
9328        let status_target = incoming
9329            .written
9330            .work_status()
9331            .map(|(kind, status)| self.resolved_target(kind, status.category))
9332            .transpose()?;
9333        let column = match (incoming.written.work_status(), status_target.as_ref()) {
9334            (Some((kind, status)), Some(target)) => {
9335                self.column_for(&board.fields, kind, status.category, target)?
9336            }
9337            _ => None,
9338        };
9339        // Resolved before anything is created, for the reason the column above is: a
9340        // priority this board has no option for is refused while nothing has been written.
9341        let priority_write = match incoming.priority {
9342            Some(priority) => self.priority_write(&board.fields, existing, priority)?,
9343            None => None,
9344        };
9345        // And for the reason the priority is: a value no projected field can hold, or a board
9346        // that cannot hold the field, is refused while nothing has been written.
9347        let (projections, projected) = self.projection_writes(
9348            &board.fields,
9349            existing.map(|item| &item.projected),
9350            incoming.metadata,
9351            None,
9352        )?;
9353        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
9354        if content_kind == ContentKind::DraftIssue {
9355            if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
9356                (status_target.as_ref(), incoming.written.status())
9357            {
9358                return Err(self.closes_a_draft(status.category));
9359            }
9360            if incoming.parent.is_some() {
9361                return Err(SourceError::Refused {
9362                    message: "GitHub draft items cannot be a project's sub-issue".into(),
9363                });
9364            }
9365        }
9366        match existing {
9367            Some(item) if content_kind == ContentKind::Issue => {
9368                if item.labels != incoming.labels {
9369                    return Err(SourceError::Refused {
9370                        message: "GitHub issue labels differ from the labels being written".into(),
9371                    });
9372                }
9373            }
9374            _ => {
9375                if !incoming.labels.is_empty() {
9376                    return Err(SourceError::Refused {
9377                        message: "GitHub items created by this destination carry no labels".into(),
9378                    });
9379                }
9380            }
9381        }
9382
9383        // The repository the issue really lives in is what the slot below is written against,
9384        // so a single entry that is where the issue is created travels as no key at all, and
9385        // the read side derives it back from the issue.
9386        let own_repository = match &destination {
9387            WriteDestination::Existing(item) => item.own_repository.clone(),
9388            WriteDestination::New(target) => Some(
9389                Repository::try_from(target.origin())
9390                    .map_err(|message| SourceError::Config { message })?,
9391            ),
9392        };
9393        let (native, fallback) = self
9394            .partition_edges(
9395                incoming.written.kind(),
9396                content_kind,
9397                existing.and_then(|item| item.blocked_by.as_deref()),
9398                depends_on,
9399            )
9400            .await?;
9401        let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
9402        let content = match incoming.assets {
9403            Some(assets) => {
9404                let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
9405                let rewritten = onetaskgraph_plugin_api::serve_asset_references(
9406                    incoming.content.unwrap_or_default(),
9407                    &mut slot,
9408                    &uploads,
9409                );
9410                incoming.content.map(|_| rewritten)
9411            }
9412            None => incoming.content.map(str::to_owned),
9413        };
9414        let body = compose_body(content.as_deref(), &slot)?;
9415        // Read before anything is created, for the reason the field below is: a value
9416        // this destination cannot store has to refuse, and refusing after `createIssue`
9417        // would leave an issue behind that nothing asked for. The engine writes a
9418        // qualified id here; a caller handing this key anything else is told so rather
9419        // than having it silently stored as no origin at all.
9420        // 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.
9421        let origin = match incoming.metadata.get(ORIGIN_KEY) {
9422            None => "",
9423            Some(Value::String(origin)) => origin.as_str(),
9424            Some(other) => {
9425                return Err(SourceError::Refused {
9426                    message: format!(
9427                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
9428                         is {other}"
9429                    ),
9430                });
9431            }
9432        };
9433        // Resolved before anything is created: a board that cannot carry the copy origin
9434        // has to refuse the write, and refusing it after `createIssue` would leave an
9435        // issue behind that nothing asked for.
9436        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
9437            Some(field) => {
9438                if required_str(field, "__typename")? != "ProjectV2Field" {
9439                    return Err(SourceError::Refused {
9440                        message: format!(
9441                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
9442                        ),
9443                    });
9444                }
9445                Some(required_str(field, "id")?.to_owned())
9446            }
9447            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
9448                return Err(SourceError::Refused {
9449                    message: format!(
9450                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
9451                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
9452                         the board"
9453                    ),
9454                });
9455            }
9456            None => None,
9457        };
9458
9459        Ok(Prepared {
9460            origin: origin.to_owned(),
9461            destination,
9462            board,
9463            status_target,
9464            column,
9465            priority_write,
9466            projections,
9467            projected,
9468            content_kind,
9469            own_repository,
9470            native,
9471            slot,
9472            content,
9473            body,
9474            origin_field,
9475        })
9476    }
9477
9478    /// Everything one write does once the item it writes exists: its board fields, its
9479    /// relationships and — for an item that was already there — its content last, then what this
9480    /// process remembers of it.
9481    ///
9482    /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
9483    /// item this command created has its fields, its closing and its edges held for
9484    /// [`TaskSource::finish_writes`] rather than sent here.
9485    async fn complete_write(
9486        &self,
9487        incoming: &Incoming<'_>,
9488        prepared: Prepared,
9489        landed: Landed,
9490    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9491        let Prepared {
9492            destination,
9493            board,
9494            status_target,
9495            column,
9496            priority_write,
9497            projections,
9498            projected,
9499            content_kind,
9500            own_repository,
9501            native,
9502            slot,
9503            content,
9504            body,
9505            origin,
9506            origin_field,
9507        } = prepared;
9508        let existing = destination.existing();
9509        let origin = origin.as_str();
9510        let Landed {
9511            content_id,
9512            item_id,
9513            url,
9514            number,
9515        } = landed;
9516
9517        // Only what differs from the item as this write read it is sent: a priority the item
9518        // already holds is not, so an unchanged re-copy writes no field and a person's edit of
9519        // it since that read is left alone.
9520        let priority_write = priority_write.filter(|_| {
9521            !existing.is_some_and(|item| {
9522                incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
9523            })
9524        });
9525        let written_option = column.as_ref().map(|(_, _, name)| name.clone());
9526        let column = column
9527            .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
9528            .map(|(field, option, _)| (field, option));
9529        // Creating an item here is several calls — `createIssue`, which files it on the
9530        // board, then its board fields, the parent and the dependencies — and GitHub can fail
9531        // at any of them. Everything this source can refuse *before* the first of those is
9532        // already checked above, so what is left is GitHub itself failing part way. When it
9533        // does over an item this call created, the issue is taken back: a write that
9534        // refused must not leave an item behind that nobody asked for, and one that does
9535        // makes the retry create a second.
9536        // Whether the board-field write carrying a moved origin was answered as landing whole.
9537        // When it was refused, GitHub does not say which of its fields ran before the one that
9538        // failed, so the origin may or may not have moved.
9539        let mut origin_landed = false;
9540        // An item this command created while writes are held back has every part that can land
9541        // later held for `finish_writes` — its own create filed it on the board and under its
9542        // project already — and so does a later write of it in the same command.
9543        let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
9544        let landed = if holding {
9545            self.hold_back(
9546                &board.id,
9547                &content_id,
9548                existing,
9549                origin_field.as_deref(),
9550                origin,
9551                column,
9552                status_target.as_ref(),
9553                priority_write.as_ref(),
9554                &projections,
9555                &native,
9556            )
9557        } else if let Some(item_id) = item_id.as_deref() {
9558            self.finish_write(
9559                board.id.as_str(),
9560                incoming,
9561                &content_id,
9562                item_id,
9563                content_kind,
9564                existing,
9565                origin_field.as_deref(),
9566                origin,
9567                column,
9568                status_target.as_ref(),
9569                priority_write.as_ref(),
9570                &projections,
9571                &native,
9572                &mut origin_landed,
9573            )
9574            .await
9575        } else {
9576            // An issue a create filed on the board itself has no board item this process
9577            // knows until `finish_writes` reads it, so nothing but a held-back write may land
9578            // on it before then.
9579            Err(SourceError::Malformed {
9580                message: format!(
9581                    "issue {} was written before the board item GitHub filed it as was read",
9582                    content_id.0
9583                ),
9584            })
9585        };
9586        // An existing item's title, body and state go last, in one `updateIssue`, once its board
9587        // fields and its relationships have landed: a refusal of any of those then leaves its
9588        // body — and the metadata slot inside it — exactly as it stood.
9589        let landed = match (landed, existing) {
9590            (Ok(()), Some(item)) => {
9591                self.update_existing(item, incoming, &body, status_target.as_ref())
9592                    .await
9593            }
9594            (landed, _) => landed,
9595        };
9596        if let Err(error) = landed {
9597            match existing {
9598                // Best effort, and the write's own failure is what the caller is told: a
9599                // refusal naming the tidy-up would hide why the write failed at all.
9600                None => {
9601                    let _ = self.delete_issue(&content_id).await;
9602                }
9603                // The origin field is the one piece of an existing item's metadata written
9604                // before its body, so a write refused after it puts it back as it was. When
9605                // that is refused too, the write's own failure is still what the caller is
9606                // told — with what it left behind added, because the item's metadata is then
9607                // not as it stood and a caller retrying has to know which key moved.
9608                Some(item) => {
9609                    let before = item.origin.as_deref().unwrap_or("");
9610                    if let Some(field) = origin_field.as_deref()
9611                        && before != origin
9612                        && let Err(restore) = self
9613                            .set_item_field(
9614                                board.id.as_str(),
9615                                &item.item_id,
9616                                field,
9617                                json!({"text": before}),
9618                            )
9619                            .await
9620                    {
9621                        let left = if origin_landed {
9622                            format!(
9623                                "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
9624                                 not be put back to {before:?} ({restore}), so item {} still \
9625                                 holds {origin:?} there",
9626                                item.id.0
9627                            )
9628                        } else {
9629                            format!(
9630                                "the refused write carried its {ORIGIN_KEY} from {before:?} to \
9631                                 {origin:?}, GitHub does not say whether that part of it ran, \
9632                                 and putting it back to {before:?} was refused ({restore}), so \
9633                                 item {} holds {origin:?} or {before:?} there",
9634                                item.id.0
9635                            )
9636                        };
9637                        return Err(noting(
9638                            error,
9639                            &format!(
9640                                "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
9641                                 run the write again"
9642                            ),
9643                        ));
9644                    }
9645                }
9646            }
9647            return Err(error);
9648        }
9649
9650        let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
9651            (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
9652                self.statuses
9653                    .status(kind, written_option.as_deref(), true, Some(reason.reason()))
9654            }
9655            (Some((kind, _)), Some(StatusTarget::Column(_))) => {
9656                self.statuses
9657                    .status(kind, written_option.as_deref(), false, None)
9658            }
9659            (Some((_, status)), _) => status.clone(),
9660            (None, _) => Status {
9661                category: StatusCategory::Unknown,
9662                name: "Open".to_owned(),
9663            },
9664        };
9665
9666        // So the rest of this command reads what it just did rather than what the board
9667        // said before it. See `remember_written` for which half takes it.
9668        let remembered = Resolved {
9669            // Empty for an issue whose board item is not read yet: `finish_writes` reads it
9670            // and `learn_item` puts it here.
9671            // 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.
9672            item_id: item_id.unwrap_or_default(),
9673            id: content_id.clone(),
9674            content_kind,
9675            kind: incoming.written.kind(),
9676            title: incoming.title.to_owned(),
9677            // The visible half of the body this write composed, split back off it the
9678            // way a read splits it — so what this record reports is what a read of the
9679            // same issue reports, rather than the person's text with the metadata slot
9680            // still on the end of it.
9681            body: metadata_body(body.clone())?.0,
9682            raw_body: body.clone(),
9683            // A document has no status of its own; what it reads back as is whatever
9684            // the issue's own state says, which is what a re-read reports.
9685            status: written_status,
9686            option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
9687            priority: match incoming.priority {
9688                Some(priority) => HeldPriority::Read(priority),
9689                None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
9690                    item.priority.clone()
9691                }),
9692            },
9693            // What `state_input` asked for: closed for a terminal target, open for any other
9694            // status, and the issue's own state left as it was by a document write.
9695            closed: content_kind == ContentKind::Issue
9696                && match status_target.as_ref() {
9697                    Some(StatusTarget::Terminal(_, _)) => true,
9698                    Some(_) => false,
9699                    None => existing.is_some_and(|item| item.closed),
9700                },
9701            reason: match status_target.as_ref() {
9702                Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
9703                    Some(reason.reason().to_owned())
9704                }
9705                Some(_) => None,
9706                None => existing.and_then(|item| item.reason.clone()),
9707            },
9708            delivers: incoming.delivers.to_vec(),
9709            delivered_by: incoming.delivered_by.to_vec(),
9710            labels: incoming.labels.to_vec(),
9711            parent: incoming.parent.cloned(),
9712            origin: (!origin.is_empty()).then(|| origin.to_owned()),
9713            projected,
9714            number,
9715            // In the update path this is the item's own url, read off `existing` where the
9716            // record above was bound, so one expression serves both halves.
9717            url,
9718            created_at: existing.and_then(|item| item.created_at),
9719            updated_at: existing.and_then(|item| item.updated_at),
9720            // What this write moved is GitHub's to say, so a record of it gives no token until
9721            // it is read again.
9722            item_updated_at: None,
9723            comment_count: None,
9724            relationship_events: None,
9725            served_at: None,
9726            facets: None,
9727            own_repository,
9728            repositories: incoming.repositories.to_vec(),
9729            classification: incoming.classification,
9730            slot,
9731            board_id: Some(board.id.as_str().to_owned()),
9732            fields: board
9733                .fields
9734                .get("nodes")
9735                .and_then(Value::as_array)
9736                .cloned()
9737                .unwrap_or_default(),
9738            board_fields: Some(board.fields.clone()),
9739            // What this write left the relationship holding is known by id alone, and a
9740            // later read of its edges needs each far end's kind, so it reads them again.
9741            blocked_by: None,
9742        };
9743        self.remember_written(remembered, existing.is_none())?;
9744        Ok(onetaskgraph_plugin_api::AssetsWritten {
9745            id: content_id,
9746            content,
9747        })
9748    }
9749
9750    /// Everything a write does after the item exists: its board fields, its parent, and
9751    /// its dependencies.
9752    ///
9753    /// Split out of `write_item` so there is one place a failure past the point of no
9754    /// return is caught, rather than a tidy-up repeated at each `?` above.
9755    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
9756    // so there is one place a failure past the point of no return is caught, and its
9757    // arguments are exactly the values that tail already had in scope. Bundling them into a
9758    // struct would describe no concept — it would be "the arguments of this function" — and
9759    // would put the whole of `write_item`'s locals behind one more indirection.
9760    #[allow(clippy::too_many_arguments)]
9761    async fn finish_write(
9762        &self,
9763        board_id: &str,
9764        incoming: &Incoming<'_>,
9765        content_id: &NativeId,
9766        item_id: &str,
9767        content_kind: ContentKind,
9768        existing: Option<&Resolved>,
9769        origin_field: Option<&str>,
9770        origin: &str,
9771        column: Option<(String, String)>,
9772        status_target: Option<&StatusTarget>,
9773        priority: Option<&PriorityWrite>,
9774        projections: &[ProjectionWrite],
9775        native: &[String],
9776        origin_landed: &mut bool,
9777    ) -> Result<(), SourceError> {
9778        let (fields, clears) = field_writes(
9779            existing,
9780            origin_field,
9781            origin,
9782            column,
9783            priority,
9784            projections,
9785        );
9786        self.set_item_fields(board_id, item_id, &fields, &clears)
9787            .await?;
9788        *origin_landed = true;
9789
9790        // An existing issue closes in the `updateIssue` its write ends with; one created just
9791        // now closes here, once its option is selected.
9792        if existing.is_none()
9793            && content_kind == ContentKind::Issue
9794            && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
9795        {
9796            self.update_content(
9797                ContentKind::Issue,
9798                content_id,
9799                json!({"stateInput":state_input(status_target)}),
9800            )
9801            .await?;
9802        }
9803
9804        if content_kind == ContentKind::Issue {
9805            self.reparent(
9806                existing.and_then(|item| item.parent.clone()),
9807                content_id,
9808                incoming.parent,
9809            )
9810            .await?;
9811            // A document takes part in no dependency graph, so writing one neither reads
9812            // nor changes the issue's own `blockedBy` relationships. Reconciling them
9813            // against the empty list a document write carries would *delete* whatever
9814            // relationships a person had made on that issue, which is a write nobody
9815            // asked for.
9816            if incoming.written.kind() != BoardKind::Document {
9817                let issue = match existing {
9818                    Some(item) => Issue::Existing(item.blocked_by.as_deref()),
9819                    None => Issue::Created,
9820                };
9821                self.reconcile_blocked_by(content_id, native, issue).await?;
9822            }
9823        }
9824        Ok(())
9825    }
9826
9827    /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
9828    fn deferring(&self) -> Result<bool, SourceError> {
9829        Ok(self.deferred()?.is_some())
9830    }
9831
9832    /// Whether `content` is an item this command created whose parts are held back.
9833    fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
9834        Ok(self
9835            .deferred()?
9836            .as_ref()
9837            .is_some_and(|deferred| deferred.holds(content)))
9838    }
9839
9840    fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
9841        self.deferred.lock().map_err(|_| SourceError::Unavailable {
9842            message: "this source's record of the writes it is holding back was left \
9843                      inconsistent by an earlier failure; next: run the command again"
9844                .into(),
9845        })
9846    }
9847
9848    /// Creates one issue filed on the board and under its project by the `createIssue` itself —
9849    /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
9850    /// creates with: one request where adding it to the board and filing it under its project
9851    /// were two more. GitHub adds the board item after answering, so its id is not known here;
9852    /// [`TaskSource::finish_writes`] reads it.
9853    async fn create_filed_issue(
9854        &self,
9855        board_id: &str,
9856        repository: &RepositoryTarget,
9857        incoming: &Incoming<'_>,
9858        body: &Option<String>,
9859    ) -> Result<Landed, SourceError> {
9860        let repository_id = self.repository_id(repository, incoming).await?;
9861        let data = self
9862            .graphql(
9863                graphql::CREATE_ISSUE,
9864                json!({"input": filed_input(&repository_id, board_id, incoming, body)}),
9865            )
9866            .await?;
9867        let created = data
9868            .pointer("/createIssue/issue")
9869            .filter(|value| !value.is_null())
9870            .ok_or_else(|| SourceError::Malformed {
9871                message: "GitHub issue creation returned no issue".into(),
9872            })?;
9873        self.created_landed(created).await
9874    }
9875
9876    /// Keep the address `createIssue` answered for the issue `content`, for
9877    /// [`TaskSource::written_location`].
9878    fn note_written(&self, content: &NativeId, url: Option<&str>) {
9879        if let Some(url) = url {
9880            self.written
9881                .lock()
9882                .unwrap_or_else(std::sync::PoisonError::into_inner)
9883                .insert(content.clone(), url.to_owned());
9884        }
9885    }
9886
9887    /// What one `createIssue` answered, as the item it created — or, when its number cannot be
9888    /// read, the refusal, after taking the issue back.
9889    async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
9890        let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
9891        let url = optional_str(created, "url")?.map(str::to_owned);
9892        let number = match created_issue_number(created) {
9893            Ok(number) => number,
9894            Err(error) => {
9895                let _ = self.delete_issue(&content_id).await;
9896                return Err(error);
9897            }
9898        };
9899        self.note_written(&content_id, url.as_deref());
9900        Ok(Landed {
9901            content_id,
9902            item_id: None,
9903            url,
9904            number,
9905        })
9906    }
9907
9908    /// Hold back everything a write of the created item `content` would send after creating it,
9909    /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
9910    /// `finish_write` sends, the parent excepted, which its create already named. A later write
9911    /// of the same item replaces what it changes.
9912    // 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.
9913    #[allow(clippy::too_many_arguments)]
9914    fn hold_back(
9915        &self,
9916        board_id: &BoardId,
9917        content: &NativeId,
9918        existing: Option<&Resolved>,
9919        origin_field: Option<&str>,
9920        origin: &str,
9921        column: Option<(String, String)>,
9922        status_target: Option<&StatusTarget>,
9923        priority: Option<&PriorityWrite>,
9924        projections: &[ProjectionWrite],
9925        native: &[String],
9926    ) -> Result<(), SourceError> {
9927        let status = column.map(|(field, option)| {
9928            let name = self
9929                .fields_cache()
9930                .ok()
9931                .and_then(|fields| {
9932                    fields.as_ref().and_then(|board| {
9933                        option_name(&board.fields, &field, &option).map(str::to_owned)
9934                    })
9935                })
9936                .unwrap_or_default();
9937            (field, option, name)
9938        });
9939        let (fields, clears) =
9940            field_writes(existing, origin_field, origin, None, priority, projections);
9941        let close = match status_target {
9942            Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
9943            _ => None,
9944        };
9945        let mut deferred = self.deferred()?;
9946        let deferred = deferred.get_or_insert_with(Deferred::default);
9947        if let Deferred::Nothing = deferred {
9948            *deferred = Deferred::Held {
9949                board: board_id.clone(),
9950                created: Vec::new(),
9951            };
9952        }
9953        let Deferred::Held { created, .. } = deferred else {
9954            unreachable!("held back on the line above");
9955        };
9956        let held = match created.iter().position(|held| held.content == *content) {
9957            Some(at) => &mut created[at],
9958            None => {
9959                created.push(HeldBack {
9960                    content: content.clone(),
9961                    fields: Vec::new(),
9962                    status: None,
9963                    clears: Vec::new(),
9964                    close: None,
9965                    edges: Vec::new(),
9966                });
9967                created.last_mut().expect("just held")
9968            }
9969        };
9970        for (field, value) in fields {
9971            held.fields.retain(|(held, _)| *held != field);
9972            held.clears.retain(|held| *held != field);
9973            held.fields.push((field, value));
9974        }
9975        for field in clears {
9976            held.fields.retain(|(held, _)| held != field);
9977            if !held.clears.iter().any(|held| held == field) {
9978                held.clears.push(field.to_owned());
9979            }
9980        }
9981        if status.is_some() {
9982            held.status = status;
9983        }
9984        if close.is_some() {
9985            held.close = close;
9986        }
9987        // Nothing of an item created in this command has been sent but its create, so the edges
9988        // it is to have are exactly the ones its latest write names.
9989        held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
9990        Ok(())
9991    }
9992
9993    /// Create every task of `writes`, together, filed on the board and under each one's
9994    /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
9995    ///
9996    /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
9997    /// before the first request — then created, then completed with its parts held back. A
9998    /// request refused part way leaves the issues it did create, which are taken back, with the
9999    /// rest of this call's, before the refusal is answered.
10000    async fn create_together(
10001        &self,
10002        writes: &[ItemWrite<Task>],
10003    ) -> Result<Vec<NativeId>, SourceError> {
10004        let incomings: Vec<Incoming<'_>> = writes
10005            .iter()
10006            .map(|write| self.task_incoming(write, None))
10007            .collect::<Result<_, _>>()?;
10008        let mut prepared = Vec::with_capacity(writes.len());
10009        for (incoming, write) in incomings.iter().zip(writes) {
10010            if write.target.is_some() {
10011                return Err(SourceError::Malformed {
10012                    message: "a write naming its target was handed to a batch of creates".into(),
10013                });
10014            }
10015            prepared.push(
10016                self.prepare_write(incoming, None, &write.depends_on)
10017                    .await?,
10018            );
10019        }
10020        let mut inputs = Vec::with_capacity(prepared.len());
10021        for (incoming, prepared) in incomings.iter().zip(&prepared) {
10022            let WriteDestination::New(target) = &prepared.destination else {
10023                return Err(SourceError::Malformed {
10024                    message: "an update was decided for a write handed to a batch of creates"
10025                        .into(),
10026                });
10027            };
10028            let repository_id = self.repository_id(target, incoming).await?;
10029            inputs.push(filed_input(
10030                &repository_id,
10031                prepared.board.id.as_str(),
10032                incoming,
10033                &prepared.body,
10034            ));
10035        }
10036        let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
10037        let refused = 'create: {
10038            for chunk in inputs.chunks(MUTATION_BATCH) {
10039                let mut variables = serde_json::Map::new();
10040                for slot in 0..MUTATION_BATCH {
10041                    variables.insert(
10042                        format!("create{slot}"),
10043                        chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10044                    );
10045                    variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10046                }
10047                let data = match self
10048                    .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
10049                    .await
10050                {
10051                    Ok(data) => data,
10052                    Err(error) => break 'create Some(error),
10053                };
10054                let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
10055                    let said: Vec<&str> = errors
10056                        .as_array()
10057                        .into_iter()
10058                        .flatten()
10059                        .filter_map(|error| error.get("message").and_then(Value::as_str))
10060                        .collect();
10061                    SourceError::Refused {
10062                        message: if said.is_empty() {
10063                            "GitHub returned GraphQL errors".to_owned()
10064                        } else {
10065                            said.join("; ")
10066                        },
10067                    }
10068                });
10069                for slot in 0..chunk.len() {
10070                    match data
10071                        .pointer(&format!("/create{slot}/issue"))
10072                        .filter(|value| !value.is_null())
10073                    {
10074                        Some(created) => match self.created_landed(created).await {
10075                            Ok(created) => landed.push(created),
10076                            Err(error) => {
10077                                refusal.get_or_insert(error);
10078                            }
10079                        },
10080                        // An alias GitHub refused answers nothing, and the refusal says why.
10081                        None => {
10082                            refusal.get_or_insert(SourceError::Malformed {
10083                                message: "GitHub issue creation returned no issue".into(),
10084                            });
10085                        }
10086                    }
10087                }
10088                if let Some(error) = refusal {
10089                    break 'create Some(error);
10090                }
10091            }
10092            None
10093        };
10094        if let Some(error) = refused {
10095            // None of these ids reaches the caller, so an issue GitHub will not take back is
10096            // named here or by nobody.
10097            let ids: Vec<&NativeId> = landed.iter().map(|created| &created.content_id).collect();
10098            let left = self.delete_issues(&ids).await;
10099            let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10100            return Err(left_behind(error, &left));
10101        }
10102        let created: Vec<NativeId> = landed
10103            .iter()
10104            .map(|landed| landed.content_id.clone())
10105            .collect();
10106        let mut ids = Vec::with_capacity(landed.len());
10107        for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
10108            match self.complete_write(incoming, prepared, landed).await {
10109                Ok(written) => ids.push(written.id),
10110                // 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.
10111                Err(error) => {
10112                    let ids: Vec<&NativeId> = created.iter().collect();
10113                    let left = self.delete_issues(&ids).await;
10114                    let left: Vec<&str> = left.iter().map(|id| id.0.as_str()).collect();
10115                    return Err(left_behind(error, &left));
10116                }
10117            }
10118        }
10119        Ok(ids)
10120    }
10121
10122    /// Hold back the update of the existing issue `issue` — `members`, what its own
10123    /// [`graphql::UPDATE_ISSUE`] would send beside its id — while writes are being held back — so the updates a copy makes one after
10124    /// another go to GitHub as one request. Whatever this source held of the issue is dropped
10125    /// as the mutation itself would drop it. Answers whether it was held: never while a write
10126    /// the store's cache expects runs, which records each mutation's own answer, and never for
10127    /// an issue an update is already held for, which that one is sent ahead of.
10128    fn hold_update(&self, issue: &NativeId, members: &Value) -> Result<bool, SourceError> {
10129        if self.expected.journaling() || self.deferred()?.is_none() {
10130            return Ok(false);
10131        }
10132        let mut held = self
10133            .held_updates
10134            .lock()
10135            .map_err(|_| SourceError::Unavailable {
10136                message: "issue updates held back were left inconsistent; run the command again"
10137                    .into(),
10138            })?;
10139        if held.iter().any(|earlier| earlier.issue == *issue) {
10140            return Ok(false);
10141        }
10142        let named = |value: &str| issue.0 == value;
10143        self.resolved_cache()?
10144            .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10145        self.read_ahead()?.retain(|id, item| {
10146            !named(&id.0) && !item.as_ref().is_some_and(|item| named(&item.item_id))
10147        });
10148        self.carried()?
10149            .retain(|id, item| !named(&id.0) && !named(&item.item_id));
10150        held.push(HeldUpdate::new(issue, members));
10151        Ok(true)
10152    }
10153
10154    /// Send every update [`Self::hold_update`] held back: one alone as [`graphql::UPDATE_ISSUE`]
10155    /// sends it, several as aliased `updateIssue`s of [`graphql::UPDATE_ISSUES`],
10156    /// [`MUTATION_BATCH`] to a request. Each counts one write, as it would alone.
10157    async fn send_held_updates(&self) -> Result<(), SourceError> {
10158        let held = std::mem::take(&mut *self.held_updates.lock().map_err(|_| {
10159            SourceError::Unavailable {
10160                message: "issue updates held back were left inconsistent; run the command again"
10161                    .into(),
10162            }
10163        })?);
10164        if let [one] = held.as_slice() {
10165            return self
10166                .send_update(
10167                    graphql::UPDATE_ISSUE,
10168                    "/updateIssue/issue",
10169                    &one.issue,
10170                    one.input.clone(),
10171                )
10172                .await;
10173        }
10174        for chunk in held.chunks(MUTATION_BATCH) {
10175            let mut variables = serde_json::Map::new();
10176            for slot in 0..MUTATION_BATCH {
10177                variables.insert(
10178                    format!("update{slot}"),
10179                    chunk.get(slot).unwrap_or(&chunk[0]).input.clone(),
10180                );
10181                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10182            }
10183            let data = self
10184                .graphql(graphql::UPDATE_ISSUES, Value::Object(variables))
10185                .await?;
10186            let inputs: Vec<&Value> = chunk.iter().map(|held| &held.input).collect();
10187            check_aliased_answers(
10188                graphql::UPDATE_ISSUES,
10189                &data,
10190                "update",
10191                &inputs,
10192                &[("issue", "id")],
10193            )?;
10194        }
10195        Ok(())
10196    }
10197
10198    /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
10199    /// created items' board items, then their field values and edges together in requests of
10200    /// [`WRITE_BATCH`], then their closes in requests of [`MUTATION_BATCH`]. Anything refused
10201    /// takes back every issue held, [`DELETE_BATCH`] to a request, and answers the refusal.
10202    async fn send_held_back(&self) -> Result<(), SourceError> {
10203        let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
10204            return Ok(());
10205        };
10206        if created.is_empty() {
10207            return Ok(());
10208        }
10209        let sent = self.send_deferred(board.as_str(), &created).await;
10210        if let Err(error) = sent {
10211            // Best effort, and the failure is what the caller is told, as a write of one item
10212            // that fails part way takes back what it created: the copy that held these back
10213            // journalled every one, so one this cannot take back its undo tries again, and says
10214            // so when it cannot either.
10215            let ids: Vec<&NativeId> = created.iter().map(|held| &held.content).collect();
10216            let _ = self.delete_issues(&ids).await;
10217            return Err(error);
10218        }
10219        Ok(())
10220    }
10221
10222    /// The requests [`Self::send_held_back`] makes.
10223    async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
10224        let items = self.created_board_items(board, created).await?;
10225        let mut fields: Vec<Value> = Vec::new();
10226        let mut closes: Vec<Value> = Vec::new();
10227        let mut edges: Vec<Value> = Vec::new();
10228        for held in created {
10229            let (item, status) = &items[&held.content];
10230            let mut values = held.fields.clone();
10231            // A status the board already shows — set by a workflow a person turned on to file
10232            // new items at it — is not written again.
10233            if let Some((field, option, name)) = &held.status
10234                && status.as_deref() != Some(name.as_str())
10235            {
10236                values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
10237            }
10238            for (field, value) in values {
10239                fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
10240            }
10241            if let Some(reason) = held.close {
10242                closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
10243            }
10244            for far in &held.edges {
10245                edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
10246            }
10247            if !held.clears.is_empty() {
10248                let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
10249                self.set_item_fields(board, item, &[], &clears).await?;
10250            }
10251        }
10252        // Each alias answers with what it wrote, which is what its input named: the board item a
10253        // field value is set on, the issue closed, the issue blocked and the one blocking it.
10254        let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
10255        let close_answer: &[(&str, &str)] = &[("issue", "id")];
10256        let block_answer: &[(&str, &str)] =
10257            &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
10258        // Every field value, then every edge, as one ordered list sent `WRITE_BATCH` aliases to a
10259        // request: a request holding both kinds is the one document carrying both, so a plan's
10260        // values and edges cost one request where they fit in one.
10261        let writes: Vec<HeldWrite<'_>> = fields
10262            .iter()
10263            .map(HeldWrite::Field)
10264            .chain(edges.iter().map(HeldWrite::Edge))
10265            .collect();
10266        for chunk in writes.chunks(WRITE_BATCH) {
10267            let values: Vec<&Value> = chunk
10268                .iter()
10269                .filter_map(|write| match write {
10270                    HeldWrite::Field(input) => Some(*input),
10271                    HeldWrite::Edge(_) => None,
10272                })
10273                .collect();
10274            let blocks: Vec<&Value> = chunk
10275                .iter()
10276                .filter_map(|write| match write {
10277                    HeldWrite::Edge(input) => Some(*input),
10278                    HeldWrite::Field(_) => None,
10279                })
10280                .collect();
10281            let (document, sends) = match (values.is_empty(), blocks.is_empty()) {
10282                (false, false) => (graphql::WRITE_FIELDS_AND_EDGES, ["sendField", "sendBlock"]),
10283                (false, true) => (graphql::WRITE_FIELD_VALUES, ["send", "send"]),
10284                _ => (graphql::ADD_BLOCKED_BYS, ["send", "send"]),
10285            };
10286            let mut variables = serde_json::Map::new();
10287            let kinds = [
10288                ("field", &values, sends[0], field_answer),
10289                ("block", &blocks, sends[1], block_answer),
10290            ];
10291            for (prefix, inputs, send, _) in &kinds {
10292                if inputs.is_empty() {
10293                    continue;
10294                }
10295                for slot in 0..WRITE_BATCH {
10296                    variables.insert(
10297                        format!("{prefix}{slot}"),
10298                        (*inputs.get(slot).unwrap_or(&inputs[0])).clone(),
10299                    );
10300                    variables.insert(format!("{send}{slot}"), json!(slot < inputs.len()));
10301                }
10302            }
10303            let data = self.graphql(document, Value::Object(variables)).await?;
10304            for (prefix, inputs, _, answers) in kinds {
10305                check_aliased_answers(document, &data, prefix, inputs, answers)?;
10306            }
10307        }
10308        for chunk in closes.chunks(MUTATION_BATCH) {
10309            let mut variables = serde_json::Map::new();
10310            for slot in 0..MUTATION_BATCH {
10311                variables.insert(
10312                    format!("close{slot}"),
10313                    chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
10314                );
10315                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10316            }
10317            let data = self
10318                .graphql(graphql::CLOSE_ISSUES, Value::Object(variables))
10319                .await?;
10320            let inputs: Vec<&Value> = chunk.iter().collect();
10321            check_aliased_answers(graphql::CLOSE_ISSUES, &data, "close", &inputs, close_answer)?;
10322        }
10323        for held in created {
10324            self.learn_item(&held.content, &items[&held.content].0)?;
10325        }
10326        Ok(())
10327    }
10328
10329    /// The board item each of `created` is on this board, with its `Status` as the board set it:
10330    /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
10331    /// eight and sixteen — for only those GitHub has not filed yet.
10332    ///
10333    /// **Membership is never assumed.** `createIssue`'s `projectV2Ids` files an issue some time
10334    /// after GitHub answered the create — the feasibility spike saw 0.45 to 1.84 seconds, and no
10335    /// create answer carrying the item — so only a read says it is there. One still absent once
10336    /// those reads are spent is filed with [`graphql::ADD_TO_BOARD`], one write each; GitHub
10337    /// filing it meanwhile refuses that "Content already exists in this project", which says it
10338    /// is placed, and one more read finds the item rather than a second one being made.
10339    async fn created_board_items(
10340        &self,
10341        board: &str,
10342        created: &[HeldBack],
10343    ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
10344        let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
10345        let unfiled = |found: &BTreeMap<NativeId, (String, Option<String>)>| -> Vec<NativeId> {
10346            created
10347                .iter()
10348                .map(|held| held.content.clone())
10349                .filter(|id| !found.contains_key(id))
10350                .collect()
10351        };
10352        let mut wait = Duration::from_secs(1);
10353        for attempt in 0..6 {
10354            let absent = unfiled(&found);
10355            if absent.is_empty() {
10356                return Ok(found);
10357            }
10358            if attempt > 0 {
10359                self.clock
10360                    .sleep_because(wait, pacing::cause::BOARD_FILING)
10361                    .await;
10362                wait = wait.saturating_mul(2);
10363            }
10364            self.read_created_items(board, &absent, &mut found).await?;
10365        }
10366        let mut placed_meanwhile = Vec::new();
10367        for id in unfiled(&found) {
10368            match self
10369                .graphql(
10370                    graphql::ADD_TO_BOARD,
10371                    json!({"input":{"projectId":board,"contentId":id.0}}),
10372                )
10373                .await
10374            {
10375                Ok(added) => {
10376                    let item = added
10377                        .pointer("/addProjectV2ItemById/item")
10378                        .filter(|value| !value.is_null())
10379                        .ok_or_else(|| SourceError::Malformed {
10380                            message: "GitHub board addition returned no project item".into(),
10381                        })?;
10382                    found.insert(id, (required_nonblank_str(item, "id")?.to_owned(), None));
10383                }
10384                Err(error) if already_on_the_board(&error) => placed_meanwhile.push(id),
10385                Err(error) => return Err(error),
10386            }
10387        }
10388        if !placed_meanwhile.is_empty() {
10389            self.read_created_items(board, &placed_meanwhile, &mut found)
10390                .await?;
10391        }
10392        let absent = unfiled(&found);
10393        if absent.is_empty() {
10394            return Ok(found);
10395        }
10396        let absent: Vec<&str> = absent.iter().map(|id| id.0.as_str()).collect();
10397        Err(SourceError::Unavailable {
10398            message: format!(
10399                "GitHub created {} filed on board {board} and, after 31 seconds and adding {} \
10400                 to it, still answers {} on no item of it: {}; next: run the copy again once \
10401                 GitHub has caught up",
10402                if absent.len() == 1 {
10403                    "an issue"
10404                } else {
10405                    "issues"
10406                },
10407                if absent.len() == 1 { "it" } else { "them" },
10408                if absent.len() == 1 { "it" } else { "them" },
10409                absent.join(", ")
10410            ),
10411        })
10412    }
10413
10414    /// One [`graphql::CREATED_BOARD_ITEMS`] read of each of `ids`, [`COPY_DETAIL_BATCH`] to a
10415    /// request, adding to `found` the board item of every one GitHub has filed on `board`.
10416    async fn read_created_items(
10417        &self,
10418        board: &str,
10419        ids: &[NativeId],
10420        found: &mut BTreeMap<NativeId, (String, Option<String>)>,
10421    ) -> Result<(), SourceError> {
10422        for chunk in ids.chunks(COPY_DETAIL_BATCH) {
10423            let mut variables = serde_json::Map::new();
10424            for slot in 0..COPY_DETAIL_BATCH {
10425                variables.insert(
10426                    format!("id{slot}"),
10427                    json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
10428                );
10429                variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
10430            }
10431            variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
10432            variables.insert("statusName".into(), json!(STATUS_FIELD));
10433            let data = self
10434                .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
10435                .await?;
10436            for (slot, id) in chunk.iter().enumerate() {
10437                // An answer for another issue than the one asked about says nothing of
10438                // where that one is filed, and its board item is not this one's.
10439                let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
10440                let named = node.map(|node| node.get("id").and_then(Value::as_str));
10441                if let Some(named) = named
10442                    && named != Some(id.0.as_str())
10443                {
10444                    return Err(SourceError::Malformed {
10445                        message: format!(
10446                            "GitHub answered the board items of {} when asked for those of {}",
10447                            named.unwrap_or("an issue it did not name"),
10448                            id.0
10449                        ),
10450                    });
10451                }
10452                // An issue GitHub does not answer yet is not filed yet; one it answers is
10453                // read whole, its connection, each board item's board and its status, and
10454                // any of them unreadable is refused rather than read as not filed.
10455                let Some(node) = node else { continue };
10456                let malformed = |what: &str| SourceError::Malformed {
10457                    message: format!("GitHub answered the board items of {} with {what}", id.0),
10458                };
10459                let nodes = node
10460                    .pointer("/projectItems/nodes")
10461                    .and_then(Value::as_array)
10462                    .ok_or_else(|| malformed("no list of board items"))?;
10463                let mut item = None;
10464                for node in nodes {
10465                    let on = node
10466                        .pointer("/project/id")
10467                        .and_then(Value::as_str)
10468                        .ok_or_else(|| malformed("a board item naming no board"))?;
10469                    if on == board {
10470                        item = Some(node);
10471                    }
10472                }
10473                if let Some(item) = item {
10474                    let status = match item.get("status") {
10475                        None | Some(Value::Null) => None,
10476                        Some(status) => Some(
10477                            status
10478                                .get("name")
10479                                .and_then(Value::as_str)
10480                                .ok_or_else(|| malformed("a status that names no option"))?
10481                                .to_owned(),
10482                        ),
10483                    };
10484                    found.insert(
10485                        id.clone(),
10486                        (required_nonblank_str(item, "id")?.to_owned(), status),
10487                    );
10488                }
10489            }
10490        }
10491        Ok(())
10492    }
10493
10494    /// The board item id of `content`, which this process created and remembered without one,
10495    /// in every half of its own view.
10496    fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
10497        if let Some(held) = self.resolved_cache()?.get_mut(content) {
10498            held.item_id = item.to_owned();
10499        }
10500        if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
10501            held.item_id = item.to_owned();
10502        }
10503        Ok(())
10504    }
10505
10506    /// Ask together which items each of `origins` names, and what creating an item reads; see
10507    /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
10508    /// page is left to its own lookup.
10509    async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
10510        if self.repository.is_none() {
10511            return Ok(());
10512        }
10513        let unasked: Vec<&String> = {
10514            let held = self.narrowed_cache()?;
10515            let mut unasked: Vec<&String> = Vec::new();
10516            for origin in origins {
10517                if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
10518                    && !unasked.contains(&origin)
10519                {
10520                    unasked.push(origin);
10521                }
10522            }
10523            unasked
10524        };
10525        if unasked.is_empty() {
10526            return Ok(());
10527        }
10528        // One batch is held back for the read ahead that follows, which may carry a project's
10529        // members beside it in the same request.
10530        if unasked.len() <= ORIGIN_BATCH {
10531            let mut pending =
10532                self.pending_origins
10533                    .lock()
10534                    .map_err(|_| SourceError::Unavailable {
10535                        message:
10536                            "origins held for a read ahead were left inconsistent; run the command \
10537                          again"
10538                                .into(),
10539                    })?;
10540            for origin in unasked {
10541                if !pending.contains(origin) {
10542                    pending.push(origin.clone());
10543                }
10544            }
10545            return Ok(());
10546        }
10547        let unasked: Vec<String> = unasked.into_iter().cloned().collect();
10548        self.send_origin_lookups(&unasked, None).await
10549    }
10550
10551    /// Hold, for [`TaskSource::member_classifications`] of `project`, each member's
10552    /// classification and repositories as the members page `data` carries them — when that page
10553    /// is every member, each one names `project` as its parent, and every record it holds is
10554    /// readable. Any other page holds nothing, so the members are read as they always were.
10555    fn hold_member_classes(&self, project: &NativeId, data: &Value) -> Result<(), SourceError> {
10556        let Some(page) = data
10557            .pointer("/members/subIssues")
10558            .filter(|page| !page.is_null())
10559        else {
10560            return Ok(());
10561        };
10562        if page
10563            .pointer("/pageInfo/hasNextPage")
10564            .and_then(Value::as_bool)
10565            != Some(false)
10566        {
10567            return Ok(());
10568        }
10569        let Some(nodes) = page.get("nodes").and_then(Value::as_array) else {
10570            return Ok(());
10571        };
10572        let mut members = Vec::with_capacity(nodes.len());
10573        for node in nodes {
10574            if node.pointer("/parent/id").and_then(Value::as_str) != Some(project.0.as_str()) {
10575                return Ok(());
10576            }
10577            let Some(body) = node.get("body").and_then(Value::as_str) else {
10578                return Ok(());
10579            };
10580            // Every node here is filed under `project`, which `BoardKind::of` reads as one of its
10581            // tasks whatever its marker says, so each is a member the full read would classify.
10582            let Ok((_, slot)) = metadata_body(Some(body.to_owned())) else {
10583                return Ok(());
10584            };
10585            let Ok(classification) = Classification::from_metadata(&slot) else {
10586                return Ok(());
10587            };
10588            let repositories = if slot.contains_key(Repository::METADATA_KEY) {
10589                let Ok(repositories) = Repository::from_metadata(&slot) else {
10590                    return Ok(());
10591                };
10592                repositories
10593            } else {
10594                let Some(origin) = node
10595                    .pointer("/repository/nameWithOwner")
10596                    .and_then(Value::as_str)
10597                else {
10598                    return Ok(());
10599                };
10600                let Ok(own) = Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST))
10601                else {
10602                    return Ok(());
10603                };
10604                vec![own]
10605            };
10606            members.push((classification, repositories));
10607        }
10608        self.member_classes
10609            .lock()
10610            .map_err(|_| SourceError::Unavailable {
10611                message: "a project's members held for its classification were left \
10612                          inconsistent; run the command again"
10613                    .into(),
10614            })?
10615            .insert(project.clone(), members);
10616        Ok(())
10617    }
10618
10619    /// Send every origin held back for the read ahead, beside the members of `members` when it
10620    /// names a project — or nothing, when none is held.
10621    async fn send_pending_origins(&self, members: Option<&NativeId>) -> Result<(), SourceError> {
10622        let pending = std::mem::take(&mut *self.pending_origins.lock().map_err(|_| {
10623            SourceError::Unavailable {
10624                message: "origins held for a read ahead were left inconsistent; run the command \
10625                          again"
10626                    .into(),
10627            }
10628        })?);
10629        if pending.is_empty() {
10630            return Ok(());
10631        }
10632        self.send_origin_lookups(&pending, members).await
10633    }
10634
10635    /// [`graphql::ORIGIN_LOOKUPS`] of `unasked`, [`ORIGIN_BATCH`] to a request — the first one
10636    /// [`graphql::ORIGIN_LOOKUPS_WITH_MEMBERS`], carrying the members of `members`, when it names
10637    /// a project — holding each origin's answer and, from a members page that is the whole of
10638    /// them, each member's classification.
10639    async fn send_origin_lookups(
10640        &self,
10641        unasked: &[String],
10642        members: Option<&NativeId>,
10643    ) -> Result<(), SourceError> {
10644        let Some(repository) = self.repository.clone() else {
10645            return Ok(());
10646        };
10647        let mut members = members;
10648        for chunk in unasked.chunks(ORIGIN_BATCH) {
10649            let mut variables = json!({"owner":self.owner,"number":self.project_number,
10650                "type":"ISSUE","lookupFirst":ORIGIN_LOOKUPS_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
10651                "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
10652                "repositoryOwner":repository.owner,"repositoryName":repository.name});
10653            for slot in 0..ORIGIN_BATCH {
10654                let origin = chunk.get(slot).unwrap_or(&chunk[0]);
10655                variables[format!("filter{slot}")] =
10656                    json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
10657                variables[format!("search{slot}")] = json!(
10658                    self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
10659                );
10660                variables[format!("ask{slot}")] = json!(slot < chunk.len());
10661            }
10662            let project = members.take();
10663            let document = match project {
10664                Some(project) => {
10665                    variables["project"] = json!(project.0);
10666                    variables["memberFirst"] = json!(MEMBER_PAGE_SIZE);
10667                    graphql::ORIGIN_LOOKUPS_WITH_MEMBERS
10668                }
10669                None => graphql::ORIGIN_LOOKUPS,
10670            };
10671            let data = self.graphql(document, variables).await?;
10672            self.fields_read(&data)?;
10673            if let Some(project) = project {
10674                self.hold_member_classes(project, &data)?;
10675            }
10676            if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
10677                let id = required_nonblank_str(node, "id")?.to_owned();
10678                self.repository_cache()?.insert(repository.clone(), id);
10679                if let Some(numeric) = node
10680                    .get("databaseId")
10681                    .and_then(Value::as_u64)
10682                    .and_then(std::num::NonZeroU64::new)
10683                {
10684                    self.numeric_repositories
10685                        .lock()
10686                        .await
10687                        .insert(repository.clone(), numeric);
10688                }
10689            }
10690            for (slot, origin) in chunk.iter().enumerate() {
10691                let items = data
10692                    .pointer(&format!("/o{slot}/projectV2/items"))
10693                    .filter(|value| !value.is_null())
10694                    .ok_or_else(|| SourceError::Refused {
10695                        message: format!(
10696                            "GitHub project {}/{} was not found or is not visible to the token",
10697                            self.owner, self.project_number
10698                        ),
10699                    })?;
10700                let searched =
10701                    data.get(format!("s{slot}"))
10702                        .ok_or_else(|| SourceError::Malformed {
10703                            message: "GitHub search response has no search connection".into(),
10704                        })?;
10705                if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
10706                    continue;
10707                }
10708                let mut found: Vec<Resolved> = Vec::new();
10709                for item in optional_nodes(Some(items), "project items")?
10710                    .into_iter()
10711                    .flatten()
10712                {
10713                    if let Some(resolved) = self.resolve(item)?
10714                        && resolved.content_kind == ContentKind::Issue
10715                        && !found.iter().any(|held| held.id == resolved.id)
10716                    {
10717                        found.push(resolved);
10718                    }
10719                }
10720                for node in optional_nodes(Some(searched), "search")?
10721                    .into_iter()
10722                    .flatten()
10723                {
10724                    if let Some(resolved) = self.resolve_issue(node).await?
10725                        && !found.iter().any(|held| held.id == resolved.id)
10726                    {
10727                        found.push(resolved);
10728                    }
10729                }
10730                self.narrowed_cache()?
10731                    .insert(Narrowing::Origin((*origin).clone()).key(), found);
10732            }
10733        }
10734        Ok(())
10735    }
10736
10737    /// Delete every issue of `ids`, [`DELETE_BATCH`] to a request with
10738    /// [`graphql::DELETE_ISSUES`], and answer those still there: each a request's answer does not
10739    /// confirm, every one of a request refused whole included.
10740    async fn delete_issues(&self, ids: &[&NativeId]) -> Vec<NativeId> {
10741        let mut left = Vec::new();
10742        for chunk in ids.chunks(DELETE_BATCH) {
10743            let mut variables = serde_json::Map::new();
10744            for slot in 0..DELETE_BATCH {
10745                let id = chunk.get(slot).unwrap_or(&chunk[0]);
10746                variables.insert(format!("delete{slot}"), json!({"issueId": id.0}));
10747                variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
10748            }
10749            let data = self
10750                .graphql(graphql::DELETE_ISSUES, Value::Object(variables))
10751                .await
10752                .unwrap_or(Value::Null);
10753            for (slot, id) in chunk.iter().enumerate() {
10754                // Confirmed only by the repository the deleted issue was in, named by a
10755                // non-blank id: anything else says nothing of whether the issue is gone.
10756                let confirmed = data
10757                    .pointer(&format!("/delete{slot}/repository/id"))
10758                    .and_then(Value::as_str)
10759                    .is_some_and(|id| !id.trim().is_empty());
10760                if !confirmed {
10761                    left.push((*id).clone());
10762                }
10763            }
10764        }
10765        left
10766    }
10767
10768    /// Delete one issue, which takes its board item with it.
10769    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
10770        let data = self
10771            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10772            .await?;
10773        data.pointer("/deleteIssue/repository")
10774            .filter(|value| !value.is_null())
10775            .ok_or_else(|| SourceError::Malformed {
10776                message: "GitHub issue deletion returned no repository".into(),
10777            })?;
10778        self.forget(id)?;
10779        Ok(())
10780    }
10781
10782    /// Remove one item this copy created, so a copy that could not finish leaves the board
10783    /// as it found it.
10784    ///
10785    /// Deleting the issue takes its board item with it, so there is no second mutation to
10786    /// keep in step. An id the board does not hold is not an error: the item is already
10787    /// gone, which is the state this asks for. Which that is, is decided by reading the item
10788    /// by its own id — a listing of the board can still be missing an item it holds, and
10789    /// reading that as *already gone* would leave behind the very item this was asked to
10790    /// take back.
10791    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
10792        let Some(item) = self.bound_item(id).await? else {
10793            return Ok(());
10794        };
10795        if item.content_kind == ContentKind::DraftIssue {
10796            return Err(SourceError::Refused {
10797                message: format!(
10798                    "GitHub item {} is a draft, and this source removes an item by deleting \
10799                     its issue; next: remove it from the board by hand",
10800                    id.0
10801                ),
10802            });
10803        }
10804        let data = self
10805            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10806            .await?;
10807        data.pointer("/deleteIssue/repository")
10808            .filter(|value| !value.is_null())
10809            .ok_or_else(|| SourceError::Malformed {
10810                message: "GitHub issue deletion returned no repository".into(),
10811            })?;
10812        self.forget(id)?;
10813        Ok(())
10814    }
10815
10816    /// The issue a comment call on `task` is about, or `None` when this board holds no such
10817    /// task.
10818    ///
10819    /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
10820    /// read of the task cannot disagree about which ids name one: a project or a document of
10821    /// this board is not a task here either.
10822    ///
10823    /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
10824    /// issues and a draft is not one. It is refused rather than answered with an empty page,
10825    /// which would read as a task nobody has commented on yet.
10826    async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
10827        let cached = self.resolved_cache()?.get(task).cloned();
10828        let Some(item) = (match cached {
10829            Some(item) => Some(item),
10830            None => self.item_by_id(task).await?,
10831        })
10832        .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10833            return Ok(None);
10834        };
10835        if item.content_kind == ContentKind::DraftIssue {
10836            return Err(self.draft_has_no_comments(task));
10837        }
10838        Ok(Some(item.id))
10839    }
10840
10841    /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
10842    /// issues, and a draft is not one.
10843    fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
10844        SourceError::Refused {
10845            message: format!(
10846                "task {} of source {} is a draft item on the board, and GitHub keeps \
10847                 comments on issues alone, so a draft has none to read or write; next: \
10848                 convert the draft to an issue on the board, then comment on the issue it \
10849                 becomes",
10850                task.0, self.name
10851            ),
10852        }
10853    }
10854
10855    /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
10856    /// request — or `None` when this board holds no task by that id.
10857    ///
10858    /// What `task show` and a comment listing read. A draft is a task with no comments, so it
10859    /// is answered with the draft and the refusal, at the price of the draft's own read.
10860    async fn issue_detail(
10861        &self,
10862        id: &NativeId,
10863        page: &PageRequest,
10864    ) -> Result<Option<TaskDetailRead>, SourceError> {
10865        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
10866        let asked = self
10867            .graphql(
10868                graphql::ISSUE_DETAIL,
10869                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
10870                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
10871                       "duplicates":true}),
10872            )
10873            .await;
10874        let data = match asked {
10875            Ok(data) => data,
10876            Err(error) if unresolvable_node(&error) => return Ok(None),
10877            Err(error) => return Err(error),
10878        };
10879        // `node` is null for an id that names nothing, and absent only from an answer this
10880        // source cannot read — never the same thing.
10881        let node = data.get("node").ok_or_else(|| SourceError::Malformed {
10882            message: format!("GitHub answered the read of {} with no node", id.0),
10883        })?;
10884        self.detail_of(id, node, true, after).await
10885    }
10886
10887    /// Several tasks, each with the first page of its comments when `comments` is set, read
10888    /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
10889    /// order.
10890    ///
10891    /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
10892    /// one item at a time, so that id is answered as missing and the others as themselves; any
10893    /// other refusal is every id of that batch's answer.
10894    async fn issue_details(
10895        &self,
10896        ids: &[NativeId],
10897        comments: Option<&PageRequest>,
10898    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10899        let mut read = Vec::with_capacity(ids.len());
10900        for batch in ids.chunks(DETAIL_BATCH) {
10901            match self
10902                .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
10903                .await
10904            {
10905                Ok(data) => {
10906                    for (slot, id) in batch.iter().enumerate() {
10907                        // Every alias asked for is answered, null for an id naming nothing;
10908                        // one missing is an answer this source cannot read.
10909                        let read_one = match data.get(format!("i{slot}")) {
10910                            Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
10911                            None => Err(SourceError::Malformed {
10912                                message: format!(
10913                                    "GitHub answered a batch read with no item for {}",
10914                                    id.0
10915                                ),
10916                            }),
10917                        };
10918                        read.push(read_one);
10919                    }
10920                }
10921                Err(error) if unresolvable_node(&error) => {
10922                    for id in batch {
10923                        read.push(match comments {
10924                            Some(page) => self.issue_detail(id, page).await,
10925                            None => self.task_read(id).await,
10926                        });
10927                    }
10928                }
10929                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
10930            }
10931        }
10932        read
10933    }
10934
10935    /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
10936    async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
10937        Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
10938            task,
10939            comments: None,
10940        }))
10941    }
10942
10943    /// What one node a detail read reached says: the task this board holds by `id`, with the
10944    /// page of comments the node carries when `commented` — or `None` for a node that is no
10945    /// task of this board.
10946    ///
10947    /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
10948    /// and an item this process created answers from this process's own record, which a node
10949    /// read taken moments after the write can still be behind.
10950    async fn detail_of(
10951        &self,
10952        id: &NativeId,
10953        node: &Value,
10954        commented: bool,
10955        after: Option<&str>,
10956    ) -> Result<Option<TaskDetailRead>, SourceError> {
10957        if node.is_null() {
10958            return Ok(None);
10959        }
10960        let draft = optional_str(node, "__typename")? == Some("DraftIssue");
10961        // An issue answered under one id is that id's, or the answer is not one this source
10962        // can report: reporting another issue's task and comments under the qualified id asked
10963        // for would be the one wrong answer here. A draft's own read checks the same.
10964        if !draft
10965            && optional_str(node, "__typename")? == Some("Issue")
10966            && required_str(node, "id")? != id.0
10967        {
10968            return Err(SourceError::Malformed {
10969                message: format!(
10970                    "GitHub answered the read of {} with issue {}",
10971                    id.0,
10972                    required_str(node, "id")?
10973                ),
10974            });
10975        }
10976        let item = if draft {
10977            self.draft_by_id(id).await?
10978        } else {
10979            self.resolve_issue(node).await?
10980        };
10981        // A read that carried the whole of what the issue is blocked by — a copy's — is kept
10982        // for this command, so the dependency read the copy makes next is answered from it.
10983        if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
10984            self.resolved_cache()?.insert(read.id.clone(), read.clone());
10985        }
10986        let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10987            return Ok(None);
10988        };
10989        let own = self.created()?.iter().find(|own| own.id == *id).cloned();
10990        let task = own.unwrap_or(item).task()?;
10991        let comments = match (commented, draft) {
10992            (false, _) => None,
10993            (true, true) => Some(Err(self.draft_has_no_comments(id))),
10994            (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
10995        };
10996        if commented && !draft && after.is_none() {
10997            self.expected.learn_comments(id, node);
10998        }
10999        Ok(Some(TaskDetailRead { task, comments }))
11000    }
11001
11002    /// Whether the comment `comment` is one of `issue`'s own.
11003    ///
11004    /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
11005    /// comment's id and nothing else: a comment id given against the wrong task would
11006    /// otherwise change a comment on some other issue entirely. An id that names nothing, or
11007    /// names something that is not an issue comment, is a comment this task does not have —
11008    /// which is what GitHub refusing to resolve it means too.
11009    async fn comment_is_on(
11010        &self,
11011        issue: &NativeId,
11012        comment: &NativeId,
11013    ) -> Result<bool, SourceError> {
11014        Ok(self.comment_on(issue, comment).await?.is_some())
11015    }
11016
11017    /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
11018    /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
11019    async fn comment_on(
11020        &self,
11021        issue: &NativeId,
11022        comment: &NativeId,
11023    ) -> Result<Option<String>, SourceError> {
11024        // A write the store's cache expects knows which comments its issue held, and their
11025        // bodies, at the version it expects: what this read would answer.
11026        if let Some(primed) = self.expected.primed_comment(issue, comment) {
11027            return Ok(primed);
11028        }
11029        let asked = self
11030            .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
11031            .await;
11032        let data = match asked {
11033            Ok(data) => data,
11034            Err(error) if unresolvable_node(&error) => return Ok(None),
11035            Err(error) => return Err(error),
11036        };
11037        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
11038            return Ok(None);
11039        };
11040        if optional_str(node, "__typename")? != Some("IssueComment") {
11041            return Ok(None);
11042        }
11043        let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
11044            message: format!("GitHub issue comment {} names no issue", comment.0),
11045        })?;
11046        if required_str(on, "id")? != issue.0 {
11047            return Ok(None);
11048        }
11049        Ok(Some(required_str(node, "body")?.to_owned()))
11050    }
11051
11052    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
11053    async fn partition_edges(
11054        &self,
11055        near_kind: BoardKind,
11056        near_content: ContentKind,
11057        carried: Option<&[Value]>,
11058        depends_on: &[DependencyEdge],
11059    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
11060        let mut native = Vec::new();
11061        let mut fallback = Vec::new();
11062        let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
11063            .iter()
11064            .map(|edge| {
11065                // An opaque far end names another tracker, so it is never this board's own.
11066                let same_source = edge.to.designator().is_none()
11067                    && edge
11068                        .to
11069                        .source()
11070                        .is_none_or(|source| source == self.name.as_str());
11071                // A qualified id's source segment runs to its *first* colon — `GlobalId` and
11072                // `DependencyEndpoint::source` both read it that way — and a native id may hold
11073                // colons of its own, so the far end is everything after that one separator.
11074                // Splitting at the last would truncate `work:urn:task:7` to `7`.
11075                let far_id = if edge.to.is_qualified() {
11076                    edge.to
11077                        .id()
11078                        .split_once(':')
11079                        .map_or(edge.to.id(), |(_, native)| native)
11080                } else {
11081                    edge.to.id()
11082                };
11083                // One that already blocks the near issue was answered by that issue's own
11084                // read, which carried each of its blockers' kinds — an issue every one — so it
11085                // is not read again.
11086                let blocking = carried.and_then(|nodes| {
11087                    nodes
11088                        .iter()
11089                        .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
11090                });
11091                (edge, far_id, same_source, blocking)
11092            })
11093            .collect();
11094        // Every other same-source far end is read by its own id, exactly as the item it is a
11095        // far end of is: whether this board holds it is that read's answer, never a listing's.
11096        // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
11097        let mut unread: Vec<NativeId> = Vec::new();
11098        for (_, far_id, same_source, blocking) in &far_ends {
11099            let id = NativeId((*far_id).to_owned());
11100            if *same_source && blocking.is_none() && !unread.contains(&id) {
11101                unread.push(id);
11102            }
11103        }
11104        let read: BTreeMap<NativeId, Option<Resolved>> = unread
11105            .iter()
11106            .cloned()
11107            .zip(self.items_by_ids(&unread).await?)
11108            .collect();
11109        for (edge, far_id, same_source, blocking) in far_ends {
11110            let far = match (same_source, blocking) {
11111                (false, _) => None,
11112                (true, Some(node)) => Some(FarEnd {
11113                    kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
11114                        BoardKind::Document
11115                    } else {
11116                        BoardKind::Work(related_kind(node)?)
11117                    },
11118                    content_kind: ContentKind::Issue,
11119                }),
11120                (true, None) => {
11121                    let read = read
11122                        .get(&NativeId(far_id.to_owned()))
11123                        .cloned()
11124                        .flatten()
11125                        .ok_or_else(|| SourceError::Refused {
11126                            message: format!("GitHub dependency item {far_id} was not found"),
11127                        })?;
11128                    Some(FarEnd {
11129                        kind: read.kind,
11130                        content_kind: read.content_kind,
11131                    })
11132                }
11133            };
11134            let far = far.as_ref();
11135            // The caller says which kind the far end is, and this board holds the far end
11136            // itself, so a disagreement is settled here rather than stored: recorded, the
11137            // wrong kind would read back as a cross-level edge that never existed; written
11138            // natively, it would name a relationship of a different level than the caller
11139            // asked for.
11140            //
11141            // A far end this board holds as a *document* fails the same comparison and is
11142            // refused by the same sentence: `ItemKind` has no document variant because
11143            // nothing may point at one, so no caller can name it correctly and the refusal
11144            // is the only honest answer.
11145            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
11146                return Err(SourceError::Refused {
11147                    message: format!(
11148                        "GitHub dependency item {far_id} is a {} of this board, and this item \
11149                         names it as a {}; record the kind it is",
11150                        disagreeing.kind.describes(),
11151                        edge.to.kind.marker()
11152                    ),
11153                });
11154            }
11155            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
11156            // however the far end is spelled — and one classified native here would be
11157            // written nowhere at all, because a draft's native reconciliation never runs.
11158            let native_here = near_content == ContentKind::Issue
11159                && far.is_some_and(|far| {
11160                    far.content_kind == ContentKind::Issue
11161                        && BoardKind::Work(edge.to.kind) == near_kind
11162                });
11163            if native_here {
11164                native.push(far_id.to_owned());
11165            } else {
11166                fallback.push(edge.clone());
11167            }
11168        }
11169        Ok((native, fallback))
11170    }
11171
11172    async fn update_existing(
11173        &self,
11174        item: &Resolved,
11175        incoming: &Incoming<'_>,
11176        body: &Option<String>,
11177        status_target: Option<&StatusTarget>,
11178    ) -> Result<(), SourceError> {
11179        let title = incoming.written_title();
11180        // A terminal status closes the issue here, in the same mutation as its body: its board
11181        // option was selected before this, so a close never lands on an item whose board cannot
11182        // show it.
11183        let mut fields = match item.content_kind {
11184            ContentKind::DraftIssue => json!({"title":title,"body":body}),
11185            ContentKind::Issue => json!({"title":title,"body":body,
11186                                         "stateInput":state_input(status_target)}),
11187        };
11188        // Only the members whose value differs from the item as this write read it are sent,
11189        // and nothing at all when none does — whether or not the store's cache expects a
11190        // version of it. A member left out is one GitHub leaves as it is, so a person's edit
11191        // of it since that read is kept rather than overwritten with what it already held,
11192        // and a re-copy of an unchanged item costs no mutation.
11193        if let Some(members) = fields.as_object_mut() {
11194            if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
11195                members.remove("title");
11196            }
11197            let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
11198                (Some(Value::String(sent)), Some(held)) => sent == held,
11199                (Some(Value::Null), held) => held.is_none_or(str::is_empty),
11200                _ => false,
11201            };
11202            if body_unchanged {
11203                members.remove("body");
11204            }
11205            // An issue already in the state the target asks for keeps it: an open one stays
11206            // open, and a closed one whose closure reads as the target's category keeps its
11207            // reason — a duplicate's says more than this write would.
11208            let unchanged_state = match status_target {
11209                Some(StatusTarget::Terminal(_, reason)) => {
11210                    item.closed
11211                        && closed_category(item.reason.as_deref())
11212                            == closed_category(Some(reason.reason()))
11213                }
11214                Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
11215                None => true,
11216            };
11217            if unchanged_state {
11218                members.remove("stateInput");
11219            }
11220            if members.is_empty() {
11221                return Ok(());
11222            }
11223        }
11224        self.update_content(item.content_kind, &item.id, fields)
11225            .await
11226    }
11227
11228    /// Update one board item's content with exactly `fields` beside its id, through the
11229    /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
11230    /// a draft.
11231    ///
11232    /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
11233    /// is what lets a narrow write carry the one thing it changes and nothing else.
11234    async fn update_content(
11235        &self,
11236        kind: ContentKind,
11237        id: &NativeId,
11238        fields: Value,
11239    ) -> Result<(), SourceError> {
11240        let (operation, id_key, pointer) = match kind {
11241            ContentKind::DraftIssue => (
11242                graphql::UPDATE_DRAFT,
11243                "draftIssueId",
11244                "/updateProjectV2DraftIssue/draftIssue",
11245            ),
11246            ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
11247        };
11248        if kind == ContentKind::Issue && self.hold_update(id, &fields)? {
11249            return Ok(());
11250        }
11251        let mut input = fields;
11252        input[id_key] = json!(id.0);
11253        self.send_update(operation, pointer, id, input).await
11254    }
11255
11256    /// Send one item's update, `input`, as `operation`, and hold GitHub's answer at `pointer`
11257    /// to naming that item, `id`.
11258    async fn send_update(
11259        &self,
11260        operation: &str,
11261        pointer: &str,
11262        id: &NativeId,
11263        input: Value,
11264    ) -> Result<(), SourceError> {
11265        let data = self.graphql(operation, json!({"input":input})).await?;
11266        let returned = data
11267            .pointer(pointer)
11268            .ok_or_else(|| SourceError::Malformed {
11269                message: "GitHub item update returned no item".into(),
11270            })?;
11271        if required_str(returned, "id")? != id.0 {
11272            return Err(SourceError::Malformed {
11273                message: "GitHub item update returned the wrong item".into(),
11274            });
11275        }
11276        Ok(())
11277    }
11278
11279    /// Creates one issue, files it on the board, and reports what a read of it would say:
11280    /// its content id, its board item id, and the web address GitHub gave it.
11281    ///
11282    /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
11283    /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
11284    /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
11285    /// board item, and the `addProjectV2ItemById` that then had to follow was refused
11286    /// "Content already exists in this project". A terminal status is not written here:
11287    /// `finish_write` selects its option first and closes the issue after, so a close never
11288    /// lands on an item whose board cannot show it.
11289    ///
11290    /// The address and the number come back here because this is the only place either is
11291    /// known before GitHub's own board read catches up — an item this run created answers
11292    /// the reads that follow it out of the record below, and one remembered without them
11293    /// would report no location and no key for the rest of the run.
11294    async fn create_and_file_issue(
11295        &self,
11296        board_id: &str,
11297        repository: &RepositoryTarget,
11298        incoming: &Incoming<'_>,
11299        body: &Option<String>,
11300    ) -> Result<Landed, SourceError> {
11301        let repository_id = self.repository_id(repository, incoming).await?;
11302        let data = self
11303            .graphql(
11304                graphql::CREATE_ISSUE,
11305                json!({"input":{
11306                    "repositoryId":repository_id,"title":incoming.written_title(),"body":body
11307                }}),
11308            )
11309            .await?;
11310        let created = data
11311            .pointer("/createIssue/issue")
11312            .filter(|value| !value.is_null())
11313            .ok_or_else(|| SourceError::Malformed {
11314                message: "GitHub issue creation returned no issue".into(),
11315            })?;
11316        let content_id = NativeId(required_str(created, "id")?.to_owned());
11317        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
11318        // a response without it is not worth failing a landed write over — the item simply
11319        // reports no location until the board read catches up, which is what it did before.
11320        let url = optional_str(created, "url")?.map(str::to_owned);
11321        // The issue exists from here on, so an unreadable number and a refused board
11322        // filing below each try, best effort, to take it back: an issue in the repository
11323        // that is on no board is an item nobody asked for and nothing here would find again.
11324        //
11325        // Its number is optional on the same terms its address is — a landed write is not
11326        // worth failing over a member that came back missing, and such an item reports no
11327        // handle until a board read catches up. A number that is *present* and is not an
11328        // unsigned integer is still a response this source cannot read.
11329        let number = match created_issue_number(created) {
11330            Ok(number) => number,
11331            Err(error) => {
11332                let _ = self.delete_issue(&content_id).await;
11333                return Err(error);
11334            }
11335        };
11336        let added = match self
11337            .graphql(
11338                graphql::ADD_TO_BOARD,
11339                json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
11340            )
11341            .await
11342        {
11343            Ok(added) => added,
11344            Err(error) => {
11345                let _ = self.delete_issue(&content_id).await;
11346                return Err(error);
11347            }
11348        };
11349        let item = added
11350            .pointer("/addProjectV2ItemById/item")
11351            .filter(|value| !value.is_null())
11352            .ok_or_else(|| SourceError::Malformed {
11353                message: "GitHub board addition returned no project item".into(),
11354            })?;
11355        self.note_written(&content_id, url.as_deref());
11356        Ok(Landed {
11357            content_id,
11358            item_id: Some(required_str(item, "id")?.to_owned()),
11359            url,
11360            number,
11361        })
11362    }
11363
11364    /// Move one issue under the project it now belongs to, or out of the one it left.
11365    async fn reparent(
11366        &self,
11367        held: Option<NativeId>,
11368        child: &NativeId,
11369        wanted: Option<&NativeId>,
11370    ) -> Result<(), SourceError> {
11371        if held.as_ref() == wanted {
11372            return Ok(());
11373        }
11374        if let Some(held) = &held {
11375            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
11376                .await?;
11377        }
11378        if let Some(wanted) = wanted {
11379            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
11380                .await?;
11381        }
11382        Ok(())
11383    }
11384
11385    async fn sub_issue(
11386        &self,
11387        operation: &str,
11388        parent: &NativeId,
11389        child: &NativeId,
11390        root: &str,
11391    ) -> Result<(), SourceError> {
11392        let data = self
11393            .graphql(
11394                operation,
11395                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
11396            )
11397            .await?;
11398        let issue =
11399            data.pointer(&format!("/{root}/issue"))
11400                .ok_or_else(|| SourceError::Malformed {
11401                    message: "GitHub sub-issue update returned no issue".into(),
11402                })?;
11403        let sub =
11404            data.pointer(&format!("/{root}/subIssue"))
11405                .ok_or_else(|| SourceError::Malformed {
11406                    message: "GitHub sub-issue update returned no sub-issue".into(),
11407                })?;
11408        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
11409            return Err(SourceError::Malformed {
11410                message: "GitHub sub-issue update returned the wrong issues".into(),
11411            });
11412        }
11413        Ok(())
11414    }
11415
11416    /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
11417    /// whether there was one.
11418    ///
11419    /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
11420    /// relationships are not read: there is nothing a read of them could find.
11421    async fn reconcile_blocked_by(
11422        &self,
11423        content_id: &NativeId,
11424        native: &[String],
11425        issue: Issue<'_>,
11426    ) -> Result<bool, SourceError> {
11427        let current = match issue {
11428            Issue::Created => Vec::new(),
11429            Issue::Existing(Some(held)) => held
11430                .iter()
11431                .map(|far| required_str(far, "id").map(str::to_owned))
11432                .collect::<Result<Vec<_>, _>>()?,
11433            Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
11434        };
11435        let mut changed = false;
11436        for (operation, far_id) in current
11437            .iter()
11438            .filter(|id| !native.contains(id))
11439            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
11440            .chain(
11441                native
11442                    .iter()
11443                    .filter(|id| !current.contains(id))
11444                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
11445            )
11446        {
11447            let data = self
11448                .graphql(
11449                    operation,
11450                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
11451                )
11452                .await?;
11453            let root = if operation == graphql::ADD_BLOCKED_BY {
11454                "addBlockedBy"
11455            } else {
11456                "removeBlockedBy"
11457            };
11458            let issue =
11459                data.pointer(&format!("/{root}/issue"))
11460                    .ok_or_else(|| SourceError::Malformed {
11461                        message: "GitHub dependency update returned no issue".into(),
11462                    })?;
11463            let blocker = data
11464                .pointer(&format!("/{root}/blockingIssue"))
11465                .ok_or_else(|| SourceError::Malformed {
11466                    message: "GitHub dependency update returned no blocking issue".into(),
11467                })?;
11468            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
11469            {
11470                return Err(SourceError::Malformed {
11471                    message: "GitHub dependency update returned the wrong issues".into(),
11472                });
11473            }
11474            changed = true;
11475        }
11476        Ok(changed)
11477    }
11478}
11479
11480/// The board field values one write sends, in the order GitHub runs them, and the fields it
11481/// clears after them: the origin field when it moves, the `Status` option `column` names, the
11482/// priority, then each projected field.
11483fn field_writes<'a>(
11484    existing: Option<&Resolved>,
11485    origin_field: Option<&str>,
11486    origin: &str,
11487    column: Option<(String, String)>,
11488    priority: Option<&'a PriorityWrite>,
11489    projections: &'a [ProjectionWrite],
11490) -> (Vec<(String, Value)>, Vec<&'a str>) {
11491    let mut fields = Vec::new();
11492    if let Some(field_id) = origin_field
11493        && existing.map_or(!origin.is_empty(), |item| {
11494            item.origin.as_deref().unwrap_or("") != origin
11495        })
11496    {
11497        fields.push((field_id.to_owned(), json!({"text":origin})));
11498    }
11499    if let Some((field_id, option_id)) = column {
11500        fields.push((field_id, json!({"singleSelectOptionId":option_id})));
11501    }
11502    let mut clears = Vec::new();
11503    match priority {
11504        Some(PriorityWrite::Select { field, option }) => {
11505            fields.push((field.clone(), json!({"singleSelectOptionId":option})));
11506        }
11507        Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
11508        None => {}
11509    }
11510    for projection in projections {
11511        projection.join(&mut fields, &mut clears);
11512    }
11513    (fields, clears)
11514}
11515
11516/// Everything one write decided before sending anything: see
11517/// [`GitHubProjectsSource::prepare_write`].
11518struct Prepared {
11519    /// What the write lands on.
11520    destination: WriteDestination,
11521    board: BoardFields,
11522    status_target: Option<StatusTarget>,
11523    /// The `Status` field, the option and the option's name the status lands on.
11524    // 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.
11525    column: Option<(String, String, String)>,
11526    priority_write: Option<PriorityWrite>,
11527    projections: Vec<ProjectionWrite>,
11528    projected: BTreeMap<String, String>,
11529    content_kind: ContentKind,
11530    own_repository: Option<Repository>,
11531    /// The far ends of the item's native blocked-by relationships.
11532    // 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.
11533    native: Vec<String>,
11534    slot: BTreeMap<String, Value>,
11535    content: Option<String>,
11536    body: Option<String>,
11537    /// The copy origin the item records, empty for none.
11538    // 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.
11539    origin: String,
11540    origin_field: Option<String>,
11541}
11542
11543/// One write a copy held back of the kinds sent together: a board field value, or a blocked-by
11544/// edge, each its input.
11545enum HeldWrite<'a> {
11546    Field(&'a Value),
11547    Edge(&'a Value),
11548}
11549
11550/// Whether `error` is GitHub refusing to file an issue on a board because it is filed there
11551/// already — what `addProjectV2ItemById` answers once `createIssue`'s `projectV2Ids` has placed
11552/// it, rather than answering with the item it holds.
11553fn already_on_the_board(error: &SourceError) -> bool {
11554    error
11555        .to_string()
11556        .contains("Content already exists in this project")
11557}
11558
11559/// Hold every alias `inputs` sent under `prefix` in `data` to answering with what its input
11560/// named — each `(root, named)` of `answers` — and refuse one that answered nothing or another
11561/// item.
11562fn check_aliased_answers(
11563    document: &str,
11564    data: &Value,
11565    prefix: &str,
11566    inputs: &[&Value],
11567    answers: &[(&str, &str)],
11568) -> Result<(), SourceError> {
11569    for (slot, input) in inputs.iter().enumerate() {
11570        for (root, named) in answers {
11571            let answered = data
11572                .pointer(&format!("/{prefix}{slot}/{root}/id"))
11573                .and_then(Value::as_str)
11574                .ok_or_else(|| SourceError::Malformed {
11575                    message: format!(
11576                        "GitHub answered {} with nothing at {prefix}{slot}",
11577                        operation_description(document)
11578                    ),
11579                })?;
11580            let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
11581            if answered != asked {
11582                return Err(SourceError::Malformed {
11583                    message: format!(
11584                        "GitHub answered {} at {prefix}{slot} with {answered}, which is not \
11585                         {asked}, the item it was asked to write",
11586                        operation_description(document)
11587                    ),
11588                });
11589            }
11590        }
11591    }
11592    Ok(())
11593}
11594
11595/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
11596/// back — each issue of `left` it created that is still in the repository, which its caller,
11597/// never handed their ids, could not name.
11598fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
11599    if left.is_empty() {
11600        return error;
11601    }
11602    SourceError::Refused {
11603        message: format!(
11604            "{error}; GitHub then refused to take back {}, which this write created and which \
11605             are still in the repository; next: delete {} before writing again",
11606            left.join(", "),
11607            if left.len() == 1 { "it" } else { "them" }
11608        ),
11609    }
11610}
11611
11612/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
11613enum WriteDestination {
11614    /// The item as it stood, for an update.
11615    Existing(Box<Resolved>),
11616    /// Where a new item is created.
11617    New(RepositoryTarget),
11618}
11619
11620impl WriteDestination {
11621    /// The item an update writes over, as it stood; `None` for a create.
11622    fn existing(&self) -> Option<&Resolved> {
11623        match self {
11624            Self::Existing(item) => Some(item),
11625            Self::New(_) => None,
11626        }
11627    }
11628}
11629
11630/// One existing issue's update held back by [`GitHubProjectsSource::hold_update`]: the issue,
11631/// and the `UpdateIssueInput` its own [`graphql::UPDATE_ISSUE`] would have sent — the members
11632/// it writes beside that issue's id, which only [`Self::new`] sets.
11633struct HeldUpdate {
11634    issue: NativeId,
11635    // 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.
11636    input: Value,
11637}
11638
11639impl HeldUpdate {
11640    /// The update writing `members` — an `UpdateIssueInput` without its id — to `issue`.
11641    fn new(issue: &NativeId, members: &Value) -> Self {
11642        let mut input = members.clone();
11643        input["id"] = json!(issue.0);
11644        Self {
11645            issue: issue.clone(),
11646            input,
11647        }
11648    }
11649}
11650
11651/// What a copy has held back of the items it created; see
11652/// [`GitHubProjectsSource::hold_back`].
11653#[derive(Debug, Default)]
11654enum Deferred {
11655    /// Writes are being held back, and nothing has been held yet.
11656    #[default]
11657    Nothing,
11658    /// The items held back, every one of them on `board`.
11659    Held {
11660        /// The board every one of them is on.
11661        board: BoardId,
11662        /// Each created item, in the order it was created.
11663        created: Vec<HeldBack>,
11664    },
11665}
11666
11667impl Deferred {
11668    /// Whether `content` is one of the items held back.
11669    fn holds(&self, content: &NativeId) -> bool {
11670        match self {
11671            Self::Nothing => false,
11672            Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
11673        }
11674    }
11675}
11676
11677/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
11678#[derive(Debug)]
11679struct HeldBack {
11680    /// The issue.
11681    content: NativeId,
11682    /// Its board field values other than its `Status`, in the order they are written.
11683    fields: Vec<(String, Value)>,
11684    /// The `Status` field, and the option and its name that the item's status lands on.
11685    // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
11686    status: Option<(String, String, String)>,
11687    /// The fields it clears after those are written.
11688    clears: Vec<String>,
11689    /// The reason it is closed for, once its option is selected; `None` leaves it open.
11690    close: Option<ClosedState>,
11691    /// The issues it is blocked by.
11692    edges: Vec<NativeId>,
11693}
11694
11695/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
11696/// no GitHub type has a field of this name.
11697const ANSWER_ERRORS: &str = "onetaskgraphErrors";
11698
11699/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
11700/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
11701///
11702/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
11703/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
11704/// created named in the data beside it — the ids a copy needs to take them back.
11705fn answer_with_errors(body: &str) -> Option<Value> {
11706    let parsed: Value = serde_json::from_str(body).ok()?;
11707    let errors = parsed
11708        .get("errors")?
11709        .as_array()
11710        .filter(|errors| !errors.is_empty())?;
11711    let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
11712    data[ANSWER_ERRORS] = Value::Array(errors.clone());
11713    Some(data)
11714}
11715
11716/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
11717/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
11718fn filed_input(
11719    repository: &str,
11720    board: &str,
11721    incoming: &Incoming<'_>,
11722    body: &Option<String>,
11723) -> Value {
11724    let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
11725                           "body":body,"projectV2Ids":[board]});
11726    if let Some(parent) = incoming.parent {
11727        input["parentIssueId"] = json!(parent.0);
11728    }
11729    input
11730}
11731
11732/// The name of option `option` of field `field` among a board's `fields`.
11733fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
11734    fields
11735        .get("nodes")?
11736        .as_array()?
11737        .iter()
11738        .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
11739        .get("options")?
11740        .as_array()?
11741        .iter()
11742        .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
11743        .get("name")?
11744        .as_str()
11745}
11746
11747/// What a write needs to know of one far end it names: which kind of item it is, and whether
11748/// it is an issue a native relationship can name.
11749struct FarEnd {
11750    kind: BoardKind,
11751    content_kind: ContentKind,
11752}
11753
11754/// Whether the issue one write reconciles was created by that write or was already there.
11755#[derive(Clone, Copy, PartialEq, Eq)]
11756enum Issue<'a> {
11757    /// Created by this write, so it holds no relationships yet.
11758    Created,
11759    /// On the board before this write, holding whatever relationships it holds — the far
11760    /// ends of its whole `blockedBy`, when the read that reached it carried them.
11761    Existing(Option<&'a [Value]>),
11762}
11763
11764/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
11765enum Reached {
11766    /// An issue this board holds, resolved into everything this source reports about it.
11767    Held(Box<Resolved>),
11768    /// Nothing this board holds: no such node, or a node on some other board.
11769    Nothing,
11770    /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
11771    /// again by [`GitHubProjectsSource::draft_by_id`].
11772    Draft,
11773}
11774
11775/// What GitHub says when a string is not a node id it can resolve.
11776///
11777/// Matched because it is the ordinary answer to a project selector naming a project by its
11778/// *name*, and reporting that as a failure would make naming one impossible. It is read
11779/// off the refusal GitHub sent, never guessed from the shape of the string: this source
11780/// does not define the syntax of a GitHub node id and would be wrong about it.
11781const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
11782
11783/// `error` with `note` added to the end of what it says, its kind and every other member
11784/// unchanged — so a caller still branches on the failure that happened, and reads beside it
11785/// what that failure left behind.
11786fn noting(error: SourceError, note: &str) -> SourceError {
11787    match error {
11788        SourceError::Config { message } => SourceError::Config {
11789            message: message + note,
11790        },
11791        SourceError::Auth { message } => SourceError::Auth {
11792            message: message + note,
11793        },
11794        SourceError::Refused { message } => SourceError::Refused {
11795            message: message + note,
11796        },
11797        SourceError::RateLimited {
11798            retry_after_seconds,
11799            message,
11800        } => SourceError::RateLimited {
11801            retry_after_seconds,
11802            message: Some(message.unwrap_or_default() + note),
11803        },
11804        SourceError::Unavailable { message } => SourceError::Unavailable {
11805            message: message + note,
11806        },
11807        SourceError::Malformed { message } => SourceError::Malformed {
11808            message: message + note,
11809        },
11810    }
11811}
11812
11813/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
11814/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
11815fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
11816    let mut variables = serde_json::Map::new();
11817    for slot in 0..COPY_DETAIL_BATCH {
11818        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11819        variables.insert(format!("id{slot}"), json!(id));
11820    }
11821    variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
11822    variables.insert("children".to_owned(), json!(children));
11823    variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
11824    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11825    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11826    variables.insert("duplicates".to_owned(), json!(true));
11827    Value::Object(variables)
11828}
11829
11830/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
11831/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
11832/// for them.
11833///
11834/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
11835/// is read again at no added price.
11836fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
11837    let mut variables = serde_json::Map::new();
11838    for slot in 0..DETAIL_BATCH {
11839        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11840        variables.insert(format!("id{slot}"), json!(id));
11841    }
11842    variables.insert(
11843        "first".to_owned(),
11844        json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
11845    );
11846    variables.insert("comments".to_owned(), json!(comments.is_some()));
11847    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11848    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11849    variables.insert("duplicates".to_owned(), json!(true));
11850    Value::Object(variables)
11851}
11852
11853/// Whether this refusal is GitHub saying the id names no node at all.
11854fn unresolvable_node(error: &SourceError) -> bool {
11855    matches!(error, SourceError::Refused { message }
11856        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
11857}
11858
11859/// One project name, as a search qualifier which filters on it at the server.
11860///
11861/// Quoted so the whole title is one phrase rather than a bag of words, with the two
11862/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
11863/// the way it documents. A title matched here is still compared for equality afterwards:
11864/// the qualifier narrows what the server sends, and this source decides what it names.
11865fn title_qualifier(name: &str) -> String {
11866    format!("in:title {}", quoted(name))
11867}
11868
11869/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
11870/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
11871/// it documents — so a value holding a qualifier's spelling is searched for rather than
11872/// obeyed.
11873fn quoted(value: &str) -> String {
11874    let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
11875    format!("\"{escaped}\"")
11876}
11877
11878/// The search qualifier for the issues updated at or after `since`.
11879///
11880/// Written to the second, rounded down, which can only widen what the search returns.
11881fn updated_qualifier(since: DateTime<Utc>) -> String {
11882    format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
11883}
11884
11885/// The search terms that narrow a board-scoped issue search to a task query's text and
11886/// metadata predicates, or `None` when it carries neither.
11887///
11888/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
11889/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
11890/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
11891/// matches each in any field the `in:` qualifier names, so a query naming a title search and
11892/// a metadata value searches both fields for both — wider than asked, never narrower, and
11893/// every candidate is confirmed in process afterwards.
11894///
11895/// **This narrows a text search, and that is this source's declared semantics.** GitHub
11896/// matches whole tokens where a substring rule would match inside a word, so an item holding
11897/// the text only inside a longer word is not returned. A text of nothing but whitespace
11898/// matches every item, so it narrows nothing and is not sent.
11899fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
11900    let text = query
11901        .text
11902        .as_ref()
11903        .filter(|text| !text.terms.trim().is_empty());
11904    if text.is_none() && query.metadata.is_empty() {
11905        return None;
11906    }
11907    let (title, body) = match text.map(|text| text.fields) {
11908        None => (false, true),
11909        Some(TextFields::Title) => (true, !query.metadata.is_empty()),
11910        Some(TextFields::Content) => (false, true),
11911        Some(TextFields::TitleOrContent) => (true, true),
11912    };
11913    let fields = match (title, body) {
11914        (true, true) => "in:title,body",
11915        (true, false) => "in:title",
11916        _ => "in:body",
11917    };
11918    let phrases = text
11919        .map(|text| text.terms.clone())
11920        .into_iter()
11921        .chain(
11922            query
11923                .metadata
11924                .iter()
11925                .map(|wanted| as_stored(wanted.value())),
11926        )
11927        .map(|phrase| quoted(&phrase))
11928        .collect::<Vec<_>>();
11929    Some(format!("{fields} {}", phrases.join(" ")))
11930}
11931
11932/// The search terms that narrow a board-scoped issue search to a project or document query's
11933/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
11934/// carrying that text alone is sent as by [`narrowing_qualifiers`].
11935fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
11936    narrowing_qualifiers(&TaskQuery {
11937        text: text.cloned(),
11938        ..TaskQuery::default()
11939    })
11940}
11941
11942/// Refuses a project or document query's text GitHub's issue search cannot find, before
11943/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
11944/// query's.
11945fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
11946    refuse_unsearchable(&TaskQuery {
11947        text: text.cloned(),
11948        ..TaskQuery::default()
11949    })
11950}
11951
11952/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
11953/// before anything is asked of GitHub.
11954///
11955/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
11956/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
11957/// left out, the search is every issue of the board. So this source says it cannot answer
11958/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
11959/// nothing GitHub could search for, and keeps the board read it always had.
11960fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
11961    const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
11962                       letter or digit with a bounded query";
11963    if let Some(text) = &query.text
11964        && !text.terms.trim().is_empty()
11965        && !has_words(&text.terms)
11966    {
11967        return Err(SourceError::Refused {
11968            message: format!(
11969                "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
11970                text.terms
11971            ),
11972        });
11973    }
11974    if let Some(wanted) = query
11975        .metadata
11976        .iter()
11977        .find(|wanted| !has_words(wanted.value()))
11978    {
11979        return Err(SourceError::Refused {
11980            message: format!(
11981                "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
11982                wanted.value(),
11983                std::iter::once(wanted.key())
11984                    .chain(wanted.path().iter().map(String::as_str))
11985                    .collect::<Vec<_>>()
11986                    .join("/"),
11987            ),
11988        });
11989    }
11990    Ok(())
11991}
11992
11993/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
11994fn has_words(phrase: &str) -> bool {
11995    phrase.chars().any(char::is_alphanumeric)
11996}
11997
11998/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
11999///
12000/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
12001/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
12002/// which GitHub's word match would read as different words.
12003fn as_stored(value: &str) -> String {
12004    let encoded = Value::String(value.to_owned()).to_string();
12005    encoded[1..encoded.len() - 1].to_owned()
12006}
12007
12008/// The one narrower question a task query carrying a text, metadata or origin predicate is
12009/// sent as.
12010enum Narrowing {
12011    /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
12012    Origin(String),
12013    /// The board-scoped issue search narrowed by these qualifiers.
12014    Search(String),
12015}
12016
12017impl Narrowing {
12018    /// What this question is remembered under for the length of one command.
12019    fn key(&self) -> String {
12020        match self {
12021            Self::Origin(origin) => format!("origin {origin}"),
12022            Self::Search(also) => format!("search {also}"),
12023        }
12024    }
12025}
12026
12027/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
12028enum Resumed {
12029    /// It reported another page, which starts after this cursor.
12030    More(String),
12031    /// It has ended. Sending this cursor again — the page's own end when it had one, and
12032    /// otherwise the cursor it was reached from — answers an empty page, so the one document
12033    /// can go on walking the other connection.
12034    Ended(Option<String>),
12035}
12036
12037impl Resumed {
12038    /// Whether the connection has another page.
12039    const fn has_more(&self) -> bool {
12040        matches!(self, Self::More(_))
12041    }
12042
12043    /// The cursor to send this connection next.
12044    fn cursor(self) -> Option<String> {
12045        match self {
12046            Self::More(next) => Some(next),
12047            Self::Ended(last) => last,
12048        }
12049    }
12050}
12051
12052/// Where `connection`, reached from `after`, resumes — refused when it reports another page
12053/// with no cursor to it, or from a cursor that does not advance.
12054fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
12055    let info = connection
12056        .get("pageInfo")
12057        .ok_or_else(|| SourceError::Malformed {
12058            message: "GitHub connection has no pageInfo".into(),
12059        })?;
12060    let end = optional_str(info, "endCursor")?;
12061    if required_bool(info, "hasNextPage")? {
12062        let next = end.ok_or_else(|| SourceError::Malformed {
12063            message: "GitHub connection reports another page and no endCursor".into(),
12064        })?;
12065        validate_cursor_progress(after, next)?;
12066        return Ok(Resumed::More(next.to_owned()));
12067    }
12068    Ok(Resumed::Ended(
12069        end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
12070    ))
12071}
12072
12073/// The board, and every item on it this source reports.
12074#[derive(Clone)]
12075struct Board {
12076    id: String,
12077    fields: Value,
12078    items: Vec<Resolved>,
12079}
12080
12081/// What a write needs of the board and nothing more: its node id and its field
12082/// definitions, in the shape a read of the board's own `fields` gives them.
12083///
12084/// Deliberately no items. A write decides which item it writes, which parent it files
12085/// under and which far ends it names by reading each of them by its own id; this is the
12086/// half of the board those reads cannot carry, and holding no item is what keeps it from
12087/// ever being asked whether an item is there.
12088#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12089struct BoardFields {
12090    id: BoardId,
12091    fields: Value,
12092}
12093
12094/// A board's node id: what a field write and `addProjectV2ItemById` address.
12095///
12096/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
12097/// refused where it is read, and one an item names blank is read as not named at all.
12098#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
12099#[serde(try_from = "String")]
12100struct BoardId(String);
12101
12102impl TryFrom<String> for BoardId {
12103    type Error = String;
12104
12105    /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
12106    fn try_from(id: String) -> Result<Self, Self::Error> {
12107        Self::parse(&id).map_err(|error| error.to_string())
12108    }
12109}
12110
12111/// Where one write left its item, for the record the rest of the command reads it out of.
12112///
12113/// A named record rather than a tuple because the update arm and the create arm each fill
12114/// all four, and two `Option`s of different meaning side by side in a tuple are two
12115/// positions a reader has to count.
12116struct Landed {
12117    /// The issue's own node id, which is the [`NativeId`] this source reports.
12118    content_id: NativeId,
12119    /// The board item's id, which is what a field write addresses — `None` for an issue its
12120    /// create filed on the board itself, whose board item GitHub adds after answering and
12121    /// [`TaskSource::finish_writes`] reads.
12122    // 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.
12123    item_id: Option<String>,
12124    /// The web address GitHub gave the issue, when it gave one.
12125    // 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.
12126    url: Option<String>,
12127    /// The issue's number on its repository, when GitHub reported one.
12128    number: Option<u64>,
12129}
12130
12131impl BoardId {
12132    fn parse(id: &str) -> Result<Self, SourceError> {
12133        if id.trim().is_empty() {
12134            return Err(SourceError::Malformed {
12135                message: "GitHub named a board with a blank node id".into(),
12136            });
12137        }
12138        Ok(Self(id.to_owned()))
12139    }
12140
12141    fn as_str(&self) -> &str {
12142        &self.0
12143    }
12144}
12145
12146impl Board {
12147    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
12148        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
12149        let nodes = fields
12150            .get("nodes")
12151            .and_then(Value::as_array)
12152            .ok_or_else(|| SourceError::Malformed {
12153                message: "GitHub project fields.nodes is not an array".into(),
12154            })?;
12155        Ok(nodes
12156            .iter()
12157            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
12158    }
12159}
12160
12161/// One board item, resolved into everything this source reports about it.
12162///
12163/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
12164/// [`TaskSource::prime_write`] takes back — see [`expected`].
12165#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
12166struct Resolved {
12167    item_id: String,
12168    id: NativeId,
12169    content_kind: ContentKind,
12170    kind: BoardKind,
12171    title: String,
12172    body: Option<String>,
12173    /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
12174    /// that changes the slot alone has to keep byte for byte outside it.
12175    raw_body: Option<String>,
12176    status: Status,
12177    /// The name of the board `Status` option this item sits in, as the board spells it.
12178    option: Option<String>,
12179    /// What its `Priority` field says, read through this instance's mapping.
12180    priority: HeldPriority,
12181    /// Whether this item's issue is closed. A draft has no such state and is never closed.
12182    closed: bool,
12183    /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
12184    /// issue, a draft, and an issue closed before GitHub recorded reasons.
12185    #[serde(default)]
12186    reason: Option<String>,
12187    /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
12188    delivers: Vec<TaskRef>,
12189    /// Every task that delivers this one, read out of its slot. Empty for anything not a
12190    /// task.
12191    delivered_by: Vec<TaskRef>,
12192    labels: Vec<Label>,
12193    parent: Option<NativeId>,
12194    // 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.
12195    origin: Option<String>,
12196    /// What each board text field this instance projects metadata onto holds for this item,
12197    /// by field name — only the fields holding a value, an empty text included. What a write compares the
12198    /// item's metadata against, so a field already holding its value is not written again;
12199    /// never read back as metadata, which the slot alone reports.
12200    projected: BTreeMap<String, String>,
12201    /// The issue's own number on its repository, as GitHub reports it.
12202    ///
12203    /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
12204    /// declares none, and a draft is not filed in a repository to be numbered by one — and
12205    /// an issue this run created whose creating mutation answered without one, which is a
12206    /// response GitHub's own schema says cannot happen and which a landed write is not
12207    /// worth failing over. An `Issue` read off the board always has one.
12208    number: Option<u64>,
12209    url: Option<String>,
12210    created_at: Option<DateTime<Utc>>,
12211    updated_at: Option<DateTime<Utc>>,
12212    /// When the board item itself last changed — what a board field write moves — when the
12213    /// read that reached it said.
12214    item_updated_at: Option<DateTime<Utc>>,
12215    /// How many comments the issue holds, when the read that reached it said.
12216    comment_count: Option<u64>,
12217    /// How many relationship events the issue's timeline holds — every dependency, as
12218    /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
12219    /// removed — when the read that reached it said.
12220    relationship_events: Option<u64>,
12221    /// The server's clock on the response that read it — its `Date` header — when the
12222    /// response carried one.
12223    served_at: Option<DateTime<Utc>>,
12224    /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
12225    /// its labels — when the read that reached it carried all three whole.
12226    #[serde(default)]
12227    facets: Option<change_token::FacetsDigest>,
12228    own_repository: Option<Repository>,
12229    repositories: Vec<Repository>,
12230    classification: Classification,
12231    slot: BTreeMap<String, Value>,
12232    /// The node id of the board this item sits on, when the read that reached it said.
12233    board_id: Option<String>,
12234    /// The definition of every board field this item holds a value of, in the shape a read
12235    /// of the board's own `fields` gives one.
12236    ///
12237    /// Only the fields this item has a value in: a field it holds nothing of is not here,
12238    /// which says nothing about whether the board has it.
12239    fields: Vec<Value>,
12240    /// Every field the board this item sits on defines, as its own read of the board's
12241    /// `fields` gives them — when the read that reached the item carried them, which a read
12242    /// of it by its own id does. What a write of it needs of the board, then, needs no read
12243    /// of the board.
12244    board_fields: Option<Value>,
12245    /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
12246    /// selects one — when the read that reached it carried the connection to its end, which a
12247    /// read of it by its own id does for any issue blocked by no more than a page. What a
12248    /// write reconciles that relationship against, and what a read of its forward edges in
12249    /// the same command answers with.
12250    blocked_by: Option<Vec<Value>>,
12251}
12252
12253impl Resolved {
12254    /// The board this item's own read names it on, when that read named one this source can
12255    /// address.
12256    fn named_board(&self) -> Option<BoardId> {
12257        self.board_id
12258            .as_deref()
12259            .and_then(|id| BoardId::parse(id).ok())
12260    }
12261
12262    /// The board's id and every field it defines, when the read that reached this item
12263    /// carried both — which a read of it by its own id does.
12264    fn carried_board(&self) -> Option<BoardFields> {
12265        Some(BoardFields {
12266            id: self.named_board()?,
12267            fields: self.board_fields.clone()?,
12268        })
12269    }
12270
12271    /// Whether this item holds a value of the board field called `name`, and so carries
12272    /// that field's definition. `false` says nothing about whether the board has the field.
12273    fn defines(&self, name: &str) -> bool {
12274        self.fields
12275            .iter()
12276            .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
12277    }
12278
12279    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
12280    /// in a field of its own, and none of the five keys that are only an encoding.
12281    ///
12282    /// The two delivery keys are left out for every kind, not only for a task: they are
12283    /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
12284    /// document carrying one holds nothing a caller's own metadata could mean by it.
12285    fn metadata(&self) -> BTreeMap<String, Value> {
12286        let mut metadata = self.slot.clone();
12287        metadata.remove(Repository::METADATA_KEY);
12288        metadata.remove(DependencyEdge::RECORDED_KEY);
12289        metadata.remove(ItemKind::METADATA_KEY);
12290        metadata.remove(TaskRef::DELIVERS_KEY);
12291        metadata.remove(TaskRef::DELIVERED_BY_KEY);
12292        metadata.remove(Classification::METADATA_KEY);
12293        // The board field is the origin, and the body's copy of it is only a mirror for the
12294        // issue search to find: an item whose field holds none has none, whatever its body
12295        // says, so no reader ever sees two answers.
12296        metadata.remove(ORIGIN_KEY);
12297        if let Some(origin) = &self.origin {
12298            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
12299        }
12300        metadata
12301    }
12302
12303    /// Where this item is, as a link a reader can open.
12304    ///
12305    /// A board is a hosted place and every issue on it has a web address, so that address
12306    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
12307    /// of place it is, so a reader knows to open it rather than to read a file out. It
12308    /// does not replace or derive from `url`: the field goes on reporting exactly what it
12309    /// reported before, and this says what that address *is*.
12310    ///
12311    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
12312    /// rather than a third variant, which is the contract's "the source did not say". An
12313    /// issue this run created is not one of those: its address comes back from the
12314    /// creating mutation, so it is somewhere a reader can open from the moment it exists
12315    /// rather than from whenever the board read catches up.
12316    fn location(&self) -> Option<Location> {
12317        self.url.clone().map(Location::Url)
12318    }
12319
12320    /// The short handle this board's backend shows people for a task: the issue's number
12321    /// alone, as a decimal string.
12322    ///
12323    /// The number alone rather than `owner/repo#1043`, because that is the contract's
12324    /// value for this backend. A draft has no number and so no handle, which is the
12325    /// contract's *absent* rather than a handle of some other shape — and the native
12326    /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
12327    /// derives from.
12328    fn key(&self) -> Option<String> {
12329        self.number.map(|number| number.to_string())
12330    }
12331
12332    /// This item's change token — see [`change_token`] — or `None` when the read that
12333    /// reached it did not carry all six of its parts, which a draft never does, did not say
12334    /// when GitHub served it, or found it changed too recently to have settled.
12335    fn version(&self) -> Option<String> {
12336        match self.content_kind {
12337            ContentKind::Issue => {
12338                let (issue, item) = (self.updated_at?, self.item_updated_at?);
12339                let parts = change_token::Parts {
12340                    issue,
12341                    item,
12342                    comments: self.comment_count?,
12343                    relationships: self.relationship_events?,
12344                    facets: self.facets.clone()?,
12345                    repository: self.own_repository.clone()?,
12346                };
12347                change_token::settled(issue, item, self.served_at?)
12348                    .then(|| change_token::spell(&parts))
12349            }
12350            ContentKind::DraftIssue => None,
12351        }
12352    }
12353
12354    /// Whether its `Priority` field holds a value at all, mapped or not.
12355    fn holds_priority(&self) -> bool {
12356        self.priority != HeldPriority::Read(Priority::None)
12357    }
12358
12359    /// The task this item is.
12360    ///
12361    /// Fails for an item whose `Priority` field holds an option the mapping does not name:
12362    /// reading that as a level would be a guess, and reading it as `none` would let the next
12363    /// copy clear a priority a person set.
12364    fn task(&self) -> Result<Task, SourceError> {
12365        let priority = match &self.priority {
12366            HeldPriority::Read(priority) => *priority,
12367            HeldPriority::Unmapped(option) => {
12368                return Err(SourceError::Malformed {
12369                    message: format!(
12370                        "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
12371                         this source's priority_mapping does not name, so its priority cannot be \
12372                         read; next: name {option:?} under priority_mapping, or move the item to \
12373                         a mapped option",
12374                        self.id,
12375                        self.number
12376                            .map(|number| format!(" (#{number})"))
12377                            .unwrap_or_default()
12378                    ),
12379                });
12380            }
12381        };
12382        Ok(Task {
12383            version: self.version(),
12384            id: self.id.clone(),
12385            key: self.key(),
12386            title: self.title.clone(),
12387            content: self.body.clone(),
12388            status: self.status.clone(),
12389            priority,
12390            labels: self.labels.clone(),
12391            project: self.parent.clone(),
12392            url: self.url.clone(),
12393            location: self.location(),
12394            created_at: self.created_at,
12395            updated_at: self.updated_at,
12396            metadata: self.metadata(),
12397            repositories: self.repositories.clone(),
12398            delivers: self.delivers.clone(),
12399            delivered_by: self.delivered_by.clone(),
12400            classification: self.classification,
12401        })
12402    }
12403
12404    fn project(&self) -> Project {
12405        Project {
12406            version: self.version(),
12407            id: self.id.clone(),
12408            title: self.title.clone(),
12409            content: self.body.clone(),
12410            status: self.status.clone(),
12411            labels: self.labels.clone(),
12412            url: self.url.clone(),
12413            location: self.location(),
12414            created_at: self.created_at,
12415            updated_at: self.updated_at,
12416            metadata: self.metadata(),
12417            repositories: self.repositories.clone(),
12418            classification: self.classification,
12419        }
12420    }
12421
12422    /// The same issue as a document: the project it is filed under, and no status and no
12423    /// dependencies, because a document is not work.
12424    fn document(&self) -> Document {
12425        Document {
12426            version: self.version(),
12427            id: self.id.clone(),
12428            title: self.title.clone(),
12429            content: self.body.clone(),
12430            project: self.parent.clone(),
12431            labels: self.labels.clone(),
12432            url: self.url.clone(),
12433            location: self.location(),
12434            created_at: self.created_at,
12435            updated_at: self.updated_at,
12436            metadata: self.metadata(),
12437            repositories: self.repositories.clone(),
12438            classification: self.classification,
12439        }
12440    }
12441}
12442
12443/// Where one targeted update moves an item's status, and which of its two halves move.
12444struct StatusMove {
12445    /// The board the item's `Status` field is on.
12446    board: BoardId,
12447    /// The `Status` field's id.
12448    field: String,
12449    /// The option's id.
12450    option: String,
12451    /// The option's name, as the board spells it.
12452    name: String,
12453    /// What the status asks of the issue's state.
12454    target: StatusTarget,
12455    /// The status the item reads as once it is there.
12456    landed: Status,
12457    /// Which of the status's two halves differ from what the item holds.
12458    moves: Moves,
12459}
12460
12461/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
12462/// closed state of its issue, or both. A status neither half of which differs is no move at all,
12463/// and is not a value of this type.
12464#[derive(Clone, Copy, PartialEq, Eq)]
12465enum Moves {
12466    /// The option alone.
12467    Option,
12468    /// The issue's state alone: open, closed, or closed with another reason.
12469    State,
12470    /// Both.
12471    Both,
12472}
12473
12474impl Moves {
12475    /// What differs, or `None` when nothing does.
12476    const fn of(option: bool, state: bool) -> Option<Self> {
12477        match (option, state) {
12478            (true, true) => Some(Self::Both),
12479            (true, false) => Some(Self::Option),
12480            (false, true) => Some(Self::State),
12481            (false, false) => None,
12482        }
12483    }
12484
12485    /// Whether the option moves.
12486    const fn option(self) -> bool {
12487        matches!(self, Self::Option | Self::Both)
12488    }
12489
12490    /// Whether the issue's state moves.
12491    const fn state(self) -> bool {
12492        matches!(self, Self::State | Self::Both)
12493    }
12494}
12495
12496/// What one write is, and the status that comes with being it.
12497///
12498/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
12499/// status and a task or a project always has one, so "a document carrying a status" and
12500/// "a task carrying none" are states a write cannot be in rather than states every use
12501/// site below has to defend against.
12502enum Written<'a> {
12503    /// A document, which is not work and so has no status at all.
12504    Document,
12505    /// A task or a project, and the status it is being written with.
12506    Work(ItemKind, &'a Status),
12507}
12508
12509impl Written<'_> {
12510    /// Which of the board's three kinds this write is.
12511    const fn kind(&self) -> BoardKind {
12512        match self {
12513            Self::Document => BoardKind::Document,
12514            Self::Work(kind, _) => BoardKind::Work(*kind),
12515        }
12516    }
12517
12518    /// The status this write carries. A document carries none, so a write of one says
12519    /// nothing about the issue's open or closed state and selects no board `Status`
12520    /// option.
12521    const fn status(&self) -> Option<&Status> {
12522        match self {
12523            Self::Document => None,
12524            Self::Work(_, status) => Some(status),
12525        }
12526    }
12527
12528    /// The status this write carries with the kind whose half of `status_mapping` it is
12529    /// written through.
12530    const fn work_status(&self) -> Option<(ItemKind, &Status)> {
12531        match self {
12532            Self::Document => None,
12533            Self::Work(kind, status) => Some((*kind, status)),
12534        }
12535    }
12536}
12537
12538/// The item being written, in the one shape all three write methods reach.
12539struct Incoming<'a> {
12540    written: Written<'a>,
12541    /// The title a person wrote. A document's goes onto the issue with
12542    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
12543    title: &'a str,
12544    content: Option<&'a str>,
12545    assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
12546    labels: &'a [Label],
12547    metadata: &'a BTreeMap<String, Value>,
12548    repositories: &'a [Repository],
12549    /// Recorded in the slot while private, and nowhere while public.
12550    classification: Classification,
12551    parent: Option<&'a NativeId>,
12552    /// [`Task::delivers`], already checked. Empty for a project or a document, which is
12553    /// what keeps either key out of their slot.
12554    delivers: &'a [TaskRef],
12555    /// [`Task::delivered_by`], already checked. Empty for a project or a document.
12556    delivered_by: &'a [TaskRef],
12557    /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
12558    /// project, a document, and every write to an instance with no `priority_mapping` —
12559    /// which is what keeps such a write's requests exactly what they were before.
12560    priority: Option<Priority>,
12561}
12562
12563/// What one write does to one projected metadata text field, by the field's id.
12564#[derive(Debug, Clone)]
12565enum ProjectionWrite {
12566    /// Write this text.
12567    Set {
12568        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.
12569        text: String,
12570    },
12571    /// Clear the field's value.
12572    Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
12573}
12574
12575impl ProjectionWrite {
12576    /// Add this write to a field write's list of values and list of clears.
12577    fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
12578        match self {
12579            Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
12580            Self::Clear { field } => clears.push(field),
12581        }
12582    }
12583}
12584
12585/// What one write does to an item's `Priority` field.
12586enum PriorityWrite {
12587    /// Select this option of this field.
12588    Select {
12589        /// The `Priority` field's id.
12590        field: String,
12591        /// The mapped option's id.
12592        option: String,
12593    },
12594    /// Clear the field's value, which is what `none` is.
12595    Clear {
12596        /// The `Priority` field's id.
12597        field: String,
12598    },
12599}
12600
12601impl Incoming<'_> {
12602    /// The title this write puts on the issue.
12603    fn written_title(&self) -> String {
12604        match self.written {
12605            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
12606            Written::Work(..) => self.title.to_owned(),
12607        }
12608    }
12609}
12610
12611#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12612enum ContentKind {
12613    DraftIssue,
12614    Issue,
12615}
12616
12617/// What one board issue is: a document, or the work an [`ItemKind`] names.
12618///
12619/// A type of this source's own rather than an `ItemKind` with a third variant, because
12620/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
12621/// document — the contract keeps a document out of that enum deliberately. Holding the
12622/// board's three answers in one value is what makes every place that asks "which is this?"
12623/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
12624/// two thirds of the board.
12625#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12626enum BoardKind {
12627    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
12628    Document,
12629    /// Every other issue, and every draft.
12630    Work(ItemKind),
12631}
12632
12633impl BoardKind {
12634    /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
12635    /// sub-issues and carrying the kind marker `marked` in its slot.
12636    ///
12637    /// The design prefix is read *first*, before either of the two rules that separate a
12638    /// project from a task. A document is not work whatever sub-issues it has and whatever
12639    /// marker it carries, and reading the prefix later would make a design issue with none of
12640    /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
12641    /// overrides it: an issue filed under a project is that project's task even when it has
12642    /// sub-issues of its own.
12643    fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
12644        if title.starts_with(DESIGN_TITLE_PREFIX) {
12645            Self::Document
12646        } else if filed {
12647            Self::Work(ItemKind::Task)
12648        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
12649            Self::Work(ItemKind::Project)
12650        } else {
12651            Self::Work(ItemKind::Task)
12652        }
12653    }
12654
12655    /// What the change token's facets call this kind.
12656    const fn facet(self) -> change_token::FacetKind {
12657        match self {
12658            Self::Document => change_token::FacetKind::Document,
12659            Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
12660            Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
12661        }
12662    }
12663
12664    /// Whose half of `status_mapping` an item of this kind reads its status through. A
12665    /// document has no status of its own, so the task half stands in for whatever the issue
12666    /// holds; nothing reports it.
12667    const fn status_kind(self) -> ItemKind {
12668        match self {
12669            Self::Document => ItemKind::Task,
12670            Self::Work(kind) => kind,
12671        }
12672    }
12673
12674    /// How a refusal names this kind to the person reading it.
12675    const fn describes(self) -> &'static str {
12676        match self {
12677            Self::Document => "document",
12678            Self::Work(kind) => kind.marker(),
12679        }
12680    }
12681}
12682
12683/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
12684///
12685/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
12686/// the shared cross-source journeys assert one answer to one question, so two sources
12687/// that disagree about what "carries the label bug" means fail them.
12688fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
12689    let holds = |name: &String| {
12690        labels
12691            .iter()
12692            .any(|label| label.name.eq_ignore_ascii_case(name))
12693    };
12694    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
12695        && filter.all_of.iter().all(holds)
12696        && !filter.none_of.iter().any(holds)
12697}
12698
12699/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
12700/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
12701fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
12702    statuses.is_empty() || statuses.contains(&category)
12703}
12704
12705/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
12706///
12707/// `content` is the item's own prose — the body with this source's trailing metadata
12708/// comment already taken off — so a search never matches an encoding the author of the
12709/// issue never wrote.
12710fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
12711    let terms = query.terms.to_lowercase();
12712    let in_title = title.to_lowercase().contains(&terms);
12713    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
12714    match query.fields {
12715        TextFields::Title => in_title,
12716        TextFields::Content => in_content,
12717        TextFields::TitleOrContent => in_title || in_content,
12718    }
12719}
12720
12721/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
12722///
12723/// The project predicate is passed separately because a read narrowed to one project has
12724/// already answered it by asking *that project* for its own items — and re-applying it
12725/// there would compare the caller's selector, which may be a project's **name**, against
12726/// the id of the project that name resolved to, and keep nothing. Every other read passes
12727/// `query.project` and applies it here, which is what keeps `projects` a predicate this
12728/// source really does apply.
12729fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
12730    labels_match(&task.labels, &query.labels)
12731        && status_matches(task.status.category, &query.statuses)
12732        && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
12733        && match project {
12734            ProjectFilter::Any => true,
12735            ProjectFilter::Orphans => task.project.is_none(),
12736            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
12737        }
12738        && query
12739            .text
12740            .as_ref()
12741            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
12742        // Against the parsed metadata slot, and against the origin field, which is where
12743        // `Resolved::metadata` reads each of them from.
12744        && query.metadata_matches(&task.metadata)
12745        && query.origin_matches(&task.metadata)
12746}
12747
12748fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
12749    labels_match(&project.labels, &query.labels)
12750        && status_matches(project.status.category, &query.statuses)
12751        && query
12752            .text
12753            .as_ref()
12754            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
12755}
12756
12757/// The same three predicates a task query carries, minus the status filter.
12758///
12759/// A document is not work, so it has no status for one to compare against and the query
12760/// type carries none. The project predicate is the same one — a design issue filed under a
12761/// project issue is in that project, and one filed under nothing is in none — so it is
12762/// spelled the same way here rather than answered differently.
12763fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
12764    labels_match(&document.labels, &query.labels)
12765        && match project {
12766            ProjectFilter::Any => true,
12767            ProjectFilter::Orphans => document.project.is_none(),
12768            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
12769        }
12770        && query
12771            .text
12772            .as_ref()
12773            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
12774}
12775
12776/// Why a GitHub item's change token does not vouch for its forward edges.
12777///
12778/// A forward edge names its far end and that far end's kind, and the near item's token moves
12779/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
12780/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
12781/// nothing the near token reads, so an edge kept at the near version could name a far end that
12782/// is no longer what it said.
12783const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
12784     whether it became a document, or its deletion, so its forward edges are always read from \
12785     github";
12786
12787/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
12788/// categories an open issue reads as narrows a board-scoped search with.
12789const OPEN_QUALIFIER: &str = "is:open";
12790
12791/// The board filter `task status reconcile` reads the board through: closed items alone.
12792const CLOSED_FILTER: &str = "is:closed";
12793
12794/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
12795/// one reference, and a slot the batch has none for names its last one again.
12796fn reference_batch(batch: &[IssueRef]) -> Value {
12797    let mut variables = serde_json::Map::new();
12798    for slot in 0..REFERENCE_BATCH {
12799        let issue = batch.get(slot).or(batch.last());
12800        variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
12801        variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
12802        variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
12803    }
12804    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12805    variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
12806    variables.insert("duplicates".to_owned(), json!(true));
12807    Value::Object(variables)
12808}
12809
12810/// One issue an issue read by reference answered, in the shape every other read of an issue
12811/// hands its resolver: each board membership's `Status` value, read by name, as the one field
12812/// value that membership carries.
12813///
12814/// A membership's `status` is `null` when its card sits at no option — or on a board with no
12815/// field of that name — and otherwise an option value naming its option; anything else is not
12816/// an answer to this read, and is refused rather than read as a card at no option.
12817fn held_issue(issue: &Value) -> Result<Value, SourceError> {
12818    let malformed = |what: &str| SourceError::Malformed {
12819        message: format!("GitHub answered an issue read by reference with {what}"),
12820    };
12821    let mut issue = issue.clone();
12822    let nodes = issue
12823        .pointer_mut("/projectItems/nodes")
12824        .and_then(Value::as_array_mut)
12825        .ok_or_else(|| malformed("no list of board memberships"))?;
12826    for node in nodes {
12827        let node = node
12828            .as_object_mut()
12829            .ok_or_else(|| malformed("a board membership that is not an object"))?;
12830        let status = match node.get("status") {
12831            None => return Err(malformed("a board membership with no status member")),
12832            Some(Value::Null) => None,
12833            Some(status) if status.get("name").is_some_and(Value::is_string) => {
12834                Some(status.clone())
12835            }
12836            // A single-select value of another field type answers `{}`; only an option value
12837            // carries a name.
12838            Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
12839            Some(_) => return Err(malformed("a membership status that names no option")),
12840        };
12841        node.insert(
12842            "fieldValues".to_owned(),
12843            json!({"nodes": status.into_iter().collect::<Vec<_>>(),
12844                   "pageInfo": {"hasNextPage": false}}),
12845        );
12846    }
12847    Ok(issue)
12848}
12849
12850/// The issue one slot of a read by reference answered, `None` when GitHub answered that
12851/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
12852/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
12853/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
12854/// than a reference nothing holds.
12855fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
12856    let alias = format!("r{slot}");
12857    let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
12858        message: format!("GitHub answered a read by reference with no {alias}"),
12859    })?;
12860    if repository.is_null() {
12861        return Ok(None);
12862    }
12863    let issue = repository
12864        .get("issue")
12865        .ok_or_else(|| SourceError::Malformed {
12866            message: format!("GitHub answered {alias} of a read by reference with no issue"),
12867        })?;
12868    if issue.is_null() {
12869        return Ok(None);
12870    }
12871    // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
12872    if optional_str(issue, "__typename")? != Some("Issue") {
12873        return Err(SourceError::Malformed {
12874            message: format!(
12875                "GitHub answered {alias} of a read by reference with an issue whose __typename \
12876                 is not Issue"
12877            ),
12878        });
12879    }
12880    Ok(Some(issue))
12881}
12882
12883/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
12884fn spoken_reason(reason: &str) -> String {
12885    reason.to_ascii_lowercase().replace('_', " ")
12886}
12887
12888impl GitHubProjectsSource {
12889    /// Every task and project of this board whose card contradicts its issue's closure, each
12890    /// judged against its own kind's half of `status_mapping`, read through the board's own
12891    /// `is:closed` filter, page by page — never the board's unfiltered `items`.
12892    ///
12893    /// Each item the filter answers is confirmed from its own node: an open one, one whose
12894    /// closure reads `unknown`, and one whose card is already in line are left out whatever
12895    /// the filter let through. The board's fields ride along and are held for the option
12896    /// writes that follow, so those need no read of the board.
12897    async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
12898        let mut after: Option<String> = None;
12899        let mut found = Vec::new();
12900        loop {
12901            let data = self
12902                .graphql(
12903                    graphql::CLOSED_ITEMS,
12904                    json!({"owner":self.owner,"number":self.project_number,
12905                           "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
12906                           "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
12907                )
12908                .await?;
12909            let board = data
12910                .pointer("/closedItems/projectV2")
12911                .filter(|value| !value.is_null())
12912                .ok_or_else(|| SourceError::Refused {
12913                    message: format!(
12914                        "GitHub project {}/{} was not found or is not visible to the token",
12915                        self.owner, self.project_number
12916                    ),
12917                })?;
12918            *self.fields_cache()? = Some(BoardFields {
12919                id: BoardId::parse(required_str(board, "id")?)?,
12920                fields: board.get("fields").cloned().unwrap_or(Value::Null),
12921            });
12922            let items = board.get("items").ok_or_else(|| SourceError::Malformed {
12923                message: "GitHub project closed items are missing".into(),
12924            })?;
12925            for item in items
12926                .get("nodes")
12927                .and_then(Value::as_array)
12928                .ok_or_else(|| SourceError::Malformed {
12929                    message: "GitHub project items.nodes is not an array".into(),
12930                })?
12931            {
12932                let Some(resolved) = self.resolve(item)? else {
12933                    continue;
12934                };
12935                let BoardKind::Work(kind) = resolved.kind else {
12936                    continue;
12937                };
12938                let Some((closed_as, name)) = self.statuses.contradicted(
12939                    kind,
12940                    resolved.option.as_deref(),
12941                    resolved.closed,
12942                    resolved.reason.as_deref(),
12943                ) else {
12944                    continue;
12945                };
12946                found.push(StatusContradiction {
12947                    closed_as,
12948                    to: name.to_owned(),
12949                    option: resolved.option.clone(),
12950                    item: match kind {
12951                        ItemKind::Task => ContradictedItem::Task(resolved.task()?),
12952                        ItemKind::Project => ContradictedItem::Project(resolved.project()),
12953                    },
12954                });
12955            }
12956            let info = items
12957                .get("pageInfo")
12958                .ok_or_else(|| SourceError::Malformed {
12959                    message: "GitHub project items have no pageInfo".into(),
12960                })?;
12961            if !required_bool(info, "hasNextPage")? {
12962                return Ok(found);
12963            }
12964            let next = required_str(info, "endCursor")?;
12965            validate_cursor_progress(after.as_deref(), next)?;
12966            after = Some(next.to_owned());
12967        }
12968    }
12969
12970    /// What this board holds for each issue in `issues`, once each in the order given, read
12971    /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
12972    ///
12973    /// An issue GitHub cannot find for this source's credential, and one with no item on this
12974    /// board, are both absent. A closure that already says the work will not be done here —
12975    /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
12976    /// completed whose card sits at its kind's `done` option is unchanged, and every other item
12977    /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
12978    /// tasks as sub-issues — is held under exactly these rules, judged against the project
12979    /// kind's half of `status_mapping`.
12980    async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
12981        let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
12982        for issue in issues {
12983            if !distinct.contains(issue) {
12984                distinct.push(issue.clone());
12985            }
12986        }
12987        let done = |kind: ItemKind| {
12988            self.statuses
12989                .target(kind, StatusCategory::Done)
12990                .option()
12991                .unwrap_or("Done")
12992                .to_owned()
12993        };
12994        // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
12995        // its range names no issue any repository can hold: it is answered absent and never sent,
12996        // where binding it would have GitHub refuse the whole batch.
12997        let readable: Vec<IssueRef> = distinct
12998            .iter()
12999            .filter(|issue| i32::try_from(issue.number).is_ok())
13000            .cloned()
13001            .collect();
13002        let mut holdings = Vec::with_capacity(readable.len());
13003        for batch in readable.chunks(REFERENCE_BATCH) {
13004            let data = self
13005                .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
13006                .await?;
13007            for (slot, issue) in batch.iter().enumerate() {
13008                let node = referenced_issue(&data, slot)?;
13009                let resolved = match node {
13010                    Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
13011                    None => None,
13012                };
13013                // A task and a project are both held; a document is not work, so nothing is.
13014                let Some((item, kind)) = resolved.and_then(|item| match item.kind {
13015                    BoardKind::Work(kind) => Some((item, kind)),
13016                    BoardKind::Document => None,
13017                }) else {
13018                    holdings.push(IssueHolding {
13019                        issue: issue.clone(),
13020                        completion: Completion::Absent,
13021                    });
13022                    continue;
13023                };
13024                let task = match kind {
13025                    ItemKind::Task => {
13026                        let held = item.task()?;
13027                        HeldTask {
13028                            id: held.id,
13029                            kind,
13030                            option: item.option.clone(),
13031                            classification: held.classification,
13032                            repositories: held.repositories,
13033                            delivers: held.delivers,
13034                        }
13035                    }
13036                    // A project delivers no task, so what its write keeps in step is nothing.
13037                    ItemKind::Project => {
13038                        let held = item.project();
13039                        HeldTask {
13040                            id: held.id,
13041                            kind,
13042                            option: item.option.clone(),
13043                            classification: held.classification,
13044                            repositories: held.repositories,
13045                            delivers: Vec::new(),
13046                        }
13047                    }
13048                };
13049                let done = done(kind);
13050                let completion = match (item.closed, item.reason.as_deref()) {
13051                    (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
13052                        task,
13053                        reason: format!("closed as {}", spoken_reason(reason)),
13054                    },
13055                    (true, None | Some("COMPLETED"))
13056                        if item
13057                            .option
13058                            .as_deref()
13059                            .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
13060                    {
13061                        Completion::Unchanged { task }
13062                    }
13063                    _ => {
13064                        let kept = item.clone();
13065                        self.held_cache
13066                            .lock()
13067                            .map_err(|_| SourceError::Unavailable {
13068                                message: "items read by issue reference were left \
13069                                          inconsistent; run the command again"
13070                                    .into(),
13071                            })?
13072                            .insert(kept.id.clone(), kept);
13073                        Completion::Due {
13074                            task,
13075                            to: done.clone(),
13076                        }
13077                    }
13078                };
13079                holdings.push(IssueHolding {
13080                    issue: issue.clone(),
13081                    completion,
13082                });
13083            }
13084        }
13085        let mut read = holdings.into_iter();
13086        let mut answered = Vec::with_capacity(distinct.len());
13087        for issue in distinct {
13088            if i32::try_from(issue.number).is_ok() {
13089                answered.push(read.next().ok_or_else(|| SourceError::Malformed {
13090                    message: format!("no holding was read for {issue}"),
13091                })?);
13092            } else {
13093                answered.push(IssueHolding {
13094                    issue,
13095                    completion: Completion::Absent,
13096                });
13097            }
13098        }
13099        Ok(answered)
13100    }
13101}
13102
13103#[async_trait::async_trait]
13104impl TaskSource for GitHubProjectsSource {
13105    fn kind(&self) -> &'static str {
13106        KIND
13107    }
13108    fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
13109        change_token::order(a, b)
13110    }
13111    /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
13112    /// member, the change each part of the token moves for. Not its forward edges — see
13113    /// [`FAR_ENDS_UNCOVERED`].
13114    fn version_coverage(&self) -> VersionCoverage {
13115        VersionCoverage {
13116            forward_edges: Coverage::Uncovered {
13117                reason: FAR_ENDS_UNCOVERED.to_owned(),
13118            },
13119            ..VersionCoverage::whole()
13120        }
13121    }
13122    /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
13123    /// and for a far end deleted, so the token sees every change to a task's edges but one — a
13124    /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
13125    /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
13126    /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
13127    // 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.
13128    fn copy_version_coverage(&self) -> VersionCoverage {
13129        VersionCoverage::whole()
13130    }
13131    /// A project's fields, and not its forward edges, for the same reason a task's are not.
13132    fn project_version_coverage(&self) -> ProjectVersionCoverage {
13133        ProjectVersionCoverage {
13134            fields: Coverage::Covered,
13135            forward_edges: Coverage::Uncovered {
13136                reason: FAR_ENDS_UNCOVERED.to_owned(),
13137            },
13138        }
13139    }
13140    /// A document's fields and the manifest of its image assets, which its metadata slot
13141    /// records: both are the issue's body, whose every edit moves the token's issue time.
13142    fn document_version_coverage(&self) -> DocumentVersionCoverage {
13143        DocumentVersionCoverage::whole()
13144    }
13145    fn write_context(&self, id: &NativeId) -> Option<Value> {
13146        self.context_of(id)
13147    }
13148    fn board_context(&self) -> Option<Value> {
13149        self.board_context_held()
13150    }
13151    fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
13152        self.prime(id, context, board)
13153    }
13154    fn unprime(&self, id: &NativeId) {
13155        self.unprime_held(id);
13156    }
13157    fn begin_journal(&self) {
13158        self.expected.begin();
13159    }
13160    fn end_journal(&self) -> Option<WriteJournal> {
13161        Some(self.expected.end())
13162    }
13163    fn capabilities(&self) -> Capabilities {
13164        Capabilities {
13165            projects: Support::Native,
13166            documents: Support::Native,
13167            comments: Support::Native,
13168            assets: Support::Native,
13169            priority: if self.priorities.is_some() {
13170                Support::Native
13171            } else {
13172                Support::Unsupported
13173            },
13174            filter_by_priority: Support::Native,
13175            filter_by_comment_activity: Support::Native,
13176            filter_by_metadata: Support::Native,
13177            filter_by_origin: Support::Native,
13178            reconcile_status: Support::Native,
13179            complete_by_issue: Support::Native,
13180            orphan_tasks: Support::Native,
13181            filter_by_label: Support::Native,
13182            filter_by_status: Support::Native,
13183            search_title: Support::Native,
13184            search_content: Support::Native,
13185            task_dependencies: DependencySupport::BothDirections,
13186            project_dependencies: DependencySupport::BothDirections,
13187            max_page_size: MAX_PAGE_SIZE,
13188        }
13189    }
13190    async fn visibility(
13191        &self,
13192        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
13193    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
13194        self.write_visibility(target).await
13195    }
13196    async fn health(&self) -> Result<Health, SourceError> {
13197        let board = self.board_page(None, 1).await?;
13198        Ok(Health {
13199            reachable: true,
13200            detail: Some(format!(
13201                "reading GitHub project {}/{} ({})",
13202                self.owner,
13203                self.project_number,
13204                required_str(&board, "title")?
13205            )),
13206        })
13207    }
13208    /// The board's own node id, digested: the one request reading its fields, or none when
13209    /// this command has read them already.
13210    async fn designator(&self) -> Result<Option<Designator>, SourceError> {
13211        let board = self.board_fields().await?;
13212        Designator::of_backend(KIND, board.id.as_str())
13213            .map(Some)
13214            .map_err(|message| SourceError::Malformed { message })
13215    }
13216    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
13217        self.item_by_id(id)
13218            .await?
13219            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13220            .map(|item| item.task())
13221            .transpose()
13222    }
13223    async fn task_assets(
13224        &self,
13225        id: &NativeId,
13226    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13227        self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
13228    }
13229    async fn task_asset(
13230        &self,
13231        id: &NativeId,
13232        name: &onetaskgraph_plugin_api::AssetName,
13233    ) -> Result<Option<Vec<u8>>, SourceError> {
13234        self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
13235            .await
13236    }
13237    async fn set_task_rendering_with_assets(
13238        &self,
13239        id: &NativeId,
13240        content: &str,
13241        provenance: &Value,
13242        _answers: &BTreeMap<String, Value>,
13243        assets: &onetaskgraph_plugin_api::AssetWrite,
13244    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13245        self.replace_rendering(
13246            id,
13247            BoardKind::Work(ItemKind::Task),
13248            content,
13249            provenance,
13250            Some(assets),
13251        )
13252        .await
13253    }
13254    async fn document_assets(
13255        &self,
13256        id: &NativeId,
13257    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
13258        self.held_assets(id, BoardKind::Document).await
13259    }
13260    async fn document_asset(
13261        &self,
13262        id: &NativeId,
13263        name: &onetaskgraph_plugin_api::AssetName,
13264    ) -> Result<Option<Vec<u8>>, SourceError> {
13265        self.held_asset(id, BoardKind::Document, name).await
13266    }
13267    async fn set_document_rendering_with_assets(
13268        &self,
13269        id: &NativeId,
13270        content: &str,
13271        provenance: &Value,
13272        _answers: &BTreeMap<String, Value>,
13273        assets: &onetaskgraph_plugin_api::AssetWrite,
13274    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
13275        self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
13276            .await
13277    }
13278    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
13279        Ok(self
13280            .item_by_id(id)
13281            .await?
13282            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13283            .map(|item| item.project()))
13284    }
13285    async fn query_tasks(
13286        &self,
13287        query: &TaskQuery,
13288        page: &PageRequest,
13289    ) -> Result<Page<Task>, SourceError> {
13290        validate_page(page)?;
13291        refuse_unsearchable(query)?;
13292        // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
13293        // category — `unknown`; every other category reads only off an open issue or a draft. So
13294        // a status filter naming none of those three asks every issue search it sends for open
13295        // issues alone; each candidate's category is still confirmed from its own item below.
13296        let open_only = !query.statuses.is_empty()
13297            && query.statuses.iter().all(|category| {
13298                !matches!(
13299                    category,
13300                    StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
13301                )
13302            });
13303        if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
13304            let mut qualifiers: Vec<String> = [
13305                query.commented_since.map(updated_qualifier),
13306                narrowing_qualifiers(query),
13307            ]
13308            .into_iter()
13309            .flatten()
13310            .collect();
13311            // A narrowed search never answers a draft, which is no issue, so narrowing it to
13312            // open issues too leaves out nothing such a search could have answered.
13313            if !qualifiers.is_empty() {
13314                if open_only {
13315                    qualifiers.push(OPEN_QUALIFIER.to_owned());
13316                }
13317                return self.search_tasks(query, page, &qualifiers.join(" ")).await;
13318            }
13319        }
13320
13321        // A read narrowed to one project asks that project for its own tasks, so nothing
13322        // about it costs what the rest of the board holds. A read carrying a text, metadata
13323        // or origin predicate asks GitHub the narrower question those predicates are, and a
13324        // read narrowed to comment activity alone asks the board's own issue search for the
13325        // issues updated since, which is every issue a comment could have been written or
13326        // edited on since. Every other task read is a question about the whole board and is
13327        // answered by reading it.
13328        let (held, membership) = match (&query.project, query.commented_since) {
13329            (ProjectFilter::Is(project), _) => (
13330                match self.held_task_children(project)? {
13331                    // Taken as `project_children` takes the project's own sub-issue read: with
13332                    // every item this process wrote under it since, and none it moved away.
13333                    Some(held) => self.completed_with_written(
13334                        held.into_iter()
13335                            .filter(|child| child.parent.as_ref() == Some(project))
13336                            .collect(),
13337                        |own| own.parent.as_ref() == Some(project),
13338                    )?,
13339                    None => self.project_children(project).await?,
13340                },
13341                // Answered by where these items came from; see `task_matches`.
13342                &ProjectFilter::Any,
13343            ),
13344            (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
13345                match (self.narrowed(query).await?, since) {
13346                    (Some(narrowed), _) => (narrowed, &query.project),
13347                    (None, Some(since)) => (self.updated_since(since).await?, &query.project),
13348                    // The board's own items list every draft; only the search half, which
13349                    // completes them with issues they lag behind on, is narrowed — a closed
13350                    // issue that half leaves out is at no category the filter keeps.
13351                    (None, None) if open_only => (
13352                        self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
13353                        &query.project,
13354                    ),
13355                    (None, None) => (self.board().await?.items, &query.project),
13356                }
13357            }
13358        };
13359        // Filtered before paged: a page of a filtered result is a page of the survivors,
13360        // never the survivors of a page.
13361        let mut tasks = Vec::new();
13362        for item in held
13363            .iter()
13364            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13365        {
13366            let task = item.task()?;
13367            if task_matches(&task, query, membership)
13368                && self.commented_since(item, query.commented_since).await?
13369            {
13370                tasks.push(task);
13371            }
13372        }
13373        Ok(offset_page(
13374            tasks,
13375            numeric_cursor(page.cursor.as_ref())?,
13376            page.limit.min(MAX_PAGE_SIZE) as usize,
13377        ))
13378    }
13379    async fn query_projects(
13380        &self,
13381        query: &ProjectQuery,
13382        page: &PageRequest,
13383    ) -> Result<Page<Project>, SourceError> {
13384        validate_page(page)?;
13385        refuse_unsearchable_text(query.text.as_ref())?;
13386        // The projects a board holds are found by an issue search scoped to that board,
13387        // never by walking the board's own item connection: what tells a project from a
13388        // task is the `parent` each issue carries, which costs nothing to read. A query
13389        // carrying a text asks that search for the text too, so it reads the issues that
13390        // hold it rather than every issue of the board.
13391        let held = match self.text_searched(query.text.as_ref()).await? {
13392            Some(searched) => searched,
13393            None => self.board_issues().await?,
13394        };
13395        let projects = held
13396            .iter()
13397            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
13398            .map(Resolved::project)
13399            .filter(|project| project_matches(project, query))
13400            .collect();
13401        Ok(offset_page(
13402            projects,
13403            numeric_cursor(page.cursor.as_ref())?,
13404            page.limit.min(MAX_PAGE_SIZE) as usize,
13405        ))
13406    }
13407    async fn query_projects_by_origin(
13408        &self,
13409        origin: &str,
13410        page: &PageRequest,
13411    ) -> Result<Page<Project>, SourceError> {
13412        validate_page(page)?;
13413        let projects = self
13414            .copied_from(origin, BoardKind::Work(ItemKind::Project))
13415            .await?
13416            .iter()
13417            .map(Resolved::project)
13418            .collect();
13419        Ok(offset_page(
13420            projects,
13421            numeric_cursor(page.cursor.as_ref())?,
13422            page.limit.min(MAX_PAGE_SIZE) as usize,
13423        ))
13424    }
13425    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
13426        let Some(item) = self
13427            .document_by_id(id)
13428            .await?
13429            .filter(|item| item.kind == BoardKind::Document)
13430        else {
13431            return Ok(None);
13432        };
13433        self.expected.read_document(&item);
13434        Ok(Some(item.document()))
13435    }
13436    async fn query_documents_by_origin(
13437        &self,
13438        origin: &str,
13439        page: &PageRequest,
13440    ) -> Result<Page<Document>, SourceError> {
13441        validate_page(page)?;
13442        let documents = self
13443            .copied_from(origin, BoardKind::Document)
13444            .await?
13445            .iter()
13446            .map(Resolved::document)
13447            .collect();
13448        Ok(offset_page(
13449            documents,
13450            numeric_cursor(page.cursor.as_ref())?,
13451            page.limit.min(MAX_PAGE_SIZE) as usize,
13452        ))
13453    }
13454    async fn query_documents(
13455        &self,
13456        query: &DocumentQuery,
13457        page: &PageRequest,
13458    ) -> Result<Page<Document>, SourceError> {
13459        validate_page(page)?;
13460        // Narrowed to one project, this is the same sub-issue read a task list scoped to
13461        // that project makes — a document filed under a project is a sub-issue of it too,
13462        // and which of them come back is the kind this caller asked for. Unscoped, a query
13463        // carrying a text asks the board-scoped issue search for it, as a task query does,
13464        // and only one carrying none reads the board.
13465        let (held, membership) = match &query.project {
13466            ProjectFilter::Is(project) => (
13467                self.project_children(project).await?,
13468                // Answered by where these items came from; see `task_matches`.
13469                &ProjectFilter::Any,
13470            ),
13471            ProjectFilter::Any | ProjectFilter::Orphans => {
13472                refuse_unsearchable_text(query.text.as_ref())?;
13473                match self.text_searched(query.text.as_ref()).await? {
13474                    Some(searched) => (searched, &query.project),
13475                    None => (self.board().await?.items, &query.project),
13476                }
13477            }
13478        };
13479        // Filtered before paged, exactly as a task read is: a page of a filtered result is
13480        // a page of the survivors, never the survivors of a page.
13481        let documents = held
13482            .iter()
13483            .filter(|item| item.kind == BoardKind::Document)
13484            .map(Resolved::document)
13485            .filter(|document| document_matches(document, query, membership))
13486            .collect();
13487        Ok(offset_page(
13488            documents,
13489            numeric_cursor(page.cursor.as_ref())?,
13490            page.limit.min(MAX_PAGE_SIZE) as usize,
13491        ))
13492    }
13493    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
13494        validate_page(page)?;
13495        let offset = numeric_cursor(page.cursor.as_ref())?;
13496        let mut labels = self
13497            .board()
13498            .await?
13499            .items
13500            .into_iter()
13501            .flat_map(|item| item.labels)
13502            .fold(Vec::new(), |mut all, label| {
13503                if !all.iter().any(|x: &Label| x.id == label.id) {
13504                    all.push(label);
13505                }
13506                all
13507            });
13508        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
13509        Ok(offset_page(
13510            labels,
13511            offset,
13512            page.limit.min(MAX_PAGE_SIZE) as usize,
13513        ))
13514    }
13515    async fn task_dependencies(
13516        &self,
13517        id: &NativeId,
13518        direction: Direction,
13519        page: &PageRequest,
13520    ) -> Result<Page<DependencyEdge>, SourceError> {
13521        self.dependencies(id, ItemKind::Task, direction, page).await
13522    }
13523    async fn project_dependencies(
13524        &self,
13525        id: &NativeId,
13526        direction: Direction,
13527        page: &PageRequest,
13528    ) -> Result<Page<DependencyEdge>, SourceError> {
13529        self.dependencies(id, ItemKind::Project, direction, page)
13530            .await
13531    }
13532
13533    fn writes(&self) -> WriteSupport {
13534        WriteSupport::Supported
13535    }
13536
13537    /// Create or update one task.
13538    ///
13539    /// Its `delivers` and `delivered_by` are checked before anything is read or written —
13540    /// neither may name the task itself or name one task twice — and land in the body's
13541    /// metadata slot under their reserved keys, in place of any caller metadata of those
13542    /// names.
13543    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
13544        self.write_task_assets(write, None)
13545            .await
13546            .map(|written| written.id)
13547    }
13548
13549    async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
13550        self.origins_ahead(origins).await
13551    }
13552
13553    async fn member_classifications(
13554        &self,
13555        project: &NativeId,
13556    ) -> Result<Option<Vec<(Classification, Vec<Repository>)>>, SourceError> {
13557        Ok(self
13558            .member_classes
13559            .lock()
13560            .map_err(|_| SourceError::Unavailable {
13561                message: "a project's members held for its classification were left \
13562                          inconsistent; run the command again"
13563                    .into(),
13564            })?
13565            .remove(project))
13566    }
13567
13568    /// The issue's own web address, as the `createIssue` that made it in this command answered
13569    /// it: a task, a project and a document are each an issue here.
13570    fn written_location(
13571        &self,
13572        _record: onetaskgraph_plugin_api::MetadataRecord,
13573        id: &NativeId,
13574    ) -> Option<Location> {
13575        self.written
13576            .lock()
13577            .unwrap_or_else(std::sync::PoisonError::into_inner)
13578            .get(id)
13579            .cloned()
13580            .map(Location::Url)
13581    }
13582
13583    /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
13584    /// in one request, and a copy's tasks are created together only then, since their fields
13585    /// and edges are what is held back.
13586    fn create_batch_limit(&self) -> usize {
13587        if self.deferring().unwrap_or(false) {
13588            MUTATION_BATCH
13589        } else {
13590            1
13591        }
13592    }
13593
13594    // 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.
13595    async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
13596        if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
13597            return Err(SourceError::Refused {
13598                message: format!(
13599                    "write_tasks creates tasks, and the write of {} names the existing task {}; \
13600                     next: update it with write_task",
13601                    write.item.id,
13602                    write.target.as_ref().map_or("", |target| target.0.as_str())
13603                ),
13604            });
13605        }
13606        if !self.deferring()? {
13607            let mut created = Vec::with_capacity(writes.len());
13608            for write in writes {
13609                match self.write_task(write).await {
13610                    Ok(id) => created.push(id),
13611                    Err(error) => {
13612                        let mut left: Vec<&str> = Vec::new();
13613                        for id in &created {
13614                            if self.delete_issue(id).await.is_err() {
13615                                left.push(id.0.as_str());
13616                            }
13617                        }
13618                        return Err(left_behind(error, &left));
13619                    }
13620                }
13621            }
13622            return Ok(created);
13623        }
13624        self.create_together(writes).await
13625    }
13626
13627    fn begin_deferred_writes(&self) {
13628        if let Ok(mut deferred) = self.deferred.lock() {
13629            deferred.get_or_insert_with(Deferred::default);
13630        }
13631    }
13632
13633    async fn finish_writes(&self) -> Result<(), SourceError> {
13634        self.send_held_updates().await?;
13635        self.send_held_back().await
13636    }
13637
13638    async fn write_task_with_assets(
13639        &self,
13640        write: &ItemWrite<Task>,
13641        _answers: Option<&BTreeMap<String, Value>>,
13642        assets: &onetaskgraph_plugin_api::AssetWrite,
13643    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
13644        self.write_task_assets(write, Some(assets)).await
13645    }
13646
13647    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
13648        self.write_item(
13649            &Incoming {
13650                written: Written::Work(ItemKind::Project, &write.item.status),
13651                title: &write.item.title,
13652                content: write.item.content.as_deref(),
13653                assets: None,
13654                labels: &write.item.labels,
13655                metadata: &write.item.metadata,
13656                repositories: &write.item.repositories,
13657                classification: write.item.classification,
13658                parent: None,
13659                delivers: &[],
13660                delivered_by: &[],
13661                priority: None,
13662            },
13663            write.target.as_ref(),
13664            &write.depends_on,
13665        )
13666        .await
13667        .map(|written| written.id)
13668    }
13669
13670    /// Create or update one document, which is one issue titled the way this board spells
13671    /// a document.
13672    ///
13673    /// Everything else is exactly a task write: caller metadata goes to the same canonical
13674    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
13675    /// or a field this board cannot carry is refused by name rather than dropped, a target
13676    /// naming an issue this board does not hold is refused rather than created, and an
13677    /// issue this call created is taken back when the rest of the write fails.
13678    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
13679        self.write_document_assets(write, None)
13680            .await
13681            .map(|written| written.id)
13682    }
13683
13684    async fn write_document_with_assets(
13685        &self,
13686        write: &ItemWrite<Document>,
13687        _answers: Option<&BTreeMap<String, Value>>,
13688        assets: &onetaskgraph_plugin_api::AssetWrite,
13689    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
13690        self.write_document_assets(write, Some(assets)).await
13691    }
13692
13693    /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
13694    /// which reads nothing; then the board's `Status` option. Over an existing item that is
13695    /// read off the item, as the write reads it, and the item is held among this command's
13696    /// resolved records so the write that follows reuses that read rather than repeating it;
13697    /// an item that does not carry the field takes the board's fields, which are held once
13698    /// read. A create is checked against the board's fields only when this command already
13699    /// holds them, because a create reads them together with its repository, in one request,
13700    /// and refuses a missing option before it writes anything.
13701    async fn check_status_write(
13702        &self,
13703        kind: ItemKind,
13704        category: StatusCategory,
13705        target: Option<&NativeId>,
13706    ) -> Result<(), SourceError> {
13707        let status = self.resolved_target(kind, category)?;
13708        if status.option().is_none() {
13709            return Ok(());
13710        }
13711        let fields = match target {
13712            Some(target) => {
13713                // A target this board does not hold is the write's own refusal to make.
13714                let Some(item) = self.bound_item(target).await? else {
13715                    return Ok(());
13716                };
13717                self.resolved_cache()?.insert(target.clone(), item.clone());
13718                // Only the `Status` field decides whether the write can land.
13719                self.fields_for(Some(&item), false, true, false)
13720                    .await?
13721                    .fields
13722            }
13723            None => {
13724                let held = self
13725                    .board_cache()?
13726                    .as_ref()
13727                    .map(|board| board.fields.clone());
13728                match held.or_else(|| {
13729                    self.fields_cache()
13730                        .ok()
13731                        .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
13732                }) {
13733                    Some(fields) => fields,
13734                    None => return Ok(()),
13735                }
13736            }
13737        };
13738        self.column_for(&fields, kind, category, &status)
13739            .map(|_| ())
13740    }
13741
13742    /// What a write of `status` reads back as: the `Status` option its category maps to for
13743    /// `kind`, and for a closing category the closure that option and its fixed reason read
13744    /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
13745    /// this instance has no option for is answered as given, since writing it is refused.
13746    fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
13747        match self.statuses.target(kind, status.category) {
13748            StatusTarget::Column(name) => {
13749                self.statuses.status(kind, Some(name.as_str()), false, None)
13750            }
13751            StatusTarget::Terminal(name, reason) => {
13752                self.statuses
13753                    .status(kind, Some(name.as_str()), true, Some(reason.reason()))
13754            }
13755            StatusTarget::Disabled(_) => status.clone(),
13756        }
13757    }
13758
13759    /// Set one task's status alone.
13760    ///
13761    /// An open target reopens a closed issue with an `updateIssue` carrying only its
13762    /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
13763    /// terminal target selects its mapped option, then closes with its fixed reason. No
13764    /// request carries a title, a body or a label. The status
13765    /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
13766    /// what a re-read reports.
13767    async fn set_task_status(
13768        &self,
13769        id: &NativeId,
13770        category: StatusCategory,
13771    ) -> Result<Option<Status>, SourceError> {
13772        self.set_status(id, ItemKind::Task, category).await
13773    }
13774
13775    /// Set one task's status and answer the task with it, reading the item once: the read the
13776    /// task is answered from is handed to the status write, which would otherwise read the
13777    /// same issue again — two requests on a fresh process, the read and the field write,
13778    /// where the default's `get_task` before `set_task_status` sent three.
13779    async fn set_task_status_reading(
13780        &self,
13781        id: &NativeId,
13782        category: StatusCategory,
13783    ) -> Result<Option<Task>, SourceError> {
13784        // Refused before anything is read, in the words the write itself refuses it.
13785        self.resolved_target(ItemKind::Task, category)?;
13786        let Some(item) = self
13787            .bound_item(id)
13788            .await?
13789            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13790        else {
13791            return Ok(None);
13792        };
13793        let mut task = item.task()?;
13794        self.held_cache
13795            .lock()
13796            .map_err(|_| SourceError::Unavailable {
13797                message: "items read by issue reference were left inconsistent; run the \
13798                          command again"
13799                    .into(),
13800            })?
13801            .insert(id.clone(), item);
13802        let Some(status) = self.set_status(id, ItemKind::Task, category).await? else {
13803            return Ok(None);
13804        };
13805        task.status = status;
13806        Ok(Some(task))
13807    }
13808
13809    /// Set one project's status alone, by exactly the paired write a task's status takes, for
13810    /// the project kind: a terminal category selects the project kind's mapped option and then
13811    /// closes the issue with the category's fixed reason — an issue already closed with a
13812    /// reason that reads that category is left as it is — and an open category reopens a closed
13813    /// issue and then selects its option. A board draft is refused a terminal category as a
13814    /// task's is.
13815    async fn set_project_status(
13816        &self,
13817        id: &NativeId,
13818        category: StatusCategory,
13819    ) -> Result<Option<Status>, SourceError> {
13820        self.set_status(id, ItemKind::Project, category).await
13821    }
13822
13823    /// Every task and project whose card contradicts its issue's closure, read through the
13824    /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
13825    async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
13826        self.contradictions().await
13827    }
13828
13829    /// What this board holds for each issue reference, one request per fifty of them; see
13830    /// [`TaskSource::issue_holdings`].
13831    async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
13832        self.holdings(issues).await
13833    }
13834
13835    /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
13836    /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
13837    /// for `none`. Refused by an instance with no `priority_mapping`.
13838    async fn set_task_priority(
13839        &self,
13840        id: &NativeId,
13841        priority: Priority,
13842    ) -> Result<Option<Priority>, SourceError> {
13843        self.set_priority(id, priority).await
13844    }
13845
13846    /// Replace one task's content with a single body update that keeps the metadata slot
13847    /// byte for byte.
13848    async fn set_task_content(
13849        &self,
13850        id: &NativeId,
13851        content: &str,
13852    ) -> Result<Option<()>, SourceError> {
13853        self.replace_content(id, content).await
13854    }
13855
13856    /// Replace one task issue's content and its provenance slot entry with a single body
13857    /// update. The answers are not kept: see `replace_rendering`.
13858    async fn set_task_rendering(
13859        &self,
13860        id: &NativeId,
13861        content: &str,
13862        provenance: &Value,
13863        _answers: &BTreeMap<String, Value>,
13864    ) -> Result<Option<()>, SourceError> {
13865        self.replace_rendering(
13866            id,
13867            BoardKind::Work(ItemKind::Task),
13868            content,
13869            provenance,
13870            None,
13871        )
13872        .await
13873        .map(|written| written.map(|_| ()))
13874    }
13875
13876    /// Replace one design-document issue's content and its provenance slot entry, on exactly
13877    /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13878    async fn set_document_rendering(
13879        &self,
13880        id: &NativeId,
13881        content: &str,
13882        provenance: &Value,
13883        _answers: &BTreeMap<String, Value>,
13884    ) -> Result<Option<()>, SourceError> {
13885        self.replace_rendering(id, BoardKind::Document, content, provenance, None)
13886            .await
13887            .map(|written| written.map(|_| ()))
13888    }
13889
13890    /// Replace one project issue's content and its provenance slot entry, on exactly the
13891    /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13892    async fn set_project_rendering(
13893        &self,
13894        id: &NativeId,
13895        content: &str,
13896        provenance: &Value,
13897        _answers: &BTreeMap<String, Value>,
13898    ) -> Result<Option<()>, SourceError> {
13899        self.replace_rendering(
13900            id,
13901            BoardKind::Work(ItemKind::Project),
13902            content,
13903            provenance,
13904            None,
13905        )
13906        .await
13907        .map(|written| written.map(|_| ()))
13908    }
13909
13910    /// Apply a targeted update with one read of the item and a write only for what differs:
13911    /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
13912    /// and last one `updateIssue` for title, body and state. See `targeted_update`.
13913    async fn update_task(
13914        &self,
13915        id: &NativeId,
13916        update: &TaskUpdate,
13917    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
13918        self.targeted_update(id, update).await
13919    }
13920
13921    /// Replace one task's `delivered_by` with a single body update that changes the
13922    /// metadata slot and nothing outside it.
13923    async fn set_delivered_by(
13924        &self,
13925        id: &NativeId,
13926        delivered_by: &[TaskRef],
13927    ) -> Result<Option<()>, SourceError> {
13928        self.replace_delivered_by(id, delivered_by).await
13929    }
13930
13931    /// Set one key of one task issue's metadata with a single body update that changes the
13932    /// metadata slot and nothing outside it — no title, label, state or board field request —
13933    /// and sends nothing when the task already holds that value under the key.
13934    async fn set_task_metadata(
13935        &self,
13936        id: &NativeId,
13937        key: &MetadataKey,
13938        value: &Value,
13939    ) -> Result<Option<Task>, SourceError> {
13940        Ok(self
13941            .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
13942            .await?
13943            .map(|item| item.task())
13944            .transpose()?)
13945    }
13946
13947    /// Set one key of one project issue's metadata, on exactly the terms of
13948    /// [`set_task_metadata`](TaskSource::set_task_metadata).
13949    async fn set_project_metadata(
13950        &self,
13951        id: &NativeId,
13952        key: &MetadataKey,
13953        value: &Value,
13954    ) -> Result<Option<Project>, SourceError> {
13955        Ok(self
13956            .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
13957            .await?
13958            .map(|item| item.project()))
13959    }
13960
13961    /// Set one key of one design-document issue's metadata, on exactly the terms of
13962    /// [`set_task_metadata`](TaskSource::set_task_metadata).
13963    async fn set_document_metadata(
13964        &self,
13965        id: &NativeId,
13966        key: &MetadataKey,
13967        value: &Value,
13968    ) -> Result<Option<Document>, SourceError> {
13969        Ok(self
13970            .set_slot_key(id, BoardKind::Document, key, value)
13971            .await?
13972            .map(|item| item.document()))
13973    }
13974
13975    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
13976        self.delete_item(id).await
13977    }
13978
13979    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
13980        self.delete_item(id).await
13981    }
13982
13983    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
13984        self.delete_item(id).await
13985    }
13986
13987    /// One page of the task issue's own comments, walked by GitHub's own cursor.
13988    ///
13989    /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
13990    /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
13991    ///
13992    /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
13993    /// board is the read of its comments. A draft this process already resolved is refused
13994    /// without one.
13995    async fn task_comments(
13996        &self,
13997        task: &NativeId,
13998        page: &PageRequest,
13999    ) -> Result<Option<Page<Comment>>, SourceError> {
14000        validate_page(page)?;
14001        let cached = self.resolved_cache()?.get(task).cloned();
14002        if let Some(item) = cached {
14003            if item.kind != BoardKind::Work(ItemKind::Task) {
14004                return Ok(None);
14005            }
14006            if item.content_kind == ContentKind::DraftIssue {
14007                return Err(self.draft_has_no_comments(task));
14008            }
14009        }
14010        match self.issue_detail(task, page).await? {
14011            Some(TaskDetailRead {
14012                comments: Some(comments),
14013                ..
14014            }) => comments,
14015            _ => Ok(None),
14016        }
14017    }
14018
14019    /// Every id's task, with the first page of its comments when `comments` names it:
14020    /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
14021    /// comments in one [`graphql::ISSUE_DETAIL`] request.
14022    async fn get_task_details(
14023        &self,
14024        ids: &[NativeId],
14025        comments: Option<&PageRequest>,
14026    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14027        if let Some(page) = comments
14028            && let Err(error) = validate_page(page)
14029        {
14030            return ids.iter().map(|_| Err(error.clone())).collect();
14031        }
14032        match (ids, comments) {
14033            ([id], Some(page)) => vec![self.issue_detail(id, page).await],
14034            ([id], None) => vec![self.task_read(id).await],
14035            _ => self.issue_details(ids, comments).await,
14036        }
14037    }
14038
14039    /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
14040    /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
14041    /// request, so the dependency read a copy makes of each next is answered from this one.
14042    async fn get_task_details_for_copy(
14043        &self,
14044        ids: &[NativeId],
14045        comments: &PageRequest,
14046    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
14047        // A page past the first is a page of one task's comments, which the detail read of it
14048        // alone answers.
14049        if comments.cursor.is_some() {
14050            return self.get_task_details(ids, Some(comments)).await;
14051        }
14052        if let Err(error) = validate_page(comments) {
14053            return ids.iter().map(|_| Err(error.clone())).collect();
14054        }
14055        let mut read = Vec::with_capacity(ids.len());
14056        for batch in ids.chunks(COPY_DETAIL_BATCH) {
14057            match self
14058                .graphql(
14059                    graphql::ISSUE_COPY_DETAILS,
14060                    copy_detail_batch(batch, comments, false),
14061                )
14062                .await
14063            {
14064                Ok(data) => {
14065                    for (slot, id) in batch.iter().enumerate() {
14066                        let read_one = match data.get(format!("i{slot}")) {
14067                            Some(node) => self.detail_of(id, node, true, None).await,
14068                            None => Err(SourceError::Malformed {
14069                                message: format!(
14070                                    "GitHub answered a batch read with no item for {}",
14071                                    id.0
14072                                ),
14073                            }),
14074                        };
14075                        read.push(read_one);
14076                    }
14077                }
14078                Err(error) if unresolvable_node(&error) => {
14079                    for id in batch {
14080                        read.push(self.issue_detail(id, comments).await);
14081                    }
14082                }
14083                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
14084            }
14085        }
14086        read
14087    }
14088
14089    /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
14090    /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
14091    /// and held for the next read of each by id; see `read_ahead`.
14092    ///
14093    /// What this run wrote is never read: its own record answers, as it does for a read by id.
14094    /// Nor is an item a listing of its project carried whole earlier in this command — see
14095    /// `carried` — which is held in place of reading it.
14096    /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
14097    /// so each of its ids is read on its own and answered as that read answers; a draft, and an
14098    /// answer naming another issue than the one asked for, are left to their own reads too.
14099    async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
14100        // The origins asked just before go out now, beside the members of the one project this
14101        // names when it names one; a project those origins answered whole is held for its next
14102        // read by id rather than read again here.
14103        let hinted = match ids {
14104            [one] => Some(one),
14105            _ => None,
14106        };
14107        self.send_pending_origins(hinted).await?;
14108        if let Some(project) = hinted {
14109            let answered = self.resolved_cache()?.get(project).cloned();
14110            if let Some(answered) = answered
14111                && answered.kind == BoardKind::Work(ItemKind::Project)
14112            {
14113                self.read_ahead()?.insert(project.clone(), Some(answered));
14114                return Ok(());
14115            }
14116        }
14117        let mut unread: Vec<NativeId> = Vec::new();
14118        // Read whole by a listing of its project in this command, which a mutation naming it
14119        // would have dropped: held as a batch would have held it, once every batch is answered.
14120        let mut listed: Vec<NativeId> = Vec::new();
14121        {
14122            let created = self.created()?;
14123            let held = self.read_ahead()?;
14124            let carried = self.carried()?;
14125            for id in ids {
14126                if unread.contains(id)
14127                    || listed.contains(id)
14128                    || held.contains_key(id)
14129                    || created.iter().any(|own| own.id == *id)
14130                {
14131                    continue;
14132                }
14133                // An issue a listing carried without the whole of what blocks it is read in a
14134                // batch too, which carries that, rather than having its dependency read sent
14135                // on its own once the copy reconciles its edges.
14136                let whole = carried.get(id).is_some_and(|item| {
14137                    item.content_kind != ContentKind::Issue || item.blocked_by.is_some()
14138                });
14139                if whole {
14140                    listed.push(id.clone());
14141                } else {
14142                    unread.push(id.clone());
14143                }
14144            }
14145        }
14146        // Nothing of an item's comments is wanted here: the smallest page keeps the read to
14147        // what the item itself costs.
14148        let comments = PageRequest {
14149            cursor: None,
14150            limit: 1,
14151        };
14152        // Held only once every batch has been answered, so a read-ahead that fails part way
14153        // holds nothing — what a listing carried included, which stays carried — and every
14154        // item is read on its own, as the method promises.
14155        let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
14156        // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
14157        // answered them: what a project copy's orphan check asks, when that first id is its
14158        // project.
14159        let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
14160        for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
14161            let data = match self
14162                .graphql(
14163                    graphql::ISSUE_COPY_DETAILS,
14164                    copy_detail_batch(batch, &comments, index == 0),
14165                )
14166                .await
14167            {
14168                Ok(data) => data,
14169                Err(error) if unresolvable_node(&error) => continue,
14170                Err(error) => return Err(error),
14171            };
14172            for (slot, id) in batch.iter().enumerate() {
14173                let Some(node) = data.get(format!("i{slot}")) else {
14174                    continue;
14175                };
14176                let held = if node.is_null() {
14177                    None
14178                } else {
14179                    match optional_str(node, "__typename")? {
14180                        Some("DraftIssue") => continue,
14181                        Some("Issue") if required_str(node, "id")? != id.0 => continue,
14182                        _ => {}
14183                    }
14184                    // Remembered below with the rest, so a later batch refused leaves no
14185                    // dependency read answered from this one either.
14186                    self.resolve_issue_as(node, false).await?
14187                };
14188                read.push((id.clone(), held));
14189            }
14190            if index == 0
14191                && let Some(node) = data.get("children").filter(|node| !node.is_null())
14192                && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
14193                && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14194            {
14195                // A list GitHub did not answer is not an empty one: it would answer the project's
14196                // task listing with nothing, and the orphan check with no orphans.
14197                let listed = node
14198                    .pointer("/subIssues/nodes")
14199                    .and_then(Value::as_array)
14200                    .ok_or_else(|| SourceError::Malformed {
14201                        message: format!(
14202                            "GitHub answered the sub-issues of project {} with no list of them",
14203                            batch[0].0
14204                        ),
14205                    })?
14206                    .iter()
14207                    .map(|child| {
14208                        Ok((
14209                            required_str(child, "id")?.to_owned(),
14210                            required_str(child, "title")?.to_owned(),
14211                        ))
14212                    })
14213                    .collect::<Result<Vec<_>, SourceError>>()?;
14214                children = Some((batch[0].clone(), listed));
14215            }
14216        }
14217        // The project's tasks are known from this read alone when every child it named is held
14218        // from it, but for a document, which its title says is one and which a task listing
14219        // never answers: then a listing of the project's tasks is answered from them.
14220        if let Some((project, listed)) = children
14221            && read.iter().any(|(id, held)| {
14222                *id == project
14223                    && held
14224                        .as_ref()
14225                        .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
14226            })
14227        {
14228            let mut tasks = Vec::new();
14229            let mut whole = true;
14230            for (child, title) in &listed {
14231                match read.iter().find(|(id, _)| id.0 == *child) {
14232                    Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
14233                        if held.kind == BoardKind::Work(ItemKind::Task) {
14234                            tasks.push(held.clone());
14235                        }
14236                    }
14237                    _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
14238                    _ => whole = false,
14239                }
14240            }
14241            if whole {
14242                self.task_children()?.insert(project, tasks);
14243            }
14244        }
14245        let mut resolved = self.resolved_cache()?;
14246        let mut ahead = self.read_ahead()?;
14247        let mut carried = self.carried()?;
14248        for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
14249            resolved.insert(item.id.clone(), item.clone());
14250        }
14251        // Taken only if still carried: a mutation naming it while the batches ran dropped it,
14252        // and its next read is then its own.
14253        for id in &listed {
14254            let Some(item) = carried.remove(id) else {
14255                continue;
14256            };
14257            // The whole of what it is blocked by came with it, so the dependency read a copy
14258            // makes next is answered from this one, as a copy's detail read is.
14259            if item.blocked_by.is_some() {
14260                resolved.insert(item.id.clone(), item.clone());
14261            }
14262            read.push((id.clone(), Some(item)));
14263        }
14264        for (id, held) in read {
14265            ahead.insert(id, held);
14266        }
14267        Ok(())
14268    }
14269
14270    /// Add one comment to the task's issue, as the account the token belongs to.
14271    ///
14272    /// The author is refused before anything is sent — not even the task is read — because
14273    /// no answer GitHub could give would make posting under another name than the one asked
14274    /// for the right outcome.
14275    async fn add_comment(
14276        &self,
14277        task: &NativeId,
14278        comment: &NewComment,
14279    ) -> Result<Option<Comment>, SourceError> {
14280        if let Some(author) = &comment.author {
14281            return Err(SourceError::Refused {
14282                message: format!(
14283                    "source {} cannot post a comment as {author:?}: GitHub records the account \
14284                     the token signs in as the author of every comment; next: leave --author \
14285                     out, and the comment is posted as that account",
14286                    self.name
14287                ),
14288            });
14289        }
14290        let Some(issue) = self.commented_issue(task).await? else {
14291            return Ok(None);
14292        };
14293        let data = self
14294            .graphql(
14295                graphql::ADD_COMMENT,
14296                json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
14297            )
14298            .await?;
14299        let subject = data
14300            .pointer("/addComment/subject")
14301            .filter(|value| !value.is_null())
14302            .ok_or_else(|| SourceError::Malformed {
14303                message: "GitHub comment addition returned no subject".into(),
14304            })?;
14305        if required_str(subject, "id")? != issue.0 {
14306            return Err(SourceError::Malformed {
14307                message: "GitHub comment addition answered about another issue".into(),
14308            });
14309        }
14310        let added = data
14311            .pointer("/addComment/commentEdge/node")
14312            .filter(|value| !value.is_null())
14313            .ok_or_else(|| SourceError::Malformed {
14314                message: "GitHub comment addition returned no comment".into(),
14315            })?;
14316        let added = comment_from(added)?;
14317        self.remember_commented(&issue)?;
14318        Ok(Some(added))
14319    }
14320
14321    async fn edit_comment(
14322        &self,
14323        task: &NativeId,
14324        comment: &NativeId,
14325        body: &CommentBody,
14326    ) -> Result<Option<Comment>, SourceError> {
14327        let Some(issue) = self.commented_issue(task).await? else {
14328            return Ok(None);
14329        };
14330        let Some(held) = self.comment_on(&issue, comment).await? else {
14331            return Ok(None);
14332        };
14333        // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
14334        let body = CommentOrigin::kept(&held, body)?;
14335        let data = self
14336            .graphql(
14337                graphql::UPDATE_COMMENT,
14338                json!({"input":{"id":comment.0,"body":body.as_str()}}),
14339            )
14340            .await?;
14341        let edited = data
14342            .pointer("/updateIssueComment/issueComment")
14343            .filter(|value| !value.is_null())
14344            .ok_or_else(|| SourceError::Malformed {
14345                message: "GitHub comment update returned no comment".into(),
14346            })?;
14347        let edited = comment_from(edited)?;
14348        if edited.id != *comment {
14349            return Err(SourceError::Malformed {
14350                message: "GitHub comment update returned the wrong comment".into(),
14351            });
14352        }
14353        self.remember_commented(&issue)?;
14354        Ok(Some(edited))
14355    }
14356
14357    async fn delete_comment(
14358        &self,
14359        task: &NativeId,
14360        comment: &NativeId,
14361    ) -> Result<Option<NativeId>, SourceError> {
14362        let Some(issue) = self.commented_issue(task).await? else {
14363            return Ok(None);
14364        };
14365        if !self.comment_is_on(&issue, comment).await? {
14366            return Ok(None);
14367        }
14368        let data = self
14369            .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
14370            .await?;
14371        // The payload says nothing about the comment it removed, so what is checked is that
14372        // GitHub answered the mutation at all rather than leaving it unanswered.
14373        data.get("deleteIssueComment")
14374            .filter(|value| !value.is_null())
14375            .ok_or_else(|| SourceError::Malformed {
14376                message: "GitHub comment deletion returned no payload".into(),
14377            })?;
14378        Ok(Some(comment.clone()))
14379    }
14380
14381    /// Every request this source has recorded, and what each of GitHub's two budgets was
14382    /// attributed — read off the same accounting the session report is rendered from, so
14383    /// the two cannot count one request two ways.
14384    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
14385        Ok(Some(self.ledger.snapshot().metering()))
14386    }
14387
14388    /// Drop every item, search answer and board read this source holds, so the next command
14389    /// reads the board as a person has since left it.
14390    ///
14391    /// Every one of those is held on the assumption that nothing but this source writes the
14392    /// board while a command runs, which stops being true the moment the command is over: a
14393    /// body a person edited would be overwritten from the record held here, and a card they
14394    /// moved would be read as still where this source left it. The board's own field
14395    /// definitions go too, because a person can add or delete a `Status` option and a write
14396    /// resolved against the held list would not re-read on a miss. What stays is what stays
14397    /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
14398    /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
14399    /// accounting [`metering`](TaskSource::metering) answers from.
14400    ///
14401    /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
14402    /// refused, because clearing it is what puts it right.
14403    async fn end_command(&self) -> Result<(), SourceError> {
14404        fn clear<T: Default>(held: &Mutex<T>) {
14405            *held
14406                .lock()
14407                .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
14408            held.clear_poison();
14409        }
14410        clear(&self.created);
14411        clear(&self.updated);
14412        clear(&self.commented);
14413        clear(&self.board_cache);
14414        clear(&self.search_cache);
14415        clear(&self.narrowed_cache);
14416        clear(&self.search_next);
14417        clear(&self.resolved_cache);
14418        clear(&self.read_ahead);
14419        clear(&self.pending_origins);
14420        clear(&self.member_classes);
14421        clear(&self.carried);
14422        clear(&self.held_cache);
14423        clear(&self.children_cache);
14424        clear(&self.task_children);
14425        clear(&self.written);
14426        clear(&self.fields_cache);
14427        self.expected.end_command();
14428        Ok(())
14429    }
14430}
14431
14432/// One member of a project as a classification-only read answers it: its classification and
14433/// the repositories its work changes.
14434type MemberClass = (Classification, Vec<Repository>);
14435
14436/// Each project's sub-issues as one command read them, keyed by the selector they were asked
14437/// for under, beside the project that selector named; see `children_cache`.
14438type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
14439
14440/// One issue comment as the contract carries it.
14441///
14442/// `author` is absent both when GitHub answers `null` for an account that no longer exists
14443/// and when it answers an actor with no login, because either way the source did not say who
14444/// wrote it — which is what an absent author means, rather than an author called nothing.
14445///
14446/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
14447/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
14448/// and is read back with the marker taken out and reported as its origin.
14449fn comment_from(value: &Value) -> Result<Comment, SourceError> {
14450    Ok(Comment {
14451        origin: None,
14452        id: NativeId(required_str(value, "id")?.to_owned()),
14453        author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
14454            .map(str::to_owned),
14455        created_at: optional_time(value, "createdAt")?,
14456        updated_at: optional_time(value, "updatedAt")?,
14457        body: required_str(value, "body")?.to_owned(),
14458        url: optional_str(value, "url")?.map(str::to_owned),
14459    }
14460    .unmarked())
14461}
14462
14463/// The page of comments one issue node carries, resumed from `after`.
14464fn comment_page(
14465    node: &Value,
14466    issue: &str,
14467    after: Option<&str>,
14468) -> Result<Page<Comment>, SourceError> {
14469    let connection = node
14470        .get("comments")
14471        .filter(|value| !value.is_null())
14472        .ok_or_else(|| SourceError::Malformed {
14473            message: format!("GitHub issue {issue} answered with no comments connection"),
14474        })?;
14475    let items = optional_nodes(Some(connection), "issue comments")?
14476        .into_iter()
14477        .flatten()
14478        .map(comment_from)
14479        .collect::<Result<Vec<_>, _>>()?;
14480    let next = next_cursor(connection)?;
14481    if let Some(next) = &next {
14482        validate_cursor_progress(after, &next.0)?;
14483    }
14484    Ok(Page { items, next })
14485}
14486
14487/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
14488/// end — `None` when it carried none, or a page with more past it.
14489fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
14490    let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
14491        return Ok(None);
14492    };
14493    if next_cursor(connection)?.is_some() {
14494        return Ok(None);
14495    }
14496    Ok(Some(
14497        optional_nodes(Some(connection), "blocked-by issues")?
14498            .into_iter()
14499            .flatten()
14500            .cloned()
14501            .collect(),
14502    ))
14503}
14504
14505/// Where the recorded tail of a dependency walk resumes; see
14506/// [`GitHubProjectsSource::recorded_edges`].
14507const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
14508
14509/// The board text field this source keeps a copy's origin in.
14510///
14511/// Named after the key it holds, and held to that name by the guard below rather than by
14512/// a reader noticing.
14513const ORIGIN_FIELD: &str = "onetaskgraph.origin";
14514
14515/// The metadata key that field holds.
14516///
14517/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
14518/// constructs or interprets the qualified id it carries. This source names it only to
14519/// route it — a short, typed value belongs in a typed field rather than in the body slot
14520/// a caller's own prose shares.
14521///
14522/// Restated rather than imported, because no plugin crate may depend on the engine. What
14523/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
14524/// target in `check`: it reads the engine's own literal and fails naming the file and the
14525/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
14526/// that creates a second item every run instead of finding the one it wrote — and that is
14527/// too late to learn it.
14528const ORIGIN_KEY: &str = "onetaskgraph.origin";
14529
14530/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
14531///
14532/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
14533/// is derived from the far end, never written down on the near item — so only a forward
14534/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
14535/// it did not come from, and it is told so rather than answered with an empty page that
14536/// reads as a walk which ended.
14537fn recorded_offset(
14538    cursor: Option<&str>,
14539    direction: Direction,
14540) -> Result<Option<usize>, SourceError> {
14541    cursor
14542        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
14543        .map(|offset| {
14544            if direction != Direction::DependsOn {
14545                return Err(SourceError::Config {
14546                    message: format!(
14547                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
14548                         reverse dependency read never issues; resume it in the direction \
14549                         that reported it"
14550                    ),
14551                });
14552            }
14553            offset.parse().map_err(|_| SourceError::Config {
14554                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
14555            })
14556        })
14557        .transpose()
14558}
14559
14560/// What a dependency read's own answer says of a board draft: `None` when the item is not a
14561/// draft, or the answer did not carry its board item; `Some(None)` for a draft whose one board
14562/// item is not on this board, which this source does not hold; and `Some(Some(slot))`, the
14563/// draft's metadata, for one it does — its board item naming this board by its number and by
14564/// the id the same answer read of the board, as [`GitHubProjectsSource::draft_by_id`] decides.
14565fn draft_slot(
14566    data: &Value,
14567    id: &NativeId,
14568    project_number: u32,
14569) -> Result<Option<Option<BTreeMap<String, Value>>>, SourceError> {
14570    let Some(draft) = data
14571        .get("node")
14572        .filter(|node| node.get("__typename").and_then(Value::as_str) == Some("DraftIssue"))
14573    else {
14574        return Ok(None);
14575    };
14576    let Some(items) = data.pointer("/draft/projectV2Items") else {
14577        return Ok(None);
14578    };
14579    if required_str(draft, "id")? != id.0 {
14580        return Err(SourceError::Malformed {
14581            message: format!("GitHub answered a different draft for {}", id.0),
14582        });
14583    }
14584    let nodes = items
14585        .get("nodes")
14586        .and_then(Value::as_array)
14587        .ok_or_else(|| SourceError::Malformed {
14588            message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
14589        })?;
14590    let info = items
14591        .get("pageInfo")
14592        .ok_or_else(|| SourceError::Malformed {
14593            message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
14594        })?;
14595    if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
14596        return Err(SourceError::Malformed {
14597            message: format!(
14598                "GitHub draft {} reports more board items than the one GitHub links a draft to",
14599                id.0
14600            ),
14601        });
14602    }
14603    let board = data
14604        .pointer("/board/projectV2/id")
14605        .and_then(Value::as_str)
14606        .ok_or_else(|| SourceError::Malformed {
14607            message: format!(
14608                "GitHub answered draft {} without this board's own id beside it",
14609                id.0
14610            ),
14611        })?;
14612    let mut held = false;
14613    for node in nodes {
14614        let project = node.get("project").ok_or_else(|| SourceError::Malformed {
14615            message: format!("GitHub draft {} board item has no project", id.0),
14616        })?;
14617        let number = project
14618            .get("number")
14619            .and_then(Value::as_u64)
14620            .ok_or_else(|| SourceError::Malformed {
14621                message: format!(
14622                    "GitHub draft {} board item has no numeric project number",
14623                    id.0
14624                ),
14625            })?;
14626        let project_id = required_str(project, "id")?;
14627        held |= number == u64::from(project_number) && project_id == board;
14628    }
14629    if !held {
14630        return Ok(Some(None));
14631    }
14632    Ok(Some(Some(
14633        metadata_body(optional_str(draft, "body")?.map(str::to_owned))?.1,
14634    )))
14635}
14636
14637fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
14638    let mut page = offset_page(edges, offset, limit.max(1));
14639    page.next = page
14640        .next
14641        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
14642    page
14643}
14644
14645/// The kind of one issue reached through a dependency connection.
14646///
14647/// The same questions the board scan asks, over the fields the dependency document
14648/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
14649/// then anything with sub-issues or the marker is a project.
14650///
14651/// # Errors
14652///
14653/// A far end this board holds as a document is refused rather than reported. The two
14654/// answers that are not refusals would both be wrong: reporting it as a task names an id
14655/// no task read of this source can find, and reporting it as a project names one no
14656/// project read can. There is no third value to return — `ItemKind` has no document
14657/// variant, because nothing may point at a document — so the relationship itself is what
14658/// the person is told about.
14659fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
14660    let id = required_str(value, "id")?;
14661    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
14662        return Err(SourceError::Refused {
14663            message: format!(
14664                "GitHub issue {id} is a document of this board — its title begins \
14665                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
14666                 on by one; next: remove that issue's blocking relationship on this board"
14667            ),
14668        });
14669    }
14670    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
14671    if parent.is_some() {
14672        return Ok(ItemKind::Task);
14673    }
14674    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
14675    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
14676        message: format!("GitHub issue {id}: {message}"),
14677    })?;
14678    let sub_issues = sub_issue_total(value)?;
14679    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
14680        ItemKind::Project
14681    } else {
14682        ItemKind::Task
14683    })
14684}
14685
14686/// The `stateInput` that closes an issue for `reason`.
14687fn closed_input(reason: ClosedState) -> Value {
14688    json!({"value":"CLOSED","stateReason":reason.reason()})
14689}
14690
14691/// The `IssueStateUpdateInput` one status target asks for.
14692///
14693/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
14694/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
14695/// a currently-closed issue: without that the item would read back `Unknown` and a copy
14696/// would report a change forever. A document has no status at all, and asks for neither.
14697fn state_input(target: Option<&StatusTarget>) -> Value {
14698    match target {
14699        Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
14700        Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
14701        // A document has no status, so a write of one says nothing about the issue's open
14702        // or closed state rather than forcing it open: `stateInput` is what carries that
14703        // instruction, and an explicit null asks for no change to it.
14704        None => Value::Null,
14705    }
14706}
14707
14708/// The metadata one write stores in the item's body slot.
14709///
14710/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
14711/// rather than carried: the kind marker so an empty project stays readable, the
14712/// repository list only when it is not exactly the issue's own repository, and the far
14713/// ends no relationship here can name.
14714///
14715/// The copy origin is the one typed field that is also mirrored here, and only as a
14716/// mirror: it lands in the board's origin field as well, which stays the one every reader
14717/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
14718/// and catches up with a write in seconds rather than minutes — can find the item by it.
14719/// A reader of the release before this one drops the slot's copy and reads the field, so an
14720/// item written here still reads with exactly one origin there.
14721fn slot_metadata(
14722    incoming: &Incoming<'_>,
14723    own_repository: Option<&Repository>,
14724    fallback: &[DependencyEdge],
14725) -> BTreeMap<String, Value> {
14726    let mut metadata = incoming.metadata.clone();
14727    match metadata.remove(ORIGIN_KEY) {
14728        Some(Value::String(origin)) if !origin.is_empty() => {
14729            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
14730        }
14731        _ => {}
14732    }
14733    match incoming.written.kind() {
14734        BoardKind::Work(kind) => metadata.insert(
14735            ItemKind::METADATA_KEY.to_owned(),
14736            Value::String(kind.marker().to_owned()),
14737        ),
14738        // A document is told by its title, so it carries no kind marker: that key names
14739        // what a dependency endpoint points at, and nothing may point at a document.
14740        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
14741    };
14742    incoming.classification.record(&mut metadata);
14743    let derivable = own_repository
14744        .map(|own| incoming.repositories == [own.clone()])
14745        .unwrap_or(incoming.repositories.is_empty());
14746    if derivable {
14747        metadata.remove(Repository::METADATA_KEY);
14748    } else {
14749        metadata.insert(
14750            Repository::METADATA_KEY.to_owned(),
14751            Value::Array(
14752                incoming
14753                    .repositories
14754                    .iter()
14755                    .map(|repository| Value::String(repository.as_str().to_owned()))
14756                    .collect(),
14757            ),
14758        );
14759    }
14760    // The typed lists are what land, whatever the caller's own metadata held under their
14761    // keys: a key of either name travelling beside the field would otherwise be a second
14762    // answer to the same question, and the field is the one the contract names.
14763    for (key, entries) in [
14764        (TaskRef::DELIVERS_KEY, incoming.delivers),
14765        (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
14766    ] {
14767        set_task_list(&mut metadata, key, entries);
14768    }
14769    record_edges(&mut metadata, fallback);
14770    metadata
14771}
14772
14773/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
14774/// one slot's metadata, or no such key when there are none.
14775fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
14776    if fallback.is_empty() {
14777        metadata.remove(DependencyEdge::RECORDED_KEY);
14778    } else {
14779        metadata.insert(
14780            DependencyEdge::RECORDED_KEY.to_owned(),
14781            Value::Array(
14782                fallback
14783                    .iter()
14784                    .map(|edge| {
14785                        serde_json::to_value(&edge.to).expect("an endpoint renders as JSON")
14786                    })
14787                    .collect(),
14788            ),
14789        );
14790    }
14791}
14792
14793/// Every label one item carries, from its content's own connection and nowhere else.
14794///
14795/// There is no second place to read one from: no document this source sends selects the
14796/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
14797/// cannot carry one at all. The module documentation records the three schema facts that
14798/// settle it.
14799fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
14800    optional_nodes(content.get("labels"), "content labels")?
14801        .into_iter()
14802        .flatten()
14803        .map(|v| {
14804            Ok(Label {
14805                id: NativeId(required_str(v, "id")?.to_owned()),
14806                name: required_str(v, "name")?.to_owned(),
14807                color: optional_str(v, "color")?.map(str::to_owned),
14808            })
14809        })
14810        .collect()
14811}
14812
14813/// Each board field value one item holds, as the change token's facets name it: the field's
14814/// name, with the single-select option's name or the text. A value of a type no read here
14815/// selects, and one naming no field, says nothing a person can rename and is left out.
14816fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
14817    field_values
14818        .iter()
14819        .filter_map(|value| {
14820            let field = value.pointer("/field/name")?.as_str()?;
14821            let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
14822            Some((field.to_owned(), held.to_owned()))
14823        })
14824        .collect()
14825}
14826
14827/// The digest of the facets of the issue `content`, whose board item's values are
14828/// `field_values` — or `None` when the response did not carry, whole, every member they are read
14829/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
14830/// label, and every field value.
14831///
14832/// What a write's own response spells its token from, by exactly the rules a read takes them by.
14833fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
14834    let ended = |connection: &Value| {
14835        connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14836    };
14837    let labels = content.get("labels").filter(|labels| ended(labels))?;
14838    if !ended(field_values) {
14839        return None;
14840    }
14841    let parent = content.get("parent")?;
14842    let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
14843    let kind = BoardKind::of(
14844        content.get("title")?.as_str()?,
14845        !parent.is_null(),
14846        sub_issue_total(content).ok()?,
14847        ItemKind::from_metadata(&slot).ok()?,
14848    );
14849    let labels = self::labels(&json!({"labels": labels})).ok()?;
14850    let nodes = field_values.get("nodes")?.as_array()?;
14851    Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
14852}
14853
14854/// The definition of each board field one item's values are values of, in the shape a read
14855/// of the board's own `fields` gives one.
14856///
14857/// A value names its field through a fragment on that field's own type, so the type is
14858/// known from which kind of value it is: a single-select value's field is a
14859/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
14860/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
14861fn field_definitions(field_values: &[Value]) -> Vec<Value> {
14862    field_values
14863        .iter()
14864        .filter_map(|value| {
14865            let field = value.get("field")?.as_object()?;
14866            field.get("id")?.as_str().filter(|id| !id.is_empty())?;
14867            let typename = if value.get("text").is_some() {
14868                "ProjectV2Field"
14869            } else if value.get("name").is_some() {
14870                "ProjectV2SingleSelectField"
14871            } else {
14872                return None;
14873            };
14874            let mut defined = field.clone();
14875            defined.insert("__typename".to_owned(), json!(typename));
14876            // Only a text field holds a text value, so the value says the field's type too.
14877            if typename == "ProjectV2Field" {
14878                defined.insert("dataType".to_owned(), json!("TEXT"));
14879            }
14880            Some(Value::Object(defined))
14881        })
14882        .collect()
14883}
14884
14885fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
14886    let Some(node) = field_values
14887        .iter()
14888        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
14889    else {
14890        return Ok(None);
14891    };
14892    Ok(optional_str(node, "text")?.map(str::to_owned))
14893}
14894
14895fn valid_github_owner(owner: &str) -> bool {
14896    !owner.is_empty()
14897        && owner.len() <= 39
14898        && !owner.starts_with('-')
14899        && !owner.ends_with('-')
14900        && !owner.contains("--")
14901        && owner
14902            .bytes()
14903            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
14904}
14905
14906/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
14907/// neither of the two names a path segment already means.
14908fn valid_github_repository_name(name: &str) -> bool {
14909    !name.is_empty()
14910        && name.len() <= 100
14911        && name != "."
14912        && name != ".."
14913        && name
14914            .bytes()
14915            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
14916}
14917
14918fn valid_environment_name(name: &str) -> bool {
14919    let mut bytes = name.bytes();
14920    bytes
14921        .next()
14922        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
14923        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
14924}
14925
14926/// How many sub-issues one issue has.
14927///
14928/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
14929/// absent or non-integer one is a response this source cannot read — and reading it as
14930/// zero would classify a project as a task, which is exactly the mistake the marker
14931/// exists to keep from happening quietly.
14932fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
14933    let summary = issue
14934        .get("subIssuesSummary")
14935        .ok_or_else(|| SourceError::Malformed {
14936            message: "GitHub issue is missing subIssuesSummary".into(),
14937        })?;
14938    summary
14939        .get("total")
14940        .and_then(Value::as_u64)
14941        .ok_or_else(|| SourceError::Malformed {
14942            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
14943        })
14944}
14945
14946/// One issue's own `number`.
14947///
14948/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
14949/// an issue in this module asks for it. So a read of one that comes back without it, or
14950/// with something that is not an unsigned integer, is a response this source cannot read —
14951/// absence here is **not** "this issue has no number". A draft is the content that has
14952/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
14953/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
14954fn issue_number(issue: &Value) -> Result<u64, SourceError> {
14955    issue
14956        .get("number")
14957        .and_then(Value::as_u64)
14958        .ok_or_else(|| SourceError::Malformed {
14959            message: "GitHub issue number is missing or is not an unsigned integer".into(),
14960        })
14961}
14962
14963/// The `number` a creating mutation answered with, and `None` when it answered without one;
14964/// why a missing one is tolerated is at the call in `create_and_file_issue`.
14965fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
14966    match created.get("number") {
14967        None | Some(Value::Null) => Ok(None),
14968        Some(value) => value
14969            .as_u64()
14970            .map(Some)
14971            .ok_or_else(|| SourceError::Malformed {
14972                message: "GitHub created issue number is not an unsigned integer".into(),
14973            }),
14974    }
14975}
14976
14977fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
14978    value
14979        .get(field)
14980        .and_then(Value::as_str)
14981        .ok_or_else(|| SourceError::Malformed {
14982            message: format!("GitHub response is missing string field {field}"),
14983        })
14984}
14985
14986fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
14987    let found = required_str(value, field)?;
14988    if found.trim().is_empty() {
14989        return Err(SourceError::Malformed {
14990            message: format!("GitHub response has blank string field {field}"),
14991        });
14992    }
14993    Ok(found)
14994}
14995
14996/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
14997/// needs one — Linear spells them too, in its own description field.
14998///
14999/// Restated rather than shared, because a plugin crate depends on the contract crate and
15000/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
15001/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
15002/// source round-trips its own writes perfectly well under its own spelling.
15003const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
15004const METADATA_CLOSE: &str = "\n-->";
15005
15006/// What the composer puts between a non-empty visible body and the slot, and the one thing
15007/// the parser takes off the visible body when it takes the slot off — exactly once, so every
15008/// other trailing byte of the body comes back as it was written.
15009// 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.
15010const METADATA_SEPARATOR: &str = "\n\n";
15011
15012/// The visible body and the metadata slot at the end of it.
15013///
15014/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
15015/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
15016/// own content and is left alone. The visible body is everything before the slot less the
15017/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
15018fn metadata_body(
15019    body: Option<String>,
15020) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
15021    let Some(body) = body else {
15022        return Ok((None, BTreeMap::new()));
15023    };
15024    let Some(slot) = slot_span(&body)? else {
15025        return Ok((Some(body), BTreeMap::new()));
15026    };
15027    let metadata =
15028        serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
15029            SourceError::Malformed {
15030                message: format!(
15031                    "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
15032                ),
15033            }
15034        })?;
15035    let before = &body[..slot.start];
15036    let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
15037    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
15038}
15039
15040/// Where the metadata slot sits in one body, as byte offsets into it.
15041struct SlotSpan {
15042    /// Where [`METADATA_OPEN`] begins.
15043    start: usize,
15044    /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
15045    encoded_start: usize,
15046    /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
15047    encoded_end: usize,
15048    /// Just past [`METADATA_CLOSE`].
15049    end: usize,
15050}
15051
15052/// The slot at the very end of `body`, or `None` when it has none.
15053///
15054/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
15055/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
15056/// slot.
15057fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
15058    let Some(start) = body.rfind(METADATA_OPEN) else {
15059        return Ok(None);
15060    };
15061    let encoded_start = start + METADATA_OPEN.len();
15062    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
15063        return Err(SourceError::Malformed {
15064            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
15065        });
15066    };
15067    let encoded_end = encoded_start + relative_end;
15068    let end = encoded_end + METADATA_CLOSE.len();
15069    if !body[end..].trim().is_empty() {
15070        return Ok(None);
15071    }
15072    Ok(Some(SlotSpan {
15073        start,
15074        encoded_start,
15075        encoded_end,
15076        end,
15077    }))
15078}
15079
15080/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
15081/// slot as it was.
15082///
15083/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
15084/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
15085/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
15086/// or alone in an empty body — and a body with no slot that is given no metadata is
15087/// returned as it is.
15088fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
15089    let encoded = if metadata.is_empty() {
15090        None
15091    } else {
15092        Some(
15093            serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15094                message: error.to_string(),
15095            })?,
15096        )
15097    };
15098    Ok(match (slot_span(body)?, encoded) {
15099        (Some(slot), Some(encoded)) => format!(
15100            "{}{encoded}{}",
15101            &body[..slot.encoded_start],
15102            &body[slot.encoded_end..]
15103        ),
15104        (Some(slot), None) => {
15105            let before = &body[..slot.start];
15106            format!(
15107                "{}{}",
15108                before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
15109                &body[slot.end..]
15110            )
15111        }
15112        (None, None) => body.to_owned(),
15113        (None, Some(encoded)) if body.is_empty() => {
15114            format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15115        }
15116        (None, Some(encoded)) => {
15117            format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15118        }
15119    })
15120}
15121
15122/// `body` with everything before its metadata slot replaced by `content`, and the slot
15123/// itself kept byte for byte.
15124///
15125/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
15126/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
15127/// `content` is empty — so a read of the result reports `content` as the visible body and
15128/// the slot's metadata exactly as it was.
15129fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
15130    let Some(slot) = slot_span(body)? else {
15131        return Ok(content.to_owned());
15132    };
15133    let kept = &body[slot.start..];
15134    Ok(if content.is_empty() {
15135        kept.to_owned()
15136    } else {
15137        format!("{content}{METADATA_SEPARATOR}{kept}")
15138    })
15139}
15140
15141/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
15142fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
15143    if entries.is_empty() {
15144        metadata.remove(key);
15145    } else {
15146        metadata.insert(
15147            key.to_owned(),
15148            Value::Array(
15149                entries
15150                    .iter()
15151                    .map(|entry| Value::String(entry.as_str().to_owned()))
15152                    .collect(),
15153            ),
15154        );
15155    }
15156}
15157
15158fn compose_body(
15159    content: Option<&str>,
15160    metadata: &BTreeMap<String, Value>,
15161) -> Result<Option<String>, SourceError> {
15162    let visible = content.unwrap_or_default();
15163    if metadata.is_empty() {
15164        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
15165    }
15166    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
15167        message: error.to_string(),
15168    })?;
15169    Ok(Some(if visible.is_empty() {
15170        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15171    } else {
15172        format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
15173    }))
15174}
15175
15176fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
15177    value
15178        .get(field)
15179        .and_then(Value::as_bool)
15180        .ok_or_else(|| SourceError::Malformed {
15181            message: format!("GitHub response is missing boolean field {field}"),
15182        })
15183}
15184fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
15185    match value.get(field) {
15186        None | Some(Value::Null) => Ok(None),
15187        Some(value) => value
15188            .as_str()
15189            .map(Some)
15190            .ok_or_else(|| SourceError::Malformed {
15191                message: format!("GitHub response field {field} is not a string or null"),
15192            }),
15193    }
15194}
15195fn optional_nodes<'a>(
15196    connection: Option<&'a Value>,
15197    name: &str,
15198) -> Result<Option<&'a Vec<Value>>, SourceError> {
15199    match connection {
15200        None | Some(Value::Null) => Ok(None),
15201        Some(value) => value
15202            .get("nodes")
15203            .and_then(Value::as_array)
15204            .map(Some)
15205            .ok_or_else(|| SourceError::Malformed {
15206                message: format!("GitHub {name}.nodes is not an array"),
15207            }),
15208    }
15209}
15210fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
15211    let page_info = connection
15212        .get("pageInfo")
15213        .ok_or_else(|| SourceError::Malformed {
15214            message: format!("GitHub {name} has no pageInfo"),
15215        })?;
15216    if required_bool(page_info, "hasNextPage")? {
15217        return Err(SourceError::Malformed {
15218            message: format!(
15219                "GitHub {name} exceeds the supported nested connection size of {size}"
15220            ),
15221        });
15222    }
15223    Ok(())
15224}
15225fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
15226    optional_str(value, field)?
15227        .map(|timestamp| {
15228            timestamp.parse().map_err(|error| SourceError::Malformed {
15229                message: format!("GitHub response field {field} is not a timestamp: {error}"),
15230            })
15231        })
15232        .transpose()
15233}
15234fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
15235    if page.limit == 0 {
15236        Err(SourceError::Config {
15237            message: "page limit must be at least 1".into(),
15238        })
15239    } else {
15240        Ok(())
15241    }
15242}
15243fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
15244    let page = connection
15245        .get("pageInfo")
15246        .filter(|value| value.is_object())
15247        .ok_or_else(|| SourceError::Malformed {
15248            message: "GitHub connection is missing pageInfo".into(),
15249        })?;
15250    if required_bool(page, "hasNextPage")? {
15251        let cursor = required_str(page, "endCursor")?;
15252        validate_cursor_progress(None, cursor)?;
15253        Ok(Some(Cursor(cursor.into())))
15254    } else {
15255        Ok(None)
15256    }
15257}
15258fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
15259    if next.is_empty() || previous == Some(next) {
15260        Err(SourceError::Malformed {
15261            message: "GitHub pagination cursor is empty or did not advance".into(),
15262        })
15263    } else {
15264        Ok(())
15265    }
15266}
15267/// The version of this plugin's opaque narrowing-search cursor.
15268pub const SEARCH_CURSOR_VERSION: u32 = 4;
15269
15270#[derive(Serialize, Deserialize)]
15271#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
15272enum SearchConnection {
15273    Initial {},
15274    Continuing { after: Cursor },
15275    Exhausted {},
15276}
15277impl SearchConnection {
15278    fn after(&self) -> Option<&str> {
15279        match self {
15280            Self::Continuing { after } => Some(&after.0),
15281            _ => None,
15282        }
15283    }
15284    fn exhausted(&self) -> bool {
15285        matches!(self, Self::Exhausted { .. })
15286    }
15287    /// Whether a cursor naming this position, `offset` rows into its page, is one this
15288    /// plugin could have handed out: a page is resumed only part of the way through it — an
15289    /// offset of a whole page or more would skip rows nobody was given — an initial page
15290    /// only once some of it was handed out, and an exhausted connection has no page to be
15291    /// part of the way through.
15292    fn valid_resume(&self, offset: usize) -> bool {
15293        let within = offset < SEARCH_PAGE_SIZE as usize;
15294        match self {
15295            Self::Initial { .. } => offset > 0 && within,
15296            Self::Continuing { after } => !after.0.is_empty() && within,
15297            Self::Exhausted { .. } => offset == 0,
15298        }
15299    }
15300}
15301
15302/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
15303#[derive(Serialize, Deserialize)]
15304#[serde(deny_unknown_fields)]
15305struct SearchPosition {
15306    version: u32,
15307    connection: SearchConnection,
15308    /// How many rows of the page `connection` starts were already handed out.
15309    #[serde(default, skip_serializing_if = "is_zero")]
15310    offset: usize,
15311    #[serde(default, skip_serializing_if = "Vec::is_empty")]
15312    seen: Vec<NativeId>,
15313    #[serde(default, skip_serializing_if = "Vec::is_empty")]
15314    own: Vec<NativeId>,
15315}
15316impl Default for SearchPosition {
15317    fn default() -> Self {
15318        Self {
15319            version: SEARCH_CURSOR_VERSION,
15320            connection: SearchConnection::Initial {},
15321            offset: 0,
15322            seen: Vec::new(),
15323            own: Vec::new(),
15324        }
15325    }
15326}
15327
15328fn is_zero(offset: &usize) -> bool {
15329    *offset == 0
15330}
15331
15332fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
15333    cursor.map_or(Ok(0), |c| {
15334        c.0.parse().map_err(|_| SourceError::Config {
15335            message: "page cursor is invalid".into(),
15336        })
15337    })
15338}
15339fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
15340    if offset > items.len() {
15341        return Page::last(vec![]);
15342    }
15343    let tail = items.split_off(offset);
15344    let mut selected = tail;
15345    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
15346    selected.truncate(limit);
15347    Page {
15348        items: selected,
15349        next,
15350    }
15351}
15352
15353impl GitHubProjectsSource {
15354    async fn write_task_assets(
15355        &self,
15356        write: &ItemWrite<Task>,
15357        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15358    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15359        self.write_item(
15360            &self.task_incoming(write, assets)?,
15361            write.target.as_ref(),
15362            &write.depends_on,
15363        )
15364        .await
15365    }
15366
15367    /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
15368    /// are known to be ones this source can write.
15369    fn task_incoming<'a>(
15370        &self,
15371        write: &'a ItemWrite<Task>,
15372        assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
15373    ) -> Result<Incoming<'a>, SourceError> {
15374        let near = write.target.as_ref().unwrap_or(&write.item.id);
15375        for (key, entries) in [
15376            (TaskRef::DELIVERS_KEY, &write.item.delivers),
15377            (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
15378        ] {
15379            TaskRef::listed(key, near, Some(&self.name), entries.clone())
15380                .map_err(|message| SourceError::Refused { message })?;
15381        }
15382        if self.priorities.is_none() && write.item.priority != Priority::None {
15383            return Err(self.holds_no_priority());
15384        }
15385        Ok(Incoming {
15386            written: Written::Work(ItemKind::Task, &write.item.status),
15387            title: &write.item.title,
15388            content: write.item.content.as_deref(),
15389            assets,
15390            labels: &write.item.labels,
15391            metadata: &write.item.metadata,
15392            repositories: &write.item.repositories,
15393            classification: write.item.classification,
15394            parent: write.item.project.as_ref(),
15395            delivers: &write.item.delivers,
15396            delivered_by: &write.item.delivered_by,
15397            priority: self.priorities.as_ref().map(|_| write.item.priority),
15398        })
15399    }
15400    async fn write_document_assets(
15401        &self,
15402        write: &ItemWrite<Document>,
15403        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
15404    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
15405        // A document takes part in no dependency graph, so there is no far end to write
15406        // natively and none to record: a caller naming one is told so rather than having it
15407        // stored under the reserved key, where a later read would report an edge the
15408        // contract says cannot exist.
15409        if !write.depends_on.is_empty() {
15410            return Err(SourceError::Refused {
15411                message: format!(
15412                    "this write names {} dependencies for a document, and a document takes \
15413                     part in no dependency graph; next: put the dependency on the task or \
15414                     project the document is about",
15415                    write.depends_on.len()
15416                ),
15417            });
15418        }
15419        self.write_item(
15420            &Incoming {
15421                written: Written::Document,
15422                title: &write.item.title,
15423                content: write.item.content.as_deref(),
15424                assets,
15425                labels: &write.item.labels,
15426                metadata: &write.item.metadata,
15427                repositories: &write.item.repositories,
15428                classification: write.item.classification,
15429                parent: write.item.project.as_ref(),
15430                delivers: &[],
15431                delivered_by: &[],
15432                priority: None,
15433            },
15434            write.target.as_ref(),
15435            &[],
15436        )
15437        .await
15438    }
15439}
15440
15441/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
15442/// itself, for the two things no journey can observe.
15443///
15444/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
15445/// with and without the call, that a settlement, a board listing and a metadata search each
15446/// read afresh after it — the resolved records, the written-item overlay, the board and its
15447/// search, and the narrowed searches. What they cannot reach is the held field definitions,
15448/// because a status write naming an option a person deleted is refused the same whether or
15449/// not the list is held, and a poisoned lock, because nothing outside the source can panic
15450/// while one of its locks is held. So these assert those directly, and every other holder
15451/// beside them so a holder added later without a clear in the call fails here.
15452#[cfg(test)]
15453mod end_command_tests {
15454    use super::*;
15455
15456    struct Token;
15457
15458    impl SecretResolver for Token {
15459        fn get(&self, var: &str) -> Option<SecretString> {
15460            (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
15461        }
15462    }
15463
15464    fn source() -> GitHubProjectsSource {
15465        let config = serde_json::from_value(json!({
15466            "owner": "octo-org", "project_number": 7, "repository": "acme/work",
15467            // Nothing here is sent: the source is only built and its state inspected.
15468            "endpoint": "http://127.0.0.1:9/graphql",
15469        }))
15470        .expect("a usable configuration");
15471        GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
15472            .expect("the source builds")
15473    }
15474
15475    /// One issue as a board read answers it.
15476    fn resolved(source: &GitHubProjectsSource) -> Resolved {
15477        source
15478            .resolve(&json!({
15479                "id": "ITEM-1",
15480                "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
15481                            "body": "what a person may since have edited", "state": "OPEN",
15482                            "stateReason": null, "url": null, "number": 1,
15483                            "subIssuesSummary": {"total": 0},
15484                            "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
15485                "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
15486            }))
15487            .expect("the item reads")
15488            .expect("an issue")
15489    }
15490
15491    /// Hold something in every holder the call clears, and the repository id it keeps.
15492    fn fill(source: &GitHubProjectsSource) {
15493        let item = resolved(source);
15494        source.created.lock().unwrap().push(item.clone());
15495        source.updated.lock().unwrap().push(item.clone());
15496        *source.board_cache.lock().unwrap() = Some(Board {
15497            id: "PVT-board".into(),
15498            fields: json!({"nodes": []}),
15499            items: vec![item.clone()],
15500        });
15501        *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
15502        source
15503            .narrowed_cache
15504            .lock()
15505            .unwrap()
15506            .insert("status:todo".into(), vec![item.clone()]);
15507        source.children_cache.lock().unwrap().insert(
15508            NativeId("P-1".into()),
15509            (NativeId("P-1".into()), vec![item.clone()]),
15510        );
15511        source
15512            .search_next
15513            .lock()
15514            .unwrap()
15515            .insert("status:todo".into(), Some("cursor".into()));
15516        source
15517            .resolved_cache
15518            .lock()
15519            .unwrap()
15520            .insert(item.id.clone(), item);
15521        *source.fields_cache.lock().unwrap() = Some(BoardFields {
15522            id: BoardId::parse("PVT-board").unwrap(),
15523            fields: json!({"nodes": []}),
15524        });
15525        source
15526            .repository_cache
15527            .lock()
15528            .unwrap()
15529            .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
15530    }
15531
15532    fn assert_dropped(source: &GitHubProjectsSource) {
15533        assert!(source.created().unwrap().is_empty(), "created");
15534        assert!(source.updated().unwrap().is_empty(), "updated");
15535        assert!(source.board_cache().unwrap().is_none(), "board");
15536        assert!(source.search_cache.lock().unwrap().is_none(), "search");
15537        assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
15538        assert!(
15539            source.children_cache.lock().unwrap().is_empty(),
15540            "project children"
15541        );
15542        assert!(
15543            source.search_next.lock().unwrap().is_empty(),
15544            "search paging"
15545        );
15546        assert!(
15547            source.resolved_cache().unwrap().is_empty(),
15548            "resolved records"
15549        );
15550        assert!(source.fields_cache().unwrap().is_none(), "board fields");
15551        assert_eq!(
15552            source.repository_cache().unwrap().len(),
15553            1,
15554            "a repository's node id stays valid and is kept"
15555        );
15556    }
15557
15558    fn end(source: &GitHubProjectsSource) {
15559        tokio::runtime::Builder::new_current_thread()
15560            .build()
15561            .unwrap()
15562            .block_on(source.end_command())
15563            .expect("the command ends");
15564    }
15565
15566    #[test]
15567    fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
15568        let source = source();
15569        fill(&source);
15570        end(&source);
15571        assert_dropped(&source);
15572    }
15573
15574    #[test]
15575    fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
15576        fn poison<T: Send>(held: &Mutex<T>) {
15577            std::thread::scope(|scope| {
15578                let _ = scope
15579                    .spawn(|| {
15580                        let _guard = held.lock().unwrap();
15581                        panic!("a failure while the lock is held");
15582                    })
15583                    .join();
15584            });
15585            assert!(held.is_poisoned());
15586        }
15587        let source = source();
15588        fill(&source);
15589        poison(&source.created);
15590        poison(&source.updated);
15591        poison(&source.board_cache);
15592        poison(&source.search_cache);
15593        poison(&source.narrowed_cache);
15594        poison(&source.search_next);
15595        poison(&source.resolved_cache);
15596        poison(&source.fields_cache);
15597        assert!(
15598            source.resolved_cache().is_err(),
15599            "a poisoned lock is refused before the call"
15600        );
15601        end(&source);
15602        assert_dropped(&source);
15603    }
15604}
15605
15606#[cfg(test)]
15607mod unprojectable_fields_tests {
15608    use super::*;
15609
15610    /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
15611    fn documented() -> std::collections::BTreeSet<String> {
15612        let document = std::fs::read_to_string(
15613            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
15614        )
15615        .expect("docs/metadata.md is readable");
15616        let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
15617        let lead = "a `field` equal, ignoring case, to ";
15618        let start = document.find(lead).expect("the refused-field sentence") + lead.len();
15619        let sentence = &document[start..];
15620        let sentence = &sentence[..sentence
15621            .find("; and two entries naming one field")
15622            .expect("the refused-field sentence ends at the duplicate rule")];
15623        let (own, github) = sentence
15624            .split_once(" or a field GitHub owns on every board (")
15625            .expect("the sentence names GitHub's own fields");
15626        own.split('`')
15627            .skip(1)
15628            .step_by(2)
15629            .chain(
15630                github
15631                    .strip_suffix(')')
15632                    .expect("GitHub's own fields close the sentence")
15633                    .split(", "),
15634            )
15635            .map(str::to_owned)
15636            .collect()
15637    }
15638
15639    #[test]
15640    fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
15641        let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
15642            .iter()
15643            .map(|&name| name.to_owned())
15644            .collect();
15645        assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
15646        let instance = SourceName::new("work").unwrap();
15647        for name in UNPROJECTABLE_FIELDS {
15648            for spelled in [name.to_owned(), name.to_uppercase()] {
15649                let entry = MetadataFieldConfig {
15650                    field: spelled.clone(),
15651                    key: "team.machine".to_owned(),
15652                    path: Vec::new(),
15653                };
15654                let refused = MetadataField::resolve(vec![entry], &instance)
15655                    .expect_err("an unprojectable field is refused");
15656                assert!(
15657                    refused.to_string().contains(&format!("{name:?}")),
15658                    "{spelled}: {refused}"
15659                );
15660            }
15661        }
15662    }
15663}