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 / 5 | ORIGIN_LOOKUPS (the origin, the board's fields and the repository's node and numeric ids together; two points), 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 writes carries: [`graphql::CREATE_ISSUES`],
697/// [`graphql::WRITE_FIELD_VALUES`], [`graphql::CLOSE_ISSUES`] and [`graphql::ADD_BLOCKED_BYS`].
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 origins one [`graphql::ORIGIN_LOOKUPS`] request asks about: the nine records of a
705/// plan and the project's document, and no more, since each carries a page of the board's field
706/// filter and one of the issue search.
707pub const ORIGIN_BATCH: usize = 10;
708
709/// The most nodes any one document this source sends may be asked to return.
710///
711/// GitHub's own published per-query ceiling, taken from
712/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
713/// workspace cannot hold a stale copy of somebody else's number. A query above it is
714/// **refused before it is executed**, whoever is asking and whatever board they are
715/// asking about — so this is a bound on the documents rather than a budget that runs out.
716///
717/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
718/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
719/// everything the credential does — two numbers against two limits, and this constant
720/// bounds only the first. The second is computed offline too, per document:
721/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
722/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
723/// lane. There is no constant like this one to hold a price under, because points are an
724/// hourly allowance rather than a per-call bound.
725///
726/// Neither is a session's price. What `session-cost.md` records of a whole session is its
727/// **requests** and its **worst-case nodes**; what a whole session spends in points is
728/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
729/// [`accounting`]. The module section on the three ways this source reaches an item says how
730/// the count is arrived at, and which of the page sizes below decide it.
731pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
732
733/// Nested connection size for the connections that hang off one item.
734///
735/// It multiplies through every document that reaches an item under a page — the count
736/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
737/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
738/// every document under these constants and fails naming any that reaches the limit, so
739/// raising this is caught there rather than by GitHub.
740const NESTED_PAGE_SIZE: u32 = 50;
741/// How many of one issue's board memberships are read when an issue is reached directly.
742///
743/// An issue reached through a search or through its own node id carries its board half in
744/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
745/// under a page of issues, so every point of it multiplies through the whole document and
746/// is paid for whether or not any issue is on a second board — which is why it is
747/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
748///
749/// **Three, because what a page misses is now recovered rather than refused**, and the
750/// recovery is what the value is chosen against. An issue whose entry for this board sits
751/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
752/// that page's own cursor — so the value trades a bound every read pays for a request only
753/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
754/// boards would pay that request *per issue*, which is order N against the one page per
755/// hundred issues a read costs today. At three it is only reached by an issue on four or
756/// more boards at once, which keeps the recovery path exceptional rather than routine for
757/// a plausible deployment.
758const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
759/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
760/// its two connections for.
761///
762/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
763/// second is a duplicate a copy already takes the first of. Both connections are walked to
764/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
765/// never what the answer is. It is small because every point of it is paid on every lookup,
766/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
767/// worst-case nodes than the one whole-board read they replaced.
768const ORIGIN_PAGE_SIZE: u32 = 3;
769
770/// How many of one project's sub-issues one [`graphql::SUB_ISSUES`] page asks for: the most
771/// one point buys, since every row carries six connections GitHub charges for once per row.
772/// `session-cost.md` derives it; `tests/point_cost.rs` prices the document at one point and
773/// fails if one row more would still be priced at one.
774pub const MEMBER_PAGE_SIZE: u32 = 23;
775
776pub use github_graphql_node_count::{NodeCountError, Variables};
777
778/// The largest value this source can bind to each page-size variable its documents name.
779///
780/// Every `first:` in [`graphql`] reads one of these five, and each is capped at the
781/// constant above it wherever a caller's own limit could reach it — `$first` at
782/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
783/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`, `$memberFirst` at
784/// [`MEMBER_PAGE_SIZE`]. So this is the worst case a caller can drive this source to,
785/// not one configuration of it, which is what makes a bound computed under it a bound on
786/// every read.
787pub fn largest_page_sizes() -> Variables {
788    Variables::from([
789        ("first".to_owned(), MAX_PAGE_SIZE),
790        ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
791        ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
792        ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
793        ("memberFirst".to_owned(), MEMBER_PAGE_SIZE),
794    ])
795}
796
797/// The most nodes `document` could be asked to return, by GitHub's published rules.
798///
799/// Computed offline from the document's own text under [`largest_page_sizes`] — no
800/// network, no credential and no schema — by
801/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
802/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
803/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
804///
805/// # Errors
806///
807/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
808/// no single operation, or binds a page size this source does not name — each of which is
809/// a defect in the document rather than a number.
810pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
811    node_count(document, &largest_page_sizes())
812}
813
814/// The most rate-limit points one call of `document` could spend, by GitHub's published
815/// rules.
816///
817/// Computed offline from the document's own text under [`largest_page_sizes`] — no
818/// network, no credential and no schema — by
819/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
820/// This is `cost`, metered **per hour** against the allowance one credential shares across
821/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
822/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
823/// under, so what `tests/point_cost.rs` does with it is pin every document in
824/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
825/// figures against GitHub's own reported `cost`.
826///
827/// # Errors
828///
829/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
830/// no single operation, or binds a page size this source does not name — each of which is
831/// a defect in the document rather than a number.
832pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
833    github_graphql_node_count::point_cost(document, &largest_page_sizes())
834}
835
836/// The most nodes `document` could be asked to return under `variables`.
837///
838/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
839/// [`accounting`] is this under the bindings one request really sent — one spelling of the
840/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
841/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
842///
843/// # Errors
844///
845/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
846/// no single operation, or binds a page size `variables` does not name.
847pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
848    github_graphql_node_count::node_count(document, variables)
849}
850
851/// The issue-title prefix that makes a board issue a document.
852///
853/// A GitHub Projects board has no document type — it holds issues — so the discriminator
854/// is the title, and this is the whole of it: an issue whose title begins with these bytes
855/// is a document and every other issue is the task or project the sub-issue rule makes it.
856///
857/// It is spelled **once**, here, and read rather than restated everywhere else — including
858/// by the shared journeys, which take it from this constant so a board fixture cannot
859/// drift from what this source reads. `docs/metadata.md` records the two consequences that
860/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
861/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
862/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
863pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
864
865/// Exact GraphQL query documents issued by this plugin.
866///
867/// Keeping the production documents here lets the pinned-schema test validate the same
868/// bytes that are sent to GitHub, rather than a test-only copy which could drift
869/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
870/// field, and its guarded caller always supplies the complete existing option set with ids.
871pub mod graphql {
872    /// The board half of one item: the field values every document here reads it from.
873    ///
874    /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
875    /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
876    /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
877    /// *the same value*, because
878    /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
879    /// one path. Three spellings of it is what would drift, so there is one.
880    ///
881    /// The `Status` option and this source's own origin text field are the whole of it. It
882    /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
883    /// content, so it holds nothing the content's own `labels` do not already say, and it
884    /// would sit a label connection two page sizes deep.
885    macro_rules! board_item_values {
886        () => {
887            r#"fieldValues(first:$nestedFirst){nodes{
888          ... on ProjectV2ItemFieldSingleSelectValue{name field{
889            ... on ProjectV2SingleSelectField{id name options{id name}}
890          }}
891          ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
892        }pageInfo{hasNextPage}}"#
893        };
894    }
895
896    /// The variable every document reading an issue declares for the relationship events its
897    /// change token counts, with its value as the default, so no caller binds it.
898    ///
899    /// A variable rather than a literal list because every argument a production document
900    /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
901    /// its default to the pinned enum. Its default is the six event types GitHub records a
902    /// relationship added or removed with: a dependency on either end, as `blockedBy` and
903    /// `blocking`, so one a person makes from the far end moves this end's count too; and a
904    /// project membership on the task, as its parent, which GitHub records there whichever
905    /// issue the move was made from. A sub-issue's events on its parent are not counted: a
906    /// project carries no task token, and a copy writes nothing of a task its own sub-issues
907    /// decide. [`super::change_token`] says why the count is in the token.
908    macro_rules! relationship_events_variable {
909        () => {
910            ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
911        };
912    }
913
914    /// Everything this source reads about one issue, wherever it reaches that issue.
915    ///
916    /// A macro rather than a constant so the three documents below can `concat!` it: one
917    /// spelling of these fields is what makes an issue read through the board-scoped
918    /// search, through its own node id, and through its project's sub-issue relationship
919    /// resolve to *the same* item, which is the whole of what
920    /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
921    ///
922    /// `projectItems` is what carries the board half of an issue: the board item's own id
923    /// and the [`board_item_values!`] above — the `Status` option and this source's origin
924    /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
925    /// issue rather than on the board, which is what makes the cost of a read proportional
926    /// to what was asked for instead of to the board's size.
927    ///
928    /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
929    /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
930    /// not on that page: a page here is where the search for the entry starts rather than
931    /// where it ends.
932    ///
933    /// It does **not** select the board's `Labels` field value, and that is the whole of
934    /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
935    /// a label connection there sits under `fieldValues` under `projectItems` under a page
936    /// of issues, spending `$nestedFirst` twice down one path, and took
937    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
938    /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
939    /// above, and that connection is where every label this source reports comes from. No
940    /// document in this module selects the board field any longer, [`BOARD`] included; the
941    /// module documentation records why nothing it could have held is lost.
942    macro_rules! board_issue {
943        () => {
944            concat!(
945                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}
946      labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
947      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
948        "#,
949                board_item_values!(),
950                r#"}pageInfo{hasNextPage endCursor}}}"#
951            )
952        };
953    }
954
955    /// Every issue of one board, found by a search scoped to that board.
956    ///
957    /// This is how the projects a board holds are listed, and it selects no `items`
958    /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
959    /// container walked page by page, so nothing nested inside a board item is paid for.
960    /// Which of the issues it returns is a project is then read off `parent` — GitHub
961    /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
962    /// discriminator has to be applied to the field, which is a scalar on the issue and
963    /// costs nothing.
964    pub const SEARCH_ISSUES: &str = concat!(
965        r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
966        relationship_events_variable!(),
967        r#"){
968      search(query:$search,type:$type,first:$first,after:$after){
969        pageInfo{hasNextPage endCursor}
970        nodes{__typename ...BoardIssue}
971      }
972    }"#,
973        board_issue!()
974    );
975
976    /// What a dependency read selects of each far end: enough to say which kind of item it
977    /// is, its body included for the kind marker.
978    macro_rules! related_issue {
979        () => {
980            " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
981        };
982    }
983
984    /// One issue by its own node id, which is what a qualified id names here — with what a
985    /// write of it needs and the issue does not carry in `board_issue!`: the field
986    /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
987    ///
988    /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
989    /// answers a write made moments ago with the value from before it, and resolving a node
990    /// id does not.
991    ///
992    /// **Why those two ride here and not on the fragment.** A copy or an update of an item
993    /// reads it by its own id, and with them that one read answers everything the write
994    /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
995    /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
996    /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
997    /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
998    /// they sit under one item, and this read is still one point.
999    pub const ISSUE: &str = concat!(
1000        r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1001        relationship_events_variable!(),
1002        r#"){
1003      node(id:$id){__typename ...BoardIssue ... on Issue{
1004        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
1005          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1006          ... on ProjectV2Field{__typename id name dataType}
1007        }pageInfo{hasNextPage}}}}}
1008        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1009      }}
1010    }"#,
1011        board_issue!(),
1012        related_issue!()
1013    );
1014
1015    /// One project's tasks: the sub-issues of the issue that project is, each with a page of
1016    /// what blocks it.
1017    ///
1018    /// Paged at `$memberFirst` — [`MEMBER_PAGE_SIZE`](super::MEMBER_PAGE_SIZE), the most rows
1019    /// one point buys — rather than at `$first`, because every row carries six connections and
1020    /// a hundred-row page cost six points to list a project of ten.
1021    ///
1022    /// The work this costs is the project's own size. Nothing about it grows as the board
1023    /// gains projects, or as those projects gain tasks.
1024    ///
1025    /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
1026    /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
1027    /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
1028    /// costs grow with its tasks rather than with the pages of them. Carried here, a task
1029    /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
1030    /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
1031    /// It adds a connection under each issue of the page — one rate-limit point per page,
1032    /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
1033    /// answer questions that never read an edge.
1034    ///
1035    /// **The project issue itself rides here too**, with the page of what blocks it, because
1036    /// the reader of a project's tasks reads the project next: a copy writing the project reads
1037    /// it and its edges before it writes them. Carried on the request that already names the
1038    /// project, the first page answers that read and the dependency read after it, at the
1039    /// state GitHub answered in this command, rather than spending one more read on an item
1040    /// the cache's token cannot vouch for whole. Each sub-issue is kept the same way, so a
1041    /// copy's read of a task this listing carried is answered by it rather than sent again.
1042    /// Only a copy's read-before-write takes them; every other read by id reads GitHub.
1043    pub const SUB_ISSUES: &str = concat!(
1044        r#"query($id:ID!,$memberFirst:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1045        relationship_events_variable!(),
1046        r#"){
1047      node(id:$id){__typename ...BoardIssue
1048        ... on Issue{blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1049          subIssues(first:$memberFirst,after:$after){
1050          pageInfo{hasNextPage endCursor}
1051          nodes{__typename ...BoardIssue ... on Issue{
1052            blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1053          }}
1054        }}}
1055    }"#,
1056        board_issue!(),
1057        related_issue!()
1058    );
1059
1060    /// What a read of the board's own `items` selects of each item's content.
1061    ///
1062    /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1063    /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1064    /// content by one spelling.
1065    macro_rules! board_item_content {
1066        () => {
1067            r#" content{
1068        ... 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}}}
1069        ... on PullRequest{__typename id}
1070        ... on DraftIssue{__typename id title body createdAt updatedAt}
1071      }"#
1072        };
1073    }
1074
1075    /// Reads the board's fields and one page of its items.
1076    pub const BOARD: &str = concat!(
1077        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1078        relationship_events_variable!(),
1079        r#"){
1080      owner:repositoryOwner(login:$owner){
1081        ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1082      }
1083    } fragment Board on ProjectV2 { id title
1084      fields(first:$nestedFirst){nodes{
1085        ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1086        ... on ProjectV2Field{__typename id name dataType}
1087      }pageInfo{hasNextPage}}
1088      items(first:$first,after:$after){nodes{id updatedAt "#,
1089        board_item_values!(),
1090        board_item_content!(),
1091        r#"} pageInfo{hasNextPage endCursor}}
1092    }"#
1093    );
1094
1095    /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1096    /// the board.
1097    ///
1098    /// **`originItems`** is the board's own items narrowed by its own field filter —
1099    /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1100    /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1101    /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1102    /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1103    /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1104    /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1105    /// fresh `addProjectV2ItemById` the way that connection does.
1106    ///
1107    /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1108    /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1109    /// indexes that comment, and the index catches up with a write in a second or two rather
1110    /// than in minutes, so it finds a carrier another process wrote that the first read is
1111    /// still behind on.
1112    ///
1113    /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1114    /// — and resumes from its own cursor; a connection already walked to its end is resumed
1115    /// from its last cursor, which answers an empty page. Every candidate either read returns
1116    /// is confirmed against its own origin field before it is reported, so a token match of
1117    /// the search or anything else the filter admits never is.
1118    ///
1119    /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1120    /// own whole reads counts this one among them.
1121    pub const ORIGIN_LOOKUP: &str = concat!(
1122        r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1123        relationship_events_variable!(),
1124        r#"){
1125      originItems:repositoryOwner(login:$owner){
1126        ... on ProjectV2Owner{projectV2(number:$number){
1127          items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1128        board_item_values!(),
1129        board_item_content!(),
1130        r#"} pageInfo{hasNextPage endCursor}}
1131        }}
1132      }
1133      search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1134        pageInfo{hasNextPage endCursor}
1135        nodes{__typename ...BoardIssue}
1136      }
1137    }"#,
1138        board_issue!()
1139    );
1140
1141    /// Every closed item of the board, by the board's own field filter — what
1142    /// `task status reconcile` reads, and nothing else of the board.
1143    ///
1144    /// **`ProjectV2.items(query:)` narrowed by `$filter`**, which this source binds to
1145    /// `is:closed` alone, so a board whose open items are most of it is not read whole to find
1146    /// the closed ones. It is narrowed rather than trusted: every item it answers is confirmed
1147    /// closed, with a reason and a card that contradict each other, from the item node it
1148    /// already carries, so a filter that also let open items through would cost reads and
1149    /// write nothing wrong.
1150    ///
1151    /// The board's own id and field definitions ride along, as [`BOARD`] carries them, so the
1152    /// option writes that follow need no read of the board. The root is aliased `closedItems`
1153    /// rather than `owner`, so nothing counting the board's own whole reads counts this one
1154    /// among them.
1155    pub const CLOSED_ITEMS: &str = concat!(
1156        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$filter:String!,$nestedFirst:Int!,$duplicates:Boolean!"#,
1157        relationship_events_variable!(),
1158        r#"){
1159      closedItems:repositoryOwner(login:$owner){
1160        ... on ProjectV2Owner{projectV2(number:$number){id
1161          fields(first:$nestedFirst){nodes{
1162            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1163            ... on ProjectV2Field{__typename id name dataType}
1164          }pageInfo{hasNextPage}}
1165          items(first:$first,after:$after,query:$filter){nodes{id updatedAt "#,
1166        board_item_values!(),
1167        board_item_content!(),
1168        r#"} pageInfo{hasNextPage endCursor}}
1169        }}
1170      }
1171    }"#
1172    );
1173
1174    /// One alias of [`ISSUES_BY_REFERENCE`]: the issue `$owner<n>/$name<n>#$number<n>` names.
1175    macro_rules! reference_alias {
1176        ($n:literal) => {
1177            concat!(
1178                "\n      r",
1179                stringify!($n),
1180                ":repository(owner:$owner",
1181                stringify!($n),
1182                ",name:$name",
1183                stringify!($n),
1184                "){issue(number:$number",
1185                stringify!($n),
1186                "){...HeldIssue}}"
1187            )
1188        };
1189    }
1190
1191    /// [`REFERENCE_BATCH`](super::REFERENCE_BATCH) issues at once, each by its repository and
1192    /// number — the one read `task status complete` makes of a source, and nothing else of
1193    /// the board: no `items`, no search and no project read.
1194    ///
1195    /// **Its own fragment, lighter than `board_issue!`, and that is what makes fifty
1196    /// references one point.** It selects no labels, and of each board membership only the
1197    /// `Status` value, by name, with `fieldValueByName` — so each alias asks GitHub for one
1198    /// connection, its page of memberships, where the whole fragment asks for five. What a
1199    /// completion writes is a card and, at most, the issue's state, so the labels and the other
1200    /// field values it leaves out are never what it needs; an item read this way is held for
1201    /// that write alone and never answers another read. An issue whose entry for this board is
1202    /// past its first page of memberships is walked with [`ISSUE_BOARD_ITEMS`], exactly as
1203    /// every other read of an issue recovers one.
1204    ///
1205    /// Fixed-size, as [`ISSUE_DETAILS`] is, so there is one document to price: a batch of
1206    /// fewer references binds the slots it has none for to its last one.
1207    pub const ISSUES_BY_REFERENCE: &str = concat!(
1208        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!"#,
1209        relationship_events_variable!(),
1210        r#"){"#,
1211        reference_alias!(0),
1212        reference_alias!(1),
1213        reference_alias!(2),
1214        reference_alias!(3),
1215        reference_alias!(4),
1216        reference_alias!(5),
1217        reference_alias!(6),
1218        reference_alias!(7),
1219        reference_alias!(8),
1220        reference_alias!(9),
1221        reference_alias!(10),
1222        reference_alias!(11),
1223        reference_alias!(12),
1224        reference_alias!(13),
1225        reference_alias!(14),
1226        reference_alias!(15),
1227        reference_alias!(16),
1228        reference_alias!(17),
1229        reference_alias!(18),
1230        reference_alias!(19),
1231        reference_alias!(20),
1232        reference_alias!(21),
1233        reference_alias!(22),
1234        reference_alias!(23),
1235        reference_alias!(24),
1236        reference_alias!(25),
1237        reference_alias!(26),
1238        reference_alias!(27),
1239        reference_alias!(28),
1240        reference_alias!(29),
1241        reference_alias!(30),
1242        reference_alias!(31),
1243        reference_alias!(32),
1244        reference_alias!(33),
1245        reference_alias!(34),
1246        reference_alias!(35),
1247        reference_alias!(36),
1248        reference_alias!(37),
1249        reference_alias!(38),
1250        reference_alias!(39),
1251        reference_alias!(40),
1252        reference_alias!(41),
1253        reference_alias!(42),
1254        reference_alias!(43),
1255        reference_alias!(44),
1256        reference_alias!(45),
1257        reference_alias!(46),
1258        reference_alias!(47),
1259        reference_alias!(48),
1260        reference_alias!(49),
1261        "\n    }",
1262        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}
1263      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1264        status:fieldValueByName(name:$statusField){... on ProjectV2ItemFieldSingleSelectValue{name field{
1265          ... on ProjectV2SingleSelectField{id name options{id name}}
1266        }}}
1267      }pageInfo{hasNextPage endCursor}}}"#
1268    );
1269
1270    /// The board's own id and field definitions, and not one of its items.
1271    ///
1272    /// What a write needs of the board when the item it writes does not say: the id a field
1273    /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1274    /// origin fields. It selects no `items`, so what it costs is the board's field list
1275    /// however many items the board holds — and it decides nothing about which items those
1276    /// are, which is the question a read of one item by its own id answers instead.
1277    ///
1278    /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1279    /// board's item reads by their root counts this one among them.
1280    pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1281      boardFields:repositoryOwner(login:$owner){
1282        ... on ProjectV2Owner{projectV2(number:$number){id
1283          fields(first:$nestedFirst){nodes{
1284            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1285            ... on ProjectV2Field{__typename id name dataType}
1286          }pageInfo{hasNextPage}}
1287        }}
1288      }
1289    }"#;
1290
1291    /// One board draft by its own node id, with the board item it sits in.
1292    ///
1293    /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1294    /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1295    /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1296    /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1297    /// board listing hands it to, and nothing has to list the board to find one.
1298    pub const DRAFT: &str = concat!(
1299        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1300      node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1301        projectV2Items(first:$boardItems){nodes{id project{id number}
1302        "#,
1303        board_item_values!(),
1304        r#"}pageInfo{hasNextPage endCursor}}}}
1305    }"#
1306    );
1307
1308    /// One issue's board memberships alone, walked past the page a read of it carried.
1309    ///
1310    /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1311    /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1312    /// boards than that page holds may have this board's entry past its end. This asks that
1313    /// one issue for its memberships and nothing else — the caller already holds the issue —
1314    /// so an answer of "this board does not hold it" is only ever given about a connection
1315    /// read to exhaustion.
1316    ///
1317    /// It selects the board item's id, its project number and the same
1318    /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1319    /// very same resolver: an issue recovered this way reports the same title, the same
1320    /// status, the same labels and the same qualified id as one whose entry was on the
1321    /// page.
1322    ///
1323    /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1324    /// multiplies through it and the membership connection can be walked at
1325    /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1326    /// further request for any issue a person really keeps.
1327    pub const ISSUE_BOARD_ITEMS: &str = concat!(
1328        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1329      node(id:$id){
1330        ... on Issue{projectItems(first:$first,after:$after){
1331          nodes{id updatedAt project{id number}
1332        "#,
1333        board_item_values!(),
1334        r#"}
1335          pageInfo{hasNextPage endCursor}}}
1336      }
1337    }"#
1338    );
1339    /// Whether the board's Project is public — half of what decides whether a write here can
1340    /// be read by anybody. Needs the `read:project` scope.
1341    pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1342    /// Resolves the configured repository's node id, which creating an issue requires.
1343    pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1344    /// What creating an issue needs and has not read yet: the board's own id and field
1345    /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1346    /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1347    ///
1348    /// Sent at the point a create knows which repository it is for, when neither half is
1349    /// already known to this process; a create needing only one of them sends that one's own
1350    /// document. Neither half is kept past the process: a field's option ids are re-minted by
1351    /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1352    /// status.
1353    pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1354      boardFields:repositoryOwner(login:$owner){
1355        ... on ProjectV2Owner{projectV2(number:$number){id
1356          fields(first:$nestedFirst){nodes{
1357            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1358            ... on ProjectV2Field{__typename id name dataType}
1359          }pageInfo{hasNextPage}}
1360        }}
1361      }
1362      repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1363    }"#;
1364    /// Reads both dependency directions for one issue, with each far end's own kind — and
1365    /// the issue's own body, which is where an edge to another source is recorded, so that
1366    /// half of a dependency read needs no second read of the issue or of the board.
1367    pub const ISSUE_DEPENDENCIES: &str = concat!(
1368        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1369      ... on Issue{body
1370        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1371        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1372      }}}"#,
1373        related_issue!()
1374    );
1375    /// What one item depends on: [`ISSUE_DEPENDENCIES`] narrowed to `blockedBy`, so a
1376    /// dependency read asks GitHub for the one direction it answers. The issue's `body` rides
1377    /// along because its metadata slot is where an edge to another source is recorded — and a
1378    /// board draft's `body` too, with the one board item GitHub links a draft to and, aliased
1379    /// `board`, this board's own id, so a draft's recorded edges are read off this same answer
1380    /// and only when that item is this board's: no draft read and no board read follow it.
1381    ///
1382    /// The draft's board item is asked for under a root of its own, aliased `draft`, rather
1383    /// than beside its `body` under `node`. GitHub counts the type-conditional fragments of
1384    /// one selection as the largest of them — `blockedBy`'s 100 alone, where this workspace's
1385    /// calculation adds the draft's 3 — while separate roots are summed by both, so this is
1386    /// the spelling whose node count GitHub and the calculation agree on. Both price it at
1387    /// one point; the credentialed lane's reconciliation is what holds the two to that.
1388    pub const ISSUE_DEPENDS_ON: &str = concat!(
1389        r#"query($id:ID!,$first:Int!,$after:String,$owner:String!,$number:Int!,$boardItems:Int!){
1390      node(id:$id){__typename
1391      ... on Issue{body
1392        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1393      }
1394      ... on DraftIssue{id body}}
1395      draft:node(id:$id){... on DraftIssue{projectV2Items(first:$boardItems){nodes{project{id number}}pageInfo{hasNextPage}}}}
1396      board:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){id}}}}"#,
1397        related_issue!()
1398    );
1399    /// What depends on one issue: [`ISSUE_DEPENDENCIES`] with its `blocking` alone. No `body`:
1400    /// the reverse of a recorded edge is derived from the far end, never written down here.
1401    pub const ISSUE_DEPENDED_ON_BY: &str = concat!(
1402        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1403      ... on Issue{
1404        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1405      }}}"#,
1406        related_issue!()
1407    );
1408    /// Creates one issue. Outside a copy it is created on no board, and [`ADD_TO_BOARD`] files
1409    /// it; inside one, its input names the board in `projectV2Ids` and its project in
1410    /// `parentIssueId`, so this one request files it under both — see
1411    /// [`GitHubProjectsSource::create_filed_issue`](super::GitHubProjectsSource).
1412    pub const CREATE_ISSUE: &str =
1413        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1414    /// Puts an existing issue on the configured board.
1415    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1416    /// Updates an issue's visible fields and its open or closed state in one call.
1417    pub const UPDATE_ISSUE: &str =
1418        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1419    /// Updates an existing draft's user-visible fields.
1420    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1421    /// Updates a text or single-select value on one project item.
1422    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}}}}}}}}"#;
1423    /// Writes up to six board fields and clears up to three in one ordered mutation: every
1424    /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1425    /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1426    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}}}"#;
1427    /// Clears one project item's value of one field, which is what a `none` priority is.
1428    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}}}}}}}}"#;
1429    /// Creates one field: a single-select one with its options, or a text one for a projected
1430    /// metadata value. Only the guarded field setup may use this document, and only for a
1431    /// field the board lacks.
1432    pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1433    /// Replaces a single-select field's options. Only the guarded field setup — the
1434    /// `status-options` and `fields` operations — may use this document, because GitHub
1435    /// treats the input as the complete option list.
1436    pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1437    /// A fresh snapshot of the Status field and every board item's assignment.
1438    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}}}}}}"#;
1439    /// Files one issue under another as a sub-issue, which is what project membership is.
1440    pub const ADD_SUB_ISSUE: &str =
1441        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1442    /// Takes one issue back out of its parent.
1443    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1444    /// Adds GitHub's native issue blocked-by relationship.
1445    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1446    /// One origin of [`ORIGIN_LOOKUPS`]: [`ORIGIN_LOOKUP`]'s two connections for the origin the
1447    /// `$filter<n>` and `$search<n>` of slot `<n>` name, sent only while `$ask<n>` holds.
1448    macro_rules! origin_lookup_alias {
1449        ($n:literal) => {
1450            concat!(
1451                "\n      o",
1452                stringify!($n),
1453                ":repositoryOwner(login:$owner) @include(if:$ask",
1454                stringify!($n),
1455                "){... on ProjectV2Owner{projectV2(number:$number){items(first:$originFirst,query:$filter",
1456                stringify!($n),
1457                "){nodes{id updatedAt ",
1458                board_item_values!(),
1459                board_item_content!(),
1460                "} pageInfo{hasNextPage endCursor}}}}}\n      s",
1461                stringify!($n),
1462                ":search(query:$search",
1463                stringify!($n),
1464                ",type:$type,first:$originFirst) @include(if:$ask",
1465                stringify!($n),
1466                "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}"
1467            )
1468        };
1469    }
1470
1471    /// The first page of [`ORIGIN_LOOKUP`] for up to [`ORIGIN_BATCH`](super::ORIGIN_BATCH)
1472    /// origins a copy is about to look for, and everything creating an item then reads —
1473    /// the board's own id and field definitions, as [`CREATION_CONTEXT`] reads them, and the node
1474    /// id and the numeric id of the repository an issue is created in — in one request.
1475    ///
1476    /// What a copy reads before it writes anything, so that finding none of its records on the
1477    /// board and creating every one costs this read and the writes. An origin whose page says
1478    /// there is more is left to its own [`ORIGIN_LOOKUP`]. The repository's numeric id is what an
1479    /// image upload names, so a copy carrying assets needs no `GET /repos/{owner}/{repo}` either.
1480    pub const ORIGIN_LOOKUPS: &str = concat!(
1481        r#"query($owner:String!,$number:Int!,$type:SearchType!,$originFirst: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!"#,
1482        relationship_events_variable!(),
1483        r#"){
1484      boardFields:repositoryOwner(login:$owner){
1485        ... on ProjectV2Owner{projectV2(number:$number){id
1486          fields(first:$nestedFirst){nodes{
1487            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1488            ... on ProjectV2Field{__typename id name dataType}
1489          }pageInfo{hasNextPage}}
1490        }}
1491      }
1492      repository(owner:$repositoryOwner,name:$repositoryName){id databaseId nameWithOwner}"#,
1493        origin_lookup_alias!(0),
1494        origin_lookup_alias!(1),
1495        origin_lookup_alias!(2),
1496        origin_lookup_alias!(3),
1497        origin_lookup_alias!(4),
1498        origin_lookup_alias!(5),
1499        origin_lookup_alias!(6),
1500        origin_lookup_alias!(7),
1501        origin_lookup_alias!(8),
1502        origin_lookup_alias!(9),
1503        "\n    }",
1504        board_issue!()
1505    );
1506
1507    /// One slot `<n>` of a copy's aliased writes: `$<prefix><n>` is its input and `$send<n>`
1508    /// whether it is sent.
1509    macro_rules! write_slot {
1510        ($prefix:literal, $field:literal, $selection:literal, $n:literal) => {
1511            concat!(
1512                " ",
1513                $prefix,
1514                stringify!($n),
1515                ":",
1516                $field,
1517                "(input:$",
1518                $prefix,
1519                stringify!($n),
1520                ") @include(if:$send",
1521                stringify!($n),
1522                "){",
1523                $selection,
1524                "}"
1525            )
1526        };
1527    }
1528
1529    /// The variables of a copy's aliased writes: eleven inputs of `$type` named `$<prefix><n>`,
1530    /// and eleven `$send<n>`.
1531    macro_rules! write_variables {
1532        ($prefix:literal, $type:literal) => {
1533            concat!(
1534                "mutation(",
1535                "$",
1536                $prefix,
1537                "0:",
1538                $type,
1539                "!,$",
1540                $prefix,
1541                "1:",
1542                $type,
1543                "!,$",
1544                $prefix,
1545                "2:",
1546                $type,
1547                "!,$",
1548                $prefix,
1549                "3:",
1550                $type,
1551                "!,$",
1552                $prefix,
1553                "4:",
1554                $type,
1555                "!,$",
1556                $prefix,
1557                "5:",
1558                $type,
1559                "!,$",
1560                $prefix,
1561                "6:",
1562                $type,
1563                "!,$",
1564                $prefix,
1565                "7:",
1566                $type,
1567                "!,$",
1568                $prefix,
1569                "8:",
1570                $type,
1571                "!,$",
1572                $prefix,
1573                "9:",
1574                $type,
1575                "!,$",
1576                $prefix,
1577                "10:",
1578                $type,
1579                "!,",
1580                "$send0:Boolean!,$send1:Boolean!,$send2:Boolean!,$send3:Boolean!,$send4:Boolean!,",
1581                "$send5:Boolean!,$send6:Boolean!,$send7:Boolean!,$send8:Boolean!,$send9:Boolean!,",
1582                "$send10:Boolean!){"
1583            )
1584        };
1585    }
1586
1587    /// A copy's aliased writes of one kind: [`MUTATION_BATCH`](super::MUTATION_BATCH) slots of
1588    /// `$field`, each sent only while its `$send<n>` holds.
1589    macro_rules! write_batch {
1590        ($prefix:literal, $type:literal, $field:literal, $selection:literal) => {
1591            concat!(
1592                write_variables!($prefix, $type),
1593                write_slot!($prefix, $field, $selection, 0),
1594                write_slot!($prefix, $field, $selection, 1),
1595                write_slot!($prefix, $field, $selection, 2),
1596                write_slot!($prefix, $field, $selection, 3),
1597                write_slot!($prefix, $field, $selection, 4),
1598                write_slot!($prefix, $field, $selection, 5),
1599                write_slot!($prefix, $field, $selection, 6),
1600                write_slot!($prefix, $field, $selection, 7),
1601                write_slot!($prefix, $field, $selection, 8),
1602                write_slot!($prefix, $field, $selection, 9),
1603                write_slot!($prefix, $field, $selection, 10),
1604                "}"
1605            )
1606        };
1607    }
1608
1609    /// Creates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues in one request, each filed on
1610    /// the board and under its project by the same `createIssue` — its `projectV2Ids` and
1611    /// `parentIssueId` — as [`CREATE_ISSUE`] creates one.
1612    pub const CREATE_ISSUES: &str = write_batch!(
1613        "create",
1614        "CreateIssueInput",
1615        "createIssue",
1616        "issue{id number url}"
1617    );
1618
1619    /// One issue of [`CREATED_BOARD_ITEMS`]: the board items the issue `$id<n>` names is on, with
1620    /// the `Status` value of each, read only while `$read<n>` holds.
1621    macro_rules! created_item_alias {
1622        ($n:literal) => {
1623            concat!(
1624                "\n      i",
1625                stringify!($n),
1626                ":node(id:$id",
1627                stringify!($n),
1628                ") @include(if:$read",
1629                stringify!($n),
1630                "){... on Issue{id projectItems(first:$boardItems){nodes{id project{id} status:fieldValueByName(name:$statusName){... on ProjectV2ItemFieldSingleSelectValue{name}}}}}}"
1631            )
1632        };
1633    }
1634
1635    /// The board item of each issue a copy created, up to
1636    /// [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH) at a time, with its `Status` as the board
1637    /// set it: what `createIssue`'s `projectV2Ids` files the issue as, which GitHub adds after
1638    /// answering the create, so its id comes from a read made once every create has been sent.
1639    pub const CREATED_BOARD_ITEMS: &str = concat!(
1640        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!){"#,
1641        created_item_alias!(0),
1642        created_item_alias!(1),
1643        created_item_alias!(2),
1644        created_item_alias!(3),
1645        created_item_alias!(4),
1646        created_item_alias!(5),
1647        created_item_alias!(6),
1648        created_item_alias!(7),
1649        created_item_alias!(8),
1650        created_item_alias!(9),
1651        created_item_alias!(10),
1652        created_item_alias!(11),
1653        created_item_alias!(12),
1654        created_item_alias!(13),
1655        created_item_alias!(14),
1656        created_item_alias!(15),
1657        created_item_alias!(16),
1658        created_item_alias!(17),
1659        created_item_alias!(18),
1660        created_item_alias!(19),
1661        created_item_alias!(20),
1662        "\n    }"
1663    );
1664
1665    /// Up to [`MUTATION_BATCH`](super::MUTATION_BATCH) board field values a copy writes on the
1666    /// items it created, each on its own item, in one ordered request.
1667    pub const WRITE_FIELD_VALUES: &str = write_batch!(
1668        "field",
1669        "UpdateProjectV2ItemFieldValueInput",
1670        "updateProjectV2ItemFieldValue",
1671        "projectV2Item{id}"
1672    );
1673
1674    /// Closes up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues a copy created at a closing
1675    /// status, each with its own `stateInput` and nothing else, once its option is selected.
1676    pub const CLOSE_ISSUES: &str =
1677        write_batch!("close", "UpdateIssueInput", "updateIssue", "issue{id}");
1678
1679    /// Up to [`MUTATION_BATCH`](super::MUTATION_BATCH) native blocked-by relationships a copy adds
1680    /// to the issues it created, in one request.
1681    pub const ADD_BLOCKED_BYS: &str = write_batch!(
1682        "block",
1683        "AddBlockedByInput",
1684        "addBlockedBy",
1685        "issue{id} blockingIssue{id}"
1686    );
1687
1688    /// Removes one native issue blocked-by relationship.
1689    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1690    /// Deletes one issue, which takes its board item with it.
1691    ///
1692    /// The engine sends this in one situation only: undoing a copy that could not finish,
1693    /// over the items that same copy created. Deleting the issue removes the board item
1694    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1695    pub const DELETE_ISSUE: &str =
1696        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1697
1698    /// Everything this source reads about one issue comment, wherever it reaches one.
1699    ///
1700    /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1701    /// and a comment just edited are handed to one mapper, so they are selected by one
1702    /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1703    /// longer exists, and `login` is the one member every kind of actor carries.
1704    macro_rules! issue_comment {
1705        () => {
1706            "id author{login} createdAt updatedAt body url"
1707        };
1708    }
1709
1710    /// One task's comments: a page of its issue's own `comments` connection.
1711    ///
1712    /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1713    /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1714    /// list every time somebody edited it; left unordered the connection answers in the order
1715    /// the comments were written, which is the order GitHub documents for the same collection
1716    /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1717    /// node count and the caller's own page size is pushed straight down.
1718    pub const ISSUE_COMMENTS: &str = concat!(
1719        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1720        issue_comment!(),
1721        r#"}pageInfo{hasNextPage endCursor}}}}}"#
1722    );
1723    /// One issue by its own node id, with a page of its comments: what `task show` and a
1724    /// comment listing read, in one request.
1725    ///
1726    /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1727    /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1728    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1729    /// connection there would multiply through both of those documents' price, and neither
1730    /// needs one.
1731    pub const ISSUE_DETAIL: &str = concat!(
1732        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1733        relationship_events_variable!(),
1734        r#"){
1735      node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1736        issue_comment!(),
1737        r#"}pageInfo{hasNextPage endCursor}}}}
1738    }"#,
1739        board_issue!()
1740    );
1741
1742    /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1743    /// page of its comments when `$comments` asks for them.
1744    macro_rules! issue_details_alias {
1745        ($n:literal) => {
1746            concat!(
1747                "\n      i",
1748                stringify!($n),
1749                ":node(id:$id",
1750                stringify!($n),
1751                "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1752                issue_comment!(),
1753                "}pageInfo{hasNextPage endCursor}}}}"
1754            )
1755        };
1756    }
1757
1758    /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1759    /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1760    ///
1761    /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1762    /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1763    /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1764    /// neither — so every connection under it would be priced at nothing and the pin in
1765    /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1766    /// one-item read the model already prices, so the batch costs what its aliases cost.
1767    ///
1768    /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1769    /// slots it has no item for to the last item it does, and reads that item again; the
1770    /// price is the document's, whatever its variables, so a short batch costs what a full
1771    /// one does and nothing more.
1772    pub const ISSUE_DETAILS: &str = concat!(
1773        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!"#,
1774        relationship_events_variable!(),
1775        r#"){"#,
1776        issue_details_alias!(0),
1777        issue_details_alias!(1),
1778        issue_details_alias!(2),
1779        issue_details_alias!(3),
1780        issue_details_alias!(4),
1781        issue_details_alias!(5),
1782        issue_details_alias!(6),
1783        issue_details_alias!(7),
1784        issue_details_alias!(8),
1785        issue_details_alias!(9),
1786        issue_details_alias!(10),
1787        issue_details_alias!(11),
1788        issue_details_alias!(12),
1789        issue_details_alias!(13),
1790        issue_details_alias!(14),
1791        issue_details_alias!(15),
1792        issue_details_alias!(16),
1793        issue_details_alias!(17),
1794        issue_details_alias!(18),
1795        issue_details_alias!(19),
1796        issue_details_alias!(20),
1797        issue_details_alias!(21),
1798        issue_details_alias!(22),
1799        issue_details_alias!(23),
1800        "\n    }",
1801        board_issue!()
1802    );
1803
1804    /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1805    /// page of its comments and the issues it is blocked by.
1806    macro_rules! issue_copy_details_alias {
1807        ($n:literal) => {
1808            concat!(
1809                "\n      i",
1810                stringify!($n),
1811                ":node(id:$id",
1812                stringify!($n),
1813                "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1814                issue_comment!(),
1815                "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1816            )
1817        };
1818    }
1819
1820    /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1821    /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1822    /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1823    /// at a time, so a copy of a changed task costs this read and nothing else.
1824    ///
1825    /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1826    /// what `task show-many` is priced by and which reads no edge.
1827    ///
1828    /// `children`, read only while `$children` holds, is which issues the issue `$project` has
1829    /// as sub-issues — each one's id and title, the page `$nestedFirst` allows — so that a
1830    /// project copy, which reads its project and every task at once, knows from this same read
1831    /// which tasks the project holds at the destination, and its orphan check is answered by it
1832    /// rather than by a listing of the project.
1833    pub const ISSUE_COPY_DETAILS: &str = concat!(
1834        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!"#,
1835        relationship_events_variable!(),
1836        r#"){"#,
1837        issue_copy_details_alias!(0),
1838        issue_copy_details_alias!(1),
1839        issue_copy_details_alias!(2),
1840        issue_copy_details_alias!(3),
1841        issue_copy_details_alias!(4),
1842        issue_copy_details_alias!(5),
1843        issue_copy_details_alias!(6),
1844        issue_copy_details_alias!(7),
1845        issue_copy_details_alias!(8),
1846        issue_copy_details_alias!(9),
1847        issue_copy_details_alias!(10),
1848        issue_copy_details_alias!(11),
1849        issue_copy_details_alias!(12),
1850        issue_copy_details_alias!(13),
1851        issue_copy_details_alias!(14),
1852        issue_copy_details_alias!(15),
1853        issue_copy_details_alias!(16),
1854        issue_copy_details_alias!(17),
1855        issue_copy_details_alias!(18),
1856        issue_copy_details_alias!(19),
1857        issue_copy_details_alias!(20),
1858        "\n      children:node(id:$project) @include(if:$children){... on Issue{id subIssues(first:$nestedFirst){nodes{id title} pageInfo{hasNextPage}}}}",
1859        "\n    }",
1860        board_issue!(),
1861        related_issue!()
1862    );
1863
1864    /// Which issue one comment is on, read before that comment is edited or removed.
1865    ///
1866    /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1867    /// comment id given against the wrong task would change a comment on another issue.
1868    pub const COMMENT_ISSUE: &str =
1869        r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1870    /// Adds one comment to an issue, signed as the account the token belongs to.
1871    pub const ADD_COMMENT: &str = concat!(
1872        r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1873        issue_comment!(),
1874        r#"}}}}"#
1875    );
1876    /// Replaces the body of one issue comment.
1877    pub const UPDATE_COMMENT: &str = concat!(
1878        r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
1879        issue_comment!(),
1880        r#"}}}"#
1881    );
1882    /// Removes one issue comment. Its payload carries nothing about the comment it removed.
1883    pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
1884
1885    /// What a mutation of a write the store's cache expects selects of the issue it wrote:
1886    /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
1887    /// change token's every part and the first page of its comments.
1888    ///
1889    /// Sent **only** while that write runs, in place of the document named beside each
1890    /// below, so every other write's documents, prices and request counts are what they
1891    /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
1892    /// mutation's response is stamped seconds after the change it made, so against GitHub
1893    /// itself a response rarely determines a version; the cache is then left unserveable
1894    /// for the task, and its next read refreshes it.
1895    macro_rules! whole_issue {
1896        () => {
1897            concat!(
1898                r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
1899                issue_comment!(),
1900                r#"}pageInfo{hasNextPage endCursor}}}"#
1901            )
1902        };
1903    }
1904
1905    /// The variables every document of `whole_issue!` declares beside its own.
1906    macro_rules! whole_variables {
1907        () => {
1908            concat!(
1909                r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1910                relationship_events_variable!()
1911            )
1912        };
1913    }
1914
1915    /// [`ADD_COMMENT`], selecting the whole issue commented on.
1916    pub const ADD_COMMENT_WHOLE: &str = concat!(
1917        r#"mutation($input:AddCommentInput!"#,
1918        whole_variables!(),
1919        r#"){addComment(input:$input){subject{id "#,
1920        whole_issue!(),
1921        r#"} commentEdge{node{"#,
1922        issue_comment!(),
1923        r#"}}}}"#,
1924        board_issue!()
1925    );
1926    /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
1927    pub const UPDATE_COMMENT_WHOLE: &str = concat!(
1928        r#"mutation($input:UpdateIssueCommentInput!"#,
1929        whole_variables!(),
1930        r#"){updateIssueComment(input:$input){issueComment{"#,
1931        issue_comment!(),
1932        r#" issue{"#,
1933        whole_issue!(),
1934        r#"}}}}"#,
1935        board_issue!()
1936    );
1937    /// [`UPDATE_ISSUE`], selecting the whole issue updated.
1938    pub const UPDATE_ISSUE_WHOLE: &str = concat!(
1939        r#"mutation($input:UpdateIssueInput!"#,
1940        whole_variables!(),
1941        r#"){updateIssue(input:$input){issue{id "#,
1942        whole_issue!(),
1943        r#"}}}"#,
1944        board_issue!()
1945    );
1946    /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
1947    pub const UPDATE_FIELD_WHOLE: &str = concat!(
1948        r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
1949        whole_variables!(),
1950        r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
1951        whole_issue!(),
1952        r#"}}}}"#,
1953        board_issue!()
1954    );
1955    /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
1956    pub const CLEAR_FIELD_WHOLE: &str = concat!(
1957        r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
1958        whole_variables!(),
1959        r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
1960        whole_issue!(),
1961        r#"}}}}"#,
1962        board_issue!()
1963    );
1964
1965    /// The document sent in `document`'s place while a write the store's cache expects runs,
1966    /// when it has one.
1967    #[must_use]
1968    pub fn whole_twin(document: &str) -> Option<&'static str> {
1969        [
1970            (ADD_COMMENT, ADD_COMMENT_WHOLE),
1971            (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
1972            (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
1973            (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
1974            (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
1975        ]
1976        .into_iter()
1977        .find(|(plain, _)| *plain == document)
1978        .map(|(_, whole)| whole)
1979    }
1980
1981    /// Every document above, with what this source is doing when it sends one.
1982    ///
1983    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
1984    /// name the call that was refused, and a `match` with a catch-all arm would answer a
1985    /// document added later with "talking to GitHub" and never say so.
1986    ///
1987    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
1988    /// const` here that this list omits, so the two cannot part — which is the same guard
1989    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
1990    pub const DOCUMENTS: [(&str, &str); 50] = [
1991        (SEARCH_ISSUES, "searching this board's issues"),
1992        (ISSUE, "reading one issue"),
1993        (
1994            ISSUE_BOARD_ITEMS,
1995            "reading one issue's board memberships past the page it came with",
1996        ),
1997        (SUB_ISSUES, "reading a project's tasks"),
1998        (BOARD, "reading the board"),
1999        (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
2000        (CLOSED_ITEMS, "reading the board's closed items"),
2001        (
2002            ISSUES_BY_REFERENCE,
2003            "reading a batch of issues by their references",
2004        ),
2005        (BOARD_FIELDS, "reading the board's fields"),
2006        (DRAFT, "reading one draft"),
2007        (REPOSITORY, "reading the destination repository"),
2008        (
2009            PROJECT_VISIBILITY,
2010            "reading whether the board's project is public",
2011        ),
2012        (
2013            CREATION_CONTEXT,
2014            "reading the board's fields and the destination repository",
2015        ),
2016        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
2017        (ISSUE_DEPENDS_ON, "reading what an item depends on"),
2018        (ISSUE_DEPENDED_ON_BY, "reading what depends on an issue"),
2019        (CREATE_ISSUE, "creating an issue"),
2020        (
2021            ORIGIN_LOOKUPS,
2022            "looking up the items copied from several origins, and what creating one reads",
2023        ),
2024        (CREATE_ISSUES, "creating several issues together"),
2025        (
2026            CREATED_BOARD_ITEMS,
2027            "reading the board items of the issues a copy created",
2028        ),
2029        (
2030            WRITE_FIELD_VALUES,
2031            "writing board fields of the items a copy created",
2032        ),
2033        (CLOSE_ISSUES, "closing the issues a copy created"),
2034        (ADD_BLOCKED_BYS, "recording several dependencies together"),
2035        (ADD_TO_BOARD, "adding an issue to the board"),
2036        (UPDATE_ISSUE, "updating an issue"),
2037        (UPDATE_DRAFT, "updating a draft item"),
2038        (UPDATE_FIELD, "writing a board field"),
2039        (UPDATE_FIELDS, "writing board fields together"),
2040        (CLEAR_FIELD, "clearing a board field"),
2041        (
2042            CREATE_FIELD,
2043            "creating a board single-select field with its options",
2044        ),
2045        (
2046            STATUS_OPTIONS_SNAPSHOT,
2047            "snapshotting board Status options and assignments",
2048        ),
2049        (
2050            STATUS_OPTIONS_UPDATE,
2051            "safely replacing the board Status option list",
2052        ),
2053        (ADD_SUB_ISSUE, "filing an issue under its project"),
2054        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
2055        (ADD_BLOCKED_BY, "recording a dependency"),
2056        (REMOVE_BLOCKED_BY, "removing a dependency"),
2057        (DELETE_ISSUE, "deleting an issue"),
2058        (ISSUE_COMMENTS, "reading a task's comments"),
2059        (ISSUE_DETAIL, "reading one issue with its comments"),
2060        (
2061            ISSUE_DETAILS,
2062            "reading a batch of issues with their comments",
2063        ),
2064        (
2065            ISSUE_COPY_DETAILS,
2066            "reading a batch of issues with their comments and dependencies for a copy",
2067        ),
2068        (COMMENT_ISSUE, "reading which issue a comment is on"),
2069        (ADD_COMMENT, "adding a comment"),
2070        (UPDATE_COMMENT, "editing a comment"),
2071        (DELETE_COMMENT, "deleting a comment"),
2072        (
2073            ADD_COMMENT_WHOLE,
2074            "adding a comment, answered with the whole issue",
2075        ),
2076        (
2077            UPDATE_COMMENT_WHOLE,
2078            "editing a comment, answered with the whole issue",
2079        ),
2080        (
2081            UPDATE_ISSUE_WHOLE,
2082            "updating an issue, answered with it whole",
2083        ),
2084        (
2085            UPDATE_FIELD_WHOLE,
2086            "writing a board field, answered with the whole issue",
2087        ),
2088        (
2089            CLEAR_FIELD_WHOLE,
2090            "clearing a board field, answered with the whole issue",
2091        ),
2092    ];
2093}
2094
2095/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
2096/// under, the variable carrying its input, and the boolean variable that includes it.
2097#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2098pub struct FieldSlot {
2099    /// The key of the answer's `data` this field's payload is under.
2100    pub alias: &'static str,
2101    /// The variable carrying this field's input.
2102    pub variable: &'static str,
2103    /// The boolean variable whose `true` sends this field.
2104    pub include: &'static str,
2105}
2106
2107/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
2108pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
2109    FieldSlot {
2110        alias: "updateProjectV2ItemFieldValue",
2111        variable: "input",
2112        include: "writeFirst",
2113    },
2114    FieldSlot {
2115        alias: "second",
2116        variable: "second",
2117        include: "writeSecond",
2118    },
2119    FieldSlot {
2120        alias: "third",
2121        variable: "third",
2122        include: "writeThird",
2123    },
2124    FieldSlot {
2125        alias: "fourth",
2126        variable: "fourth",
2127        include: "writeFourth",
2128    },
2129    FieldSlot {
2130        alias: "fifth",
2131        variable: "fifth",
2132        include: "writeFifth",
2133    },
2134    FieldSlot {
2135        alias: "sixth",
2136        variable: "sixth",
2137        include: "writeSixth",
2138    },
2139];
2140
2141/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
2142pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
2143    FieldSlot {
2144        alias: "cleared",
2145        variable: "clear",
2146        include: "writeClear",
2147    },
2148    FieldSlot {
2149        alias: "clearedSecond",
2150        variable: "clearSecond",
2151        include: "writeClearSecond",
2152    },
2153    FieldSlot {
2154        alias: "clearedThird",
2155        variable: "clearThird",
2156        include: "writeClearThird",
2157    },
2158];
2159
2160/// Which of GitHub's two rate limiters refused a request.
2161///
2162/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
2163/// secondary one — so an operator told the wrong one takes the wrong next step, which is
2164/// the whole reason this is carried rather than collapsed into "rate limited".
2165#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2166enum Limiter {
2167    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
2168    Primary,
2169    /// The burst limiter over content-generating requests, which nothing reports.
2170    Secondary,
2171}
2172
2173/// The wordings GitHub answers a secondary rate limit with.
2174///
2175/// It sends them under a forbidden status, under a too-many-requests status, and inside
2176/// the `errors` of a *successful* response, which is why the text is what this matches on
2177/// rather than the status. `abuse detection` is the wording GitHub used before the
2178/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
2179/// what a burst of content creation is refused with.
2180///
2181/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
2182/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
2183/// when, and the drift gate reconciles the two lists both ways. Public for that gate
2184/// alone — a caller has no use for it, and matching on a refusal is this source's job.
2185pub const SECONDARY_WORDINGS: [&str; 5] = [
2186    "secondary rate limit",
2187    "temporarily blocked from content creation",
2188    "abuse detection",
2189    "submitted too quickly",
2190    "exceeded a secondary",
2191];
2192
2193/// The wordings GitHub answers an exhausted primary budget with.
2194///
2195/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
2196/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
2197/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
2198/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
2199/// two phrases is a substring of it, so without it that answer read as a refusal that will
2200/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
2201/// one reason.
2202pub const PRIMARY_WORDINGS: [&str; 4] = [
2203    "api rate limit exceeded",
2204    "api rate limit already exceeded",
2205    "rate limit exceeded",
2206    "rate_limited",
2207];
2208
2209/// What a response *says about itself*, which is the only place a refusal can be read.
2210///
2211/// Deliberately not the whole response body. A board is a place people write about their
2212/// own work, and a task on it titled "the secondary rate limit" would, matched across the
2213/// raw text, turn a perfectly good answer into a refusal this source then waited out and
2214/// reported. So the item data is never read: what is read is GitHub's own REST-style
2215/// `message` envelope, which is what a forbidden status carries, and the `message` and
2216/// `type` of each GraphQL error, which is where a *successful* response says it.
2217///
2218/// A body that is not JSON at all has nothing structured to read, so only a failing
2219/// response's own text is taken — a successful response that is not JSON is malformed
2220/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
2221fn refusal_wording(status: StatusCode, body: &str) -> String {
2222    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
2223        return if status.is_success() {
2224            String::new()
2225        } else {
2226            body.to_owned()
2227        };
2228    };
2229    let mut said: Vec<&str> = parsed
2230        .get("message")
2231        .and_then(Value::as_str)
2232        .into_iter()
2233        .collect();
2234    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
2235        for error in errors {
2236            said.extend(
2237                ["message", "type"]
2238                    .into_iter()
2239                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
2240            );
2241        }
2242    }
2243    said.join("; ")
2244}
2245
2246impl Limiter {
2247    /// Which limiter refused this response, or `None` when none of them did.
2248    ///
2249    /// The wording is read first and the status only decides what carries none of it,
2250    /// because GitHub answers a secondary limit with a forbidden status far more often
2251    /// than with too-many-requests — while a forbidden status saying nothing about a limit
2252    /// really is a credential this token lacks.
2253    ///
2254    /// A response is a refusal because of its status or its own wording. A spent budget
2255    /// only ever explains one; it never turns an answer into a refusal.
2256    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
2257        let normalized = refusal_wording(status, body).to_ascii_lowercase();
2258        if SECONDARY_WORDINGS
2259            .iter()
2260            .any(|wording| normalized.contains(wording))
2261        {
2262            return Some(Self::Secondary);
2263        }
2264        if status == StatusCode::TOO_MANY_REQUESTS {
2265            return Some(Self::Primary);
2266        }
2267        // An exhausted budget *explains* a response that failed; it does not make one that
2268        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
2269        // request the budget allowed as well as on the ones it then refuses, so reading
2270        // the header alone threw away a good answer — and, once refusals were retried,
2271        // replayed a request that had already taken effect.
2272        if !status.is_success() && budget_exhausted {
2273            return Some(Self::Primary);
2274        }
2275        // A successful response saying it: GitHub reports a GraphQL rate limit in the
2276        // `errors` of an HTTP 200, where nothing about the status says so at all.
2277        if status.is_success()
2278            && PRIMARY_WORDINGS
2279                .iter()
2280                .any(|wording| normalized.contains(wording))
2281        {
2282            return Some(Self::Primary);
2283        }
2284        None
2285    }
2286
2287    /// What this limiter is called where an operator can look it up.
2288    const fn name(self) -> &'static str {
2289        match self {
2290            Self::Primary => "GitHub's primary API rate limit",
2291            Self::Secondary => "GitHub's secondary rate limit",
2292        }
2293    }
2294
2295    /// What the endpoint an operator would go and check says about this limiter.
2296    const fn where_to_look(self) -> &'static str {
2297        match self {
2298            Self::Primary => {
2299                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
2300                 comes back."
2301            }
2302            Self::Secondary => {
2303                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
2304                 primary budget and does not report this one, so budget showing there says \
2305                 nothing about this refusal, and every further attempt extends it."
2306            }
2307        }
2308    }
2309
2310    /// The next step this limiter actually calls for.
2311    const fn what_to_do(self) -> &'static str {
2312        match self {
2313            Self::Primary => {
2314                "wait for the reset `gh api rate_limit` reports, then run the command again."
2315            }
2316            Self::Secondary => {
2317                "leave this board alone for a few minutes, then run the command again — or \
2318                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
2319            }
2320        }
2321    }
2322}
2323
2324/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
2325#[derive(Debug, Clone, Copy)]
2326struct Limited {
2327    limiter: Limiter,
2328    hint: Option<u64>,
2329    /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
2330    /// `x-ratelimit-reset`.
2331    hinted_by: Hint,
2332}
2333
2334/// Where a refusal's hint came from, which is what its wait is named after.
2335#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2336enum Hint {
2337    /// `retry-after`, or no hint at all.
2338    RetryAfter,
2339    /// An exhausted primary budget's `x-ratelimit-reset`.
2340    PrimaryReset,
2341}
2342
2343impl Limited {
2344    /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
2345    /// and the cause it is named by: the hint, raised to the schedule's own next wait —
2346    /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
2347    /// extends a secondary limit — or the schedule alone when there is none.
2348    fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
2349        match (self.hint, self.hinted_by) {
2350            (Some(hint), Hint::RetryAfter) => (
2351                Duration::from_secs(hint).max(backoff),
2352                pacing::cause::RETRY_AFTER,
2353            ),
2354            (Some(hint), Hint::PrimaryReset) => (
2355                Duration::from_secs(hint).max(backoff),
2356                pacing::cause::PRIMARY_RESET,
2357            ),
2358            (None, _) => (backoff, pacing::cause::BACKOFF),
2359        }
2360    }
2361
2362    /// What the caller is told once this source has waited as long as it may.
2363    ///
2364    /// Both limiters report as [`SourceError::RateLimited`], because that is what
2365    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2366    /// about *which* limiter it was makes it a different kind of failure. What differs is
2367    /// the operator's next step, and that is what the message carries — a secondary
2368    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2369    /// budget looks fine, and then back to retry the very burst that was refused.
2370    fn exhausted(
2371        self,
2372        doing: &str,
2373        waits: u32,
2374        waited: Duration,
2375        needed: Duration,
2376        budget: Duration,
2377    ) -> SourceError {
2378        SourceError::RateLimited {
2379            retry_after_seconds: self.hint,
2380            message: Some(format!(
2381                "{} refused this source while {doing}; it waited {} out over {} and was refused \
2382                 again, and the next wait of {} would take it past the {} one call may spend \
2383                 waiting. {} next: {}",
2384                self.limiter.name(),
2385                plural(waits, "refusal"),
2386                seconds(waited),
2387                seconds(needed),
2388                seconds(budget),
2389                self.limiter.where_to_look(),
2390                self.limiter.what_to_do(),
2391            )),
2392        }
2393    }
2394}
2395
2396/// One HTTP attempt's result, with what its response said about the rate limit.
2397///
2398/// The two travel together so the record and the outcome are written from the same place:
2399/// what a response said about the budget is only readable while that response is in hand,
2400/// and what the attempt *meant* is only decidable once its body has been read.
2401struct Attempted {
2402    result: Result<Value, Attempt>,
2403    limits: accounting::RateLimit,
2404    /// GitHub's own reported cost for this call, for a document that asked for it.
2405    reported_cost: Option<u64>,
2406    response_outcome: accounting::ResponseOutcome,
2407}
2408
2409/// One attempt's outcome: an error to report, or a rate limit to wait out.
2410enum Attempt {
2411    Failed(SourceError),
2412    Limited(Limited),
2413}
2414
2415fn plural(count: u32, thing: &str) -> String {
2416    if count == 1 {
2417        format!("{count} {thing}")
2418    } else {
2419        format!("{count} {thing}s")
2420    }
2421}
2422
2423fn seconds(duration: Duration) -> String {
2424    format!("{:.1}s", duration.as_secs_f64())
2425}
2426
2427/// The member [`stamp_served`] writes onto every node of a response that says when it last
2428/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2429const SERVED_AT: &str = "onetaskgraphServedAt";
2430
2431/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2432/// node's time later knows the server's clock on the response that read it — which is what
2433/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2434fn stamp_served(data: &mut Value, served: &str) {
2435    match data {
2436        Value::Object(members) => {
2437            if members.contains_key("updatedAt") {
2438                members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2439            }
2440            for member in members.values_mut() {
2441                stamp_served(member, served);
2442            }
2443        }
2444        Value::Array(items) => {
2445            for item in items {
2446                stamp_served(item, served);
2447            }
2448        }
2449        _ => {}
2450    }
2451}
2452
2453/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2454///
2455/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2456/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2457/// header, and neither is what makes a response a refusal — so the whole cost of one this
2458/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2459/// instead. Refusing the response over the header would turn a readable refusal into an
2460/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2461fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2462    value
2463        .and_then(|value| value.to_str().ok())
2464        .and_then(|value| value.trim().parse::<u64>().ok())
2465}
2466
2467/// A transport failure with every cause beneath it, outermost first.
2468///
2469/// The client's own text for a request it could not complete is `error sending request for
2470/// url (…)`, and the socket error that says why — a refused or reset connection, an exhausted
2471/// port range, a connection closed before its answer — is only in the chain of sources below
2472/// it. Those differ in what to do next, so all of them are kept.
2473pub(crate) fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
2474    let mut text = error.to_string();
2475    let mut cause = error.source();
2476    while let Some(next) = cause {
2477        let said = next.to_string();
2478        if !text.ends_with(&said) {
2479            text = format!("{text}: {said}");
2480        }
2481        cause = next.source();
2482    }
2483    text
2484}
2485
2486/// Every mutation this source sends creates content — an issue, a board item, a field of
2487/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2488/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2489/// and what the keyword says are the same set. That is what makes the keyword a sound test
2490/// rather than a convenient one: counting an edit or a removal the limiter might not have
2491/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
2492pub(crate) fn is_mutation(query: &str) -> bool {
2493    query.trim_start().starts_with("mutation")
2494}
2495
2496/// What this source was doing, for a diagnostic that has to say so.
2497///
2498/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2499/// a document added without a description is caught by that list's own gate instead of
2500/// falling through to the vague arm below.
2501fn operation_description(query: &str) -> &'static str {
2502    graphql::DOCUMENTS
2503        .iter()
2504        .find(|(document, _)| *document == query)
2505        .map_or("talking to GitHub", |(_, doing)| *doing)
2506}
2507
2508/// GitHub's published ceiling on content-generating requests in any one minute.
2509///
2510/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2511/// GitHub's number rather than this source's. This source paces at it over its window: a
2512/// content-creating request goes out at once while the minute's creations leave room for it,
2513/// and waits until they do otherwise — see the `pacing` module.
2514pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2515/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
2516/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
2517pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2518/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
2519/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
2520/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
2521pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
2522/// The secondary points one GraphQL request carrying a mutation is charged.
2523pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
2524/// The secondary points one GraphQL request carrying no mutation is charged.
2525pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
2526/// The fastest *steady* spacing of content-creating mutations that cannot exceed
2527/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
2528///
2529/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
2530/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
2531/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
2532/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
2533/// value that holds a long sustained burst under the per-minute bound by spacing alone.
2534pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2535/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2536///
2537/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2538/// own advice for a secondary limit — wait, and wait longer each time — without spending
2539/// the first minute of a transient refusal doing nothing.
2540pub const RETRY_BACKOFF_MS: u64 = 1_000;
2541/// Total time one call may spend waiting out rate limits before it reports a failure.
2542///
2543/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2544/// short enough that a command an operator is watching returns. The bound is what makes
2545/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2546/// the limiter, not in a process nobody can tell from a wedged one.
2547pub const RETRY_BUDGET_MS: u64 = 120_000;
2548
2549fn default_token_env() -> String {
2550    "GH_PROJECTS_TOKEN".to_owned()
2551}
2552fn default_endpoint() -> String {
2553    "https://api.github.com/graphql".to_owned()
2554}
2555
2556/// The name of a `Status` single-select option on the board.
2557///
2558/// Validated on the way in rather than checked later, so a blank option name — which
2559/// nothing on a board can be — is a state this type cannot hold.
2560#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2561#[serde(try_from = "String")]
2562#[schemars(extend("minLength" = 1))]
2563pub struct ColumnName(String);
2564
2565impl ColumnName {
2566    /// The option name, as the board spells it.
2567    fn as_str(&self) -> &str {
2568        &self.0
2569    }
2570}
2571
2572impl TryFrom<String> for ColumnName {
2573    type Error = String;
2574
2575    fn try_from(name: String) -> Result<Self, Self::Error> {
2576        if name.trim().is_empty() {
2577            return Err("a status_mapping option name cannot be blank".to_owned());
2578        }
2579        Ok(Self(name))
2580    }
2581}
2582
2583/// The two closed states this product can mean.
2584///
2585/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2586/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2587/// product never decides on a person's behalf that two issues are the same work.
2588#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2589#[serde(rename_all = "kebab-case")]
2590pub enum ClosedState {
2591    /// `COMPLETED` — precisely done.
2592    Completed,
2593    /// `NOT_PLANNED` — precisely cancelled.
2594    NotPlanned,
2595}
2596
2597impl ClosedState {
2598    const fn reason(self) -> &'static str {
2599        match self {
2600            Self::Completed => "COMPLETED",
2601            Self::NotPlanned => "NOT_PLANNED",
2602        }
2603    }
2604}
2605
2606/// Configuration for one GitHub Projects v2 board.
2607///
2608/// It serializes to a document this same type reads back as itself, so a caller writing a
2609/// configuration — a consumer filing onto a board it configures — can build one here rather
2610/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2611#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2612#[serde(default, deny_unknown_fields)]
2613pub struct GitHubProjectsConfig {
2614    /// Login of the user or organization which owns the board.
2615    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2616    /// The project number shown in the board's GitHub URL.
2617    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2618    // 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.
2619    /// `owner/name` of the repository this source creates an issue in when the item's own
2620    /// `repositories` field does not decide it.
2621    ///
2622    /// An item naming exactly one repository is created there; a task or a document naming
2623    /// none or several is created in its parent project's repository; and a project, or a
2624    /// task or document with no parent, naming none or several is created here. A board
2625    /// has no repository of its own and `createIssue` requires one, so a write without
2626    /// this is refused naming the field. Reads never need it.
2627    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2628    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2629    /// Environment variable containing a fine-grained token with Projects and Issues
2630    /// read/write plus Pull requests read-only access for every repository represented on
2631    /// the board.
2632    #[serde(default = "default_token_env")]
2633    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2634    /// GraphQL endpoint. GitHub Enterprise installations may override it.
2635    #[serde(default = "default_endpoint")]
2636    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2637    /// Per-instance mapping from a status category to the option of the board's one
2638    /// `Status` field it lands on, for a task and for a project.
2639    ///
2640    /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2641    /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2642    /// where a kind left out leaves the category unmapped for that kind. A category this
2643    /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2644    /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2645    /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2646    /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2647    /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2648    /// either kind. No two categories may name one option for the same kind, ignoring case.
2649    /// `unknown` may name one existing option; every unknown word then lands on it and
2650    /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2651    /// each unknown word because it never creates board options.
2652    #[serde(default)]
2653    pub status_mapping: StatusMapping,
2654    /// Per-instance mapping from a task's priority to an option of this board's
2655    /// single-select field named `Priority`.
2656    ///
2657    /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2658    /// other priority is refused before it reaches this board. Present, each of `urgent`,
2659    /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2660    /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2661    /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2662    /// no two levels may name one option. Reads and writes never create the field or an
2663    /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2664    /// the board lacks is refused pointing there.
2665    #[serde(default)]
2666    pub priority_mapping: Option<PriorityMappingConfig>,
2667    /// Caller metadata values this source also writes to board text fields, so the board can
2668    /// filter on them.
2669    ///
2670    /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2671    /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2672    /// value. On every item write — create, update and copy, of a task, a project or a
2673    /// document — a string found there is written to the field, and nothing is sent when the
2674    /// field already holds it; nothing there, or `null`, clears the field when it holds a
2675    /// value; any other JSON type is refused before any mutation. The metadata comment in the
2676    /// issue body stays the one home of the value, and every read reports metadata from it.
2677    /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2678    /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2679    /// non-text field of that name, is refused pointing there.
2680    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2681    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2682    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2683    #[schemars(!skip_serializing_if)]
2684    pub metadata_fields: Vec<MetadataFieldConfig>,
2685    /// How fast this source writes, and how long it waits out a rate-limit refusal.
2686    ///
2687    /// Every field keeps its shipped default when it is absent, and the defaults are
2688    /// GitHub's own published limits rather than taste. See [`Pacing`].
2689    #[serde(default)]
2690    pub pacing: PacingConfig,
2691}
2692
2693/// Which option of the board's `Priority` field each priority lands on.
2694///
2695/// One member per level rather than a map, so a key that is not a level is refused where
2696/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2697/// value in the field, not an option of it.
2698#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2699#[serde(default, deny_unknown_fields)]
2700pub struct PriorityMappingConfig {
2701    /// The option `urgent` lands on; `Urgent` when absent.
2702    pub urgent: Option<PriorityOptionName>,
2703    /// The option `high` lands on; `High` when absent.
2704    pub high: Option<PriorityOptionName>,
2705    /// The option `medium` lands on; `Medium` when absent.
2706    pub medium: Option<PriorityOptionName>,
2707    /// The option `low` lands on; `Low` when absent.
2708    pub low: Option<PriorityOptionName>,
2709}
2710
2711/// The name of an option of the board's `Priority` single-select field.
2712///
2713/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2714/// blank name.
2715#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2716#[serde(try_from = "String")]
2717#[schemars(extend("minLength" = 1))]
2718pub struct PriorityOptionName(String);
2719
2720impl PriorityOptionName {
2721    /// The option name, as the board spells it.
2722    fn as_str(&self) -> &str {
2723        &self.0
2724    }
2725}
2726
2727impl TryFrom<String> for PriorityOptionName {
2728    type Error = String;
2729
2730    fn try_from(name: String) -> Result<Self, Self::Error> {
2731        if name.trim().is_empty() {
2732            return Err("a priority_mapping option name cannot be blank".to_owned());
2733        }
2734        Ok(Self(name))
2735    }
2736}
2737
2738/// The name of the board field a priority is held in.
2739pub const PRIORITY_FIELD: &str = "Priority";
2740
2741/// The four priorities a board option can hold, in the order a new `Priority` field lists
2742/// them. `none` is not among them: it is the field holding no value.
2743///
2744/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2745/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2746/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2747/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2748pub const PRIORITY_LEVELS: [Priority; 4] = [
2749    Priority::Urgent,
2750    Priority::High,
2751    Priority::Medium,
2752    Priority::Low,
2753];
2754
2755/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2756/// see that list for what this pins.
2757#[must_use]
2758pub const fn level_position(priority: Priority) -> Option<usize> {
2759    match priority {
2760        Priority::None => None,
2761        Priority::Urgent => Some(0),
2762        Priority::High => Some(1),
2763        Priority::Medium => Some(2),
2764        Priority::Low => Some(3),
2765    }
2766}
2767
2768/// This instance's complete priority-to-option mapping, read in both directions.
2769///
2770/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2771/// two levels name one option.
2772#[derive(Debug, Clone)]
2773struct PriorityMapping {
2774    options: [PriorityOptionName; 4],
2775}
2776
2777impl PriorityMapping {
2778    fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2779        let shipped = |name: &str| PriorityOptionName(name.to_owned());
2780        let mapping = Self {
2781            options: [
2782                config.urgent.unwrap_or_else(|| shipped("Urgent")),
2783                config.high.unwrap_or_else(|| shipped("High")),
2784                config.medium.unwrap_or_else(|| shipped("Medium")),
2785                config.low.unwrap_or_else(|| shipped("Low")),
2786            ],
2787        };
2788        for (index, option) in mapping.options.iter().enumerate() {
2789            if let Some(earlier) = mapping.options[..index]
2790                .iter()
2791                .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2792            {
2793                return Err(SourceError::Config {
2794                    message: format!(
2795                        "priority_mapping of source {instance} sends both {} and {} to the board \
2796                         option {:?}; one option cannot read back as two priorities",
2797                        PRIORITY_LEVELS[earlier],
2798                        PRIORITY_LEVELS[index],
2799                        option.as_str()
2800                    ),
2801                });
2802            }
2803        }
2804        Ok(mapping)
2805    }
2806
2807    /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2808    fn option(&self, priority: Priority) -> Option<&str> {
2809        level_position(priority).map(|index| self.options[index].as_str())
2810    }
2811
2812    /// The priority a board option name reports, or `None` when nothing maps to it.
2813    fn priority_of(&self, option: &str) -> Option<Priority> {
2814        self.options
2815            .iter()
2816            .position(|name| name.as_str().eq_ignore_ascii_case(option))
2817            .map(|index| PRIORITY_LEVELS[index])
2818    }
2819
2820    /// Every mapped option name, in the order a new `Priority` field lists them.
2821    fn names(&self) -> impl Iterator<Item = &str> {
2822        self.options.iter().map(PriorityOptionName::as_str)
2823    }
2824}
2825
2826/// One metadata value this source projects onto a board text field.
2827#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2828#[serde(deny_unknown_fields)]
2829pub struct MetadataFieldConfig {
2830    /// The name of the board text field the value is written to.
2831    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.
2832    /// The top-level metadata key the value is read from. A key under the reserved
2833    /// `onetaskgraph.` prefix is refused.
2834    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.
2835    /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2836    /// key's own value is the one projected. Left out of a serialized entry when empty.
2837    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2838    // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
2839    #[schemars(!skip_serializing_if)]
2840    pub path: Vec<String>,
2841}
2842
2843/// The board fields no metadata value may be projected onto, compared ignoring case: the
2844/// three this source writes itself, and those GitHub owns on every board.
2845// 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.
2846const UNPROJECTABLE_FIELDS: [&str; 13] = [
2847    STATUS_FIELD,
2848    PRIORITY_FIELD,
2849    ORIGIN_FIELD,
2850    "Title",
2851    "Assignees",
2852    "Labels",
2853    "Linked pull requests",
2854    "Milestone",
2855    "Repository",
2856    "Reviewers",
2857    "Parent issue",
2858    "Sub-issues progress",
2859    "Type",
2860];
2861
2862/// One validated [`MetadataFieldConfig`] entry.
2863#[derive(Debug, Clone)]
2864struct MetadataField {
2865    field: ProjectedField,
2866    key: ProjectedKey,
2867    path: Vec<String>,
2868}
2869
2870/// The name of a board text field a metadata value is projected onto: never blank, never one
2871/// this source or GitHub writes, and never one another entry of the same source names.
2872#[derive(Debug, Clone, PartialEq, Eq)]
2873struct ProjectedField(String);
2874
2875/// The top-level metadata key a projected value is read from: never blank, and never under
2876/// the namespace this product reserves.
2877#[derive(Debug, Clone, PartialEq, Eq)]
2878struct ProjectedKey(String);
2879
2880impl std::ops::Deref for ProjectedField {
2881    type Target = str;
2882    fn deref(&self) -> &str {
2883        &self.0
2884    }
2885}
2886
2887impl std::ops::Deref for ProjectedKey {
2888    type Target = str;
2889    fn deref(&self) -> &str {
2890        &self.0
2891    }
2892}
2893
2894/// What one item's metadata says one projected field should hold.
2895#[derive(Debug, Clone, PartialEq, Eq)]
2896enum Projected {
2897    /// A string to hold.
2898    Text(String),
2899    /// Nothing: the key or a step of the path is absent, or the value is `null`.
2900    Nothing,
2901}
2902
2903impl MetadataField {
2904    /// Validate every entry of one instance's `metadata_fields`, refusing the first that
2905    /// cannot stand, naming the source and the entry.
2906    fn resolve(
2907        configured: Vec<MetadataFieldConfig>,
2908        instance: &SourceName,
2909    ) -> Result<Vec<Self>, SourceError> {
2910        let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
2911        for (index, entry) in configured.into_iter().enumerate() {
2912            let refuse = |why: String| SourceError::Config {
2913                message: format!(
2914                    "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
2915                    entry.field, entry.key
2916                ),
2917            };
2918            if entry.field.trim().is_empty() {
2919                return Err(refuse("names a blank field".to_owned()));
2920            }
2921            if entry.key.trim().is_empty() {
2922                return Err(refuse("names a blank key".to_owned()));
2923            }
2924            if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
2925                return Err(refuse(format!(
2926                    "names a key under the reserved \"{}.\" prefix, which this product keeps \
2927                     for its own keys; next: project a caller key instead",
2928                    MetadataKey::RESERVED_NAMESPACE
2929                )));
2930            }
2931            if let Some(owned) = UNPROJECTABLE_FIELDS
2932                .iter()
2933                .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
2934            {
2935                return Err(refuse(format!(
2936                    "names the board field {owned:?}, which this source or GitHub already \
2937                     writes; next: name a text field of your own"
2938                )));
2939            }
2940            if let Some(earlier) = resolved
2941                .iter()
2942                .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
2943            {
2944                return Err(refuse(format!(
2945                    "names the same board field as metadata_fields[{earlier}]; one field \
2946                     cannot hold two values"
2947                )));
2948            }
2949            resolved.push(Self {
2950                field: ProjectedField(entry.field),
2951                key: ProjectedKey(entry.key),
2952                path: entry.path,
2953            });
2954        }
2955        Ok(resolved)
2956    }
2957
2958    /// The path as a refusal spells it: `[]` for the key's own value.
2959    fn spelled_path(&self) -> String {
2960        serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
2961    }
2962
2963    /// What `metadata` says this field should hold, or the refusal for a value no text field
2964    /// can hold.
2965    fn projected(
2966        &self,
2967        metadata: &BTreeMap<String, Value>,
2968        instance: &SourceName,
2969    ) -> Result<Projected, SourceError> {
2970        let mut value = metadata.get(&*self.key);
2971        for step in &self.path {
2972            value = value.and_then(|held| held.get(step.as_str()));
2973        }
2974        let found = match value {
2975            None | Some(Value::Null) => return Ok(Projected::Nothing),
2976            Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
2977            Some(Value::Bool(_)) => "a boolean",
2978            Some(Value::Number(_)) => "a number",
2979            Some(Value::Array(_)) => "an array",
2980            Some(Value::Object(_)) => "an object",
2981        };
2982        Err(SourceError::Refused {
2983            message: format!(
2984                "source {instance} projects metadata key {:?} at path {} onto its board text \
2985                 field {:?}, and this item holds {found} there; only a string, null or nothing \
2986                 can be written to a text field; next: store a string there, or remove the \
2987                 value",
2988                &*self.key,
2989                self.spelled_path(),
2990                &*self.field
2991            ),
2992        })
2993    }
2994}
2995
2996/// What one item's `Priority` field says, read through this instance's mapping.
2997#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
2998enum HeldPriority {
2999    /// A priority this source reports: an option the mapping names, or no value (`none`).
3000    Read(Priority),
3001    /// An option the mapping does not name, which is never read as a level or as `none`.
3002    Unmapped(String),
3003}
3004
3005/// How fast this source writes, and how long it waits out a rate-limit refusal.
3006///
3007/// Configurable because a GitHub Enterprise installation sets its own limits and an
3008/// operator who has already been refused may want to go slower still — not because the
3009/// defaults are guesses.
3010#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3011#[serde(default, deny_unknown_fields)]
3012pub struct PacingConfig {
3013    /// Shortest interval between two content-creating mutations, in milliseconds.
3014    ///
3015    /// Zero sends them as fast as they are asked for, which is what a fixture server on
3016    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
3017    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.
3018    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
3019    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
3020    /// zero while there is a budget to spend, because a schedule of zero-length waits
3021    /// consumes none of it and so never ends.
3022    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.
3023    /// Total time one call may spend waiting out rate limits, in milliseconds.
3024    ///
3025    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
3026    /// the bound is what makes this a wait rather than a hang.
3027    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.
3028}
3029
3030/// The largest any pacing setting may be, in milliseconds.
3031///
3032/// One hour. GitHub's own harshest published bound on content-generating requests works
3033/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
3034/// anything a real limit asks for, and past it the settings stop describing pacing at all:
3035/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
3036/// and an interval beyond it is a command that never sends its second mutation. It also
3037/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
3038/// what a `Duration` can hold on every platform.
3039pub const MAX_PACING_MS: u64 = 3_600_000;
3040
3041/// [`PacingConfig`] with every default resolved and every value checked, which is what the
3042/// source holds.
3043///
3044/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
3045/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
3046/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
3047/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
3048/// is asked for, windows included.
3049#[derive(Debug, Clone, Copy)]
3050struct Pacing {
3051    /// Whether requests are held for GitHub's secondary windows: always, unless
3052    /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
3053    /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
3054    windows: bool,
3055    min_mutation_interval: Duration,
3056    retry_backoff: Duration,
3057    retry_budget: Duration,
3058}
3059
3060impl Pacing {
3061    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
3062    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3063        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
3064            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
3065                message: format!(
3066                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3067                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3068                     GitHub's own harshest published limit"
3069                ),
3070            }),
3071            Some(value) => Ok(Duration::from_millis(value)),
3072            None => Ok(Duration::from_millis(default)),
3073        };
3074        let retry_backoff = bounded(
3075            config.retry_backoff_ms,
3076            RETRY_BACKOFF_MS,
3077            "retry_backoff_ms",
3078        )?;
3079        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3080        if retry_backoff.is_zero() && !retry_budget.is_zero() {
3081            return Err(SourceError::Config {
3082                message: format!(
3083                    "pacing.retry_backoff_ms of source {instance} is 0 while \
3084                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3085                     none of that budget, so it would retry a refusal forever. Set a backoff of \
3086                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3087                     waiting at all",
3088                    retry_budget.as_millis()
3089                ),
3090            });
3091        }
3092        Ok(Self {
3093            windows: config.min_mutation_interval_ms != Some(0),
3094            min_mutation_interval: bounded(
3095                config.min_mutation_interval_ms,
3096                0,
3097                "min_mutation_interval_ms",
3098            )?,
3099            retry_backoff,
3100            retry_budget,
3101        })
3102    }
3103}
3104
3105/// Factory for [`GitHubProjectsSource`].
3106#[derive(Debug, Clone, Copy, Default)]
3107pub struct Plugin;
3108
3109impl SourcePlugin for Plugin {
3110    fn kind(&self) -> &'static str {
3111        KIND
3112    }
3113    /// The endpoint, the owner and the board's number: what names one board.
3114    fn remote_identity(&self, config: &Value) -> Option<String> {
3115        let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
3116        Some(format!(
3117            "{}\n{}\n{}",
3118            config.endpoint, config.owner, config.project_number
3119        ))
3120    }
3121    fn config_schema(&self) -> Schema {
3122        schema_for!(GitHubProjectsConfig)
3123    }
3124    fn build(
3125        &self,
3126        name: &SourceName,
3127        config: &Value,
3128        secrets: &dyn SecretResolver,
3129    ) -> Result<Box<dyn TaskSource>, SourceError> {
3130        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
3131    }
3132
3133    fn build_with_clock(
3134        &self,
3135        name: &SourceName,
3136        config: &Value,
3137        secrets: &dyn SecretResolver,
3138        clock: SharedClock,
3139    ) -> Result<Box<dyn TaskSource>, SourceError> {
3140        self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
3141    }
3142}
3143
3144/// The client `cell` holds, building it with `build` the first time it is asked for.
3145///
3146/// A failed build is the request's failure, with the message the source's construction gave it
3147/// before clients were deferred.
3148// 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`.
3149fn built(
3150    cell: &std::sync::OnceLock<Client>,
3151    build: impl FnOnce() -> Result<Client, SourceError>,
3152) -> Result<&Client, SourceError> {
3153    if let Some(client) = cell.get() {
3154        return Ok(client);
3155    }
3156    let client = build()?;
3157    Ok(cell.get_or_init(|| client))
3158}
3159
3160impl Plugin {
3161    /// Build a source recording every request it sends into an accounting the caller holds.
3162    ///
3163    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
3164    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
3165    /// session total rather than two — see [`accounting`] and
3166    /// [`GitHubProjectsSource::recording_into`].
3167    ///
3168    /// # Errors
3169    ///
3170    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
3171    /// [`SourceError::Config`] for configuration this plugin cannot use and
3172    /// [`SourceError::Auth`] for a credential it cannot find.
3173    pub fn build_recording_into(
3174        &self,
3175        name: &SourceName,
3176        config: &Value,
3177        secrets: &dyn SecretResolver,
3178        ledger: Arc<Accounting>,
3179    ) -> Result<Box<dyn TaskSource>, SourceError> {
3180        self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
3181    }
3182
3183    fn build_recording_with_clock(
3184        &self,
3185        name: &SourceName,
3186        config: &Value,
3187        secrets: &dyn SecretResolver,
3188        ledger: Arc<Accounting>,
3189        clock: SharedClock,
3190    ) -> Result<Box<dyn TaskSource>, SourceError> {
3191        let config: GitHubProjectsConfig =
3192            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
3193                message: format!("source {name}: {e}"),
3194            })?;
3195        let prefix = format!("source {name}: ");
3196        let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
3197            .map_err(|error| match error {
3198                // The shared `StatusMapping::distinct` names the source itself.
3199                SourceError::Config { message } if message.starts_with(&prefix) => {
3200                    SourceError::Config { message }
3201                }
3202                SourceError::Config { message } => SourceError::Config {
3203                    message: format!("{prefix}{message}"),
3204                },
3205                SourceError::Auth { message } => SourceError::Auth {
3206                    message: format!("source {name}: {message}"),
3207                },
3208                other => other,
3209            })?;
3210        source.clock = clock;
3211        Ok(Box::new(source))
3212    }
3213}
3214
3215/// Where a status category lands on this board, once configuration is resolved.
3216#[derive(Debug, Clone, PartialEq, Eq)]
3217enum StatusTarget {
3218    /// Not usable against this instance for this kind, and why.
3219    Disabled(UnmappedStatus),
3220    /// The board's `Status` option of this name.
3221    Column(ColumnName),
3222    /// A closed issue, with both its board option and the reason that says which closed it means.
3223    // 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.
3224    Terminal(ColumnName, ClosedState),
3225}
3226
3227/// Every status category, in the order the vocabulary declares them.
3228///
3229/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
3230/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
3231/// added to the shared vocabulary fails to compile until it is named there, and this
3232/// crate's suite reconciles this list against that enum's own derived schema, which is
3233/// generated from the variants rather than written beside them. The schema is what
3234/// catches a list left one short — a list checking only the positions it already holds
3235/// would pass while every mapping indexed by the new position panicked.
3236pub const CATEGORIES: [StatusCategory; 8] = [
3237    StatusCategory::Draft,
3238    StatusCategory::Backlog,
3239    StatusCategory::Todo,
3240    StatusCategory::Queued,
3241    StatusCategory::InProgress,
3242    StatusCategory::Done,
3243    StatusCategory::Cancelled,
3244    StatusCategory::Unknown,
3245];
3246
3247/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
3248#[must_use]
3249pub const fn category_position(category: StatusCategory) -> usize {
3250    match category {
3251        StatusCategory::Draft => 0,
3252        StatusCategory::Backlog => 1,
3253        StatusCategory::Todo => 2,
3254        StatusCategory::Queued => 3,
3255        StatusCategory::InProgress => 4,
3256        StatusCategory::Done => 5,
3257        StatusCategory::Cancelled => 6,
3258        StatusCategory::Unknown => 7,
3259    }
3260}
3261
3262/// The spelling a status category is configured and reported under.
3263fn category_name(category: StatusCategory) -> &'static str {
3264    match category {
3265        StatusCategory::Draft => "draft",
3266        StatusCategory::Backlog => "backlog",
3267        StatusCategory::Todo => "todo",
3268        StatusCategory::Queued => "queued",
3269        StatusCategory::InProgress => "in-progress",
3270        StatusCategory::Done => "done",
3271        StatusCategory::Cancelled => "cancelled",
3272        StatusCategory::Unknown => "unknown",
3273    }
3274}
3275
3276/// A shipped default's option name.
3277///
3278/// The literals below are this file's own and non-blank, and they are validated by the
3279/// one constructor a configured name goes through rather than beside it.
3280fn shipped_column(name: &'static str) -> ColumnName {
3281    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
3282}
3283
3284/// The shipped default for one category this instance's `status_mapping` does not mention,
3285/// for either kind.
3286fn shipped_default(category: StatusCategory) -> StatusTarget {
3287    match category {
3288        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
3289        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
3290        StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
3291        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
3292        StatusCategory::Done => {
3293            StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
3294        }
3295        StatusCategory::Cancelled => {
3296            StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
3297        }
3298        StatusCategory::Draft | StatusCategory::Unknown => {
3299            StatusTarget::Disabled(UnmappedStatus::Unconfigured)
3300        }
3301    }
3302}
3303
3304/// The two kinds a status is written and read for, each with its own half of the mapping.
3305const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
3306
3307/// This instance's complete category-to-target mapping for each kind, read in both
3308/// directions.
3309///
3310/// One target per category per kind, held at that category's own [`category_position`], so
3311/// a category missing from the mapping, named twice in it, or filed out of order is a state
3312/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
3313/// targets are options of the board's one `Status` field.
3314#[derive(Debug, Clone)]
3315struct BoardStatuses {
3316    tasks: [StatusTarget; CATEGORIES.len()],
3317    projects: [StatusTarget; CATEGORIES.len()],
3318}
3319
3320impl BoardStatuses {
3321    /// Resolve `configured` against the shipped defaults, refusing two categories one kind
3322    /// would read back from one option.
3323    ///
3324    /// A category the mapping does not mention keeps its shipped default for both kinds; one
3325    /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
3326    /// omits unmapped rather than defaulted.
3327    fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
3328        let resolve_kind =
3329            |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
3330                // `CATEGORIES[position] == category` for every category — the crate's suite
3331                // asserts it — so mapping the list in order fills each category's own slot.
3332                let mut targets = CATEGORIES.map(shipped_default);
3333                for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
3334                    if !configured.mentions(category) {
3335                        continue;
3336                    }
3337                    *slot = match configured.name_for(category, kind) {
3338                        Err(why) => StatusTarget::Disabled(why),
3339                        Ok(name) => {
3340                            let option = ColumnName::try_from(name.as_str().to_owned())
3341                                .map_err(|message| SourceError::Config { message })?;
3342                            match category {
3343                                StatusCategory::Done => {
3344                                    StatusTarget::Terminal(option, ClosedState::Completed)
3345                                }
3346                                StatusCategory::Cancelled => {
3347                                    StatusTarget::Terminal(option, ClosedState::NotPlanned)
3348                                }
3349                                _ => StatusTarget::Column(option),
3350                            }
3351                        }
3352                    };
3353                }
3354                StatusMapping::distinct(
3355                    instance,
3356                    kind,
3357                    CATEGORIES
3358                        .iter()
3359                        .zip(&targets)
3360                        .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
3361                )?;
3362                Ok(targets)
3363            };
3364        Ok(Self {
3365            tasks: resolve_kind(ItemKind::Task)?,
3366            projects: resolve_kind(ItemKind::Project)?,
3367        })
3368    }
3369
3370    /// Every category's target for `kind`, in category order.
3371    const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
3372        match kind {
3373            ItemKind::Task => &self.tasks,
3374            ItemKind::Project => &self.projects,
3375        }
3376    }
3377
3378    fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
3379        &self.targets(kind)[category_position(category)]
3380    }
3381
3382    /// The category a board option name reports for `kind`, or `None` when nothing of that
3383    /// kind maps to it.
3384    fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
3385        CATEGORIES.into_iter().find(|category| {
3386            self.target(kind, *category)
3387                .option()
3388                .is_some_and(|name| name.eq_ignore_ascii_case(option))
3389        })
3390    }
3391
3392    /// Every option name either kind maps a category to, each once ignoring case, in
3393    /// category order with a task's name before a project's — what the guarded setup asks
3394    /// the `Status` field to hold.
3395    fn wanted(&self) -> Vec<String> {
3396        let mut wanted: Vec<String> = Vec::new();
3397        for category in CATEGORIES {
3398            for kind in STATUS_KINDS {
3399                if let Some(name) = self.target(kind, category).option()
3400                    && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
3401                {
3402                    wanted.push(name.to_owned());
3403                }
3404            }
3405        }
3406        wanted
3407    }
3408
3409    /// The status an item of `kind` reports, from the three things a read of it says: its
3410    /// board `Status` option, whether its issue is closed, and the reason it was closed with.
3411    ///
3412    /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
3413    /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
3414    /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
3415    /// produce, so it is read permissively rather than refused — reads are faithful, and
3416    /// refusals belong on writes.
3417    ///
3418    /// **Its name follows the closure too, unless the option agrees with it.** The option's
3419    /// own name is kept when this kind's mapping reads that option as the same category, or
3420    /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3421    /// named `Shipped`. An option the mapping reads as another category contradicts the
3422    /// closure, and so does no option at all: the name is then the option the mapping gives
3423    /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3424    /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3425    /// it. An `unknown` closure keeps the option, or `Closed` without one.
3426    ///
3427    /// An open item's option reads through its own kind's mapping, and an option that mapping
3428    /// does not name reads as `Unknown` under its own name.
3429    ///
3430    /// One function of those three rather than of a response, so a narrow status write can
3431    /// answer what a re-read would report by applying it to the state it has just written.
3432    fn status(
3433        &self,
3434        kind: ItemKind,
3435        option: Option<&str>,
3436        closed: bool,
3437        reason: Option<&str>,
3438    ) -> Status {
3439        if closed {
3440            let category = closed_category(reason);
3441            let agrees = option.filter(|option| {
3442                category == StatusCategory::Unknown
3443                    || self
3444                        .category_of(kind, option)
3445                        .is_none_or(|read| read == category)
3446            });
3447            let name = agrees.or_else(|| self.target(kind, category).option());
3448            let fallback = match category {
3449                StatusCategory::Done => "Done",
3450                StatusCategory::Cancelled => "Cancelled",
3451                _ => "Closed",
3452            };
3453            return Status {
3454                category,
3455                name: name.or(option).unwrap_or(fallback).to_owned(),
3456            };
3457        }
3458        let name = option.unwrap_or("Open").to_owned();
3459        Status {
3460            category: self
3461                .category_of(kind, &name)
3462                .unwrap_or(StatusCategory::Unknown),
3463            name,
3464        }
3465    }
3466
3467    /// The option a closed item's `Status` has to hold for its card to agree with its
3468    /// closure, and the category that closure reads as — or `None` when the item is open,
3469    /// its closure reads `unknown`, or its option already agrees.
3470    ///
3471    /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3472    /// in line, each to the one option the mapping gives its category — an item with no
3473    /// option, or at one the mapping does not name, included.
3474    fn contradicted(
3475        &self,
3476        kind: ItemKind,
3477        option: Option<&str>,
3478        closed: bool,
3479        reason: Option<&str>,
3480    ) -> Option<(ClosedAs, &str)> {
3481        if !closed {
3482            return None;
3483        }
3484        let closed_as = match closed_category(reason) {
3485            StatusCategory::Done => ClosedAs::Done,
3486            StatusCategory::Cancelled => ClosedAs::Cancelled,
3487            _ => return None,
3488        };
3489        let wanted = self.target(kind, closed_as.category()).option()?;
3490        if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3491            return None;
3492        }
3493        Some((closed_as, wanted))
3494    }
3495}
3496
3497/// The category a closed issue reads as, from the reason it was closed with.
3498///
3499/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3500/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3501/// is one this source cannot read a category from.
3502fn closed_category(reason: Option<&str>) -> StatusCategory {
3503    match reason {
3504        None | Some("COMPLETED") => StatusCategory::Done,
3505        Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3506        Some(_) => StatusCategory::Unknown,
3507    }
3508}
3509
3510impl BoardStatuses {
3511    /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3512    /// case; a kind lacking none is left out.
3513    fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3514        STATUS_KINDS
3515            .into_iter()
3516            .filter_map(|kind| {
3517                let missing: Vec<String> = self
3518                    .targets(kind)
3519                    .iter()
3520                    .filter_map(StatusTarget::option)
3521                    .filter(|wanted| {
3522                        !existing
3523                            .iter()
3524                            .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3525                    })
3526                    .map(str::to_owned)
3527                    .collect();
3528                (!missing.is_empty()).then_some(KindMissing { kind, missing })
3529            })
3530            .collect()
3531    }
3532}
3533
3534impl StatusTarget {
3535    /// The board option this target selects, or `None` for an unmapped one.
3536    fn option(&self) -> Option<&str> {
3537        match self {
3538            Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3539            Self::Disabled(_) => None,
3540        }
3541    }
3542}
3543
3544// 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.
3545/// One repository this source can create an issue in, as `owner/name`.
3546///
3547/// Every `createIssue` this source sends names one of these: the item's own single
3548/// `repositories` entry, else its parent project issue's repository, else the configured
3549/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3550/// that choice and says what it refuses before `createIssue`.
3551// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3552#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
3553struct RepositoryTarget {
3554    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3555    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3556}
3557
3558impl RepositoryTarget {
3559    fn parse(value: &str) -> Result<Self, SourceError> {
3560        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3561            message: format!(
3562                "repository must be spelled owner/name; {value:?} names no repository"
3563            ),
3564        })?;
3565        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3566            return Err(SourceError::Config {
3567                message: format!(
3568                    "repository must be spelled owner/name with a GitHub login and one \
3569                     repository name; {value:?} is not"
3570                ),
3571            });
3572        }
3573        Ok(Self {
3574            owner: owner.to_owned(),
3575            name: name.to_owned(),
3576        })
3577    }
3578
3579    /// The one host whose repositories this source creates issues in, spelled once: it is
3580    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3581    const HOST: &str = "github.com";
3582
3583    fn origin(&self) -> String {
3584        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3585    }
3586
3587    /// The repository a normalized origin names, or why it is none this source can create
3588    /// an issue in: another host, or more or fewer than `owner/name` under this one.
3589    fn from_origin(origin: &Repository) -> Result<Self, String> {
3590        let not_here = || {
3591            format!(
3592                "{} is not a {}/owner/name repository",
3593                origin.as_str(),
3594                Self::HOST
3595            )
3596        };
3597        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3598        if host != Self::HOST {
3599            return Err(not_here());
3600        }
3601        Self::parse(rest).map_err(|_| not_here())
3602    }
3603
3604    fn slug(&self) -> String {
3605        format!("{}/{}", self.owner, self.name)
3606    }
3607}
3608
3609/// A source which reads GitHub afresh for every operation.
3610pub struct GitHubProjectsSource {
3611    /// This source's configured name, used both to tell a far end naming this source
3612    /// from one naming a system it knows nothing about, and to name the instance a
3613    /// status refusal is about.
3614    name: SourceName,
3615    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3616    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3617    repository: Option<RepositoryTarget>,
3618    endpoint: Url,
3619    token: SecretString,
3620    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3621    statuses: BoardStatuses,
3622    /// Where each priority lands on this board, or `None` when this instance holds none.
3623    priorities: Option<PriorityMapping>,
3624    /// The metadata values this instance projects onto board text fields, in configured order.
3625    metadata_fields: Vec<MetadataField>,
3626    /// Built on the first request rather than with the source, as [`Self::client`] says.
3627    client: std::sync::OnceLock<Client>,
3628    /// Built on the first attachment request, as [`Self::asset_client`] says.
3629    asset_client: std::sync::OnceLock<Client>,
3630    /// Every item this source has created in this command, in the order it created them —
3631    /// dropped by [`TaskSource::end_command`].
3632    ///
3633    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3634    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3635    /// a copy resolving a dependency on an item it had just created refused it as not
3636    /// found. A board read is completed from this — an item remembered here and absent from
3637    /// the read is added back, because the board really does hold it and only the read is
3638    /// behind.
3639    ///
3640    /// It is not a cache of a user's work: nothing is remembered that this process did not
3641    /// itself just write, it lives and dies with the process, and it is never consulted for
3642    /// an item this source did not create.
3643    created: Mutex<Vec<Resolved>>,
3644    /// Every item that already existed and that this source has written in this command, as
3645    /// it wrote it — dropped by [`TaskSource::end_command`].
3646    ///
3647    /// The other half of [`Self::created`], held on the same terms and for the reason a
3648    /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3649    /// filter is an index behind a write this process made moments ago, so a query matching
3650    /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3651    /// is remembered that this process did not itself just write.
3652    updated: Mutex<Vec<Resolved>>,
3653    /// Every issue this source has added a comment to or edited a comment of in this command
3654    /// — dropped by [`TaskSource::end_command`].
3655    ///
3656    /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3657    /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3658    /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3659    /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3660    /// from before — until the index caught up. Each id here is a candidate of every such
3661    /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3662    /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3663    /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3664    /// process wrote is still found only once the index has it.
3665    commented: Mutex<Vec<NativeId>>,
3666    /// How fast this source writes, and how long it waits out a refusal.
3667    pacing: Pacing,
3668    /// When the last content-creating mutation finished, or the moment the furthest-out
3669    /// reserved slot releases the next one, whichever is later — so the one after it can be
3670    /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
3671    /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
3672    /// measured from.
3673    last_mutation: Mutex<Option<Duration>>,
3674    /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
3675    windows: Mutex<pacing::Windows>,
3676    /// The tasks a project holds, as a copy's batched read of the project and every one of them
3677    /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
3678    /// a listing of that project's tasks is answered without a read of its own.
3679    task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
3680    /// The web address of each issue this source created in the current command, as its
3681    /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
3682    /// request.
3683    written: Mutex<BTreeMap<NativeId, String>>,
3684    /// What a copy has held back of the items it created, between
3685    /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
3686    /// nothing is being held back, when every write lands whole as it is made.
3687    deferred: Mutex<Option<Deferred>>,
3688    clock: SharedClock,
3689    numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3690    /// The board as this process last read it, for the length of one command — dropped by
3691    /// [`TaskSource::end_command`].
3692    ///
3693    /// A copy of a project used to re-read the whole board, paged, before writing each of
3694    /// its items, which is by far the largest part of a copy's request count and none of
3695    /// its work. Nothing else changes this board while a command runs — this source's own
3696    /// writes are the only writer — so one read answers them all.
3697    ///
3698    /// It is not a store of a user's work and it is not the cache the no-persistence
3699    /// invariant forbids: it lives and dies with the process exactly as `created` does,
3700    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3701    /// an item this command created and then depends on resolves whether or not GitHub's
3702    /// own eventually-consistent read has caught up. A write to an item already on the
3703    /// board updates the entry here too, so what this holds is the last read plus this
3704    /// process's own writes rather than a snapshot taken before them.
3705    board_cache: Mutex<Option<Board>>,
3706    /// Every issue this board's own search reported, for the length of one command — dropped
3707    /// by [`TaskSource::end_command`].
3708    ///
3709    /// The second half of a board read, and cached for the same reason and on the same
3710    /// terms as the first: it lives and dies with the process, nothing is written down, and
3711    /// a write this process makes updates the entry here exactly as it updates the one in
3712    /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3713    /// that lists this board's projects and its tasks pays for one search rather than two.
3714    search_cache: Mutex<Option<Vec<Resolved>>>,
3715    /// What each narrowed question GitHub was asked answered, keyed by that question, for
3716    /// the length of one command — dropped by [`TaskSource::end_command`].
3717    ///
3718    /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3719    /// and dies with the process, nothing is written down, a write this process makes
3720    /// updates the entry here as it updates the other two, and every answer is completed
3721    /// with this process's own writes each time it is given. A command that asks the same
3722    /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3723    /// write — pays for it once, which is what the whole-board read it replaced gave it.
3724    narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3725    search_next: Mutex<BTreeMap<String, Option<String>>>,
3726    /// Records already resolved in this command, reused by writes and for comment identity.
3727    /// Explicit item reads still reach GitHub. Nothing is persisted, and
3728    /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3729    /// its item as a person has since left it.
3730    resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3731    /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
3732    /// answered as holding nothing — each taken by the next read of that item by id and by
3733    /// nothing else, dropped by any mutation naming it, and cleared by
3734    /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
3735    /// read-before-write, made in the same request as the others' rather than on its own.
3736    read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
3737    /// Every item a listing of one project's members carried whole in this command — the
3738    /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
3739    /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
3740    /// it again. No other read is answered from it, so a read by id outside a copy's
3741    /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
3742    /// item, and cleared by [`TaskSource::end_command`].
3743    carried: Mutex<BTreeMap<NativeId, Resolved>>,
3744    /// Items an issue read by reference reached, held for the one status write each was read
3745    /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
3746    /// what such a write needs. Dropped by [`TaskSource::end_command`].
3747    held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3748    /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3749    /// asked for under, with the project that selector named — for the length of one command,
3750    /// dropped by [`TaskSource::end_command`].
3751    ///
3752    /// A caller pages through a project's tasks one engine page at a time, and every page
3753    /// is cut from the whole list of them, so without this each page walked every
3754    /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3755    /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3756    /// dies with the process, nothing is written down, a write this process makes updates or
3757    /// removes the entry here as it does there, and every answer is completed with this
3758    /// process's own writes each time it is given.
3759    children_cache: Mutex<ProjectChildren>,
3760    /// The board's own id and field definitions as this process last read them on their
3761    /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3762    ///
3763    /// What a write needs of the board and its item does not say, read once per command
3764    /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3765    /// lives and dies with the process and nothing is written down. It holds no item and so
3766    /// can answer no question about one — see [`Self::board_fields`].
3767    fields_cache: Mutex<Option<BoardFields>>,
3768    /// Each destination repository's node id, resolved once per repository
3769    /// rather than per issue created.
3770    ///
3771    /// A repository's node id does not change, and re-reading it for every issue of a copy
3772    /// spent one request per item on an answer this source already had. It is a map rather
3773    /// than one entry because a copy files each item in the repository its own
3774    /// `repositories` field names, so a plan across five repositories asks GitHub five
3775    /// times and not once per item.
3776    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3777    /// What every request this source sends is recorded into.
3778    ///
3779    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3780    /// a request leaves this crate, so nothing has to be switched on for a session to be
3781    /// counted. It is shared rather than owned so a caller accounting for a whole session —
3782    /// its own schema verification, board lookups, residue sweep and cleanup beside this
3783    /// source's reads and writes — adds up one accounting instead of two. See
3784    /// [`accounting`] for what a record carries and what a session's spend is and is not.
3785    ledger: Arc<Accounting>,
3786    /// What [`expected`] holds for a write the store's cache expects: the journal while one is
3787    /// open, the issues primed from the cache, and the comments known on each.
3788    expected: expected::Held,
3789}
3790
3791/// GitHub's closed single-select color vocabulary.
3792#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3793#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
3794pub enum StatusOptionColor {
3795    /// Gray.
3796    Gray,
3797    /// Blue.
3798    Blue,
3799    /// Green.
3800    Green,
3801    /// Yellow.
3802    Yellow,
3803    /// Purple.
3804    Purple,
3805    /// Red.
3806    Red,
3807    /// Orange.
3808    Orange,
3809    /// Pink.
3810    Pink,
3811}
3812
3813/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
3814/// applies its additions.
3815#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3816pub enum SetupMode {
3817    /// Read without mutation.
3818    Plan,
3819    /// Apply and verify.
3820    Apply,
3821}
3822
3823/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
3824/// against it goes on compiling.
3825pub type StatusOptionsMode = SetupMode;
3826
3827/// The explicit result of the requested operation.
3828#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3829#[serde(rename_all = "kebab-case")]
3830pub enum StatusOptionsOutcome {
3831    /// A read-only plan.
3832    Planned,
3833    /// Apply found nothing missing.
3834    Unchanged,
3835    /// Additions were applied and verified.
3836    Applied,
3837}
3838
3839/// A GitHub single-select option's opaque GraphQL node identifier.
3840#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3841#[serde(transparent)]
3842pub struct StatusOptionId(#[schemars(length(min = 1))] String);
3843
3844impl TryFrom<String> for StatusOptionId {
3845    type Error = String;
3846
3847    fn try_from(id: String) -> Result<Self, Self::Error> {
3848        if id.trim().is_empty() {
3849            return Err("a GitHub Status option id cannot be blank".to_owned());
3850        }
3851        Ok(Self(id))
3852    }
3853}
3854
3855/// One existing or proposed option in a guarded Status-field update.
3856#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3857pub struct StatusOption {
3858    /// GitHub's stable id.
3859    pub id: StatusOptionId,
3860    /// The visible option name.
3861    pub name: ColumnName,
3862    /// GitHub's single-select color token.
3863    pub color: StatusOptionColor,
3864    /// The option description, including an empty one.
3865    pub description: String,
3866}
3867
3868/// One board item's Status assignment, retained as recovery data.
3869#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3870pub struct StatusAssignment {
3871    /// The project item id whose assignment this is.
3872    // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
3873    // carried verbatim as operator recovery data; introducing a semantic type would claim
3874    // validation rules GitHub does not publish and no operation here interprets.
3875    pub item_id: String,
3876    /// The selected option, absent when the item has no status.
3877    #[serde(skip_serializing_if = "Option::is_none")]
3878    pub option: Option<AssignedStatusOption>,
3879}
3880
3881/// The inseparable id and name of an assigned option.
3882#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3883pub struct AssignedStatusOption {
3884    /// GitHub's stable id.
3885    pub id: StatusOptionId,
3886    /// The visible name.
3887    pub name: ColumnName,
3888}
3889
3890/// The plan and verified outcome of reconciling configured Status options.
3891#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3892pub struct StatusOptionsReport {
3893    /// The configured source name.
3894    pub source: SourceName,
3895    /// Configured option names absent before the operation.
3896    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
3897    // `ColumnName` and has therefore already passed its nonblank validation; retaining the
3898    // serialized string here preserves the report's intentionally simple public contract.
3899    pub missing: Vec<String>,
3900    /// What the requested operation did.
3901    pub outcome: StatusOptionsOutcome,
3902    /// The complete option list observed before any mutation.
3903    pub existing: Vec<StatusOption>,
3904}
3905
3906#[derive(Debug, Clone, PartialEq, Eq)]
3907struct StatusSnapshot {
3908    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3909    // passed back as the mutation's project identity; a newtype could enforce no stronger
3910    // invariant because GitHub publishes no grammar for it.
3911    board_id: String,
3912    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3913    // passed back as the mutation's field identity; a newtype could enforce no stronger
3914    // invariant because GitHub publishes no grammar for it.
3915    field_id: String,
3916    options: Vec<StatusOption>,
3917    assignments: Vec<StatusAssignment>,
3918}
3919
3920/// The name of the board field a status is held in.
3921const STATUS_FIELD: &str = "Status";
3922
3923/// Every item's value of each field `report` names, as it stood before the setup wrote
3924/// anything — what a person puts back when the setup is refused part way.
3925fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
3926    let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
3927        .fields
3928        .iter()
3929        .map(|field| (field.field.name(), before.assignments(field.field)))
3930        .collect();
3931    serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
3932        message: format!("cannot render the pre-write field recovery snapshot: {error}"),
3933    })
3934}
3935
3936/// One board field the guarded setup reads and writes — every one it reads, and the only
3937/// ones it writes.
3938#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
3939pub enum BoardField {
3940    /// The single-select `Status` field every instance's `status_mapping` resolves into.
3941    Status,
3942    /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
3943    Priority,
3944}
3945
3946impl BoardField {
3947    /// The field's name on the board.
3948    #[must_use]
3949    pub const fn name(self) -> &'static str {
3950        match self {
3951            Self::Status => STATUS_FIELD,
3952            Self::Priority => PRIORITY_FIELD,
3953        }
3954    }
3955
3956    /// The field a board calls `name`, or `None` for one this setup does not own.
3957    fn named(name: &str) -> Option<Self> {
3958        [Self::Status, Self::Priority]
3959            .into_iter()
3960            .find(|field| field.name() == name)
3961    }
3962}
3963
3964/// What the guarded setup did to one field.
3965#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3966#[serde(rename_all = "kebab-case")]
3967pub enum FieldOutcome {
3968    /// A read-only plan.
3969    Planned,
3970    /// Apply found the field there with every configured option.
3971    Unchanged,
3972    /// Missing options were added to the field that was there, and verified.
3973    Applied,
3974    /// The field was not there; it was created holding the configured options, and verified.
3975    Created,
3976}
3977
3978/// One field's plan, or its verified outcome.
3979#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3980pub struct FieldReport {
3981    /// Which field.
3982    pub field: BoardField,
3983    /// Whether the board had the field before the operation.
3984    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
3985    // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
3986    // "outcome", "existing"}` — so folding one into the other would change a published JSON
3987    // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
3988    // one constructor, and it derives `outcome` from `exists` in one match.
3989    pub exists: bool,
3990    /// Configured option names the field lacked before the operation — every one of them,
3991    /// in the order a new field lists them, when the field was not there at all.
3992    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3993    // mapping name and has therefore already passed its nonblank validation; the serialized
3994    // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
3995    pub missing: Vec<String>,
3996    /// For the `Status` field, which item kind each missing name is configured for: one
3997    /// entry per kind `status_mapping` names a missing option for, task before project, each
3998    /// listing that kind's missing names in category order. A name both kinds use is in
3999    /// both. Empty — and left out of the JSON — when nothing is missing, and always for
4000    /// `Priority`, which only a task holds.
4001    #[serde(default, skip_serializing_if = "Vec::is_empty")]
4002    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4003    // both SDKs model an absent `kinds` as an empty list rather than as `null`.
4004    #[schemars(!skip_serializing_if)]
4005    pub kinds: Vec<KindMissing>,
4006    /// What the requested operation did.
4007    pub outcome: FieldOutcome,
4008    /// The field's complete option list observed before any mutation; empty when the field
4009    /// was not there.
4010    pub existing: Vec<StatusOption>,
4011}
4012
4013/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
4014#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4015pub struct KindMissing {
4016    /// The kind these names are configured for.
4017    pub kind: ItemKind,
4018    /// The names that kind maps a category to and the field lacked, in category order.
4019    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
4020    // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
4021    // intentionally simple public contract.
4022    pub missing: Vec<String>,
4023}
4024
4025/// The plan and verified outcome of setting up every field a source's configuration names.
4026#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4027pub struct FieldsReport {
4028    /// The configured source name.
4029    pub source: SourceName,
4030    /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
4031    // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
4032    // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
4033    // per field would change a published JSON shape. The states the list could hold and the
4034    // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
4035    // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
4036    pub fields: Vec<FieldReport>,
4037    /// Each board text field the source's `metadata_fields` projects a value onto, in
4038    /// configured order. Empty — and left out of the JSON — when it projects none.
4039    #[serde(default, skip_serializing_if = "Vec::is_empty")]
4040    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
4041    // both SDKs model an absent list as an empty one rather than as `null`.
4042    #[schemars(!skip_serializing_if)]
4043    pub metadata_fields: Vec<MetadataFieldReport>,
4044}
4045
4046/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
4047/// its GraphQL type when it has none — which is what a projected field's conflict reports.
4048///
4049/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
4050/// no conflict.
4051// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
4052// refuses exactly what `try_from` below does.
4053#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4054#[serde(transparent)]
4055pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
4056
4057/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
4058/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
4059/// neither SDK's validator can be relied on to support.
4060const NON_TEXT_FIELD_TYPE_PATTERN: &str =
4061    r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
4062
4063impl TryFrom<String> for NonTextFieldType {
4064    type Error = String;
4065
4066    fn try_from(kind: String) -> Result<Self, Self::Error> {
4067        if kind.is_empty() || kind.starts_with(char::is_whitespace) {
4068            return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
4069        }
4070        if kind == "TEXT" {
4071            return Err("a TEXT field is no conflict for a projected text field".to_owned());
4072        }
4073        Ok(Self(kind))
4074    }
4075}
4076
4077impl std::fmt::Display for NonTextFieldType {
4078    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4079        formatter.write_str(&self.0)
4080    }
4081}
4082
4083/// One projected metadata text field's plan, or its verified outcome.
4084///
4085/// The setup creates a missing one as a text field and never alters a field that is there:
4086/// a same-named field of another type is reported in `conflict` and left as it is.
4087#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4088pub struct MetadataFieldReport {
4089    /// The board field's name.
4090    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.
4091    /// The metadata key the value is read from.
4092    pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
4093    /// The object keys walked inside that key's value; empty for the key's own value.
4094    pub path: Vec<String>,
4095    /// Whether the board had a text field of that name before the operation.
4096    // 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.
4097    pub exists: bool,
4098    /// The type of a field of that name that is not a text field, which the setup leaves as
4099    /// it is; absent — and left out of the JSON — when there is none.
4100    #[serde(default, skip_serializing_if = "Option::is_none")]
4101    pub conflict: Option<NonTextFieldType>,
4102    /// What the requested operation did: `planned`; `created` for a field that was not
4103    /// there; `unchanged` for one that was, and for a conflict, which is never changed.
4104    pub outcome: FieldOutcome,
4105}
4106
4107/// Which options one field is configured with, in the order a new field would list them.
4108struct FieldPlan {
4109    field: BoardField,
4110    wanted: Vec<String>,
4111}
4112
4113/// One single-select field as the guarded setup snapshots it.
4114#[derive(Debug, Clone, PartialEq, Eq)]
4115struct SnapshotField {
4116    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4117    // passed back as the mutation's field identity; a newtype could enforce no stronger
4118    // invariant because GitHub publishes no grammar for it.
4119    field_id: String,
4120    options: Vec<StatusOption>,
4121}
4122
4123/// Every single-select field of a board and every item's value of each.
4124#[derive(Debug, Clone, PartialEq, Eq)]
4125struct BoardSnapshot {
4126    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4127    // passed back as the mutation's project identity; a newtype could enforce no stronger
4128    // invariant because GitHub publishes no grammar for it.
4129    board_id: String,
4130    fields: BTreeMap<BoardField, SnapshotField>,
4131    /// Each board item's id, and its value of each field this setup owns that it holds one of.
4132    items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
4133}
4134
4135impl BoardSnapshot {
4136    /// Every item's value of `field`, in board order — the recovery data a drift refusal
4137    /// carries.
4138    fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
4139        self.items
4140            .iter()
4141            .map(|(item_id, values)| StatusAssignment {
4142                item_id: item_id.clone(),
4143                option: values.get(&field).cloned(),
4144            })
4145            .collect()
4146    }
4147}
4148
4149impl GitHubProjectsSource {
4150    /// Report missing configured Status options and, when `apply` is true, add them with
4151    /// a whole-list mutation that preserves every existing id and verifies the result.
4152    ///
4153    /// # Errors
4154    ///
4155    /// Refuses a board without a single-select `Status` field. A post-write difference in
4156    /// any pre-existing option id or item assignment is refused with the complete pre-write
4157    /// assignment snapshot in the diagnostic for recovery.
4158    // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
4159    // successful mutation, both drift refusals, source selection, missing Status, casing,
4160    // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
4161    // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
4162    // responses from entering the defensive malformed-response branches below.
4163    pub async fn status_options(
4164        &self,
4165        mode: StatusOptionsMode,
4166    ) -> Result<StatusOptionsReport, SourceError> {
4167        let before = self.status_snapshot().await?;
4168        // A terminal category's option is as configured as an open one's: a terminal
4169        // write validates it before closing and refuses when the board lacks it. Both
4170        // kinds' names are options of the one field, so both are asked for.
4171        let missing = self
4172            .statuses
4173            .wanted()
4174            .into_iter()
4175            .filter(|wanted| {
4176                !before
4177                    .options
4178                    .iter()
4179                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4180            })
4181            .collect::<Vec<_>>();
4182        let report = StatusOptionsReport {
4183            source: self.name.clone(),
4184            missing: missing.clone(),
4185            outcome: match (mode, missing.is_empty()) {
4186                (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
4187                (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
4188                (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
4189            },
4190            existing: before.options.clone(),
4191        };
4192        if mode == StatusOptionsMode::Plan || missing.is_empty() {
4193            return Ok(report);
4194        }
4195        let mut options = before
4196            .options
4197            .iter()
4198            .map(|option| {
4199                json!({
4200                    "id": option.id, "name": option.name, "color": option.color,
4201                    "description": option.description,
4202                })
4203            })
4204            .collect::<Vec<_>>();
4205        options.extend(missing.iter().map(|name| {
4206            json!({
4207                "name": name, "color": "GRAY", "description": ""
4208            })
4209        }));
4210        self.graphql(
4211            graphql::STATUS_OPTIONS_UPDATE,
4212            json!({"input": {
4213                "projectId": before.board_id, "fieldId": before.field_id,
4214                "singleSelectOptions": options,
4215            }}),
4216        )
4217        .await?;
4218        let after = self.status_snapshot().await?;
4219        let options_preserved = before
4220            .options
4221            .iter()
4222            .all(|old| after.options.iter().any(|new| new == old));
4223        let additions_present = missing.iter().all(|wanted| {
4224            after
4225                .options
4226                .iter()
4227                .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4228        });
4229        if !options_preserved || !additions_present || after.assignments != before.assignments {
4230            let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
4231                SourceError::Malformed {
4232                    message: format!("cannot render pre-write Status recovery snapshot: {error}"),
4233                }
4234            })?;
4235            return Err(SourceError::Refused {
4236                message: format!(
4237                    "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}"
4238                ),
4239            });
4240        }
4241        Ok(report)
4242    }
4243
4244    /// A fresh snapshot of the Status field and every board item's assignment of it.
4245    ///
4246    /// # Errors
4247    ///
4248    /// Refuses a board without a single-select `Status` field, and one the token cannot see.
4249    async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
4250        // Status alone, as this operation has always read it: a `Priority` field is another
4251        // operation's, so nothing about it can refuse this one.
4252        let mut board = self.board_snapshot(&[BoardField::Status]).await?;
4253        let field = board
4254            .fields
4255            .remove(&BoardField::Status)
4256            .ok_or_else(|| self.no_status_field())?;
4257        Ok(StatusSnapshot {
4258            assignments: board.assignments(BoardField::Status),
4259            board_id: board.board_id,
4260            field_id: field.field_id,
4261            options: field.options,
4262        })
4263    }
4264
4265    /// The refusal a board with no `Status` field is answered with by the guarded setup.
4266    fn no_status_field(&self) -> SourceError {
4267        SourceError::Refused {
4268            message: format!("source {} board has no Status field", self.name),
4269        }
4270    }
4271
4272    // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
4273    // the real CLI loopback journey, including pagination. The individual malformed guards
4274    // are defensive validation of a schema-pinned third-party response, not separate user
4275    // journeys; drift and missing-field failures cover the operation's recovery behavior.
4276    /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
4277    /// every board item's value of each, walked to the end of the board's items. A field not
4278    /// in `owned` is read past whatever it holds.
4279    async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
4280        let mut after: Option<String> = None;
4281        let mut snapshot: Option<BoardSnapshot> = None;
4282        loop {
4283            let data = self
4284                .graphql(
4285                    graphql::STATUS_OPTIONS_SNAPSHOT,
4286                    json!({
4287                        "owner": self.owner, "number": self.project_number,
4288                        "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
4289                    }),
4290                )
4291                .await?;
4292            let board = data
4293                .pointer("/owner/projectV2")
4294                .filter(|board| board.is_object())
4295                .ok_or_else(|| SourceError::Refused {
4296                    message: format!(
4297                        "source {} has no accessible GitHub Projects board",
4298                        self.name
4299                    ),
4300                })?;
4301            if board
4302                .pointer("/fields/pageInfo/hasNextPage")
4303                .and_then(Value::as_bool)
4304                != Some(false)
4305            {
4306                return Err(SourceError::Malformed {
4307                    message:
4308                        "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
4309                            .into(),
4310                });
4311            }
4312            let mut fields = BTreeMap::new();
4313            // Only the fields this setup owns, by name: a node the single-select fragment did not
4314            // match carries no name, and a person's own single-select field — a `Size`, a
4315            // `Team` — is none of this setup's business, so nothing about it can refuse one. A
4316            // `Status` or `Priority` field without its options is malformed, not absent.
4317            // 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.
4318            for (owned, field) in board
4319                .pointer("/fields/nodes")
4320                .and_then(Value::as_array)
4321                .ok_or_else(|| SourceError::Malformed {
4322                    message: "GitHub project fields.nodes is not an array".into(),
4323                })?
4324                .iter()
4325                .filter_map(|field| {
4326                    let named = BoardField::named(field.get("name")?.as_str()?)?;
4327                    owned.contains(&named).then_some((named, field))
4328                })
4329            {
4330                let options = field
4331                    .get("options")
4332                    .and_then(Value::as_array)
4333                    .ok_or_else(|| SourceError::Malformed {
4334                        message: "GitHub single-select field options is not an array".into(),
4335                    })?
4336                    .iter()
4337                    .map(|option| {
4338                        Ok(StatusOption {
4339                            id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
4340                                .map_err(|message| SourceError::Malformed { message })?,
4341                            name: ColumnName::try_from(required_str(option, "name")?.to_owned())
4342                                .map_err(|message| SourceError::Malformed {
4343                                    message: format!(
4344                                        "GitHub single-select option name is invalid: {message}"
4345                                    ),
4346                                })?,
4347                            color: serde_json::from_value(
4348                                option.get("color").cloned().unwrap_or(Value::Null),
4349                            )
4350                            .map_err(|error| {
4351                                SourceError::Malformed {
4352                                    message: format!(
4353                                        "GitHub single-select option color is invalid: {error}"
4354                                    ),
4355                                }
4356                            })?,
4357                            description: optional_str(option, "description")?
4358                                .unwrap_or_default()
4359                                .to_owned(),
4360                        })
4361                    })
4362                    .collect::<Result<Vec<_>, SourceError>>()?;
4363                let snapshot = SnapshotField {
4364                    field_id: required_nonblank_str(field, "id")?.to_owned(),
4365                    options,
4366                };
4367                // A board's field names are unique, so a second one is an answer that cannot
4368                // say which field the setup would act on — refused rather than one chosen.
4369                if fields.insert(owned, snapshot).is_some() {
4370                    return Err(SourceError::Malformed {
4371                        message: format!(
4372                            "GitHub answered two {} fields for this board",
4373                            owned.name()
4374                        ),
4375                    });
4376                }
4377            }
4378            let board_id = required_nonblank_str(board, "id")?.to_owned();
4379            let current = snapshot.get_or_insert_with(|| BoardSnapshot {
4380                board_id,
4381                fields,
4382                items: Vec::new(),
4383            });
4384            let items = board
4385                .pointer("/items/nodes")
4386                .and_then(Value::as_array)
4387                .ok_or_else(|| SourceError::Malformed {
4388                    message: "GitHub project items.nodes is not an array".into(),
4389                })?;
4390            for item in items {
4391                let field_values =
4392                    item.get("fieldValues")
4393                        .ok_or_else(|| SourceError::Malformed {
4394                            message: "GitHub project item is missing fieldValues".into(),
4395                        })?;
4396                if field_values
4397                    .pointer("/pageInfo/hasNextPage")
4398                    .and_then(Value::as_bool)
4399                    != Some(false)
4400                {
4401                    return Err(SourceError::Malformed {
4402                        message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
4403                    });
4404                }
4405                let values = item
4406                    .pointer("/fieldValues/nodes")
4407                    .and_then(Value::as_array)
4408                    .ok_or_else(|| SourceError::Malformed {
4409                        message: "GitHub project item fieldValues.nodes is not an array".into(),
4410                    })?;
4411                let item_id = required_nonblank_str(item, "id")?;
4412                let mut assigned = BTreeMap::new();
4413                for value in values {
4414                    let Some(field) = value
4415                        .pointer("/field/name")
4416                        .and_then(Value::as_str)
4417                        .and_then(BoardField::named)
4418                        .filter(|field| owned.contains(field))
4419                    else {
4420                        continue;
4421                    };
4422                    let held = assigned.insert(
4423                        field,
4424                        AssignedStatusOption {
4425                            id: StatusOptionId::try_from(
4426                                required_str(value, "optionId")?.to_owned(),
4427                            )
4428                            .map_err(|message| SourceError::Malformed { message })?,
4429                            name: ColumnName::try_from(required_str(value, "name")?.to_owned())
4430                                .map_err(|message| SourceError::Malformed {
4431                                    message: format!(
4432                                        "GitHub assigned {} name is invalid: {message}",
4433                                        field.name()
4434                                    ),
4435                                })?,
4436                        },
4437                    );
4438                    // An item holds one value of a field, so a second one leaves no way to
4439                    // tell which it holds — and a verification or recovery built on either
4440                    // could restore the wrong one.
4441                    if held.is_some() {
4442                        return Err(SourceError::Malformed {
4443                            message: format!(
4444                                "GitHub answered two {} values for board item {item_id}",
4445                                field.name()
4446                            ),
4447                        });
4448                    }
4449                }
4450                current.items.push((item_id.to_owned(), assigned));
4451            }
4452            let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4453                message: "GitHub project is missing items".into(),
4454            })?;
4455            let has_next = page
4456                .pointer("/pageInfo/hasNextPage")
4457                .and_then(Value::as_bool)
4458                .ok_or_else(|| SourceError::Malformed {
4459                    message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4460                })?;
4461            if !has_next {
4462                break;
4463            }
4464            let next =
4465                required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4466            validate_cursor_progress(after.as_deref(), next)?;
4467            after = Some(next.to_owned());
4468        }
4469        snapshot.ok_or_else(|| SourceError::Malformed {
4470            message: "GitHub returned no board field snapshot".into(),
4471        })
4472    }
4473    // llmlint: ignore-end[changed_behavior_has_e2e]
4474
4475    /// Report every board field this source's configuration names and, with
4476    /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4477    /// the `Priority` field when the board has none.
4478    ///
4479    /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4480    /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4481    /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4482    /// color and description: the whole option list goes back with every existing id, because
4483    /// a re-minted id clears every item's value.
4484    ///
4485    /// # Errors
4486    ///
4487    /// Refuses a board without a single-select `Status` field. After an apply the board is
4488    /// read again, and a pre-existing option or any item's value of either field that moved is
4489    /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4490    // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4491    // unchanged apply, a created field, an added option to each field, drift refusal, a board
4492    // with no Status field and a non-github-projects source through the compiled CLI against
4493    // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4494    pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4495        let owned: Vec<BoardField> = if self.priorities.is_some() {
4496            vec![BoardField::Status, BoardField::Priority]
4497        } else {
4498            vec![BoardField::Status]
4499        };
4500        let before = self.board_snapshot(&owned).await?;
4501        let mut plans = vec![FieldPlan {
4502            field: BoardField::Status,
4503            wanted: self.statuses.wanted(),
4504        }];
4505        if !before.fields.contains_key(&BoardField::Status) {
4506            return Err(self.no_status_field());
4507        }
4508        if let Some(mapping) = &self.priorities {
4509            plans.push(FieldPlan {
4510                field: BoardField::Priority,
4511                wanted: mapping.names().map(str::to_owned).collect(),
4512            });
4513        }
4514        // The snapshot reads single-select fields alone, so a field it did not find may still
4515        // be on the board under the name, of another type: creating one beside it would fail
4516        // part way, or leave two fields of one name. Asked of the board's own field list, and
4517        // only when a field is missing.
4518        if plans
4519            .iter()
4520            .any(|plan| !before.fields.contains_key(&plan.field))
4521        {
4522            let board = self.board_fields().await?;
4523            for plan in plans
4524                .iter()
4525                .filter(|plan| !before.fields.contains_key(&plan.field))
4526            {
4527                if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4528                    return Err(SourceError::Refused {
4529                        message: format!(
4530                            "source {}'s board has a {} field that is not a single-select field \
4531                             (it is a {}), so it cannot hold this source's options; next: rename \
4532                             or remove that field, then run this again",
4533                            self.name,
4534                            plan.field.name(),
4535                            optional_str(field, "__typename")?.unwrap_or("field of another type")
4536                        ),
4537                    });
4538                }
4539            }
4540        }
4541        let mut reports = Vec::new();
4542        for plan in &plans {
4543            let held = before.fields.get(&plan.field);
4544            let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4545            let mut missing: Vec<String> = Vec::new();
4546            for wanted in &plan.wanted {
4547                let present = existing
4548                    .iter()
4549                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4550                    || missing
4551                        .iter()
4552                        .any(|named| named.eq_ignore_ascii_case(wanted));
4553                if !present {
4554                    missing.push(wanted.clone());
4555                }
4556            }
4557            let kinds = match plan.field {
4558                BoardField::Status => self.statuses.missing_by_kind(&existing),
4559                BoardField::Priority => Vec::new(),
4560            };
4561            reports.push(FieldReport {
4562                field: plan.field,
4563                exists: held.is_some(),
4564                kinds,
4565                outcome: match (mode, held.is_some(), missing.is_empty()) {
4566                    (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4567                    (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4568                    (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4569                    (SetupMode::Apply, false, _) => FieldOutcome::Created,
4570                },
4571                missing,
4572                existing,
4573            });
4574        }
4575        // A projected field is a text field, which the single-select snapshot cannot see, so
4576        // it is asked of the board's own field list — and only when one is configured.
4577        let mut metadata_fields = Vec::new();
4578        if !self.metadata_fields.is_empty() {
4579            let board = self.board_fields().await?;
4580            for projection in &self.metadata_fields {
4581                let conflict = match Board::field(&board.fields, &projection.field)? {
4582                    None => None,
4583                    Some(field) => {
4584                        let typename = optional_str(field, "__typename")?;
4585                        let data_type = optional_str(field, "dataType")?;
4586                        if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4587                            None
4588                        } else {
4589                            let named = data_type
4590                                .filter(|kind| *kind != "TEXT")
4591                                .or(typename)
4592                                .unwrap_or("field of another type");
4593                            Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4594                                |message| SourceError::Malformed {
4595                                    message: format!(
4596                                        "GitHub answered the board's {} field with a type this \
4597                                         setup cannot read: {message}",
4598                                        &*projection.field
4599                                    ),
4600                                },
4601                            )?)
4602                        }
4603                    }
4604                };
4605                let exists =
4606                    conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4607                metadata_fields.push(MetadataFieldReport {
4608                    field: projection.field.to_string(),
4609                    key: projection.key.to_string(),
4610                    path: projection.path.clone(),
4611                    exists,
4612                    outcome: match (mode, exists || conflict.is_some()) {
4613                        (SetupMode::Plan, _) => FieldOutcome::Planned,
4614                        (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4615                        (SetupMode::Apply, false) => FieldOutcome::Created,
4616                    },
4617                    conflict,
4618                });
4619            }
4620        }
4621        let report = FieldsReport {
4622            source: self.name.clone(),
4623            fields: reports,
4624            metadata_fields,
4625        };
4626        let writes: Vec<&FieldReport> = report
4627            .fields
4628            .iter()
4629            .filter(|field| !field.missing.is_empty() || !field.exists)
4630            .collect();
4631        let creates: Vec<&MetadataFieldReport> = report
4632            .metadata_fields
4633            .iter()
4634            .filter(|field| field.outcome == FieldOutcome::Created)
4635            .collect();
4636        if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4637            return Ok(report);
4638        }
4639        let mut landed: Vec<&str> = Vec::new();
4640        for field in &writes {
4641            let added = field
4642                .missing
4643                .iter()
4644                .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4645            let sent = match before.fields.get(&field.field) {
4646                Some(held) => {
4647                    let mut options = held
4648                        .options
4649                        .iter()
4650                        .map(|option| {
4651                            json!({
4652                                "id": option.id, "name": option.name, "color": option.color,
4653                                "description": option.description,
4654                            })
4655                        })
4656                        .collect::<Vec<_>>();
4657                    options.extend(added);
4658                    self.graphql(
4659                        graphql::STATUS_OPTIONS_UPDATE,
4660                        json!({"input": {
4661                            "projectId": before.board_id, "fieldId": held.field_id,
4662                            "singleSelectOptions": options,
4663                        }}),
4664                    )
4665                    .await
4666                }
4667                None => {
4668                    self.graphql(
4669                        graphql::CREATE_FIELD,
4670                        json!({"input": {
4671                            "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4672                            "name": field.field.name(),
4673                            "singleSelectOptions": added.collect::<Vec<_>>(),
4674                        }}),
4675                    )
4676                    .await
4677                }
4678            };
4679            // A mutation that failed does not establish that GitHub left its field as it was,
4680            // so every failure from here on carries the recovery data a drift refusal does.
4681            match sent {
4682                Ok(_) => landed.push(field.field.name()),
4683                Err(error) => {
4684                    let changed = if landed.is_empty() {
4685                        String::new()
4686                    } else {
4687                        format!("changed the {} field and then ", landed.join(" and "))
4688                    };
4689                    return Err(SourceError::Refused {
4690                        message: format!(
4691                            "the guarded field setup {changed}failed on the {} field, which it may \
4692                             have changed part way: {error}; the pre-write item assignments \
4693                             are:\n{}",
4694                            field.field.name(),
4695                            recovery(&report, &before)?
4696                        ),
4697                    });
4698                }
4699            }
4700        }
4701        for field in &creates {
4702            let sent = self
4703                .graphql(
4704                    graphql::CREATE_FIELD,
4705                    json!({"input": {
4706                        "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4707                    }}),
4708                )
4709                .await;
4710            if let Err(error) = sent {
4711                let changed = if landed.is_empty() {
4712                    String::new()
4713                } else {
4714                    format!("changed the {} field and then ", landed.join(" and "))
4715                };
4716                return Err(SourceError::Refused {
4717                    message: format!(
4718                        "the guarded field setup {changed}failed creating the {} text field: \
4719                         {error}; run it again to create what is still missing",
4720                        field.field
4721                    ),
4722                });
4723            }
4724            landed.push(&field.field);
4725        }
4726        // The board has been written, so a verification read that fails leaves it unverified
4727        // rather than unchanged, and says what to put back.
4728        let after = match self.board_snapshot(&owned).await {
4729            Ok(after) => after,
4730            Err(error) => {
4731                return Err(SourceError::Refused {
4732                    message: format!(
4733                        "the guarded field setup changed the {} field and then could not read the \
4734                         board back to verify it: {error}; the pre-write item assignments are:\n{}",
4735                        landed.join(" and "),
4736                        recovery(&report, &before)?
4737                    ),
4738                });
4739            }
4740        };
4741        let mut moved = Vec::new();
4742        for field in &report.fields {
4743            let name = field.field.name();
4744            let now = after
4745                .fields
4746                .get(&field.field)
4747                .map(|held| held.options.as_slice())
4748                .unwrap_or_default();
4749            if !field.existing.iter().all(|old| now.contains(old)) {
4750                moved.push(format!(
4751                    "a pre-existing {name} option id, name, color or description"
4752                ));
4753            }
4754            if !field.missing.iter().all(|wanted| {
4755                now.iter()
4756                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4757            }) {
4758                moved.push(format!("an added {name} option"));
4759            }
4760            if after.assignments(field.field) != before.assignments(field.field) {
4761                moved.push(format!("an item's {name} value"));
4762            }
4763        }
4764        if !creates.is_empty() {
4765            // Read afresh: the view held for this command is the one from before the creates.
4766            *self.fields_cache()? = None;
4767            let board = match self.board_fields().await {
4768                Ok(board) => board,
4769                Err(error) => {
4770                    return Err(SourceError::Refused {
4771                        message: format!(
4772                            "the guarded field setup changed the {} field and then could not \
4773                             read the board back to verify it: {error}; run it again to verify \
4774                             it, which creates nothing a second time",
4775                            landed.join(" and ")
4776                        ),
4777                    });
4778                }
4779            };
4780            for field in &creates {
4781                let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
4782                    held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
4783                        && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
4784                });
4785                if !text {
4786                    moved.push(format!("the created {} text field", field.field));
4787                }
4788            }
4789        }
4790        if !moved.is_empty() {
4791            return Err(SourceError::Refused {
4792                message: format!(
4793                    "GitHub changed {} after the guarded field setup; the pre-write item \
4794                     assignments are:\n{}",
4795                    moved.join(", "),
4796                    recovery(&report, &before)?
4797                ),
4798            });
4799        }
4800        Ok(report)
4801    }
4802
4803    /// Validate configuration and capture the named credential without exposing it.
4804    ///
4805    /// # Errors
4806    ///
4807    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
4808    /// [`SourceError::Auth`] when the named credential is missing or empty.
4809    pub fn new(
4810        name: &SourceName,
4811        config: GitHubProjectsConfig,
4812        secrets: &dyn SecretResolver,
4813    ) -> Result<Self, SourceError> {
4814        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
4815    }
4816
4817    /// The same, recording every request it sends into an accounting the caller holds too.
4818    ///
4819    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
4820    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
4821    /// up — passes the one it records those into, so the session total accounts for the
4822    /// whole session rather than for this source's share of it.
4823    ///
4824    /// # Errors
4825    ///
4826    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
4827    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
4828    pub fn recording_into(
4829        name: &SourceName,
4830        config: GitHubProjectsConfig,
4831        secrets: &dyn SecretResolver,
4832        ledger: Arc<Accounting>,
4833    ) -> Result<Self, SourceError> {
4834        if !valid_github_owner(&config.owner) {
4835            return Err(SourceError::Config {
4836                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
4837            });
4838        }
4839        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
4840            return Err(SourceError::Config {
4841                message: format!("project_number must be between 1 and {}", i32::MAX),
4842            });
4843        }
4844        if !valid_environment_name(&config.token_env) {
4845            return Err(SourceError::Config {
4846                message: "token_env must be a valid environment-variable name".into(),
4847            });
4848        }
4849        let repository = config
4850            .repository
4851            .as_deref()
4852            .map(RepositoryTarget::parse)
4853            .transpose()?;
4854        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
4855            message: format!("endpoint is not a valid URL: {e}"),
4856        })?;
4857        if endpoint.scheme() != "https"
4858            && !(endpoint.scheme() == "http"
4859                && endpoint
4860                    .host_str()
4861                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
4862        {
4863            return Err(SourceError::Config {
4864                message:
4865                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
4866                        .into(),
4867            });
4868        }
4869        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
4870            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),
4871        })?;
4872        Ok(Self {
4873            name: name.clone(),
4874            owner: config.owner,
4875            project_number: config.project_number,
4876            repository,
4877            asset_client: std::sync::OnceLock::new(),
4878            endpoint,
4879            token,
4880            credential_name: config.token_env,
4881            statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
4882            priorities: config
4883                .priority_mapping
4884                .map(|mapping| PriorityMapping::resolve(mapping, name))
4885                .transpose()?,
4886            metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
4887            client: std::sync::OnceLock::new(),
4888            created: Mutex::new(Vec::new()),
4889            updated: Mutex::new(Vec::new()),
4890            commented: Mutex::new(Vec::new()),
4891            pacing: Pacing::resolve(config.pacing, name)?,
4892            last_mutation: Mutex::new(None),
4893            windows: Mutex::new(pacing::Windows::default()),
4894            task_children: Mutex::new(BTreeMap::new()),
4895            written: Mutex::new(BTreeMap::new()),
4896            deferred: Mutex::new(None),
4897            clock: system_clock(),
4898            numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
4899            board_cache: Mutex::new(None),
4900            search_cache: Mutex::new(None),
4901            narrowed_cache: Mutex::new(BTreeMap::new()),
4902            resolved_cache: Mutex::new(BTreeMap::new()),
4903            read_ahead: Mutex::new(BTreeMap::new()),
4904            carried: Mutex::new(BTreeMap::new()),
4905            held_cache: Mutex::new(BTreeMap::new()),
4906            children_cache: Mutex::new(BTreeMap::new()),
4907            search_next: Mutex::new(BTreeMap::new()),
4908            fields_cache: Mutex::new(None),
4909            repository_cache: Mutex::new(BTreeMap::new()),
4910            ledger,
4911            expected: expected::Held::default(),
4912        })
4913    }
4914
4915    /// The client every API request goes through, built on the first one.
4916    ///
4917    /// Not with the source: building a client loads the system's certificate store, which
4918    /// costs tens of milliseconds, and a command that configures this board but never sends to
4919    /// it — a read of another source — should not pay that in its process.
4920    fn client(&self) -> Result<&Client, SourceError> {
4921        built(&self.client, || {
4922            Client::builder()
4923                .user_agent("onetaskgraph")
4924                .build()
4925                .map_err(|e| SourceError::Config {
4926                    message: format!("cannot build HTTP client: {e}"),
4927                })
4928        })
4929    }
4930
4931    /// The client attachment requests go through, with its own redirect policy, built on the
4932    /// first one for the reason [`Self::client`] gives.
4933    fn asset_client(&self) -> Result<&Client, SourceError> {
4934        built(&self.asset_client, || assets::client(&self.endpoint))
4935    }
4936
4937    /// A snapshot of every request this source has sent, and what each cost.
4938    ///
4939    /// A value to hold and compare rather than a borrow of the accounting itself, so two
4940    /// of them can sit side by side. When this source was built with
4941    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
4942    /// point of building it that way.
4943    #[must_use]
4944    pub fn accounting(&self) -> accounting::Session {
4945        self.ledger.snapshot()
4946    }
4947
4948    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
4949    /// rate limit rather than handing it straight back as an error.
4950    ///
4951    /// Retrying is safe for every document here, including the mutations, and the reason
4952    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
4953    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
4954    /// this replays has already taken effect. An outcome this source cannot know — the
4955    /// send failed, or the body could not be read, so the mutation may well have landed —
4956    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
4957    /// attempt. A duplicate write would come from replaying one of those, and none is
4958    /// replayed.
4959    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4960        if !self.expected.journaling() {
4961            return self.graphql_sent(query, variables).await;
4962        }
4963        // While a write the store's cache expects runs, its mutations select the whole issue
4964        // they leave behind, so the response vouches for what the cache keeps — and every
4965        // request is recorded, so the write's parts are reported from their own responses.
4966        let (query, variables) = match graphql::whole_twin(query) {
4967            Some(twin) => (twin, expected::whole_variables(variables)),
4968            None => (query, variables),
4969        };
4970        let sent = self.graphql_sent(query, variables.clone()).await;
4971        self.journal(query, &variables, &sent).await;
4972        sent
4973    }
4974
4975    /// One document sent, retried while GitHub asks for a wait, exactly as asked.
4976    async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4977        if is_mutation(query)
4978            && ![
4979                graphql::ADD_COMMENT,
4980                graphql::UPDATE_COMMENT,
4981                graphql::DELETE_COMMENT,
4982                graphql::ADD_COMMENT_WHOLE,
4983                graphql::UPDATE_COMMENT_WHOLE,
4984            ]
4985            .contains(&query)
4986        {
4987            let mut cache = self.resolved_cache()?;
4988            let mut ahead = self.read_ahead()?;
4989            let mut carried = self.carried()?;
4990            for argument in ["input", "second", "third", "clear"] {
4991                if let Some(input) = variables.get(argument) {
4992                    let names = |id: &NativeId, item_id: Option<&str>| {
4993                        ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
4994                            input
4995                                .get(key)
4996                                .and_then(Value::as_str)
4997                                .is_some_and(|value| value == id.0 || Some(value) == item_id)
4998                        })
4999                    };
5000                    cache.retain(|id, item| !names(id, Some(&item.item_id)));
5001                    ahead.retain(|id, item| {
5002                        !names(id, item.as_ref().map(|item| item.item_id.as_str()))
5003                    });
5004                    carried.retain(|id, item| !names(id, Some(&item.item_id)));
5005                }
5006            }
5007        }
5008        let doing = operation_description(query);
5009        let mutation = is_mutation(query);
5010        let charge = pacing::Charge::graphql(query, &variables);
5011        let mut waited = Duration::ZERO;
5012        let mut waits = 0_u32;
5013        let mut backoff = self.pacing.retry_backoff;
5014        loop {
5015            let ticket = self.pace(charge, mutation).await;
5016            let attempt = self.send_once(query, &variables, waits + 1).await;
5017            self.paced(ticket, mutation);
5018            let limited = match attempt {
5019                Ok(data) => return Ok(data),
5020                Err(Attempt::Failed(error)) => return Err(error),
5021                Err(Attempt::Limited(limited)) => limited,
5022            };
5023            let (wait, cause) = limited.wait(backoff);
5024            let remaining = self.pacing.retry_budget.saturating_sub(waited);
5025            // A wait of nothing spends none of the budget, so it is exhaustion rather
5026            // than a retry. `Pacing::resolve` rules out every way of configuring one
5027            // except a budget of zero, where reporting the first refusal is the ask.
5028            if wait.is_zero() || wait > remaining {
5029                return Err(limited.exhausted(
5030                    doing,
5031                    waits,
5032                    waited,
5033                    wait,
5034                    self.pacing.retry_budget,
5035                ));
5036            }
5037            self.clock.sleep_because(wait, cause).await;
5038            waited += wait;
5039            waits += 1;
5040            backoff = backoff.saturating_mul(2);
5041        }
5042    }
5043
5044    /// Hold one request charging `charge` until every secondary window it charges has room for
5045    /// the whole of it — and, for a mutation of an instance configured with
5046    /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
5047    /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
5048    ///
5049    /// The moment is decided and counted under the lock and waited out outside it, so two
5050    /// callers take two places rather than the same one, and no lock is held across an await.
5051    /// A request with room goes out at once: nothing here waits that a window does not ask for.
5052    async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
5053        let (wait, cause, ticket) = {
5054            // A poisoned lock here costs pacing, not correctness, and refusing the write over
5055            // it would turn an earlier failure into a second one for no gain.
5056            let mut windows = self
5057                .windows
5058                .lock()
5059                .unwrap_or_else(std::sync::PoisonError::into_inner);
5060            let now = self.clock.now();
5061            let (mut at, mut cause) = if self.pacing.windows {
5062                windows.room(now, charge)
5063            } else {
5064                (now, None)
5065            };
5066            if mutation && !self.pacing.min_mutation_interval.is_zero() {
5067                let mut last = self
5068                    .last_mutation
5069                    .lock()
5070                    .unwrap_or_else(std::sync::PoisonError::into_inner);
5071                // `checked_add` rather than `+`: adding durations can panic on overflow, and
5072                // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
5073                if let Some(earliest) = last
5074                    .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
5075                    && earliest > at
5076                {
5077                    at = earliest;
5078                    cause = Some(pacing::cause::MIN_INTERVAL);
5079                }
5080                *last = Some(at);
5081            }
5082            let ticket = windows.charge(at, charge);
5083            (at.saturating_sub(now), cause, ticket)
5084        };
5085        if let (false, Some(cause)) = (wait.is_zero(), cause) {
5086            self.clock.sleep_because(wait, cause).await;
5087        }
5088        ticket
5089    }
5090
5091    /// Record that the request `ticket` was charged for has finished, so each window — and
5092    /// the configured spacing — counts it from here rather than from the moment it left.
5093    ///
5094    /// This source can only choose when a request *departs*; GitHub's limiter counts when it
5095    /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
5096    /// departure would hand the limiter a window that looked roomier than it is, so a source
5097    /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
5098    /// paced well inside a board's threshold was once refused by it on a slower machine while
5099    /// passing on a quick one. The previous request had arrived before its response came back,
5100    /// so counting from here is never earlier than the limiter does, whatever transit costs
5101    /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
5102    /// reads for the next fifty minutes.
5103    ///
5104    /// A failed attempt is finished too: a request refused by the limiter still arrived, and
5105    /// one that never left costs only room nobody needed.
5106    fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
5107        let now = self.clock.now();
5108        self.windows
5109            .lock()
5110            .unwrap_or_else(std::sync::PoisonError::into_inner)
5111            .finish(ticket, now);
5112        if mutation && !self.pacing.min_mutation_interval.is_zero() {
5113            let mut last = self
5114                .last_mutation
5115                .lock()
5116                .unwrap_or_else(std::sync::PoisonError::into_inner);
5117            // `max` rather than an assignment: a concurrent caller may already have reserved a
5118            // slot further out, and completing this request must never pull that slot back in.
5119            *last = Some(last.map_or(now, |reserved| reserved.max(now)));
5120        }
5121    }
5122
5123    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
5124    /// failure that waiting cannot help — and recorded, whichever of the three it was.
5125    ///
5126    /// This is the one place a request leaves this crate, which is why the accounting is
5127    /// here rather than at each of the callers: a read path added later is counted without
5128    /// anybody remembering to count it, and
5129    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
5130    /// when one is not.
5131    async fn send_once(
5132        &self,
5133        query: &str,
5134        variables: &Value,
5135        number: u32,
5136    ) -> Result<Value, Attempt> {
5137        let executed = self.ledger.priced_document(query);
5138        let Attempted {
5139            result,
5140            limits,
5141            reported_cost,
5142            response_outcome,
5143        } = self.attempt(executed.as_ref(), variables).await;
5144        // No `otherwise` name: every document this source sends is one of its own, and the
5145        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
5146        let sending = accounting::Request::graphql(query, variables, None, reported_cost);
5147        let outcome = match &result {
5148            Ok(_) => accounting::Outcome::Answered,
5149            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
5150            Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
5151        };
5152        self.ledger
5153            .record(sending.finished(outcome, limits).with_execution(
5154                Some(executed.into_owned()),
5155                response_outcome,
5156                number,
5157            ));
5158        result
5159    }
5160
5161    /// The attempt itself, with what its response said about the rate limit alongside.
5162    ///
5163    /// The two are returned together rather than recorded here because every one of the
5164    /// early exits below is a different outcome, and a record written at each of them is a
5165    /// record one of them can be added without.
5166    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
5167        let mut limits = accounting::RateLimit::default();
5168        let mut reported_cost = None;
5169        let mut response_outcome = accounting::ResponseOutcome::TransportError;
5170        let result = self
5171            .attempted(
5172                query,
5173                variables,
5174                &mut limits,
5175                &mut reported_cost,
5176                &mut response_outcome,
5177            )
5178            .await;
5179        Attempted {
5180            result,
5181            limits,
5182            reported_cost,
5183            response_outcome,
5184        }
5185    }
5186
5187    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
5188    async fn attempted(
5189        &self,
5190        query: &str,
5191        variables: &Value,
5192        limits: &mut accounting::RateLimit,
5193        reported_cost: &mut Option<u64>,
5194        response_outcome: &mut accounting::ResponseOutcome,
5195    ) -> Result<Value, Attempt> {
5196        let response = self
5197            .client()
5198            .map_err(Attempt::Failed)?
5199            .post(self.endpoint.clone())
5200            .bearer_auth(self.token.expose_secret())
5201            .json(&json!({"query": query, "variables": variables}))
5202            .send()
5203            .await
5204            .map_err(|e| {
5205                Attempt::Failed(SourceError::Unavailable {
5206                    message: format!("GitHub GraphQL request failed: {}", transport_error(&e)),
5207                })
5208            })?;
5209        let status = response.status();
5210        if !status.is_success() {
5211            *response_outcome = accounting::ResponseOutcome::HttpError;
5212        }
5213        let header = |name: &str| whole_seconds(response.headers().get(name));
5214        let served = response
5215            .headers()
5216            .get(reqwest::header::DATE)
5217            .and_then(|value| value.to_str().ok())
5218            .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
5219            .map(|at| at.with_timezone(&Utc));
5220        *limits = accounting::RateLimit::read(|name| {
5221            response
5222                .headers()
5223                .get(name)
5224                .and_then(|value| value.to_str().ok())
5225                .map(str::to_owned)
5226        });
5227        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
5228        // that are not text at all — is "not known to be exhausted". This never makes a
5229        // response a refusal on its own: it says which limiter a refusal is attributed to
5230        // and where its hint comes from, so a value this cannot read costs a hint rather
5231        // than an answer.
5232        let exhausted = response
5233            .headers()
5234            .get("x-ratelimit-remaining")
5235            .and_then(|value| value.to_str().ok())
5236            == Some("0");
5237        // `retry-after` is what GitHub asks for when it asks; when it does not and the
5238        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
5239        // which is the same question answered as an absolute time. Nothing else here is a
5240        // hint, and a schedule is what answers a refusal that carries none.
5241        let retry_after = header("retry-after");
5242        let hinted_by = if retry_after.is_some() || !exhausted {
5243            Hint::RetryAfter
5244        } else {
5245            Hint::PrimaryReset
5246        };
5247        let hint = retry_after.or_else(|| {
5248            exhausted
5249                .then(|| header("x-ratelimit-reset"))
5250                .flatten()
5251                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
5252        });
5253        // Read before it is parsed, because the evidence which tells a secondary rate
5254        // limit from a rejected credential is in the body of a response whose status says
5255        // only "forbidden" — and a non-success response was never parsed at all.
5256        let body = response.text().await.map_err(|e| {
5257            Attempt::Failed(SourceError::Unavailable {
5258                message: format!(
5259                    "GitHub GraphQL response could not be read: {}",
5260                    transport_error(&e)
5261                ),
5262            })
5263        })?;
5264        *reported_cost = serde_json::from_str::<Value>(&body)
5265            .ok()
5266            .as_ref()
5267            .and_then(|body| body.pointer("/data/rateLimit/cost"))
5268            .and_then(Value::as_u64);
5269        let parsed = serde_json::from_str::<Value>(&body).ok();
5270        *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
5271        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
5272            return Err(Attempt::Limited(Limited {
5273                limiter,
5274                hint,
5275                hinted_by,
5276            }));
5277        }
5278        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
5279            return Err(Attempt::Failed(SourceError::Auth {
5280                message: format!(
5281                    "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"
5282                ),
5283            }));
5284        }
5285        if !status.is_success() {
5286            return Err(Attempt::Failed(SourceError::Unavailable {
5287                message: format!("GitHub GraphQL returned HTTP {status}"),
5288            }));
5289        }
5290        let answered = if query == graphql::ISSUES_BY_REFERENCE {
5291            self.answer_unresolved(&body)
5292        } else if query == graphql::CREATE_ISSUES {
5293            answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
5294        } else {
5295            self.answer(&body)
5296        };
5297        let mut data = answered.map_err(Attempt::Failed)?;
5298        if let Some(served) = served {
5299            stamp_served(&mut data, &served.to_rfc3339());
5300        }
5301        Ok(data)
5302    }
5303
5304    /// What one successful HTTP response says, once its GraphQL errors are read.
5305    /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
5306    /// for this credential — a repository it cannot see, an issue number it does not hold —
5307    /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
5308    /// that reference rather than a failure of the others. Any other error refuses the read,
5309    /// as there.
5310    fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
5311        let parsed: Option<Value> = serde_json::from_str(body).ok();
5312        // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
5313        // `issue`, and the answer holds `null` at exactly that path: anything else — another
5314        // kind of error, one naming no slot, or one beside a value it says is not there — is a
5315        // refusal of the whole read.
5316        let unresolved = |data: &Value, error: &Value| {
5317            error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
5318                && error
5319                    .get("path")
5320                    .and_then(Value::as_array)
5321                    .is_some_and(|path| {
5322                        let alias = path.first().and_then(Value::as_str).and_then(|alias| {
5323                            alias
5324                                .strip_prefix('r')
5325                                .and_then(|slot| slot.parse::<usize>().ok())
5326                                .filter(|slot| *slot < REFERENCE_BATCH)
5327                                .map(|_| alias)
5328                        });
5329                        match (alias, path.get(1).and_then(Value::as_str), path.len()) {
5330                            (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
5331                            (Some(alias), Some("issue"), 2) => data
5332                                .get(alias)
5333                                .and_then(|repository| repository.get("issue"))
5334                                .is_some_and(Value::is_null),
5335                            _ => false,
5336                        }
5337                    })
5338        };
5339        let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
5340            parsed.get("data").is_some_and(|data| {
5341                data.is_object()
5342                    && parsed
5343                        .get("errors")
5344                        .and_then(Value::as_array)
5345                        .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
5346            })
5347        });
5348        match parsed {
5349            Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
5350            _ => self.answer(body),
5351        }
5352    }
5353
5354    fn answer(&self, body: &str) -> Result<Value, SourceError> {
5355        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
5356            message: format!("GitHub returned invalid JSON: {e}"),
5357        })?;
5358        let errors = body
5359            .get("errors")
5360            .map(|value| {
5361                value.as_array().ok_or_else(|| SourceError::Malformed {
5362                    message: "GitHub response errors is not an array".into(),
5363                })
5364            })
5365            .transpose()?;
5366        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
5367            let messages = errors
5368                .iter()
5369                .filter_map(|e| e.get("message").and_then(Value::as_str))
5370                .collect::<Vec<_>>()
5371                .join("; ");
5372            let message = if messages.is_empty() {
5373                "GitHub returned GraphQL errors".into()
5374            } else {
5375                messages
5376            };
5377            let normalized = message.to_ascii_lowercase();
5378            if normalized.contains("resource not accessible") || normalized.contains("scope") {
5379                return Err(SourceError::Auth {
5380                    message: format!(
5381                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
5382                        self.credential_name
5383                    ),
5384                });
5385            }
5386            return Err(SourceError::Refused { message });
5387        }
5388        body.get("data")
5389            .filter(|data| data.is_object())
5390            .cloned()
5391            .ok_or_else(|| SourceError::Malformed {
5392                message: "GitHub response has no data object".into(),
5393            })
5394    }
5395
5396    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
5397    // GraphQL cannot independently page them inside the outer item page. This source page is
5398    // deliberately bounded at that published maximum; the live drift journey exercises it.
5399    async fn board_page(
5400        &self,
5401        items_after: Option<&str>,
5402        items_first: u32,
5403    ) -> Result<Value, SourceError> {
5404        let data = self
5405            .graphql(
5406                graphql::BOARD,
5407                json!({"owner":self.owner,"number":self.project_number,
5408                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
5409                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
5410            )
5411            .await?;
5412        data.pointer("/owner/projectV2")
5413            .filter(|v| !v.is_null())
5414            .cloned()
5415            .ok_or_else(|| SourceError::Refused {
5416                message: format!(
5417                    "GitHub project {}/{} was not found or is not visible to the token",
5418                    self.owner, self.project_number
5419                ),
5420            })
5421    }
5422
5423    /// The search that finds the issues of this board, narrowed by `also` when it is
5424    /// given.
5425    ///
5426    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
5427    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
5428    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
5429    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
5430    /// from a task by the `parent` field each issue carries rather than by the search.
5431    fn board_search(&self, also: Option<&str>) -> String {
5432        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
5433        match also {
5434            Some(also) => format!("{scope} {also}"),
5435            None => scope,
5436        }
5437    }
5438
5439    /// One issue this source reached directly, as the board item a read of the board would
5440    /// have produced — or `None` when this board does not hold it.
5441    ///
5442    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
5443    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
5444    /// item's own id, that item's field values, and the issue as its content. One resolver
5445    /// for both routes is what makes an issue read through a search, through its own node
5446    /// id, or through its project's sub-issues report the same title, the same status, the
5447    /// same labels and the same qualified id.
5448    ///
5449    /// An issue with no entry for *this* board is not this source's to report, which is
5450    /// what keeps an id naming some other repository's issue from being answered as an item
5451    /// of this board. That answer is given about an **exhausted** connection and never
5452    /// about an unread page: the entry is looked for on the page in hand, and only if that
5453    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
5454    /// rest of it.
5455    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
5456        self.resolve_issue_as(issue, true).await
5457    }
5458
5459    /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
5460    /// and writes only when `remember` says to.
5461    async fn resolve_issue_as(
5462        &self,
5463        issue: &Value,
5464        remember: bool,
5465    ) -> Result<Option<Resolved>, SourceError> {
5466        if optional_str(issue, "__typename")? != Some("Issue") {
5467            return Ok(None);
5468        }
5469        let memberships = issue
5470            .get("projectItems")
5471            .ok_or_else(|| SourceError::Malformed {
5472                message: "GitHub issue is missing projectItems".into(),
5473            })?;
5474        let nodes = memberships
5475            .get("nodes")
5476            .and_then(Value::as_array)
5477            .ok_or_else(|| SourceError::Malformed {
5478                message: "GitHub issue projectItems.nodes is not an array".into(),
5479            })?;
5480        let held = match self.board_entry(nodes) {
5481            Some(held) => held.clone(),
5482            None => {
5483                let info = memberships
5484                    .get("pageInfo")
5485                    .ok_or_else(|| SourceError::Malformed {
5486                        message: "GitHub issue projectItems has no pageInfo".into(),
5487                    })?;
5488                // The page held no entry for this board. Whether that means the issue is
5489                // not on it is a question about the rest of the connection, and only a
5490                // connection with no rest answers it here.
5491                if !required_bool(info, "hasNextPage")? {
5492                    return Ok(None);
5493                }
5494                let cursor = required_str(info, "endCursor")?;
5495                validate_cursor_progress(None, cursor)?;
5496                let issue_id = required_str(issue, "id")?;
5497                match self.board_membership(issue_id, cursor).await? {
5498                    Some(held) => held,
5499                    None => return Ok(None),
5500                }
5501            }
5502        };
5503        // The membership's own `updatedAt` rides along: it is the board half of the item's change
5504        // token, which a field change moves where the issue's own time does not.
5505        let item = json!({
5506            "id": required_str(&held, "id")?,
5507            "updatedAt": held.get("updatedAt"),
5508            "project": held.get("project"),
5509            "fieldValues": held.get("fieldValues"),
5510            "content": issue,
5511        });
5512        if remember {
5513            self.resolve(&item)
5514        } else {
5515            self.resolve_unremembered(&item)
5516        }
5517    }
5518
5519    /// This board's own entry among one page of an issue's `Issue.projectItems`.
5520    ///
5521    /// One spelling of *which membership is this board's*, so the page a read carries and
5522    /// the pages [`Self::board_membership`] walks are searched by the same rule.
5523    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5524        nodes.iter().find(|node| {
5525            node.pointer("/project/number").and_then(Value::as_u64)
5526                == Some(u64::from(self.project_number))
5527        })
5528    }
5529
5530    /// The rest of one issue's board memberships, from `after`, for this board's entry.
5531    ///
5532    /// The recovery read: a page of memberships that holds no entry for this board says
5533    /// nothing about the memberships past it, so the connection is walked to exhaustion
5534    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5535    /// positive answer — the whole connection was read and no entry named this board —
5536    /// rather than a failure, and the walk is held to
5537    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5538    /// with a cursor that does not advance is refused instead of spun on.
5539    async fn board_membership(
5540        &self,
5541        issue: &str,
5542        after: &str,
5543    ) -> Result<Option<Value>, SourceError> {
5544        let mut after = after.to_owned();
5545        loop {
5546            let data = self
5547                .graphql(
5548                    graphql::ISSUE_BOARD_ITEMS,
5549                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5550                           "nestedFirst":NESTED_PAGE_SIZE}),
5551                )
5552                .await?;
5553            let Some(connection) = data
5554                .pointer("/node/projectItems")
5555                .filter(|value| !value.is_null())
5556            else {
5557                // The id resolved to nothing, or to something with no memberships to walk —
5558                // which is the same answer as a connection holding no entry for this board.
5559                return Ok(None);
5560            };
5561            let nodes = connection
5562                .get("nodes")
5563                .and_then(Value::as_array)
5564                .ok_or_else(|| SourceError::Malformed {
5565                    message: "GitHub issue projectItems.nodes is not an array".into(),
5566                })?;
5567            if let Some(held) = self.board_entry(nodes) {
5568                return Ok(Some(held.clone()));
5569            }
5570            let info = connection
5571                .get("pageInfo")
5572                .ok_or_else(|| SourceError::Malformed {
5573                    message: "GitHub issue projectItems has no pageInfo".into(),
5574                })?;
5575            let next = required_bool(info, "hasNextPage")?
5576                .then(|| required_str(info, "endCursor"))
5577                .transpose()?;
5578            match next {
5579                Some(next) => {
5580                    validate_cursor_progress(Some(&after), next)?;
5581                    after = next.to_owned();
5582                }
5583                None => return Ok(None),
5584            }
5585        }
5586    }
5587
5588    /// One page of a board-scoped issue search, and where the next page resumes.
5589    async fn search_page(
5590        &self,
5591        search: &str,
5592        first: u32,
5593        after: Option<&str>,
5594    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5595        let data = self
5596            .graphql(
5597                graphql::SEARCH_ISSUES,
5598                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5599                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5600                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5601            )
5602            .await?;
5603        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5604            message: "GitHub search response has no search connection".into(),
5605        })?;
5606        let mut found = Vec::new();
5607        for node in connection
5608            .get("nodes")
5609            .and_then(Value::as_array)
5610            .ok_or_else(|| SourceError::Malformed {
5611                message: "GitHub search nodes is not an array".into(),
5612            })?
5613        {
5614            if let Some(resolved) = self.resolve_issue(node).await? {
5615                found.push(resolved);
5616            }
5617        }
5618        let info = connection
5619            .get("pageInfo")
5620            .ok_or_else(|| SourceError::Malformed {
5621                message: "GitHub search connection has no pageInfo".into(),
5622            })?;
5623        let next = required_bool(info, "hasNextPage")?
5624            .then(|| required_str(info, "endCursor"))
5625            .transpose()?
5626            .map(str::to_owned);
5627        if let Some(next) = &next {
5628            validate_cursor_progress(after, next)?;
5629        }
5630        Ok((found, next))
5631    }
5632
5633    /// Every issue this board holds, completed with what this run wrote.
5634    ///
5635    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
5636    /// is an index and is eventually consistent, so an issue this run created seconds ago
5637    /// can be absent from it, and a project listed straight after being written would
5638    /// otherwise be missing from its own board. What is added back is only what this
5639    /// process itself wrote, out of [`Self::created`], which lives and dies with the
5640    /// process.
5641    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5642        let found = self.searched_issues().await?;
5643        self.completed_with_written(found, |_| true)
5644    }
5645
5646    /// Every issue this board's own search reports, walked to exhaustion, read once per
5647    /// source.
5648    ///
5649    /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
5650    /// needs it too and the two would otherwise walk the same search twice in one command.
5651    /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
5652    /// is.
5653    async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5654        let cached = self.search_cache()?.clone();
5655        if let Some(held) = cached {
5656            return Ok(held);
5657        }
5658        let mut after: Option<String> = None;
5659        let mut found = Vec::new();
5660        let search = self.board_search(None);
5661        loop {
5662            let (page, next) = self
5663                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5664                .await?;
5665            found.extend(page);
5666            match next {
5667                Some(next) => after = Some(next),
5668                None => break,
5669            }
5670        }
5671        *self.search_cache()? = Some(found.clone());
5672        Ok(found)
5673    }
5674
5675    /// Every issue this board's own search reports with `also` beside its scope, walked to
5676    /// exhaustion — held for no later read, which may not be narrowed alike.
5677    async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5678        let mut after: Option<String> = None;
5679        let mut found = Vec::new();
5680        let search = self.board_search(Some(also));
5681        loop {
5682            let (page, next) = self
5683                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5684                .await?;
5685            found.extend(page);
5686            match next {
5687                Some(next) => after = Some(next),
5688                None => return Ok(found),
5689            }
5690        }
5691    }
5692
5693    /// This process's own view of the board's issues, or the refusal a poisoned lock is.
5694    fn search_cache(
5695        &self,
5696    ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
5697        self.search_cache
5698            .lock()
5699            .map_err(|_| SourceError::Unavailable {
5700                message: "this source's view of the board's issues was left inconsistent by an \
5701                      earlier failure; next: run the command again"
5702                    .into(),
5703            })
5704    }
5705
5706    /// `found`, with everything this run wrote that `keep` accepts and the read did not
5707    /// report.
5708    ///
5709    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
5710    /// at all: the search index is behind, and a node read of an item filed moments ago can
5711    /// be too.
5712    fn completed_with_written(
5713        &self,
5714        mut found: Vec<Resolved>,
5715        keep: impl Fn(&Resolved) -> bool,
5716    ) -> Result<Vec<Resolved>, SourceError> {
5717        for own in self.created()?.iter().filter(|own| keep(own)) {
5718            if !found.iter().any(|item| item.id == own.id) {
5719                found.push(own.clone());
5720            }
5721        }
5722        Ok(found)
5723    }
5724
5725    /// What resolving one node id reached.
5726    ///
5727    /// Three answers rather than an `Option`, because a board *draft* is none of the other
5728    /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
5729    /// is completed by a read of the draft itself rather than reported as nothing.
5730    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
5731        let asked = self
5732            .graphql(
5733                graphql::ISSUE,
5734                json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
5735                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5736            )
5737            .await;
5738        let data = match asked {
5739            Ok(data) => data,
5740            // A string that is not a node id at all is not a failure to report: it is an id
5741            // this board does not hold, which is what every read of one already answers.
5742            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
5743            Err(error) => return Err(error),
5744        };
5745        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5746            return Ok(Reached::Nothing);
5747        };
5748        if optional_str(node, "__typename")? == Some("DraftIssue") {
5749            return Ok(Reached::Draft);
5750        }
5751        Ok(match self.resolve_issue(node).await? {
5752            Some(item) => Reached::Held(Box::new(item)),
5753            None => Reached::Nothing,
5754        })
5755    }
5756
5757    /// One item of this board by its own id, whatever kind it is.
5758    ///
5759    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
5760    /// run wrote is read first, because a node read of an item created moments ago can
5761    /// still be behind the board field values written onto it — see [`Self::created`].
5762    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5763        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
5764            return Ok(Some(own.clone()));
5765        }
5766        if let Some(held) = self.read_ahead()?.remove(id) {
5767            return Ok(held);
5768        }
5769        match self.reach(id).await? {
5770            Reached::Held(item) => Ok(Some(*item)),
5771            Reached::Nothing => Ok(None),
5772            Reached::Draft => self.draft_by_id(id).await,
5773        }
5774    }
5775
5776    /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
5777    /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
5778    /// than one request per id.
5779    ///
5780    /// What this run wrote answers first, as it does there, and only the rest is read. One id
5781    /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
5782    /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
5783    /// id at a time, so that id is answered as not held and the others as themselves; a draft
5784    /// is completed by a read of the draft, exactly as there.
5785    async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
5786        let mut found: Vec<Option<Option<Resolved>>> = {
5787            let created = self.created()?;
5788            ids.iter()
5789                .map(|id| {
5790                    created
5791                        .iter()
5792                        .find(|own| own.id == *id)
5793                        .map(|own| Some(own.clone()))
5794                })
5795                .collect()
5796        };
5797        let unread: Vec<NativeId> = ids
5798            .iter()
5799            .zip(&found)
5800            .filter(|(_, found)| found.is_none())
5801            .map(|(id, _)| id.clone())
5802            .collect();
5803        let mut read = Vec::with_capacity(unread.len());
5804        if let [one] = unread.as_slice() {
5805            read.push(self.item_by_id(one).await?);
5806        } else {
5807            for batch in unread.chunks(DETAIL_BATCH) {
5808                let data = match self
5809                    .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
5810                    .await
5811                {
5812                    Ok(data) => data,
5813                    Err(error) if unresolvable_node(&error) => {
5814                        for id in batch {
5815                            read.push(self.item_by_id(id).await?);
5816                        }
5817                        continue;
5818                    }
5819                    Err(error) => return Err(error),
5820                };
5821                for (slot, id) in batch.iter().enumerate() {
5822                    let node =
5823                        data.get(format!("i{slot}"))
5824                            .ok_or_else(|| SourceError::Malformed {
5825                                message: format!(
5826                                    "GitHub answered a batch read with no item for {}",
5827                                    id.0
5828                                ),
5829                            })?;
5830                    read.push(if node.is_null() {
5831                        None
5832                    } else if optional_str(node, "__typename")? == Some("DraftIssue") {
5833                        self.draft_by_id(id).await?
5834                    } else {
5835                        if optional_str(node, "__typename")? == Some("Issue")
5836                            && required_str(node, "id")? != id.0
5837                        {
5838                            return Err(SourceError::Malformed {
5839                                message: format!(
5840                                    "GitHub answered the read of {} with issue {}",
5841                                    id.0,
5842                                    required_str(node, "id")?
5843                                ),
5844                            });
5845                        }
5846                        self.resolve_issue(node).await?
5847                    });
5848                }
5849            }
5850        }
5851        let mut read = read.into_iter();
5852        Ok(found
5853            .iter_mut()
5854            .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
5855            .collect())
5856    }
5857
5858    fn read_ahead(
5859        &self,
5860    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
5861        self.read_ahead
5862            .lock()
5863            .map_err(|_| SourceError::Unavailable {
5864                message:
5865                    "items read ahead for a copy were left inconsistent; run the command again"
5866                        .into(),
5867            })
5868    }
5869
5870    fn resolved_cache(
5871        &self,
5872    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
5873        self.resolved_cache
5874            .lock()
5875            .map_err(|_| SourceError::Unavailable {
5876                message: "resolved item records were left inconsistent; run the command again"
5877                    .into(),
5878            })
5879    }
5880
5881    /// Reuse a record this invocation already resolved. The mutation sender invalidates
5882    /// it before writing, so a partial failure cannot leave a pre-write binding behind.
5883    async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5884        let cached = self.resolved_cache()?.get(id).cloned();
5885        if let Some(item) = cached {
5886            return Ok(Some(item));
5887        }
5888        self.item_by_id(id).await
5889    }
5890
5891    /// Take out the item an issue read by reference reached for `id`, when one is held: the
5892    /// status write it was read for is the one caller, so no other read or write is ever
5893    /// answered from a read that selected only what that write needs.
5894    fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5895        Ok(self
5896            .held_cache
5897            .lock()
5898            .map_err(|_| SourceError::Unavailable {
5899                message: "items read by issue reference were left inconsistent; run the \
5900                          command again"
5901                    .into(),
5902            })?
5903            .remove(id))
5904    }
5905
5906    /// One board draft by its own id, with the board item it sits in — or `None` when no
5907    /// item of this board is that draft's.
5908    ///
5909    /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
5910    /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
5911    /// links a draft to one board item, so the page this read carries is the whole of that
5912    /// connection, and a page that reports more than it holds is refused rather than read
5913    /// as an answer about memberships nobody read.
5914    async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5915        let data = self
5916            .graphql(
5917                graphql::DRAFT,
5918                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
5919                       "boardItems":BOARD_ITEMS_PAGE_SIZE}),
5920            )
5921            .await?;
5922        // Gone between the two reads is an answer — the draft is no longer there. Anything
5923        // else than the draft [`Self::reach`] was just told this id is, is not one.
5924        let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
5925            return Ok(None);
5926        };
5927        if optional_str(draft, "__typename")? != Some("DraftIssue") {
5928            return Err(SourceError::Malformed {
5929                message: format!(
5930                    "GitHub answered {} as a draft and then as something else",
5931                    id.0
5932                ),
5933            });
5934        }
5935        if required_str(draft, "id")? != id.0 {
5936            return Err(SourceError::Malformed {
5937                message: format!("GitHub answered a different draft for {}", id.0),
5938            });
5939        }
5940        let memberships = draft
5941            .get("projectV2Items")
5942            .ok_or_else(|| SourceError::Malformed {
5943                message: format!("GitHub draft {} is missing projectV2Items", id.0),
5944            })?;
5945        let nodes = memberships
5946            .get("nodes")
5947            .and_then(Value::as_array)
5948            .ok_or_else(|| SourceError::Malformed {
5949                message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
5950            })?;
5951        let info = memberships
5952            .get("pageInfo")
5953            .ok_or_else(|| SourceError::Malformed {
5954                message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
5955            })?;
5956        // Read whether or not this board's entry is on the page: a page claiming more than
5957        // the one item GitHub links a draft to is a malformed answer either way.
5958        if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
5959            return Err(SourceError::Malformed {
5960                message: format!(
5961                    "GitHub draft {} reports more board items than the one GitHub links a draft \
5962                     to",
5963                    id.0
5964                ),
5965            });
5966        }
5967        if let Some(node) = nodes.first()
5968            && node
5969                .pointer("/project/number")
5970                .and_then(Value::as_u64)
5971                .is_none()
5972        {
5973            return Err(SourceError::Malformed {
5974                message: format!(
5975                    "GitHub draft {} board item has no numeric project number",
5976                    id.0
5977                ),
5978            });
5979        }
5980        let Some(held) = self.board_entry(nodes) else {
5981            return Ok(None);
5982        };
5983        if required_str(
5984            held.get("project").ok_or_else(|| SourceError::Malformed {
5985                message: format!("GitHub draft {} board item has no project", id.0),
5986            })?,
5987            "id",
5988        )? != self.board_fields().await?.id.as_str()
5989        {
5990            return Ok(None);
5991        }
5992        let item = json!({
5993            "id": required_str(held, "id")?,
5994            "project": held.get("project"),
5995            "fieldValues": held.get("fieldValues"),
5996            "content": draft,
5997        });
5998        self.resolve(&item)
5999    }
6000
6001    /// The board's own id and field definitions, for a write whose item does not carry
6002    /// them — never its items.
6003    ///
6004    /// A board this command has already listed supplies them, since it read them beside its
6005    /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
6006    /// is consulted about which items the board holds: see the module documentation for
6007    /// why a question about one known item is answered by reading that item.
6008    async fn board_fields(&self) -> Result<BoardFields, SourceError> {
6009        if let Some(board) = self.board_cache()?.as_ref() {
6010            return Ok(BoardFields {
6011                id: BoardId::parse(&board.id)?,
6012                fields: board.fields.clone(),
6013            });
6014        }
6015        if let Some(held) = self.fields_cache()?.clone() {
6016            return Ok(held);
6017        }
6018        let data = self
6019            .graphql(
6020                graphql::BOARD_FIELDS,
6021                json!({"owner":self.owner,"number":self.project_number,
6022                       "nestedFirst":NESTED_PAGE_SIZE}),
6023            )
6024            .await?;
6025        self.fields_read(&data)
6026    }
6027
6028    /// The board's id and fields out of an answer carrying the `boardFields` root, held for
6029    /// the rest of this command.
6030    fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
6031        let board = data
6032            .pointer("/boardFields/projectV2")
6033            .filter(|value| !value.is_null())
6034            .ok_or_else(|| SourceError::Refused {
6035                message: format!(
6036                    "GitHub project {}/{} was not found or is not visible to the token",
6037                    self.owner, self.project_number
6038                ),
6039            })?;
6040        let read = BoardFields {
6041            id: BoardId::parse(required_str(board, "id")?)?,
6042            fields: board.get("fields").cloned().unwrap_or(Value::Null),
6043        };
6044        *self.fields_cache()? = Some(read.clone());
6045        Ok(read)
6046    }
6047
6048    /// Read what creating an issue in `repository` needs and this command has not read yet —
6049    /// the board's fields and the repository's node id — in one request when it needs both.
6050    ///
6051    /// When either is already known this sends nothing, and the other is read by its own
6052    /// document where it is asked for, so no create reads anything twice.
6053    async fn creation_context(
6054        &self,
6055        repository: &RepositoryTarget,
6056        incoming: &Incoming<'_>,
6057    ) -> Result<(), SourceError> {
6058        let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
6059        if fields_known || self.repository_cache()?.contains_key(repository) {
6060            return Ok(());
6061        }
6062        let data = self
6063            .graphql(
6064                graphql::CREATION_CONTEXT,
6065                json!({"owner":self.owner,"number":self.project_number,
6066                       "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
6067                       "repositoryName":repository.name}),
6068            )
6069            .await?;
6070        self.fields_read(&data)?;
6071        self.repository_read(&data, repository, incoming)?;
6072        Ok(())
6073    }
6074
6075    /// This process's own view of the board's fields, or the refusal a poisoned lock is.
6076    fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
6077        self.fields_cache
6078            .lock()
6079            .map_err(|_| SourceError::Unavailable {
6080                message: "this source's view of the board's fields was left inconsistent by an \
6081                      earlier failure; next: run the command again"
6082                    .into(),
6083            })
6084    }
6085
6086    /// What a write to `item` needs of the board, read off that item when it says enough and
6087    /// off [`Self::board_fields`] when it does not.
6088    ///
6089    /// A node read of an item names its board and carries the definition of every field it
6090    /// holds a value of — so an item naming its board, holding a value of the origin field,
6091    /// and, when the write carries a status, holding a `Status` value, needs no read of the
6092    /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
6093    /// of may still be on the board, and a view reading it as absent would refuse a write the
6094    /// board can take or skip a field write the board needs, so such an item — and a create,
6095    /// which has no item yet — takes the board's fields from their own read instead.
6096    async fn fields_for(
6097        &self,
6098        item: Option<&Resolved>,
6099        writes_status: bool,
6100        selects_priority: bool,
6101    ) -> Result<BoardFields, SourceError> {
6102        if let Some(board) = item.and_then(Resolved::carried_board) {
6103            return Ok(board);
6104        }
6105        if let Some(item) = item
6106            && let Some(board_id) = item.named_board()
6107            && item.defines(ORIGIN_FIELD)
6108            && (!writes_status || item.defines("Status"))
6109            && (!selects_priority || item.defines(PRIORITY_FIELD))
6110            && self
6111                .metadata_fields
6112                .iter()
6113                .all(|projection| item.defines(&projection.field))
6114        {
6115            return Ok(BoardFields {
6116                id: board_id,
6117                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6118            });
6119        }
6120        self.board_fields().await
6121    }
6122
6123    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
6124    /// when that id names nothing here with a sub-issue relationship to walk.
6125    ///
6126    /// `None` and an empty answer are different: `None` is *this is not an issue of this
6127    /// GitHub*, which is what sends a project selector on to be read as a name, and an
6128    /// empty vector is a project that holds nothing.
6129    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6130        let mut after: Option<String> = None;
6131        let mut children = Vec::new();
6132        // The project issue as the first page carried it, held once the walk has ended.
6133        let mut parent: Option<Resolved> = None;
6134        loop {
6135            let asked = self
6136                .graphql(
6137                    graphql::SUB_ISSUES,
6138                    json!({"id":id.0,"memberFirst":MEMBER_PAGE_SIZE,"after":after,
6139                           "nestedFirst":NESTED_PAGE_SIZE,
6140                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6141                )
6142                .await;
6143            let data = match asked {
6144                Ok(data) => data,
6145                // A string that is not a node id at all is not a failure to report: it is
6146                // the ordinary answer to a selector naming a project by its name.
6147                Err(error) if unresolvable_node(&error) => return Ok(None),
6148                Err(error) => return Err(error),
6149            };
6150            let Some(connection) = data
6151                .pointer("/node/subIssues")
6152                .filter(|value| !value.is_null())
6153            else {
6154                // No such node, or one with no sub-issue relationship — a board draft is
6155                // the one this board can really hold.
6156                return Ok(None);
6157            };
6158            if after.is_none()
6159                && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
6160            {
6161                parent = self
6162                    .resolve_issue(node)
6163                    .await?
6164                    .filter(|resolved| resolved.id == *id);
6165            }
6166            for node in connection
6167                .get("nodes")
6168                .and_then(Value::as_array)
6169                .ok_or_else(|| SourceError::Malformed {
6170                    message: "GitHub subIssues.nodes is not an array".into(),
6171                })?
6172            {
6173                if let Some(resolved) = self.resolve_issue(node).await? {
6174                    children.push(resolved);
6175                }
6176            }
6177            let info = connection
6178                .get("pageInfo")
6179                .ok_or_else(|| SourceError::Malformed {
6180                    message: "GitHub subIssues connection has no pageInfo".into(),
6181                })?;
6182            let next = required_bool(info, "hasNextPage")?
6183                .then(|| required_str(info, "endCursor"))
6184                .transpose()?;
6185            match next {
6186                Some(next) => {
6187                    validate_cursor_progress(after.as_deref(), next)?;
6188                    after = Some(next.to_owned());
6189                }
6190                None => {
6191                    // Held only once the walk has ended, so one failing part way holds
6192                    // nothing and every read it would have answered is made.
6193                    let mut carried = self.carried()?;
6194                    for item in parent.into_iter().chain(children.iter().cloned()) {
6195                        carried.insert(item.id.clone(), item);
6196                    }
6197                    return Ok(Some(children));
6198                }
6199            }
6200        }
6201    }
6202
6203    fn carried(
6204        &self,
6205    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6206        self.carried.lock().map_err(|_| SourceError::Unavailable {
6207            message: "items a project's listing carried were left inconsistent; run the \
6208                      command again"
6209                .into(),
6210        })
6211    }
6212
6213    /// Which issue of this board a project *name* is, or `None` when none is.
6214    ///
6215    /// One bounded query which filters on that name at the server, rather than a walk of
6216    /// every issue the board holds. The name is compared again here: the qualifier narrows
6217    /// what GitHub sends, and this source decides what it names.
6218    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
6219        let search = self.board_search(Some(&title_qualifier(name)));
6220        let mut after = None;
6221        loop {
6222            let (candidates, next) = self
6223                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6224                .await?;
6225            if let Some(item) = candidates.into_iter().find(|item| {
6226                item.kind == BoardKind::Work(ItemKind::Project)
6227                    && item.title.eq_ignore_ascii_case(name)
6228            }) {
6229                return Ok(Some(item.id));
6230            }
6231            match next {
6232                Some(next) => after = Some(next),
6233                None => return Ok(None),
6234            }
6235        }
6236    }
6237
6238    /// Everything filed under one project of this board: the sub-issues of the issue that
6239    /// project is.
6240    ///
6241    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
6242    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
6243    /// gains projects, or as another project gains tasks.
6244    ///
6245    /// A qualified id names the issue and is asked for its sub-issues directly: one
6246    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
6247    /// read as a project *name*, which costs the one bounded search
6248    /// [`Self::project_by_name`] makes.
6249    ///
6250    /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
6251    /// — and each answer is still cut to the items whose parent is this project, so one this
6252    /// process has since filed elsewhere is not reported here, and completed with what this
6253    /// process filed under it.
6254    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
6255        let held = self.children_cache()?.get(selector).cloned();
6256        let (project, children) = match held {
6257            Some(held) => held,
6258            None => {
6259                let answered = match self.sub_issues(selector).await? {
6260                    Some(children) => (selector.clone(), children),
6261                    None => match self.project_by_name(&selector.0).await? {
6262                        Some(project) => {
6263                            let children = self.sub_issues(&project).await?.unwrap_or_default();
6264                            (project, children)
6265                        }
6266                        None => return Ok(Vec::new()),
6267                    },
6268                };
6269                self.children_cache()?
6270                    .insert(selector.clone(), answered.clone());
6271                answered
6272            }
6273        };
6274        let mut children: Vec<Resolved> = children
6275            .into_iter()
6276            .filter(|child| child.parent.as_ref() == Some(&project))
6277            .collect();
6278        for own in self.updated()?.iter() {
6279            if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
6280                children.push(own.clone());
6281            }
6282        }
6283        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
6284    }
6285
6286    /// Every issue of this board GitHub's issue search reports updated at or after `since`,
6287    /// completed with what this run wrote — the candidates a comment-activity read confirms.
6288    ///
6289    /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
6290    /// on the board whatever repository, and whatever owner, it lives in, so no repository or
6291    /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
6292    /// sufficient is a fact about GitHub rather than about this source: a comment written on an
6293    /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
6294    /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
6295    /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
6296    /// rather than silently narrowing a caller's answer.
6297    ///
6298    /// The instant is written to the second, rounded down, which can only widen what the
6299    /// search returns; confirmation against each candidate's own comments is what makes the
6300    /// answer exact. The search is an index that lags a write by a second or two — the module
6301    /// documentation records it — so a caller that asks again from its last instant should
6302    /// overlap the two by more than that.
6303    async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
6304        let found = self.searched(&updated_qualifier(since)).await?;
6305        self.completed_with_written(found, |_| true)
6306    }
6307
6308    /// Every issue of this board GitHub's issue search reports for the board-scoped search
6309    /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
6310    ///
6311    /// Uncompleted: what this process wrote is added by the caller, which knows whether its
6312    /// own record is the fresher of the two.
6313    async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6314        let search = self.board_search(Some(also));
6315        let mut after: Option<String> = None;
6316        let mut found = Vec::new();
6317        loop {
6318            let (page, next) = self
6319                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6320                .await?;
6321            found.extend(page);
6322            match next {
6323                Some(next) => after = Some(next),
6324                None => return Ok(found),
6325            }
6326        }
6327    }
6328
6329    /// A bounded task answer; the versioned cursor carries the connection position, how
6330    /// many rows of the page starting there were already handed out, and the own-write ids
6331    /// already observed, including across a new source instance.
6332    ///
6333    /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
6334    /// sliced from the pages it needs; why is the module documentation's paging contract.
6335    async fn search_tasks(
6336        &self,
6337        query: &TaskQuery,
6338        page: &PageRequest,
6339        also: &str,
6340    ) -> Result<Page<Task>, SourceError> {
6341        let mut position = match &page.cursor {
6342            None => SearchPosition::default(),
6343            Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
6344                .ok()
6345                .filter(|position| {
6346                    position.version == SEARCH_CURSOR_VERSION
6347                        && position.connection.valid_resume(position.offset)
6348                })
6349                .ok_or_else(|| SourceError::Config {
6350                    message: "page cursor is invalid".into(),
6351                })?,
6352        };
6353        let search = self.board_search(Some(also));
6354        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6355        let own = self.with_own_writes(Vec::new())?;
6356        // An issue this process commented on is a candidate of a comment-activity read
6357        // whether or not the search has caught up with the comment; see `Self::commented`.
6358        let commented = match query.commented_since {
6359            Some(_) => self.commented()?.clone(),
6360            None => Vec::new(),
6361        };
6362        for id in own.iter().map(|item| &item.id).chain(&commented) {
6363            if !position.own.contains(id) {
6364                position.own.push(id.clone());
6365            }
6366        }
6367        let mut tasks = Vec::new();
6368        while !position.connection.exhausted() && tasks.len() < limit {
6369            let first = SEARCH_PAGE_SIZE;
6370            // Page size is part of the key: a short cached answer cannot answer a wider ask.
6371            let key =
6372                serde_json::to_string(&("page", &search, &position.connection.after(), first))
6373                    .expect("search page key is serializable");
6374            let cached = if query.commented_since.is_none() {
6375                self.narrowed_cache()?.get(&key).cloned()
6376            } else {
6377                None
6378            };
6379            let (found, next) = match cached {
6380                Some(found) => {
6381                    let next = self
6382                        .search_next
6383                        .lock()
6384                        .map_err(|_| SourceError::Unavailable {
6385                            message:
6386                                "search pagination was left inconsistent; run the command again"
6387                                    .into(),
6388                        })?
6389                        .get(&key)
6390                        .cloned()
6391                        .flatten();
6392                    (found, next)
6393                }
6394                None => {
6395                    let (found, next) = self
6396                        .search_page(&search, first, position.connection.after())
6397                        .await?;
6398                    if query.commented_since.is_none() {
6399                        self.search_next
6400                            .lock()
6401                            .map_err(|_| SourceError::Unavailable {
6402                                message:
6403                                    "search pagination was left inconsistent; run the command again"
6404                                        .into(),
6405                            })?
6406                            .insert(key.clone(), next.clone());
6407                        self.narrowed_cache()?.insert(key, found.clone());
6408                    }
6409                    (found, next)
6410                }
6411            };
6412            let rows = found.len();
6413            for mut item in found.into_iter().skip(position.offset) {
6414                if tasks.len() == limit {
6415                    break;
6416                }
6417                position.offset += 1;
6418                if position.own.contains(&item.id) {
6419                    if position.seen.contains(&item.id) {
6420                        continue;
6421                    }
6422                    position.seen.push(item.id.clone());
6423                    // The search's own copy of an issue this process only commented on is as
6424                    // good as a node read of it, since its comments are read either way.
6425                    let only_commented = commented.contains(&item.id)
6426                        && !own.iter().any(|written| written.id == item.id);
6427                    if !only_commented {
6428                        let updated_at = item.updated_at;
6429                        let Some(written) = self.search_written(&own, &item.id).await? else {
6430                            continue;
6431                        };
6432                        item = written;
6433                        item.updated_at = item.updated_at.max(updated_at);
6434                        self.resolved_cache()?.insert(item.id.clone(), item.clone());
6435                    }
6436                }
6437                if item.kind == BoardKind::Work(ItemKind::Task) {
6438                    let task = item.task()?;
6439                    if task_matches(&task, query, &query.project)
6440                        && self.commented_since(&item, query.commented_since).await?
6441                    {
6442                        tasks.push(task);
6443                    }
6444                }
6445            }
6446            if position.offset < rows {
6447                continue;
6448            }
6449            position.offset = 0;
6450            position.connection = match next {
6451                Some(after) => SearchConnection::Continuing {
6452                    after: Cursor(after),
6453                },
6454                None => SearchConnection::Exhausted {},
6455            };
6456        }
6457        if position.connection.exhausted() {
6458            for id in position.own.clone() {
6459                if position.seen.contains(&id) {
6460                    continue;
6461                }
6462                if tasks.len() == limit {
6463                    break;
6464                }
6465                position.seen.push(id.clone());
6466                let Some(item) = self.search_written(&own, &id).await? else {
6467                    continue;
6468                };
6469                if item.kind == BoardKind::Work(ItemKind::Task) {
6470                    let task = item.task()?;
6471                    if task_matches(&task, query, &query.project)
6472                        && self.commented_since(&item, query.commented_since).await?
6473                    {
6474                        tasks.push(task);
6475                    }
6476                }
6477            }
6478        }
6479        let more = !position.connection.exhausted()
6480            || position.own.iter().any(|id| !position.seen.contains(id));
6481        Ok(Page {
6482            items: tasks,
6483            next: more.then(|| {
6484                Cursor(serde_json::to_string(&position).expect("search position is serializable"))
6485            }),
6486        })
6487    }
6488
6489    /// A resumed process has the ids but no write records; resolve only a record the
6490    /// current page needs, by its uncached node read rather than the lagging search index.
6491    async fn search_written(
6492        &self,
6493        own: &[Resolved],
6494        id: &NativeId,
6495    ) -> Result<Option<Resolved>, SourceError> {
6496        match own.iter().find(|item| item.id == *id) {
6497            Some(item) => Ok(Some(item.clone())),
6498            None => self.item_by_id(id).await,
6499        }
6500    }
6501
6502    /// The candidates for a task query carrying a text, metadata or origin predicate, read
6503    /// without enumerating the board — or `None` for a query carrying none of the three, which
6504    /// keeps the reads it always had.
6505    ///
6506    /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
6507    /// because it names at most a handful of items. Text and metadata are answered by one
6508    /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
6509    /// further by `updated:>=` when the query also asks for comment activity, since both
6510    /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
6511    /// process afterwards by the same predicates [`task_matches`] applies to every read.
6512    ///
6513    /// Completed with what this process wrote, its own record winning over the index's copy
6514    /// of the same item: see [`Self::with_own_writes`].
6515    async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
6516        let asked = match (&query.origin, narrowing_qualifiers(query)) {
6517            (Some(origin), _) => Narrowing::Origin(origin.clone()),
6518            (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
6519                Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
6520                None => qualifiers,
6521            }),
6522            (None, None) => return Ok(None),
6523        };
6524        // A question about comment activity is asked afresh every time, as it always was: it
6525        // is the one a caller polls from one source while waiting for the index, and an
6526        // answer held from the first poll would be the answer to every later one.
6527        let key = query.commented_since.is_none().then(|| asked.key());
6528        let cached = match &key {
6529            Some(key) => self.narrowed_cache()?.get(key).cloned(),
6530            None => None,
6531        };
6532        let found = match cached {
6533            Some(found) => found,
6534            None => {
6535                let found = match &asked {
6536                    Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
6537                    Narrowing::Search(also) => self.searched(also).await?,
6538                };
6539                if let Some(key) = key {
6540                    self.narrowed_cache()?.insert(key, found.clone());
6541                }
6542                found
6543            }
6544        };
6545        self.with_own_writes(found).map(Some)
6546    }
6547
6548    /// The candidates for a project or unscoped document query carrying a searchable text,
6549    /// read without enumerating the board — or `None` for a query with no text or a blank one,
6550    /// which keeps the read it always had.
6551    ///
6552    /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
6553    /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
6554    /// costs is the issues that match and never the board. Its answer is held for the command
6555    /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
6556    /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
6557    /// substring rule, exactly as an item of the wider read was, and is completed with what this
6558    /// process wrote: see [`Self::with_own_writes`].
6559    async fn text_searched(
6560        &self,
6561        text: Option<&TextQuery>,
6562    ) -> Result<Option<Vec<Resolved>>, SourceError> {
6563        let Some(also) = text_qualifiers(text) else {
6564            return Ok(None);
6565        };
6566        let key = Narrowing::Search(also.clone()).key();
6567        let cached = self.narrowed_cache()?.get(&key).cloned();
6568        let found = match cached {
6569            Some(found) => found,
6570            None => {
6571                let found = self.searched(&also).await?;
6572                self.narrowed_cache()?.insert(key, found.clone());
6573                found
6574            }
6575        };
6576        self.with_own_writes(found).map(Some)
6577    }
6578
6579    /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
6580    /// [`Self::narrowed`] reads for a task query carrying that origin — one
6581    /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
6582    /// what this process wrote — each confirmed against its own origin entry.
6583    ///
6584    /// What answers a project or document listing by origin, so finding every copy of one plan
6585    /// costs the items that carry its origin and never a page of the board.
6586    async fn copied_from(
6587        &self,
6588        origin: &str,
6589        kind: BoardKind,
6590    ) -> Result<Vec<Resolved>, SourceError> {
6591        let wanted = TaskQuery {
6592            origin: Some(origin.to_owned()),
6593            ..TaskQuery::default()
6594        };
6595        Ok(self
6596            .narrowed(&wanted)
6597            .await?
6598            .unwrap_or_default()
6599            .into_iter()
6600            .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
6601            .collect())
6602    }
6603
6604    /// Every item of this board that may carry `origin` — a superset of those that do — found
6605    /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
6606    ///
6607    /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
6608    /// which reads the field every carrier holds, whichever release wrote it — and the
6609    /// board-scoped issue search for the same id as a phrase in the body, where this source
6610    /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
6611    /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
6612    /// query's, exactly.
6613    ///
6614    /// Both connections are walked to exhaustion, each from its own cursor. One that has
6615    /// already ended is sent its last cursor again, which answers an empty page, so the one
6616    /// document serves every page of either. What the two leave is stated in the module
6617    /// documentation: a carrier another process added within the last second or two, before
6618    /// either index has it.
6619    async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
6620        let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
6621        let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
6622        let mut items_after: Option<String> = None;
6623        let mut search_after: Option<String> = None;
6624        let mut found: Vec<Resolved> = Vec::new();
6625        let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
6626            if !found.iter().any(|held| held.id == resolved.id) {
6627                found.push(resolved);
6628            }
6629        };
6630        loop {
6631            let data = self
6632                .graphql(
6633                    graphql::ORIGIN_LOOKUP,
6634                    json!({"owner":self.owner,"number":self.project_number,"filter":filter,
6635                           "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
6636                           "itemsAfter":items_after,"searchAfter":search_after,
6637                           "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6638                           "duplicates":true}),
6639                )
6640                .await?;
6641            let items = data
6642                .pointer("/originItems/projectV2/items")
6643                .filter(|value| !value.is_null())
6644                .ok_or_else(|| SourceError::Refused {
6645                    message: format!(
6646                        "GitHub project {}/{} was not found or is not visible to the token",
6647                        self.owner, self.project_number
6648                    ),
6649                })?;
6650            for item in optional_nodes(Some(items), "project items")?
6651                .into_iter()
6652                .flatten()
6653            {
6654                // The board's own items list its drafts too, and a draft is not an issue: no
6655                // narrowed read answers with one, whatever its origin field holds.
6656                if let Some(resolved) = self.resolve(item)?
6657                    && resolved.content_kind == ContentKind::Issue
6658                {
6659                    keep(resolved, &mut found);
6660                }
6661            }
6662            let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
6663                message: "GitHub search response has no search connection".into(),
6664            })?;
6665            for node in optional_nodes(Some(searched), "search")?
6666                .into_iter()
6667                .flatten()
6668            {
6669                if let Some(resolved) = self.resolve_issue(node).await? {
6670                    keep(resolved, &mut found);
6671                }
6672            }
6673            let items_next = resumed(items, items_after.as_deref())?;
6674            let search_next = resumed(searched, search_after.as_deref())?;
6675            if !items_next.has_more() && !search_next.has_more() {
6676                return Ok(found);
6677            }
6678            items_after = items_next.cursor();
6679            search_after = search_next.cursor();
6680        }
6681    }
6682
6683    /// `found`, with every item this process created or wrote in its place, and every one of
6684    /// them the read did not report added.
6685    ///
6686    /// This process's own record wins over the read's copy of the same item, because a read
6687    /// of an item written moments ago can still be behind what was written onto it — the
6688    /// origin field included, which is the one a narrowed read is confirmed against — and a
6689    /// read that still names an item under a predicate this process's write moved it out of
6690    /// must not return it. The one thing the read knows that the record cannot is when GitHub
6691    /// last saw the item change, which is what a comment-activity read rules a candidate out
6692    /// by, so the read's `updatedAt` is kept when the record has none of its own. See
6693    /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
6694    fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
6695        // A board draft is not an issue, so no narrowed read returns one, and this process
6696        // having written one does not make it an answer either.
6697        let own: Vec<Resolved> = self
6698            .created()?
6699            .iter()
6700            .chain(self.updated()?.iter())
6701            .filter(|own| own.content_kind == ContentKind::Issue)
6702            .cloned()
6703            .collect();
6704        for mut own in own {
6705            self.resolved_cache()?.insert(own.id.clone(), own.clone());
6706            match found.iter_mut().find(|read| read.id == own.id) {
6707                Some(read) => {
6708                    own.updated_at = own.updated_at.max(read.updated_at);
6709                    *read = own;
6710                }
6711                None => found.push(own),
6712            }
6713        }
6714        Ok(found)
6715    }
6716
6717    /// Whether `item` has a comment created or last edited at or after `since` — always, when
6718    /// there is no instant to hold it to.
6719    ///
6720    /// The candidate's own `updatedAt` is read first, because a comment written or edited at
6721    /// or after the instant moved it there: an issue not updated since holds no such comment,
6722    /// and its comments are never asked for — unless this process commented on it in this
6723    /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
6724    /// only as far as the first that matches. A board draft is not an issue and has no
6725    /// comments, so it never matches.
6726    async fn commented_since(
6727        &self,
6728        item: &Resolved,
6729        since: Option<DateTime<Utc>>,
6730    ) -> Result<bool, SourceError> {
6731        let Some(since) = since else {
6732            return Ok(true);
6733        };
6734        if item.content_kind == ContentKind::DraftIssue {
6735            return Ok(false);
6736        }
6737        // An `updatedAt` this process's own record or a lagging index holds can predate a
6738        // comment this process wrote since, so only an issue it did not comment on is ruled
6739        // out by one.
6740        if item.updated_at.is_some_and(|updated| updated < since)
6741            && !self.commented()?.contains(&item.id)
6742        {
6743            return Ok(false);
6744        }
6745        let query = TaskQuery {
6746            commented_since: Some(since),
6747            ..TaskQuery::default()
6748        };
6749        let mut after: Option<String> = None;
6750        loop {
6751            let data = self
6752                .graphql(
6753                    graphql::ISSUE_COMMENTS,
6754                    json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
6755                )
6756                .await?;
6757            let Some(connection) = data
6758                .get("node")
6759                .filter(|value| !value.is_null())
6760                .and_then(|node| node.get("comments"))
6761                .filter(|value| !value.is_null())
6762            else {
6763                // Removed since the search reported it: no longer an issue with comments.
6764                return Ok(false);
6765            };
6766            let comments = optional_nodes(Some(connection), "issue comments")?
6767                .into_iter()
6768                .flatten()
6769                .map(comment_from)
6770                .collect::<Result<Vec<_>, _>>()?;
6771            if query.comments_match(&comments) {
6772                return Ok(true);
6773            }
6774            match next_cursor(connection)? {
6775                Some(next) => {
6776                    validate_cursor_progress(after.as_deref(), &next.0)?;
6777                    after = Some(next.0);
6778                }
6779                None => return Ok(false),
6780            }
6781        }
6782    }
6783
6784    /// Every item on the board: the union of both enumerations GitHub offers of one.
6785    ///
6786    /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
6787    /// board **draft** and reads the board's own fields beside its items, and only the search
6788    /// reports an item that connection is behind on. The module documentation is where the lag and the
6789    /// measurements behind it are written down.
6790    ///
6791    /// A search result is admitted on the same terms as any other issue this source reaches
6792    /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
6793    /// names *this* board — so an issue the index still believes is here after it was taken
6794    /// off is refused rather than reported.
6795    ///
6796    /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
6797    /// which is what the cache could otherwise have broken.
6798    async fn board(&self) -> Result<Board, SourceError> {
6799        self.board_completed_by(None).await
6800    }
6801
6802    /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
6803    /// that half could add is then answered only if it holds.
6804    async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
6805        let cached = self.board_cache()?.clone();
6806        let mut board = match cached {
6807            Some(board) => board,
6808            None => {
6809                let read = self.read_board().await?;
6810                *self.board_cache()? = Some(read.clone());
6811                read
6812            }
6813        };
6814        let searched = match also {
6815            None => self.searched_issues().await?,
6816            Some(also) => self.searched_narrowly(also).await?,
6817        };
6818        for held in searched {
6819            if !board.items.iter().any(|item| item.id == held.id) {
6820                board.items.push(held);
6821            }
6822        }
6823        for own in self.created()?.iter() {
6824            if !board.items.iter().any(|item| item.id == own.id) {
6825                board.items.push(own.clone());
6826            }
6827        }
6828        Ok(board)
6829    }
6830
6831    /// This process's own view of the board, or the refusal a poisoned lock is.
6832    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
6833        self.board_cache
6834            .lock()
6835            .map_err(|_| SourceError::Unavailable {
6836                message: "this source's view of the board was left inconsistent by an earlier \
6837                      failure; next: run the command again"
6838                    .into(),
6839            })
6840    }
6841
6842    /// Bring this process's own view of the board up to an item it has just written.
6843    ///
6844    /// A created item goes to `created`, which is what completes a board read GitHub's own
6845    /// eventual consistency has left behind. An item that was already there is replaced
6846    /// where it sits, so a second write of it in the same command reads its real parent
6847    /// rather than the one it had before the first write.
6848    ///
6849    /// "Where it sits" is three places, and missing an earlier one leaves a stale record
6850    /// that wins: an item this same run created is held in `created` and not in the cached
6851    /// board, and `board` completes the cached board *from* `created`, so replacing only
6852    /// the cached copy of such an item replaces nothing and the read still reports the
6853    /// title it was created with. The search is the third, and it is the one an item the
6854    /// board's own projection is behind on sits in *alone* — which is exactly the item this
6855    /// source is least able to re-read, so leaving it out would put the stale title back on
6856    /// the only items the completion in [`Self::board`] exists for.
6857    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
6858        self.resolved_cache()?.insert(item.id.clone(), item.clone());
6859        if created {
6860            self.created()?.push(item);
6861            return Ok(());
6862        }
6863        {
6864            let mut own = self.created()?;
6865            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
6866                *held = item;
6867                return Ok(());
6868            }
6869        }
6870        {
6871            let mut own = self.updated()?;
6872            match own.iter_mut().find(|held| held.id == item.id) {
6873                Some(held) => *held = item.clone(),
6874                None => own.push(item.clone()),
6875            }
6876        }
6877        if let Some(board) = self.board_cache()?.as_mut()
6878            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
6879        {
6880            *held = item.clone();
6881        }
6882        if let Some(found) = self.search_cache()?.as_mut()
6883            && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
6884        {
6885            *held = item.clone();
6886        }
6887        for found in self.narrowed_cache()?.values_mut() {
6888            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
6889                *held = item.clone();
6890            }
6891        }
6892        for (_, found) in self.children_cache()?.values_mut() {
6893            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
6894                *held = item.clone();
6895            }
6896        }
6897        Ok(())
6898    }
6899
6900    /// Forget one item this process has just deleted, from every half of its own view.
6901    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
6902        if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
6903            created.retain(|held| held.content != *id);
6904        }
6905        self.resolved_cache()?.remove(id);
6906        self.read_ahead()?.remove(id);
6907        self.carried()?.remove(id);
6908        self.created()?.retain(|own| own.id != *id);
6909        self.updated()?.retain(|own| own.id != *id);
6910        self.commented()?.retain(|own| own != id);
6911        if let Some(board) = self.board_cache()?.as_mut() {
6912            board.items.retain(|item| item.id != *id);
6913        }
6914        if let Some(found) = self.search_cache()?.as_mut() {
6915            found.retain(|item| item.id != *id);
6916        }
6917        for found in self.narrowed_cache()?.values_mut() {
6918            found.retain(|item| item.id != *id);
6919        }
6920        for (_, found) in self.children_cache()?.values_mut() {
6921            found.retain(|item| item.id != *id);
6922        }
6923        Ok(())
6924    }
6925
6926    /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
6927    fn narrowed_cache(
6928        &self,
6929    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
6930        self.narrowed_cache
6931            .lock()
6932            .map_err(|_| SourceError::Unavailable {
6933                message: "this source's view of a narrowed read was left inconsistent by an \
6934                      earlier failure; next: run the command again"
6935                    .into(),
6936            })
6937    }
6938
6939    /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
6940    /// is.
6941    /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
6942    fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6943        Ok(self.task_children()?.get(project).cloned())
6944    }
6945
6946    fn task_children(
6947        &self,
6948    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
6949        self.task_children
6950            .lock()
6951            .map_err(|_| SourceError::Unavailable {
6952                message: "this source's record of a project's tasks was left inconsistent by an \
6953                          earlier failure; next: run the command again"
6954                    .into(),
6955            })
6956    }
6957
6958    fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
6959        self.children_cache
6960            .lock()
6961            .map_err(|_| SourceError::Unavailable {
6962                message: "this source's view of a project's tasks was left inconsistent by an \
6963                      earlier failure; next: run the command again"
6964                    .into(),
6965            })
6966    }
6967
6968    /// Every page of the board, read from GitHub.
6969    async fn read_board(&self) -> Result<Board, SourceError> {
6970        let mut after: Option<String> = None;
6971        let mut items = Vec::new();
6972        let mut board;
6973        loop {
6974            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
6975            for item in page
6976                .pointer("/items/nodes")
6977                .and_then(Value::as_array)
6978                .ok_or_else(|| SourceError::Malformed {
6979                    message: "GitHub project items.nodes is not an array".into(),
6980                })?
6981            {
6982                if let Some(resolved) = self.resolve(item)? {
6983                    items.push(resolved);
6984                }
6985            }
6986            let info = page
6987                .pointer("/items/pageInfo")
6988                .ok_or_else(|| SourceError::Malformed {
6989                    message: "GitHub project items have no pageInfo".into(),
6990                })?;
6991            let has_next = required_bool(info, "hasNextPage")?;
6992            let next = has_next
6993                .then(|| required_str(info, "endCursor"))
6994                .transpose()?;
6995            board = page.clone();
6996            match next {
6997                Some(next) => {
6998                    validate_cursor_progress(after.as_deref(), next)?;
6999                    after = Some(next.to_owned());
7000                }
7001                None => break,
7002            }
7003        }
7004        Ok(Board {
7005            id: required_str(&board, "id")?.to_owned(),
7006            fields: board.get("fields").cloned().unwrap_or(Value::Null),
7007            items,
7008        })
7009    }
7010
7011    /// The existing items this source has written, for completing a narrowed read that is
7012    /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
7013    fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7014        self.updated.lock().map_err(|_| SourceError::Unavailable {
7015            message: "this source's record of what it wrote in this run was left inconsistent \
7016                      by an earlier failure; next: run the command again"
7017                .into(),
7018        })
7019    }
7020
7021    /// The issues this source has commented on in this command; see
7022    /// [`Self::commented`](GitHubProjectsSource::commented).
7023    fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
7024        self.commented.lock().map_err(|_| SourceError::Unavailable {
7025            message: "this source's record of what it commented on in this run was left \
7026                      inconsistent by an earlier failure; next: run the command again"
7027                .into(),
7028        })
7029    }
7030
7031    /// Called only once GitHub has answered the comment write, so an issue whose comment
7032    /// failed is never made a candidate a later read would pay a node read for.
7033    fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
7034        let mut commented = self.commented()?;
7035        if !commented.contains(issue) {
7036            commented.push(issue.clone());
7037        }
7038        Ok(())
7039    }
7040
7041    /// The items this source has created, for completing a board read that is behind.
7042    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
7043        self.created.lock().map_err(|_| SourceError::Unavailable {
7044            message: "this source's record of what it created in this run was left \
7045                      inconsistent by an earlier failure; next: run the command again"
7046                .into(),
7047        })
7048    }
7049
7050    /// One board item as this source reports it, or `None` for content it ignores.
7051    ///
7052    /// A pull request is neither a project nor a task — it is somebody's change, not a
7053    /// unit of plan — and an item whose content the token cannot see has nothing to
7054    /// report at all.
7055    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7056        let resolved = self.resolve_unremembered(item)?;
7057        if let Some(resolved) = &resolved {
7058            self.resolved_cache()?
7059                .insert(resolved.id.clone(), resolved.clone());
7060        }
7061        Ok(resolved)
7062    }
7063
7064    /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
7065    /// write's own response needs, which says nothing this process should answer a later read
7066    /// with.
7067    fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
7068        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
7069            message: "GitHub project item is missing content".into(),
7070        })?;
7071        if content.is_null() {
7072            return Ok(None);
7073        }
7074        let content_kind = match required_str(content, "__typename")? {
7075            "Issue" => ContentKind::Issue,
7076            "DraftIssue" => ContentKind::DraftIssue,
7077            _ => return Ok(None),
7078        };
7079        let field_values = item
7080            .get("fieldValues")
7081            .ok_or_else(|| SourceError::Malformed {
7082                message: "GitHub project item is missing fieldValues".into(),
7083            })?;
7084        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
7085        let nodes = field_values
7086            .get("nodes")
7087            .and_then(Value::as_array)
7088            .ok_or_else(|| SourceError::Malformed {
7089                message: "GitHub project item fieldValues.nodes is not an array".into(),
7090            })?;
7091        if let Some(labels) = content.get("labels") {
7092            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
7093        }
7094        let raw_body = optional_str(content, "body")?.map(str::to_owned);
7095        let (body, slot) = metadata_body(raw_body.clone())?;
7096        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
7097            .map(|id| NativeId(id.to_owned()));
7098        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
7099        // to read one from; it is a task, and never a project.
7100        let sub_issues = match content_kind {
7101            ContentKind::Issue => sub_issue_total(content)?,
7102            ContentKind::DraftIssue => 0,
7103        };
7104        let content_id = required_str(content, "id")?;
7105        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7106            message: format!("GitHub issue {content_id}: {message}"),
7107        })?;
7108        let raw_title = required_str(content, "title")?;
7109        let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
7110        // The title a person wrote, which for a document is the one without the prefix —
7111        // the same way `content` above is the body without this source's metadata slot.
7112        let title = match kind {
7113            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
7114            BoardKind::Work(_) => raw_title.to_owned(),
7115        };
7116        let own_repository = content
7117            .pointer("/repository/nameWithOwner")
7118            .and_then(Value::as_str)
7119            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
7120            .transpose()
7121            .map_err(|message| SourceError::Malformed { message })?;
7122        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
7123            Repository::from_metadata(&slot)
7124                .map_err(|message| SourceError::Malformed { message })?
7125        } else {
7126            own_repository.clone().into_iter().collect()
7127        };
7128        let classification = Classification::from_metadata(&slot)
7129            .map_err(|message| SourceError::Malformed { message })?;
7130        let id = NativeId(content_id.to_owned());
7131        // Read only for a task, because only a task has either list: a project or a
7132        // document holding one of these keys holds nothing this source reports, and the
7133        // keys are left out of its caller-visible metadata all the same.
7134        let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
7135            let listed = |key: &str| {
7136                TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
7137                    .map_err(|message| SourceError::Malformed { message })
7138            };
7139            (
7140                listed(TaskRef::DELIVERS_KEY)?,
7141                listed(TaskRef::DELIVERED_BY_KEY)?,
7142            )
7143        } else {
7144            (Vec::new(), Vec::new())
7145        };
7146        let (option, closed, reason) = Self::status_parts(nodes, content)?;
7147        let priority = self.held_priority(nodes)?;
7148        let item_labels = labels(content)?;
7149        // Present when the item was reached through its own issue, whose board entry
7150        // names the board; a read of the board's own items has the board already. An
7151        // empty id names nothing a field write could address, so it is read as absent and
7152        // the write goes back to reading the board.
7153        let board_id = item
7154            .pointer("/project/id")
7155            .and_then(Value::as_str)
7156            .filter(|id| !id.is_empty());
7157        let resolved = Resolved {
7158            item_id: required_str(item, "id")?.to_owned(),
7159            id,
7160            content_kind,
7161            kind,
7162            title,
7163            body: body.filter(|value| !value.is_empty()),
7164            raw_body,
7165            status: self
7166                .statuses
7167                .status(kind.status_kind(), option, closed, reason),
7168            option: option.map(str::to_owned),
7169            priority,
7170            closed,
7171            reason: closed.then(|| reason.map(str::to_owned)).flatten(),
7172            delivers,
7173            delivered_by,
7174            labels: item_labels.clone(),
7175            parent,
7176            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
7177            projected: self.held_projections(nodes)?,
7178            number: match content_kind {
7179                ContentKind::Issue => Some(issue_number(content)?),
7180                // A draft is filed in no repository, so nothing ever numbered it:
7181                // `DraftIssue` declares no `number` at all, exactly as it declares no
7182                // `subIssuesSummary` the branch above reads.
7183                ContentKind::DraftIssue => None,
7184            },
7185            url: optional_str(content, "url")?.map(str::to_owned),
7186            created_at: optional_time(content, "createdAt")?,
7187            updated_at: optional_time(content, "updatedAt")?,
7188            item_updated_at: optional_time(item, "updatedAt")?,
7189            comment_count: content
7190                .pointer("/commentCount/totalCount")
7191                .and_then(Value::as_u64),
7192            relationship_events: content
7193                .pointer("/relationshipEvents/totalCount")
7194                .and_then(Value::as_u64),
7195            // The issue node is stamped wherever it was read; a board item a read of the issue
7196            // put together from its memberships is not.
7197            served_at: match optional_time(content, SERVED_AT)? {
7198                Some(served) => Some(served),
7199                None => optional_time(item, SERVED_AT)?,
7200            },
7201            // Both connections were read to their end above — one reporting a further page is
7202            // refused — so these are every field value and every label the item holds. A
7203            // response that did not select the labels at all says nothing of them, and gives
7204            // no facets.
7205            facets: content.get("labels").is_some().then(|| {
7206                change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
7207            }),
7208            own_repository,
7209            repositories,
7210            classification,
7211            slot,
7212            board_id: board_id.map(str::to_owned),
7213            fields: field_definitions(nodes),
7214            board_fields: Self::carried_board_fields(content, board_id)?,
7215            blocked_by: carried_blocked_by(content)?,
7216        };
7217        Ok(Some(resolved))
7218    }
7219
7220    /// The field definitions of the board `board_id` names — the project this issue's own
7221    /// board item is on — off the `boards` page a read of an issue by its own id carries, or
7222    /// `None` when the read carried none, carried no entry for that board, or the board item
7223    /// named no board, which a write then answers by reading the board's fields itself.
7224    ///
7225    /// Matched by the board's node id and never by its number alone: a project number is
7226    /// unique only within its owner, so another owner's board numbered alike can sit on the
7227    /// same page, and its field and option ids address nothing on this one.
7228    fn carried_board_fields(
7229        content: &Value,
7230        board_id: Option<&str>,
7231    ) -> Result<Option<Value>, SourceError> {
7232        let (Some(nodes), Some(board_id)) = (
7233            content.pointer("/boards/nodes").and_then(Value::as_array),
7234            board_id,
7235        ) else {
7236            return Ok(None);
7237        };
7238        let Some(board) = nodes.iter().find_map(|node| {
7239            let project = node.get("project")?;
7240            (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
7241        }) else {
7242            return Ok(None);
7243        };
7244        let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
7245            return Ok(None);
7246        };
7247        complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
7248        Ok(Some(fields.clone()))
7249    }
7250
7251    /// What one board item's `Priority` field says, through this instance's mapping.
7252    ///
7253    /// An instance with no mapping holds no priority, so every item reads as `none` whatever
7254    /// its board holds. With one, no value is `none`, a mapped option is its level, and an
7255    /// option the mapping does not name is kept as itself — never read as a level or as
7256    /// `none` — for a read of the task to report by name.
7257    fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
7258        let Some(mapping) = &self.priorities else {
7259            return Ok(HeldPriority::Read(Priority::None));
7260        };
7261        // A value of the field that names no option — a text field someone called `Priority` —
7262        // is malformed rather than `none`: reading it as no priority would let the next copy
7263        // clear one a person set.
7264        let Some(option) = field_values
7265            .iter()
7266            .find(|value| {
7267                value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
7268            })
7269            .map(|value| required_str(value, "name"))
7270            .transpose()?
7271        else {
7272            return Ok(HeldPriority::Read(Priority::None));
7273        };
7274        Ok(mapping.priority_of(option).map_or_else(
7275            || HeldPriority::Unmapped(option.to_owned()),
7276            HeldPriority::Read,
7277        ))
7278    }
7279
7280    /// What each projected metadata field holds for one board item, by field name — every text
7281    /// value GitHub answers, an empty one included.
7282    fn held_projections(
7283        &self,
7284        field_values: &[Value],
7285    ) -> Result<BTreeMap<String, String>, SourceError> {
7286        let mut held = BTreeMap::new();
7287        for projection in &self.metadata_fields {
7288            if let Some(text) = text_field(field_values, &projection.field)? {
7289                held.insert(projection.field.to_string(), text);
7290            }
7291        }
7292        Ok(held)
7293    }
7294
7295    /// What one board item's status is read from: its `Status` option, whether its issue
7296    /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
7297    /// three into the status it reports.
7298    fn status_parts<'a>(
7299        field_values: &'a [Value],
7300        content: &'a Value,
7301    ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
7302        let option = field_values
7303            .iter()
7304            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
7305            .map(|value| required_str(value, "name"))
7306            .transpose()?;
7307        let closed = optional_str(content, "state")? == Some("CLOSED");
7308        Ok((option, closed, optional_str(content, "stateReason")?))
7309    }
7310
7311    /// The board Status option this write selects, or the refusal that says why not.
7312    ///
7313    /// The mapped option is required for both open and terminal targets. A terminal write
7314    /// validates it before changing either representation, so it can never fall back to
7315    /// closing an issue whose board cannot display the matching status.
7316    ///
7317    /// Answers the field's id, the option's id, and the option's name as the board spells
7318    /// it — which is the name a read of the item reports once it sits there.
7319    fn column_for(
7320        &self,
7321        fields: &Value,
7322        kind: ItemKind,
7323        category: StatusCategory,
7324        target: &StatusTarget,
7325    ) -> Result<Option<(String, String, String)>, SourceError> {
7326        let Some(wanted) = target.option() else {
7327            return Ok(None);
7328        };
7329        let missing = |detail: &str| SourceError::Refused {
7330            message: format!(
7331                "{} status {} of source {} needs the board Status option {wanted:?}, and \
7332                 {detail}; next: add that option to the board, which `onetaskgraph sources \
7333                 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
7334                 it has",
7335                kind.marker(),
7336                category_name(category),
7337                self.name,
7338                self.name,
7339                category_name(category),
7340                kind.marker()
7341            ),
7342        };
7343        let Some(field) = Board::field(fields, "Status")? else {
7344            return Err(missing("this board has no Status field"));
7345        };
7346        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7347            return Err(missing(
7348                "this board's Status field is not a single-select field",
7349            ));
7350        }
7351        let option = field
7352            .get("options")
7353            .and_then(Value::as_array)
7354            .and_then(|options| {
7355                options.iter().find(|option| {
7356                    option
7357                        .get("name")
7358                        .and_then(Value::as_str)
7359                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7360                })
7361            });
7362        match option {
7363            None => Err(missing("this board does not have it")),
7364            Some(option) => Ok(Some((
7365                required_str(field, "id")?.to_owned(),
7366                required_str(option, "id")?.to_owned(),
7367                required_str(option, "name")?.to_owned(),
7368            ))),
7369        }
7370    }
7371
7372    /// The refusal a status that closes an issue is answered with over a board draft.
7373    fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
7374        SourceError::Refused {
7375            message: format!(
7376                "status {} of source {} closes the item's issue, and GitHub draft items have \
7377                 no open or closed state",
7378                category_name(category),
7379                self.name
7380            ),
7381        }
7382    }
7383
7384    /// What a status write to one item needs of the board: the board's id and the
7385    /// definition of its `Status` field, read off the item when the item says both.
7386    ///
7387    /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
7388    /// and its `Status` value carries that field's definition, options and all. An item that
7389    /// does not say — no board id, or no `Status` value to read the field off — takes them
7390    /// from [`Self::board_fields`], which reads no item.
7391    async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7392        if let Some(board) = item.carried_board() {
7393            return Ok(board);
7394        }
7395        if item.defines("Status")
7396            && let Some(board_id) = item.named_board()
7397        {
7398            return Ok(BoardFields {
7399                id: board_id,
7400                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7401            });
7402        }
7403        self.board_fields().await
7404    }
7405
7406    /// Set one task's or one project's status and nothing else; see
7407    /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
7408    async fn set_status(
7409        &self,
7410        id: &NativeId,
7411        kind: ItemKind,
7412        category: StatusCategory,
7413    ) -> Result<Option<Status>, SourceError> {
7414        // Refused before anything is read, in the words a write of the same status is.
7415        let target = self.resolved_target(kind, category)?;
7416        // An item read by issue reference carries only what this write needs, so what it
7417        // becomes is never remembered for a later read to be answered from.
7418        let held = self.take_held(id)?;
7419        let by_reference = held.is_some();
7420        let bound = match held {
7421            Some(item) => Some(item),
7422            None => self.bound_item(id).await?,
7423        };
7424        let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
7425            return Ok(None);
7426        };
7427        let board = self.status_board(&item).await?;
7428        let (field, option, name) = self
7429            .column_for(&board.fields, kind, category, &target)?
7430            .ok_or_else(|| SourceError::Malformed {
7431                message: format!(
7432                    "status {} of source {} names no board Status option",
7433                    category_name(category),
7434                    self.name
7435                ),
7436            })?;
7437        if item.status.category == category && item.option.as_deref() == Some(&name) {
7438            return Ok(Some(item.status));
7439        }
7440        match &target {
7441            StatusTarget::Terminal(_, reason) => {
7442                if item.content_kind == ContentKind::DraftIssue {
7443                    return Err(self.closes_a_draft(category));
7444                }
7445                // The option first, so a refused selection leaves an open issue open rather
7446                // than closed behind a card that still reads open.
7447                self.set_item_field(
7448                    board.id.as_str(),
7449                    &item.item_id,
7450                    &field,
7451                    json!({"singleSelectOptionId": option}),
7452                )
7453                .await?;
7454                // An issue already closed in a way that reads as this category keeps its state
7455                // and its reason: only its card was out of line, and closing it again would
7456                // overwrite a reason — a duplicate's — that says more than this write does.
7457                let already = item.closed && item.status.category == category;
7458                if !already {
7459                    self.update_content(
7460                        ContentKind::Issue,
7461                        &item.id,
7462                        json!({"stateInput": state_input(Some(&target))}),
7463                    )
7464                    .await?;
7465                }
7466                let reason = if already {
7467                    item.reason.clone()
7468                } else {
7469                    Some(reason.reason().to_owned())
7470                };
7471                item.closed = true;
7472                item.status = self
7473                    .statuses
7474                    .status(kind, Some(&name), true, reason.as_deref());
7475                item.option = Some(name);
7476                item.reason = reason;
7477            }
7478            StatusTarget::Column(_) => {
7479                // An option is what an open item's status is, so a closed issue is reopened
7480                // first — sitting closed in the column, it would read back as closed. A draft has
7481                // no state to reopen.
7482                if item.content_kind == ContentKind::Issue && item.closed {
7483                    self.update_content(
7484                        ContentKind::Issue,
7485                        &item.id,
7486                        json!({"stateInput": state_input(Some(&target))}),
7487                    )
7488                    .await?;
7489                    item.closed = false;
7490                }
7491                self.set_item_field(
7492                    board.id.as_str(),
7493                    &item.item_id,
7494                    &field,
7495                    json!({"singleSelectOptionId": option}),
7496                )
7497                .await?;
7498                item.status = self.statuses.status(kind, Some(&name), false, None);
7499                item.option = Some(name);
7500            }
7501            StatusTarget::Disabled(_) => {
7502                unreachable!("resolved_target refused a disabled status")
7503            }
7504        }
7505        let status = item.status.clone();
7506        if !by_reference {
7507            self.remember_written(item, false)?;
7508        }
7509        Ok(Some(status))
7510    }
7511
7512    /// Replace one task's `delivered_by` and nothing else; see
7513    /// [`TaskSource::set_delivered_by`].
7514    ///
7515    /// One update of the body, which differs from the body GitHub holds only inside the
7516    /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
7517    async fn replace_delivered_by(
7518        &self,
7519        id: &NativeId,
7520        delivered_by: &[TaskRef],
7521    ) -> Result<Option<()>, SourceError> {
7522        let entries = TaskRef::listed(
7523            TaskRef::DELIVERED_BY_KEY,
7524            id,
7525            Some(&self.name),
7526            delivered_by.to_vec(),
7527        )
7528        .map_err(|message| SourceError::Refused { message })?;
7529        let Some(mut item) = self
7530            .bound_item(id)
7531            .await?
7532            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7533        else {
7534            return Ok(None);
7535        };
7536        let mut slot = item.slot.clone();
7537        set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
7538        self.write_slot(&mut item, &slot).await?;
7539        item.delivered_by = entries;
7540        self.remember_written(item, false)?;
7541        Ok(Some(()))
7542    }
7543
7544    /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
7545    /// see [`TaskSource::set_task_metadata`].
7546    ///
7547    /// `None` when this board holds no item by that id, or holds one of another kind. The
7548    /// answer is the item as this source now reads it, so what a caller is told the key
7549    /// holds is what the slot holds.
7550    ///
7551    /// A key already holding the value is answered without a write, compared as JSON rather
7552    /// than as the body's bytes: a slot a person spelled with other whitespace would
7553    /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
7554    async fn set_slot_key(
7555        &self,
7556        id: &NativeId,
7557        kind: BoardKind,
7558        key: &MetadataKey,
7559        value: &Value,
7560    ) -> Result<Option<Resolved>, SourceError> {
7561        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7562            return Ok(None);
7563        };
7564        if item.slot.get(key.as_str()) == Some(value) {
7565            return Ok(Some(item));
7566        }
7567        let mut slot = item.slot.clone();
7568        slot.insert(key.as_str().to_owned(), value.clone());
7569        // A key a board text field projects moves that field first, so the body — the value's
7570        // one authoritative home — is written last, as every write of an existing item is.
7571        if self
7572            .metadata_fields
7573            .iter()
7574            .any(|projection| &*projection.key == key.as_str())
7575        {
7576            let board = self.projection_board(&item).await?;
7577            let (writes, projected) = self.projection_writes(
7578                &board.fields,
7579                Some(&item.projected),
7580                &slot,
7581                Some(&[key.as_str()]),
7582            )?;
7583            let (mut values, mut clears) = (Vec::new(), Vec::new());
7584            for write in &writes {
7585                write.join(&mut values, &mut clears);
7586            }
7587            self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
7588                .await?;
7589            item.projected = projected;
7590        }
7591        self.write_slot(&mut item, &slot).await?;
7592        self.remember_written(item.clone(), false)?;
7593        Ok(Some(item))
7594    }
7595
7596    /// What a projection write to `item` needs of the board, read off the item when it says
7597    /// enough, as [`Self::fields_for`] reads it.
7598    async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7599        if let Some(board) = item.carried_board() {
7600            return Ok(board);
7601        }
7602        if let Some(board_id) = item.named_board()
7603            && self
7604                .metadata_fields
7605                .iter()
7606                .all(|projection| item.defines(&projection.field))
7607        {
7608            return Ok(BoardFields {
7609                id: board_id,
7610                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7611            });
7612        }
7613        self.board_fields().await
7614    }
7615
7616    /// Put `slot` in one item's metadata slot with a single update of its body, and bring
7617    /// `item` up to what that write left.
7618    ///
7619    /// The body sent differs from the body GitHub holds only inside the slot — see
7620    /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
7621    /// the mutation the item's content takes, so a board draft's body is written with
7622    /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
7623    async fn write_slot(
7624        &self,
7625        item: &mut Resolved,
7626        slot: &BTreeMap<String, Value>,
7627    ) -> Result<(), SourceError> {
7628        let held = item.raw_body.clone().unwrap_or_default();
7629        let body = with_slot(&held, slot)?;
7630        if body != held {
7631            self.update_content(item.content_kind, &item.id, json!({"body": body}))
7632                .await?;
7633        }
7634        let (visible, slot) = metadata_body(Some(body.clone()))?;
7635        item.body = visible.filter(|value| !value.is_empty());
7636        item.raw_body = Some(body);
7637        item.slot = slot;
7638        Ok(())
7639    }
7640
7641    /// This instance's target for a category written to an item of `kind`, refusing one
7642    /// that kind has no option for — before anything is read or written.
7643    ///
7644    /// Nothing here mutates the board's option set to make room for a status. GitHub
7645    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
7646    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
7647    /// field and every item's status.
7648    fn resolved_target(
7649        &self,
7650        kind: ItemKind,
7651        category: StatusCategory,
7652    ) -> Result<StatusTarget, SourceError> {
7653        let target = self.statuses.target(kind, category).clone();
7654        let StatusTarget::Disabled(why) = target else {
7655            return Ok(target);
7656        };
7657        let refusal = why.refusal(&self.name, category, kind);
7658        // Why there is no shipped default, which is the question a person meeting this
7659        // refusal on a source that never mentioned the category asks.
7660        let shipped_none = match category {
7661            StatusCategory::Draft => Some(
7662                "draft has no shipped default because GitHub draft issues cannot have \
7663                 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
7664            ),
7665            StatusCategory::Unknown => Some(
7666                "unknown has no shipped default because this board keeps no open-ended status \
7667                 word: every word classified unknown is written to the one board Status option \
7668                 status_mapping.unknown names",
7669            ),
7670            _ => None,
7671        };
7672        Err(match (refusal, shipped_none, why) {
7673            (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
7674                SourceError::Refused {
7675                    message: format!("{message}; {note}"),
7676                }
7677            }
7678            (refusal, _, _) => refusal,
7679        })
7680    }
7681
7682    /// What writing `priority` does to one item's `Priority` field on this board, or the
7683    /// refusal naming what the board lacks.
7684    ///
7685    /// `none` is no value, so it clears the field — and asks nothing of an item that holds
7686    /// none already, or of an item not created yet. Every other priority selects the option
7687    /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
7688    /// without that option, is refused rather than given one: reads and writes never create
7689    /// a field or an option.
7690    fn priority_write(
7691        &self,
7692        fields: &Value,
7693        existing: Option<&Resolved>,
7694        priority: Priority,
7695    ) -> Result<Option<PriorityWrite>, SourceError> {
7696        let Some(mapping) = &self.priorities else {
7697            return Err(self.holds_no_priority());
7698        };
7699        let Some(wanted) = mapping.option(priority) else {
7700            if !existing.is_some_and(Resolved::holds_priority) {
7701                return Ok(None);
7702            }
7703            let field =
7704                Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
7705                    message: format!(
7706                        "an item holding a {PRIORITY_FIELD} value was read without that field"
7707                    ),
7708                })?;
7709            return Ok(Some(PriorityWrite::Clear {
7710                field: required_str(field, "id")?.to_owned(),
7711            }));
7712        };
7713        let missing = |detail: &str| SourceError::Refused {
7714            message: format!(
7715                "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
7716                 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
7717                 it, or point priority_mapping.{priority} of this source at an option the board \
7718                 has",
7719                self.name, self.name
7720            ),
7721        };
7722        let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
7723            return Err(missing(&format!(
7724                "this board has no {PRIORITY_FIELD} field"
7725            )));
7726        };
7727        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7728            return Err(missing(&format!(
7729                "this board's {PRIORITY_FIELD} field is not a single-select field"
7730            )));
7731        }
7732        // An options list that is absent or not a list is an answer this source cannot read,
7733        // not a board lacking the option: `sources fields --apply` is no remedy for it.
7734        let option = field
7735            .get("options")
7736            .and_then(Value::as_array)
7737            .ok_or_else(|| SourceError::Malformed {
7738                message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
7739            })?
7740            .iter()
7741            .find(|option| {
7742                option
7743                    .get("name")
7744                    .and_then(Value::as_str)
7745                    .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7746            })
7747            .ok_or_else(|| missing("this board does not have it"))?;
7748        Ok(Some(PriorityWrite::Select {
7749            field: required_str(field, "id")?.to_owned(),
7750            option: required_str(option, "id")?.to_owned(),
7751        }))
7752    }
7753
7754    /// What writing `metadata` does to each projected text field of one item, and what those
7755    /// fields hold after it — or the refusal, before anything is sent.
7756    ///
7757    /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
7758    /// narrows the work to the entries projecting one of those metadata keys, for a write that
7759    /// touches only them; `None` is every entry. A string the field already holds, and nothing
7760    /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
7761    /// and so is a board without the field, or with a field of that name that is not a text
7762    /// field: reads and writes never create a field.
7763    fn projection_writes(
7764        &self,
7765        fields: &Value,
7766        held: Option<&BTreeMap<String, String>>,
7767        metadata: &BTreeMap<String, Value>,
7768        keys: Option<&[&str]>,
7769    ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
7770        let mut projected = held.cloned().unwrap_or_default();
7771        let mut writes = Vec::new();
7772        for projection in self
7773            .metadata_fields
7774            .iter()
7775            .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
7776        {
7777            let wanted = projection.projected(metadata, &self.name)?;
7778            let field = self.projection_field(fields, projection)?;
7779            let holding = projected.get(&*projection.field);
7780            match wanted {
7781                Projected::Text(text) if holding != Some(&text) => {
7782                    writes.push(ProjectionWrite::Set {
7783                        field,
7784                        text: text.clone(),
7785                    });
7786                    projected.insert(projection.field.to_string(), text);
7787                }
7788                Projected::Nothing if holding.is_some() => {
7789                    writes.push(ProjectionWrite::Clear { field });
7790                    projected.remove(&*projection.field);
7791                }
7792                Projected::Text(_) | Projected::Nothing => {}
7793            }
7794        }
7795        Ok((writes, projected))
7796    }
7797
7798    /// The id of the board text field one projection writes, or the refusal naming what the
7799    /// board has instead.
7800    fn projection_field(
7801        &self,
7802        fields: &Value,
7803        projection: &MetadataField,
7804    ) -> Result<String, SourceError> {
7805        let refuse = |detail: String| SourceError::Refused {
7806            message: format!(
7807                "source {} projects metadata key {:?} onto the board text field {:?}, and \
7808                 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
7809                 text field, or name another field in metadata_fields",
7810                self.name, &*projection.key, &*projection.field, self.name
7811            ),
7812        };
7813        let Some(field) = Board::field(fields, &projection.field)? else {
7814            return Err(refuse("this board has no field of that name".to_owned()));
7815        };
7816        let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
7817        let data_type = optional_str(field, "dataType")?;
7818        if typename != "ProjectV2Field" || data_type != Some("TEXT") {
7819            return Err(refuse(format!(
7820                "this board's field of that name is not a text field (it is a {}), so it is \
7821                 left as it is; rename or remove that field first",
7822                data_type.unwrap_or(typename)
7823            )));
7824        }
7825        Ok(required_str(field, "id")?.to_owned())
7826    }
7827
7828    /// Apply one priority write to one board item.
7829    async fn write_priority(
7830        &self,
7831        board_id: &str,
7832        item_id: &str,
7833        write: &PriorityWrite,
7834    ) -> Result<(), SourceError> {
7835        match write {
7836            PriorityWrite::Select { field, option } => {
7837                self.set_item_field(
7838                    board_id,
7839                    item_id,
7840                    field,
7841                    json!({"singleSelectOptionId": option}),
7842                )
7843                .await
7844            }
7845            PriorityWrite::Clear { field } => {
7846                let data = self
7847                    .graphql(
7848                        graphql::CLEAR_FIELD,
7849                        json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
7850                            "readPriority":false,"priorityName":PRIORITY_FIELD}),
7851                    )
7852                    .await?;
7853                let returned = data
7854                    .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
7855                    .ok_or_else(|| SourceError::Malformed {
7856                        message: "GitHub field clear returned no project item".into(),
7857                    })?;
7858                if required_str(returned, "id")? != item_id {
7859                    return Err(SourceError::Malformed {
7860                        message: "GitHub field clear returned the wrong project item".into(),
7861                    });
7862                }
7863                Ok(())
7864            }
7865        }
7866    }
7867
7868    /// The refusal a priority is answered with by an instance configured with no
7869    /// `priority_mapping`, which holds none.
7870    fn holds_no_priority(&self) -> SourceError {
7871        SourceError::Refused {
7872            message: format!(
7873                "source {} holds no task priority: its configuration sets no priority_mapping; \
7874                 next: set priority_mapping on this source, then run `onetaskgraph sources \
7875                 fields {} --apply` to set its board up",
7876                self.name, self.name
7877            ),
7878        }
7879    }
7880
7881    /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
7882    ///
7883    /// One field write — a select, or a clear for `none` — and no title, body, label, state
7884    /// or `Status` request. Clearing a priority an item does not hold sends nothing.
7885    async fn set_priority(
7886        &self,
7887        id: &NativeId,
7888        priority: Priority,
7889    ) -> Result<Option<Priority>, SourceError> {
7890        if self.priorities.is_none() {
7891            return Err(self.holds_no_priority());
7892        }
7893        let Some(mut item) = self
7894            .bound_item(id)
7895            .await?
7896            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7897        else {
7898            return Ok(None);
7899        };
7900        if priority == Priority::None && !item.holds_priority() {
7901            return Ok(Some(priority));
7902        }
7903        // The item's own read carries the field's definition whenever it holds a value of
7904        // it, which a clear always does; a select onto an item holding none reads the board.
7905        let board = match (item.carried_board(), item.named_board()) {
7906            (Some(board), _) => board,
7907            (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
7908                id,
7909                fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
7910            },
7911            _ => self.board_fields().await?,
7912        };
7913        let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
7914            return Ok(Some(priority));
7915        };
7916        let (document, root, input) = match write {
7917            PriorityWrite::Select { field, option } => (
7918                graphql::UPDATE_FIELD,
7919                "updateProjectV2ItemFieldValue",
7920                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
7921            ),
7922            PriorityWrite::Clear { field } => (
7923                graphql::CLEAR_FIELD,
7924                "clearProjectV2ItemFieldValue",
7925                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
7926            ),
7927        };
7928        let data = self
7929            .graphql(
7930                document,
7931                json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
7932            )
7933            .await?;
7934        let returned = data
7935            .get(root)
7936            .and_then(|value| value.get("projectV2Item"))
7937            .ok_or_else(|| SourceError::Malformed {
7938                message: "GitHub priority write returned no project item".into(),
7939            })?;
7940        if required_str(returned, "id")? != item.item_id {
7941            return Err(SourceError::Malformed {
7942                message: "GitHub priority write returned the wrong project item".into(),
7943            });
7944        }
7945        let value = returned
7946            .get("fieldValueByName")
7947            .ok_or_else(|| SourceError::Malformed {
7948                message: "GitHub priority write returned no priority read-back".into(),
7949            })?;
7950        if !value.is_null()
7951            && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
7952        {
7953            return Err(SourceError::Malformed {
7954                message: "GitHub priority read-back is not a Priority field value".into(),
7955            });
7956        }
7957        let values = if value.is_null() {
7958            Vec::new()
7959        } else {
7960            vec![value.clone()]
7961        };
7962        item.priority = self.held_priority(&values)?;
7963        let answer = item.task()?.priority;
7964        self.remember_written(item, false)?;
7965        Ok(Some(answer))
7966    }
7967
7968    /// Replace one task's visible body and nothing else; see
7969    /// [`TaskSource::set_task_content`].
7970    ///
7971    /// One update of the body, which differs from the body GitHub holds only outside the
7972    /// metadata slot — the slot is kept byte for byte, so every caller key and every list
7973    /// this source keeps there reads back as it was. A body that would not change is not
7974    /// sent at all.
7975    async fn replace_content(
7976        &self,
7977        id: &NativeId,
7978        content: &str,
7979    ) -> Result<Option<()>, SourceError> {
7980        let Some(mut item) = self
7981            .bound_item(id)
7982            .await?
7983            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7984        else {
7985            return Ok(None);
7986        };
7987        let held = item.raw_body.clone().unwrap_or_default();
7988        let body = with_content(&held, content)?;
7989        // Checked before anything is sent: content ending in what this source reads as its own
7990        // metadata slot would read back as metadata rather than as the content it was.
7991        let (visible, slot) = metadata_body(Some(body.clone()))?;
7992        if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
7993            return Err(SourceError::Refused {
7994                message: format!(
7995                    "this content ends in what source {} reads as its own metadata slot \
7996                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7997                     as content; next: remove that trailing block from the content",
7998                    self.name
7999                ),
8000            });
8001        }
8002        if body != held {
8003            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8004                .await?;
8005        }
8006        item.body = visible.filter(|value| !value.is_empty());
8007        item.raw_body = Some(body);
8008        item.slot = slot;
8009        self.remember_written(item, false)?;
8010        Ok(Some(()))
8011    }
8012
8013    /// Apply one targeted update to one task; see [`TaskSource::update_task`].
8014    ///
8015    /// One read of the item — which carries the board's field definitions and the issue's
8016    /// `blockedBy`, so neither is read again — and then only what differs from it: the
8017    /// `Status` option and the `Priority` field together in one request, the `blockedBy`
8018    /// additions and removals the named edges differ by, and last one `updateIssue` carrying
8019    /// the title, the body — visible content and metadata slot together — and a state change.
8020    /// So an update naming any of title, body, metadata, status and priority is one read and
8021    /// at most two writes. The body goes last so that a write refused part-way leaves it, and
8022    /// the metadata in it, as it stood. A terminal status selects its option and then closes,
8023    /// as a whole write does; an open one selects its option and then reopens. The origin
8024    /// field is never written: an update is of an item that already exists, whose origin is
8025    /// what it is.
8026    ///
8027    /// The task answered is the item as those writes left it, built from the read and what was
8028    /// sent rather than read again — the same record a later read in this run answers from.
8029    async fn targeted_update(
8030        &self,
8031        id: &NativeId,
8032        update: &TaskUpdate,
8033    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
8034        // Everything this source can refuse without reading the item is refused first, in the
8035        // words a whole write of the same fields is refused with.
8036        update.consistent()?;
8037        if update
8038            .title
8039            .as_deref()
8040            .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
8041        {
8042            return Err(SourceError::Refused {
8043                message: format!(
8044                    "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8045                     spells a document, so it would read back as one rather than as a task; \
8046                     retitle it",
8047                    self.name
8048                ),
8049            });
8050        }
8051        if let Some(delivers) = &update.delivers {
8052            TaskRef::listed(
8053                TaskRef::DELIVERS_KEY,
8054                id,
8055                Some(&self.name),
8056                delivers.clone(),
8057            )
8058            .map_err(|message| SourceError::Refused { message })?;
8059        }
8060        if self.priorities.is_none()
8061            && update
8062                .priority
8063                .is_some_and(|priority| priority != Priority::None)
8064        {
8065            return Err(self.holds_no_priority());
8066        }
8067        let target = update
8068            .status
8069            .as_ref()
8070            .map(|status| self.resolved_target(ItemKind::Task, status.category))
8071            .transpose()?;
8072        let Some(mut item) = self
8073            .bound_item(id)
8074            .await?
8075            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8076        else {
8077            return Ok(None);
8078        };
8079        let before = item.task()?;
8080
8081        let mut status_move = None;
8082        if let (Some(status), Some(target)) = (&update.status, target) {
8083            let board = self.status_board(&item).await?;
8084            let (field, option, name) = self
8085                .column_for(&board.fields, ItemKind::Task, status.category, &target)?
8086                .ok_or_else(|| SourceError::Malformed {
8087                    message: format!(
8088                        "status {} of source {} names no board Status option",
8089                        category_name(status.category),
8090                        self.name
8091                    ),
8092                })?;
8093            let terminal = matches!(target, StatusTarget::Terminal(_, _));
8094            if terminal && item.content_kind == ContentKind::DraftIssue {
8095                return Err(self.closes_a_draft(status.category));
8096            }
8097            let landed = match &target {
8098                StatusTarget::Terminal(_, reason) => {
8099                    self.statuses
8100                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
8101                }
8102                _ => self
8103                    .statuses
8104                    .status(ItemKind::Task, Some(&name), false, None),
8105            };
8106            let option_moves = item
8107                .option
8108                .as_deref()
8109                .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
8110            let state_moves = item.content_kind == ContentKind::Issue
8111                && (item.closed != terminal || (terminal && item.status != landed));
8112            if let Some(moves) = Moves::of(option_moves, state_moves) {
8113                status_move = Some(StatusMove {
8114                    board: board.id,
8115                    field,
8116                    option,
8117                    name,
8118                    target,
8119                    landed,
8120                    moves,
8121                });
8122            }
8123        }
8124
8125        let mut priority_move = None;
8126        if let Some(priority) = update.priority
8127            && self.priorities.is_some()
8128            && item.priority != HeldPriority::Read(priority)
8129        {
8130            let board = match (item.carried_board(), item.named_board()) {
8131                (Some(board), _) => board,
8132                (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
8133                    id: board,
8134                    fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8135                },
8136                _ => self.board_fields().await?,
8137            };
8138            if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
8139                priority_move = Some((board.id, write, priority));
8140            }
8141        }
8142
8143        // The metadata keys this update names that a board text field projects, and what
8144        // that does to those fields — refused, as a whole write refuses it, before anything
8145        // is sent.
8146        let touched: Vec<&str> = update
8147            .metadata_set
8148            .keys()
8149            .chain(update.metadata_remove.iter())
8150            .map(MetadataKey::as_str)
8151            .collect();
8152        let mut projection_move = None;
8153        if self
8154            .metadata_fields
8155            .iter()
8156            .any(|projection| touched.contains(&&*projection.key))
8157        {
8158            let mut metadata = item.slot.clone();
8159            for (key, value) in &update.metadata_set {
8160                metadata.insert(key.as_str().to_owned(), value.clone());
8161            }
8162            for key in &update.metadata_remove {
8163                metadata.remove(key.as_str());
8164            }
8165            let board = self.projection_board(&item).await?;
8166            let (writes, projected) = self.projection_writes(
8167                &board.fields,
8168                Some(&item.projected),
8169                &metadata,
8170                Some(&touched),
8171            )?;
8172            projection_move = Some((board.id, writes, projected));
8173        }
8174
8175        // Resolved before the body is composed, because a far end `blockedBy` cannot name is
8176        // recorded in the slot, and the slot travels in the one body update below.
8177        let edges = match &update.depends_on {
8178            Some(edges) => Some(
8179                self.partition_edges(
8180                    BoardKind::Work(ItemKind::Task),
8181                    item.content_kind,
8182                    item.blocked_by.as_deref(),
8183                    edges,
8184                )
8185                .await?,
8186            ),
8187            None => None,
8188        };
8189
8190        let mut slot = item.slot.clone();
8191        for (key, value) in &update.metadata_set {
8192            slot.insert(key.as_str().to_owned(), value.clone());
8193        }
8194        for key in &update.metadata_remove {
8195            slot.remove(key.as_str());
8196        }
8197        if let Some(delivers) = &update.delivers {
8198            set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
8199        }
8200        if let Some((_, recorded)) = &edges {
8201            record_edges(&mut slot, recorded);
8202        }
8203        let held = item.raw_body.clone().unwrap_or_default();
8204        let content = match &update.content {
8205            Some(content) => with_content(&held, content)?,
8206            None => held.clone(),
8207        };
8208        // A slot holding what it held is kept byte for byte, compared as JSON rather than as
8209        // the body's bytes, as a metadata write compares it: a slot a person spelled with
8210        // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
8211        let body = if slot == item.slot {
8212            content
8213        } else {
8214            with_slot(&content, &slot)?
8215        };
8216        // Checked before anything is sent, as a content write checks it: content ending in
8217        // what this source reads as its own slot would read back as metadata.
8218        let (visible, read) = metadata_body(Some(body.clone()))?;
8219        let wanted = update.content.as_deref().or(item.body.as_deref());
8220        if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
8221            return Err(SourceError::Refused {
8222                message: format!(
8223                    "this content ends in what source {} reads as its own metadata slot \
8224                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8225                     as content; next: remove that trailing block from the content",
8226                    self.name
8227                ),
8228            });
8229        }
8230        let recorded_moves =
8231            slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
8232
8233        // One `updateIssue` carries all three, because every mutation spends the secondary
8234        // limiter and the title, body and state are one mutation's inputs.
8235        let mut fields = serde_json::Map::new();
8236        if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
8237            fields.insert("title".to_owned(), json!(title));
8238        }
8239        if body != held {
8240            fields.insert("body".to_owned(), json!(body));
8241        }
8242        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
8243            fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
8244        }
8245        // **The body is written last, and that is the guarantee a refusal part-way keeps.**
8246        // GitHub runs no two requests as one, and runs one document's mutation fields in order
8247        // without undoing an earlier field when a later one fails — so a body written before a
8248        // board field the board then refused would be left changed. Written after every other
8249        // write has landed, a refusal anywhere leaves the item's body, and every metadata key
8250        // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
8251        // first, together in one request — a terminal option selected before the issue
8252        // closes, as a whole write does — then the `blockedBy` difference, then the body.
8253        let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
8254        let mut clears: Vec<(&BoardId, &str)> = Vec::new();
8255        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
8256            board_writes.push((
8257                &moving.board,
8258                (
8259                    moving.field.clone(),
8260                    json!({"singleSelectOptionId": moving.option}),
8261                ),
8262            ));
8263        }
8264        match &priority_move {
8265            Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
8266                board,
8267                (field.clone(), json!({"singleSelectOptionId": option})),
8268            )),
8269            Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
8270            None => {}
8271        }
8272        if let Some((board, writes, _)) = &projection_move {
8273            for write in writes {
8274                let (mut values, mut cleared) = (Vec::new(), Vec::new());
8275                write.join(&mut values, &mut cleared);
8276                board_writes.extend(values.into_iter().map(|value| (board, value)));
8277                clears.extend(cleared.into_iter().map(|field| (board, field)));
8278            }
8279        }
8280        let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
8281        boards.extend(clears.iter().map(|(board, _)| *board));
8282        boards.dedup_by(|one, other| one.as_str() == other.as_str());
8283        for board in boards {
8284            let writes = board_writes
8285                .iter()
8286                .filter(|(on, _)| on.as_str() == board.as_str())
8287                .map(|(_, write)| write.clone())
8288                .collect::<Vec<_>>();
8289            let cleared = clears
8290                .iter()
8291                .filter(|(on, _)| on.as_str() == board.as_str())
8292                .map(|(_, field)| *field)
8293                .collect::<Vec<_>>();
8294            self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
8295                .await?;
8296        }
8297        let mut blocked_by_moved = false;
8298        if let Some((native, _)) = &edges
8299            && item.content_kind == ContentKind::Issue
8300        {
8301            blocked_by_moved = self
8302                .reconcile_blocked_by(
8303                    &item.id,
8304                    native,
8305                    Issue::Existing(item.blocked_by.as_deref()),
8306                )
8307                .await?;
8308        }
8309        if !fields.is_empty() {
8310            self.update_content(item.content_kind, &item.id, Value::Object(fields))
8311                .await?;
8312        }
8313
8314        // The `blockedBy` this item was read with is what the write just changed, so it is no
8315        // answer any more: a later write in this command would reconcile against it — re-adding
8316        // what it kept and never removing what it dropped — and a forward-edge read would
8317        // answer with it. Left unknown, each asks GitHub instead.
8318        if blocked_by_moved {
8319            item.blocked_by = None;
8320        }
8321        if let Some(title) = &update.title {
8322            item.title.clone_from(title);
8323        }
8324        item.body = visible.filter(|value| !value.is_empty());
8325        item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
8326        item.slot = slot;
8327        if let Some(delivers) = &update.delivers {
8328            item.delivers.clone_from(delivers);
8329        }
8330        if let Some(moving) = status_move {
8331            item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
8332                && item.content_kind == ContentKind::Issue;
8333            item.status = moving.landed;
8334            item.option = Some(moving.name);
8335        }
8336        if let Some((_, _, priority)) = priority_move {
8337            item.priority = HeldPriority::Read(priority);
8338        }
8339        if let Some((_, _, projected)) = projection_move {
8340            item.projected = projected;
8341        }
8342        let task = item.task()?;
8343        let mut written = update.changed(&before, &task);
8344        if blocked_by_moved || recorded_moves {
8345            written.insert(UpdatedField::DependsOn);
8346        }
8347        self.remember_written(item, false)?;
8348        Ok(Some(TaskUpdateOutcome {
8349            task,
8350            written,
8351            delivers_before: before.delivers,
8352        }))
8353    }
8354
8355    /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
8356    /// together, and nothing else; see [`TaskSource::set_task_rendering`].
8357    ///
8358    /// One update of the body: the content outside the slot, and inside it that one entry,
8359    /// every other entry kept as it was. This source keeps no template answers — an issue has
8360    /// no room beside itself that is not its body, and answers written there would duplicate
8361    /// what the content already says and count against GitHub's body limit — so `answers`
8362    /// reaches nothing here. A body that would not change is not sent at all.
8363    async fn replace_rendering(
8364        &self,
8365        id: &NativeId,
8366        kind: BoardKind,
8367        content: &str,
8368        provenance: &Value,
8369        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
8370    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
8371        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8372            return Ok(None);
8373        };
8374        let held = item.raw_body.clone().unwrap_or_default();
8375        let mut slot = item.slot.clone();
8376        slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
8377        let rewritten;
8378        let content = if let Some(assets) = assets {
8379            let uploads = self
8380                .upload_assets(item.own_repository.as_ref(), assets)
8381                .await?;
8382            rewritten =
8383                onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
8384            rewritten.as_str()
8385        } else {
8386            content
8387        };
8388        let body = with_slot(&with_content(&held, content)?, &slot)?;
8389        // Checked before anything is sent, as a content write checks it.
8390        let (visible, read) = metadata_body(Some(body.clone()))?;
8391        if visible.as_deref().unwrap_or_default() != content || read != slot {
8392            return Err(SourceError::Refused {
8393                message: format!(
8394                    "this content ends in what source {} reads as its own metadata slot \
8395                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8396                     as content; next: remove that trailing block from the template",
8397                    self.name
8398                ),
8399            });
8400        }
8401        if body != held {
8402            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8403                .await?;
8404        }
8405        item.body = visible.filter(|value| !value.is_empty());
8406        item.raw_body = Some(body);
8407        item.slot = read;
8408        self.remember_written(item, false)?;
8409        Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
8410            id: id.clone(),
8411            content: Some(content.to_owned()),
8412        }))
8413    }
8414
8415    async fn set_item_field(
8416        &self,
8417        board_id: &str,
8418        item_id: &str,
8419        field_id: &str,
8420        value: Value,
8421    ) -> Result<(), SourceError> {
8422        let data = self
8423            .graphql(
8424                graphql::UPDATE_FIELD,
8425                json!({"input":{
8426                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
8427                },"readPriority":false,"priorityName":PRIORITY_FIELD}),
8428            )
8429            .await?;
8430        let returned = data
8431            .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
8432            .ok_or_else(|| SourceError::Malformed {
8433                message: "GitHub field update returned no project item".into(),
8434            })?;
8435        if required_str(returned, "id")? != item_id {
8436            return Err(SourceError::Malformed {
8437                message: "GitHub field update returned the wrong project item".into(),
8438            });
8439        }
8440        Ok(())
8441    }
8442
8443    /// GitHub accepts one value per field mutation; aliases combine those mutations in
8444    /// one request — writes in the order given, then clears. A lone write is
8445    /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
8446    /// request's slots hold go in further requests of the same document, in order. Every
8447    /// returned item id is checked, including optional aliases.
8448    async fn set_item_fields(
8449        &self,
8450        board: &str,
8451        item: &str,
8452        fields: &[(String, Value)],
8453        clears: &[&str],
8454    ) -> Result<(), SourceError> {
8455        match (fields, clears) {
8456            ([], []) => return Ok(()),
8457            ([(field, value)], []) => {
8458                return self.set_item_field(board, item, field, value.clone()).await;
8459            }
8460            ([], [field]) => {
8461                return self
8462                    .write_priority(
8463                        board,
8464                        item,
8465                        &PriorityWrite::Clear {
8466                            field: (*field).to_owned(),
8467                        },
8468                    )
8469                    .await;
8470            }
8471            _ => {}
8472        }
8473        // Any field of the request stands in for an input a slot leaves out, which GitHub
8474        // still requires to be well formed although it never runs it.
8475        let filler = fields
8476            .first()
8477            .map_or_else(|| clears[0], |(field, _)| field.as_str());
8478        let mut writes = fields.iter();
8479        let mut cleared = clears.iter();
8480        loop {
8481            let batch: Vec<&(String, Value)> =
8482                writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
8483            let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
8484            if batch.is_empty() && batch_clears.is_empty() {
8485                return Ok(());
8486            }
8487            let mut variables = serde_json::Map::new();
8488            for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
8489                let input = match batch.get(index) {
8490                    Some((field, value)) => {
8491                        json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
8492                    }
8493                    None => json!({"projectId":board,"itemId":item,"fieldId":filler,
8494                                   "value":{"text":""}}),
8495                };
8496                variables.insert(slot.variable.to_owned(), input);
8497                variables.insert(slot.include.to_owned(), json!(index < batch.len()));
8498            }
8499            for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
8500                let field = batch_clears.get(index).map_or(filler, |field| **field);
8501                variables.insert(
8502                    slot.variable.to_owned(),
8503                    json!({"projectId":board,"itemId":item,"fieldId":field}),
8504                );
8505                variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
8506            }
8507            let data = self
8508                .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
8509                .await?;
8510            let sent = FIELD_WRITE_SLOTS[..batch.len()]
8511                .iter()
8512                .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
8513            for slot in sent {
8514                let alias = slot.alias;
8515                let returned = data
8516                    .get(alias)
8517                    .and_then(|value| value.get("projectV2Item"))
8518                    .ok_or_else(|| SourceError::Malformed {
8519                        message: format!("GitHub field update {alias} returned no project item"),
8520                    })?;
8521                if required_str(returned, "id")? != item {
8522                    return Err(SourceError::Malformed {
8523                        message: format!(
8524                            "GitHub field update {alias} returned the wrong project item"
8525                        ),
8526                    });
8527                }
8528            }
8529        }
8530    }
8531
8532    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
8533        let mut after: Option<String> = None;
8534        let mut ids = Vec::new();
8535        loop {
8536            let data = self
8537                .graphql(
8538                    graphql::ISSUE_DEPENDENCIES,
8539                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
8540                )
8541                .await?;
8542            let connection =
8543                data.pointer("/node/blockedBy")
8544                    .ok_or_else(|| SourceError::Malformed {
8545                        message: "GitHub dependency response has no blockedBy connection".into(),
8546                    })?;
8547            ids.extend(
8548                connection
8549                    .get("nodes")
8550                    .and_then(Value::as_array)
8551                    .ok_or_else(|| SourceError::Malformed {
8552                        message: "GitHub dependency response nodes is not an array".into(),
8553                    })?
8554                    .iter()
8555                    .map(|value| required_str(value, "id").map(str::to_owned))
8556                    .collect::<Result<Vec<_>, _>>()?,
8557            );
8558            let next = next_cursor(connection)?;
8559            if let Some(next) = &next {
8560                validate_cursor_progress(after.as_deref(), &next.0)?;
8561            }
8562            after = next.map(|cursor| cursor.0);
8563            if after.is_none() {
8564                return Ok(ids);
8565            }
8566        }
8567    }
8568
8569    async fn dependencies(
8570        &self,
8571        id: &NativeId,
8572        near_kind: ItemKind,
8573        direction: Direction,
8574        page: &PageRequest,
8575    ) -> Result<Page<DependencyEdge>, SourceError> {
8576        validate_page(page)?;
8577        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
8578        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
8579        let recorded = recorded_offset(cursor, direction)?;
8580        // What this issue is blocked by, when a read of it by its own id in this command
8581        // already carried the whole connection — a copy reads the item it writes before it
8582        // reads its edges — and the page asked for is the whole of it, or the recorded tail
8583        // after it. Answered from that read, in the shape the dependency read answers in;
8584        // anything else is asked of GitHub.
8585        let carried = match direction {
8586            Direction::DependsOn => self
8587                .resolved_cache()?
8588                .get(id)
8589                .filter(|item| item.content_kind == ContentKind::Issue)
8590                .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
8591            Direction::DependedOnBy => None,
8592        }
8593        .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
8594        // Asked for even in the recorded phase, whose page reads nothing from the
8595        // connection: `__typename` is what says whether this item has a native
8596        // relationship at all, and that is what decides which far ends the reserved key is
8597        // allowed to hold.
8598        let data = match carried {
8599            Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
8600                "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
8601            None => {
8602                let after = if recorded.is_some() { None } else { cursor };
8603                let first = page.limit.min(MAX_PAGE_SIZE);
8604                match direction {
8605                    Direction::DependsOn => {
8606                        self.graphql(
8607                            graphql::ISSUE_DEPENDS_ON,
8608                            json!({"id":id.0,"first":first,"after":after,"owner":self.owner,
8609                                   "number":self.project_number,
8610                                   "boardItems":BOARD_ITEMS_PAGE_SIZE}),
8611                        )
8612                        .await?
8613                    }
8614                    Direction::DependedOnBy => {
8615                        self.graphql(
8616                            graphql::ISSUE_DEPENDED_ON_BY,
8617                            json!({"id":id.0,"first":first,"after":after}),
8618                        )
8619                        .await?
8620                    }
8621                }
8622            }
8623        };
8624        let draft_slot = draft_slot(&data, id, self.project_number)?;
8625        let node =
8626            data.get("node")
8627                .filter(|v| !v.is_null())
8628                .ok_or_else(|| SourceError::Refused {
8629                    message: format!(
8630                        "GitHub item {} was not found or does not support dependencies",
8631                        id.0
8632                    ),
8633                })?;
8634        let connection_name = match direction {
8635            Direction::DependsOn => "blockedBy",
8636            Direction::DependedOnBy => "blocking",
8637        };
8638        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
8639        // named natively and the reserved key may hold any far end. An issue's connections
8640        // hold issues, and this source reads them at the near item's own level.
8641        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
8642        if let Some(offset) = recorded {
8643            return Ok(recorded_page(
8644                self.recorded_edges(
8645                    id,
8646                    near_kind,
8647                    direction,
8648                    natively_names,
8649                    node,
8650                    draft_slot.as_ref(),
8651                )
8652                .await?,
8653                offset,
8654                limit,
8655            ));
8656        }
8657        if natively_names.is_none() {
8658            return Ok(recorded_page(
8659                self.recorded_edges(
8660                    id,
8661                    near_kind,
8662                    direction,
8663                    natively_names,
8664                    node,
8665                    draft_slot.as_ref(),
8666                )
8667                .await?,
8668                0,
8669                limit,
8670            ));
8671        }
8672        let connection = node
8673            .get(connection_name)
8674            .ok_or_else(|| SourceError::Malformed {
8675                message: "GitHub dependency response is missing its connection".into(),
8676            })?;
8677        let nodes = connection
8678            .get("nodes")
8679            .and_then(Value::as_array)
8680            .ok_or_else(|| SourceError::Malformed {
8681                message: "GitHub dependency response nodes is not an array".into(),
8682            })?;
8683        // `from` depends on `to`, always. GitHub spells the same relationship from either
8684        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
8685        // it — so the near item is `from` in one direction and `to` in the other.
8686        let items = nodes
8687            .iter()
8688            .map(|value| {
8689                let related = NativeId(required_str(value, "id")?.into());
8690                let related_kind = related_kind(value)?;
8691                let (from, to) = match direction {
8692                    Direction::DependsOn => (
8693                        DependencyEndpoint::from_native(id.clone(), near_kind),
8694                        DependencyEndpoint::from_native(related, related_kind),
8695                    ),
8696                    Direction::DependedOnBy => (
8697                        DependencyEndpoint::from_native(related, related_kind),
8698                        DependencyEndpoint::from_native(id.clone(), near_kind),
8699                    ),
8700                };
8701                Ok(DependencyEdge {
8702                    from,
8703                    to,
8704                    kind: DependencyKind::Blocks,
8705                })
8706            })
8707            .collect::<Result<Vec<_>, SourceError>>()?;
8708        let mut next = next_cursor(connection)?;
8709        if let Some(next) = &next {
8710            validate_cursor_progress(cursor, &next.0)?;
8711        }
8712        // The recorded far ends ride on the last page of `blockedBy`, whose answer carried the
8713        // body they are recorded in, as far as the page has room — so they cost no request of
8714        // their own — and only what does not fit is resumed by a cursor.
8715        let mut items = items;
8716        if next.is_none() {
8717            let recorded = self
8718                .recorded_edges(
8719                    id,
8720                    near_kind,
8721                    direction,
8722                    natively_names,
8723                    node,
8724                    draft_slot.as_ref(),
8725                )
8726                .await?;
8727            let room = limit.saturating_sub(items.len());
8728            let total = recorded.len();
8729            items.extend(recorded.into_iter().take(room));
8730            if total > room {
8731                next = Some(Cursor(format!("{RECORDED_CURSOR}{room}")));
8732            }
8733        }
8734        Ok(Page { items, next })
8735    }
8736
8737    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
8738    /// a far end in another source has to live: no GitHub issue relationship can name one.
8739    ///
8740    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
8741    /// source never writes one down.
8742    ///
8743    /// The metadata lives in the item's own body slot, and `node` is the dependency read's
8744    /// own answer, which carries an issue's body — so an issue's recorded edges cost no
8745    /// request beyond the read already made, and reading the board for them would be a
8746    /// walk of every item for one field of one. A draft has no body in that answer, because
8747    /// a draft is not an issue, so a draft's are read off its own read by id — never off a
8748    /// listing of the board, which can be behind on the very item asked about.
8749    async fn recorded_edges(
8750        &self,
8751        id: &NativeId,
8752        near_kind: ItemKind,
8753        direction: Direction,
8754        natively_names: Option<ItemKind>,
8755        node: &Value,
8756        draft_slot: Option<&Option<BTreeMap<String, Value>>>,
8757    ) -> Result<Vec<DependencyEdge>, SourceError> {
8758        if direction != Direction::DependsOn {
8759            return Ok(Vec::new());
8760        }
8761        let slot = match (node.get("body"), draft_slot) {
8762            (Some(body), _) if natively_names.is_some() => {
8763                metadata_body(body.as_str().map(str::to_owned))?.1
8764            }
8765            // A draft the dependency read itself answered: this board's, or none of its.
8766            (_, Some(Some(slot))) => slot.clone(),
8767            (_, Some(None)) => return Ok(Vec::new()),
8768            _ => {
8769                let Some(item) = self.bound_item(id).await? else {
8770                    return Ok(Vec::new());
8771                };
8772                item.slot
8773            }
8774        };
8775        DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
8776            .map_err(|message| SourceError::Malformed { message })
8777    }
8778
8779    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
8780        self.repository
8781            .as_ref()
8782            .ok_or_else(|| SourceError::Refused {
8783                message: format!(
8784                    "source {} has no repository configured, and a GitHub Projects board has no \
8785                 repository of its own to create an issue in; set repository: owner/name on \
8786                 this source",
8787                    self.name
8788                ),
8789            })
8790    }
8791
8792    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
8793    /// states.
8794    ///
8795    /// The fallback is demanded first, whichever arm answers: a write without a configured
8796    /// repository is refused naming the field exactly as it was before the rule existed,
8797    /// so a source that could not write before cannot write now, rather than writing for
8798    /// the one item whose own field happens to decide it.
8799    ///
8800    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
8801    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
8802    /// entry owned by someone other than the owner of the parent issue's repository —
8803    /// GitHub accepts a sub-issue from another repository of the same owner and from no
8804    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
8805    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
8806    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
8807    /// and is visible to the token is checked where its node id is resolved, still before
8808    /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
8809    /// looked up in a listing of the board, which can be minutes behind an issue its own
8810    /// `projectItems` already places on it — and that read answers first from this process's
8811    /// own record, so a project created moments ago in this command answers though GitHub
8812    /// has not caught up.
8813    async fn creation_target(
8814        &self,
8815        incoming: &Incoming<'_>,
8816    ) -> Result<RepositoryTarget, SourceError> {
8817        let fallback = self.configured_repository()?;
8818        let what = |incoming: &Incoming<'_>| {
8819            format!(
8820                "{} {:?}",
8821                incoming.written.kind().describes(),
8822                incoming.title
8823            )
8824        };
8825        let parent = match incoming.parent {
8826            Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
8827                SourceError::Refused {
8828                    message: format!(
8829                        "GitHub project issue {} was not found on the board of source {}, so {} \
8830                         cannot be filed under it",
8831                        parent.0,
8832                        self.name,
8833                        what(incoming)
8834                    ),
8835                }
8836            })?),
8837            None => None,
8838        };
8839        let parents_repository = parent
8840            .as_ref()
8841            .map(|parent| {
8842                // A draft is on the board and so is found, but it has no repository to
8843                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
8844                // would refuse the task only once `createIssue` had made it.
8845                if parent.content_kind == ContentKind::DraftIssue {
8846                    return Err(SourceError::Refused {
8847                        message: format!(
8848                            "GitHub project item {} on the board of source {} is a draft, \
8849                             which cannot have sub-issues, so {} cannot be filed under it",
8850                            parent.id.0,
8851                            self.name,
8852                            what(incoming)
8853                        ),
8854                    });
8855                }
8856                // An issue's repository is where a sub-issue is placed and whose owner it
8857                // is compared against, so a parent whose repository this source cannot
8858                // spell as `owner/name` — GitHub's login grammar is wider than this
8859                // source's floor — is one nothing can be filed under.
8860                parent
8861                    .own_repository
8862                    .as_ref()
8863                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
8864                    .ok_or_else(|| SourceError::Malformed {
8865                        message: format!(
8866                            "GitHub project issue {} on the board of source {} is in {}, which \
8867                             is not a {}/owner/name repository this source can place {} in",
8868                            parent.id.0,
8869                            self.name,
8870                            parent
8871                                .own_repository
8872                                .as_ref()
8873                                .map_or("no repository", Repository::as_str),
8874                            RepositoryTarget::HOST,
8875                            what(incoming)
8876                        ),
8877                    })
8878            })
8879            .transpose()?;
8880        match incoming.repositories {
8881            [named] => {
8882                let target =
8883                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
8884                        message: format!(
8885                            "{} names repository {}, which is not a {}/owner/name repository \
8886                             source {} can create an issue in; name one that is, or name none",
8887                            what(incoming),
8888                            named.as_str(),
8889                            RepositoryTarget::HOST,
8890                            self.name
8891                        ),
8892                    })?;
8893                if let Some(parents) = &parents_repository
8894                    && parents.owner != target.owner
8895                {
8896                    return Err(SourceError::Refused {
8897                        message: format!(
8898                            "{} names repository {}, owned by {}, but its project's issue is in \
8899                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
8900                             of the same owner as its parent issue; name a repository of {}, or \
8901                             name none",
8902                            what(incoming),
8903                            target.slug(),
8904                            target.owner,
8905                            parents.slug(),
8906                            parents.owner,
8907                            parents.owner
8908                        ),
8909                    });
8910                }
8911                Ok(target)
8912            }
8913            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
8914        }
8915    }
8916
8917    /// The node id of the repository `incoming` is being created in, or the refusal naming
8918    /// the item and the repository the token cannot see.
8919    ///
8920    /// Resolved once per command per repository; see [`Self::repository_cache`].
8921    async fn repository_id(
8922        &self,
8923        repository: &RepositoryTarget,
8924        incoming: &Incoming<'_>,
8925    ) -> Result<String, SourceError> {
8926        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
8927            return Ok(id);
8928        }
8929        let data = self
8930            .graphql(
8931                graphql::REPOSITORY,
8932                json!({"owner":repository.owner,"name":repository.name}),
8933            )
8934            .await?;
8935        self.repository_read(&data, repository, incoming)
8936    }
8937
8938    /// The repository's node id out of an answer carrying the `repository` root, held for
8939    /// the rest of this command, or the refusal naming the item that cannot be created in it.
8940    fn repository_read(
8941        &self,
8942        data: &Value,
8943        repository: &RepositoryTarget,
8944        incoming: &Incoming<'_>,
8945    ) -> Result<String, SourceError> {
8946        let node = data
8947            .get("repository")
8948            .filter(|value| !value.is_null())
8949            .ok_or_else(|| SourceError::Refused {
8950                message: format!(
8951                    "GitHub repository {} was not found or is not visible to the token, so {} \
8952                     {:?} cannot be created in it",
8953                    repository.slug(),
8954                    incoming.written.kind().describes(),
8955                    incoming.title
8956                ),
8957            })?;
8958        let id = required_str(node, "id")?.to_owned();
8959        self.repository_cache()?
8960            .insert(repository.clone(), id.clone());
8961        Ok(id)
8962    }
8963
8964    fn repository_cache(
8965        &self,
8966    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
8967        self.repository_cache
8968            .lock()
8969            .map_err(|_| SourceError::Unavailable {
8970                message: "this source's record of the destination repository was left \
8971                          inconsistent by an earlier failure; next: run the command again"
8972                    .into(),
8973            })
8974    }
8975
8976    /// Create or update one board item, whichever kind it is.
8977    async fn write_item(
8978        &self,
8979        incoming: &Incoming<'_>,
8980        target: Option<&NativeId>,
8981        depends_on: &[DependencyEdge],
8982    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
8983        let prepared = self.prepare_write(incoming, target, depends_on).await?;
8984        let landed = match &prepared.destination {
8985            // Its content is written last, by `complete_write`, once everything else has landed.
8986            WriteDestination::Existing(item) => Landed {
8987                content_id: item.id.clone(),
8988                item_id: Some(item.item_id.clone()),
8989                url: item.url.clone(),
8990                number: item.number,
8991            },
8992            WriteDestination::New(target) => {
8993                if self.deferring()? {
8994                    self.create_filed_issue(
8995                        prepared.board.id.as_str(),
8996                        target,
8997                        incoming,
8998                        &prepared.body,
8999                    )
9000                    .await?
9001                } else {
9002                    self.create_and_file_issue(
9003                        prepared.board.id.as_str(),
9004                        target,
9005                        incoming,
9006                        &prepared.body,
9007                    )
9008                    .await?
9009                }
9010            }
9011        };
9012        self.complete_write(incoming, prepared, landed).await
9013    }
9014
9015    /// Everything one write of `incoming` reads and decides before it sends anything: what is
9016    /// there now, where a new item is created, the board's fields, and every value refused while
9017    /// nothing has been written — the item's body included.
9018    async fn prepare_write(
9019        &self,
9020        incoming: &Incoming<'_>,
9021        target: Option<&NativeId>,
9022        depends_on: &[DependencyEdge],
9023    ) -> Result<Prepared, SourceError> {
9024        // Refused before anything is read or written: a task or a project titled the way
9025        // this board spells a document would land as an issue this same source reads back
9026        // as a document, so the field this destination cannot carry is named rather than
9027        // written and silently reclassified.
9028        if let Written::Work(kind, _) = incoming.written
9029            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
9030        {
9031            return Err(SourceError::Refused {
9032                message: format!(
9033                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
9034                     spells a document, so it would read back as one rather than as a {}; \
9035                     retitle it, or copy it as a document",
9036                    kind.marker(),
9037                    self.name,
9038                    kind.marker()
9039                ),
9040            });
9041        }
9042        // The destination is read by its own id, and whether this board holds it is decided
9043        // by that read — its own `projectItems` — rather than by whether a listing of the
9044        // board happens to include it yet. See the module documentation.
9045        // An existing issue is never moved; a new one is created where the rule says — and
9046        // knowing where is what lets the board's fields and that repository's id be read
9047        // together, before anything below needs either.
9048        let destination = match target {
9049            Some(target) => {
9050                WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
9051                    || SourceError::Refused {
9052                        message: format!("GitHub destination item {} was not found", target.0),
9053                    },
9054                )?))
9055            }
9056            None => {
9057                let target = self.creation_target(incoming).await?;
9058                self.creation_context(&target, incoming).await?;
9059                WriteDestination::New(target)
9060            }
9061        };
9062        let existing = destination.existing();
9063        let board = self
9064            .fields_for(
9065                existing,
9066                incoming.written.status().is_some(),
9067                incoming
9068                    .priority
9069                    .is_some_and(|priority| priority != Priority::None),
9070            )
9071            .await?;
9072        let status_target = incoming
9073            .written
9074            .work_status()
9075            .map(|(kind, status)| self.resolved_target(kind, status.category))
9076            .transpose()?;
9077        let column = match (incoming.written.work_status(), status_target.as_ref()) {
9078            (Some((kind, status)), Some(target)) => {
9079                self.column_for(&board.fields, kind, status.category, target)?
9080            }
9081            _ => None,
9082        };
9083        // Resolved before anything is created, for the reason the column above is: a
9084        // priority this board has no option for is refused while nothing has been written.
9085        let priority_write = match incoming.priority {
9086            Some(priority) => self.priority_write(&board.fields, existing, priority)?,
9087            None => None,
9088        };
9089        // And for the reason the priority is: a value no projected field can hold, or a board
9090        // that cannot hold the field, is refused while nothing has been written.
9091        let (projections, projected) = self.projection_writes(
9092            &board.fields,
9093            existing.map(|item| &item.projected),
9094            incoming.metadata,
9095            None,
9096        )?;
9097        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
9098        if content_kind == ContentKind::DraftIssue {
9099            if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
9100                (status_target.as_ref(), incoming.written.status())
9101            {
9102                return Err(self.closes_a_draft(status.category));
9103            }
9104            if incoming.parent.is_some() {
9105                return Err(SourceError::Refused {
9106                    message: "GitHub draft items cannot be a project's sub-issue".into(),
9107                });
9108            }
9109        }
9110        match existing {
9111            Some(item) if content_kind == ContentKind::Issue => {
9112                if item.labels != incoming.labels {
9113                    return Err(SourceError::Refused {
9114                        message: "GitHub issue labels differ from the labels being written".into(),
9115                    });
9116                }
9117            }
9118            _ => {
9119                if !incoming.labels.is_empty() {
9120                    return Err(SourceError::Refused {
9121                        message: "GitHub items created by this destination carry no labels".into(),
9122                    });
9123                }
9124            }
9125        }
9126
9127        // The repository the issue really lives in is what the slot below is written against,
9128        // so a single entry that is where the issue is created travels as no key at all, and
9129        // the read side derives it back from the issue.
9130        let own_repository = match &destination {
9131            WriteDestination::Existing(item) => item.own_repository.clone(),
9132            WriteDestination::New(target) => Some(
9133                Repository::try_from(target.origin())
9134                    .map_err(|message| SourceError::Config { message })?,
9135            ),
9136        };
9137        let (native, fallback) = self
9138            .partition_edges(
9139                incoming.written.kind(),
9140                content_kind,
9141                existing.and_then(|item| item.blocked_by.as_deref()),
9142                depends_on,
9143            )
9144            .await?;
9145        let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
9146        let content = match incoming.assets {
9147            Some(assets) => {
9148                let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
9149                let rewritten = onetaskgraph_plugin_api::serve_asset_references(
9150                    incoming.content.unwrap_or_default(),
9151                    &mut slot,
9152                    &uploads,
9153                );
9154                incoming.content.map(|_| rewritten)
9155            }
9156            None => incoming.content.map(str::to_owned),
9157        };
9158        let body = compose_body(content.as_deref(), &slot)?;
9159        // Read before anything is created, for the reason the field below is: a value
9160        // this destination cannot store has to refuse, and refusing after `createIssue`
9161        // would leave an issue behind that nothing asked for. The engine writes a
9162        // qualified id here; a caller handing this key anything else is told so rather
9163        // than having it silently stored as no origin at all.
9164        // 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.
9165        let origin = match incoming.metadata.get(ORIGIN_KEY) {
9166            None => "",
9167            Some(Value::String(origin)) => origin.as_str(),
9168            Some(other) => {
9169                return Err(SourceError::Refused {
9170                    message: format!(
9171                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
9172                         is {other}"
9173                    ),
9174                });
9175            }
9176        };
9177        // Resolved before anything is created: a board that cannot carry the copy origin
9178        // has to refuse the write, and refusing it after `createIssue` would leave an
9179        // issue behind that nothing asked for.
9180        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
9181            Some(field) => {
9182                if required_str(field, "__typename")? != "ProjectV2Field" {
9183                    return Err(SourceError::Refused {
9184                        message: format!(
9185                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
9186                        ),
9187                    });
9188                }
9189                Some(required_str(field, "id")?.to_owned())
9190            }
9191            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
9192                return Err(SourceError::Refused {
9193                    message: format!(
9194                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
9195                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
9196                         the board"
9197                    ),
9198                });
9199            }
9200            None => None,
9201        };
9202
9203        Ok(Prepared {
9204            origin: origin.to_owned(),
9205            destination,
9206            board,
9207            status_target,
9208            column,
9209            priority_write,
9210            projections,
9211            projected,
9212            content_kind,
9213            own_repository,
9214            native,
9215            slot,
9216            content,
9217            body,
9218            origin_field,
9219        })
9220    }
9221
9222    /// Everything one write does once the item it writes exists: its board fields, its
9223    /// relationships and — for an item that was already there — its content last, then what this
9224    /// process remembers of it.
9225    ///
9226    /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
9227    /// item this command created has its fields, its closing and its edges held for
9228    /// [`TaskSource::finish_writes`] rather than sent here.
9229    async fn complete_write(
9230        &self,
9231        incoming: &Incoming<'_>,
9232        prepared: Prepared,
9233        landed: Landed,
9234    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9235        let Prepared {
9236            destination,
9237            board,
9238            status_target,
9239            column,
9240            priority_write,
9241            projections,
9242            projected,
9243            content_kind,
9244            own_repository,
9245            native,
9246            slot,
9247            content,
9248            body,
9249            origin,
9250            origin_field,
9251        } = prepared;
9252        let existing = destination.existing();
9253        let origin = origin.as_str();
9254        let Landed {
9255            content_id,
9256            item_id,
9257            url,
9258            number,
9259        } = landed;
9260
9261        // Only what differs from the item as this write read it is sent: a priority the item
9262        // already holds is not, so an unchanged re-copy writes no field and a person's edit of
9263        // it since that read is left alone.
9264        let priority_write = priority_write.filter(|_| {
9265            !existing.is_some_and(|item| {
9266                incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
9267            })
9268        });
9269        let written_option = column.as_ref().map(|(_, _, name)| name.clone());
9270        let column = column
9271            .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
9272            .map(|(field, option, _)| (field, option));
9273        // Creating an item here is several calls — `createIssue`, which files it on the
9274        // board, then its board fields, the parent and the dependencies — and GitHub can fail
9275        // at any of them. Everything this source can refuse *before* the first of those is
9276        // already checked above, so what is left is GitHub itself failing part way. When it
9277        // does over an item this call created, the issue is taken back: a write that
9278        // refused must not leave an item behind that nobody asked for, and one that does
9279        // makes the retry create a second.
9280        // Whether the board-field write carrying a moved origin was answered as landing whole.
9281        // When it was refused, GitHub does not say which of its fields ran before the one that
9282        // failed, so the origin may or may not have moved.
9283        let mut origin_landed = false;
9284        // An item this command created while writes are held back has every part that can land
9285        // later held for `finish_writes` — its own create filed it on the board and under its
9286        // project already — and so does a later write of it in the same command.
9287        let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
9288        let landed = if holding {
9289            self.hold_back(
9290                &board.id,
9291                &content_id,
9292                existing,
9293                origin_field.as_deref(),
9294                origin,
9295                column,
9296                status_target.as_ref(),
9297                priority_write.as_ref(),
9298                &projections,
9299                &native,
9300            )
9301        } else if let Some(item_id) = item_id.as_deref() {
9302            self.finish_write(
9303                board.id.as_str(),
9304                incoming,
9305                &content_id,
9306                item_id,
9307                content_kind,
9308                existing,
9309                origin_field.as_deref(),
9310                origin,
9311                column,
9312                status_target.as_ref(),
9313                priority_write.as_ref(),
9314                &projections,
9315                &native,
9316                &mut origin_landed,
9317            )
9318            .await
9319        } else {
9320            // An issue a create filed on the board itself has no board item this process
9321            // knows until `finish_writes` reads it, so nothing but a held-back write may land
9322            // on it before then.
9323            Err(SourceError::Malformed {
9324                message: format!(
9325                    "issue {} was written before the board item GitHub filed it as was read",
9326                    content_id.0
9327                ),
9328            })
9329        };
9330        // An existing item's title, body and state go last, in one `updateIssue`, once its board
9331        // fields and its relationships have landed: a refusal of any of those then leaves its
9332        // body — and the metadata slot inside it — exactly as it stood.
9333        let landed = match (landed, existing) {
9334            (Ok(()), Some(item)) => {
9335                self.update_existing(item, incoming, &body, status_target.as_ref())
9336                    .await
9337            }
9338            (landed, _) => landed,
9339        };
9340        if let Err(error) = landed {
9341            match existing {
9342                // Best effort, and the write's own failure is what the caller is told: a
9343                // refusal naming the tidy-up would hide why the write failed at all.
9344                None => {
9345                    let _ = self.delete_issue(&content_id).await;
9346                }
9347                // The origin field is the one piece of an existing item's metadata written
9348                // before its body, so a write refused after it puts it back as it was. When
9349                // that is refused too, the write's own failure is still what the caller is
9350                // told — with what it left behind added, because the item's metadata is then
9351                // not as it stood and a caller retrying has to know which key moved.
9352                Some(item) => {
9353                    let before = item.origin.as_deref().unwrap_or("");
9354                    if let Some(field) = origin_field.as_deref()
9355                        && before != origin
9356                        && let Err(restore) = self
9357                            .set_item_field(
9358                                board.id.as_str(),
9359                                &item.item_id,
9360                                field,
9361                                json!({"text": before}),
9362                            )
9363                            .await
9364                    {
9365                        let left = if origin_landed {
9366                            format!(
9367                                "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
9368                                 not be put back to {before:?} ({restore}), so item {} still \
9369                                 holds {origin:?} there",
9370                                item.id.0
9371                            )
9372                        } else {
9373                            format!(
9374                                "the refused write carried its {ORIGIN_KEY} from {before:?} to \
9375                                 {origin:?}, GitHub does not say whether that part of it ran, \
9376                                 and putting it back to {before:?} was refused ({restore}), so \
9377                                 item {} holds {origin:?} or {before:?} there",
9378                                item.id.0
9379                            )
9380                        };
9381                        return Err(noting(
9382                            error,
9383                            &format!(
9384                                "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
9385                                 run the write again"
9386                            ),
9387                        ));
9388                    }
9389                }
9390            }
9391            return Err(error);
9392        }
9393
9394        let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
9395            (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
9396                self.statuses
9397                    .status(kind, written_option.as_deref(), true, Some(reason.reason()))
9398            }
9399            (Some((kind, _)), Some(StatusTarget::Column(_))) => {
9400                self.statuses
9401                    .status(kind, written_option.as_deref(), false, None)
9402            }
9403            (Some((_, status)), _) => status.clone(),
9404            (None, _) => Status {
9405                category: StatusCategory::Unknown,
9406                name: "Open".to_owned(),
9407            },
9408        };
9409
9410        // So the rest of this command reads what it just did rather than what the board
9411        // said before it. See `remember_written` for which half takes it.
9412        let remembered = Resolved {
9413            // Empty for an issue whose board item is not read yet: `finish_writes` reads it
9414            // and `learn_item` puts it here.
9415            // 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.
9416            item_id: item_id.unwrap_or_default(),
9417            id: content_id.clone(),
9418            content_kind,
9419            kind: incoming.written.kind(),
9420            title: incoming.title.to_owned(),
9421            // The visible half of the body this write composed, split back off it the
9422            // way a read splits it — so what this record reports is what a read of the
9423            // same issue reports, rather than the person's text with the metadata slot
9424            // still on the end of it.
9425            body: metadata_body(body.clone())?.0,
9426            raw_body: body.clone(),
9427            // A document has no status of its own; what it reads back as is whatever
9428            // the issue's own state says, which is what a re-read reports.
9429            status: written_status,
9430            option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
9431            priority: match incoming.priority {
9432                Some(priority) => HeldPriority::Read(priority),
9433                None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
9434                    item.priority.clone()
9435                }),
9436            },
9437            // What `state_input` asked for: closed for a terminal target, open for any other
9438            // status, and the issue's own state left as it was by a document write.
9439            closed: content_kind == ContentKind::Issue
9440                && match status_target.as_ref() {
9441                    Some(StatusTarget::Terminal(_, _)) => true,
9442                    Some(_) => false,
9443                    None => existing.is_some_and(|item| item.closed),
9444                },
9445            reason: match status_target.as_ref() {
9446                Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
9447                    Some(reason.reason().to_owned())
9448                }
9449                Some(_) => None,
9450                None => existing.and_then(|item| item.reason.clone()),
9451            },
9452            delivers: incoming.delivers.to_vec(),
9453            delivered_by: incoming.delivered_by.to_vec(),
9454            labels: incoming.labels.to_vec(),
9455            parent: incoming.parent.cloned(),
9456            origin: (!origin.is_empty()).then(|| origin.to_owned()),
9457            projected,
9458            number,
9459            // In the update path this is the item's own url, read off `existing` where the
9460            // record above was bound, so one expression serves both halves.
9461            url,
9462            created_at: existing.and_then(|item| item.created_at),
9463            updated_at: existing.and_then(|item| item.updated_at),
9464            // What this write moved is GitHub's to say, so a record of it gives no token until
9465            // it is read again.
9466            item_updated_at: None,
9467            comment_count: None,
9468            relationship_events: None,
9469            served_at: None,
9470            facets: None,
9471            own_repository,
9472            repositories: incoming.repositories.to_vec(),
9473            classification: incoming.classification,
9474            slot,
9475            board_id: Some(board.id.as_str().to_owned()),
9476            fields: board
9477                .fields
9478                .get("nodes")
9479                .and_then(Value::as_array)
9480                .cloned()
9481                .unwrap_or_default(),
9482            board_fields: Some(board.fields.clone()),
9483            // What this write left the relationship holding is known by id alone, and a
9484            // later read of its edges needs each far end's kind, so it reads them again.
9485            blocked_by: None,
9486        };
9487        self.remember_written(remembered, existing.is_none())?;
9488        Ok(onetaskgraph_plugin_api::AssetsWritten {
9489            id: content_id,
9490            content,
9491        })
9492    }
9493
9494    /// Everything a write does after the item exists: its board fields, its parent, and
9495    /// its dependencies.
9496    ///
9497    /// Split out of `write_item` so there is one place a failure past the point of no
9498    /// return is caught, rather than a tidy-up repeated at each `?` above.
9499    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
9500    // so there is one place a failure past the point of no return is caught, and its
9501    // arguments are exactly the values that tail already had in scope. Bundling them into a
9502    // struct would describe no concept — it would be "the arguments of this function" — and
9503    // would put the whole of `write_item`'s locals behind one more indirection.
9504    #[allow(clippy::too_many_arguments)]
9505    async fn finish_write(
9506        &self,
9507        board_id: &str,
9508        incoming: &Incoming<'_>,
9509        content_id: &NativeId,
9510        item_id: &str,
9511        content_kind: ContentKind,
9512        existing: Option<&Resolved>,
9513        origin_field: Option<&str>,
9514        origin: &str,
9515        column: Option<(String, String)>,
9516        status_target: Option<&StatusTarget>,
9517        priority: Option<&PriorityWrite>,
9518        projections: &[ProjectionWrite],
9519        native: &[String],
9520        origin_landed: &mut bool,
9521    ) -> Result<(), SourceError> {
9522        let (fields, clears) = field_writes(
9523            existing,
9524            origin_field,
9525            origin,
9526            column,
9527            priority,
9528            projections,
9529        );
9530        self.set_item_fields(board_id, item_id, &fields, &clears)
9531            .await?;
9532        *origin_landed = true;
9533
9534        // An existing issue closes in the `updateIssue` its write ends with; one created just
9535        // now closes here, once its option is selected.
9536        if existing.is_none()
9537            && content_kind == ContentKind::Issue
9538            && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
9539        {
9540            self.update_content(
9541                ContentKind::Issue,
9542                content_id,
9543                json!({"stateInput":state_input(status_target)}),
9544            )
9545            .await?;
9546        }
9547
9548        if content_kind == ContentKind::Issue {
9549            self.reparent(
9550                existing.and_then(|item| item.parent.clone()),
9551                content_id,
9552                incoming.parent,
9553            )
9554            .await?;
9555            // A document takes part in no dependency graph, so writing one neither reads
9556            // nor changes the issue's own `blockedBy` relationships. Reconciling them
9557            // against the empty list a document write carries would *delete* whatever
9558            // relationships a person had made on that issue, which is a write nobody
9559            // asked for.
9560            if incoming.written.kind() != BoardKind::Document {
9561                let issue = match existing {
9562                    Some(item) => Issue::Existing(item.blocked_by.as_deref()),
9563                    None => Issue::Created,
9564                };
9565                self.reconcile_blocked_by(content_id, native, issue).await?;
9566            }
9567        }
9568        Ok(())
9569    }
9570
9571    /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
9572    fn deferring(&self) -> Result<bool, SourceError> {
9573        Ok(self.deferred()?.is_some())
9574    }
9575
9576    /// Whether `content` is an item this command created whose parts are held back.
9577    fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
9578        Ok(self
9579            .deferred()?
9580            .as_ref()
9581            .is_some_and(|deferred| deferred.holds(content)))
9582    }
9583
9584    fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
9585        self.deferred.lock().map_err(|_| SourceError::Unavailable {
9586            message: "this source's record of the writes it is holding back was left \
9587                      inconsistent by an earlier failure; next: run the command again"
9588                .into(),
9589        })
9590    }
9591
9592    /// Creates one issue filed on the board and under its project by the `createIssue` itself —
9593    /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
9594    /// creates with: one request where adding it to the board and filing it under its project
9595    /// were two more. GitHub adds the board item after answering, so its id is not known here;
9596    /// [`TaskSource::finish_writes`] reads it.
9597    async fn create_filed_issue(
9598        &self,
9599        board_id: &str,
9600        repository: &RepositoryTarget,
9601        incoming: &Incoming<'_>,
9602        body: &Option<String>,
9603    ) -> Result<Landed, SourceError> {
9604        let repository_id = self.repository_id(repository, incoming).await?;
9605        let data = self
9606            .graphql(
9607                graphql::CREATE_ISSUE,
9608                json!({"input": filed_input(&repository_id, board_id, incoming, body)}),
9609            )
9610            .await?;
9611        let created = data
9612            .pointer("/createIssue/issue")
9613            .filter(|value| !value.is_null())
9614            .ok_or_else(|| SourceError::Malformed {
9615                message: "GitHub issue creation returned no issue".into(),
9616            })?;
9617        self.created_landed(created).await
9618    }
9619
9620    /// Keep the address `createIssue` answered for the issue `content`, for
9621    /// [`TaskSource::written_location`].
9622    fn note_written(&self, content: &NativeId, url: Option<&str>) {
9623        if let Some(url) = url {
9624            self.written
9625                .lock()
9626                .unwrap_or_else(std::sync::PoisonError::into_inner)
9627                .insert(content.clone(), url.to_owned());
9628        }
9629    }
9630
9631    /// What one `createIssue` answered, as the item it created — or, when its number cannot be
9632    /// read, the refusal, after taking the issue back.
9633    async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
9634        let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
9635        let url = optional_str(created, "url")?.map(str::to_owned);
9636        let number = match created_issue_number(created) {
9637            Ok(number) => number,
9638            Err(error) => {
9639                let _ = self.delete_issue(&content_id).await;
9640                return Err(error);
9641            }
9642        };
9643        self.note_written(&content_id, url.as_deref());
9644        Ok(Landed {
9645            content_id,
9646            item_id: None,
9647            url,
9648            number,
9649        })
9650    }
9651
9652    /// Hold back everything a write of the created item `content` would send after creating it,
9653    /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
9654    /// `finish_write` sends, the parent excepted, which its create already named. A later write
9655    /// of the same item replaces what it changes.
9656    // 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.
9657    #[allow(clippy::too_many_arguments)]
9658    fn hold_back(
9659        &self,
9660        board_id: &BoardId,
9661        content: &NativeId,
9662        existing: Option<&Resolved>,
9663        origin_field: Option<&str>,
9664        origin: &str,
9665        column: Option<(String, String)>,
9666        status_target: Option<&StatusTarget>,
9667        priority: Option<&PriorityWrite>,
9668        projections: &[ProjectionWrite],
9669        native: &[String],
9670    ) -> Result<(), SourceError> {
9671        let status = column.map(|(field, option)| {
9672            let name = self
9673                .fields_cache()
9674                .ok()
9675                .and_then(|fields| {
9676                    fields.as_ref().and_then(|board| {
9677                        option_name(&board.fields, &field, &option).map(str::to_owned)
9678                    })
9679                })
9680                .unwrap_or_default();
9681            (field, option, name)
9682        });
9683        let (fields, clears) =
9684            field_writes(existing, origin_field, origin, None, priority, projections);
9685        let close = match status_target {
9686            Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
9687            _ => None,
9688        };
9689        let mut deferred = self.deferred()?;
9690        let deferred = deferred.get_or_insert_with(Deferred::default);
9691        if let Deferred::Nothing = deferred {
9692            *deferred = Deferred::Held {
9693                board: board_id.clone(),
9694                created: Vec::new(),
9695            };
9696        }
9697        let Deferred::Held { created, .. } = deferred else {
9698            unreachable!("held back on the line above");
9699        };
9700        let held = match created.iter().position(|held| held.content == *content) {
9701            Some(at) => &mut created[at],
9702            None => {
9703                created.push(HeldBack {
9704                    content: content.clone(),
9705                    fields: Vec::new(),
9706                    status: None,
9707                    clears: Vec::new(),
9708                    close: None,
9709                    edges: Vec::new(),
9710                });
9711                created.last_mut().expect("just held")
9712            }
9713        };
9714        for (field, value) in fields {
9715            held.fields.retain(|(held, _)| *held != field);
9716            held.clears.retain(|held| *held != field);
9717            held.fields.push((field, value));
9718        }
9719        for field in clears {
9720            held.fields.retain(|(held, _)| held != field);
9721            if !held.clears.iter().any(|held| held == field) {
9722                held.clears.push(field.to_owned());
9723            }
9724        }
9725        if status.is_some() {
9726            held.status = status;
9727        }
9728        if close.is_some() {
9729            held.close = close;
9730        }
9731        // Nothing of an item created in this command has been sent but its create, so the edges
9732        // it is to have are exactly the ones its latest write names.
9733        held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
9734        Ok(())
9735    }
9736
9737    /// Create every task of `writes`, together, filed on the board and under each one's
9738    /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
9739    ///
9740    /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
9741    /// before the first request — then created, then completed with its parts held back. A
9742    /// request refused part way leaves the issues it did create, which are taken back, with the
9743    /// rest of this call's, before the refusal is answered.
9744    async fn create_together(
9745        &self,
9746        writes: &[ItemWrite<Task>],
9747    ) -> Result<Vec<NativeId>, SourceError> {
9748        let incomings: Vec<Incoming<'_>> = writes
9749            .iter()
9750            .map(|write| self.task_incoming(write, None))
9751            .collect::<Result<_, _>>()?;
9752        let mut prepared = Vec::with_capacity(writes.len());
9753        for (incoming, write) in incomings.iter().zip(writes) {
9754            if write.target.is_some() {
9755                return Err(SourceError::Malformed {
9756                    message: "a write naming its target was handed to a batch of creates".into(),
9757                });
9758            }
9759            prepared.push(
9760                self.prepare_write(incoming, None, &write.depends_on)
9761                    .await?,
9762            );
9763        }
9764        let mut inputs = Vec::with_capacity(prepared.len());
9765        for (incoming, prepared) in incomings.iter().zip(&prepared) {
9766            let WriteDestination::New(target) = &prepared.destination else {
9767                return Err(SourceError::Malformed {
9768                    message: "an update was decided for a write handed to a batch of creates"
9769                        .into(),
9770                });
9771            };
9772            let repository_id = self.repository_id(target, incoming).await?;
9773            inputs.push(filed_input(
9774                &repository_id,
9775                prepared.board.id.as_str(),
9776                incoming,
9777                &prepared.body,
9778            ));
9779        }
9780        let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
9781        let refused = 'create: {
9782            for chunk in inputs.chunks(MUTATION_BATCH) {
9783                let mut variables = serde_json::Map::new();
9784                for slot in 0..MUTATION_BATCH {
9785                    variables.insert(
9786                        format!("create{slot}"),
9787                        chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
9788                    );
9789                    variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
9790                }
9791                let data = match self
9792                    .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
9793                    .await
9794                {
9795                    Ok(data) => data,
9796                    Err(error) => break 'create Some(error),
9797                };
9798                let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
9799                    let said: Vec<&str> = errors
9800                        .as_array()
9801                        .into_iter()
9802                        .flatten()
9803                        .filter_map(|error| error.get("message").and_then(Value::as_str))
9804                        .collect();
9805                    SourceError::Refused {
9806                        message: if said.is_empty() {
9807                            "GitHub returned GraphQL errors".to_owned()
9808                        } else {
9809                            said.join("; ")
9810                        },
9811                    }
9812                });
9813                for slot in 0..chunk.len() {
9814                    match data
9815                        .pointer(&format!("/create{slot}/issue"))
9816                        .filter(|value| !value.is_null())
9817                    {
9818                        Some(created) => match self.created_landed(created).await {
9819                            Ok(created) => landed.push(created),
9820                            Err(error) => {
9821                                refusal.get_or_insert(error);
9822                            }
9823                        },
9824                        // An alias GitHub refused answers nothing, and the refusal says why.
9825                        None => {
9826                            refusal.get_or_insert(SourceError::Malformed {
9827                                message: "GitHub issue creation returned no issue".into(),
9828                            });
9829                        }
9830                    }
9831                }
9832                if let Some(error) = refusal {
9833                    break 'create Some(error);
9834                }
9835            }
9836            None
9837        };
9838        if let Some(error) = refused {
9839            // None of these ids reaches the caller, so an issue GitHub will not take back is
9840            // named here or by nobody.
9841            let mut left: Vec<&str> = Vec::new();
9842            for created in &landed {
9843                if self.delete_issue(&created.content_id).await.is_err() {
9844                    left.push(created.content_id.0.as_str());
9845                }
9846            }
9847            return Err(left_behind(error, &left));
9848        }
9849        let created: Vec<NativeId> = landed
9850            .iter()
9851            .map(|landed| landed.content_id.clone())
9852            .collect();
9853        let mut ids = Vec::with_capacity(landed.len());
9854        for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
9855            match self.complete_write(incoming, prepared, landed).await {
9856                Ok(written) => ids.push(written.id),
9857                // 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.
9858                Err(error) => {
9859                    let mut left: Vec<&str> = Vec::new();
9860                    for id in &created {
9861                        if self.delete_issue(id).await.is_err() {
9862                            left.push(id.0.as_str());
9863                        }
9864                    }
9865                    return Err(left_behind(error, &left));
9866                }
9867            }
9868        }
9869        Ok(ids)
9870    }
9871
9872    /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
9873    /// created items' board items, then their field values, closes and edges, each kind in
9874    /// requests of [`MUTATION_BATCH`]. Anything refused takes back every issue held, and
9875    /// answers the refusal.
9876    async fn send_held_back(&self) -> Result<(), SourceError> {
9877        let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
9878            return Ok(());
9879        };
9880        if created.is_empty() {
9881            return Ok(());
9882        }
9883        let sent = self.send_deferred(board.as_str(), &created).await;
9884        if let Err(error) = sent {
9885            // Best effort, and the failure is what the caller is told, as a write of one item
9886            // that fails part way takes back what it created: the copy that held these back
9887            // journalled every one, so one this cannot take back its undo tries again, and says
9888            // so when it cannot either.
9889            for held in &created {
9890                let _ = self.delete_issue(&held.content).await;
9891            }
9892            return Err(error);
9893        }
9894        Ok(())
9895    }
9896
9897    /// The requests [`Self::send_held_back`] makes.
9898    async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
9899        let items = self.created_board_items(board, created).await?;
9900        let mut fields: Vec<Value> = Vec::new();
9901        let mut closes: Vec<Value> = Vec::new();
9902        let mut edges: Vec<Value> = Vec::new();
9903        for held in created {
9904            let (item, status) = &items[&held.content];
9905            let mut values = held.fields.clone();
9906            // A status the board already shows — set by a workflow a person turned on to file
9907            // new items at it — is not written again.
9908            if let Some((field, option, name)) = &held.status
9909                && status.as_deref() != Some(name.as_str())
9910            {
9911                values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
9912            }
9913            for (field, value) in values {
9914                fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
9915            }
9916            if let Some(reason) = held.close {
9917                closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
9918            }
9919            for far in &held.edges {
9920                edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
9921            }
9922            if !held.clears.is_empty() {
9923                let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
9924                self.set_item_fields(board, item, &[], &clears).await?;
9925            }
9926        }
9927        // Each alias answers with what it wrote, which is what its input named: the board item a
9928        // field value is set on, the issue closed, the issue blocked and the one blocking it.
9929        let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
9930        let close_answer: &[(&str, &str)] = &[("issue", "id")];
9931        let block_answer: &[(&str, &str)] =
9932            &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
9933        for (document, prefix, inputs, answers) in [
9934            (graphql::WRITE_FIELD_VALUES, "field", &fields, field_answer),
9935            (graphql::CLOSE_ISSUES, "close", &closes, close_answer),
9936            (graphql::ADD_BLOCKED_BYS, "block", &edges, block_answer),
9937        ] {
9938            for chunk in inputs.chunks(MUTATION_BATCH) {
9939                let mut variables = serde_json::Map::new();
9940                for slot in 0..MUTATION_BATCH {
9941                    variables.insert(
9942                        format!("{prefix}{slot}"),
9943                        chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
9944                    );
9945                    variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
9946                }
9947                let data = self.graphql(document, Value::Object(variables)).await?;
9948                for (slot, input) in chunk.iter().enumerate() {
9949                    for (root, named) in answers {
9950                        let answered = data
9951                            .pointer(&format!("/{prefix}{slot}/{root}/id"))
9952                            .and_then(Value::as_str)
9953                            .ok_or_else(|| SourceError::Malformed {
9954                                message: format!(
9955                                    "GitHub answered {} with nothing at {prefix}{slot}",
9956                                    operation_description(document)
9957                                ),
9958                            })?;
9959                        let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
9960                        if answered != asked {
9961                            return Err(SourceError::Malformed {
9962                                message: format!(
9963                                    "GitHub answered {} at {prefix}{slot} with {answered}, which \
9964                                     is not {asked}, the item it was asked to write",
9965                                    operation_description(document)
9966                                ),
9967                            });
9968                        }
9969                    }
9970                }
9971            }
9972        }
9973        for held in created {
9974            self.learn_item(&held.content, &items[&held.content].0)?;
9975        }
9976        Ok(())
9977    }
9978
9979    /// The board item each of `created` is on this board, with its `Status` as the board set it:
9980    /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
9981    /// eight and sixteen — while GitHub has not filed one yet.
9982    async fn created_board_items(
9983        &self,
9984        board: &str,
9985        created: &[HeldBack],
9986    ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
9987        let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
9988        let mut wait = Duration::from_secs(1);
9989        for attempt in 0..6 {
9990            let unfiled: Vec<&NativeId> = created
9991                .iter()
9992                .map(|held| &held.content)
9993                .filter(|id| !found.contains_key(*id))
9994                .collect();
9995            if unfiled.is_empty() {
9996                return Ok(found);
9997            }
9998            if attempt > 0 {
9999                self.clock
10000                    .sleep_because(wait, pacing::cause::BOARD_FILING)
10001                    .await;
10002                wait = wait.saturating_mul(2);
10003            }
10004            for chunk in unfiled.chunks(COPY_DETAIL_BATCH) {
10005                let mut variables = serde_json::Map::new();
10006                for slot in 0..COPY_DETAIL_BATCH {
10007                    variables.insert(
10008                        format!("id{slot}"),
10009                        json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
10010                    );
10011                    variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
10012                }
10013                variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
10014                variables.insert("statusName".into(), json!(STATUS_FIELD));
10015                let data = self
10016                    .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
10017                    .await?;
10018                for (slot, id) in chunk.iter().enumerate() {
10019                    // An answer for another issue than the one asked about says nothing of
10020                    // where that one is filed, and its board item is not this one's.
10021                    let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
10022                    let named = node.map(|node| node.get("id").and_then(Value::as_str));
10023                    if let Some(named) = named
10024                        && named != Some(id.0.as_str())
10025                    {
10026                        return Err(SourceError::Malformed {
10027                            message: format!(
10028                                "GitHub answered the board items of {} when asked for those of \
10029                                 {}",
10030                                named.unwrap_or("an issue it did not name"),
10031                                id.0
10032                            ),
10033                        });
10034                    }
10035                    // An issue GitHub does not answer yet is not filed yet; one it answers is
10036                    // read whole, its connection, each board item's board and its status, and
10037                    // any of them unreadable is refused rather than read as not filed.
10038                    let Some(node) = node else { continue };
10039                    let malformed = |what: &str| SourceError::Malformed {
10040                        message: format!("GitHub answered the board items of {} with {what}", id.0),
10041                    };
10042                    let nodes = node
10043                        .pointer("/projectItems/nodes")
10044                        .and_then(Value::as_array)
10045                        .ok_or_else(|| malformed("no list of board items"))?;
10046                    let mut item = None;
10047                    for node in nodes {
10048                        let on = node
10049                            .pointer("/project/id")
10050                            .and_then(Value::as_str)
10051                            .ok_or_else(|| malformed("a board item naming no board"))?;
10052                        if on == board {
10053                            item = Some(node);
10054                        }
10055                    }
10056                    if let Some(item) = item {
10057                        let status = match item.get("status") {
10058                            None | Some(Value::Null) => None,
10059                            Some(status) => Some(
10060                                status
10061                                    .get("name")
10062                                    .and_then(Value::as_str)
10063                                    .ok_or_else(|| malformed("a status that names no option"))?
10064                                    .to_owned(),
10065                            ),
10066                        };
10067                        found.insert(
10068                            (*id).clone(),
10069                            (required_nonblank_str(item, "id")?.to_owned(), status),
10070                        );
10071                    }
10072                }
10073            }
10074        }
10075        let unfiled: Vec<&str> = created
10076            .iter()
10077            .map(|held| held.content.0.as_str())
10078            .filter(|id| !found.keys().any(|found| found.0 == *id))
10079            .collect();
10080        // The last read may be the one that finds the rest.
10081        if unfiled.is_empty() {
10082            return Ok(found);
10083        }
10084        Err(SourceError::Unavailable {
10085            message: format!(
10086                "GitHub created {} filed on board {board} and has not added {} to it after 31 \
10087                 seconds: {}; next: run the copy again once GitHub has caught up",
10088                if unfiled.len() == 1 {
10089                    "an issue"
10090                } else {
10091                    "issues"
10092                },
10093                if unfiled.len() == 1 { "it" } else { "them" },
10094                unfiled.join(", ")
10095            ),
10096        })
10097    }
10098
10099    /// The board item id of `content`, which this process created and remembered without one,
10100    /// in every half of its own view.
10101    fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
10102        if let Some(held) = self.resolved_cache()?.get_mut(content) {
10103            held.item_id = item.to_owned();
10104        }
10105        if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
10106            held.item_id = item.to_owned();
10107        }
10108        Ok(())
10109    }
10110
10111    /// Ask together which items each of `origins` names, and what creating an item reads; see
10112    /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
10113    /// page is left to its own lookup.
10114    async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
10115        let Some(repository) = self.repository.clone() else {
10116            return Ok(());
10117        };
10118        let unasked: Vec<&String> = {
10119            let held = self.narrowed_cache()?;
10120            let mut unasked: Vec<&String> = Vec::new();
10121            for origin in origins {
10122                if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
10123                    && !unasked.contains(&origin)
10124                {
10125                    unasked.push(origin);
10126                }
10127            }
10128            unasked
10129        };
10130        if unasked.is_empty() {
10131            return Ok(());
10132        }
10133        for chunk in unasked.chunks(ORIGIN_BATCH) {
10134            let mut variables = json!({"owner":self.owner,"number":self.project_number,
10135                "type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
10136                "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
10137                "repositoryOwner":repository.owner,"repositoryName":repository.name});
10138            for slot in 0..ORIGIN_BATCH {
10139                let origin = chunk.get(slot).unwrap_or(&chunk[0]);
10140                variables[format!("filter{slot}")] =
10141                    json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
10142                variables[format!("search{slot}")] = json!(
10143                    self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
10144                );
10145                variables[format!("ask{slot}")] = json!(slot < chunk.len());
10146            }
10147            let data = self.graphql(graphql::ORIGIN_LOOKUPS, variables).await?;
10148            self.fields_read(&data)?;
10149            if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
10150                let id = required_nonblank_str(node, "id")?.to_owned();
10151                self.repository_cache()?.insert(repository.clone(), id);
10152                if let Some(numeric) = node
10153                    .get("databaseId")
10154                    .and_then(Value::as_u64)
10155                    .and_then(std::num::NonZeroU64::new)
10156                {
10157                    self.numeric_repositories
10158                        .lock()
10159                        .await
10160                        .insert(repository.clone(), numeric);
10161                }
10162            }
10163            for (slot, origin) in chunk.iter().enumerate() {
10164                let items = data
10165                    .pointer(&format!("/o{slot}/projectV2/items"))
10166                    .filter(|value| !value.is_null())
10167                    .ok_or_else(|| SourceError::Refused {
10168                        message: format!(
10169                            "GitHub project {}/{} was not found or is not visible to the token",
10170                            self.owner, self.project_number
10171                        ),
10172                    })?;
10173                let searched =
10174                    data.get(format!("s{slot}"))
10175                        .ok_or_else(|| SourceError::Malformed {
10176                            message: "GitHub search response has no search connection".into(),
10177                        })?;
10178                if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
10179                    continue;
10180                }
10181                let mut found: Vec<Resolved> = Vec::new();
10182                for item in optional_nodes(Some(items), "project items")?
10183                    .into_iter()
10184                    .flatten()
10185                {
10186                    if let Some(resolved) = self.resolve(item)?
10187                        && resolved.content_kind == ContentKind::Issue
10188                        && !found.iter().any(|held| held.id == resolved.id)
10189                    {
10190                        found.push(resolved);
10191                    }
10192                }
10193                for node in optional_nodes(Some(searched), "search")?
10194                    .into_iter()
10195                    .flatten()
10196                {
10197                    if let Some(resolved) = self.resolve_issue(node).await?
10198                        && !found.iter().any(|held| held.id == resolved.id)
10199                    {
10200                        found.push(resolved);
10201                    }
10202                }
10203                self.narrowed_cache()?
10204                    .insert(Narrowing::Origin((*origin).clone()).key(), found);
10205            }
10206        }
10207        Ok(())
10208    }
10209
10210    /// Delete one issue, which takes its board item with it.
10211    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
10212        let data = self
10213            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10214            .await?;
10215        data.pointer("/deleteIssue/repository")
10216            .filter(|value| !value.is_null())
10217            .ok_or_else(|| SourceError::Malformed {
10218                message: "GitHub issue deletion returned no repository".into(),
10219            })?;
10220        self.forget(id)?;
10221        Ok(())
10222    }
10223
10224    /// Remove one item this copy created, so a copy that could not finish leaves the board
10225    /// as it found it.
10226    ///
10227    /// Deleting the issue takes its board item with it, so there is no second mutation to
10228    /// keep in step. An id the board does not hold is not an error: the item is already
10229    /// gone, which is the state this asks for. Which that is, is decided by reading the item
10230    /// by its own id — a listing of the board can still be missing an item it holds, and
10231    /// reading that as *already gone* would leave behind the very item this was asked to
10232    /// take back.
10233    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
10234        let Some(item) = self.bound_item(id).await? else {
10235            return Ok(());
10236        };
10237        if item.content_kind == ContentKind::DraftIssue {
10238            return Err(SourceError::Refused {
10239                message: format!(
10240                    "GitHub item {} is a draft, and this source removes an item by deleting \
10241                     its issue; next: remove it from the board by hand",
10242                    id.0
10243                ),
10244            });
10245        }
10246        let data = self
10247            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10248            .await?;
10249        data.pointer("/deleteIssue/repository")
10250            .filter(|value| !value.is_null())
10251            .ok_or_else(|| SourceError::Malformed {
10252                message: "GitHub issue deletion returned no repository".into(),
10253            })?;
10254        self.forget(id)?;
10255        Ok(())
10256    }
10257
10258    /// The issue a comment call on `task` is about, or `None` when this board holds no such
10259    /// task.
10260    ///
10261    /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
10262    /// read of the task cannot disagree about which ids name one: a project or a document of
10263    /// this board is not a task here either.
10264    ///
10265    /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
10266    /// issues and a draft is not one. It is refused rather than answered with an empty page,
10267    /// which would read as a task nobody has commented on yet.
10268    async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
10269        let cached = self.resolved_cache()?.get(task).cloned();
10270        let Some(item) = (match cached {
10271            Some(item) => Some(item),
10272            None => self.item_by_id(task).await?,
10273        })
10274        .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10275            return Ok(None);
10276        };
10277        if item.content_kind == ContentKind::DraftIssue {
10278            return Err(self.draft_has_no_comments(task));
10279        }
10280        Ok(Some(item.id))
10281    }
10282
10283    /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
10284    /// issues, and a draft is not one.
10285    fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
10286        SourceError::Refused {
10287            message: format!(
10288                "task {} of source {} is a draft item on the board, and GitHub keeps \
10289                 comments on issues alone, so a draft has none to read or write; next: \
10290                 convert the draft to an issue on the board, then comment on the issue it \
10291                 becomes",
10292                task.0, self.name
10293            ),
10294        }
10295    }
10296
10297    /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
10298    /// request — or `None` when this board holds no task by that id.
10299    ///
10300    /// What `task show` and a comment listing read. A draft is a task with no comments, so it
10301    /// is answered with the draft and the refusal, at the price of the draft's own read.
10302    async fn issue_detail(
10303        &self,
10304        id: &NativeId,
10305        page: &PageRequest,
10306    ) -> Result<Option<TaskDetailRead>, SourceError> {
10307        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
10308        let asked = self
10309            .graphql(
10310                graphql::ISSUE_DETAIL,
10311                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
10312                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
10313                       "duplicates":true}),
10314            )
10315            .await;
10316        let data = match asked {
10317            Ok(data) => data,
10318            Err(error) if unresolvable_node(&error) => return Ok(None),
10319            Err(error) => return Err(error),
10320        };
10321        // `node` is null for an id that names nothing, and absent only from an answer this
10322        // source cannot read — never the same thing.
10323        let node = data.get("node").ok_or_else(|| SourceError::Malformed {
10324            message: format!("GitHub answered the read of {} with no node", id.0),
10325        })?;
10326        self.detail_of(id, node, true, after).await
10327    }
10328
10329    /// Several tasks, each with the first page of its comments when `comments` is set, read
10330    /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
10331    /// order.
10332    ///
10333    /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
10334    /// one item at a time, so that id is answered as missing and the others as themselves; any
10335    /// other refusal is every id of that batch's answer.
10336    async fn issue_details(
10337        &self,
10338        ids: &[NativeId],
10339        comments: Option<&PageRequest>,
10340    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10341        let mut read = Vec::with_capacity(ids.len());
10342        for batch in ids.chunks(DETAIL_BATCH) {
10343            match self
10344                .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
10345                .await
10346            {
10347                Ok(data) => {
10348                    for (slot, id) in batch.iter().enumerate() {
10349                        // Every alias asked for is answered, null for an id naming nothing;
10350                        // one missing is an answer this source cannot read.
10351                        let read_one = match data.get(format!("i{slot}")) {
10352                            Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
10353                            None => Err(SourceError::Malformed {
10354                                message: format!(
10355                                    "GitHub answered a batch read with no item for {}",
10356                                    id.0
10357                                ),
10358                            }),
10359                        };
10360                        read.push(read_one);
10361                    }
10362                }
10363                Err(error) if unresolvable_node(&error) => {
10364                    for id in batch {
10365                        read.push(match comments {
10366                            Some(page) => self.issue_detail(id, page).await,
10367                            None => self.task_read(id).await,
10368                        });
10369                    }
10370                }
10371                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
10372            }
10373        }
10374        read
10375    }
10376
10377    /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
10378    async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
10379        Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
10380            task,
10381            comments: None,
10382        }))
10383    }
10384
10385    /// What one node a detail read reached says: the task this board holds by `id`, with the
10386    /// page of comments the node carries when `commented` — or `None` for a node that is no
10387    /// task of this board.
10388    ///
10389    /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
10390    /// and an item this process created answers from this process's own record, which a node
10391    /// read taken moments after the write can still be behind.
10392    async fn detail_of(
10393        &self,
10394        id: &NativeId,
10395        node: &Value,
10396        commented: bool,
10397        after: Option<&str>,
10398    ) -> Result<Option<TaskDetailRead>, SourceError> {
10399        if node.is_null() {
10400            return Ok(None);
10401        }
10402        let draft = optional_str(node, "__typename")? == Some("DraftIssue");
10403        // An issue answered under one id is that id's, or the answer is not one this source
10404        // can report: reporting another issue's task and comments under the qualified id asked
10405        // for would be the one wrong answer here. A draft's own read checks the same.
10406        if !draft
10407            && optional_str(node, "__typename")? == Some("Issue")
10408            && required_str(node, "id")? != id.0
10409        {
10410            return Err(SourceError::Malformed {
10411                message: format!(
10412                    "GitHub answered the read of {} with issue {}",
10413                    id.0,
10414                    required_str(node, "id")?
10415                ),
10416            });
10417        }
10418        let item = if draft {
10419            self.draft_by_id(id).await?
10420        } else {
10421            self.resolve_issue(node).await?
10422        };
10423        // A read that carried the whole of what the issue is blocked by — a copy's — is kept
10424        // for this command, so the dependency read the copy makes next is answered from it.
10425        if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
10426            self.resolved_cache()?.insert(read.id.clone(), read.clone());
10427        }
10428        let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10429            return Ok(None);
10430        };
10431        let own = self.created()?.iter().find(|own| own.id == *id).cloned();
10432        let task = own.unwrap_or(item).task()?;
10433        let comments = match (commented, draft) {
10434            (false, _) => None,
10435            (true, true) => Some(Err(self.draft_has_no_comments(id))),
10436            (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
10437        };
10438        if commented && !draft && after.is_none() {
10439            self.expected.learn_comments(id, node);
10440        }
10441        Ok(Some(TaskDetailRead { task, comments }))
10442    }
10443
10444    /// Whether the comment `comment` is one of `issue`'s own.
10445    ///
10446    /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
10447    /// comment's id and nothing else: a comment id given against the wrong task would
10448    /// otherwise change a comment on some other issue entirely. An id that names nothing, or
10449    /// names something that is not an issue comment, is a comment this task does not have —
10450    /// which is what GitHub refusing to resolve it means too.
10451    async fn comment_is_on(
10452        &self,
10453        issue: &NativeId,
10454        comment: &NativeId,
10455    ) -> Result<bool, SourceError> {
10456        Ok(self.comment_on(issue, comment).await?.is_some())
10457    }
10458
10459    /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
10460    /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
10461    async fn comment_on(
10462        &self,
10463        issue: &NativeId,
10464        comment: &NativeId,
10465    ) -> Result<Option<String>, SourceError> {
10466        // A write the store's cache expects knows which comments its issue held, and their
10467        // bodies, at the version it expects: what this read would answer.
10468        if let Some(primed) = self.expected.primed_comment(issue, comment) {
10469            return Ok(primed);
10470        }
10471        let asked = self
10472            .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
10473            .await;
10474        let data = match asked {
10475            Ok(data) => data,
10476            Err(error) if unresolvable_node(&error) => return Ok(None),
10477            Err(error) => return Err(error),
10478        };
10479        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
10480            return Ok(None);
10481        };
10482        if optional_str(node, "__typename")? != Some("IssueComment") {
10483            return Ok(None);
10484        }
10485        let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
10486            message: format!("GitHub issue comment {} names no issue", comment.0),
10487        })?;
10488        if required_str(on, "id")? != issue.0 {
10489            return Ok(None);
10490        }
10491        Ok(Some(required_str(node, "body")?.to_owned()))
10492    }
10493
10494    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
10495    async fn partition_edges(
10496        &self,
10497        near_kind: BoardKind,
10498        near_content: ContentKind,
10499        carried: Option<&[Value]>,
10500        depends_on: &[DependencyEdge],
10501    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
10502        let mut native = Vec::new();
10503        let mut fallback = Vec::new();
10504        let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
10505            .iter()
10506            .map(|edge| {
10507                // An opaque far end names another tracker, so it is never this board's own.
10508                let same_source = edge.to.designator().is_none()
10509                    && edge
10510                        .to
10511                        .source()
10512                        .is_none_or(|source| source == self.name.as_str());
10513                // A qualified id's source segment runs to its *first* colon — `GlobalId` and
10514                // `DependencyEndpoint::source` both read it that way — and a native id may hold
10515                // colons of its own, so the far end is everything after that one separator.
10516                // Splitting at the last would truncate `work:urn:task:7` to `7`.
10517                let far_id = if edge.to.is_qualified() {
10518                    edge.to
10519                        .id()
10520                        .split_once(':')
10521                        .map_or(edge.to.id(), |(_, native)| native)
10522                } else {
10523                    edge.to.id()
10524                };
10525                // One that already blocks the near issue was answered by that issue's own
10526                // read, which carried each of its blockers' kinds — an issue every one — so it
10527                // is not read again.
10528                let blocking = carried.and_then(|nodes| {
10529                    nodes
10530                        .iter()
10531                        .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
10532                });
10533                (edge, far_id, same_source, blocking)
10534            })
10535            .collect();
10536        // Every other same-source far end is read by its own id, exactly as the item it is a
10537        // far end of is: whether this board holds it is that read's answer, never a listing's.
10538        // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
10539        let mut unread: Vec<NativeId> = Vec::new();
10540        for (_, far_id, same_source, blocking) in &far_ends {
10541            let id = NativeId((*far_id).to_owned());
10542            if *same_source && blocking.is_none() && !unread.contains(&id) {
10543                unread.push(id);
10544            }
10545        }
10546        let read: BTreeMap<NativeId, Option<Resolved>> = unread
10547            .iter()
10548            .cloned()
10549            .zip(self.items_by_ids(&unread).await?)
10550            .collect();
10551        for (edge, far_id, same_source, blocking) in far_ends {
10552            let far = match (same_source, blocking) {
10553                (false, _) => None,
10554                (true, Some(node)) => Some(FarEnd {
10555                    kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
10556                        BoardKind::Document
10557                    } else {
10558                        BoardKind::Work(related_kind(node)?)
10559                    },
10560                    content_kind: ContentKind::Issue,
10561                }),
10562                (true, None) => {
10563                    let read = read
10564                        .get(&NativeId(far_id.to_owned()))
10565                        .cloned()
10566                        .flatten()
10567                        .ok_or_else(|| SourceError::Refused {
10568                            message: format!("GitHub dependency item {far_id} was not found"),
10569                        })?;
10570                    Some(FarEnd {
10571                        kind: read.kind,
10572                        content_kind: read.content_kind,
10573                    })
10574                }
10575            };
10576            let far = far.as_ref();
10577            // The caller says which kind the far end is, and this board holds the far end
10578            // itself, so a disagreement is settled here rather than stored: recorded, the
10579            // wrong kind would read back as a cross-level edge that never existed; written
10580            // natively, it would name a relationship of a different level than the caller
10581            // asked for.
10582            //
10583            // A far end this board holds as a *document* fails the same comparison and is
10584            // refused by the same sentence: `ItemKind` has no document variant because
10585            // nothing may point at one, so no caller can name it correctly and the refusal
10586            // is the only honest answer.
10587            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
10588                return Err(SourceError::Refused {
10589                    message: format!(
10590                        "GitHub dependency item {far_id} is a {} of this board, and this item \
10591                         names it as a {}; record the kind it is",
10592                        disagreeing.kind.describes(),
10593                        edge.to.kind.marker()
10594                    ),
10595                });
10596            }
10597            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
10598            // however the far end is spelled — and one classified native here would be
10599            // written nowhere at all, because a draft's native reconciliation never runs.
10600            let native_here = near_content == ContentKind::Issue
10601                && far.is_some_and(|far| {
10602                    far.content_kind == ContentKind::Issue
10603                        && BoardKind::Work(edge.to.kind) == near_kind
10604                });
10605            if native_here {
10606                native.push(far_id.to_owned());
10607            } else {
10608                fallback.push(edge.clone());
10609            }
10610        }
10611        Ok((native, fallback))
10612    }
10613
10614    async fn update_existing(
10615        &self,
10616        item: &Resolved,
10617        incoming: &Incoming<'_>,
10618        body: &Option<String>,
10619        status_target: Option<&StatusTarget>,
10620    ) -> Result<(), SourceError> {
10621        let title = incoming.written_title();
10622        // A terminal status closes the issue here, in the same mutation as its body: its board
10623        // option was selected before this, so a close never lands on an item whose board cannot
10624        // show it.
10625        let mut fields = match item.content_kind {
10626            ContentKind::DraftIssue => json!({"title":title,"body":body}),
10627            ContentKind::Issue => json!({"title":title,"body":body,
10628                                         "stateInput":state_input(status_target)}),
10629        };
10630        // Only the members whose value differs from the item as this write read it are sent,
10631        // and nothing at all when none does — whether or not the store's cache expects a
10632        // version of it. A member left out is one GitHub leaves as it is, so a person's edit
10633        // of it since that read is kept rather than overwritten with what it already held,
10634        // and a re-copy of an unchanged item costs no mutation.
10635        if let Some(members) = fields.as_object_mut() {
10636            if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
10637                members.remove("title");
10638            }
10639            let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
10640                (Some(Value::String(sent)), Some(held)) => sent == held,
10641                (Some(Value::Null), held) => held.is_none_or(str::is_empty),
10642                _ => false,
10643            };
10644            if body_unchanged {
10645                members.remove("body");
10646            }
10647            // An issue already in the state the target asks for keeps it: an open one stays
10648            // open, and a closed one whose closure reads as the target's category keeps its
10649            // reason — a duplicate's says more than this write would.
10650            let unchanged_state = match status_target {
10651                Some(StatusTarget::Terminal(_, reason)) => {
10652                    item.closed
10653                        && closed_category(item.reason.as_deref())
10654                            == closed_category(Some(reason.reason()))
10655                }
10656                Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
10657                None => true,
10658            };
10659            if unchanged_state {
10660                members.remove("stateInput");
10661            }
10662            if members.is_empty() {
10663                return Ok(());
10664            }
10665        }
10666        self.update_content(item.content_kind, &item.id, fields)
10667            .await
10668    }
10669
10670    /// Update one board item's content with exactly `fields` beside its id, through the
10671    /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
10672    /// a draft.
10673    ///
10674    /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
10675    /// is what lets a narrow write carry the one thing it changes and nothing else.
10676    async fn update_content(
10677        &self,
10678        kind: ContentKind,
10679        id: &NativeId,
10680        fields: Value,
10681    ) -> Result<(), SourceError> {
10682        let (operation, id_key, pointer) = match kind {
10683            ContentKind::DraftIssue => (
10684                graphql::UPDATE_DRAFT,
10685                "draftIssueId",
10686                "/updateProjectV2DraftIssue/draftIssue",
10687            ),
10688            ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
10689        };
10690        let mut input = fields;
10691        input[id_key] = json!(id.0);
10692        let data = self.graphql(operation, json!({"input":input})).await?;
10693        let returned = data
10694            .pointer(pointer)
10695            .ok_or_else(|| SourceError::Malformed {
10696                message: "GitHub item update returned no item".into(),
10697            })?;
10698        if required_str(returned, "id")? != id.0 {
10699            return Err(SourceError::Malformed {
10700                message: "GitHub item update returned the wrong item".into(),
10701            });
10702        }
10703        Ok(())
10704    }
10705
10706    /// Creates one issue, files it on the board, and reports what a read of it would say:
10707    /// its content id, its board item id, and the web address GitHub gave it.
10708    ///
10709    /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
10710    /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
10711    /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
10712    /// board item, and the `addProjectV2ItemById` that then had to follow was refused
10713    /// "Content already exists in this project". A terminal status is not written here:
10714    /// `finish_write` selects its option first and closes the issue after, so a close never
10715    /// lands on an item whose board cannot show it.
10716    ///
10717    /// The address and the number come back here because this is the only place either is
10718    /// known before GitHub's own board read catches up — an item this run created answers
10719    /// the reads that follow it out of the record below, and one remembered without them
10720    /// would report no location and no key for the rest of the run.
10721    async fn create_and_file_issue(
10722        &self,
10723        board_id: &str,
10724        repository: &RepositoryTarget,
10725        incoming: &Incoming<'_>,
10726        body: &Option<String>,
10727    ) -> Result<Landed, SourceError> {
10728        let repository_id = self.repository_id(repository, incoming).await?;
10729        let data = self
10730            .graphql(
10731                graphql::CREATE_ISSUE,
10732                json!({"input":{
10733                    "repositoryId":repository_id,"title":incoming.written_title(),"body":body
10734                }}),
10735            )
10736            .await?;
10737        let created = data
10738            .pointer("/createIssue/issue")
10739            .filter(|value| !value.is_null())
10740            .ok_or_else(|| SourceError::Malformed {
10741                message: "GitHub issue creation returned no issue".into(),
10742            })?;
10743        let content_id = NativeId(required_str(created, "id")?.to_owned());
10744        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
10745        // a response without it is not worth failing a landed write over — the item simply
10746        // reports no location until the board read catches up, which is what it did before.
10747        let url = optional_str(created, "url")?.map(str::to_owned);
10748        // The issue exists from here on, so an unreadable number and a refused board
10749        // filing below each try, best effort, to take it back: an issue in the repository
10750        // that is on no board is an item nobody asked for and nothing here would find again.
10751        //
10752        // Its number is optional on the same terms its address is — a landed write is not
10753        // worth failing over a member that came back missing, and such an item reports no
10754        // handle until a board read catches up. A number that is *present* and is not an
10755        // unsigned integer is still a response this source cannot read.
10756        let number = match created_issue_number(created) {
10757            Ok(number) => number,
10758            Err(error) => {
10759                let _ = self.delete_issue(&content_id).await;
10760                return Err(error);
10761            }
10762        };
10763        let added = match self
10764            .graphql(
10765                graphql::ADD_TO_BOARD,
10766                json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
10767            )
10768            .await
10769        {
10770            Ok(added) => added,
10771            Err(error) => {
10772                let _ = self.delete_issue(&content_id).await;
10773                return Err(error);
10774            }
10775        };
10776        let item = added
10777            .pointer("/addProjectV2ItemById/item")
10778            .filter(|value| !value.is_null())
10779            .ok_or_else(|| SourceError::Malformed {
10780                message: "GitHub board addition returned no project item".into(),
10781            })?;
10782        self.note_written(&content_id, url.as_deref());
10783        Ok(Landed {
10784            content_id,
10785            item_id: Some(required_str(item, "id")?.to_owned()),
10786            url,
10787            number,
10788        })
10789    }
10790
10791    /// Move one issue under the project it now belongs to, or out of the one it left.
10792    async fn reparent(
10793        &self,
10794        held: Option<NativeId>,
10795        child: &NativeId,
10796        wanted: Option<&NativeId>,
10797    ) -> Result<(), SourceError> {
10798        if held.as_ref() == wanted {
10799            return Ok(());
10800        }
10801        if let Some(held) = &held {
10802            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
10803                .await?;
10804        }
10805        if let Some(wanted) = wanted {
10806            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
10807                .await?;
10808        }
10809        Ok(())
10810    }
10811
10812    async fn sub_issue(
10813        &self,
10814        operation: &str,
10815        parent: &NativeId,
10816        child: &NativeId,
10817        root: &str,
10818    ) -> Result<(), SourceError> {
10819        let data = self
10820            .graphql(
10821                operation,
10822                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
10823            )
10824            .await?;
10825        let issue =
10826            data.pointer(&format!("/{root}/issue"))
10827                .ok_or_else(|| SourceError::Malformed {
10828                    message: "GitHub sub-issue update returned no issue".into(),
10829                })?;
10830        let sub =
10831            data.pointer(&format!("/{root}/subIssue"))
10832                .ok_or_else(|| SourceError::Malformed {
10833                    message: "GitHub sub-issue update returned no sub-issue".into(),
10834                })?;
10835        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
10836            return Err(SourceError::Malformed {
10837                message: "GitHub sub-issue update returned the wrong issues".into(),
10838            });
10839        }
10840        Ok(())
10841    }
10842
10843    /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
10844    /// whether there was one.
10845    ///
10846    /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
10847    /// relationships are not read: there is nothing a read of them could find.
10848    async fn reconcile_blocked_by(
10849        &self,
10850        content_id: &NativeId,
10851        native: &[String],
10852        issue: Issue<'_>,
10853    ) -> Result<bool, SourceError> {
10854        let current = match issue {
10855            Issue::Created => Vec::new(),
10856            Issue::Existing(Some(held)) => held
10857                .iter()
10858                .map(|far| required_str(far, "id").map(str::to_owned))
10859                .collect::<Result<Vec<_>, _>>()?,
10860            Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
10861        };
10862        let mut changed = false;
10863        for (operation, far_id) in current
10864            .iter()
10865            .filter(|id| !native.contains(id))
10866            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
10867            .chain(
10868                native
10869                    .iter()
10870                    .filter(|id| !current.contains(id))
10871                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
10872            )
10873        {
10874            let data = self
10875                .graphql(
10876                    operation,
10877                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
10878                )
10879                .await?;
10880            let root = if operation == graphql::ADD_BLOCKED_BY {
10881                "addBlockedBy"
10882            } else {
10883                "removeBlockedBy"
10884            };
10885            let issue =
10886                data.pointer(&format!("/{root}/issue"))
10887                    .ok_or_else(|| SourceError::Malformed {
10888                        message: "GitHub dependency update returned no issue".into(),
10889                    })?;
10890            let blocker = data
10891                .pointer(&format!("/{root}/blockingIssue"))
10892                .ok_or_else(|| SourceError::Malformed {
10893                    message: "GitHub dependency update returned no blocking issue".into(),
10894                })?;
10895            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
10896            {
10897                return Err(SourceError::Malformed {
10898                    message: "GitHub dependency update returned the wrong issues".into(),
10899                });
10900            }
10901            changed = true;
10902        }
10903        Ok(changed)
10904    }
10905}
10906
10907/// The board field values one write sends, in the order GitHub runs them, and the fields it
10908/// clears after them: the origin field when it moves, the `Status` option `column` names, the
10909/// priority, then each projected field.
10910fn field_writes<'a>(
10911    existing: Option<&Resolved>,
10912    origin_field: Option<&str>,
10913    origin: &str,
10914    column: Option<(String, String)>,
10915    priority: Option<&'a PriorityWrite>,
10916    projections: &'a [ProjectionWrite],
10917) -> (Vec<(String, Value)>, Vec<&'a str>) {
10918    let mut fields = Vec::new();
10919    if let Some(field_id) = origin_field
10920        && existing.map_or(!origin.is_empty(), |item| {
10921            item.origin.as_deref().unwrap_or("") != origin
10922        })
10923    {
10924        fields.push((field_id.to_owned(), json!({"text":origin})));
10925    }
10926    if let Some((field_id, option_id)) = column {
10927        fields.push((field_id, json!({"singleSelectOptionId":option_id})));
10928    }
10929    let mut clears = Vec::new();
10930    match priority {
10931        Some(PriorityWrite::Select { field, option }) => {
10932            fields.push((field.clone(), json!({"singleSelectOptionId":option})));
10933        }
10934        Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
10935        None => {}
10936    }
10937    for projection in projections {
10938        projection.join(&mut fields, &mut clears);
10939    }
10940    (fields, clears)
10941}
10942
10943/// Everything one write decided before sending anything: see
10944/// [`GitHubProjectsSource::prepare_write`].
10945struct Prepared {
10946    /// What the write lands on.
10947    destination: WriteDestination,
10948    board: BoardFields,
10949    status_target: Option<StatusTarget>,
10950    /// The `Status` field, the option and the option's name the status lands on.
10951    // 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.
10952    column: Option<(String, String, String)>,
10953    priority_write: Option<PriorityWrite>,
10954    projections: Vec<ProjectionWrite>,
10955    projected: BTreeMap<String, String>,
10956    content_kind: ContentKind,
10957    own_repository: Option<Repository>,
10958    /// The far ends of the item's native blocked-by relationships.
10959    // 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.
10960    native: Vec<String>,
10961    slot: BTreeMap<String, Value>,
10962    content: Option<String>,
10963    body: Option<String>,
10964    /// The copy origin the item records, empty for none.
10965    // 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.
10966    origin: String,
10967    origin_field: Option<String>,
10968}
10969
10970/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
10971/// back — each issue of `left` it created that is still in the repository, which its caller,
10972/// never handed their ids, could not name.
10973fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
10974    if left.is_empty() {
10975        return error;
10976    }
10977    SourceError::Refused {
10978        message: format!(
10979            "{error}; GitHub then refused to take back {}, which this write created and which \
10980             are still in the repository; next: delete {} before writing again",
10981            left.join(", "),
10982            if left.len() == 1 { "it" } else { "them" }
10983        ),
10984    }
10985}
10986
10987/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
10988enum WriteDestination {
10989    /// The item as it stood, for an update.
10990    Existing(Box<Resolved>),
10991    /// Where a new item is created.
10992    New(RepositoryTarget),
10993}
10994
10995impl WriteDestination {
10996    /// The item an update writes over, as it stood; `None` for a create.
10997    fn existing(&self) -> Option<&Resolved> {
10998        match self {
10999            Self::Existing(item) => Some(item),
11000            Self::New(_) => None,
11001        }
11002    }
11003}
11004
11005/// What a copy has held back of the items it created; see
11006/// [`GitHubProjectsSource::hold_back`].
11007#[derive(Debug, Default)]
11008enum Deferred {
11009    /// Writes are being held back, and nothing has been held yet.
11010    #[default]
11011    Nothing,
11012    /// The items held back, every one of them on `board`.
11013    Held {
11014        /// The board every one of them is on.
11015        board: BoardId,
11016        /// Each created item, in the order it was created.
11017        created: Vec<HeldBack>,
11018    },
11019}
11020
11021impl Deferred {
11022    /// Whether `content` is one of the items held back.
11023    fn holds(&self, content: &NativeId) -> bool {
11024        match self {
11025            Self::Nothing => false,
11026            Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
11027        }
11028    }
11029}
11030
11031/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
11032#[derive(Debug)]
11033struct HeldBack {
11034    /// The issue.
11035    content: NativeId,
11036    /// Its board field values other than its `Status`, in the order they are written.
11037    fields: Vec<(String, Value)>,
11038    /// The `Status` field, and the option and its name that the item's status lands on.
11039    // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
11040    status: Option<(String, String, String)>,
11041    /// The fields it clears after those are written.
11042    clears: Vec<String>,
11043    /// The reason it is closed for, once its option is selected; `None` leaves it open.
11044    close: Option<ClosedState>,
11045    /// The issues it is blocked by.
11046    edges: Vec<NativeId>,
11047}
11048
11049/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
11050/// no GitHub type has a field of this name.
11051const ANSWER_ERRORS: &str = "onetaskgraphErrors";
11052
11053/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
11054/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
11055///
11056/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
11057/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
11058/// created named in the data beside it — the ids a copy needs to take them back.
11059fn answer_with_errors(body: &str) -> Option<Value> {
11060    let parsed: Value = serde_json::from_str(body).ok()?;
11061    let errors = parsed
11062        .get("errors")?
11063        .as_array()
11064        .filter(|errors| !errors.is_empty())?;
11065    let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
11066    data[ANSWER_ERRORS] = Value::Array(errors.clone());
11067    Some(data)
11068}
11069
11070/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
11071/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
11072fn filed_input(
11073    repository: &str,
11074    board: &str,
11075    incoming: &Incoming<'_>,
11076    body: &Option<String>,
11077) -> Value {
11078    let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
11079                           "body":body,"projectV2Ids":[board]});
11080    if let Some(parent) = incoming.parent {
11081        input["parentIssueId"] = json!(parent.0);
11082    }
11083    input
11084}
11085
11086/// The name of option `option` of field `field` among a board's `fields`.
11087fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
11088    fields
11089        .get("nodes")?
11090        .as_array()?
11091        .iter()
11092        .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
11093        .get("options")?
11094        .as_array()?
11095        .iter()
11096        .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
11097        .get("name")?
11098        .as_str()
11099}
11100
11101/// What a write needs to know of one far end it names: which kind of item it is, and whether
11102/// it is an issue a native relationship can name.
11103struct FarEnd {
11104    kind: BoardKind,
11105    content_kind: ContentKind,
11106}
11107
11108/// Whether the issue one write reconciles was created by that write or was already there.
11109#[derive(Clone, Copy, PartialEq, Eq)]
11110enum Issue<'a> {
11111    /// Created by this write, so it holds no relationships yet.
11112    Created,
11113    /// On the board before this write, holding whatever relationships it holds — the far
11114    /// ends of its whole `blockedBy`, when the read that reached it carried them.
11115    Existing(Option<&'a [Value]>),
11116}
11117
11118/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
11119enum Reached {
11120    /// An issue this board holds, resolved into everything this source reports about it.
11121    Held(Box<Resolved>),
11122    /// Nothing this board holds: no such node, or a node on some other board.
11123    Nothing,
11124    /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
11125    /// again by [`GitHubProjectsSource::draft_by_id`].
11126    Draft,
11127}
11128
11129/// What GitHub says when a string is not a node id it can resolve.
11130///
11131/// Matched because it is the ordinary answer to a project selector naming a project by its
11132/// *name*, and reporting that as a failure would make naming one impossible. It is read
11133/// off the refusal GitHub sent, never guessed from the shape of the string: this source
11134/// does not define the syntax of a GitHub node id and would be wrong about it.
11135const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
11136
11137/// `error` with `note` added to the end of what it says, its kind and every other member
11138/// unchanged — so a caller still branches on the failure that happened, and reads beside it
11139/// what that failure left behind.
11140fn noting(error: SourceError, note: &str) -> SourceError {
11141    match error {
11142        SourceError::Config { message } => SourceError::Config {
11143            message: message + note,
11144        },
11145        SourceError::Auth { message } => SourceError::Auth {
11146            message: message + note,
11147        },
11148        SourceError::Refused { message } => SourceError::Refused {
11149            message: message + note,
11150        },
11151        SourceError::RateLimited {
11152            retry_after_seconds,
11153            message,
11154        } => SourceError::RateLimited {
11155            retry_after_seconds,
11156            message: Some(message.unwrap_or_default() + note),
11157        },
11158        SourceError::Unavailable { message } => SourceError::Unavailable {
11159            message: message + note,
11160        },
11161        SourceError::Malformed { message } => SourceError::Malformed {
11162            message: message + note,
11163        },
11164    }
11165}
11166
11167/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
11168/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
11169fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
11170    let mut variables = serde_json::Map::new();
11171    for slot in 0..COPY_DETAIL_BATCH {
11172        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11173        variables.insert(format!("id{slot}"), json!(id));
11174    }
11175    variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
11176    variables.insert("children".to_owned(), json!(children));
11177    variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
11178    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11179    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11180    variables.insert("duplicates".to_owned(), json!(true));
11181    Value::Object(variables)
11182}
11183
11184/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
11185/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
11186/// for them.
11187///
11188/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
11189/// is read again at no added price.
11190fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
11191    let mut variables = serde_json::Map::new();
11192    for slot in 0..DETAIL_BATCH {
11193        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11194        variables.insert(format!("id{slot}"), json!(id));
11195    }
11196    variables.insert(
11197        "first".to_owned(),
11198        json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
11199    );
11200    variables.insert("comments".to_owned(), json!(comments.is_some()));
11201    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11202    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11203    variables.insert("duplicates".to_owned(), json!(true));
11204    Value::Object(variables)
11205}
11206
11207/// Whether this refusal is GitHub saying the id names no node at all.
11208fn unresolvable_node(error: &SourceError) -> bool {
11209    matches!(error, SourceError::Refused { message }
11210        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
11211}
11212
11213/// One project name, as a search qualifier which filters on it at the server.
11214///
11215/// Quoted so the whole title is one phrase rather than a bag of words, with the two
11216/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
11217/// the way it documents. A title matched here is still compared for equality afterwards:
11218/// the qualifier narrows what the server sends, and this source decides what it names.
11219fn title_qualifier(name: &str) -> String {
11220    format!("in:title {}", quoted(name))
11221}
11222
11223/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
11224/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
11225/// it documents — so a value holding a qualifier's spelling is searched for rather than
11226/// obeyed.
11227fn quoted(value: &str) -> String {
11228    let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
11229    format!("\"{escaped}\"")
11230}
11231
11232/// The search qualifier for the issues updated at or after `since`.
11233///
11234/// Written to the second, rounded down, which can only widen what the search returns.
11235fn updated_qualifier(since: DateTime<Utc>) -> String {
11236    format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
11237}
11238
11239/// The search terms that narrow a board-scoped issue search to a task query's text and
11240/// metadata predicates, or `None` when it carries neither.
11241///
11242/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
11243/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
11244/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
11245/// matches each in any field the `in:` qualifier names, so a query naming a title search and
11246/// a metadata value searches both fields for both — wider than asked, never narrower, and
11247/// every candidate is confirmed in process afterwards.
11248///
11249/// **This narrows a text search, and that is this source's declared semantics.** GitHub
11250/// matches whole tokens where a substring rule would match inside a word, so an item holding
11251/// the text only inside a longer word is not returned. A text of nothing but whitespace
11252/// matches every item, so it narrows nothing and is not sent.
11253fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
11254    let text = query
11255        .text
11256        .as_ref()
11257        .filter(|text| !text.terms.trim().is_empty());
11258    if text.is_none() && query.metadata.is_empty() {
11259        return None;
11260    }
11261    let (title, body) = match text.map(|text| text.fields) {
11262        None => (false, true),
11263        Some(TextFields::Title) => (true, !query.metadata.is_empty()),
11264        Some(TextFields::Content) => (false, true),
11265        Some(TextFields::TitleOrContent) => (true, true),
11266    };
11267    let fields = match (title, body) {
11268        (true, true) => "in:title,body",
11269        (true, false) => "in:title",
11270        _ => "in:body",
11271    };
11272    let phrases = text
11273        .map(|text| text.terms.clone())
11274        .into_iter()
11275        .chain(
11276            query
11277                .metadata
11278                .iter()
11279                .map(|wanted| as_stored(wanted.value())),
11280        )
11281        .map(|phrase| quoted(&phrase))
11282        .collect::<Vec<_>>();
11283    Some(format!("{fields} {}", phrases.join(" ")))
11284}
11285
11286/// The search terms that narrow a board-scoped issue search to a project or document query's
11287/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
11288/// carrying that text alone is sent as by [`narrowing_qualifiers`].
11289fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
11290    narrowing_qualifiers(&TaskQuery {
11291        text: text.cloned(),
11292        ..TaskQuery::default()
11293    })
11294}
11295
11296/// Refuses a project or document query's text GitHub's issue search cannot find, before
11297/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
11298/// query's.
11299fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
11300    refuse_unsearchable(&TaskQuery {
11301        text: text.cloned(),
11302        ..TaskQuery::default()
11303    })
11304}
11305
11306/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
11307/// before anything is asked of GitHub.
11308///
11309/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
11310/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
11311/// left out, the search is every issue of the board. So this source says it cannot answer
11312/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
11313/// nothing GitHub could search for, and keeps the board read it always had.
11314fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
11315    const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
11316                       letter or digit with a bounded query";
11317    if let Some(text) = &query.text
11318        && !text.terms.trim().is_empty()
11319        && !has_words(&text.terms)
11320    {
11321        return Err(SourceError::Refused {
11322            message: format!(
11323                "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
11324                text.terms
11325            ),
11326        });
11327    }
11328    if let Some(wanted) = query
11329        .metadata
11330        .iter()
11331        .find(|wanted| !has_words(wanted.value()))
11332    {
11333        return Err(SourceError::Refused {
11334            message: format!(
11335                "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
11336                wanted.value(),
11337                std::iter::once(wanted.key())
11338                    .chain(wanted.path().iter().map(String::as_str))
11339                    .collect::<Vec<_>>()
11340                    .join("/"),
11341            ),
11342        });
11343    }
11344    Ok(())
11345}
11346
11347/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
11348fn has_words(phrase: &str) -> bool {
11349    phrase.chars().any(char::is_alphanumeric)
11350}
11351
11352/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
11353///
11354/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
11355/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
11356/// which GitHub's word match would read as different words.
11357fn as_stored(value: &str) -> String {
11358    let encoded = Value::String(value.to_owned()).to_string();
11359    encoded[1..encoded.len() - 1].to_owned()
11360}
11361
11362/// The one narrower question a task query carrying a text, metadata or origin predicate is
11363/// sent as.
11364enum Narrowing {
11365    /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
11366    Origin(String),
11367    /// The board-scoped issue search narrowed by these qualifiers.
11368    Search(String),
11369}
11370
11371impl Narrowing {
11372    /// What this question is remembered under for the length of one command.
11373    fn key(&self) -> String {
11374        match self {
11375            Self::Origin(origin) => format!("origin {origin}"),
11376            Self::Search(also) => format!("search {also}"),
11377        }
11378    }
11379}
11380
11381/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
11382enum Resumed {
11383    /// It reported another page, which starts after this cursor.
11384    More(String),
11385    /// It has ended. Sending this cursor again — the page's own end when it had one, and
11386    /// otherwise the cursor it was reached from — answers an empty page, so the one document
11387    /// can go on walking the other connection.
11388    Ended(Option<String>),
11389}
11390
11391impl Resumed {
11392    /// Whether the connection has another page.
11393    const fn has_more(&self) -> bool {
11394        matches!(self, Self::More(_))
11395    }
11396
11397    /// The cursor to send this connection next.
11398    fn cursor(self) -> Option<String> {
11399        match self {
11400            Self::More(next) => Some(next),
11401            Self::Ended(last) => last,
11402        }
11403    }
11404}
11405
11406/// Where `connection`, reached from `after`, resumes — refused when it reports another page
11407/// with no cursor to it, or from a cursor that does not advance.
11408fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
11409    let info = connection
11410        .get("pageInfo")
11411        .ok_or_else(|| SourceError::Malformed {
11412            message: "GitHub connection has no pageInfo".into(),
11413        })?;
11414    let end = optional_str(info, "endCursor")?;
11415    if required_bool(info, "hasNextPage")? {
11416        let next = end.ok_or_else(|| SourceError::Malformed {
11417            message: "GitHub connection reports another page and no endCursor".into(),
11418        })?;
11419        validate_cursor_progress(after, next)?;
11420        return Ok(Resumed::More(next.to_owned()));
11421    }
11422    Ok(Resumed::Ended(
11423        end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
11424    ))
11425}
11426
11427/// The board, and every item on it this source reports.
11428#[derive(Clone)]
11429struct Board {
11430    id: String,
11431    fields: Value,
11432    items: Vec<Resolved>,
11433}
11434
11435/// What a write needs of the board and nothing more: its node id and its field
11436/// definitions, in the shape a read of the board's own `fields` gives them.
11437///
11438/// Deliberately no items. A write decides which item it writes, which parent it files
11439/// under and which far ends it names by reading each of them by its own id; this is the
11440/// half of the board those reads cannot carry, and holding no item is what keeps it from
11441/// ever being asked whether an item is there.
11442#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
11443struct BoardFields {
11444    id: BoardId,
11445    fields: Value,
11446}
11447
11448/// A board's node id: what a field write and `addProjectV2ItemById` address.
11449///
11450/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
11451/// refused where it is read, and one an item names blank is read as not named at all.
11452#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
11453#[serde(try_from = "String")]
11454struct BoardId(String);
11455
11456impl TryFrom<String> for BoardId {
11457    type Error = String;
11458
11459    /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
11460    fn try_from(id: String) -> Result<Self, Self::Error> {
11461        Self::parse(&id).map_err(|error| error.to_string())
11462    }
11463}
11464
11465/// Where one write left its item, for the record the rest of the command reads it out of.
11466///
11467/// A named record rather than a tuple because the update arm and the create arm each fill
11468/// all four, and two `Option`s of different meaning side by side in a tuple are two
11469/// positions a reader has to count.
11470struct Landed {
11471    /// The issue's own node id, which is the [`NativeId`] this source reports.
11472    content_id: NativeId,
11473    /// The board item's id, which is what a field write addresses — `None` for an issue its
11474    /// create filed on the board itself, whose board item GitHub adds after answering and
11475    /// [`TaskSource::finish_writes`] reads.
11476    // 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.
11477    item_id: Option<String>,
11478    /// The web address GitHub gave the issue, when it gave one.
11479    // 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.
11480    url: Option<String>,
11481    /// The issue's number on its repository, when GitHub reported one.
11482    number: Option<u64>,
11483}
11484
11485impl BoardId {
11486    fn parse(id: &str) -> Result<Self, SourceError> {
11487        if id.trim().is_empty() {
11488            return Err(SourceError::Malformed {
11489                message: "GitHub named a board with a blank node id".into(),
11490            });
11491        }
11492        Ok(Self(id.to_owned()))
11493    }
11494
11495    fn as_str(&self) -> &str {
11496        &self.0
11497    }
11498}
11499
11500impl Board {
11501    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
11502        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
11503        let nodes = fields
11504            .get("nodes")
11505            .and_then(Value::as_array)
11506            .ok_or_else(|| SourceError::Malformed {
11507                message: "GitHub project fields.nodes is not an array".into(),
11508            })?;
11509        Ok(nodes
11510            .iter()
11511            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
11512    }
11513}
11514
11515/// One board item, resolved into everything this source reports about it.
11516///
11517/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
11518/// [`TaskSource::prime_write`] takes back — see [`expected`].
11519#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
11520struct Resolved {
11521    item_id: String,
11522    id: NativeId,
11523    content_kind: ContentKind,
11524    kind: BoardKind,
11525    title: String,
11526    body: Option<String>,
11527    /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
11528    /// that changes the slot alone has to keep byte for byte outside it.
11529    raw_body: Option<String>,
11530    status: Status,
11531    /// The name of the board `Status` option this item sits in, as the board spells it.
11532    option: Option<String>,
11533    /// What its `Priority` field says, read through this instance's mapping.
11534    priority: HeldPriority,
11535    /// Whether this item's issue is closed. A draft has no such state and is never closed.
11536    closed: bool,
11537    /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
11538    /// issue, a draft, and an issue closed before GitHub recorded reasons.
11539    #[serde(default)]
11540    reason: Option<String>,
11541    /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
11542    delivers: Vec<TaskRef>,
11543    /// Every task that delivers this one, read out of its slot. Empty for anything not a
11544    /// task.
11545    delivered_by: Vec<TaskRef>,
11546    labels: Vec<Label>,
11547    parent: Option<NativeId>,
11548    // 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.
11549    origin: Option<String>,
11550    /// What each board text field this instance projects metadata onto holds for this item,
11551    /// by field name — only the fields holding a value, an empty text included. What a write compares the
11552    /// item's metadata against, so a field already holding its value is not written again;
11553    /// never read back as metadata, which the slot alone reports.
11554    projected: BTreeMap<String, String>,
11555    /// The issue's own number on its repository, as GitHub reports it.
11556    ///
11557    /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
11558    /// declares none, and a draft is not filed in a repository to be numbered by one — and
11559    /// an issue this run created whose creating mutation answered without one, which is a
11560    /// response GitHub's own schema says cannot happen and which a landed write is not
11561    /// worth failing over. An `Issue` read off the board always has one.
11562    number: Option<u64>,
11563    url: Option<String>,
11564    created_at: Option<DateTime<Utc>>,
11565    updated_at: Option<DateTime<Utc>>,
11566    /// When the board item itself last changed — what a board field write moves — when the
11567    /// read that reached it said.
11568    item_updated_at: Option<DateTime<Utc>>,
11569    /// How many comments the issue holds, when the read that reached it said.
11570    comment_count: Option<u64>,
11571    /// How many relationship events the issue's timeline holds — every dependency, as
11572    /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
11573    /// removed — when the read that reached it said.
11574    relationship_events: Option<u64>,
11575    /// The server's clock on the response that read it — its `Date` header — when the
11576    /// response carried one.
11577    served_at: Option<DateTime<Utc>>,
11578    /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
11579    /// its labels — when the read that reached it carried all three whole.
11580    #[serde(default)]
11581    facets: Option<change_token::FacetsDigest>,
11582    own_repository: Option<Repository>,
11583    repositories: Vec<Repository>,
11584    classification: Classification,
11585    slot: BTreeMap<String, Value>,
11586    /// The node id of the board this item sits on, when the read that reached it said.
11587    board_id: Option<String>,
11588    /// The definition of every board field this item holds a value of, in the shape a read
11589    /// of the board's own `fields` gives one.
11590    ///
11591    /// Only the fields this item has a value in: a field it holds nothing of is not here,
11592    /// which says nothing about whether the board has it.
11593    fields: Vec<Value>,
11594    /// Every field the board this item sits on defines, as its own read of the board's
11595    /// `fields` gives them — when the read that reached the item carried them, which a read
11596    /// of it by its own id does. What a write of it needs of the board, then, needs no read
11597    /// of the board.
11598    board_fields: Option<Value>,
11599    /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
11600    /// selects one — when the read that reached it carried the connection to its end, which a
11601    /// read of it by its own id does for any issue blocked by no more than a page. What a
11602    /// write reconciles that relationship against, and what a read of its forward edges in
11603    /// the same command answers with.
11604    blocked_by: Option<Vec<Value>>,
11605}
11606
11607impl Resolved {
11608    /// The board this item's own read names it on, when that read named one this source can
11609    /// address.
11610    fn named_board(&self) -> Option<BoardId> {
11611        self.board_id
11612            .as_deref()
11613            .and_then(|id| BoardId::parse(id).ok())
11614    }
11615
11616    /// The board's id and every field it defines, when the read that reached this item
11617    /// carried both — which a read of it by its own id does.
11618    fn carried_board(&self) -> Option<BoardFields> {
11619        Some(BoardFields {
11620            id: self.named_board()?,
11621            fields: self.board_fields.clone()?,
11622        })
11623    }
11624
11625    /// Whether this item holds a value of the board field called `name`, and so carries
11626    /// that field's definition. `false` says nothing about whether the board has the field.
11627    fn defines(&self, name: &str) -> bool {
11628        self.fields
11629            .iter()
11630            .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
11631    }
11632
11633    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
11634    /// in a field of its own, and none of the five keys that are only an encoding.
11635    ///
11636    /// The two delivery keys are left out for every kind, not only for a task: they are
11637    /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
11638    /// document carrying one holds nothing a caller's own metadata could mean by it.
11639    fn metadata(&self) -> BTreeMap<String, Value> {
11640        let mut metadata = self.slot.clone();
11641        metadata.remove(Repository::METADATA_KEY);
11642        metadata.remove(DependencyEdge::RECORDED_KEY);
11643        metadata.remove(ItemKind::METADATA_KEY);
11644        metadata.remove(TaskRef::DELIVERS_KEY);
11645        metadata.remove(TaskRef::DELIVERED_BY_KEY);
11646        metadata.remove(Classification::METADATA_KEY);
11647        // The board field is the origin, and the body's copy of it is only a mirror for the
11648        // issue search to find: an item whose field holds none has none, whatever its body
11649        // says, so no reader ever sees two answers.
11650        metadata.remove(ORIGIN_KEY);
11651        if let Some(origin) = &self.origin {
11652            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
11653        }
11654        metadata
11655    }
11656
11657    /// Where this item is, as a link a reader can open.
11658    ///
11659    /// A board is a hosted place and every issue on it has a web address, so that address
11660    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
11661    /// of place it is, so a reader knows to open it rather than to read a file out. It
11662    /// does not replace or derive from `url`: the field goes on reporting exactly what it
11663    /// reported before, and this says what that address *is*.
11664    ///
11665    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
11666    /// rather than a third variant, which is the contract's "the source did not say". An
11667    /// issue this run created is not one of those: its address comes back from the
11668    /// creating mutation, so it is somewhere a reader can open from the moment it exists
11669    /// rather than from whenever the board read catches up.
11670    fn location(&self) -> Option<Location> {
11671        self.url.clone().map(Location::Url)
11672    }
11673
11674    /// The short handle this board's backend shows people for a task: the issue's number
11675    /// alone, as a decimal string.
11676    ///
11677    /// The number alone rather than `owner/repo#1043`, because that is the contract's
11678    /// value for this backend. A draft has no number and so no handle, which is the
11679    /// contract's *absent* rather than a handle of some other shape — and the native
11680    /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
11681    /// derives from.
11682    fn key(&self) -> Option<String> {
11683        self.number.map(|number| number.to_string())
11684    }
11685
11686    /// This item's change token — see [`change_token`] — or `None` when the read that
11687    /// reached it did not carry all six of its parts, which a draft never does, did not say
11688    /// when GitHub served it, or found it changed too recently to have settled.
11689    fn version(&self) -> Option<String> {
11690        match self.content_kind {
11691            ContentKind::Issue => {
11692                let (issue, item) = (self.updated_at?, self.item_updated_at?);
11693                let parts = change_token::Parts {
11694                    issue,
11695                    item,
11696                    comments: self.comment_count?,
11697                    relationships: self.relationship_events?,
11698                    facets: self.facets.clone()?,
11699                    repository: self.own_repository.clone()?,
11700                };
11701                change_token::settled(issue, item, self.served_at?)
11702                    .then(|| change_token::spell(&parts))
11703            }
11704            ContentKind::DraftIssue => None,
11705        }
11706    }
11707
11708    /// Whether its `Priority` field holds a value at all, mapped or not.
11709    fn holds_priority(&self) -> bool {
11710        self.priority != HeldPriority::Read(Priority::None)
11711    }
11712
11713    /// The task this item is.
11714    ///
11715    /// Fails for an item whose `Priority` field holds an option the mapping does not name:
11716    /// reading that as a level would be a guess, and reading it as `none` would let the next
11717    /// copy clear a priority a person set.
11718    fn task(&self) -> Result<Task, SourceError> {
11719        let priority = match &self.priority {
11720            HeldPriority::Read(priority) => *priority,
11721            HeldPriority::Unmapped(option) => {
11722                return Err(SourceError::Malformed {
11723                    message: format!(
11724                        "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
11725                         this source's priority_mapping does not name, so its priority cannot be \
11726                         read; next: name {option:?} under priority_mapping, or move the item to \
11727                         a mapped option",
11728                        self.id,
11729                        self.number
11730                            .map(|number| format!(" (#{number})"))
11731                            .unwrap_or_default()
11732                    ),
11733                });
11734            }
11735        };
11736        Ok(Task {
11737            version: self.version(),
11738            id: self.id.clone(),
11739            key: self.key(),
11740            title: self.title.clone(),
11741            content: self.body.clone(),
11742            status: self.status.clone(),
11743            priority,
11744            labels: self.labels.clone(),
11745            project: self.parent.clone(),
11746            url: self.url.clone(),
11747            location: self.location(),
11748            created_at: self.created_at,
11749            updated_at: self.updated_at,
11750            metadata: self.metadata(),
11751            repositories: self.repositories.clone(),
11752            delivers: self.delivers.clone(),
11753            delivered_by: self.delivered_by.clone(),
11754            classification: self.classification,
11755        })
11756    }
11757
11758    fn project(&self) -> Project {
11759        Project {
11760            version: self.version(),
11761            id: self.id.clone(),
11762            title: self.title.clone(),
11763            content: self.body.clone(),
11764            status: self.status.clone(),
11765            labels: self.labels.clone(),
11766            url: self.url.clone(),
11767            location: self.location(),
11768            created_at: self.created_at,
11769            updated_at: self.updated_at,
11770            metadata: self.metadata(),
11771            repositories: self.repositories.clone(),
11772            classification: self.classification,
11773        }
11774    }
11775
11776    /// The same issue as a document: the project it is filed under, and no status and no
11777    /// dependencies, because a document is not work.
11778    fn document(&self) -> Document {
11779        Document {
11780            version: self.version(),
11781            id: self.id.clone(),
11782            title: self.title.clone(),
11783            content: self.body.clone(),
11784            project: self.parent.clone(),
11785            labels: self.labels.clone(),
11786            url: self.url.clone(),
11787            location: self.location(),
11788            created_at: self.created_at,
11789            updated_at: self.updated_at,
11790            metadata: self.metadata(),
11791            repositories: self.repositories.clone(),
11792            classification: self.classification,
11793        }
11794    }
11795}
11796
11797/// Where one targeted update moves an item's status, and which of its two halves move.
11798struct StatusMove {
11799    /// The board the item's `Status` field is on.
11800    board: BoardId,
11801    /// The `Status` field's id.
11802    field: String,
11803    /// The option's id.
11804    option: String,
11805    /// The option's name, as the board spells it.
11806    name: String,
11807    /// What the status asks of the issue's state.
11808    target: StatusTarget,
11809    /// The status the item reads as once it is there.
11810    landed: Status,
11811    /// Which of the status's two halves differ from what the item holds.
11812    moves: Moves,
11813}
11814
11815/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
11816/// closed state of its issue, or both. A status neither half of which differs is no move at all,
11817/// and is not a value of this type.
11818#[derive(Clone, Copy, PartialEq, Eq)]
11819enum Moves {
11820    /// The option alone.
11821    Option,
11822    /// The issue's state alone: open, closed, or closed with another reason.
11823    State,
11824    /// Both.
11825    Both,
11826}
11827
11828impl Moves {
11829    /// What differs, or `None` when nothing does.
11830    const fn of(option: bool, state: bool) -> Option<Self> {
11831        match (option, state) {
11832            (true, true) => Some(Self::Both),
11833            (true, false) => Some(Self::Option),
11834            (false, true) => Some(Self::State),
11835            (false, false) => None,
11836        }
11837    }
11838
11839    /// Whether the option moves.
11840    const fn option(self) -> bool {
11841        matches!(self, Self::Option | Self::Both)
11842    }
11843
11844    /// Whether the issue's state moves.
11845    const fn state(self) -> bool {
11846        matches!(self, Self::State | Self::Both)
11847    }
11848}
11849
11850/// What one write is, and the status that comes with being it.
11851///
11852/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
11853/// status and a task or a project always has one, so "a document carrying a status" and
11854/// "a task carrying none" are states a write cannot be in rather than states every use
11855/// site below has to defend against.
11856enum Written<'a> {
11857    /// A document, which is not work and so has no status at all.
11858    Document,
11859    /// A task or a project, and the status it is being written with.
11860    Work(ItemKind, &'a Status),
11861}
11862
11863impl Written<'_> {
11864    /// Which of the board's three kinds this write is.
11865    const fn kind(&self) -> BoardKind {
11866        match self {
11867            Self::Document => BoardKind::Document,
11868            Self::Work(kind, _) => BoardKind::Work(*kind),
11869        }
11870    }
11871
11872    /// The status this write carries. A document carries none, so a write of one says
11873    /// nothing about the issue's open or closed state and selects no board `Status`
11874    /// option.
11875    const fn status(&self) -> Option<&Status> {
11876        match self {
11877            Self::Document => None,
11878            Self::Work(_, status) => Some(status),
11879        }
11880    }
11881
11882    /// The status this write carries with the kind whose half of `status_mapping` it is
11883    /// written through.
11884    const fn work_status(&self) -> Option<(ItemKind, &Status)> {
11885        match self {
11886            Self::Document => None,
11887            Self::Work(kind, status) => Some((*kind, status)),
11888        }
11889    }
11890}
11891
11892/// The item being written, in the one shape all three write methods reach.
11893struct Incoming<'a> {
11894    written: Written<'a>,
11895    /// The title a person wrote. A document's goes onto the issue with
11896    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
11897    title: &'a str,
11898    content: Option<&'a str>,
11899    assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
11900    labels: &'a [Label],
11901    metadata: &'a BTreeMap<String, Value>,
11902    repositories: &'a [Repository],
11903    /// Recorded in the slot while private, and nowhere while public.
11904    classification: Classification,
11905    parent: Option<&'a NativeId>,
11906    /// [`Task::delivers`], already checked. Empty for a project or a document, which is
11907    /// what keeps either key out of their slot.
11908    delivers: &'a [TaskRef],
11909    /// [`Task::delivered_by`], already checked. Empty for a project or a document.
11910    delivered_by: &'a [TaskRef],
11911    /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
11912    /// project, a document, and every write to an instance with no `priority_mapping` —
11913    /// which is what keeps such a write's requests exactly what they were before.
11914    priority: Option<Priority>,
11915}
11916
11917/// What one write does to one projected metadata text field, by the field's id.
11918#[derive(Debug, Clone)]
11919enum ProjectionWrite {
11920    /// Write this text.
11921    Set {
11922        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.
11923        text: String,
11924    },
11925    /// Clear the field's value.
11926    Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
11927}
11928
11929impl ProjectionWrite {
11930    /// Add this write to a field write's list of values and list of clears.
11931    fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
11932        match self {
11933            Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
11934            Self::Clear { field } => clears.push(field),
11935        }
11936    }
11937}
11938
11939/// What one write does to an item's `Priority` field.
11940enum PriorityWrite {
11941    /// Select this option of this field.
11942    Select {
11943        /// The `Priority` field's id.
11944        field: String,
11945        /// The mapped option's id.
11946        option: String,
11947    },
11948    /// Clear the field's value, which is what `none` is.
11949    Clear {
11950        /// The `Priority` field's id.
11951        field: String,
11952    },
11953}
11954
11955impl Incoming<'_> {
11956    /// The title this write puts on the issue.
11957    fn written_title(&self) -> String {
11958        match self.written {
11959            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
11960            Written::Work(..) => self.title.to_owned(),
11961        }
11962    }
11963}
11964
11965#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
11966enum ContentKind {
11967    DraftIssue,
11968    Issue,
11969}
11970
11971/// What one board issue is: a document, or the work an [`ItemKind`] names.
11972///
11973/// A type of this source's own rather than an `ItemKind` with a third variant, because
11974/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
11975/// document — the contract keeps a document out of that enum deliberately. Holding the
11976/// board's three answers in one value is what makes every place that asks "which is this?"
11977/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
11978/// two thirds of the board.
11979#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
11980enum BoardKind {
11981    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
11982    Document,
11983    /// Every other issue, and every draft.
11984    Work(ItemKind),
11985}
11986
11987impl BoardKind {
11988    /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
11989    /// sub-issues and carrying the kind marker `marked` in its slot.
11990    ///
11991    /// The design prefix is read *first*, before either of the two rules that separate a
11992    /// project from a task. A document is not work whatever sub-issues it has and whatever
11993    /// marker it carries, and reading the prefix later would make a design issue with none of
11994    /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
11995    /// overrides it: an issue filed under a project is that project's task even when it has
11996    /// sub-issues of its own.
11997    fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
11998        if title.starts_with(DESIGN_TITLE_PREFIX) {
11999            Self::Document
12000        } else if filed {
12001            Self::Work(ItemKind::Task)
12002        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
12003            Self::Work(ItemKind::Project)
12004        } else {
12005            Self::Work(ItemKind::Task)
12006        }
12007    }
12008
12009    /// What the change token's facets call this kind.
12010    const fn facet(self) -> change_token::FacetKind {
12011        match self {
12012            Self::Document => change_token::FacetKind::Document,
12013            Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
12014            Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
12015        }
12016    }
12017
12018    /// Whose half of `status_mapping` an item of this kind reads its status through. A
12019    /// document has no status of its own, so the task half stands in for whatever the issue
12020    /// holds; nothing reports it.
12021    const fn status_kind(self) -> ItemKind {
12022        match self {
12023            Self::Document => ItemKind::Task,
12024            Self::Work(kind) => kind,
12025        }
12026    }
12027
12028    /// How a refusal names this kind to the person reading it.
12029    const fn describes(self) -> &'static str {
12030        match self {
12031            Self::Document => "document",
12032            Self::Work(kind) => kind.marker(),
12033        }
12034    }
12035}
12036
12037/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
12038///
12039/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
12040/// the shared cross-source journeys assert one answer to one question, so two sources
12041/// that disagree about what "carries the label bug" means fail them.
12042fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
12043    let holds = |name: &String| {
12044        labels
12045            .iter()
12046            .any(|label| label.name.eq_ignore_ascii_case(name))
12047    };
12048    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
12049        && filter.all_of.iter().all(holds)
12050        && !filter.none_of.iter().any(holds)
12051}
12052
12053/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
12054/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
12055fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
12056    statuses.is_empty() || statuses.contains(&category)
12057}
12058
12059/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
12060///
12061/// `content` is the item's own prose — the body with this source's trailing metadata
12062/// comment already taken off — so a search never matches an encoding the author of the
12063/// issue never wrote.
12064fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
12065    let terms = query.terms.to_lowercase();
12066    let in_title = title.to_lowercase().contains(&terms);
12067    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
12068    match query.fields {
12069        TextFields::Title => in_title,
12070        TextFields::Content => in_content,
12071        TextFields::TitleOrContent => in_title || in_content,
12072    }
12073}
12074
12075/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
12076///
12077/// The project predicate is passed separately because a read narrowed to one project has
12078/// already answered it by asking *that project* for its own items — and re-applying it
12079/// there would compare the caller's selector, which may be a project's **name**, against
12080/// the id of the project that name resolved to, and keep nothing. Every other read passes
12081/// `query.project` and applies it here, which is what keeps `projects` a predicate this
12082/// source really does apply.
12083fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
12084    labels_match(&task.labels, &query.labels)
12085        && status_matches(task.status.category, &query.statuses)
12086        && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
12087        && match project {
12088            ProjectFilter::Any => true,
12089            ProjectFilter::Orphans => task.project.is_none(),
12090            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
12091        }
12092        && query
12093            .text
12094            .as_ref()
12095            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
12096        // Against the parsed metadata slot, and against the origin field, which is where
12097        // `Resolved::metadata` reads each of them from.
12098        && query.metadata_matches(&task.metadata)
12099        && query.origin_matches(&task.metadata)
12100}
12101
12102fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
12103    labels_match(&project.labels, &query.labels)
12104        && status_matches(project.status.category, &query.statuses)
12105        && query
12106            .text
12107            .as_ref()
12108            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
12109}
12110
12111/// The same three predicates a task query carries, minus the status filter.
12112///
12113/// A document is not work, so it has no status for one to compare against and the query
12114/// type carries none. The project predicate is the same one — a design issue filed under a
12115/// project issue is in that project, and one filed under nothing is in none — so it is
12116/// spelled the same way here rather than answered differently.
12117fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
12118    labels_match(&document.labels, &query.labels)
12119        && match project {
12120            ProjectFilter::Any => true,
12121            ProjectFilter::Orphans => document.project.is_none(),
12122            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
12123        }
12124        && query
12125            .text
12126            .as_ref()
12127            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
12128}
12129
12130/// Why a GitHub item's change token does not vouch for its forward edges.
12131///
12132/// A forward edge names its far end and that far end's kind, and the near item's token moves
12133/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
12134/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
12135/// nothing the near token reads, so an edge kept at the near version could name a far end that
12136/// is no longer what it said.
12137const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
12138     whether it became a document, or its deletion, so its forward edges are always read from \
12139     github";
12140
12141/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
12142/// categories an open issue reads as narrows a board-scoped search with.
12143const OPEN_QUALIFIER: &str = "is:open";
12144
12145/// The board filter `task status reconcile` reads the board through: closed items alone.
12146const CLOSED_FILTER: &str = "is:closed";
12147
12148/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
12149/// one reference, and a slot the batch has none for names its last one again.
12150fn reference_batch(batch: &[IssueRef]) -> Value {
12151    let mut variables = serde_json::Map::new();
12152    for slot in 0..REFERENCE_BATCH {
12153        let issue = batch.get(slot).or(batch.last());
12154        variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
12155        variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
12156        variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
12157    }
12158    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12159    variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
12160    variables.insert("duplicates".to_owned(), json!(true));
12161    Value::Object(variables)
12162}
12163
12164/// One issue an issue read by reference answered, in the shape every other read of an issue
12165/// hands its resolver: each board membership's `Status` value, read by name, as the one field
12166/// value that membership carries.
12167///
12168/// A membership's `status` is `null` when its card sits at no option — or on a board with no
12169/// field of that name — and otherwise an option value naming its option; anything else is not
12170/// an answer to this read, and is refused rather than read as a card at no option.
12171fn held_issue(issue: &Value) -> Result<Value, SourceError> {
12172    let malformed = |what: &str| SourceError::Malformed {
12173        message: format!("GitHub answered an issue read by reference with {what}"),
12174    };
12175    let mut issue = issue.clone();
12176    let nodes = issue
12177        .pointer_mut("/projectItems/nodes")
12178        .and_then(Value::as_array_mut)
12179        .ok_or_else(|| malformed("no list of board memberships"))?;
12180    for node in nodes {
12181        let node = node
12182            .as_object_mut()
12183            .ok_or_else(|| malformed("a board membership that is not an object"))?;
12184        let status = match node.get("status") {
12185            None => return Err(malformed("a board membership with no status member")),
12186            Some(Value::Null) => None,
12187            Some(status) if status.get("name").is_some_and(Value::is_string) => {
12188                Some(status.clone())
12189            }
12190            // A single-select value of another field type answers `{}`; only an option value
12191            // carries a name.
12192            Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
12193            Some(_) => return Err(malformed("a membership status that names no option")),
12194        };
12195        node.insert(
12196            "fieldValues".to_owned(),
12197            json!({"nodes": status.into_iter().collect::<Vec<_>>(),
12198                   "pageInfo": {"hasNextPage": false}}),
12199        );
12200    }
12201    Ok(issue)
12202}
12203
12204/// The issue one slot of a read by reference answered, `None` when GitHub answered that
12205/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
12206/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
12207/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
12208/// than a reference nothing holds.
12209fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
12210    let alias = format!("r{slot}");
12211    let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
12212        message: format!("GitHub answered a read by reference with no {alias}"),
12213    })?;
12214    if repository.is_null() {
12215        return Ok(None);
12216    }
12217    let issue = repository
12218        .get("issue")
12219        .ok_or_else(|| SourceError::Malformed {
12220            message: format!("GitHub answered {alias} of a read by reference with no issue"),
12221        })?;
12222    if issue.is_null() {
12223        return Ok(None);
12224    }
12225    // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
12226    if optional_str(issue, "__typename")? != Some("Issue") {
12227        return Err(SourceError::Malformed {
12228            message: format!(
12229                "GitHub answered {alias} of a read by reference with an issue whose __typename \
12230                 is not Issue"
12231            ),
12232        });
12233    }
12234    Ok(Some(issue))
12235}
12236
12237/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
12238fn spoken_reason(reason: &str) -> String {
12239    reason.to_ascii_lowercase().replace('_', " ")
12240}
12241
12242impl GitHubProjectsSource {
12243    /// Every task and project of this board whose card contradicts its issue's closure, each
12244    /// judged against its own kind's half of `status_mapping`, read through the board's own
12245    /// `is:closed` filter, page by page — never the board's unfiltered `items`.
12246    ///
12247    /// Each item the filter answers is confirmed from its own node: an open one, one whose
12248    /// closure reads `unknown`, and one whose card is already in line are left out whatever
12249    /// the filter let through. The board's fields ride along and are held for the option
12250    /// writes that follow, so those need no read of the board.
12251    async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
12252        let mut after: Option<String> = None;
12253        let mut found = Vec::new();
12254        loop {
12255            let data = self
12256                .graphql(
12257                    graphql::CLOSED_ITEMS,
12258                    json!({"owner":self.owner,"number":self.project_number,
12259                           "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
12260                           "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
12261                )
12262                .await?;
12263            let board = data
12264                .pointer("/closedItems/projectV2")
12265                .filter(|value| !value.is_null())
12266                .ok_or_else(|| SourceError::Refused {
12267                    message: format!(
12268                        "GitHub project {}/{} was not found or is not visible to the token",
12269                        self.owner, self.project_number
12270                    ),
12271                })?;
12272            *self.fields_cache()? = Some(BoardFields {
12273                id: BoardId::parse(required_str(board, "id")?)?,
12274                fields: board.get("fields").cloned().unwrap_or(Value::Null),
12275            });
12276            let items = board.get("items").ok_or_else(|| SourceError::Malformed {
12277                message: "GitHub project closed items are missing".into(),
12278            })?;
12279            for item in items
12280                .get("nodes")
12281                .and_then(Value::as_array)
12282                .ok_or_else(|| SourceError::Malformed {
12283                    message: "GitHub project items.nodes is not an array".into(),
12284                })?
12285            {
12286                let Some(resolved) = self.resolve(item)? else {
12287                    continue;
12288                };
12289                let BoardKind::Work(kind) = resolved.kind else {
12290                    continue;
12291                };
12292                let Some((closed_as, name)) = self.statuses.contradicted(
12293                    kind,
12294                    resolved.option.as_deref(),
12295                    resolved.closed,
12296                    resolved.reason.as_deref(),
12297                ) else {
12298                    continue;
12299                };
12300                found.push(StatusContradiction {
12301                    closed_as,
12302                    to: name.to_owned(),
12303                    option: resolved.option.clone(),
12304                    item: match kind {
12305                        ItemKind::Task => ContradictedItem::Task(resolved.task()?),
12306                        ItemKind::Project => ContradictedItem::Project(resolved.project()),
12307                    },
12308                });
12309            }
12310            let info = items
12311                .get("pageInfo")
12312                .ok_or_else(|| SourceError::Malformed {
12313                    message: "GitHub project items have no pageInfo".into(),
12314                })?;
12315            if !required_bool(info, "hasNextPage")? {
12316                return Ok(found);
12317            }
12318            let next = required_str(info, "endCursor")?;
12319            validate_cursor_progress(after.as_deref(), next)?;
12320            after = Some(next.to_owned());
12321        }
12322    }
12323
12324    /// What this board holds for each issue in `issues`, once each in the order given, read
12325    /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
12326    ///
12327    /// An issue GitHub cannot find for this source's credential, and one with no item on this
12328    /// board, are both absent. A closure that already says the work will not be done here —
12329    /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
12330    /// completed whose card sits at its kind's `done` option is unchanged, and every other item
12331    /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
12332    /// tasks as sub-issues — is held under exactly these rules, judged against the project
12333    /// kind's half of `status_mapping`.
12334    async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
12335        let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
12336        for issue in issues {
12337            if !distinct.contains(issue) {
12338                distinct.push(issue.clone());
12339            }
12340        }
12341        let done = |kind: ItemKind| {
12342            self.statuses
12343                .target(kind, StatusCategory::Done)
12344                .option()
12345                .unwrap_or("Done")
12346                .to_owned()
12347        };
12348        // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
12349        // its range names no issue any repository can hold: it is answered absent and never sent,
12350        // where binding it would have GitHub refuse the whole batch.
12351        let readable: Vec<IssueRef> = distinct
12352            .iter()
12353            .filter(|issue| i32::try_from(issue.number).is_ok())
12354            .cloned()
12355            .collect();
12356        let mut holdings = Vec::with_capacity(readable.len());
12357        for batch in readable.chunks(REFERENCE_BATCH) {
12358            let data = self
12359                .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
12360                .await?;
12361            for (slot, issue) in batch.iter().enumerate() {
12362                let node = referenced_issue(&data, slot)?;
12363                let resolved = match node {
12364                    Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
12365                    None => None,
12366                };
12367                // A task and a project are both held; a document is not work, so nothing is.
12368                let Some((item, kind)) = resolved.and_then(|item| match item.kind {
12369                    BoardKind::Work(kind) => Some((item, kind)),
12370                    BoardKind::Document => None,
12371                }) else {
12372                    holdings.push(IssueHolding {
12373                        issue: issue.clone(),
12374                        completion: Completion::Absent,
12375                    });
12376                    continue;
12377                };
12378                let task = match kind {
12379                    ItemKind::Task => {
12380                        let held = item.task()?;
12381                        HeldTask {
12382                            id: held.id,
12383                            kind,
12384                            option: item.option.clone(),
12385                            classification: held.classification,
12386                            repositories: held.repositories,
12387                            delivers: held.delivers,
12388                        }
12389                    }
12390                    // A project delivers no task, so what its write keeps in step is nothing.
12391                    ItemKind::Project => {
12392                        let held = item.project();
12393                        HeldTask {
12394                            id: held.id,
12395                            kind,
12396                            option: item.option.clone(),
12397                            classification: held.classification,
12398                            repositories: held.repositories,
12399                            delivers: Vec::new(),
12400                        }
12401                    }
12402                };
12403                let done = done(kind);
12404                let completion = match (item.closed, item.reason.as_deref()) {
12405                    (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
12406                        task,
12407                        reason: format!("closed as {}", spoken_reason(reason)),
12408                    },
12409                    (true, None | Some("COMPLETED"))
12410                        if item
12411                            .option
12412                            .as_deref()
12413                            .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
12414                    {
12415                        Completion::Unchanged { task }
12416                    }
12417                    _ => {
12418                        let kept = item.clone();
12419                        self.held_cache
12420                            .lock()
12421                            .map_err(|_| SourceError::Unavailable {
12422                                message: "items read by issue reference were left \
12423                                          inconsistent; run the command again"
12424                                    .into(),
12425                            })?
12426                            .insert(kept.id.clone(), kept);
12427                        Completion::Due {
12428                            task,
12429                            to: done.clone(),
12430                        }
12431                    }
12432                };
12433                holdings.push(IssueHolding {
12434                    issue: issue.clone(),
12435                    completion,
12436                });
12437            }
12438        }
12439        let mut read = holdings.into_iter();
12440        let mut answered = Vec::with_capacity(distinct.len());
12441        for issue in distinct {
12442            if i32::try_from(issue.number).is_ok() {
12443                answered.push(read.next().ok_or_else(|| SourceError::Malformed {
12444                    message: format!("no holding was read for {issue}"),
12445                })?);
12446            } else {
12447                answered.push(IssueHolding {
12448                    issue,
12449                    completion: Completion::Absent,
12450                });
12451            }
12452        }
12453        Ok(answered)
12454    }
12455}
12456
12457#[async_trait::async_trait]
12458impl TaskSource for GitHubProjectsSource {
12459    fn kind(&self) -> &'static str {
12460        KIND
12461    }
12462    fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
12463        change_token::order(a, b)
12464    }
12465    /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
12466    /// member, the change each part of the token moves for. Not its forward edges — see
12467    /// [`FAR_ENDS_UNCOVERED`].
12468    fn version_coverage(&self) -> VersionCoverage {
12469        VersionCoverage {
12470            forward_edges: Coverage::Uncovered {
12471                reason: FAR_ENDS_UNCOVERED.to_owned(),
12472            },
12473            ..VersionCoverage::whole()
12474        }
12475    }
12476    /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
12477    /// and for a far end deleted, so the token sees every change to a task's edges but one — a
12478    /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
12479    /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
12480    /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
12481    // 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.
12482    fn copy_version_coverage(&self) -> VersionCoverage {
12483        VersionCoverage::whole()
12484    }
12485    /// A project's fields, and not its forward edges, for the same reason a task's are not.
12486    fn project_version_coverage(&self) -> ProjectVersionCoverage {
12487        ProjectVersionCoverage {
12488            fields: Coverage::Covered,
12489            forward_edges: Coverage::Uncovered {
12490                reason: FAR_ENDS_UNCOVERED.to_owned(),
12491            },
12492        }
12493    }
12494    /// A document's fields and the manifest of its image assets, which its metadata slot
12495    /// records: both are the issue's body, whose every edit moves the token's issue time.
12496    fn document_version_coverage(&self) -> DocumentVersionCoverage {
12497        DocumentVersionCoverage::whole()
12498    }
12499    fn write_context(&self, id: &NativeId) -> Option<Value> {
12500        self.context_of(id)
12501    }
12502    fn board_context(&self) -> Option<Value> {
12503        self.board_context_held()
12504    }
12505    fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
12506        self.prime(id, context, board)
12507    }
12508    fn unprime(&self, id: &NativeId) {
12509        self.unprime_held(id);
12510    }
12511    fn begin_journal(&self) {
12512        self.expected.begin();
12513    }
12514    fn end_journal(&self) -> Option<WriteJournal> {
12515        Some(self.expected.end())
12516    }
12517    fn capabilities(&self) -> Capabilities {
12518        Capabilities {
12519            projects: Support::Native,
12520            documents: Support::Native,
12521            comments: Support::Native,
12522            assets: Support::Native,
12523            priority: if self.priorities.is_some() {
12524                Support::Native
12525            } else {
12526                Support::Unsupported
12527            },
12528            filter_by_priority: Support::Native,
12529            filter_by_comment_activity: Support::Native,
12530            filter_by_metadata: Support::Native,
12531            filter_by_origin: Support::Native,
12532            reconcile_status: Support::Native,
12533            complete_by_issue: Support::Native,
12534            orphan_tasks: Support::Native,
12535            filter_by_label: Support::Native,
12536            filter_by_status: Support::Native,
12537            search_title: Support::Native,
12538            search_content: Support::Native,
12539            task_dependencies: DependencySupport::BothDirections,
12540            project_dependencies: DependencySupport::BothDirections,
12541            max_page_size: MAX_PAGE_SIZE,
12542        }
12543    }
12544    async fn visibility(
12545        &self,
12546        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
12547    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
12548        self.write_visibility(target).await
12549    }
12550    async fn health(&self) -> Result<Health, SourceError> {
12551        let board = self.board_page(None, 1).await?;
12552        Ok(Health {
12553            reachable: true,
12554            detail: Some(format!(
12555                "reading GitHub project {}/{} ({})",
12556                self.owner,
12557                self.project_number,
12558                required_str(&board, "title")?
12559            )),
12560        })
12561    }
12562    /// The board's own node id, digested: the one request reading its fields, or none when
12563    /// this command has read them already.
12564    async fn designator(&self) -> Result<Option<Designator>, SourceError> {
12565        let board = self.board_fields().await?;
12566        Designator::of_backend(KIND, board.id.as_str())
12567            .map(Some)
12568            .map_err(|message| SourceError::Malformed { message })
12569    }
12570    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
12571        self.item_by_id(id)
12572            .await?
12573            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
12574            .map(|item| item.task())
12575            .transpose()
12576    }
12577    async fn task_assets(
12578        &self,
12579        id: &NativeId,
12580    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
12581        self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
12582    }
12583    async fn task_asset(
12584        &self,
12585        id: &NativeId,
12586        name: &onetaskgraph_plugin_api::AssetName,
12587    ) -> Result<Option<Vec<u8>>, SourceError> {
12588        self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
12589            .await
12590    }
12591    async fn set_task_rendering_with_assets(
12592        &self,
12593        id: &NativeId,
12594        content: &str,
12595        provenance: &Value,
12596        _answers: &BTreeMap<String, Value>,
12597        assets: &onetaskgraph_plugin_api::AssetWrite,
12598    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
12599        self.replace_rendering(
12600            id,
12601            BoardKind::Work(ItemKind::Task),
12602            content,
12603            provenance,
12604            Some(assets),
12605        )
12606        .await
12607    }
12608    async fn document_assets(
12609        &self,
12610        id: &NativeId,
12611    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
12612        self.held_assets(id, BoardKind::Document).await
12613    }
12614    async fn document_asset(
12615        &self,
12616        id: &NativeId,
12617        name: &onetaskgraph_plugin_api::AssetName,
12618    ) -> Result<Option<Vec<u8>>, SourceError> {
12619        self.held_asset(id, BoardKind::Document, name).await
12620    }
12621    async fn set_document_rendering_with_assets(
12622        &self,
12623        id: &NativeId,
12624        content: &str,
12625        provenance: &Value,
12626        _answers: &BTreeMap<String, Value>,
12627        assets: &onetaskgraph_plugin_api::AssetWrite,
12628    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
12629        self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
12630            .await
12631    }
12632    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
12633        Ok(self
12634            .item_by_id(id)
12635            .await?
12636            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
12637            .map(|item| item.project()))
12638    }
12639    async fn query_tasks(
12640        &self,
12641        query: &TaskQuery,
12642        page: &PageRequest,
12643    ) -> Result<Page<Task>, SourceError> {
12644        validate_page(page)?;
12645        refuse_unsearchable(query)?;
12646        // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
12647        // category — `unknown`; every other category reads only off an open issue or a draft. So
12648        // a status filter naming none of those three asks every issue search it sends for open
12649        // issues alone; each candidate's category is still confirmed from its own item below.
12650        let open_only = !query.statuses.is_empty()
12651            && query.statuses.iter().all(|category| {
12652                !matches!(
12653                    category,
12654                    StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
12655                )
12656            });
12657        if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
12658            let mut qualifiers: Vec<String> = [
12659                query.commented_since.map(updated_qualifier),
12660                narrowing_qualifiers(query),
12661            ]
12662            .into_iter()
12663            .flatten()
12664            .collect();
12665            // A narrowed search never answers a draft, which is no issue, so narrowing it to
12666            // open issues too leaves out nothing such a search could have answered.
12667            if !qualifiers.is_empty() {
12668                if open_only {
12669                    qualifiers.push(OPEN_QUALIFIER.to_owned());
12670                }
12671                return self.search_tasks(query, page, &qualifiers.join(" ")).await;
12672            }
12673        }
12674
12675        // A read narrowed to one project asks that project for its own tasks, so nothing
12676        // about it costs what the rest of the board holds. A read carrying a text, metadata
12677        // or origin predicate asks GitHub the narrower question those predicates are, and a
12678        // read narrowed to comment activity alone asks the board's own issue search for the
12679        // issues updated since, which is every issue a comment could have been written or
12680        // edited on since. Every other task read is a question about the whole board and is
12681        // answered by reading it.
12682        let (held, membership) = match (&query.project, query.commented_since) {
12683            (ProjectFilter::Is(project), _) => (
12684                match self.held_task_children(project)? {
12685                    // Taken as `project_children` takes the project's own sub-issue read: with
12686                    // every item this process wrote under it since, and none it moved away.
12687                    Some(held) => self.completed_with_written(
12688                        held.into_iter()
12689                            .filter(|child| child.parent.as_ref() == Some(project))
12690                            .collect(),
12691                        |own| own.parent.as_ref() == Some(project),
12692                    )?,
12693                    None => self.project_children(project).await?,
12694                },
12695                // Answered by where these items came from; see `task_matches`.
12696                &ProjectFilter::Any,
12697            ),
12698            (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
12699                match (self.narrowed(query).await?, since) {
12700                    (Some(narrowed), _) => (narrowed, &query.project),
12701                    (None, Some(since)) => (self.updated_since(since).await?, &query.project),
12702                    // The board's own items list every draft; only the search half, which
12703                    // completes them with issues they lag behind on, is narrowed — a closed
12704                    // issue that half leaves out is at no category the filter keeps.
12705                    (None, None) if open_only => (
12706                        self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
12707                        &query.project,
12708                    ),
12709                    (None, None) => (self.board().await?.items, &query.project),
12710                }
12711            }
12712        };
12713        // Filtered before paged: a page of a filtered result is a page of the survivors,
12714        // never the survivors of a page.
12715        let mut tasks = Vec::new();
12716        for item in held
12717            .iter()
12718            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
12719        {
12720            let task = item.task()?;
12721            if task_matches(&task, query, membership)
12722                && self.commented_since(item, query.commented_since).await?
12723            {
12724                tasks.push(task);
12725            }
12726        }
12727        Ok(offset_page(
12728            tasks,
12729            numeric_cursor(page.cursor.as_ref())?,
12730            page.limit.min(MAX_PAGE_SIZE) as usize,
12731        ))
12732    }
12733    async fn query_projects(
12734        &self,
12735        query: &ProjectQuery,
12736        page: &PageRequest,
12737    ) -> Result<Page<Project>, SourceError> {
12738        validate_page(page)?;
12739        refuse_unsearchable_text(query.text.as_ref())?;
12740        // The projects a board holds are found by an issue search scoped to that board,
12741        // never by walking the board's own item connection: what tells a project from a
12742        // task is the `parent` each issue carries, which costs nothing to read. A query
12743        // carrying a text asks that search for the text too, so it reads the issues that
12744        // hold it rather than every issue of the board.
12745        let held = match self.text_searched(query.text.as_ref()).await? {
12746            Some(searched) => searched,
12747            None => self.board_issues().await?,
12748        };
12749        let projects = held
12750            .iter()
12751            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
12752            .map(Resolved::project)
12753            .filter(|project| project_matches(project, query))
12754            .collect();
12755        Ok(offset_page(
12756            projects,
12757            numeric_cursor(page.cursor.as_ref())?,
12758            page.limit.min(MAX_PAGE_SIZE) as usize,
12759        ))
12760    }
12761    async fn query_projects_by_origin(
12762        &self,
12763        origin: &str,
12764        page: &PageRequest,
12765    ) -> Result<Page<Project>, SourceError> {
12766        validate_page(page)?;
12767        let projects = self
12768            .copied_from(origin, BoardKind::Work(ItemKind::Project))
12769            .await?
12770            .iter()
12771            .map(Resolved::project)
12772            .collect();
12773        Ok(offset_page(
12774            projects,
12775            numeric_cursor(page.cursor.as_ref())?,
12776            page.limit.min(MAX_PAGE_SIZE) as usize,
12777        ))
12778    }
12779    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
12780        let Some(item) = self
12781            .item_by_id(id)
12782            .await?
12783            .filter(|item| item.kind == BoardKind::Document)
12784        else {
12785            return Ok(None);
12786        };
12787        self.expected.read_document(&item);
12788        Ok(Some(item.document()))
12789    }
12790    async fn query_documents_by_origin(
12791        &self,
12792        origin: &str,
12793        page: &PageRequest,
12794    ) -> Result<Page<Document>, SourceError> {
12795        validate_page(page)?;
12796        let documents = self
12797            .copied_from(origin, BoardKind::Document)
12798            .await?
12799            .iter()
12800            .map(Resolved::document)
12801            .collect();
12802        Ok(offset_page(
12803            documents,
12804            numeric_cursor(page.cursor.as_ref())?,
12805            page.limit.min(MAX_PAGE_SIZE) as usize,
12806        ))
12807    }
12808    async fn query_documents(
12809        &self,
12810        query: &DocumentQuery,
12811        page: &PageRequest,
12812    ) -> Result<Page<Document>, SourceError> {
12813        validate_page(page)?;
12814        // Narrowed to one project, this is the same sub-issue read a task list scoped to
12815        // that project makes — a document filed under a project is a sub-issue of it too,
12816        // and which of them come back is the kind this caller asked for. Unscoped, a query
12817        // carrying a text asks the board-scoped issue search for it, as a task query does,
12818        // and only one carrying none reads the board.
12819        let (held, membership) = match &query.project {
12820            ProjectFilter::Is(project) => (
12821                self.project_children(project).await?,
12822                // Answered by where these items came from; see `task_matches`.
12823                &ProjectFilter::Any,
12824            ),
12825            ProjectFilter::Any | ProjectFilter::Orphans => {
12826                refuse_unsearchable_text(query.text.as_ref())?;
12827                match self.text_searched(query.text.as_ref()).await? {
12828                    Some(searched) => (searched, &query.project),
12829                    None => (self.board().await?.items, &query.project),
12830                }
12831            }
12832        };
12833        // Filtered before paged, exactly as a task read is: a page of a filtered result is
12834        // a page of the survivors, never the survivors of a page.
12835        let documents = held
12836            .iter()
12837            .filter(|item| item.kind == BoardKind::Document)
12838            .map(Resolved::document)
12839            .filter(|document| document_matches(document, query, membership))
12840            .collect();
12841        Ok(offset_page(
12842            documents,
12843            numeric_cursor(page.cursor.as_ref())?,
12844            page.limit.min(MAX_PAGE_SIZE) as usize,
12845        ))
12846    }
12847    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
12848        validate_page(page)?;
12849        let offset = numeric_cursor(page.cursor.as_ref())?;
12850        let mut labels = self
12851            .board()
12852            .await?
12853            .items
12854            .into_iter()
12855            .flat_map(|item| item.labels)
12856            .fold(Vec::new(), |mut all, label| {
12857                if !all.iter().any(|x: &Label| x.id == label.id) {
12858                    all.push(label);
12859                }
12860                all
12861            });
12862        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
12863        Ok(offset_page(
12864            labels,
12865            offset,
12866            page.limit.min(MAX_PAGE_SIZE) as usize,
12867        ))
12868    }
12869    async fn task_dependencies(
12870        &self,
12871        id: &NativeId,
12872        direction: Direction,
12873        page: &PageRequest,
12874    ) -> Result<Page<DependencyEdge>, SourceError> {
12875        self.dependencies(id, ItemKind::Task, direction, page).await
12876    }
12877    async fn project_dependencies(
12878        &self,
12879        id: &NativeId,
12880        direction: Direction,
12881        page: &PageRequest,
12882    ) -> Result<Page<DependencyEdge>, SourceError> {
12883        self.dependencies(id, ItemKind::Project, direction, page)
12884            .await
12885    }
12886
12887    fn writes(&self) -> WriteSupport {
12888        WriteSupport::Supported
12889    }
12890
12891    /// Create or update one task.
12892    ///
12893    /// Its `delivers` and `delivered_by` are checked before anything is read or written —
12894    /// neither may name the task itself or name one task twice — and land in the body's
12895    /// metadata slot under their reserved keys, in place of any caller metadata of those
12896    /// names.
12897    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
12898        self.write_task_assets(write, None)
12899            .await
12900            .map(|written| written.id)
12901    }
12902
12903    async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
12904        self.origins_ahead(origins).await
12905    }
12906
12907    /// The issue's own web address, as the `createIssue` that made it in this command answered
12908    /// it: a task, a project and a document are each an issue here.
12909    fn written_location(
12910        &self,
12911        _record: onetaskgraph_plugin_api::MetadataRecord,
12912        id: &NativeId,
12913    ) -> Option<Location> {
12914        self.written
12915            .lock()
12916            .unwrap_or_else(std::sync::PoisonError::into_inner)
12917            .get(id)
12918            .cloned()
12919            .map(Location::Url)
12920    }
12921
12922    /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
12923    /// in one request, and a copy's tasks are created together only then, since their fields
12924    /// and edges are what is held back.
12925    fn create_batch_limit(&self) -> usize {
12926        if self.deferring().unwrap_or(false) {
12927            MUTATION_BATCH
12928        } else {
12929            1
12930        }
12931    }
12932
12933    // 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.
12934    async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
12935        if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
12936            return Err(SourceError::Refused {
12937                message: format!(
12938                    "write_tasks creates tasks, and the write of {} names the existing task {}; \
12939                     next: update it with write_task",
12940                    write.item.id,
12941                    write.target.as_ref().map_or("", |target| target.0.as_str())
12942                ),
12943            });
12944        }
12945        if !self.deferring()? {
12946            let mut created = Vec::with_capacity(writes.len());
12947            for write in writes {
12948                match self.write_task(write).await {
12949                    Ok(id) => created.push(id),
12950                    Err(error) => {
12951                        let mut left: Vec<&str> = Vec::new();
12952                        for id in &created {
12953                            if self.delete_issue(id).await.is_err() {
12954                                left.push(id.0.as_str());
12955                            }
12956                        }
12957                        return Err(left_behind(error, &left));
12958                    }
12959                }
12960            }
12961            return Ok(created);
12962        }
12963        self.create_together(writes).await
12964    }
12965
12966    fn begin_deferred_writes(&self) {
12967        if let Ok(mut deferred) = self.deferred.lock() {
12968            deferred.get_or_insert_with(Deferred::default);
12969        }
12970    }
12971
12972    async fn finish_writes(&self) -> Result<(), SourceError> {
12973        self.send_held_back().await
12974    }
12975
12976    async fn write_task_with_assets(
12977        &self,
12978        write: &ItemWrite<Task>,
12979        _answers: Option<&BTreeMap<String, Value>>,
12980        assets: &onetaskgraph_plugin_api::AssetWrite,
12981    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
12982        self.write_task_assets(write, Some(assets)).await
12983    }
12984
12985    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
12986        self.write_item(
12987            &Incoming {
12988                written: Written::Work(ItemKind::Project, &write.item.status),
12989                title: &write.item.title,
12990                content: write.item.content.as_deref(),
12991                assets: None,
12992                labels: &write.item.labels,
12993                metadata: &write.item.metadata,
12994                repositories: &write.item.repositories,
12995                classification: write.item.classification,
12996                parent: None,
12997                delivers: &[],
12998                delivered_by: &[],
12999                priority: None,
13000            },
13001            write.target.as_ref(),
13002            &write.depends_on,
13003        )
13004        .await
13005        .map(|written| written.id)
13006    }
13007
13008    /// Create or update one document, which is one issue titled the way this board spells
13009    /// a document.
13010    ///
13011    /// Everything else is exactly a task write: caller metadata goes to the same canonical
13012    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
13013    /// or a field this board cannot carry is refused by name rather than dropped, a target
13014    /// naming an issue this board does not hold is refused rather than created, and an
13015    /// issue this call created is taken back when the rest of the write fails.
13016    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
13017        self.write_document_assets(write, None)
13018            .await
13019            .map(|written| written.id)
13020    }
13021
13022    async fn write_document_with_assets(
13023        &self,
13024        write: &ItemWrite<Document>,
13025        _answers: Option<&BTreeMap<String, Value>>,
13026        assets: &onetaskgraph_plugin_api::AssetWrite,
13027    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
13028        self.write_document_assets(write, Some(assets)).await
13029    }
13030
13031    /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
13032    /// which reads nothing; then the board's `Status` option. Over an existing item that is
13033    /// read off the item, as the write reads it, and the item is held among this command's
13034    /// resolved records so the write that follows reuses that read rather than repeating it;
13035    /// an item that does not carry the field takes the board's fields, which are held once
13036    /// read. A create is checked against the board's fields only when this command already
13037    /// holds them, because a create reads them together with its repository, in one request,
13038    /// and refuses a missing option before it writes anything.
13039    async fn check_status_write(
13040        &self,
13041        kind: ItemKind,
13042        category: StatusCategory,
13043        target: Option<&NativeId>,
13044    ) -> Result<(), SourceError> {
13045        let status = self.resolved_target(kind, category)?;
13046        if status.option().is_none() {
13047            return Ok(());
13048        }
13049        let fields = match target {
13050            Some(target) => {
13051                // A target this board does not hold is the write's own refusal to make.
13052                let Some(item) = self.bound_item(target).await? else {
13053                    return Ok(());
13054                };
13055                self.resolved_cache()?.insert(target.clone(), item.clone());
13056                self.fields_for(Some(&item), true, false).await?.fields
13057            }
13058            None => {
13059                let held = self
13060                    .board_cache()?
13061                    .as_ref()
13062                    .map(|board| board.fields.clone());
13063                match held.or_else(|| {
13064                    self.fields_cache()
13065                        .ok()
13066                        .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
13067                }) {
13068                    Some(fields) => fields,
13069                    None => return Ok(()),
13070                }
13071            }
13072        };
13073        self.column_for(&fields, kind, category, &status)
13074            .map(|_| ())
13075    }
13076
13077    /// What a write of `status` reads back as: the `Status` option its category maps to for
13078    /// `kind`, and for a closing category the closure that option and its fixed reason read
13079    /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
13080    /// this instance has no option for is answered as given, since writing it is refused.
13081    fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
13082        match self.statuses.target(kind, status.category) {
13083            StatusTarget::Column(name) => {
13084                self.statuses.status(kind, Some(name.as_str()), false, None)
13085            }
13086            StatusTarget::Terminal(name, reason) => {
13087                self.statuses
13088                    .status(kind, Some(name.as_str()), true, Some(reason.reason()))
13089            }
13090            StatusTarget::Disabled(_) => status.clone(),
13091        }
13092    }
13093
13094    /// Set one task's status alone.
13095    ///
13096    /// An open target reopens a closed issue with an `updateIssue` carrying only its
13097    /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
13098    /// terminal target selects its mapped option, then closes with its fixed reason. No
13099    /// request carries a title, a body or a label. The status
13100    /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
13101    /// what a re-read reports.
13102    async fn set_task_status(
13103        &self,
13104        id: &NativeId,
13105        category: StatusCategory,
13106    ) -> Result<Option<Status>, SourceError> {
13107        self.set_status(id, ItemKind::Task, category).await
13108    }
13109
13110    /// Set one task's status and answer the task with it, reading the item once: the read the
13111    /// task is answered from is handed to the status write, which would otherwise read the
13112    /// same issue again — two requests on a fresh process, the read and the field write,
13113    /// where the default's `get_task` before `set_task_status` sent three.
13114    async fn set_task_status_reading(
13115        &self,
13116        id: &NativeId,
13117        category: StatusCategory,
13118    ) -> Result<Option<Task>, SourceError> {
13119        // Refused before anything is read, in the words the write itself refuses it.
13120        self.resolved_target(ItemKind::Task, category)?;
13121        let Some(item) = self
13122            .bound_item(id)
13123            .await?
13124            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
13125        else {
13126            return Ok(None);
13127        };
13128        let mut task = item.task()?;
13129        self.held_cache
13130            .lock()
13131            .map_err(|_| SourceError::Unavailable {
13132                message: "items read by issue reference were left inconsistent; run the \
13133                          command again"
13134                    .into(),
13135            })?
13136            .insert(id.clone(), item);
13137        let Some(status) = self.set_status(id, ItemKind::Task, category).await? else {
13138            return Ok(None);
13139        };
13140        task.status = status;
13141        Ok(Some(task))
13142    }
13143
13144    /// Set one project's status alone, by exactly the paired write a task's status takes, for
13145    /// the project kind: a terminal category selects the project kind's mapped option and then
13146    /// closes the issue with the category's fixed reason — an issue already closed with a
13147    /// reason that reads that category is left as it is — and an open category reopens a closed
13148    /// issue and then selects its option. A board draft is refused a terminal category as a
13149    /// task's is.
13150    async fn set_project_status(
13151        &self,
13152        id: &NativeId,
13153        category: StatusCategory,
13154    ) -> Result<Option<Status>, SourceError> {
13155        self.set_status(id, ItemKind::Project, category).await
13156    }
13157
13158    /// Every task and project whose card contradicts its issue's closure, read through the
13159    /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
13160    async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
13161        self.contradictions().await
13162    }
13163
13164    /// What this board holds for each issue reference, one request per fifty of them; see
13165    /// [`TaskSource::issue_holdings`].
13166    async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
13167        self.holdings(issues).await
13168    }
13169
13170    /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
13171    /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
13172    /// for `none`. Refused by an instance with no `priority_mapping`.
13173    async fn set_task_priority(
13174        &self,
13175        id: &NativeId,
13176        priority: Priority,
13177    ) -> Result<Option<Priority>, SourceError> {
13178        self.set_priority(id, priority).await
13179    }
13180
13181    /// Replace one task's content with a single body update that keeps the metadata slot
13182    /// byte for byte.
13183    async fn set_task_content(
13184        &self,
13185        id: &NativeId,
13186        content: &str,
13187    ) -> Result<Option<()>, SourceError> {
13188        self.replace_content(id, content).await
13189    }
13190
13191    /// Replace one task issue's content and its provenance slot entry with a single body
13192    /// update. The answers are not kept: see `replace_rendering`.
13193    async fn set_task_rendering(
13194        &self,
13195        id: &NativeId,
13196        content: &str,
13197        provenance: &Value,
13198        _answers: &BTreeMap<String, Value>,
13199    ) -> Result<Option<()>, SourceError> {
13200        self.replace_rendering(
13201            id,
13202            BoardKind::Work(ItemKind::Task),
13203            content,
13204            provenance,
13205            None,
13206        )
13207        .await
13208        .map(|written| written.map(|_| ()))
13209    }
13210
13211    /// Replace one design-document issue's content and its provenance slot entry, on exactly
13212    /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13213    async fn set_document_rendering(
13214        &self,
13215        id: &NativeId,
13216        content: &str,
13217        provenance: &Value,
13218        _answers: &BTreeMap<String, Value>,
13219    ) -> Result<Option<()>, SourceError> {
13220        self.replace_rendering(id, BoardKind::Document, content, provenance, None)
13221            .await
13222            .map(|written| written.map(|_| ()))
13223    }
13224
13225    /// Replace one project issue's content and its provenance slot entry, on exactly the
13226    /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13227    async fn set_project_rendering(
13228        &self,
13229        id: &NativeId,
13230        content: &str,
13231        provenance: &Value,
13232        _answers: &BTreeMap<String, Value>,
13233    ) -> Result<Option<()>, SourceError> {
13234        self.replace_rendering(
13235            id,
13236            BoardKind::Work(ItemKind::Project),
13237            content,
13238            provenance,
13239            None,
13240        )
13241        .await
13242        .map(|written| written.map(|_| ()))
13243    }
13244
13245    /// Apply a targeted update with one read of the item and a write only for what differs:
13246    /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
13247    /// and last one `updateIssue` for title, body and state. See `targeted_update`.
13248    async fn update_task(
13249        &self,
13250        id: &NativeId,
13251        update: &TaskUpdate,
13252    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
13253        self.targeted_update(id, update).await
13254    }
13255
13256    /// Replace one task's `delivered_by` with a single body update that changes the
13257    /// metadata slot and nothing outside it.
13258    async fn set_delivered_by(
13259        &self,
13260        id: &NativeId,
13261        delivered_by: &[TaskRef],
13262    ) -> Result<Option<()>, SourceError> {
13263        self.replace_delivered_by(id, delivered_by).await
13264    }
13265
13266    /// Set one key of one task issue's metadata with a single body update that changes the
13267    /// metadata slot and nothing outside it — no title, label, state or board field request —
13268    /// and sends nothing when the task already holds that value under the key.
13269    async fn set_task_metadata(
13270        &self,
13271        id: &NativeId,
13272        key: &MetadataKey,
13273        value: &Value,
13274    ) -> Result<Option<Task>, SourceError> {
13275        Ok(self
13276            .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
13277            .await?
13278            .map(|item| item.task())
13279            .transpose()?)
13280    }
13281
13282    /// Set one key of one project issue's metadata, on exactly the terms of
13283    /// [`set_task_metadata`](TaskSource::set_task_metadata).
13284    async fn set_project_metadata(
13285        &self,
13286        id: &NativeId,
13287        key: &MetadataKey,
13288        value: &Value,
13289    ) -> Result<Option<Project>, SourceError> {
13290        Ok(self
13291            .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
13292            .await?
13293            .map(|item| item.project()))
13294    }
13295
13296    /// Set one key of one design-document issue's metadata, on exactly the terms of
13297    /// [`set_task_metadata`](TaskSource::set_task_metadata).
13298    async fn set_document_metadata(
13299        &self,
13300        id: &NativeId,
13301        key: &MetadataKey,
13302        value: &Value,
13303    ) -> Result<Option<Document>, SourceError> {
13304        Ok(self
13305            .set_slot_key(id, BoardKind::Document, key, value)
13306            .await?
13307            .map(|item| item.document()))
13308    }
13309
13310    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
13311        self.delete_item(id).await
13312    }
13313
13314    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
13315        self.delete_item(id).await
13316    }
13317
13318    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
13319        self.delete_item(id).await
13320    }
13321
13322    /// One page of the task issue's own comments, walked by GitHub's own cursor.
13323    ///
13324    /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
13325    /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
13326    ///
13327    /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
13328    /// board is the read of its comments. A draft this process already resolved is refused
13329    /// without one.
13330    async fn task_comments(
13331        &self,
13332        task: &NativeId,
13333        page: &PageRequest,
13334    ) -> Result<Option<Page<Comment>>, SourceError> {
13335        validate_page(page)?;
13336        let cached = self.resolved_cache()?.get(task).cloned();
13337        if let Some(item) = cached {
13338            if item.kind != BoardKind::Work(ItemKind::Task) {
13339                return Ok(None);
13340            }
13341            if item.content_kind == ContentKind::DraftIssue {
13342                return Err(self.draft_has_no_comments(task));
13343            }
13344        }
13345        match self.issue_detail(task, page).await? {
13346            Some(TaskDetailRead {
13347                comments: Some(comments),
13348                ..
13349            }) => comments,
13350            _ => Ok(None),
13351        }
13352    }
13353
13354    /// Every id's task, with the first page of its comments when `comments` names it:
13355    /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
13356    /// comments in one [`graphql::ISSUE_DETAIL`] request.
13357    async fn get_task_details(
13358        &self,
13359        ids: &[NativeId],
13360        comments: Option<&PageRequest>,
13361    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
13362        if let Some(page) = comments
13363            && let Err(error) = validate_page(page)
13364        {
13365            return ids.iter().map(|_| Err(error.clone())).collect();
13366        }
13367        match (ids, comments) {
13368            ([id], Some(page)) => vec![self.issue_detail(id, page).await],
13369            ([id], None) => vec![self.task_read(id).await],
13370            _ => self.issue_details(ids, comments).await,
13371        }
13372    }
13373
13374    /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
13375    /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
13376    /// request, so the dependency read a copy makes of each next is answered from this one.
13377    async fn get_task_details_for_copy(
13378        &self,
13379        ids: &[NativeId],
13380        comments: &PageRequest,
13381    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
13382        // A page past the first is a page of one task's comments, which the detail read of it
13383        // alone answers.
13384        if comments.cursor.is_some() {
13385            return self.get_task_details(ids, Some(comments)).await;
13386        }
13387        if let Err(error) = validate_page(comments) {
13388            return ids.iter().map(|_| Err(error.clone())).collect();
13389        }
13390        let mut read = Vec::with_capacity(ids.len());
13391        for batch in ids.chunks(COPY_DETAIL_BATCH) {
13392            match self
13393                .graphql(
13394                    graphql::ISSUE_COPY_DETAILS,
13395                    copy_detail_batch(batch, comments, false),
13396                )
13397                .await
13398            {
13399                Ok(data) => {
13400                    for (slot, id) in batch.iter().enumerate() {
13401                        let read_one = match data.get(format!("i{slot}")) {
13402                            Some(node) => self.detail_of(id, node, true, None).await,
13403                            None => Err(SourceError::Malformed {
13404                                message: format!(
13405                                    "GitHub answered a batch read with no item for {}",
13406                                    id.0
13407                                ),
13408                            }),
13409                        };
13410                        read.push(read_one);
13411                    }
13412                }
13413                Err(error) if unresolvable_node(&error) => {
13414                    for id in batch {
13415                        read.push(self.issue_detail(id, comments).await);
13416                    }
13417                }
13418                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
13419            }
13420        }
13421        read
13422    }
13423
13424    /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
13425    /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
13426    /// and held for the next read of each by id; see `read_ahead`.
13427    ///
13428    /// What this run wrote is never read: its own record answers, as it does for a read by id.
13429    /// Nor is an item a listing of its project carried whole earlier in this command — see
13430    /// `carried` — which is held in place of reading it.
13431    /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
13432    /// so each of its ids is read on its own and answered as that read answers; a draft, and an
13433    /// answer naming another issue than the one asked for, are left to their own reads too.
13434    async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
13435        let mut unread: Vec<NativeId> = Vec::new();
13436        // Read whole by a listing of its project in this command, which a mutation naming it
13437        // would have dropped: held as a batch would have held it, once every batch is answered.
13438        let mut listed: Vec<NativeId> = Vec::new();
13439        {
13440            let created = self.created()?;
13441            let held = self.read_ahead()?;
13442            let carried = self.carried()?;
13443            for id in ids {
13444                if unread.contains(id)
13445                    || listed.contains(id)
13446                    || held.contains_key(id)
13447                    || created.iter().any(|own| own.id == *id)
13448                {
13449                    continue;
13450                }
13451                if carried.contains_key(id) {
13452                    listed.push(id.clone());
13453                } else {
13454                    unread.push(id.clone());
13455                }
13456            }
13457        }
13458        // Nothing of an item's comments is wanted here: the smallest page keeps the read to
13459        // what the item itself costs.
13460        let comments = PageRequest {
13461            cursor: None,
13462            limit: 1,
13463        };
13464        // Held only once every batch has been answered, so a read-ahead that fails part way
13465        // holds nothing — what a listing carried included, which stays carried — and every
13466        // item is read on its own, as the method promises.
13467        let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
13468        // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
13469        // answered them: what a project copy's orphan check asks, when that first id is its
13470        // project.
13471        let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
13472        for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
13473            let data = match self
13474                .graphql(
13475                    graphql::ISSUE_COPY_DETAILS,
13476                    copy_detail_batch(batch, &comments, index == 0),
13477                )
13478                .await
13479            {
13480                Ok(data) => data,
13481                Err(error) if unresolvable_node(&error) => continue,
13482                Err(error) => return Err(error),
13483            };
13484            for (slot, id) in batch.iter().enumerate() {
13485                let Some(node) = data.get(format!("i{slot}")) else {
13486                    continue;
13487                };
13488                let held = if node.is_null() {
13489                    None
13490                } else {
13491                    match optional_str(node, "__typename")? {
13492                        Some("DraftIssue") => continue,
13493                        Some("Issue") if required_str(node, "id")? != id.0 => continue,
13494                        _ => {}
13495                    }
13496                    // Remembered below with the rest, so a later batch refused leaves no
13497                    // dependency read answered from this one either.
13498                    self.resolve_issue_as(node, false).await?
13499                };
13500                read.push((id.clone(), held));
13501            }
13502            if index == 0
13503                && let Some(node) = data.get("children").filter(|node| !node.is_null())
13504                && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
13505                && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
13506            {
13507                // A list GitHub did not answer is not an empty one: it would answer the project's
13508                // task listing with nothing, and the orphan check with no orphans.
13509                let listed = node
13510                    .pointer("/subIssues/nodes")
13511                    .and_then(Value::as_array)
13512                    .ok_or_else(|| SourceError::Malformed {
13513                        message: format!(
13514                            "GitHub answered the sub-issues of project {} with no list of them",
13515                            batch[0].0
13516                        ),
13517                    })?
13518                    .iter()
13519                    .map(|child| {
13520                        Ok((
13521                            required_str(child, "id")?.to_owned(),
13522                            required_str(child, "title")?.to_owned(),
13523                        ))
13524                    })
13525                    .collect::<Result<Vec<_>, SourceError>>()?;
13526                children = Some((batch[0].clone(), listed));
13527            }
13528        }
13529        // The project's tasks are known from this read alone when every child it named is held
13530        // from it, but for a document, which its title says is one and which a task listing
13531        // never answers: then a listing of the project's tasks is answered from them.
13532        if let Some((project, listed)) = children
13533            && read.iter().any(|(id, held)| {
13534                *id == project
13535                    && held
13536                        .as_ref()
13537                        .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
13538            })
13539        {
13540            let mut tasks = Vec::new();
13541            let mut whole = true;
13542            for (child, title) in &listed {
13543                match read.iter().find(|(id, _)| id.0 == *child) {
13544                    Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
13545                        if held.kind == BoardKind::Work(ItemKind::Task) {
13546                            tasks.push(held.clone());
13547                        }
13548                    }
13549                    _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
13550                    _ => whole = false,
13551                }
13552            }
13553            if whole {
13554                self.task_children()?.insert(project, tasks);
13555            }
13556        }
13557        let mut resolved = self.resolved_cache()?;
13558        let mut ahead = self.read_ahead()?;
13559        let mut carried = self.carried()?;
13560        for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
13561            resolved.insert(item.id.clone(), item.clone());
13562        }
13563        // Taken only if still carried: a mutation naming it while the batches ran dropped it,
13564        // and its next read is then its own.
13565        for id in &listed {
13566            let Some(item) = carried.remove(id) else {
13567                continue;
13568            };
13569            // The whole of what it is blocked by came with it, so the dependency read a copy
13570            // makes next is answered from this one, as a copy's detail read is.
13571            if item.blocked_by.is_some() {
13572                resolved.insert(item.id.clone(), item.clone());
13573            }
13574            read.push((id.clone(), Some(item)));
13575        }
13576        for (id, held) in read {
13577            ahead.insert(id, held);
13578        }
13579        Ok(())
13580    }
13581
13582    /// Add one comment to the task's issue, as the account the token belongs to.
13583    ///
13584    /// The author is refused before anything is sent — not even the task is read — because
13585    /// no answer GitHub could give would make posting under another name than the one asked
13586    /// for the right outcome.
13587    async fn add_comment(
13588        &self,
13589        task: &NativeId,
13590        comment: &NewComment,
13591    ) -> Result<Option<Comment>, SourceError> {
13592        if let Some(author) = &comment.author {
13593            return Err(SourceError::Refused {
13594                message: format!(
13595                    "source {} cannot post a comment as {author:?}: GitHub records the account \
13596                     the token signs in as the author of every comment; next: leave --author \
13597                     out, and the comment is posted as that account",
13598                    self.name
13599                ),
13600            });
13601        }
13602        let Some(issue) = self.commented_issue(task).await? else {
13603            return Ok(None);
13604        };
13605        let data = self
13606            .graphql(
13607                graphql::ADD_COMMENT,
13608                json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
13609            )
13610            .await?;
13611        let subject = data
13612            .pointer("/addComment/subject")
13613            .filter(|value| !value.is_null())
13614            .ok_or_else(|| SourceError::Malformed {
13615                message: "GitHub comment addition returned no subject".into(),
13616            })?;
13617        if required_str(subject, "id")? != issue.0 {
13618            return Err(SourceError::Malformed {
13619                message: "GitHub comment addition answered about another issue".into(),
13620            });
13621        }
13622        let added = data
13623            .pointer("/addComment/commentEdge/node")
13624            .filter(|value| !value.is_null())
13625            .ok_or_else(|| SourceError::Malformed {
13626                message: "GitHub comment addition returned no comment".into(),
13627            })?;
13628        let added = comment_from(added)?;
13629        self.remember_commented(&issue)?;
13630        Ok(Some(added))
13631    }
13632
13633    async fn edit_comment(
13634        &self,
13635        task: &NativeId,
13636        comment: &NativeId,
13637        body: &CommentBody,
13638    ) -> Result<Option<Comment>, SourceError> {
13639        let Some(issue) = self.commented_issue(task).await? else {
13640            return Ok(None);
13641        };
13642        let Some(held) = self.comment_on(&issue, comment).await? else {
13643            return Ok(None);
13644        };
13645        // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
13646        let body = CommentOrigin::kept(&held, body)?;
13647        let data = self
13648            .graphql(
13649                graphql::UPDATE_COMMENT,
13650                json!({"input":{"id":comment.0,"body":body.as_str()}}),
13651            )
13652            .await?;
13653        let edited = data
13654            .pointer("/updateIssueComment/issueComment")
13655            .filter(|value| !value.is_null())
13656            .ok_or_else(|| SourceError::Malformed {
13657                message: "GitHub comment update returned no comment".into(),
13658            })?;
13659        let edited = comment_from(edited)?;
13660        if edited.id != *comment {
13661            return Err(SourceError::Malformed {
13662                message: "GitHub comment update returned the wrong comment".into(),
13663            });
13664        }
13665        self.remember_commented(&issue)?;
13666        Ok(Some(edited))
13667    }
13668
13669    async fn delete_comment(
13670        &self,
13671        task: &NativeId,
13672        comment: &NativeId,
13673    ) -> Result<Option<NativeId>, SourceError> {
13674        let Some(issue) = self.commented_issue(task).await? else {
13675            return Ok(None);
13676        };
13677        if !self.comment_is_on(&issue, comment).await? {
13678            return Ok(None);
13679        }
13680        let data = self
13681            .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
13682            .await?;
13683        // The payload says nothing about the comment it removed, so what is checked is that
13684        // GitHub answered the mutation at all rather than leaving it unanswered.
13685        data.get("deleteIssueComment")
13686            .filter(|value| !value.is_null())
13687            .ok_or_else(|| SourceError::Malformed {
13688                message: "GitHub comment deletion returned no payload".into(),
13689            })?;
13690        Ok(Some(comment.clone()))
13691    }
13692
13693    /// Every request this source has recorded, and what each of GitHub's two budgets was
13694    /// attributed — read off the same accounting the session report is rendered from, so
13695    /// the two cannot count one request two ways.
13696    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
13697        Ok(Some(self.ledger.snapshot().metering()))
13698    }
13699
13700    /// Drop every item, search answer and board read this source holds, so the next command
13701    /// reads the board as a person has since left it.
13702    ///
13703    /// Every one of those is held on the assumption that nothing but this source writes the
13704    /// board while a command runs, which stops being true the moment the command is over: a
13705    /// body a person edited would be overwritten from the record held here, and a card they
13706    /// moved would be read as still where this source left it. The board's own field
13707    /// definitions go too, because a person can add or delete a `Status` option and a write
13708    /// resolved against the held list would not re-read on a miss. What stays is what stays
13709    /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
13710    /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
13711    /// accounting [`metering`](TaskSource::metering) answers from.
13712    ///
13713    /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
13714    /// refused, because clearing it is what puts it right.
13715    async fn end_command(&self) -> Result<(), SourceError> {
13716        fn clear<T: Default>(held: &Mutex<T>) {
13717            *held
13718                .lock()
13719                .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
13720            held.clear_poison();
13721        }
13722        clear(&self.created);
13723        clear(&self.updated);
13724        clear(&self.commented);
13725        clear(&self.board_cache);
13726        clear(&self.search_cache);
13727        clear(&self.narrowed_cache);
13728        clear(&self.search_next);
13729        clear(&self.resolved_cache);
13730        clear(&self.read_ahead);
13731        clear(&self.carried);
13732        clear(&self.held_cache);
13733        clear(&self.children_cache);
13734        clear(&self.task_children);
13735        clear(&self.written);
13736        clear(&self.fields_cache);
13737        self.expected.end_command();
13738        Ok(())
13739    }
13740}
13741
13742/// Each project's sub-issues as one command read them, keyed by the selector they were asked
13743/// for under, beside the project that selector named; see `children_cache`.
13744type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
13745
13746/// One issue comment as the contract carries it.
13747///
13748/// `author` is absent both when GitHub answers `null` for an account that no longer exists
13749/// and when it answers an actor with no login, because either way the source did not say who
13750/// wrote it — which is what an absent author means, rather than an author called nothing.
13751///
13752/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
13753/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
13754/// and is read back with the marker taken out and reported as its origin.
13755fn comment_from(value: &Value) -> Result<Comment, SourceError> {
13756    Ok(Comment {
13757        origin: None,
13758        id: NativeId(required_str(value, "id")?.to_owned()),
13759        author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
13760            .map(str::to_owned),
13761        created_at: optional_time(value, "createdAt")?,
13762        updated_at: optional_time(value, "updatedAt")?,
13763        body: required_str(value, "body")?.to_owned(),
13764        url: optional_str(value, "url")?.map(str::to_owned),
13765    }
13766    .unmarked())
13767}
13768
13769/// The page of comments one issue node carries, resumed from `after`.
13770fn comment_page(
13771    node: &Value,
13772    issue: &str,
13773    after: Option<&str>,
13774) -> Result<Page<Comment>, SourceError> {
13775    let connection = node
13776        .get("comments")
13777        .filter(|value| !value.is_null())
13778        .ok_or_else(|| SourceError::Malformed {
13779            message: format!("GitHub issue {issue} answered with no comments connection"),
13780        })?;
13781    let items = optional_nodes(Some(connection), "issue comments")?
13782        .into_iter()
13783        .flatten()
13784        .map(comment_from)
13785        .collect::<Result<Vec<_>, _>>()?;
13786    let next = next_cursor(connection)?;
13787    if let Some(next) = &next {
13788        validate_cursor_progress(after, &next.0)?;
13789    }
13790    Ok(Page { items, next })
13791}
13792
13793/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
13794/// end — `None` when it carried none, or a page with more past it.
13795fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
13796    let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
13797        return Ok(None);
13798    };
13799    if next_cursor(connection)?.is_some() {
13800        return Ok(None);
13801    }
13802    Ok(Some(
13803        optional_nodes(Some(connection), "blocked-by issues")?
13804            .into_iter()
13805            .flatten()
13806            .cloned()
13807            .collect(),
13808    ))
13809}
13810
13811/// Where the recorded tail of a dependency walk resumes; see
13812/// [`GitHubProjectsSource::recorded_edges`].
13813const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
13814
13815/// The board text field this source keeps a copy's origin in.
13816///
13817/// Named after the key it holds, and held to that name by the guard below rather than by
13818/// a reader noticing.
13819const ORIGIN_FIELD: &str = "onetaskgraph.origin";
13820
13821/// The metadata key that field holds.
13822///
13823/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
13824/// constructs or interprets the qualified id it carries. This source names it only to
13825/// route it — a short, typed value belongs in a typed field rather than in the body slot
13826/// a caller's own prose shares.
13827///
13828/// Restated rather than imported, because no plugin crate may depend on the engine. What
13829/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
13830/// target in `check`: it reads the engine's own literal and fails naming the file and the
13831/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
13832/// that creates a second item every run instead of finding the one it wrote — and that is
13833/// too late to learn it.
13834const ORIGIN_KEY: &str = "onetaskgraph.origin";
13835
13836/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
13837///
13838/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
13839/// is derived from the far end, never written down on the near item — so only a forward
13840/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
13841/// it did not come from, and it is told so rather than answered with an empty page that
13842/// reads as a walk which ended.
13843fn recorded_offset(
13844    cursor: Option<&str>,
13845    direction: Direction,
13846) -> Result<Option<usize>, SourceError> {
13847    cursor
13848        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
13849        .map(|offset| {
13850            if direction != Direction::DependsOn {
13851                return Err(SourceError::Config {
13852                    message: format!(
13853                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
13854                         reverse dependency read never issues; resume it in the direction \
13855                         that reported it"
13856                    ),
13857                });
13858            }
13859            offset.parse().map_err(|_| SourceError::Config {
13860                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
13861            })
13862        })
13863        .transpose()
13864}
13865
13866/// What a dependency read's own answer says of a board draft: `None` when the item is not a
13867/// draft, or the answer did not carry its board item; `Some(None)` for a draft whose one board
13868/// item is not on this board, which this source does not hold; and `Some(Some(slot))`, the
13869/// draft's metadata, for one it does — its board item naming this board by its number and by
13870/// the id the same answer read of the board, as [`GitHubProjectsSource::draft_by_id`] decides.
13871fn draft_slot(
13872    data: &Value,
13873    id: &NativeId,
13874    project_number: u32,
13875) -> Result<Option<Option<BTreeMap<String, Value>>>, SourceError> {
13876    let Some(draft) = data
13877        .get("node")
13878        .filter(|node| node.get("__typename").and_then(Value::as_str) == Some("DraftIssue"))
13879    else {
13880        return Ok(None);
13881    };
13882    let Some(items) = data.pointer("/draft/projectV2Items") else {
13883        return Ok(None);
13884    };
13885    if required_str(draft, "id")? != id.0 {
13886        return Err(SourceError::Malformed {
13887            message: format!("GitHub answered a different draft for {}", id.0),
13888        });
13889    }
13890    let nodes = items
13891        .get("nodes")
13892        .and_then(Value::as_array)
13893        .ok_or_else(|| SourceError::Malformed {
13894            message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
13895        })?;
13896    let info = items
13897        .get("pageInfo")
13898        .ok_or_else(|| SourceError::Malformed {
13899            message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
13900        })?;
13901    if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
13902        return Err(SourceError::Malformed {
13903            message: format!(
13904                "GitHub draft {} reports more board items than the one GitHub links a draft to",
13905                id.0
13906            ),
13907        });
13908    }
13909    let board = data
13910        .pointer("/board/projectV2/id")
13911        .and_then(Value::as_str)
13912        .ok_or_else(|| SourceError::Malformed {
13913            message: format!(
13914                "GitHub answered draft {} without this board's own id beside it",
13915                id.0
13916            ),
13917        })?;
13918    let mut held = false;
13919    for node in nodes {
13920        let project = node.get("project").ok_or_else(|| SourceError::Malformed {
13921            message: format!("GitHub draft {} board item has no project", id.0),
13922        })?;
13923        let number = project
13924            .get("number")
13925            .and_then(Value::as_u64)
13926            .ok_or_else(|| SourceError::Malformed {
13927                message: format!(
13928                    "GitHub draft {} board item has no numeric project number",
13929                    id.0
13930                ),
13931            })?;
13932        let project_id = required_str(project, "id")?;
13933        held |= number == u64::from(project_number) && project_id == board;
13934    }
13935    if !held {
13936        return Ok(Some(None));
13937    }
13938    Ok(Some(Some(
13939        metadata_body(optional_str(draft, "body")?.map(str::to_owned))?.1,
13940    )))
13941}
13942
13943fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
13944    let mut page = offset_page(edges, offset, limit.max(1));
13945    page.next = page
13946        .next
13947        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
13948    page
13949}
13950
13951/// The kind of one issue reached through a dependency connection.
13952///
13953/// The same questions the board scan asks, over the fields the dependency document
13954/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
13955/// then anything with sub-issues or the marker is a project.
13956///
13957/// # Errors
13958///
13959/// A far end this board holds as a document is refused rather than reported. The two
13960/// answers that are not refusals would both be wrong: reporting it as a task names an id
13961/// no task read of this source can find, and reporting it as a project names one no
13962/// project read can. There is no third value to return — `ItemKind` has no document
13963/// variant, because nothing may point at a document — so the relationship itself is what
13964/// the person is told about.
13965fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
13966    let id = required_str(value, "id")?;
13967    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
13968        return Err(SourceError::Refused {
13969            message: format!(
13970                "GitHub issue {id} is a document of this board — its title begins \
13971                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
13972                 on by one; next: remove that issue's blocking relationship on this board"
13973            ),
13974        });
13975    }
13976    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
13977    if parent.is_some() {
13978        return Ok(ItemKind::Task);
13979    }
13980    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
13981    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
13982        message: format!("GitHub issue {id}: {message}"),
13983    })?;
13984    let sub_issues = sub_issue_total(value)?;
13985    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
13986        ItemKind::Project
13987    } else {
13988        ItemKind::Task
13989    })
13990}
13991
13992/// The `stateInput` that closes an issue for `reason`.
13993fn closed_input(reason: ClosedState) -> Value {
13994    json!({"value":"CLOSED","stateReason":reason.reason()})
13995}
13996
13997/// The `IssueStateUpdateInput` one status target asks for.
13998///
13999/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
14000/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
14001/// a currently-closed issue: without that the item would read back `Unknown` and a copy
14002/// would report a change forever. A document has no status at all, and asks for neither.
14003fn state_input(target: Option<&StatusTarget>) -> Value {
14004    match target {
14005        Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
14006        Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
14007        // A document has no status, so a write of one says nothing about the issue's open
14008        // or closed state rather than forcing it open: `stateInput` is what carries that
14009        // instruction, and an explicit null asks for no change to it.
14010        None => Value::Null,
14011    }
14012}
14013
14014/// The metadata one write stores in the item's body slot.
14015///
14016/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
14017/// rather than carried: the kind marker so an empty project stays readable, the
14018/// repository list only when it is not exactly the issue's own repository, and the far
14019/// ends no relationship here can name.
14020///
14021/// The copy origin is the one typed field that is also mirrored here, and only as a
14022/// mirror: it lands in the board's origin field as well, which stays the one every reader
14023/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
14024/// and catches up with a write in seconds rather than minutes — can find the item by it.
14025/// A reader of the release before this one drops the slot's copy and reads the field, so an
14026/// item written here still reads with exactly one origin there.
14027fn slot_metadata(
14028    incoming: &Incoming<'_>,
14029    own_repository: Option<&Repository>,
14030    fallback: &[DependencyEdge],
14031) -> BTreeMap<String, Value> {
14032    let mut metadata = incoming.metadata.clone();
14033    match metadata.remove(ORIGIN_KEY) {
14034        Some(Value::String(origin)) if !origin.is_empty() => {
14035            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
14036        }
14037        _ => {}
14038    }
14039    match incoming.written.kind() {
14040        BoardKind::Work(kind) => metadata.insert(
14041            ItemKind::METADATA_KEY.to_owned(),
14042            Value::String(kind.marker().to_owned()),
14043        ),
14044        // A document is told by its title, so it carries no kind marker: that key names
14045        // what a dependency endpoint points at, and nothing may point at a document.
14046        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
14047    };
14048    incoming.classification.record(&mut metadata);
14049    let derivable = own_repository
14050        .map(|own| incoming.repositories == [own.clone()])
14051        .unwrap_or(incoming.repositories.is_empty());
14052    if derivable {
14053        metadata.remove(Repository::METADATA_KEY);
14054    } else {
14055        metadata.insert(
14056            Repository::METADATA_KEY.to_owned(),
14057            Value::Array(
14058                incoming
14059                    .repositories
14060                    .iter()
14061                    .map(|repository| Value::String(repository.as_str().to_owned()))
14062                    .collect(),
14063            ),
14064        );
14065    }
14066    // The typed lists are what land, whatever the caller's own metadata held under their
14067    // keys: a key of either name travelling beside the field would otherwise be a second
14068    // answer to the same question, and the field is the one the contract names.
14069    for (key, entries) in [
14070        (TaskRef::DELIVERS_KEY, incoming.delivers),
14071        (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
14072    ] {
14073        set_task_list(&mut metadata, key, entries);
14074    }
14075    record_edges(&mut metadata, fallback);
14076    metadata
14077}
14078
14079/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
14080/// one slot's metadata, or no such key when there are none.
14081fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
14082    if fallback.is_empty() {
14083        metadata.remove(DependencyEdge::RECORDED_KEY);
14084    } else {
14085        metadata.insert(
14086            DependencyEdge::RECORDED_KEY.to_owned(),
14087            Value::Array(
14088                fallback
14089                    .iter()
14090                    .map(|edge| {
14091                        serde_json::to_value(&edge.to).expect("an endpoint renders as JSON")
14092                    })
14093                    .collect(),
14094            ),
14095        );
14096    }
14097}
14098
14099/// Every label one item carries, from its content's own connection and nowhere else.
14100///
14101/// There is no second place to read one from: no document this source sends selects the
14102/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
14103/// cannot carry one at all. The module documentation records the three schema facts that
14104/// settle it.
14105fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
14106    optional_nodes(content.get("labels"), "content labels")?
14107        .into_iter()
14108        .flatten()
14109        .map(|v| {
14110            Ok(Label {
14111                id: NativeId(required_str(v, "id")?.to_owned()),
14112                name: required_str(v, "name")?.to_owned(),
14113                color: optional_str(v, "color")?.map(str::to_owned),
14114            })
14115        })
14116        .collect()
14117}
14118
14119/// Each board field value one item holds, as the change token's facets name it: the field's
14120/// name, with the single-select option's name or the text. A value of a type no read here
14121/// selects, and one naming no field, says nothing a person can rename and is left out.
14122fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
14123    field_values
14124        .iter()
14125        .filter_map(|value| {
14126            let field = value.pointer("/field/name")?.as_str()?;
14127            let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
14128            Some((field.to_owned(), held.to_owned()))
14129        })
14130        .collect()
14131}
14132
14133/// The digest of the facets of the issue `content`, whose board item's values are
14134/// `field_values` — or `None` when the response did not carry, whole, every member they are read
14135/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
14136/// label, and every field value.
14137///
14138/// What a write's own response spells its token from, by exactly the rules a read takes them by.
14139fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
14140    let ended = |connection: &Value| {
14141        connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
14142    };
14143    let labels = content.get("labels").filter(|labels| ended(labels))?;
14144    if !ended(field_values) {
14145        return None;
14146    }
14147    let parent = content.get("parent")?;
14148    let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
14149    let kind = BoardKind::of(
14150        content.get("title")?.as_str()?,
14151        !parent.is_null(),
14152        sub_issue_total(content).ok()?,
14153        ItemKind::from_metadata(&slot).ok()?,
14154    );
14155    let labels = self::labels(&json!({"labels": labels})).ok()?;
14156    let nodes = field_values.get("nodes")?.as_array()?;
14157    Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
14158}
14159
14160/// The definition of each board field one item's values are values of, in the shape a read
14161/// of the board's own `fields` gives one.
14162///
14163/// A value names its field through a fragment on that field's own type, so the type is
14164/// known from which kind of value it is: a single-select value's field is a
14165/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
14166/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
14167fn field_definitions(field_values: &[Value]) -> Vec<Value> {
14168    field_values
14169        .iter()
14170        .filter_map(|value| {
14171            let field = value.get("field")?.as_object()?;
14172            field.get("id")?.as_str().filter(|id| !id.is_empty())?;
14173            let typename = if value.get("text").is_some() {
14174                "ProjectV2Field"
14175            } else if value.get("name").is_some() {
14176                "ProjectV2SingleSelectField"
14177            } else {
14178                return None;
14179            };
14180            let mut defined = field.clone();
14181            defined.insert("__typename".to_owned(), json!(typename));
14182            // Only a text field holds a text value, so the value says the field's type too.
14183            if typename == "ProjectV2Field" {
14184                defined.insert("dataType".to_owned(), json!("TEXT"));
14185            }
14186            Some(Value::Object(defined))
14187        })
14188        .collect()
14189}
14190
14191fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
14192    let Some(node) = field_values
14193        .iter()
14194        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
14195    else {
14196        return Ok(None);
14197    };
14198    Ok(optional_str(node, "text")?.map(str::to_owned))
14199}
14200
14201fn valid_github_owner(owner: &str) -> bool {
14202    !owner.is_empty()
14203        && owner.len() <= 39
14204        && !owner.starts_with('-')
14205        && !owner.ends_with('-')
14206        && !owner.contains("--")
14207        && owner
14208            .bytes()
14209            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
14210}
14211
14212/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
14213/// neither of the two names a path segment already means.
14214fn valid_github_repository_name(name: &str) -> bool {
14215    !name.is_empty()
14216        && name.len() <= 100
14217        && name != "."
14218        && name != ".."
14219        && name
14220            .bytes()
14221            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
14222}
14223
14224fn valid_environment_name(name: &str) -> bool {
14225    let mut bytes = name.bytes();
14226    bytes
14227        .next()
14228        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
14229        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
14230}
14231
14232/// How many sub-issues one issue has.
14233///
14234/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
14235/// absent or non-integer one is a response this source cannot read — and reading it as
14236/// zero would classify a project as a task, which is exactly the mistake the marker
14237/// exists to keep from happening quietly.
14238fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
14239    let summary = issue
14240        .get("subIssuesSummary")
14241        .ok_or_else(|| SourceError::Malformed {
14242            message: "GitHub issue is missing subIssuesSummary".into(),
14243        })?;
14244    summary
14245        .get("total")
14246        .and_then(Value::as_u64)
14247        .ok_or_else(|| SourceError::Malformed {
14248            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
14249        })
14250}
14251
14252/// One issue's own `number`.
14253///
14254/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
14255/// an issue in this module asks for it. So a read of one that comes back without it, or
14256/// with something that is not an unsigned integer, is a response this source cannot read —
14257/// absence here is **not** "this issue has no number". A draft is the content that has
14258/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
14259/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
14260fn issue_number(issue: &Value) -> Result<u64, SourceError> {
14261    issue
14262        .get("number")
14263        .and_then(Value::as_u64)
14264        .ok_or_else(|| SourceError::Malformed {
14265            message: "GitHub issue number is missing or is not an unsigned integer".into(),
14266        })
14267}
14268
14269/// The `number` a creating mutation answered with, and `None` when it answered without one;
14270/// why a missing one is tolerated is at the call in `create_and_file_issue`.
14271fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
14272    match created.get("number") {
14273        None | Some(Value::Null) => Ok(None),
14274        Some(value) => value
14275            .as_u64()
14276            .map(Some)
14277            .ok_or_else(|| SourceError::Malformed {
14278                message: "GitHub created issue number is not an unsigned integer".into(),
14279            }),
14280    }
14281}
14282
14283fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
14284    value
14285        .get(field)
14286        .and_then(Value::as_str)
14287        .ok_or_else(|| SourceError::Malformed {
14288            message: format!("GitHub response is missing string field {field}"),
14289        })
14290}
14291
14292fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
14293    let found = required_str(value, field)?;
14294    if found.trim().is_empty() {
14295        return Err(SourceError::Malformed {
14296            message: format!("GitHub response has blank string field {field}"),
14297        });
14298    }
14299    Ok(found)
14300}
14301
14302/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
14303/// needs one — Linear spells them too, in its own description field.
14304///
14305/// Restated rather than shared, because a plugin crate depends on the contract crate and
14306/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
14307/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
14308/// source round-trips its own writes perfectly well under its own spelling.
14309const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
14310const METADATA_CLOSE: &str = "\n-->";
14311
14312/// What the composer puts between a non-empty visible body and the slot, and the one thing
14313/// the parser takes off the visible body when it takes the slot off — exactly once, so every
14314/// other trailing byte of the body comes back as it was written.
14315// 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.
14316const METADATA_SEPARATOR: &str = "\n\n";
14317
14318/// The visible body and the metadata slot at the end of it.
14319///
14320/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
14321/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
14322/// own content and is left alone. The visible body is everything before the slot less the
14323/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
14324fn metadata_body(
14325    body: Option<String>,
14326) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
14327    let Some(body) = body else {
14328        return Ok((None, BTreeMap::new()));
14329    };
14330    let Some(slot) = slot_span(&body)? else {
14331        return Ok((Some(body), BTreeMap::new()));
14332    };
14333    let metadata =
14334        serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
14335            SourceError::Malformed {
14336                message: format!(
14337                    "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
14338                ),
14339            }
14340        })?;
14341    let before = &body[..slot.start];
14342    let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
14343    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
14344}
14345
14346/// Where the metadata slot sits in one body, as byte offsets into it.
14347struct SlotSpan {
14348    /// Where [`METADATA_OPEN`] begins.
14349    start: usize,
14350    /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
14351    encoded_start: usize,
14352    /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
14353    encoded_end: usize,
14354    /// Just past [`METADATA_CLOSE`].
14355    end: usize,
14356}
14357
14358/// The slot at the very end of `body`, or `None` when it has none.
14359///
14360/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
14361/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
14362/// slot.
14363fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
14364    let Some(start) = body.rfind(METADATA_OPEN) else {
14365        return Ok(None);
14366    };
14367    let encoded_start = start + METADATA_OPEN.len();
14368    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
14369        return Err(SourceError::Malformed {
14370            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
14371        });
14372    };
14373    let encoded_end = encoded_start + relative_end;
14374    let end = encoded_end + METADATA_CLOSE.len();
14375    if !body[end..].trim().is_empty() {
14376        return Ok(None);
14377    }
14378    Ok(Some(SlotSpan {
14379        start,
14380        encoded_start,
14381        encoded_end,
14382        end,
14383    }))
14384}
14385
14386/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
14387/// slot as it was.
14388///
14389/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
14390/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
14391/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
14392/// or alone in an empty body — and a body with no slot that is given no metadata is
14393/// returned as it is.
14394fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
14395    let encoded = if metadata.is_empty() {
14396        None
14397    } else {
14398        Some(
14399            serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
14400                message: error.to_string(),
14401            })?,
14402        )
14403    };
14404    Ok(match (slot_span(body)?, encoded) {
14405        (Some(slot), Some(encoded)) => format!(
14406            "{}{encoded}{}",
14407            &body[..slot.encoded_start],
14408            &body[slot.encoded_end..]
14409        ),
14410        (Some(slot), None) => {
14411            let before = &body[..slot.start];
14412            format!(
14413                "{}{}",
14414                before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
14415                &body[slot.end..]
14416            )
14417        }
14418        (None, None) => body.to_owned(),
14419        (None, Some(encoded)) if body.is_empty() => {
14420            format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14421        }
14422        (None, Some(encoded)) => {
14423            format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14424        }
14425    })
14426}
14427
14428/// `body` with everything before its metadata slot replaced by `content`, and the slot
14429/// itself kept byte for byte.
14430///
14431/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
14432/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
14433/// `content` is empty — so a read of the result reports `content` as the visible body and
14434/// the slot's metadata exactly as it was.
14435fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
14436    let Some(slot) = slot_span(body)? else {
14437        return Ok(content.to_owned());
14438    };
14439    let kept = &body[slot.start..];
14440    Ok(if content.is_empty() {
14441        kept.to_owned()
14442    } else {
14443        format!("{content}{METADATA_SEPARATOR}{kept}")
14444    })
14445}
14446
14447/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
14448fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
14449    if entries.is_empty() {
14450        metadata.remove(key);
14451    } else {
14452        metadata.insert(
14453            key.to_owned(),
14454            Value::Array(
14455                entries
14456                    .iter()
14457                    .map(|entry| Value::String(entry.as_str().to_owned()))
14458                    .collect(),
14459            ),
14460        );
14461    }
14462}
14463
14464fn compose_body(
14465    content: Option<&str>,
14466    metadata: &BTreeMap<String, Value>,
14467) -> Result<Option<String>, SourceError> {
14468    let visible = content.unwrap_or_default();
14469    if metadata.is_empty() {
14470        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
14471    }
14472    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
14473        message: error.to_string(),
14474    })?;
14475    Ok(Some(if visible.is_empty() {
14476        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14477    } else {
14478        format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14479    }))
14480}
14481
14482fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
14483    value
14484        .get(field)
14485        .and_then(Value::as_bool)
14486        .ok_or_else(|| SourceError::Malformed {
14487            message: format!("GitHub response is missing boolean field {field}"),
14488        })
14489}
14490fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
14491    match value.get(field) {
14492        None | Some(Value::Null) => Ok(None),
14493        Some(value) => value
14494            .as_str()
14495            .map(Some)
14496            .ok_or_else(|| SourceError::Malformed {
14497                message: format!("GitHub response field {field} is not a string or null"),
14498            }),
14499    }
14500}
14501fn optional_nodes<'a>(
14502    connection: Option<&'a Value>,
14503    name: &str,
14504) -> Result<Option<&'a Vec<Value>>, SourceError> {
14505    match connection {
14506        None | Some(Value::Null) => Ok(None),
14507        Some(value) => value
14508            .get("nodes")
14509            .and_then(Value::as_array)
14510            .map(Some)
14511            .ok_or_else(|| SourceError::Malformed {
14512                message: format!("GitHub {name}.nodes is not an array"),
14513            }),
14514    }
14515}
14516fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
14517    let page_info = connection
14518        .get("pageInfo")
14519        .ok_or_else(|| SourceError::Malformed {
14520            message: format!("GitHub {name} has no pageInfo"),
14521        })?;
14522    if required_bool(page_info, "hasNextPage")? {
14523        return Err(SourceError::Malformed {
14524            message: format!(
14525                "GitHub {name} exceeds the supported nested connection size of {size}"
14526            ),
14527        });
14528    }
14529    Ok(())
14530}
14531fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
14532    optional_str(value, field)?
14533        .map(|timestamp| {
14534            timestamp.parse().map_err(|error| SourceError::Malformed {
14535                message: format!("GitHub response field {field} is not a timestamp: {error}"),
14536            })
14537        })
14538        .transpose()
14539}
14540fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
14541    if page.limit == 0 {
14542        Err(SourceError::Config {
14543            message: "page limit must be at least 1".into(),
14544        })
14545    } else {
14546        Ok(())
14547    }
14548}
14549fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
14550    let page = connection
14551        .get("pageInfo")
14552        .filter(|value| value.is_object())
14553        .ok_or_else(|| SourceError::Malformed {
14554            message: "GitHub connection is missing pageInfo".into(),
14555        })?;
14556    if required_bool(page, "hasNextPage")? {
14557        let cursor = required_str(page, "endCursor")?;
14558        validate_cursor_progress(None, cursor)?;
14559        Ok(Some(Cursor(cursor.into())))
14560    } else {
14561        Ok(None)
14562    }
14563}
14564fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
14565    if next.is_empty() || previous == Some(next) {
14566        Err(SourceError::Malformed {
14567            message: "GitHub pagination cursor is empty or did not advance".into(),
14568        })
14569    } else {
14570        Ok(())
14571    }
14572}
14573/// The version of this plugin's opaque narrowing-search cursor.
14574pub const SEARCH_CURSOR_VERSION: u32 = 4;
14575
14576#[derive(Serialize, Deserialize)]
14577#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
14578enum SearchConnection {
14579    Initial {},
14580    Continuing { after: Cursor },
14581    Exhausted {},
14582}
14583impl SearchConnection {
14584    fn after(&self) -> Option<&str> {
14585        match self {
14586            Self::Continuing { after } => Some(&after.0),
14587            _ => None,
14588        }
14589    }
14590    fn exhausted(&self) -> bool {
14591        matches!(self, Self::Exhausted { .. })
14592    }
14593    /// Whether a cursor naming this position, `offset` rows into its page, is one this
14594    /// plugin could have handed out: a page is resumed only part of the way through it — an
14595    /// offset of a whole page or more would skip rows nobody was given — an initial page
14596    /// only once some of it was handed out, and an exhausted connection has no page to be
14597    /// part of the way through.
14598    fn valid_resume(&self, offset: usize) -> bool {
14599        let within = offset < SEARCH_PAGE_SIZE as usize;
14600        match self {
14601            Self::Initial { .. } => offset > 0 && within,
14602            Self::Continuing { after } => !after.0.is_empty() && within,
14603            Self::Exhausted { .. } => offset == 0,
14604        }
14605    }
14606}
14607
14608/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
14609#[derive(Serialize, Deserialize)]
14610#[serde(deny_unknown_fields)]
14611struct SearchPosition {
14612    version: u32,
14613    connection: SearchConnection,
14614    /// How many rows of the page `connection` starts were already handed out.
14615    #[serde(default, skip_serializing_if = "is_zero")]
14616    offset: usize,
14617    #[serde(default, skip_serializing_if = "Vec::is_empty")]
14618    seen: Vec<NativeId>,
14619    #[serde(default, skip_serializing_if = "Vec::is_empty")]
14620    own: Vec<NativeId>,
14621}
14622impl Default for SearchPosition {
14623    fn default() -> Self {
14624        Self {
14625            version: SEARCH_CURSOR_VERSION,
14626            connection: SearchConnection::Initial {},
14627            offset: 0,
14628            seen: Vec::new(),
14629            own: Vec::new(),
14630        }
14631    }
14632}
14633
14634fn is_zero(offset: &usize) -> bool {
14635    *offset == 0
14636}
14637
14638fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
14639    cursor.map_or(Ok(0), |c| {
14640        c.0.parse().map_err(|_| SourceError::Config {
14641            message: "page cursor is invalid".into(),
14642        })
14643    })
14644}
14645fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
14646    if offset > items.len() {
14647        return Page::last(vec![]);
14648    }
14649    let tail = items.split_off(offset);
14650    let mut selected = tail;
14651    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
14652    selected.truncate(limit);
14653    Page {
14654        items: selected,
14655        next,
14656    }
14657}
14658
14659impl GitHubProjectsSource {
14660    async fn write_task_assets(
14661        &self,
14662        write: &ItemWrite<Task>,
14663        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
14664    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14665        self.write_item(
14666            &self.task_incoming(write, assets)?,
14667            write.target.as_ref(),
14668            &write.depends_on,
14669        )
14670        .await
14671    }
14672
14673    /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
14674    /// are known to be ones this source can write.
14675    fn task_incoming<'a>(
14676        &self,
14677        write: &'a ItemWrite<Task>,
14678        assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
14679    ) -> Result<Incoming<'a>, SourceError> {
14680        let near = write.target.as_ref().unwrap_or(&write.item.id);
14681        for (key, entries) in [
14682            (TaskRef::DELIVERS_KEY, &write.item.delivers),
14683            (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
14684        ] {
14685            TaskRef::listed(key, near, Some(&self.name), entries.clone())
14686                .map_err(|message| SourceError::Refused { message })?;
14687        }
14688        if self.priorities.is_none() && write.item.priority != Priority::None {
14689            return Err(self.holds_no_priority());
14690        }
14691        Ok(Incoming {
14692            written: Written::Work(ItemKind::Task, &write.item.status),
14693            title: &write.item.title,
14694            content: write.item.content.as_deref(),
14695            assets,
14696            labels: &write.item.labels,
14697            metadata: &write.item.metadata,
14698            repositories: &write.item.repositories,
14699            classification: write.item.classification,
14700            parent: write.item.project.as_ref(),
14701            delivers: &write.item.delivers,
14702            delivered_by: &write.item.delivered_by,
14703            priority: self.priorities.as_ref().map(|_| write.item.priority),
14704        })
14705    }
14706    async fn write_document_assets(
14707        &self,
14708        write: &ItemWrite<Document>,
14709        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
14710    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14711        // A document takes part in no dependency graph, so there is no far end to write
14712        // natively and none to record: a caller naming one is told so rather than having it
14713        // stored under the reserved key, where a later read would report an edge the
14714        // contract says cannot exist.
14715        if !write.depends_on.is_empty() {
14716            return Err(SourceError::Refused {
14717                message: format!(
14718                    "this write names {} dependencies for a document, and a document takes \
14719                     part in no dependency graph; next: put the dependency on the task or \
14720                     project the document is about",
14721                    write.depends_on.len()
14722                ),
14723            });
14724        }
14725        self.write_item(
14726            &Incoming {
14727                written: Written::Document,
14728                title: &write.item.title,
14729                content: write.item.content.as_deref(),
14730                assets,
14731                labels: &write.item.labels,
14732                metadata: &write.item.metadata,
14733                repositories: &write.item.repositories,
14734                classification: write.item.classification,
14735                parent: write.item.project.as_ref(),
14736                delivers: &[],
14737                delivered_by: &[],
14738                priority: None,
14739            },
14740            write.target.as_ref(),
14741            &[],
14742        )
14743        .await
14744    }
14745}
14746
14747/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
14748/// itself, for the two things no journey can observe.
14749///
14750/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
14751/// with and without the call, that a settlement, a board listing and a metadata search each
14752/// read afresh after it — the resolved records, the written-item overlay, the board and its
14753/// search, and the narrowed searches. What they cannot reach is the held field definitions,
14754/// because a status write naming an option a person deleted is refused the same whether or
14755/// not the list is held, and a poisoned lock, because nothing outside the source can panic
14756/// while one of its locks is held. So these assert those directly, and every other holder
14757/// beside them so a holder added later without a clear in the call fails here.
14758#[cfg(test)]
14759mod end_command_tests {
14760    use super::*;
14761
14762    struct Token;
14763
14764    impl SecretResolver for Token {
14765        fn get(&self, var: &str) -> Option<SecretString> {
14766            (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
14767        }
14768    }
14769
14770    fn source() -> GitHubProjectsSource {
14771        let config = serde_json::from_value(json!({
14772            "owner": "octo-org", "project_number": 7, "repository": "acme/work",
14773            // Nothing here is sent: the source is only built and its state inspected.
14774            "endpoint": "http://127.0.0.1:9/graphql",
14775        }))
14776        .expect("a usable configuration");
14777        GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
14778            .expect("the source builds")
14779    }
14780
14781    /// One issue as a board read answers it.
14782    fn resolved(source: &GitHubProjectsSource) -> Resolved {
14783        source
14784            .resolve(&json!({
14785                "id": "ITEM-1",
14786                "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
14787                            "body": "what a person may since have edited", "state": "OPEN",
14788                            "stateReason": null, "url": null, "number": 1,
14789                            "subIssuesSummary": {"total": 0},
14790                            "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
14791                "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
14792            }))
14793            .expect("the item reads")
14794            .expect("an issue")
14795    }
14796
14797    /// Hold something in every holder the call clears, and the repository id it keeps.
14798    fn fill(source: &GitHubProjectsSource) {
14799        let item = resolved(source);
14800        source.created.lock().unwrap().push(item.clone());
14801        source.updated.lock().unwrap().push(item.clone());
14802        *source.board_cache.lock().unwrap() = Some(Board {
14803            id: "PVT-board".into(),
14804            fields: json!({"nodes": []}),
14805            items: vec![item.clone()],
14806        });
14807        *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
14808        source
14809            .narrowed_cache
14810            .lock()
14811            .unwrap()
14812            .insert("status:todo".into(), vec![item.clone()]);
14813        source.children_cache.lock().unwrap().insert(
14814            NativeId("P-1".into()),
14815            (NativeId("P-1".into()), vec![item.clone()]),
14816        );
14817        source
14818            .search_next
14819            .lock()
14820            .unwrap()
14821            .insert("status:todo".into(), Some("cursor".into()));
14822        source
14823            .resolved_cache
14824            .lock()
14825            .unwrap()
14826            .insert(item.id.clone(), item);
14827        *source.fields_cache.lock().unwrap() = Some(BoardFields {
14828            id: BoardId::parse("PVT-board").unwrap(),
14829            fields: json!({"nodes": []}),
14830        });
14831        source
14832            .repository_cache
14833            .lock()
14834            .unwrap()
14835            .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
14836    }
14837
14838    fn assert_dropped(source: &GitHubProjectsSource) {
14839        assert!(source.created().unwrap().is_empty(), "created");
14840        assert!(source.updated().unwrap().is_empty(), "updated");
14841        assert!(source.board_cache().unwrap().is_none(), "board");
14842        assert!(source.search_cache.lock().unwrap().is_none(), "search");
14843        assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
14844        assert!(
14845            source.children_cache.lock().unwrap().is_empty(),
14846            "project children"
14847        );
14848        assert!(
14849            source.search_next.lock().unwrap().is_empty(),
14850            "search paging"
14851        );
14852        assert!(
14853            source.resolved_cache().unwrap().is_empty(),
14854            "resolved records"
14855        );
14856        assert!(source.fields_cache().unwrap().is_none(), "board fields");
14857        assert_eq!(
14858            source.repository_cache().unwrap().len(),
14859            1,
14860            "a repository's node id stays valid and is kept"
14861        );
14862    }
14863
14864    fn end(source: &GitHubProjectsSource) {
14865        tokio::runtime::Builder::new_current_thread()
14866            .build()
14867            .unwrap()
14868            .block_on(source.end_command())
14869            .expect("the command ends");
14870    }
14871
14872    #[test]
14873    fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
14874        let source = source();
14875        fill(&source);
14876        end(&source);
14877        assert_dropped(&source);
14878    }
14879
14880    #[test]
14881    fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
14882        fn poison<T: Send>(held: &Mutex<T>) {
14883            std::thread::scope(|scope| {
14884                let _ = scope
14885                    .spawn(|| {
14886                        let _guard = held.lock().unwrap();
14887                        panic!("a failure while the lock is held");
14888                    })
14889                    .join();
14890            });
14891            assert!(held.is_poisoned());
14892        }
14893        let source = source();
14894        fill(&source);
14895        poison(&source.created);
14896        poison(&source.updated);
14897        poison(&source.board_cache);
14898        poison(&source.search_cache);
14899        poison(&source.narrowed_cache);
14900        poison(&source.search_next);
14901        poison(&source.resolved_cache);
14902        poison(&source.fields_cache);
14903        assert!(
14904            source.resolved_cache().is_err(),
14905            "a poisoned lock is refused before the call"
14906        );
14907        end(&source);
14908        assert_dropped(&source);
14909    }
14910}
14911
14912#[cfg(test)]
14913mod unprojectable_fields_tests {
14914    use super::*;
14915
14916    /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
14917    fn documented() -> std::collections::BTreeSet<String> {
14918        let document = std::fs::read_to_string(
14919            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
14920        )
14921        .expect("docs/metadata.md is readable");
14922        let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
14923        let lead = "a `field` equal, ignoring case, to ";
14924        let start = document.find(lead).expect("the refused-field sentence") + lead.len();
14925        let sentence = &document[start..];
14926        let sentence = &sentence[..sentence
14927            .find("; and two entries naming one field")
14928            .expect("the refused-field sentence ends at the duplicate rule")];
14929        let (own, github) = sentence
14930            .split_once(" or a field GitHub owns on every board (")
14931            .expect("the sentence names GitHub's own fields");
14932        own.split('`')
14933            .skip(1)
14934            .step_by(2)
14935            .chain(
14936                github
14937                    .strip_suffix(')')
14938                    .expect("GitHub's own fields close the sentence")
14939                    .split(", "),
14940            )
14941            .map(str::to_owned)
14942            .collect()
14943    }
14944
14945    #[test]
14946    fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
14947        let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
14948            .iter()
14949            .map(|&name| name.to_owned())
14950            .collect();
14951        assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
14952        let instance = SourceName::new("work").unwrap();
14953        for name in UNPROJECTABLE_FIELDS {
14954            for spelled in [name.to_owned(), name.to_uppercase()] {
14955                let entry = MetadataFieldConfig {
14956                    field: spelled.clone(),
14957                    key: "team.machine".to_owned(),
14958                    path: Vec::new(),
14959                };
14960                let refused = MetadataField::resolve(vec![entry], &instance)
14961                    .expect_err("an unprojectable field is refused");
14962                assert!(
14963                    refused.to_string().contains(&format!("{name:?}")),
14964                    "{spelled}: {refused}"
14965                );
14966            }
14967        }
14968    }
14969}