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, Direction, Document, DocumentQuery, DocumentVersionCoverage, Health,
637    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    /// Creates one issue. Outside a copy it is created on no board, and [`ADD_TO_BOARD`] files
1376    /// it; inside one, its input names the board in `projectV2Ids` and its project in
1377    /// `parentIssueId`, so this one request files it under both — see
1378    /// [`GitHubProjectsSource::create_filed_issue`](super::GitHubProjectsSource).
1379    pub const CREATE_ISSUE: &str =
1380        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1381    /// Puts an existing issue on the configured board.
1382    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1383    /// Updates an issue's visible fields and its open or closed state in one call.
1384    pub const UPDATE_ISSUE: &str =
1385        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1386    /// Updates an existing draft's user-visible fields.
1387    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1388    /// Updates a text or single-select value on one project item.
1389    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}}}}}}}}"#;
1390    /// Writes up to six board fields and clears up to three in one ordered mutation: every
1391    /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1392    /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1393    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}}}"#;
1394    /// Clears one project item's value of one field, which is what a `none` priority is.
1395    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}}}}}}}}"#;
1396    /// Creates one field: a single-select one with its options, or a text one for a projected
1397    /// metadata value. Only the guarded field setup may use this document, and only for a
1398    /// field the board lacks.
1399    pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1400    /// Replaces a single-select field's options. Only the guarded field setup — the
1401    /// `status-options` and `fields` operations — may use this document, because GitHub
1402    /// treats the input as the complete option list.
1403    pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1404    /// A fresh snapshot of the Status field and every board item's assignment.
1405    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}}}}}}"#;
1406    /// Files one issue under another as a sub-issue, which is what project membership is.
1407    pub const ADD_SUB_ISSUE: &str =
1408        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1409    /// Takes one issue back out of its parent.
1410    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1411    /// Adds GitHub's native issue blocked-by relationship.
1412    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1413    /// One origin of [`ORIGIN_LOOKUPS`]: [`ORIGIN_LOOKUP`]'s two connections for the origin the
1414    /// `$filter<n>` and `$search<n>` of slot `<n>` name, sent only while `$ask<n>` holds.
1415    macro_rules! origin_lookup_alias {
1416        ($n:literal) => {
1417            concat!(
1418                "\n      o",
1419                stringify!($n),
1420                ":repositoryOwner(login:$owner) @include(if:$ask",
1421                stringify!($n),
1422                "){... on ProjectV2Owner{projectV2(number:$number){items(first:$originFirst,query:$filter",
1423                stringify!($n),
1424                "){nodes{id updatedAt ",
1425                board_item_values!(),
1426                board_item_content!(),
1427                "} pageInfo{hasNextPage endCursor}}}}}\n      s",
1428                stringify!($n),
1429                ":search(query:$search",
1430                stringify!($n),
1431                ",type:$type,first:$originFirst) @include(if:$ask",
1432                stringify!($n),
1433                "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}"
1434            )
1435        };
1436    }
1437
1438    /// The first page of [`ORIGIN_LOOKUP`] for up to [`ORIGIN_BATCH`](super::ORIGIN_BATCH)
1439    /// origins a copy is about to look for, and everything creating an item then reads —
1440    /// the board's own id and field definitions, as [`CREATION_CONTEXT`] reads them, and the node
1441    /// id and the numeric id of the repository an issue is created in — in one request.
1442    ///
1443    /// What a copy reads before it writes anything, so that finding none of its records on the
1444    /// board and creating every one costs this read and the writes. An origin whose page says
1445    /// there is more is left to its own [`ORIGIN_LOOKUP`]. The repository's numeric id is what an
1446    /// image upload names, so a copy carrying assets needs no `GET /repos/{owner}/{repo}` either.
1447    pub const ORIGIN_LOOKUPS: &str = concat!(
1448        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!"#,
1449        relationship_events_variable!(),
1450        r#"){
1451      boardFields:repositoryOwner(login:$owner){
1452        ... on ProjectV2Owner{projectV2(number:$number){id
1453          fields(first:$nestedFirst){nodes{
1454            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1455            ... on ProjectV2Field{__typename id name dataType}
1456          }pageInfo{hasNextPage}}
1457        }}
1458      }
1459      repository(owner:$repositoryOwner,name:$repositoryName){id databaseId nameWithOwner}"#,
1460        origin_lookup_alias!(0),
1461        origin_lookup_alias!(1),
1462        origin_lookup_alias!(2),
1463        origin_lookup_alias!(3),
1464        origin_lookup_alias!(4),
1465        origin_lookup_alias!(5),
1466        origin_lookup_alias!(6),
1467        origin_lookup_alias!(7),
1468        origin_lookup_alias!(8),
1469        origin_lookup_alias!(9),
1470        "\n    }",
1471        board_issue!()
1472    );
1473
1474    /// One slot `<n>` of a copy's aliased writes: `$<prefix><n>` is its input and `$send<n>`
1475    /// whether it is sent.
1476    macro_rules! write_slot {
1477        ($prefix:literal, $field:literal, $selection:literal, $n:literal) => {
1478            concat!(
1479                " ",
1480                $prefix,
1481                stringify!($n),
1482                ":",
1483                $field,
1484                "(input:$",
1485                $prefix,
1486                stringify!($n),
1487                ") @include(if:$send",
1488                stringify!($n),
1489                "){",
1490                $selection,
1491                "}"
1492            )
1493        };
1494    }
1495
1496    /// The variables of a copy's aliased writes: eleven inputs of `$type` named `$<prefix><n>`,
1497    /// and eleven `$send<n>`.
1498    macro_rules! write_variables {
1499        ($prefix:literal, $type:literal) => {
1500            concat!(
1501                "mutation(",
1502                "$",
1503                $prefix,
1504                "0:",
1505                $type,
1506                "!,$",
1507                $prefix,
1508                "1:",
1509                $type,
1510                "!,$",
1511                $prefix,
1512                "2:",
1513                $type,
1514                "!,$",
1515                $prefix,
1516                "3:",
1517                $type,
1518                "!,$",
1519                $prefix,
1520                "4:",
1521                $type,
1522                "!,$",
1523                $prefix,
1524                "5:",
1525                $type,
1526                "!,$",
1527                $prefix,
1528                "6:",
1529                $type,
1530                "!,$",
1531                $prefix,
1532                "7:",
1533                $type,
1534                "!,$",
1535                $prefix,
1536                "8:",
1537                $type,
1538                "!,$",
1539                $prefix,
1540                "9:",
1541                $type,
1542                "!,$",
1543                $prefix,
1544                "10:",
1545                $type,
1546                "!,",
1547                "$send0:Boolean!,$send1:Boolean!,$send2:Boolean!,$send3:Boolean!,$send4:Boolean!,",
1548                "$send5:Boolean!,$send6:Boolean!,$send7:Boolean!,$send8:Boolean!,$send9:Boolean!,",
1549                "$send10:Boolean!){"
1550            )
1551        };
1552    }
1553
1554    /// A copy's aliased writes of one kind: [`MUTATION_BATCH`](super::MUTATION_BATCH) slots of
1555    /// `$field`, each sent only while its `$send<n>` holds.
1556    macro_rules! write_batch {
1557        ($prefix:literal, $type:literal, $field:literal, $selection:literal) => {
1558            concat!(
1559                write_variables!($prefix, $type),
1560                write_slot!($prefix, $field, $selection, 0),
1561                write_slot!($prefix, $field, $selection, 1),
1562                write_slot!($prefix, $field, $selection, 2),
1563                write_slot!($prefix, $field, $selection, 3),
1564                write_slot!($prefix, $field, $selection, 4),
1565                write_slot!($prefix, $field, $selection, 5),
1566                write_slot!($prefix, $field, $selection, 6),
1567                write_slot!($prefix, $field, $selection, 7),
1568                write_slot!($prefix, $field, $selection, 8),
1569                write_slot!($prefix, $field, $selection, 9),
1570                write_slot!($prefix, $field, $selection, 10),
1571                "}"
1572            )
1573        };
1574    }
1575
1576    /// Creates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues in one request, each filed on
1577    /// the board and under its project by the same `createIssue` — its `projectV2Ids` and
1578    /// `parentIssueId` — as [`CREATE_ISSUE`] creates one.
1579    pub const CREATE_ISSUES: &str = write_batch!(
1580        "create",
1581        "CreateIssueInput",
1582        "createIssue",
1583        "issue{id number url}"
1584    );
1585
1586    /// One issue of [`CREATED_BOARD_ITEMS`]: the board items the issue `$id<n>` names is on, with
1587    /// the `Status` value of each, read only while `$read<n>` holds.
1588    macro_rules! created_item_alias {
1589        ($n:literal) => {
1590            concat!(
1591                "\n      i",
1592                stringify!($n),
1593                ":node(id:$id",
1594                stringify!($n),
1595                ") @include(if:$read",
1596                stringify!($n),
1597                "){... on Issue{id projectItems(first:$boardItems){nodes{id project{id} status:fieldValueByName(name:$statusName){... on ProjectV2ItemFieldSingleSelectValue{name}}}}}}"
1598            )
1599        };
1600    }
1601
1602    /// The board item of each issue a copy created, up to
1603    /// [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH) at a time, with its `Status` as the board
1604    /// set it: what `createIssue`'s `projectV2Ids` files the issue as, which GitHub adds after
1605    /// answering the create, so its id comes from a read made once every create has been sent.
1606    pub const CREATED_BOARD_ITEMS: &str = concat!(
1607        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!){"#,
1608        created_item_alias!(0),
1609        created_item_alias!(1),
1610        created_item_alias!(2),
1611        created_item_alias!(3),
1612        created_item_alias!(4),
1613        created_item_alias!(5),
1614        created_item_alias!(6),
1615        created_item_alias!(7),
1616        created_item_alias!(8),
1617        created_item_alias!(9),
1618        created_item_alias!(10),
1619        created_item_alias!(11),
1620        created_item_alias!(12),
1621        created_item_alias!(13),
1622        created_item_alias!(14),
1623        created_item_alias!(15),
1624        created_item_alias!(16),
1625        created_item_alias!(17),
1626        created_item_alias!(18),
1627        created_item_alias!(19),
1628        created_item_alias!(20),
1629        "\n    }"
1630    );
1631
1632    /// Up to [`MUTATION_BATCH`](super::MUTATION_BATCH) board field values a copy writes on the
1633    /// items it created, each on its own item, in one ordered request.
1634    pub const WRITE_FIELD_VALUES: &str = write_batch!(
1635        "field",
1636        "UpdateProjectV2ItemFieldValueInput",
1637        "updateProjectV2ItemFieldValue",
1638        "projectV2Item{id}"
1639    );
1640
1641    /// Closes up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues a copy created at a closing
1642    /// status, each with its own `stateInput` and nothing else, once its option is selected.
1643    pub const CLOSE_ISSUES: &str =
1644        write_batch!("close", "UpdateIssueInput", "updateIssue", "issue{id}");
1645
1646    /// Up to [`MUTATION_BATCH`](super::MUTATION_BATCH) native blocked-by relationships a copy adds
1647    /// to the issues it created, in one request.
1648    pub const ADD_BLOCKED_BYS: &str = write_batch!(
1649        "block",
1650        "AddBlockedByInput",
1651        "addBlockedBy",
1652        "issue{id} blockingIssue{id}"
1653    );
1654
1655    /// Removes one native issue blocked-by relationship.
1656    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1657    /// Deletes one issue, which takes its board item with it.
1658    ///
1659    /// The engine sends this in one situation only: undoing a copy that could not finish,
1660    /// over the items that same copy created. Deleting the issue removes the board item
1661    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1662    pub const DELETE_ISSUE: &str =
1663        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1664
1665    /// Everything this source reads about one issue comment, wherever it reaches one.
1666    ///
1667    /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1668    /// and a comment just edited are handed to one mapper, so they are selected by one
1669    /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1670    /// longer exists, and `login` is the one member every kind of actor carries.
1671    macro_rules! issue_comment {
1672        () => {
1673            "id author{login} createdAt updatedAt body url"
1674        };
1675    }
1676
1677    /// One task's comments: a page of its issue's own `comments` connection.
1678    ///
1679    /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1680    /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1681    /// list every time somebody edited it; left unordered the connection answers in the order
1682    /// the comments were written, which is the order GitHub documents for the same collection
1683    /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1684    /// node count and the caller's own page size is pushed straight down.
1685    pub const ISSUE_COMMENTS: &str = concat!(
1686        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1687        issue_comment!(),
1688        r#"}pageInfo{hasNextPage endCursor}}}}}"#
1689    );
1690    /// One issue by its own node id, with a page of its comments: what `task show` and a
1691    /// comment listing read, in one request.
1692    ///
1693    /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1694    /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1695    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1696    /// connection there would multiply through both of those documents' price, and neither
1697    /// needs one.
1698    pub const ISSUE_DETAIL: &str = concat!(
1699        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1700        relationship_events_variable!(),
1701        r#"){
1702      node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1703        issue_comment!(),
1704        r#"}pageInfo{hasNextPage endCursor}}}}
1705    }"#,
1706        board_issue!()
1707    );
1708
1709    /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1710    /// page of its comments when `$comments` asks for them.
1711    macro_rules! issue_details_alias {
1712        ($n:literal) => {
1713            concat!(
1714                "\n      i",
1715                stringify!($n),
1716                ":node(id:$id",
1717                stringify!($n),
1718                "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1719                issue_comment!(),
1720                "}pageInfo{hasNextPage endCursor}}}}"
1721            )
1722        };
1723    }
1724
1725    /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1726    /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1727    ///
1728    /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1729    /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1730    /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1731    /// neither — so every connection under it would be priced at nothing and the pin in
1732    /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1733    /// one-item read the model already prices, so the batch costs what its aliases cost.
1734    ///
1735    /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1736    /// slots it has no item for to the last item it does, and reads that item again; the
1737    /// price is the document's, whatever its variables, so a short batch costs what a full
1738    /// one does and nothing more.
1739    pub const ISSUE_DETAILS: &str = concat!(
1740        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!"#,
1741        relationship_events_variable!(),
1742        r#"){"#,
1743        issue_details_alias!(0),
1744        issue_details_alias!(1),
1745        issue_details_alias!(2),
1746        issue_details_alias!(3),
1747        issue_details_alias!(4),
1748        issue_details_alias!(5),
1749        issue_details_alias!(6),
1750        issue_details_alias!(7),
1751        issue_details_alias!(8),
1752        issue_details_alias!(9),
1753        issue_details_alias!(10),
1754        issue_details_alias!(11),
1755        issue_details_alias!(12),
1756        issue_details_alias!(13),
1757        issue_details_alias!(14),
1758        issue_details_alias!(15),
1759        issue_details_alias!(16),
1760        issue_details_alias!(17),
1761        issue_details_alias!(18),
1762        issue_details_alias!(19),
1763        issue_details_alias!(20),
1764        issue_details_alias!(21),
1765        issue_details_alias!(22),
1766        issue_details_alias!(23),
1767        "\n    }",
1768        board_issue!()
1769    );
1770
1771    /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1772    /// page of its comments and the issues it is blocked by.
1773    macro_rules! issue_copy_details_alias {
1774        ($n:literal) => {
1775            concat!(
1776                "\n      i",
1777                stringify!($n),
1778                ":node(id:$id",
1779                stringify!($n),
1780                "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1781                issue_comment!(),
1782                "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1783            )
1784        };
1785    }
1786
1787    /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1788    /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1789    /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1790    /// at a time, so a copy of a changed task costs this read and nothing else.
1791    ///
1792    /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1793    /// what `task show-many` is priced by and which reads no edge.
1794    ///
1795    /// `children`, read only while `$children` holds, is which issues the issue `$project` has
1796    /// as sub-issues — each one's id and title, the page `$nestedFirst` allows — so that a
1797    /// project copy, which reads its project and every task at once, knows from this same read
1798    /// which tasks the project holds at the destination, and its orphan check is answered by it
1799    /// rather than by a listing of the project.
1800    pub const ISSUE_COPY_DETAILS: &str = concat!(
1801        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!"#,
1802        relationship_events_variable!(),
1803        r#"){"#,
1804        issue_copy_details_alias!(0),
1805        issue_copy_details_alias!(1),
1806        issue_copy_details_alias!(2),
1807        issue_copy_details_alias!(3),
1808        issue_copy_details_alias!(4),
1809        issue_copy_details_alias!(5),
1810        issue_copy_details_alias!(6),
1811        issue_copy_details_alias!(7),
1812        issue_copy_details_alias!(8),
1813        issue_copy_details_alias!(9),
1814        issue_copy_details_alias!(10),
1815        issue_copy_details_alias!(11),
1816        issue_copy_details_alias!(12),
1817        issue_copy_details_alias!(13),
1818        issue_copy_details_alias!(14),
1819        issue_copy_details_alias!(15),
1820        issue_copy_details_alias!(16),
1821        issue_copy_details_alias!(17),
1822        issue_copy_details_alias!(18),
1823        issue_copy_details_alias!(19),
1824        issue_copy_details_alias!(20),
1825        "\n      children:node(id:$project) @include(if:$children){... on Issue{id subIssues(first:$nestedFirst){nodes{id title} pageInfo{hasNextPage}}}}",
1826        "\n    }",
1827        board_issue!(),
1828        related_issue!()
1829    );
1830
1831    /// Which issue one comment is on, read before that comment is edited or removed.
1832    ///
1833    /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1834    /// comment id given against the wrong task would change a comment on another issue.
1835    pub const COMMENT_ISSUE: &str =
1836        r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1837    /// Adds one comment to an issue, signed as the account the token belongs to.
1838    pub const ADD_COMMENT: &str = concat!(
1839        r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1840        issue_comment!(),
1841        r#"}}}}"#
1842    );
1843    /// Replaces the body of one issue comment.
1844    pub const UPDATE_COMMENT: &str = concat!(
1845        r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
1846        issue_comment!(),
1847        r#"}}}"#
1848    );
1849    /// Removes one issue comment. Its payload carries nothing about the comment it removed.
1850    pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
1851
1852    /// What a mutation of a write the store's cache expects selects of the issue it wrote:
1853    /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
1854    /// change token's every part and the first page of its comments.
1855    ///
1856    /// Sent **only** while that write runs, in place of the document named beside each
1857    /// below, so every other write's documents, prices and request counts are what they
1858    /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
1859    /// mutation's response is stamped seconds after the change it made, so against GitHub
1860    /// itself a response rarely determines a version; the cache is then left unserveable
1861    /// for the task, and its next read refreshes it.
1862    macro_rules! whole_issue {
1863        () => {
1864            concat!(
1865                r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
1866                issue_comment!(),
1867                r#"}pageInfo{hasNextPage endCursor}}}"#
1868            )
1869        };
1870    }
1871
1872    /// The variables every document of `whole_issue!` declares beside its own.
1873    macro_rules! whole_variables {
1874        () => {
1875            concat!(
1876                r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1877                relationship_events_variable!()
1878            )
1879        };
1880    }
1881
1882    /// [`ADD_COMMENT`], selecting the whole issue commented on.
1883    pub const ADD_COMMENT_WHOLE: &str = concat!(
1884        r#"mutation($input:AddCommentInput!"#,
1885        whole_variables!(),
1886        r#"){addComment(input:$input){subject{id "#,
1887        whole_issue!(),
1888        r#"} commentEdge{node{"#,
1889        issue_comment!(),
1890        r#"}}}}"#,
1891        board_issue!()
1892    );
1893    /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
1894    pub const UPDATE_COMMENT_WHOLE: &str = concat!(
1895        r#"mutation($input:UpdateIssueCommentInput!"#,
1896        whole_variables!(),
1897        r#"){updateIssueComment(input:$input){issueComment{"#,
1898        issue_comment!(),
1899        r#" issue{"#,
1900        whole_issue!(),
1901        r#"}}}}"#,
1902        board_issue!()
1903    );
1904    /// [`UPDATE_ISSUE`], selecting the whole issue updated.
1905    pub const UPDATE_ISSUE_WHOLE: &str = concat!(
1906        r#"mutation($input:UpdateIssueInput!"#,
1907        whole_variables!(),
1908        r#"){updateIssue(input:$input){issue{id "#,
1909        whole_issue!(),
1910        r#"}}}"#,
1911        board_issue!()
1912    );
1913    /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
1914    pub const UPDATE_FIELD_WHOLE: &str = concat!(
1915        r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
1916        whole_variables!(),
1917        r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
1918        whole_issue!(),
1919        r#"}}}}"#,
1920        board_issue!()
1921    );
1922    /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
1923    pub const CLEAR_FIELD_WHOLE: &str = concat!(
1924        r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
1925        whole_variables!(),
1926        r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
1927        whole_issue!(),
1928        r#"}}}}"#,
1929        board_issue!()
1930    );
1931
1932    /// The document sent in `document`'s place while a write the store's cache expects runs,
1933    /// when it has one.
1934    #[must_use]
1935    pub fn whole_twin(document: &str) -> Option<&'static str> {
1936        [
1937            (ADD_COMMENT, ADD_COMMENT_WHOLE),
1938            (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
1939            (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
1940            (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
1941            (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
1942        ]
1943        .into_iter()
1944        .find(|(plain, _)| *plain == document)
1945        .map(|(_, whole)| whole)
1946    }
1947
1948    /// Every document above, with what this source is doing when it sends one.
1949    ///
1950    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
1951    /// name the call that was refused, and a `match` with a catch-all arm would answer a
1952    /// document added later with "talking to GitHub" and never say so.
1953    ///
1954    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
1955    /// const` here that this list omits, so the two cannot part — which is the same guard
1956    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
1957    pub const DOCUMENTS: [(&str, &str); 48] = [
1958        (SEARCH_ISSUES, "searching this board's issues"),
1959        (ISSUE, "reading one issue"),
1960        (
1961            ISSUE_BOARD_ITEMS,
1962            "reading one issue's board memberships past the page it came with",
1963        ),
1964        (SUB_ISSUES, "reading a project's tasks"),
1965        (BOARD, "reading the board"),
1966        (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
1967        (CLOSED_ITEMS, "reading the board's closed items"),
1968        (
1969            ISSUES_BY_REFERENCE,
1970            "reading a batch of issues by their references",
1971        ),
1972        (BOARD_FIELDS, "reading the board's fields"),
1973        (DRAFT, "reading one draft"),
1974        (REPOSITORY, "reading the destination repository"),
1975        (
1976            PROJECT_VISIBILITY,
1977            "reading whether the board's project is public",
1978        ),
1979        (
1980            CREATION_CONTEXT,
1981            "reading the board's fields and the destination repository",
1982        ),
1983        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
1984        (CREATE_ISSUE, "creating an issue"),
1985        (
1986            ORIGIN_LOOKUPS,
1987            "looking up the items copied from several origins, and what creating one reads",
1988        ),
1989        (CREATE_ISSUES, "creating several issues together"),
1990        (
1991            CREATED_BOARD_ITEMS,
1992            "reading the board items of the issues a copy created",
1993        ),
1994        (
1995            WRITE_FIELD_VALUES,
1996            "writing board fields of the items a copy created",
1997        ),
1998        (CLOSE_ISSUES, "closing the issues a copy created"),
1999        (ADD_BLOCKED_BYS, "recording several dependencies together"),
2000        (ADD_TO_BOARD, "adding an issue to the board"),
2001        (UPDATE_ISSUE, "updating an issue"),
2002        (UPDATE_DRAFT, "updating a draft item"),
2003        (UPDATE_FIELD, "writing a board field"),
2004        (UPDATE_FIELDS, "writing board fields together"),
2005        (CLEAR_FIELD, "clearing a board field"),
2006        (
2007            CREATE_FIELD,
2008            "creating a board single-select field with its options",
2009        ),
2010        (
2011            STATUS_OPTIONS_SNAPSHOT,
2012            "snapshotting board Status options and assignments",
2013        ),
2014        (
2015            STATUS_OPTIONS_UPDATE,
2016            "safely replacing the board Status option list",
2017        ),
2018        (ADD_SUB_ISSUE, "filing an issue under its project"),
2019        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
2020        (ADD_BLOCKED_BY, "recording a dependency"),
2021        (REMOVE_BLOCKED_BY, "removing a dependency"),
2022        (DELETE_ISSUE, "deleting an issue"),
2023        (ISSUE_COMMENTS, "reading a task's comments"),
2024        (ISSUE_DETAIL, "reading one issue with its comments"),
2025        (
2026            ISSUE_DETAILS,
2027            "reading a batch of issues with their comments",
2028        ),
2029        (
2030            ISSUE_COPY_DETAILS,
2031            "reading a batch of issues with their comments and dependencies for a copy",
2032        ),
2033        (COMMENT_ISSUE, "reading which issue a comment is on"),
2034        (ADD_COMMENT, "adding a comment"),
2035        (UPDATE_COMMENT, "editing a comment"),
2036        (DELETE_COMMENT, "deleting a comment"),
2037        (
2038            ADD_COMMENT_WHOLE,
2039            "adding a comment, answered with the whole issue",
2040        ),
2041        (
2042            UPDATE_COMMENT_WHOLE,
2043            "editing a comment, answered with the whole issue",
2044        ),
2045        (
2046            UPDATE_ISSUE_WHOLE,
2047            "updating an issue, answered with it whole",
2048        ),
2049        (
2050            UPDATE_FIELD_WHOLE,
2051            "writing a board field, answered with the whole issue",
2052        ),
2053        (
2054            CLEAR_FIELD_WHOLE,
2055            "clearing a board field, answered with the whole issue",
2056        ),
2057    ];
2058}
2059
2060/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
2061/// under, the variable carrying its input, and the boolean variable that includes it.
2062#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2063pub struct FieldSlot {
2064    /// The key of the answer's `data` this field's payload is under.
2065    pub alias: &'static str,
2066    /// The variable carrying this field's input.
2067    pub variable: &'static str,
2068    /// The boolean variable whose `true` sends this field.
2069    pub include: &'static str,
2070}
2071
2072/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
2073pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
2074    FieldSlot {
2075        alias: "updateProjectV2ItemFieldValue",
2076        variable: "input",
2077        include: "writeFirst",
2078    },
2079    FieldSlot {
2080        alias: "second",
2081        variable: "second",
2082        include: "writeSecond",
2083    },
2084    FieldSlot {
2085        alias: "third",
2086        variable: "third",
2087        include: "writeThird",
2088    },
2089    FieldSlot {
2090        alias: "fourth",
2091        variable: "fourth",
2092        include: "writeFourth",
2093    },
2094    FieldSlot {
2095        alias: "fifth",
2096        variable: "fifth",
2097        include: "writeFifth",
2098    },
2099    FieldSlot {
2100        alias: "sixth",
2101        variable: "sixth",
2102        include: "writeSixth",
2103    },
2104];
2105
2106/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
2107pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
2108    FieldSlot {
2109        alias: "cleared",
2110        variable: "clear",
2111        include: "writeClear",
2112    },
2113    FieldSlot {
2114        alias: "clearedSecond",
2115        variable: "clearSecond",
2116        include: "writeClearSecond",
2117    },
2118    FieldSlot {
2119        alias: "clearedThird",
2120        variable: "clearThird",
2121        include: "writeClearThird",
2122    },
2123];
2124
2125/// Which of GitHub's two rate limiters refused a request.
2126///
2127/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
2128/// secondary one — so an operator told the wrong one takes the wrong next step, which is
2129/// the whole reason this is carried rather than collapsed into "rate limited".
2130#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2131enum Limiter {
2132    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
2133    Primary,
2134    /// The burst limiter over content-generating requests, which nothing reports.
2135    Secondary,
2136}
2137
2138/// The wordings GitHub answers a secondary rate limit with.
2139///
2140/// It sends them under a forbidden status, under a too-many-requests status, and inside
2141/// the `errors` of a *successful* response, which is why the text is what this matches on
2142/// rather than the status. `abuse detection` is the wording GitHub used before the
2143/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
2144/// what a burst of content creation is refused with.
2145///
2146/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
2147/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
2148/// when, and the drift gate reconciles the two lists both ways. Public for that gate
2149/// alone — a caller has no use for it, and matching on a refusal is this source's job.
2150pub const SECONDARY_WORDINGS: [&str; 5] = [
2151    "secondary rate limit",
2152    "temporarily blocked from content creation",
2153    "abuse detection",
2154    "submitted too quickly",
2155    "exceeded a secondary",
2156];
2157
2158/// The wordings GitHub answers an exhausted primary budget with.
2159///
2160/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
2161/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
2162/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
2163/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
2164/// two phrases is a substring of it, so without it that answer read as a refusal that will
2165/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
2166/// one reason.
2167pub const PRIMARY_WORDINGS: [&str; 4] = [
2168    "api rate limit exceeded",
2169    "api rate limit already exceeded",
2170    "rate limit exceeded",
2171    "rate_limited",
2172];
2173
2174/// What a response *says about itself*, which is the only place a refusal can be read.
2175///
2176/// Deliberately not the whole response body. A board is a place people write about their
2177/// own work, and a task on it titled "the secondary rate limit" would, matched across the
2178/// raw text, turn a perfectly good answer into a refusal this source then waited out and
2179/// reported. So the item data is never read: what is read is GitHub's own REST-style
2180/// `message` envelope, which is what a forbidden status carries, and the `message` and
2181/// `type` of each GraphQL error, which is where a *successful* response says it.
2182///
2183/// A body that is not JSON at all has nothing structured to read, so only a failing
2184/// response's own text is taken — a successful response that is not JSON is malformed
2185/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
2186fn refusal_wording(status: StatusCode, body: &str) -> String {
2187    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
2188        return if status.is_success() {
2189            String::new()
2190        } else {
2191            body.to_owned()
2192        };
2193    };
2194    let mut said: Vec<&str> = parsed
2195        .get("message")
2196        .and_then(Value::as_str)
2197        .into_iter()
2198        .collect();
2199    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
2200        for error in errors {
2201            said.extend(
2202                ["message", "type"]
2203                    .into_iter()
2204                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
2205            );
2206        }
2207    }
2208    said.join("; ")
2209}
2210
2211impl Limiter {
2212    /// Which limiter refused this response, or `None` when none of them did.
2213    ///
2214    /// The wording is read first and the status only decides what carries none of it,
2215    /// because GitHub answers a secondary limit with a forbidden status far more often
2216    /// than with too-many-requests — while a forbidden status saying nothing about a limit
2217    /// really is a credential this token lacks.
2218    ///
2219    /// A response is a refusal because of its status or its own wording. A spent budget
2220    /// only ever explains one; it never turns an answer into a refusal.
2221    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
2222        let normalized = refusal_wording(status, body).to_ascii_lowercase();
2223        if SECONDARY_WORDINGS
2224            .iter()
2225            .any(|wording| normalized.contains(wording))
2226        {
2227            return Some(Self::Secondary);
2228        }
2229        if status == StatusCode::TOO_MANY_REQUESTS {
2230            return Some(Self::Primary);
2231        }
2232        // An exhausted budget *explains* a response that failed; it does not make one that
2233        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
2234        // request the budget allowed as well as on the ones it then refuses, so reading
2235        // the header alone threw away a good answer — and, once refusals were retried,
2236        // replayed a request that had already taken effect.
2237        if !status.is_success() && budget_exhausted {
2238            return Some(Self::Primary);
2239        }
2240        // A successful response saying it: GitHub reports a GraphQL rate limit in the
2241        // `errors` of an HTTP 200, where nothing about the status says so at all.
2242        if status.is_success()
2243            && PRIMARY_WORDINGS
2244                .iter()
2245                .any(|wording| normalized.contains(wording))
2246        {
2247            return Some(Self::Primary);
2248        }
2249        None
2250    }
2251
2252    /// What this limiter is called where an operator can look it up.
2253    const fn name(self) -> &'static str {
2254        match self {
2255            Self::Primary => "GitHub's primary API rate limit",
2256            Self::Secondary => "GitHub's secondary rate limit",
2257        }
2258    }
2259
2260    /// What the endpoint an operator would go and check says about this limiter.
2261    const fn where_to_look(self) -> &'static str {
2262        match self {
2263            Self::Primary => {
2264                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
2265                 comes back."
2266            }
2267            Self::Secondary => {
2268                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
2269                 primary budget and does not report this one, so budget showing there says \
2270                 nothing about this refusal, and every further attempt extends it."
2271            }
2272        }
2273    }
2274
2275    /// The next step this limiter actually calls for.
2276    const fn what_to_do(self) -> &'static str {
2277        match self {
2278            Self::Primary => {
2279                "wait for the reset `gh api rate_limit` reports, then run the command again."
2280            }
2281            Self::Secondary => {
2282                "leave this board alone for a few minutes, then run the command again — or \
2283                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
2284            }
2285        }
2286    }
2287}
2288
2289/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
2290#[derive(Debug, Clone, Copy)]
2291struct Limited {
2292    limiter: Limiter,
2293    hint: Option<u64>,
2294    /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
2295    /// `x-ratelimit-reset`.
2296    hinted_by: Hint,
2297}
2298
2299/// Where a refusal's hint came from, which is what its wait is named after.
2300#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2301enum Hint {
2302    /// `retry-after`, or no hint at all.
2303    RetryAfter,
2304    /// An exhausted primary budget's `x-ratelimit-reset`.
2305    PrimaryReset,
2306}
2307
2308impl Limited {
2309    /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
2310    /// and the cause it is named by: the hint, raised to the schedule's own next wait —
2311    /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
2312    /// extends a secondary limit — or the schedule alone when there is none.
2313    fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
2314        match (self.hint, self.hinted_by) {
2315            (Some(hint), Hint::RetryAfter) => (
2316                Duration::from_secs(hint).max(backoff),
2317                pacing::cause::RETRY_AFTER,
2318            ),
2319            (Some(hint), Hint::PrimaryReset) => (
2320                Duration::from_secs(hint).max(backoff),
2321                pacing::cause::PRIMARY_RESET,
2322            ),
2323            (None, _) => (backoff, pacing::cause::BACKOFF),
2324        }
2325    }
2326
2327    /// What the caller is told once this source has waited as long as it may.
2328    ///
2329    /// Both limiters report as [`SourceError::RateLimited`], because that is what
2330    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2331    /// about *which* limiter it was makes it a different kind of failure. What differs is
2332    /// the operator's next step, and that is what the message carries — a secondary
2333    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2334    /// budget looks fine, and then back to retry the very burst that was refused.
2335    fn exhausted(
2336        self,
2337        doing: &str,
2338        waits: u32,
2339        waited: Duration,
2340        needed: Duration,
2341        budget: Duration,
2342    ) -> SourceError {
2343        SourceError::RateLimited {
2344            retry_after_seconds: self.hint,
2345            message: Some(format!(
2346                "{} refused this source while {doing}; it waited {} out over {} and was refused \
2347                 again, and the next wait of {} would take it past the {} one call may spend \
2348                 waiting. {} next: {}",
2349                self.limiter.name(),
2350                plural(waits, "refusal"),
2351                seconds(waited),
2352                seconds(needed),
2353                seconds(budget),
2354                self.limiter.where_to_look(),
2355                self.limiter.what_to_do(),
2356            )),
2357        }
2358    }
2359}
2360
2361/// One HTTP attempt's result, with what its response said about the rate limit.
2362///
2363/// The two travel together so the record and the outcome are written from the same place:
2364/// what a response said about the budget is only readable while that response is in hand,
2365/// and what the attempt *meant* is only decidable once its body has been read.
2366struct Attempted {
2367    result: Result<Value, Attempt>,
2368    limits: accounting::RateLimit,
2369    /// GitHub's own reported cost for this call, for a document that asked for it.
2370    reported_cost: Option<u64>,
2371    response_outcome: accounting::ResponseOutcome,
2372}
2373
2374/// One attempt's outcome: an error to report, or a rate limit to wait out.
2375enum Attempt {
2376    Failed(SourceError),
2377    Limited(Limited),
2378}
2379
2380fn plural(count: u32, thing: &str) -> String {
2381    if count == 1 {
2382        format!("{count} {thing}")
2383    } else {
2384        format!("{count} {thing}s")
2385    }
2386}
2387
2388fn seconds(duration: Duration) -> String {
2389    format!("{:.1}s", duration.as_secs_f64())
2390}
2391
2392/// The member [`stamp_served`] writes onto every node of a response that says when it last
2393/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2394const SERVED_AT: &str = "onetaskgraphServedAt";
2395
2396/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2397/// node's time later knows the server's clock on the response that read it — which is what
2398/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2399fn stamp_served(data: &mut Value, served: &str) {
2400    match data {
2401        Value::Object(members) => {
2402            if members.contains_key("updatedAt") {
2403                members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2404            }
2405            for member in members.values_mut() {
2406                stamp_served(member, served);
2407            }
2408        }
2409        Value::Array(items) => {
2410            for item in items {
2411                stamp_served(item, served);
2412            }
2413        }
2414        _ => {}
2415    }
2416}
2417
2418/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2419///
2420/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2421/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2422/// header, and neither is what makes a response a refusal — so the whole cost of one this
2423/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2424/// instead. Refusing the response over the header would turn a readable refusal into an
2425/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2426fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2427    value
2428        .and_then(|value| value.to_str().ok())
2429        .and_then(|value| value.trim().parse::<u64>().ok())
2430}
2431
2432/// A transport failure with every cause beneath it, outermost first.
2433///
2434/// The client's own text for a request it could not complete is `error sending request for
2435/// url (…)`, and the socket error that says why — a refused or reset connection, an exhausted
2436/// port range, a connection closed before its answer — is only in the chain of sources below
2437/// it. Those differ in what to do next, so all of them are kept.
2438pub(crate) fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
2439    let mut text = error.to_string();
2440    let mut cause = error.source();
2441    while let Some(next) = cause {
2442        let said = next.to_string();
2443        if !text.ends_with(&said) {
2444            text = format!("{text}: {said}");
2445        }
2446        cause = next.source();
2447    }
2448    text
2449}
2450
2451/// Every mutation this source sends creates content — an issue, a board item, a field of
2452/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2453/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2454/// and what the keyword says are the same set. That is what makes the keyword a sound test
2455/// rather than a convenient one: counting an edit or a removal the limiter might not have
2456/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
2457pub(crate) fn is_mutation(query: &str) -> bool {
2458    query.trim_start().starts_with("mutation")
2459}
2460
2461/// What this source was doing, for a diagnostic that has to say so.
2462///
2463/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2464/// a document added without a description is caught by that list's own gate instead of
2465/// falling through to the vague arm below.
2466fn operation_description(query: &str) -> &'static str {
2467    graphql::DOCUMENTS
2468        .iter()
2469        .find(|(document, _)| *document == query)
2470        .map_or("talking to GitHub", |(_, doing)| *doing)
2471}
2472
2473/// GitHub's published ceiling on content-generating requests in any one minute.
2474///
2475/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2476/// GitHub's number rather than this source's. This source paces at it over its window: a
2477/// content-creating request goes out at once while the minute's creations leave room for it,
2478/// and waits until they do otherwise — see the `pacing` module.
2479pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2480/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
2481/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
2482pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2483/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
2484/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
2485/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
2486pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
2487/// The secondary points one GraphQL request carrying a mutation is charged.
2488pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
2489/// The secondary points one GraphQL request carrying no mutation is charged.
2490pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
2491/// The fastest *steady* spacing of content-creating mutations that cannot exceed
2492/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
2493///
2494/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
2495/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
2496/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
2497/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
2498/// value that holds a long sustained burst under the per-minute bound by spacing alone.
2499pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2500/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2501///
2502/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2503/// own advice for a secondary limit — wait, and wait longer each time — without spending
2504/// the first minute of a transient refusal doing nothing.
2505pub const RETRY_BACKOFF_MS: u64 = 1_000;
2506/// Total time one call may spend waiting out rate limits before it reports a failure.
2507///
2508/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2509/// short enough that a command an operator is watching returns. The bound is what makes
2510/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2511/// the limiter, not in a process nobody can tell from a wedged one.
2512pub const RETRY_BUDGET_MS: u64 = 120_000;
2513
2514fn default_token_env() -> String {
2515    "GH_PROJECTS_TOKEN".to_owned()
2516}
2517fn default_endpoint() -> String {
2518    "https://api.github.com/graphql".to_owned()
2519}
2520
2521/// The name of a `Status` single-select option on the board.
2522///
2523/// Validated on the way in rather than checked later, so a blank option name — which
2524/// nothing on a board can be — is a state this type cannot hold.
2525#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2526#[serde(try_from = "String")]
2527#[schemars(extend("minLength" = 1))]
2528pub struct ColumnName(String);
2529
2530impl ColumnName {
2531    /// The option name, as the board spells it.
2532    fn as_str(&self) -> &str {
2533        &self.0
2534    }
2535}
2536
2537impl TryFrom<String> for ColumnName {
2538    type Error = String;
2539
2540    fn try_from(name: String) -> Result<Self, Self::Error> {
2541        if name.trim().is_empty() {
2542            return Err("a status_mapping option name cannot be blank".to_owned());
2543        }
2544        Ok(Self(name))
2545    }
2546}
2547
2548/// The two closed states this product can mean.
2549///
2550/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2551/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2552/// product never decides on a person's behalf that two issues are the same work.
2553#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2554#[serde(rename_all = "kebab-case")]
2555pub enum ClosedState {
2556    /// `COMPLETED` — precisely done.
2557    Completed,
2558    /// `NOT_PLANNED` — precisely cancelled.
2559    NotPlanned,
2560}
2561
2562impl ClosedState {
2563    const fn reason(self) -> &'static str {
2564        match self {
2565            Self::Completed => "COMPLETED",
2566            Self::NotPlanned => "NOT_PLANNED",
2567        }
2568    }
2569}
2570
2571/// Configuration for one GitHub Projects v2 board.
2572///
2573/// It serializes to a document this same type reads back as itself, so a caller writing a
2574/// configuration — a consumer filing onto a board it configures — can build one here rather
2575/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2576#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2577#[serde(default, deny_unknown_fields)]
2578pub struct GitHubProjectsConfig {
2579    /// Login of the user or organization which owns the board.
2580    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2581    /// The project number shown in the board's GitHub URL.
2582    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2583    // 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.
2584    /// `owner/name` of the repository this source creates an issue in when the item's own
2585    /// `repositories` field does not decide it.
2586    ///
2587    /// An item naming exactly one repository is created there; a task or a document naming
2588    /// none or several is created in its parent project's repository; and a project, or a
2589    /// task or document with no parent, naming none or several is created here. A board
2590    /// has no repository of its own and `createIssue` requires one, so a write without
2591    /// this is refused naming the field. Reads never need it.
2592    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2593    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2594    /// Environment variable containing a fine-grained token with Projects and Issues
2595    /// read/write plus Pull requests read-only access for every repository represented on
2596    /// the board.
2597    #[serde(default = "default_token_env")]
2598    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2599    /// GraphQL endpoint. GitHub Enterprise installations may override it.
2600    #[serde(default = "default_endpoint")]
2601    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2602    /// Per-instance mapping from a status category to the option of the board's one
2603    /// `Status` field it lands on, for a task and for a project.
2604    ///
2605    /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2606    /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2607    /// where a kind left out leaves the category unmapped for that kind. A category this
2608    /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2609    /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2610    /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2611    /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2612    /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2613    /// either kind. No two categories may name one option for the same kind, ignoring case.
2614    /// `unknown` may name one existing option; every unknown word then lands on it and
2615    /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2616    /// each unknown word because it never creates board options.
2617    #[serde(default)]
2618    pub status_mapping: StatusMapping,
2619    /// Per-instance mapping from a task's priority to an option of this board's
2620    /// single-select field named `Priority`.
2621    ///
2622    /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2623    /// other priority is refused before it reaches this board. Present, each of `urgent`,
2624    /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2625    /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2626    /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2627    /// no two levels may name one option. Reads and writes never create the field or an
2628    /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2629    /// the board lacks is refused pointing there.
2630    #[serde(default)]
2631    pub priority_mapping: Option<PriorityMappingConfig>,
2632    /// Caller metadata values this source also writes to board text fields, so the board can
2633    /// filter on them.
2634    ///
2635    /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2636    /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2637    /// value. On every item write — create, update and copy, of a task, a project or a
2638    /// document — a string found there is written to the field, and nothing is sent when the
2639    /// field already holds it; nothing there, or `null`, clears the field when it holds a
2640    /// value; any other JSON type is refused before any mutation. The metadata comment in the
2641    /// issue body stays the one home of the value, and every read reports metadata from it.
2642    /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2643    /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2644    /// non-text field of that name, is refused pointing there.
2645    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2646    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2647    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2648    #[schemars(!skip_serializing_if)]
2649    pub metadata_fields: Vec<MetadataFieldConfig>,
2650    /// How fast this source writes, and how long it waits out a rate-limit refusal.
2651    ///
2652    /// Every field keeps its shipped default when it is absent, and the defaults are
2653    /// GitHub's own published limits rather than taste. See [`Pacing`].
2654    #[serde(default)]
2655    pub pacing: PacingConfig,
2656}
2657
2658/// Which option of the board's `Priority` field each priority lands on.
2659///
2660/// One member per level rather than a map, so a key that is not a level is refused where
2661/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2662/// value in the field, not an option of it.
2663#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2664#[serde(default, deny_unknown_fields)]
2665pub struct PriorityMappingConfig {
2666    /// The option `urgent` lands on; `Urgent` when absent.
2667    pub urgent: Option<PriorityOptionName>,
2668    /// The option `high` lands on; `High` when absent.
2669    pub high: Option<PriorityOptionName>,
2670    /// The option `medium` lands on; `Medium` when absent.
2671    pub medium: Option<PriorityOptionName>,
2672    /// The option `low` lands on; `Low` when absent.
2673    pub low: Option<PriorityOptionName>,
2674}
2675
2676/// The name of an option of the board's `Priority` single-select field.
2677///
2678/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2679/// blank name.
2680#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2681#[serde(try_from = "String")]
2682#[schemars(extend("minLength" = 1))]
2683pub struct PriorityOptionName(String);
2684
2685impl PriorityOptionName {
2686    /// The option name, as the board spells it.
2687    fn as_str(&self) -> &str {
2688        &self.0
2689    }
2690}
2691
2692impl TryFrom<String> for PriorityOptionName {
2693    type Error = String;
2694
2695    fn try_from(name: String) -> Result<Self, Self::Error> {
2696        if name.trim().is_empty() {
2697            return Err("a priority_mapping option name cannot be blank".to_owned());
2698        }
2699        Ok(Self(name))
2700    }
2701}
2702
2703/// The name of the board field a priority is held in.
2704pub const PRIORITY_FIELD: &str = "Priority";
2705
2706/// The four priorities a board option can hold, in the order a new `Priority` field lists
2707/// them. `none` is not among them: it is the field holding no value.
2708///
2709/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2710/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2711/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2712/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2713pub const PRIORITY_LEVELS: [Priority; 4] = [
2714    Priority::Urgent,
2715    Priority::High,
2716    Priority::Medium,
2717    Priority::Low,
2718];
2719
2720/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2721/// see that list for what this pins.
2722#[must_use]
2723pub const fn level_position(priority: Priority) -> Option<usize> {
2724    match priority {
2725        Priority::None => None,
2726        Priority::Urgent => Some(0),
2727        Priority::High => Some(1),
2728        Priority::Medium => Some(2),
2729        Priority::Low => Some(3),
2730    }
2731}
2732
2733/// This instance's complete priority-to-option mapping, read in both directions.
2734///
2735/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2736/// two levels name one option.
2737#[derive(Debug, Clone)]
2738struct PriorityMapping {
2739    options: [PriorityOptionName; 4],
2740}
2741
2742impl PriorityMapping {
2743    fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2744        let shipped = |name: &str| PriorityOptionName(name.to_owned());
2745        let mapping = Self {
2746            options: [
2747                config.urgent.unwrap_or_else(|| shipped("Urgent")),
2748                config.high.unwrap_or_else(|| shipped("High")),
2749                config.medium.unwrap_or_else(|| shipped("Medium")),
2750                config.low.unwrap_or_else(|| shipped("Low")),
2751            ],
2752        };
2753        for (index, option) in mapping.options.iter().enumerate() {
2754            if let Some(earlier) = mapping.options[..index]
2755                .iter()
2756                .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2757            {
2758                return Err(SourceError::Config {
2759                    message: format!(
2760                        "priority_mapping of source {instance} sends both {} and {} to the board \
2761                         option {:?}; one option cannot read back as two priorities",
2762                        PRIORITY_LEVELS[earlier],
2763                        PRIORITY_LEVELS[index],
2764                        option.as_str()
2765                    ),
2766                });
2767            }
2768        }
2769        Ok(mapping)
2770    }
2771
2772    /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2773    fn option(&self, priority: Priority) -> Option<&str> {
2774        level_position(priority).map(|index| self.options[index].as_str())
2775    }
2776
2777    /// The priority a board option name reports, or `None` when nothing maps to it.
2778    fn priority_of(&self, option: &str) -> Option<Priority> {
2779        self.options
2780            .iter()
2781            .position(|name| name.as_str().eq_ignore_ascii_case(option))
2782            .map(|index| PRIORITY_LEVELS[index])
2783    }
2784
2785    /// Every mapped option name, in the order a new `Priority` field lists them.
2786    fn names(&self) -> impl Iterator<Item = &str> {
2787        self.options.iter().map(PriorityOptionName::as_str)
2788    }
2789}
2790
2791/// One metadata value this source projects onto a board text field.
2792#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2793#[serde(deny_unknown_fields)]
2794pub struct MetadataFieldConfig {
2795    /// The name of the board text field the value is written to.
2796    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.
2797    /// The top-level metadata key the value is read from. A key under the reserved
2798    /// `onetaskgraph.` prefix is refused.
2799    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.
2800    /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2801    /// key's own value is the one projected. Left out of a serialized entry when empty.
2802    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2803    // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
2804    #[schemars(!skip_serializing_if)]
2805    pub path: Vec<String>,
2806}
2807
2808/// The board fields no metadata value may be projected onto, compared ignoring case: the
2809/// three this source writes itself, and those GitHub owns on every board.
2810// 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.
2811const UNPROJECTABLE_FIELDS: [&str; 13] = [
2812    STATUS_FIELD,
2813    PRIORITY_FIELD,
2814    ORIGIN_FIELD,
2815    "Title",
2816    "Assignees",
2817    "Labels",
2818    "Linked pull requests",
2819    "Milestone",
2820    "Repository",
2821    "Reviewers",
2822    "Parent issue",
2823    "Sub-issues progress",
2824    "Type",
2825];
2826
2827/// One validated [`MetadataFieldConfig`] entry.
2828#[derive(Debug, Clone)]
2829struct MetadataField {
2830    field: ProjectedField,
2831    key: ProjectedKey,
2832    path: Vec<String>,
2833}
2834
2835/// The name of a board text field a metadata value is projected onto: never blank, never one
2836/// this source or GitHub writes, and never one another entry of the same source names.
2837#[derive(Debug, Clone, PartialEq, Eq)]
2838struct ProjectedField(String);
2839
2840/// The top-level metadata key a projected value is read from: never blank, and never under
2841/// the namespace this product reserves.
2842#[derive(Debug, Clone, PartialEq, Eq)]
2843struct ProjectedKey(String);
2844
2845impl std::ops::Deref for ProjectedField {
2846    type Target = str;
2847    fn deref(&self) -> &str {
2848        &self.0
2849    }
2850}
2851
2852impl std::ops::Deref for ProjectedKey {
2853    type Target = str;
2854    fn deref(&self) -> &str {
2855        &self.0
2856    }
2857}
2858
2859/// What one item's metadata says one projected field should hold.
2860#[derive(Debug, Clone, PartialEq, Eq)]
2861enum Projected {
2862    /// A string to hold.
2863    Text(String),
2864    /// Nothing: the key or a step of the path is absent, or the value is `null`.
2865    Nothing,
2866}
2867
2868impl MetadataField {
2869    /// Validate every entry of one instance's `metadata_fields`, refusing the first that
2870    /// cannot stand, naming the source and the entry.
2871    fn resolve(
2872        configured: Vec<MetadataFieldConfig>,
2873        instance: &SourceName,
2874    ) -> Result<Vec<Self>, SourceError> {
2875        let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
2876        for (index, entry) in configured.into_iter().enumerate() {
2877            let refuse = |why: String| SourceError::Config {
2878                message: format!(
2879                    "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
2880                    entry.field, entry.key
2881                ),
2882            };
2883            if entry.field.trim().is_empty() {
2884                return Err(refuse("names a blank field".to_owned()));
2885            }
2886            if entry.key.trim().is_empty() {
2887                return Err(refuse("names a blank key".to_owned()));
2888            }
2889            if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
2890                return Err(refuse(format!(
2891                    "names a key under the reserved \"{}.\" prefix, which this product keeps \
2892                     for its own keys; next: project a caller key instead",
2893                    MetadataKey::RESERVED_NAMESPACE
2894                )));
2895            }
2896            if let Some(owned) = UNPROJECTABLE_FIELDS
2897                .iter()
2898                .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
2899            {
2900                return Err(refuse(format!(
2901                    "names the board field {owned:?}, which this source or GitHub already \
2902                     writes; next: name a text field of your own"
2903                )));
2904            }
2905            if let Some(earlier) = resolved
2906                .iter()
2907                .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
2908            {
2909                return Err(refuse(format!(
2910                    "names the same board field as metadata_fields[{earlier}]; one field \
2911                     cannot hold two values"
2912                )));
2913            }
2914            resolved.push(Self {
2915                field: ProjectedField(entry.field),
2916                key: ProjectedKey(entry.key),
2917                path: entry.path,
2918            });
2919        }
2920        Ok(resolved)
2921    }
2922
2923    /// The path as a refusal spells it: `[]` for the key's own value.
2924    fn spelled_path(&self) -> String {
2925        serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
2926    }
2927
2928    /// What `metadata` says this field should hold, or the refusal for a value no text field
2929    /// can hold.
2930    fn projected(
2931        &self,
2932        metadata: &BTreeMap<String, Value>,
2933        instance: &SourceName,
2934    ) -> Result<Projected, SourceError> {
2935        let mut value = metadata.get(&*self.key);
2936        for step in &self.path {
2937            value = value.and_then(|held| held.get(step.as_str()));
2938        }
2939        let found = match value {
2940            None | Some(Value::Null) => return Ok(Projected::Nothing),
2941            Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
2942            Some(Value::Bool(_)) => "a boolean",
2943            Some(Value::Number(_)) => "a number",
2944            Some(Value::Array(_)) => "an array",
2945            Some(Value::Object(_)) => "an object",
2946        };
2947        Err(SourceError::Refused {
2948            message: format!(
2949                "source {instance} projects metadata key {:?} at path {} onto its board text \
2950                 field {:?}, and this item holds {found} there; only a string, null or nothing \
2951                 can be written to a text field; next: store a string there, or remove the \
2952                 value",
2953                &*self.key,
2954                self.spelled_path(),
2955                &*self.field
2956            ),
2957        })
2958    }
2959}
2960
2961/// What one item's `Priority` field says, read through this instance's mapping.
2962#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
2963enum HeldPriority {
2964    /// A priority this source reports: an option the mapping names, or no value (`none`).
2965    Read(Priority),
2966    /// An option the mapping does not name, which is never read as a level or as `none`.
2967    Unmapped(String),
2968}
2969
2970/// How fast this source writes, and how long it waits out a rate-limit refusal.
2971///
2972/// Configurable because a GitHub Enterprise installation sets its own limits and an
2973/// operator who has already been refused may want to go slower still — not because the
2974/// defaults are guesses.
2975#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2976#[serde(default, deny_unknown_fields)]
2977pub struct PacingConfig {
2978    /// Shortest interval between two content-creating mutations, in milliseconds.
2979    ///
2980    /// Zero sends them as fast as they are asked for, which is what a fixture server on
2981    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
2982    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.
2983    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
2984    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
2985    /// zero while there is a budget to spend, because a schedule of zero-length waits
2986    /// consumes none of it and so never ends.
2987    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.
2988    /// Total time one call may spend waiting out rate limits, in milliseconds.
2989    ///
2990    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
2991    /// the bound is what makes this a wait rather than a hang.
2992    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.
2993}
2994
2995/// The largest any pacing setting may be, in milliseconds.
2996///
2997/// One hour. GitHub's own harshest published bound on content-generating requests works
2998/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
2999/// anything a real limit asks for, and past it the settings stop describing pacing at all:
3000/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
3001/// and an interval beyond it is a command that never sends its second mutation. It also
3002/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
3003/// what a `Duration` can hold on every platform.
3004pub const MAX_PACING_MS: u64 = 3_600_000;
3005
3006/// [`PacingConfig`] with every default resolved and every value checked, which is what the
3007/// source holds.
3008///
3009/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
3010/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
3011/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
3012/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
3013/// is asked for, windows included.
3014#[derive(Debug, Clone, Copy)]
3015struct Pacing {
3016    /// Whether requests are held for GitHub's secondary windows: always, unless
3017    /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
3018    /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
3019    windows: bool,
3020    min_mutation_interval: Duration,
3021    retry_backoff: Duration,
3022    retry_budget: Duration,
3023}
3024
3025impl Pacing {
3026    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
3027    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
3028        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
3029            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
3030                message: format!(
3031                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3032                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3033                     GitHub's own harshest published limit"
3034                ),
3035            }),
3036            Some(value) => Ok(Duration::from_millis(value)),
3037            None => Ok(Duration::from_millis(default)),
3038        };
3039        let retry_backoff = bounded(
3040            config.retry_backoff_ms,
3041            RETRY_BACKOFF_MS,
3042            "retry_backoff_ms",
3043        )?;
3044        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3045        if retry_backoff.is_zero() && !retry_budget.is_zero() {
3046            return Err(SourceError::Config {
3047                message: format!(
3048                    "pacing.retry_backoff_ms of source {instance} is 0 while \
3049                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3050                     none of that budget, so it would retry a refusal forever. Set a backoff of \
3051                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3052                     waiting at all",
3053                    retry_budget.as_millis()
3054                ),
3055            });
3056        }
3057        Ok(Self {
3058            windows: config.min_mutation_interval_ms != Some(0),
3059            min_mutation_interval: bounded(
3060                config.min_mutation_interval_ms,
3061                0,
3062                "min_mutation_interval_ms",
3063            )?,
3064            retry_backoff,
3065            retry_budget,
3066        })
3067    }
3068}
3069
3070/// Factory for [`GitHubProjectsSource`].
3071#[derive(Debug, Clone, Copy, Default)]
3072pub struct Plugin;
3073
3074impl SourcePlugin for Plugin {
3075    fn kind(&self) -> &'static str {
3076        KIND
3077    }
3078    /// The endpoint, the owner and the board's number: what names one board.
3079    fn remote_identity(&self, config: &Value) -> Option<String> {
3080        let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
3081        Some(format!(
3082            "{}\n{}\n{}",
3083            config.endpoint, config.owner, config.project_number
3084        ))
3085    }
3086    fn config_schema(&self) -> Schema {
3087        schema_for!(GitHubProjectsConfig)
3088    }
3089    fn build(
3090        &self,
3091        name: &SourceName,
3092        config: &Value,
3093        secrets: &dyn SecretResolver,
3094    ) -> Result<Box<dyn TaskSource>, SourceError> {
3095        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
3096    }
3097
3098    fn build_with_clock(
3099        &self,
3100        name: &SourceName,
3101        config: &Value,
3102        secrets: &dyn SecretResolver,
3103        clock: SharedClock,
3104    ) -> Result<Box<dyn TaskSource>, SourceError> {
3105        self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
3106    }
3107}
3108
3109impl Plugin {
3110    /// Build a source recording every request it sends into an accounting the caller holds.
3111    ///
3112    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
3113    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
3114    /// session total rather than two — see [`accounting`] and
3115    /// [`GitHubProjectsSource::recording_into`].
3116    ///
3117    /// # Errors
3118    ///
3119    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
3120    /// [`SourceError::Config`] for configuration this plugin cannot use and
3121    /// [`SourceError::Auth`] for a credential it cannot find.
3122    pub fn build_recording_into(
3123        &self,
3124        name: &SourceName,
3125        config: &Value,
3126        secrets: &dyn SecretResolver,
3127        ledger: Arc<Accounting>,
3128    ) -> Result<Box<dyn TaskSource>, SourceError> {
3129        self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
3130    }
3131
3132    fn build_recording_with_clock(
3133        &self,
3134        name: &SourceName,
3135        config: &Value,
3136        secrets: &dyn SecretResolver,
3137        ledger: Arc<Accounting>,
3138        clock: SharedClock,
3139    ) -> Result<Box<dyn TaskSource>, SourceError> {
3140        let config: GitHubProjectsConfig =
3141            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
3142                message: format!("source {name}: {e}"),
3143            })?;
3144        let prefix = format!("source {name}: ");
3145        let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
3146            .map_err(|error| match error {
3147                // The shared `StatusMapping::distinct` names the source itself.
3148                SourceError::Config { message } if message.starts_with(&prefix) => {
3149                    SourceError::Config { message }
3150                }
3151                SourceError::Config { message } => SourceError::Config {
3152                    message: format!("{prefix}{message}"),
3153                },
3154                SourceError::Auth { message } => SourceError::Auth {
3155                    message: format!("source {name}: {message}"),
3156                },
3157                other => other,
3158            })?;
3159        source.clock = clock;
3160        Ok(Box::new(source))
3161    }
3162}
3163
3164/// Where a status category lands on this board, once configuration is resolved.
3165#[derive(Debug, Clone, PartialEq, Eq)]
3166enum StatusTarget {
3167    /// Not usable against this instance for this kind, and why.
3168    Disabled(UnmappedStatus),
3169    /// The board's `Status` option of this name.
3170    Column(ColumnName),
3171    /// A closed issue, with both its board option and the reason that says which closed it means.
3172    // 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.
3173    Terminal(ColumnName, ClosedState),
3174}
3175
3176/// Every status category, in the order the vocabulary declares them.
3177///
3178/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
3179/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
3180/// added to the shared vocabulary fails to compile until it is named there, and this
3181/// crate's suite reconciles this list against that enum's own derived schema, which is
3182/// generated from the variants rather than written beside them. The schema is what
3183/// catches a list left one short — a list checking only the positions it already holds
3184/// would pass while every mapping indexed by the new position panicked.
3185pub const CATEGORIES: [StatusCategory; 8] = [
3186    StatusCategory::Draft,
3187    StatusCategory::Backlog,
3188    StatusCategory::Todo,
3189    StatusCategory::Queued,
3190    StatusCategory::InProgress,
3191    StatusCategory::Done,
3192    StatusCategory::Cancelled,
3193    StatusCategory::Unknown,
3194];
3195
3196/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
3197#[must_use]
3198pub const fn category_position(category: StatusCategory) -> usize {
3199    match category {
3200        StatusCategory::Draft => 0,
3201        StatusCategory::Backlog => 1,
3202        StatusCategory::Todo => 2,
3203        StatusCategory::Queued => 3,
3204        StatusCategory::InProgress => 4,
3205        StatusCategory::Done => 5,
3206        StatusCategory::Cancelled => 6,
3207        StatusCategory::Unknown => 7,
3208    }
3209}
3210
3211/// The spelling a status category is configured and reported under.
3212fn category_name(category: StatusCategory) -> &'static str {
3213    match category {
3214        StatusCategory::Draft => "draft",
3215        StatusCategory::Backlog => "backlog",
3216        StatusCategory::Todo => "todo",
3217        StatusCategory::Queued => "queued",
3218        StatusCategory::InProgress => "in-progress",
3219        StatusCategory::Done => "done",
3220        StatusCategory::Cancelled => "cancelled",
3221        StatusCategory::Unknown => "unknown",
3222    }
3223}
3224
3225/// A shipped default's option name.
3226///
3227/// The literals below are this file's own and non-blank, and they are validated by the
3228/// one constructor a configured name goes through rather than beside it.
3229fn shipped_column(name: &'static str) -> ColumnName {
3230    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
3231}
3232
3233/// The shipped default for one category this instance's `status_mapping` does not mention,
3234/// for either kind.
3235fn shipped_default(category: StatusCategory) -> StatusTarget {
3236    match category {
3237        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
3238        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
3239        StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
3240        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
3241        StatusCategory::Done => {
3242            StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
3243        }
3244        StatusCategory::Cancelled => {
3245            StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
3246        }
3247        StatusCategory::Draft | StatusCategory::Unknown => {
3248            StatusTarget::Disabled(UnmappedStatus::Unconfigured)
3249        }
3250    }
3251}
3252
3253/// The two kinds a status is written and read for, each with its own half of the mapping.
3254const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
3255
3256/// This instance's complete category-to-target mapping for each kind, read in both
3257/// directions.
3258///
3259/// One target per category per kind, held at that category's own [`category_position`], so
3260/// a category missing from the mapping, named twice in it, or filed out of order is a state
3261/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
3262/// targets are options of the board's one `Status` field.
3263#[derive(Debug, Clone)]
3264struct BoardStatuses {
3265    tasks: [StatusTarget; CATEGORIES.len()],
3266    projects: [StatusTarget; CATEGORIES.len()],
3267}
3268
3269impl BoardStatuses {
3270    /// Resolve `configured` against the shipped defaults, refusing two categories one kind
3271    /// would read back from one option.
3272    ///
3273    /// A category the mapping does not mention keeps its shipped default for both kinds; one
3274    /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
3275    /// omits unmapped rather than defaulted.
3276    fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
3277        let resolve_kind =
3278            |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
3279                // `CATEGORIES[position] == category` for every category — the crate's suite
3280                // asserts it — so mapping the list in order fills each category's own slot.
3281                let mut targets = CATEGORIES.map(shipped_default);
3282                for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
3283                    if !configured.mentions(category) {
3284                        continue;
3285                    }
3286                    *slot = match configured.name_for(category, kind) {
3287                        Err(why) => StatusTarget::Disabled(why),
3288                        Ok(name) => {
3289                            let option = ColumnName::try_from(name.as_str().to_owned())
3290                                .map_err(|message| SourceError::Config { message })?;
3291                            match category {
3292                                StatusCategory::Done => {
3293                                    StatusTarget::Terminal(option, ClosedState::Completed)
3294                                }
3295                                StatusCategory::Cancelled => {
3296                                    StatusTarget::Terminal(option, ClosedState::NotPlanned)
3297                                }
3298                                _ => StatusTarget::Column(option),
3299                            }
3300                        }
3301                    };
3302                }
3303                StatusMapping::distinct(
3304                    instance,
3305                    kind,
3306                    CATEGORIES
3307                        .iter()
3308                        .zip(&targets)
3309                        .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
3310                )?;
3311                Ok(targets)
3312            };
3313        Ok(Self {
3314            tasks: resolve_kind(ItemKind::Task)?,
3315            projects: resolve_kind(ItemKind::Project)?,
3316        })
3317    }
3318
3319    /// Every category's target for `kind`, in category order.
3320    const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
3321        match kind {
3322            ItemKind::Task => &self.tasks,
3323            ItemKind::Project => &self.projects,
3324        }
3325    }
3326
3327    fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
3328        &self.targets(kind)[category_position(category)]
3329    }
3330
3331    /// The category a board option name reports for `kind`, or `None` when nothing of that
3332    /// kind maps to it.
3333    fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
3334        CATEGORIES.into_iter().find(|category| {
3335            self.target(kind, *category)
3336                .option()
3337                .is_some_and(|name| name.eq_ignore_ascii_case(option))
3338        })
3339    }
3340
3341    /// Every option name either kind maps a category to, each once ignoring case, in
3342    /// category order with a task's name before a project's — what the guarded setup asks
3343    /// the `Status` field to hold.
3344    fn wanted(&self) -> Vec<String> {
3345        let mut wanted: Vec<String> = Vec::new();
3346        for category in CATEGORIES {
3347            for kind in STATUS_KINDS {
3348                if let Some(name) = self.target(kind, category).option()
3349                    && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
3350                {
3351                    wanted.push(name.to_owned());
3352                }
3353            }
3354        }
3355        wanted
3356    }
3357
3358    /// The status an item of `kind` reports, from the three things a read of it says: its
3359    /// board `Status` option, whether its issue is closed, and the reason it was closed with.
3360    ///
3361    /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
3362    /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
3363    /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
3364    /// produce, so it is read permissively rather than refused — reads are faithful, and
3365    /// refusals belong on writes.
3366    ///
3367    /// **Its name follows the closure too, unless the option agrees with it.** The option's
3368    /// own name is kept when this kind's mapping reads that option as the same category, or
3369    /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3370    /// named `Shipped`. An option the mapping reads as another category contradicts the
3371    /// closure, and so does no option at all: the name is then the option the mapping gives
3372    /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3373    /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3374    /// it. An `unknown` closure keeps the option, or `Closed` without one.
3375    ///
3376    /// An open item's option reads through its own kind's mapping, and an option that mapping
3377    /// does not name reads as `Unknown` under its own name.
3378    ///
3379    /// One function of those three rather than of a response, so a narrow status write can
3380    /// answer what a re-read would report by applying it to the state it has just written.
3381    fn status(
3382        &self,
3383        kind: ItemKind,
3384        option: Option<&str>,
3385        closed: bool,
3386        reason: Option<&str>,
3387    ) -> Status {
3388        if closed {
3389            let category = closed_category(reason);
3390            let agrees = option.filter(|option| {
3391                category == StatusCategory::Unknown
3392                    || self
3393                        .category_of(kind, option)
3394                        .is_none_or(|read| read == category)
3395            });
3396            let name = agrees.or_else(|| self.target(kind, category).option());
3397            let fallback = match category {
3398                StatusCategory::Done => "Done",
3399                StatusCategory::Cancelled => "Cancelled",
3400                _ => "Closed",
3401            };
3402            return Status {
3403                category,
3404                name: name.or(option).unwrap_or(fallback).to_owned(),
3405            };
3406        }
3407        let name = option.unwrap_or("Open").to_owned();
3408        Status {
3409            category: self
3410                .category_of(kind, &name)
3411                .unwrap_or(StatusCategory::Unknown),
3412            name,
3413        }
3414    }
3415
3416    /// The option a closed item's `Status` has to hold for its card to agree with its
3417    /// closure, and the category that closure reads as — or `None` when the item is open,
3418    /// its closure reads `unknown`, or its option already agrees.
3419    ///
3420    /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3421    /// in line, each to the one option the mapping gives its category — an item with no
3422    /// option, or at one the mapping does not name, included.
3423    fn contradicted(
3424        &self,
3425        kind: ItemKind,
3426        option: Option<&str>,
3427        closed: bool,
3428        reason: Option<&str>,
3429    ) -> Option<(ClosedAs, &str)> {
3430        if !closed {
3431            return None;
3432        }
3433        let closed_as = match closed_category(reason) {
3434            StatusCategory::Done => ClosedAs::Done,
3435            StatusCategory::Cancelled => ClosedAs::Cancelled,
3436            _ => return None,
3437        };
3438        let wanted = self.target(kind, closed_as.category()).option()?;
3439        if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3440            return None;
3441        }
3442        Some((closed_as, wanted))
3443    }
3444}
3445
3446/// The category a closed issue reads as, from the reason it was closed with.
3447///
3448/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3449/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3450/// is one this source cannot read a category from.
3451fn closed_category(reason: Option<&str>) -> StatusCategory {
3452    match reason {
3453        None | Some("COMPLETED") => StatusCategory::Done,
3454        Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3455        Some(_) => StatusCategory::Unknown,
3456    }
3457}
3458
3459impl BoardStatuses {
3460    /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3461    /// case; a kind lacking none is left out.
3462    fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3463        STATUS_KINDS
3464            .into_iter()
3465            .filter_map(|kind| {
3466                let missing: Vec<String> = self
3467                    .targets(kind)
3468                    .iter()
3469                    .filter_map(StatusTarget::option)
3470                    .filter(|wanted| {
3471                        !existing
3472                            .iter()
3473                            .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3474                    })
3475                    .map(str::to_owned)
3476                    .collect();
3477                (!missing.is_empty()).then_some(KindMissing { kind, missing })
3478            })
3479            .collect()
3480    }
3481}
3482
3483impl StatusTarget {
3484    /// The board option this target selects, or `None` for an unmapped one.
3485    fn option(&self) -> Option<&str> {
3486        match self {
3487            Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3488            Self::Disabled(_) => None,
3489        }
3490    }
3491}
3492
3493// 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.
3494/// One repository this source can create an issue in, as `owner/name`.
3495///
3496/// Every `createIssue` this source sends names one of these: the item's own single
3497/// `repositories` entry, else its parent project issue's repository, else the configured
3498/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3499/// that choice and says what it refuses before `createIssue`.
3500// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3501#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
3502struct RepositoryTarget {
3503    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3504    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3505}
3506
3507impl RepositoryTarget {
3508    fn parse(value: &str) -> Result<Self, SourceError> {
3509        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3510            message: format!(
3511                "repository must be spelled owner/name; {value:?} names no repository"
3512            ),
3513        })?;
3514        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3515            return Err(SourceError::Config {
3516                message: format!(
3517                    "repository must be spelled owner/name with a GitHub login and one \
3518                     repository name; {value:?} is not"
3519                ),
3520            });
3521        }
3522        Ok(Self {
3523            owner: owner.to_owned(),
3524            name: name.to_owned(),
3525        })
3526    }
3527
3528    /// The one host whose repositories this source creates issues in, spelled once: it is
3529    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3530    const HOST: &str = "github.com";
3531
3532    fn origin(&self) -> String {
3533        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3534    }
3535
3536    /// The repository a normalized origin names, or why it is none this source can create
3537    /// an issue in: another host, or more or fewer than `owner/name` under this one.
3538    fn from_origin(origin: &Repository) -> Result<Self, String> {
3539        let not_here = || {
3540            format!(
3541                "{} is not a {}/owner/name repository",
3542                origin.as_str(),
3543                Self::HOST
3544            )
3545        };
3546        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3547        if host != Self::HOST {
3548            return Err(not_here());
3549        }
3550        Self::parse(rest).map_err(|_| not_here())
3551    }
3552
3553    fn slug(&self) -> String {
3554        format!("{}/{}", self.owner, self.name)
3555    }
3556}
3557
3558/// A source which reads GitHub afresh for every operation.
3559pub struct GitHubProjectsSource {
3560    /// This source's configured name, used both to tell a far end naming this source
3561    /// from one naming a system it knows nothing about, and to name the instance a
3562    /// status refusal is about.
3563    name: SourceName,
3564    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3565    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3566    repository: Option<RepositoryTarget>,
3567    endpoint: Url,
3568    token: SecretString,
3569    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3570    statuses: BoardStatuses,
3571    /// Where each priority lands on this board, or `None` when this instance holds none.
3572    priorities: Option<PriorityMapping>,
3573    /// The metadata values this instance projects onto board text fields, in configured order.
3574    metadata_fields: Vec<MetadataField>,
3575    client: Client,
3576    asset_client: Client,
3577    /// Every item this source has created in this command, in the order it created them —
3578    /// dropped by [`TaskSource::end_command`].
3579    ///
3580    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3581    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3582    /// a copy resolving a dependency on an item it had just created refused it as not
3583    /// found. A board read is completed from this — an item remembered here and absent from
3584    /// the read is added back, because the board really does hold it and only the read is
3585    /// behind.
3586    ///
3587    /// It is not a cache of a user's work: nothing is remembered that this process did not
3588    /// itself just write, it lives and dies with the process, and it is never consulted for
3589    /// an item this source did not create.
3590    created: Mutex<Vec<Resolved>>,
3591    /// Every item that already existed and that this source has written in this command, as
3592    /// it wrote it — dropped by [`TaskSource::end_command`].
3593    ///
3594    /// The other half of [`Self::created`], held on the same terms and for the reason a
3595    /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3596    /// filter is an index behind a write this process made moments ago, so a query matching
3597    /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3598    /// is remembered that this process did not itself just write.
3599    updated: Mutex<Vec<Resolved>>,
3600    /// Every issue this source has added a comment to or edited a comment of in this command
3601    /// — dropped by [`TaskSource::end_command`].
3602    ///
3603    /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3604    /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3605    /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3606    /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3607    /// from before — until the index caught up. Each id here is a candidate of every such
3608    /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3609    /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3610    /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3611    /// process wrote is still found only once the index has it.
3612    commented: Mutex<Vec<NativeId>>,
3613    /// How fast this source writes, and how long it waits out a refusal.
3614    pacing: Pacing,
3615    /// When the last content-creating mutation finished, or the moment the furthest-out
3616    /// reserved slot releases the next one, whichever is later — so the one after it can be
3617    /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
3618    /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
3619    /// measured from.
3620    last_mutation: Mutex<Option<Duration>>,
3621    /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
3622    windows: Mutex<pacing::Windows>,
3623    /// The tasks a project holds, as a copy's batched read of the project and every one of them
3624    /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
3625    /// a listing of that project's tasks is answered without a read of its own.
3626    task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
3627    /// The web address of each issue this source created in the current command, as its
3628    /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
3629    /// request.
3630    written: Mutex<BTreeMap<NativeId, String>>,
3631    /// What a copy has held back of the items it created, between
3632    /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
3633    /// nothing is being held back, when every write lands whole as it is made.
3634    deferred: Mutex<Option<Deferred>>,
3635    clock: SharedClock,
3636    numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3637    /// The board as this process last read it, for the length of one command — dropped by
3638    /// [`TaskSource::end_command`].
3639    ///
3640    /// A copy of a project used to re-read the whole board, paged, before writing each of
3641    /// its items, which is by far the largest part of a copy's request count and none of
3642    /// its work. Nothing else changes this board while a command runs — this source's own
3643    /// writes are the only writer — so one read answers them all.
3644    ///
3645    /// It is not a store of a user's work and it is not the cache the no-persistence
3646    /// invariant forbids: it lives and dies with the process exactly as `created` does,
3647    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3648    /// an item this command created and then depends on resolves whether or not GitHub's
3649    /// own eventually-consistent read has caught up. A write to an item already on the
3650    /// board updates the entry here too, so what this holds is the last read plus this
3651    /// process's own writes rather than a snapshot taken before them.
3652    board_cache: Mutex<Option<Board>>,
3653    /// Every issue this board's own search reported, for the length of one command — dropped
3654    /// by [`TaskSource::end_command`].
3655    ///
3656    /// The second half of a board read, and cached for the same reason and on the same
3657    /// terms as the first: it lives and dies with the process, nothing is written down, and
3658    /// a write this process makes updates the entry here exactly as it updates the one in
3659    /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3660    /// that lists this board's projects and its tasks pays for one search rather than two.
3661    search_cache: Mutex<Option<Vec<Resolved>>>,
3662    /// What each narrowed question GitHub was asked answered, keyed by that question, for
3663    /// the length of one command — dropped by [`TaskSource::end_command`].
3664    ///
3665    /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3666    /// and dies with the process, nothing is written down, a write this process makes
3667    /// updates the entry here as it updates the other two, and every answer is completed
3668    /// with this process's own writes each time it is given. A command that asks the same
3669    /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3670    /// write — pays for it once, which is what the whole-board read it replaced gave it.
3671    narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3672    search_next: Mutex<BTreeMap<String, Option<String>>>,
3673    /// Records already resolved in this command, reused by writes and for comment identity.
3674    /// Explicit item reads still reach GitHub. Nothing is persisted, and
3675    /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3676    /// its item as a person has since left it.
3677    resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3678    /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
3679    /// answered as holding nothing — each taken by the next read of that item by id and by
3680    /// nothing else, dropped by any mutation naming it, and cleared by
3681    /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
3682    /// read-before-write, made in the same request as the others' rather than on its own.
3683    read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
3684    /// Every item a listing of one project's members carried whole in this command — the
3685    /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
3686    /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
3687    /// it again. No other read is answered from it, so a read by id outside a copy's
3688    /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
3689    /// item, and cleared by [`TaskSource::end_command`].
3690    carried: Mutex<BTreeMap<NativeId, Resolved>>,
3691    /// Items an issue read by reference reached, held for the one status write each was read
3692    /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
3693    /// what such a write needs. Dropped by [`TaskSource::end_command`].
3694    held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3695    /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3696    /// asked for under, with the project that selector named — for the length of one command,
3697    /// dropped by [`TaskSource::end_command`].
3698    ///
3699    /// A caller pages through a project's tasks one engine page at a time, and every page
3700    /// is cut from the whole list of them, so without this each page walked every
3701    /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3702    /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3703    /// dies with the process, nothing is written down, a write this process makes updates or
3704    /// removes the entry here as it does there, and every answer is completed with this
3705    /// process's own writes each time it is given.
3706    children_cache: Mutex<ProjectChildren>,
3707    /// The board's own id and field definitions as this process last read them on their
3708    /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3709    ///
3710    /// What a write needs of the board and its item does not say, read once per command
3711    /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3712    /// lives and dies with the process and nothing is written down. It holds no item and so
3713    /// can answer no question about one — see [`Self::board_fields`].
3714    fields_cache: Mutex<Option<BoardFields>>,
3715    /// Each destination repository's node id, resolved once per repository
3716    /// rather than per issue created.
3717    ///
3718    /// A repository's node id does not change, and re-reading it for every issue of a copy
3719    /// spent one request per item on an answer this source already had. It is a map rather
3720    /// than one entry because a copy files each item in the repository its own
3721    /// `repositories` field names, so a plan across five repositories asks GitHub five
3722    /// times and not once per item.
3723    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3724    /// What every request this source sends is recorded into.
3725    ///
3726    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3727    /// a request leaves this crate, so nothing has to be switched on for a session to be
3728    /// counted. It is shared rather than owned so a caller accounting for a whole session —
3729    /// its own schema verification, board lookups, residue sweep and cleanup beside this
3730    /// source's reads and writes — adds up one accounting instead of two. See
3731    /// [`accounting`] for what a record carries and what a session's spend is and is not.
3732    ledger: Arc<Accounting>,
3733    /// What [`expected`] holds for a write the store's cache expects: the journal while one is
3734    /// open, the issues primed from the cache, and the comments known on each.
3735    expected: expected::Held,
3736}
3737
3738/// GitHub's closed single-select color vocabulary.
3739#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3740#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
3741pub enum StatusOptionColor {
3742    /// Gray.
3743    Gray,
3744    /// Blue.
3745    Blue,
3746    /// Green.
3747    Green,
3748    /// Yellow.
3749    Yellow,
3750    /// Purple.
3751    Purple,
3752    /// Red.
3753    Red,
3754    /// Orange.
3755    Orange,
3756    /// Pink.
3757    Pink,
3758}
3759
3760/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
3761/// applies its additions.
3762#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3763pub enum SetupMode {
3764    /// Read without mutation.
3765    Plan,
3766    /// Apply and verify.
3767    Apply,
3768}
3769
3770/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
3771/// against it goes on compiling.
3772pub type StatusOptionsMode = SetupMode;
3773
3774/// The explicit result of the requested operation.
3775#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3776#[serde(rename_all = "kebab-case")]
3777pub enum StatusOptionsOutcome {
3778    /// A read-only plan.
3779    Planned,
3780    /// Apply found nothing missing.
3781    Unchanged,
3782    /// Additions were applied and verified.
3783    Applied,
3784}
3785
3786/// A GitHub single-select option's opaque GraphQL node identifier.
3787#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3788#[serde(transparent)]
3789pub struct StatusOptionId(#[schemars(length(min = 1))] String);
3790
3791impl TryFrom<String> for StatusOptionId {
3792    type Error = String;
3793
3794    fn try_from(id: String) -> Result<Self, Self::Error> {
3795        if id.trim().is_empty() {
3796            return Err("a GitHub Status option id cannot be blank".to_owned());
3797        }
3798        Ok(Self(id))
3799    }
3800}
3801
3802/// One existing or proposed option in a guarded Status-field update.
3803#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3804pub struct StatusOption {
3805    /// GitHub's stable id.
3806    pub id: StatusOptionId,
3807    /// The visible option name.
3808    pub name: ColumnName,
3809    /// GitHub's single-select color token.
3810    pub color: StatusOptionColor,
3811    /// The option description, including an empty one.
3812    pub description: String,
3813}
3814
3815/// One board item's Status assignment, retained as recovery data.
3816#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3817pub struct StatusAssignment {
3818    /// The project item id whose assignment this is.
3819    // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
3820    // carried verbatim as operator recovery data; introducing a semantic type would claim
3821    // validation rules GitHub does not publish and no operation here interprets.
3822    pub item_id: String,
3823    /// The selected option, absent when the item has no status.
3824    #[serde(skip_serializing_if = "Option::is_none")]
3825    pub option: Option<AssignedStatusOption>,
3826}
3827
3828/// The inseparable id and name of an assigned option.
3829#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3830pub struct AssignedStatusOption {
3831    /// GitHub's stable id.
3832    pub id: StatusOptionId,
3833    /// The visible name.
3834    pub name: ColumnName,
3835}
3836
3837/// The plan and verified outcome of reconciling configured Status options.
3838#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3839pub struct StatusOptionsReport {
3840    /// The configured source name.
3841    pub source: SourceName,
3842    /// Configured option names absent before the operation.
3843    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
3844    // `ColumnName` and has therefore already passed its nonblank validation; retaining the
3845    // serialized string here preserves the report's intentionally simple public contract.
3846    pub missing: Vec<String>,
3847    /// What the requested operation did.
3848    pub outcome: StatusOptionsOutcome,
3849    /// The complete option list observed before any mutation.
3850    pub existing: Vec<StatusOption>,
3851}
3852
3853#[derive(Debug, Clone, PartialEq, Eq)]
3854struct StatusSnapshot {
3855    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3856    // passed back as the mutation's project identity; a newtype could enforce no stronger
3857    // invariant because GitHub publishes no grammar for it.
3858    board_id: String,
3859    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3860    // passed back as the mutation's field identity; a newtype could enforce no stronger
3861    // invariant because GitHub publishes no grammar for it.
3862    field_id: String,
3863    options: Vec<StatusOption>,
3864    assignments: Vec<StatusAssignment>,
3865}
3866
3867/// The name of the board field a status is held in.
3868const STATUS_FIELD: &str = "Status";
3869
3870/// Every item's value of each field `report` names, as it stood before the setup wrote
3871/// anything — what a person puts back when the setup is refused part way.
3872fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
3873    let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
3874        .fields
3875        .iter()
3876        .map(|field| (field.field.name(), before.assignments(field.field)))
3877        .collect();
3878    serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
3879        message: format!("cannot render the pre-write field recovery snapshot: {error}"),
3880    })
3881}
3882
3883/// One board field the guarded setup reads and writes — every one it reads, and the only
3884/// ones it writes.
3885#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
3886pub enum BoardField {
3887    /// The single-select `Status` field every instance's `status_mapping` resolves into.
3888    Status,
3889    /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
3890    Priority,
3891}
3892
3893impl BoardField {
3894    /// The field's name on the board.
3895    #[must_use]
3896    pub const fn name(self) -> &'static str {
3897        match self {
3898            Self::Status => STATUS_FIELD,
3899            Self::Priority => PRIORITY_FIELD,
3900        }
3901    }
3902
3903    /// The field a board calls `name`, or `None` for one this setup does not own.
3904    fn named(name: &str) -> Option<Self> {
3905        [Self::Status, Self::Priority]
3906            .into_iter()
3907            .find(|field| field.name() == name)
3908    }
3909}
3910
3911/// What the guarded setup did to one field.
3912#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3913#[serde(rename_all = "kebab-case")]
3914pub enum FieldOutcome {
3915    /// A read-only plan.
3916    Planned,
3917    /// Apply found the field there with every configured option.
3918    Unchanged,
3919    /// Missing options were added to the field that was there, and verified.
3920    Applied,
3921    /// The field was not there; it was created holding the configured options, and verified.
3922    Created,
3923}
3924
3925/// One field's plan, or its verified outcome.
3926#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3927pub struct FieldReport {
3928    /// Which field.
3929    pub field: BoardField,
3930    /// Whether the board had the field before the operation.
3931    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
3932    // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
3933    // "outcome", "existing"}` — so folding one into the other would change a published JSON
3934    // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
3935    // one constructor, and it derives `outcome` from `exists` in one match.
3936    pub exists: bool,
3937    /// Configured option names the field lacked before the operation — every one of them,
3938    /// in the order a new field lists them, when the field was not there at all.
3939    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3940    // mapping name and has therefore already passed its nonblank validation; the serialized
3941    // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
3942    pub missing: Vec<String>,
3943    /// For the `Status` field, which item kind each missing name is configured for: one
3944    /// entry per kind `status_mapping` names a missing option for, task before project, each
3945    /// listing that kind's missing names in category order. A name both kinds use is in
3946    /// both. Empty — and left out of the JSON — when nothing is missing, and always for
3947    /// `Priority`, which only a task holds.
3948    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3949    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3950    // both SDKs model an absent `kinds` as an empty list rather than as `null`.
3951    #[schemars(!skip_serializing_if)]
3952    pub kinds: Vec<KindMissing>,
3953    /// What the requested operation did.
3954    pub outcome: FieldOutcome,
3955    /// The field's complete option list observed before any mutation; empty when the field
3956    /// was not there.
3957    pub existing: Vec<StatusOption>,
3958}
3959
3960/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
3961#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3962pub struct KindMissing {
3963    /// The kind these names are configured for.
3964    pub kind: ItemKind,
3965    /// The names that kind maps a category to and the field lacked, in category order.
3966    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3967    // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
3968    // intentionally simple public contract.
3969    pub missing: Vec<String>,
3970}
3971
3972/// The plan and verified outcome of setting up every field a source's configuration names.
3973#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3974pub struct FieldsReport {
3975    /// The configured source name.
3976    pub source: SourceName,
3977    /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
3978    // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
3979    // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
3980    // per field would change a published JSON shape. The states the list could hold and the
3981    // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
3982    // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
3983    pub fields: Vec<FieldReport>,
3984    /// Each board text field the source's `metadata_fields` projects a value onto, in
3985    /// configured order. Empty — and left out of the JSON — when it projects none.
3986    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3987    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3988    // both SDKs model an absent list as an empty one rather than as `null`.
3989    #[schemars(!skip_serializing_if)]
3990    pub metadata_fields: Vec<MetadataFieldReport>,
3991}
3992
3993/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
3994/// its GraphQL type when it has none — which is what a projected field's conflict reports.
3995///
3996/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
3997/// no conflict.
3998// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
3999// refuses exactly what `try_from` below does.
4000#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4001#[serde(transparent)]
4002pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
4003
4004/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
4005/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
4006/// neither SDK's validator can be relied on to support.
4007const NON_TEXT_FIELD_TYPE_PATTERN: &str =
4008    r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
4009
4010impl TryFrom<String> for NonTextFieldType {
4011    type Error = String;
4012
4013    fn try_from(kind: String) -> Result<Self, Self::Error> {
4014        if kind.is_empty() || kind.starts_with(char::is_whitespace) {
4015            return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
4016        }
4017        if kind == "TEXT" {
4018            return Err("a TEXT field is no conflict for a projected text field".to_owned());
4019        }
4020        Ok(Self(kind))
4021    }
4022}
4023
4024impl std::fmt::Display for NonTextFieldType {
4025    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4026        formatter.write_str(&self.0)
4027    }
4028}
4029
4030/// One projected metadata text field's plan, or its verified outcome.
4031///
4032/// The setup creates a missing one as a text field and never alters a field that is there:
4033/// a same-named field of another type is reported in `conflict` and left as it is.
4034#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4035pub struct MetadataFieldReport {
4036    /// The board field's name.
4037    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.
4038    /// The metadata key the value is read from.
4039    pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
4040    /// The object keys walked inside that key's value; empty for the key's own value.
4041    pub path: Vec<String>,
4042    /// Whether the board had a text field of that name before the operation.
4043    // 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.
4044    pub exists: bool,
4045    /// The type of a field of that name that is not a text field, which the setup leaves as
4046    /// it is; absent — and left out of the JSON — when there is none.
4047    #[serde(default, skip_serializing_if = "Option::is_none")]
4048    pub conflict: Option<NonTextFieldType>,
4049    /// What the requested operation did: `planned`; `created` for a field that was not
4050    /// there; `unchanged` for one that was, and for a conflict, which is never changed.
4051    pub outcome: FieldOutcome,
4052}
4053
4054/// Which options one field is configured with, in the order a new field would list them.
4055struct FieldPlan {
4056    field: BoardField,
4057    wanted: Vec<String>,
4058}
4059
4060/// One single-select field as the guarded setup snapshots it.
4061#[derive(Debug, Clone, PartialEq, Eq)]
4062struct SnapshotField {
4063    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4064    // passed back as the mutation's field identity; a newtype could enforce no stronger
4065    // invariant because GitHub publishes no grammar for it.
4066    field_id: String,
4067    options: Vec<StatusOption>,
4068}
4069
4070/// Every single-select field of a board and every item's value of each.
4071#[derive(Debug, Clone, PartialEq, Eq)]
4072struct BoardSnapshot {
4073    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4074    // passed back as the mutation's project identity; a newtype could enforce no stronger
4075    // invariant because GitHub publishes no grammar for it.
4076    board_id: String,
4077    fields: BTreeMap<BoardField, SnapshotField>,
4078    /// Each board item's id, and its value of each field this setup owns that it holds one of.
4079    items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
4080}
4081
4082impl BoardSnapshot {
4083    /// Every item's value of `field`, in board order — the recovery data a drift refusal
4084    /// carries.
4085    fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
4086        self.items
4087            .iter()
4088            .map(|(item_id, values)| StatusAssignment {
4089                item_id: item_id.clone(),
4090                option: values.get(&field).cloned(),
4091            })
4092            .collect()
4093    }
4094}
4095
4096impl GitHubProjectsSource {
4097    /// Report missing configured Status options and, when `apply` is true, add them with
4098    /// a whole-list mutation that preserves every existing id and verifies the result.
4099    ///
4100    /// # Errors
4101    ///
4102    /// Refuses a board without a single-select `Status` field. A post-write difference in
4103    /// any pre-existing option id or item assignment is refused with the complete pre-write
4104    /// assignment snapshot in the diagnostic for recovery.
4105    // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
4106    // successful mutation, both drift refusals, source selection, missing Status, casing,
4107    // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
4108    // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
4109    // responses from entering the defensive malformed-response branches below.
4110    pub async fn status_options(
4111        &self,
4112        mode: StatusOptionsMode,
4113    ) -> Result<StatusOptionsReport, SourceError> {
4114        let before = self.status_snapshot().await?;
4115        // A terminal category's option is as configured as an open one's: a terminal
4116        // write validates it before closing and refuses when the board lacks it. Both
4117        // kinds' names are options of the one field, so both are asked for.
4118        let missing = self
4119            .statuses
4120            .wanted()
4121            .into_iter()
4122            .filter(|wanted| {
4123                !before
4124                    .options
4125                    .iter()
4126                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4127            })
4128            .collect::<Vec<_>>();
4129        let report = StatusOptionsReport {
4130            source: self.name.clone(),
4131            missing: missing.clone(),
4132            outcome: match (mode, missing.is_empty()) {
4133                (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
4134                (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
4135                (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
4136            },
4137            existing: before.options.clone(),
4138        };
4139        if mode == StatusOptionsMode::Plan || missing.is_empty() {
4140            return Ok(report);
4141        }
4142        let mut options = before
4143            .options
4144            .iter()
4145            .map(|option| {
4146                json!({
4147                    "id": option.id, "name": option.name, "color": option.color,
4148                    "description": option.description,
4149                })
4150            })
4151            .collect::<Vec<_>>();
4152        options.extend(missing.iter().map(|name| {
4153            json!({
4154                "name": name, "color": "GRAY", "description": ""
4155            })
4156        }));
4157        self.graphql(
4158            graphql::STATUS_OPTIONS_UPDATE,
4159            json!({"input": {
4160                "projectId": before.board_id, "fieldId": before.field_id,
4161                "singleSelectOptions": options,
4162            }}),
4163        )
4164        .await?;
4165        let after = self.status_snapshot().await?;
4166        let options_preserved = before
4167            .options
4168            .iter()
4169            .all(|old| after.options.iter().any(|new| new == old));
4170        let additions_present = missing.iter().all(|wanted| {
4171            after
4172                .options
4173                .iter()
4174                .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4175        });
4176        if !options_preserved || !additions_present || after.assignments != before.assignments {
4177            let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
4178                SourceError::Malformed {
4179                    message: format!("cannot render pre-write Status recovery snapshot: {error}"),
4180                }
4181            })?;
4182            return Err(SourceError::Refused {
4183                message: format!(
4184                    "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}"
4185                ),
4186            });
4187        }
4188        Ok(report)
4189    }
4190
4191    /// A fresh snapshot of the Status field and every board item's assignment of it.
4192    ///
4193    /// # Errors
4194    ///
4195    /// Refuses a board without a single-select `Status` field, and one the token cannot see.
4196    async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
4197        // Status alone, as this operation has always read it: a `Priority` field is another
4198        // operation's, so nothing about it can refuse this one.
4199        let mut board = self.board_snapshot(&[BoardField::Status]).await?;
4200        let field = board
4201            .fields
4202            .remove(&BoardField::Status)
4203            .ok_or_else(|| self.no_status_field())?;
4204        Ok(StatusSnapshot {
4205            assignments: board.assignments(BoardField::Status),
4206            board_id: board.board_id,
4207            field_id: field.field_id,
4208            options: field.options,
4209        })
4210    }
4211
4212    /// The refusal a board with no `Status` field is answered with by the guarded setup.
4213    fn no_status_field(&self) -> SourceError {
4214        SourceError::Refused {
4215            message: format!("source {} board has no Status field", self.name),
4216        }
4217    }
4218
4219    // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
4220    // the real CLI loopback journey, including pagination. The individual malformed guards
4221    // are defensive validation of a schema-pinned third-party response, not separate user
4222    // journeys; drift and missing-field failures cover the operation's recovery behavior.
4223    /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
4224    /// every board item's value of each, walked to the end of the board's items. A field not
4225    /// in `owned` is read past whatever it holds.
4226    async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
4227        let mut after: Option<String> = None;
4228        let mut snapshot: Option<BoardSnapshot> = None;
4229        loop {
4230            let data = self
4231                .graphql(
4232                    graphql::STATUS_OPTIONS_SNAPSHOT,
4233                    json!({
4234                        "owner": self.owner, "number": self.project_number,
4235                        "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
4236                    }),
4237                )
4238                .await?;
4239            let board = data
4240                .pointer("/owner/projectV2")
4241                .filter(|board| board.is_object())
4242                .ok_or_else(|| SourceError::Refused {
4243                    message: format!(
4244                        "source {} has no accessible GitHub Projects board",
4245                        self.name
4246                    ),
4247                })?;
4248            if board
4249                .pointer("/fields/pageInfo/hasNextPage")
4250                .and_then(Value::as_bool)
4251                != Some(false)
4252            {
4253                return Err(SourceError::Malformed {
4254                    message:
4255                        "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
4256                            .into(),
4257                });
4258            }
4259            let mut fields = BTreeMap::new();
4260            // Only the fields this setup owns, by name: a node the single-select fragment did not
4261            // match carries no name, and a person's own single-select field — a `Size`, a
4262            // `Team` — is none of this setup's business, so nothing about it can refuse one. A
4263            // `Status` or `Priority` field without its options is malformed, not absent.
4264            // 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.
4265            for (owned, field) in board
4266                .pointer("/fields/nodes")
4267                .and_then(Value::as_array)
4268                .ok_or_else(|| SourceError::Malformed {
4269                    message: "GitHub project fields.nodes is not an array".into(),
4270                })?
4271                .iter()
4272                .filter_map(|field| {
4273                    let named = BoardField::named(field.get("name")?.as_str()?)?;
4274                    owned.contains(&named).then_some((named, field))
4275                })
4276            {
4277                let options = field
4278                    .get("options")
4279                    .and_then(Value::as_array)
4280                    .ok_or_else(|| SourceError::Malformed {
4281                        message: "GitHub single-select field options is not an array".into(),
4282                    })?
4283                    .iter()
4284                    .map(|option| {
4285                        Ok(StatusOption {
4286                            id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
4287                                .map_err(|message| SourceError::Malformed { message })?,
4288                            name: ColumnName::try_from(required_str(option, "name")?.to_owned())
4289                                .map_err(|message| SourceError::Malformed {
4290                                    message: format!(
4291                                        "GitHub single-select option name is invalid: {message}"
4292                                    ),
4293                                })?,
4294                            color: serde_json::from_value(
4295                                option.get("color").cloned().unwrap_or(Value::Null),
4296                            )
4297                            .map_err(|error| {
4298                                SourceError::Malformed {
4299                                    message: format!(
4300                                        "GitHub single-select option color is invalid: {error}"
4301                                    ),
4302                                }
4303                            })?,
4304                            description: optional_str(option, "description")?
4305                                .unwrap_or_default()
4306                                .to_owned(),
4307                        })
4308                    })
4309                    .collect::<Result<Vec<_>, SourceError>>()?;
4310                let snapshot = SnapshotField {
4311                    field_id: required_nonblank_str(field, "id")?.to_owned(),
4312                    options,
4313                };
4314                // A board's field names are unique, so a second one is an answer that cannot
4315                // say which field the setup would act on — refused rather than one chosen.
4316                if fields.insert(owned, snapshot).is_some() {
4317                    return Err(SourceError::Malformed {
4318                        message: format!(
4319                            "GitHub answered two {} fields for this board",
4320                            owned.name()
4321                        ),
4322                    });
4323                }
4324            }
4325            let board_id = required_nonblank_str(board, "id")?.to_owned();
4326            let current = snapshot.get_or_insert_with(|| BoardSnapshot {
4327                board_id,
4328                fields,
4329                items: Vec::new(),
4330            });
4331            let items = board
4332                .pointer("/items/nodes")
4333                .and_then(Value::as_array)
4334                .ok_or_else(|| SourceError::Malformed {
4335                    message: "GitHub project items.nodes is not an array".into(),
4336                })?;
4337            for item in items {
4338                let field_values =
4339                    item.get("fieldValues")
4340                        .ok_or_else(|| SourceError::Malformed {
4341                            message: "GitHub project item is missing fieldValues".into(),
4342                        })?;
4343                if field_values
4344                    .pointer("/pageInfo/hasNextPage")
4345                    .and_then(Value::as_bool)
4346                    != Some(false)
4347                {
4348                    return Err(SourceError::Malformed {
4349                        message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
4350                    });
4351                }
4352                let values = item
4353                    .pointer("/fieldValues/nodes")
4354                    .and_then(Value::as_array)
4355                    .ok_or_else(|| SourceError::Malformed {
4356                        message: "GitHub project item fieldValues.nodes is not an array".into(),
4357                    })?;
4358                let item_id = required_nonblank_str(item, "id")?;
4359                let mut assigned = BTreeMap::new();
4360                for value in values {
4361                    let Some(field) = value
4362                        .pointer("/field/name")
4363                        .and_then(Value::as_str)
4364                        .and_then(BoardField::named)
4365                        .filter(|field| owned.contains(field))
4366                    else {
4367                        continue;
4368                    };
4369                    let held = assigned.insert(
4370                        field,
4371                        AssignedStatusOption {
4372                            id: StatusOptionId::try_from(
4373                                required_str(value, "optionId")?.to_owned(),
4374                            )
4375                            .map_err(|message| SourceError::Malformed { message })?,
4376                            name: ColumnName::try_from(required_str(value, "name")?.to_owned())
4377                                .map_err(|message| SourceError::Malformed {
4378                                    message: format!(
4379                                        "GitHub assigned {} name is invalid: {message}",
4380                                        field.name()
4381                                    ),
4382                                })?,
4383                        },
4384                    );
4385                    // An item holds one value of a field, so a second one leaves no way to
4386                    // tell which it holds — and a verification or recovery built on either
4387                    // could restore the wrong one.
4388                    if held.is_some() {
4389                        return Err(SourceError::Malformed {
4390                            message: format!(
4391                                "GitHub answered two {} values for board item {item_id}",
4392                                field.name()
4393                            ),
4394                        });
4395                    }
4396                }
4397                current.items.push((item_id.to_owned(), assigned));
4398            }
4399            let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4400                message: "GitHub project is missing items".into(),
4401            })?;
4402            let has_next = page
4403                .pointer("/pageInfo/hasNextPage")
4404                .and_then(Value::as_bool)
4405                .ok_or_else(|| SourceError::Malformed {
4406                    message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4407                })?;
4408            if !has_next {
4409                break;
4410            }
4411            let next =
4412                required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4413            validate_cursor_progress(after.as_deref(), next)?;
4414            after = Some(next.to_owned());
4415        }
4416        snapshot.ok_or_else(|| SourceError::Malformed {
4417            message: "GitHub returned no board field snapshot".into(),
4418        })
4419    }
4420    // llmlint: ignore-end[changed_behavior_has_e2e]
4421
4422    /// Report every board field this source's configuration names and, with
4423    /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4424    /// the `Priority` field when the board has none.
4425    ///
4426    /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4427    /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4428    /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4429    /// color and description: the whole option list goes back with every existing id, because
4430    /// a re-minted id clears every item's value.
4431    ///
4432    /// # Errors
4433    ///
4434    /// Refuses a board without a single-select `Status` field. After an apply the board is
4435    /// read again, and a pre-existing option or any item's value of either field that moved is
4436    /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4437    // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4438    // unchanged apply, a created field, an added option to each field, drift refusal, a board
4439    // with no Status field and a non-github-projects source through the compiled CLI against
4440    // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4441    pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4442        let owned: Vec<BoardField> = if self.priorities.is_some() {
4443            vec![BoardField::Status, BoardField::Priority]
4444        } else {
4445            vec![BoardField::Status]
4446        };
4447        let before = self.board_snapshot(&owned).await?;
4448        let mut plans = vec![FieldPlan {
4449            field: BoardField::Status,
4450            wanted: self.statuses.wanted(),
4451        }];
4452        if !before.fields.contains_key(&BoardField::Status) {
4453            return Err(self.no_status_field());
4454        }
4455        if let Some(mapping) = &self.priorities {
4456            plans.push(FieldPlan {
4457                field: BoardField::Priority,
4458                wanted: mapping.names().map(str::to_owned).collect(),
4459            });
4460        }
4461        // The snapshot reads single-select fields alone, so a field it did not find may still
4462        // be on the board under the name, of another type: creating one beside it would fail
4463        // part way, or leave two fields of one name. Asked of the board's own field list, and
4464        // only when a field is missing.
4465        if plans
4466            .iter()
4467            .any(|plan| !before.fields.contains_key(&plan.field))
4468        {
4469            let board = self.board_fields().await?;
4470            for plan in plans
4471                .iter()
4472                .filter(|plan| !before.fields.contains_key(&plan.field))
4473            {
4474                if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4475                    return Err(SourceError::Refused {
4476                        message: format!(
4477                            "source {}'s board has a {} field that is not a single-select field \
4478                             (it is a {}), so it cannot hold this source's options; next: rename \
4479                             or remove that field, then run this again",
4480                            self.name,
4481                            plan.field.name(),
4482                            optional_str(field, "__typename")?.unwrap_or("field of another type")
4483                        ),
4484                    });
4485                }
4486            }
4487        }
4488        let mut reports = Vec::new();
4489        for plan in &plans {
4490            let held = before.fields.get(&plan.field);
4491            let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4492            let mut missing: Vec<String> = Vec::new();
4493            for wanted in &plan.wanted {
4494                let present = existing
4495                    .iter()
4496                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4497                    || missing
4498                        .iter()
4499                        .any(|named| named.eq_ignore_ascii_case(wanted));
4500                if !present {
4501                    missing.push(wanted.clone());
4502                }
4503            }
4504            let kinds = match plan.field {
4505                BoardField::Status => self.statuses.missing_by_kind(&existing),
4506                BoardField::Priority => Vec::new(),
4507            };
4508            reports.push(FieldReport {
4509                field: plan.field,
4510                exists: held.is_some(),
4511                kinds,
4512                outcome: match (mode, held.is_some(), missing.is_empty()) {
4513                    (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4514                    (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4515                    (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4516                    (SetupMode::Apply, false, _) => FieldOutcome::Created,
4517                },
4518                missing,
4519                existing,
4520            });
4521        }
4522        // A projected field is a text field, which the single-select snapshot cannot see, so
4523        // it is asked of the board's own field list — and only when one is configured.
4524        let mut metadata_fields = Vec::new();
4525        if !self.metadata_fields.is_empty() {
4526            let board = self.board_fields().await?;
4527            for projection in &self.metadata_fields {
4528                let conflict = match Board::field(&board.fields, &projection.field)? {
4529                    None => None,
4530                    Some(field) => {
4531                        let typename = optional_str(field, "__typename")?;
4532                        let data_type = optional_str(field, "dataType")?;
4533                        if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4534                            None
4535                        } else {
4536                            let named = data_type
4537                                .filter(|kind| *kind != "TEXT")
4538                                .or(typename)
4539                                .unwrap_or("field of another type");
4540                            Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4541                                |message| SourceError::Malformed {
4542                                    message: format!(
4543                                        "GitHub answered the board's {} field with a type this \
4544                                         setup cannot read: {message}",
4545                                        &*projection.field
4546                                    ),
4547                                },
4548                            )?)
4549                        }
4550                    }
4551                };
4552                let exists =
4553                    conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4554                metadata_fields.push(MetadataFieldReport {
4555                    field: projection.field.to_string(),
4556                    key: projection.key.to_string(),
4557                    path: projection.path.clone(),
4558                    exists,
4559                    outcome: match (mode, exists || conflict.is_some()) {
4560                        (SetupMode::Plan, _) => FieldOutcome::Planned,
4561                        (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4562                        (SetupMode::Apply, false) => FieldOutcome::Created,
4563                    },
4564                    conflict,
4565                });
4566            }
4567        }
4568        let report = FieldsReport {
4569            source: self.name.clone(),
4570            fields: reports,
4571            metadata_fields,
4572        };
4573        let writes: Vec<&FieldReport> = report
4574            .fields
4575            .iter()
4576            .filter(|field| !field.missing.is_empty() || !field.exists)
4577            .collect();
4578        let creates: Vec<&MetadataFieldReport> = report
4579            .metadata_fields
4580            .iter()
4581            .filter(|field| field.outcome == FieldOutcome::Created)
4582            .collect();
4583        if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4584            return Ok(report);
4585        }
4586        let mut landed: Vec<&str> = Vec::new();
4587        for field in &writes {
4588            let added = field
4589                .missing
4590                .iter()
4591                .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4592            let sent = match before.fields.get(&field.field) {
4593                Some(held) => {
4594                    let mut options = held
4595                        .options
4596                        .iter()
4597                        .map(|option| {
4598                            json!({
4599                                "id": option.id, "name": option.name, "color": option.color,
4600                                "description": option.description,
4601                            })
4602                        })
4603                        .collect::<Vec<_>>();
4604                    options.extend(added);
4605                    self.graphql(
4606                        graphql::STATUS_OPTIONS_UPDATE,
4607                        json!({"input": {
4608                            "projectId": before.board_id, "fieldId": held.field_id,
4609                            "singleSelectOptions": options,
4610                        }}),
4611                    )
4612                    .await
4613                }
4614                None => {
4615                    self.graphql(
4616                        graphql::CREATE_FIELD,
4617                        json!({"input": {
4618                            "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4619                            "name": field.field.name(),
4620                            "singleSelectOptions": added.collect::<Vec<_>>(),
4621                        }}),
4622                    )
4623                    .await
4624                }
4625            };
4626            // A mutation that failed does not establish that GitHub left its field as it was,
4627            // so every failure from here on carries the recovery data a drift refusal does.
4628            match sent {
4629                Ok(_) => landed.push(field.field.name()),
4630                Err(error) => {
4631                    let changed = if landed.is_empty() {
4632                        String::new()
4633                    } else {
4634                        format!("changed the {} field and then ", landed.join(" and "))
4635                    };
4636                    return Err(SourceError::Refused {
4637                        message: format!(
4638                            "the guarded field setup {changed}failed on the {} field, which it may \
4639                             have changed part way: {error}; the pre-write item assignments \
4640                             are:\n{}",
4641                            field.field.name(),
4642                            recovery(&report, &before)?
4643                        ),
4644                    });
4645                }
4646            }
4647        }
4648        for field in &creates {
4649            let sent = self
4650                .graphql(
4651                    graphql::CREATE_FIELD,
4652                    json!({"input": {
4653                        "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4654                    }}),
4655                )
4656                .await;
4657            if let Err(error) = sent {
4658                let changed = if landed.is_empty() {
4659                    String::new()
4660                } else {
4661                    format!("changed the {} field and then ", landed.join(" and "))
4662                };
4663                return Err(SourceError::Refused {
4664                    message: format!(
4665                        "the guarded field setup {changed}failed creating the {} text field: \
4666                         {error}; run it again to create what is still missing",
4667                        field.field
4668                    ),
4669                });
4670            }
4671            landed.push(&field.field);
4672        }
4673        // The board has been written, so a verification read that fails leaves it unverified
4674        // rather than unchanged, and says what to put back.
4675        let after = match self.board_snapshot(&owned).await {
4676            Ok(after) => after,
4677            Err(error) => {
4678                return Err(SourceError::Refused {
4679                    message: format!(
4680                        "the guarded field setup changed the {} field and then could not read the \
4681                         board back to verify it: {error}; the pre-write item assignments are:\n{}",
4682                        landed.join(" and "),
4683                        recovery(&report, &before)?
4684                    ),
4685                });
4686            }
4687        };
4688        let mut moved = Vec::new();
4689        for field in &report.fields {
4690            let name = field.field.name();
4691            let now = after
4692                .fields
4693                .get(&field.field)
4694                .map(|held| held.options.as_slice())
4695                .unwrap_or_default();
4696            if !field.existing.iter().all(|old| now.contains(old)) {
4697                moved.push(format!(
4698                    "a pre-existing {name} option id, name, color or description"
4699                ));
4700            }
4701            if !field.missing.iter().all(|wanted| {
4702                now.iter()
4703                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4704            }) {
4705                moved.push(format!("an added {name} option"));
4706            }
4707            if after.assignments(field.field) != before.assignments(field.field) {
4708                moved.push(format!("an item's {name} value"));
4709            }
4710        }
4711        if !creates.is_empty() {
4712            // Read afresh: the view held for this command is the one from before the creates.
4713            *self.fields_cache()? = None;
4714            let board = match self.board_fields().await {
4715                Ok(board) => board,
4716                Err(error) => {
4717                    return Err(SourceError::Refused {
4718                        message: format!(
4719                            "the guarded field setup changed the {} field and then could not \
4720                             read the board back to verify it: {error}; run it again to verify \
4721                             it, which creates nothing a second time",
4722                            landed.join(" and ")
4723                        ),
4724                    });
4725                }
4726            };
4727            for field in &creates {
4728                let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
4729                    held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
4730                        && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
4731                });
4732                if !text {
4733                    moved.push(format!("the created {} text field", field.field));
4734                }
4735            }
4736        }
4737        if !moved.is_empty() {
4738            return Err(SourceError::Refused {
4739                message: format!(
4740                    "GitHub changed {} after the guarded field setup; the pre-write item \
4741                     assignments are:\n{}",
4742                    moved.join(", "),
4743                    recovery(&report, &before)?
4744                ),
4745            });
4746        }
4747        Ok(report)
4748    }
4749
4750    /// Validate configuration and capture the named credential without exposing it.
4751    ///
4752    /// # Errors
4753    ///
4754    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
4755    /// [`SourceError::Auth`] when the named credential is missing or empty.
4756    pub fn new(
4757        name: &SourceName,
4758        config: GitHubProjectsConfig,
4759        secrets: &dyn SecretResolver,
4760    ) -> Result<Self, SourceError> {
4761        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
4762    }
4763
4764    /// The same, recording every request it sends into an accounting the caller holds too.
4765    ///
4766    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
4767    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
4768    /// up — passes the one it records those into, so the session total accounts for the
4769    /// whole session rather than for this source's share of it.
4770    ///
4771    /// # Errors
4772    ///
4773    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
4774    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
4775    pub fn recording_into(
4776        name: &SourceName,
4777        config: GitHubProjectsConfig,
4778        secrets: &dyn SecretResolver,
4779        ledger: Arc<Accounting>,
4780    ) -> Result<Self, SourceError> {
4781        if !valid_github_owner(&config.owner) {
4782            return Err(SourceError::Config {
4783                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
4784            });
4785        }
4786        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
4787            return Err(SourceError::Config {
4788                message: format!("project_number must be between 1 and {}", i32::MAX),
4789            });
4790        }
4791        if !valid_environment_name(&config.token_env) {
4792            return Err(SourceError::Config {
4793                message: "token_env must be a valid environment-variable name".into(),
4794            });
4795        }
4796        let repository = config
4797            .repository
4798            .as_deref()
4799            .map(RepositoryTarget::parse)
4800            .transpose()?;
4801        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
4802            message: format!("endpoint is not a valid URL: {e}"),
4803        })?;
4804        if endpoint.scheme() != "https"
4805            && !(endpoint.scheme() == "http"
4806                && endpoint
4807                    .host_str()
4808                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
4809        {
4810            return Err(SourceError::Config {
4811                message:
4812                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
4813                        .into(),
4814            });
4815        }
4816        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
4817            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),
4818        })?;
4819        Ok(Self {
4820            name: name.clone(),
4821            owner: config.owner,
4822            project_number: config.project_number,
4823            repository,
4824            asset_client: assets::client(&endpoint)?,
4825            endpoint,
4826            token,
4827            credential_name: config.token_env,
4828            statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
4829            priorities: config
4830                .priority_mapping
4831                .map(|mapping| PriorityMapping::resolve(mapping, name))
4832                .transpose()?,
4833            metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
4834            client: Client::builder()
4835                .user_agent("onetaskgraph")
4836                .build()
4837                .map_err(|e| SourceError::Config {
4838                    message: format!("cannot build HTTP client: {e}"),
4839                })?,
4840            created: Mutex::new(Vec::new()),
4841            updated: Mutex::new(Vec::new()),
4842            commented: Mutex::new(Vec::new()),
4843            pacing: Pacing::resolve(config.pacing, name)?,
4844            last_mutation: Mutex::new(None),
4845            windows: Mutex::new(pacing::Windows::default()),
4846            task_children: Mutex::new(BTreeMap::new()),
4847            written: Mutex::new(BTreeMap::new()),
4848            deferred: Mutex::new(None),
4849            clock: system_clock(),
4850            numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
4851            board_cache: Mutex::new(None),
4852            search_cache: Mutex::new(None),
4853            narrowed_cache: Mutex::new(BTreeMap::new()),
4854            resolved_cache: Mutex::new(BTreeMap::new()),
4855            read_ahead: Mutex::new(BTreeMap::new()),
4856            carried: Mutex::new(BTreeMap::new()),
4857            held_cache: Mutex::new(BTreeMap::new()),
4858            children_cache: Mutex::new(BTreeMap::new()),
4859            search_next: Mutex::new(BTreeMap::new()),
4860            fields_cache: Mutex::new(None),
4861            repository_cache: Mutex::new(BTreeMap::new()),
4862            ledger,
4863            expected: expected::Held::default(),
4864        })
4865    }
4866
4867    /// A snapshot of every request this source has sent, and what each cost.
4868    ///
4869    /// A value to hold and compare rather than a borrow of the accounting itself, so two
4870    /// of them can sit side by side. When this source was built with
4871    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
4872    /// point of building it that way.
4873    #[must_use]
4874    pub fn accounting(&self) -> accounting::Session {
4875        self.ledger.snapshot()
4876    }
4877
4878    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
4879    /// rate limit rather than handing it straight back as an error.
4880    ///
4881    /// Retrying is safe for every document here, including the mutations, and the reason
4882    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
4883    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
4884    /// this replays has already taken effect. An outcome this source cannot know — the
4885    /// send failed, or the body could not be read, so the mutation may well have landed —
4886    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
4887    /// attempt. A duplicate write would come from replaying one of those, and none is
4888    /// replayed.
4889    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4890        if !self.expected.journaling() {
4891            return self.graphql_sent(query, variables).await;
4892        }
4893        // While a write the store's cache expects runs, its mutations select the whole issue
4894        // they leave behind, so the response vouches for what the cache keeps — and every
4895        // request is recorded, so the write's parts are reported from their own responses.
4896        let (query, variables) = match graphql::whole_twin(query) {
4897            Some(twin) => (twin, expected::whole_variables(variables)),
4898            None => (query, variables),
4899        };
4900        let sent = self.graphql_sent(query, variables.clone()).await;
4901        self.journal(query, &variables, &sent).await;
4902        sent
4903    }
4904
4905    /// One document sent, retried while GitHub asks for a wait, exactly as asked.
4906    async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4907        if is_mutation(query)
4908            && ![
4909                graphql::ADD_COMMENT,
4910                graphql::UPDATE_COMMENT,
4911                graphql::DELETE_COMMENT,
4912                graphql::ADD_COMMENT_WHOLE,
4913                graphql::UPDATE_COMMENT_WHOLE,
4914            ]
4915            .contains(&query)
4916        {
4917            let mut cache = self.resolved_cache()?;
4918            let mut ahead = self.read_ahead()?;
4919            let mut carried = self.carried()?;
4920            for argument in ["input", "second", "third", "clear"] {
4921                if let Some(input) = variables.get(argument) {
4922                    let names = |id: &NativeId, item_id: Option<&str>| {
4923                        ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
4924                            input
4925                                .get(key)
4926                                .and_then(Value::as_str)
4927                                .is_some_and(|value| value == id.0 || Some(value) == item_id)
4928                        })
4929                    };
4930                    cache.retain(|id, item| !names(id, Some(&item.item_id)));
4931                    ahead.retain(|id, item| {
4932                        !names(id, item.as_ref().map(|item| item.item_id.as_str()))
4933                    });
4934                    carried.retain(|id, item| !names(id, Some(&item.item_id)));
4935                }
4936            }
4937        }
4938        let doing = operation_description(query);
4939        let mutation = is_mutation(query);
4940        let charge = pacing::Charge::graphql(query, &variables);
4941        let mut waited = Duration::ZERO;
4942        let mut waits = 0_u32;
4943        let mut backoff = self.pacing.retry_backoff;
4944        loop {
4945            let ticket = self.pace(charge, mutation).await;
4946            let attempt = self.send_once(query, &variables, waits + 1).await;
4947            self.paced(ticket, mutation);
4948            let limited = match attempt {
4949                Ok(data) => return Ok(data),
4950                Err(Attempt::Failed(error)) => return Err(error),
4951                Err(Attempt::Limited(limited)) => limited,
4952            };
4953            let (wait, cause) = limited.wait(backoff);
4954            let remaining = self.pacing.retry_budget.saturating_sub(waited);
4955            // A wait of nothing spends none of the budget, so it is exhaustion rather
4956            // than a retry. `Pacing::resolve` rules out every way of configuring one
4957            // except a budget of zero, where reporting the first refusal is the ask.
4958            if wait.is_zero() || wait > remaining {
4959                return Err(limited.exhausted(
4960                    doing,
4961                    waits,
4962                    waited,
4963                    wait,
4964                    self.pacing.retry_budget,
4965                ));
4966            }
4967            self.clock.sleep_because(wait, cause).await;
4968            waited += wait;
4969            waits += 1;
4970            backoff = backoff.saturating_mul(2);
4971        }
4972    }
4973
4974    /// Hold one request charging `charge` until every secondary window it charges has room for
4975    /// the whole of it — and, for a mutation of an instance configured with
4976    /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
4977    /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
4978    ///
4979    /// The moment is decided and counted under the lock and waited out outside it, so two
4980    /// callers take two places rather than the same one, and no lock is held across an await.
4981    /// A request with room goes out at once: nothing here waits that a window does not ask for.
4982    async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
4983        let (wait, cause, ticket) = {
4984            // A poisoned lock here costs pacing, not correctness, and refusing the write over
4985            // it would turn an earlier failure into a second one for no gain.
4986            let mut windows = self
4987                .windows
4988                .lock()
4989                .unwrap_or_else(std::sync::PoisonError::into_inner);
4990            let now = self.clock.now();
4991            let (mut at, mut cause) = if self.pacing.windows {
4992                windows.room(now, charge)
4993            } else {
4994                (now, None)
4995            };
4996            if mutation && !self.pacing.min_mutation_interval.is_zero() {
4997                let mut last = self
4998                    .last_mutation
4999                    .lock()
5000                    .unwrap_or_else(std::sync::PoisonError::into_inner);
5001                // `checked_add` rather than `+`: adding durations can panic on overflow, and
5002                // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
5003                if let Some(earliest) = last
5004                    .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
5005                    && earliest > at
5006                {
5007                    at = earliest;
5008                    cause = Some(pacing::cause::MIN_INTERVAL);
5009                }
5010                *last = Some(at);
5011            }
5012            let ticket = windows.charge(at, charge);
5013            (at.saturating_sub(now), cause, ticket)
5014        };
5015        if let (false, Some(cause)) = (wait.is_zero(), cause) {
5016            self.clock.sleep_because(wait, cause).await;
5017        }
5018        ticket
5019    }
5020
5021    /// Record that the request `ticket` was charged for has finished, so each window — and
5022    /// the configured spacing — counts it from here rather than from the moment it left.
5023    ///
5024    /// This source can only choose when a request *departs*; GitHub's limiter counts when it
5025    /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
5026    /// departure would hand the limiter a window that looked roomier than it is, so a source
5027    /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
5028    /// paced well inside a board's threshold was once refused by it on a slower machine while
5029    /// passing on a quick one. The previous request had arrived before its response came back,
5030    /// so counting from here is never earlier than the limiter does, whatever transit costs
5031    /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
5032    /// reads for the next fifty minutes.
5033    ///
5034    /// A failed attempt is finished too: a request refused by the limiter still arrived, and
5035    /// one that never left costs only room nobody needed.
5036    fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
5037        let now = self.clock.now();
5038        self.windows
5039            .lock()
5040            .unwrap_or_else(std::sync::PoisonError::into_inner)
5041            .finish(ticket, now);
5042        if mutation && !self.pacing.min_mutation_interval.is_zero() {
5043            let mut last = self
5044                .last_mutation
5045                .lock()
5046                .unwrap_or_else(std::sync::PoisonError::into_inner);
5047            // `max` rather than an assignment: a concurrent caller may already have reserved a
5048            // slot further out, and completing this request must never pull that slot back in.
5049            *last = Some(last.map_or(now, |reserved| reserved.max(now)));
5050        }
5051    }
5052
5053    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
5054    /// failure that waiting cannot help — and recorded, whichever of the three it was.
5055    ///
5056    /// This is the one place a request leaves this crate, which is why the accounting is
5057    /// here rather than at each of the callers: a read path added later is counted without
5058    /// anybody remembering to count it, and
5059    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
5060    /// when one is not.
5061    async fn send_once(
5062        &self,
5063        query: &str,
5064        variables: &Value,
5065        number: u32,
5066    ) -> Result<Value, Attempt> {
5067        let executed = self.ledger.priced_document(query);
5068        let Attempted {
5069            result,
5070            limits,
5071            reported_cost,
5072            response_outcome,
5073        } = self.attempt(executed.as_ref(), variables).await;
5074        // No `otherwise` name: every document this source sends is one of its own, and the
5075        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
5076        let sending = accounting::Request::graphql(query, variables, None, reported_cost);
5077        let outcome = match &result {
5078            Ok(_) => accounting::Outcome::Answered,
5079            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
5080            Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
5081        };
5082        self.ledger
5083            .record(sending.finished(outcome, limits).with_execution(
5084                Some(executed.into_owned()),
5085                response_outcome,
5086                number,
5087            ));
5088        result
5089    }
5090
5091    /// The attempt itself, with what its response said about the rate limit alongside.
5092    ///
5093    /// The two are returned together rather than recorded here because every one of the
5094    /// early exits below is a different outcome, and a record written at each of them is a
5095    /// record one of them can be added without.
5096    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
5097        let mut limits = accounting::RateLimit::default();
5098        let mut reported_cost = None;
5099        let mut response_outcome = accounting::ResponseOutcome::TransportError;
5100        let result = self
5101            .attempted(
5102                query,
5103                variables,
5104                &mut limits,
5105                &mut reported_cost,
5106                &mut response_outcome,
5107            )
5108            .await;
5109        Attempted {
5110            result,
5111            limits,
5112            reported_cost,
5113            response_outcome,
5114        }
5115    }
5116
5117    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
5118    async fn attempted(
5119        &self,
5120        query: &str,
5121        variables: &Value,
5122        limits: &mut accounting::RateLimit,
5123        reported_cost: &mut Option<u64>,
5124        response_outcome: &mut accounting::ResponseOutcome,
5125    ) -> Result<Value, Attempt> {
5126        let response = self
5127            .client
5128            .post(self.endpoint.clone())
5129            .bearer_auth(self.token.expose_secret())
5130            .json(&json!({"query": query, "variables": variables}))
5131            .send()
5132            .await
5133            .map_err(|e| {
5134                Attempt::Failed(SourceError::Unavailable {
5135                    message: format!("GitHub GraphQL request failed: {}", transport_error(&e)),
5136                })
5137            })?;
5138        let status = response.status();
5139        if !status.is_success() {
5140            *response_outcome = accounting::ResponseOutcome::HttpError;
5141        }
5142        let header = |name: &str| whole_seconds(response.headers().get(name));
5143        let served = response
5144            .headers()
5145            .get(reqwest::header::DATE)
5146            .and_then(|value| value.to_str().ok())
5147            .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
5148            .map(|at| at.with_timezone(&Utc));
5149        *limits = accounting::RateLimit::read(|name| {
5150            response
5151                .headers()
5152                .get(name)
5153                .and_then(|value| value.to_str().ok())
5154                .map(str::to_owned)
5155        });
5156        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
5157        // that are not text at all — is "not known to be exhausted". This never makes a
5158        // response a refusal on its own: it says which limiter a refusal is attributed to
5159        // and where its hint comes from, so a value this cannot read costs a hint rather
5160        // than an answer.
5161        let exhausted = response
5162            .headers()
5163            .get("x-ratelimit-remaining")
5164            .and_then(|value| value.to_str().ok())
5165            == Some("0");
5166        // `retry-after` is what GitHub asks for when it asks; when it does not and the
5167        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
5168        // which is the same question answered as an absolute time. Nothing else here is a
5169        // hint, and a schedule is what answers a refusal that carries none.
5170        let retry_after = header("retry-after");
5171        let hinted_by = if retry_after.is_some() || !exhausted {
5172            Hint::RetryAfter
5173        } else {
5174            Hint::PrimaryReset
5175        };
5176        let hint = retry_after.or_else(|| {
5177            exhausted
5178                .then(|| header("x-ratelimit-reset"))
5179                .flatten()
5180                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
5181        });
5182        // Read before it is parsed, because the evidence which tells a secondary rate
5183        // limit from a rejected credential is in the body of a response whose status says
5184        // only "forbidden" — and a non-success response was never parsed at all.
5185        let body = response.text().await.map_err(|e| {
5186            Attempt::Failed(SourceError::Unavailable {
5187                message: format!(
5188                    "GitHub GraphQL response could not be read: {}",
5189                    transport_error(&e)
5190                ),
5191            })
5192        })?;
5193        *reported_cost = serde_json::from_str::<Value>(&body)
5194            .ok()
5195            .as_ref()
5196            .and_then(|body| body.pointer("/data/rateLimit/cost"))
5197            .and_then(Value::as_u64);
5198        let parsed = serde_json::from_str::<Value>(&body).ok();
5199        *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
5200        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
5201            return Err(Attempt::Limited(Limited {
5202                limiter,
5203                hint,
5204                hinted_by,
5205            }));
5206        }
5207        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
5208            return Err(Attempt::Failed(SourceError::Auth {
5209                message: format!(
5210                    "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"
5211                ),
5212            }));
5213        }
5214        if !status.is_success() {
5215            return Err(Attempt::Failed(SourceError::Unavailable {
5216                message: format!("GitHub GraphQL returned HTTP {status}"),
5217            }));
5218        }
5219        let answered = if query == graphql::ISSUES_BY_REFERENCE {
5220            self.answer_unresolved(&body)
5221        } else if query == graphql::CREATE_ISSUES {
5222            answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
5223        } else {
5224            self.answer(&body)
5225        };
5226        let mut data = answered.map_err(Attempt::Failed)?;
5227        if let Some(served) = served {
5228            stamp_served(&mut data, &served.to_rfc3339());
5229        }
5230        Ok(data)
5231    }
5232
5233    /// What one successful HTTP response says, once its GraphQL errors are read.
5234    /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
5235    /// for this credential — a repository it cannot see, an issue number it does not hold —
5236    /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
5237    /// that reference rather than a failure of the others. Any other error refuses the read,
5238    /// as there.
5239    fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
5240        let parsed: Option<Value> = serde_json::from_str(body).ok();
5241        // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
5242        // `issue`, and the answer holds `null` at exactly that path: anything else — another
5243        // kind of error, one naming no slot, or one beside a value it says is not there — is a
5244        // refusal of the whole read.
5245        let unresolved = |data: &Value, error: &Value| {
5246            error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
5247                && error
5248                    .get("path")
5249                    .and_then(Value::as_array)
5250                    .is_some_and(|path| {
5251                        let alias = path.first().and_then(Value::as_str).and_then(|alias| {
5252                            alias
5253                                .strip_prefix('r')
5254                                .and_then(|slot| slot.parse::<usize>().ok())
5255                                .filter(|slot| *slot < REFERENCE_BATCH)
5256                                .map(|_| alias)
5257                        });
5258                        match (alias, path.get(1).and_then(Value::as_str), path.len()) {
5259                            (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
5260                            (Some(alias), Some("issue"), 2) => data
5261                                .get(alias)
5262                                .and_then(|repository| repository.get("issue"))
5263                                .is_some_and(Value::is_null),
5264                            _ => false,
5265                        }
5266                    })
5267        };
5268        let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
5269            parsed.get("data").is_some_and(|data| {
5270                data.is_object()
5271                    && parsed
5272                        .get("errors")
5273                        .and_then(Value::as_array)
5274                        .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
5275            })
5276        });
5277        match parsed {
5278            Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
5279            _ => self.answer(body),
5280        }
5281    }
5282
5283    fn answer(&self, body: &str) -> Result<Value, SourceError> {
5284        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
5285            message: format!("GitHub returned invalid JSON: {e}"),
5286        })?;
5287        let errors = body
5288            .get("errors")
5289            .map(|value| {
5290                value.as_array().ok_or_else(|| SourceError::Malformed {
5291                    message: "GitHub response errors is not an array".into(),
5292                })
5293            })
5294            .transpose()?;
5295        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
5296            let messages = errors
5297                .iter()
5298                .filter_map(|e| e.get("message").and_then(Value::as_str))
5299                .collect::<Vec<_>>()
5300                .join("; ");
5301            let message = if messages.is_empty() {
5302                "GitHub returned GraphQL errors".into()
5303            } else {
5304                messages
5305            };
5306            let normalized = message.to_ascii_lowercase();
5307            if normalized.contains("resource not accessible") || normalized.contains("scope") {
5308                return Err(SourceError::Auth {
5309                    message: format!(
5310                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
5311                        self.credential_name
5312                    ),
5313                });
5314            }
5315            return Err(SourceError::Refused { message });
5316        }
5317        body.get("data")
5318            .filter(|data| data.is_object())
5319            .cloned()
5320            .ok_or_else(|| SourceError::Malformed {
5321                message: "GitHub response has no data object".into(),
5322            })
5323    }
5324
5325    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
5326    // GraphQL cannot independently page them inside the outer item page. This source page is
5327    // deliberately bounded at that published maximum; the live drift journey exercises it.
5328    async fn board_page(
5329        &self,
5330        items_after: Option<&str>,
5331        items_first: u32,
5332    ) -> Result<Value, SourceError> {
5333        let data = self
5334            .graphql(
5335                graphql::BOARD,
5336                json!({"owner":self.owner,"number":self.project_number,
5337                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
5338                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
5339            )
5340            .await?;
5341        data.pointer("/owner/projectV2")
5342            .filter(|v| !v.is_null())
5343            .cloned()
5344            .ok_or_else(|| SourceError::Refused {
5345                message: format!(
5346                    "GitHub project {}/{} was not found or is not visible to the token",
5347                    self.owner, self.project_number
5348                ),
5349            })
5350    }
5351
5352    /// The search that finds the issues of this board, narrowed by `also` when it is
5353    /// given.
5354    ///
5355    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
5356    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
5357    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
5358    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
5359    /// from a task by the `parent` field each issue carries rather than by the search.
5360    fn board_search(&self, also: Option<&str>) -> String {
5361        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
5362        match also {
5363            Some(also) => format!("{scope} {also}"),
5364            None => scope,
5365        }
5366    }
5367
5368    /// One issue this source reached directly, as the board item a read of the board would
5369    /// have produced — or `None` when this board does not hold it.
5370    ///
5371    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
5372    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
5373    /// item's own id, that item's field values, and the issue as its content. One resolver
5374    /// for both routes is what makes an issue read through a search, through its own node
5375    /// id, or through its project's sub-issues report the same title, the same status, the
5376    /// same labels and the same qualified id.
5377    ///
5378    /// An issue with no entry for *this* board is not this source's to report, which is
5379    /// what keeps an id naming some other repository's issue from being answered as an item
5380    /// of this board. That answer is given about an **exhausted** connection and never
5381    /// about an unread page: the entry is looked for on the page in hand, and only if that
5382    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
5383    /// rest of it.
5384    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
5385        self.resolve_issue_as(issue, true).await
5386    }
5387
5388    /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
5389    /// and writes only when `remember` says to.
5390    async fn resolve_issue_as(
5391        &self,
5392        issue: &Value,
5393        remember: bool,
5394    ) -> Result<Option<Resolved>, SourceError> {
5395        if optional_str(issue, "__typename")? != Some("Issue") {
5396            return Ok(None);
5397        }
5398        let memberships = issue
5399            .get("projectItems")
5400            .ok_or_else(|| SourceError::Malformed {
5401                message: "GitHub issue is missing projectItems".into(),
5402            })?;
5403        let nodes = memberships
5404            .get("nodes")
5405            .and_then(Value::as_array)
5406            .ok_or_else(|| SourceError::Malformed {
5407                message: "GitHub issue projectItems.nodes is not an array".into(),
5408            })?;
5409        let held = match self.board_entry(nodes) {
5410            Some(held) => held.clone(),
5411            None => {
5412                let info = memberships
5413                    .get("pageInfo")
5414                    .ok_or_else(|| SourceError::Malformed {
5415                        message: "GitHub issue projectItems has no pageInfo".into(),
5416                    })?;
5417                // The page held no entry for this board. Whether that means the issue is
5418                // not on it is a question about the rest of the connection, and only a
5419                // connection with no rest answers it here.
5420                if !required_bool(info, "hasNextPage")? {
5421                    return Ok(None);
5422                }
5423                let cursor = required_str(info, "endCursor")?;
5424                validate_cursor_progress(None, cursor)?;
5425                let issue_id = required_str(issue, "id")?;
5426                match self.board_membership(issue_id, cursor).await? {
5427                    Some(held) => held,
5428                    None => return Ok(None),
5429                }
5430            }
5431        };
5432        // The membership's own `updatedAt` rides along: it is the board half of the item's change
5433        // token, which a field change moves where the issue's own time does not.
5434        let item = json!({
5435            "id": required_str(&held, "id")?,
5436            "updatedAt": held.get("updatedAt"),
5437            "project": held.get("project"),
5438            "fieldValues": held.get("fieldValues"),
5439            "content": issue,
5440        });
5441        if remember {
5442            self.resolve(&item)
5443        } else {
5444            self.resolve_unremembered(&item)
5445        }
5446    }
5447
5448    /// This board's own entry among one page of an issue's `Issue.projectItems`.
5449    ///
5450    /// One spelling of *which membership is this board's*, so the page a read carries and
5451    /// the pages [`Self::board_membership`] walks are searched by the same rule.
5452    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5453        nodes.iter().find(|node| {
5454            node.pointer("/project/number").and_then(Value::as_u64)
5455                == Some(u64::from(self.project_number))
5456        })
5457    }
5458
5459    /// The rest of one issue's board memberships, from `after`, for this board's entry.
5460    ///
5461    /// The recovery read: a page of memberships that holds no entry for this board says
5462    /// nothing about the memberships past it, so the connection is walked to exhaustion
5463    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5464    /// positive answer — the whole connection was read and no entry named this board —
5465    /// rather than a failure, and the walk is held to
5466    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5467    /// with a cursor that does not advance is refused instead of spun on.
5468    async fn board_membership(
5469        &self,
5470        issue: &str,
5471        after: &str,
5472    ) -> Result<Option<Value>, SourceError> {
5473        let mut after = after.to_owned();
5474        loop {
5475            let data = self
5476                .graphql(
5477                    graphql::ISSUE_BOARD_ITEMS,
5478                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5479                           "nestedFirst":NESTED_PAGE_SIZE}),
5480                )
5481                .await?;
5482            let Some(connection) = data
5483                .pointer("/node/projectItems")
5484                .filter(|value| !value.is_null())
5485            else {
5486                // The id resolved to nothing, or to something with no memberships to walk —
5487                // which is the same answer as a connection holding no entry for this board.
5488                return Ok(None);
5489            };
5490            let nodes = connection
5491                .get("nodes")
5492                .and_then(Value::as_array)
5493                .ok_or_else(|| SourceError::Malformed {
5494                    message: "GitHub issue projectItems.nodes is not an array".into(),
5495                })?;
5496            if let Some(held) = self.board_entry(nodes) {
5497                return Ok(Some(held.clone()));
5498            }
5499            let info = connection
5500                .get("pageInfo")
5501                .ok_or_else(|| SourceError::Malformed {
5502                    message: "GitHub issue projectItems has no pageInfo".into(),
5503                })?;
5504            let next = required_bool(info, "hasNextPage")?
5505                .then(|| required_str(info, "endCursor"))
5506                .transpose()?;
5507            match next {
5508                Some(next) => {
5509                    validate_cursor_progress(Some(&after), next)?;
5510                    after = next.to_owned();
5511                }
5512                None => return Ok(None),
5513            }
5514        }
5515    }
5516
5517    /// One page of a board-scoped issue search, and where the next page resumes.
5518    async fn search_page(
5519        &self,
5520        search: &str,
5521        first: u32,
5522        after: Option<&str>,
5523    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5524        let data = self
5525            .graphql(
5526                graphql::SEARCH_ISSUES,
5527                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5528                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5529                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5530            )
5531            .await?;
5532        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5533            message: "GitHub search response has no search connection".into(),
5534        })?;
5535        let mut found = Vec::new();
5536        for node in connection
5537            .get("nodes")
5538            .and_then(Value::as_array)
5539            .ok_or_else(|| SourceError::Malformed {
5540                message: "GitHub search nodes is not an array".into(),
5541            })?
5542        {
5543            if let Some(resolved) = self.resolve_issue(node).await? {
5544                found.push(resolved);
5545            }
5546        }
5547        let info = connection
5548            .get("pageInfo")
5549            .ok_or_else(|| SourceError::Malformed {
5550                message: "GitHub search connection has no pageInfo".into(),
5551            })?;
5552        let next = required_bool(info, "hasNextPage")?
5553            .then(|| required_str(info, "endCursor"))
5554            .transpose()?
5555            .map(str::to_owned);
5556        if let Some(next) = &next {
5557            validate_cursor_progress(after, next)?;
5558        }
5559        Ok((found, next))
5560    }
5561
5562    /// Every issue this board holds, completed with what this run wrote.
5563    ///
5564    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
5565    /// is an index and is eventually consistent, so an issue this run created seconds ago
5566    /// can be absent from it, and a project listed straight after being written would
5567    /// otherwise be missing from its own board. What is added back is only what this
5568    /// process itself wrote, out of [`Self::created`], which lives and dies with the
5569    /// process.
5570    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5571        let found = self.searched_issues().await?;
5572        self.completed_with_written(found, |_| true)
5573    }
5574
5575    /// Every issue this board's own search reports, walked to exhaustion, read once per
5576    /// source.
5577    ///
5578    /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
5579    /// needs it too and the two would otherwise walk the same search twice in one command.
5580    /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
5581    /// is.
5582    async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5583        let cached = self.search_cache()?.clone();
5584        if let Some(held) = cached {
5585            return Ok(held);
5586        }
5587        let mut after: Option<String> = None;
5588        let mut found = Vec::new();
5589        let search = self.board_search(None);
5590        loop {
5591            let (page, next) = self
5592                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5593                .await?;
5594            found.extend(page);
5595            match next {
5596                Some(next) => after = Some(next),
5597                None => break,
5598            }
5599        }
5600        *self.search_cache()? = Some(found.clone());
5601        Ok(found)
5602    }
5603
5604    /// Every issue this board's own search reports with `also` beside its scope, walked to
5605    /// exhaustion — held for no later read, which may not be narrowed alike.
5606    async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5607        let mut after: Option<String> = None;
5608        let mut found = Vec::new();
5609        let search = self.board_search(Some(also));
5610        loop {
5611            let (page, next) = self
5612                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5613                .await?;
5614            found.extend(page);
5615            match next {
5616                Some(next) => after = Some(next),
5617                None => return Ok(found),
5618            }
5619        }
5620    }
5621
5622    /// This process's own view of the board's issues, or the refusal a poisoned lock is.
5623    fn search_cache(
5624        &self,
5625    ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
5626        self.search_cache
5627            .lock()
5628            .map_err(|_| SourceError::Unavailable {
5629                message: "this source's view of the board's issues was left inconsistent by an \
5630                      earlier failure; next: run the command again"
5631                    .into(),
5632            })
5633    }
5634
5635    /// `found`, with everything this run wrote that `keep` accepts and the read did not
5636    /// report.
5637    ///
5638    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
5639    /// at all: the search index is behind, and a node read of an item filed moments ago can
5640    /// be too.
5641    fn completed_with_written(
5642        &self,
5643        mut found: Vec<Resolved>,
5644        keep: impl Fn(&Resolved) -> bool,
5645    ) -> Result<Vec<Resolved>, SourceError> {
5646        for own in self.created()?.iter().filter(|own| keep(own)) {
5647            if !found.iter().any(|item| item.id == own.id) {
5648                found.push(own.clone());
5649            }
5650        }
5651        Ok(found)
5652    }
5653
5654    /// What resolving one node id reached.
5655    ///
5656    /// Three answers rather than an `Option`, because a board *draft* is none of the other
5657    /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
5658    /// is completed by a read of the draft itself rather than reported as nothing.
5659    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
5660        let asked = self
5661            .graphql(
5662                graphql::ISSUE,
5663                json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
5664                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5665            )
5666            .await;
5667        let data = match asked {
5668            Ok(data) => data,
5669            // A string that is not a node id at all is not a failure to report: it is an id
5670            // this board does not hold, which is what every read of one already answers.
5671            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
5672            Err(error) => return Err(error),
5673        };
5674        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5675            return Ok(Reached::Nothing);
5676        };
5677        if optional_str(node, "__typename")? == Some("DraftIssue") {
5678            return Ok(Reached::Draft);
5679        }
5680        Ok(match self.resolve_issue(node).await? {
5681            Some(item) => Reached::Held(Box::new(item)),
5682            None => Reached::Nothing,
5683        })
5684    }
5685
5686    /// One item of this board by its own id, whatever kind it is.
5687    ///
5688    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
5689    /// run wrote is read first, because a node read of an item created moments ago can
5690    /// still be behind the board field values written onto it — see [`Self::created`].
5691    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5692        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
5693            return Ok(Some(own.clone()));
5694        }
5695        if let Some(held) = self.read_ahead()?.remove(id) {
5696            return Ok(held);
5697        }
5698        match self.reach(id).await? {
5699            Reached::Held(item) => Ok(Some(*item)),
5700            Reached::Nothing => Ok(None),
5701            Reached::Draft => self.draft_by_id(id).await,
5702        }
5703    }
5704
5705    /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
5706    /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
5707    /// than one request per id.
5708    ///
5709    /// What this run wrote answers first, as it does there, and only the rest is read. One id
5710    /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
5711    /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
5712    /// id at a time, so that id is answered as not held and the others as themselves; a draft
5713    /// is completed by a read of the draft, exactly as there.
5714    async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
5715        let mut found: Vec<Option<Option<Resolved>>> = {
5716            let created = self.created()?;
5717            ids.iter()
5718                .map(|id| {
5719                    created
5720                        .iter()
5721                        .find(|own| own.id == *id)
5722                        .map(|own| Some(own.clone()))
5723                })
5724                .collect()
5725        };
5726        let unread: Vec<NativeId> = ids
5727            .iter()
5728            .zip(&found)
5729            .filter(|(_, found)| found.is_none())
5730            .map(|(id, _)| id.clone())
5731            .collect();
5732        let mut read = Vec::with_capacity(unread.len());
5733        if let [one] = unread.as_slice() {
5734            read.push(self.item_by_id(one).await?);
5735        } else {
5736            for batch in unread.chunks(DETAIL_BATCH) {
5737                let data = match self
5738                    .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
5739                    .await
5740                {
5741                    Ok(data) => data,
5742                    Err(error) if unresolvable_node(&error) => {
5743                        for id in batch {
5744                            read.push(self.item_by_id(id).await?);
5745                        }
5746                        continue;
5747                    }
5748                    Err(error) => return Err(error),
5749                };
5750                for (slot, id) in batch.iter().enumerate() {
5751                    let node =
5752                        data.get(format!("i{slot}"))
5753                            .ok_or_else(|| SourceError::Malformed {
5754                                message: format!(
5755                                    "GitHub answered a batch read with no item for {}",
5756                                    id.0
5757                                ),
5758                            })?;
5759                    read.push(if node.is_null() {
5760                        None
5761                    } else if optional_str(node, "__typename")? == Some("DraftIssue") {
5762                        self.draft_by_id(id).await?
5763                    } else {
5764                        if optional_str(node, "__typename")? == Some("Issue")
5765                            && required_str(node, "id")? != id.0
5766                        {
5767                            return Err(SourceError::Malformed {
5768                                message: format!(
5769                                    "GitHub answered the read of {} with issue {}",
5770                                    id.0,
5771                                    required_str(node, "id")?
5772                                ),
5773                            });
5774                        }
5775                        self.resolve_issue(node).await?
5776                    });
5777                }
5778            }
5779        }
5780        let mut read = read.into_iter();
5781        Ok(found
5782            .iter_mut()
5783            .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
5784            .collect())
5785    }
5786
5787    fn read_ahead(
5788        &self,
5789    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
5790        self.read_ahead
5791            .lock()
5792            .map_err(|_| SourceError::Unavailable {
5793                message:
5794                    "items read ahead for a copy were left inconsistent; run the command again"
5795                        .into(),
5796            })
5797    }
5798
5799    fn resolved_cache(
5800        &self,
5801    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
5802        self.resolved_cache
5803            .lock()
5804            .map_err(|_| SourceError::Unavailable {
5805                message: "resolved item records were left inconsistent; run the command again"
5806                    .into(),
5807            })
5808    }
5809
5810    /// Reuse a record this invocation already resolved. The mutation sender invalidates
5811    /// it before writing, so a partial failure cannot leave a pre-write binding behind.
5812    async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5813        let cached = self.resolved_cache()?.get(id).cloned();
5814        if let Some(item) = cached {
5815            return Ok(Some(item));
5816        }
5817        self.item_by_id(id).await
5818    }
5819
5820    /// Take out the item an issue read by reference reached for `id`, when one is held: the
5821    /// status write it was read for is the one caller, so no other read or write is ever
5822    /// answered from a read that selected only what that write needs.
5823    fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5824        Ok(self
5825            .held_cache
5826            .lock()
5827            .map_err(|_| SourceError::Unavailable {
5828                message: "items read by issue reference were left inconsistent; run the \
5829                          command again"
5830                    .into(),
5831            })?
5832            .remove(id))
5833    }
5834
5835    /// One board draft by its own id, with the board item it sits in — or `None` when no
5836    /// item of this board is that draft's.
5837    ///
5838    /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
5839    /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
5840    /// links a draft to one board item, so the page this read carries is the whole of that
5841    /// connection, and a page that reports more than it holds is refused rather than read
5842    /// as an answer about memberships nobody read.
5843    async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5844        let data = self
5845            .graphql(
5846                graphql::DRAFT,
5847                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
5848                       "boardItems":BOARD_ITEMS_PAGE_SIZE}),
5849            )
5850            .await?;
5851        // Gone between the two reads is an answer — the draft is no longer there. Anything
5852        // else than the draft [`Self::reach`] was just told this id is, is not one.
5853        let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
5854            return Ok(None);
5855        };
5856        if optional_str(draft, "__typename")? != Some("DraftIssue") {
5857            return Err(SourceError::Malformed {
5858                message: format!(
5859                    "GitHub answered {} as a draft and then as something else",
5860                    id.0
5861                ),
5862            });
5863        }
5864        if required_str(draft, "id")? != id.0 {
5865            return Err(SourceError::Malformed {
5866                message: format!("GitHub answered a different draft for {}", id.0),
5867            });
5868        }
5869        let memberships = draft
5870            .get("projectV2Items")
5871            .ok_or_else(|| SourceError::Malformed {
5872                message: format!("GitHub draft {} is missing projectV2Items", id.0),
5873            })?;
5874        let nodes = memberships
5875            .get("nodes")
5876            .and_then(Value::as_array)
5877            .ok_or_else(|| SourceError::Malformed {
5878                message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
5879            })?;
5880        let info = memberships
5881            .get("pageInfo")
5882            .ok_or_else(|| SourceError::Malformed {
5883                message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
5884            })?;
5885        // Read whether or not this board's entry is on the page: a page claiming more than
5886        // the one item GitHub links a draft to is a malformed answer either way.
5887        if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
5888            return Err(SourceError::Malformed {
5889                message: format!(
5890                    "GitHub draft {} reports more board items than the one GitHub links a draft \
5891                     to",
5892                    id.0
5893                ),
5894            });
5895        }
5896        if let Some(node) = nodes.first()
5897            && node
5898                .pointer("/project/number")
5899                .and_then(Value::as_u64)
5900                .is_none()
5901        {
5902            return Err(SourceError::Malformed {
5903                message: format!(
5904                    "GitHub draft {} board item has no numeric project number",
5905                    id.0
5906                ),
5907            });
5908        }
5909        let Some(held) = self.board_entry(nodes) else {
5910            return Ok(None);
5911        };
5912        if required_str(
5913            held.get("project").ok_or_else(|| SourceError::Malformed {
5914                message: format!("GitHub draft {} board item has no project", id.0),
5915            })?,
5916            "id",
5917        )? != self.board_fields().await?.id.as_str()
5918        {
5919            return Ok(None);
5920        }
5921        let item = json!({
5922            "id": required_str(held, "id")?,
5923            "project": held.get("project"),
5924            "fieldValues": held.get("fieldValues"),
5925            "content": draft,
5926        });
5927        self.resolve(&item)
5928    }
5929
5930    /// The board's own id and field definitions, for a write whose item does not carry
5931    /// them — never its items.
5932    ///
5933    /// A board this command has already listed supplies them, since it read them beside its
5934    /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
5935    /// is consulted about which items the board holds: see the module documentation for
5936    /// why a question about one known item is answered by reading that item.
5937    async fn board_fields(&self) -> Result<BoardFields, SourceError> {
5938        if let Some(board) = self.board_cache()?.as_ref() {
5939            return Ok(BoardFields {
5940                id: BoardId::parse(&board.id)?,
5941                fields: board.fields.clone(),
5942            });
5943        }
5944        if let Some(held) = self.fields_cache()?.clone() {
5945            return Ok(held);
5946        }
5947        let data = self
5948            .graphql(
5949                graphql::BOARD_FIELDS,
5950                json!({"owner":self.owner,"number":self.project_number,
5951                       "nestedFirst":NESTED_PAGE_SIZE}),
5952            )
5953            .await?;
5954        self.fields_read(&data)
5955    }
5956
5957    /// The board's id and fields out of an answer carrying the `boardFields` root, held for
5958    /// the rest of this command.
5959    fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
5960        let board = data
5961            .pointer("/boardFields/projectV2")
5962            .filter(|value| !value.is_null())
5963            .ok_or_else(|| SourceError::Refused {
5964                message: format!(
5965                    "GitHub project {}/{} was not found or is not visible to the token",
5966                    self.owner, self.project_number
5967                ),
5968            })?;
5969        let read = BoardFields {
5970            id: BoardId::parse(required_str(board, "id")?)?,
5971            fields: board.get("fields").cloned().unwrap_or(Value::Null),
5972        };
5973        *self.fields_cache()? = Some(read.clone());
5974        Ok(read)
5975    }
5976
5977    /// Read what creating an issue in `repository` needs and this command has not read yet —
5978    /// the board's fields and the repository's node id — in one request when it needs both.
5979    ///
5980    /// When either is already known this sends nothing, and the other is read by its own
5981    /// document where it is asked for, so no create reads anything twice.
5982    async fn creation_context(
5983        &self,
5984        repository: &RepositoryTarget,
5985        incoming: &Incoming<'_>,
5986    ) -> Result<(), SourceError> {
5987        let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
5988        if fields_known || self.repository_cache()?.contains_key(repository) {
5989            return Ok(());
5990        }
5991        let data = self
5992            .graphql(
5993                graphql::CREATION_CONTEXT,
5994                json!({"owner":self.owner,"number":self.project_number,
5995                       "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
5996                       "repositoryName":repository.name}),
5997            )
5998            .await?;
5999        self.fields_read(&data)?;
6000        self.repository_read(&data, repository, incoming)?;
6001        Ok(())
6002    }
6003
6004    /// This process's own view of the board's fields, or the refusal a poisoned lock is.
6005    fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
6006        self.fields_cache
6007            .lock()
6008            .map_err(|_| SourceError::Unavailable {
6009                message: "this source's view of the board's fields was left inconsistent by an \
6010                      earlier failure; next: run the command again"
6011                    .into(),
6012            })
6013    }
6014
6015    /// What a write to `item` needs of the board, read off that item when it says enough and
6016    /// off [`Self::board_fields`] when it does not.
6017    ///
6018    /// A node read of an item names its board and carries the definition of every field it
6019    /// holds a value of — so an item naming its board, holding a value of the origin field,
6020    /// and, when the write carries a status, holding a `Status` value, needs no read of the
6021    /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
6022    /// of may still be on the board, and a view reading it as absent would refuse a write the
6023    /// board can take or skip a field write the board needs, so such an item — and a create,
6024    /// which has no item yet — takes the board's fields from their own read instead.
6025    async fn fields_for(
6026        &self,
6027        item: Option<&Resolved>,
6028        writes_status: bool,
6029        selects_priority: bool,
6030    ) -> Result<BoardFields, SourceError> {
6031        if let Some(board) = item.and_then(Resolved::carried_board) {
6032            return Ok(board);
6033        }
6034        if let Some(item) = item
6035            && let Some(board_id) = item.named_board()
6036            && item.defines(ORIGIN_FIELD)
6037            && (!writes_status || item.defines("Status"))
6038            && (!selects_priority || item.defines(PRIORITY_FIELD))
6039            && self
6040                .metadata_fields
6041                .iter()
6042                .all(|projection| item.defines(&projection.field))
6043        {
6044            return Ok(BoardFields {
6045                id: board_id,
6046                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6047            });
6048        }
6049        self.board_fields().await
6050    }
6051
6052    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
6053    /// when that id names nothing here with a sub-issue relationship to walk.
6054    ///
6055    /// `None` and an empty answer are different: `None` is *this is not an issue of this
6056    /// GitHub*, which is what sends a project selector on to be read as a name, and an
6057    /// empty vector is a project that holds nothing.
6058    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6059        let mut after: Option<String> = None;
6060        let mut children = Vec::new();
6061        // The project issue as the first page carried it, held once the walk has ended.
6062        let mut parent: Option<Resolved> = None;
6063        loop {
6064            let asked = self
6065                .graphql(
6066                    graphql::SUB_ISSUES,
6067                    json!({"id":id.0,"memberFirst":MEMBER_PAGE_SIZE,"after":after,
6068                           "nestedFirst":NESTED_PAGE_SIZE,
6069                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6070                )
6071                .await;
6072            let data = match asked {
6073                Ok(data) => data,
6074                // A string that is not a node id at all is not a failure to report: it is
6075                // the ordinary answer to a selector naming a project by its name.
6076                Err(error) if unresolvable_node(&error) => return Ok(None),
6077                Err(error) => return Err(error),
6078            };
6079            let Some(connection) = data
6080                .pointer("/node/subIssues")
6081                .filter(|value| !value.is_null())
6082            else {
6083                // No such node, or one with no sub-issue relationship — a board draft is
6084                // the one this board can really hold.
6085                return Ok(None);
6086            };
6087            if after.is_none()
6088                && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
6089            {
6090                parent = self
6091                    .resolve_issue(node)
6092                    .await?
6093                    .filter(|resolved| resolved.id == *id);
6094            }
6095            for node in connection
6096                .get("nodes")
6097                .and_then(Value::as_array)
6098                .ok_or_else(|| SourceError::Malformed {
6099                    message: "GitHub subIssues.nodes is not an array".into(),
6100                })?
6101            {
6102                if let Some(resolved) = self.resolve_issue(node).await? {
6103                    children.push(resolved);
6104                }
6105            }
6106            let info = connection
6107                .get("pageInfo")
6108                .ok_or_else(|| SourceError::Malformed {
6109                    message: "GitHub subIssues connection has no pageInfo".into(),
6110                })?;
6111            let next = required_bool(info, "hasNextPage")?
6112                .then(|| required_str(info, "endCursor"))
6113                .transpose()?;
6114            match next {
6115                Some(next) => {
6116                    validate_cursor_progress(after.as_deref(), next)?;
6117                    after = Some(next.to_owned());
6118                }
6119                None => {
6120                    // Held only once the walk has ended, so one failing part way holds
6121                    // nothing and every read it would have answered is made.
6122                    let mut carried = self.carried()?;
6123                    for item in parent.into_iter().chain(children.iter().cloned()) {
6124                        carried.insert(item.id.clone(), item);
6125                    }
6126                    return Ok(Some(children));
6127                }
6128            }
6129        }
6130    }
6131
6132    fn carried(
6133        &self,
6134    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6135        self.carried.lock().map_err(|_| SourceError::Unavailable {
6136            message: "items a project's listing carried were left inconsistent; run the \
6137                      command again"
6138                .into(),
6139        })
6140    }
6141
6142    /// Which issue of this board a project *name* is, or `None` when none is.
6143    ///
6144    /// One bounded query which filters on that name at the server, rather than a walk of
6145    /// every issue the board holds. The name is compared again here: the qualifier narrows
6146    /// what GitHub sends, and this source decides what it names.
6147    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
6148        let search = self.board_search(Some(&title_qualifier(name)));
6149        let mut after = None;
6150        loop {
6151            let (candidates, next) = self
6152                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6153                .await?;
6154            if let Some(item) = candidates.into_iter().find(|item| {
6155                item.kind == BoardKind::Work(ItemKind::Project)
6156                    && item.title.eq_ignore_ascii_case(name)
6157            }) {
6158                return Ok(Some(item.id));
6159            }
6160            match next {
6161                Some(next) => after = Some(next),
6162                None => return Ok(None),
6163            }
6164        }
6165    }
6166
6167    /// Everything filed under one project of this board: the sub-issues of the issue that
6168    /// project is.
6169    ///
6170    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
6171    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
6172    /// gains projects, or as another project gains tasks.
6173    ///
6174    /// A qualified id names the issue and is asked for its sub-issues directly: one
6175    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
6176    /// read as a project *name*, which costs the one bounded search
6177    /// [`Self::project_by_name`] makes.
6178    ///
6179    /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
6180    /// — and each answer is still cut to the items whose parent is this project, so one this
6181    /// process has since filed elsewhere is not reported here, and completed with what this
6182    /// process filed under it.
6183    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
6184        let held = self.children_cache()?.get(selector).cloned();
6185        let (project, children) = match held {
6186            Some(held) => held,
6187            None => {
6188                let answered = match self.sub_issues(selector).await? {
6189                    Some(children) => (selector.clone(), children),
6190                    None => match self.project_by_name(&selector.0).await? {
6191                        Some(project) => {
6192                            let children = self.sub_issues(&project).await?.unwrap_or_default();
6193                            (project, children)
6194                        }
6195                        None => return Ok(Vec::new()),
6196                    },
6197                };
6198                self.children_cache()?
6199                    .insert(selector.clone(), answered.clone());
6200                answered
6201            }
6202        };
6203        let mut children: Vec<Resolved> = children
6204            .into_iter()
6205            .filter(|child| child.parent.as_ref() == Some(&project))
6206            .collect();
6207        for own in self.updated()?.iter() {
6208            if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
6209                children.push(own.clone());
6210            }
6211        }
6212        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
6213    }
6214
6215    /// Every issue of this board GitHub's issue search reports updated at or after `since`,
6216    /// completed with what this run wrote — the candidates a comment-activity read confirms.
6217    ///
6218    /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
6219    /// on the board whatever repository, and whatever owner, it lives in, so no repository or
6220    /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
6221    /// sufficient is a fact about GitHub rather than about this source: a comment written on an
6222    /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
6223    /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
6224    /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
6225    /// rather than silently narrowing a caller's answer.
6226    ///
6227    /// The instant is written to the second, rounded down, which can only widen what the
6228    /// search returns; confirmation against each candidate's own comments is what makes the
6229    /// answer exact. The search is an index that lags a write by a second or two — the module
6230    /// documentation records it — so a caller that asks again from its last instant should
6231    /// overlap the two by more than that.
6232    async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
6233        let found = self.searched(&updated_qualifier(since)).await?;
6234        self.completed_with_written(found, |_| true)
6235    }
6236
6237    /// Every issue of this board GitHub's issue search reports for the board-scoped search
6238    /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
6239    ///
6240    /// Uncompleted: what this process wrote is added by the caller, which knows whether its
6241    /// own record is the fresher of the two.
6242    async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6243        let search = self.board_search(Some(also));
6244        let mut after: Option<String> = None;
6245        let mut found = Vec::new();
6246        loop {
6247            let (page, next) = self
6248                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6249                .await?;
6250            found.extend(page);
6251            match next {
6252                Some(next) => after = Some(next),
6253                None => return Ok(found),
6254            }
6255        }
6256    }
6257
6258    /// A bounded task answer; the versioned cursor carries the connection position, how
6259    /// many rows of the page starting there were already handed out, and the own-write ids
6260    /// already observed, including across a new source instance.
6261    ///
6262    /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
6263    /// sliced from the pages it needs; why is the module documentation's paging contract.
6264    async fn search_tasks(
6265        &self,
6266        query: &TaskQuery,
6267        page: &PageRequest,
6268        also: &str,
6269    ) -> Result<Page<Task>, SourceError> {
6270        let mut position = match &page.cursor {
6271            None => SearchPosition::default(),
6272            Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
6273                .ok()
6274                .filter(|position| {
6275                    position.version == SEARCH_CURSOR_VERSION
6276                        && position.connection.valid_resume(position.offset)
6277                })
6278                .ok_or_else(|| SourceError::Config {
6279                    message: "page cursor is invalid".into(),
6280                })?,
6281        };
6282        let search = self.board_search(Some(also));
6283        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6284        let own = self.with_own_writes(Vec::new())?;
6285        // An issue this process commented on is a candidate of a comment-activity read
6286        // whether or not the search has caught up with the comment; see `Self::commented`.
6287        let commented = match query.commented_since {
6288            Some(_) => self.commented()?.clone(),
6289            None => Vec::new(),
6290        };
6291        for id in own.iter().map(|item| &item.id).chain(&commented) {
6292            if !position.own.contains(id) {
6293                position.own.push(id.clone());
6294            }
6295        }
6296        let mut tasks = Vec::new();
6297        while !position.connection.exhausted() && tasks.len() < limit {
6298            let first = SEARCH_PAGE_SIZE;
6299            // Page size is part of the key: a short cached answer cannot answer a wider ask.
6300            let key =
6301                serde_json::to_string(&("page", &search, &position.connection.after(), first))
6302                    .expect("search page key is serializable");
6303            let cached = if query.commented_since.is_none() {
6304                self.narrowed_cache()?.get(&key).cloned()
6305            } else {
6306                None
6307            };
6308            let (found, next) = match cached {
6309                Some(found) => {
6310                    let next = self
6311                        .search_next
6312                        .lock()
6313                        .map_err(|_| SourceError::Unavailable {
6314                            message:
6315                                "search pagination was left inconsistent; run the command again"
6316                                    .into(),
6317                        })?
6318                        .get(&key)
6319                        .cloned()
6320                        .flatten();
6321                    (found, next)
6322                }
6323                None => {
6324                    let (found, next) = self
6325                        .search_page(&search, first, position.connection.after())
6326                        .await?;
6327                    if query.commented_since.is_none() {
6328                        self.search_next
6329                            .lock()
6330                            .map_err(|_| SourceError::Unavailable {
6331                                message:
6332                                    "search pagination was left inconsistent; run the command again"
6333                                        .into(),
6334                            })?
6335                            .insert(key.clone(), next.clone());
6336                        self.narrowed_cache()?.insert(key, found.clone());
6337                    }
6338                    (found, next)
6339                }
6340            };
6341            let rows = found.len();
6342            for mut item in found.into_iter().skip(position.offset) {
6343                if tasks.len() == limit {
6344                    break;
6345                }
6346                position.offset += 1;
6347                if position.own.contains(&item.id) {
6348                    if position.seen.contains(&item.id) {
6349                        continue;
6350                    }
6351                    position.seen.push(item.id.clone());
6352                    // The search's own copy of an issue this process only commented on is as
6353                    // good as a node read of it, since its comments are read either way.
6354                    let only_commented = commented.contains(&item.id)
6355                        && !own.iter().any(|written| written.id == item.id);
6356                    if !only_commented {
6357                        let updated_at = item.updated_at;
6358                        let Some(written) = self.search_written(&own, &item.id).await? else {
6359                            continue;
6360                        };
6361                        item = written;
6362                        item.updated_at = item.updated_at.max(updated_at);
6363                        self.resolved_cache()?.insert(item.id.clone(), item.clone());
6364                    }
6365                }
6366                if item.kind == BoardKind::Work(ItemKind::Task) {
6367                    let task = item.task()?;
6368                    if task_matches(&task, query, &query.project)
6369                        && self.commented_since(&item, query.commented_since).await?
6370                    {
6371                        tasks.push(task);
6372                    }
6373                }
6374            }
6375            if position.offset < rows {
6376                continue;
6377            }
6378            position.offset = 0;
6379            position.connection = match next {
6380                Some(after) => SearchConnection::Continuing {
6381                    after: Cursor(after),
6382                },
6383                None => SearchConnection::Exhausted {},
6384            };
6385        }
6386        if position.connection.exhausted() {
6387            for id in position.own.clone() {
6388                if position.seen.contains(&id) {
6389                    continue;
6390                }
6391                if tasks.len() == limit {
6392                    break;
6393                }
6394                position.seen.push(id.clone());
6395                let Some(item) = self.search_written(&own, &id).await? else {
6396                    continue;
6397                };
6398                if item.kind == BoardKind::Work(ItemKind::Task) {
6399                    let task = item.task()?;
6400                    if task_matches(&task, query, &query.project)
6401                        && self.commented_since(&item, query.commented_since).await?
6402                    {
6403                        tasks.push(task);
6404                    }
6405                }
6406            }
6407        }
6408        let more = !position.connection.exhausted()
6409            || position.own.iter().any(|id| !position.seen.contains(id));
6410        Ok(Page {
6411            items: tasks,
6412            next: more.then(|| {
6413                Cursor(serde_json::to_string(&position).expect("search position is serializable"))
6414            }),
6415        })
6416    }
6417
6418    /// A resumed process has the ids but no write records; resolve only a record the
6419    /// current page needs, by its uncached node read rather than the lagging search index.
6420    async fn search_written(
6421        &self,
6422        own: &[Resolved],
6423        id: &NativeId,
6424    ) -> Result<Option<Resolved>, SourceError> {
6425        match own.iter().find(|item| item.id == *id) {
6426            Some(item) => Ok(Some(item.clone())),
6427            None => self.item_by_id(id).await,
6428        }
6429    }
6430
6431    /// The candidates for a task query carrying a text, metadata or origin predicate, read
6432    /// without enumerating the board — or `None` for a query carrying none of the three, which
6433    /// keeps the reads it always had.
6434    ///
6435    /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
6436    /// because it names at most a handful of items. Text and metadata are answered by one
6437    /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
6438    /// further by `updated:>=` when the query also asks for comment activity, since both
6439    /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
6440    /// process afterwards by the same predicates [`task_matches`] applies to every read.
6441    ///
6442    /// Completed with what this process wrote, its own record winning over the index's copy
6443    /// of the same item: see [`Self::with_own_writes`].
6444    async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
6445        let asked = match (&query.origin, narrowing_qualifiers(query)) {
6446            (Some(origin), _) => Narrowing::Origin(origin.clone()),
6447            (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
6448                Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
6449                None => qualifiers,
6450            }),
6451            (None, None) => return Ok(None),
6452        };
6453        // A question about comment activity is asked afresh every time, as it always was: it
6454        // is the one a caller polls from one source while waiting for the index, and an
6455        // answer held from the first poll would be the answer to every later one.
6456        let key = query.commented_since.is_none().then(|| asked.key());
6457        let cached = match &key {
6458            Some(key) => self.narrowed_cache()?.get(key).cloned(),
6459            None => None,
6460        };
6461        let found = match cached {
6462            Some(found) => found,
6463            None => {
6464                let found = match &asked {
6465                    Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
6466                    Narrowing::Search(also) => self.searched(also).await?,
6467                };
6468                if let Some(key) = key {
6469                    self.narrowed_cache()?.insert(key, found.clone());
6470                }
6471                found
6472            }
6473        };
6474        self.with_own_writes(found).map(Some)
6475    }
6476
6477    /// The candidates for a project or unscoped document query carrying a searchable text,
6478    /// read without enumerating the board — or `None` for a query with no text or a blank one,
6479    /// which keeps the read it always had.
6480    ///
6481    /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
6482    /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
6483    /// costs is the issues that match and never the board. Its answer is held for the command
6484    /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
6485    /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
6486    /// substring rule, exactly as an item of the wider read was, and is completed with what this
6487    /// process wrote: see [`Self::with_own_writes`].
6488    async fn text_searched(
6489        &self,
6490        text: Option<&TextQuery>,
6491    ) -> Result<Option<Vec<Resolved>>, SourceError> {
6492        let Some(also) = text_qualifiers(text) else {
6493            return Ok(None);
6494        };
6495        let key = Narrowing::Search(also.clone()).key();
6496        let cached = self.narrowed_cache()?.get(&key).cloned();
6497        let found = match cached {
6498            Some(found) => found,
6499            None => {
6500                let found = self.searched(&also).await?;
6501                self.narrowed_cache()?.insert(key, found.clone());
6502                found
6503            }
6504        };
6505        self.with_own_writes(found).map(Some)
6506    }
6507
6508    /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
6509    /// [`Self::narrowed`] reads for a task query carrying that origin — one
6510    /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
6511    /// what this process wrote — each confirmed against its own origin entry.
6512    ///
6513    /// What answers a project or document listing by origin, so finding every copy of one plan
6514    /// costs the items that carry its origin and never a page of the board.
6515    async fn copied_from(
6516        &self,
6517        origin: &str,
6518        kind: BoardKind,
6519    ) -> Result<Vec<Resolved>, SourceError> {
6520        let wanted = TaskQuery {
6521            origin: Some(origin.to_owned()),
6522            ..TaskQuery::default()
6523        };
6524        Ok(self
6525            .narrowed(&wanted)
6526            .await?
6527            .unwrap_or_default()
6528            .into_iter()
6529            .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
6530            .collect())
6531    }
6532
6533    /// Every item of this board that may carry `origin` — a superset of those that do — found
6534    /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
6535    ///
6536    /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
6537    /// which reads the field every carrier holds, whichever release wrote it — and the
6538    /// board-scoped issue search for the same id as a phrase in the body, where this source
6539    /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
6540    /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
6541    /// query's, exactly.
6542    ///
6543    /// Both connections are walked to exhaustion, each from its own cursor. One that has
6544    /// already ended is sent its last cursor again, which answers an empty page, so the one
6545    /// document serves every page of either. What the two leave is stated in the module
6546    /// documentation: a carrier another process added within the last second or two, before
6547    /// either index has it.
6548    async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
6549        let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
6550        let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
6551        let mut items_after: Option<String> = None;
6552        let mut search_after: Option<String> = None;
6553        let mut found: Vec<Resolved> = Vec::new();
6554        let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
6555            if !found.iter().any(|held| held.id == resolved.id) {
6556                found.push(resolved);
6557            }
6558        };
6559        loop {
6560            let data = self
6561                .graphql(
6562                    graphql::ORIGIN_LOOKUP,
6563                    json!({"owner":self.owner,"number":self.project_number,"filter":filter,
6564                           "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
6565                           "itemsAfter":items_after,"searchAfter":search_after,
6566                           "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6567                           "duplicates":true}),
6568                )
6569                .await?;
6570            let items = data
6571                .pointer("/originItems/projectV2/items")
6572                .filter(|value| !value.is_null())
6573                .ok_or_else(|| SourceError::Refused {
6574                    message: format!(
6575                        "GitHub project {}/{} was not found or is not visible to the token",
6576                        self.owner, self.project_number
6577                    ),
6578                })?;
6579            for item in optional_nodes(Some(items), "project items")?
6580                .into_iter()
6581                .flatten()
6582            {
6583                // The board's own items list its drafts too, and a draft is not an issue: no
6584                // narrowed read answers with one, whatever its origin field holds.
6585                if let Some(resolved) = self.resolve(item)?
6586                    && resolved.content_kind == ContentKind::Issue
6587                {
6588                    keep(resolved, &mut found);
6589                }
6590            }
6591            let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
6592                message: "GitHub search response has no search connection".into(),
6593            })?;
6594            for node in optional_nodes(Some(searched), "search")?
6595                .into_iter()
6596                .flatten()
6597            {
6598                if let Some(resolved) = self.resolve_issue(node).await? {
6599                    keep(resolved, &mut found);
6600                }
6601            }
6602            let items_next = resumed(items, items_after.as_deref())?;
6603            let search_next = resumed(searched, search_after.as_deref())?;
6604            if !items_next.has_more() && !search_next.has_more() {
6605                return Ok(found);
6606            }
6607            items_after = items_next.cursor();
6608            search_after = search_next.cursor();
6609        }
6610    }
6611
6612    /// `found`, with every item this process created or wrote in its place, and every one of
6613    /// them the read did not report added.
6614    ///
6615    /// This process's own record wins over the read's copy of the same item, because a read
6616    /// of an item written moments ago can still be behind what was written onto it — the
6617    /// origin field included, which is the one a narrowed read is confirmed against — and a
6618    /// read that still names an item under a predicate this process's write moved it out of
6619    /// must not return it. The one thing the read knows that the record cannot is when GitHub
6620    /// last saw the item change, which is what a comment-activity read rules a candidate out
6621    /// by, so the read's `updatedAt` is kept when the record has none of its own. See
6622    /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
6623    fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
6624        // A board draft is not an issue, so no narrowed read returns one, and this process
6625        // having written one does not make it an answer either.
6626        let own: Vec<Resolved> = self
6627            .created()?
6628            .iter()
6629            .chain(self.updated()?.iter())
6630            .filter(|own| own.content_kind == ContentKind::Issue)
6631            .cloned()
6632            .collect();
6633        for mut own in own {
6634            self.resolved_cache()?.insert(own.id.clone(), own.clone());
6635            match found.iter_mut().find(|read| read.id == own.id) {
6636                Some(read) => {
6637                    own.updated_at = own.updated_at.max(read.updated_at);
6638                    *read = own;
6639                }
6640                None => found.push(own),
6641            }
6642        }
6643        Ok(found)
6644    }
6645
6646    /// Whether `item` has a comment created or last edited at or after `since` — always, when
6647    /// there is no instant to hold it to.
6648    ///
6649    /// The candidate's own `updatedAt` is read first, because a comment written or edited at
6650    /// or after the instant moved it there: an issue not updated since holds no such comment,
6651    /// and its comments are never asked for — unless this process commented on it in this
6652    /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
6653    /// only as far as the first that matches. A board draft is not an issue and has no
6654    /// comments, so it never matches.
6655    async fn commented_since(
6656        &self,
6657        item: &Resolved,
6658        since: Option<DateTime<Utc>>,
6659    ) -> Result<bool, SourceError> {
6660        let Some(since) = since else {
6661            return Ok(true);
6662        };
6663        if item.content_kind == ContentKind::DraftIssue {
6664            return Ok(false);
6665        }
6666        // An `updatedAt` this process's own record or a lagging index holds can predate a
6667        // comment this process wrote since, so only an issue it did not comment on is ruled
6668        // out by one.
6669        if item.updated_at.is_some_and(|updated| updated < since)
6670            && !self.commented()?.contains(&item.id)
6671        {
6672            return Ok(false);
6673        }
6674        let query = TaskQuery {
6675            commented_since: Some(since),
6676            ..TaskQuery::default()
6677        };
6678        let mut after: Option<String> = None;
6679        loop {
6680            let data = self
6681                .graphql(
6682                    graphql::ISSUE_COMMENTS,
6683                    json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
6684                )
6685                .await?;
6686            let Some(connection) = data
6687                .get("node")
6688                .filter(|value| !value.is_null())
6689                .and_then(|node| node.get("comments"))
6690                .filter(|value| !value.is_null())
6691            else {
6692                // Removed since the search reported it: no longer an issue with comments.
6693                return Ok(false);
6694            };
6695            let comments = optional_nodes(Some(connection), "issue comments")?
6696                .into_iter()
6697                .flatten()
6698                .map(comment_from)
6699                .collect::<Result<Vec<_>, _>>()?;
6700            if query.comments_match(&comments) {
6701                return Ok(true);
6702            }
6703            match next_cursor(connection)? {
6704                Some(next) => {
6705                    validate_cursor_progress(after.as_deref(), &next.0)?;
6706                    after = Some(next.0);
6707                }
6708                None => return Ok(false),
6709            }
6710        }
6711    }
6712
6713    /// Every item on the board: the union of both enumerations GitHub offers of one.
6714    ///
6715    /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
6716    /// board **draft** and reads the board's own fields beside its items, and only the search
6717    /// reports an item that connection is behind on. The module documentation is where the lag and the
6718    /// measurements behind it are written down.
6719    ///
6720    /// A search result is admitted on the same terms as any other issue this source reaches
6721    /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
6722    /// names *this* board — so an issue the index still believes is here after it was taken
6723    /// off is refused rather than reported.
6724    ///
6725    /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
6726    /// which is what the cache could otherwise have broken.
6727    async fn board(&self) -> Result<Board, SourceError> {
6728        self.board_completed_by(None).await
6729    }
6730
6731    /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
6732    /// that half could add is then answered only if it holds.
6733    async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
6734        let cached = self.board_cache()?.clone();
6735        let mut board = match cached {
6736            Some(board) => board,
6737            None => {
6738                let read = self.read_board().await?;
6739                *self.board_cache()? = Some(read.clone());
6740                read
6741            }
6742        };
6743        let searched = match also {
6744            None => self.searched_issues().await?,
6745            Some(also) => self.searched_narrowly(also).await?,
6746        };
6747        for held in searched {
6748            if !board.items.iter().any(|item| item.id == held.id) {
6749                board.items.push(held);
6750            }
6751        }
6752        for own in self.created()?.iter() {
6753            if !board.items.iter().any(|item| item.id == own.id) {
6754                board.items.push(own.clone());
6755            }
6756        }
6757        Ok(board)
6758    }
6759
6760    /// This process's own view of the board, or the refusal a poisoned lock is.
6761    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
6762        self.board_cache
6763            .lock()
6764            .map_err(|_| SourceError::Unavailable {
6765                message: "this source's view of the board was left inconsistent by an earlier \
6766                      failure; next: run the command again"
6767                    .into(),
6768            })
6769    }
6770
6771    /// Bring this process's own view of the board up to an item it has just written.
6772    ///
6773    /// A created item goes to `created`, which is what completes a board read GitHub's own
6774    /// eventual consistency has left behind. An item that was already there is replaced
6775    /// where it sits, so a second write of it in the same command reads its real parent
6776    /// rather than the one it had before the first write.
6777    ///
6778    /// "Where it sits" is three places, and missing an earlier one leaves a stale record
6779    /// that wins: an item this same run created is held in `created` and not in the cached
6780    /// board, and `board` completes the cached board *from* `created`, so replacing only
6781    /// the cached copy of such an item replaces nothing and the read still reports the
6782    /// title it was created with. The search is the third, and it is the one an item the
6783    /// board's own projection is behind on sits in *alone* — which is exactly the item this
6784    /// source is least able to re-read, so leaving it out would put the stale title back on
6785    /// the only items the completion in [`Self::board`] exists for.
6786    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
6787        self.resolved_cache()?.insert(item.id.clone(), item.clone());
6788        if created {
6789            self.created()?.push(item);
6790            return Ok(());
6791        }
6792        {
6793            let mut own = self.created()?;
6794            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
6795                *held = item;
6796                return Ok(());
6797            }
6798        }
6799        {
6800            let mut own = self.updated()?;
6801            match own.iter_mut().find(|held| held.id == item.id) {
6802                Some(held) => *held = item.clone(),
6803                None => own.push(item.clone()),
6804            }
6805        }
6806        if let Some(board) = self.board_cache()?.as_mut()
6807            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
6808        {
6809            *held = item.clone();
6810        }
6811        if let Some(found) = self.search_cache()?.as_mut()
6812            && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
6813        {
6814            *held = item.clone();
6815        }
6816        for found in self.narrowed_cache()?.values_mut() {
6817            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
6818                *held = item.clone();
6819            }
6820        }
6821        for (_, found) in self.children_cache()?.values_mut() {
6822            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
6823                *held = item.clone();
6824            }
6825        }
6826        Ok(())
6827    }
6828
6829    /// Forget one item this process has just deleted, from every half of its own view.
6830    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
6831        if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
6832            created.retain(|held| held.content != *id);
6833        }
6834        self.resolved_cache()?.remove(id);
6835        self.read_ahead()?.remove(id);
6836        self.carried()?.remove(id);
6837        self.created()?.retain(|own| own.id != *id);
6838        self.updated()?.retain(|own| own.id != *id);
6839        self.commented()?.retain(|own| own != id);
6840        if let Some(board) = self.board_cache()?.as_mut() {
6841            board.items.retain(|item| item.id != *id);
6842        }
6843        if let Some(found) = self.search_cache()?.as_mut() {
6844            found.retain(|item| item.id != *id);
6845        }
6846        for found in self.narrowed_cache()?.values_mut() {
6847            found.retain(|item| item.id != *id);
6848        }
6849        for (_, found) in self.children_cache()?.values_mut() {
6850            found.retain(|item| item.id != *id);
6851        }
6852        Ok(())
6853    }
6854
6855    /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
6856    fn narrowed_cache(
6857        &self,
6858    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
6859        self.narrowed_cache
6860            .lock()
6861            .map_err(|_| SourceError::Unavailable {
6862                message: "this source's view of a narrowed read was left inconsistent by an \
6863                      earlier failure; next: run the command again"
6864                    .into(),
6865            })
6866    }
6867
6868    /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
6869    /// is.
6870    /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
6871    fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6872        Ok(self.task_children()?.get(project).cloned())
6873    }
6874
6875    fn task_children(
6876        &self,
6877    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
6878        self.task_children
6879            .lock()
6880            .map_err(|_| SourceError::Unavailable {
6881                message: "this source's record of a project's tasks was left inconsistent by an \
6882                          earlier failure; next: run the command again"
6883                    .into(),
6884            })
6885    }
6886
6887    fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
6888        self.children_cache
6889            .lock()
6890            .map_err(|_| SourceError::Unavailable {
6891                message: "this source's view of a project's tasks was left inconsistent by an \
6892                      earlier failure; next: run the command again"
6893                    .into(),
6894            })
6895    }
6896
6897    /// Every page of the board, read from GitHub.
6898    async fn read_board(&self) -> Result<Board, SourceError> {
6899        let mut after: Option<String> = None;
6900        let mut items = Vec::new();
6901        let mut board;
6902        loop {
6903            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
6904            for item in page
6905                .pointer("/items/nodes")
6906                .and_then(Value::as_array)
6907                .ok_or_else(|| SourceError::Malformed {
6908                    message: "GitHub project items.nodes is not an array".into(),
6909                })?
6910            {
6911                if let Some(resolved) = self.resolve(item)? {
6912                    items.push(resolved);
6913                }
6914            }
6915            let info = page
6916                .pointer("/items/pageInfo")
6917                .ok_or_else(|| SourceError::Malformed {
6918                    message: "GitHub project items have no pageInfo".into(),
6919                })?;
6920            let has_next = required_bool(info, "hasNextPage")?;
6921            let next = has_next
6922                .then(|| required_str(info, "endCursor"))
6923                .transpose()?;
6924            board = page.clone();
6925            match next {
6926                Some(next) => {
6927                    validate_cursor_progress(after.as_deref(), next)?;
6928                    after = Some(next.to_owned());
6929                }
6930                None => break,
6931            }
6932        }
6933        Ok(Board {
6934            id: required_str(&board, "id")?.to_owned(),
6935            fields: board.get("fields").cloned().unwrap_or(Value::Null),
6936            items,
6937        })
6938    }
6939
6940    /// The existing items this source has written, for completing a narrowed read that is
6941    /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
6942    fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
6943        self.updated.lock().map_err(|_| SourceError::Unavailable {
6944            message: "this source's record of what it wrote in this run was left inconsistent \
6945                      by an earlier failure; next: run the command again"
6946                .into(),
6947        })
6948    }
6949
6950    /// The issues this source has commented on in this command; see
6951    /// [`Self::commented`](GitHubProjectsSource::commented).
6952    fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
6953        self.commented.lock().map_err(|_| SourceError::Unavailable {
6954            message: "this source's record of what it commented on in this run was left \
6955                      inconsistent by an earlier failure; next: run the command again"
6956                .into(),
6957        })
6958    }
6959
6960    /// Called only once GitHub has answered the comment write, so an issue whose comment
6961    /// failed is never made a candidate a later read would pay a node read for.
6962    fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
6963        let mut commented = self.commented()?;
6964        if !commented.contains(issue) {
6965            commented.push(issue.clone());
6966        }
6967        Ok(())
6968    }
6969
6970    /// The items this source has created, for completing a board read that is behind.
6971    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
6972        self.created.lock().map_err(|_| SourceError::Unavailable {
6973            message: "this source's record of what it created in this run was left \
6974                      inconsistent by an earlier failure; next: run the command again"
6975                .into(),
6976        })
6977    }
6978
6979    /// One board item as this source reports it, or `None` for content it ignores.
6980    ///
6981    /// A pull request is neither a project nor a task — it is somebody's change, not a
6982    /// unit of plan — and an item whose content the token cannot see has nothing to
6983    /// report at all.
6984    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
6985        let resolved = self.resolve_unremembered(item)?;
6986        if let Some(resolved) = &resolved {
6987            self.resolved_cache()?
6988                .insert(resolved.id.clone(), resolved.clone());
6989        }
6990        Ok(resolved)
6991    }
6992
6993    /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
6994    /// write's own response needs, which says nothing this process should answer a later read
6995    /// with.
6996    fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
6997        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
6998            message: "GitHub project item is missing content".into(),
6999        })?;
7000        if content.is_null() {
7001            return Ok(None);
7002        }
7003        let content_kind = match required_str(content, "__typename")? {
7004            "Issue" => ContentKind::Issue,
7005            "DraftIssue" => ContentKind::DraftIssue,
7006            _ => return Ok(None),
7007        };
7008        let field_values = item
7009            .get("fieldValues")
7010            .ok_or_else(|| SourceError::Malformed {
7011                message: "GitHub project item is missing fieldValues".into(),
7012            })?;
7013        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
7014        let nodes = field_values
7015            .get("nodes")
7016            .and_then(Value::as_array)
7017            .ok_or_else(|| SourceError::Malformed {
7018                message: "GitHub project item fieldValues.nodes is not an array".into(),
7019            })?;
7020        if let Some(labels) = content.get("labels") {
7021            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
7022        }
7023        let raw_body = optional_str(content, "body")?.map(str::to_owned);
7024        let (body, slot) = metadata_body(raw_body.clone())?;
7025        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
7026            .map(|id| NativeId(id.to_owned()));
7027        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
7028        // to read one from; it is a task, and never a project.
7029        let sub_issues = match content_kind {
7030            ContentKind::Issue => sub_issue_total(content)?,
7031            ContentKind::DraftIssue => 0,
7032        };
7033        let content_id = required_str(content, "id")?;
7034        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7035            message: format!("GitHub issue {content_id}: {message}"),
7036        })?;
7037        let raw_title = required_str(content, "title")?;
7038        let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
7039        // The title a person wrote, which for a document is the one without the prefix —
7040        // the same way `content` above is the body without this source's metadata slot.
7041        let title = match kind {
7042            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
7043            BoardKind::Work(_) => raw_title.to_owned(),
7044        };
7045        let own_repository = content
7046            .pointer("/repository/nameWithOwner")
7047            .and_then(Value::as_str)
7048            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
7049            .transpose()
7050            .map_err(|message| SourceError::Malformed { message })?;
7051        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
7052            Repository::from_metadata(&slot)
7053                .map_err(|message| SourceError::Malformed { message })?
7054        } else {
7055            own_repository.clone().into_iter().collect()
7056        };
7057        let classification = Classification::from_metadata(&slot)
7058            .map_err(|message| SourceError::Malformed { message })?;
7059        let id = NativeId(content_id.to_owned());
7060        // Read only for a task, because only a task has either list: a project or a
7061        // document holding one of these keys holds nothing this source reports, and the
7062        // keys are left out of its caller-visible metadata all the same.
7063        let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
7064            let listed = |key: &str| {
7065                TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
7066                    .map_err(|message| SourceError::Malformed { message })
7067            };
7068            (
7069                listed(TaskRef::DELIVERS_KEY)?,
7070                listed(TaskRef::DELIVERED_BY_KEY)?,
7071            )
7072        } else {
7073            (Vec::new(), Vec::new())
7074        };
7075        let (option, closed, reason) = Self::status_parts(nodes, content)?;
7076        let priority = self.held_priority(nodes)?;
7077        let item_labels = labels(content)?;
7078        // Present when the item was reached through its own issue, whose board entry
7079        // names the board; a read of the board's own items has the board already. An
7080        // empty id names nothing a field write could address, so it is read as absent and
7081        // the write goes back to reading the board.
7082        let board_id = item
7083            .pointer("/project/id")
7084            .and_then(Value::as_str)
7085            .filter(|id| !id.is_empty());
7086        let resolved = Resolved {
7087            item_id: required_str(item, "id")?.to_owned(),
7088            id,
7089            content_kind,
7090            kind,
7091            title,
7092            body: body.filter(|value| !value.is_empty()),
7093            raw_body,
7094            status: self
7095                .statuses
7096                .status(kind.status_kind(), option, closed, reason),
7097            option: option.map(str::to_owned),
7098            priority,
7099            closed,
7100            reason: closed.then(|| reason.map(str::to_owned)).flatten(),
7101            delivers,
7102            delivered_by,
7103            labels: item_labels.clone(),
7104            parent,
7105            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
7106            projected: self.held_projections(nodes)?,
7107            number: match content_kind {
7108                ContentKind::Issue => Some(issue_number(content)?),
7109                // A draft is filed in no repository, so nothing ever numbered it:
7110                // `DraftIssue` declares no `number` at all, exactly as it declares no
7111                // `subIssuesSummary` the branch above reads.
7112                ContentKind::DraftIssue => None,
7113            },
7114            url: optional_str(content, "url")?.map(str::to_owned),
7115            created_at: optional_time(content, "createdAt")?,
7116            updated_at: optional_time(content, "updatedAt")?,
7117            item_updated_at: optional_time(item, "updatedAt")?,
7118            comment_count: content
7119                .pointer("/commentCount/totalCount")
7120                .and_then(Value::as_u64),
7121            relationship_events: content
7122                .pointer("/relationshipEvents/totalCount")
7123                .and_then(Value::as_u64),
7124            // The issue node is stamped wherever it was read; a board item a read of the issue
7125            // put together from its memberships is not.
7126            served_at: match optional_time(content, SERVED_AT)? {
7127                Some(served) => Some(served),
7128                None => optional_time(item, SERVED_AT)?,
7129            },
7130            // Both connections were read to their end above — one reporting a further page is
7131            // refused — so these are every field value and every label the item holds. A
7132            // response that did not select the labels at all says nothing of them, and gives
7133            // no facets.
7134            facets: content.get("labels").is_some().then(|| {
7135                change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
7136            }),
7137            own_repository,
7138            repositories,
7139            classification,
7140            slot,
7141            board_id: board_id.map(str::to_owned),
7142            fields: field_definitions(nodes),
7143            board_fields: Self::carried_board_fields(content, board_id)?,
7144            blocked_by: carried_blocked_by(content)?,
7145        };
7146        Ok(Some(resolved))
7147    }
7148
7149    /// The field definitions of the board `board_id` names — the project this issue's own
7150    /// board item is on — off the `boards` page a read of an issue by its own id carries, or
7151    /// `None` when the read carried none, carried no entry for that board, or the board item
7152    /// named no board, which a write then answers by reading the board's fields itself.
7153    ///
7154    /// Matched by the board's node id and never by its number alone: a project number is
7155    /// unique only within its owner, so another owner's board numbered alike can sit on the
7156    /// same page, and its field and option ids address nothing on this one.
7157    fn carried_board_fields(
7158        content: &Value,
7159        board_id: Option<&str>,
7160    ) -> Result<Option<Value>, SourceError> {
7161        let (Some(nodes), Some(board_id)) = (
7162            content.pointer("/boards/nodes").and_then(Value::as_array),
7163            board_id,
7164        ) else {
7165            return Ok(None);
7166        };
7167        let Some(board) = nodes.iter().find_map(|node| {
7168            let project = node.get("project")?;
7169            (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
7170        }) else {
7171            return Ok(None);
7172        };
7173        let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
7174            return Ok(None);
7175        };
7176        complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
7177        Ok(Some(fields.clone()))
7178    }
7179
7180    /// What one board item's `Priority` field says, through this instance's mapping.
7181    ///
7182    /// An instance with no mapping holds no priority, so every item reads as `none` whatever
7183    /// its board holds. With one, no value is `none`, a mapped option is its level, and an
7184    /// option the mapping does not name is kept as itself — never read as a level or as
7185    /// `none` — for a read of the task to report by name.
7186    fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
7187        let Some(mapping) = &self.priorities else {
7188            return Ok(HeldPriority::Read(Priority::None));
7189        };
7190        // A value of the field that names no option — a text field someone called `Priority` —
7191        // is malformed rather than `none`: reading it as no priority would let the next copy
7192        // clear one a person set.
7193        let Some(option) = field_values
7194            .iter()
7195            .find(|value| {
7196                value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
7197            })
7198            .map(|value| required_str(value, "name"))
7199            .transpose()?
7200        else {
7201            return Ok(HeldPriority::Read(Priority::None));
7202        };
7203        Ok(mapping.priority_of(option).map_or_else(
7204            || HeldPriority::Unmapped(option.to_owned()),
7205            HeldPriority::Read,
7206        ))
7207    }
7208
7209    /// What each projected metadata field holds for one board item, by field name — every text
7210    /// value GitHub answers, an empty one included.
7211    fn held_projections(
7212        &self,
7213        field_values: &[Value],
7214    ) -> Result<BTreeMap<String, String>, SourceError> {
7215        let mut held = BTreeMap::new();
7216        for projection in &self.metadata_fields {
7217            if let Some(text) = text_field(field_values, &projection.field)? {
7218                held.insert(projection.field.to_string(), text);
7219            }
7220        }
7221        Ok(held)
7222    }
7223
7224    /// What one board item's status is read from: its `Status` option, whether its issue
7225    /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
7226    /// three into the status it reports.
7227    fn status_parts<'a>(
7228        field_values: &'a [Value],
7229        content: &'a Value,
7230    ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
7231        let option = field_values
7232            .iter()
7233            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
7234            .map(|value| required_str(value, "name"))
7235            .transpose()?;
7236        let closed = optional_str(content, "state")? == Some("CLOSED");
7237        Ok((option, closed, optional_str(content, "stateReason")?))
7238    }
7239
7240    /// The board Status option this write selects, or the refusal that says why not.
7241    ///
7242    /// The mapped option is required for both open and terminal targets. A terminal write
7243    /// validates it before changing either representation, so it can never fall back to
7244    /// closing an issue whose board cannot display the matching status.
7245    ///
7246    /// Answers the field's id, the option's id, and the option's name as the board spells
7247    /// it — which is the name a read of the item reports once it sits there.
7248    fn column_for(
7249        &self,
7250        fields: &Value,
7251        kind: ItemKind,
7252        category: StatusCategory,
7253        target: &StatusTarget,
7254    ) -> Result<Option<(String, String, String)>, SourceError> {
7255        let Some(wanted) = target.option() else {
7256            return Ok(None);
7257        };
7258        let missing = |detail: &str| SourceError::Refused {
7259            message: format!(
7260                "{} status {} of source {} needs the board Status option {wanted:?}, and \
7261                 {detail}; next: add that option to the board, which `onetaskgraph sources \
7262                 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
7263                 it has",
7264                kind.marker(),
7265                category_name(category),
7266                self.name,
7267                self.name,
7268                category_name(category),
7269                kind.marker()
7270            ),
7271        };
7272        let Some(field) = Board::field(fields, "Status")? else {
7273            return Err(missing("this board has no Status field"));
7274        };
7275        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7276            return Err(missing(
7277                "this board's Status field is not a single-select field",
7278            ));
7279        }
7280        let option = field
7281            .get("options")
7282            .and_then(Value::as_array)
7283            .and_then(|options| {
7284                options.iter().find(|option| {
7285                    option
7286                        .get("name")
7287                        .and_then(Value::as_str)
7288                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7289                })
7290            });
7291        match option {
7292            None => Err(missing("this board does not have it")),
7293            Some(option) => Ok(Some((
7294                required_str(field, "id")?.to_owned(),
7295                required_str(option, "id")?.to_owned(),
7296                required_str(option, "name")?.to_owned(),
7297            ))),
7298        }
7299    }
7300
7301    /// The refusal a status that closes an issue is answered with over a board draft.
7302    fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
7303        SourceError::Refused {
7304            message: format!(
7305                "status {} of source {} closes the item's issue, and GitHub draft items have \
7306                 no open or closed state",
7307                category_name(category),
7308                self.name
7309            ),
7310        }
7311    }
7312
7313    /// What a status write to one item needs of the board: the board's id and the
7314    /// definition of its `Status` field, read off the item when the item says both.
7315    ///
7316    /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
7317    /// and its `Status` value carries that field's definition, options and all. An item that
7318    /// does not say — no board id, or no `Status` value to read the field off — takes them
7319    /// from [`Self::board_fields`], which reads no item.
7320    async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7321        if let Some(board) = item.carried_board() {
7322            return Ok(board);
7323        }
7324        if item.defines("Status")
7325            && let Some(board_id) = item.named_board()
7326        {
7327            return Ok(BoardFields {
7328                id: board_id,
7329                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7330            });
7331        }
7332        self.board_fields().await
7333    }
7334
7335    /// Set one task's or one project's status and nothing else; see
7336    /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
7337    async fn set_status(
7338        &self,
7339        id: &NativeId,
7340        kind: ItemKind,
7341        category: StatusCategory,
7342    ) -> Result<Option<Status>, SourceError> {
7343        // Refused before anything is read, in the words a write of the same status is.
7344        let target = self.resolved_target(kind, category)?;
7345        // An item read by issue reference carries only what this write needs, so what it
7346        // becomes is never remembered for a later read to be answered from.
7347        let held = self.take_held(id)?;
7348        let by_reference = held.is_some();
7349        let bound = match held {
7350            Some(item) => Some(item),
7351            None => self.bound_item(id).await?,
7352        };
7353        let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
7354            return Ok(None);
7355        };
7356        let board = self.status_board(&item).await?;
7357        let (field, option, name) = self
7358            .column_for(&board.fields, kind, category, &target)?
7359            .ok_or_else(|| SourceError::Malformed {
7360                message: format!(
7361                    "status {} of source {} names no board Status option",
7362                    category_name(category),
7363                    self.name
7364                ),
7365            })?;
7366        if item.status.category == category && item.option.as_deref() == Some(&name) {
7367            return Ok(Some(item.status));
7368        }
7369        match &target {
7370            StatusTarget::Terminal(_, reason) => {
7371                if item.content_kind == ContentKind::DraftIssue {
7372                    return Err(self.closes_a_draft(category));
7373                }
7374                // The option first, so a refused selection leaves an open issue open rather
7375                // than closed behind a card that still reads open.
7376                self.set_item_field(
7377                    board.id.as_str(),
7378                    &item.item_id,
7379                    &field,
7380                    json!({"singleSelectOptionId": option}),
7381                )
7382                .await?;
7383                // An issue already closed in a way that reads as this category keeps its state
7384                // and its reason: only its card was out of line, and closing it again would
7385                // overwrite a reason — a duplicate's — that says more than this write does.
7386                let already = item.closed && item.status.category == category;
7387                if !already {
7388                    self.update_content(
7389                        ContentKind::Issue,
7390                        &item.id,
7391                        json!({"stateInput": state_input(Some(&target))}),
7392                    )
7393                    .await?;
7394                }
7395                let reason = if already {
7396                    item.reason.clone()
7397                } else {
7398                    Some(reason.reason().to_owned())
7399                };
7400                item.closed = true;
7401                item.status = self
7402                    .statuses
7403                    .status(kind, Some(&name), true, reason.as_deref());
7404                item.option = Some(name);
7405                item.reason = reason;
7406            }
7407            StatusTarget::Column(_) => {
7408                // An option is what an open item's status is, so a closed issue is reopened
7409                // first — sitting closed in the column, it would read back as closed. A draft has
7410                // no state to reopen.
7411                if item.content_kind == ContentKind::Issue && item.closed {
7412                    self.update_content(
7413                        ContentKind::Issue,
7414                        &item.id,
7415                        json!({"stateInput": state_input(Some(&target))}),
7416                    )
7417                    .await?;
7418                    item.closed = false;
7419                }
7420                self.set_item_field(
7421                    board.id.as_str(),
7422                    &item.item_id,
7423                    &field,
7424                    json!({"singleSelectOptionId": option}),
7425                )
7426                .await?;
7427                item.status = self.statuses.status(kind, Some(&name), false, None);
7428                item.option = Some(name);
7429            }
7430            StatusTarget::Disabled(_) => {
7431                unreachable!("resolved_target refused a disabled status")
7432            }
7433        }
7434        let status = item.status.clone();
7435        if !by_reference {
7436            self.remember_written(item, false)?;
7437        }
7438        Ok(Some(status))
7439    }
7440
7441    /// Replace one task's `delivered_by` and nothing else; see
7442    /// [`TaskSource::set_delivered_by`].
7443    ///
7444    /// One update of the body, which differs from the body GitHub holds only inside the
7445    /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
7446    async fn replace_delivered_by(
7447        &self,
7448        id: &NativeId,
7449        delivered_by: &[TaskRef],
7450    ) -> Result<Option<()>, SourceError> {
7451        let entries = TaskRef::listed(
7452            TaskRef::DELIVERED_BY_KEY,
7453            id,
7454            Some(&self.name),
7455            delivered_by.to_vec(),
7456        )
7457        .map_err(|message| SourceError::Refused { message })?;
7458        let Some(mut item) = self
7459            .bound_item(id)
7460            .await?
7461            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7462        else {
7463            return Ok(None);
7464        };
7465        let mut slot = item.slot.clone();
7466        set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
7467        self.write_slot(&mut item, &slot).await?;
7468        item.delivered_by = entries;
7469        self.remember_written(item, false)?;
7470        Ok(Some(()))
7471    }
7472
7473    /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
7474    /// see [`TaskSource::set_task_metadata`].
7475    ///
7476    /// `None` when this board holds no item by that id, or holds one of another kind. The
7477    /// answer is the item as this source now reads it, so what a caller is told the key
7478    /// holds is what the slot holds.
7479    ///
7480    /// A key already holding the value is answered without a write, compared as JSON rather
7481    /// than as the body's bytes: a slot a person spelled with other whitespace would
7482    /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
7483    async fn set_slot_key(
7484        &self,
7485        id: &NativeId,
7486        kind: BoardKind,
7487        key: &MetadataKey,
7488        value: &Value,
7489    ) -> Result<Option<Resolved>, SourceError> {
7490        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7491            return Ok(None);
7492        };
7493        if item.slot.get(key.as_str()) == Some(value) {
7494            return Ok(Some(item));
7495        }
7496        let mut slot = item.slot.clone();
7497        slot.insert(key.as_str().to_owned(), value.clone());
7498        // A key a board text field projects moves that field first, so the body — the value's
7499        // one authoritative home — is written last, as every write of an existing item is.
7500        if self
7501            .metadata_fields
7502            .iter()
7503            .any(|projection| &*projection.key == key.as_str())
7504        {
7505            let board = self.projection_board(&item).await?;
7506            let (writes, projected) = self.projection_writes(
7507                &board.fields,
7508                Some(&item.projected),
7509                &slot,
7510                Some(&[key.as_str()]),
7511            )?;
7512            let (mut values, mut clears) = (Vec::new(), Vec::new());
7513            for write in &writes {
7514                write.join(&mut values, &mut clears);
7515            }
7516            self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
7517                .await?;
7518            item.projected = projected;
7519        }
7520        self.write_slot(&mut item, &slot).await?;
7521        self.remember_written(item.clone(), false)?;
7522        Ok(Some(item))
7523    }
7524
7525    /// What a projection write to `item` needs of the board, read off the item when it says
7526    /// enough, as [`Self::fields_for`] reads it.
7527    async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7528        if let Some(board) = item.carried_board() {
7529            return Ok(board);
7530        }
7531        if let Some(board_id) = item.named_board()
7532            && self
7533                .metadata_fields
7534                .iter()
7535                .all(|projection| item.defines(&projection.field))
7536        {
7537            return Ok(BoardFields {
7538                id: board_id,
7539                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7540            });
7541        }
7542        self.board_fields().await
7543    }
7544
7545    /// Put `slot` in one item's metadata slot with a single update of its body, and bring
7546    /// `item` up to what that write left.
7547    ///
7548    /// The body sent differs from the body GitHub holds only inside the slot — see
7549    /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
7550    /// the mutation the item's content takes, so a board draft's body is written with
7551    /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
7552    async fn write_slot(
7553        &self,
7554        item: &mut Resolved,
7555        slot: &BTreeMap<String, Value>,
7556    ) -> Result<(), SourceError> {
7557        let held = item.raw_body.clone().unwrap_or_default();
7558        let body = with_slot(&held, slot)?;
7559        if body != held {
7560            self.update_content(item.content_kind, &item.id, json!({"body": body}))
7561                .await?;
7562        }
7563        let (visible, slot) = metadata_body(Some(body.clone()))?;
7564        item.body = visible.filter(|value| !value.is_empty());
7565        item.raw_body = Some(body);
7566        item.slot = slot;
7567        Ok(())
7568    }
7569
7570    /// This instance's target for a category written to an item of `kind`, refusing one
7571    /// that kind has no option for — before anything is read or written.
7572    ///
7573    /// Nothing here mutates the board's option set to make room for a status. GitHub
7574    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
7575    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
7576    /// field and every item's status.
7577    fn resolved_target(
7578        &self,
7579        kind: ItemKind,
7580        category: StatusCategory,
7581    ) -> Result<StatusTarget, SourceError> {
7582        let target = self.statuses.target(kind, category).clone();
7583        let StatusTarget::Disabled(why) = target else {
7584            return Ok(target);
7585        };
7586        let refusal = why.refusal(&self.name, category, kind);
7587        // Why there is no shipped default, which is the question a person meeting this
7588        // refusal on a source that never mentioned the category asks.
7589        let shipped_none = match category {
7590            StatusCategory::Draft => Some(
7591                "draft has no shipped default because GitHub draft issues cannot have \
7592                 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
7593            ),
7594            StatusCategory::Unknown => Some(
7595                "unknown has no shipped default because this board keeps no open-ended status \
7596                 word: every word classified unknown is written to the one board Status option \
7597                 status_mapping.unknown names",
7598            ),
7599            _ => None,
7600        };
7601        Err(match (refusal, shipped_none, why) {
7602            (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
7603                SourceError::Refused {
7604                    message: format!("{message}; {note}"),
7605                }
7606            }
7607            (refusal, _, _) => refusal,
7608        })
7609    }
7610
7611    /// What writing `priority` does to one item's `Priority` field on this board, or the
7612    /// refusal naming what the board lacks.
7613    ///
7614    /// `none` is no value, so it clears the field — and asks nothing of an item that holds
7615    /// none already, or of an item not created yet. Every other priority selects the option
7616    /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
7617    /// without that option, is refused rather than given one: reads and writes never create
7618    /// a field or an option.
7619    fn priority_write(
7620        &self,
7621        fields: &Value,
7622        existing: Option<&Resolved>,
7623        priority: Priority,
7624    ) -> Result<Option<PriorityWrite>, SourceError> {
7625        let Some(mapping) = &self.priorities else {
7626            return Err(self.holds_no_priority());
7627        };
7628        let Some(wanted) = mapping.option(priority) else {
7629            if !existing.is_some_and(Resolved::holds_priority) {
7630                return Ok(None);
7631            }
7632            let field =
7633                Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
7634                    message: format!(
7635                        "an item holding a {PRIORITY_FIELD} value was read without that field"
7636                    ),
7637                })?;
7638            return Ok(Some(PriorityWrite::Clear {
7639                field: required_str(field, "id")?.to_owned(),
7640            }));
7641        };
7642        let missing = |detail: &str| SourceError::Refused {
7643            message: format!(
7644                "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
7645                 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
7646                 it, or point priority_mapping.{priority} of this source at an option the board \
7647                 has",
7648                self.name, self.name
7649            ),
7650        };
7651        let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
7652            return Err(missing(&format!(
7653                "this board has no {PRIORITY_FIELD} field"
7654            )));
7655        };
7656        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7657            return Err(missing(&format!(
7658                "this board's {PRIORITY_FIELD} field is not a single-select field"
7659            )));
7660        }
7661        // An options list that is absent or not a list is an answer this source cannot read,
7662        // not a board lacking the option: `sources fields --apply` is no remedy for it.
7663        let option = field
7664            .get("options")
7665            .and_then(Value::as_array)
7666            .ok_or_else(|| SourceError::Malformed {
7667                message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
7668            })?
7669            .iter()
7670            .find(|option| {
7671                option
7672                    .get("name")
7673                    .and_then(Value::as_str)
7674                    .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7675            })
7676            .ok_or_else(|| missing("this board does not have it"))?;
7677        Ok(Some(PriorityWrite::Select {
7678            field: required_str(field, "id")?.to_owned(),
7679            option: required_str(option, "id")?.to_owned(),
7680        }))
7681    }
7682
7683    /// What writing `metadata` does to each projected text field of one item, and what those
7684    /// fields hold after it — or the refusal, before anything is sent.
7685    ///
7686    /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
7687    /// narrows the work to the entries projecting one of those metadata keys, for a write that
7688    /// touches only them; `None` is every entry. A string the field already holds, and nothing
7689    /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
7690    /// and so is a board without the field, or with a field of that name that is not a text
7691    /// field: reads and writes never create a field.
7692    fn projection_writes(
7693        &self,
7694        fields: &Value,
7695        held: Option<&BTreeMap<String, String>>,
7696        metadata: &BTreeMap<String, Value>,
7697        keys: Option<&[&str]>,
7698    ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
7699        let mut projected = held.cloned().unwrap_or_default();
7700        let mut writes = Vec::new();
7701        for projection in self
7702            .metadata_fields
7703            .iter()
7704            .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
7705        {
7706            let wanted = projection.projected(metadata, &self.name)?;
7707            let field = self.projection_field(fields, projection)?;
7708            let holding = projected.get(&*projection.field);
7709            match wanted {
7710                Projected::Text(text) if holding != Some(&text) => {
7711                    writes.push(ProjectionWrite::Set {
7712                        field,
7713                        text: text.clone(),
7714                    });
7715                    projected.insert(projection.field.to_string(), text);
7716                }
7717                Projected::Nothing if holding.is_some() => {
7718                    writes.push(ProjectionWrite::Clear { field });
7719                    projected.remove(&*projection.field);
7720                }
7721                Projected::Text(_) | Projected::Nothing => {}
7722            }
7723        }
7724        Ok((writes, projected))
7725    }
7726
7727    /// The id of the board text field one projection writes, or the refusal naming what the
7728    /// board has instead.
7729    fn projection_field(
7730        &self,
7731        fields: &Value,
7732        projection: &MetadataField,
7733    ) -> Result<String, SourceError> {
7734        let refuse = |detail: String| SourceError::Refused {
7735            message: format!(
7736                "source {} projects metadata key {:?} onto the board text field {:?}, and \
7737                 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
7738                 text field, or name another field in metadata_fields",
7739                self.name, &*projection.key, &*projection.field, self.name
7740            ),
7741        };
7742        let Some(field) = Board::field(fields, &projection.field)? else {
7743            return Err(refuse("this board has no field of that name".to_owned()));
7744        };
7745        let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
7746        let data_type = optional_str(field, "dataType")?;
7747        if typename != "ProjectV2Field" || data_type != Some("TEXT") {
7748            return Err(refuse(format!(
7749                "this board's field of that name is not a text field (it is a {}), so it is \
7750                 left as it is; rename or remove that field first",
7751                data_type.unwrap_or(typename)
7752            )));
7753        }
7754        Ok(required_str(field, "id")?.to_owned())
7755    }
7756
7757    /// Apply one priority write to one board item.
7758    async fn write_priority(
7759        &self,
7760        board_id: &str,
7761        item_id: &str,
7762        write: &PriorityWrite,
7763    ) -> Result<(), SourceError> {
7764        match write {
7765            PriorityWrite::Select { field, option } => {
7766                self.set_item_field(
7767                    board_id,
7768                    item_id,
7769                    field,
7770                    json!({"singleSelectOptionId": option}),
7771                )
7772                .await
7773            }
7774            PriorityWrite::Clear { field } => {
7775                let data = self
7776                    .graphql(
7777                        graphql::CLEAR_FIELD,
7778                        json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
7779                            "readPriority":false,"priorityName":PRIORITY_FIELD}),
7780                    )
7781                    .await?;
7782                let returned = data
7783                    .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
7784                    .ok_or_else(|| SourceError::Malformed {
7785                        message: "GitHub field clear returned no project item".into(),
7786                    })?;
7787                if required_str(returned, "id")? != item_id {
7788                    return Err(SourceError::Malformed {
7789                        message: "GitHub field clear returned the wrong project item".into(),
7790                    });
7791                }
7792                Ok(())
7793            }
7794        }
7795    }
7796
7797    /// The refusal a priority is answered with by an instance configured with no
7798    /// `priority_mapping`, which holds none.
7799    fn holds_no_priority(&self) -> SourceError {
7800        SourceError::Refused {
7801            message: format!(
7802                "source {} holds no task priority: its configuration sets no priority_mapping; \
7803                 next: set priority_mapping on this source, then run `onetaskgraph sources \
7804                 fields {} --apply` to set its board up",
7805                self.name, self.name
7806            ),
7807        }
7808    }
7809
7810    /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
7811    ///
7812    /// One field write — a select, or a clear for `none` — and no title, body, label, state
7813    /// or `Status` request. Clearing a priority an item does not hold sends nothing.
7814    async fn set_priority(
7815        &self,
7816        id: &NativeId,
7817        priority: Priority,
7818    ) -> Result<Option<Priority>, SourceError> {
7819        if self.priorities.is_none() {
7820            return Err(self.holds_no_priority());
7821        }
7822        let Some(mut item) = self
7823            .bound_item(id)
7824            .await?
7825            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7826        else {
7827            return Ok(None);
7828        };
7829        if priority == Priority::None && !item.holds_priority() {
7830            return Ok(Some(priority));
7831        }
7832        // The item's own read carries the field's definition whenever it holds a value of
7833        // it, which a clear always does; a select onto an item holding none reads the board.
7834        let board = match (item.carried_board(), item.named_board()) {
7835            (Some(board), _) => board,
7836            (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
7837                id,
7838                fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
7839            },
7840            _ => self.board_fields().await?,
7841        };
7842        let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
7843            return Ok(Some(priority));
7844        };
7845        let (document, root, input) = match write {
7846            PriorityWrite::Select { field, option } => (
7847                graphql::UPDATE_FIELD,
7848                "updateProjectV2ItemFieldValue",
7849                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
7850            ),
7851            PriorityWrite::Clear { field } => (
7852                graphql::CLEAR_FIELD,
7853                "clearProjectV2ItemFieldValue",
7854                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
7855            ),
7856        };
7857        let data = self
7858            .graphql(
7859                document,
7860                json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
7861            )
7862            .await?;
7863        let returned = data
7864            .get(root)
7865            .and_then(|value| value.get("projectV2Item"))
7866            .ok_or_else(|| SourceError::Malformed {
7867                message: "GitHub priority write returned no project item".into(),
7868            })?;
7869        if required_str(returned, "id")? != item.item_id {
7870            return Err(SourceError::Malformed {
7871                message: "GitHub priority write returned the wrong project item".into(),
7872            });
7873        }
7874        let value = returned
7875            .get("fieldValueByName")
7876            .ok_or_else(|| SourceError::Malformed {
7877                message: "GitHub priority write returned no priority read-back".into(),
7878            })?;
7879        if !value.is_null()
7880            && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
7881        {
7882            return Err(SourceError::Malformed {
7883                message: "GitHub priority read-back is not a Priority field value".into(),
7884            });
7885        }
7886        let values = if value.is_null() {
7887            Vec::new()
7888        } else {
7889            vec![value.clone()]
7890        };
7891        item.priority = self.held_priority(&values)?;
7892        let answer = item.task()?.priority;
7893        self.remember_written(item, false)?;
7894        Ok(Some(answer))
7895    }
7896
7897    /// Replace one task's visible body and nothing else; see
7898    /// [`TaskSource::set_task_content`].
7899    ///
7900    /// One update of the body, which differs from the body GitHub holds only outside the
7901    /// metadata slot — the slot is kept byte for byte, so every caller key and every list
7902    /// this source keeps there reads back as it was. A body that would not change is not
7903    /// sent at all.
7904    async fn replace_content(
7905        &self,
7906        id: &NativeId,
7907        content: &str,
7908    ) -> Result<Option<()>, SourceError> {
7909        let Some(mut item) = self
7910            .bound_item(id)
7911            .await?
7912            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7913        else {
7914            return Ok(None);
7915        };
7916        let held = item.raw_body.clone().unwrap_or_default();
7917        let body = with_content(&held, content)?;
7918        // Checked before anything is sent: content ending in what this source reads as its own
7919        // metadata slot would read back as metadata rather than as the content it was.
7920        let (visible, slot) = metadata_body(Some(body.clone()))?;
7921        if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
7922            return Err(SourceError::Refused {
7923                message: format!(
7924                    "this content ends in what source {} reads as its own metadata slot \
7925                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7926                     as content; next: remove that trailing block from the content",
7927                    self.name
7928                ),
7929            });
7930        }
7931        if body != held {
7932            self.update_content(item.content_kind, &item.id, json!({"body": body}))
7933                .await?;
7934        }
7935        item.body = visible.filter(|value| !value.is_empty());
7936        item.raw_body = Some(body);
7937        item.slot = slot;
7938        self.remember_written(item, false)?;
7939        Ok(Some(()))
7940    }
7941
7942    /// Apply one targeted update to one task; see [`TaskSource::update_task`].
7943    ///
7944    /// One read of the item — which carries the board's field definitions and the issue's
7945    /// `blockedBy`, so neither is read again — and then only what differs from it: the
7946    /// `Status` option and the `Priority` field together in one request, the `blockedBy`
7947    /// additions and removals the named edges differ by, and last one `updateIssue` carrying
7948    /// the title, the body — visible content and metadata slot together — and a state change.
7949    /// So an update naming any of title, body, metadata, status and priority is one read and
7950    /// at most two writes. The body goes last so that a write refused part-way leaves it, and
7951    /// the metadata in it, as it stood. A terminal status selects its option and then closes,
7952    /// as a whole write does; an open one selects its option and then reopens. The origin
7953    /// field is never written: an update is of an item that already exists, whose origin is
7954    /// what it is.
7955    ///
7956    /// The task answered is the item as those writes left it, built from the read and what was
7957    /// sent rather than read again — the same record a later read in this run answers from.
7958    async fn targeted_update(
7959        &self,
7960        id: &NativeId,
7961        update: &TaskUpdate,
7962    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
7963        // Everything this source can refuse without reading the item is refused first, in the
7964        // words a whole write of the same fields is refused with.
7965        update.consistent()?;
7966        if update
7967            .title
7968            .as_deref()
7969            .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
7970        {
7971            return Err(SourceError::Refused {
7972                message: format!(
7973                    "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
7974                     spells a document, so it would read back as one rather than as a task; \
7975                     retitle it",
7976                    self.name
7977                ),
7978            });
7979        }
7980        if let Some(delivers) = &update.delivers {
7981            TaskRef::listed(
7982                TaskRef::DELIVERS_KEY,
7983                id,
7984                Some(&self.name),
7985                delivers.clone(),
7986            )
7987            .map_err(|message| SourceError::Refused { message })?;
7988        }
7989        if self.priorities.is_none()
7990            && update
7991                .priority
7992                .is_some_and(|priority| priority != Priority::None)
7993        {
7994            return Err(self.holds_no_priority());
7995        }
7996        let target = update
7997            .status
7998            .as_ref()
7999            .map(|status| self.resolved_target(ItemKind::Task, status.category))
8000            .transpose()?;
8001        let Some(mut item) = self
8002            .bound_item(id)
8003            .await?
8004            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
8005        else {
8006            return Ok(None);
8007        };
8008        let before = item.task()?;
8009
8010        let mut status_move = None;
8011        if let (Some(status), Some(target)) = (&update.status, target) {
8012            let board = self.status_board(&item).await?;
8013            let (field, option, name) = self
8014                .column_for(&board.fields, ItemKind::Task, status.category, &target)?
8015                .ok_or_else(|| SourceError::Malformed {
8016                    message: format!(
8017                        "status {} of source {} names no board Status option",
8018                        category_name(status.category),
8019                        self.name
8020                    ),
8021                })?;
8022            let terminal = matches!(target, StatusTarget::Terminal(_, _));
8023            if terminal && item.content_kind == ContentKind::DraftIssue {
8024                return Err(self.closes_a_draft(status.category));
8025            }
8026            let landed = match &target {
8027                StatusTarget::Terminal(_, reason) => {
8028                    self.statuses
8029                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
8030                }
8031                _ => self
8032                    .statuses
8033                    .status(ItemKind::Task, Some(&name), false, None),
8034            };
8035            let option_moves = item
8036                .option
8037                .as_deref()
8038                .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
8039            let state_moves = item.content_kind == ContentKind::Issue
8040                && (item.closed != terminal || (terminal && item.status != landed));
8041            if let Some(moves) = Moves::of(option_moves, state_moves) {
8042                status_move = Some(StatusMove {
8043                    board: board.id,
8044                    field,
8045                    option,
8046                    name,
8047                    target,
8048                    landed,
8049                    moves,
8050                });
8051            }
8052        }
8053
8054        let mut priority_move = None;
8055        if let Some(priority) = update.priority
8056            && self.priorities.is_some()
8057            && item.priority != HeldPriority::Read(priority)
8058        {
8059            let board = match (item.carried_board(), item.named_board()) {
8060                (Some(board), _) => board,
8061                (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
8062                    id: board,
8063                    fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8064                },
8065                _ => self.board_fields().await?,
8066            };
8067            if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
8068                priority_move = Some((board.id, write, priority));
8069            }
8070        }
8071
8072        // The metadata keys this update names that a board text field projects, and what
8073        // that does to those fields — refused, as a whole write refuses it, before anything
8074        // is sent.
8075        let touched: Vec<&str> = update
8076            .metadata_set
8077            .keys()
8078            .chain(update.metadata_remove.iter())
8079            .map(MetadataKey::as_str)
8080            .collect();
8081        let mut projection_move = None;
8082        if self
8083            .metadata_fields
8084            .iter()
8085            .any(|projection| touched.contains(&&*projection.key))
8086        {
8087            let mut metadata = item.slot.clone();
8088            for (key, value) in &update.metadata_set {
8089                metadata.insert(key.as_str().to_owned(), value.clone());
8090            }
8091            for key in &update.metadata_remove {
8092                metadata.remove(key.as_str());
8093            }
8094            let board = self.projection_board(&item).await?;
8095            let (writes, projected) = self.projection_writes(
8096                &board.fields,
8097                Some(&item.projected),
8098                &metadata,
8099                Some(&touched),
8100            )?;
8101            projection_move = Some((board.id, writes, projected));
8102        }
8103
8104        // Resolved before the body is composed, because a far end `blockedBy` cannot name is
8105        // recorded in the slot, and the slot travels in the one body update below.
8106        let edges = match &update.depends_on {
8107            Some(edges) => Some(
8108                self.partition_edges(
8109                    BoardKind::Work(ItemKind::Task),
8110                    item.content_kind,
8111                    item.blocked_by.as_deref(),
8112                    edges,
8113                )
8114                .await?,
8115            ),
8116            None => None,
8117        };
8118
8119        let mut slot = item.slot.clone();
8120        for (key, value) in &update.metadata_set {
8121            slot.insert(key.as_str().to_owned(), value.clone());
8122        }
8123        for key in &update.metadata_remove {
8124            slot.remove(key.as_str());
8125        }
8126        if let Some(delivers) = &update.delivers {
8127            set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
8128        }
8129        if let Some((_, recorded)) = &edges {
8130            record_edges(&mut slot, recorded);
8131        }
8132        let held = item.raw_body.clone().unwrap_or_default();
8133        let content = match &update.content {
8134            Some(content) => with_content(&held, content)?,
8135            None => held.clone(),
8136        };
8137        // A slot holding what it held is kept byte for byte, compared as JSON rather than as
8138        // the body's bytes, as a metadata write compares it: a slot a person spelled with
8139        // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
8140        let body = if slot == item.slot {
8141            content
8142        } else {
8143            with_slot(&content, &slot)?
8144        };
8145        // Checked before anything is sent, as a content write checks it: content ending in
8146        // what this source reads as its own slot would read back as metadata.
8147        let (visible, read) = metadata_body(Some(body.clone()))?;
8148        let wanted = update.content.as_deref().or(item.body.as_deref());
8149        if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
8150            return Err(SourceError::Refused {
8151                message: format!(
8152                    "this content ends in what source {} reads as its own metadata slot \
8153                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8154                     as content; next: remove that trailing block from the content",
8155                    self.name
8156                ),
8157            });
8158        }
8159        let recorded_moves =
8160            slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
8161
8162        // One `updateIssue` carries all three, because every mutation spends the secondary
8163        // limiter and the title, body and state are one mutation's inputs.
8164        let mut fields = serde_json::Map::new();
8165        if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
8166            fields.insert("title".to_owned(), json!(title));
8167        }
8168        if body != held {
8169            fields.insert("body".to_owned(), json!(body));
8170        }
8171        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
8172            fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
8173        }
8174        // **The body is written last, and that is the guarantee a refusal part-way keeps.**
8175        // GitHub runs no two requests as one, and runs one document's mutation fields in order
8176        // without undoing an earlier field when a later one fails — so a body written before a
8177        // board field the board then refused would be left changed. Written after every other
8178        // write has landed, a refusal anywhere leaves the item's body, and every metadata key
8179        // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
8180        // first, together in one request — a terminal option selected before the issue
8181        // closes, as a whole write does — then the `blockedBy` difference, then the body.
8182        let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
8183        let mut clears: Vec<(&BoardId, &str)> = Vec::new();
8184        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
8185            board_writes.push((
8186                &moving.board,
8187                (
8188                    moving.field.clone(),
8189                    json!({"singleSelectOptionId": moving.option}),
8190                ),
8191            ));
8192        }
8193        match &priority_move {
8194            Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
8195                board,
8196                (field.clone(), json!({"singleSelectOptionId": option})),
8197            )),
8198            Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
8199            None => {}
8200        }
8201        if let Some((board, writes, _)) = &projection_move {
8202            for write in writes {
8203                let (mut values, mut cleared) = (Vec::new(), Vec::new());
8204                write.join(&mut values, &mut cleared);
8205                board_writes.extend(values.into_iter().map(|value| (board, value)));
8206                clears.extend(cleared.into_iter().map(|field| (board, field)));
8207            }
8208        }
8209        let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
8210        boards.extend(clears.iter().map(|(board, _)| *board));
8211        boards.dedup_by(|one, other| one.as_str() == other.as_str());
8212        for board in boards {
8213            let writes = board_writes
8214                .iter()
8215                .filter(|(on, _)| on.as_str() == board.as_str())
8216                .map(|(_, write)| write.clone())
8217                .collect::<Vec<_>>();
8218            let cleared = clears
8219                .iter()
8220                .filter(|(on, _)| on.as_str() == board.as_str())
8221                .map(|(_, field)| *field)
8222                .collect::<Vec<_>>();
8223            self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
8224                .await?;
8225        }
8226        let mut blocked_by_moved = false;
8227        if let Some((native, _)) = &edges
8228            && item.content_kind == ContentKind::Issue
8229        {
8230            blocked_by_moved = self
8231                .reconcile_blocked_by(
8232                    &item.id,
8233                    native,
8234                    Issue::Existing(item.blocked_by.as_deref()),
8235                )
8236                .await?;
8237        }
8238        if !fields.is_empty() {
8239            self.update_content(item.content_kind, &item.id, Value::Object(fields))
8240                .await?;
8241        }
8242
8243        // The `blockedBy` this item was read with is what the write just changed, so it is no
8244        // answer any more: a later write in this command would reconcile against it — re-adding
8245        // what it kept and never removing what it dropped — and a forward-edge read would
8246        // answer with it. Left unknown, each asks GitHub instead.
8247        if blocked_by_moved {
8248            item.blocked_by = None;
8249        }
8250        if let Some(title) = &update.title {
8251            item.title.clone_from(title);
8252        }
8253        item.body = visible.filter(|value| !value.is_empty());
8254        item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
8255        item.slot = slot;
8256        if let Some(delivers) = &update.delivers {
8257            item.delivers.clone_from(delivers);
8258        }
8259        if let Some(moving) = status_move {
8260            item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
8261                && item.content_kind == ContentKind::Issue;
8262            item.status = moving.landed;
8263            item.option = Some(moving.name);
8264        }
8265        if let Some((_, _, priority)) = priority_move {
8266            item.priority = HeldPriority::Read(priority);
8267        }
8268        if let Some((_, _, projected)) = projection_move {
8269            item.projected = projected;
8270        }
8271        let task = item.task()?;
8272        let mut written = update.changed(&before, &task);
8273        if blocked_by_moved || recorded_moves {
8274            written.insert(UpdatedField::DependsOn);
8275        }
8276        self.remember_written(item, false)?;
8277        Ok(Some(TaskUpdateOutcome {
8278            task,
8279            written,
8280            delivers_before: before.delivers,
8281        }))
8282    }
8283
8284    /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
8285    /// together, and nothing else; see [`TaskSource::set_task_rendering`].
8286    ///
8287    /// One update of the body: the content outside the slot, and inside it that one entry,
8288    /// every other entry kept as it was. This source keeps no template answers — an issue has
8289    /// no room beside itself that is not its body, and answers written there would duplicate
8290    /// what the content already says and count against GitHub's body limit — so `answers`
8291    /// reaches nothing here. A body that would not change is not sent at all.
8292    async fn replace_rendering(
8293        &self,
8294        id: &NativeId,
8295        kind: BoardKind,
8296        content: &str,
8297        provenance: &Value,
8298        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
8299    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
8300        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8301            return Ok(None);
8302        };
8303        let held = item.raw_body.clone().unwrap_or_default();
8304        let mut slot = item.slot.clone();
8305        slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
8306        let rewritten;
8307        let content = if let Some(assets) = assets {
8308            let uploads = self
8309                .upload_assets(item.own_repository.as_ref(), assets)
8310                .await?;
8311            rewritten =
8312                onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
8313            rewritten.as_str()
8314        } else {
8315            content
8316        };
8317        let body = with_slot(&with_content(&held, content)?, &slot)?;
8318        // Checked before anything is sent, as a content write checks it.
8319        let (visible, read) = metadata_body(Some(body.clone()))?;
8320        if visible.as_deref().unwrap_or_default() != content || read != slot {
8321            return Err(SourceError::Refused {
8322                message: format!(
8323                    "this content ends in what source {} reads as its own metadata slot \
8324                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8325                     as content; next: remove that trailing block from the template",
8326                    self.name
8327                ),
8328            });
8329        }
8330        if body != held {
8331            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8332                .await?;
8333        }
8334        item.body = visible.filter(|value| !value.is_empty());
8335        item.raw_body = Some(body);
8336        item.slot = read;
8337        self.remember_written(item, false)?;
8338        Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
8339            id: id.clone(),
8340            content: Some(content.to_owned()),
8341        }))
8342    }
8343
8344    async fn set_item_field(
8345        &self,
8346        board_id: &str,
8347        item_id: &str,
8348        field_id: &str,
8349        value: Value,
8350    ) -> Result<(), SourceError> {
8351        let data = self
8352            .graphql(
8353                graphql::UPDATE_FIELD,
8354                json!({"input":{
8355                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
8356                },"readPriority":false,"priorityName":PRIORITY_FIELD}),
8357            )
8358            .await?;
8359        let returned = data
8360            .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
8361            .ok_or_else(|| SourceError::Malformed {
8362                message: "GitHub field update returned no project item".into(),
8363            })?;
8364        if required_str(returned, "id")? != item_id {
8365            return Err(SourceError::Malformed {
8366                message: "GitHub field update returned the wrong project item".into(),
8367            });
8368        }
8369        Ok(())
8370    }
8371
8372    /// GitHub accepts one value per field mutation; aliases combine those mutations in
8373    /// one request — writes in the order given, then clears. A lone write is
8374    /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
8375    /// request's slots hold go in further requests of the same document, in order. Every
8376    /// returned item id is checked, including optional aliases.
8377    async fn set_item_fields(
8378        &self,
8379        board: &str,
8380        item: &str,
8381        fields: &[(String, Value)],
8382        clears: &[&str],
8383    ) -> Result<(), SourceError> {
8384        match (fields, clears) {
8385            ([], []) => return Ok(()),
8386            ([(field, value)], []) => {
8387                return self.set_item_field(board, item, field, value.clone()).await;
8388            }
8389            ([], [field]) => {
8390                return self
8391                    .write_priority(
8392                        board,
8393                        item,
8394                        &PriorityWrite::Clear {
8395                            field: (*field).to_owned(),
8396                        },
8397                    )
8398                    .await;
8399            }
8400            _ => {}
8401        }
8402        // Any field of the request stands in for an input a slot leaves out, which GitHub
8403        // still requires to be well formed although it never runs it.
8404        let filler = fields
8405            .first()
8406            .map_or_else(|| clears[0], |(field, _)| field.as_str());
8407        let mut writes = fields.iter();
8408        let mut cleared = clears.iter();
8409        loop {
8410            let batch: Vec<&(String, Value)> =
8411                writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
8412            let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
8413            if batch.is_empty() && batch_clears.is_empty() {
8414                return Ok(());
8415            }
8416            let mut variables = serde_json::Map::new();
8417            for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
8418                let input = match batch.get(index) {
8419                    Some((field, value)) => {
8420                        json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
8421                    }
8422                    None => json!({"projectId":board,"itemId":item,"fieldId":filler,
8423                                   "value":{"text":""}}),
8424                };
8425                variables.insert(slot.variable.to_owned(), input);
8426                variables.insert(slot.include.to_owned(), json!(index < batch.len()));
8427            }
8428            for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
8429                let field = batch_clears.get(index).map_or(filler, |field| **field);
8430                variables.insert(
8431                    slot.variable.to_owned(),
8432                    json!({"projectId":board,"itemId":item,"fieldId":field}),
8433                );
8434                variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
8435            }
8436            let data = self
8437                .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
8438                .await?;
8439            let sent = FIELD_WRITE_SLOTS[..batch.len()]
8440                .iter()
8441                .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
8442            for slot in sent {
8443                let alias = slot.alias;
8444                let returned = data
8445                    .get(alias)
8446                    .and_then(|value| value.get("projectV2Item"))
8447                    .ok_or_else(|| SourceError::Malformed {
8448                        message: format!("GitHub field update {alias} returned no project item"),
8449                    })?;
8450                if required_str(returned, "id")? != item {
8451                    return Err(SourceError::Malformed {
8452                        message: format!(
8453                            "GitHub field update {alias} returned the wrong project item"
8454                        ),
8455                    });
8456                }
8457            }
8458        }
8459    }
8460
8461    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
8462        let mut after: Option<String> = None;
8463        let mut ids = Vec::new();
8464        loop {
8465            let data = self
8466                .graphql(
8467                    graphql::ISSUE_DEPENDENCIES,
8468                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
8469                )
8470                .await?;
8471            let connection =
8472                data.pointer("/node/blockedBy")
8473                    .ok_or_else(|| SourceError::Malformed {
8474                        message: "GitHub dependency response has no blockedBy connection".into(),
8475                    })?;
8476            ids.extend(
8477                connection
8478                    .get("nodes")
8479                    .and_then(Value::as_array)
8480                    .ok_or_else(|| SourceError::Malformed {
8481                        message: "GitHub dependency response nodes is not an array".into(),
8482                    })?
8483                    .iter()
8484                    .map(|value| required_str(value, "id").map(str::to_owned))
8485                    .collect::<Result<Vec<_>, _>>()?,
8486            );
8487            let next = next_cursor(connection)?;
8488            if let Some(next) = &next {
8489                validate_cursor_progress(after.as_deref(), &next.0)?;
8490            }
8491            after = next.map(|cursor| cursor.0);
8492            if after.is_none() {
8493                return Ok(ids);
8494            }
8495        }
8496    }
8497
8498    async fn dependencies(
8499        &self,
8500        id: &NativeId,
8501        near_kind: ItemKind,
8502        direction: Direction,
8503        page: &PageRequest,
8504    ) -> Result<Page<DependencyEdge>, SourceError> {
8505        validate_page(page)?;
8506        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
8507        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
8508        let recorded = recorded_offset(cursor, direction)?;
8509        // What this issue is blocked by, when a read of it by its own id in this command
8510        // already carried the whole connection — a copy reads the item it writes before it
8511        // reads its edges — and the page asked for is the whole of it, or the recorded tail
8512        // after it. Answered from that read, in the shape the dependency read answers in;
8513        // anything else is asked of GitHub.
8514        let carried = match direction {
8515            Direction::DependsOn => self
8516                .resolved_cache()?
8517                .get(id)
8518                .filter(|item| item.content_kind == ContentKind::Issue)
8519                .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
8520            Direction::DependedOnBy => None,
8521        }
8522        .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
8523        // Asked for even in the recorded phase, whose page reads nothing from the
8524        // connection: `__typename` is what says whether this item has a native
8525        // relationship at all, and that is what decides which far ends the reserved key is
8526        // allowed to hold.
8527        let data = match carried {
8528            Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
8529                "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
8530            None => {
8531                self.graphql(
8532                    graphql::ISSUE_DEPENDENCIES,
8533                    json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
8534                           "after":if recorded.is_some() {None} else {cursor}}),
8535                )
8536                .await?
8537            }
8538        };
8539        let node =
8540            data.get("node")
8541                .filter(|v| !v.is_null())
8542                .ok_or_else(|| SourceError::Refused {
8543                    message: format!(
8544                        "GitHub item {} was not found or does not support dependencies",
8545                        id.0
8546                    ),
8547                })?;
8548        let connection_name = match direction {
8549            Direction::DependsOn => "blockedBy",
8550            Direction::DependedOnBy => "blocking",
8551        };
8552        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
8553        // named natively and the reserved key may hold any far end. An issue's connections
8554        // hold issues, and this source reads them at the near item's own level.
8555        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
8556        if let Some(offset) = recorded {
8557            return Ok(recorded_page(
8558                self.recorded_edges(id, near_kind, direction, natively_names, node)
8559                    .await?,
8560                offset,
8561                limit,
8562            ));
8563        }
8564        if natively_names.is_none() {
8565            return Ok(recorded_page(
8566                self.recorded_edges(id, near_kind, direction, natively_names, node)
8567                    .await?,
8568                0,
8569                limit,
8570            ));
8571        }
8572        let connection = node
8573            .get(connection_name)
8574            .ok_or_else(|| SourceError::Malformed {
8575                message: "GitHub dependency response is missing its connection".into(),
8576            })?;
8577        let nodes = connection
8578            .get("nodes")
8579            .and_then(Value::as_array)
8580            .ok_or_else(|| SourceError::Malformed {
8581                message: "GitHub dependency response nodes is not an array".into(),
8582            })?;
8583        // `from` depends on `to`, always. GitHub spells the same relationship from either
8584        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
8585        // it — so the near item is `from` in one direction and `to` in the other.
8586        let items = nodes
8587            .iter()
8588            .map(|value| {
8589                let related = NativeId(required_str(value, "id")?.into());
8590                let related_kind = related_kind(value)?;
8591                let (from, to) = match direction {
8592                    Direction::DependsOn => (
8593                        DependencyEndpoint::from_native(id.clone(), near_kind),
8594                        DependencyEndpoint::from_native(related, related_kind),
8595                    ),
8596                    Direction::DependedOnBy => (
8597                        DependencyEndpoint::from_native(related, related_kind),
8598                        DependencyEndpoint::from_native(id.clone(), near_kind),
8599                    ),
8600                };
8601                Ok(DependencyEdge {
8602                    from,
8603                    to,
8604                    kind: DependencyKind::Blocks,
8605                })
8606            })
8607            .collect::<Result<Vec<_>, SourceError>>()?;
8608        let mut next = next_cursor(connection)?;
8609        if let Some(next) = &next {
8610            validate_cursor_progress(cursor, &next.0)?;
8611        }
8612        if next.is_none()
8613            && !self
8614                .recorded_edges(id, near_kind, direction, natively_names, node)
8615                .await?
8616                .is_empty()
8617        {
8618            next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
8619        }
8620        Ok(Page { items, next })
8621    }
8622
8623    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
8624    /// a far end in another source has to live: no GitHub issue relationship can name one.
8625    ///
8626    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
8627    /// source never writes one down.
8628    ///
8629    /// The metadata lives in the item's own body slot, and `node` is the dependency read's
8630    /// own answer, which carries an issue's body — so an issue's recorded edges cost no
8631    /// request beyond the read already made, and reading the board for them would be a
8632    /// walk of every item for one field of one. A draft has no body in that answer, because
8633    /// a draft is not an issue, so a draft's are read off its own read by id — never off a
8634    /// listing of the board, which can be behind on the very item asked about.
8635    async fn recorded_edges(
8636        &self,
8637        id: &NativeId,
8638        near_kind: ItemKind,
8639        direction: Direction,
8640        natively_names: Option<ItemKind>,
8641        node: &Value,
8642    ) -> Result<Vec<DependencyEdge>, SourceError> {
8643        if direction != Direction::DependsOn {
8644            return Ok(Vec::new());
8645        }
8646        let slot = match node.get("body") {
8647            Some(body) if natively_names.is_some() => {
8648                metadata_body(body.as_str().map(str::to_owned))?.1
8649            }
8650            _ => {
8651                let Some(item) = self.bound_item(id).await? else {
8652                    return Ok(Vec::new());
8653                };
8654                item.slot
8655            }
8656        };
8657        DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
8658            .map_err(|message| SourceError::Malformed { message })
8659    }
8660
8661    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
8662        self.repository
8663            .as_ref()
8664            .ok_or_else(|| SourceError::Refused {
8665                message: format!(
8666                    "source {} has no repository configured, and a GitHub Projects board has no \
8667                 repository of its own to create an issue in; set repository: owner/name on \
8668                 this source",
8669                    self.name
8670                ),
8671            })
8672    }
8673
8674    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
8675    /// states.
8676    ///
8677    /// The fallback is demanded first, whichever arm answers: a write without a configured
8678    /// repository is refused naming the field exactly as it was before the rule existed,
8679    /// so a source that could not write before cannot write now, rather than writing for
8680    /// the one item whose own field happens to decide it.
8681    ///
8682    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
8683    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
8684    /// entry owned by someone other than the owner of the parent issue's repository —
8685    /// GitHub accepts a sub-issue from another repository of the same owner and from no
8686    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
8687    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
8688    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
8689    /// and is visible to the token is checked where its node id is resolved, still before
8690    /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
8691    /// looked up in a listing of the board, which can be minutes behind an issue its own
8692    /// `projectItems` already places on it — and that read answers first from this process's
8693    /// own record, so a project created moments ago in this command answers though GitHub
8694    /// has not caught up.
8695    async fn creation_target(
8696        &self,
8697        incoming: &Incoming<'_>,
8698    ) -> Result<RepositoryTarget, SourceError> {
8699        let fallback = self.configured_repository()?;
8700        let what = |incoming: &Incoming<'_>| {
8701            format!(
8702                "{} {:?}",
8703                incoming.written.kind().describes(),
8704                incoming.title
8705            )
8706        };
8707        let parent = match incoming.parent {
8708            Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
8709                SourceError::Refused {
8710                    message: format!(
8711                        "GitHub project issue {} was not found on the board of source {}, so {} \
8712                         cannot be filed under it",
8713                        parent.0,
8714                        self.name,
8715                        what(incoming)
8716                    ),
8717                }
8718            })?),
8719            None => None,
8720        };
8721        let parents_repository = parent
8722            .as_ref()
8723            .map(|parent| {
8724                // A draft is on the board and so is found, but it has no repository to
8725                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
8726                // would refuse the task only once `createIssue` had made it.
8727                if parent.content_kind == ContentKind::DraftIssue {
8728                    return Err(SourceError::Refused {
8729                        message: format!(
8730                            "GitHub project item {} on the board of source {} is a draft, \
8731                             which cannot have sub-issues, so {} cannot be filed under it",
8732                            parent.id.0,
8733                            self.name,
8734                            what(incoming)
8735                        ),
8736                    });
8737                }
8738                // An issue's repository is where a sub-issue is placed and whose owner it
8739                // is compared against, so a parent whose repository this source cannot
8740                // spell as `owner/name` — GitHub's login grammar is wider than this
8741                // source's floor — is one nothing can be filed under.
8742                parent
8743                    .own_repository
8744                    .as_ref()
8745                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
8746                    .ok_or_else(|| SourceError::Malformed {
8747                        message: format!(
8748                            "GitHub project issue {} on the board of source {} is in {}, which \
8749                             is not a {}/owner/name repository this source can place {} in",
8750                            parent.id.0,
8751                            self.name,
8752                            parent
8753                                .own_repository
8754                                .as_ref()
8755                                .map_or("no repository", Repository::as_str),
8756                            RepositoryTarget::HOST,
8757                            what(incoming)
8758                        ),
8759                    })
8760            })
8761            .transpose()?;
8762        match incoming.repositories {
8763            [named] => {
8764                let target =
8765                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
8766                        message: format!(
8767                            "{} names repository {}, which is not a {}/owner/name repository \
8768                             source {} can create an issue in; name one that is, or name none",
8769                            what(incoming),
8770                            named.as_str(),
8771                            RepositoryTarget::HOST,
8772                            self.name
8773                        ),
8774                    })?;
8775                if let Some(parents) = &parents_repository
8776                    && parents.owner != target.owner
8777                {
8778                    return Err(SourceError::Refused {
8779                        message: format!(
8780                            "{} names repository {}, owned by {}, but its project's issue is in \
8781                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
8782                             of the same owner as its parent issue; name a repository of {}, or \
8783                             name none",
8784                            what(incoming),
8785                            target.slug(),
8786                            target.owner,
8787                            parents.slug(),
8788                            parents.owner,
8789                            parents.owner
8790                        ),
8791                    });
8792                }
8793                Ok(target)
8794            }
8795            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
8796        }
8797    }
8798
8799    /// The node id of the repository `incoming` is being created in, or the refusal naming
8800    /// the item and the repository the token cannot see.
8801    ///
8802    /// Resolved once per command per repository; see [`Self::repository_cache`].
8803    async fn repository_id(
8804        &self,
8805        repository: &RepositoryTarget,
8806        incoming: &Incoming<'_>,
8807    ) -> Result<String, SourceError> {
8808        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
8809            return Ok(id);
8810        }
8811        let data = self
8812            .graphql(
8813                graphql::REPOSITORY,
8814                json!({"owner":repository.owner,"name":repository.name}),
8815            )
8816            .await?;
8817        self.repository_read(&data, repository, incoming)
8818    }
8819
8820    /// The repository's node id out of an answer carrying the `repository` root, held for
8821    /// the rest of this command, or the refusal naming the item that cannot be created in it.
8822    fn repository_read(
8823        &self,
8824        data: &Value,
8825        repository: &RepositoryTarget,
8826        incoming: &Incoming<'_>,
8827    ) -> Result<String, SourceError> {
8828        let node = data
8829            .get("repository")
8830            .filter(|value| !value.is_null())
8831            .ok_or_else(|| SourceError::Refused {
8832                message: format!(
8833                    "GitHub repository {} was not found or is not visible to the token, so {} \
8834                     {:?} cannot be created in it",
8835                    repository.slug(),
8836                    incoming.written.kind().describes(),
8837                    incoming.title
8838                ),
8839            })?;
8840        let id = required_str(node, "id")?.to_owned();
8841        self.repository_cache()?
8842            .insert(repository.clone(), id.clone());
8843        Ok(id)
8844    }
8845
8846    fn repository_cache(
8847        &self,
8848    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
8849        self.repository_cache
8850            .lock()
8851            .map_err(|_| SourceError::Unavailable {
8852                message: "this source's record of the destination repository was left \
8853                          inconsistent by an earlier failure; next: run the command again"
8854                    .into(),
8855            })
8856    }
8857
8858    /// Create or update one board item, whichever kind it is.
8859    async fn write_item(
8860        &self,
8861        incoming: &Incoming<'_>,
8862        target: Option<&NativeId>,
8863        depends_on: &[DependencyEdge],
8864    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
8865        let prepared = self.prepare_write(incoming, target, depends_on).await?;
8866        let landed = match &prepared.destination {
8867            // Its content is written last, by `complete_write`, once everything else has landed.
8868            WriteDestination::Existing(item) => Landed {
8869                content_id: item.id.clone(),
8870                item_id: Some(item.item_id.clone()),
8871                url: item.url.clone(),
8872                number: item.number,
8873            },
8874            WriteDestination::New(target) => {
8875                if self.deferring()? {
8876                    self.create_filed_issue(
8877                        prepared.board.id.as_str(),
8878                        target,
8879                        incoming,
8880                        &prepared.body,
8881                    )
8882                    .await?
8883                } else {
8884                    self.create_and_file_issue(
8885                        prepared.board.id.as_str(),
8886                        target,
8887                        incoming,
8888                        &prepared.body,
8889                    )
8890                    .await?
8891                }
8892            }
8893        };
8894        self.complete_write(incoming, prepared, landed).await
8895    }
8896
8897    /// Everything one write of `incoming` reads and decides before it sends anything: what is
8898    /// there now, where a new item is created, the board's fields, and every value refused while
8899    /// nothing has been written — the item's body included.
8900    async fn prepare_write(
8901        &self,
8902        incoming: &Incoming<'_>,
8903        target: Option<&NativeId>,
8904        depends_on: &[DependencyEdge],
8905    ) -> Result<Prepared, SourceError> {
8906        // Refused before anything is read or written: a task or a project titled the way
8907        // this board spells a document would land as an issue this same source reads back
8908        // as a document, so the field this destination cannot carry is named rather than
8909        // written and silently reclassified.
8910        if let Written::Work(kind, _) = incoming.written
8911            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
8912        {
8913            return Err(SourceError::Refused {
8914                message: format!(
8915                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8916                     spells a document, so it would read back as one rather than as a {}; \
8917                     retitle it, or copy it as a document",
8918                    kind.marker(),
8919                    self.name,
8920                    kind.marker()
8921                ),
8922            });
8923        }
8924        // The destination is read by its own id, and whether this board holds it is decided
8925        // by that read — its own `projectItems` — rather than by whether a listing of the
8926        // board happens to include it yet. See the module documentation.
8927        // An existing issue is never moved; a new one is created where the rule says — and
8928        // knowing where is what lets the board's fields and that repository's id be read
8929        // together, before anything below needs either.
8930        let destination = match target {
8931            Some(target) => {
8932                WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
8933                    || SourceError::Refused {
8934                        message: format!("GitHub destination item {} was not found", target.0),
8935                    },
8936                )?))
8937            }
8938            None => {
8939                let target = self.creation_target(incoming).await?;
8940                self.creation_context(&target, incoming).await?;
8941                WriteDestination::New(target)
8942            }
8943        };
8944        let existing = destination.existing();
8945        let board = self
8946            .fields_for(
8947                existing,
8948                incoming.written.status().is_some(),
8949                incoming
8950                    .priority
8951                    .is_some_and(|priority| priority != Priority::None),
8952            )
8953            .await?;
8954        let status_target = incoming
8955            .written
8956            .work_status()
8957            .map(|(kind, status)| self.resolved_target(kind, status.category))
8958            .transpose()?;
8959        let column = match (incoming.written.work_status(), status_target.as_ref()) {
8960            (Some((kind, status)), Some(target)) => {
8961                self.column_for(&board.fields, kind, status.category, target)?
8962            }
8963            _ => None,
8964        };
8965        // Resolved before anything is created, for the reason the column above is: a
8966        // priority this board has no option for is refused while nothing has been written.
8967        let priority_write = match incoming.priority {
8968            Some(priority) => self.priority_write(&board.fields, existing, priority)?,
8969            None => None,
8970        };
8971        // And for the reason the priority is: a value no projected field can hold, or a board
8972        // that cannot hold the field, is refused while nothing has been written.
8973        let (projections, projected) = self.projection_writes(
8974            &board.fields,
8975            existing.map(|item| &item.projected),
8976            incoming.metadata,
8977            None,
8978        )?;
8979        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
8980        if content_kind == ContentKind::DraftIssue {
8981            if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
8982                (status_target.as_ref(), incoming.written.status())
8983            {
8984                return Err(self.closes_a_draft(status.category));
8985            }
8986            if incoming.parent.is_some() {
8987                return Err(SourceError::Refused {
8988                    message: "GitHub draft items cannot be a project's sub-issue".into(),
8989                });
8990            }
8991        }
8992        match existing {
8993            Some(item) if content_kind == ContentKind::Issue => {
8994                if item.labels != incoming.labels {
8995                    return Err(SourceError::Refused {
8996                        message: "GitHub issue labels differ from the labels being written".into(),
8997                    });
8998                }
8999            }
9000            _ => {
9001                if !incoming.labels.is_empty() {
9002                    return Err(SourceError::Refused {
9003                        message: "GitHub items created by this destination carry no labels".into(),
9004                    });
9005                }
9006            }
9007        }
9008
9009        // The repository the issue really lives in is what the slot below is written against,
9010        // so a single entry that is where the issue is created travels as no key at all, and
9011        // the read side derives it back from the issue.
9012        let own_repository = match &destination {
9013            WriteDestination::Existing(item) => item.own_repository.clone(),
9014            WriteDestination::New(target) => Some(
9015                Repository::try_from(target.origin())
9016                    .map_err(|message| SourceError::Config { message })?,
9017            ),
9018        };
9019        let (native, fallback) = self
9020            .partition_edges(
9021                incoming.written.kind(),
9022                content_kind,
9023                existing.and_then(|item| item.blocked_by.as_deref()),
9024                depends_on,
9025            )
9026            .await?;
9027        let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
9028        let content = match incoming.assets {
9029            Some(assets) => {
9030                let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
9031                let rewritten = onetaskgraph_plugin_api::serve_asset_references(
9032                    incoming.content.unwrap_or_default(),
9033                    &mut slot,
9034                    &uploads,
9035                );
9036                incoming.content.map(|_| rewritten)
9037            }
9038            None => incoming.content.map(str::to_owned),
9039        };
9040        let body = compose_body(content.as_deref(), &slot)?;
9041        // Read before anything is created, for the reason the field below is: a value
9042        // this destination cannot store has to refuse, and refusing after `createIssue`
9043        // would leave an issue behind that nothing asked for. The engine writes a
9044        // qualified id here; a caller handing this key anything else is told so rather
9045        // than having it silently stored as no origin at all.
9046        // 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.
9047        let origin = match incoming.metadata.get(ORIGIN_KEY) {
9048            None => "",
9049            Some(Value::String(origin)) => origin.as_str(),
9050            Some(other) => {
9051                return Err(SourceError::Refused {
9052                    message: format!(
9053                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
9054                         is {other}"
9055                    ),
9056                });
9057            }
9058        };
9059        // Resolved before anything is created: a board that cannot carry the copy origin
9060        // has to refuse the write, and refusing it after `createIssue` would leave an
9061        // issue behind that nothing asked for.
9062        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
9063            Some(field) => {
9064                if required_str(field, "__typename")? != "ProjectV2Field" {
9065                    return Err(SourceError::Refused {
9066                        message: format!(
9067                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
9068                        ),
9069                    });
9070                }
9071                Some(required_str(field, "id")?.to_owned())
9072            }
9073            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
9074                return Err(SourceError::Refused {
9075                    message: format!(
9076                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
9077                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
9078                         the board"
9079                    ),
9080                });
9081            }
9082            None => None,
9083        };
9084
9085        Ok(Prepared {
9086            origin: origin.to_owned(),
9087            destination,
9088            board,
9089            status_target,
9090            column,
9091            priority_write,
9092            projections,
9093            projected,
9094            content_kind,
9095            own_repository,
9096            native,
9097            slot,
9098            content,
9099            body,
9100            origin_field,
9101        })
9102    }
9103
9104    /// Everything one write does once the item it writes exists: its board fields, its
9105    /// relationships and — for an item that was already there — its content last, then what this
9106    /// process remembers of it.
9107    ///
9108    /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
9109    /// item this command created has its fields, its closing and its edges held for
9110    /// [`TaskSource::finish_writes`] rather than sent here.
9111    async fn complete_write(
9112        &self,
9113        incoming: &Incoming<'_>,
9114        prepared: Prepared,
9115        landed: Landed,
9116    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9117        let Prepared {
9118            destination,
9119            board,
9120            status_target,
9121            column,
9122            priority_write,
9123            projections,
9124            projected,
9125            content_kind,
9126            own_repository,
9127            native,
9128            slot,
9129            content,
9130            body,
9131            origin,
9132            origin_field,
9133        } = prepared;
9134        let existing = destination.existing();
9135        let origin = origin.as_str();
9136        let Landed {
9137            content_id,
9138            item_id,
9139            url,
9140            number,
9141        } = landed;
9142
9143        // Only what differs from the item as this write read it is sent: a priority the item
9144        // already holds is not, so an unchanged re-copy writes no field and a person's edit of
9145        // it since that read is left alone.
9146        let priority_write = priority_write.filter(|_| {
9147            !existing.is_some_and(|item| {
9148                incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
9149            })
9150        });
9151        let written_option = column.as_ref().map(|(_, _, name)| name.clone());
9152        let column = column
9153            .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
9154            .map(|(field, option, _)| (field, option));
9155        // Creating an item here is several calls — `createIssue`, which files it on the
9156        // board, then its board fields, the parent and the dependencies — and GitHub can fail
9157        // at any of them. Everything this source can refuse *before* the first of those is
9158        // already checked above, so what is left is GitHub itself failing part way. When it
9159        // does over an item this call created, the issue is taken back: a write that
9160        // refused must not leave an item behind that nobody asked for, and one that does
9161        // makes the retry create a second.
9162        // Whether the board-field write carrying a moved origin was answered as landing whole.
9163        // When it was refused, GitHub does not say which of its fields ran before the one that
9164        // failed, so the origin may or may not have moved.
9165        let mut origin_landed = false;
9166        // An item this command created while writes are held back has every part that can land
9167        // later held for `finish_writes` — its own create filed it on the board and under its
9168        // project already — and so does a later write of it in the same command.
9169        let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
9170        let landed = if holding {
9171            self.hold_back(
9172                &board.id,
9173                &content_id,
9174                existing,
9175                origin_field.as_deref(),
9176                origin,
9177                column,
9178                status_target.as_ref(),
9179                priority_write.as_ref(),
9180                &projections,
9181                &native,
9182            )
9183        } else if let Some(item_id) = item_id.as_deref() {
9184            self.finish_write(
9185                board.id.as_str(),
9186                incoming,
9187                &content_id,
9188                item_id,
9189                content_kind,
9190                existing,
9191                origin_field.as_deref(),
9192                origin,
9193                column,
9194                status_target.as_ref(),
9195                priority_write.as_ref(),
9196                &projections,
9197                &native,
9198                &mut origin_landed,
9199            )
9200            .await
9201        } else {
9202            // An issue a create filed on the board itself has no board item this process
9203            // knows until `finish_writes` reads it, so nothing but a held-back write may land
9204            // on it before then.
9205            Err(SourceError::Malformed {
9206                message: format!(
9207                    "issue {} was written before the board item GitHub filed it as was read",
9208                    content_id.0
9209                ),
9210            })
9211        };
9212        // An existing item's title, body and state go last, in one `updateIssue`, once its board
9213        // fields and its relationships have landed: a refusal of any of those then leaves its
9214        // body — and the metadata slot inside it — exactly as it stood.
9215        let landed = match (landed, existing) {
9216            (Ok(()), Some(item)) => {
9217                self.update_existing(item, incoming, &body, status_target.as_ref())
9218                    .await
9219            }
9220            (landed, _) => landed,
9221        };
9222        if let Err(error) = landed {
9223            match existing {
9224                // Best effort, and the write's own failure is what the caller is told: a
9225                // refusal naming the tidy-up would hide why the write failed at all.
9226                None => {
9227                    let _ = self.delete_issue(&content_id).await;
9228                }
9229                // The origin field is the one piece of an existing item's metadata written
9230                // before its body, so a write refused after it puts it back as it was. When
9231                // that is refused too, the write's own failure is still what the caller is
9232                // told — with what it left behind added, because the item's metadata is then
9233                // not as it stood and a caller retrying has to know which key moved.
9234                Some(item) => {
9235                    let before = item.origin.as_deref().unwrap_or("");
9236                    if let Some(field) = origin_field.as_deref()
9237                        && before != origin
9238                        && let Err(restore) = self
9239                            .set_item_field(
9240                                board.id.as_str(),
9241                                &item.item_id,
9242                                field,
9243                                json!({"text": before}),
9244                            )
9245                            .await
9246                    {
9247                        let left = if origin_landed {
9248                            format!(
9249                                "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
9250                                 not be put back to {before:?} ({restore}), so item {} still \
9251                                 holds {origin:?} there",
9252                                item.id.0
9253                            )
9254                        } else {
9255                            format!(
9256                                "the refused write carried its {ORIGIN_KEY} from {before:?} to \
9257                                 {origin:?}, GitHub does not say whether that part of it ran, \
9258                                 and putting it back to {before:?} was refused ({restore}), so \
9259                                 item {} holds {origin:?} or {before:?} there",
9260                                item.id.0
9261                            )
9262                        };
9263                        return Err(noting(
9264                            error,
9265                            &format!(
9266                                "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
9267                                 run the write again"
9268                            ),
9269                        ));
9270                    }
9271                }
9272            }
9273            return Err(error);
9274        }
9275
9276        let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
9277            (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
9278                self.statuses
9279                    .status(kind, written_option.as_deref(), true, Some(reason.reason()))
9280            }
9281            (Some((kind, _)), Some(StatusTarget::Column(_))) => {
9282                self.statuses
9283                    .status(kind, written_option.as_deref(), false, None)
9284            }
9285            (Some((_, status)), _) => status.clone(),
9286            (None, _) => Status {
9287                category: StatusCategory::Unknown,
9288                name: "Open".to_owned(),
9289            },
9290        };
9291
9292        // So the rest of this command reads what it just did rather than what the board
9293        // said before it. See `remember_written` for which half takes it.
9294        let remembered = Resolved {
9295            // Empty for an issue whose board item is not read yet: `finish_writes` reads it
9296            // and `learn_item` puts it here.
9297            // 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.
9298            item_id: item_id.unwrap_or_default(),
9299            id: content_id.clone(),
9300            content_kind,
9301            kind: incoming.written.kind(),
9302            title: incoming.title.to_owned(),
9303            // The visible half of the body this write composed, split back off it the
9304            // way a read splits it — so what this record reports is what a read of the
9305            // same issue reports, rather than the person's text with the metadata slot
9306            // still on the end of it.
9307            body: metadata_body(body.clone())?.0,
9308            raw_body: body.clone(),
9309            // A document has no status of its own; what it reads back as is whatever
9310            // the issue's own state says, which is what a re-read reports.
9311            status: written_status,
9312            option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
9313            priority: match incoming.priority {
9314                Some(priority) => HeldPriority::Read(priority),
9315                None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
9316                    item.priority.clone()
9317                }),
9318            },
9319            // What `state_input` asked for: closed for a terminal target, open for any other
9320            // status, and the issue's own state left as it was by a document write.
9321            closed: content_kind == ContentKind::Issue
9322                && match status_target.as_ref() {
9323                    Some(StatusTarget::Terminal(_, _)) => true,
9324                    Some(_) => false,
9325                    None => existing.is_some_and(|item| item.closed),
9326                },
9327            reason: match status_target.as_ref() {
9328                Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
9329                    Some(reason.reason().to_owned())
9330                }
9331                Some(_) => None,
9332                None => existing.and_then(|item| item.reason.clone()),
9333            },
9334            delivers: incoming.delivers.to_vec(),
9335            delivered_by: incoming.delivered_by.to_vec(),
9336            labels: incoming.labels.to_vec(),
9337            parent: incoming.parent.cloned(),
9338            origin: (!origin.is_empty()).then(|| origin.to_owned()),
9339            projected,
9340            number,
9341            // In the update path this is the item's own url, read off `existing` where the
9342            // record above was bound, so one expression serves both halves.
9343            url,
9344            created_at: existing.and_then(|item| item.created_at),
9345            updated_at: existing.and_then(|item| item.updated_at),
9346            // What this write moved is GitHub's to say, so a record of it gives no token until
9347            // it is read again.
9348            item_updated_at: None,
9349            comment_count: None,
9350            relationship_events: None,
9351            served_at: None,
9352            facets: None,
9353            own_repository,
9354            repositories: incoming.repositories.to_vec(),
9355            classification: incoming.classification,
9356            slot,
9357            board_id: Some(board.id.as_str().to_owned()),
9358            fields: board
9359                .fields
9360                .get("nodes")
9361                .and_then(Value::as_array)
9362                .cloned()
9363                .unwrap_or_default(),
9364            board_fields: Some(board.fields.clone()),
9365            // What this write left the relationship holding is known by id alone, and a
9366            // later read of its edges needs each far end's kind, so it reads them again.
9367            blocked_by: None,
9368        };
9369        self.remember_written(remembered, existing.is_none())?;
9370        Ok(onetaskgraph_plugin_api::AssetsWritten {
9371            id: content_id,
9372            content,
9373        })
9374    }
9375
9376    /// Everything a write does after the item exists: its board fields, its parent, and
9377    /// its dependencies.
9378    ///
9379    /// Split out of `write_item` so there is one place a failure past the point of no
9380    /// return is caught, rather than a tidy-up repeated at each `?` above.
9381    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
9382    // so there is one place a failure past the point of no return is caught, and its
9383    // arguments are exactly the values that tail already had in scope. Bundling them into a
9384    // struct would describe no concept — it would be "the arguments of this function" — and
9385    // would put the whole of `write_item`'s locals behind one more indirection.
9386    #[allow(clippy::too_many_arguments)]
9387    async fn finish_write(
9388        &self,
9389        board_id: &str,
9390        incoming: &Incoming<'_>,
9391        content_id: &NativeId,
9392        item_id: &str,
9393        content_kind: ContentKind,
9394        existing: Option<&Resolved>,
9395        origin_field: Option<&str>,
9396        origin: &str,
9397        column: Option<(String, String)>,
9398        status_target: Option<&StatusTarget>,
9399        priority: Option<&PriorityWrite>,
9400        projections: &[ProjectionWrite],
9401        native: &[String],
9402        origin_landed: &mut bool,
9403    ) -> Result<(), SourceError> {
9404        let (fields, clears) = field_writes(
9405            existing,
9406            origin_field,
9407            origin,
9408            column,
9409            priority,
9410            projections,
9411        );
9412        self.set_item_fields(board_id, item_id, &fields, &clears)
9413            .await?;
9414        *origin_landed = true;
9415
9416        // An existing issue closes in the `updateIssue` its write ends with; one created just
9417        // now closes here, once its option is selected.
9418        if existing.is_none()
9419            && content_kind == ContentKind::Issue
9420            && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
9421        {
9422            self.update_content(
9423                ContentKind::Issue,
9424                content_id,
9425                json!({"stateInput":state_input(status_target)}),
9426            )
9427            .await?;
9428        }
9429
9430        if content_kind == ContentKind::Issue {
9431            self.reparent(
9432                existing.and_then(|item| item.parent.clone()),
9433                content_id,
9434                incoming.parent,
9435            )
9436            .await?;
9437            // A document takes part in no dependency graph, so writing one neither reads
9438            // nor changes the issue's own `blockedBy` relationships. Reconciling them
9439            // against the empty list a document write carries would *delete* whatever
9440            // relationships a person had made on that issue, which is a write nobody
9441            // asked for.
9442            if incoming.written.kind() != BoardKind::Document {
9443                let issue = match existing {
9444                    Some(item) => Issue::Existing(item.blocked_by.as_deref()),
9445                    None => Issue::Created,
9446                };
9447                self.reconcile_blocked_by(content_id, native, issue).await?;
9448            }
9449        }
9450        Ok(())
9451    }
9452
9453    /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
9454    fn deferring(&self) -> Result<bool, SourceError> {
9455        Ok(self.deferred()?.is_some())
9456    }
9457
9458    /// Whether `content` is an item this command created whose parts are held back.
9459    fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
9460        Ok(self
9461            .deferred()?
9462            .as_ref()
9463            .is_some_and(|deferred| deferred.holds(content)))
9464    }
9465
9466    fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
9467        self.deferred.lock().map_err(|_| SourceError::Unavailable {
9468            message: "this source's record of the writes it is holding back was left \
9469                      inconsistent by an earlier failure; next: run the command again"
9470                .into(),
9471        })
9472    }
9473
9474    /// Creates one issue filed on the board and under its project by the `createIssue` itself —
9475    /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
9476    /// creates with: one request where adding it to the board and filing it under its project
9477    /// were two more. GitHub adds the board item after answering, so its id is not known here;
9478    /// [`TaskSource::finish_writes`] reads it.
9479    async fn create_filed_issue(
9480        &self,
9481        board_id: &str,
9482        repository: &RepositoryTarget,
9483        incoming: &Incoming<'_>,
9484        body: &Option<String>,
9485    ) -> Result<Landed, SourceError> {
9486        let repository_id = self.repository_id(repository, incoming).await?;
9487        let data = self
9488            .graphql(
9489                graphql::CREATE_ISSUE,
9490                json!({"input": filed_input(&repository_id, board_id, incoming, body)}),
9491            )
9492            .await?;
9493        let created = data
9494            .pointer("/createIssue/issue")
9495            .filter(|value| !value.is_null())
9496            .ok_or_else(|| SourceError::Malformed {
9497                message: "GitHub issue creation returned no issue".into(),
9498            })?;
9499        self.created_landed(created).await
9500    }
9501
9502    /// Keep the address `createIssue` answered for the issue `content`, for
9503    /// [`TaskSource::written_location`].
9504    fn note_written(&self, content: &NativeId, url: Option<&str>) {
9505        if let Some(url) = url {
9506            self.written
9507                .lock()
9508                .unwrap_or_else(std::sync::PoisonError::into_inner)
9509                .insert(content.clone(), url.to_owned());
9510        }
9511    }
9512
9513    /// What one `createIssue` answered, as the item it created — or, when its number cannot be
9514    /// read, the refusal, after taking the issue back.
9515    async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
9516        let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
9517        let url = optional_str(created, "url")?.map(str::to_owned);
9518        let number = match created_issue_number(created) {
9519            Ok(number) => number,
9520            Err(error) => {
9521                let _ = self.delete_issue(&content_id).await;
9522                return Err(error);
9523            }
9524        };
9525        self.note_written(&content_id, url.as_deref());
9526        Ok(Landed {
9527            content_id,
9528            item_id: None,
9529            url,
9530            number,
9531        })
9532    }
9533
9534    /// Hold back everything a write of the created item `content` would send after creating it,
9535    /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
9536    /// `finish_write` sends, the parent excepted, which its create already named. A later write
9537    /// of the same item replaces what it changes.
9538    // 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.
9539    #[allow(clippy::too_many_arguments)]
9540    fn hold_back(
9541        &self,
9542        board_id: &BoardId,
9543        content: &NativeId,
9544        existing: Option<&Resolved>,
9545        origin_field: Option<&str>,
9546        origin: &str,
9547        column: Option<(String, String)>,
9548        status_target: Option<&StatusTarget>,
9549        priority: Option<&PriorityWrite>,
9550        projections: &[ProjectionWrite],
9551        native: &[String],
9552    ) -> Result<(), SourceError> {
9553        let status = column.map(|(field, option)| {
9554            let name = self
9555                .fields_cache()
9556                .ok()
9557                .and_then(|fields| {
9558                    fields.as_ref().and_then(|board| {
9559                        option_name(&board.fields, &field, &option).map(str::to_owned)
9560                    })
9561                })
9562                .unwrap_or_default();
9563            (field, option, name)
9564        });
9565        let (fields, clears) =
9566            field_writes(existing, origin_field, origin, None, priority, projections);
9567        let close = match status_target {
9568            Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
9569            _ => None,
9570        };
9571        let mut deferred = self.deferred()?;
9572        let deferred = deferred.get_or_insert_with(Deferred::default);
9573        if let Deferred::Nothing = deferred {
9574            *deferred = Deferred::Held {
9575                board: board_id.clone(),
9576                created: Vec::new(),
9577            };
9578        }
9579        let Deferred::Held { created, .. } = deferred else {
9580            unreachable!("held back on the line above");
9581        };
9582        let held = match created.iter().position(|held| held.content == *content) {
9583            Some(at) => &mut created[at],
9584            None => {
9585                created.push(HeldBack {
9586                    content: content.clone(),
9587                    fields: Vec::new(),
9588                    status: None,
9589                    clears: Vec::new(),
9590                    close: None,
9591                    edges: Vec::new(),
9592                });
9593                created.last_mut().expect("just held")
9594            }
9595        };
9596        for (field, value) in fields {
9597            held.fields.retain(|(held, _)| *held != field);
9598            held.clears.retain(|held| *held != field);
9599            held.fields.push((field, value));
9600        }
9601        for field in clears {
9602            held.fields.retain(|(held, _)| held != field);
9603            if !held.clears.iter().any(|held| held == field) {
9604                held.clears.push(field.to_owned());
9605            }
9606        }
9607        if status.is_some() {
9608            held.status = status;
9609        }
9610        if close.is_some() {
9611            held.close = close;
9612        }
9613        // Nothing of an item created in this command has been sent but its create, so the edges
9614        // it is to have are exactly the ones its latest write names.
9615        held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
9616        Ok(())
9617    }
9618
9619    /// Create every task of `writes`, together, filed on the board and under each one's
9620    /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
9621    ///
9622    /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
9623    /// before the first request — then created, then completed with its parts held back. A
9624    /// request refused part way leaves the issues it did create, which are taken back, with the
9625    /// rest of this call's, before the refusal is answered.
9626    async fn create_together(
9627        &self,
9628        writes: &[ItemWrite<Task>],
9629    ) -> Result<Vec<NativeId>, SourceError> {
9630        let incomings: Vec<Incoming<'_>> = writes
9631            .iter()
9632            .map(|write| self.task_incoming(write, None))
9633            .collect::<Result<_, _>>()?;
9634        let mut prepared = Vec::with_capacity(writes.len());
9635        for (incoming, write) in incomings.iter().zip(writes) {
9636            if write.target.is_some() {
9637                return Err(SourceError::Malformed {
9638                    message: "a write naming its target was handed to a batch of creates".into(),
9639                });
9640            }
9641            prepared.push(
9642                self.prepare_write(incoming, None, &write.depends_on)
9643                    .await?,
9644            );
9645        }
9646        let mut inputs = Vec::with_capacity(prepared.len());
9647        for (incoming, prepared) in incomings.iter().zip(&prepared) {
9648            let WriteDestination::New(target) = &prepared.destination else {
9649                return Err(SourceError::Malformed {
9650                    message: "an update was decided for a write handed to a batch of creates"
9651                        .into(),
9652                });
9653            };
9654            let repository_id = self.repository_id(target, incoming).await?;
9655            inputs.push(filed_input(
9656                &repository_id,
9657                prepared.board.id.as_str(),
9658                incoming,
9659                &prepared.body,
9660            ));
9661        }
9662        let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
9663        let refused = 'create: {
9664            for chunk in inputs.chunks(MUTATION_BATCH) {
9665                let mut variables = serde_json::Map::new();
9666                for slot in 0..MUTATION_BATCH {
9667                    variables.insert(
9668                        format!("create{slot}"),
9669                        chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
9670                    );
9671                    variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
9672                }
9673                let data = match self
9674                    .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
9675                    .await
9676                {
9677                    Ok(data) => data,
9678                    Err(error) => break 'create Some(error),
9679                };
9680                let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
9681                    let said: Vec<&str> = errors
9682                        .as_array()
9683                        .into_iter()
9684                        .flatten()
9685                        .filter_map(|error| error.get("message").and_then(Value::as_str))
9686                        .collect();
9687                    SourceError::Refused {
9688                        message: if said.is_empty() {
9689                            "GitHub returned GraphQL errors".to_owned()
9690                        } else {
9691                            said.join("; ")
9692                        },
9693                    }
9694                });
9695                for slot in 0..chunk.len() {
9696                    match data
9697                        .pointer(&format!("/create{slot}/issue"))
9698                        .filter(|value| !value.is_null())
9699                    {
9700                        Some(created) => match self.created_landed(created).await {
9701                            Ok(created) => landed.push(created),
9702                            Err(error) => {
9703                                refusal.get_or_insert(error);
9704                            }
9705                        },
9706                        // An alias GitHub refused answers nothing, and the refusal says why.
9707                        None => {
9708                            refusal.get_or_insert(SourceError::Malformed {
9709                                message: "GitHub issue creation returned no issue".into(),
9710                            });
9711                        }
9712                    }
9713                }
9714                if let Some(error) = refusal {
9715                    break 'create Some(error);
9716                }
9717            }
9718            None
9719        };
9720        if let Some(error) = refused {
9721            // None of these ids reaches the caller, so an issue GitHub will not take back is
9722            // named here or by nobody.
9723            let mut left: Vec<&str> = Vec::new();
9724            for created in &landed {
9725                if self.delete_issue(&created.content_id).await.is_err() {
9726                    left.push(created.content_id.0.as_str());
9727                }
9728            }
9729            return Err(left_behind(error, &left));
9730        }
9731        let created: Vec<NativeId> = landed
9732            .iter()
9733            .map(|landed| landed.content_id.clone())
9734            .collect();
9735        let mut ids = Vec::with_capacity(landed.len());
9736        for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
9737            match self.complete_write(incoming, prepared, landed).await {
9738                Ok(written) => ids.push(written.id),
9739                // 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.
9740                Err(error) => {
9741                    let mut left: Vec<&str> = Vec::new();
9742                    for id in &created {
9743                        if self.delete_issue(id).await.is_err() {
9744                            left.push(id.0.as_str());
9745                        }
9746                    }
9747                    return Err(left_behind(error, &left));
9748                }
9749            }
9750        }
9751        Ok(ids)
9752    }
9753
9754    /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
9755    /// created items' board items, then their field values, closes and edges, each kind in
9756    /// requests of [`MUTATION_BATCH`]. Anything refused takes back every issue held, and
9757    /// answers the refusal.
9758    async fn send_held_back(&self) -> Result<(), SourceError> {
9759        let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
9760            return Ok(());
9761        };
9762        if created.is_empty() {
9763            return Ok(());
9764        }
9765        let sent = self.send_deferred(board.as_str(), &created).await;
9766        if let Err(error) = sent {
9767            // Best effort, and the failure is what the caller is told, as a write of one item
9768            // that fails part way takes back what it created: the copy that held these back
9769            // journalled every one, so one this cannot take back its undo tries again, and says
9770            // so when it cannot either.
9771            for held in &created {
9772                let _ = self.delete_issue(&held.content).await;
9773            }
9774            return Err(error);
9775        }
9776        Ok(())
9777    }
9778
9779    /// The requests [`Self::send_held_back`] makes.
9780    async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
9781        let items = self.created_board_items(board, created).await?;
9782        let mut fields: Vec<Value> = Vec::new();
9783        let mut closes: Vec<Value> = Vec::new();
9784        let mut edges: Vec<Value> = Vec::new();
9785        for held in created {
9786            let (item, status) = &items[&held.content];
9787            let mut values = held.fields.clone();
9788            // A status the board already shows — set by a workflow a person turned on to file
9789            // new items at it — is not written again.
9790            if let Some((field, option, name)) = &held.status
9791                && status.as_deref() != Some(name.as_str())
9792            {
9793                values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
9794            }
9795            for (field, value) in values {
9796                fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
9797            }
9798            if let Some(reason) = held.close {
9799                closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
9800            }
9801            for far in &held.edges {
9802                edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
9803            }
9804            if !held.clears.is_empty() {
9805                let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
9806                self.set_item_fields(board, item, &[], &clears).await?;
9807            }
9808        }
9809        // Each alias answers with what it wrote, which is what its input named: the board item a
9810        // field value is set on, the issue closed, the issue blocked and the one blocking it.
9811        let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
9812        let close_answer: &[(&str, &str)] = &[("issue", "id")];
9813        let block_answer: &[(&str, &str)] =
9814            &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
9815        for (document, prefix, inputs, answers) in [
9816            (graphql::WRITE_FIELD_VALUES, "field", &fields, field_answer),
9817            (graphql::CLOSE_ISSUES, "close", &closes, close_answer),
9818            (graphql::ADD_BLOCKED_BYS, "block", &edges, block_answer),
9819        ] {
9820            for chunk in inputs.chunks(MUTATION_BATCH) {
9821                let mut variables = serde_json::Map::new();
9822                for slot in 0..MUTATION_BATCH {
9823                    variables.insert(
9824                        format!("{prefix}{slot}"),
9825                        chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
9826                    );
9827                    variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
9828                }
9829                let data = self.graphql(document, Value::Object(variables)).await?;
9830                for (slot, input) in chunk.iter().enumerate() {
9831                    for (root, named) in answers {
9832                        let answered = data
9833                            .pointer(&format!("/{prefix}{slot}/{root}/id"))
9834                            .and_then(Value::as_str)
9835                            .ok_or_else(|| SourceError::Malformed {
9836                                message: format!(
9837                                    "GitHub answered {} with nothing at {prefix}{slot}",
9838                                    operation_description(document)
9839                                ),
9840                            })?;
9841                        let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
9842                        if answered != asked {
9843                            return Err(SourceError::Malformed {
9844                                message: format!(
9845                                    "GitHub answered {} at {prefix}{slot} with {answered}, which \
9846                                     is not {asked}, the item it was asked to write",
9847                                    operation_description(document)
9848                                ),
9849                            });
9850                        }
9851                    }
9852                }
9853            }
9854        }
9855        for held in created {
9856            self.learn_item(&held.content, &items[&held.content].0)?;
9857        }
9858        Ok(())
9859    }
9860
9861    /// The board item each of `created` is on this board, with its `Status` as the board set it:
9862    /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
9863    /// eight and sixteen — while GitHub has not filed one yet.
9864    async fn created_board_items(
9865        &self,
9866        board: &str,
9867        created: &[HeldBack],
9868    ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
9869        let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
9870        let mut wait = Duration::from_secs(1);
9871        for attempt in 0..6 {
9872            let unfiled: Vec<&NativeId> = created
9873                .iter()
9874                .map(|held| &held.content)
9875                .filter(|id| !found.contains_key(*id))
9876                .collect();
9877            if unfiled.is_empty() {
9878                return Ok(found);
9879            }
9880            if attempt > 0 {
9881                self.clock
9882                    .sleep_because(wait, pacing::cause::BOARD_FILING)
9883                    .await;
9884                wait = wait.saturating_mul(2);
9885            }
9886            for chunk in unfiled.chunks(COPY_DETAIL_BATCH) {
9887                let mut variables = serde_json::Map::new();
9888                for slot in 0..COPY_DETAIL_BATCH {
9889                    variables.insert(
9890                        format!("id{slot}"),
9891                        json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
9892                    );
9893                    variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
9894                }
9895                variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
9896                variables.insert("statusName".into(), json!(STATUS_FIELD));
9897                let data = self
9898                    .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
9899                    .await?;
9900                for (slot, id) in chunk.iter().enumerate() {
9901                    // An answer for another issue than the one asked about says nothing of
9902                    // where that one is filed, and its board item is not this one's.
9903                    let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
9904                    let named = node.map(|node| node.get("id").and_then(Value::as_str));
9905                    if let Some(named) = named
9906                        && named != Some(id.0.as_str())
9907                    {
9908                        return Err(SourceError::Malformed {
9909                            message: format!(
9910                                "GitHub answered the board items of {} when asked for those of \
9911                                 {}",
9912                                named.unwrap_or("an issue it did not name"),
9913                                id.0
9914                            ),
9915                        });
9916                    }
9917                    // An issue GitHub does not answer yet is not filed yet; one it answers is
9918                    // read whole, its connection, each board item's board and its status, and
9919                    // any of them unreadable is refused rather than read as not filed.
9920                    let Some(node) = node else { continue };
9921                    let malformed = |what: &str| SourceError::Malformed {
9922                        message: format!("GitHub answered the board items of {} with {what}", id.0),
9923                    };
9924                    let nodes = node
9925                        .pointer("/projectItems/nodes")
9926                        .and_then(Value::as_array)
9927                        .ok_or_else(|| malformed("no list of board items"))?;
9928                    let mut item = None;
9929                    for node in nodes {
9930                        let on = node
9931                            .pointer("/project/id")
9932                            .and_then(Value::as_str)
9933                            .ok_or_else(|| malformed("a board item naming no board"))?;
9934                        if on == board {
9935                            item = Some(node);
9936                        }
9937                    }
9938                    if let Some(item) = item {
9939                        let status = match item.get("status") {
9940                            None | Some(Value::Null) => None,
9941                            Some(status) => Some(
9942                                status
9943                                    .get("name")
9944                                    .and_then(Value::as_str)
9945                                    .ok_or_else(|| malformed("a status that names no option"))?
9946                                    .to_owned(),
9947                            ),
9948                        };
9949                        found.insert(
9950                            (*id).clone(),
9951                            (required_nonblank_str(item, "id")?.to_owned(), status),
9952                        );
9953                    }
9954                }
9955            }
9956        }
9957        let unfiled: Vec<&str> = created
9958            .iter()
9959            .map(|held| held.content.0.as_str())
9960            .filter(|id| !found.keys().any(|found| found.0 == *id))
9961            .collect();
9962        // The last read may be the one that finds the rest.
9963        if unfiled.is_empty() {
9964            return Ok(found);
9965        }
9966        Err(SourceError::Unavailable {
9967            message: format!(
9968                "GitHub created {} filed on board {board} and has not added {} to it after 31 \
9969                 seconds: {}; next: run the copy again once GitHub has caught up",
9970                if unfiled.len() == 1 {
9971                    "an issue"
9972                } else {
9973                    "issues"
9974                },
9975                if unfiled.len() == 1 { "it" } else { "them" },
9976                unfiled.join(", ")
9977            ),
9978        })
9979    }
9980
9981    /// The board item id of `content`, which this process created and remembered without one,
9982    /// in every half of its own view.
9983    fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
9984        if let Some(held) = self.resolved_cache()?.get_mut(content) {
9985            held.item_id = item.to_owned();
9986        }
9987        if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
9988            held.item_id = item.to_owned();
9989        }
9990        Ok(())
9991    }
9992
9993    /// Ask together which items each of `origins` names, and what creating an item reads; see
9994    /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
9995    /// page is left to its own lookup.
9996    async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
9997        let Some(repository) = self.repository.clone() else {
9998            return Ok(());
9999        };
10000        let unasked: Vec<&String> = {
10001            let held = self.narrowed_cache()?;
10002            let mut unasked: Vec<&String> = Vec::new();
10003            for origin in origins {
10004                if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
10005                    && !unasked.contains(&origin)
10006                {
10007                    unasked.push(origin);
10008                }
10009            }
10010            unasked
10011        };
10012        if unasked.is_empty() {
10013            return Ok(());
10014        }
10015        for chunk in unasked.chunks(ORIGIN_BATCH) {
10016            let mut variables = json!({"owner":self.owner,"number":self.project_number,
10017                "type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
10018                "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
10019                "repositoryOwner":repository.owner,"repositoryName":repository.name});
10020            for slot in 0..ORIGIN_BATCH {
10021                let origin = chunk.get(slot).unwrap_or(&chunk[0]);
10022                variables[format!("filter{slot}")] =
10023                    json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
10024                variables[format!("search{slot}")] = json!(
10025                    self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
10026                );
10027                variables[format!("ask{slot}")] = json!(slot < chunk.len());
10028            }
10029            let data = self.graphql(graphql::ORIGIN_LOOKUPS, variables).await?;
10030            self.fields_read(&data)?;
10031            if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
10032                let id = required_nonblank_str(node, "id")?.to_owned();
10033                self.repository_cache()?.insert(repository.clone(), id);
10034                if let Some(numeric) = node
10035                    .get("databaseId")
10036                    .and_then(Value::as_u64)
10037                    .and_then(std::num::NonZeroU64::new)
10038                {
10039                    self.numeric_repositories
10040                        .lock()
10041                        .await
10042                        .insert(repository.clone(), numeric);
10043                }
10044            }
10045            for (slot, origin) in chunk.iter().enumerate() {
10046                let items = data
10047                    .pointer(&format!("/o{slot}/projectV2/items"))
10048                    .filter(|value| !value.is_null())
10049                    .ok_or_else(|| SourceError::Refused {
10050                        message: format!(
10051                            "GitHub project {}/{} was not found or is not visible to the token",
10052                            self.owner, self.project_number
10053                        ),
10054                    })?;
10055                let searched =
10056                    data.get(format!("s{slot}"))
10057                        .ok_or_else(|| SourceError::Malformed {
10058                            message: "GitHub search response has no search connection".into(),
10059                        })?;
10060                if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
10061                    continue;
10062                }
10063                let mut found: Vec<Resolved> = Vec::new();
10064                for item in optional_nodes(Some(items), "project items")?
10065                    .into_iter()
10066                    .flatten()
10067                {
10068                    if let Some(resolved) = self.resolve(item)?
10069                        && resolved.content_kind == ContentKind::Issue
10070                        && !found.iter().any(|held| held.id == resolved.id)
10071                    {
10072                        found.push(resolved);
10073                    }
10074                }
10075                for node in optional_nodes(Some(searched), "search")?
10076                    .into_iter()
10077                    .flatten()
10078                {
10079                    if let Some(resolved) = self.resolve_issue(node).await?
10080                        && !found.iter().any(|held| held.id == resolved.id)
10081                    {
10082                        found.push(resolved);
10083                    }
10084                }
10085                self.narrowed_cache()?
10086                    .insert(Narrowing::Origin((*origin).clone()).key(), found);
10087            }
10088        }
10089        Ok(())
10090    }
10091
10092    /// Delete one issue, which takes its board item with it.
10093    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
10094        let data = self
10095            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10096            .await?;
10097        data.pointer("/deleteIssue/repository")
10098            .filter(|value| !value.is_null())
10099            .ok_or_else(|| SourceError::Malformed {
10100                message: "GitHub issue deletion returned no repository".into(),
10101            })?;
10102        self.forget(id)?;
10103        Ok(())
10104    }
10105
10106    /// Remove one item this copy created, so a copy that could not finish leaves the board
10107    /// as it found it.
10108    ///
10109    /// Deleting the issue takes its board item with it, so there is no second mutation to
10110    /// keep in step. An id the board does not hold is not an error: the item is already
10111    /// gone, which is the state this asks for. Which that is, is decided by reading the item
10112    /// by its own id — a listing of the board can still be missing an item it holds, and
10113    /// reading that as *already gone* would leave behind the very item this was asked to
10114    /// take back.
10115    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
10116        let Some(item) = self.bound_item(id).await? else {
10117            return Ok(());
10118        };
10119        if item.content_kind == ContentKind::DraftIssue {
10120            return Err(SourceError::Refused {
10121                message: format!(
10122                    "GitHub item {} is a draft, and this source removes an item by deleting \
10123                     its issue; next: remove it from the board by hand",
10124                    id.0
10125                ),
10126            });
10127        }
10128        let data = self
10129            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10130            .await?;
10131        data.pointer("/deleteIssue/repository")
10132            .filter(|value| !value.is_null())
10133            .ok_or_else(|| SourceError::Malformed {
10134                message: "GitHub issue deletion returned no repository".into(),
10135            })?;
10136        self.forget(id)?;
10137        Ok(())
10138    }
10139
10140    /// The issue a comment call on `task` is about, or `None` when this board holds no such
10141    /// task.
10142    ///
10143    /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
10144    /// read of the task cannot disagree about which ids name one: a project or a document of
10145    /// this board is not a task here either.
10146    ///
10147    /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
10148    /// issues and a draft is not one. It is refused rather than answered with an empty page,
10149    /// which would read as a task nobody has commented on yet.
10150    async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
10151        let cached = self.resolved_cache()?.get(task).cloned();
10152        let Some(item) = (match cached {
10153            Some(item) => Some(item),
10154            None => self.item_by_id(task).await?,
10155        })
10156        .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10157            return Ok(None);
10158        };
10159        if item.content_kind == ContentKind::DraftIssue {
10160            return Err(self.draft_has_no_comments(task));
10161        }
10162        Ok(Some(item.id))
10163    }
10164
10165    /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
10166    /// issues, and a draft is not one.
10167    fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
10168        SourceError::Refused {
10169            message: format!(
10170                "task {} of source {} is a draft item on the board, and GitHub keeps \
10171                 comments on issues alone, so a draft has none to read or write; next: \
10172                 convert the draft to an issue on the board, then comment on the issue it \
10173                 becomes",
10174                task.0, self.name
10175            ),
10176        }
10177    }
10178
10179    /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
10180    /// request — or `None` when this board holds no task by that id.
10181    ///
10182    /// What `task show` and a comment listing read. A draft is a task with no comments, so it
10183    /// is answered with the draft and the refusal, at the price of the draft's own read.
10184    async fn issue_detail(
10185        &self,
10186        id: &NativeId,
10187        page: &PageRequest,
10188    ) -> Result<Option<TaskDetailRead>, SourceError> {
10189        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
10190        let asked = self
10191            .graphql(
10192                graphql::ISSUE_DETAIL,
10193                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
10194                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
10195                       "duplicates":true}),
10196            )
10197            .await;
10198        let data = match asked {
10199            Ok(data) => data,
10200            Err(error) if unresolvable_node(&error) => return Ok(None),
10201            Err(error) => return Err(error),
10202        };
10203        // `node` is null for an id that names nothing, and absent only from an answer this
10204        // source cannot read — never the same thing.
10205        let node = data.get("node").ok_or_else(|| SourceError::Malformed {
10206            message: format!("GitHub answered the read of {} with no node", id.0),
10207        })?;
10208        self.detail_of(id, node, true, after).await
10209    }
10210
10211    /// Several tasks, each with the first page of its comments when `comments` is set, read
10212    /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
10213    /// order.
10214    ///
10215    /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
10216    /// one item at a time, so that id is answered as missing and the others as themselves; any
10217    /// other refusal is every id of that batch's answer.
10218    async fn issue_details(
10219        &self,
10220        ids: &[NativeId],
10221        comments: Option<&PageRequest>,
10222    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10223        let mut read = Vec::with_capacity(ids.len());
10224        for batch in ids.chunks(DETAIL_BATCH) {
10225            match self
10226                .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
10227                .await
10228            {
10229                Ok(data) => {
10230                    for (slot, id) in batch.iter().enumerate() {
10231                        // Every alias asked for is answered, null for an id naming nothing;
10232                        // one missing is an answer this source cannot read.
10233                        let read_one = match data.get(format!("i{slot}")) {
10234                            Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
10235                            None => Err(SourceError::Malformed {
10236                                message: format!(
10237                                    "GitHub answered a batch read with no item for {}",
10238                                    id.0
10239                                ),
10240                            }),
10241                        };
10242                        read.push(read_one);
10243                    }
10244                }
10245                Err(error) if unresolvable_node(&error) => {
10246                    for id in batch {
10247                        read.push(match comments {
10248                            Some(page) => self.issue_detail(id, page).await,
10249                            None => self.task_read(id).await,
10250                        });
10251                    }
10252                }
10253                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
10254            }
10255        }
10256        read
10257    }
10258
10259    /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
10260    async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
10261        Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
10262            task,
10263            comments: None,
10264        }))
10265    }
10266
10267    /// What one node a detail read reached says: the task this board holds by `id`, with the
10268    /// page of comments the node carries when `commented` — or `None` for a node that is no
10269    /// task of this board.
10270    ///
10271    /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
10272    /// and an item this process created answers from this process's own record, which a node
10273    /// read taken moments after the write can still be behind.
10274    async fn detail_of(
10275        &self,
10276        id: &NativeId,
10277        node: &Value,
10278        commented: bool,
10279        after: Option<&str>,
10280    ) -> Result<Option<TaskDetailRead>, SourceError> {
10281        if node.is_null() {
10282            return Ok(None);
10283        }
10284        let draft = optional_str(node, "__typename")? == Some("DraftIssue");
10285        // An issue answered under one id is that id's, or the answer is not one this source
10286        // can report: reporting another issue's task and comments under the qualified id asked
10287        // for would be the one wrong answer here. A draft's own read checks the same.
10288        if !draft
10289            && optional_str(node, "__typename")? == Some("Issue")
10290            && required_str(node, "id")? != id.0
10291        {
10292            return Err(SourceError::Malformed {
10293                message: format!(
10294                    "GitHub answered the read of {} with issue {}",
10295                    id.0,
10296                    required_str(node, "id")?
10297                ),
10298            });
10299        }
10300        let item = if draft {
10301            self.draft_by_id(id).await?
10302        } else {
10303            self.resolve_issue(node).await?
10304        };
10305        // A read that carried the whole of what the issue is blocked by — a copy's — is kept
10306        // for this command, so the dependency read the copy makes next is answered from it.
10307        if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
10308            self.resolved_cache()?.insert(read.id.clone(), read.clone());
10309        }
10310        let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10311            return Ok(None);
10312        };
10313        let own = self.created()?.iter().find(|own| own.id == *id).cloned();
10314        let task = own.unwrap_or(item).task()?;
10315        let comments = match (commented, draft) {
10316            (false, _) => None,
10317            (true, true) => Some(Err(self.draft_has_no_comments(id))),
10318            (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
10319        };
10320        if commented && !draft && after.is_none() {
10321            self.expected.learn_comments(id, node);
10322        }
10323        Ok(Some(TaskDetailRead { task, comments }))
10324    }
10325
10326    /// Whether the comment `comment` is one of `issue`'s own.
10327    ///
10328    /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
10329    /// comment's id and nothing else: a comment id given against the wrong task would
10330    /// otherwise change a comment on some other issue entirely. An id that names nothing, or
10331    /// names something that is not an issue comment, is a comment this task does not have —
10332    /// which is what GitHub refusing to resolve it means too.
10333    async fn comment_is_on(
10334        &self,
10335        issue: &NativeId,
10336        comment: &NativeId,
10337    ) -> Result<bool, SourceError> {
10338        Ok(self.comment_on(issue, comment).await?.is_some())
10339    }
10340
10341    /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
10342    /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
10343    async fn comment_on(
10344        &self,
10345        issue: &NativeId,
10346        comment: &NativeId,
10347    ) -> Result<Option<String>, SourceError> {
10348        // A write the store's cache expects knows which comments its issue held, and their
10349        // bodies, at the version it expects: what this read would answer.
10350        if let Some(primed) = self.expected.primed_comment(issue, comment) {
10351            return Ok(primed);
10352        }
10353        let asked = self
10354            .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
10355            .await;
10356        let data = match asked {
10357            Ok(data) => data,
10358            Err(error) if unresolvable_node(&error) => return Ok(None),
10359            Err(error) => return Err(error),
10360        };
10361        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
10362            return Ok(None);
10363        };
10364        if optional_str(node, "__typename")? != Some("IssueComment") {
10365            return Ok(None);
10366        }
10367        let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
10368            message: format!("GitHub issue comment {} names no issue", comment.0),
10369        })?;
10370        if required_str(on, "id")? != issue.0 {
10371            return Ok(None);
10372        }
10373        Ok(Some(required_str(node, "body")?.to_owned()))
10374    }
10375
10376    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
10377    async fn partition_edges(
10378        &self,
10379        near_kind: BoardKind,
10380        near_content: ContentKind,
10381        carried: Option<&[Value]>,
10382        depends_on: &[DependencyEdge],
10383    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
10384        let mut native = Vec::new();
10385        let mut fallback = Vec::new();
10386        let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
10387            .iter()
10388            .map(|edge| {
10389                let same_source = edge
10390                    .to
10391                    .source()
10392                    .is_none_or(|source| source == self.name.as_str());
10393                // A qualified id's source segment runs to its *first* colon — `GlobalId` and
10394                // `DependencyEndpoint::source` both read it that way — and a native id may hold
10395                // colons of its own, so the far end is everything after that one separator.
10396                // Splitting at the last would truncate `work:urn:task:7` to `7`.
10397                let far_id = if edge.to.is_qualified() {
10398                    edge.to
10399                        .id()
10400                        .split_once(':')
10401                        .map_or(edge.to.id(), |(_, native)| native)
10402                } else {
10403                    edge.to.id()
10404                };
10405                // One that already blocks the near issue was answered by that issue's own
10406                // read, which carried each of its blockers' kinds — an issue every one — so it
10407                // is not read again.
10408                let blocking = carried.and_then(|nodes| {
10409                    nodes
10410                        .iter()
10411                        .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
10412                });
10413                (edge, far_id, same_source, blocking)
10414            })
10415            .collect();
10416        // Every other same-source far end is read by its own id, exactly as the item it is a
10417        // far end of is: whether this board holds it is that read's answer, never a listing's.
10418        // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
10419        let mut unread: Vec<NativeId> = Vec::new();
10420        for (_, far_id, same_source, blocking) in &far_ends {
10421            let id = NativeId((*far_id).to_owned());
10422            if *same_source && blocking.is_none() && !unread.contains(&id) {
10423                unread.push(id);
10424            }
10425        }
10426        let read: BTreeMap<NativeId, Option<Resolved>> = unread
10427            .iter()
10428            .cloned()
10429            .zip(self.items_by_ids(&unread).await?)
10430            .collect();
10431        for (edge, far_id, same_source, blocking) in far_ends {
10432            let far = match (same_source, blocking) {
10433                (false, _) => None,
10434                (true, Some(node)) => Some(FarEnd {
10435                    kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
10436                        BoardKind::Document
10437                    } else {
10438                        BoardKind::Work(related_kind(node)?)
10439                    },
10440                    content_kind: ContentKind::Issue,
10441                }),
10442                (true, None) => {
10443                    let read = read
10444                        .get(&NativeId(far_id.to_owned()))
10445                        .cloned()
10446                        .flatten()
10447                        .ok_or_else(|| SourceError::Refused {
10448                            message: format!("GitHub dependency item {far_id} was not found"),
10449                        })?;
10450                    Some(FarEnd {
10451                        kind: read.kind,
10452                        content_kind: read.content_kind,
10453                    })
10454                }
10455            };
10456            let far = far.as_ref();
10457            // The caller says which kind the far end is, and this board holds the far end
10458            // itself, so a disagreement is settled here rather than stored: recorded, the
10459            // wrong kind would read back as a cross-level edge that never existed; written
10460            // natively, it would name a relationship of a different level than the caller
10461            // asked for.
10462            //
10463            // A far end this board holds as a *document* fails the same comparison and is
10464            // refused by the same sentence: `ItemKind` has no document variant because
10465            // nothing may point at one, so no caller can name it correctly and the refusal
10466            // is the only honest answer.
10467            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
10468                return Err(SourceError::Refused {
10469                    message: format!(
10470                        "GitHub dependency item {far_id} is a {} of this board, and this item \
10471                         names it as a {}; record the kind it is",
10472                        disagreeing.kind.describes(),
10473                        edge.to.kind.marker()
10474                    ),
10475                });
10476            }
10477            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
10478            // however the far end is spelled — and one classified native here would be
10479            // written nowhere at all, because a draft's native reconciliation never runs.
10480            let native_here = near_content == ContentKind::Issue
10481                && far.is_some_and(|far| {
10482                    far.content_kind == ContentKind::Issue
10483                        && BoardKind::Work(edge.to.kind) == near_kind
10484                });
10485            if native_here {
10486                native.push(far_id.to_owned());
10487            } else {
10488                fallback.push(edge.clone());
10489            }
10490        }
10491        Ok((native, fallback))
10492    }
10493
10494    async fn update_existing(
10495        &self,
10496        item: &Resolved,
10497        incoming: &Incoming<'_>,
10498        body: &Option<String>,
10499        status_target: Option<&StatusTarget>,
10500    ) -> Result<(), SourceError> {
10501        let title = incoming.written_title();
10502        // A terminal status closes the issue here, in the same mutation as its body: its board
10503        // option was selected before this, so a close never lands on an item whose board cannot
10504        // show it.
10505        let mut fields = match item.content_kind {
10506            ContentKind::DraftIssue => json!({"title":title,"body":body}),
10507            ContentKind::Issue => json!({"title":title,"body":body,
10508                                         "stateInput":state_input(status_target)}),
10509        };
10510        // Only the members whose value differs from the item as this write read it are sent,
10511        // and nothing at all when none does — whether or not the store's cache expects a
10512        // version of it. A member left out is one GitHub leaves as it is, so a person's edit
10513        // of it since that read is kept rather than overwritten with what it already held,
10514        // and a re-copy of an unchanged item costs no mutation.
10515        if let Some(members) = fields.as_object_mut() {
10516            if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
10517                members.remove("title");
10518            }
10519            let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
10520                (Some(Value::String(sent)), Some(held)) => sent == held,
10521                (Some(Value::Null), held) => held.is_none_or(str::is_empty),
10522                _ => false,
10523            };
10524            if body_unchanged {
10525                members.remove("body");
10526            }
10527            // An issue already in the state the target asks for keeps it: an open one stays
10528            // open, and a closed one whose closure reads as the target's category keeps its
10529            // reason — a duplicate's says more than this write would.
10530            let unchanged_state = match status_target {
10531                Some(StatusTarget::Terminal(_, reason)) => {
10532                    item.closed
10533                        && closed_category(item.reason.as_deref())
10534                            == closed_category(Some(reason.reason()))
10535                }
10536                Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
10537                None => true,
10538            };
10539            if unchanged_state {
10540                members.remove("stateInput");
10541            }
10542            if members.is_empty() {
10543                return Ok(());
10544            }
10545        }
10546        self.update_content(item.content_kind, &item.id, fields)
10547            .await
10548    }
10549
10550    /// Update one board item's content with exactly `fields` beside its id, through the
10551    /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
10552    /// a draft.
10553    ///
10554    /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
10555    /// is what lets a narrow write carry the one thing it changes and nothing else.
10556    async fn update_content(
10557        &self,
10558        kind: ContentKind,
10559        id: &NativeId,
10560        fields: Value,
10561    ) -> Result<(), SourceError> {
10562        let (operation, id_key, pointer) = match kind {
10563            ContentKind::DraftIssue => (
10564                graphql::UPDATE_DRAFT,
10565                "draftIssueId",
10566                "/updateProjectV2DraftIssue/draftIssue",
10567            ),
10568            ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
10569        };
10570        let mut input = fields;
10571        input[id_key] = json!(id.0);
10572        let data = self.graphql(operation, json!({"input":input})).await?;
10573        let returned = data
10574            .pointer(pointer)
10575            .ok_or_else(|| SourceError::Malformed {
10576                message: "GitHub item update returned no item".into(),
10577            })?;
10578        if required_str(returned, "id")? != id.0 {
10579            return Err(SourceError::Malformed {
10580                message: "GitHub item update returned the wrong item".into(),
10581            });
10582        }
10583        Ok(())
10584    }
10585
10586    /// Creates one issue, files it on the board, and reports what a read of it would say:
10587    /// its content id, its board item id, and the web address GitHub gave it.
10588    ///
10589    /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
10590    /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
10591    /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
10592    /// board item, and the `addProjectV2ItemById` that then had to follow was refused
10593    /// "Content already exists in this project". A terminal status is not written here:
10594    /// `finish_write` selects its option first and closes the issue after, so a close never
10595    /// lands on an item whose board cannot show it.
10596    ///
10597    /// The address and the number come back here because this is the only place either is
10598    /// known before GitHub's own board read catches up — an item this run created answers
10599    /// the reads that follow it out of the record below, and one remembered without them
10600    /// would report no location and no key for the rest of the run.
10601    async fn create_and_file_issue(
10602        &self,
10603        board_id: &str,
10604        repository: &RepositoryTarget,
10605        incoming: &Incoming<'_>,
10606        body: &Option<String>,
10607    ) -> Result<Landed, SourceError> {
10608        let repository_id = self.repository_id(repository, incoming).await?;
10609        let data = self
10610            .graphql(
10611                graphql::CREATE_ISSUE,
10612                json!({"input":{
10613                    "repositoryId":repository_id,"title":incoming.written_title(),"body":body
10614                }}),
10615            )
10616            .await?;
10617        let created = data
10618            .pointer("/createIssue/issue")
10619            .filter(|value| !value.is_null())
10620            .ok_or_else(|| SourceError::Malformed {
10621                message: "GitHub issue creation returned no issue".into(),
10622            })?;
10623        let content_id = NativeId(required_str(created, "id")?.to_owned());
10624        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
10625        // a response without it is not worth failing a landed write over — the item simply
10626        // reports no location until the board read catches up, which is what it did before.
10627        let url = optional_str(created, "url")?.map(str::to_owned);
10628        // The issue exists from here on, so an unreadable number and a refused board
10629        // filing below each try, best effort, to take it back: an issue in the repository
10630        // that is on no board is an item nobody asked for and nothing here would find again.
10631        //
10632        // Its number is optional on the same terms its address is — a landed write is not
10633        // worth failing over a member that came back missing, and such an item reports no
10634        // handle until a board read catches up. A number that is *present* and is not an
10635        // unsigned integer is still a response this source cannot read.
10636        let number = match created_issue_number(created) {
10637            Ok(number) => number,
10638            Err(error) => {
10639                let _ = self.delete_issue(&content_id).await;
10640                return Err(error);
10641            }
10642        };
10643        let added = match self
10644            .graphql(
10645                graphql::ADD_TO_BOARD,
10646                json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
10647            )
10648            .await
10649        {
10650            Ok(added) => added,
10651            Err(error) => {
10652                let _ = self.delete_issue(&content_id).await;
10653                return Err(error);
10654            }
10655        };
10656        let item = added
10657            .pointer("/addProjectV2ItemById/item")
10658            .filter(|value| !value.is_null())
10659            .ok_or_else(|| SourceError::Malformed {
10660                message: "GitHub board addition returned no project item".into(),
10661            })?;
10662        self.note_written(&content_id, url.as_deref());
10663        Ok(Landed {
10664            content_id,
10665            item_id: Some(required_str(item, "id")?.to_owned()),
10666            url,
10667            number,
10668        })
10669    }
10670
10671    /// Move one issue under the project it now belongs to, or out of the one it left.
10672    async fn reparent(
10673        &self,
10674        held: Option<NativeId>,
10675        child: &NativeId,
10676        wanted: Option<&NativeId>,
10677    ) -> Result<(), SourceError> {
10678        if held.as_ref() == wanted {
10679            return Ok(());
10680        }
10681        if let Some(held) = &held {
10682            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
10683                .await?;
10684        }
10685        if let Some(wanted) = wanted {
10686            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
10687                .await?;
10688        }
10689        Ok(())
10690    }
10691
10692    async fn sub_issue(
10693        &self,
10694        operation: &str,
10695        parent: &NativeId,
10696        child: &NativeId,
10697        root: &str,
10698    ) -> Result<(), SourceError> {
10699        let data = self
10700            .graphql(
10701                operation,
10702                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
10703            )
10704            .await?;
10705        let issue =
10706            data.pointer(&format!("/{root}/issue"))
10707                .ok_or_else(|| SourceError::Malformed {
10708                    message: "GitHub sub-issue update returned no issue".into(),
10709                })?;
10710        let sub =
10711            data.pointer(&format!("/{root}/subIssue"))
10712                .ok_or_else(|| SourceError::Malformed {
10713                    message: "GitHub sub-issue update returned no sub-issue".into(),
10714                })?;
10715        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
10716            return Err(SourceError::Malformed {
10717                message: "GitHub sub-issue update returned the wrong issues".into(),
10718            });
10719        }
10720        Ok(())
10721    }
10722
10723    /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
10724    /// whether there was one.
10725    ///
10726    /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
10727    /// relationships are not read: there is nothing a read of them could find.
10728    async fn reconcile_blocked_by(
10729        &self,
10730        content_id: &NativeId,
10731        native: &[String],
10732        issue: Issue<'_>,
10733    ) -> Result<bool, SourceError> {
10734        let current = match issue {
10735            Issue::Created => Vec::new(),
10736            Issue::Existing(Some(held)) => held
10737                .iter()
10738                .map(|far| required_str(far, "id").map(str::to_owned))
10739                .collect::<Result<Vec<_>, _>>()?,
10740            Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
10741        };
10742        let mut changed = false;
10743        for (operation, far_id) in current
10744            .iter()
10745            .filter(|id| !native.contains(id))
10746            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
10747            .chain(
10748                native
10749                    .iter()
10750                    .filter(|id| !current.contains(id))
10751                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
10752            )
10753        {
10754            let data = self
10755                .graphql(
10756                    operation,
10757                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
10758                )
10759                .await?;
10760            let root = if operation == graphql::ADD_BLOCKED_BY {
10761                "addBlockedBy"
10762            } else {
10763                "removeBlockedBy"
10764            };
10765            let issue =
10766                data.pointer(&format!("/{root}/issue"))
10767                    .ok_or_else(|| SourceError::Malformed {
10768                        message: "GitHub dependency update returned no issue".into(),
10769                    })?;
10770            let blocker = data
10771                .pointer(&format!("/{root}/blockingIssue"))
10772                .ok_or_else(|| SourceError::Malformed {
10773                    message: "GitHub dependency update returned no blocking issue".into(),
10774                })?;
10775            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
10776            {
10777                return Err(SourceError::Malformed {
10778                    message: "GitHub dependency update returned the wrong issues".into(),
10779                });
10780            }
10781            changed = true;
10782        }
10783        Ok(changed)
10784    }
10785}
10786
10787/// The board field values one write sends, in the order GitHub runs them, and the fields it
10788/// clears after them: the origin field when it moves, the `Status` option `column` names, the
10789/// priority, then each projected field.
10790fn field_writes<'a>(
10791    existing: Option<&Resolved>,
10792    origin_field: Option<&str>,
10793    origin: &str,
10794    column: Option<(String, String)>,
10795    priority: Option<&'a PriorityWrite>,
10796    projections: &'a [ProjectionWrite],
10797) -> (Vec<(String, Value)>, Vec<&'a str>) {
10798    let mut fields = Vec::new();
10799    if let Some(field_id) = origin_field
10800        && existing.map_or(!origin.is_empty(), |item| {
10801            item.origin.as_deref().unwrap_or("") != origin
10802        })
10803    {
10804        fields.push((field_id.to_owned(), json!({"text":origin})));
10805    }
10806    if let Some((field_id, option_id)) = column {
10807        fields.push((field_id, json!({"singleSelectOptionId":option_id})));
10808    }
10809    let mut clears = Vec::new();
10810    match priority {
10811        Some(PriorityWrite::Select { field, option }) => {
10812            fields.push((field.clone(), json!({"singleSelectOptionId":option})));
10813        }
10814        Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
10815        None => {}
10816    }
10817    for projection in projections {
10818        projection.join(&mut fields, &mut clears);
10819    }
10820    (fields, clears)
10821}
10822
10823/// Everything one write decided before sending anything: see
10824/// [`GitHubProjectsSource::prepare_write`].
10825struct Prepared {
10826    /// What the write lands on.
10827    destination: WriteDestination,
10828    board: BoardFields,
10829    status_target: Option<StatusTarget>,
10830    /// The `Status` field, the option and the option's name the status lands on.
10831    // 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.
10832    column: Option<(String, String, String)>,
10833    priority_write: Option<PriorityWrite>,
10834    projections: Vec<ProjectionWrite>,
10835    projected: BTreeMap<String, String>,
10836    content_kind: ContentKind,
10837    own_repository: Option<Repository>,
10838    /// The far ends of the item's native blocked-by relationships.
10839    // 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.
10840    native: Vec<String>,
10841    slot: BTreeMap<String, Value>,
10842    content: Option<String>,
10843    body: Option<String>,
10844    /// The copy origin the item records, empty for none.
10845    // 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.
10846    origin: String,
10847    origin_field: Option<String>,
10848}
10849
10850/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
10851/// back — each issue of `left` it created that is still in the repository, which its caller,
10852/// never handed their ids, could not name.
10853fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
10854    if left.is_empty() {
10855        return error;
10856    }
10857    SourceError::Refused {
10858        message: format!(
10859            "{error}; GitHub then refused to take back {}, which this write created and which \
10860             are still in the repository; next: delete {} before writing again",
10861            left.join(", "),
10862            if left.len() == 1 { "it" } else { "them" }
10863        ),
10864    }
10865}
10866
10867/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
10868enum WriteDestination {
10869    /// The item as it stood, for an update.
10870    Existing(Box<Resolved>),
10871    /// Where a new item is created.
10872    New(RepositoryTarget),
10873}
10874
10875impl WriteDestination {
10876    /// The item an update writes over, as it stood; `None` for a create.
10877    fn existing(&self) -> Option<&Resolved> {
10878        match self {
10879            Self::Existing(item) => Some(item),
10880            Self::New(_) => None,
10881        }
10882    }
10883}
10884
10885/// What a copy has held back of the items it created; see
10886/// [`GitHubProjectsSource::hold_back`].
10887#[derive(Debug, Default)]
10888enum Deferred {
10889    /// Writes are being held back, and nothing has been held yet.
10890    #[default]
10891    Nothing,
10892    /// The items held back, every one of them on `board`.
10893    Held {
10894        /// The board every one of them is on.
10895        board: BoardId,
10896        /// Each created item, in the order it was created.
10897        created: Vec<HeldBack>,
10898    },
10899}
10900
10901impl Deferred {
10902    /// Whether `content` is one of the items held back.
10903    fn holds(&self, content: &NativeId) -> bool {
10904        match self {
10905            Self::Nothing => false,
10906            Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
10907        }
10908    }
10909}
10910
10911/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
10912#[derive(Debug)]
10913struct HeldBack {
10914    /// The issue.
10915    content: NativeId,
10916    /// Its board field values other than its `Status`, in the order they are written.
10917    fields: Vec<(String, Value)>,
10918    /// The `Status` field, and the option and its name that the item's status lands on.
10919    // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
10920    status: Option<(String, String, String)>,
10921    /// The fields it clears after those are written.
10922    clears: Vec<String>,
10923    /// The reason it is closed for, once its option is selected; `None` leaves it open.
10924    close: Option<ClosedState>,
10925    /// The issues it is blocked by.
10926    edges: Vec<NativeId>,
10927}
10928
10929/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
10930/// no GitHub type has a field of this name.
10931const ANSWER_ERRORS: &str = "onetaskgraphErrors";
10932
10933/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
10934/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
10935///
10936/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
10937/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
10938/// created named in the data beside it — the ids a copy needs to take them back.
10939fn answer_with_errors(body: &str) -> Option<Value> {
10940    let parsed: Value = serde_json::from_str(body).ok()?;
10941    let errors = parsed
10942        .get("errors")?
10943        .as_array()
10944        .filter(|errors| !errors.is_empty())?;
10945    let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
10946    data[ANSWER_ERRORS] = Value::Array(errors.clone());
10947    Some(data)
10948}
10949
10950/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
10951/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
10952fn filed_input(
10953    repository: &str,
10954    board: &str,
10955    incoming: &Incoming<'_>,
10956    body: &Option<String>,
10957) -> Value {
10958    let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
10959                           "body":body,"projectV2Ids":[board]});
10960    if let Some(parent) = incoming.parent {
10961        input["parentIssueId"] = json!(parent.0);
10962    }
10963    input
10964}
10965
10966/// The name of option `option` of field `field` among a board's `fields`.
10967fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
10968    fields
10969        .get("nodes")?
10970        .as_array()?
10971        .iter()
10972        .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
10973        .get("options")?
10974        .as_array()?
10975        .iter()
10976        .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
10977        .get("name")?
10978        .as_str()
10979}
10980
10981/// What a write needs to know of one far end it names: which kind of item it is, and whether
10982/// it is an issue a native relationship can name.
10983struct FarEnd {
10984    kind: BoardKind,
10985    content_kind: ContentKind,
10986}
10987
10988/// Whether the issue one write reconciles was created by that write or was already there.
10989#[derive(Clone, Copy, PartialEq, Eq)]
10990enum Issue<'a> {
10991    /// Created by this write, so it holds no relationships yet.
10992    Created,
10993    /// On the board before this write, holding whatever relationships it holds — the far
10994    /// ends of its whole `blockedBy`, when the read that reached it carried them.
10995    Existing(Option<&'a [Value]>),
10996}
10997
10998/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
10999enum Reached {
11000    /// An issue this board holds, resolved into everything this source reports about it.
11001    Held(Box<Resolved>),
11002    /// Nothing this board holds: no such node, or a node on some other board.
11003    Nothing,
11004    /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
11005    /// again by [`GitHubProjectsSource::draft_by_id`].
11006    Draft,
11007}
11008
11009/// What GitHub says when a string is not a node id it can resolve.
11010///
11011/// Matched because it is the ordinary answer to a project selector naming a project by its
11012/// *name*, and reporting that as a failure would make naming one impossible. It is read
11013/// off the refusal GitHub sent, never guessed from the shape of the string: this source
11014/// does not define the syntax of a GitHub node id and would be wrong about it.
11015const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
11016
11017/// `error` with `note` added to the end of what it says, its kind and every other member
11018/// unchanged — so a caller still branches on the failure that happened, and reads beside it
11019/// what that failure left behind.
11020fn noting(error: SourceError, note: &str) -> SourceError {
11021    match error {
11022        SourceError::Config { message } => SourceError::Config {
11023            message: message + note,
11024        },
11025        SourceError::Auth { message } => SourceError::Auth {
11026            message: message + note,
11027        },
11028        SourceError::Refused { message } => SourceError::Refused {
11029            message: message + note,
11030        },
11031        SourceError::RateLimited {
11032            retry_after_seconds,
11033            message,
11034        } => SourceError::RateLimited {
11035            retry_after_seconds,
11036            message: Some(message.unwrap_or_default() + note),
11037        },
11038        SourceError::Unavailable { message } => SourceError::Unavailable {
11039            message: message + note,
11040        },
11041        SourceError::Malformed { message } => SourceError::Malformed {
11042            message: message + note,
11043        },
11044    }
11045}
11046
11047/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
11048/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
11049fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
11050    let mut variables = serde_json::Map::new();
11051    for slot in 0..COPY_DETAIL_BATCH {
11052        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11053        variables.insert(format!("id{slot}"), json!(id));
11054    }
11055    variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
11056    variables.insert("children".to_owned(), json!(children));
11057    variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
11058    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11059    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11060    variables.insert("duplicates".to_owned(), json!(true));
11061    Value::Object(variables)
11062}
11063
11064/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
11065/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
11066/// for them.
11067///
11068/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
11069/// is read again at no added price.
11070fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
11071    let mut variables = serde_json::Map::new();
11072    for slot in 0..DETAIL_BATCH {
11073        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11074        variables.insert(format!("id{slot}"), json!(id));
11075    }
11076    variables.insert(
11077        "first".to_owned(),
11078        json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
11079    );
11080    variables.insert("comments".to_owned(), json!(comments.is_some()));
11081    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11082    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11083    variables.insert("duplicates".to_owned(), json!(true));
11084    Value::Object(variables)
11085}
11086
11087/// Whether this refusal is GitHub saying the id names no node at all.
11088fn unresolvable_node(error: &SourceError) -> bool {
11089    matches!(error, SourceError::Refused { message }
11090        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
11091}
11092
11093/// One project name, as a search qualifier which filters on it at the server.
11094///
11095/// Quoted so the whole title is one phrase rather than a bag of words, with the two
11096/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
11097/// the way it documents. A title matched here is still compared for equality afterwards:
11098/// the qualifier narrows what the server sends, and this source decides what it names.
11099fn title_qualifier(name: &str) -> String {
11100    format!("in:title {}", quoted(name))
11101}
11102
11103/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
11104/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
11105/// it documents — so a value holding a qualifier's spelling is searched for rather than
11106/// obeyed.
11107fn quoted(value: &str) -> String {
11108    let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
11109    format!("\"{escaped}\"")
11110}
11111
11112/// The search qualifier for the issues updated at or after `since`.
11113///
11114/// Written to the second, rounded down, which can only widen what the search returns.
11115fn updated_qualifier(since: DateTime<Utc>) -> String {
11116    format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
11117}
11118
11119/// The search terms that narrow a board-scoped issue search to a task query's text and
11120/// metadata predicates, or `None` when it carries neither.
11121///
11122/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
11123/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
11124/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
11125/// matches each in any field the `in:` qualifier names, so a query naming a title search and
11126/// a metadata value searches both fields for both — wider than asked, never narrower, and
11127/// every candidate is confirmed in process afterwards.
11128///
11129/// **This narrows a text search, and that is this source's declared semantics.** GitHub
11130/// matches whole tokens where a substring rule would match inside a word, so an item holding
11131/// the text only inside a longer word is not returned. A text of nothing but whitespace
11132/// matches every item, so it narrows nothing and is not sent.
11133fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
11134    let text = query
11135        .text
11136        .as_ref()
11137        .filter(|text| !text.terms.trim().is_empty());
11138    if text.is_none() && query.metadata.is_empty() {
11139        return None;
11140    }
11141    let (title, body) = match text.map(|text| text.fields) {
11142        None => (false, true),
11143        Some(TextFields::Title) => (true, !query.metadata.is_empty()),
11144        Some(TextFields::Content) => (false, true),
11145        Some(TextFields::TitleOrContent) => (true, true),
11146    };
11147    let fields = match (title, body) {
11148        (true, true) => "in:title,body",
11149        (true, false) => "in:title",
11150        _ => "in:body",
11151    };
11152    let phrases = text
11153        .map(|text| text.terms.clone())
11154        .into_iter()
11155        .chain(
11156            query
11157                .metadata
11158                .iter()
11159                .map(|wanted| as_stored(wanted.value())),
11160        )
11161        .map(|phrase| quoted(&phrase))
11162        .collect::<Vec<_>>();
11163    Some(format!("{fields} {}", phrases.join(" ")))
11164}
11165
11166/// The search terms that narrow a board-scoped issue search to a project or document query's
11167/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
11168/// carrying that text alone is sent as by [`narrowing_qualifiers`].
11169fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
11170    narrowing_qualifiers(&TaskQuery {
11171        text: text.cloned(),
11172        ..TaskQuery::default()
11173    })
11174}
11175
11176/// Refuses a project or document query's text GitHub's issue search cannot find, before
11177/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
11178/// query's.
11179fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
11180    refuse_unsearchable(&TaskQuery {
11181        text: text.cloned(),
11182        ..TaskQuery::default()
11183    })
11184}
11185
11186/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
11187/// before anything is asked of GitHub.
11188///
11189/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
11190/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
11191/// left out, the search is every issue of the board. So this source says it cannot answer
11192/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
11193/// nothing GitHub could search for, and keeps the board read it always had.
11194fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
11195    const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
11196                       letter or digit with a bounded query";
11197    if let Some(text) = &query.text
11198        && !text.terms.trim().is_empty()
11199        && !has_words(&text.terms)
11200    {
11201        return Err(SourceError::Refused {
11202            message: format!(
11203                "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
11204                text.terms
11205            ),
11206        });
11207    }
11208    if let Some(wanted) = query
11209        .metadata
11210        .iter()
11211        .find(|wanted| !has_words(wanted.value()))
11212    {
11213        return Err(SourceError::Refused {
11214            message: format!(
11215                "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
11216                wanted.value(),
11217                std::iter::once(wanted.key())
11218                    .chain(wanted.path().iter().map(String::as_str))
11219                    .collect::<Vec<_>>()
11220                    .join("/"),
11221            ),
11222        });
11223    }
11224    Ok(())
11225}
11226
11227/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
11228fn has_words(phrase: &str) -> bool {
11229    phrase.chars().any(char::is_alphanumeric)
11230}
11231
11232/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
11233///
11234/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
11235/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
11236/// which GitHub's word match would read as different words.
11237fn as_stored(value: &str) -> String {
11238    let encoded = Value::String(value.to_owned()).to_string();
11239    encoded[1..encoded.len() - 1].to_owned()
11240}
11241
11242/// The one narrower question a task query carrying a text, metadata or origin predicate is
11243/// sent as.
11244enum Narrowing {
11245    /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
11246    Origin(String),
11247    /// The board-scoped issue search narrowed by these qualifiers.
11248    Search(String),
11249}
11250
11251impl Narrowing {
11252    /// What this question is remembered under for the length of one command.
11253    fn key(&self) -> String {
11254        match self {
11255            Self::Origin(origin) => format!("origin {origin}"),
11256            Self::Search(also) => format!("search {also}"),
11257        }
11258    }
11259}
11260
11261/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
11262enum Resumed {
11263    /// It reported another page, which starts after this cursor.
11264    More(String),
11265    /// It has ended. Sending this cursor again — the page's own end when it had one, and
11266    /// otherwise the cursor it was reached from — answers an empty page, so the one document
11267    /// can go on walking the other connection.
11268    Ended(Option<String>),
11269}
11270
11271impl Resumed {
11272    /// Whether the connection has another page.
11273    const fn has_more(&self) -> bool {
11274        matches!(self, Self::More(_))
11275    }
11276
11277    /// The cursor to send this connection next.
11278    fn cursor(self) -> Option<String> {
11279        match self {
11280            Self::More(next) => Some(next),
11281            Self::Ended(last) => last,
11282        }
11283    }
11284}
11285
11286/// Where `connection`, reached from `after`, resumes — refused when it reports another page
11287/// with no cursor to it, or from a cursor that does not advance.
11288fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
11289    let info = connection
11290        .get("pageInfo")
11291        .ok_or_else(|| SourceError::Malformed {
11292            message: "GitHub connection has no pageInfo".into(),
11293        })?;
11294    let end = optional_str(info, "endCursor")?;
11295    if required_bool(info, "hasNextPage")? {
11296        let next = end.ok_or_else(|| SourceError::Malformed {
11297            message: "GitHub connection reports another page and no endCursor".into(),
11298        })?;
11299        validate_cursor_progress(after, next)?;
11300        return Ok(Resumed::More(next.to_owned()));
11301    }
11302    Ok(Resumed::Ended(
11303        end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
11304    ))
11305}
11306
11307/// The board, and every item on it this source reports.
11308#[derive(Clone)]
11309struct Board {
11310    id: String,
11311    fields: Value,
11312    items: Vec<Resolved>,
11313}
11314
11315/// What a write needs of the board and nothing more: its node id and its field
11316/// definitions, in the shape a read of the board's own `fields` gives them.
11317///
11318/// Deliberately no items. A write decides which item it writes, which parent it files
11319/// under and which far ends it names by reading each of them by its own id; this is the
11320/// half of the board those reads cannot carry, and holding no item is what keeps it from
11321/// ever being asked whether an item is there.
11322#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
11323struct BoardFields {
11324    id: BoardId,
11325    fields: Value,
11326}
11327
11328/// A board's node id: what a field write and `addProjectV2ItemById` address.
11329///
11330/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
11331/// refused where it is read, and one an item names blank is read as not named at all.
11332#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
11333#[serde(try_from = "String")]
11334struct BoardId(String);
11335
11336impl TryFrom<String> for BoardId {
11337    type Error = String;
11338
11339    /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
11340    fn try_from(id: String) -> Result<Self, Self::Error> {
11341        Self::parse(&id).map_err(|error| error.to_string())
11342    }
11343}
11344
11345/// Where one write left its item, for the record the rest of the command reads it out of.
11346///
11347/// A named record rather than a tuple because the update arm and the create arm each fill
11348/// all four, and two `Option`s of different meaning side by side in a tuple are two
11349/// positions a reader has to count.
11350struct Landed {
11351    /// The issue's own node id, which is the [`NativeId`] this source reports.
11352    content_id: NativeId,
11353    /// The board item's id, which is what a field write addresses — `None` for an issue its
11354    /// create filed on the board itself, whose board item GitHub adds after answering and
11355    /// [`TaskSource::finish_writes`] reads.
11356    // 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.
11357    item_id: Option<String>,
11358    /// The web address GitHub gave the issue, when it gave one.
11359    // 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.
11360    url: Option<String>,
11361    /// The issue's number on its repository, when GitHub reported one.
11362    number: Option<u64>,
11363}
11364
11365impl BoardId {
11366    fn parse(id: &str) -> Result<Self, SourceError> {
11367        if id.trim().is_empty() {
11368            return Err(SourceError::Malformed {
11369                message: "GitHub named a board with a blank node id".into(),
11370            });
11371        }
11372        Ok(Self(id.to_owned()))
11373    }
11374
11375    fn as_str(&self) -> &str {
11376        &self.0
11377    }
11378}
11379
11380impl Board {
11381    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
11382        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
11383        let nodes = fields
11384            .get("nodes")
11385            .and_then(Value::as_array)
11386            .ok_or_else(|| SourceError::Malformed {
11387                message: "GitHub project fields.nodes is not an array".into(),
11388            })?;
11389        Ok(nodes
11390            .iter()
11391            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
11392    }
11393}
11394
11395/// One board item, resolved into everything this source reports about it.
11396///
11397/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
11398/// [`TaskSource::prime_write`] takes back — see [`expected`].
11399#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
11400struct Resolved {
11401    item_id: String,
11402    id: NativeId,
11403    content_kind: ContentKind,
11404    kind: BoardKind,
11405    title: String,
11406    body: Option<String>,
11407    /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
11408    /// that changes the slot alone has to keep byte for byte outside it.
11409    raw_body: Option<String>,
11410    status: Status,
11411    /// The name of the board `Status` option this item sits in, as the board spells it.
11412    option: Option<String>,
11413    /// What its `Priority` field says, read through this instance's mapping.
11414    priority: HeldPriority,
11415    /// Whether this item's issue is closed. A draft has no such state and is never closed.
11416    closed: bool,
11417    /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
11418    /// issue, a draft, and an issue closed before GitHub recorded reasons.
11419    #[serde(default)]
11420    reason: Option<String>,
11421    /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
11422    delivers: Vec<TaskRef>,
11423    /// Every task that delivers this one, read out of its slot. Empty for anything not a
11424    /// task.
11425    delivered_by: Vec<TaskRef>,
11426    labels: Vec<Label>,
11427    parent: Option<NativeId>,
11428    // 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.
11429    origin: Option<String>,
11430    /// What each board text field this instance projects metadata onto holds for this item,
11431    /// by field name — only the fields holding a value, an empty text included. What a write compares the
11432    /// item's metadata against, so a field already holding its value is not written again;
11433    /// never read back as metadata, which the slot alone reports.
11434    projected: BTreeMap<String, String>,
11435    /// The issue's own number on its repository, as GitHub reports it.
11436    ///
11437    /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
11438    /// declares none, and a draft is not filed in a repository to be numbered by one — and
11439    /// an issue this run created whose creating mutation answered without one, which is a
11440    /// response GitHub's own schema says cannot happen and which a landed write is not
11441    /// worth failing over. An `Issue` read off the board always has one.
11442    number: Option<u64>,
11443    url: Option<String>,
11444    created_at: Option<DateTime<Utc>>,
11445    updated_at: Option<DateTime<Utc>>,
11446    /// When the board item itself last changed — what a board field write moves — when the
11447    /// read that reached it said.
11448    item_updated_at: Option<DateTime<Utc>>,
11449    /// How many comments the issue holds, when the read that reached it said.
11450    comment_count: Option<u64>,
11451    /// How many relationship events the issue's timeline holds — every dependency, as
11452    /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
11453    /// removed — when the read that reached it said.
11454    relationship_events: Option<u64>,
11455    /// The server's clock on the response that read it — its `Date` header — when the
11456    /// response carried one.
11457    served_at: Option<DateTime<Utc>>,
11458    /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
11459    /// its labels — when the read that reached it carried all three whole.
11460    #[serde(default)]
11461    facets: Option<change_token::FacetsDigest>,
11462    own_repository: Option<Repository>,
11463    repositories: Vec<Repository>,
11464    classification: Classification,
11465    slot: BTreeMap<String, Value>,
11466    /// The node id of the board this item sits on, when the read that reached it said.
11467    board_id: Option<String>,
11468    /// The definition of every board field this item holds a value of, in the shape a read
11469    /// of the board's own `fields` gives one.
11470    ///
11471    /// Only the fields this item has a value in: a field it holds nothing of is not here,
11472    /// which says nothing about whether the board has it.
11473    fields: Vec<Value>,
11474    /// Every field the board this item sits on defines, as its own read of the board's
11475    /// `fields` gives them — when the read that reached the item carried them, which a read
11476    /// of it by its own id does. What a write of it needs of the board, then, needs no read
11477    /// of the board.
11478    board_fields: Option<Value>,
11479    /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
11480    /// selects one — when the read that reached it carried the connection to its end, which a
11481    /// read of it by its own id does for any issue blocked by no more than a page. What a
11482    /// write reconciles that relationship against, and what a read of its forward edges in
11483    /// the same command answers with.
11484    blocked_by: Option<Vec<Value>>,
11485}
11486
11487impl Resolved {
11488    /// The board this item's own read names it on, when that read named one this source can
11489    /// address.
11490    fn named_board(&self) -> Option<BoardId> {
11491        self.board_id
11492            .as_deref()
11493            .and_then(|id| BoardId::parse(id).ok())
11494    }
11495
11496    /// The board's id and every field it defines, when the read that reached this item
11497    /// carried both — which a read of it by its own id does.
11498    fn carried_board(&self) -> Option<BoardFields> {
11499        Some(BoardFields {
11500            id: self.named_board()?,
11501            fields: self.board_fields.clone()?,
11502        })
11503    }
11504
11505    /// Whether this item holds a value of the board field called `name`, and so carries
11506    /// that field's definition. `false` says nothing about whether the board has the field.
11507    fn defines(&self, name: &str) -> bool {
11508        self.fields
11509            .iter()
11510            .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
11511    }
11512
11513    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
11514    /// in a field of its own, and none of the five keys that are only an encoding.
11515    ///
11516    /// The two delivery keys are left out for every kind, not only for a task: they are
11517    /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
11518    /// document carrying one holds nothing a caller's own metadata could mean by it.
11519    fn metadata(&self) -> BTreeMap<String, Value> {
11520        let mut metadata = self.slot.clone();
11521        metadata.remove(Repository::METADATA_KEY);
11522        metadata.remove(DependencyEdge::RECORDED_KEY);
11523        metadata.remove(ItemKind::METADATA_KEY);
11524        metadata.remove(TaskRef::DELIVERS_KEY);
11525        metadata.remove(TaskRef::DELIVERED_BY_KEY);
11526        metadata.remove(Classification::METADATA_KEY);
11527        // The board field is the origin, and the body's copy of it is only a mirror for the
11528        // issue search to find: an item whose field holds none has none, whatever its body
11529        // says, so no reader ever sees two answers.
11530        metadata.remove(ORIGIN_KEY);
11531        if let Some(origin) = &self.origin {
11532            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
11533        }
11534        metadata
11535    }
11536
11537    /// Where this item is, as a link a reader can open.
11538    ///
11539    /// A board is a hosted place and every issue on it has a web address, so that address
11540    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
11541    /// of place it is, so a reader knows to open it rather than to read a file out. It
11542    /// does not replace or derive from `url`: the field goes on reporting exactly what it
11543    /// reported before, and this says what that address *is*.
11544    ///
11545    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
11546    /// rather than a third variant, which is the contract's "the source did not say". An
11547    /// issue this run created is not one of those: its address comes back from the
11548    /// creating mutation, so it is somewhere a reader can open from the moment it exists
11549    /// rather than from whenever the board read catches up.
11550    fn location(&self) -> Option<Location> {
11551        self.url.clone().map(Location::Url)
11552    }
11553
11554    /// The short handle this board's backend shows people for a task: the issue's number
11555    /// alone, as a decimal string.
11556    ///
11557    /// The number alone rather than `owner/repo#1043`, because that is the contract's
11558    /// value for this backend. A draft has no number and so no handle, which is the
11559    /// contract's *absent* rather than a handle of some other shape — and the native
11560    /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
11561    /// derives from.
11562    fn key(&self) -> Option<String> {
11563        self.number.map(|number| number.to_string())
11564    }
11565
11566    /// This item's change token — see [`change_token`] — or `None` when the read that
11567    /// reached it did not carry all six of its parts, which a draft never does, did not say
11568    /// when GitHub served it, or found it changed too recently to have settled.
11569    fn version(&self) -> Option<String> {
11570        match self.content_kind {
11571            ContentKind::Issue => {
11572                let (issue, item) = (self.updated_at?, self.item_updated_at?);
11573                let parts = change_token::Parts {
11574                    issue,
11575                    item,
11576                    comments: self.comment_count?,
11577                    relationships: self.relationship_events?,
11578                    facets: self.facets.clone()?,
11579                    repository: self.own_repository.clone()?,
11580                };
11581                change_token::settled(issue, item, self.served_at?)
11582                    .then(|| change_token::spell(&parts))
11583            }
11584            ContentKind::DraftIssue => None,
11585        }
11586    }
11587
11588    /// Whether its `Priority` field holds a value at all, mapped or not.
11589    fn holds_priority(&self) -> bool {
11590        self.priority != HeldPriority::Read(Priority::None)
11591    }
11592
11593    /// The task this item is.
11594    ///
11595    /// Fails for an item whose `Priority` field holds an option the mapping does not name:
11596    /// reading that as a level would be a guess, and reading it as `none` would let the next
11597    /// copy clear a priority a person set.
11598    fn task(&self) -> Result<Task, SourceError> {
11599        let priority = match &self.priority {
11600            HeldPriority::Read(priority) => *priority,
11601            HeldPriority::Unmapped(option) => {
11602                return Err(SourceError::Malformed {
11603                    message: format!(
11604                        "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
11605                         this source's priority_mapping does not name, so its priority cannot be \
11606                         read; next: name {option:?} under priority_mapping, or move the item to \
11607                         a mapped option",
11608                        self.id,
11609                        self.number
11610                            .map(|number| format!(" (#{number})"))
11611                            .unwrap_or_default()
11612                    ),
11613                });
11614            }
11615        };
11616        Ok(Task {
11617            version: self.version(),
11618            id: self.id.clone(),
11619            key: self.key(),
11620            title: self.title.clone(),
11621            content: self.body.clone(),
11622            status: self.status.clone(),
11623            priority,
11624            labels: self.labels.clone(),
11625            project: self.parent.clone(),
11626            url: self.url.clone(),
11627            location: self.location(),
11628            created_at: self.created_at,
11629            updated_at: self.updated_at,
11630            metadata: self.metadata(),
11631            repositories: self.repositories.clone(),
11632            delivers: self.delivers.clone(),
11633            delivered_by: self.delivered_by.clone(),
11634            classification: self.classification,
11635        })
11636    }
11637
11638    fn project(&self) -> Project {
11639        Project {
11640            version: self.version(),
11641            id: self.id.clone(),
11642            title: self.title.clone(),
11643            content: self.body.clone(),
11644            status: self.status.clone(),
11645            labels: self.labels.clone(),
11646            url: self.url.clone(),
11647            location: self.location(),
11648            created_at: self.created_at,
11649            updated_at: self.updated_at,
11650            metadata: self.metadata(),
11651            repositories: self.repositories.clone(),
11652            classification: self.classification,
11653        }
11654    }
11655
11656    /// The same issue as a document: the project it is filed under, and no status and no
11657    /// dependencies, because a document is not work.
11658    fn document(&self) -> Document {
11659        Document {
11660            version: self.version(),
11661            id: self.id.clone(),
11662            title: self.title.clone(),
11663            content: self.body.clone(),
11664            project: self.parent.clone(),
11665            labels: self.labels.clone(),
11666            url: self.url.clone(),
11667            location: self.location(),
11668            created_at: self.created_at,
11669            updated_at: self.updated_at,
11670            metadata: self.metadata(),
11671            repositories: self.repositories.clone(),
11672            classification: self.classification,
11673        }
11674    }
11675}
11676
11677/// Where one targeted update moves an item's status, and which of its two halves move.
11678struct StatusMove {
11679    /// The board the item's `Status` field is on.
11680    board: BoardId,
11681    /// The `Status` field's id.
11682    field: String,
11683    /// The option's id.
11684    option: String,
11685    /// The option's name, as the board spells it.
11686    name: String,
11687    /// What the status asks of the issue's state.
11688    target: StatusTarget,
11689    /// The status the item reads as once it is there.
11690    landed: Status,
11691    /// Which of the status's two halves differ from what the item holds.
11692    moves: Moves,
11693}
11694
11695/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
11696/// closed state of its issue, or both. A status neither half of which differs is no move at all,
11697/// and is not a value of this type.
11698#[derive(Clone, Copy, PartialEq, Eq)]
11699enum Moves {
11700    /// The option alone.
11701    Option,
11702    /// The issue's state alone: open, closed, or closed with another reason.
11703    State,
11704    /// Both.
11705    Both,
11706}
11707
11708impl Moves {
11709    /// What differs, or `None` when nothing does.
11710    const fn of(option: bool, state: bool) -> Option<Self> {
11711        match (option, state) {
11712            (true, true) => Some(Self::Both),
11713            (true, false) => Some(Self::Option),
11714            (false, true) => Some(Self::State),
11715            (false, false) => None,
11716        }
11717    }
11718
11719    /// Whether the option moves.
11720    const fn option(self) -> bool {
11721        matches!(self, Self::Option | Self::Both)
11722    }
11723
11724    /// Whether the issue's state moves.
11725    const fn state(self) -> bool {
11726        matches!(self, Self::State | Self::Both)
11727    }
11728}
11729
11730/// What one write is, and the status that comes with being it.
11731///
11732/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
11733/// status and a task or a project always has one, so "a document carrying a status" and
11734/// "a task carrying none" are states a write cannot be in rather than states every use
11735/// site below has to defend against.
11736enum Written<'a> {
11737    /// A document, which is not work and so has no status at all.
11738    Document,
11739    /// A task or a project, and the status it is being written with.
11740    Work(ItemKind, &'a Status),
11741}
11742
11743impl Written<'_> {
11744    /// Which of the board's three kinds this write is.
11745    const fn kind(&self) -> BoardKind {
11746        match self {
11747            Self::Document => BoardKind::Document,
11748            Self::Work(kind, _) => BoardKind::Work(*kind),
11749        }
11750    }
11751
11752    /// The status this write carries. A document carries none, so a write of one says
11753    /// nothing about the issue's open or closed state and selects no board `Status`
11754    /// option.
11755    const fn status(&self) -> Option<&Status> {
11756        match self {
11757            Self::Document => None,
11758            Self::Work(_, status) => Some(status),
11759        }
11760    }
11761
11762    /// The status this write carries with the kind whose half of `status_mapping` it is
11763    /// written through.
11764    const fn work_status(&self) -> Option<(ItemKind, &Status)> {
11765        match self {
11766            Self::Document => None,
11767            Self::Work(kind, status) => Some((*kind, status)),
11768        }
11769    }
11770}
11771
11772/// The item being written, in the one shape all three write methods reach.
11773struct Incoming<'a> {
11774    written: Written<'a>,
11775    /// The title a person wrote. A document's goes onto the issue with
11776    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
11777    title: &'a str,
11778    content: Option<&'a str>,
11779    assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
11780    labels: &'a [Label],
11781    metadata: &'a BTreeMap<String, Value>,
11782    repositories: &'a [Repository],
11783    /// Recorded in the slot while private, and nowhere while public.
11784    classification: Classification,
11785    parent: Option<&'a NativeId>,
11786    /// [`Task::delivers`], already checked. Empty for a project or a document, which is
11787    /// what keeps either key out of their slot.
11788    delivers: &'a [TaskRef],
11789    /// [`Task::delivered_by`], already checked. Empty for a project or a document.
11790    delivered_by: &'a [TaskRef],
11791    /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
11792    /// project, a document, and every write to an instance with no `priority_mapping` —
11793    /// which is what keeps such a write's requests exactly what they were before.
11794    priority: Option<Priority>,
11795}
11796
11797/// What one write does to one projected metadata text field, by the field's id.
11798#[derive(Debug, Clone)]
11799enum ProjectionWrite {
11800    /// Write this text.
11801    Set {
11802        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.
11803        text: String,
11804    },
11805    /// Clear the field's value.
11806    Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
11807}
11808
11809impl ProjectionWrite {
11810    /// Add this write to a field write's list of values and list of clears.
11811    fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
11812        match self {
11813            Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
11814            Self::Clear { field } => clears.push(field),
11815        }
11816    }
11817}
11818
11819/// What one write does to an item's `Priority` field.
11820enum PriorityWrite {
11821    /// Select this option of this field.
11822    Select {
11823        /// The `Priority` field's id.
11824        field: String,
11825        /// The mapped option's id.
11826        option: String,
11827    },
11828    /// Clear the field's value, which is what `none` is.
11829    Clear {
11830        /// The `Priority` field's id.
11831        field: String,
11832    },
11833}
11834
11835impl Incoming<'_> {
11836    /// The title this write puts on the issue.
11837    fn written_title(&self) -> String {
11838        match self.written {
11839            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
11840            Written::Work(..) => self.title.to_owned(),
11841        }
11842    }
11843}
11844
11845#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
11846enum ContentKind {
11847    DraftIssue,
11848    Issue,
11849}
11850
11851/// What one board issue is: a document, or the work an [`ItemKind`] names.
11852///
11853/// A type of this source's own rather than an `ItemKind` with a third variant, because
11854/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
11855/// document — the contract keeps a document out of that enum deliberately. Holding the
11856/// board's three answers in one value is what makes every place that asks "which is this?"
11857/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
11858/// two thirds of the board.
11859#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
11860enum BoardKind {
11861    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
11862    Document,
11863    /// Every other issue, and every draft.
11864    Work(ItemKind),
11865}
11866
11867impl BoardKind {
11868    /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
11869    /// sub-issues and carrying the kind marker `marked` in its slot.
11870    ///
11871    /// The design prefix is read *first*, before either of the two rules that separate a
11872    /// project from a task. A document is not work whatever sub-issues it has and whatever
11873    /// marker it carries, and reading the prefix later would make a design issue with none of
11874    /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
11875    /// overrides it: an issue filed under a project is that project's task even when it has
11876    /// sub-issues of its own.
11877    fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
11878        if title.starts_with(DESIGN_TITLE_PREFIX) {
11879            Self::Document
11880        } else if filed {
11881            Self::Work(ItemKind::Task)
11882        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
11883            Self::Work(ItemKind::Project)
11884        } else {
11885            Self::Work(ItemKind::Task)
11886        }
11887    }
11888
11889    /// What the change token's facets call this kind.
11890    const fn facet(self) -> change_token::FacetKind {
11891        match self {
11892            Self::Document => change_token::FacetKind::Document,
11893            Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
11894            Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
11895        }
11896    }
11897
11898    /// Whose half of `status_mapping` an item of this kind reads its status through. A
11899    /// document has no status of its own, so the task half stands in for whatever the issue
11900    /// holds; nothing reports it.
11901    const fn status_kind(self) -> ItemKind {
11902        match self {
11903            Self::Document => ItemKind::Task,
11904            Self::Work(kind) => kind,
11905        }
11906    }
11907
11908    /// How a refusal names this kind to the person reading it.
11909    const fn describes(self) -> &'static str {
11910        match self {
11911            Self::Document => "document",
11912            Self::Work(kind) => kind.marker(),
11913        }
11914    }
11915}
11916
11917/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
11918///
11919/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
11920/// the shared cross-source journeys assert one answer to one question, so two sources
11921/// that disagree about what "carries the label bug" means fail them.
11922fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
11923    let holds = |name: &String| {
11924        labels
11925            .iter()
11926            .any(|label| label.name.eq_ignore_ascii_case(name))
11927    };
11928    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
11929        && filter.all_of.iter().all(holds)
11930        && !filter.none_of.iter().any(holds)
11931}
11932
11933/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
11934/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
11935fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
11936    statuses.is_empty() || statuses.contains(&category)
11937}
11938
11939/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
11940///
11941/// `content` is the item's own prose — the body with this source's trailing metadata
11942/// comment already taken off — so a search never matches an encoding the author of the
11943/// issue never wrote.
11944fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
11945    let terms = query.terms.to_lowercase();
11946    let in_title = title.to_lowercase().contains(&terms);
11947    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
11948    match query.fields {
11949        TextFields::Title => in_title,
11950        TextFields::Content => in_content,
11951        TextFields::TitleOrContent => in_title || in_content,
11952    }
11953}
11954
11955/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
11956///
11957/// The project predicate is passed separately because a read narrowed to one project has
11958/// already answered it by asking *that project* for its own items — and re-applying it
11959/// there would compare the caller's selector, which may be a project's **name**, against
11960/// the id of the project that name resolved to, and keep nothing. Every other read passes
11961/// `query.project` and applies it here, which is what keeps `projects` a predicate this
11962/// source really does apply.
11963fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
11964    labels_match(&task.labels, &query.labels)
11965        && status_matches(task.status.category, &query.statuses)
11966        && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
11967        && match project {
11968            ProjectFilter::Any => true,
11969            ProjectFilter::Orphans => task.project.is_none(),
11970            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
11971        }
11972        && query
11973            .text
11974            .as_ref()
11975            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
11976        // Against the parsed metadata slot, and against the origin field, which is where
11977        // `Resolved::metadata` reads each of them from.
11978        && query.metadata_matches(&task.metadata)
11979        && query.origin_matches(&task.metadata)
11980}
11981
11982fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
11983    labels_match(&project.labels, &query.labels)
11984        && status_matches(project.status.category, &query.statuses)
11985        && query
11986            .text
11987            .as_ref()
11988            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
11989}
11990
11991/// The same three predicates a task query carries, minus the status filter.
11992///
11993/// A document is not work, so it has no status for one to compare against and the query
11994/// type carries none. The project predicate is the same one — a design issue filed under a
11995/// project issue is in that project, and one filed under nothing is in none — so it is
11996/// spelled the same way here rather than answered differently.
11997fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
11998    labels_match(&document.labels, &query.labels)
11999        && match project {
12000            ProjectFilter::Any => true,
12001            ProjectFilter::Orphans => document.project.is_none(),
12002            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
12003        }
12004        && query
12005            .text
12006            .as_ref()
12007            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
12008}
12009
12010/// Why a GitHub item's change token does not vouch for its forward edges.
12011///
12012/// A forward edge names its far end and that far end's kind, and the near item's token moves
12013/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
12014/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
12015/// nothing the near token reads, so an edge kept at the near version could name a far end that
12016/// is no longer what it said.
12017const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
12018     whether it became a document, or its deletion, so its forward edges are always read from \
12019     github";
12020
12021/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
12022/// categories an open issue reads as narrows a board-scoped search with.
12023const OPEN_QUALIFIER: &str = "is:open";
12024
12025/// The board filter `task status reconcile` reads the board through: closed items alone.
12026const CLOSED_FILTER: &str = "is:closed";
12027
12028/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
12029/// one reference, and a slot the batch has none for names its last one again.
12030fn reference_batch(batch: &[IssueRef]) -> Value {
12031    let mut variables = serde_json::Map::new();
12032    for slot in 0..REFERENCE_BATCH {
12033        let issue = batch.get(slot).or(batch.last());
12034        variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
12035        variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
12036        variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
12037    }
12038    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12039    variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
12040    variables.insert("duplicates".to_owned(), json!(true));
12041    Value::Object(variables)
12042}
12043
12044/// One issue an issue read by reference answered, in the shape every other read of an issue
12045/// hands its resolver: each board membership's `Status` value, read by name, as the one field
12046/// value that membership carries.
12047///
12048/// A membership's `status` is `null` when its card sits at no option — or on a board with no
12049/// field of that name — and otherwise an option value naming its option; anything else is not
12050/// an answer to this read, and is refused rather than read as a card at no option.
12051fn held_issue(issue: &Value) -> Result<Value, SourceError> {
12052    let malformed = |what: &str| SourceError::Malformed {
12053        message: format!("GitHub answered an issue read by reference with {what}"),
12054    };
12055    let mut issue = issue.clone();
12056    let nodes = issue
12057        .pointer_mut("/projectItems/nodes")
12058        .and_then(Value::as_array_mut)
12059        .ok_or_else(|| malformed("no list of board memberships"))?;
12060    for node in nodes {
12061        let node = node
12062            .as_object_mut()
12063            .ok_or_else(|| malformed("a board membership that is not an object"))?;
12064        let status = match node.get("status") {
12065            None => return Err(malformed("a board membership with no status member")),
12066            Some(Value::Null) => None,
12067            Some(status) if status.get("name").is_some_and(Value::is_string) => {
12068                Some(status.clone())
12069            }
12070            // A single-select value of another field type answers `{}`; only an option value
12071            // carries a name.
12072            Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
12073            Some(_) => return Err(malformed("a membership status that names no option")),
12074        };
12075        node.insert(
12076            "fieldValues".to_owned(),
12077            json!({"nodes": status.into_iter().collect::<Vec<_>>(),
12078                   "pageInfo": {"hasNextPage": false}}),
12079        );
12080    }
12081    Ok(issue)
12082}
12083
12084/// The issue one slot of a read by reference answered, `None` when GitHub answered that
12085/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
12086/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
12087/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
12088/// than a reference nothing holds.
12089fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
12090    let alias = format!("r{slot}");
12091    let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
12092        message: format!("GitHub answered a read by reference with no {alias}"),
12093    })?;
12094    if repository.is_null() {
12095        return Ok(None);
12096    }
12097    let issue = repository
12098        .get("issue")
12099        .ok_or_else(|| SourceError::Malformed {
12100            message: format!("GitHub answered {alias} of a read by reference with no issue"),
12101        })?;
12102    if issue.is_null() {
12103        return Ok(None);
12104    }
12105    // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
12106    if optional_str(issue, "__typename")? != Some("Issue") {
12107        return Err(SourceError::Malformed {
12108            message: format!(
12109                "GitHub answered {alias} of a read by reference with an issue whose __typename \
12110                 is not Issue"
12111            ),
12112        });
12113    }
12114    Ok(Some(issue))
12115}
12116
12117/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
12118fn spoken_reason(reason: &str) -> String {
12119    reason.to_ascii_lowercase().replace('_', " ")
12120}
12121
12122impl GitHubProjectsSource {
12123    /// Every task and project of this board whose card contradicts its issue's closure, each
12124    /// judged against its own kind's half of `status_mapping`, read through the board's own
12125    /// `is:closed` filter, page by page — never the board's unfiltered `items`.
12126    ///
12127    /// Each item the filter answers is confirmed from its own node: an open one, one whose
12128    /// closure reads `unknown`, and one whose card is already in line are left out whatever
12129    /// the filter let through. The board's fields ride along and are held for the option
12130    /// writes that follow, so those need no read of the board.
12131    async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
12132        let mut after: Option<String> = None;
12133        let mut found = Vec::new();
12134        loop {
12135            let data = self
12136                .graphql(
12137                    graphql::CLOSED_ITEMS,
12138                    json!({"owner":self.owner,"number":self.project_number,
12139                           "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
12140                           "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
12141                )
12142                .await?;
12143            let board = data
12144                .pointer("/closedItems/projectV2")
12145                .filter(|value| !value.is_null())
12146                .ok_or_else(|| SourceError::Refused {
12147                    message: format!(
12148                        "GitHub project {}/{} was not found or is not visible to the token",
12149                        self.owner, self.project_number
12150                    ),
12151                })?;
12152            *self.fields_cache()? = Some(BoardFields {
12153                id: BoardId::parse(required_str(board, "id")?)?,
12154                fields: board.get("fields").cloned().unwrap_or(Value::Null),
12155            });
12156            let items = board.get("items").ok_or_else(|| SourceError::Malformed {
12157                message: "GitHub project closed items are missing".into(),
12158            })?;
12159            for item in items
12160                .get("nodes")
12161                .and_then(Value::as_array)
12162                .ok_or_else(|| SourceError::Malformed {
12163                    message: "GitHub project items.nodes is not an array".into(),
12164                })?
12165            {
12166                let Some(resolved) = self.resolve(item)? else {
12167                    continue;
12168                };
12169                let BoardKind::Work(kind) = resolved.kind else {
12170                    continue;
12171                };
12172                let Some((closed_as, name)) = self.statuses.contradicted(
12173                    kind,
12174                    resolved.option.as_deref(),
12175                    resolved.closed,
12176                    resolved.reason.as_deref(),
12177                ) else {
12178                    continue;
12179                };
12180                found.push(StatusContradiction {
12181                    closed_as,
12182                    to: name.to_owned(),
12183                    option: resolved.option.clone(),
12184                    item: match kind {
12185                        ItemKind::Task => ContradictedItem::Task(resolved.task()?),
12186                        ItemKind::Project => ContradictedItem::Project(resolved.project()),
12187                    },
12188                });
12189            }
12190            let info = items
12191                .get("pageInfo")
12192                .ok_or_else(|| SourceError::Malformed {
12193                    message: "GitHub project items have no pageInfo".into(),
12194                })?;
12195            if !required_bool(info, "hasNextPage")? {
12196                return Ok(found);
12197            }
12198            let next = required_str(info, "endCursor")?;
12199            validate_cursor_progress(after.as_deref(), next)?;
12200            after = Some(next.to_owned());
12201        }
12202    }
12203
12204    /// What this board holds for each issue in `issues`, once each in the order given, read
12205    /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
12206    ///
12207    /// An issue GitHub cannot find for this source's credential, and one with no item on this
12208    /// board, are both absent. A closure that already says the work will not be done here —
12209    /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
12210    /// completed whose card sits at its kind's `done` option is unchanged, and every other item
12211    /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
12212    /// tasks as sub-issues — is held under exactly these rules, judged against the project
12213    /// kind's half of `status_mapping`.
12214    async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
12215        let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
12216        for issue in issues {
12217            if !distinct.contains(issue) {
12218                distinct.push(issue.clone());
12219            }
12220        }
12221        let done = |kind: ItemKind| {
12222            self.statuses
12223                .target(kind, StatusCategory::Done)
12224                .option()
12225                .unwrap_or("Done")
12226                .to_owned()
12227        };
12228        // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
12229        // its range names no issue any repository can hold: it is answered absent and never sent,
12230        // where binding it would have GitHub refuse the whole batch.
12231        let readable: Vec<IssueRef> = distinct
12232            .iter()
12233            .filter(|issue| i32::try_from(issue.number).is_ok())
12234            .cloned()
12235            .collect();
12236        let mut holdings = Vec::with_capacity(readable.len());
12237        for batch in readable.chunks(REFERENCE_BATCH) {
12238            let data = self
12239                .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
12240                .await?;
12241            for (slot, issue) in batch.iter().enumerate() {
12242                let node = referenced_issue(&data, slot)?;
12243                let resolved = match node {
12244                    Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
12245                    None => None,
12246                };
12247                // A task and a project are both held; a document is not work, so nothing is.
12248                let Some((item, kind)) = resolved.and_then(|item| match item.kind {
12249                    BoardKind::Work(kind) => Some((item, kind)),
12250                    BoardKind::Document => None,
12251                }) else {
12252                    holdings.push(IssueHolding {
12253                        issue: issue.clone(),
12254                        completion: Completion::Absent,
12255                    });
12256                    continue;
12257                };
12258                let task = match kind {
12259                    ItemKind::Task => {
12260                        let held = item.task()?;
12261                        HeldTask {
12262                            id: held.id,
12263                            kind,
12264                            option: item.option.clone(),
12265                            classification: held.classification,
12266                            repositories: held.repositories,
12267                            delivers: held.delivers,
12268                        }
12269                    }
12270                    // A project delivers no task, so what its write keeps in step is nothing.
12271                    ItemKind::Project => {
12272                        let held = item.project();
12273                        HeldTask {
12274                            id: held.id,
12275                            kind,
12276                            option: item.option.clone(),
12277                            classification: held.classification,
12278                            repositories: held.repositories,
12279                            delivers: Vec::new(),
12280                        }
12281                    }
12282                };
12283                let done = done(kind);
12284                let completion = match (item.closed, item.reason.as_deref()) {
12285                    (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
12286                        task,
12287                        reason: format!("closed as {}", spoken_reason(reason)),
12288                    },
12289                    (true, None | Some("COMPLETED"))
12290                        if item
12291                            .option
12292                            .as_deref()
12293                            .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
12294                    {
12295                        Completion::Unchanged { task }
12296                    }
12297                    _ => {
12298                        let kept = item.clone();
12299                        self.held_cache
12300                            .lock()
12301                            .map_err(|_| SourceError::Unavailable {
12302                                message: "items read by issue reference were left \
12303                                          inconsistent; run the command again"
12304                                    .into(),
12305                            })?
12306                            .insert(kept.id.clone(), kept);
12307                        Completion::Due {
12308                            task,
12309                            to: done.clone(),
12310                        }
12311                    }
12312                };
12313                holdings.push(IssueHolding {
12314                    issue: issue.clone(),
12315                    completion,
12316                });
12317            }
12318        }
12319        let mut read = holdings.into_iter();
12320        let mut answered = Vec::with_capacity(distinct.len());
12321        for issue in distinct {
12322            if i32::try_from(issue.number).is_ok() {
12323                answered.push(read.next().ok_or_else(|| SourceError::Malformed {
12324                    message: format!("no holding was read for {issue}"),
12325                })?);
12326            } else {
12327                answered.push(IssueHolding {
12328                    issue,
12329                    completion: Completion::Absent,
12330                });
12331            }
12332        }
12333        Ok(answered)
12334    }
12335}
12336
12337#[async_trait::async_trait]
12338impl TaskSource for GitHubProjectsSource {
12339    fn kind(&self) -> &'static str {
12340        KIND
12341    }
12342    fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
12343        change_token::order(a, b)
12344    }
12345    /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
12346    /// member, the change each part of the token moves for. Not its forward edges — see
12347    /// [`FAR_ENDS_UNCOVERED`].
12348    fn version_coverage(&self) -> VersionCoverage {
12349        VersionCoverage {
12350            forward_edges: Coverage::Uncovered {
12351                reason: FAR_ENDS_UNCOVERED.to_owned(),
12352            },
12353            ..VersionCoverage::whole()
12354        }
12355    }
12356    /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
12357    /// and for a far end deleted, so the token sees every change to a task's edges but one — a
12358    /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
12359    /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
12360    /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
12361    // 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.
12362    fn copy_version_coverage(&self) -> VersionCoverage {
12363        VersionCoverage::whole()
12364    }
12365    /// A project's fields, and not its forward edges, for the same reason a task's are not.
12366    fn project_version_coverage(&self) -> ProjectVersionCoverage {
12367        ProjectVersionCoverage {
12368            fields: Coverage::Covered,
12369            forward_edges: Coverage::Uncovered {
12370                reason: FAR_ENDS_UNCOVERED.to_owned(),
12371            },
12372        }
12373    }
12374    /// A document's fields and the manifest of its image assets, which its metadata slot
12375    /// records: both are the issue's body, whose every edit moves the token's issue time.
12376    fn document_version_coverage(&self) -> DocumentVersionCoverage {
12377        DocumentVersionCoverage::whole()
12378    }
12379    fn write_context(&self, id: &NativeId) -> Option<Value> {
12380        self.context_of(id)
12381    }
12382    fn board_context(&self) -> Option<Value> {
12383        self.board_context_held()
12384    }
12385    fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
12386        self.prime(id, context, board)
12387    }
12388    fn unprime(&self, id: &NativeId) {
12389        self.unprime_held(id);
12390    }
12391    fn begin_journal(&self) {
12392        self.expected.begin();
12393    }
12394    fn end_journal(&self) -> Option<WriteJournal> {
12395        Some(self.expected.end())
12396    }
12397    fn capabilities(&self) -> Capabilities {
12398        Capabilities {
12399            projects: Support::Native,
12400            documents: Support::Native,
12401            comments: Support::Native,
12402            assets: Support::Native,
12403            priority: if self.priorities.is_some() {
12404                Support::Native
12405            } else {
12406                Support::Unsupported
12407            },
12408            filter_by_priority: Support::Native,
12409            filter_by_comment_activity: Support::Native,
12410            filter_by_metadata: Support::Native,
12411            filter_by_origin: Support::Native,
12412            reconcile_status: Support::Native,
12413            complete_by_issue: Support::Native,
12414            orphan_tasks: Support::Native,
12415            filter_by_label: Support::Native,
12416            filter_by_status: Support::Native,
12417            search_title: Support::Native,
12418            search_content: Support::Native,
12419            task_dependencies: DependencySupport::BothDirections,
12420            project_dependencies: DependencySupport::BothDirections,
12421            max_page_size: MAX_PAGE_SIZE,
12422        }
12423    }
12424    async fn visibility(
12425        &self,
12426        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
12427    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
12428        self.write_visibility(target).await
12429    }
12430    async fn health(&self) -> Result<Health, SourceError> {
12431        let board = self.board_page(None, 1).await?;
12432        Ok(Health {
12433            reachable: true,
12434            detail: Some(format!(
12435                "reading GitHub project {}/{} ({})",
12436                self.owner,
12437                self.project_number,
12438                required_str(&board, "title")?
12439            )),
12440        })
12441    }
12442    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
12443        self.item_by_id(id)
12444            .await?
12445            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
12446            .map(|item| item.task())
12447            .transpose()
12448    }
12449    async fn task_assets(
12450        &self,
12451        id: &NativeId,
12452    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
12453        self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
12454    }
12455    async fn task_asset(
12456        &self,
12457        id: &NativeId,
12458        name: &onetaskgraph_plugin_api::AssetName,
12459    ) -> Result<Option<Vec<u8>>, SourceError> {
12460        self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
12461            .await
12462    }
12463    async fn set_task_rendering_with_assets(
12464        &self,
12465        id: &NativeId,
12466        content: &str,
12467        provenance: &Value,
12468        _answers: &BTreeMap<String, Value>,
12469        assets: &onetaskgraph_plugin_api::AssetWrite,
12470    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
12471        self.replace_rendering(
12472            id,
12473            BoardKind::Work(ItemKind::Task),
12474            content,
12475            provenance,
12476            Some(assets),
12477        )
12478        .await
12479    }
12480    async fn document_assets(
12481        &self,
12482        id: &NativeId,
12483    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
12484        self.held_assets(id, BoardKind::Document).await
12485    }
12486    async fn document_asset(
12487        &self,
12488        id: &NativeId,
12489        name: &onetaskgraph_plugin_api::AssetName,
12490    ) -> Result<Option<Vec<u8>>, SourceError> {
12491        self.held_asset(id, BoardKind::Document, name).await
12492    }
12493    async fn set_document_rendering_with_assets(
12494        &self,
12495        id: &NativeId,
12496        content: &str,
12497        provenance: &Value,
12498        _answers: &BTreeMap<String, Value>,
12499        assets: &onetaskgraph_plugin_api::AssetWrite,
12500    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
12501        self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
12502            .await
12503    }
12504    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
12505        Ok(self
12506            .item_by_id(id)
12507            .await?
12508            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
12509            .map(|item| item.project()))
12510    }
12511    async fn query_tasks(
12512        &self,
12513        query: &TaskQuery,
12514        page: &PageRequest,
12515    ) -> Result<Page<Task>, SourceError> {
12516        validate_page(page)?;
12517        refuse_unsearchable(query)?;
12518        // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
12519        // category — `unknown`; every other category reads only off an open issue or a draft. So
12520        // a status filter naming none of those three asks every issue search it sends for open
12521        // issues alone; each candidate's category is still confirmed from its own item below.
12522        let open_only = !query.statuses.is_empty()
12523            && query.statuses.iter().all(|category| {
12524                !matches!(
12525                    category,
12526                    StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
12527                )
12528            });
12529        if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
12530            let mut qualifiers: Vec<String> = [
12531                query.commented_since.map(updated_qualifier),
12532                narrowing_qualifiers(query),
12533            ]
12534            .into_iter()
12535            .flatten()
12536            .collect();
12537            // A narrowed search never answers a draft, which is no issue, so narrowing it to
12538            // open issues too leaves out nothing such a search could have answered.
12539            if !qualifiers.is_empty() {
12540                if open_only {
12541                    qualifiers.push(OPEN_QUALIFIER.to_owned());
12542                }
12543                return self.search_tasks(query, page, &qualifiers.join(" ")).await;
12544            }
12545        }
12546
12547        // A read narrowed to one project asks that project for its own tasks, so nothing
12548        // about it costs what the rest of the board holds. A read carrying a text, metadata
12549        // or origin predicate asks GitHub the narrower question those predicates are, and a
12550        // read narrowed to comment activity alone asks the board's own issue search for the
12551        // issues updated since, which is every issue a comment could have been written or
12552        // edited on since. Every other task read is a question about the whole board and is
12553        // answered by reading it.
12554        let (held, membership) = match (&query.project, query.commented_since) {
12555            (ProjectFilter::Is(project), _) => (
12556                match self.held_task_children(project)? {
12557                    // Taken as `project_children` takes the project's own sub-issue read: with
12558                    // every item this process wrote under it since, and none it moved away.
12559                    Some(held) => self.completed_with_written(
12560                        held.into_iter()
12561                            .filter(|child| child.parent.as_ref() == Some(project))
12562                            .collect(),
12563                        |own| own.parent.as_ref() == Some(project),
12564                    )?,
12565                    None => self.project_children(project).await?,
12566                },
12567                // Answered by where these items came from; see `task_matches`.
12568                &ProjectFilter::Any,
12569            ),
12570            (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
12571                match (self.narrowed(query).await?, since) {
12572                    (Some(narrowed), _) => (narrowed, &query.project),
12573                    (None, Some(since)) => (self.updated_since(since).await?, &query.project),
12574                    // The board's own items list every draft; only the search half, which
12575                    // completes them with issues they lag behind on, is narrowed — a closed
12576                    // issue that half leaves out is at no category the filter keeps.
12577                    (None, None) if open_only => (
12578                        self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
12579                        &query.project,
12580                    ),
12581                    (None, None) => (self.board().await?.items, &query.project),
12582                }
12583            }
12584        };
12585        // Filtered before paged: a page of a filtered result is a page of the survivors,
12586        // never the survivors of a page.
12587        let mut tasks = Vec::new();
12588        for item in held
12589            .iter()
12590            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
12591        {
12592            let task = item.task()?;
12593            if task_matches(&task, query, membership)
12594                && self.commented_since(item, query.commented_since).await?
12595            {
12596                tasks.push(task);
12597            }
12598        }
12599        Ok(offset_page(
12600            tasks,
12601            numeric_cursor(page.cursor.as_ref())?,
12602            page.limit.min(MAX_PAGE_SIZE) as usize,
12603        ))
12604    }
12605    async fn query_projects(
12606        &self,
12607        query: &ProjectQuery,
12608        page: &PageRequest,
12609    ) -> Result<Page<Project>, SourceError> {
12610        validate_page(page)?;
12611        refuse_unsearchable_text(query.text.as_ref())?;
12612        // The projects a board holds are found by an issue search scoped to that board,
12613        // never by walking the board's own item connection: what tells a project from a
12614        // task is the `parent` each issue carries, which costs nothing to read. A query
12615        // carrying a text asks that search for the text too, so it reads the issues that
12616        // hold it rather than every issue of the board.
12617        let held = match self.text_searched(query.text.as_ref()).await? {
12618            Some(searched) => searched,
12619            None => self.board_issues().await?,
12620        };
12621        let projects = held
12622            .iter()
12623            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
12624            .map(Resolved::project)
12625            .filter(|project| project_matches(project, query))
12626            .collect();
12627        Ok(offset_page(
12628            projects,
12629            numeric_cursor(page.cursor.as_ref())?,
12630            page.limit.min(MAX_PAGE_SIZE) as usize,
12631        ))
12632    }
12633    async fn query_projects_by_origin(
12634        &self,
12635        origin: &str,
12636        page: &PageRequest,
12637    ) -> Result<Page<Project>, SourceError> {
12638        validate_page(page)?;
12639        let projects = self
12640            .copied_from(origin, BoardKind::Work(ItemKind::Project))
12641            .await?
12642            .iter()
12643            .map(Resolved::project)
12644            .collect();
12645        Ok(offset_page(
12646            projects,
12647            numeric_cursor(page.cursor.as_ref())?,
12648            page.limit.min(MAX_PAGE_SIZE) as usize,
12649        ))
12650    }
12651    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
12652        let Some(item) = self
12653            .item_by_id(id)
12654            .await?
12655            .filter(|item| item.kind == BoardKind::Document)
12656        else {
12657            return Ok(None);
12658        };
12659        self.expected.read_document(&item);
12660        Ok(Some(item.document()))
12661    }
12662    async fn query_documents_by_origin(
12663        &self,
12664        origin: &str,
12665        page: &PageRequest,
12666    ) -> Result<Page<Document>, SourceError> {
12667        validate_page(page)?;
12668        let documents = self
12669            .copied_from(origin, BoardKind::Document)
12670            .await?
12671            .iter()
12672            .map(Resolved::document)
12673            .collect();
12674        Ok(offset_page(
12675            documents,
12676            numeric_cursor(page.cursor.as_ref())?,
12677            page.limit.min(MAX_PAGE_SIZE) as usize,
12678        ))
12679    }
12680    async fn query_documents(
12681        &self,
12682        query: &DocumentQuery,
12683        page: &PageRequest,
12684    ) -> Result<Page<Document>, SourceError> {
12685        validate_page(page)?;
12686        // Narrowed to one project, this is the same sub-issue read a task list scoped to
12687        // that project makes — a document filed under a project is a sub-issue of it too,
12688        // and which of them come back is the kind this caller asked for. Unscoped, a query
12689        // carrying a text asks the board-scoped issue search for it, as a task query does,
12690        // and only one carrying none reads the board.
12691        let (held, membership) = match &query.project {
12692            ProjectFilter::Is(project) => (
12693                self.project_children(project).await?,
12694                // Answered by where these items came from; see `task_matches`.
12695                &ProjectFilter::Any,
12696            ),
12697            ProjectFilter::Any | ProjectFilter::Orphans => {
12698                refuse_unsearchable_text(query.text.as_ref())?;
12699                match self.text_searched(query.text.as_ref()).await? {
12700                    Some(searched) => (searched, &query.project),
12701                    None => (self.board().await?.items, &query.project),
12702                }
12703            }
12704        };
12705        // Filtered before paged, exactly as a task read is: a page of a filtered result is
12706        // a page of the survivors, never the survivors of a page.
12707        let documents = held
12708            .iter()
12709            .filter(|item| item.kind == BoardKind::Document)
12710            .map(Resolved::document)
12711            .filter(|document| document_matches(document, query, membership))
12712            .collect();
12713        Ok(offset_page(
12714            documents,
12715            numeric_cursor(page.cursor.as_ref())?,
12716            page.limit.min(MAX_PAGE_SIZE) as usize,
12717        ))
12718    }
12719    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
12720        validate_page(page)?;
12721        let offset = numeric_cursor(page.cursor.as_ref())?;
12722        let mut labels = self
12723            .board()
12724            .await?
12725            .items
12726            .into_iter()
12727            .flat_map(|item| item.labels)
12728            .fold(Vec::new(), |mut all, label| {
12729                if !all.iter().any(|x: &Label| x.id == label.id) {
12730                    all.push(label);
12731                }
12732                all
12733            });
12734        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
12735        Ok(offset_page(
12736            labels,
12737            offset,
12738            page.limit.min(MAX_PAGE_SIZE) as usize,
12739        ))
12740    }
12741    async fn task_dependencies(
12742        &self,
12743        id: &NativeId,
12744        direction: Direction,
12745        page: &PageRequest,
12746    ) -> Result<Page<DependencyEdge>, SourceError> {
12747        self.dependencies(id, ItemKind::Task, direction, page).await
12748    }
12749    async fn project_dependencies(
12750        &self,
12751        id: &NativeId,
12752        direction: Direction,
12753        page: &PageRequest,
12754    ) -> Result<Page<DependencyEdge>, SourceError> {
12755        self.dependencies(id, ItemKind::Project, direction, page)
12756            .await
12757    }
12758
12759    fn writes(&self) -> WriteSupport {
12760        WriteSupport::Supported
12761    }
12762
12763    /// Create or update one task.
12764    ///
12765    /// Its `delivers` and `delivered_by` are checked before anything is read or written —
12766    /// neither may name the task itself or name one task twice — and land in the body's
12767    /// metadata slot under their reserved keys, in place of any caller metadata of those
12768    /// names.
12769    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
12770        self.write_task_assets(write, None)
12771            .await
12772            .map(|written| written.id)
12773    }
12774
12775    async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
12776        self.origins_ahead(origins).await
12777    }
12778
12779    /// The issue's own web address, as the `createIssue` that made it in this command answered
12780    /// it: a task, a project and a document are each an issue here.
12781    fn written_location(
12782        &self,
12783        _record: onetaskgraph_plugin_api::MetadataRecord,
12784        id: &NativeId,
12785    ) -> Option<Location> {
12786        self.written
12787            .lock()
12788            .unwrap_or_else(std::sync::PoisonError::into_inner)
12789            .get(id)
12790            .cloned()
12791            .map(Location::Url)
12792    }
12793
12794    /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
12795    /// in one request, and a copy's tasks are created together only then, since their fields
12796    /// and edges are what is held back.
12797    fn create_batch_limit(&self) -> usize {
12798        if self.deferring().unwrap_or(false) {
12799            MUTATION_BATCH
12800        } else {
12801            1
12802        }
12803    }
12804
12805    // 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.
12806    async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
12807        if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
12808            return Err(SourceError::Refused {
12809                message: format!(
12810                    "write_tasks creates tasks, and the write of {} names the existing task {}; \
12811                     next: update it with write_task",
12812                    write.item.id,
12813                    write.target.as_ref().map_or("", |target| target.0.as_str())
12814                ),
12815            });
12816        }
12817        if !self.deferring()? {
12818            let mut created = Vec::with_capacity(writes.len());
12819            for write in writes {
12820                match self.write_task(write).await {
12821                    Ok(id) => created.push(id),
12822                    Err(error) => {
12823                        let mut left: Vec<&str> = Vec::new();
12824                        for id in &created {
12825                            if self.delete_issue(id).await.is_err() {
12826                                left.push(id.0.as_str());
12827                            }
12828                        }
12829                        return Err(left_behind(error, &left));
12830                    }
12831                }
12832            }
12833            return Ok(created);
12834        }
12835        self.create_together(writes).await
12836    }
12837
12838    fn begin_deferred_writes(&self) {
12839        if let Ok(mut deferred) = self.deferred.lock() {
12840            deferred.get_or_insert_with(Deferred::default);
12841        }
12842    }
12843
12844    async fn finish_writes(&self) -> Result<(), SourceError> {
12845        self.send_held_back().await
12846    }
12847
12848    async fn write_task_with_assets(
12849        &self,
12850        write: &ItemWrite<Task>,
12851        _answers: Option<&BTreeMap<String, Value>>,
12852        assets: &onetaskgraph_plugin_api::AssetWrite,
12853    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
12854        self.write_task_assets(write, Some(assets)).await
12855    }
12856
12857    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
12858        self.write_item(
12859            &Incoming {
12860                written: Written::Work(ItemKind::Project, &write.item.status),
12861                title: &write.item.title,
12862                content: write.item.content.as_deref(),
12863                assets: None,
12864                labels: &write.item.labels,
12865                metadata: &write.item.metadata,
12866                repositories: &write.item.repositories,
12867                classification: write.item.classification,
12868                parent: None,
12869                delivers: &[],
12870                delivered_by: &[],
12871                priority: None,
12872            },
12873            write.target.as_ref(),
12874            &write.depends_on,
12875        )
12876        .await
12877        .map(|written| written.id)
12878    }
12879
12880    /// Create or update one document, which is one issue titled the way this board spells
12881    /// a document.
12882    ///
12883    /// Everything else is exactly a task write: caller metadata goes to the same canonical
12884    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
12885    /// or a field this board cannot carry is refused by name rather than dropped, a target
12886    /// naming an issue this board does not hold is refused rather than created, and an
12887    /// issue this call created is taken back when the rest of the write fails.
12888    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
12889        self.write_document_assets(write, None)
12890            .await
12891            .map(|written| written.id)
12892    }
12893
12894    async fn write_document_with_assets(
12895        &self,
12896        write: &ItemWrite<Document>,
12897        _answers: Option<&BTreeMap<String, Value>>,
12898        assets: &onetaskgraph_plugin_api::AssetWrite,
12899    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
12900        self.write_document_assets(write, Some(assets)).await
12901    }
12902
12903    /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
12904    /// which reads nothing; then the board's `Status` option. Over an existing item that is
12905    /// read off the item, as the write reads it, and the item is held among this command's
12906    /// resolved records so the write that follows reuses that read rather than repeating it;
12907    /// an item that does not carry the field takes the board's fields, which are held once
12908    /// read. A create is checked against the board's fields only when this command already
12909    /// holds them, because a create reads them together with its repository, in one request,
12910    /// and refuses a missing option before it writes anything.
12911    async fn check_status_write(
12912        &self,
12913        kind: ItemKind,
12914        category: StatusCategory,
12915        target: Option<&NativeId>,
12916    ) -> Result<(), SourceError> {
12917        let status = self.resolved_target(kind, category)?;
12918        if status.option().is_none() {
12919            return Ok(());
12920        }
12921        let fields = match target {
12922            Some(target) => {
12923                // A target this board does not hold is the write's own refusal to make.
12924                let Some(item) = self.bound_item(target).await? else {
12925                    return Ok(());
12926                };
12927                self.resolved_cache()?.insert(target.clone(), item.clone());
12928                self.fields_for(Some(&item), true, false).await?.fields
12929            }
12930            None => {
12931                let held = self
12932                    .board_cache()?
12933                    .as_ref()
12934                    .map(|board| board.fields.clone());
12935                match held.or_else(|| {
12936                    self.fields_cache()
12937                        .ok()
12938                        .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
12939                }) {
12940                    Some(fields) => fields,
12941                    None => return Ok(()),
12942                }
12943            }
12944        };
12945        self.column_for(&fields, kind, category, &status)
12946            .map(|_| ())
12947    }
12948
12949    /// What a write of `status` reads back as: the `Status` option its category maps to for
12950    /// `kind`, and for a closing category the closure that option and its fixed reason read
12951    /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
12952    /// this instance has no option for is answered as given, since writing it is refused.
12953    fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
12954        match self.statuses.target(kind, status.category) {
12955            StatusTarget::Column(name) => {
12956                self.statuses.status(kind, Some(name.as_str()), false, None)
12957            }
12958            StatusTarget::Terminal(name, reason) => {
12959                self.statuses
12960                    .status(kind, Some(name.as_str()), true, Some(reason.reason()))
12961            }
12962            StatusTarget::Disabled(_) => status.clone(),
12963        }
12964    }
12965
12966    /// Set one task's status alone.
12967    ///
12968    /// An open target reopens a closed issue with an `updateIssue` carrying only its
12969    /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
12970    /// terminal target selects its mapped option, then closes with its fixed reason. No
12971    /// request carries a title, a body or a label. The status
12972    /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
12973    /// what a re-read reports.
12974    async fn set_task_status(
12975        &self,
12976        id: &NativeId,
12977        category: StatusCategory,
12978    ) -> Result<Option<Status>, SourceError> {
12979        self.set_status(id, ItemKind::Task, category).await
12980    }
12981
12982    /// Set one project's status alone, by exactly the paired write a task's status takes, for
12983    /// the project kind: a terminal category selects the project kind's mapped option and then
12984    /// closes the issue with the category's fixed reason — an issue already closed with a
12985    /// reason that reads that category is left as it is — and an open category reopens a closed
12986    /// issue and then selects its option. A board draft is refused a terminal category as a
12987    /// task's is.
12988    async fn set_project_status(
12989        &self,
12990        id: &NativeId,
12991        category: StatusCategory,
12992    ) -> Result<Option<Status>, SourceError> {
12993        self.set_status(id, ItemKind::Project, category).await
12994    }
12995
12996    /// Every task and project whose card contradicts its issue's closure, read through the
12997    /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
12998    async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
12999        self.contradictions().await
13000    }
13001
13002    /// What this board holds for each issue reference, one request per fifty of them; see
13003    /// [`TaskSource::issue_holdings`].
13004    async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
13005        self.holdings(issues).await
13006    }
13007
13008    /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
13009    /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
13010    /// for `none`. Refused by an instance with no `priority_mapping`.
13011    async fn set_task_priority(
13012        &self,
13013        id: &NativeId,
13014        priority: Priority,
13015    ) -> Result<Option<Priority>, SourceError> {
13016        self.set_priority(id, priority).await
13017    }
13018
13019    /// Replace one task's content with a single body update that keeps the metadata slot
13020    /// byte for byte.
13021    async fn set_task_content(
13022        &self,
13023        id: &NativeId,
13024        content: &str,
13025    ) -> Result<Option<()>, SourceError> {
13026        self.replace_content(id, content).await
13027    }
13028
13029    /// Replace one task issue's content and its provenance slot entry with a single body
13030    /// update. The answers are not kept: see `replace_rendering`.
13031    async fn set_task_rendering(
13032        &self,
13033        id: &NativeId,
13034        content: &str,
13035        provenance: &Value,
13036        _answers: &BTreeMap<String, Value>,
13037    ) -> Result<Option<()>, SourceError> {
13038        self.replace_rendering(
13039            id,
13040            BoardKind::Work(ItemKind::Task),
13041            content,
13042            provenance,
13043            None,
13044        )
13045        .await
13046        .map(|written| written.map(|_| ()))
13047    }
13048
13049    /// Replace one design-document issue's content and its provenance slot entry, on exactly
13050    /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13051    async fn set_document_rendering(
13052        &self,
13053        id: &NativeId,
13054        content: &str,
13055        provenance: &Value,
13056        _answers: &BTreeMap<String, Value>,
13057    ) -> Result<Option<()>, SourceError> {
13058        self.replace_rendering(id, BoardKind::Document, content, provenance, None)
13059            .await
13060            .map(|written| written.map(|_| ()))
13061    }
13062
13063    /// Replace one project issue's content and its provenance slot entry, on exactly the
13064    /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13065    async fn set_project_rendering(
13066        &self,
13067        id: &NativeId,
13068        content: &str,
13069        provenance: &Value,
13070        _answers: &BTreeMap<String, Value>,
13071    ) -> Result<Option<()>, SourceError> {
13072        self.replace_rendering(
13073            id,
13074            BoardKind::Work(ItemKind::Project),
13075            content,
13076            provenance,
13077            None,
13078        )
13079        .await
13080        .map(|written| written.map(|_| ()))
13081    }
13082
13083    /// Apply a targeted update with one read of the item and a write only for what differs:
13084    /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
13085    /// and last one `updateIssue` for title, body and state. See `targeted_update`.
13086    async fn update_task(
13087        &self,
13088        id: &NativeId,
13089        update: &TaskUpdate,
13090    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
13091        self.targeted_update(id, update).await
13092    }
13093
13094    /// Replace one task's `delivered_by` with a single body update that changes the
13095    /// metadata slot and nothing outside it.
13096    async fn set_delivered_by(
13097        &self,
13098        id: &NativeId,
13099        delivered_by: &[TaskRef],
13100    ) -> Result<Option<()>, SourceError> {
13101        self.replace_delivered_by(id, delivered_by).await
13102    }
13103
13104    /// Set one key of one task issue's metadata with a single body update that changes the
13105    /// metadata slot and nothing outside it — no title, label, state or board field request —
13106    /// and sends nothing when the task already holds that value under the key.
13107    async fn set_task_metadata(
13108        &self,
13109        id: &NativeId,
13110        key: &MetadataKey,
13111        value: &Value,
13112    ) -> Result<Option<Task>, SourceError> {
13113        Ok(self
13114            .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
13115            .await?
13116            .map(|item| item.task())
13117            .transpose()?)
13118    }
13119
13120    /// Set one key of one project issue's metadata, on exactly the terms of
13121    /// [`set_task_metadata`](TaskSource::set_task_metadata).
13122    async fn set_project_metadata(
13123        &self,
13124        id: &NativeId,
13125        key: &MetadataKey,
13126        value: &Value,
13127    ) -> Result<Option<Project>, SourceError> {
13128        Ok(self
13129            .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
13130            .await?
13131            .map(|item| item.project()))
13132    }
13133
13134    /// Set one key of one design-document issue's metadata, on exactly the terms of
13135    /// [`set_task_metadata`](TaskSource::set_task_metadata).
13136    async fn set_document_metadata(
13137        &self,
13138        id: &NativeId,
13139        key: &MetadataKey,
13140        value: &Value,
13141    ) -> Result<Option<Document>, SourceError> {
13142        Ok(self
13143            .set_slot_key(id, BoardKind::Document, key, value)
13144            .await?
13145            .map(|item| item.document()))
13146    }
13147
13148    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
13149        self.delete_item(id).await
13150    }
13151
13152    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
13153        self.delete_item(id).await
13154    }
13155
13156    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
13157        self.delete_item(id).await
13158    }
13159
13160    /// One page of the task issue's own comments, walked by GitHub's own cursor.
13161    ///
13162    /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
13163    /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
13164    ///
13165    /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
13166    /// board is the read of its comments. A draft this process already resolved is refused
13167    /// without one.
13168    async fn task_comments(
13169        &self,
13170        task: &NativeId,
13171        page: &PageRequest,
13172    ) -> Result<Option<Page<Comment>>, SourceError> {
13173        validate_page(page)?;
13174        let cached = self.resolved_cache()?.get(task).cloned();
13175        if let Some(item) = cached {
13176            if item.kind != BoardKind::Work(ItemKind::Task) {
13177                return Ok(None);
13178            }
13179            if item.content_kind == ContentKind::DraftIssue {
13180                return Err(self.draft_has_no_comments(task));
13181            }
13182        }
13183        match self.issue_detail(task, page).await? {
13184            Some(TaskDetailRead {
13185                comments: Some(comments),
13186                ..
13187            }) => comments,
13188            _ => Ok(None),
13189        }
13190    }
13191
13192    /// Every id's task, with the first page of its comments when `comments` names it:
13193    /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
13194    /// comments in one [`graphql::ISSUE_DETAIL`] request.
13195    async fn get_task_details(
13196        &self,
13197        ids: &[NativeId],
13198        comments: Option<&PageRequest>,
13199    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
13200        if let Some(page) = comments
13201            && let Err(error) = validate_page(page)
13202        {
13203            return ids.iter().map(|_| Err(error.clone())).collect();
13204        }
13205        match (ids, comments) {
13206            ([id], Some(page)) => vec![self.issue_detail(id, page).await],
13207            ([id], None) => vec![self.task_read(id).await],
13208            _ => self.issue_details(ids, comments).await,
13209        }
13210    }
13211
13212    /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
13213    /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
13214    /// request, so the dependency read a copy makes of each next is answered from this one.
13215    async fn get_task_details_for_copy(
13216        &self,
13217        ids: &[NativeId],
13218        comments: &PageRequest,
13219    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
13220        // A page past the first is a page of one task's comments, which the detail read of it
13221        // alone answers.
13222        if comments.cursor.is_some() {
13223            return self.get_task_details(ids, Some(comments)).await;
13224        }
13225        if let Err(error) = validate_page(comments) {
13226            return ids.iter().map(|_| Err(error.clone())).collect();
13227        }
13228        let mut read = Vec::with_capacity(ids.len());
13229        for batch in ids.chunks(COPY_DETAIL_BATCH) {
13230            match self
13231                .graphql(
13232                    graphql::ISSUE_COPY_DETAILS,
13233                    copy_detail_batch(batch, comments, false),
13234                )
13235                .await
13236            {
13237                Ok(data) => {
13238                    for (slot, id) in batch.iter().enumerate() {
13239                        let read_one = match data.get(format!("i{slot}")) {
13240                            Some(node) => self.detail_of(id, node, true, None).await,
13241                            None => Err(SourceError::Malformed {
13242                                message: format!(
13243                                    "GitHub answered a batch read with no item for {}",
13244                                    id.0
13245                                ),
13246                            }),
13247                        };
13248                        read.push(read_one);
13249                    }
13250                }
13251                Err(error) if unresolvable_node(&error) => {
13252                    for id in batch {
13253                        read.push(self.issue_detail(id, comments).await);
13254                    }
13255                }
13256                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
13257            }
13258        }
13259        read
13260    }
13261
13262    /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
13263    /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
13264    /// and held for the next read of each by id; see `read_ahead`.
13265    ///
13266    /// What this run wrote is never read: its own record answers, as it does for a read by id.
13267    /// Nor is an item a listing of its project carried whole earlier in this command — see
13268    /// `carried` — which is held in place of reading it.
13269    /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
13270    /// so each of its ids is read on its own and answered as that read answers; a draft, and an
13271    /// answer naming another issue than the one asked for, are left to their own reads too.
13272    async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
13273        let mut unread: Vec<NativeId> = Vec::new();
13274        // Read whole by a listing of its project in this command, which a mutation naming it
13275        // would have dropped: held as a batch would have held it, once every batch is answered.
13276        let mut listed: Vec<NativeId> = Vec::new();
13277        {
13278            let created = self.created()?;
13279            let held = self.read_ahead()?;
13280            let carried = self.carried()?;
13281            for id in ids {
13282                if unread.contains(id)
13283                    || listed.contains(id)
13284                    || held.contains_key(id)
13285                    || created.iter().any(|own| own.id == *id)
13286                {
13287                    continue;
13288                }
13289                if carried.contains_key(id) {
13290                    listed.push(id.clone());
13291                } else {
13292                    unread.push(id.clone());
13293                }
13294            }
13295        }
13296        // Nothing of an item's comments is wanted here: the smallest page keeps the read to
13297        // what the item itself costs.
13298        let comments = PageRequest {
13299            cursor: None,
13300            limit: 1,
13301        };
13302        // Held only once every batch has been answered, so a read-ahead that fails part way
13303        // holds nothing — what a listing carried included, which stays carried — and every
13304        // item is read on its own, as the method promises.
13305        let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
13306        // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
13307        // answered them: what a project copy's orphan check asks, when that first id is its
13308        // project.
13309        let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
13310        for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
13311            let data = match self
13312                .graphql(
13313                    graphql::ISSUE_COPY_DETAILS,
13314                    copy_detail_batch(batch, &comments, index == 0),
13315                )
13316                .await
13317            {
13318                Ok(data) => data,
13319                Err(error) if unresolvable_node(&error) => continue,
13320                Err(error) => return Err(error),
13321            };
13322            for (slot, id) in batch.iter().enumerate() {
13323                let Some(node) = data.get(format!("i{slot}")) else {
13324                    continue;
13325                };
13326                let held = if node.is_null() {
13327                    None
13328                } else {
13329                    match optional_str(node, "__typename")? {
13330                        Some("DraftIssue") => continue,
13331                        Some("Issue") if required_str(node, "id")? != id.0 => continue,
13332                        _ => {}
13333                    }
13334                    // Remembered below with the rest, so a later batch refused leaves no
13335                    // dependency read answered from this one either.
13336                    self.resolve_issue_as(node, false).await?
13337                };
13338                read.push((id.clone(), held));
13339            }
13340            if index == 0
13341                && let Some(node) = data.get("children").filter(|node| !node.is_null())
13342                && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
13343                && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
13344            {
13345                // A list GitHub did not answer is not an empty one: it would answer the project's
13346                // task listing with nothing, and the orphan check with no orphans.
13347                let listed = node
13348                    .pointer("/subIssues/nodes")
13349                    .and_then(Value::as_array)
13350                    .ok_or_else(|| SourceError::Malformed {
13351                        message: format!(
13352                            "GitHub answered the sub-issues of project {} with no list of them",
13353                            batch[0].0
13354                        ),
13355                    })?
13356                    .iter()
13357                    .map(|child| {
13358                        Ok((
13359                            required_str(child, "id")?.to_owned(),
13360                            required_str(child, "title")?.to_owned(),
13361                        ))
13362                    })
13363                    .collect::<Result<Vec<_>, SourceError>>()?;
13364                children = Some((batch[0].clone(), listed));
13365            }
13366        }
13367        // The project's tasks are known from this read alone when every child it named is held
13368        // from it, but for a document, which its title says is one and which a task listing
13369        // never answers: then a listing of the project's tasks is answered from them.
13370        if let Some((project, listed)) = children
13371            && read.iter().any(|(id, held)| {
13372                *id == project
13373                    && held
13374                        .as_ref()
13375                        .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
13376            })
13377        {
13378            let mut tasks = Vec::new();
13379            let mut whole = true;
13380            for (child, title) in &listed {
13381                match read.iter().find(|(id, _)| id.0 == *child) {
13382                    Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
13383                        if held.kind == BoardKind::Work(ItemKind::Task) {
13384                            tasks.push(held.clone());
13385                        }
13386                    }
13387                    _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
13388                    _ => whole = false,
13389                }
13390            }
13391            if whole {
13392                self.task_children()?.insert(project, tasks);
13393            }
13394        }
13395        let mut resolved = self.resolved_cache()?;
13396        let mut ahead = self.read_ahead()?;
13397        let mut carried = self.carried()?;
13398        for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
13399            resolved.insert(item.id.clone(), item.clone());
13400        }
13401        // Taken only if still carried: a mutation naming it while the batches ran dropped it,
13402        // and its next read is then its own.
13403        for id in &listed {
13404            let Some(item) = carried.remove(id) else {
13405                continue;
13406            };
13407            // The whole of what it is blocked by came with it, so the dependency read a copy
13408            // makes next is answered from this one, as a copy's detail read is.
13409            if item.blocked_by.is_some() {
13410                resolved.insert(item.id.clone(), item.clone());
13411            }
13412            read.push((id.clone(), Some(item)));
13413        }
13414        for (id, held) in read {
13415            ahead.insert(id, held);
13416        }
13417        Ok(())
13418    }
13419
13420    /// Add one comment to the task's issue, as the account the token belongs to.
13421    ///
13422    /// The author is refused before anything is sent — not even the task is read — because
13423    /// no answer GitHub could give would make posting under another name than the one asked
13424    /// for the right outcome.
13425    async fn add_comment(
13426        &self,
13427        task: &NativeId,
13428        comment: &NewComment,
13429    ) -> Result<Option<Comment>, SourceError> {
13430        if let Some(author) = &comment.author {
13431            return Err(SourceError::Refused {
13432                message: format!(
13433                    "source {} cannot post a comment as {author:?}: GitHub records the account \
13434                     the token signs in as the author of every comment; next: leave --author \
13435                     out, and the comment is posted as that account",
13436                    self.name
13437                ),
13438            });
13439        }
13440        let Some(issue) = self.commented_issue(task).await? else {
13441            return Ok(None);
13442        };
13443        let data = self
13444            .graphql(
13445                graphql::ADD_COMMENT,
13446                json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
13447            )
13448            .await?;
13449        let subject = data
13450            .pointer("/addComment/subject")
13451            .filter(|value| !value.is_null())
13452            .ok_or_else(|| SourceError::Malformed {
13453                message: "GitHub comment addition returned no subject".into(),
13454            })?;
13455        if required_str(subject, "id")? != issue.0 {
13456            return Err(SourceError::Malformed {
13457                message: "GitHub comment addition answered about another issue".into(),
13458            });
13459        }
13460        let added = data
13461            .pointer("/addComment/commentEdge/node")
13462            .filter(|value| !value.is_null())
13463            .ok_or_else(|| SourceError::Malformed {
13464                message: "GitHub comment addition returned no comment".into(),
13465            })?;
13466        let added = comment_from(added)?;
13467        self.remember_commented(&issue)?;
13468        Ok(Some(added))
13469    }
13470
13471    async fn edit_comment(
13472        &self,
13473        task: &NativeId,
13474        comment: &NativeId,
13475        body: &CommentBody,
13476    ) -> Result<Option<Comment>, SourceError> {
13477        let Some(issue) = self.commented_issue(task).await? else {
13478            return Ok(None);
13479        };
13480        let Some(held) = self.comment_on(&issue, comment).await? else {
13481            return Ok(None);
13482        };
13483        // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
13484        let body = CommentOrigin::kept(&held, body)?;
13485        let data = self
13486            .graphql(
13487                graphql::UPDATE_COMMENT,
13488                json!({"input":{"id":comment.0,"body":body.as_str()}}),
13489            )
13490            .await?;
13491        let edited = data
13492            .pointer("/updateIssueComment/issueComment")
13493            .filter(|value| !value.is_null())
13494            .ok_or_else(|| SourceError::Malformed {
13495                message: "GitHub comment update returned no comment".into(),
13496            })?;
13497        let edited = comment_from(edited)?;
13498        if edited.id != *comment {
13499            return Err(SourceError::Malformed {
13500                message: "GitHub comment update returned the wrong comment".into(),
13501            });
13502        }
13503        self.remember_commented(&issue)?;
13504        Ok(Some(edited))
13505    }
13506
13507    async fn delete_comment(
13508        &self,
13509        task: &NativeId,
13510        comment: &NativeId,
13511    ) -> Result<Option<NativeId>, SourceError> {
13512        let Some(issue) = self.commented_issue(task).await? else {
13513            return Ok(None);
13514        };
13515        if !self.comment_is_on(&issue, comment).await? {
13516            return Ok(None);
13517        }
13518        let data = self
13519            .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
13520            .await?;
13521        // The payload says nothing about the comment it removed, so what is checked is that
13522        // GitHub answered the mutation at all rather than leaving it unanswered.
13523        data.get("deleteIssueComment")
13524            .filter(|value| !value.is_null())
13525            .ok_or_else(|| SourceError::Malformed {
13526                message: "GitHub comment deletion returned no payload".into(),
13527            })?;
13528        Ok(Some(comment.clone()))
13529    }
13530
13531    /// Every request this source has recorded, and what each of GitHub's two budgets was
13532    /// attributed — read off the same accounting the session report is rendered from, so
13533    /// the two cannot count one request two ways.
13534    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
13535        Ok(Some(self.ledger.snapshot().metering()))
13536    }
13537
13538    /// Drop every item, search answer and board read this source holds, so the next command
13539    /// reads the board as a person has since left it.
13540    ///
13541    /// Every one of those is held on the assumption that nothing but this source writes the
13542    /// board while a command runs, which stops being true the moment the command is over: a
13543    /// body a person edited would be overwritten from the record held here, and a card they
13544    /// moved would be read as still where this source left it. The board's own field
13545    /// definitions go too, because a person can add or delete a `Status` option and a write
13546    /// resolved against the held list would not re-read on a miss. What stays is what stays
13547    /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
13548    /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
13549    /// accounting [`metering`](TaskSource::metering) answers from.
13550    ///
13551    /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
13552    /// refused, because clearing it is what puts it right.
13553    async fn end_command(&self) -> Result<(), SourceError> {
13554        fn clear<T: Default>(held: &Mutex<T>) {
13555            *held
13556                .lock()
13557                .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
13558            held.clear_poison();
13559        }
13560        clear(&self.created);
13561        clear(&self.updated);
13562        clear(&self.commented);
13563        clear(&self.board_cache);
13564        clear(&self.search_cache);
13565        clear(&self.narrowed_cache);
13566        clear(&self.search_next);
13567        clear(&self.resolved_cache);
13568        clear(&self.read_ahead);
13569        clear(&self.carried);
13570        clear(&self.held_cache);
13571        clear(&self.children_cache);
13572        clear(&self.task_children);
13573        clear(&self.written);
13574        clear(&self.fields_cache);
13575        self.expected.end_command();
13576        Ok(())
13577    }
13578}
13579
13580/// Each project's sub-issues as one command read them, keyed by the selector they were asked
13581/// for under, beside the project that selector named; see `children_cache`.
13582type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
13583
13584/// One issue comment as the contract carries it.
13585///
13586/// `author` is absent both when GitHub answers `null` for an account that no longer exists
13587/// and when it answers an actor with no login, because either way the source did not say who
13588/// wrote it — which is what an absent author means, rather than an author called nothing.
13589///
13590/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
13591/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
13592/// and is read back with the marker taken out and reported as its origin.
13593fn comment_from(value: &Value) -> Result<Comment, SourceError> {
13594    Ok(Comment {
13595        origin: None,
13596        id: NativeId(required_str(value, "id")?.to_owned()),
13597        author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
13598            .map(str::to_owned),
13599        created_at: optional_time(value, "createdAt")?,
13600        updated_at: optional_time(value, "updatedAt")?,
13601        body: required_str(value, "body")?.to_owned(),
13602        url: optional_str(value, "url")?.map(str::to_owned),
13603    }
13604    .unmarked())
13605}
13606
13607/// The page of comments one issue node carries, resumed from `after`.
13608fn comment_page(
13609    node: &Value,
13610    issue: &str,
13611    after: Option<&str>,
13612) -> Result<Page<Comment>, SourceError> {
13613    let connection = node
13614        .get("comments")
13615        .filter(|value| !value.is_null())
13616        .ok_or_else(|| SourceError::Malformed {
13617            message: format!("GitHub issue {issue} answered with no comments connection"),
13618        })?;
13619    let items = optional_nodes(Some(connection), "issue comments")?
13620        .into_iter()
13621        .flatten()
13622        .map(comment_from)
13623        .collect::<Result<Vec<_>, _>>()?;
13624    let next = next_cursor(connection)?;
13625    if let Some(next) = &next {
13626        validate_cursor_progress(after, &next.0)?;
13627    }
13628    Ok(Page { items, next })
13629}
13630
13631/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
13632/// end — `None` when it carried none, or a page with more past it.
13633fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
13634    let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
13635        return Ok(None);
13636    };
13637    if next_cursor(connection)?.is_some() {
13638        return Ok(None);
13639    }
13640    Ok(Some(
13641        optional_nodes(Some(connection), "blocked-by issues")?
13642            .into_iter()
13643            .flatten()
13644            .cloned()
13645            .collect(),
13646    ))
13647}
13648
13649/// Where the recorded tail of a dependency walk resumes; see
13650/// [`GitHubProjectsSource::recorded_edges`].
13651const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
13652
13653/// The board text field this source keeps a copy's origin in.
13654///
13655/// Named after the key it holds, and held to that name by the guard below rather than by
13656/// a reader noticing.
13657const ORIGIN_FIELD: &str = "onetaskgraph.origin";
13658
13659/// The metadata key that field holds.
13660///
13661/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
13662/// constructs or interprets the qualified id it carries. This source names it only to
13663/// route it — a short, typed value belongs in a typed field rather than in the body slot
13664/// a caller's own prose shares.
13665///
13666/// Restated rather than imported, because no plugin crate may depend on the engine. What
13667/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
13668/// target in `check`: it reads the engine's own literal and fails naming the file and the
13669/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
13670/// that creates a second item every run instead of finding the one it wrote — and that is
13671/// too late to learn it.
13672const ORIGIN_KEY: &str = "onetaskgraph.origin";
13673
13674/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
13675///
13676/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
13677/// is derived from the far end, never written down on the near item — so only a forward
13678/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
13679/// it did not come from, and it is told so rather than answered with an empty page that
13680/// reads as a walk which ended.
13681fn recorded_offset(
13682    cursor: Option<&str>,
13683    direction: Direction,
13684) -> Result<Option<usize>, SourceError> {
13685    cursor
13686        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
13687        .map(|offset| {
13688            if direction != Direction::DependsOn {
13689                return Err(SourceError::Config {
13690                    message: format!(
13691                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
13692                         reverse dependency read never issues; resume it in the direction \
13693                         that reported it"
13694                    ),
13695                });
13696            }
13697            offset.parse().map_err(|_| SourceError::Config {
13698                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
13699            })
13700        })
13701        .transpose()
13702}
13703
13704fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
13705    let mut page = offset_page(edges, offset, limit.max(1));
13706    page.next = page
13707        .next
13708        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
13709    page
13710}
13711
13712/// The kind of one issue reached through a dependency connection.
13713///
13714/// The same questions the board scan asks, over the fields the dependency document
13715/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
13716/// then anything with sub-issues or the marker is a project.
13717///
13718/// # Errors
13719///
13720/// A far end this board holds as a document is refused rather than reported. The two
13721/// answers that are not refusals would both be wrong: reporting it as a task names an id
13722/// no task read of this source can find, and reporting it as a project names one no
13723/// project read can. There is no third value to return — `ItemKind` has no document
13724/// variant, because nothing may point at a document — so the relationship itself is what
13725/// the person is told about.
13726fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
13727    let id = required_str(value, "id")?;
13728    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
13729        return Err(SourceError::Refused {
13730            message: format!(
13731                "GitHub issue {id} is a document of this board — its title begins \
13732                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
13733                 on by one; next: remove that issue's blocking relationship on this board"
13734            ),
13735        });
13736    }
13737    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
13738    if parent.is_some() {
13739        return Ok(ItemKind::Task);
13740    }
13741    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
13742    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
13743        message: format!("GitHub issue {id}: {message}"),
13744    })?;
13745    let sub_issues = sub_issue_total(value)?;
13746    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
13747        ItemKind::Project
13748    } else {
13749        ItemKind::Task
13750    })
13751}
13752
13753/// The `stateInput` that closes an issue for `reason`.
13754fn closed_input(reason: ClosedState) -> Value {
13755    json!({"value":"CLOSED","stateReason":reason.reason()})
13756}
13757
13758/// The `IssueStateUpdateInput` one status target asks for.
13759///
13760/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
13761/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
13762/// a currently-closed issue: without that the item would read back `Unknown` and a copy
13763/// would report a change forever. A document has no status at all, and asks for neither.
13764fn state_input(target: Option<&StatusTarget>) -> Value {
13765    match target {
13766        Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
13767        Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
13768        // A document has no status, so a write of one says nothing about the issue's open
13769        // or closed state rather than forcing it open: `stateInput` is what carries that
13770        // instruction, and an explicit null asks for no change to it.
13771        None => Value::Null,
13772    }
13773}
13774
13775/// The metadata one write stores in the item's body slot.
13776///
13777/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
13778/// rather than carried: the kind marker so an empty project stays readable, the
13779/// repository list only when it is not exactly the issue's own repository, and the far
13780/// ends no relationship here can name.
13781///
13782/// The copy origin is the one typed field that is also mirrored here, and only as a
13783/// mirror: it lands in the board's origin field as well, which stays the one every reader
13784/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
13785/// and catches up with a write in seconds rather than minutes — can find the item by it.
13786/// A reader of the release before this one drops the slot's copy and reads the field, so an
13787/// item written here still reads with exactly one origin there.
13788fn slot_metadata(
13789    incoming: &Incoming<'_>,
13790    own_repository: Option<&Repository>,
13791    fallback: &[DependencyEdge],
13792) -> BTreeMap<String, Value> {
13793    let mut metadata = incoming.metadata.clone();
13794    match metadata.remove(ORIGIN_KEY) {
13795        Some(Value::String(origin)) if !origin.is_empty() => {
13796            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
13797        }
13798        _ => {}
13799    }
13800    match incoming.written.kind() {
13801        BoardKind::Work(kind) => metadata.insert(
13802            ItemKind::METADATA_KEY.to_owned(),
13803            Value::String(kind.marker().to_owned()),
13804        ),
13805        // A document is told by its title, so it carries no kind marker: that key names
13806        // what a dependency endpoint points at, and nothing may point at a document.
13807        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
13808    };
13809    incoming.classification.record(&mut metadata);
13810    let derivable = own_repository
13811        .map(|own| incoming.repositories == [own.clone()])
13812        .unwrap_or(incoming.repositories.is_empty());
13813    if derivable {
13814        metadata.remove(Repository::METADATA_KEY);
13815    } else {
13816        metadata.insert(
13817            Repository::METADATA_KEY.to_owned(),
13818            Value::Array(
13819                incoming
13820                    .repositories
13821                    .iter()
13822                    .map(|repository| Value::String(repository.as_str().to_owned()))
13823                    .collect(),
13824            ),
13825        );
13826    }
13827    // The typed lists are what land, whatever the caller's own metadata held under their
13828    // keys: a key of either name travelling beside the field would otherwise be a second
13829    // answer to the same question, and the field is the one the contract names.
13830    for (key, entries) in [
13831        (TaskRef::DELIVERS_KEY, incoming.delivers),
13832        (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
13833    ] {
13834        set_task_list(&mut metadata, key, entries);
13835    }
13836    record_edges(&mut metadata, fallback);
13837    metadata
13838}
13839
13840/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
13841/// one slot's metadata, or no such key when there are none.
13842fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
13843    if fallback.is_empty() {
13844        metadata.remove(DependencyEdge::RECORDED_KEY);
13845    } else {
13846        metadata.insert(
13847            DependencyEdge::RECORDED_KEY.to_owned(),
13848            Value::Array(
13849                fallback
13850                    .iter()
13851                    .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
13852                    .collect(),
13853            ),
13854        );
13855    }
13856}
13857
13858/// Every label one item carries, from its content's own connection and nowhere else.
13859///
13860/// There is no second place to read one from: no document this source sends selects the
13861/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
13862/// cannot carry one at all. The module documentation records the three schema facts that
13863/// settle it.
13864fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
13865    optional_nodes(content.get("labels"), "content labels")?
13866        .into_iter()
13867        .flatten()
13868        .map(|v| {
13869            Ok(Label {
13870                id: NativeId(required_str(v, "id")?.to_owned()),
13871                name: required_str(v, "name")?.to_owned(),
13872                color: optional_str(v, "color")?.map(str::to_owned),
13873            })
13874        })
13875        .collect()
13876}
13877
13878/// Each board field value one item holds, as the change token's facets name it: the field's
13879/// name, with the single-select option's name or the text. A value of a type no read here
13880/// selects, and one naming no field, says nothing a person can rename and is left out.
13881fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
13882    field_values
13883        .iter()
13884        .filter_map(|value| {
13885            let field = value.pointer("/field/name")?.as_str()?;
13886            let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
13887            Some((field.to_owned(), held.to_owned()))
13888        })
13889        .collect()
13890}
13891
13892/// The digest of the facets of the issue `content`, whose board item's values are
13893/// `field_values` — or `None` when the response did not carry, whole, every member they are read
13894/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
13895/// label, and every field value.
13896///
13897/// What a write's own response spells its token from, by exactly the rules a read takes them by.
13898fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
13899    let ended = |connection: &Value| {
13900        connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
13901    };
13902    let labels = content.get("labels").filter(|labels| ended(labels))?;
13903    if !ended(field_values) {
13904        return None;
13905    }
13906    let parent = content.get("parent")?;
13907    let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
13908    let kind = BoardKind::of(
13909        content.get("title")?.as_str()?,
13910        !parent.is_null(),
13911        sub_issue_total(content).ok()?,
13912        ItemKind::from_metadata(&slot).ok()?,
13913    );
13914    let labels = self::labels(&json!({"labels": labels})).ok()?;
13915    let nodes = field_values.get("nodes")?.as_array()?;
13916    Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
13917}
13918
13919/// The definition of each board field one item's values are values of, in the shape a read
13920/// of the board's own `fields` gives one.
13921///
13922/// A value names its field through a fragment on that field's own type, so the type is
13923/// known from which kind of value it is: a single-select value's field is a
13924/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
13925/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
13926fn field_definitions(field_values: &[Value]) -> Vec<Value> {
13927    field_values
13928        .iter()
13929        .filter_map(|value| {
13930            let field = value.get("field")?.as_object()?;
13931            field.get("id")?.as_str().filter(|id| !id.is_empty())?;
13932            let typename = if value.get("text").is_some() {
13933                "ProjectV2Field"
13934            } else if value.get("name").is_some() {
13935                "ProjectV2SingleSelectField"
13936            } else {
13937                return None;
13938            };
13939            let mut defined = field.clone();
13940            defined.insert("__typename".to_owned(), json!(typename));
13941            // Only a text field holds a text value, so the value says the field's type too.
13942            if typename == "ProjectV2Field" {
13943                defined.insert("dataType".to_owned(), json!("TEXT"));
13944            }
13945            Some(Value::Object(defined))
13946        })
13947        .collect()
13948}
13949
13950fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
13951    let Some(node) = field_values
13952        .iter()
13953        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
13954    else {
13955        return Ok(None);
13956    };
13957    Ok(optional_str(node, "text")?.map(str::to_owned))
13958}
13959
13960fn valid_github_owner(owner: &str) -> bool {
13961    !owner.is_empty()
13962        && owner.len() <= 39
13963        && !owner.starts_with('-')
13964        && !owner.ends_with('-')
13965        && !owner.contains("--")
13966        && owner
13967            .bytes()
13968            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
13969}
13970
13971/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
13972/// neither of the two names a path segment already means.
13973fn valid_github_repository_name(name: &str) -> bool {
13974    !name.is_empty()
13975        && name.len() <= 100
13976        && name != "."
13977        && name != ".."
13978        && name
13979            .bytes()
13980            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
13981}
13982
13983fn valid_environment_name(name: &str) -> bool {
13984    let mut bytes = name.bytes();
13985    bytes
13986        .next()
13987        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
13988        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
13989}
13990
13991/// How many sub-issues one issue has.
13992///
13993/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
13994/// absent or non-integer one is a response this source cannot read — and reading it as
13995/// zero would classify a project as a task, which is exactly the mistake the marker
13996/// exists to keep from happening quietly.
13997fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
13998    let summary = issue
13999        .get("subIssuesSummary")
14000        .ok_or_else(|| SourceError::Malformed {
14001            message: "GitHub issue is missing subIssuesSummary".into(),
14002        })?;
14003    summary
14004        .get("total")
14005        .and_then(Value::as_u64)
14006        .ok_or_else(|| SourceError::Malformed {
14007            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
14008        })
14009}
14010
14011/// One issue's own `number`.
14012///
14013/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
14014/// an issue in this module asks for it. So a read of one that comes back without it, or
14015/// with something that is not an unsigned integer, is a response this source cannot read —
14016/// absence here is **not** "this issue has no number". A draft is the content that has
14017/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
14018/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
14019fn issue_number(issue: &Value) -> Result<u64, SourceError> {
14020    issue
14021        .get("number")
14022        .and_then(Value::as_u64)
14023        .ok_or_else(|| SourceError::Malformed {
14024            message: "GitHub issue number is missing or is not an unsigned integer".into(),
14025        })
14026}
14027
14028/// The `number` a creating mutation answered with, and `None` when it answered without one;
14029/// why a missing one is tolerated is at the call in `create_and_file_issue`.
14030fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
14031    match created.get("number") {
14032        None | Some(Value::Null) => Ok(None),
14033        Some(value) => value
14034            .as_u64()
14035            .map(Some)
14036            .ok_or_else(|| SourceError::Malformed {
14037                message: "GitHub created issue number is not an unsigned integer".into(),
14038            }),
14039    }
14040}
14041
14042fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
14043    value
14044        .get(field)
14045        .and_then(Value::as_str)
14046        .ok_or_else(|| SourceError::Malformed {
14047            message: format!("GitHub response is missing string field {field}"),
14048        })
14049}
14050
14051fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
14052    let found = required_str(value, field)?;
14053    if found.trim().is_empty() {
14054        return Err(SourceError::Malformed {
14055            message: format!("GitHub response has blank string field {field}"),
14056        });
14057    }
14058    Ok(found)
14059}
14060
14061/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
14062/// needs one — Linear spells them too, in its own description field.
14063///
14064/// Restated rather than shared, because a plugin crate depends on the contract crate and
14065/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
14066/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
14067/// source round-trips its own writes perfectly well under its own spelling.
14068const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
14069const METADATA_CLOSE: &str = "\n-->";
14070
14071/// What the composer puts between a non-empty visible body and the slot, and the one thing
14072/// the parser takes off the visible body when it takes the slot off — exactly once, so every
14073/// other trailing byte of the body comes back as it was written.
14074// 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.
14075const METADATA_SEPARATOR: &str = "\n\n";
14076
14077/// The visible body and the metadata slot at the end of it.
14078///
14079/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
14080/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
14081/// own content and is left alone. The visible body is everything before the slot less the
14082/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
14083fn metadata_body(
14084    body: Option<String>,
14085) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
14086    let Some(body) = body else {
14087        return Ok((None, BTreeMap::new()));
14088    };
14089    let Some(slot) = slot_span(&body)? else {
14090        return Ok((Some(body), BTreeMap::new()));
14091    };
14092    let metadata =
14093        serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
14094            SourceError::Malformed {
14095                message: format!(
14096                    "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
14097                ),
14098            }
14099        })?;
14100    let before = &body[..slot.start];
14101    let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
14102    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
14103}
14104
14105/// Where the metadata slot sits in one body, as byte offsets into it.
14106struct SlotSpan {
14107    /// Where [`METADATA_OPEN`] begins.
14108    start: usize,
14109    /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
14110    encoded_start: usize,
14111    /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
14112    encoded_end: usize,
14113    /// Just past [`METADATA_CLOSE`].
14114    end: usize,
14115}
14116
14117/// The slot at the very end of `body`, or `None` when it has none.
14118///
14119/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
14120/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
14121/// slot.
14122fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
14123    let Some(start) = body.rfind(METADATA_OPEN) else {
14124        return Ok(None);
14125    };
14126    let encoded_start = start + METADATA_OPEN.len();
14127    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
14128        return Err(SourceError::Malformed {
14129            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
14130        });
14131    };
14132    let encoded_end = encoded_start + relative_end;
14133    let end = encoded_end + METADATA_CLOSE.len();
14134    if !body[end..].trim().is_empty() {
14135        return Ok(None);
14136    }
14137    Ok(Some(SlotSpan {
14138        start,
14139        encoded_start,
14140        encoded_end,
14141        end,
14142    }))
14143}
14144
14145/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
14146/// slot as it was.
14147///
14148/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
14149/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
14150/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
14151/// or alone in an empty body — and a body with no slot that is given no metadata is
14152/// returned as it is.
14153fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
14154    let encoded = if metadata.is_empty() {
14155        None
14156    } else {
14157        Some(
14158            serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
14159                message: error.to_string(),
14160            })?,
14161        )
14162    };
14163    Ok(match (slot_span(body)?, encoded) {
14164        (Some(slot), Some(encoded)) => format!(
14165            "{}{encoded}{}",
14166            &body[..slot.encoded_start],
14167            &body[slot.encoded_end..]
14168        ),
14169        (Some(slot), None) => {
14170            let before = &body[..slot.start];
14171            format!(
14172                "{}{}",
14173                before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
14174                &body[slot.end..]
14175            )
14176        }
14177        (None, None) => body.to_owned(),
14178        (None, Some(encoded)) if body.is_empty() => {
14179            format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14180        }
14181        (None, Some(encoded)) => {
14182            format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14183        }
14184    })
14185}
14186
14187/// `body` with everything before its metadata slot replaced by `content`, and the slot
14188/// itself kept byte for byte.
14189///
14190/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
14191/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
14192/// `content` is empty — so a read of the result reports `content` as the visible body and
14193/// the slot's metadata exactly as it was.
14194fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
14195    let Some(slot) = slot_span(body)? else {
14196        return Ok(content.to_owned());
14197    };
14198    let kept = &body[slot.start..];
14199    Ok(if content.is_empty() {
14200        kept.to_owned()
14201    } else {
14202        format!("{content}{METADATA_SEPARATOR}{kept}")
14203    })
14204}
14205
14206/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
14207fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
14208    if entries.is_empty() {
14209        metadata.remove(key);
14210    } else {
14211        metadata.insert(
14212            key.to_owned(),
14213            Value::Array(
14214                entries
14215                    .iter()
14216                    .map(|entry| Value::String(entry.as_str().to_owned()))
14217                    .collect(),
14218            ),
14219        );
14220    }
14221}
14222
14223fn compose_body(
14224    content: Option<&str>,
14225    metadata: &BTreeMap<String, Value>,
14226) -> Result<Option<String>, SourceError> {
14227    let visible = content.unwrap_or_default();
14228    if metadata.is_empty() {
14229        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
14230    }
14231    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
14232        message: error.to_string(),
14233    })?;
14234    Ok(Some(if visible.is_empty() {
14235        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14236    } else {
14237        format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14238    }))
14239}
14240
14241fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
14242    value
14243        .get(field)
14244        .and_then(Value::as_bool)
14245        .ok_or_else(|| SourceError::Malformed {
14246            message: format!("GitHub response is missing boolean field {field}"),
14247        })
14248}
14249fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
14250    match value.get(field) {
14251        None | Some(Value::Null) => Ok(None),
14252        Some(value) => value
14253            .as_str()
14254            .map(Some)
14255            .ok_or_else(|| SourceError::Malformed {
14256                message: format!("GitHub response field {field} is not a string or null"),
14257            }),
14258    }
14259}
14260fn optional_nodes<'a>(
14261    connection: Option<&'a Value>,
14262    name: &str,
14263) -> Result<Option<&'a Vec<Value>>, SourceError> {
14264    match connection {
14265        None | Some(Value::Null) => Ok(None),
14266        Some(value) => value
14267            .get("nodes")
14268            .and_then(Value::as_array)
14269            .map(Some)
14270            .ok_or_else(|| SourceError::Malformed {
14271                message: format!("GitHub {name}.nodes is not an array"),
14272            }),
14273    }
14274}
14275fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
14276    let page_info = connection
14277        .get("pageInfo")
14278        .ok_or_else(|| SourceError::Malformed {
14279            message: format!("GitHub {name} has no pageInfo"),
14280        })?;
14281    if required_bool(page_info, "hasNextPage")? {
14282        return Err(SourceError::Malformed {
14283            message: format!(
14284                "GitHub {name} exceeds the supported nested connection size of {size}"
14285            ),
14286        });
14287    }
14288    Ok(())
14289}
14290fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
14291    optional_str(value, field)?
14292        .map(|timestamp| {
14293            timestamp.parse().map_err(|error| SourceError::Malformed {
14294                message: format!("GitHub response field {field} is not a timestamp: {error}"),
14295            })
14296        })
14297        .transpose()
14298}
14299fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
14300    if page.limit == 0 {
14301        Err(SourceError::Config {
14302            message: "page limit must be at least 1".into(),
14303        })
14304    } else {
14305        Ok(())
14306    }
14307}
14308fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
14309    let page = connection
14310        .get("pageInfo")
14311        .filter(|value| value.is_object())
14312        .ok_or_else(|| SourceError::Malformed {
14313            message: "GitHub connection is missing pageInfo".into(),
14314        })?;
14315    if required_bool(page, "hasNextPage")? {
14316        let cursor = required_str(page, "endCursor")?;
14317        validate_cursor_progress(None, cursor)?;
14318        Ok(Some(Cursor(cursor.into())))
14319    } else {
14320        Ok(None)
14321    }
14322}
14323fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
14324    if next.is_empty() || previous == Some(next) {
14325        Err(SourceError::Malformed {
14326            message: "GitHub pagination cursor is empty or did not advance".into(),
14327        })
14328    } else {
14329        Ok(())
14330    }
14331}
14332/// The version of this plugin's opaque narrowing-search cursor.
14333pub const SEARCH_CURSOR_VERSION: u32 = 4;
14334
14335#[derive(Serialize, Deserialize)]
14336#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
14337enum SearchConnection {
14338    Initial {},
14339    Continuing { after: Cursor },
14340    Exhausted {},
14341}
14342impl SearchConnection {
14343    fn after(&self) -> Option<&str> {
14344        match self {
14345            Self::Continuing { after } => Some(&after.0),
14346            _ => None,
14347        }
14348    }
14349    fn exhausted(&self) -> bool {
14350        matches!(self, Self::Exhausted { .. })
14351    }
14352    /// Whether a cursor naming this position, `offset` rows into its page, is one this
14353    /// plugin could have handed out: a page is resumed only part of the way through it — an
14354    /// offset of a whole page or more would skip rows nobody was given — an initial page
14355    /// only once some of it was handed out, and an exhausted connection has no page to be
14356    /// part of the way through.
14357    fn valid_resume(&self, offset: usize) -> bool {
14358        let within = offset < SEARCH_PAGE_SIZE as usize;
14359        match self {
14360            Self::Initial { .. } => offset > 0 && within,
14361            Self::Continuing { after } => !after.0.is_empty() && within,
14362            Self::Exhausted { .. } => offset == 0,
14363        }
14364    }
14365}
14366
14367/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
14368#[derive(Serialize, Deserialize)]
14369#[serde(deny_unknown_fields)]
14370struct SearchPosition {
14371    version: u32,
14372    connection: SearchConnection,
14373    /// How many rows of the page `connection` starts were already handed out.
14374    #[serde(default, skip_serializing_if = "is_zero")]
14375    offset: usize,
14376    #[serde(default, skip_serializing_if = "Vec::is_empty")]
14377    seen: Vec<NativeId>,
14378    #[serde(default, skip_serializing_if = "Vec::is_empty")]
14379    own: Vec<NativeId>,
14380}
14381impl Default for SearchPosition {
14382    fn default() -> Self {
14383        Self {
14384            version: SEARCH_CURSOR_VERSION,
14385            connection: SearchConnection::Initial {},
14386            offset: 0,
14387            seen: Vec::new(),
14388            own: Vec::new(),
14389        }
14390    }
14391}
14392
14393fn is_zero(offset: &usize) -> bool {
14394    *offset == 0
14395}
14396
14397fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
14398    cursor.map_or(Ok(0), |c| {
14399        c.0.parse().map_err(|_| SourceError::Config {
14400            message: "page cursor is invalid".into(),
14401        })
14402    })
14403}
14404fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
14405    if offset > items.len() {
14406        return Page::last(vec![]);
14407    }
14408    let tail = items.split_off(offset);
14409    let mut selected = tail;
14410    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
14411    selected.truncate(limit);
14412    Page {
14413        items: selected,
14414        next,
14415    }
14416}
14417
14418impl GitHubProjectsSource {
14419    async fn write_task_assets(
14420        &self,
14421        write: &ItemWrite<Task>,
14422        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
14423    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14424        self.write_item(
14425            &self.task_incoming(write, assets)?,
14426            write.target.as_ref(),
14427            &write.depends_on,
14428        )
14429        .await
14430    }
14431
14432    /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
14433    /// are known to be ones this source can write.
14434    fn task_incoming<'a>(
14435        &self,
14436        write: &'a ItemWrite<Task>,
14437        assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
14438    ) -> Result<Incoming<'a>, SourceError> {
14439        let near = write.target.as_ref().unwrap_or(&write.item.id);
14440        for (key, entries) in [
14441            (TaskRef::DELIVERS_KEY, &write.item.delivers),
14442            (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
14443        ] {
14444            TaskRef::listed(key, near, Some(&self.name), entries.clone())
14445                .map_err(|message| SourceError::Refused { message })?;
14446        }
14447        if self.priorities.is_none() && write.item.priority != Priority::None {
14448            return Err(self.holds_no_priority());
14449        }
14450        Ok(Incoming {
14451            written: Written::Work(ItemKind::Task, &write.item.status),
14452            title: &write.item.title,
14453            content: write.item.content.as_deref(),
14454            assets,
14455            labels: &write.item.labels,
14456            metadata: &write.item.metadata,
14457            repositories: &write.item.repositories,
14458            classification: write.item.classification,
14459            parent: write.item.project.as_ref(),
14460            delivers: &write.item.delivers,
14461            delivered_by: &write.item.delivered_by,
14462            priority: self.priorities.as_ref().map(|_| write.item.priority),
14463        })
14464    }
14465    async fn write_document_assets(
14466        &self,
14467        write: &ItemWrite<Document>,
14468        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
14469    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14470        // A document takes part in no dependency graph, so there is no far end to write
14471        // natively and none to record: a caller naming one is told so rather than having it
14472        // stored under the reserved key, where a later read would report an edge the
14473        // contract says cannot exist.
14474        if !write.depends_on.is_empty() {
14475            return Err(SourceError::Refused {
14476                message: format!(
14477                    "this write names {} dependencies for a document, and a document takes \
14478                     part in no dependency graph; next: put the dependency on the task or \
14479                     project the document is about",
14480                    write.depends_on.len()
14481                ),
14482            });
14483        }
14484        self.write_item(
14485            &Incoming {
14486                written: Written::Document,
14487                title: &write.item.title,
14488                content: write.item.content.as_deref(),
14489                assets,
14490                labels: &write.item.labels,
14491                metadata: &write.item.metadata,
14492                repositories: &write.item.repositories,
14493                classification: write.item.classification,
14494                parent: write.item.project.as_ref(),
14495                delivers: &[],
14496                delivered_by: &[],
14497                priority: None,
14498            },
14499            write.target.as_ref(),
14500            &[],
14501        )
14502        .await
14503    }
14504}
14505
14506/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
14507/// itself, for the two things no journey can observe.
14508///
14509/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
14510/// with and without the call, that a settlement, a board listing and a metadata search each
14511/// read afresh after it — the resolved records, the written-item overlay, the board and its
14512/// search, and the narrowed searches. What they cannot reach is the held field definitions,
14513/// because a status write naming an option a person deleted is refused the same whether or
14514/// not the list is held, and a poisoned lock, because nothing outside the source can panic
14515/// while one of its locks is held. So these assert those directly, and every other holder
14516/// beside them so a holder added later without a clear in the call fails here.
14517#[cfg(test)]
14518mod end_command_tests {
14519    use super::*;
14520
14521    struct Token;
14522
14523    impl SecretResolver for Token {
14524        fn get(&self, var: &str) -> Option<SecretString> {
14525            (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
14526        }
14527    }
14528
14529    fn source() -> GitHubProjectsSource {
14530        let config = serde_json::from_value(json!({
14531            "owner": "octo-org", "project_number": 7, "repository": "acme/work",
14532            // Nothing here is sent: the source is only built and its state inspected.
14533            "endpoint": "http://127.0.0.1:9/graphql",
14534        }))
14535        .expect("a usable configuration");
14536        GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
14537            .expect("the source builds")
14538    }
14539
14540    /// One issue as a board read answers it.
14541    fn resolved(source: &GitHubProjectsSource) -> Resolved {
14542        source
14543            .resolve(&json!({
14544                "id": "ITEM-1",
14545                "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
14546                            "body": "what a person may since have edited", "state": "OPEN",
14547                            "stateReason": null, "url": null, "number": 1,
14548                            "subIssuesSummary": {"total": 0},
14549                            "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
14550                "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
14551            }))
14552            .expect("the item reads")
14553            .expect("an issue")
14554    }
14555
14556    /// Hold something in every holder the call clears, and the repository id it keeps.
14557    fn fill(source: &GitHubProjectsSource) {
14558        let item = resolved(source);
14559        source.created.lock().unwrap().push(item.clone());
14560        source.updated.lock().unwrap().push(item.clone());
14561        *source.board_cache.lock().unwrap() = Some(Board {
14562            id: "PVT-board".into(),
14563            fields: json!({"nodes": []}),
14564            items: vec![item.clone()],
14565        });
14566        *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
14567        source
14568            .narrowed_cache
14569            .lock()
14570            .unwrap()
14571            .insert("status:todo".into(), vec![item.clone()]);
14572        source.children_cache.lock().unwrap().insert(
14573            NativeId("P-1".into()),
14574            (NativeId("P-1".into()), vec![item.clone()]),
14575        );
14576        source
14577            .search_next
14578            .lock()
14579            .unwrap()
14580            .insert("status:todo".into(), Some("cursor".into()));
14581        source
14582            .resolved_cache
14583            .lock()
14584            .unwrap()
14585            .insert(item.id.clone(), item);
14586        *source.fields_cache.lock().unwrap() = Some(BoardFields {
14587            id: BoardId::parse("PVT-board").unwrap(),
14588            fields: json!({"nodes": []}),
14589        });
14590        source
14591            .repository_cache
14592            .lock()
14593            .unwrap()
14594            .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
14595    }
14596
14597    fn assert_dropped(source: &GitHubProjectsSource) {
14598        assert!(source.created().unwrap().is_empty(), "created");
14599        assert!(source.updated().unwrap().is_empty(), "updated");
14600        assert!(source.board_cache().unwrap().is_none(), "board");
14601        assert!(source.search_cache.lock().unwrap().is_none(), "search");
14602        assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
14603        assert!(
14604            source.children_cache.lock().unwrap().is_empty(),
14605            "project children"
14606        );
14607        assert!(
14608            source.search_next.lock().unwrap().is_empty(),
14609            "search paging"
14610        );
14611        assert!(
14612            source.resolved_cache().unwrap().is_empty(),
14613            "resolved records"
14614        );
14615        assert!(source.fields_cache().unwrap().is_none(), "board fields");
14616        assert_eq!(
14617            source.repository_cache().unwrap().len(),
14618            1,
14619            "a repository's node id stays valid and is kept"
14620        );
14621    }
14622
14623    fn end(source: &GitHubProjectsSource) {
14624        tokio::runtime::Builder::new_current_thread()
14625            .build()
14626            .unwrap()
14627            .block_on(source.end_command())
14628            .expect("the command ends");
14629    }
14630
14631    #[test]
14632    fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
14633        let source = source();
14634        fill(&source);
14635        end(&source);
14636        assert_dropped(&source);
14637    }
14638
14639    #[test]
14640    fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
14641        fn poison<T: Send>(held: &Mutex<T>) {
14642            std::thread::scope(|scope| {
14643                let _ = scope
14644                    .spawn(|| {
14645                        let _guard = held.lock().unwrap();
14646                        panic!("a failure while the lock is held");
14647                    })
14648                    .join();
14649            });
14650            assert!(held.is_poisoned());
14651        }
14652        let source = source();
14653        fill(&source);
14654        poison(&source.created);
14655        poison(&source.updated);
14656        poison(&source.board_cache);
14657        poison(&source.search_cache);
14658        poison(&source.narrowed_cache);
14659        poison(&source.search_next);
14660        poison(&source.resolved_cache);
14661        poison(&source.fields_cache);
14662        assert!(
14663            source.resolved_cache().is_err(),
14664            "a poisoned lock is refused before the call"
14665        );
14666        end(&source);
14667        assert_dropped(&source);
14668    }
14669}
14670
14671#[cfg(test)]
14672mod unprojectable_fields_tests {
14673    use super::*;
14674
14675    /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
14676    fn documented() -> std::collections::BTreeSet<String> {
14677        let document = std::fs::read_to_string(
14678            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
14679        )
14680        .expect("docs/metadata.md is readable");
14681        let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
14682        let lead = "a `field` equal, ignoring case, to ";
14683        let start = document.find(lead).expect("the refused-field sentence") + lead.len();
14684        let sentence = &document[start..];
14685        let sentence = &sentence[..sentence
14686            .find("; and two entries naming one field")
14687            .expect("the refused-field sentence ends at the duplicate rule")];
14688        let (own, github) = sentence
14689            .split_once(" or a field GitHub owns on every board (")
14690            .expect("the sentence names GitHub's own fields");
14691        own.split('`')
14692            .skip(1)
14693            .step_by(2)
14694            .chain(
14695                github
14696                    .strip_suffix(')')
14697                    .expect("GitHub's own fields close the sentence")
14698                    .split(", "),
14699            )
14700            .map(str::to_owned)
14701            .collect()
14702    }
14703
14704    #[test]
14705    fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
14706        let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
14707            .iter()
14708            .map(|&name| name.to_owned())
14709            .collect();
14710        assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
14711        let instance = SourceName::new("work").unwrap();
14712        for name in UNPROJECTABLE_FIELDS {
14713            for spelled in [name.to_owned(), name.to_uppercase()] {
14714                let entry = MetadataFieldConfig {
14715                    field: spelled.clone(),
14716                    key: "team.machine".to_owned(),
14717                    path: Vec::new(),
14718                };
14719                let refused = MetadataField::resolve(vec![entry], &instance)
14720                    .expect_err("an unprojectable field is refused");
14721                assert!(
14722                    refused.to_string().contains(&format!("{name:?}")),
14723                    "{spelled}: {refused}"
14724                );
14725            }
14726        }
14727    }
14728}