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//! | 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 |
322//! | record only | 1 | ISSUE |
323//!
324//! <!-- github-search-paging:start -->
325//! Board-scoped text, metadata, project-name and comment-activity searches send every
326//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
327//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
328//! needs rows. A page is never resized to the rows still needed: GitHub orders one
329//! search differently at different page sizes, so one fixed size makes a paged walk
330//! send exactly the requests one whole read sends, and the answer's order is the order
331//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
332//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
333//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
334//! returned and fetched pages: a limit is sliced from the pages it needs, and local
335//! confirmation can require more candidates than matching rows. Walking all pages
336//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
337//! cursor and how far into that page the last answer stopped, and resumes in the same
338//! process or a new one, without duplicates or gaps. It carries no rows: one process
339//! sends each page's search once, and a new process re-reads only the page it resumes
340//! in, then sends a further page once, never as a re-read, only when its limit still
341//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
342//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
343//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
344//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
345//! not required to include the original process's writes still omitted by the index.
346//! <!-- github-search-paging:end -->
347//!
348//! The board half of an issue — its board item's id, its `Status` option and this
349//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
350//! an item reached any of those ways resolves through the same
351//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
352//! same status, the same labels and the same qualified id. That connection comes back a
353//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
354//! on the page in hand and — only if that page reports more of the connection — in the
355//! last row's read of that one issue's memberships, resumed from the page's own cursor and
356//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
357//! report, which is what keeps an id naming another repository's issue from being answered
358//! as an item of this board; and because the page is where the search starts rather than
359//! where it ends, that answer is one about a connection read to exhaustion and never about
360//! an unread page. Nothing costs the extra read but an issue on more boards than a page
361//! holds: an issue this board really does not hold reports no next page, so its
362//! memberships are already exhausted where they arrived.
363//!
364//! **No document here selects the board's own `Labels` field, and nothing is lost by
365//! that.** An item's labels are read from its content alone, wherever that content is
366//! reached: the three documents above select `Issue.labels` on the fragment, and
367//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
368//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
369//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
370//! create one, and `ProjectV2FieldValue` — the whole of what
371//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
372//! it from the content, for every content type it exists on, and there is nothing it can
373//! hold that the content does not already say: for an `Issue` it *is* that issue's own
374//! labels, so selecting it beside them unions a set with itself.
375//!
376//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
377//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
378//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
379//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
380//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
381//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
382//! label set, and that set is the fixture issue's own, by
383//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
384//! item whose content is a draft reports an empty set, by
385//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
386//! selection is held over [`graphql::DOCUMENTS`] by
387//! `no_document_selects_the_boards_own_labels_field`.
388//!
389//! The whole-board row is still the board's own item connection, and deliberately: a
390//! **draft** board item is not an issue, so no search can list one, and the reads that have
391//! to answer for the whole board are the ones whose cost is the board's size anyway.
392//!
393//! **A question about one item this source already names by id never lists the board.**
394//! Whether that item is on this board, and what its board fields are, is answered by reading
395//! that item — its own `Issue.projectItems`, walked to exhaustion by
396//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
397//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
398//! holds. That covers a write's destination, the project a new item is filed under, a
399//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
400//! and the delete that takes back an item a copy made. What such a write needs of the board
401//! and the item does not carry — the board's id, the `Status` and origin field definitions —
402//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
403//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
404//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
405//! minutes. Scanning this host's 842-item board has refused a document copy and an update
406//! even though the items' own reads named that board. A scan there gives the wrong answer
407//! as well as paying for every page. So a `board.items` lookup does not belong on any of
408//! those paths.
409//!
410//! **What a read may return is capped too, and that cap is on the document rather than on
411//! the board.** GitHub limits the number of nodes **one query may return** to
412//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
413//! an error naming the connection the count crossed at, not a slow or a partial result.
414//! Every board this source reads is refused the same way, so no board is too big for these
415//! documents and none is small enough to save one that is over.
416//!
417//! The count is arithmetic over the document's own text: each connection contributes the
418//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
419//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
420//! restate them — `github-graphql-node-count` implements them, and
421//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
422//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
423//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
424//! same text on every run and fails naming any that reaches the limit — so a connection
425//! added to a shared fragment is caught there rather than by GitHub.
426//!
427//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
428//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
429//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
430//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
431//! effectively squared there, which is why it is the one the limit is most sensitive to.
432//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
433//! page of memberships misses is recovered by one further read rather than refused, so it
434//! buys a bound every read pays for at the price of a request only a multi-board issue
435//! pays.
436//!
437//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
438//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
439//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
440//! `cost` is rate-limit points, metered per hour across everything one credential does; it
441//! is what the two limiters [`Limiter`] tells apart meter, and a document under
442//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
443//! that second number, and `tests/point_cost.rs` pins every document in
444//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
445//! one under, the pin itself is the check. The credentialed lane reconciles both figures
446//! against GitHub's own, off a probe it already sends.
447//!
448//! **What is pinned that way is a per-document price and never a session's.** The record in
449//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
450//! **requests** and **worst-case nodes** — and neither is points. What one whole session
451//! consumes of the hourly point allowance is observable only from a credentialed run's own
452//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
453//! and what `tests/live.rs` prints at the end of every run.
454//!
455//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
456//!
457//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
458//! enumerations of a board can supply one alone.** Resolving a node id is strongly
459//! consistent, so a read by id and a project's own sub-issues are already current. The
460//! other two are not, and they are behind by different amounts and in different directions:
461//!
462//! - GitHub's **issue search** is an index and answers a write made moments ago with the
463//!   value from before it — usually for a second or two.
464//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
465//!   on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
466//!   content withheld, absent, with the connection walked to its own `hasNextPage: false` —
467//!   for *minutes*, while `Issue.projectItems` names the same membership at once.
468//!
469//! That second one is a measurement rather than a caution. This repository's own
470//! credentialed journey writes a project and waits for the board to report it, then writes a
471//! task and waits for the same thing seconds later on the same board: the project wait is
472//! answered through the search and converged in two or three attempts in each of three runs,
473//! and the task wait is answered through `ProjectV2.items` and converged in none of them
474//! inside thirty. Separately, an item added to a second and larger board was read back by
475//! `Issue.projectItems` on that board's own id while every one of that connection's nine
476//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
477//! through the lagging one alone is what had a board read deny an issue that had certainly
478//! landed on it.
479//!
480//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
481//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
482//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
483//! search reports what the projection is behind on. What closes the last
484//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
485//! source answers is completed with what this process itself wrote, so an item created
486//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
487//! nothing is written down, and the record dies with the process. **A wait that has to
488//! observe GitHub's own data cannot be answered from that record** — which is why the
489//! credentialed journey asks through a source built afresh, and why the union above rather
490//! than a longer wait is what makes such a wait converge.
491//!
492//! **A narrowed read is the same bargain, stated for each of the three predicates it
493//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
494//! than walking the board, and every such answer is completed with what this process wrote —
495//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
496//! each filtered by the same predicates as the rest — so an item this command wrote a moment
497//! ago is returned by a query that matches it whether or not the index has caught up. An item
498//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
499//! consistent. What is left is stated rather than papered over:
500//!
501//! | Read | Finds | Behind by |
502//! | --- | --- | --- |
503//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
504//! | 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 |
505//! | 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 |
506//! | origin, third read | this process's own writes | nothing |
507//!
508//! So an origin carrier another process added within the last second or two, before either
509//! index has it, can be missing from an origin query, and one written by the release before
510//! this one — its origin in the field alone — can be missing for as long as the board's own
511//! item connection is behind on it. A copy that must not duplicate its own earlier write
512//! relies on the link it records, not on either index. **A board draft is not an issue**, so
513//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
514//! lists one, the origin lookup drops any the board's own field filter names, and one this
515//! process wrote is not added back either.
516//!
517//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
518//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
519//! body's metadata slot, so the issue search can find it in seconds. The field is
520//! authoritative: this source reads an item's origin from the field alone, so a slot that
521//! disagrees with it, or holds one where the field holds none, is never read as a second
522//! origin — and the release before this one reads the slot, drops that key's copy for the
523//! field's, and sees the same one origin.
524//!
525//! Filtering happens before paging, so a page of a filtered result is a page of the
526//! survivors rather than the survivors of a page. Label matching and the substring rule a
527//! text candidate is confirmed by answer the same question the same way the local Markdown
528//! source's do; which candidates a text search has to confirm is GitHub's word match, which
529//! is the one place the two sources can answer the same text differently.
530//!
531//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
532//! has one source, `capabilities`, and the note above is the reasoning behind it rather
533//! than a second copy of it: without the three facts recorded here a reader takes the
534//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
535//! crate's own capabilities test, which pins every field of it against a fully spelled-out
536//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
537//! fails to compile there rather than going unasserted. -->
538//! The fixture-server tests above run wherever this crate is selected; the credentialed
539//! lane runs in the same required check, beside them, and can fail it — it verifies the
540//! 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,
541//! one filed under neither, a label on one of the three and a closed status on another —
542//! because that shape is what tells an honoured predicate from an ignored one: a board
543//! holding a single project answers a project filter the same way whether or not this
544//! source applies it, which is exactly how the defect above went unseen.
545//!
546//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
547//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
548//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
549//! session or request, independently of the live demand. Fix that variable in the machine's
550//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
551//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
552//! policy crate; absent credentials or nominations otherwise skip.
553//!
554//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
555//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
556//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
557//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
558//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
559//! after its run reads back as `completed`, immediately before the listed-artifact batch.
560//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
561//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
562//! and the board's `onetaskgraph.origin` field untouched.
563//!
564//! # What a session of requests costs, and where the report is
565//!
566//! This source records **every** request it sends into [`accounting::Accounting`], at
567//! `send_once` — the one place a request leaves this crate, which is why a read path added
568//! later is counted without anybody remembering to count it. That is the whole of what this
569//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
570//! session's spend is arrived at, and what it deliberately does not know are set out.
571//!
572//! What one whole session of the live journey costs, counted that way against this crate's
573//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
574//! reduction it came out of, and with what it does and does not say about rate-limit points.
575//!
576//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
577//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
578//! code path — no environment variable, no feature, no build configuration — because an
579//! instrument nobody switches on measures nothing, and
580//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
581//! source's counts the whole session rather than this source's share. The credentialed lane
582//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
583//! passed or failed.
584//!
585//! **A live session refuses to start unless the account can afford it.** Before it does any
586//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
587//! GitHub documents as not counting against the REST rate limit and which answers both of
588//! its budgets at once — and starts only if, for each of them, what remains minus this
589//! session's estimated cost is still at least
590//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
591//! allowance. A session that cannot **declines**: it did not run, so it is
592//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
593//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
594//! waiting for the budget to come back. The estimate is derived offline from
595//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
596//! which is also where the published rule that model rests on is cited; the accounting
597//! above records the gate's own read like any other request, and
598//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
599//!
600//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
601//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
602//! text, which is what lets it run on every platform and on a pull request from a fork with
603//! no credential — and that is what actually stops a regression merging. But an offline
604//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
605//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
606//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
607//! number of nodes this query may return"* and whose `cost` is what that document would
608//! spend, both for a document **without executing it**, and the lane asks it for every query
609//! document this source sends, under the largest bindings this source sends, and fails when
610//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
611//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
612//! one; the offline pins still cover it. It records what those calls reported about the
613//! account's own allowance, because whether asking is free is a thing to observe rather than
614//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
615//! and `cost` is metered against an hourly allowance the accounting above reads off a
616//! credentialed run's own response headers.
617//!
618//! **GitHub has two rate limiters and this source is refused by both, so nothing here
619//! treats them as one thing.** The primary budget is the hourly allowance `gh api
620//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
621//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
622//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
623//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
624//! one part of.
625#![deny(missing_docs)]
626
627use std::collections::BTreeMap;
628use std::sync::{Arc, Mutex};
629use std::time::Duration;
630
631use chrono::{DateTime, Utc};
632use onetaskgraph_plugin_api::{
633    Capabilities, Classification, ClosedAs, Comment, CommentBody, CommentOrigin, Completion,
634    ContradictedItem, Coverage, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
635    DependencySupport, Direction, Document, DocumentQuery, DocumentVersionCoverage, Health,
636    HeldTask, IssueHolding, IssueRef, ItemKind, ItemWrite, Label, LabelFilter, Location,
637    MetadataKey, Metering, NativeId, NewComment, Page, PageRequest, Priority, Project,
638    ProjectFilter, ProjectQuery, ProjectVersionCoverage, Repository, SecretResolver, SharedClock,
639    SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusContradiction,
640    StatusMapping, Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate,
641    TaskUpdateOutcome, TextFields, TextQuery, UnmappedStatus, UpdatedField, VersionCoverage,
642    WaitCause, WriteJournal, WriteSupport, system_clock,
643};
644use reqwest::{Client, StatusCode, Url};
645use schemars::{Schema, schema_for};
646use secrecy::{ExposeSecret, SecretString};
647use serde::{Deserialize, Serialize};
648use serde_json::{Value, json};
649
650pub mod accounting;
651mod assets;
652pub mod change_token;
653mod expected;
654pub use expected::{CONTEXT_VERSION, WHOLE_MEMBERS, context_shapes};
655mod pacing;
656mod visibility;
657
658use accounting::Accounting;
659
660/// The registry name for this plugin.
661pub const KIND: &str = "github-projects";
662/// GitHub's maximum connection page size.
663pub const MAX_PAGE_SIZE: u32 = 100;
664/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
665/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
666/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
667pub const SEARCH_PAGE_SIZE: u32 = 20;
668/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
669/// its comments: the largest batch the node-count model prices at one point.
670///
671/// Each aliased item is resolved once, and what GitHub charges for it is the connections
672/// under it — its labels, its page of board memberships, the field values of each of those
673/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
674/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
675/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
676/// one point and fails if one item more would still be priced at one.
677pub const DETAIL_BATCH: usize = 24;
678/// How many issue references one [`graphql::ISSUES_BY_REFERENCE`] request reads.
679///
680/// Each reference asks GitHub for one connection — a page of its board memberships, each with
681/// its `Status` value read by name rather than through a connection of field values — so fifty
682/// come to fifty requests' worth of the aggregate GitHub divides by a hundred, which is its
683/// one-point minimum, and the batch a landing's closures are read in costs one point per fifty
684/// issues per source. `tests/point_cost.rs` prices the document at one point.
685pub const REFERENCE_BATCH: usize = 50;
686/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
687/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
688/// point.
689///
690/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
691/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
692/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
693/// if one item more would still be priced at one.
694pub const COPY_DETAIL_BATCH: usize = 21;
695/// How many mutations one aliased request of a copy's writes carries: [`graphql::CREATE_ISSUES`],
696/// [`graphql::WRITE_FIELD_VALUES`], [`graphql::CLOSE_ISSUES`] and [`graphql::ADD_BLOCKED_BYS`].
697///
698/// Eleven is what GitHub was seen to accept in one request — the timing spike
699/// (`board-copy-speed-2026-10-18-spike-copy-timing-report`) sent eleven aliased mutations of four
700/// kinds, three `createIssue`, three `addProjectV2ItemById`, three field updates, an
701/// `addSubIssue` and an `addBlockedBy`, and every one ran — and no more was established.
702pub const MUTATION_BATCH: usize = 11;
703/// How many origins one [`graphql::ORIGIN_LOOKUPS`] request asks about: the nine records of a
704/// plan and the project's document, and no more, since each carries a page of the board's field
705/// filter and one of the issue search.
706pub const ORIGIN_BATCH: usize = 10;
707
708/// The most nodes any one document this source sends may be asked to return.
709///
710/// GitHub's own published per-query ceiling, taken from
711/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
712/// workspace cannot hold a stale copy of somebody else's number. A query above it is
713/// **refused before it is executed**, whoever is asking and whatever board they are
714/// asking about — so this is a bound on the documents rather than a budget that runs out.
715///
716/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
717/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
718/// everything the credential does — two numbers against two limits, and this constant
719/// bounds only the first. The second is computed offline too, per document:
720/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
721/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
722/// lane. There is no constant like this one to hold a price under, because points are an
723/// hourly allowance rather than a per-call bound.
724///
725/// Neither is a session's price. What `session-cost.md` records of a whole session is its
726/// **requests** and its **worst-case nodes**; what a whole session spends in points is
727/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
728/// [`accounting`]. The module section on the three ways this source reaches an item says how
729/// the count is arrived at, and which of the page sizes below decide it.
730pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
731
732/// Nested connection size for the connections that hang off one item.
733///
734/// It multiplies through every document that reaches an item under a page — the count
735/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
736/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
737/// every document under these constants and fails naming any that reaches the limit, so
738/// raising this is caught there rather than by GitHub.
739const NESTED_PAGE_SIZE: u32 = 50;
740/// How many of one issue's board memberships are read when an issue is reached directly.
741///
742/// An issue reached through a search or through its own node id carries its board half in
743/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
744/// under a page of issues, so every point of it multiplies through the whole document and
745/// is paid for whether or not any issue is on a second board — which is why it is
746/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
747///
748/// **Three, because what a page misses is now recovered rather than refused**, and the
749/// recovery is what the value is chosen against. An issue whose entry for this board sits
750/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
751/// that page's own cursor — so the value trades a bound every read pays for a request only
752/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
753/// boards would pay that request *per issue*, which is order N against the one page per
754/// hundred issues a read costs today. At three it is only reached by an issue on four or
755/// more boards at once, which keeps the recovery path exceptional rather than routine for
756/// a plausible deployment.
757const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
758/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
759/// its two connections for.
760///
761/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
762/// second is a duplicate a copy already takes the first of. Both connections are walked to
763/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
764/// never what the answer is. It is small because every point of it is paid on every lookup,
765/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
766/// worst-case nodes than the one whole-board read they replaced.
767const ORIGIN_PAGE_SIZE: u32 = 3;
768
769pub use github_graphql_node_count::{NodeCountError, Variables};
770
771/// The largest value this source can bind to each page-size variable its documents name.
772///
773/// Every `first:` in [`graphql`] reads one of these four, and each is capped at the
774/// constant above it wherever a caller's own limit could reach it — `$first` at
775/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
776/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`. So this is the worst case a caller can drive this source to,
777/// not one configuration of it, which is what makes a bound computed under it a bound on
778/// every read.
779pub fn largest_page_sizes() -> Variables {
780    Variables::from([
781        ("first".to_owned(), MAX_PAGE_SIZE),
782        ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
783        ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
784        ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
785    ])
786}
787
788/// The most nodes `document` could be asked to return, by GitHub's published rules.
789///
790/// Computed offline from the document's own text under [`largest_page_sizes`] — no
791/// network, no credential and no schema — by
792/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
793/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
794/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
795///
796/// # Errors
797///
798/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
799/// no single operation, or binds a page size this source does not name — each of which is
800/// a defect in the document rather than a number.
801pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
802    node_count(document, &largest_page_sizes())
803}
804
805/// The most rate-limit points one call of `document` could spend, by GitHub's published
806/// rules.
807///
808/// Computed offline from the document's own text under [`largest_page_sizes`] — no
809/// network, no credential and no schema — by
810/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
811/// This is `cost`, metered **per hour** against the allowance one credential shares across
812/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
813/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
814/// under, so what `tests/point_cost.rs` does with it is pin every document in
815/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
816/// figures against GitHub's own reported `cost`.
817///
818/// # Errors
819///
820/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
821/// no single operation, or binds a page size this source does not name — each of which is
822/// a defect in the document rather than a number.
823pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
824    github_graphql_node_count::point_cost(document, &largest_page_sizes())
825}
826
827/// The most nodes `document` could be asked to return under `variables`.
828///
829/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
830/// [`accounting`] is this under the bindings one request really sent — one spelling of the
831/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
832/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
833///
834/// # Errors
835///
836/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
837/// no single operation, or binds a page size `variables` does not name.
838pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
839    github_graphql_node_count::node_count(document, variables)
840}
841
842/// The issue-title prefix that makes a board issue a document.
843///
844/// A GitHub Projects board has no document type — it holds issues — so the discriminator
845/// is the title, and this is the whole of it: an issue whose title begins with these bytes
846/// is a document and every other issue is the task or project the sub-issue rule makes it.
847///
848/// It is spelled **once**, here, and read rather than restated everywhere else — including
849/// by the shared journeys, which take it from this constant so a board fixture cannot
850/// drift from what this source reads. `docs/metadata.md` records the two consequences that
851/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
852/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
853/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
854pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
855
856/// Exact GraphQL query documents issued by this plugin.
857///
858/// Keeping the production documents here lets the pinned-schema test validate the same
859/// bytes that are sent to GitHub, rather than a test-only copy which could drift
860/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
861/// field, and its guarded caller always supplies the complete existing option set with ids.
862pub mod graphql {
863    /// The board half of one item: the field values every document here reads it from.
864    ///
865    /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
866    /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
867    /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
868    /// *the same value*, because
869    /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
870    /// one path. Three spellings of it is what would drift, so there is one.
871    ///
872    /// The `Status` option and this source's own origin text field are the whole of it. It
873    /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
874    /// content, so it holds nothing the content's own `labels` do not already say, and it
875    /// would sit a label connection two page sizes deep.
876    macro_rules! board_item_values {
877        () => {
878            r#"fieldValues(first:$nestedFirst){nodes{
879          ... on ProjectV2ItemFieldSingleSelectValue{name field{
880            ... on ProjectV2SingleSelectField{id name options{id name}}
881          }}
882          ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
883        }pageInfo{hasNextPage}}"#
884        };
885    }
886
887    /// The variable every document reading an issue declares for the relationship events its
888    /// change token counts, with its value as the default, so no caller binds it.
889    ///
890    /// A variable rather than a literal list because every argument a production document
891    /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
892    /// its default to the pinned enum. Its default is the six event types GitHub records a
893    /// relationship added or removed with: a dependency on either end, as `blockedBy` and
894    /// `blocking`, so one a person makes from the far end moves this end's count too; and a
895    /// project membership on the task, as its parent, which GitHub records there whichever
896    /// issue the move was made from. A sub-issue's events on its parent are not counted: a
897    /// project carries no task token, and a copy writes nothing of a task its own sub-issues
898    /// decide. [`super::change_token`] says why the count is in the token.
899    macro_rules! relationship_events_variable {
900        () => {
901            ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
902        };
903    }
904
905    /// Everything this source reads about one issue, wherever it reaches that issue.
906    ///
907    /// A macro rather than a constant so the three documents below can `concat!` it: one
908    /// spelling of these fields is what makes an issue read through the board-scoped
909    /// search, through its own node id, and through its project's sub-issue relationship
910    /// resolve to *the same* item, which is the whole of what
911    /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
912    ///
913    /// `projectItems` is what carries the board half of an issue: the board item's own id
914    /// and the [`board_item_values!`] above — the `Status` option and this source's origin
915    /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
916    /// issue rather than on the board, which is what makes the cost of a read proportional
917    /// to what was asked for instead of to the board's size.
918    ///
919    /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
920    /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
921    /// not on that page: a page here is where the search for the entry starts rather than
922    /// where it ends.
923    ///
924    /// It does **not** select the board's `Labels` field value, and that is the whole of
925    /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
926    /// a label connection there sits under `fieldValues` under `projectItems` under a page
927    /// of issues, spending `$nestedFirst` twice down one path, and took
928    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
929    /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
930    /// above, and that connection is where every label this source reports comes from. No
931    /// document in this module selects the board field any longer, [`BOARD`] included; the
932    /// module documentation records why nothing it could have held is lost.
933    macro_rules! board_issue {
934        () => {
935            concat!(
936                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}
937      labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
938      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
939        "#,
940                board_item_values!(),
941                r#"}pageInfo{hasNextPage endCursor}}}"#
942            )
943        };
944    }
945
946    /// Every issue of one board, found by a search scoped to that board.
947    ///
948    /// This is how the projects a board holds are listed, and it selects no `items`
949    /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
950    /// container walked page by page, so nothing nested inside a board item is paid for.
951    /// Which of the issues it returns is a project is then read off `parent` — GitHub
952    /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
953    /// discriminator has to be applied to the field, which is a scalar on the issue and
954    /// costs nothing.
955    pub const SEARCH_ISSUES: &str = concat!(
956        r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
957        relationship_events_variable!(),
958        r#"){
959      search(query:$search,type:$type,first:$first,after:$after){
960        pageInfo{hasNextPage endCursor}
961        nodes{__typename ...BoardIssue}
962      }
963    }"#,
964        board_issue!()
965    );
966
967    /// What a dependency read selects of each far end: enough to say which kind of item it
968    /// is, its body included for the kind marker.
969    macro_rules! related_issue {
970        () => {
971            " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
972        };
973    }
974
975    /// One issue by its own node id, which is what a qualified id names here — with what a
976    /// write of it needs and the issue does not carry in `board_issue!`: the field
977    /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
978    ///
979    /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
980    /// answers a write made moments ago with the value from before it, and resolving a node
981    /// id does not.
982    ///
983    /// **Why those two ride here and not on the fragment.** A copy or an update of an item
984    /// reads it by its own id, and with them that one read answers everything the write
985    /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
986    /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
987    /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
988    /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
989    /// they sit under one item, and this read is still one point.
990    pub const ISSUE: &str = concat!(
991        r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
992        relationship_events_variable!(),
993        r#"){
994      node(id:$id){__typename ...BoardIssue ... on Issue{
995        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
996          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
997          ... on ProjectV2Field{__typename id name dataType}
998        }pageInfo{hasNextPage}}}}}
999        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
1000      }}
1001    }"#,
1002        board_issue!(),
1003        related_issue!()
1004    );
1005
1006    /// One project's tasks: the sub-issues of the issue that project is, each with a page of
1007    /// what blocks it.
1008    ///
1009    /// The work this costs is the project's own size. Nothing about it grows as the board
1010    /// gains projects, or as those projects gain tasks.
1011    ///
1012    /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
1013    /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
1014    /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
1015    /// costs grow with its tasks rather than with the pages of them. Carried here, a task
1016    /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
1017    /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
1018    /// It adds a connection under each issue of the page — one rate-limit point per page,
1019    /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
1020    /// answer questions that never read an edge.
1021    ///
1022    /// **The project issue itself rides here too**, with the page of what blocks it, because
1023    /// the reader of a project's tasks reads the project next: a copy writing the project reads
1024    /// it and its edges before it writes them. Carried on the request that already names the
1025    /// project, the first page answers that read and the dependency read after it, at the
1026    /// state GitHub answered in this command, rather than spending one more read on an item
1027    /// the cache's token cannot vouch for whole. Each sub-issue is kept the same way, so a
1028    /// copy's read of a task this listing carried is answered by it rather than sent again.
1029    /// Only a copy's read-before-write takes them; every other read by id reads GitHub.
1030    pub const SUB_ISSUES: &str = concat!(
1031        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1032        relationship_events_variable!(),
1033        r#"){
1034      node(id:$id){__typename ...BoardIssue
1035        ... on Issue{blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1036          subIssues(first:$first,after:$after){
1037          pageInfo{hasNextPage endCursor}
1038          nodes{__typename ...BoardIssue ... on Issue{
1039            blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1040          }}
1041        }}}
1042    }"#,
1043        board_issue!(),
1044        related_issue!()
1045    );
1046
1047    /// What a read of the board's own `items` selects of each item's content.
1048    ///
1049    /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1050    /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1051    /// content by one spelling.
1052    macro_rules! board_item_content {
1053        () => {
1054            r#" content{
1055        ... 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}}}
1056        ... on PullRequest{__typename id}
1057        ... on DraftIssue{__typename id title body createdAt updatedAt}
1058      }"#
1059        };
1060    }
1061
1062    /// Reads the board's fields and one page of its items.
1063    pub const BOARD: &str = concat!(
1064        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1065        relationship_events_variable!(),
1066        r#"){
1067      owner:repositoryOwner(login:$owner){
1068        ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1069      }
1070    } fragment Board on ProjectV2 { id title
1071      fields(first:$nestedFirst){nodes{
1072        ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1073        ... on ProjectV2Field{__typename id name dataType}
1074      }pageInfo{hasNextPage}}
1075      items(first:$first,after:$after){nodes{id updatedAt "#,
1076        board_item_values!(),
1077        board_item_content!(),
1078        r#"} pageInfo{hasNextPage endCursor}}
1079    }"#
1080    );
1081
1082    /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1083    /// the board.
1084    ///
1085    /// **`originItems`** is the board's own items narrowed by its own field filter —
1086    /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1087    /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1088    /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1089    /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1090    /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1091    /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1092    /// fresh `addProjectV2ItemById` the way that connection does.
1093    ///
1094    /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1095    /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1096    /// indexes that comment, and the index catches up with a write in a second or two rather
1097    /// than in minutes, so it finds a carrier another process wrote that the first read is
1098    /// still behind on.
1099    ///
1100    /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1101    /// — and resumes from its own cursor; a connection already walked to its end is resumed
1102    /// from its last cursor, which answers an empty page. Every candidate either read returns
1103    /// is confirmed against its own origin field before it is reported, so a token match of
1104    /// the search or anything else the filter admits never is.
1105    ///
1106    /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1107    /// own whole reads counts this one among them.
1108    pub const ORIGIN_LOOKUP: &str = concat!(
1109        r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1110        relationship_events_variable!(),
1111        r#"){
1112      originItems:repositoryOwner(login:$owner){
1113        ... on ProjectV2Owner{projectV2(number:$number){
1114          items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1115        board_item_values!(),
1116        board_item_content!(),
1117        r#"} pageInfo{hasNextPage endCursor}}
1118        }}
1119      }
1120      search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1121        pageInfo{hasNextPage endCursor}
1122        nodes{__typename ...BoardIssue}
1123      }
1124    }"#,
1125        board_issue!()
1126    );
1127
1128    /// Every closed item of the board, by the board's own field filter — what
1129    /// `task status reconcile` reads, and nothing else of the board.
1130    ///
1131    /// **`ProjectV2.items(query:)` narrowed by `$filter`**, which this source binds to
1132    /// `is:closed` alone, so a board whose open items are most of it is not read whole to find
1133    /// the closed ones. It is narrowed rather than trusted: every item it answers is confirmed
1134    /// closed, with a reason and a card that contradict each other, from the item node it
1135    /// already carries, so a filter that also let open items through would cost reads and
1136    /// write nothing wrong.
1137    ///
1138    /// The board's own id and field definitions ride along, as [`BOARD`] carries them, so the
1139    /// option writes that follow need no read of the board. The root is aliased `closedItems`
1140    /// rather than `owner`, so nothing counting the board's own whole reads counts this one
1141    /// among them.
1142    pub const CLOSED_ITEMS: &str = concat!(
1143        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$filter:String!,$nestedFirst:Int!,$duplicates:Boolean!"#,
1144        relationship_events_variable!(),
1145        r#"){
1146      closedItems:repositoryOwner(login:$owner){
1147        ... on ProjectV2Owner{projectV2(number:$number){id
1148          fields(first:$nestedFirst){nodes{
1149            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1150            ... on ProjectV2Field{__typename id name dataType}
1151          }pageInfo{hasNextPage}}
1152          items(first:$first,after:$after,query:$filter){nodes{id updatedAt "#,
1153        board_item_values!(),
1154        board_item_content!(),
1155        r#"} pageInfo{hasNextPage endCursor}}
1156        }}
1157      }
1158    }"#
1159    );
1160
1161    /// One alias of [`ISSUES_BY_REFERENCE`]: the issue `$owner<n>/$name<n>#$number<n>` names.
1162    macro_rules! reference_alias {
1163        ($n:literal) => {
1164            concat!(
1165                "\n      r",
1166                stringify!($n),
1167                ":repository(owner:$owner",
1168                stringify!($n),
1169                ",name:$name",
1170                stringify!($n),
1171                "){issue(number:$number",
1172                stringify!($n),
1173                "){...HeldIssue}}"
1174            )
1175        };
1176    }
1177
1178    /// [`REFERENCE_BATCH`](super::REFERENCE_BATCH) issues at once, each by its repository and
1179    /// number — the one read `task status complete` makes of a source, and nothing else of
1180    /// the board: no `items`, no search and no project read.
1181    ///
1182    /// **Its own fragment, lighter than `board_issue!`, and that is what makes fifty
1183    /// references one point.** It selects no labels, and of each board membership only the
1184    /// `Status` value, by name, with `fieldValueByName` — so each alias asks GitHub for one
1185    /// connection, its page of memberships, where the whole fragment asks for five. What a
1186    /// completion writes is a card and, at most, the issue's state, so the labels and the other
1187    /// field values it leaves out are never what it needs; an item read this way is held for
1188    /// that write alone and never answers another read. An issue whose entry for this board is
1189    /// past its first page of memberships is walked with [`ISSUE_BOARD_ITEMS`], exactly as
1190    /// every other read of an issue recovers one.
1191    ///
1192    /// Fixed-size, as [`ISSUE_DETAILS`] is, so there is one document to price: a batch of
1193    /// fewer references binds the slots it has none for to its last one.
1194    pub const ISSUES_BY_REFERENCE: &str = concat!(
1195        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!"#,
1196        relationship_events_variable!(),
1197        r#"){"#,
1198        reference_alias!(0),
1199        reference_alias!(1),
1200        reference_alias!(2),
1201        reference_alias!(3),
1202        reference_alias!(4),
1203        reference_alias!(5),
1204        reference_alias!(6),
1205        reference_alias!(7),
1206        reference_alias!(8),
1207        reference_alias!(9),
1208        reference_alias!(10),
1209        reference_alias!(11),
1210        reference_alias!(12),
1211        reference_alias!(13),
1212        reference_alias!(14),
1213        reference_alias!(15),
1214        reference_alias!(16),
1215        reference_alias!(17),
1216        reference_alias!(18),
1217        reference_alias!(19),
1218        reference_alias!(20),
1219        reference_alias!(21),
1220        reference_alias!(22),
1221        reference_alias!(23),
1222        reference_alias!(24),
1223        reference_alias!(25),
1224        reference_alias!(26),
1225        reference_alias!(27),
1226        reference_alias!(28),
1227        reference_alias!(29),
1228        reference_alias!(30),
1229        reference_alias!(31),
1230        reference_alias!(32),
1231        reference_alias!(33),
1232        reference_alias!(34),
1233        reference_alias!(35),
1234        reference_alias!(36),
1235        reference_alias!(37),
1236        reference_alias!(38),
1237        reference_alias!(39),
1238        reference_alias!(40),
1239        reference_alias!(41),
1240        reference_alias!(42),
1241        reference_alias!(43),
1242        reference_alias!(44),
1243        reference_alias!(45),
1244        reference_alias!(46),
1245        reference_alias!(47),
1246        reference_alias!(48),
1247        reference_alias!(49),
1248        "\n    }",
1249        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}
1250      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
1251        status:fieldValueByName(name:$statusField){... on ProjectV2ItemFieldSingleSelectValue{name field{
1252          ... on ProjectV2SingleSelectField{id name options{id name}}
1253        }}}
1254      }pageInfo{hasNextPage endCursor}}}"#
1255    );
1256
1257    /// The board's own id and field definitions, and not one of its items.
1258    ///
1259    /// What a write needs of the board when the item it writes does not say: the id a field
1260    /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1261    /// origin fields. It selects no `items`, so what it costs is the board's field list
1262    /// however many items the board holds — and it decides nothing about which items those
1263    /// are, which is the question a read of one item by its own id answers instead.
1264    ///
1265    /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1266    /// board's item reads by their root counts this one among them.
1267    pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1268      boardFields:repositoryOwner(login:$owner){
1269        ... on ProjectV2Owner{projectV2(number:$number){id
1270          fields(first:$nestedFirst){nodes{
1271            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1272            ... on ProjectV2Field{__typename id name dataType}
1273          }pageInfo{hasNextPage}}
1274        }}
1275      }
1276    }"#;
1277
1278    /// One board draft by its own node id, with the board item it sits in.
1279    ///
1280    /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1281    /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1282    /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1283    /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1284    /// board listing hands it to, and nothing has to list the board to find one.
1285    pub const DRAFT: &str = concat!(
1286        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1287      node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1288        projectV2Items(first:$boardItems){nodes{id project{id number}
1289        "#,
1290        board_item_values!(),
1291        r#"}pageInfo{hasNextPage endCursor}}}}
1292    }"#
1293    );
1294
1295    /// One issue's board memberships alone, walked past the page a read of it carried.
1296    ///
1297    /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1298    /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1299    /// boards than that page holds may have this board's entry past its end. This asks that
1300    /// one issue for its memberships and nothing else — the caller already holds the issue —
1301    /// so an answer of "this board does not hold it" is only ever given about a connection
1302    /// read to exhaustion.
1303    ///
1304    /// It selects the board item's id, its project number and the same
1305    /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1306    /// very same resolver: an issue recovered this way reports the same title, the same
1307    /// status, the same labels and the same qualified id as one whose entry was on the
1308    /// page.
1309    ///
1310    /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1311    /// multiplies through it and the membership connection can be walked at
1312    /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1313    /// further request for any issue a person really keeps.
1314    pub const ISSUE_BOARD_ITEMS: &str = concat!(
1315        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1316      node(id:$id){
1317        ... on Issue{projectItems(first:$first,after:$after){
1318          nodes{id updatedAt project{id number}
1319        "#,
1320        board_item_values!(),
1321        r#"}
1322          pageInfo{hasNextPage endCursor}}}
1323      }
1324    }"#
1325    );
1326    /// Whether the board's Project is public — half of what decides whether a write here can
1327    /// be read by anybody. Needs the `read:project` scope.
1328    pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1329    /// Resolves the configured repository's node id, which creating an issue requires.
1330    pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1331    /// What creating an issue needs and has not read yet: the board's own id and field
1332    /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1333    /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1334    ///
1335    /// Sent at the point a create knows which repository it is for, when neither half is
1336    /// already known to this process; a create needing only one of them sends that one's own
1337    /// document. Neither half is kept past the process: a field's option ids are re-minted by
1338    /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1339    /// status.
1340    pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1341      boardFields:repositoryOwner(login:$owner){
1342        ... on ProjectV2Owner{projectV2(number:$number){id
1343          fields(first:$nestedFirst){nodes{
1344            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1345            ... on ProjectV2Field{__typename id name dataType}
1346          }pageInfo{hasNextPage}}
1347        }}
1348      }
1349      repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1350    }"#;
1351    /// Reads both dependency directions for one issue, with each far end's own kind — and
1352    /// the issue's own body, which is where an edge to another source is recorded, so that
1353    /// half of a dependency read needs no second read of the issue or of the board.
1354    pub const ISSUE_DEPENDENCIES: &str = concat!(
1355        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1356      ... on Issue{body
1357        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1358        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1359      }}}"#,
1360        related_issue!()
1361    );
1362    /// Creates one issue. Outside a copy it is created on no board, and [`ADD_TO_BOARD`] files
1363    /// it; inside one, its input names the board in `projectV2Ids` and its project in
1364    /// `parentIssueId`, so this one request files it under both — see
1365    /// [`GitHubProjectsSource::create_filed_issue`](super::GitHubProjectsSource).
1366    pub const CREATE_ISSUE: &str =
1367        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1368    /// Puts an existing issue on the configured board.
1369    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1370    /// Updates an issue's visible fields and its open or closed state in one call.
1371    pub const UPDATE_ISSUE: &str =
1372        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1373    /// Updates an existing draft's user-visible fields.
1374    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1375    /// Updates a text or single-select value on one project item.
1376    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}}}}}}}}"#;
1377    /// Writes up to six board fields and clears up to three in one ordered mutation: every
1378    /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1379    /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1380    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}}}"#;
1381    /// Clears one project item's value of one field, which is what a `none` priority is.
1382    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}}}}}}}}"#;
1383    /// Creates one field: a single-select one with its options, or a text one for a projected
1384    /// metadata value. Only the guarded field setup may use this document, and only for a
1385    /// field the board lacks.
1386    pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1387    /// Replaces a single-select field's options. Only the guarded field setup — the
1388    /// `status-options` and `fields` operations — may use this document, because GitHub
1389    /// treats the input as the complete option list.
1390    pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1391    /// A fresh snapshot of the Status field and every board item's assignment.
1392    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}}}}}}"#;
1393    /// Files one issue under another as a sub-issue, which is what project membership is.
1394    pub const ADD_SUB_ISSUE: &str =
1395        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1396    /// Takes one issue back out of its parent.
1397    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1398    /// Adds GitHub's native issue blocked-by relationship.
1399    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1400    /// One origin of [`ORIGIN_LOOKUPS`]: [`ORIGIN_LOOKUP`]'s two connections for the origin the
1401    /// `$filter<n>` and `$search<n>` of slot `<n>` name, sent only while `$ask<n>` holds.
1402    macro_rules! origin_lookup_alias {
1403        ($n:literal) => {
1404            concat!(
1405                "\n      o",
1406                stringify!($n),
1407                ":repositoryOwner(login:$owner) @include(if:$ask",
1408                stringify!($n),
1409                "){... on ProjectV2Owner{projectV2(number:$number){items(first:$originFirst,query:$filter",
1410                stringify!($n),
1411                "){nodes{id updatedAt ",
1412                board_item_values!(),
1413                board_item_content!(),
1414                "} pageInfo{hasNextPage endCursor}}}}}\n      s",
1415                stringify!($n),
1416                ":search(query:$search",
1417                stringify!($n),
1418                ",type:$type,first:$originFirst) @include(if:$ask",
1419                stringify!($n),
1420                "){pageInfo{hasNextPage endCursor} nodes{__typename ...BoardIssue}}"
1421            )
1422        };
1423    }
1424
1425    /// The first page of [`ORIGIN_LOOKUP`] for up to [`ORIGIN_BATCH`](super::ORIGIN_BATCH)
1426    /// origins a copy is about to look for, and everything creating an item then reads —
1427    /// the board's own id and field definitions, as [`CREATION_CONTEXT`] reads them, and the node
1428    /// id and the numeric id of the repository an issue is created in — in one request.
1429    ///
1430    /// What a copy reads before it writes anything, so that finding none of its records on the
1431    /// board and creating every one costs this read and the writes. An origin whose page says
1432    /// there is more is left to its own [`ORIGIN_LOOKUP`]. The repository's numeric id is what an
1433    /// image upload names, so a copy carrying assets needs no `GET /repos/{owner}/{repo}` either.
1434    pub const ORIGIN_LOOKUPS: &str = concat!(
1435        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!"#,
1436        relationship_events_variable!(),
1437        r#"){
1438      boardFields:repositoryOwner(login:$owner){
1439        ... on ProjectV2Owner{projectV2(number:$number){id
1440          fields(first:$nestedFirst){nodes{
1441            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1442            ... on ProjectV2Field{__typename id name dataType}
1443          }pageInfo{hasNextPage}}
1444        }}
1445      }
1446      repository(owner:$repositoryOwner,name:$repositoryName){id databaseId nameWithOwner}"#,
1447        origin_lookup_alias!(0),
1448        origin_lookup_alias!(1),
1449        origin_lookup_alias!(2),
1450        origin_lookup_alias!(3),
1451        origin_lookup_alias!(4),
1452        origin_lookup_alias!(5),
1453        origin_lookup_alias!(6),
1454        origin_lookup_alias!(7),
1455        origin_lookup_alias!(8),
1456        origin_lookup_alias!(9),
1457        "\n    }",
1458        board_issue!()
1459    );
1460
1461    /// One slot `<n>` of a copy's aliased writes: `$<prefix><n>` is its input and `$send<n>`
1462    /// whether it is sent.
1463    macro_rules! write_slot {
1464        ($prefix:literal, $field:literal, $selection:literal, $n:literal) => {
1465            concat!(
1466                " ",
1467                $prefix,
1468                stringify!($n),
1469                ":",
1470                $field,
1471                "(input:$",
1472                $prefix,
1473                stringify!($n),
1474                ") @include(if:$send",
1475                stringify!($n),
1476                "){",
1477                $selection,
1478                "}"
1479            )
1480        };
1481    }
1482
1483    /// The variables of a copy's aliased writes: eleven inputs of `$type` named `$<prefix><n>`,
1484    /// and eleven `$send<n>`.
1485    macro_rules! write_variables {
1486        ($prefix:literal, $type:literal) => {
1487            concat!(
1488                "mutation(",
1489                "$",
1490                $prefix,
1491                "0:",
1492                $type,
1493                "!,$",
1494                $prefix,
1495                "1:",
1496                $type,
1497                "!,$",
1498                $prefix,
1499                "2:",
1500                $type,
1501                "!,$",
1502                $prefix,
1503                "3:",
1504                $type,
1505                "!,$",
1506                $prefix,
1507                "4:",
1508                $type,
1509                "!,$",
1510                $prefix,
1511                "5:",
1512                $type,
1513                "!,$",
1514                $prefix,
1515                "6:",
1516                $type,
1517                "!,$",
1518                $prefix,
1519                "7:",
1520                $type,
1521                "!,$",
1522                $prefix,
1523                "8:",
1524                $type,
1525                "!,$",
1526                $prefix,
1527                "9:",
1528                $type,
1529                "!,$",
1530                $prefix,
1531                "10:",
1532                $type,
1533                "!,",
1534                "$send0:Boolean!,$send1:Boolean!,$send2:Boolean!,$send3:Boolean!,$send4:Boolean!,",
1535                "$send5:Boolean!,$send6:Boolean!,$send7:Boolean!,$send8:Boolean!,$send9:Boolean!,",
1536                "$send10:Boolean!){"
1537            )
1538        };
1539    }
1540
1541    /// A copy's aliased writes of one kind: [`MUTATION_BATCH`](super::MUTATION_BATCH) slots of
1542    /// `$field`, each sent only while its `$send<n>` holds.
1543    macro_rules! write_batch {
1544        ($prefix:literal, $type:literal, $field:literal, $selection:literal) => {
1545            concat!(
1546                write_variables!($prefix, $type),
1547                write_slot!($prefix, $field, $selection, 0),
1548                write_slot!($prefix, $field, $selection, 1),
1549                write_slot!($prefix, $field, $selection, 2),
1550                write_slot!($prefix, $field, $selection, 3),
1551                write_slot!($prefix, $field, $selection, 4),
1552                write_slot!($prefix, $field, $selection, 5),
1553                write_slot!($prefix, $field, $selection, 6),
1554                write_slot!($prefix, $field, $selection, 7),
1555                write_slot!($prefix, $field, $selection, 8),
1556                write_slot!($prefix, $field, $selection, 9),
1557                write_slot!($prefix, $field, $selection, 10),
1558                "}"
1559            )
1560        };
1561    }
1562
1563    /// Creates up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues in one request, each filed on
1564    /// the board and under its project by the same `createIssue` — its `projectV2Ids` and
1565    /// `parentIssueId` — as [`CREATE_ISSUE`] creates one.
1566    pub const CREATE_ISSUES: &str = write_batch!(
1567        "create",
1568        "CreateIssueInput",
1569        "createIssue",
1570        "issue{id number url}"
1571    );
1572
1573    /// One issue of [`CREATED_BOARD_ITEMS`]: the board items the issue `$id<n>` names is on, with
1574    /// the `Status` value of each, read only while `$read<n>` holds.
1575    macro_rules! created_item_alias {
1576        ($n:literal) => {
1577            concat!(
1578                "\n      i",
1579                stringify!($n),
1580                ":node(id:$id",
1581                stringify!($n),
1582                ") @include(if:$read",
1583                stringify!($n),
1584                "){... on Issue{id projectItems(first:$boardItems){nodes{id project{id} status:fieldValueByName(name:$statusName){... on ProjectV2ItemFieldSingleSelectValue{name}}}}}}"
1585            )
1586        };
1587    }
1588
1589    /// The board item of each issue a copy created, up to
1590    /// [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH) at a time, with its `Status` as the board
1591    /// set it: what `createIssue`'s `projectV2Ids` files the issue as, which GitHub adds after
1592    /// answering the create, so its id comes from a read made once every create has been sent.
1593    pub const CREATED_BOARD_ITEMS: &str = concat!(
1594        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!){"#,
1595        created_item_alias!(0),
1596        created_item_alias!(1),
1597        created_item_alias!(2),
1598        created_item_alias!(3),
1599        created_item_alias!(4),
1600        created_item_alias!(5),
1601        created_item_alias!(6),
1602        created_item_alias!(7),
1603        created_item_alias!(8),
1604        created_item_alias!(9),
1605        created_item_alias!(10),
1606        created_item_alias!(11),
1607        created_item_alias!(12),
1608        created_item_alias!(13),
1609        created_item_alias!(14),
1610        created_item_alias!(15),
1611        created_item_alias!(16),
1612        created_item_alias!(17),
1613        created_item_alias!(18),
1614        created_item_alias!(19),
1615        created_item_alias!(20),
1616        "\n    }"
1617    );
1618
1619    /// Up to [`MUTATION_BATCH`](super::MUTATION_BATCH) board field values a copy writes on the
1620    /// items it created, each on its own item, in one ordered request.
1621    pub const WRITE_FIELD_VALUES: &str = write_batch!(
1622        "field",
1623        "UpdateProjectV2ItemFieldValueInput",
1624        "updateProjectV2ItemFieldValue",
1625        "projectV2Item{id}"
1626    );
1627
1628    /// Closes up to [`MUTATION_BATCH`](super::MUTATION_BATCH) issues a copy created at a closing
1629    /// status, each with its own `stateInput` and nothing else, once its option is selected.
1630    pub const CLOSE_ISSUES: &str =
1631        write_batch!("close", "UpdateIssueInput", "updateIssue", "issue{id}");
1632
1633    /// Up to [`MUTATION_BATCH`](super::MUTATION_BATCH) native blocked-by relationships a copy adds
1634    /// to the issues it created, in one request.
1635    pub const ADD_BLOCKED_BYS: &str = write_batch!(
1636        "block",
1637        "AddBlockedByInput",
1638        "addBlockedBy",
1639        "issue{id} blockingIssue{id}"
1640    );
1641
1642    /// Removes one native issue blocked-by relationship.
1643    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1644    /// Deletes one issue, which takes its board item with it.
1645    ///
1646    /// The engine sends this in one situation only: undoing a copy that could not finish,
1647    /// over the items that same copy created. Deleting the issue removes the board item
1648    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1649    pub const DELETE_ISSUE: &str =
1650        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1651
1652    /// Everything this source reads about one issue comment, wherever it reaches one.
1653    ///
1654    /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1655    /// and a comment just edited are handed to one mapper, so they are selected by one
1656    /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1657    /// longer exists, and `login` is the one member every kind of actor carries.
1658    macro_rules! issue_comment {
1659        () => {
1660            "id author{login} createdAt updatedAt body url"
1661        };
1662    }
1663
1664    /// One task's comments: a page of its issue's own `comments` connection.
1665    ///
1666    /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1667    /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1668    /// list every time somebody edited it; left unordered the connection answers in the order
1669    /// the comments were written, which is the order GitHub documents for the same collection
1670    /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1671    /// node count and the caller's own page size is pushed straight down.
1672    pub const ISSUE_COMMENTS: &str = concat!(
1673        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1674        issue_comment!(),
1675        r#"}pageInfo{hasNextPage endCursor}}}}}"#
1676    );
1677    /// One issue by its own node id, with a page of its comments: what `task show` and a
1678    /// comment listing read, in one request.
1679    ///
1680    /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1681    /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1682    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1683    /// connection there would multiply through both of those documents' price, and neither
1684    /// needs one.
1685    pub const ISSUE_DETAIL: &str = concat!(
1686        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1687        relationship_events_variable!(),
1688        r#"){
1689      node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1690        issue_comment!(),
1691        r#"}pageInfo{hasNextPage endCursor}}}}
1692    }"#,
1693        board_issue!()
1694    );
1695
1696    /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1697    /// page of its comments when `$comments` asks for them.
1698    macro_rules! issue_details_alias {
1699        ($n:literal) => {
1700            concat!(
1701                "\n      i",
1702                stringify!($n),
1703                ":node(id:$id",
1704                stringify!($n),
1705                "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1706                issue_comment!(),
1707                "}pageInfo{hasNextPage endCursor}}}}"
1708            )
1709        };
1710    }
1711
1712    /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1713    /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1714    ///
1715    /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1716    /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1717    /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1718    /// neither — so every connection under it would be priced at nothing and the pin in
1719    /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1720    /// one-item read the model already prices, so the batch costs what its aliases cost.
1721    ///
1722    /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1723    /// slots it has no item for to the last item it does, and reads that item again; the
1724    /// price is the document's, whatever its variables, so a short batch costs what a full
1725    /// one does and nothing more.
1726    pub const ISSUE_DETAILS: &str = concat!(
1727        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!"#,
1728        relationship_events_variable!(),
1729        r#"){"#,
1730        issue_details_alias!(0),
1731        issue_details_alias!(1),
1732        issue_details_alias!(2),
1733        issue_details_alias!(3),
1734        issue_details_alias!(4),
1735        issue_details_alias!(5),
1736        issue_details_alias!(6),
1737        issue_details_alias!(7),
1738        issue_details_alias!(8),
1739        issue_details_alias!(9),
1740        issue_details_alias!(10),
1741        issue_details_alias!(11),
1742        issue_details_alias!(12),
1743        issue_details_alias!(13),
1744        issue_details_alias!(14),
1745        issue_details_alias!(15),
1746        issue_details_alias!(16),
1747        issue_details_alias!(17),
1748        issue_details_alias!(18),
1749        issue_details_alias!(19),
1750        issue_details_alias!(20),
1751        issue_details_alias!(21),
1752        issue_details_alias!(22),
1753        issue_details_alias!(23),
1754        "\n    }",
1755        board_issue!()
1756    );
1757
1758    /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1759    /// page of its comments and the issues it is blocked by.
1760    macro_rules! issue_copy_details_alias {
1761        ($n:literal) => {
1762            concat!(
1763                "\n      i",
1764                stringify!($n),
1765                ":node(id:$id",
1766                stringify!($n),
1767                "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1768                issue_comment!(),
1769                "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1770            )
1771        };
1772    }
1773
1774    /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1775    /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1776    /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1777    /// at a time, so a copy of a changed task costs this read and nothing else.
1778    ///
1779    /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1780    /// what `task show-many` is priced by and which reads no edge.
1781    ///
1782    /// `children`, read only while `$children` holds, is which issues the issue `$project` has
1783    /// as sub-issues — each one's id and title, the page `$nestedFirst` allows — so that a
1784    /// project copy, which reads its project and every task at once, knows from this same read
1785    /// which tasks the project holds at the destination, and its orphan check is answered by it
1786    /// rather than by a listing of the project.
1787    pub const ISSUE_COPY_DETAILS: &str = concat!(
1788        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!"#,
1789        relationship_events_variable!(),
1790        r#"){"#,
1791        issue_copy_details_alias!(0),
1792        issue_copy_details_alias!(1),
1793        issue_copy_details_alias!(2),
1794        issue_copy_details_alias!(3),
1795        issue_copy_details_alias!(4),
1796        issue_copy_details_alias!(5),
1797        issue_copy_details_alias!(6),
1798        issue_copy_details_alias!(7),
1799        issue_copy_details_alias!(8),
1800        issue_copy_details_alias!(9),
1801        issue_copy_details_alias!(10),
1802        issue_copy_details_alias!(11),
1803        issue_copy_details_alias!(12),
1804        issue_copy_details_alias!(13),
1805        issue_copy_details_alias!(14),
1806        issue_copy_details_alias!(15),
1807        issue_copy_details_alias!(16),
1808        issue_copy_details_alias!(17),
1809        issue_copy_details_alias!(18),
1810        issue_copy_details_alias!(19),
1811        issue_copy_details_alias!(20),
1812        "\n      children:node(id:$project) @include(if:$children){... on Issue{id subIssues(first:$nestedFirst){nodes{id title} pageInfo{hasNextPage}}}}",
1813        "\n    }",
1814        board_issue!(),
1815        related_issue!()
1816    );
1817
1818    /// Which issue one comment is on, read before that comment is edited or removed.
1819    ///
1820    /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1821    /// comment id given against the wrong task would change a comment on another issue.
1822    pub const COMMENT_ISSUE: &str =
1823        r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1824    /// Adds one comment to an issue, signed as the account the token belongs to.
1825    pub const ADD_COMMENT: &str = concat!(
1826        r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1827        issue_comment!(),
1828        r#"}}}}"#
1829    );
1830    /// Replaces the body of one issue comment.
1831    pub const UPDATE_COMMENT: &str = concat!(
1832        r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
1833        issue_comment!(),
1834        r#"}}}"#
1835    );
1836    /// Removes one issue comment. Its payload carries nothing about the comment it removed.
1837    pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
1838
1839    /// What a mutation of a write the store's cache expects selects of the issue it wrote:
1840    /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
1841    /// change token's every part and the first page of its comments.
1842    ///
1843    /// Sent **only** while that write runs, in place of the document named beside each
1844    /// below, so every other write's documents, prices and request counts are what they
1845    /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
1846    /// mutation's response is stamped seconds after the change it made, so against GitHub
1847    /// itself a response rarely determines a version; the cache is then left unserveable
1848    /// for the task, and its next read refreshes it.
1849    macro_rules! whole_issue {
1850        () => {
1851            concat!(
1852                r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
1853                issue_comment!(),
1854                r#"}pageInfo{hasNextPage endCursor}}}"#
1855            )
1856        };
1857    }
1858
1859    /// The variables every document of `whole_issue!` declares beside its own.
1860    macro_rules! whole_variables {
1861        () => {
1862            concat!(
1863                r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1864                relationship_events_variable!()
1865            )
1866        };
1867    }
1868
1869    /// [`ADD_COMMENT`], selecting the whole issue commented on.
1870    pub const ADD_COMMENT_WHOLE: &str = concat!(
1871        r#"mutation($input:AddCommentInput!"#,
1872        whole_variables!(),
1873        r#"){addComment(input:$input){subject{id "#,
1874        whole_issue!(),
1875        r#"} commentEdge{node{"#,
1876        issue_comment!(),
1877        r#"}}}}"#,
1878        board_issue!()
1879    );
1880    /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
1881    pub const UPDATE_COMMENT_WHOLE: &str = concat!(
1882        r#"mutation($input:UpdateIssueCommentInput!"#,
1883        whole_variables!(),
1884        r#"){updateIssueComment(input:$input){issueComment{"#,
1885        issue_comment!(),
1886        r#" issue{"#,
1887        whole_issue!(),
1888        r#"}}}}"#,
1889        board_issue!()
1890    );
1891    /// [`UPDATE_ISSUE`], selecting the whole issue updated.
1892    pub const UPDATE_ISSUE_WHOLE: &str = concat!(
1893        r#"mutation($input:UpdateIssueInput!"#,
1894        whole_variables!(),
1895        r#"){updateIssue(input:$input){issue{id "#,
1896        whole_issue!(),
1897        r#"}}}"#,
1898        board_issue!()
1899    );
1900    /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
1901    pub const UPDATE_FIELD_WHOLE: &str = concat!(
1902        r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
1903        whole_variables!(),
1904        r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
1905        whole_issue!(),
1906        r#"}}}}"#,
1907        board_issue!()
1908    );
1909    /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
1910    pub const CLEAR_FIELD_WHOLE: &str = concat!(
1911        r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
1912        whole_variables!(),
1913        r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
1914        whole_issue!(),
1915        r#"}}}}"#,
1916        board_issue!()
1917    );
1918
1919    /// The document sent in `document`'s place while a write the store's cache expects runs,
1920    /// when it has one.
1921    #[must_use]
1922    pub fn whole_twin(document: &str) -> Option<&'static str> {
1923        [
1924            (ADD_COMMENT, ADD_COMMENT_WHOLE),
1925            (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
1926            (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
1927            (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
1928            (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
1929        ]
1930        .into_iter()
1931        .find(|(plain, _)| *plain == document)
1932        .map(|(_, whole)| whole)
1933    }
1934
1935    /// Every document above, with what this source is doing when it sends one.
1936    ///
1937    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
1938    /// name the call that was refused, and a `match` with a catch-all arm would answer a
1939    /// document added later with "talking to GitHub" and never say so.
1940    ///
1941    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
1942    /// const` here that this list omits, so the two cannot part — which is the same guard
1943    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
1944    pub const DOCUMENTS: [(&str, &str); 48] = [
1945        (SEARCH_ISSUES, "searching this board's issues"),
1946        (ISSUE, "reading one issue"),
1947        (
1948            ISSUE_BOARD_ITEMS,
1949            "reading one issue's board memberships past the page it came with",
1950        ),
1951        (SUB_ISSUES, "reading a project's tasks"),
1952        (BOARD, "reading the board"),
1953        (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
1954        (CLOSED_ITEMS, "reading the board's closed items"),
1955        (
1956            ISSUES_BY_REFERENCE,
1957            "reading a batch of issues by their references",
1958        ),
1959        (BOARD_FIELDS, "reading the board's fields"),
1960        (DRAFT, "reading one draft"),
1961        (REPOSITORY, "reading the destination repository"),
1962        (
1963            PROJECT_VISIBILITY,
1964            "reading whether the board's project is public",
1965        ),
1966        (
1967            CREATION_CONTEXT,
1968            "reading the board's fields and the destination repository",
1969        ),
1970        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
1971        (CREATE_ISSUE, "creating an issue"),
1972        (
1973            ORIGIN_LOOKUPS,
1974            "looking up the items copied from several origins, and what creating one reads",
1975        ),
1976        (CREATE_ISSUES, "creating several issues together"),
1977        (
1978            CREATED_BOARD_ITEMS,
1979            "reading the board items of the issues a copy created",
1980        ),
1981        (
1982            WRITE_FIELD_VALUES,
1983            "writing board fields of the items a copy created",
1984        ),
1985        (CLOSE_ISSUES, "closing the issues a copy created"),
1986        (ADD_BLOCKED_BYS, "recording several dependencies together"),
1987        (ADD_TO_BOARD, "adding an issue to the board"),
1988        (UPDATE_ISSUE, "updating an issue"),
1989        (UPDATE_DRAFT, "updating a draft item"),
1990        (UPDATE_FIELD, "writing a board field"),
1991        (UPDATE_FIELDS, "writing board fields together"),
1992        (CLEAR_FIELD, "clearing a board field"),
1993        (
1994            CREATE_FIELD,
1995            "creating a board single-select field with its options",
1996        ),
1997        (
1998            STATUS_OPTIONS_SNAPSHOT,
1999            "snapshotting board Status options and assignments",
2000        ),
2001        (
2002            STATUS_OPTIONS_UPDATE,
2003            "safely replacing the board Status option list",
2004        ),
2005        (ADD_SUB_ISSUE, "filing an issue under its project"),
2006        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
2007        (ADD_BLOCKED_BY, "recording a dependency"),
2008        (REMOVE_BLOCKED_BY, "removing a dependency"),
2009        (DELETE_ISSUE, "deleting an issue"),
2010        (ISSUE_COMMENTS, "reading a task's comments"),
2011        (ISSUE_DETAIL, "reading one issue with its comments"),
2012        (
2013            ISSUE_DETAILS,
2014            "reading a batch of issues with their comments",
2015        ),
2016        (
2017            ISSUE_COPY_DETAILS,
2018            "reading a batch of issues with their comments and dependencies for a copy",
2019        ),
2020        (COMMENT_ISSUE, "reading which issue a comment is on"),
2021        (ADD_COMMENT, "adding a comment"),
2022        (UPDATE_COMMENT, "editing a comment"),
2023        (DELETE_COMMENT, "deleting a comment"),
2024        (
2025            ADD_COMMENT_WHOLE,
2026            "adding a comment, answered with the whole issue",
2027        ),
2028        (
2029            UPDATE_COMMENT_WHOLE,
2030            "editing a comment, answered with the whole issue",
2031        ),
2032        (
2033            UPDATE_ISSUE_WHOLE,
2034            "updating an issue, answered with it whole",
2035        ),
2036        (
2037            UPDATE_FIELD_WHOLE,
2038            "writing a board field, answered with the whole issue",
2039        ),
2040        (
2041            CLEAR_FIELD_WHOLE,
2042            "clearing a board field, answered with the whole issue",
2043        ),
2044    ];
2045}
2046
2047/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
2048/// under, the variable carrying its input, and the boolean variable that includes it.
2049#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2050pub struct FieldSlot {
2051    /// The key of the answer's `data` this field's payload is under.
2052    pub alias: &'static str,
2053    /// The variable carrying this field's input.
2054    pub variable: &'static str,
2055    /// The boolean variable whose `true` sends this field.
2056    pub include: &'static str,
2057}
2058
2059/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
2060pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
2061    FieldSlot {
2062        alias: "updateProjectV2ItemFieldValue",
2063        variable: "input",
2064        include: "writeFirst",
2065    },
2066    FieldSlot {
2067        alias: "second",
2068        variable: "second",
2069        include: "writeSecond",
2070    },
2071    FieldSlot {
2072        alias: "third",
2073        variable: "third",
2074        include: "writeThird",
2075    },
2076    FieldSlot {
2077        alias: "fourth",
2078        variable: "fourth",
2079        include: "writeFourth",
2080    },
2081    FieldSlot {
2082        alias: "fifth",
2083        variable: "fifth",
2084        include: "writeFifth",
2085    },
2086    FieldSlot {
2087        alias: "sixth",
2088        variable: "sixth",
2089        include: "writeSixth",
2090    },
2091];
2092
2093/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
2094pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
2095    FieldSlot {
2096        alias: "cleared",
2097        variable: "clear",
2098        include: "writeClear",
2099    },
2100    FieldSlot {
2101        alias: "clearedSecond",
2102        variable: "clearSecond",
2103        include: "writeClearSecond",
2104    },
2105    FieldSlot {
2106        alias: "clearedThird",
2107        variable: "clearThird",
2108        include: "writeClearThird",
2109    },
2110];
2111
2112/// Which of GitHub's two rate limiters refused a request.
2113///
2114/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
2115/// secondary one — so an operator told the wrong one takes the wrong next step, which is
2116/// the whole reason this is carried rather than collapsed into "rate limited".
2117#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2118enum Limiter {
2119    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
2120    Primary,
2121    /// The burst limiter over content-generating requests, which nothing reports.
2122    Secondary,
2123}
2124
2125/// The wordings GitHub answers a secondary rate limit with.
2126///
2127/// It sends them under a forbidden status, under a too-many-requests status, and inside
2128/// the `errors` of a *successful* response, which is why the text is what this matches on
2129/// rather than the status. `abuse detection` is the wording GitHub used before the
2130/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
2131/// what a burst of content creation is refused with.
2132///
2133/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
2134/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
2135/// when, and the drift gate reconciles the two lists both ways. Public for that gate
2136/// alone — a caller has no use for it, and matching on a refusal is this source's job.
2137pub const SECONDARY_WORDINGS: [&str; 5] = [
2138    "secondary rate limit",
2139    "temporarily blocked from content creation",
2140    "abuse detection",
2141    "submitted too quickly",
2142    "exceeded a secondary",
2143];
2144
2145/// The wordings GitHub answers an exhausted primary budget with.
2146///
2147/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
2148/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
2149/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
2150/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
2151/// two phrases is a substring of it, so without it that answer read as a refusal that will
2152/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
2153/// one reason.
2154pub const PRIMARY_WORDINGS: [&str; 4] = [
2155    "api rate limit exceeded",
2156    "api rate limit already exceeded",
2157    "rate limit exceeded",
2158    "rate_limited",
2159];
2160
2161/// What a response *says about itself*, which is the only place a refusal can be read.
2162///
2163/// Deliberately not the whole response body. A board is a place people write about their
2164/// own work, and a task on it titled "the secondary rate limit" would, matched across the
2165/// raw text, turn a perfectly good answer into a refusal this source then waited out and
2166/// reported. So the item data is never read: what is read is GitHub's own REST-style
2167/// `message` envelope, which is what a forbidden status carries, and the `message` and
2168/// `type` of each GraphQL error, which is where a *successful* response says it.
2169///
2170/// A body that is not JSON at all has nothing structured to read, so only a failing
2171/// response's own text is taken — a successful response that is not JSON is malformed
2172/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
2173fn refusal_wording(status: StatusCode, body: &str) -> String {
2174    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
2175        return if status.is_success() {
2176            String::new()
2177        } else {
2178            body.to_owned()
2179        };
2180    };
2181    let mut said: Vec<&str> = parsed
2182        .get("message")
2183        .and_then(Value::as_str)
2184        .into_iter()
2185        .collect();
2186    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
2187        for error in errors {
2188            said.extend(
2189                ["message", "type"]
2190                    .into_iter()
2191                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
2192            );
2193        }
2194    }
2195    said.join("; ")
2196}
2197
2198impl Limiter {
2199    /// Which limiter refused this response, or `None` when none of them did.
2200    ///
2201    /// The wording is read first and the status only decides what carries none of it,
2202    /// because GitHub answers a secondary limit with a forbidden status far more often
2203    /// than with too-many-requests — while a forbidden status saying nothing about a limit
2204    /// really is a credential this token lacks.
2205    ///
2206    /// A response is a refusal because of its status or its own wording. A spent budget
2207    /// only ever explains one; it never turns an answer into a refusal.
2208    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
2209        let normalized = refusal_wording(status, body).to_ascii_lowercase();
2210        if SECONDARY_WORDINGS
2211            .iter()
2212            .any(|wording| normalized.contains(wording))
2213        {
2214            return Some(Self::Secondary);
2215        }
2216        if status == StatusCode::TOO_MANY_REQUESTS {
2217            return Some(Self::Primary);
2218        }
2219        // An exhausted budget *explains* a response that failed; it does not make one that
2220        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
2221        // request the budget allowed as well as on the ones it then refuses, so reading
2222        // the header alone threw away a good answer — and, once refusals were retried,
2223        // replayed a request that had already taken effect.
2224        if !status.is_success() && budget_exhausted {
2225            return Some(Self::Primary);
2226        }
2227        // A successful response saying it: GitHub reports a GraphQL rate limit in the
2228        // `errors` of an HTTP 200, where nothing about the status says so at all.
2229        if status.is_success()
2230            && PRIMARY_WORDINGS
2231                .iter()
2232                .any(|wording| normalized.contains(wording))
2233        {
2234            return Some(Self::Primary);
2235        }
2236        None
2237    }
2238
2239    /// What this limiter is called where an operator can look it up.
2240    const fn name(self) -> &'static str {
2241        match self {
2242            Self::Primary => "GitHub's primary API rate limit",
2243            Self::Secondary => "GitHub's secondary rate limit",
2244        }
2245    }
2246
2247    /// What the endpoint an operator would go and check says about this limiter.
2248    const fn where_to_look(self) -> &'static str {
2249        match self {
2250            Self::Primary => {
2251                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
2252                 comes back."
2253            }
2254            Self::Secondary => {
2255                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
2256                 primary budget and does not report this one, so budget showing there says \
2257                 nothing about this refusal, and every further attempt extends it."
2258            }
2259        }
2260    }
2261
2262    /// The next step this limiter actually calls for.
2263    const fn what_to_do(self) -> &'static str {
2264        match self {
2265            Self::Primary => {
2266                "wait for the reset `gh api rate_limit` reports, then run the command again."
2267            }
2268            Self::Secondary => {
2269                "leave this board alone for a few minutes, then run the command again — or \
2270                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
2271            }
2272        }
2273    }
2274}
2275
2276/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
2277#[derive(Debug, Clone, Copy)]
2278struct Limited {
2279    limiter: Limiter,
2280    hint: Option<u64>,
2281    /// Which header the hint came from: `retry-after`, or an exhausted primary budget's
2282    /// `x-ratelimit-reset`.
2283    hinted_by: Hint,
2284}
2285
2286/// Where a refusal's hint came from, which is what its wait is named after.
2287#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2288enum Hint {
2289    /// `retry-after`, or no hint at all.
2290    RetryAfter,
2291    /// An exhausted primary budget's `x-ratelimit-reset`.
2292    PrimaryReset,
2293}
2294
2295impl Limited {
2296    /// The wait this refusal is answered with, when the backing-off schedule is at `backoff`,
2297    /// and the cause it is named by: the hint, raised to the schedule's own next wait —
2298    /// GitHub really does send `retry-after: 0`, and retrying at once is the one move that
2299    /// extends a secondary limit — or the schedule alone when there is none.
2300    fn wait(self, backoff: Duration) -> (Duration, WaitCause) {
2301        match (self.hint, self.hinted_by) {
2302            (Some(hint), Hint::RetryAfter) => (
2303                Duration::from_secs(hint).max(backoff),
2304                pacing::cause::RETRY_AFTER,
2305            ),
2306            (Some(hint), Hint::PrimaryReset) => (
2307                Duration::from_secs(hint).max(backoff),
2308                pacing::cause::PRIMARY_RESET,
2309            ),
2310            (None, _) => (backoff, pacing::cause::BACKOFF),
2311        }
2312    }
2313
2314    /// What the caller is told once this source has waited as long as it may.
2315    ///
2316    /// Both limiters report as [`SourceError::RateLimited`], because that is what
2317    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
2318    /// about *which* limiter it was makes it a different kind of failure. What differs is
2319    /// the operator's next step, and that is what the message carries — a secondary
2320    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
2321    /// budget looks fine, and then back to retry the very burst that was refused.
2322    fn exhausted(
2323        self,
2324        doing: &str,
2325        waits: u32,
2326        waited: Duration,
2327        needed: Duration,
2328        budget: Duration,
2329    ) -> SourceError {
2330        SourceError::RateLimited {
2331            retry_after_seconds: self.hint,
2332            message: Some(format!(
2333                "{} refused this source while {doing}; it waited {} out over {} and was refused \
2334                 again, and the next wait of {} would take it past the {} one call may spend \
2335                 waiting. {} next: {}",
2336                self.limiter.name(),
2337                plural(waits, "refusal"),
2338                seconds(waited),
2339                seconds(needed),
2340                seconds(budget),
2341                self.limiter.where_to_look(),
2342                self.limiter.what_to_do(),
2343            )),
2344        }
2345    }
2346}
2347
2348/// One HTTP attempt's result, with what its response said about the rate limit.
2349///
2350/// The two travel together so the record and the outcome are written from the same place:
2351/// what a response said about the budget is only readable while that response is in hand,
2352/// and what the attempt *meant* is only decidable once its body has been read.
2353struct Attempted {
2354    result: Result<Value, Attempt>,
2355    limits: accounting::RateLimit,
2356    /// GitHub's own reported cost for this call, for a document that asked for it.
2357    reported_cost: Option<u64>,
2358    response_outcome: accounting::ResponseOutcome,
2359}
2360
2361/// One attempt's outcome: an error to report, or a rate limit to wait out.
2362enum Attempt {
2363    Failed(SourceError),
2364    Limited(Limited),
2365}
2366
2367fn plural(count: u32, thing: &str) -> String {
2368    if count == 1 {
2369        format!("{count} {thing}")
2370    } else {
2371        format!("{count} {thing}s")
2372    }
2373}
2374
2375fn seconds(duration: Duration) -> String {
2376    format!("{:.1}s", duration.as_secs_f64())
2377}
2378
2379/// The member [`stamp_served`] writes onto every node of a response that says when it last
2380/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
2381const SERVED_AT: &str = "onetaskgraphServedAt";
2382
2383/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
2384/// node's time later knows the server's clock on the response that read it — which is what
2385/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
2386fn stamp_served(data: &mut Value, served: &str) {
2387    match data {
2388        Value::Object(members) => {
2389            if members.contains_key("updatedAt") {
2390                members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
2391            }
2392            for member in members.values_mut() {
2393                stamp_served(member, served);
2394            }
2395        }
2396        Value::Array(items) => {
2397            for item in items {
2398                stamp_served(item, served);
2399            }
2400        }
2401        _ => {}
2402    }
2403}
2404
2405/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
2406///
2407/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
2408/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
2409/// header, and neither is what makes a response a refusal — so the whole cost of one this
2410/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
2411/// instead. Refusing the response over the header would turn a readable refusal into an
2412/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
2413fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
2414    value
2415        .and_then(|value| value.to_str().ok())
2416        .and_then(|value| value.trim().parse::<u64>().ok())
2417}
2418
2419/// Every mutation this source sends creates content — an issue, a board item, a field of
2420/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
2421/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
2422/// and what the keyword says are the same set. That is what makes the keyword a sound test
2423/// rather than a convenient one: counting an edit or a removal the limiter might not have
2424/// counted costs a wait, and not counting one it did count costs the next fifty minutes.
2425pub(crate) fn is_mutation(query: &str) -> bool {
2426    query.trim_start().starts_with("mutation")
2427}
2428
2429/// What this source was doing, for a diagnostic that has to say so.
2430///
2431/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
2432/// a document added without a description is caught by that list's own gate instead of
2433/// falling through to the vague arm below.
2434fn operation_description(query: &str) -> &'static str {
2435    graphql::DOCUMENTS
2436        .iter()
2437        .find(|(document, _)| *document == query)
2438        .map_or("talking to GitHub", |(_, doing)| *doing)
2439}
2440
2441/// GitHub's published ceiling on content-generating requests in any one minute.
2442///
2443/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
2444/// GitHub's number rather than this source's. This source paces at it over its window: a
2445/// content-creating request goes out at once while the minute's creations leave room for it,
2446/// and waits until they do otherwise — see the `pacing` module.
2447pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
2448/// The same ceiling as GitHub publishes it for any one hour, paced over its window as the
2449/// per-minute one is. Pinned beside its sibling so the gate would notice either one moving.
2450pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
2451/// GitHub's published ceiling on secondary points in any one minute: what every GraphQL
2452/// request is charged against, a mutation [`SECONDARY_POINTS_PER_MUTATION`] and a query
2453/// [`SECONDARY_POINTS_PER_QUERY`]. Pinned and paced as [`CONTENT_CREATION_PER_MINUTE`] is.
2454pub const SECONDARY_POINTS_PER_MINUTE: u64 = 2_000;
2455/// The secondary points one GraphQL request carrying a mutation is charged.
2456pub const SECONDARY_POINTS_PER_MUTATION: u64 = 5;
2457/// The secondary points one GraphQL request carrying no mutation is charged.
2458pub const SECONDARY_POINTS_PER_QUERY: u64 = 1;
2459/// The fastest *steady* spacing of content-creating mutations that cannot exceed
2460/// [`CONTENT_CREATION_PER_MINUTE`], in milliseconds: 60000/80 is 750.
2461///
2462/// It is no longer the default. This source paces by GitHub's windows instead, so a copy's
2463/// few dozen writes go out as fast as GitHub answers them while the minute has room, rather
2464/// than 750 ms apart — which was most of a plan copy's wall time. An installation that wants
2465/// mutations spaced as well says so through `pacing.min_mutation_interval_ms`, and this is the
2466/// value that holds a long sustained burst under the per-minute bound by spacing alone.
2467pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2468/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2469///
2470/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2471/// own advice for a secondary limit — wait, and wait longer each time — without spending
2472/// the first minute of a transient refusal doing nothing.
2473pub const RETRY_BACKOFF_MS: u64 = 1_000;
2474/// Total time one call may spend waiting out rate limits before it reports a failure.
2475///
2476/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2477/// short enough that a command an operator is watching returns. The bound is what makes
2478/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2479/// the limiter, not in a process nobody can tell from a wedged one.
2480pub const RETRY_BUDGET_MS: u64 = 120_000;
2481
2482fn default_token_env() -> String {
2483    "GH_PROJECTS_TOKEN".to_owned()
2484}
2485fn default_endpoint() -> String {
2486    "https://api.github.com/graphql".to_owned()
2487}
2488
2489/// The name of a `Status` single-select option on the board.
2490///
2491/// Validated on the way in rather than checked later, so a blank option name — which
2492/// nothing on a board can be — is a state this type cannot hold.
2493#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2494#[serde(try_from = "String")]
2495#[schemars(extend("minLength" = 1))]
2496pub struct ColumnName(String);
2497
2498impl ColumnName {
2499    /// The option name, as the board spells it.
2500    fn as_str(&self) -> &str {
2501        &self.0
2502    }
2503}
2504
2505impl TryFrom<String> for ColumnName {
2506    type Error = String;
2507
2508    fn try_from(name: String) -> Result<Self, Self::Error> {
2509        if name.trim().is_empty() {
2510            return Err("a status_mapping option name cannot be blank".to_owned());
2511        }
2512        Ok(Self(name))
2513    }
2514}
2515
2516/// The two closed states this product can mean.
2517///
2518/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which nothing here ever writes:
2519/// an issue closed as one *reads* `cancelled`, because its work is another issue's, but this
2520/// product never decides on a person's behalf that two issues are the same work.
2521#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2522#[serde(rename_all = "kebab-case")]
2523pub enum ClosedState {
2524    /// `COMPLETED` — precisely done.
2525    Completed,
2526    /// `NOT_PLANNED` — precisely cancelled.
2527    NotPlanned,
2528}
2529
2530impl ClosedState {
2531    const fn reason(self) -> &'static str {
2532        match self {
2533            Self::Completed => "COMPLETED",
2534            Self::NotPlanned => "NOT_PLANNED",
2535        }
2536    }
2537}
2538
2539/// Configuration for one GitHub Projects v2 board.
2540///
2541/// It serializes to a document this same type reads back as itself, so a caller writing a
2542/// configuration — a consumer filing onto a board it configures — can build one here rather
2543/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2544#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2545#[serde(default, deny_unknown_fields)]
2546pub struct GitHubProjectsConfig {
2547    /// Login of the user or organization which owns the board.
2548    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2549    /// The project number shown in the board's GitHub URL.
2550    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2551    // 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.
2552    /// `owner/name` of the repository this source creates an issue in when the item's own
2553    /// `repositories` field does not decide it.
2554    ///
2555    /// An item naming exactly one repository is created there; a task or a document naming
2556    /// none or several is created in its parent project's repository; and a project, or a
2557    /// task or document with no parent, naming none or several is created here. A board
2558    /// has no repository of its own and `createIssue` requires one, so a write without
2559    /// this is refused naming the field. Reads never need it.
2560    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2561    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2562    /// Environment variable containing a fine-grained token with Projects and Issues
2563    /// read/write plus Pull requests read-only access for every repository represented on
2564    /// the board.
2565    #[serde(default = "default_token_env")]
2566    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2567    /// GraphQL endpoint. GitHub Enterprise installations may override it.
2568    #[serde(default = "default_endpoint")]
2569    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2570    /// Per-instance mapping from a status category to the option of the board's one
2571    /// `Status` field it lands on, for a task and for a project.
2572    ///
2573    /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2574    /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2575    /// where a kind left out leaves the category unmapped for that kind. A category this
2576    /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2577    /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2578    /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2579    /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2580    /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2581    /// either kind. No two categories may name one option for the same kind, ignoring case.
2582    /// `unknown` may name one existing option; every unknown word then lands on it and
2583    /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2584    /// each unknown word because it never creates board options.
2585    #[serde(default)]
2586    pub status_mapping: StatusMapping,
2587    /// Per-instance mapping from a task's priority to an option of this board's
2588    /// single-select field named `Priority`.
2589    ///
2590    /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2591    /// other priority is refused before it reaches this board. Present, each of `urgent`,
2592    /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2593    /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2594    /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2595    /// no two levels may name one option. Reads and writes never create the field or an
2596    /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2597    /// the board lacks is refused pointing there.
2598    #[serde(default)]
2599    pub priority_mapping: Option<PriorityMappingConfig>,
2600    /// Caller metadata values this source also writes to board text fields, so the board can
2601    /// filter on them.
2602    ///
2603    /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2604    /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2605    /// value. On every item write — create, update and copy, of a task, a project or a
2606    /// document — a string found there is written to the field, and nothing is sent when the
2607    /// field already holds it; nothing there, or `null`, clears the field when it holds a
2608    /// value; any other JSON type is refused before any mutation. The metadata comment in the
2609    /// issue body stays the one home of the value, and every read reports metadata from it.
2610    /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2611    /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2612    /// non-text field of that name, is refused pointing there.
2613    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2614    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2615    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2616    #[schemars(!skip_serializing_if)]
2617    pub metadata_fields: Vec<MetadataFieldConfig>,
2618    /// How fast this source writes, and how long it waits out a rate-limit refusal.
2619    ///
2620    /// Every field keeps its shipped default when it is absent, and the defaults are
2621    /// GitHub's own published limits rather than taste. See [`Pacing`].
2622    #[serde(default)]
2623    pub pacing: PacingConfig,
2624}
2625
2626/// Which option of the board's `Priority` field each priority lands on.
2627///
2628/// One member per level rather than a map, so a key that is not a level is refused where
2629/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2630/// value in the field, not an option of it.
2631#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2632#[serde(default, deny_unknown_fields)]
2633pub struct PriorityMappingConfig {
2634    /// The option `urgent` lands on; `Urgent` when absent.
2635    pub urgent: Option<PriorityOptionName>,
2636    /// The option `high` lands on; `High` when absent.
2637    pub high: Option<PriorityOptionName>,
2638    /// The option `medium` lands on; `Medium` when absent.
2639    pub medium: Option<PriorityOptionName>,
2640    /// The option `low` lands on; `Low` when absent.
2641    pub low: Option<PriorityOptionName>,
2642}
2643
2644/// The name of an option of the board's `Priority` single-select field.
2645///
2646/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2647/// blank name.
2648#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2649#[serde(try_from = "String")]
2650#[schemars(extend("minLength" = 1))]
2651pub struct PriorityOptionName(String);
2652
2653impl PriorityOptionName {
2654    /// The option name, as the board spells it.
2655    fn as_str(&self) -> &str {
2656        &self.0
2657    }
2658}
2659
2660impl TryFrom<String> for PriorityOptionName {
2661    type Error = String;
2662
2663    fn try_from(name: String) -> Result<Self, Self::Error> {
2664        if name.trim().is_empty() {
2665            return Err("a priority_mapping option name cannot be blank".to_owned());
2666        }
2667        Ok(Self(name))
2668    }
2669}
2670
2671/// The name of the board field a priority is held in.
2672pub const PRIORITY_FIELD: &str = "Priority";
2673
2674/// The four priorities a board option can hold, in the order a new `Priority` field lists
2675/// them. `none` is not among them: it is the field holding no value.
2676///
2677/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2678/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2679/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2680/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2681pub const PRIORITY_LEVELS: [Priority; 4] = [
2682    Priority::Urgent,
2683    Priority::High,
2684    Priority::Medium,
2685    Priority::Low,
2686];
2687
2688/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2689/// see that list for what this pins.
2690#[must_use]
2691pub const fn level_position(priority: Priority) -> Option<usize> {
2692    match priority {
2693        Priority::None => None,
2694        Priority::Urgent => Some(0),
2695        Priority::High => Some(1),
2696        Priority::Medium => Some(2),
2697        Priority::Low => Some(3),
2698    }
2699}
2700
2701/// This instance's complete priority-to-option mapping, read in both directions.
2702///
2703/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2704/// two levels name one option.
2705#[derive(Debug, Clone)]
2706struct PriorityMapping {
2707    options: [PriorityOptionName; 4],
2708}
2709
2710impl PriorityMapping {
2711    fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2712        let shipped = |name: &str| PriorityOptionName(name.to_owned());
2713        let mapping = Self {
2714            options: [
2715                config.urgent.unwrap_or_else(|| shipped("Urgent")),
2716                config.high.unwrap_or_else(|| shipped("High")),
2717                config.medium.unwrap_or_else(|| shipped("Medium")),
2718                config.low.unwrap_or_else(|| shipped("Low")),
2719            ],
2720        };
2721        for (index, option) in mapping.options.iter().enumerate() {
2722            if let Some(earlier) = mapping.options[..index]
2723                .iter()
2724                .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2725            {
2726                return Err(SourceError::Config {
2727                    message: format!(
2728                        "priority_mapping of source {instance} sends both {} and {} to the board \
2729                         option {:?}; one option cannot read back as two priorities",
2730                        PRIORITY_LEVELS[earlier],
2731                        PRIORITY_LEVELS[index],
2732                        option.as_str()
2733                    ),
2734                });
2735            }
2736        }
2737        Ok(mapping)
2738    }
2739
2740    /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2741    fn option(&self, priority: Priority) -> Option<&str> {
2742        level_position(priority).map(|index| self.options[index].as_str())
2743    }
2744
2745    /// The priority a board option name reports, or `None` when nothing maps to it.
2746    fn priority_of(&self, option: &str) -> Option<Priority> {
2747        self.options
2748            .iter()
2749            .position(|name| name.as_str().eq_ignore_ascii_case(option))
2750            .map(|index| PRIORITY_LEVELS[index])
2751    }
2752
2753    /// Every mapped option name, in the order a new `Priority` field lists them.
2754    fn names(&self) -> impl Iterator<Item = &str> {
2755        self.options.iter().map(PriorityOptionName::as_str)
2756    }
2757}
2758
2759/// One metadata value this source projects onto a board text field.
2760#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2761#[serde(deny_unknown_fields)]
2762pub struct MetadataFieldConfig {
2763    /// The name of the board text field the value is written to.
2764    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.
2765    /// The top-level metadata key the value is read from. A key under the reserved
2766    /// `onetaskgraph.` prefix is refused.
2767    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.
2768    /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2769    /// key's own value is the one projected. Left out of a serialized entry when empty.
2770    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2771    // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
2772    #[schemars(!skip_serializing_if)]
2773    pub path: Vec<String>,
2774}
2775
2776/// The board fields no metadata value may be projected onto, compared ignoring case: the
2777/// three this source writes itself, and those GitHub owns on every board.
2778// 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.
2779const UNPROJECTABLE_FIELDS: [&str; 13] = [
2780    STATUS_FIELD,
2781    PRIORITY_FIELD,
2782    ORIGIN_FIELD,
2783    "Title",
2784    "Assignees",
2785    "Labels",
2786    "Linked pull requests",
2787    "Milestone",
2788    "Repository",
2789    "Reviewers",
2790    "Parent issue",
2791    "Sub-issues progress",
2792    "Type",
2793];
2794
2795/// One validated [`MetadataFieldConfig`] entry.
2796#[derive(Debug, Clone)]
2797struct MetadataField {
2798    field: ProjectedField,
2799    key: ProjectedKey,
2800    path: Vec<String>,
2801}
2802
2803/// The name of a board text field a metadata value is projected onto: never blank, never one
2804/// this source or GitHub writes, and never one another entry of the same source names.
2805#[derive(Debug, Clone, PartialEq, Eq)]
2806struct ProjectedField(String);
2807
2808/// The top-level metadata key a projected value is read from: never blank, and never under
2809/// the namespace this product reserves.
2810#[derive(Debug, Clone, PartialEq, Eq)]
2811struct ProjectedKey(String);
2812
2813impl std::ops::Deref for ProjectedField {
2814    type Target = str;
2815    fn deref(&self) -> &str {
2816        &self.0
2817    }
2818}
2819
2820impl std::ops::Deref for ProjectedKey {
2821    type Target = str;
2822    fn deref(&self) -> &str {
2823        &self.0
2824    }
2825}
2826
2827/// What one item's metadata says one projected field should hold.
2828#[derive(Debug, Clone, PartialEq, Eq)]
2829enum Projected {
2830    /// A string to hold.
2831    Text(String),
2832    /// Nothing: the key or a step of the path is absent, or the value is `null`.
2833    Nothing,
2834}
2835
2836impl MetadataField {
2837    /// Validate every entry of one instance's `metadata_fields`, refusing the first that
2838    /// cannot stand, naming the source and the entry.
2839    fn resolve(
2840        configured: Vec<MetadataFieldConfig>,
2841        instance: &SourceName,
2842    ) -> Result<Vec<Self>, SourceError> {
2843        let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
2844        for (index, entry) in configured.into_iter().enumerate() {
2845            let refuse = |why: String| SourceError::Config {
2846                message: format!(
2847                    "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
2848                    entry.field, entry.key
2849                ),
2850            };
2851            if entry.field.trim().is_empty() {
2852                return Err(refuse("names a blank field".to_owned()));
2853            }
2854            if entry.key.trim().is_empty() {
2855                return Err(refuse("names a blank key".to_owned()));
2856            }
2857            if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
2858                return Err(refuse(format!(
2859                    "names a key under the reserved \"{}.\" prefix, which this product keeps \
2860                     for its own keys; next: project a caller key instead",
2861                    MetadataKey::RESERVED_NAMESPACE
2862                )));
2863            }
2864            if let Some(owned) = UNPROJECTABLE_FIELDS
2865                .iter()
2866                .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
2867            {
2868                return Err(refuse(format!(
2869                    "names the board field {owned:?}, which this source or GitHub already \
2870                     writes; next: name a text field of your own"
2871                )));
2872            }
2873            if let Some(earlier) = resolved
2874                .iter()
2875                .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
2876            {
2877                return Err(refuse(format!(
2878                    "names the same board field as metadata_fields[{earlier}]; one field \
2879                     cannot hold two values"
2880                )));
2881            }
2882            resolved.push(Self {
2883                field: ProjectedField(entry.field),
2884                key: ProjectedKey(entry.key),
2885                path: entry.path,
2886            });
2887        }
2888        Ok(resolved)
2889    }
2890
2891    /// The path as a refusal spells it: `[]` for the key's own value.
2892    fn spelled_path(&self) -> String {
2893        serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
2894    }
2895
2896    /// What `metadata` says this field should hold, or the refusal for a value no text field
2897    /// can hold.
2898    fn projected(
2899        &self,
2900        metadata: &BTreeMap<String, Value>,
2901        instance: &SourceName,
2902    ) -> Result<Projected, SourceError> {
2903        let mut value = metadata.get(&*self.key);
2904        for step in &self.path {
2905            value = value.and_then(|held| held.get(step.as_str()));
2906        }
2907        let found = match value {
2908            None | Some(Value::Null) => return Ok(Projected::Nothing),
2909            Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
2910            Some(Value::Bool(_)) => "a boolean",
2911            Some(Value::Number(_)) => "a number",
2912            Some(Value::Array(_)) => "an array",
2913            Some(Value::Object(_)) => "an object",
2914        };
2915        Err(SourceError::Refused {
2916            message: format!(
2917                "source {instance} projects metadata key {:?} at path {} onto its board text \
2918                 field {:?}, and this item holds {found} there; only a string, null or nothing \
2919                 can be written to a text field; next: store a string there, or remove the \
2920                 value",
2921                &*self.key,
2922                self.spelled_path(),
2923                &*self.field
2924            ),
2925        })
2926    }
2927}
2928
2929/// What one item's `Priority` field says, read through this instance's mapping.
2930#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
2931enum HeldPriority {
2932    /// A priority this source reports: an option the mapping names, or no value (`none`).
2933    Read(Priority),
2934    /// An option the mapping does not name, which is never read as a level or as `none`.
2935    Unmapped(String),
2936}
2937
2938/// How fast this source writes, and how long it waits out a rate-limit refusal.
2939///
2940/// Configurable because a GitHub Enterprise installation sets its own limits and an
2941/// operator who has already been refused may want to go slower still — not because the
2942/// defaults are guesses.
2943#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2944#[serde(default, deny_unknown_fields)]
2945pub struct PacingConfig {
2946    /// Shortest interval between two content-creating mutations, in milliseconds.
2947    ///
2948    /// Zero sends them as fast as they are asked for, which is what a fixture server on
2949    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
2950    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.
2951    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
2952    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
2953    /// zero while there is a budget to spend, because a schedule of zero-length waits
2954    /// consumes none of it and so never ends.
2955    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.
2956    /// Total time one call may spend waiting out rate limits, in milliseconds.
2957    ///
2958    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
2959    /// the bound is what makes this a wait rather than a hang.
2960    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.
2961}
2962
2963/// The largest any pacing setting may be, in milliseconds.
2964///
2965/// One hour. GitHub's own harshest published bound on content-generating requests works
2966/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
2967/// anything a real limit asks for, and past it the settings stop describing pacing at all:
2968/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
2969/// and an interval beyond it is a command that never sends its second mutation. It also
2970/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
2971/// what a `Duration` can hold on every platform.
2972pub const MAX_PACING_MS: u64 = 3_600_000;
2973
2974/// [`PacingConfig`] with every default resolved and every value checked, which is what the
2975/// source holds.
2976///
2977/// Unset, `min_mutation_interval_ms` spaces nothing, and every request is paced by GitHub's
2978/// secondary windows alone — see the `pacing` module; set, it spaces content-creating mutations
2979/// on top of those windows, [`MIN_MUTATION_INTERVAL_MS`] being the spacing that holds a sustained
2980/// burst under the per-minute bound by spacing alone; and zero sends every request as fast as it
2981/// is asked for, windows included.
2982#[derive(Debug, Clone, Copy)]
2983struct Pacing {
2984    /// Whether requests are held for GitHub's secondary windows: always, unless
2985    /// `min_mutation_interval_ms` is set to exactly zero, which has always asked for no
2986    /// pacing at all — what a fixture server on loopback wants and no board on github.com does.
2987    windows: bool,
2988    min_mutation_interval: Duration,
2989    retry_backoff: Duration,
2990    retry_budget: Duration,
2991}
2992
2993impl Pacing {
2994    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
2995    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2996        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
2997            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
2998                message: format!(
2999                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
3000                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
3001                     GitHub's own harshest published limit"
3002                ),
3003            }),
3004            Some(value) => Ok(Duration::from_millis(value)),
3005            None => Ok(Duration::from_millis(default)),
3006        };
3007        let retry_backoff = bounded(
3008            config.retry_backoff_ms,
3009            RETRY_BACKOFF_MS,
3010            "retry_backoff_ms",
3011        )?;
3012        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
3013        if retry_backoff.is_zero() && !retry_budget.is_zero() {
3014            return Err(SourceError::Config {
3015                message: format!(
3016                    "pacing.retry_backoff_ms of source {instance} is 0 while \
3017                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
3018                     none of that budget, so it would retry a refusal forever. Set a backoff of \
3019                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
3020                     waiting at all",
3021                    retry_budget.as_millis()
3022                ),
3023            });
3024        }
3025        Ok(Self {
3026            windows: config.min_mutation_interval_ms != Some(0),
3027            min_mutation_interval: bounded(
3028                config.min_mutation_interval_ms,
3029                0,
3030                "min_mutation_interval_ms",
3031            )?,
3032            retry_backoff,
3033            retry_budget,
3034        })
3035    }
3036}
3037
3038/// Factory for [`GitHubProjectsSource`].
3039#[derive(Debug, Clone, Copy, Default)]
3040pub struct Plugin;
3041
3042impl SourcePlugin for Plugin {
3043    fn kind(&self) -> &'static str {
3044        KIND
3045    }
3046    /// The endpoint, the owner and the board's number: what names one board.
3047    fn remote_identity(&self, config: &Value) -> Option<String> {
3048        let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
3049        Some(format!(
3050            "{}\n{}\n{}",
3051            config.endpoint, config.owner, config.project_number
3052        ))
3053    }
3054    fn config_schema(&self) -> Schema {
3055        schema_for!(GitHubProjectsConfig)
3056    }
3057    fn build(
3058        &self,
3059        name: &SourceName,
3060        config: &Value,
3061        secrets: &dyn SecretResolver,
3062    ) -> Result<Box<dyn TaskSource>, SourceError> {
3063        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
3064    }
3065
3066    fn build_with_clock(
3067        &self,
3068        name: &SourceName,
3069        config: &Value,
3070        secrets: &dyn SecretResolver,
3071        clock: SharedClock,
3072    ) -> Result<Box<dyn TaskSource>, SourceError> {
3073        self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
3074    }
3075}
3076
3077impl Plugin {
3078    /// Build a source recording every request it sends into an accounting the caller holds.
3079    ///
3080    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
3081    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
3082    /// session total rather than two — see [`accounting`] and
3083    /// [`GitHubProjectsSource::recording_into`].
3084    ///
3085    /// # Errors
3086    ///
3087    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
3088    /// [`SourceError::Config`] for configuration this plugin cannot use and
3089    /// [`SourceError::Auth`] for a credential it cannot find.
3090    pub fn build_recording_into(
3091        &self,
3092        name: &SourceName,
3093        config: &Value,
3094        secrets: &dyn SecretResolver,
3095        ledger: Arc<Accounting>,
3096    ) -> Result<Box<dyn TaskSource>, SourceError> {
3097        self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
3098    }
3099
3100    fn build_recording_with_clock(
3101        &self,
3102        name: &SourceName,
3103        config: &Value,
3104        secrets: &dyn SecretResolver,
3105        ledger: Arc<Accounting>,
3106        clock: SharedClock,
3107    ) -> Result<Box<dyn TaskSource>, SourceError> {
3108        let config: GitHubProjectsConfig =
3109            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
3110                message: format!("source {name}: {e}"),
3111            })?;
3112        let prefix = format!("source {name}: ");
3113        let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
3114            .map_err(|error| match error {
3115                // The shared `StatusMapping::distinct` names the source itself.
3116                SourceError::Config { message } if message.starts_with(&prefix) => {
3117                    SourceError::Config { message }
3118                }
3119                SourceError::Config { message } => SourceError::Config {
3120                    message: format!("{prefix}{message}"),
3121                },
3122                SourceError::Auth { message } => SourceError::Auth {
3123                    message: format!("source {name}: {message}"),
3124                },
3125                other => other,
3126            })?;
3127        source.clock = clock;
3128        Ok(Box::new(source))
3129    }
3130}
3131
3132/// Where a status category lands on this board, once configuration is resolved.
3133#[derive(Debug, Clone, PartialEq, Eq)]
3134enum StatusTarget {
3135    /// Not usable against this instance for this kind, and why.
3136    Disabled(UnmappedStatus),
3137    /// The board's `Status` option of this name.
3138    Column(ColumnName),
3139    /// A closed issue, with both its board option and the reason that says which closed it means.
3140    // 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.
3141    Terminal(ColumnName, ClosedState),
3142}
3143
3144/// Every status category, in the order the vocabulary declares them.
3145///
3146/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
3147/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
3148/// added to the shared vocabulary fails to compile until it is named there, and this
3149/// crate's suite reconciles this list against that enum's own derived schema, which is
3150/// generated from the variants rather than written beside them. The schema is what
3151/// catches a list left one short — a list checking only the positions it already holds
3152/// would pass while every mapping indexed by the new position panicked.
3153pub const CATEGORIES: [StatusCategory; 8] = [
3154    StatusCategory::Draft,
3155    StatusCategory::Backlog,
3156    StatusCategory::Todo,
3157    StatusCategory::Queued,
3158    StatusCategory::InProgress,
3159    StatusCategory::Done,
3160    StatusCategory::Cancelled,
3161    StatusCategory::Unknown,
3162];
3163
3164/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
3165#[must_use]
3166pub const fn category_position(category: StatusCategory) -> usize {
3167    match category {
3168        StatusCategory::Draft => 0,
3169        StatusCategory::Backlog => 1,
3170        StatusCategory::Todo => 2,
3171        StatusCategory::Queued => 3,
3172        StatusCategory::InProgress => 4,
3173        StatusCategory::Done => 5,
3174        StatusCategory::Cancelled => 6,
3175        StatusCategory::Unknown => 7,
3176    }
3177}
3178
3179/// The spelling a status category is configured and reported under.
3180fn category_name(category: StatusCategory) -> &'static str {
3181    match category {
3182        StatusCategory::Draft => "draft",
3183        StatusCategory::Backlog => "backlog",
3184        StatusCategory::Todo => "todo",
3185        StatusCategory::Queued => "queued",
3186        StatusCategory::InProgress => "in-progress",
3187        StatusCategory::Done => "done",
3188        StatusCategory::Cancelled => "cancelled",
3189        StatusCategory::Unknown => "unknown",
3190    }
3191}
3192
3193/// A shipped default's option name.
3194///
3195/// The literals below are this file's own and non-blank, and they are validated by the
3196/// one constructor a configured name goes through rather than beside it.
3197fn shipped_column(name: &'static str) -> ColumnName {
3198    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
3199}
3200
3201/// The shipped default for one category this instance's `status_mapping` does not mention,
3202/// for either kind.
3203fn shipped_default(category: StatusCategory) -> StatusTarget {
3204    match category {
3205        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
3206        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
3207        StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
3208        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
3209        StatusCategory::Done => {
3210            StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
3211        }
3212        StatusCategory::Cancelled => {
3213            StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
3214        }
3215        StatusCategory::Draft | StatusCategory::Unknown => {
3216            StatusTarget::Disabled(UnmappedStatus::Unconfigured)
3217        }
3218    }
3219}
3220
3221/// The two kinds a status is written and read for, each with its own half of the mapping.
3222const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
3223
3224/// This instance's complete category-to-target mapping for each kind, read in both
3225/// directions.
3226///
3227/// One target per category per kind, held at that category's own [`category_position`], so
3228/// a category missing from the mapping, named twice in it, or filed out of order is a state
3229/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
3230/// targets are options of the board's one `Status` field.
3231#[derive(Debug, Clone)]
3232struct BoardStatuses {
3233    tasks: [StatusTarget; CATEGORIES.len()],
3234    projects: [StatusTarget; CATEGORIES.len()],
3235}
3236
3237impl BoardStatuses {
3238    /// Resolve `configured` against the shipped defaults, refusing two categories one kind
3239    /// would read back from one option.
3240    ///
3241    /// A category the mapping does not mention keeps its shipped default for both kinds; one
3242    /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
3243    /// omits unmapped rather than defaulted.
3244    fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
3245        let resolve_kind =
3246            |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
3247                // `CATEGORIES[position] == category` for every category — the crate's suite
3248                // asserts it — so mapping the list in order fills each category's own slot.
3249                let mut targets = CATEGORIES.map(shipped_default);
3250                for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
3251                    if !configured.mentions(category) {
3252                        continue;
3253                    }
3254                    *slot = match configured.name_for(category, kind) {
3255                        Err(why) => StatusTarget::Disabled(why),
3256                        Ok(name) => {
3257                            let option = ColumnName::try_from(name.as_str().to_owned())
3258                                .map_err(|message| SourceError::Config { message })?;
3259                            match category {
3260                                StatusCategory::Done => {
3261                                    StatusTarget::Terminal(option, ClosedState::Completed)
3262                                }
3263                                StatusCategory::Cancelled => {
3264                                    StatusTarget::Terminal(option, ClosedState::NotPlanned)
3265                                }
3266                                _ => StatusTarget::Column(option),
3267                            }
3268                        }
3269                    };
3270                }
3271                StatusMapping::distinct(
3272                    instance,
3273                    kind,
3274                    CATEGORIES
3275                        .iter()
3276                        .zip(&targets)
3277                        .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
3278                )?;
3279                Ok(targets)
3280            };
3281        Ok(Self {
3282            tasks: resolve_kind(ItemKind::Task)?,
3283            projects: resolve_kind(ItemKind::Project)?,
3284        })
3285    }
3286
3287    /// Every category's target for `kind`, in category order.
3288    const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
3289        match kind {
3290            ItemKind::Task => &self.tasks,
3291            ItemKind::Project => &self.projects,
3292        }
3293    }
3294
3295    fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
3296        &self.targets(kind)[category_position(category)]
3297    }
3298
3299    /// The category a board option name reports for `kind`, or `None` when nothing of that
3300    /// kind maps to it.
3301    fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
3302        CATEGORIES.into_iter().find(|category| {
3303            self.target(kind, *category)
3304                .option()
3305                .is_some_and(|name| name.eq_ignore_ascii_case(option))
3306        })
3307    }
3308
3309    /// Every option name either kind maps a category to, each once ignoring case, in
3310    /// category order with a task's name before a project's — what the guarded setup asks
3311    /// the `Status` field to hold.
3312    fn wanted(&self) -> Vec<String> {
3313        let mut wanted: Vec<String> = Vec::new();
3314        for category in CATEGORIES {
3315            for kind in STATUS_KINDS {
3316                if let Some(name) = self.target(kind, category).option()
3317                    && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
3318                {
3319                    wanted.push(name.to_owned());
3320                }
3321            }
3322        }
3323        wanted
3324    }
3325
3326    /// The status an item of `kind` reports, from the three things a read of it says: its
3327    /// board `Status` option, whether its issue is closed, and the reason it was closed with.
3328    ///
3329    /// **A closed issue's category follows its closure**, by [`closed_category`]: completed
3330    /// or no reason reads `done`, not planned and duplicate read `cancelled`, and any other
3331    /// reason reads `unknown`. `REOPENED`-while-closed is a state this source can never
3332    /// produce, so it is read permissively rather than refused — reads are faithful, and
3333    /// refusals belong on writes.
3334    ///
3335    /// **Its name follows the closure too, unless the option agrees with it.** The option's
3336    /// own name is kept when this kind's mapping reads that option as the same category, or
3337    /// does not name it at all — so a closed issue in a "Shipped" column reports `done`
3338    /// named `Shipped`. An option the mapping reads as another category contradicts the
3339    /// closure, and so does no option at all: the name is then the option the mapping gives
3340    /// the closure's category, so an issue closed as completed at `Todo` reads `done` named
3341    /// by the board's `done` option, and a destination reading `Todo` as todo is never handed
3342    /// it. An `unknown` closure keeps the option, or `Closed` without one.
3343    ///
3344    /// An open item's option reads through its own kind's mapping, and an option that mapping
3345    /// does not name reads as `Unknown` under its own name.
3346    ///
3347    /// One function of those three rather than of a response, so a narrow status write can
3348    /// answer what a re-read would report by applying it to the state it has just written.
3349    fn status(
3350        &self,
3351        kind: ItemKind,
3352        option: Option<&str>,
3353        closed: bool,
3354        reason: Option<&str>,
3355    ) -> Status {
3356        if closed {
3357            let category = closed_category(reason);
3358            let agrees = option.filter(|option| {
3359                category == StatusCategory::Unknown
3360                    || self
3361                        .category_of(kind, option)
3362                        .is_none_or(|read| read == category)
3363            });
3364            let name = agrees.or_else(|| self.target(kind, category).option());
3365            let fallback = match category {
3366                StatusCategory::Done => "Done",
3367                StatusCategory::Cancelled => "Cancelled",
3368                _ => "Closed",
3369            };
3370            return Status {
3371                category,
3372                name: name.or(option).unwrap_or(fallback).to_owned(),
3373            };
3374        }
3375        let name = option.unwrap_or("Open").to_owned();
3376        Status {
3377            category: self
3378                .category_of(kind, &name)
3379                .unwrap_or(StatusCategory::Unknown),
3380            name,
3381        }
3382    }
3383
3384    /// The option a closed item's `Status` has to hold for its card to agree with its
3385    /// closure, and the category that closure reads as — or `None` when the item is open,
3386    /// its closure reads `unknown`, or its option already agrees.
3387    ///
3388    /// What `task status reconcile` writes: only `done` and `cancelled` closures are brought
3389    /// in line, each to the one option the mapping gives its category — an item with no
3390    /// option, or at one the mapping does not name, included.
3391    fn contradicted(
3392        &self,
3393        kind: ItemKind,
3394        option: Option<&str>,
3395        closed: bool,
3396        reason: Option<&str>,
3397    ) -> Option<(ClosedAs, &str)> {
3398        if !closed {
3399            return None;
3400        }
3401        let closed_as = match closed_category(reason) {
3402            StatusCategory::Done => ClosedAs::Done,
3403            StatusCategory::Cancelled => ClosedAs::Cancelled,
3404            _ => return None,
3405        };
3406        let wanted = self.target(kind, closed_as.category()).option()?;
3407        if option.is_some_and(|option| option.eq_ignore_ascii_case(wanted)) {
3408            return None;
3409        }
3410        Some((closed_as, wanted))
3411    }
3412}
3413
3414/// The category a closed issue reads as, from the reason it was closed with.
3415///
3416/// `COMPLETED`, and no reason at all, is finished work; `NOT_PLANNED` and `DUPLICATE` are both
3417/// work nobody will do here — a duplicate's work is the other issue's — and any other reason
3418/// is one this source cannot read a category from.
3419fn closed_category(reason: Option<&str>) -> StatusCategory {
3420    match reason {
3421        None | Some("COMPLETED") => StatusCategory::Done,
3422        Some("NOT_PLANNED" | "DUPLICATE") => StatusCategory::Cancelled,
3423        Some(_) => StatusCategory::Unknown,
3424    }
3425}
3426
3427impl BoardStatuses {
3428    /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
3429    /// case; a kind lacking none is left out.
3430    fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
3431        STATUS_KINDS
3432            .into_iter()
3433            .filter_map(|kind| {
3434                let missing: Vec<String> = self
3435                    .targets(kind)
3436                    .iter()
3437                    .filter_map(StatusTarget::option)
3438                    .filter(|wanted| {
3439                        !existing
3440                            .iter()
3441                            .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3442                    })
3443                    .map(str::to_owned)
3444                    .collect();
3445                (!missing.is_empty()).then_some(KindMissing { kind, missing })
3446            })
3447            .collect()
3448    }
3449}
3450
3451impl StatusTarget {
3452    /// The board option this target selects, or `None` for an unmapped one.
3453    fn option(&self) -> Option<&str> {
3454        match self {
3455            Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
3456            Self::Disabled(_) => None,
3457        }
3458    }
3459}
3460
3461// 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.
3462/// One repository this source can create an issue in, as `owner/name`.
3463///
3464/// Every `createIssue` this source sends names one of these: the item's own single
3465/// `repositories` entry, else its parent project issue's repository, else the configured
3466/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
3467/// that choice and says what it refuses before `createIssue`.
3468// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
3469#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
3470struct RepositoryTarget {
3471    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3472    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
3473}
3474
3475impl RepositoryTarget {
3476    fn parse(value: &str) -> Result<Self, SourceError> {
3477        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
3478            message: format!(
3479                "repository must be spelled owner/name; {value:?} names no repository"
3480            ),
3481        })?;
3482        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
3483            return Err(SourceError::Config {
3484                message: format!(
3485                    "repository must be spelled owner/name with a GitHub login and one \
3486                     repository name; {value:?} is not"
3487                ),
3488            });
3489        }
3490        Ok(Self {
3491            owner: owner.to_owned(),
3492            name: name.to_owned(),
3493        })
3494    }
3495
3496    /// The one host whose repositories this source creates issues in, spelled once: it is
3497    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
3498    const HOST: &str = "github.com";
3499
3500    fn origin(&self) -> String {
3501        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
3502    }
3503
3504    /// The repository a normalized origin names, or why it is none this source can create
3505    /// an issue in: another host, or more or fewer than `owner/name` under this one.
3506    fn from_origin(origin: &Repository) -> Result<Self, String> {
3507        let not_here = || {
3508            format!(
3509                "{} is not a {}/owner/name repository",
3510                origin.as_str(),
3511                Self::HOST
3512            )
3513        };
3514        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
3515        if host != Self::HOST {
3516            return Err(not_here());
3517        }
3518        Self::parse(rest).map_err(|_| not_here())
3519    }
3520
3521    fn slug(&self) -> String {
3522        format!("{}/{}", self.owner, self.name)
3523    }
3524}
3525
3526/// A source which reads GitHub afresh for every operation.
3527pub struct GitHubProjectsSource {
3528    /// This source's configured name, used both to tell a far end naming this source
3529    /// from one naming a system it knows nothing about, and to name the instance a
3530    /// status refusal is about.
3531    name: SourceName,
3532    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3533    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3534    repository: Option<RepositoryTarget>,
3535    endpoint: Url,
3536    token: SecretString,
3537    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3538    statuses: BoardStatuses,
3539    /// Where each priority lands on this board, or `None` when this instance holds none.
3540    priorities: Option<PriorityMapping>,
3541    /// The metadata values this instance projects onto board text fields, in configured order.
3542    metadata_fields: Vec<MetadataField>,
3543    client: Client,
3544    asset_client: Client,
3545    /// Every item this source has created in this command, in the order it created them —
3546    /// dropped by [`TaskSource::end_command`].
3547    ///
3548    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3549    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3550    /// a copy resolving a dependency on an item it had just created refused it as not
3551    /// found. A board read is completed from this — an item remembered here and absent from
3552    /// the read is added back, because the board really does hold it and only the read is
3553    /// behind.
3554    ///
3555    /// It is not a cache of a user's work: nothing is remembered that this process did not
3556    /// itself just write, it lives and dies with the process, and it is never consulted for
3557    /// an item this source did not create.
3558    created: Mutex<Vec<Resolved>>,
3559    /// Every item that already existed and that this source has written in this command, as
3560    /// it wrote it — dropped by [`TaskSource::end_command`].
3561    ///
3562    /// The other half of [`Self::created`], held on the same terms and for the reason a
3563    /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3564    /// filter is an index behind a write this process made moments ago, so a query matching
3565    /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3566    /// is remembered that this process did not itself just write.
3567    updated: Mutex<Vec<Resolved>>,
3568    /// Every issue this source has added a comment to or edited a comment of in this command
3569    /// — dropped by [`TaskSource::end_command`].
3570    ///
3571    /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3572    /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3573    /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3574    /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3575    /// from before — until the index caught up. Each id here is a candidate of every such
3576    /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3577    /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3578    /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3579    /// process wrote is still found only once the index has it.
3580    commented: Mutex<Vec<NativeId>>,
3581    /// How fast this source writes, and how long it waits out a refusal.
3582    pacing: Pacing,
3583    /// When the last content-creating mutation finished, or the moment the furthest-out
3584    /// reserved slot releases the next one, whichever is later — so the one after it can be
3585    /// spaced from that, when `pacing.min_mutation_interval_ms` asks for a spacing. See
3586    /// [`GitHubProjectsSource::paced`] for why completion rather than release is what it is
3587    /// measured from.
3588    last_mutation: Mutex<Option<Duration>>,
3589    /// GitHub's secondary windows as this source has charged them; see the `pacing` module.
3590    windows: Mutex<pacing::Windows>,
3591    /// The tasks a project holds, as a copy's batched read of the project and every one of them
3592    /// found them — see [`TaskSource::read_ahead_for_copy`] — for the length of one command, so
3593    /// a listing of that project's tasks is answered without a read of its own.
3594    task_children: Mutex<BTreeMap<NativeId, Vec<Resolved>>>,
3595    /// The web address of each issue this source created in the current command, as its
3596    /// `createIssue` answered it — what [`TaskSource::written_location`] answers, without a
3597    /// request.
3598    written: Mutex<BTreeMap<NativeId, String>>,
3599    /// What a copy has held back of the items it created, between
3600    /// [`TaskSource::begin_deferred_writes`] and [`TaskSource::finish_writes`] — `None` while
3601    /// nothing is being held back, when every write lands whole as it is made.
3602    deferred: Mutex<Option<Deferred>>,
3603    clock: SharedClock,
3604    numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3605    /// The board as this process last read it, for the length of one command — dropped by
3606    /// [`TaskSource::end_command`].
3607    ///
3608    /// A copy of a project used to re-read the whole board, paged, before writing each of
3609    /// its items, which is by far the largest part of a copy's request count and none of
3610    /// its work. Nothing else changes this board while a command runs — this source's own
3611    /// writes are the only writer — so one read answers them all.
3612    ///
3613    /// It is not a store of a user's work and it is not the cache the no-persistence
3614    /// invariant forbids: it lives and dies with the process exactly as `created` does,
3615    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3616    /// an item this command created and then depends on resolves whether or not GitHub's
3617    /// own eventually-consistent read has caught up. A write to an item already on the
3618    /// board updates the entry here too, so what this holds is the last read plus this
3619    /// process's own writes rather than a snapshot taken before them.
3620    board_cache: Mutex<Option<Board>>,
3621    /// Every issue this board's own search reported, for the length of one command — dropped
3622    /// by [`TaskSource::end_command`].
3623    ///
3624    /// The second half of a board read, and cached for the same reason and on the same
3625    /// terms as the first: it lives and dies with the process, nothing is written down, and
3626    /// a write this process makes updates the entry here exactly as it updates the one in
3627    /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3628    /// that lists this board's projects and its tasks pays for one search rather than two.
3629    search_cache: Mutex<Option<Vec<Resolved>>>,
3630    /// What each narrowed question GitHub was asked answered, keyed by that question, for
3631    /// the length of one command — dropped by [`TaskSource::end_command`].
3632    ///
3633    /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3634    /// and dies with the process, nothing is written down, a write this process makes
3635    /// updates the entry here as it updates the other two, and every answer is completed
3636    /// with this process's own writes each time it is given. A command that asks the same
3637    /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3638    /// write — pays for it once, which is what the whole-board read it replaced gave it.
3639    narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3640    search_next: Mutex<BTreeMap<String, Option<String>>>,
3641    /// Records already resolved in this command, reused by writes and for comment identity.
3642    /// Explicit item reads still reach GitHub. Nothing is persisted, and
3643    /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3644    /// its item as a person has since left it.
3645    resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3646    /// What [`TaskSource::read_ahead_for_copy`] read, by id — `None` for an id that read
3647    /// answered as holding nothing — each taken by the next read of that item by id and by
3648    /// nothing else, dropped by any mutation naming it, and cleared by
3649    /// [`TaskSource::end_command`]. So a read answered from it is the copy's own
3650    /// read-before-write, made in the same request as the others' rather than on its own.
3651    read_ahead: Mutex<BTreeMap<NativeId, Option<Resolved>>>,
3652    /// Every item a listing of one project's members carried whole in this command — the
3653    /// project issue and each of its sub-issues — kept for [`TaskSource::read_ahead_for_copy`]
3654    /// alone, which takes each one it is asked for into [`Self::read_ahead`] instead of reading
3655    /// it again. No other read is answered from it, so a read by id outside a copy's
3656    /// read-before-write reads GitHub as it always did. Dropped by any mutation naming an
3657    /// item, and cleared by [`TaskSource::end_command`].
3658    carried: Mutex<BTreeMap<NativeId, Resolved>>,
3659    /// Items an issue read by reference reached, held for the one status write each was read
3660    /// for and taken out by it alone — see [`Self::take_held`] — because that read selects only
3661    /// what such a write needs. Dropped by [`TaskSource::end_command`].
3662    held_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3663    /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3664    /// asked for under, with the project that selector named — for the length of one command,
3665    /// dropped by [`TaskSource::end_command`].
3666    ///
3667    /// A caller pages through a project's tasks one engine page at a time, and every page
3668    /// is cut from the whole list of them, so without this each page walked every
3669    /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3670    /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3671    /// dies with the process, nothing is written down, a write this process makes updates or
3672    /// removes the entry here as it does there, and every answer is completed with this
3673    /// process's own writes each time it is given.
3674    children_cache: Mutex<ProjectChildren>,
3675    /// The board's own id and field definitions as this process last read them on their
3676    /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3677    ///
3678    /// What a write needs of the board and its item does not say, read once per command
3679    /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3680    /// lives and dies with the process and nothing is written down. It holds no item and so
3681    /// can answer no question about one — see [`Self::board_fields`].
3682    fields_cache: Mutex<Option<BoardFields>>,
3683    /// Each destination repository's node id, resolved once per repository
3684    /// rather than per issue created.
3685    ///
3686    /// A repository's node id does not change, and re-reading it for every issue of a copy
3687    /// spent one request per item on an answer this source already had. It is a map rather
3688    /// than one entry because a copy files each item in the repository its own
3689    /// `repositories` field names, so a plan across five repositories asks GitHub five
3690    /// times and not once per item.
3691    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3692    /// What every request this source sends is recorded into.
3693    ///
3694    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3695    /// a request leaves this crate, so nothing has to be switched on for a session to be
3696    /// counted. It is shared rather than owned so a caller accounting for a whole session —
3697    /// its own schema verification, board lookups, residue sweep and cleanup beside this
3698    /// source's reads and writes — adds up one accounting instead of two. See
3699    /// [`accounting`] for what a record carries and what a session's spend is and is not.
3700    ledger: Arc<Accounting>,
3701    /// What [`expected`] holds for a write the store's cache expects: the journal while one is
3702    /// open, the issues primed from the cache, and the comments known on each.
3703    expected: expected::Held,
3704}
3705
3706/// GitHub's closed single-select color vocabulary.
3707#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3708#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
3709pub enum StatusOptionColor {
3710    /// Gray.
3711    Gray,
3712    /// Blue.
3713    Blue,
3714    /// Green.
3715    Green,
3716    /// Yellow.
3717    Yellow,
3718    /// Purple.
3719    Purple,
3720    /// Red.
3721    Red,
3722    /// Orange.
3723    Orange,
3724    /// Pink.
3725    Pink,
3726}
3727
3728/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
3729/// applies its additions.
3730#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3731pub enum SetupMode {
3732    /// Read without mutation.
3733    Plan,
3734    /// Apply and verify.
3735    Apply,
3736}
3737
3738/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
3739/// against it goes on compiling.
3740pub type StatusOptionsMode = SetupMode;
3741
3742/// The explicit result of the requested operation.
3743#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3744#[serde(rename_all = "kebab-case")]
3745pub enum StatusOptionsOutcome {
3746    /// A read-only plan.
3747    Planned,
3748    /// Apply found nothing missing.
3749    Unchanged,
3750    /// Additions were applied and verified.
3751    Applied,
3752}
3753
3754/// A GitHub single-select option's opaque GraphQL node identifier.
3755#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3756#[serde(transparent)]
3757pub struct StatusOptionId(#[schemars(length(min = 1))] String);
3758
3759impl TryFrom<String> for StatusOptionId {
3760    type Error = String;
3761
3762    fn try_from(id: String) -> Result<Self, Self::Error> {
3763        if id.trim().is_empty() {
3764            return Err("a GitHub Status option id cannot be blank".to_owned());
3765        }
3766        Ok(Self(id))
3767    }
3768}
3769
3770/// One existing or proposed option in a guarded Status-field update.
3771#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3772pub struct StatusOption {
3773    /// GitHub's stable id.
3774    pub id: StatusOptionId,
3775    /// The visible option name.
3776    pub name: ColumnName,
3777    /// GitHub's single-select color token.
3778    pub color: StatusOptionColor,
3779    /// The option description, including an empty one.
3780    pub description: String,
3781}
3782
3783/// One board item's Status assignment, retained as recovery data.
3784#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3785pub struct StatusAssignment {
3786    /// The project item id whose assignment this is.
3787    // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
3788    // carried verbatim as operator recovery data; introducing a semantic type would claim
3789    // validation rules GitHub does not publish and no operation here interprets.
3790    pub item_id: String,
3791    /// The selected option, absent when the item has no status.
3792    #[serde(skip_serializing_if = "Option::is_none")]
3793    pub option: Option<AssignedStatusOption>,
3794}
3795
3796/// The inseparable id and name of an assigned option.
3797#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3798pub struct AssignedStatusOption {
3799    /// GitHub's stable id.
3800    pub id: StatusOptionId,
3801    /// The visible name.
3802    pub name: ColumnName,
3803}
3804
3805/// The plan and verified outcome of reconciling configured Status options.
3806#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3807pub struct StatusOptionsReport {
3808    /// The configured source name.
3809    pub source: SourceName,
3810    /// Configured option names absent before the operation.
3811    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
3812    // `ColumnName` and has therefore already passed its nonblank validation; retaining the
3813    // serialized string here preserves the report's intentionally simple public contract.
3814    pub missing: Vec<String>,
3815    /// What the requested operation did.
3816    pub outcome: StatusOptionsOutcome,
3817    /// The complete option list observed before any mutation.
3818    pub existing: Vec<StatusOption>,
3819}
3820
3821#[derive(Debug, Clone, PartialEq, Eq)]
3822struct StatusSnapshot {
3823    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3824    // passed back as the mutation's project identity; a newtype could enforce no stronger
3825    // invariant because GitHub publishes no grammar for it.
3826    board_id: String,
3827    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3828    // passed back as the mutation's field identity; a newtype could enforce no stronger
3829    // invariant because GitHub publishes no grammar for it.
3830    field_id: String,
3831    options: Vec<StatusOption>,
3832    assignments: Vec<StatusAssignment>,
3833}
3834
3835/// The name of the board field a status is held in.
3836const STATUS_FIELD: &str = "Status";
3837
3838/// Every item's value of each field `report` names, as it stood before the setup wrote
3839/// anything — what a person puts back when the setup is refused part way.
3840fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
3841    let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
3842        .fields
3843        .iter()
3844        .map(|field| (field.field.name(), before.assignments(field.field)))
3845        .collect();
3846    serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
3847        message: format!("cannot render the pre-write field recovery snapshot: {error}"),
3848    })
3849}
3850
3851/// One board field the guarded setup reads and writes — every one it reads, and the only
3852/// ones it writes.
3853#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
3854pub enum BoardField {
3855    /// The single-select `Status` field every instance's `status_mapping` resolves into.
3856    Status,
3857    /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
3858    Priority,
3859}
3860
3861impl BoardField {
3862    /// The field's name on the board.
3863    #[must_use]
3864    pub const fn name(self) -> &'static str {
3865        match self {
3866            Self::Status => STATUS_FIELD,
3867            Self::Priority => PRIORITY_FIELD,
3868        }
3869    }
3870
3871    /// The field a board calls `name`, or `None` for one this setup does not own.
3872    fn named(name: &str) -> Option<Self> {
3873        [Self::Status, Self::Priority]
3874            .into_iter()
3875            .find(|field| field.name() == name)
3876    }
3877}
3878
3879/// What the guarded setup did to one field.
3880#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3881#[serde(rename_all = "kebab-case")]
3882pub enum FieldOutcome {
3883    /// A read-only plan.
3884    Planned,
3885    /// Apply found the field there with every configured option.
3886    Unchanged,
3887    /// Missing options were added to the field that was there, and verified.
3888    Applied,
3889    /// The field was not there; it was created holding the configured options, and verified.
3890    Created,
3891}
3892
3893/// One field's plan, or its verified outcome.
3894#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3895pub struct FieldReport {
3896    /// Which field.
3897    pub field: BoardField,
3898    /// Whether the board had the field before the operation.
3899    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
3900    // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
3901    // "outcome", "existing"}` — so folding one into the other would change a published JSON
3902    // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
3903    // one constructor, and it derives `outcome` from `exists` in one match.
3904    pub exists: bool,
3905    /// Configured option names the field lacked before the operation — every one of them,
3906    /// in the order a new field lists them, when the field was not there at all.
3907    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3908    // mapping name and has therefore already passed its nonblank validation; the serialized
3909    // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
3910    pub missing: Vec<String>,
3911    /// For the `Status` field, which item kind each missing name is configured for: one
3912    /// entry per kind `status_mapping` names a missing option for, task before project, each
3913    /// listing that kind's missing names in category order. A name both kinds use is in
3914    /// both. Empty — and left out of the JSON — when nothing is missing, and always for
3915    /// `Priority`, which only a task holds.
3916    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3917    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3918    // both SDKs model an absent `kinds` as an empty list rather than as `null`.
3919    #[schemars(!skip_serializing_if)]
3920    pub kinds: Vec<KindMissing>,
3921    /// What the requested operation did.
3922    pub outcome: FieldOutcome,
3923    /// The field's complete option list observed before any mutation; empty when the field
3924    /// was not there.
3925    pub existing: Vec<StatusOption>,
3926}
3927
3928/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
3929#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3930pub struct KindMissing {
3931    /// The kind these names are configured for.
3932    pub kind: ItemKind,
3933    /// The names that kind maps a category to and the field lacked, in category order.
3934    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3935    // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
3936    // intentionally simple public contract.
3937    pub missing: Vec<String>,
3938}
3939
3940/// The plan and verified outcome of setting up every field a source's configuration names.
3941#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3942pub struct FieldsReport {
3943    /// The configured source name.
3944    pub source: SourceName,
3945    /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
3946    // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
3947    // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
3948    // per field would change a published JSON shape. The states the list could hold and the
3949    // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
3950    // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
3951    pub fields: Vec<FieldReport>,
3952    /// Each board text field the source's `metadata_fields` projects a value onto, in
3953    /// configured order. Empty — and left out of the JSON — when it projects none.
3954    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3955    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3956    // both SDKs model an absent list as an empty one rather than as `null`.
3957    #[schemars(!skip_serializing_if)]
3958    pub metadata_fields: Vec<MetadataFieldReport>,
3959}
3960
3961/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
3962/// its GraphQL type when it has none — which is what a projected field's conflict reports.
3963///
3964/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
3965/// no conflict.
3966// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
3967// refuses exactly what `try_from` below does.
3968#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3969#[serde(transparent)]
3970pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
3971
3972/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
3973/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
3974/// neither SDK's validator can be relied on to support.
3975const NON_TEXT_FIELD_TYPE_PATTERN: &str =
3976    r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
3977
3978impl TryFrom<String> for NonTextFieldType {
3979    type Error = String;
3980
3981    fn try_from(kind: String) -> Result<Self, Self::Error> {
3982        if kind.is_empty() || kind.starts_with(char::is_whitespace) {
3983            return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
3984        }
3985        if kind == "TEXT" {
3986            return Err("a TEXT field is no conflict for a projected text field".to_owned());
3987        }
3988        Ok(Self(kind))
3989    }
3990}
3991
3992impl std::fmt::Display for NonTextFieldType {
3993    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3994        formatter.write_str(&self.0)
3995    }
3996}
3997
3998/// One projected metadata text field's plan, or its verified outcome.
3999///
4000/// The setup creates a missing one as a text field and never alters a field that is there:
4001/// a same-named field of another type is reported in `conflict` and left as it is.
4002#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
4003pub struct MetadataFieldReport {
4004    /// The board field's name.
4005    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.
4006    /// The metadata key the value is read from.
4007    pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
4008    /// The object keys walked inside that key's value; empty for the key's own value.
4009    pub path: Vec<String>,
4010    /// Whether the board had a text field of that name before the operation.
4011    // 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.
4012    pub exists: bool,
4013    /// The type of a field of that name that is not a text field, which the setup leaves as
4014    /// it is; absent — and left out of the JSON — when there is none.
4015    #[serde(default, skip_serializing_if = "Option::is_none")]
4016    pub conflict: Option<NonTextFieldType>,
4017    /// What the requested operation did: `planned`; `created` for a field that was not
4018    /// there; `unchanged` for one that was, and for a conflict, which is never changed.
4019    pub outcome: FieldOutcome,
4020}
4021
4022/// Which options one field is configured with, in the order a new field would list them.
4023struct FieldPlan {
4024    field: BoardField,
4025    wanted: Vec<String>,
4026}
4027
4028/// One single-select field as the guarded setup snapshots it.
4029#[derive(Debug, Clone, PartialEq, Eq)]
4030struct SnapshotField {
4031    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4032    // passed back as the mutation's field identity; a newtype could enforce no stronger
4033    // invariant because GitHub publishes no grammar for it.
4034    field_id: String,
4035    options: Vec<StatusOption>,
4036}
4037
4038/// Every single-select field of a board and every item's value of each.
4039#[derive(Debug, Clone, PartialEq, Eq)]
4040struct BoardSnapshot {
4041    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
4042    // passed back as the mutation's project identity; a newtype could enforce no stronger
4043    // invariant because GitHub publishes no grammar for it.
4044    board_id: String,
4045    fields: BTreeMap<BoardField, SnapshotField>,
4046    /// Each board item's id, and its value of each field this setup owns that it holds one of.
4047    items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
4048}
4049
4050impl BoardSnapshot {
4051    /// Every item's value of `field`, in board order — the recovery data a drift refusal
4052    /// carries.
4053    fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
4054        self.items
4055            .iter()
4056            .map(|(item_id, values)| StatusAssignment {
4057                item_id: item_id.clone(),
4058                option: values.get(&field).cloned(),
4059            })
4060            .collect()
4061    }
4062}
4063
4064impl GitHubProjectsSource {
4065    /// Report missing configured Status options and, when `apply` is true, add them with
4066    /// a whole-list mutation that preserves every existing id and verifies the result.
4067    ///
4068    /// # Errors
4069    ///
4070    /// Refuses a board without a single-select `Status` field. A post-write difference in
4071    /// any pre-existing option id or item assignment is refused with the complete pre-write
4072    /// assignment snapshot in the diagnostic for recovery.
4073    // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
4074    // successful mutation, both drift refusals, source selection, missing Status, casing,
4075    // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
4076    // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
4077    // responses from entering the defensive malformed-response branches below.
4078    pub async fn status_options(
4079        &self,
4080        mode: StatusOptionsMode,
4081    ) -> Result<StatusOptionsReport, SourceError> {
4082        let before = self.status_snapshot().await?;
4083        // A terminal category's option is as configured as an open one's: a terminal
4084        // write validates it before closing and refuses when the board lacks it. Both
4085        // kinds' names are options of the one field, so both are asked for.
4086        let missing = self
4087            .statuses
4088            .wanted()
4089            .into_iter()
4090            .filter(|wanted| {
4091                !before
4092                    .options
4093                    .iter()
4094                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4095            })
4096            .collect::<Vec<_>>();
4097        let report = StatusOptionsReport {
4098            source: self.name.clone(),
4099            missing: missing.clone(),
4100            outcome: match (mode, missing.is_empty()) {
4101                (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
4102                (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
4103                (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
4104            },
4105            existing: before.options.clone(),
4106        };
4107        if mode == StatusOptionsMode::Plan || missing.is_empty() {
4108            return Ok(report);
4109        }
4110        let mut options = before
4111            .options
4112            .iter()
4113            .map(|option| {
4114                json!({
4115                    "id": option.id, "name": option.name, "color": option.color,
4116                    "description": option.description,
4117                })
4118            })
4119            .collect::<Vec<_>>();
4120        options.extend(missing.iter().map(|name| {
4121            json!({
4122                "name": name, "color": "GRAY", "description": ""
4123            })
4124        }));
4125        self.graphql(
4126            graphql::STATUS_OPTIONS_UPDATE,
4127            json!({"input": {
4128                "projectId": before.board_id, "fieldId": before.field_id,
4129                "singleSelectOptions": options,
4130            }}),
4131        )
4132        .await?;
4133        let after = self.status_snapshot().await?;
4134        let options_preserved = before
4135            .options
4136            .iter()
4137            .all(|old| after.options.iter().any(|new| new == old));
4138        let additions_present = missing.iter().all(|wanted| {
4139            after
4140                .options
4141                .iter()
4142                .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4143        });
4144        if !options_preserved || !additions_present || after.assignments != before.assignments {
4145            let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
4146                SourceError::Malformed {
4147                    message: format!("cannot render pre-write Status recovery snapshot: {error}"),
4148                }
4149            })?;
4150            return Err(SourceError::Refused {
4151                message: format!(
4152                    "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}"
4153                ),
4154            });
4155        }
4156        Ok(report)
4157    }
4158
4159    /// A fresh snapshot of the Status field and every board item's assignment of it.
4160    ///
4161    /// # Errors
4162    ///
4163    /// Refuses a board without a single-select `Status` field, and one the token cannot see.
4164    async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
4165        // Status alone, as this operation has always read it: a `Priority` field is another
4166        // operation's, so nothing about it can refuse this one.
4167        let mut board = self.board_snapshot(&[BoardField::Status]).await?;
4168        let field = board
4169            .fields
4170            .remove(&BoardField::Status)
4171            .ok_or_else(|| self.no_status_field())?;
4172        Ok(StatusSnapshot {
4173            assignments: board.assignments(BoardField::Status),
4174            board_id: board.board_id,
4175            field_id: field.field_id,
4176            options: field.options,
4177        })
4178    }
4179
4180    /// The refusal a board with no `Status` field is answered with by the guarded setup.
4181    fn no_status_field(&self) -> SourceError {
4182        SourceError::Refused {
4183            message: format!("source {} board has no Status field", self.name),
4184        }
4185    }
4186
4187    // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
4188    // the real CLI loopback journey, including pagination. The individual malformed guards
4189    // are defensive validation of a schema-pinned third-party response, not separate user
4190    // journeys; drift and missing-field failures cover the operation's recovery behavior.
4191    /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
4192    /// every board item's value of each, walked to the end of the board's items. A field not
4193    /// in `owned` is read past whatever it holds.
4194    async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
4195        let mut after: Option<String> = None;
4196        let mut snapshot: Option<BoardSnapshot> = None;
4197        loop {
4198            let data = self
4199                .graphql(
4200                    graphql::STATUS_OPTIONS_SNAPSHOT,
4201                    json!({
4202                        "owner": self.owner, "number": self.project_number,
4203                        "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
4204                    }),
4205                )
4206                .await?;
4207            let board = data
4208                .pointer("/owner/projectV2")
4209                .filter(|board| board.is_object())
4210                .ok_or_else(|| SourceError::Refused {
4211                    message: format!(
4212                        "source {} has no accessible GitHub Projects board",
4213                        self.name
4214                    ),
4215                })?;
4216            if board
4217                .pointer("/fields/pageInfo/hasNextPage")
4218                .and_then(Value::as_bool)
4219                != Some(false)
4220            {
4221                return Err(SourceError::Malformed {
4222                    message:
4223                        "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
4224                            .into(),
4225                });
4226            }
4227            let mut fields = BTreeMap::new();
4228            // Only the fields this setup owns, by name: a node the single-select fragment did not
4229            // match carries no name, and a person's own single-select field — a `Size`, a
4230            // `Team` — is none of this setup's business, so nothing about it can refuse one. A
4231            // `Status` or `Priority` field without its options is malformed, not absent.
4232            // 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.
4233            for (owned, field) in board
4234                .pointer("/fields/nodes")
4235                .and_then(Value::as_array)
4236                .ok_or_else(|| SourceError::Malformed {
4237                    message: "GitHub project fields.nodes is not an array".into(),
4238                })?
4239                .iter()
4240                .filter_map(|field| {
4241                    let named = BoardField::named(field.get("name")?.as_str()?)?;
4242                    owned.contains(&named).then_some((named, field))
4243                })
4244            {
4245                let options = field
4246                    .get("options")
4247                    .and_then(Value::as_array)
4248                    .ok_or_else(|| SourceError::Malformed {
4249                        message: "GitHub single-select field options is not an array".into(),
4250                    })?
4251                    .iter()
4252                    .map(|option| {
4253                        Ok(StatusOption {
4254                            id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
4255                                .map_err(|message| SourceError::Malformed { message })?,
4256                            name: ColumnName::try_from(required_str(option, "name")?.to_owned())
4257                                .map_err(|message| SourceError::Malformed {
4258                                    message: format!(
4259                                        "GitHub single-select option name is invalid: {message}"
4260                                    ),
4261                                })?,
4262                            color: serde_json::from_value(
4263                                option.get("color").cloned().unwrap_or(Value::Null),
4264                            )
4265                            .map_err(|error| {
4266                                SourceError::Malformed {
4267                                    message: format!(
4268                                        "GitHub single-select option color is invalid: {error}"
4269                                    ),
4270                                }
4271                            })?,
4272                            description: optional_str(option, "description")?
4273                                .unwrap_or_default()
4274                                .to_owned(),
4275                        })
4276                    })
4277                    .collect::<Result<Vec<_>, SourceError>>()?;
4278                let snapshot = SnapshotField {
4279                    field_id: required_nonblank_str(field, "id")?.to_owned(),
4280                    options,
4281                };
4282                // A board's field names are unique, so a second one is an answer that cannot
4283                // say which field the setup would act on — refused rather than one chosen.
4284                if fields.insert(owned, snapshot).is_some() {
4285                    return Err(SourceError::Malformed {
4286                        message: format!(
4287                            "GitHub answered two {} fields for this board",
4288                            owned.name()
4289                        ),
4290                    });
4291                }
4292            }
4293            let board_id = required_nonblank_str(board, "id")?.to_owned();
4294            let current = snapshot.get_or_insert_with(|| BoardSnapshot {
4295                board_id,
4296                fields,
4297                items: Vec::new(),
4298            });
4299            let items = board
4300                .pointer("/items/nodes")
4301                .and_then(Value::as_array)
4302                .ok_or_else(|| SourceError::Malformed {
4303                    message: "GitHub project items.nodes is not an array".into(),
4304                })?;
4305            for item in items {
4306                let field_values =
4307                    item.get("fieldValues")
4308                        .ok_or_else(|| SourceError::Malformed {
4309                            message: "GitHub project item is missing fieldValues".into(),
4310                        })?;
4311                if field_values
4312                    .pointer("/pageInfo/hasNextPage")
4313                    .and_then(Value::as_bool)
4314                    != Some(false)
4315                {
4316                    return Err(SourceError::Malformed {
4317                        message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
4318                    });
4319                }
4320                let values = item
4321                    .pointer("/fieldValues/nodes")
4322                    .and_then(Value::as_array)
4323                    .ok_or_else(|| SourceError::Malformed {
4324                        message: "GitHub project item fieldValues.nodes is not an array".into(),
4325                    })?;
4326                let item_id = required_nonblank_str(item, "id")?;
4327                let mut assigned = BTreeMap::new();
4328                for value in values {
4329                    let Some(field) = value
4330                        .pointer("/field/name")
4331                        .and_then(Value::as_str)
4332                        .and_then(BoardField::named)
4333                        .filter(|field| owned.contains(field))
4334                    else {
4335                        continue;
4336                    };
4337                    let held = assigned.insert(
4338                        field,
4339                        AssignedStatusOption {
4340                            id: StatusOptionId::try_from(
4341                                required_str(value, "optionId")?.to_owned(),
4342                            )
4343                            .map_err(|message| SourceError::Malformed { message })?,
4344                            name: ColumnName::try_from(required_str(value, "name")?.to_owned())
4345                                .map_err(|message| SourceError::Malformed {
4346                                    message: format!(
4347                                        "GitHub assigned {} name is invalid: {message}",
4348                                        field.name()
4349                                    ),
4350                                })?,
4351                        },
4352                    );
4353                    // An item holds one value of a field, so a second one leaves no way to
4354                    // tell which it holds — and a verification or recovery built on either
4355                    // could restore the wrong one.
4356                    if held.is_some() {
4357                        return Err(SourceError::Malformed {
4358                            message: format!(
4359                                "GitHub answered two {} values for board item {item_id}",
4360                                field.name()
4361                            ),
4362                        });
4363                    }
4364                }
4365                current.items.push((item_id.to_owned(), assigned));
4366            }
4367            let page = board.get("items").ok_or_else(|| SourceError::Malformed {
4368                message: "GitHub project is missing items".into(),
4369            })?;
4370            let has_next = page
4371                .pointer("/pageInfo/hasNextPage")
4372                .and_then(Value::as_bool)
4373                .ok_or_else(|| SourceError::Malformed {
4374                    message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
4375                })?;
4376            if !has_next {
4377                break;
4378            }
4379            let next =
4380                required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
4381            validate_cursor_progress(after.as_deref(), next)?;
4382            after = Some(next.to_owned());
4383        }
4384        snapshot.ok_or_else(|| SourceError::Malformed {
4385            message: "GitHub returned no board field snapshot".into(),
4386        })
4387    }
4388    // llmlint: ignore-end[changed_behavior_has_e2e]
4389
4390    /// Report every board field this source's configuration names and, with
4391    /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
4392    /// the `Priority` field when the board has none.
4393    ///
4394    /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
4395    /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
4396    /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
4397    /// color and description: the whole option list goes back with every existing id, because
4398    /// a re-minted id clears every item's value.
4399    ///
4400    /// # Errors
4401    ///
4402    /// Refuses a board without a single-select `Status` field. After an apply the board is
4403    /// read again, and a pre-existing option or any item's value of either field that moved is
4404    /// refused with the complete pre-write assignments in the diagnostic, for recovery.
4405    // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
4406    // unchanged apply, a created field, an added option to each field, drift refusal, a board
4407    // with no Status field and a non-github-projects source through the compiled CLI against
4408    // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
4409    pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
4410        let owned: Vec<BoardField> = if self.priorities.is_some() {
4411            vec![BoardField::Status, BoardField::Priority]
4412        } else {
4413            vec![BoardField::Status]
4414        };
4415        let before = self.board_snapshot(&owned).await?;
4416        let mut plans = vec![FieldPlan {
4417            field: BoardField::Status,
4418            wanted: self.statuses.wanted(),
4419        }];
4420        if !before.fields.contains_key(&BoardField::Status) {
4421            return Err(self.no_status_field());
4422        }
4423        if let Some(mapping) = &self.priorities {
4424            plans.push(FieldPlan {
4425                field: BoardField::Priority,
4426                wanted: mapping.names().map(str::to_owned).collect(),
4427            });
4428        }
4429        // The snapshot reads single-select fields alone, so a field it did not find may still
4430        // be on the board under the name, of another type: creating one beside it would fail
4431        // part way, or leave two fields of one name. Asked of the board's own field list, and
4432        // only when a field is missing.
4433        if plans
4434            .iter()
4435            .any(|plan| !before.fields.contains_key(&plan.field))
4436        {
4437            let board = self.board_fields().await?;
4438            for plan in plans
4439                .iter()
4440                .filter(|plan| !before.fields.contains_key(&plan.field))
4441            {
4442                if let Some(field) = Board::field(&board.fields, plan.field.name())? {
4443                    return Err(SourceError::Refused {
4444                        message: format!(
4445                            "source {}'s board has a {} field that is not a single-select field \
4446                             (it is a {}), so it cannot hold this source's options; next: rename \
4447                             or remove that field, then run this again",
4448                            self.name,
4449                            plan.field.name(),
4450                            optional_str(field, "__typename")?.unwrap_or("field of another type")
4451                        ),
4452                    });
4453                }
4454            }
4455        }
4456        let mut reports = Vec::new();
4457        for plan in &plans {
4458            let held = before.fields.get(&plan.field);
4459            let existing = held.map(|field| field.options.clone()).unwrap_or_default();
4460            let mut missing: Vec<String> = Vec::new();
4461            for wanted in &plan.wanted {
4462                let present = existing
4463                    .iter()
4464                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4465                    || missing
4466                        .iter()
4467                        .any(|named| named.eq_ignore_ascii_case(wanted));
4468                if !present {
4469                    missing.push(wanted.clone());
4470                }
4471            }
4472            let kinds = match plan.field {
4473                BoardField::Status => self.statuses.missing_by_kind(&existing),
4474                BoardField::Priority => Vec::new(),
4475            };
4476            reports.push(FieldReport {
4477                field: plan.field,
4478                exists: held.is_some(),
4479                kinds,
4480                outcome: match (mode, held.is_some(), missing.is_empty()) {
4481                    (SetupMode::Plan, _, _) => FieldOutcome::Planned,
4482                    (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
4483                    (SetupMode::Apply, true, false) => FieldOutcome::Applied,
4484                    (SetupMode::Apply, false, _) => FieldOutcome::Created,
4485                },
4486                missing,
4487                existing,
4488            });
4489        }
4490        // A projected field is a text field, which the single-select snapshot cannot see, so
4491        // it is asked of the board's own field list — and only when one is configured.
4492        let mut metadata_fields = Vec::new();
4493        if !self.metadata_fields.is_empty() {
4494            let board = self.board_fields().await?;
4495            for projection in &self.metadata_fields {
4496                let conflict = match Board::field(&board.fields, &projection.field)? {
4497                    None => None,
4498                    Some(field) => {
4499                        let typename = optional_str(field, "__typename")?;
4500                        let data_type = optional_str(field, "dataType")?;
4501                        if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
4502                            None
4503                        } else {
4504                            let named = data_type
4505                                .filter(|kind| *kind != "TEXT")
4506                                .or(typename)
4507                                .unwrap_or("field of another type");
4508                            Some(NonTextFieldType::try_from(named.to_owned()).map_err(
4509                                |message| SourceError::Malformed {
4510                                    message: format!(
4511                                        "GitHub answered the board's {} field with a type this \
4512                                         setup cannot read: {message}",
4513                                        &*projection.field
4514                                    ),
4515                                },
4516                            )?)
4517                        }
4518                    }
4519                };
4520                let exists =
4521                    conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
4522                metadata_fields.push(MetadataFieldReport {
4523                    field: projection.field.to_string(),
4524                    key: projection.key.to_string(),
4525                    path: projection.path.clone(),
4526                    exists,
4527                    outcome: match (mode, exists || conflict.is_some()) {
4528                        (SetupMode::Plan, _) => FieldOutcome::Planned,
4529                        (SetupMode::Apply, true) => FieldOutcome::Unchanged,
4530                        (SetupMode::Apply, false) => FieldOutcome::Created,
4531                    },
4532                    conflict,
4533                });
4534            }
4535        }
4536        let report = FieldsReport {
4537            source: self.name.clone(),
4538            fields: reports,
4539            metadata_fields,
4540        };
4541        let writes: Vec<&FieldReport> = report
4542            .fields
4543            .iter()
4544            .filter(|field| !field.missing.is_empty() || !field.exists)
4545            .collect();
4546        let creates: Vec<&MetadataFieldReport> = report
4547            .metadata_fields
4548            .iter()
4549            .filter(|field| field.outcome == FieldOutcome::Created)
4550            .collect();
4551        if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
4552            return Ok(report);
4553        }
4554        let mut landed: Vec<&str> = Vec::new();
4555        for field in &writes {
4556            let added = field
4557                .missing
4558                .iter()
4559                .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
4560            let sent = match before.fields.get(&field.field) {
4561                Some(held) => {
4562                    let mut options = held
4563                        .options
4564                        .iter()
4565                        .map(|option| {
4566                            json!({
4567                                "id": option.id, "name": option.name, "color": option.color,
4568                                "description": option.description,
4569                            })
4570                        })
4571                        .collect::<Vec<_>>();
4572                    options.extend(added);
4573                    self.graphql(
4574                        graphql::STATUS_OPTIONS_UPDATE,
4575                        json!({"input": {
4576                            "projectId": before.board_id, "fieldId": held.field_id,
4577                            "singleSelectOptions": options,
4578                        }}),
4579                    )
4580                    .await
4581                }
4582                None => {
4583                    self.graphql(
4584                        graphql::CREATE_FIELD,
4585                        json!({"input": {
4586                            "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4587                            "name": field.field.name(),
4588                            "singleSelectOptions": added.collect::<Vec<_>>(),
4589                        }}),
4590                    )
4591                    .await
4592                }
4593            };
4594            // A mutation that failed does not establish that GitHub left its field as it was,
4595            // so every failure from here on carries the recovery data a drift refusal does.
4596            match sent {
4597                Ok(_) => landed.push(field.field.name()),
4598                Err(error) => {
4599                    let changed = if landed.is_empty() {
4600                        String::new()
4601                    } else {
4602                        format!("changed the {} field and then ", landed.join(" and "))
4603                    };
4604                    return Err(SourceError::Refused {
4605                        message: format!(
4606                            "the guarded field setup {changed}failed on the {} field, which it may \
4607                             have changed part way: {error}; the pre-write item assignments \
4608                             are:\n{}",
4609                            field.field.name(),
4610                            recovery(&report, &before)?
4611                        ),
4612                    });
4613                }
4614            }
4615        }
4616        for field in &creates {
4617            let sent = self
4618                .graphql(
4619                    graphql::CREATE_FIELD,
4620                    json!({"input": {
4621                        "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4622                    }}),
4623                )
4624                .await;
4625            if let Err(error) = sent {
4626                let changed = if landed.is_empty() {
4627                    String::new()
4628                } else {
4629                    format!("changed the {} field and then ", landed.join(" and "))
4630                };
4631                return Err(SourceError::Refused {
4632                    message: format!(
4633                        "the guarded field setup {changed}failed creating the {} text field: \
4634                         {error}; run it again to create what is still missing",
4635                        field.field
4636                    ),
4637                });
4638            }
4639            landed.push(&field.field);
4640        }
4641        // The board has been written, so a verification read that fails leaves it unverified
4642        // rather than unchanged, and says what to put back.
4643        let after = match self.board_snapshot(&owned).await {
4644            Ok(after) => after,
4645            Err(error) => {
4646                return Err(SourceError::Refused {
4647                    message: format!(
4648                        "the guarded field setup changed the {} field and then could not read the \
4649                         board back to verify it: {error}; the pre-write item assignments are:\n{}",
4650                        landed.join(" and "),
4651                        recovery(&report, &before)?
4652                    ),
4653                });
4654            }
4655        };
4656        let mut moved = Vec::new();
4657        for field in &report.fields {
4658            let name = field.field.name();
4659            let now = after
4660                .fields
4661                .get(&field.field)
4662                .map(|held| held.options.as_slice())
4663                .unwrap_or_default();
4664            if !field.existing.iter().all(|old| now.contains(old)) {
4665                moved.push(format!(
4666                    "a pre-existing {name} option id, name, color or description"
4667                ));
4668            }
4669            if !field.missing.iter().all(|wanted| {
4670                now.iter()
4671                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4672            }) {
4673                moved.push(format!("an added {name} option"));
4674            }
4675            if after.assignments(field.field) != before.assignments(field.field) {
4676                moved.push(format!("an item's {name} value"));
4677            }
4678        }
4679        if !creates.is_empty() {
4680            // Read afresh: the view held for this command is the one from before the creates.
4681            *self.fields_cache()? = None;
4682            let board = match self.board_fields().await {
4683                Ok(board) => board,
4684                Err(error) => {
4685                    return Err(SourceError::Refused {
4686                        message: format!(
4687                            "the guarded field setup changed the {} field and then could not \
4688                             read the board back to verify it: {error}; run it again to verify \
4689                             it, which creates nothing a second time",
4690                            landed.join(" and ")
4691                        ),
4692                    });
4693                }
4694            };
4695            for field in &creates {
4696                let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
4697                    held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
4698                        && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
4699                });
4700                if !text {
4701                    moved.push(format!("the created {} text field", field.field));
4702                }
4703            }
4704        }
4705        if !moved.is_empty() {
4706            return Err(SourceError::Refused {
4707                message: format!(
4708                    "GitHub changed {} after the guarded field setup; the pre-write item \
4709                     assignments are:\n{}",
4710                    moved.join(", "),
4711                    recovery(&report, &before)?
4712                ),
4713            });
4714        }
4715        Ok(report)
4716    }
4717
4718    /// Validate configuration and capture the named credential without exposing it.
4719    ///
4720    /// # Errors
4721    ///
4722    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
4723    /// [`SourceError::Auth`] when the named credential is missing or empty.
4724    pub fn new(
4725        name: &SourceName,
4726        config: GitHubProjectsConfig,
4727        secrets: &dyn SecretResolver,
4728    ) -> Result<Self, SourceError> {
4729        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
4730    }
4731
4732    /// The same, recording every request it sends into an accounting the caller holds too.
4733    ///
4734    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
4735    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
4736    /// up — passes the one it records those into, so the session total accounts for the
4737    /// whole session rather than for this source's share of it.
4738    ///
4739    /// # Errors
4740    ///
4741    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
4742    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
4743    pub fn recording_into(
4744        name: &SourceName,
4745        config: GitHubProjectsConfig,
4746        secrets: &dyn SecretResolver,
4747        ledger: Arc<Accounting>,
4748    ) -> Result<Self, SourceError> {
4749        if !valid_github_owner(&config.owner) {
4750            return Err(SourceError::Config {
4751                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
4752            });
4753        }
4754        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
4755            return Err(SourceError::Config {
4756                message: format!("project_number must be between 1 and {}", i32::MAX),
4757            });
4758        }
4759        if !valid_environment_name(&config.token_env) {
4760            return Err(SourceError::Config {
4761                message: "token_env must be a valid environment-variable name".into(),
4762            });
4763        }
4764        let repository = config
4765            .repository
4766            .as_deref()
4767            .map(RepositoryTarget::parse)
4768            .transpose()?;
4769        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
4770            message: format!("endpoint is not a valid URL: {e}"),
4771        })?;
4772        if endpoint.scheme() != "https"
4773            && !(endpoint.scheme() == "http"
4774                && endpoint
4775                    .host_str()
4776                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
4777        {
4778            return Err(SourceError::Config {
4779                message:
4780                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
4781                        .into(),
4782            });
4783        }
4784        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
4785            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),
4786        })?;
4787        Ok(Self {
4788            name: name.clone(),
4789            owner: config.owner,
4790            project_number: config.project_number,
4791            repository,
4792            asset_client: assets::client(&endpoint)?,
4793            endpoint,
4794            token,
4795            credential_name: config.token_env,
4796            statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
4797            priorities: config
4798                .priority_mapping
4799                .map(|mapping| PriorityMapping::resolve(mapping, name))
4800                .transpose()?,
4801            metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
4802            client: Client::builder()
4803                .user_agent("onetaskgraph")
4804                .build()
4805                .map_err(|e| SourceError::Config {
4806                    message: format!("cannot build HTTP client: {e}"),
4807                })?,
4808            created: Mutex::new(Vec::new()),
4809            updated: Mutex::new(Vec::new()),
4810            commented: Mutex::new(Vec::new()),
4811            pacing: Pacing::resolve(config.pacing, name)?,
4812            last_mutation: Mutex::new(None),
4813            windows: Mutex::new(pacing::Windows::default()),
4814            task_children: Mutex::new(BTreeMap::new()),
4815            written: Mutex::new(BTreeMap::new()),
4816            deferred: Mutex::new(None),
4817            clock: system_clock(),
4818            numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
4819            board_cache: Mutex::new(None),
4820            search_cache: Mutex::new(None),
4821            narrowed_cache: Mutex::new(BTreeMap::new()),
4822            resolved_cache: Mutex::new(BTreeMap::new()),
4823            read_ahead: Mutex::new(BTreeMap::new()),
4824            carried: Mutex::new(BTreeMap::new()),
4825            held_cache: Mutex::new(BTreeMap::new()),
4826            children_cache: Mutex::new(BTreeMap::new()),
4827            search_next: Mutex::new(BTreeMap::new()),
4828            fields_cache: Mutex::new(None),
4829            repository_cache: Mutex::new(BTreeMap::new()),
4830            ledger,
4831            expected: expected::Held::default(),
4832        })
4833    }
4834
4835    /// A snapshot of every request this source has sent, and what each cost.
4836    ///
4837    /// A value to hold and compare rather than a borrow of the accounting itself, so two
4838    /// of them can sit side by side. When this source was built with
4839    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
4840    /// point of building it that way.
4841    #[must_use]
4842    pub fn accounting(&self) -> accounting::Session {
4843        self.ledger.snapshot()
4844    }
4845
4846    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
4847    /// rate limit rather than handing it straight back as an error.
4848    ///
4849    /// Retrying is safe for every document here, including the mutations, and the reason
4850    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
4851    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
4852    /// this replays has already taken effect. An outcome this source cannot know — the
4853    /// send failed, or the body could not be read, so the mutation may well have landed —
4854    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
4855    /// attempt. A duplicate write would come from replaying one of those, and none is
4856    /// replayed.
4857    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4858        if !self.expected.journaling() {
4859            return self.graphql_sent(query, variables).await;
4860        }
4861        // While a write the store's cache expects runs, its mutations select the whole issue
4862        // they leave behind, so the response vouches for what the cache keeps — and every
4863        // request is recorded, so the write's parts are reported from their own responses.
4864        let (query, variables) = match graphql::whole_twin(query) {
4865            Some(twin) => (twin, expected::whole_variables(variables)),
4866            None => (query, variables),
4867        };
4868        let sent = self.graphql_sent(query, variables.clone()).await;
4869        self.journal(query, &variables, &sent).await;
4870        sent
4871    }
4872
4873    /// One document sent, retried while GitHub asks for a wait, exactly as asked.
4874    async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4875        if is_mutation(query)
4876            && ![
4877                graphql::ADD_COMMENT,
4878                graphql::UPDATE_COMMENT,
4879                graphql::DELETE_COMMENT,
4880                graphql::ADD_COMMENT_WHOLE,
4881                graphql::UPDATE_COMMENT_WHOLE,
4882            ]
4883            .contains(&query)
4884        {
4885            let mut cache = self.resolved_cache()?;
4886            let mut ahead = self.read_ahead()?;
4887            let mut carried = self.carried()?;
4888            for argument in ["input", "second", "third", "clear"] {
4889                if let Some(input) = variables.get(argument) {
4890                    let names = |id: &NativeId, item_id: Option<&str>| {
4891                        ["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
4892                            input
4893                                .get(key)
4894                                .and_then(Value::as_str)
4895                                .is_some_and(|value| value == id.0 || Some(value) == item_id)
4896                        })
4897                    };
4898                    cache.retain(|id, item| !names(id, Some(&item.item_id)));
4899                    ahead.retain(|id, item| {
4900                        !names(id, item.as_ref().map(|item| item.item_id.as_str()))
4901                    });
4902                    carried.retain(|id, item| !names(id, Some(&item.item_id)));
4903                }
4904            }
4905        }
4906        let doing = operation_description(query);
4907        let mutation = is_mutation(query);
4908        let charge = pacing::Charge::graphql(query, &variables);
4909        let mut waited = Duration::ZERO;
4910        let mut waits = 0_u32;
4911        let mut backoff = self.pacing.retry_backoff;
4912        loop {
4913            let ticket = self.pace(charge, mutation).await;
4914            let attempt = self.send_once(query, &variables, waits + 1).await;
4915            self.paced(ticket, mutation);
4916            let limited = match attempt {
4917                Ok(data) => return Ok(data),
4918                Err(Attempt::Failed(error)) => return Err(error),
4919                Err(Attempt::Limited(limited)) => limited,
4920            };
4921            let (wait, cause) = limited.wait(backoff);
4922            let remaining = self.pacing.retry_budget.saturating_sub(waited);
4923            // A wait of nothing spends none of the budget, so it is exhaustion rather
4924            // than a retry. `Pacing::resolve` rules out every way of configuring one
4925            // except a budget of zero, where reporting the first refusal is the ask.
4926            if wait.is_zero() || wait > remaining {
4927                return Err(limited.exhausted(
4928                    doing,
4929                    waits,
4930                    waited,
4931                    wait,
4932                    self.pacing.retry_budget,
4933                ));
4934            }
4935            self.clock.sleep_because(wait, cause).await;
4936            waited += wait;
4937            waits += 1;
4938            backoff = backoff.saturating_mul(2);
4939        }
4940    }
4941
4942    /// Hold one request charging `charge` until every secondary window it charges has room for
4943    /// the whole of it — and, for a mutation of an instance configured with
4944    /// `pacing.min_mutation_interval_ms`, until that interval has passed since the last one
4945    /// finished — then count it, answering the ticket [`Self::paced`] finishes it with.
4946    ///
4947    /// The moment is decided and counted under the lock and waited out outside it, so two
4948    /// callers take two places rather than the same one, and no lock is held across an await.
4949    /// A request with room goes out at once: nothing here waits that a window does not ask for.
4950    async fn pace(&self, charge: pacing::Charge, mutation: bool) -> pacing::Ticket {
4951        let (wait, cause, ticket) = {
4952            // A poisoned lock here costs pacing, not correctness, and refusing the write over
4953            // it would turn an earlier failure into a second one for no gain.
4954            let mut windows = self
4955                .windows
4956                .lock()
4957                .unwrap_or_else(std::sync::PoisonError::into_inner);
4958            let now = self.clock.now();
4959            let (mut at, mut cause) = if self.pacing.windows {
4960                windows.room(now, charge)
4961            } else {
4962                (now, None)
4963            };
4964            if mutation && !self.pacing.min_mutation_interval.is_zero() {
4965                let mut last = self
4966                    .last_mutation
4967                    .lock()
4968                    .unwrap_or_else(std::sync::PoisonError::into_inner);
4969                // `checked_add` rather than `+`: adding durations can panic on overflow, and
4970                // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
4971                if let Some(earliest) = last
4972                    .and_then(|previous| previous.checked_add(self.pacing.min_mutation_interval))
4973                    && earliest > at
4974                {
4975                    at = earliest;
4976                    cause = Some(pacing::cause::MIN_INTERVAL);
4977                }
4978                *last = Some(at);
4979            }
4980            let ticket = windows.charge(at, charge);
4981            (at.saturating_sub(now), cause, ticket)
4982        };
4983        if let (false, Some(cause)) = (wait.is_zero(), cause) {
4984            self.clock.sleep_because(wait, cause).await;
4985        }
4986        ticket
4987    }
4988
4989    /// Record that the request `ticket` was charged for has finished, so each window — and
4990    /// the configured spacing — counts it from here rather than from the moment it left.
4991    ///
4992    /// This source can only choose when a request *departs*; GitHub's limiter counts when it
4993    /// *arrives*, and the two differ by whatever the request spent in transit. Counting from
4994    /// departure would hand the limiter a window that looked roomier than it is, so a source
4995    /// pacing exactly at a limit could still be seen arriving over it — which is how a copy
4996    /// paced well inside a board's threshold was once refused by it on a slower machine while
4997    /// passing on a quick one. The previous request had arrived before its response came back,
4998    /// so counting from here is never earlier than the limiter does, whatever transit costs
4999    /// and on whatever platform: the safe side of a limit that punishes being wrong by refusing
5000    /// reads for the next fifty minutes.
5001    ///
5002    /// A failed attempt is finished too: a request refused by the limiter still arrived, and
5003    /// one that never left costs only room nobody needed.
5004    fn paced(&self, ticket: pacing::Ticket, mutation: bool) {
5005        let now = self.clock.now();
5006        self.windows
5007            .lock()
5008            .unwrap_or_else(std::sync::PoisonError::into_inner)
5009            .finish(ticket, now);
5010        if mutation && !self.pacing.min_mutation_interval.is_zero() {
5011            let mut last = self
5012                .last_mutation
5013                .lock()
5014                .unwrap_or_else(std::sync::PoisonError::into_inner);
5015            // `max` rather than an assignment: a concurrent caller may already have reserved a
5016            // slot further out, and completing this request must never pull that slot back in.
5017            *last = Some(last.map_or(now, |reserved| reserved.max(now)));
5018        }
5019    }
5020
5021    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
5022    /// failure that waiting cannot help — and recorded, whichever of the three it was.
5023    ///
5024    /// This is the one place a request leaves this crate, which is why the accounting is
5025    /// here rather than at each of the callers: a read path added later is counted without
5026    /// anybody remembering to count it, and
5027    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
5028    /// when one is not.
5029    async fn send_once(
5030        &self,
5031        query: &str,
5032        variables: &Value,
5033        number: u32,
5034    ) -> Result<Value, Attempt> {
5035        let executed = self.ledger.priced_document(query);
5036        let Attempted {
5037            result,
5038            limits,
5039            reported_cost,
5040            response_outcome,
5041        } = self.attempt(executed.as_ref(), variables).await;
5042        // No `otherwise` name: every document this source sends is one of its own, and the
5043        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
5044        let sending = accounting::Request::graphql(query, variables, None, reported_cost);
5045        let outcome = match &result {
5046            Ok(_) => accounting::Outcome::Answered,
5047            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
5048            Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
5049        };
5050        self.ledger
5051            .record(sending.finished(outcome, limits).with_execution(
5052                Some(executed.into_owned()),
5053                response_outcome,
5054                number,
5055            ));
5056        result
5057    }
5058
5059    /// The attempt itself, with what its response said about the rate limit alongside.
5060    ///
5061    /// The two are returned together rather than recorded here because every one of the
5062    /// early exits below is a different outcome, and a record written at each of them is a
5063    /// record one of them can be added without.
5064    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
5065        let mut limits = accounting::RateLimit::default();
5066        let mut reported_cost = None;
5067        let mut response_outcome = accounting::ResponseOutcome::TransportError;
5068        let result = self
5069            .attempted(
5070                query,
5071                variables,
5072                &mut limits,
5073                &mut reported_cost,
5074                &mut response_outcome,
5075            )
5076            .await;
5077        Attempted {
5078            result,
5079            limits,
5080            reported_cost,
5081            response_outcome,
5082        }
5083    }
5084
5085    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
5086    async fn attempted(
5087        &self,
5088        query: &str,
5089        variables: &Value,
5090        limits: &mut accounting::RateLimit,
5091        reported_cost: &mut Option<u64>,
5092        response_outcome: &mut accounting::ResponseOutcome,
5093    ) -> Result<Value, Attempt> {
5094        let response = self
5095            .client
5096            .post(self.endpoint.clone())
5097            .bearer_auth(self.token.expose_secret())
5098            .json(&json!({"query": query, "variables": variables}))
5099            .send()
5100            .await
5101            .map_err(|e| {
5102                Attempt::Failed(SourceError::Unavailable {
5103                    message: format!("GitHub GraphQL request failed: {e}"),
5104                })
5105            })?;
5106        let status = response.status();
5107        if !status.is_success() {
5108            *response_outcome = accounting::ResponseOutcome::HttpError;
5109        }
5110        let header = |name: &str| whole_seconds(response.headers().get(name));
5111        let served = response
5112            .headers()
5113            .get(reqwest::header::DATE)
5114            .and_then(|value| value.to_str().ok())
5115            .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
5116            .map(|at| at.with_timezone(&Utc));
5117        *limits = accounting::RateLimit::read(|name| {
5118            response
5119                .headers()
5120                .get(name)
5121                .and_then(|value| value.to_str().ok())
5122                .map(str::to_owned)
5123        });
5124        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
5125        // that are not text at all — is "not known to be exhausted". This never makes a
5126        // response a refusal on its own: it says which limiter a refusal is attributed to
5127        // and where its hint comes from, so a value this cannot read costs a hint rather
5128        // than an answer.
5129        let exhausted = response
5130            .headers()
5131            .get("x-ratelimit-remaining")
5132            .and_then(|value| value.to_str().ok())
5133            == Some("0");
5134        // `retry-after` is what GitHub asks for when it asks; when it does not and the
5135        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
5136        // which is the same question answered as an absolute time. Nothing else here is a
5137        // hint, and a schedule is what answers a refusal that carries none.
5138        let retry_after = header("retry-after");
5139        let hinted_by = if retry_after.is_some() || !exhausted {
5140            Hint::RetryAfter
5141        } else {
5142            Hint::PrimaryReset
5143        };
5144        let hint = retry_after.or_else(|| {
5145            exhausted
5146                .then(|| header("x-ratelimit-reset"))
5147                .flatten()
5148                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
5149        });
5150        // Read before it is parsed, because the evidence which tells a secondary rate
5151        // limit from a rejected credential is in the body of a response whose status says
5152        // only "forbidden" — and a non-success response was never parsed at all.
5153        let body = response.text().await.map_err(|e| {
5154            Attempt::Failed(SourceError::Unavailable {
5155                message: format!("GitHub GraphQL response could not be read: {e}"),
5156            })
5157        })?;
5158        *reported_cost = serde_json::from_str::<Value>(&body)
5159            .ok()
5160            .as_ref()
5161            .and_then(|body| body.pointer("/data/rateLimit/cost"))
5162            .and_then(Value::as_u64);
5163        let parsed = serde_json::from_str::<Value>(&body).ok();
5164        *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
5165        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
5166            return Err(Attempt::Limited(Limited {
5167                limiter,
5168                hint,
5169                hinted_by,
5170            }));
5171        }
5172        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
5173            return Err(Attempt::Failed(SourceError::Auth {
5174                message: format!(
5175                    "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"
5176                ),
5177            }));
5178        }
5179        if !status.is_success() {
5180            return Err(Attempt::Failed(SourceError::Unavailable {
5181                message: format!("GitHub GraphQL returned HTTP {status}"),
5182            }));
5183        }
5184        let answered = if query == graphql::ISSUES_BY_REFERENCE {
5185            self.answer_unresolved(&body)
5186        } else if query == graphql::CREATE_ISSUES {
5187            answer_with_errors(&body).map_or_else(|| self.answer(&body), Ok)
5188        } else {
5189            self.answer(&body)
5190        };
5191        let mut data = answered.map_err(Attempt::Failed)?;
5192        if let Some(served) = served {
5193            stamp_served(&mut data, &served.to_rfc3339());
5194        }
5195        Ok(data)
5196    }
5197
5198    /// What one successful HTTP response says, once its GraphQL errors are read.
5199    /// [`Self::answer`], for a read of issues by reference: a reference GitHub cannot resolve
5200    /// for this credential — a repository it cannot see, an issue number it does not hold —
5201    /// is answered `null` beside a `NOT_FOUND` error naming it, and that is an answer about
5202    /// that reference rather than a failure of the others. Any other error refuses the read,
5203    /// as there.
5204    fn answer_unresolved(&self, body: &str) -> Result<Value, SourceError> {
5205        let parsed: Option<Value> = serde_json::from_str(body).ok();
5206        // Each error is a `NOT_FOUND` whose path names one slot of the batch, `r<n>` or its
5207        // `issue`, and the answer holds `null` at exactly that path: anything else — another
5208        // kind of error, one naming no slot, or one beside a value it says is not there — is a
5209        // refusal of the whole read.
5210        let unresolved = |data: &Value, error: &Value| {
5211            error.get("type").and_then(Value::as_str) == Some("NOT_FOUND")
5212                && error
5213                    .get("path")
5214                    .and_then(Value::as_array)
5215                    .is_some_and(|path| {
5216                        let alias = path.first().and_then(Value::as_str).and_then(|alias| {
5217                            alias
5218                                .strip_prefix('r')
5219                                .and_then(|slot| slot.parse::<usize>().ok())
5220                                .filter(|slot| *slot < REFERENCE_BATCH)
5221                                .map(|_| alias)
5222                        });
5223                        match (alias, path.get(1).and_then(Value::as_str), path.len()) {
5224                            (Some(alias), None, 1) => data.get(alias).is_some_and(Value::is_null),
5225                            (Some(alias), Some("issue"), 2) => data
5226                                .get(alias)
5227                                .and_then(|repository| repository.get("issue"))
5228                                .is_some_and(Value::is_null),
5229                            _ => false,
5230                        }
5231                    })
5232        };
5233        let unresolved_only = parsed.as_ref().is_some_and(|parsed| {
5234            parsed.get("data").is_some_and(|data| {
5235                data.is_object()
5236                    && parsed
5237                        .get("errors")
5238                        .and_then(Value::as_array)
5239                        .is_some_and(|errors| errors.iter().all(|error| unresolved(data, error)))
5240            })
5241        });
5242        match parsed {
5243            Some(parsed) if unresolved_only => Ok(parsed["data"].clone()),
5244            _ => self.answer(body),
5245        }
5246    }
5247
5248    fn answer(&self, body: &str) -> Result<Value, SourceError> {
5249        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
5250            message: format!("GitHub returned invalid JSON: {e}"),
5251        })?;
5252        let errors = body
5253            .get("errors")
5254            .map(|value| {
5255                value.as_array().ok_or_else(|| SourceError::Malformed {
5256                    message: "GitHub response errors is not an array".into(),
5257                })
5258            })
5259            .transpose()?;
5260        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
5261            let messages = errors
5262                .iter()
5263                .filter_map(|e| e.get("message").and_then(Value::as_str))
5264                .collect::<Vec<_>>()
5265                .join("; ");
5266            let message = if messages.is_empty() {
5267                "GitHub returned GraphQL errors".into()
5268            } else {
5269                messages
5270            };
5271            let normalized = message.to_ascii_lowercase();
5272            if normalized.contains("resource not accessible") || normalized.contains("scope") {
5273                return Err(SourceError::Auth {
5274                    message: format!(
5275                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
5276                        self.credential_name
5277                    ),
5278                });
5279            }
5280            return Err(SourceError::Refused { message });
5281        }
5282        body.get("data")
5283            .filter(|data| data.is_object())
5284            .cloned()
5285            .ok_or_else(|| SourceError::Malformed {
5286                message: "GitHub response has no data object".into(),
5287            })
5288    }
5289
5290    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
5291    // GraphQL cannot independently page them inside the outer item page. This source page is
5292    // deliberately bounded at that published maximum; the live drift journey exercises it.
5293    async fn board_page(
5294        &self,
5295        items_after: Option<&str>,
5296        items_first: u32,
5297    ) -> Result<Value, SourceError> {
5298        let data = self
5299            .graphql(
5300                graphql::BOARD,
5301                json!({"owner":self.owner,"number":self.project_number,
5302                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
5303                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
5304            )
5305            .await?;
5306        data.pointer("/owner/projectV2")
5307            .filter(|v| !v.is_null())
5308            .cloned()
5309            .ok_or_else(|| SourceError::Refused {
5310                message: format!(
5311                    "GitHub project {}/{} was not found or is not visible to the token",
5312                    self.owner, self.project_number
5313                ),
5314            })
5315    }
5316
5317    /// The search that finds the issues of this board, narrowed by `also` when it is
5318    /// given.
5319    ///
5320    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
5321    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
5322    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
5323    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
5324    /// from a task by the `parent` field each issue carries rather than by the search.
5325    fn board_search(&self, also: Option<&str>) -> String {
5326        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
5327        match also {
5328            Some(also) => format!("{scope} {also}"),
5329            None => scope,
5330        }
5331    }
5332
5333    /// One issue this source reached directly, as the board item a read of the board would
5334    /// have produced — or `None` when this board does not hold it.
5335    ///
5336    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
5337    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
5338    /// item's own id, that item's field values, and the issue as its content. One resolver
5339    /// for both routes is what makes an issue read through a search, through its own node
5340    /// id, or through its project's sub-issues report the same title, the same status, the
5341    /// same labels and the same qualified id.
5342    ///
5343    /// An issue with no entry for *this* board is not this source's to report, which is
5344    /// what keeps an id naming some other repository's issue from being answered as an item
5345    /// of this board. That answer is given about an **exhausted** connection and never
5346    /// about an unread page: the entry is looked for on the page in hand, and only if that
5347    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
5348    /// rest of it.
5349    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
5350        self.resolve_issue_as(issue, true).await
5351    }
5352
5353    /// [`Self::resolve_issue`], remembering what it resolves for this command's later reads
5354    /// and writes only when `remember` says to.
5355    async fn resolve_issue_as(
5356        &self,
5357        issue: &Value,
5358        remember: bool,
5359    ) -> Result<Option<Resolved>, SourceError> {
5360        if optional_str(issue, "__typename")? != Some("Issue") {
5361            return Ok(None);
5362        }
5363        let memberships = issue
5364            .get("projectItems")
5365            .ok_or_else(|| SourceError::Malformed {
5366                message: "GitHub issue is missing projectItems".into(),
5367            })?;
5368        let nodes = memberships
5369            .get("nodes")
5370            .and_then(Value::as_array)
5371            .ok_or_else(|| SourceError::Malformed {
5372                message: "GitHub issue projectItems.nodes is not an array".into(),
5373            })?;
5374        let held = match self.board_entry(nodes) {
5375            Some(held) => held.clone(),
5376            None => {
5377                let info = memberships
5378                    .get("pageInfo")
5379                    .ok_or_else(|| SourceError::Malformed {
5380                        message: "GitHub issue projectItems has no pageInfo".into(),
5381                    })?;
5382                // The page held no entry for this board. Whether that means the issue is
5383                // not on it is a question about the rest of the connection, and only a
5384                // connection with no rest answers it here.
5385                if !required_bool(info, "hasNextPage")? {
5386                    return Ok(None);
5387                }
5388                let cursor = required_str(info, "endCursor")?;
5389                validate_cursor_progress(None, cursor)?;
5390                let issue_id = required_str(issue, "id")?;
5391                match self.board_membership(issue_id, cursor).await? {
5392                    Some(held) => held,
5393                    None => return Ok(None),
5394                }
5395            }
5396        };
5397        // The membership's own `updatedAt` rides along: it is the board half of the item's change
5398        // token, which a field change moves where the issue's own time does not.
5399        let item = json!({
5400            "id": required_str(&held, "id")?,
5401            "updatedAt": held.get("updatedAt"),
5402            "project": held.get("project"),
5403            "fieldValues": held.get("fieldValues"),
5404            "content": issue,
5405        });
5406        if remember {
5407            self.resolve(&item)
5408        } else {
5409            self.resolve_unremembered(&item)
5410        }
5411    }
5412
5413    /// This board's own entry among one page of an issue's `Issue.projectItems`.
5414    ///
5415    /// One spelling of *which membership is this board's*, so the page a read carries and
5416    /// the pages [`Self::board_membership`] walks are searched by the same rule.
5417    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
5418        nodes.iter().find(|node| {
5419            node.pointer("/project/number").and_then(Value::as_u64)
5420                == Some(u64::from(self.project_number))
5421        })
5422    }
5423
5424    /// The rest of one issue's board memberships, from `after`, for this board's entry.
5425    ///
5426    /// The recovery read: a page of memberships that holds no entry for this board says
5427    /// nothing about the memberships past it, so the connection is walked to exhaustion
5428    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
5429    /// positive answer — the whole connection was read and no entry named this board —
5430    /// rather than a failure, and the walk is held to
5431    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
5432    /// with a cursor that does not advance is refused instead of spun on.
5433    async fn board_membership(
5434        &self,
5435        issue: &str,
5436        after: &str,
5437    ) -> Result<Option<Value>, SourceError> {
5438        let mut after = after.to_owned();
5439        loop {
5440            let data = self
5441                .graphql(
5442                    graphql::ISSUE_BOARD_ITEMS,
5443                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
5444                           "nestedFirst":NESTED_PAGE_SIZE}),
5445                )
5446                .await?;
5447            let Some(connection) = data
5448                .pointer("/node/projectItems")
5449                .filter(|value| !value.is_null())
5450            else {
5451                // The id resolved to nothing, or to something with no memberships to walk —
5452                // which is the same answer as a connection holding no entry for this board.
5453                return Ok(None);
5454            };
5455            let nodes = connection
5456                .get("nodes")
5457                .and_then(Value::as_array)
5458                .ok_or_else(|| SourceError::Malformed {
5459                    message: "GitHub issue projectItems.nodes is not an array".into(),
5460                })?;
5461            if let Some(held) = self.board_entry(nodes) {
5462                return Ok(Some(held.clone()));
5463            }
5464            let info = connection
5465                .get("pageInfo")
5466                .ok_or_else(|| SourceError::Malformed {
5467                    message: "GitHub issue projectItems has no pageInfo".into(),
5468                })?;
5469            let next = required_bool(info, "hasNextPage")?
5470                .then(|| required_str(info, "endCursor"))
5471                .transpose()?;
5472            match next {
5473                Some(next) => {
5474                    validate_cursor_progress(Some(&after), next)?;
5475                    after = next.to_owned();
5476                }
5477                None => return Ok(None),
5478            }
5479        }
5480    }
5481
5482    /// One page of a board-scoped issue search, and where the next page resumes.
5483    async fn search_page(
5484        &self,
5485        search: &str,
5486        first: u32,
5487        after: Option<&str>,
5488    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
5489        let data = self
5490            .graphql(
5491                graphql::SEARCH_ISSUES,
5492                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
5493                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
5494                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5495            )
5496            .await?;
5497        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
5498            message: "GitHub search response has no search connection".into(),
5499        })?;
5500        let mut found = Vec::new();
5501        for node in connection
5502            .get("nodes")
5503            .and_then(Value::as_array)
5504            .ok_or_else(|| SourceError::Malformed {
5505                message: "GitHub search nodes is not an array".into(),
5506            })?
5507        {
5508            if let Some(resolved) = self.resolve_issue(node).await? {
5509                found.push(resolved);
5510            }
5511        }
5512        let info = connection
5513            .get("pageInfo")
5514            .ok_or_else(|| SourceError::Malformed {
5515                message: "GitHub search connection has no pageInfo".into(),
5516            })?;
5517        let next = required_bool(info, "hasNextPage")?
5518            .then(|| required_str(info, "endCursor"))
5519            .transpose()?
5520            .map(str::to_owned);
5521        if let Some(next) = &next {
5522            validate_cursor_progress(after, next)?;
5523        }
5524        Ok((found, next))
5525    }
5526
5527    /// Every issue this board holds, completed with what this run wrote.
5528    ///
5529    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
5530    /// is an index and is eventually consistent, so an issue this run created seconds ago
5531    /// can be absent from it, and a project listed straight after being written would
5532    /// otherwise be missing from its own board. What is added back is only what this
5533    /// process itself wrote, out of [`Self::created`], which lives and dies with the
5534    /// process.
5535    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5536        let found = self.searched_issues().await?;
5537        self.completed_with_written(found, |_| true)
5538    }
5539
5540    /// Every issue this board's own search reports, walked to exhaustion, read once per
5541    /// source.
5542    ///
5543    /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
5544    /// needs it too and the two would otherwise walk the same search twice in one command.
5545    /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
5546    /// is.
5547    async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
5548        let cached = self.search_cache()?.clone();
5549        if let Some(held) = cached {
5550            return Ok(held);
5551        }
5552        let mut after: Option<String> = None;
5553        let mut found = Vec::new();
5554        let search = self.board_search(None);
5555        loop {
5556            let (page, next) = self
5557                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5558                .await?;
5559            found.extend(page);
5560            match next {
5561                Some(next) => after = Some(next),
5562                None => break,
5563            }
5564        }
5565        *self.search_cache()? = Some(found.clone());
5566        Ok(found)
5567    }
5568
5569    /// Every issue this board's own search reports with `also` beside its scope, walked to
5570    /// exhaustion — held for no later read, which may not be narrowed alike.
5571    async fn searched_narrowly(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5572        let mut after: Option<String> = None;
5573        let mut found = Vec::new();
5574        let search = self.board_search(Some(also));
5575        loop {
5576            let (page, next) = self
5577                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
5578                .await?;
5579            found.extend(page);
5580            match next {
5581                Some(next) => after = Some(next),
5582                None => return Ok(found),
5583            }
5584        }
5585    }
5586
5587    /// This process's own view of the board's issues, or the refusal a poisoned lock is.
5588    fn search_cache(
5589        &self,
5590    ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
5591        self.search_cache
5592            .lock()
5593            .map_err(|_| SourceError::Unavailable {
5594                message: "this source's view of the board's issues was left inconsistent by an \
5595                      earlier failure; next: run the command again"
5596                    .into(),
5597            })
5598    }
5599
5600    /// `found`, with everything this run wrote that `keep` accepts and the read did not
5601    /// report.
5602    ///
5603    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
5604    /// at all: the search index is behind, and a node read of an item filed moments ago can
5605    /// be too.
5606    fn completed_with_written(
5607        &self,
5608        mut found: Vec<Resolved>,
5609        keep: impl Fn(&Resolved) -> bool,
5610    ) -> Result<Vec<Resolved>, SourceError> {
5611        for own in self.created()?.iter().filter(|own| keep(own)) {
5612            if !found.iter().any(|item| item.id == own.id) {
5613                found.push(own.clone());
5614            }
5615        }
5616        Ok(found)
5617    }
5618
5619    /// What resolving one node id reached.
5620    ///
5621    /// Three answers rather than an `Option`, because a board *draft* is none of the other
5622    /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
5623    /// is completed by a read of the draft itself rather than reported as nothing.
5624    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
5625        let asked = self
5626            .graphql(
5627                graphql::ISSUE,
5628                json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
5629                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5630            )
5631            .await;
5632        let data = match asked {
5633            Ok(data) => data,
5634            // A string that is not a node id at all is not a failure to report: it is an id
5635            // this board does not hold, which is what every read of one already answers.
5636            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
5637            Err(error) => return Err(error),
5638        };
5639        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5640            return Ok(Reached::Nothing);
5641        };
5642        if optional_str(node, "__typename")? == Some("DraftIssue") {
5643            return Ok(Reached::Draft);
5644        }
5645        Ok(match self.resolve_issue(node).await? {
5646            Some(item) => Reached::Held(Box::new(item)),
5647            None => Reached::Nothing,
5648        })
5649    }
5650
5651    /// One item of this board by its own id, whatever kind it is.
5652    ///
5653    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
5654    /// run wrote is read first, because a node read of an item created moments ago can
5655    /// still be behind the board field values written onto it — see [`Self::created`].
5656    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5657        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
5658            return Ok(Some(own.clone()));
5659        }
5660        if let Some(held) = self.read_ahead()?.remove(id) {
5661            return Ok(held);
5662        }
5663        match self.reach(id).await? {
5664            Reached::Held(item) => Ok(Some(*item)),
5665            Reached::Nothing => Ok(None),
5666            Reached::Draft => self.draft_by_id(id).await,
5667        }
5668    }
5669
5670    /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
5671    /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
5672    /// than one request per id.
5673    ///
5674    /// What this run wrote answers first, as it does there, and only the rest is read. One id
5675    /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
5676    /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
5677    /// id at a time, so that id is answered as not held and the others as themselves; a draft
5678    /// is completed by a read of the draft, exactly as there.
5679    async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
5680        let mut found: Vec<Option<Option<Resolved>>> = {
5681            let created = self.created()?;
5682            ids.iter()
5683                .map(|id| {
5684                    created
5685                        .iter()
5686                        .find(|own| own.id == *id)
5687                        .map(|own| Some(own.clone()))
5688                })
5689                .collect()
5690        };
5691        let unread: Vec<NativeId> = ids
5692            .iter()
5693            .zip(&found)
5694            .filter(|(_, found)| found.is_none())
5695            .map(|(id, _)| id.clone())
5696            .collect();
5697        let mut read = Vec::with_capacity(unread.len());
5698        if let [one] = unread.as_slice() {
5699            read.push(self.item_by_id(one).await?);
5700        } else {
5701            for batch in unread.chunks(DETAIL_BATCH) {
5702                let data = match self
5703                    .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
5704                    .await
5705                {
5706                    Ok(data) => data,
5707                    Err(error) if unresolvable_node(&error) => {
5708                        for id in batch {
5709                            read.push(self.item_by_id(id).await?);
5710                        }
5711                        continue;
5712                    }
5713                    Err(error) => return Err(error),
5714                };
5715                for (slot, id) in batch.iter().enumerate() {
5716                    let node =
5717                        data.get(format!("i{slot}"))
5718                            .ok_or_else(|| SourceError::Malformed {
5719                                message: format!(
5720                                    "GitHub answered a batch read with no item for {}",
5721                                    id.0
5722                                ),
5723                            })?;
5724                    read.push(if node.is_null() {
5725                        None
5726                    } else if optional_str(node, "__typename")? == Some("DraftIssue") {
5727                        self.draft_by_id(id).await?
5728                    } else {
5729                        if optional_str(node, "__typename")? == Some("Issue")
5730                            && required_str(node, "id")? != id.0
5731                        {
5732                            return Err(SourceError::Malformed {
5733                                message: format!(
5734                                    "GitHub answered the read of {} with issue {}",
5735                                    id.0,
5736                                    required_str(node, "id")?
5737                                ),
5738                            });
5739                        }
5740                        self.resolve_issue(node).await?
5741                    });
5742                }
5743            }
5744        }
5745        let mut read = read.into_iter();
5746        Ok(found
5747            .iter_mut()
5748            .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
5749            .collect())
5750    }
5751
5752    fn read_ahead(
5753        &self,
5754    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Option<Resolved>>>, SourceError> {
5755        self.read_ahead
5756            .lock()
5757            .map_err(|_| SourceError::Unavailable {
5758                message:
5759                    "items read ahead for a copy were left inconsistent; run the command again"
5760                        .into(),
5761            })
5762    }
5763
5764    fn resolved_cache(
5765        &self,
5766    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
5767        self.resolved_cache
5768            .lock()
5769            .map_err(|_| SourceError::Unavailable {
5770                message: "resolved item records were left inconsistent; run the command again"
5771                    .into(),
5772            })
5773    }
5774
5775    /// Reuse a record this invocation already resolved. The mutation sender invalidates
5776    /// it before writing, so a partial failure cannot leave a pre-write binding behind.
5777    async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5778        let cached = self.resolved_cache()?.get(id).cloned();
5779        if let Some(item) = cached {
5780            return Ok(Some(item));
5781        }
5782        self.item_by_id(id).await
5783    }
5784
5785    /// Take out the item an issue read by reference reached for `id`, when one is held: the
5786    /// status write it was read for is the one caller, so no other read or write is ever
5787    /// answered from a read that selected only what that write needs.
5788    fn take_held(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5789        Ok(self
5790            .held_cache
5791            .lock()
5792            .map_err(|_| SourceError::Unavailable {
5793                message: "items read by issue reference were left inconsistent; run the \
5794                          command again"
5795                    .into(),
5796            })?
5797            .remove(id))
5798    }
5799
5800    /// One board draft by its own id, with the board item it sits in — or `None` when no
5801    /// item of this board is that draft's.
5802    ///
5803    /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
5804    /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
5805    /// links a draft to one board item, so the page this read carries is the whole of that
5806    /// connection, and a page that reports more than it holds is refused rather than read
5807    /// as an answer about memberships nobody read.
5808    async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5809        let data = self
5810            .graphql(
5811                graphql::DRAFT,
5812                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
5813                       "boardItems":BOARD_ITEMS_PAGE_SIZE}),
5814            )
5815            .await?;
5816        // Gone between the two reads is an answer — the draft is no longer there. Anything
5817        // else than the draft [`Self::reach`] was just told this id is, is not one.
5818        let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
5819            return Ok(None);
5820        };
5821        if optional_str(draft, "__typename")? != Some("DraftIssue") {
5822            return Err(SourceError::Malformed {
5823                message: format!(
5824                    "GitHub answered {} as a draft and then as something else",
5825                    id.0
5826                ),
5827            });
5828        }
5829        if required_str(draft, "id")? != id.0 {
5830            return Err(SourceError::Malformed {
5831                message: format!("GitHub answered a different draft for {}", id.0),
5832            });
5833        }
5834        let memberships = draft
5835            .get("projectV2Items")
5836            .ok_or_else(|| SourceError::Malformed {
5837                message: format!("GitHub draft {} is missing projectV2Items", id.0),
5838            })?;
5839        let nodes = memberships
5840            .get("nodes")
5841            .and_then(Value::as_array)
5842            .ok_or_else(|| SourceError::Malformed {
5843                message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
5844            })?;
5845        let info = memberships
5846            .get("pageInfo")
5847            .ok_or_else(|| SourceError::Malformed {
5848                message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
5849            })?;
5850        // Read whether or not this board's entry is on the page: a page claiming more than
5851        // the one item GitHub links a draft to is a malformed answer either way.
5852        if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
5853            return Err(SourceError::Malformed {
5854                message: format!(
5855                    "GitHub draft {} reports more board items than the one GitHub links a draft \
5856                     to",
5857                    id.0
5858                ),
5859            });
5860        }
5861        if let Some(node) = nodes.first()
5862            && node
5863                .pointer("/project/number")
5864                .and_then(Value::as_u64)
5865                .is_none()
5866        {
5867            return Err(SourceError::Malformed {
5868                message: format!(
5869                    "GitHub draft {} board item has no numeric project number",
5870                    id.0
5871                ),
5872            });
5873        }
5874        let Some(held) = self.board_entry(nodes) else {
5875            return Ok(None);
5876        };
5877        if required_str(
5878            held.get("project").ok_or_else(|| SourceError::Malformed {
5879                message: format!("GitHub draft {} board item has no project", id.0),
5880            })?,
5881            "id",
5882        )? != self.board_fields().await?.id.as_str()
5883        {
5884            return Ok(None);
5885        }
5886        let item = json!({
5887            "id": required_str(held, "id")?,
5888            "project": held.get("project"),
5889            "fieldValues": held.get("fieldValues"),
5890            "content": draft,
5891        });
5892        self.resolve(&item)
5893    }
5894
5895    /// The board's own id and field definitions, for a write whose item does not carry
5896    /// them — never its items.
5897    ///
5898    /// A board this command has already listed supplies them, since it read them beside its
5899    /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
5900    /// is consulted about which items the board holds: see the module documentation for
5901    /// why a question about one known item is answered by reading that item.
5902    async fn board_fields(&self) -> Result<BoardFields, SourceError> {
5903        if let Some(board) = self.board_cache()?.as_ref() {
5904            return Ok(BoardFields {
5905                id: BoardId::parse(&board.id)?,
5906                fields: board.fields.clone(),
5907            });
5908        }
5909        if let Some(held) = self.fields_cache()?.clone() {
5910            return Ok(held);
5911        }
5912        let data = self
5913            .graphql(
5914                graphql::BOARD_FIELDS,
5915                json!({"owner":self.owner,"number":self.project_number,
5916                       "nestedFirst":NESTED_PAGE_SIZE}),
5917            )
5918            .await?;
5919        self.fields_read(&data)
5920    }
5921
5922    /// The board's id and fields out of an answer carrying the `boardFields` root, held for
5923    /// the rest of this command.
5924    fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
5925        let board = data
5926            .pointer("/boardFields/projectV2")
5927            .filter(|value| !value.is_null())
5928            .ok_or_else(|| SourceError::Refused {
5929                message: format!(
5930                    "GitHub project {}/{} was not found or is not visible to the token",
5931                    self.owner, self.project_number
5932                ),
5933            })?;
5934        let read = BoardFields {
5935            id: BoardId::parse(required_str(board, "id")?)?,
5936            fields: board.get("fields").cloned().unwrap_or(Value::Null),
5937        };
5938        *self.fields_cache()? = Some(read.clone());
5939        Ok(read)
5940    }
5941
5942    /// Read what creating an issue in `repository` needs and this command has not read yet —
5943    /// the board's fields and the repository's node id — in one request when it needs both.
5944    ///
5945    /// When either is already known this sends nothing, and the other is read by its own
5946    /// document where it is asked for, so no create reads anything twice.
5947    async fn creation_context(
5948        &self,
5949        repository: &RepositoryTarget,
5950        incoming: &Incoming<'_>,
5951    ) -> Result<(), SourceError> {
5952        let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
5953        if fields_known || self.repository_cache()?.contains_key(repository) {
5954            return Ok(());
5955        }
5956        let data = self
5957            .graphql(
5958                graphql::CREATION_CONTEXT,
5959                json!({"owner":self.owner,"number":self.project_number,
5960                       "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
5961                       "repositoryName":repository.name}),
5962            )
5963            .await?;
5964        self.fields_read(&data)?;
5965        self.repository_read(&data, repository, incoming)?;
5966        Ok(())
5967    }
5968
5969    /// This process's own view of the board's fields, or the refusal a poisoned lock is.
5970    fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
5971        self.fields_cache
5972            .lock()
5973            .map_err(|_| SourceError::Unavailable {
5974                message: "this source's view of the board's fields was left inconsistent by an \
5975                      earlier failure; next: run the command again"
5976                    .into(),
5977            })
5978    }
5979
5980    /// What a write to `item` needs of the board, read off that item when it says enough and
5981    /// off [`Self::board_fields`] when it does not.
5982    ///
5983    /// A node read of an item names its board and carries the definition of every field it
5984    /// holds a value of — so an item naming its board, holding a value of the origin field,
5985    /// and, when the write carries a status, holding a `Status` value, needs no read of the
5986    /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
5987    /// of may still be on the board, and a view reading it as absent would refuse a write the
5988    /// board can take or skip a field write the board needs, so such an item — and a create,
5989    /// which has no item yet — takes the board's fields from their own read instead.
5990    async fn fields_for(
5991        &self,
5992        item: Option<&Resolved>,
5993        writes_status: bool,
5994        selects_priority: bool,
5995    ) -> Result<BoardFields, SourceError> {
5996        if let Some(board) = item.and_then(Resolved::carried_board) {
5997            return Ok(board);
5998        }
5999        if let Some(item) = item
6000            && let Some(board_id) = item.named_board()
6001            && item.defines(ORIGIN_FIELD)
6002            && (!writes_status || item.defines("Status"))
6003            && (!selects_priority || item.defines(PRIORITY_FIELD))
6004            && self
6005                .metadata_fields
6006                .iter()
6007                .all(|projection| item.defines(&projection.field))
6008        {
6009            return Ok(BoardFields {
6010                id: board_id,
6011                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6012            });
6013        }
6014        self.board_fields().await
6015    }
6016
6017    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
6018    /// when that id names nothing here with a sub-issue relationship to walk.
6019    ///
6020    /// `None` and an empty answer are different: `None` is *this is not an issue of this
6021    /// GitHub*, which is what sends a project selector on to be read as a name, and an
6022    /// empty vector is a project that holds nothing.
6023    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6024        let mut after: Option<String> = None;
6025        let mut children = Vec::new();
6026        // The project issue as the first page carried it, held once the walk has ended.
6027        let mut parent: Option<Resolved> = None;
6028        loop {
6029            let asked = self
6030                .graphql(
6031                    graphql::SUB_ISSUES,
6032                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
6033                           "nestedFirst":NESTED_PAGE_SIZE,
6034                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
6035                )
6036                .await;
6037            let data = match asked {
6038                Ok(data) => data,
6039                // A string that is not a node id at all is not a failure to report: it is
6040                // the ordinary answer to a selector naming a project by its name.
6041                Err(error) if unresolvable_node(&error) => return Ok(None),
6042                Err(error) => return Err(error),
6043            };
6044            let Some(connection) = data
6045                .pointer("/node/subIssues")
6046                .filter(|value| !value.is_null())
6047            else {
6048                // No such node, or one with no sub-issue relationship — a board draft is
6049                // the one this board can really hold.
6050                return Ok(None);
6051            };
6052            if after.is_none()
6053                && let Some(node) = data.get("node").filter(|node| node.get("id").is_some())
6054            {
6055                parent = self
6056                    .resolve_issue(node)
6057                    .await?
6058                    .filter(|resolved| resolved.id == *id);
6059            }
6060            for node in connection
6061                .get("nodes")
6062                .and_then(Value::as_array)
6063                .ok_or_else(|| SourceError::Malformed {
6064                    message: "GitHub subIssues.nodes is not an array".into(),
6065                })?
6066            {
6067                if let Some(resolved) = self.resolve_issue(node).await? {
6068                    children.push(resolved);
6069                }
6070            }
6071            let info = connection
6072                .get("pageInfo")
6073                .ok_or_else(|| SourceError::Malformed {
6074                    message: "GitHub subIssues connection has no pageInfo".into(),
6075                })?;
6076            let next = required_bool(info, "hasNextPage")?
6077                .then(|| required_str(info, "endCursor"))
6078                .transpose()?;
6079            match next {
6080                Some(next) => {
6081                    validate_cursor_progress(after.as_deref(), next)?;
6082                    after = Some(next.to_owned());
6083                }
6084                None => {
6085                    // Held only once the walk has ended, so one failing part way holds
6086                    // nothing and every read it would have answered is made.
6087                    let mut carried = self.carried()?;
6088                    for item in parent.into_iter().chain(children.iter().cloned()) {
6089                        carried.insert(item.id.clone(), item);
6090                    }
6091                    return Ok(Some(children));
6092                }
6093            }
6094        }
6095    }
6096
6097    fn carried(
6098        &self,
6099    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
6100        self.carried.lock().map_err(|_| SourceError::Unavailable {
6101            message: "items a project's listing carried were left inconsistent; run the \
6102                      command again"
6103                .into(),
6104        })
6105    }
6106
6107    /// Which issue of this board a project *name* is, or `None` when none is.
6108    ///
6109    /// One bounded query which filters on that name at the server, rather than a walk of
6110    /// every issue the board holds. The name is compared again here: the qualifier narrows
6111    /// what GitHub sends, and this source decides what it names.
6112    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
6113        let search = self.board_search(Some(&title_qualifier(name)));
6114        let mut after = None;
6115        loop {
6116            let (candidates, next) = self
6117                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6118                .await?;
6119            if let Some(item) = candidates.into_iter().find(|item| {
6120                item.kind == BoardKind::Work(ItemKind::Project)
6121                    && item.title.eq_ignore_ascii_case(name)
6122            }) {
6123                return Ok(Some(item.id));
6124            }
6125            match next {
6126                Some(next) => after = Some(next),
6127                None => return Ok(None),
6128            }
6129        }
6130    }
6131
6132    /// Everything filed under one project of this board: the sub-issues of the issue that
6133    /// project is.
6134    ///
6135    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
6136    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
6137    /// gains projects, or as another project gains tasks.
6138    ///
6139    /// A qualified id names the issue and is asked for its sub-issues directly: one
6140    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
6141    /// read as a project *name*, which costs the one bounded search
6142    /// [`Self::project_by_name`] makes.
6143    ///
6144    /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
6145    /// — and each answer is still cut to the items whose parent is this project, so one this
6146    /// process has since filed elsewhere is not reported here, and completed with what this
6147    /// process filed under it.
6148    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
6149        let held = self.children_cache()?.get(selector).cloned();
6150        let (project, children) = match held {
6151            Some(held) => held,
6152            None => {
6153                let answered = match self.sub_issues(selector).await? {
6154                    Some(children) => (selector.clone(), children),
6155                    None => match self.project_by_name(&selector.0).await? {
6156                        Some(project) => {
6157                            let children = self.sub_issues(&project).await?.unwrap_or_default();
6158                            (project, children)
6159                        }
6160                        None => return Ok(Vec::new()),
6161                    },
6162                };
6163                self.children_cache()?
6164                    .insert(selector.clone(), answered.clone());
6165                answered
6166            }
6167        };
6168        let mut children: Vec<Resolved> = children
6169            .into_iter()
6170            .filter(|child| child.parent.as_ref() == Some(&project))
6171            .collect();
6172        for own in self.updated()?.iter() {
6173            if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
6174                children.push(own.clone());
6175            }
6176        }
6177        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
6178    }
6179
6180    /// Every issue of this board GitHub's issue search reports updated at or after `since`,
6181    /// completed with what this run wrote — the candidates a comment-activity read confirms.
6182    ///
6183    /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
6184    /// on the board whatever repository, and whatever owner, it lives in, so no repository or
6185    /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
6186    /// sufficient is a fact about GitHub rather than about this source: a comment written on an
6187    /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
6188    /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
6189    /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
6190    /// rather than silently narrowing a caller's answer.
6191    ///
6192    /// The instant is written to the second, rounded down, which can only widen what the
6193    /// search returns; confirmation against each candidate's own comments is what makes the
6194    /// answer exact. The search is an index that lags a write by a second or two — the module
6195    /// documentation records it — so a caller that asks again from its last instant should
6196    /// overlap the two by more than that.
6197    async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
6198        let found = self.searched(&updated_qualifier(since)).await?;
6199        self.completed_with_written(found, |_| true)
6200    }
6201
6202    /// Every issue of this board GitHub's issue search reports for the board-scoped search
6203    /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
6204    ///
6205    /// Uncompleted: what this process wrote is added by the caller, which knows whether its
6206    /// own record is the fresher of the two.
6207    async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
6208        let search = self.board_search(Some(also));
6209        let mut after: Option<String> = None;
6210        let mut found = Vec::new();
6211        loop {
6212            let (page, next) = self
6213                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
6214                .await?;
6215            found.extend(page);
6216            match next {
6217                Some(next) => after = Some(next),
6218                None => return Ok(found),
6219            }
6220        }
6221    }
6222
6223    /// A bounded task answer; the versioned cursor carries the connection position, how
6224    /// many rows of the page starting there were already handed out, and the own-write ids
6225    /// already observed, including across a new source instance.
6226    ///
6227    /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
6228    /// sliced from the pages it needs; why is the module documentation's paging contract.
6229    async fn search_tasks(
6230        &self,
6231        query: &TaskQuery,
6232        page: &PageRequest,
6233        also: &str,
6234    ) -> Result<Page<Task>, SourceError> {
6235        let mut position = match &page.cursor {
6236            None => SearchPosition::default(),
6237            Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
6238                .ok()
6239                .filter(|position| {
6240                    position.version == SEARCH_CURSOR_VERSION
6241                        && position.connection.valid_resume(position.offset)
6242                })
6243                .ok_or_else(|| SourceError::Config {
6244                    message: "page cursor is invalid".into(),
6245                })?,
6246        };
6247        let search = self.board_search(Some(also));
6248        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6249        let own = self.with_own_writes(Vec::new())?;
6250        // An issue this process commented on is a candidate of a comment-activity read
6251        // whether or not the search has caught up with the comment; see `Self::commented`.
6252        let commented = match query.commented_since {
6253            Some(_) => self.commented()?.clone(),
6254            None => Vec::new(),
6255        };
6256        for id in own.iter().map(|item| &item.id).chain(&commented) {
6257            if !position.own.contains(id) {
6258                position.own.push(id.clone());
6259            }
6260        }
6261        let mut tasks = Vec::new();
6262        while !position.connection.exhausted() && tasks.len() < limit {
6263            let first = SEARCH_PAGE_SIZE;
6264            // Page size is part of the key: a short cached answer cannot answer a wider ask.
6265            let key =
6266                serde_json::to_string(&("page", &search, &position.connection.after(), first))
6267                    .expect("search page key is serializable");
6268            let cached = if query.commented_since.is_none() {
6269                self.narrowed_cache()?.get(&key).cloned()
6270            } else {
6271                None
6272            };
6273            let (found, next) = match cached {
6274                Some(found) => {
6275                    let next = self
6276                        .search_next
6277                        .lock()
6278                        .map_err(|_| SourceError::Unavailable {
6279                            message:
6280                                "search pagination was left inconsistent; run the command again"
6281                                    .into(),
6282                        })?
6283                        .get(&key)
6284                        .cloned()
6285                        .flatten();
6286                    (found, next)
6287                }
6288                None => {
6289                    let (found, next) = self
6290                        .search_page(&search, first, position.connection.after())
6291                        .await?;
6292                    if query.commented_since.is_none() {
6293                        self.search_next
6294                            .lock()
6295                            .map_err(|_| SourceError::Unavailable {
6296                                message:
6297                                    "search pagination was left inconsistent; run the command again"
6298                                        .into(),
6299                            })?
6300                            .insert(key.clone(), next.clone());
6301                        self.narrowed_cache()?.insert(key, found.clone());
6302                    }
6303                    (found, next)
6304                }
6305            };
6306            let rows = found.len();
6307            for mut item in found.into_iter().skip(position.offset) {
6308                if tasks.len() == limit {
6309                    break;
6310                }
6311                position.offset += 1;
6312                if position.own.contains(&item.id) {
6313                    if position.seen.contains(&item.id) {
6314                        continue;
6315                    }
6316                    position.seen.push(item.id.clone());
6317                    // The search's own copy of an issue this process only commented on is as
6318                    // good as a node read of it, since its comments are read either way.
6319                    let only_commented = commented.contains(&item.id)
6320                        && !own.iter().any(|written| written.id == item.id);
6321                    if !only_commented {
6322                        let updated_at = item.updated_at;
6323                        let Some(written) = self.search_written(&own, &item.id).await? else {
6324                            continue;
6325                        };
6326                        item = written;
6327                        item.updated_at = item.updated_at.max(updated_at);
6328                        self.resolved_cache()?.insert(item.id.clone(), item.clone());
6329                    }
6330                }
6331                if item.kind == BoardKind::Work(ItemKind::Task) {
6332                    let task = item.task()?;
6333                    if task_matches(&task, query, &query.project)
6334                        && self.commented_since(&item, query.commented_since).await?
6335                    {
6336                        tasks.push(task);
6337                    }
6338                }
6339            }
6340            if position.offset < rows {
6341                continue;
6342            }
6343            position.offset = 0;
6344            position.connection = match next {
6345                Some(after) => SearchConnection::Continuing {
6346                    after: Cursor(after),
6347                },
6348                None => SearchConnection::Exhausted {},
6349            };
6350        }
6351        if position.connection.exhausted() {
6352            for id in position.own.clone() {
6353                if position.seen.contains(&id) {
6354                    continue;
6355                }
6356                if tasks.len() == limit {
6357                    break;
6358                }
6359                position.seen.push(id.clone());
6360                let Some(item) = self.search_written(&own, &id).await? else {
6361                    continue;
6362                };
6363                if item.kind == BoardKind::Work(ItemKind::Task) {
6364                    let task = item.task()?;
6365                    if task_matches(&task, query, &query.project)
6366                        && self.commented_since(&item, query.commented_since).await?
6367                    {
6368                        tasks.push(task);
6369                    }
6370                }
6371            }
6372        }
6373        let more = !position.connection.exhausted()
6374            || position.own.iter().any(|id| !position.seen.contains(id));
6375        Ok(Page {
6376            items: tasks,
6377            next: more.then(|| {
6378                Cursor(serde_json::to_string(&position).expect("search position is serializable"))
6379            }),
6380        })
6381    }
6382
6383    /// A resumed process has the ids but no write records; resolve only a record the
6384    /// current page needs, by its uncached node read rather than the lagging search index.
6385    async fn search_written(
6386        &self,
6387        own: &[Resolved],
6388        id: &NativeId,
6389    ) -> Result<Option<Resolved>, SourceError> {
6390        match own.iter().find(|item| item.id == *id) {
6391            Some(item) => Ok(Some(item.clone())),
6392            None => self.item_by_id(id).await,
6393        }
6394    }
6395
6396    /// The candidates for a task query carrying a text, metadata or origin predicate, read
6397    /// without enumerating the board — or `None` for a query carrying none of the three, which
6398    /// keeps the reads it always had.
6399    ///
6400    /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
6401    /// because it names at most a handful of items. Text and metadata are answered by one
6402    /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
6403    /// further by `updated:>=` when the query also asks for comment activity, since both
6404    /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
6405    /// process afterwards by the same predicates [`task_matches`] applies to every read.
6406    ///
6407    /// Completed with what this process wrote, its own record winning over the index's copy
6408    /// of the same item: see [`Self::with_own_writes`].
6409    async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
6410        let asked = match (&query.origin, narrowing_qualifiers(query)) {
6411            (Some(origin), _) => Narrowing::Origin(origin.clone()),
6412            (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
6413                Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
6414                None => qualifiers,
6415            }),
6416            (None, None) => return Ok(None),
6417        };
6418        // A question about comment activity is asked afresh every time, as it always was: it
6419        // is the one a caller polls from one source while waiting for the index, and an
6420        // answer held from the first poll would be the answer to every later one.
6421        let key = query.commented_since.is_none().then(|| asked.key());
6422        let cached = match &key {
6423            Some(key) => self.narrowed_cache()?.get(key).cloned(),
6424            None => None,
6425        };
6426        let found = match cached {
6427            Some(found) => found,
6428            None => {
6429                let found = match &asked {
6430                    Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
6431                    Narrowing::Search(also) => self.searched(also).await?,
6432                };
6433                if let Some(key) = key {
6434                    self.narrowed_cache()?.insert(key, found.clone());
6435                }
6436                found
6437            }
6438        };
6439        self.with_own_writes(found).map(Some)
6440    }
6441
6442    /// The candidates for a project or unscoped document query carrying a searchable text,
6443    /// read without enumerating the board — or `None` for a query with no text or a blank one,
6444    /// which keeps the read it always had.
6445    ///
6446    /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
6447    /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
6448    /// costs is the issues that match and never the board. Its answer is held for the command
6449    /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
6450    /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
6451    /// substring rule, exactly as an item of the wider read was, and is completed with what this
6452    /// process wrote: see [`Self::with_own_writes`].
6453    async fn text_searched(
6454        &self,
6455        text: Option<&TextQuery>,
6456    ) -> Result<Option<Vec<Resolved>>, SourceError> {
6457        let Some(also) = text_qualifiers(text) else {
6458            return Ok(None);
6459        };
6460        let key = Narrowing::Search(also.clone()).key();
6461        let cached = self.narrowed_cache()?.get(&key).cloned();
6462        let found = match cached {
6463            Some(found) => found,
6464            None => {
6465                let found = self.searched(&also).await?;
6466                self.narrowed_cache()?.insert(key, found.clone());
6467                found
6468            }
6469        };
6470        self.with_own_writes(found).map(Some)
6471    }
6472
6473    /// Every `kind` item of this board whose own origin is exactly `origin`: the candidates
6474    /// [`Self::narrowed`] reads for a task query carrying that origin — one
6475    /// [`graphql::ORIGIN_LOOKUP`] walk, held for the command under the same key, completed with
6476    /// what this process wrote — each confirmed against its own origin entry.
6477    ///
6478    /// What answers a project or document listing by origin, so finding every copy of one plan
6479    /// costs the items that carry its origin and never a page of the board.
6480    async fn copied_from(
6481        &self,
6482        origin: &str,
6483        kind: BoardKind,
6484    ) -> Result<Vec<Resolved>, SourceError> {
6485        let wanted = TaskQuery {
6486            origin: Some(origin.to_owned()),
6487            ..TaskQuery::default()
6488        };
6489        Ok(self
6490            .narrowed(&wanted)
6491            .await?
6492            .unwrap_or_default()
6493            .into_iter()
6494            .filter(|item| item.kind == kind && wanted.origin_matches(&item.metadata()))
6495            .collect())
6496    }
6497
6498    /// Every item of this board that may carry `origin` — a superset of those that do — found
6499    /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
6500    ///
6501    /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
6502    /// which reads the field every carrier holds, whichever release wrote it — and the
6503    /// board-scoped issue search for the same id as a phrase in the body, where this source
6504    /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
6505    /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
6506    /// query's, exactly.
6507    ///
6508    /// Both connections are walked to exhaustion, each from its own cursor. One that has
6509    /// already ended is sent its last cursor again, which answers an empty page, so the one
6510    /// document serves every page of either. What the two leave is stated in the module
6511    /// documentation: a carrier another process added within the last second or two, before
6512    /// either index has it.
6513    async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
6514        let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
6515        let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
6516        let mut items_after: Option<String> = None;
6517        let mut search_after: Option<String> = None;
6518        let mut found: Vec<Resolved> = Vec::new();
6519        let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
6520            if !found.iter().any(|held| held.id == resolved.id) {
6521                found.push(resolved);
6522            }
6523        };
6524        loop {
6525            let data = self
6526                .graphql(
6527                    graphql::ORIGIN_LOOKUP,
6528                    json!({"owner":self.owner,"number":self.project_number,"filter":filter,
6529                           "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
6530                           "itemsAfter":items_after,"searchAfter":search_after,
6531                           "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
6532                           "duplicates":true}),
6533                )
6534                .await?;
6535            let items = data
6536                .pointer("/originItems/projectV2/items")
6537                .filter(|value| !value.is_null())
6538                .ok_or_else(|| SourceError::Refused {
6539                    message: format!(
6540                        "GitHub project {}/{} was not found or is not visible to the token",
6541                        self.owner, self.project_number
6542                    ),
6543                })?;
6544            for item in optional_nodes(Some(items), "project items")?
6545                .into_iter()
6546                .flatten()
6547            {
6548                // The board's own items list its drafts too, and a draft is not an issue: no
6549                // narrowed read answers with one, whatever its origin field holds.
6550                if let Some(resolved) = self.resolve(item)?
6551                    && resolved.content_kind == ContentKind::Issue
6552                {
6553                    keep(resolved, &mut found);
6554                }
6555            }
6556            let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
6557                message: "GitHub search response has no search connection".into(),
6558            })?;
6559            for node in optional_nodes(Some(searched), "search")?
6560                .into_iter()
6561                .flatten()
6562            {
6563                if let Some(resolved) = self.resolve_issue(node).await? {
6564                    keep(resolved, &mut found);
6565                }
6566            }
6567            let items_next = resumed(items, items_after.as_deref())?;
6568            let search_next = resumed(searched, search_after.as_deref())?;
6569            if !items_next.has_more() && !search_next.has_more() {
6570                return Ok(found);
6571            }
6572            items_after = items_next.cursor();
6573            search_after = search_next.cursor();
6574        }
6575    }
6576
6577    /// `found`, with every item this process created or wrote in its place, and every one of
6578    /// them the read did not report added.
6579    ///
6580    /// This process's own record wins over the read's copy of the same item, because a read
6581    /// of an item written moments ago can still be behind what was written onto it — the
6582    /// origin field included, which is the one a narrowed read is confirmed against — and a
6583    /// read that still names an item under a predicate this process's write moved it out of
6584    /// must not return it. The one thing the read knows that the record cannot is when GitHub
6585    /// last saw the item change, which is what a comment-activity read rules a candidate out
6586    /// by, so the read's `updatedAt` is kept when the record has none of its own. See
6587    /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
6588    fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
6589        // A board draft is not an issue, so no narrowed read returns one, and this process
6590        // having written one does not make it an answer either.
6591        let own: Vec<Resolved> = self
6592            .created()?
6593            .iter()
6594            .chain(self.updated()?.iter())
6595            .filter(|own| own.content_kind == ContentKind::Issue)
6596            .cloned()
6597            .collect();
6598        for mut own in own {
6599            self.resolved_cache()?.insert(own.id.clone(), own.clone());
6600            match found.iter_mut().find(|read| read.id == own.id) {
6601                Some(read) => {
6602                    own.updated_at = own.updated_at.max(read.updated_at);
6603                    *read = own;
6604                }
6605                None => found.push(own),
6606            }
6607        }
6608        Ok(found)
6609    }
6610
6611    /// Whether `item` has a comment created or last edited at or after `since` — always, when
6612    /// there is no instant to hold it to.
6613    ///
6614    /// The candidate's own `updatedAt` is read first, because a comment written or edited at
6615    /// or after the instant moved it there: an issue not updated since holds no such comment,
6616    /// and its comments are never asked for — unless this process commented on it in this
6617    /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
6618    /// only as far as the first that matches. A board draft is not an issue and has no
6619    /// comments, so it never matches.
6620    async fn commented_since(
6621        &self,
6622        item: &Resolved,
6623        since: Option<DateTime<Utc>>,
6624    ) -> Result<bool, SourceError> {
6625        let Some(since) = since else {
6626            return Ok(true);
6627        };
6628        if item.content_kind == ContentKind::DraftIssue {
6629            return Ok(false);
6630        }
6631        // An `updatedAt` this process's own record or a lagging index holds can predate a
6632        // comment this process wrote since, so only an issue it did not comment on is ruled
6633        // out by one.
6634        if item.updated_at.is_some_and(|updated| updated < since)
6635            && !self.commented()?.contains(&item.id)
6636        {
6637            return Ok(false);
6638        }
6639        let query = TaskQuery {
6640            commented_since: Some(since),
6641            ..TaskQuery::default()
6642        };
6643        let mut after: Option<String> = None;
6644        loop {
6645            let data = self
6646                .graphql(
6647                    graphql::ISSUE_COMMENTS,
6648                    json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
6649                )
6650                .await?;
6651            let Some(connection) = data
6652                .get("node")
6653                .filter(|value| !value.is_null())
6654                .and_then(|node| node.get("comments"))
6655                .filter(|value| !value.is_null())
6656            else {
6657                // Removed since the search reported it: no longer an issue with comments.
6658                return Ok(false);
6659            };
6660            let comments = optional_nodes(Some(connection), "issue comments")?
6661                .into_iter()
6662                .flatten()
6663                .map(comment_from)
6664                .collect::<Result<Vec<_>, _>>()?;
6665            if query.comments_match(&comments) {
6666                return Ok(true);
6667            }
6668            match next_cursor(connection)? {
6669                Some(next) => {
6670                    validate_cursor_progress(after.as_deref(), &next.0)?;
6671                    after = Some(next.0);
6672                }
6673                None => return Ok(false),
6674            }
6675        }
6676    }
6677
6678    /// Every item on the board: the union of both enumerations GitHub offers of one.
6679    ///
6680    /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
6681    /// board **draft** and reads the board's own fields beside its items, and only the search
6682    /// reports an item that connection is behind on. The module documentation is where the lag and the
6683    /// measurements behind it are written down.
6684    ///
6685    /// A search result is admitted on the same terms as any other issue this source reaches
6686    /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
6687    /// names *this* board — so an issue the index still believes is here after it was taken
6688    /// off is refused rather than reported.
6689    ///
6690    /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
6691    /// which is what the cache could otherwise have broken.
6692    async fn board(&self) -> Result<Board, SourceError> {
6693        self.board_completed_by(None).await
6694    }
6695
6696    /// [`Self::board`], its search half narrowed by `also` when it is given — which an issue
6697    /// that half could add is then answered only if it holds.
6698    async fn board_completed_by(&self, also: Option<&str>) -> Result<Board, SourceError> {
6699        let cached = self.board_cache()?.clone();
6700        let mut board = match cached {
6701            Some(board) => board,
6702            None => {
6703                let read = self.read_board().await?;
6704                *self.board_cache()? = Some(read.clone());
6705                read
6706            }
6707        };
6708        let searched = match also {
6709            None => self.searched_issues().await?,
6710            Some(also) => self.searched_narrowly(also).await?,
6711        };
6712        for held in searched {
6713            if !board.items.iter().any(|item| item.id == held.id) {
6714                board.items.push(held);
6715            }
6716        }
6717        for own in self.created()?.iter() {
6718            if !board.items.iter().any(|item| item.id == own.id) {
6719                board.items.push(own.clone());
6720            }
6721        }
6722        Ok(board)
6723    }
6724
6725    /// This process's own view of the board, or the refusal a poisoned lock is.
6726    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
6727        self.board_cache
6728            .lock()
6729            .map_err(|_| SourceError::Unavailable {
6730                message: "this source's view of the board was left inconsistent by an earlier \
6731                      failure; next: run the command again"
6732                    .into(),
6733            })
6734    }
6735
6736    /// Bring this process's own view of the board up to an item it has just written.
6737    ///
6738    /// A created item goes to `created`, which is what completes a board read GitHub's own
6739    /// eventual consistency has left behind. An item that was already there is replaced
6740    /// where it sits, so a second write of it in the same command reads its real parent
6741    /// rather than the one it had before the first write.
6742    ///
6743    /// "Where it sits" is three places, and missing an earlier one leaves a stale record
6744    /// that wins: an item this same run created is held in `created` and not in the cached
6745    /// board, and `board` completes the cached board *from* `created`, so replacing only
6746    /// the cached copy of such an item replaces nothing and the read still reports the
6747    /// title it was created with. The search is the third, and it is the one an item the
6748    /// board's own projection is behind on sits in *alone* — which is exactly the item this
6749    /// source is least able to re-read, so leaving it out would put the stale title back on
6750    /// the only items the completion in [`Self::board`] exists for.
6751    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
6752        self.resolved_cache()?.insert(item.id.clone(), item.clone());
6753        if created {
6754            self.created()?.push(item);
6755            return Ok(());
6756        }
6757        {
6758            let mut own = self.created()?;
6759            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
6760                *held = item;
6761                return Ok(());
6762            }
6763        }
6764        {
6765            let mut own = self.updated()?;
6766            match own.iter_mut().find(|held| held.id == item.id) {
6767                Some(held) => *held = item.clone(),
6768                None => own.push(item.clone()),
6769            }
6770        }
6771        if let Some(board) = self.board_cache()?.as_mut()
6772            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
6773        {
6774            *held = item.clone();
6775        }
6776        if let Some(found) = self.search_cache()?.as_mut()
6777            && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
6778        {
6779            *held = item.clone();
6780        }
6781        for found in self.narrowed_cache()?.values_mut() {
6782            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
6783                *held = item.clone();
6784            }
6785        }
6786        for (_, found) in self.children_cache()?.values_mut() {
6787            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
6788                *held = item.clone();
6789            }
6790        }
6791        Ok(())
6792    }
6793
6794    /// Forget one item this process has just deleted, from every half of its own view.
6795    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
6796        if let Some(Deferred::Held { created, .. }) = self.deferred()?.as_mut() {
6797            created.retain(|held| held.content != *id);
6798        }
6799        self.resolved_cache()?.remove(id);
6800        self.read_ahead()?.remove(id);
6801        self.carried()?.remove(id);
6802        self.created()?.retain(|own| own.id != *id);
6803        self.updated()?.retain(|own| own.id != *id);
6804        self.commented()?.retain(|own| own != id);
6805        if let Some(board) = self.board_cache()?.as_mut() {
6806            board.items.retain(|item| item.id != *id);
6807        }
6808        if let Some(found) = self.search_cache()?.as_mut() {
6809            found.retain(|item| item.id != *id);
6810        }
6811        for found in self.narrowed_cache()?.values_mut() {
6812            found.retain(|item| item.id != *id);
6813        }
6814        for (_, found) in self.children_cache()?.values_mut() {
6815            found.retain(|item| item.id != *id);
6816        }
6817        Ok(())
6818    }
6819
6820    /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
6821    fn narrowed_cache(
6822        &self,
6823    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
6824        self.narrowed_cache
6825            .lock()
6826            .map_err(|_| SourceError::Unavailable {
6827                message: "this source's view of a narrowed read was left inconsistent by an \
6828                      earlier failure; next: run the command again"
6829                    .into(),
6830            })
6831    }
6832
6833    /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
6834    /// is.
6835    /// The tasks `project` holds, as a copy's batched read found them this command, when it did.
6836    fn held_task_children(&self, project: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
6837        Ok(self.task_children()?.get(project).cloned())
6838    }
6839
6840    fn task_children(
6841        &self,
6842    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Vec<Resolved>>>, SourceError> {
6843        self.task_children
6844            .lock()
6845            .map_err(|_| SourceError::Unavailable {
6846                message: "this source's record of a project's tasks was left inconsistent by an \
6847                          earlier failure; next: run the command again"
6848                    .into(),
6849            })
6850    }
6851
6852    fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
6853        self.children_cache
6854            .lock()
6855            .map_err(|_| SourceError::Unavailable {
6856                message: "this source's view of a project's tasks was left inconsistent by an \
6857                      earlier failure; next: run the command again"
6858                    .into(),
6859            })
6860    }
6861
6862    /// Every page of the board, read from GitHub.
6863    async fn read_board(&self) -> Result<Board, SourceError> {
6864        let mut after: Option<String> = None;
6865        let mut items = Vec::new();
6866        let mut board;
6867        loop {
6868            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
6869            for item in page
6870                .pointer("/items/nodes")
6871                .and_then(Value::as_array)
6872                .ok_or_else(|| SourceError::Malformed {
6873                    message: "GitHub project items.nodes is not an array".into(),
6874                })?
6875            {
6876                if let Some(resolved) = self.resolve(item)? {
6877                    items.push(resolved);
6878                }
6879            }
6880            let info = page
6881                .pointer("/items/pageInfo")
6882                .ok_or_else(|| SourceError::Malformed {
6883                    message: "GitHub project items have no pageInfo".into(),
6884                })?;
6885            let has_next = required_bool(info, "hasNextPage")?;
6886            let next = has_next
6887                .then(|| required_str(info, "endCursor"))
6888                .transpose()?;
6889            board = page.clone();
6890            match next {
6891                Some(next) => {
6892                    validate_cursor_progress(after.as_deref(), next)?;
6893                    after = Some(next.to_owned());
6894                }
6895                None => break,
6896            }
6897        }
6898        Ok(Board {
6899            id: required_str(&board, "id")?.to_owned(),
6900            fields: board.get("fields").cloned().unwrap_or(Value::Null),
6901            items,
6902        })
6903    }
6904
6905    /// The existing items this source has written, for completing a narrowed read that is
6906    /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
6907    fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
6908        self.updated.lock().map_err(|_| SourceError::Unavailable {
6909            message: "this source's record of what it wrote in this run was left inconsistent \
6910                      by an earlier failure; next: run the command again"
6911                .into(),
6912        })
6913    }
6914
6915    /// The issues this source has commented on in this command; see
6916    /// [`Self::commented`](GitHubProjectsSource::commented).
6917    fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
6918        self.commented.lock().map_err(|_| SourceError::Unavailable {
6919            message: "this source's record of what it commented on in this run was left \
6920                      inconsistent by an earlier failure; next: run the command again"
6921                .into(),
6922        })
6923    }
6924
6925    /// Called only once GitHub has answered the comment write, so an issue whose comment
6926    /// failed is never made a candidate a later read would pay a node read for.
6927    fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
6928        let mut commented = self.commented()?;
6929        if !commented.contains(issue) {
6930            commented.push(issue.clone());
6931        }
6932        Ok(())
6933    }
6934
6935    /// The items this source has created, for completing a board read that is behind.
6936    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
6937        self.created.lock().map_err(|_| SourceError::Unavailable {
6938            message: "this source's record of what it created in this run was left \
6939                      inconsistent by an earlier failure; next: run the command again"
6940                .into(),
6941        })
6942    }
6943
6944    /// One board item as this source reports it, or `None` for content it ignores.
6945    ///
6946    /// A pull request is neither a project nor a task — it is somebody's change, not a
6947    /// unit of plan — and an item whose content the token cannot see has nothing to
6948    /// report at all.
6949    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
6950        let resolved = self.resolve_unremembered(item)?;
6951        if let Some(resolved) = &resolved {
6952            self.resolved_cache()?
6953                .insert(resolved.id.clone(), resolved.clone());
6954        }
6955        Ok(resolved)
6956    }
6957
6958    /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
6959    /// write's own response needs, which says nothing this process should answer a later read
6960    /// with.
6961    fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
6962        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
6963            message: "GitHub project item is missing content".into(),
6964        })?;
6965        if content.is_null() {
6966            return Ok(None);
6967        }
6968        let content_kind = match required_str(content, "__typename")? {
6969            "Issue" => ContentKind::Issue,
6970            "DraftIssue" => ContentKind::DraftIssue,
6971            _ => return Ok(None),
6972        };
6973        let field_values = item
6974            .get("fieldValues")
6975            .ok_or_else(|| SourceError::Malformed {
6976                message: "GitHub project item is missing fieldValues".into(),
6977            })?;
6978        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
6979        let nodes = field_values
6980            .get("nodes")
6981            .and_then(Value::as_array)
6982            .ok_or_else(|| SourceError::Malformed {
6983                message: "GitHub project item fieldValues.nodes is not an array".into(),
6984            })?;
6985        if let Some(labels) = content.get("labels") {
6986            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
6987        }
6988        let raw_body = optional_str(content, "body")?.map(str::to_owned);
6989        let (body, slot) = metadata_body(raw_body.clone())?;
6990        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
6991            .map(|id| NativeId(id.to_owned()));
6992        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
6993        // to read one from; it is a task, and never a project.
6994        let sub_issues = match content_kind {
6995            ContentKind::Issue => sub_issue_total(content)?,
6996            ContentKind::DraftIssue => 0,
6997        };
6998        let content_id = required_str(content, "id")?;
6999        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7000            message: format!("GitHub issue {content_id}: {message}"),
7001        })?;
7002        let raw_title = required_str(content, "title")?;
7003        let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
7004        // The title a person wrote, which for a document is the one without the prefix —
7005        // the same way `content` above is the body without this source's metadata slot.
7006        let title = match kind {
7007            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
7008            BoardKind::Work(_) => raw_title.to_owned(),
7009        };
7010        let own_repository = content
7011            .pointer("/repository/nameWithOwner")
7012            .and_then(Value::as_str)
7013            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
7014            .transpose()
7015            .map_err(|message| SourceError::Malformed { message })?;
7016        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
7017            Repository::from_metadata(&slot)
7018                .map_err(|message| SourceError::Malformed { message })?
7019        } else {
7020            own_repository.clone().into_iter().collect()
7021        };
7022        let classification = Classification::from_metadata(&slot)
7023            .map_err(|message| SourceError::Malformed { message })?;
7024        let id = NativeId(content_id.to_owned());
7025        // Read only for a task, because only a task has either list: a project or a
7026        // document holding one of these keys holds nothing this source reports, and the
7027        // keys are left out of its caller-visible metadata all the same.
7028        let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
7029            let listed = |key: &str| {
7030                TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
7031                    .map_err(|message| SourceError::Malformed { message })
7032            };
7033            (
7034                listed(TaskRef::DELIVERS_KEY)?,
7035                listed(TaskRef::DELIVERED_BY_KEY)?,
7036            )
7037        } else {
7038            (Vec::new(), Vec::new())
7039        };
7040        let (option, closed, reason) = Self::status_parts(nodes, content)?;
7041        let priority = self.held_priority(nodes)?;
7042        let item_labels = labels(content)?;
7043        // Present when the item was reached through its own issue, whose board entry
7044        // names the board; a read of the board's own items has the board already. An
7045        // empty id names nothing a field write could address, so it is read as absent and
7046        // the write goes back to reading the board.
7047        let board_id = item
7048            .pointer("/project/id")
7049            .and_then(Value::as_str)
7050            .filter(|id| !id.is_empty());
7051        let resolved = Resolved {
7052            item_id: required_str(item, "id")?.to_owned(),
7053            id,
7054            content_kind,
7055            kind,
7056            title,
7057            body: body.filter(|value| !value.is_empty()),
7058            raw_body,
7059            status: self
7060                .statuses
7061                .status(kind.status_kind(), option, closed, reason),
7062            option: option.map(str::to_owned),
7063            priority,
7064            closed,
7065            reason: closed.then(|| reason.map(str::to_owned)).flatten(),
7066            delivers,
7067            delivered_by,
7068            labels: item_labels.clone(),
7069            parent,
7070            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
7071            projected: self.held_projections(nodes)?,
7072            number: match content_kind {
7073                ContentKind::Issue => Some(issue_number(content)?),
7074                // A draft is filed in no repository, so nothing ever numbered it:
7075                // `DraftIssue` declares no `number` at all, exactly as it declares no
7076                // `subIssuesSummary` the branch above reads.
7077                ContentKind::DraftIssue => None,
7078            },
7079            url: optional_str(content, "url")?.map(str::to_owned),
7080            created_at: optional_time(content, "createdAt")?,
7081            updated_at: optional_time(content, "updatedAt")?,
7082            item_updated_at: optional_time(item, "updatedAt")?,
7083            comment_count: content
7084                .pointer("/commentCount/totalCount")
7085                .and_then(Value::as_u64),
7086            relationship_events: content
7087                .pointer("/relationshipEvents/totalCount")
7088                .and_then(Value::as_u64),
7089            // The issue node is stamped wherever it was read; a board item a read of the issue
7090            // put together from its memberships is not.
7091            served_at: match optional_time(content, SERVED_AT)? {
7092                Some(served) => Some(served),
7093                None => optional_time(item, SERVED_AT)?,
7094            },
7095            // Both connections were read to their end above — one reporting a further page is
7096            // refused — so these are every field value and every label the item holds. A
7097            // response that did not select the labels at all says nothing of them, and gives
7098            // no facets.
7099            facets: content.get("labels").is_some().then(|| {
7100                change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
7101            }),
7102            own_repository,
7103            repositories,
7104            classification,
7105            slot,
7106            board_id: board_id.map(str::to_owned),
7107            fields: field_definitions(nodes),
7108            board_fields: Self::carried_board_fields(content, board_id)?,
7109            blocked_by: carried_blocked_by(content)?,
7110        };
7111        Ok(Some(resolved))
7112    }
7113
7114    /// The field definitions of the board `board_id` names — the project this issue's own
7115    /// board item is on — off the `boards` page a read of an issue by its own id carries, or
7116    /// `None` when the read carried none, carried no entry for that board, or the board item
7117    /// named no board, which a write then answers by reading the board's fields itself.
7118    ///
7119    /// Matched by the board's node id and never by its number alone: a project number is
7120    /// unique only within its owner, so another owner's board numbered alike can sit on the
7121    /// same page, and its field and option ids address nothing on this one.
7122    fn carried_board_fields(
7123        content: &Value,
7124        board_id: Option<&str>,
7125    ) -> Result<Option<Value>, SourceError> {
7126        let (Some(nodes), Some(board_id)) = (
7127            content.pointer("/boards/nodes").and_then(Value::as_array),
7128            board_id,
7129        ) else {
7130            return Ok(None);
7131        };
7132        let Some(board) = nodes.iter().find_map(|node| {
7133            let project = node.get("project")?;
7134            (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
7135        }) else {
7136            return Ok(None);
7137        };
7138        let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
7139            return Ok(None);
7140        };
7141        complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
7142        Ok(Some(fields.clone()))
7143    }
7144
7145    /// What one board item's `Priority` field says, through this instance's mapping.
7146    ///
7147    /// An instance with no mapping holds no priority, so every item reads as `none` whatever
7148    /// its board holds. With one, no value is `none`, a mapped option is its level, and an
7149    /// option the mapping does not name is kept as itself — never read as a level or as
7150    /// `none` — for a read of the task to report by name.
7151    fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
7152        let Some(mapping) = &self.priorities else {
7153            return Ok(HeldPriority::Read(Priority::None));
7154        };
7155        // A value of the field that names no option — a text field someone called `Priority` —
7156        // is malformed rather than `none`: reading it as no priority would let the next copy
7157        // clear one a person set.
7158        let Some(option) = field_values
7159            .iter()
7160            .find(|value| {
7161                value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
7162            })
7163            .map(|value| required_str(value, "name"))
7164            .transpose()?
7165        else {
7166            return Ok(HeldPriority::Read(Priority::None));
7167        };
7168        Ok(mapping.priority_of(option).map_or_else(
7169            || HeldPriority::Unmapped(option.to_owned()),
7170            HeldPriority::Read,
7171        ))
7172    }
7173
7174    /// What each projected metadata field holds for one board item, by field name — every text
7175    /// value GitHub answers, an empty one included.
7176    fn held_projections(
7177        &self,
7178        field_values: &[Value],
7179    ) -> Result<BTreeMap<String, String>, SourceError> {
7180        let mut held = BTreeMap::new();
7181        for projection in &self.metadata_fields {
7182            if let Some(text) = text_field(field_values, &projection.field)? {
7183                held.insert(projection.field.to_string(), text);
7184            }
7185        }
7186        Ok(held)
7187    }
7188
7189    /// What one board item's status is read from: its `Status` option, whether its issue
7190    /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
7191    /// three into the status it reports.
7192    fn status_parts<'a>(
7193        field_values: &'a [Value],
7194        content: &'a Value,
7195    ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
7196        let option = field_values
7197            .iter()
7198            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
7199            .map(|value| required_str(value, "name"))
7200            .transpose()?;
7201        let closed = optional_str(content, "state")? == Some("CLOSED");
7202        Ok((option, closed, optional_str(content, "stateReason")?))
7203    }
7204
7205    /// The board Status option this write selects, or the refusal that says why not.
7206    ///
7207    /// The mapped option is required for both open and terminal targets. A terminal write
7208    /// validates it before changing either representation, so it can never fall back to
7209    /// closing an issue whose board cannot display the matching status.
7210    ///
7211    /// Answers the field's id, the option's id, and the option's name as the board spells
7212    /// it — which is the name a read of the item reports once it sits there.
7213    fn column_for(
7214        &self,
7215        fields: &Value,
7216        kind: ItemKind,
7217        category: StatusCategory,
7218        target: &StatusTarget,
7219    ) -> Result<Option<(String, String, String)>, SourceError> {
7220        let Some(wanted) = target.option() else {
7221            return Ok(None);
7222        };
7223        let missing = |detail: &str| SourceError::Refused {
7224            message: format!(
7225                "{} status {} of source {} needs the board Status option {wanted:?}, and \
7226                 {detail}; next: add that option to the board, which `onetaskgraph sources \
7227                 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
7228                 it has",
7229                kind.marker(),
7230                category_name(category),
7231                self.name,
7232                self.name,
7233                category_name(category),
7234                kind.marker()
7235            ),
7236        };
7237        let Some(field) = Board::field(fields, "Status")? else {
7238            return Err(missing("this board has no Status field"));
7239        };
7240        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7241            return Err(missing(
7242                "this board's Status field is not a single-select field",
7243            ));
7244        }
7245        let option = field
7246            .get("options")
7247            .and_then(Value::as_array)
7248            .and_then(|options| {
7249                options.iter().find(|option| {
7250                    option
7251                        .get("name")
7252                        .and_then(Value::as_str)
7253                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7254                })
7255            });
7256        match option {
7257            None => Err(missing("this board does not have it")),
7258            Some(option) => Ok(Some((
7259                required_str(field, "id")?.to_owned(),
7260                required_str(option, "id")?.to_owned(),
7261                required_str(option, "name")?.to_owned(),
7262            ))),
7263        }
7264    }
7265
7266    /// The refusal a status that closes an issue is answered with over a board draft.
7267    fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
7268        SourceError::Refused {
7269            message: format!(
7270                "status {} of source {} closes the item's issue, and GitHub draft items have \
7271                 no open or closed state",
7272                category_name(category),
7273                self.name
7274            ),
7275        }
7276    }
7277
7278    /// What a status write to one item needs of the board: the board's id and the
7279    /// definition of its `Status` field, read off the item when the item says both.
7280    ///
7281    /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
7282    /// and its `Status` value carries that field's definition, options and all. An item that
7283    /// does not say — no board id, or no `Status` value to read the field off — takes them
7284    /// from [`Self::board_fields`], which reads no item.
7285    async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7286        if let Some(board) = item.carried_board() {
7287            return Ok(board);
7288        }
7289        if item.defines("Status")
7290            && let Some(board_id) = item.named_board()
7291        {
7292            return Ok(BoardFields {
7293                id: board_id,
7294                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7295            });
7296        }
7297        self.board_fields().await
7298    }
7299
7300    /// Set one task's or one project's status and nothing else; see
7301    /// [`TaskSource::set_task_status`] and [`TaskSource::set_project_status`].
7302    async fn set_status(
7303        &self,
7304        id: &NativeId,
7305        kind: ItemKind,
7306        category: StatusCategory,
7307    ) -> Result<Option<Status>, SourceError> {
7308        // Refused before anything is read, in the words a write of the same status is.
7309        let target = self.resolved_target(kind, category)?;
7310        // An item read by issue reference carries only what this write needs, so what it
7311        // becomes is never remembered for a later read to be answered from.
7312        let held = self.take_held(id)?;
7313        let by_reference = held.is_some();
7314        let bound = match held {
7315            Some(item) => Some(item),
7316            None => self.bound_item(id).await?,
7317        };
7318        let Some(mut item) = bound.filter(|item| item.kind == BoardKind::Work(kind)) else {
7319            return Ok(None);
7320        };
7321        let board = self.status_board(&item).await?;
7322        let (field, option, name) = self
7323            .column_for(&board.fields, kind, category, &target)?
7324            .ok_or_else(|| SourceError::Malformed {
7325                message: format!(
7326                    "status {} of source {} names no board Status option",
7327                    category_name(category),
7328                    self.name
7329                ),
7330            })?;
7331        if item.status.category == category && item.option.as_deref() == Some(&name) {
7332            return Ok(Some(item.status));
7333        }
7334        match &target {
7335            StatusTarget::Terminal(_, reason) => {
7336                if item.content_kind == ContentKind::DraftIssue {
7337                    return Err(self.closes_a_draft(category));
7338                }
7339                // The option first, so a refused selection leaves an open issue open rather
7340                // than closed behind a card that still reads open.
7341                self.set_item_field(
7342                    board.id.as_str(),
7343                    &item.item_id,
7344                    &field,
7345                    json!({"singleSelectOptionId": option}),
7346                )
7347                .await?;
7348                // An issue already closed in a way that reads as this category keeps its state
7349                // and its reason: only its card was out of line, and closing it again would
7350                // overwrite a reason — a duplicate's — that says more than this write does.
7351                let already = item.closed && item.status.category == category;
7352                if !already {
7353                    self.update_content(
7354                        ContentKind::Issue,
7355                        &item.id,
7356                        json!({"stateInput": state_input(Some(&target))}),
7357                    )
7358                    .await?;
7359                }
7360                let reason = if already {
7361                    item.reason.clone()
7362                } else {
7363                    Some(reason.reason().to_owned())
7364                };
7365                item.closed = true;
7366                item.status = self
7367                    .statuses
7368                    .status(kind, Some(&name), true, reason.as_deref());
7369                item.option = Some(name);
7370                item.reason = reason;
7371            }
7372            StatusTarget::Column(_) => {
7373                // An option is what an open item's status is, so a closed issue is reopened
7374                // first — sitting closed in the column, it would read back as closed. A draft has
7375                // no state to reopen.
7376                if item.content_kind == ContentKind::Issue && item.closed {
7377                    self.update_content(
7378                        ContentKind::Issue,
7379                        &item.id,
7380                        json!({"stateInput": state_input(Some(&target))}),
7381                    )
7382                    .await?;
7383                    item.closed = false;
7384                }
7385                self.set_item_field(
7386                    board.id.as_str(),
7387                    &item.item_id,
7388                    &field,
7389                    json!({"singleSelectOptionId": option}),
7390                )
7391                .await?;
7392                item.status = self.statuses.status(kind, Some(&name), false, None);
7393                item.option = Some(name);
7394            }
7395            StatusTarget::Disabled(_) => {
7396                unreachable!("resolved_target refused a disabled status")
7397            }
7398        }
7399        let status = item.status.clone();
7400        if !by_reference {
7401            self.remember_written(item, false)?;
7402        }
7403        Ok(Some(status))
7404    }
7405
7406    /// Replace one task's `delivered_by` and nothing else; see
7407    /// [`TaskSource::set_delivered_by`].
7408    ///
7409    /// One update of the body, which differs from the body GitHub holds only inside the
7410    /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
7411    async fn replace_delivered_by(
7412        &self,
7413        id: &NativeId,
7414        delivered_by: &[TaskRef],
7415    ) -> Result<Option<()>, SourceError> {
7416        let entries = TaskRef::listed(
7417            TaskRef::DELIVERED_BY_KEY,
7418            id,
7419            Some(&self.name),
7420            delivered_by.to_vec(),
7421        )
7422        .map_err(|message| SourceError::Refused { message })?;
7423        let Some(mut item) = self
7424            .bound_item(id)
7425            .await?
7426            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7427        else {
7428            return Ok(None);
7429        };
7430        let mut slot = item.slot.clone();
7431        set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
7432        self.write_slot(&mut item, &slot).await?;
7433        item.delivered_by = entries;
7434        self.remember_written(item, false)?;
7435        Ok(Some(()))
7436    }
7437
7438    /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
7439    /// see [`TaskSource::set_task_metadata`].
7440    ///
7441    /// `None` when this board holds no item by that id, or holds one of another kind. The
7442    /// answer is the item as this source now reads it, so what a caller is told the key
7443    /// holds is what the slot holds.
7444    ///
7445    /// A key already holding the value is answered without a write, compared as JSON rather
7446    /// than as the body's bytes: a slot a person spelled with other whitespace would
7447    /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
7448    async fn set_slot_key(
7449        &self,
7450        id: &NativeId,
7451        kind: BoardKind,
7452        key: &MetadataKey,
7453        value: &Value,
7454    ) -> Result<Option<Resolved>, SourceError> {
7455        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7456            return Ok(None);
7457        };
7458        if item.slot.get(key.as_str()) == Some(value) {
7459            return Ok(Some(item));
7460        }
7461        let mut slot = item.slot.clone();
7462        slot.insert(key.as_str().to_owned(), value.clone());
7463        // A key a board text field projects moves that field first, so the body — the value's
7464        // one authoritative home — is written last, as every write of an existing item is.
7465        if self
7466            .metadata_fields
7467            .iter()
7468            .any(|projection| &*projection.key == key.as_str())
7469        {
7470            let board = self.projection_board(&item).await?;
7471            let (writes, projected) = self.projection_writes(
7472                &board.fields,
7473                Some(&item.projected),
7474                &slot,
7475                Some(&[key.as_str()]),
7476            )?;
7477            let (mut values, mut clears) = (Vec::new(), Vec::new());
7478            for write in &writes {
7479                write.join(&mut values, &mut clears);
7480            }
7481            self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
7482                .await?;
7483            item.projected = projected;
7484        }
7485        self.write_slot(&mut item, &slot).await?;
7486        self.remember_written(item.clone(), false)?;
7487        Ok(Some(item))
7488    }
7489
7490    /// What a projection write to `item` needs of the board, read off the item when it says
7491    /// enough, as [`Self::fields_for`] reads it.
7492    async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
7493        if let Some(board) = item.carried_board() {
7494            return Ok(board);
7495        }
7496        if let Some(board_id) = item.named_board()
7497            && self
7498                .metadata_fields
7499                .iter()
7500                .all(|projection| item.defines(&projection.field))
7501        {
7502            return Ok(BoardFields {
7503                id: board_id,
7504                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7505            });
7506        }
7507        self.board_fields().await
7508    }
7509
7510    /// Put `slot` in one item's metadata slot with a single update of its body, and bring
7511    /// `item` up to what that write left.
7512    ///
7513    /// The body sent differs from the body GitHub holds only inside the slot — see
7514    /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
7515    /// the mutation the item's content takes, so a board draft's body is written with
7516    /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
7517    async fn write_slot(
7518        &self,
7519        item: &mut Resolved,
7520        slot: &BTreeMap<String, Value>,
7521    ) -> Result<(), SourceError> {
7522        let held = item.raw_body.clone().unwrap_or_default();
7523        let body = with_slot(&held, slot)?;
7524        if body != held {
7525            self.update_content(item.content_kind, &item.id, json!({"body": body}))
7526                .await?;
7527        }
7528        let (visible, slot) = metadata_body(Some(body.clone()))?;
7529        item.body = visible.filter(|value| !value.is_empty());
7530        item.raw_body = Some(body);
7531        item.slot = slot;
7532        Ok(())
7533    }
7534
7535    /// This instance's target for a category written to an item of `kind`, refusing one
7536    /// that kind has no option for — before anything is read or written.
7537    ///
7538    /// Nothing here mutates the board's option set to make room for a status. GitHub
7539    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
7540    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
7541    /// field and every item's status.
7542    fn resolved_target(
7543        &self,
7544        kind: ItemKind,
7545        category: StatusCategory,
7546    ) -> Result<StatusTarget, SourceError> {
7547        let target = self.statuses.target(kind, category).clone();
7548        let StatusTarget::Disabled(why) = target else {
7549            return Ok(target);
7550        };
7551        let refusal = why.refusal(&self.name, category, kind);
7552        // Why there is no shipped default, which is the question a person meeting this
7553        // refusal on a source that never mentioned the category asks.
7554        let shipped_none = match category {
7555            StatusCategory::Draft => Some(
7556                "draft has no shipped default because GitHub draft issues cannot have \
7557                 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
7558            ),
7559            StatusCategory::Unknown => Some(
7560                "unknown has no shipped default because this board keeps no open-ended status \
7561                 word: every word classified unknown is written to the one board Status option \
7562                 status_mapping.unknown names",
7563            ),
7564            _ => None,
7565        };
7566        Err(match (refusal, shipped_none, why) {
7567            (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
7568                SourceError::Refused {
7569                    message: format!("{message}; {note}"),
7570                }
7571            }
7572            (refusal, _, _) => refusal,
7573        })
7574    }
7575
7576    /// What writing `priority` does to one item's `Priority` field on this board, or the
7577    /// refusal naming what the board lacks.
7578    ///
7579    /// `none` is no value, so it clears the field — and asks nothing of an item that holds
7580    /// none already, or of an item not created yet. Every other priority selects the option
7581    /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
7582    /// without that option, is refused rather than given one: reads and writes never create
7583    /// a field or an option.
7584    fn priority_write(
7585        &self,
7586        fields: &Value,
7587        existing: Option<&Resolved>,
7588        priority: Priority,
7589    ) -> Result<Option<PriorityWrite>, SourceError> {
7590        let Some(mapping) = &self.priorities else {
7591            return Err(self.holds_no_priority());
7592        };
7593        let Some(wanted) = mapping.option(priority) else {
7594            if !existing.is_some_and(Resolved::holds_priority) {
7595                return Ok(None);
7596            }
7597            let field =
7598                Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
7599                    message: format!(
7600                        "an item holding a {PRIORITY_FIELD} value was read without that field"
7601                    ),
7602                })?;
7603            return Ok(Some(PriorityWrite::Clear {
7604                field: required_str(field, "id")?.to_owned(),
7605            }));
7606        };
7607        let missing = |detail: &str| SourceError::Refused {
7608            message: format!(
7609                "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
7610                 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
7611                 it, or point priority_mapping.{priority} of this source at an option the board \
7612                 has",
7613                self.name, self.name
7614            ),
7615        };
7616        let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
7617            return Err(missing(&format!(
7618                "this board has no {PRIORITY_FIELD} field"
7619            )));
7620        };
7621        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
7622            return Err(missing(&format!(
7623                "this board's {PRIORITY_FIELD} field is not a single-select field"
7624            )));
7625        }
7626        // An options list that is absent or not a list is an answer this source cannot read,
7627        // not a board lacking the option: `sources fields --apply` is no remedy for it.
7628        let option = field
7629            .get("options")
7630            .and_then(Value::as_array)
7631            .ok_or_else(|| SourceError::Malformed {
7632                message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
7633            })?
7634            .iter()
7635            .find(|option| {
7636                option
7637                    .get("name")
7638                    .and_then(Value::as_str)
7639                    .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
7640            })
7641            .ok_or_else(|| missing("this board does not have it"))?;
7642        Ok(Some(PriorityWrite::Select {
7643            field: required_str(field, "id")?.to_owned(),
7644            option: required_str(option, "id")?.to_owned(),
7645        }))
7646    }
7647
7648    /// What writing `metadata` does to each projected text field of one item, and what those
7649    /// fields hold after it — or the refusal, before anything is sent.
7650    ///
7651    /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
7652    /// narrows the work to the entries projecting one of those metadata keys, for a write that
7653    /// touches only them; `None` is every entry. A string the field already holds, and nothing
7654    /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
7655    /// and so is a board without the field, or with a field of that name that is not a text
7656    /// field: reads and writes never create a field.
7657    fn projection_writes(
7658        &self,
7659        fields: &Value,
7660        held: Option<&BTreeMap<String, String>>,
7661        metadata: &BTreeMap<String, Value>,
7662        keys: Option<&[&str]>,
7663    ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
7664        let mut projected = held.cloned().unwrap_or_default();
7665        let mut writes = Vec::new();
7666        for projection in self
7667            .metadata_fields
7668            .iter()
7669            .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
7670        {
7671            let wanted = projection.projected(metadata, &self.name)?;
7672            let field = self.projection_field(fields, projection)?;
7673            let holding = projected.get(&*projection.field);
7674            match wanted {
7675                Projected::Text(text) if holding != Some(&text) => {
7676                    writes.push(ProjectionWrite::Set {
7677                        field,
7678                        text: text.clone(),
7679                    });
7680                    projected.insert(projection.field.to_string(), text);
7681                }
7682                Projected::Nothing if holding.is_some() => {
7683                    writes.push(ProjectionWrite::Clear { field });
7684                    projected.remove(&*projection.field);
7685                }
7686                Projected::Text(_) | Projected::Nothing => {}
7687            }
7688        }
7689        Ok((writes, projected))
7690    }
7691
7692    /// The id of the board text field one projection writes, or the refusal naming what the
7693    /// board has instead.
7694    fn projection_field(
7695        &self,
7696        fields: &Value,
7697        projection: &MetadataField,
7698    ) -> Result<String, SourceError> {
7699        let refuse = |detail: String| SourceError::Refused {
7700            message: format!(
7701                "source {} projects metadata key {:?} onto the board text field {:?}, and \
7702                 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
7703                 text field, or name another field in metadata_fields",
7704                self.name, &*projection.key, &*projection.field, self.name
7705            ),
7706        };
7707        let Some(field) = Board::field(fields, &projection.field)? else {
7708            return Err(refuse("this board has no field of that name".to_owned()));
7709        };
7710        let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
7711        let data_type = optional_str(field, "dataType")?;
7712        if typename != "ProjectV2Field" || data_type != Some("TEXT") {
7713            return Err(refuse(format!(
7714                "this board's field of that name is not a text field (it is a {}), so it is \
7715                 left as it is; rename or remove that field first",
7716                data_type.unwrap_or(typename)
7717            )));
7718        }
7719        Ok(required_str(field, "id")?.to_owned())
7720    }
7721
7722    /// Apply one priority write to one board item.
7723    async fn write_priority(
7724        &self,
7725        board_id: &str,
7726        item_id: &str,
7727        write: &PriorityWrite,
7728    ) -> Result<(), SourceError> {
7729        match write {
7730            PriorityWrite::Select { field, option } => {
7731                self.set_item_field(
7732                    board_id,
7733                    item_id,
7734                    field,
7735                    json!({"singleSelectOptionId": option}),
7736                )
7737                .await
7738            }
7739            PriorityWrite::Clear { field } => {
7740                let data = self
7741                    .graphql(
7742                        graphql::CLEAR_FIELD,
7743                        json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
7744                            "readPriority":false,"priorityName":PRIORITY_FIELD}),
7745                    )
7746                    .await?;
7747                let returned = data
7748                    .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
7749                    .ok_or_else(|| SourceError::Malformed {
7750                        message: "GitHub field clear returned no project item".into(),
7751                    })?;
7752                if required_str(returned, "id")? != item_id {
7753                    return Err(SourceError::Malformed {
7754                        message: "GitHub field clear returned the wrong project item".into(),
7755                    });
7756                }
7757                Ok(())
7758            }
7759        }
7760    }
7761
7762    /// The refusal a priority is answered with by an instance configured with no
7763    /// `priority_mapping`, which holds none.
7764    fn holds_no_priority(&self) -> SourceError {
7765        SourceError::Refused {
7766            message: format!(
7767                "source {} holds no task priority: its configuration sets no priority_mapping; \
7768                 next: set priority_mapping on this source, then run `onetaskgraph sources \
7769                 fields {} --apply` to set its board up",
7770                self.name, self.name
7771            ),
7772        }
7773    }
7774
7775    /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
7776    ///
7777    /// One field write — a select, or a clear for `none` — and no title, body, label, state
7778    /// or `Status` request. Clearing a priority an item does not hold sends nothing.
7779    async fn set_priority(
7780        &self,
7781        id: &NativeId,
7782        priority: Priority,
7783    ) -> Result<Option<Priority>, SourceError> {
7784        if self.priorities.is_none() {
7785            return Err(self.holds_no_priority());
7786        }
7787        let Some(mut item) = self
7788            .bound_item(id)
7789            .await?
7790            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7791        else {
7792            return Ok(None);
7793        };
7794        if priority == Priority::None && !item.holds_priority() {
7795            return Ok(Some(priority));
7796        }
7797        // The item's own read carries the field's definition whenever it holds a value of
7798        // it, which a clear always does; a select onto an item holding none reads the board.
7799        let board = match (item.carried_board(), item.named_board()) {
7800            (Some(board), _) => board,
7801            (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
7802                id,
7803                fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
7804            },
7805            _ => self.board_fields().await?,
7806        };
7807        let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
7808            return Ok(Some(priority));
7809        };
7810        let (document, root, input) = match write {
7811            PriorityWrite::Select { field, option } => (
7812                graphql::UPDATE_FIELD,
7813                "updateProjectV2ItemFieldValue",
7814                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
7815            ),
7816            PriorityWrite::Clear { field } => (
7817                graphql::CLEAR_FIELD,
7818                "clearProjectV2ItemFieldValue",
7819                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
7820            ),
7821        };
7822        let data = self
7823            .graphql(
7824                document,
7825                json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
7826            )
7827            .await?;
7828        let returned = data
7829            .get(root)
7830            .and_then(|value| value.get("projectV2Item"))
7831            .ok_or_else(|| SourceError::Malformed {
7832                message: "GitHub priority write returned no project item".into(),
7833            })?;
7834        if required_str(returned, "id")? != item.item_id {
7835            return Err(SourceError::Malformed {
7836                message: "GitHub priority write returned the wrong project item".into(),
7837            });
7838        }
7839        let value = returned
7840            .get("fieldValueByName")
7841            .ok_or_else(|| SourceError::Malformed {
7842                message: "GitHub priority write returned no priority read-back".into(),
7843            })?;
7844        if !value.is_null()
7845            && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
7846        {
7847            return Err(SourceError::Malformed {
7848                message: "GitHub priority read-back is not a Priority field value".into(),
7849            });
7850        }
7851        let values = if value.is_null() {
7852            Vec::new()
7853        } else {
7854            vec![value.clone()]
7855        };
7856        item.priority = self.held_priority(&values)?;
7857        let answer = item.task()?.priority;
7858        self.remember_written(item, false)?;
7859        Ok(Some(answer))
7860    }
7861
7862    /// Replace one task's visible body and nothing else; see
7863    /// [`TaskSource::set_task_content`].
7864    ///
7865    /// One update of the body, which differs from the body GitHub holds only outside the
7866    /// metadata slot — the slot is kept byte for byte, so every caller key and every list
7867    /// this source keeps there reads back as it was. A body that would not change is not
7868    /// sent at all.
7869    async fn replace_content(
7870        &self,
7871        id: &NativeId,
7872        content: &str,
7873    ) -> Result<Option<()>, SourceError> {
7874        let Some(mut item) = self
7875            .bound_item(id)
7876            .await?
7877            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7878        else {
7879            return Ok(None);
7880        };
7881        let held = item.raw_body.clone().unwrap_or_default();
7882        let body = with_content(&held, content)?;
7883        // Checked before anything is sent: content ending in what this source reads as its own
7884        // metadata slot would read back as metadata rather than as the content it was.
7885        let (visible, slot) = metadata_body(Some(body.clone()))?;
7886        if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
7887            return Err(SourceError::Refused {
7888                message: format!(
7889                    "this content ends in what source {} reads as its own metadata slot \
7890                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7891                     as content; next: remove that trailing block from the content",
7892                    self.name
7893                ),
7894            });
7895        }
7896        if body != held {
7897            self.update_content(item.content_kind, &item.id, json!({"body": body}))
7898                .await?;
7899        }
7900        item.body = visible.filter(|value| !value.is_empty());
7901        item.raw_body = Some(body);
7902        item.slot = slot;
7903        self.remember_written(item, false)?;
7904        Ok(Some(()))
7905    }
7906
7907    /// Apply one targeted update to one task; see [`TaskSource::update_task`].
7908    ///
7909    /// One read of the item — which carries the board's field definitions and the issue's
7910    /// `blockedBy`, so neither is read again — and then only what differs from it: the
7911    /// `Status` option and the `Priority` field together in one request, the `blockedBy`
7912    /// additions and removals the named edges differ by, and last one `updateIssue` carrying
7913    /// the title, the body — visible content and metadata slot together — and a state change.
7914    /// So an update naming any of title, body, metadata, status and priority is one read and
7915    /// at most two writes. The body goes last so that a write refused part-way leaves it, and
7916    /// the metadata in it, as it stood. A terminal status selects its option and then closes,
7917    /// as a whole write does; an open one selects its option and then reopens. The origin
7918    /// field is never written: an update is of an item that already exists, whose origin is
7919    /// what it is.
7920    ///
7921    /// The task answered is the item as those writes left it, built from the read and what was
7922    /// sent rather than read again — the same record a later read in this run answers from.
7923    async fn targeted_update(
7924        &self,
7925        id: &NativeId,
7926        update: &TaskUpdate,
7927    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
7928        // Everything this source can refuse without reading the item is refused first, in the
7929        // words a whole write of the same fields is refused with.
7930        update.consistent()?;
7931        if update
7932            .title
7933            .as_deref()
7934            .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
7935        {
7936            return Err(SourceError::Refused {
7937                message: format!(
7938                    "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
7939                     spells a document, so it would read back as one rather than as a task; \
7940                     retitle it",
7941                    self.name
7942                ),
7943            });
7944        }
7945        if let Some(delivers) = &update.delivers {
7946            TaskRef::listed(
7947                TaskRef::DELIVERS_KEY,
7948                id,
7949                Some(&self.name),
7950                delivers.clone(),
7951            )
7952            .map_err(|message| SourceError::Refused { message })?;
7953        }
7954        if self.priorities.is_none()
7955            && update
7956                .priority
7957                .is_some_and(|priority| priority != Priority::None)
7958        {
7959            return Err(self.holds_no_priority());
7960        }
7961        let target = update
7962            .status
7963            .as_ref()
7964            .map(|status| self.resolved_target(ItemKind::Task, status.category))
7965            .transpose()?;
7966        let Some(mut item) = self
7967            .bound_item(id)
7968            .await?
7969            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7970        else {
7971            return Ok(None);
7972        };
7973        let before = item.task()?;
7974
7975        let mut status_move = None;
7976        if let (Some(status), Some(target)) = (&update.status, target) {
7977            let board = self.status_board(&item).await?;
7978            let (field, option, name) = self
7979                .column_for(&board.fields, ItemKind::Task, status.category, &target)?
7980                .ok_or_else(|| SourceError::Malformed {
7981                    message: format!(
7982                        "status {} of source {} names no board Status option",
7983                        category_name(status.category),
7984                        self.name
7985                    ),
7986                })?;
7987            let terminal = matches!(target, StatusTarget::Terminal(_, _));
7988            if terminal && item.content_kind == ContentKind::DraftIssue {
7989                return Err(self.closes_a_draft(status.category));
7990            }
7991            let landed = match &target {
7992                StatusTarget::Terminal(_, reason) => {
7993                    self.statuses
7994                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
7995                }
7996                _ => self
7997                    .statuses
7998                    .status(ItemKind::Task, Some(&name), false, None),
7999            };
8000            let option_moves = item
8001                .option
8002                .as_deref()
8003                .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
8004            let state_moves = item.content_kind == ContentKind::Issue
8005                && (item.closed != terminal || (terminal && item.status != landed));
8006            if let Some(moves) = Moves::of(option_moves, state_moves) {
8007                status_move = Some(StatusMove {
8008                    board: board.id,
8009                    field,
8010                    option,
8011                    name,
8012                    target,
8013                    landed,
8014                    moves,
8015                });
8016            }
8017        }
8018
8019        let mut priority_move = None;
8020        if let Some(priority) = update.priority
8021            && self.priorities.is_some()
8022            && item.priority != HeldPriority::Read(priority)
8023        {
8024            let board = match (item.carried_board(), item.named_board()) {
8025                (Some(board), _) => board,
8026                (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
8027                    id: board,
8028                    fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
8029                },
8030                _ => self.board_fields().await?,
8031            };
8032            if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
8033                priority_move = Some((board.id, write, priority));
8034            }
8035        }
8036
8037        // The metadata keys this update names that a board text field projects, and what
8038        // that does to those fields — refused, as a whole write refuses it, before anything
8039        // is sent.
8040        let touched: Vec<&str> = update
8041            .metadata_set
8042            .keys()
8043            .chain(update.metadata_remove.iter())
8044            .map(MetadataKey::as_str)
8045            .collect();
8046        let mut projection_move = None;
8047        if self
8048            .metadata_fields
8049            .iter()
8050            .any(|projection| touched.contains(&&*projection.key))
8051        {
8052            let mut metadata = item.slot.clone();
8053            for (key, value) in &update.metadata_set {
8054                metadata.insert(key.as_str().to_owned(), value.clone());
8055            }
8056            for key in &update.metadata_remove {
8057                metadata.remove(key.as_str());
8058            }
8059            let board = self.projection_board(&item).await?;
8060            let (writes, projected) = self.projection_writes(
8061                &board.fields,
8062                Some(&item.projected),
8063                &metadata,
8064                Some(&touched),
8065            )?;
8066            projection_move = Some((board.id, writes, projected));
8067        }
8068
8069        // Resolved before the body is composed, because a far end `blockedBy` cannot name is
8070        // recorded in the slot, and the slot travels in the one body update below.
8071        let edges = match &update.depends_on {
8072            Some(edges) => Some(
8073                self.partition_edges(
8074                    BoardKind::Work(ItemKind::Task),
8075                    item.content_kind,
8076                    item.blocked_by.as_deref(),
8077                    edges,
8078                )
8079                .await?,
8080            ),
8081            None => None,
8082        };
8083
8084        let mut slot = item.slot.clone();
8085        for (key, value) in &update.metadata_set {
8086            slot.insert(key.as_str().to_owned(), value.clone());
8087        }
8088        for key in &update.metadata_remove {
8089            slot.remove(key.as_str());
8090        }
8091        if let Some(delivers) = &update.delivers {
8092            set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
8093        }
8094        if let Some((_, recorded)) = &edges {
8095            record_edges(&mut slot, recorded);
8096        }
8097        let held = item.raw_body.clone().unwrap_or_default();
8098        let content = match &update.content {
8099            Some(content) => with_content(&held, content)?,
8100            None => held.clone(),
8101        };
8102        // A slot holding what it held is kept byte for byte, compared as JSON rather than as
8103        // the body's bytes, as a metadata write compares it: a slot a person spelled with
8104        // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
8105        let body = if slot == item.slot {
8106            content
8107        } else {
8108            with_slot(&content, &slot)?
8109        };
8110        // Checked before anything is sent, as a content write checks it: content ending in
8111        // what this source reads as its own slot would read back as metadata.
8112        let (visible, read) = metadata_body(Some(body.clone()))?;
8113        let wanted = update.content.as_deref().or(item.body.as_deref());
8114        if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
8115            return Err(SourceError::Refused {
8116                message: format!(
8117                    "this content ends in what source {} reads as its own metadata slot \
8118                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8119                     as content; next: remove that trailing block from the content",
8120                    self.name
8121                ),
8122            });
8123        }
8124        let recorded_moves =
8125            slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
8126
8127        // One `updateIssue` carries all three, because every mutation spends the secondary
8128        // limiter and the title, body and state are one mutation's inputs.
8129        let mut fields = serde_json::Map::new();
8130        if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
8131            fields.insert("title".to_owned(), json!(title));
8132        }
8133        if body != held {
8134            fields.insert("body".to_owned(), json!(body));
8135        }
8136        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
8137            fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
8138        }
8139        // **The body is written last, and that is the guarantee a refusal part-way keeps.**
8140        // GitHub runs no two requests as one, and runs one document's mutation fields in order
8141        // without undoing an earlier field when a later one fails — so a body written before a
8142        // board field the board then refused would be left changed. Written after every other
8143        // write has landed, a refusal anywhere leaves the item's body, and every metadata key
8144        // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
8145        // first, together in one request — a terminal option selected before the issue
8146        // closes, as a whole write does — then the `blockedBy` difference, then the body.
8147        let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
8148        let mut clears: Vec<(&BoardId, &str)> = Vec::new();
8149        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
8150            board_writes.push((
8151                &moving.board,
8152                (
8153                    moving.field.clone(),
8154                    json!({"singleSelectOptionId": moving.option}),
8155                ),
8156            ));
8157        }
8158        match &priority_move {
8159            Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
8160                board,
8161                (field.clone(), json!({"singleSelectOptionId": option})),
8162            )),
8163            Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
8164            None => {}
8165        }
8166        if let Some((board, writes, _)) = &projection_move {
8167            for write in writes {
8168                let (mut values, mut cleared) = (Vec::new(), Vec::new());
8169                write.join(&mut values, &mut cleared);
8170                board_writes.extend(values.into_iter().map(|value| (board, value)));
8171                clears.extend(cleared.into_iter().map(|field| (board, field)));
8172            }
8173        }
8174        let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
8175        boards.extend(clears.iter().map(|(board, _)| *board));
8176        boards.dedup_by(|one, other| one.as_str() == other.as_str());
8177        for board in boards {
8178            let writes = board_writes
8179                .iter()
8180                .filter(|(on, _)| on.as_str() == board.as_str())
8181                .map(|(_, write)| write.clone())
8182                .collect::<Vec<_>>();
8183            let cleared = clears
8184                .iter()
8185                .filter(|(on, _)| on.as_str() == board.as_str())
8186                .map(|(_, field)| *field)
8187                .collect::<Vec<_>>();
8188            self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
8189                .await?;
8190        }
8191        let mut blocked_by_moved = false;
8192        if let Some((native, _)) = &edges
8193            && item.content_kind == ContentKind::Issue
8194        {
8195            blocked_by_moved = self
8196                .reconcile_blocked_by(
8197                    &item.id,
8198                    native,
8199                    Issue::Existing(item.blocked_by.as_deref()),
8200                )
8201                .await?;
8202        }
8203        if !fields.is_empty() {
8204            self.update_content(item.content_kind, &item.id, Value::Object(fields))
8205                .await?;
8206        }
8207
8208        // The `blockedBy` this item was read with is what the write just changed, so it is no
8209        // answer any more: a later write in this command would reconcile against it — re-adding
8210        // what it kept and never removing what it dropped — and a forward-edge read would
8211        // answer with it. Left unknown, each asks GitHub instead.
8212        if blocked_by_moved {
8213            item.blocked_by = None;
8214        }
8215        if let Some(title) = &update.title {
8216            item.title.clone_from(title);
8217        }
8218        item.body = visible.filter(|value| !value.is_empty());
8219        item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
8220        item.slot = slot;
8221        if let Some(delivers) = &update.delivers {
8222            item.delivers.clone_from(delivers);
8223        }
8224        if let Some(moving) = status_move {
8225            item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
8226                && item.content_kind == ContentKind::Issue;
8227            item.status = moving.landed;
8228            item.option = Some(moving.name);
8229        }
8230        if let Some((_, _, priority)) = priority_move {
8231            item.priority = HeldPriority::Read(priority);
8232        }
8233        if let Some((_, _, projected)) = projection_move {
8234            item.projected = projected;
8235        }
8236        let task = item.task()?;
8237        let mut written = update.changed(&before, &task);
8238        if blocked_by_moved || recorded_moves {
8239            written.insert(UpdatedField::DependsOn);
8240        }
8241        self.remember_written(item, false)?;
8242        Ok(Some(TaskUpdateOutcome {
8243            task,
8244            written,
8245            delivers_before: before.delivers,
8246        }))
8247    }
8248
8249    /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
8250    /// together, and nothing else; see [`TaskSource::set_task_rendering`].
8251    ///
8252    /// One update of the body: the content outside the slot, and inside it that one entry,
8253    /// every other entry kept as it was. This source keeps no template answers — an issue has
8254    /// no room beside itself that is not its body, and answers written there would duplicate
8255    /// what the content already says and count against GitHub's body limit — so `answers`
8256    /// reaches nothing here. A body that would not change is not sent at all.
8257    async fn replace_rendering(
8258        &self,
8259        id: &NativeId,
8260        kind: BoardKind,
8261        content: &str,
8262        provenance: &Value,
8263        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
8264    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
8265        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
8266            return Ok(None);
8267        };
8268        let held = item.raw_body.clone().unwrap_or_default();
8269        let mut slot = item.slot.clone();
8270        slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
8271        let rewritten;
8272        let content = if let Some(assets) = assets {
8273            let uploads = self
8274                .upload_assets(item.own_repository.as_ref(), assets)
8275                .await?;
8276            rewritten =
8277                onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
8278            rewritten.as_str()
8279        } else {
8280            content
8281        };
8282        let body = with_slot(&with_content(&held, content)?, &slot)?;
8283        // Checked before anything is sent, as a content write checks it.
8284        let (visible, read) = metadata_body(Some(body.clone()))?;
8285        if visible.as_deref().unwrap_or_default() != content || read != slot {
8286            return Err(SourceError::Refused {
8287                message: format!(
8288                    "this content ends in what source {} reads as its own metadata slot \
8289                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
8290                     as content; next: remove that trailing block from the template",
8291                    self.name
8292                ),
8293            });
8294        }
8295        if body != held {
8296            self.update_content(item.content_kind, &item.id, json!({"body": body}))
8297                .await?;
8298        }
8299        item.body = visible.filter(|value| !value.is_empty());
8300        item.raw_body = Some(body);
8301        item.slot = read;
8302        self.remember_written(item, false)?;
8303        Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
8304            id: id.clone(),
8305            content: Some(content.to_owned()),
8306        }))
8307    }
8308
8309    async fn set_item_field(
8310        &self,
8311        board_id: &str,
8312        item_id: &str,
8313        field_id: &str,
8314        value: Value,
8315    ) -> Result<(), SourceError> {
8316        let data = self
8317            .graphql(
8318                graphql::UPDATE_FIELD,
8319                json!({"input":{
8320                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
8321                },"readPriority":false,"priorityName":PRIORITY_FIELD}),
8322            )
8323            .await?;
8324        let returned = data
8325            .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
8326            .ok_or_else(|| SourceError::Malformed {
8327                message: "GitHub field update returned no project item".into(),
8328            })?;
8329        if required_str(returned, "id")? != item_id {
8330            return Err(SourceError::Malformed {
8331                message: "GitHub field update returned the wrong project item".into(),
8332            });
8333        }
8334        Ok(())
8335    }
8336
8337    /// GitHub accepts one value per field mutation; aliases combine those mutations in
8338    /// one request — writes in the order given, then clears. A lone write is
8339    /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
8340    /// request's slots hold go in further requests of the same document, in order. Every
8341    /// returned item id is checked, including optional aliases.
8342    async fn set_item_fields(
8343        &self,
8344        board: &str,
8345        item: &str,
8346        fields: &[(String, Value)],
8347        clears: &[&str],
8348    ) -> Result<(), SourceError> {
8349        match (fields, clears) {
8350            ([], []) => return Ok(()),
8351            ([(field, value)], []) => {
8352                return self.set_item_field(board, item, field, value.clone()).await;
8353            }
8354            ([], [field]) => {
8355                return self
8356                    .write_priority(
8357                        board,
8358                        item,
8359                        &PriorityWrite::Clear {
8360                            field: (*field).to_owned(),
8361                        },
8362                    )
8363                    .await;
8364            }
8365            _ => {}
8366        }
8367        // Any field of the request stands in for an input a slot leaves out, which GitHub
8368        // still requires to be well formed although it never runs it.
8369        let filler = fields
8370            .first()
8371            .map_or_else(|| clears[0], |(field, _)| field.as_str());
8372        let mut writes = fields.iter();
8373        let mut cleared = clears.iter();
8374        loop {
8375            let batch: Vec<&(String, Value)> =
8376                writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
8377            let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
8378            if batch.is_empty() && batch_clears.is_empty() {
8379                return Ok(());
8380            }
8381            let mut variables = serde_json::Map::new();
8382            for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
8383                let input = match batch.get(index) {
8384                    Some((field, value)) => {
8385                        json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
8386                    }
8387                    None => json!({"projectId":board,"itemId":item,"fieldId":filler,
8388                                   "value":{"text":""}}),
8389                };
8390                variables.insert(slot.variable.to_owned(), input);
8391                variables.insert(slot.include.to_owned(), json!(index < batch.len()));
8392            }
8393            for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
8394                let field = batch_clears.get(index).map_or(filler, |field| **field);
8395                variables.insert(
8396                    slot.variable.to_owned(),
8397                    json!({"projectId":board,"itemId":item,"fieldId":field}),
8398                );
8399                variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
8400            }
8401            let data = self
8402                .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
8403                .await?;
8404            let sent = FIELD_WRITE_SLOTS[..batch.len()]
8405                .iter()
8406                .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
8407            for slot in sent {
8408                let alias = slot.alias;
8409                let returned = data
8410                    .get(alias)
8411                    .and_then(|value| value.get("projectV2Item"))
8412                    .ok_or_else(|| SourceError::Malformed {
8413                        message: format!("GitHub field update {alias} returned no project item"),
8414                    })?;
8415                if required_str(returned, "id")? != item {
8416                    return Err(SourceError::Malformed {
8417                        message: format!(
8418                            "GitHub field update {alias} returned the wrong project item"
8419                        ),
8420                    });
8421                }
8422            }
8423        }
8424    }
8425
8426    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
8427        let mut after: Option<String> = None;
8428        let mut ids = Vec::new();
8429        loop {
8430            let data = self
8431                .graphql(
8432                    graphql::ISSUE_DEPENDENCIES,
8433                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
8434                )
8435                .await?;
8436            let connection =
8437                data.pointer("/node/blockedBy")
8438                    .ok_or_else(|| SourceError::Malformed {
8439                        message: "GitHub dependency response has no blockedBy connection".into(),
8440                    })?;
8441            ids.extend(
8442                connection
8443                    .get("nodes")
8444                    .and_then(Value::as_array)
8445                    .ok_or_else(|| SourceError::Malformed {
8446                        message: "GitHub dependency response nodes is not an array".into(),
8447                    })?
8448                    .iter()
8449                    .map(|value| required_str(value, "id").map(str::to_owned))
8450                    .collect::<Result<Vec<_>, _>>()?,
8451            );
8452            let next = next_cursor(connection)?;
8453            if let Some(next) = &next {
8454                validate_cursor_progress(after.as_deref(), &next.0)?;
8455            }
8456            after = next.map(|cursor| cursor.0);
8457            if after.is_none() {
8458                return Ok(ids);
8459            }
8460        }
8461    }
8462
8463    async fn dependencies(
8464        &self,
8465        id: &NativeId,
8466        near_kind: ItemKind,
8467        direction: Direction,
8468        page: &PageRequest,
8469    ) -> Result<Page<DependencyEdge>, SourceError> {
8470        validate_page(page)?;
8471        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
8472        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
8473        let recorded = recorded_offset(cursor, direction)?;
8474        // What this issue is blocked by, when a read of it by its own id in this command
8475        // already carried the whole connection — a copy reads the item it writes before it
8476        // reads its edges — and the page asked for is the whole of it, or the recorded tail
8477        // after it. Answered from that read, in the shape the dependency read answers in;
8478        // anything else is asked of GitHub.
8479        let carried = match direction {
8480            Direction::DependsOn => self
8481                .resolved_cache()?
8482                .get(id)
8483                .filter(|item| item.content_kind == ContentKind::Issue)
8484                .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
8485            Direction::DependedOnBy => None,
8486        }
8487        .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
8488        // Asked for even in the recorded phase, whose page reads nothing from the
8489        // connection: `__typename` is what says whether this item has a native
8490        // relationship at all, and that is what decides which far ends the reserved key is
8491        // allowed to hold.
8492        let data = match carried {
8493            Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
8494                "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
8495            None => {
8496                self.graphql(
8497                    graphql::ISSUE_DEPENDENCIES,
8498                    json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
8499                           "after":if recorded.is_some() {None} else {cursor}}),
8500                )
8501                .await?
8502            }
8503        };
8504        let node =
8505            data.get("node")
8506                .filter(|v| !v.is_null())
8507                .ok_or_else(|| SourceError::Refused {
8508                    message: format!(
8509                        "GitHub item {} was not found or does not support dependencies",
8510                        id.0
8511                    ),
8512                })?;
8513        let connection_name = match direction {
8514            Direction::DependsOn => "blockedBy",
8515            Direction::DependedOnBy => "blocking",
8516        };
8517        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
8518        // named natively and the reserved key may hold any far end. An issue's connections
8519        // hold issues, and this source reads them at the near item's own level.
8520        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
8521        if let Some(offset) = recorded {
8522            return Ok(recorded_page(
8523                self.recorded_edges(id, near_kind, direction, natively_names, node)
8524                    .await?,
8525                offset,
8526                limit,
8527            ));
8528        }
8529        if natively_names.is_none() {
8530            return Ok(recorded_page(
8531                self.recorded_edges(id, near_kind, direction, natively_names, node)
8532                    .await?,
8533                0,
8534                limit,
8535            ));
8536        }
8537        let connection = node
8538            .get(connection_name)
8539            .ok_or_else(|| SourceError::Malformed {
8540                message: "GitHub dependency response is missing its connection".into(),
8541            })?;
8542        let nodes = connection
8543            .get("nodes")
8544            .and_then(Value::as_array)
8545            .ok_or_else(|| SourceError::Malformed {
8546                message: "GitHub dependency response nodes is not an array".into(),
8547            })?;
8548        // `from` depends on `to`, always. GitHub spells the same relationship from either
8549        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
8550        // it — so the near item is `from` in one direction and `to` in the other.
8551        let items = nodes
8552            .iter()
8553            .map(|value| {
8554                let related = NativeId(required_str(value, "id")?.into());
8555                let related_kind = related_kind(value)?;
8556                let (from, to) = match direction {
8557                    Direction::DependsOn => (
8558                        DependencyEndpoint::from_native(id.clone(), near_kind),
8559                        DependencyEndpoint::from_native(related, related_kind),
8560                    ),
8561                    Direction::DependedOnBy => (
8562                        DependencyEndpoint::from_native(related, related_kind),
8563                        DependencyEndpoint::from_native(id.clone(), near_kind),
8564                    ),
8565                };
8566                Ok(DependencyEdge {
8567                    from,
8568                    to,
8569                    kind: DependencyKind::Blocks,
8570                })
8571            })
8572            .collect::<Result<Vec<_>, SourceError>>()?;
8573        let mut next = next_cursor(connection)?;
8574        if let Some(next) = &next {
8575            validate_cursor_progress(cursor, &next.0)?;
8576        }
8577        if next.is_none()
8578            && !self
8579                .recorded_edges(id, near_kind, direction, natively_names, node)
8580                .await?
8581                .is_empty()
8582        {
8583            next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
8584        }
8585        Ok(Page { items, next })
8586    }
8587
8588    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
8589    /// a far end in another source has to live: no GitHub issue relationship can name one.
8590    ///
8591    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
8592    /// source never writes one down.
8593    ///
8594    /// The metadata lives in the item's own body slot, and `node` is the dependency read's
8595    /// own answer, which carries an issue's body — so an issue's recorded edges cost no
8596    /// request beyond the read already made, and reading the board for them would be a
8597    /// walk of every item for one field of one. A draft has no body in that answer, because
8598    /// a draft is not an issue, so a draft's are read off its own read by id — never off a
8599    /// listing of the board, which can be behind on the very item asked about.
8600    async fn recorded_edges(
8601        &self,
8602        id: &NativeId,
8603        near_kind: ItemKind,
8604        direction: Direction,
8605        natively_names: Option<ItemKind>,
8606        node: &Value,
8607    ) -> Result<Vec<DependencyEdge>, SourceError> {
8608        if direction != Direction::DependsOn {
8609            return Ok(Vec::new());
8610        }
8611        let slot = match node.get("body") {
8612            Some(body) if natively_names.is_some() => {
8613                metadata_body(body.as_str().map(str::to_owned))?.1
8614            }
8615            _ => {
8616                let Some(item) = self.bound_item(id).await? else {
8617                    return Ok(Vec::new());
8618                };
8619                item.slot
8620            }
8621        };
8622        DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
8623            .map_err(|message| SourceError::Malformed { message })
8624    }
8625
8626    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
8627        self.repository
8628            .as_ref()
8629            .ok_or_else(|| SourceError::Refused {
8630                message: format!(
8631                    "source {} has no repository configured, and a GitHub Projects board has no \
8632                 repository of its own to create an issue in; set repository: owner/name on \
8633                 this source",
8634                    self.name
8635                ),
8636            })
8637    }
8638
8639    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
8640    /// states.
8641    ///
8642    /// The fallback is demanded first, whichever arm answers: a write without a configured
8643    /// repository is refused naming the field exactly as it was before the rule existed,
8644    /// so a source that could not write before cannot write now, rather than writing for
8645    /// the one item whose own field happens to decide it.
8646    ///
8647    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
8648    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
8649    /// entry owned by someone other than the owner of the parent issue's repository —
8650    /// GitHub accepts a sub-issue from another repository of the same owner and from no
8651    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
8652    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
8653    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
8654    /// and is visible to the token is checked where its node id is resolved, still before
8655    /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
8656    /// looked up in a listing of the board, which can be minutes behind an issue its own
8657    /// `projectItems` already places on it — and that read answers first from this process's
8658    /// own record, so a project created moments ago in this command answers though GitHub
8659    /// has not caught up.
8660    async fn creation_target(
8661        &self,
8662        incoming: &Incoming<'_>,
8663    ) -> Result<RepositoryTarget, SourceError> {
8664        let fallback = self.configured_repository()?;
8665        let what = |incoming: &Incoming<'_>| {
8666            format!(
8667                "{} {:?}",
8668                incoming.written.kind().describes(),
8669                incoming.title
8670            )
8671        };
8672        let parent = match incoming.parent {
8673            Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
8674                SourceError::Refused {
8675                    message: format!(
8676                        "GitHub project issue {} was not found on the board of source {}, so {} \
8677                         cannot be filed under it",
8678                        parent.0,
8679                        self.name,
8680                        what(incoming)
8681                    ),
8682                }
8683            })?),
8684            None => None,
8685        };
8686        let parents_repository = parent
8687            .as_ref()
8688            .map(|parent| {
8689                // A draft is on the board and so is found, but it has no repository to
8690                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
8691                // would refuse the task only once `createIssue` had made it.
8692                if parent.content_kind == ContentKind::DraftIssue {
8693                    return Err(SourceError::Refused {
8694                        message: format!(
8695                            "GitHub project item {} on the board of source {} is a draft, \
8696                             which cannot have sub-issues, so {} cannot be filed under it",
8697                            parent.id.0,
8698                            self.name,
8699                            what(incoming)
8700                        ),
8701                    });
8702                }
8703                // An issue's repository is where a sub-issue is placed and whose owner it
8704                // is compared against, so a parent whose repository this source cannot
8705                // spell as `owner/name` — GitHub's login grammar is wider than this
8706                // source's floor — is one nothing can be filed under.
8707                parent
8708                    .own_repository
8709                    .as_ref()
8710                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
8711                    .ok_or_else(|| SourceError::Malformed {
8712                        message: format!(
8713                            "GitHub project issue {} on the board of source {} is in {}, which \
8714                             is not a {}/owner/name repository this source can place {} in",
8715                            parent.id.0,
8716                            self.name,
8717                            parent
8718                                .own_repository
8719                                .as_ref()
8720                                .map_or("no repository", Repository::as_str),
8721                            RepositoryTarget::HOST,
8722                            what(incoming)
8723                        ),
8724                    })
8725            })
8726            .transpose()?;
8727        match incoming.repositories {
8728            [named] => {
8729                let target =
8730                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
8731                        message: format!(
8732                            "{} names repository {}, which is not a {}/owner/name repository \
8733                             source {} can create an issue in; name one that is, or name none",
8734                            what(incoming),
8735                            named.as_str(),
8736                            RepositoryTarget::HOST,
8737                            self.name
8738                        ),
8739                    })?;
8740                if let Some(parents) = &parents_repository
8741                    && parents.owner != target.owner
8742                {
8743                    return Err(SourceError::Refused {
8744                        message: format!(
8745                            "{} names repository {}, owned by {}, but its project's issue is in \
8746                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
8747                             of the same owner as its parent issue; name a repository of {}, or \
8748                             name none",
8749                            what(incoming),
8750                            target.slug(),
8751                            target.owner,
8752                            parents.slug(),
8753                            parents.owner,
8754                            parents.owner
8755                        ),
8756                    });
8757                }
8758                Ok(target)
8759            }
8760            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
8761        }
8762    }
8763
8764    /// The node id of the repository `incoming` is being created in, or the refusal naming
8765    /// the item and the repository the token cannot see.
8766    ///
8767    /// Resolved once per command per repository; see [`Self::repository_cache`].
8768    async fn repository_id(
8769        &self,
8770        repository: &RepositoryTarget,
8771        incoming: &Incoming<'_>,
8772    ) -> Result<String, SourceError> {
8773        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
8774            return Ok(id);
8775        }
8776        let data = self
8777            .graphql(
8778                graphql::REPOSITORY,
8779                json!({"owner":repository.owner,"name":repository.name}),
8780            )
8781            .await?;
8782        self.repository_read(&data, repository, incoming)
8783    }
8784
8785    /// The repository's node id out of an answer carrying the `repository` root, held for
8786    /// the rest of this command, or the refusal naming the item that cannot be created in it.
8787    fn repository_read(
8788        &self,
8789        data: &Value,
8790        repository: &RepositoryTarget,
8791        incoming: &Incoming<'_>,
8792    ) -> Result<String, SourceError> {
8793        let node = data
8794            .get("repository")
8795            .filter(|value| !value.is_null())
8796            .ok_or_else(|| SourceError::Refused {
8797                message: format!(
8798                    "GitHub repository {} was not found or is not visible to the token, so {} \
8799                     {:?} cannot be created in it",
8800                    repository.slug(),
8801                    incoming.written.kind().describes(),
8802                    incoming.title
8803                ),
8804            })?;
8805        let id = required_str(node, "id")?.to_owned();
8806        self.repository_cache()?
8807            .insert(repository.clone(), id.clone());
8808        Ok(id)
8809    }
8810
8811    fn repository_cache(
8812        &self,
8813    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
8814        self.repository_cache
8815            .lock()
8816            .map_err(|_| SourceError::Unavailable {
8817                message: "this source's record of the destination repository was left \
8818                          inconsistent by an earlier failure; next: run the command again"
8819                    .into(),
8820            })
8821    }
8822
8823    /// Create or update one board item, whichever kind it is.
8824    async fn write_item(
8825        &self,
8826        incoming: &Incoming<'_>,
8827        target: Option<&NativeId>,
8828        depends_on: &[DependencyEdge],
8829    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
8830        let prepared = self.prepare_write(incoming, target, depends_on).await?;
8831        let landed = match &prepared.destination {
8832            // Its content is written last, by `complete_write`, once everything else has landed.
8833            WriteDestination::Existing(item) => Landed {
8834                content_id: item.id.clone(),
8835                item_id: Some(item.item_id.clone()),
8836                url: item.url.clone(),
8837                number: item.number,
8838            },
8839            WriteDestination::New(target) => {
8840                if self.deferring()? {
8841                    self.create_filed_issue(
8842                        prepared.board.id.as_str(),
8843                        target,
8844                        incoming,
8845                        &prepared.body,
8846                    )
8847                    .await?
8848                } else {
8849                    self.create_and_file_issue(
8850                        prepared.board.id.as_str(),
8851                        target,
8852                        incoming,
8853                        &prepared.body,
8854                    )
8855                    .await?
8856                }
8857            }
8858        };
8859        self.complete_write(incoming, prepared, landed).await
8860    }
8861
8862    /// Everything one write of `incoming` reads and decides before it sends anything: what is
8863    /// there now, where a new item is created, the board's fields, and every value refused while
8864    /// nothing has been written — the item's body included.
8865    async fn prepare_write(
8866        &self,
8867        incoming: &Incoming<'_>,
8868        target: Option<&NativeId>,
8869        depends_on: &[DependencyEdge],
8870    ) -> Result<Prepared, SourceError> {
8871        // Refused before anything is read or written: a task or a project titled the way
8872        // this board spells a document would land as an issue this same source reads back
8873        // as a document, so the field this destination cannot carry is named rather than
8874        // written and silently reclassified.
8875        if let Written::Work(kind, _) = incoming.written
8876            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
8877        {
8878            return Err(SourceError::Refused {
8879                message: format!(
8880                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8881                     spells a document, so it would read back as one rather than as a {}; \
8882                     retitle it, or copy it as a document",
8883                    kind.marker(),
8884                    self.name,
8885                    kind.marker()
8886                ),
8887            });
8888        }
8889        // The destination is read by its own id, and whether this board holds it is decided
8890        // by that read — its own `projectItems` — rather than by whether a listing of the
8891        // board happens to include it yet. See the module documentation.
8892        // An existing issue is never moved; a new one is created where the rule says — and
8893        // knowing where is what lets the board's fields and that repository's id be read
8894        // together, before anything below needs either.
8895        let destination = match target {
8896            Some(target) => {
8897                WriteDestination::Existing(Box::new(self.bound_item(target).await?.ok_or_else(
8898                    || SourceError::Refused {
8899                        message: format!("GitHub destination item {} was not found", target.0),
8900                    },
8901                )?))
8902            }
8903            None => {
8904                let target = self.creation_target(incoming).await?;
8905                self.creation_context(&target, incoming).await?;
8906                WriteDestination::New(target)
8907            }
8908        };
8909        let existing = destination.existing();
8910        let board = self
8911            .fields_for(
8912                existing,
8913                incoming.written.status().is_some(),
8914                incoming
8915                    .priority
8916                    .is_some_and(|priority| priority != Priority::None),
8917            )
8918            .await?;
8919        let status_target = incoming
8920            .written
8921            .work_status()
8922            .map(|(kind, status)| self.resolved_target(kind, status.category))
8923            .transpose()?;
8924        let column = match (incoming.written.work_status(), status_target.as_ref()) {
8925            (Some((kind, status)), Some(target)) => {
8926                self.column_for(&board.fields, kind, status.category, target)?
8927            }
8928            _ => None,
8929        };
8930        // Resolved before anything is created, for the reason the column above is: a
8931        // priority this board has no option for is refused while nothing has been written.
8932        let priority_write = match incoming.priority {
8933            Some(priority) => self.priority_write(&board.fields, existing, priority)?,
8934            None => None,
8935        };
8936        // And for the reason the priority is: a value no projected field can hold, or a board
8937        // that cannot hold the field, is refused while nothing has been written.
8938        let (projections, projected) = self.projection_writes(
8939            &board.fields,
8940            existing.map(|item| &item.projected),
8941            incoming.metadata,
8942            None,
8943        )?;
8944        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
8945        if content_kind == ContentKind::DraftIssue {
8946            if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
8947                (status_target.as_ref(), incoming.written.status())
8948            {
8949                return Err(self.closes_a_draft(status.category));
8950            }
8951            if incoming.parent.is_some() {
8952                return Err(SourceError::Refused {
8953                    message: "GitHub draft items cannot be a project's sub-issue".into(),
8954                });
8955            }
8956        }
8957        match existing {
8958            Some(item) if content_kind == ContentKind::Issue => {
8959                if item.labels != incoming.labels {
8960                    return Err(SourceError::Refused {
8961                        message: "GitHub issue labels differ from the labels being written".into(),
8962                    });
8963                }
8964            }
8965            _ => {
8966                if !incoming.labels.is_empty() {
8967                    return Err(SourceError::Refused {
8968                        message: "GitHub items created by this destination carry no labels".into(),
8969                    });
8970                }
8971            }
8972        }
8973
8974        // The repository the issue really lives in is what the slot below is written against,
8975        // so a single entry that is where the issue is created travels as no key at all, and
8976        // the read side derives it back from the issue.
8977        let own_repository = match &destination {
8978            WriteDestination::Existing(item) => item.own_repository.clone(),
8979            WriteDestination::New(target) => Some(
8980                Repository::try_from(target.origin())
8981                    .map_err(|message| SourceError::Config { message })?,
8982            ),
8983        };
8984        let (native, fallback) = self
8985            .partition_edges(
8986                incoming.written.kind(),
8987                content_kind,
8988                existing.and_then(|item| item.blocked_by.as_deref()),
8989                depends_on,
8990            )
8991            .await?;
8992        let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
8993        let content = match incoming.assets {
8994            Some(assets) => {
8995                let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
8996                let rewritten = onetaskgraph_plugin_api::serve_asset_references(
8997                    incoming.content.unwrap_or_default(),
8998                    &mut slot,
8999                    &uploads,
9000                );
9001                incoming.content.map(|_| rewritten)
9002            }
9003            None => incoming.content.map(str::to_owned),
9004        };
9005        let body = compose_body(content.as_deref(), &slot)?;
9006        // Read before anything is created, for the reason the field below is: a value
9007        // this destination cannot store has to refuse, and refusing after `createIssue`
9008        // would leave an issue behind that nothing asked for. The engine writes a
9009        // qualified id here; a caller handing this key anything else is told so rather
9010        // than having it silently stored as no origin at all.
9011        // 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.
9012        let origin = match incoming.metadata.get(ORIGIN_KEY) {
9013            None => "",
9014            Some(Value::String(origin)) => origin.as_str(),
9015            Some(other) => {
9016                return Err(SourceError::Refused {
9017                    message: format!(
9018                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
9019                         is {other}"
9020                    ),
9021                });
9022            }
9023        };
9024        // Resolved before anything is created: a board that cannot carry the copy origin
9025        // has to refuse the write, and refusing it after `createIssue` would leave an
9026        // issue behind that nothing asked for.
9027        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
9028            Some(field) => {
9029                if required_str(field, "__typename")? != "ProjectV2Field" {
9030                    return Err(SourceError::Refused {
9031                        message: format!(
9032                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
9033                        ),
9034                    });
9035                }
9036                Some(required_str(field, "id")?.to_owned())
9037            }
9038            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
9039                return Err(SourceError::Refused {
9040                    message: format!(
9041                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
9042                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
9043                         the board"
9044                    ),
9045                });
9046            }
9047            None => None,
9048        };
9049
9050        Ok(Prepared {
9051            origin: origin.to_owned(),
9052            destination,
9053            board,
9054            status_target,
9055            column,
9056            priority_write,
9057            projections,
9058            projected,
9059            content_kind,
9060            own_repository,
9061            native,
9062            slot,
9063            content,
9064            body,
9065            origin_field,
9066        })
9067    }
9068
9069    /// Everything one write does once the item it writes exists: its board fields, its
9070    /// relationships and — for an item that was already there — its content last, then what this
9071    /// process remembers of it.
9072    ///
9073    /// While this source is holding writes back — see [`TaskSource::begin_deferred_writes`] — an
9074    /// item this command created has its fields, its closing and its edges held for
9075    /// [`TaskSource::finish_writes`] rather than sent here.
9076    async fn complete_write(
9077        &self,
9078        incoming: &Incoming<'_>,
9079        prepared: Prepared,
9080        landed: Landed,
9081    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9082        let Prepared {
9083            destination,
9084            board,
9085            status_target,
9086            column,
9087            priority_write,
9088            projections,
9089            projected,
9090            content_kind,
9091            own_repository,
9092            native,
9093            slot,
9094            content,
9095            body,
9096            origin,
9097            origin_field,
9098        } = prepared;
9099        let existing = destination.existing();
9100        let origin = origin.as_str();
9101        let Landed {
9102            content_id,
9103            item_id,
9104            url,
9105            number,
9106        } = landed;
9107
9108        // Only what differs from the item as this write read it is sent: a priority the item
9109        // already holds is not, so an unchanged re-copy writes no field and a person's edit of
9110        // it since that read is left alone.
9111        let priority_write = priority_write.filter(|_| {
9112            !existing.is_some_and(|item| {
9113                incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
9114            })
9115        });
9116        let written_option = column.as_ref().map(|(_, _, name)| name.clone());
9117        let column = column
9118            .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
9119            .map(|(field, option, _)| (field, option));
9120        // Creating an item here is several calls — `createIssue`, which files it on the
9121        // board, then its board fields, the parent and the dependencies — and GitHub can fail
9122        // at any of them. Everything this source can refuse *before* the first of those is
9123        // already checked above, so what is left is GitHub itself failing part way. When it
9124        // does over an item this call created, the issue is taken back: a write that
9125        // refused must not leave an item behind that nobody asked for, and one that does
9126        // makes the retry create a second.
9127        // Whether the board-field write carrying a moved origin was answered as landing whole.
9128        // When it was refused, GitHub does not say which of its fields ran before the one that
9129        // failed, so the origin may or may not have moved.
9130        let mut origin_landed = false;
9131        // An item this command created while writes are held back has every part that can land
9132        // later held for `finish_writes` — its own create filed it on the board and under its
9133        // project already — and so does a later write of it in the same command.
9134        let holding = (existing.is_none() && self.deferring()?) || self.holds_back(&content_id)?;
9135        let landed = if holding {
9136            self.hold_back(
9137                &board.id,
9138                &content_id,
9139                existing,
9140                origin_field.as_deref(),
9141                origin,
9142                column,
9143                status_target.as_ref(),
9144                priority_write.as_ref(),
9145                &projections,
9146                &native,
9147            )
9148        } else if let Some(item_id) = item_id.as_deref() {
9149            self.finish_write(
9150                board.id.as_str(),
9151                incoming,
9152                &content_id,
9153                item_id,
9154                content_kind,
9155                existing,
9156                origin_field.as_deref(),
9157                origin,
9158                column,
9159                status_target.as_ref(),
9160                priority_write.as_ref(),
9161                &projections,
9162                &native,
9163                &mut origin_landed,
9164            )
9165            .await
9166        } else {
9167            // An issue a create filed on the board itself has no board item this process
9168            // knows until `finish_writes` reads it, so nothing but a held-back write may land
9169            // on it before then.
9170            Err(SourceError::Malformed {
9171                message: format!(
9172                    "issue {} was written before the board item GitHub filed it as was read",
9173                    content_id.0
9174                ),
9175            })
9176        };
9177        // An existing item's title, body and state go last, in one `updateIssue`, once its board
9178        // fields and its relationships have landed: a refusal of any of those then leaves its
9179        // body — and the metadata slot inside it — exactly as it stood.
9180        let landed = match (landed, existing) {
9181            (Ok(()), Some(item)) => {
9182                self.update_existing(item, incoming, &body, status_target.as_ref())
9183                    .await
9184            }
9185            (landed, _) => landed,
9186        };
9187        if let Err(error) = landed {
9188            match existing {
9189                // Best effort, and the write's own failure is what the caller is told: a
9190                // refusal naming the tidy-up would hide why the write failed at all.
9191                None => {
9192                    let _ = self.delete_issue(&content_id).await;
9193                }
9194                // The origin field is the one piece of an existing item's metadata written
9195                // before its body, so a write refused after it puts it back as it was. When
9196                // that is refused too, the write's own failure is still what the caller is
9197                // told — with what it left behind added, because the item's metadata is then
9198                // not as it stood and a caller retrying has to know which key moved.
9199                Some(item) => {
9200                    let before = item.origin.as_deref().unwrap_or("");
9201                    if let Some(field) = origin_field.as_deref()
9202                        && before != origin
9203                        && let Err(restore) = self
9204                            .set_item_field(
9205                                board.id.as_str(),
9206                                &item.item_id,
9207                                field,
9208                                json!({"text": before}),
9209                            )
9210                            .await
9211                    {
9212                        let left = if origin_landed {
9213                            format!(
9214                                "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
9215                                 not be put back to {before:?} ({restore}), so item {} still \
9216                                 holds {origin:?} there",
9217                                item.id.0
9218                            )
9219                        } else {
9220                            format!(
9221                                "the refused write carried its {ORIGIN_KEY} from {before:?} to \
9222                                 {origin:?}, GitHub does not say whether that part of it ran, \
9223                                 and putting it back to {before:?} was refused ({restore}), so \
9224                                 item {} holds {origin:?} or {before:?} there",
9225                                item.id.0
9226                            )
9227                        };
9228                        return Err(noting(
9229                            error,
9230                            &format!(
9231                                "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
9232                                 run the write again"
9233                            ),
9234                        ));
9235                    }
9236                }
9237            }
9238            return Err(error);
9239        }
9240
9241        let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
9242            (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
9243                self.statuses
9244                    .status(kind, written_option.as_deref(), true, Some(reason.reason()))
9245            }
9246            (Some((kind, _)), Some(StatusTarget::Column(_))) => {
9247                self.statuses
9248                    .status(kind, written_option.as_deref(), false, None)
9249            }
9250            (Some((_, status)), _) => status.clone(),
9251            (None, _) => Status {
9252                category: StatusCategory::Unknown,
9253                name: "Open".to_owned(),
9254            },
9255        };
9256
9257        // So the rest of this command reads what it just did rather than what the board
9258        // said before it. See `remember_written` for which half takes it.
9259        let remembered = Resolved {
9260            // Empty for an issue whose board item is not read yet: `finish_writes` reads it
9261            // and `learn_item` puts it here.
9262            // 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.
9263            item_id: item_id.unwrap_or_default(),
9264            id: content_id.clone(),
9265            content_kind,
9266            kind: incoming.written.kind(),
9267            title: incoming.title.to_owned(),
9268            // The visible half of the body this write composed, split back off it the
9269            // way a read splits it — so what this record reports is what a read of the
9270            // same issue reports, rather than the person's text with the metadata slot
9271            // still on the end of it.
9272            body: metadata_body(body.clone())?.0,
9273            raw_body: body.clone(),
9274            // A document has no status of its own; what it reads back as is whatever
9275            // the issue's own state says, which is what a re-read reports.
9276            status: written_status,
9277            option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
9278            priority: match incoming.priority {
9279                Some(priority) => HeldPriority::Read(priority),
9280                None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
9281                    item.priority.clone()
9282                }),
9283            },
9284            // What `state_input` asked for: closed for a terminal target, open for any other
9285            // status, and the issue's own state left as it was by a document write.
9286            closed: content_kind == ContentKind::Issue
9287                && match status_target.as_ref() {
9288                    Some(StatusTarget::Terminal(_, _)) => true,
9289                    Some(_) => false,
9290                    None => existing.is_some_and(|item| item.closed),
9291                },
9292            reason: match status_target.as_ref() {
9293                Some(StatusTarget::Terminal(_, reason)) if content_kind == ContentKind::Issue => {
9294                    Some(reason.reason().to_owned())
9295                }
9296                Some(_) => None,
9297                None => existing.and_then(|item| item.reason.clone()),
9298            },
9299            delivers: incoming.delivers.to_vec(),
9300            delivered_by: incoming.delivered_by.to_vec(),
9301            labels: incoming.labels.to_vec(),
9302            parent: incoming.parent.cloned(),
9303            origin: (!origin.is_empty()).then(|| origin.to_owned()),
9304            projected,
9305            number,
9306            // In the update path this is the item's own url, read off `existing` where the
9307            // record above was bound, so one expression serves both halves.
9308            url,
9309            created_at: existing.and_then(|item| item.created_at),
9310            updated_at: existing.and_then(|item| item.updated_at),
9311            // What this write moved is GitHub's to say, so a record of it gives no token until
9312            // it is read again.
9313            item_updated_at: None,
9314            comment_count: None,
9315            relationship_events: None,
9316            served_at: None,
9317            facets: None,
9318            own_repository,
9319            repositories: incoming.repositories.to_vec(),
9320            classification: incoming.classification,
9321            slot,
9322            board_id: Some(board.id.as_str().to_owned()),
9323            fields: board
9324                .fields
9325                .get("nodes")
9326                .and_then(Value::as_array)
9327                .cloned()
9328                .unwrap_or_default(),
9329            board_fields: Some(board.fields.clone()),
9330            // What this write left the relationship holding is known by id alone, and a
9331            // later read of its edges needs each far end's kind, so it reads them again.
9332            blocked_by: None,
9333        };
9334        self.remember_written(remembered, existing.is_none())?;
9335        Ok(onetaskgraph_plugin_api::AssetsWritten {
9336            id: content_id,
9337            content,
9338        })
9339    }
9340
9341    /// Everything a write does after the item exists: its board fields, its parent, and
9342    /// its dependencies.
9343    ///
9344    /// Split out of `write_item` so there is one place a failure past the point of no
9345    /// return is caught, rather than a tidy-up repeated at each `?` above.
9346    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
9347    // so there is one place a failure past the point of no return is caught, and its
9348    // arguments are exactly the values that tail already had in scope. Bundling them into a
9349    // struct would describe no concept — it would be "the arguments of this function" — and
9350    // would put the whole of `write_item`'s locals behind one more indirection.
9351    #[allow(clippy::too_many_arguments)]
9352    async fn finish_write(
9353        &self,
9354        board_id: &str,
9355        incoming: &Incoming<'_>,
9356        content_id: &NativeId,
9357        item_id: &str,
9358        content_kind: ContentKind,
9359        existing: Option<&Resolved>,
9360        origin_field: Option<&str>,
9361        origin: &str,
9362        column: Option<(String, String)>,
9363        status_target: Option<&StatusTarget>,
9364        priority: Option<&PriorityWrite>,
9365        projections: &[ProjectionWrite],
9366        native: &[String],
9367        origin_landed: &mut bool,
9368    ) -> Result<(), SourceError> {
9369        let (fields, clears) = field_writes(
9370            existing,
9371            origin_field,
9372            origin,
9373            column,
9374            priority,
9375            projections,
9376        );
9377        self.set_item_fields(board_id, item_id, &fields, &clears)
9378            .await?;
9379        *origin_landed = true;
9380
9381        // An existing issue closes in the `updateIssue` its write ends with; one created just
9382        // now closes here, once its option is selected.
9383        if existing.is_none()
9384            && content_kind == ContentKind::Issue
9385            && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
9386        {
9387            self.update_content(
9388                ContentKind::Issue,
9389                content_id,
9390                json!({"stateInput":state_input(status_target)}),
9391            )
9392            .await?;
9393        }
9394
9395        if content_kind == ContentKind::Issue {
9396            self.reparent(
9397                existing.and_then(|item| item.parent.clone()),
9398                content_id,
9399                incoming.parent,
9400            )
9401            .await?;
9402            // A document takes part in no dependency graph, so writing one neither reads
9403            // nor changes the issue's own `blockedBy` relationships. Reconciling them
9404            // against the empty list a document write carries would *delete* whatever
9405            // relationships a person had made on that issue, which is a write nobody
9406            // asked for.
9407            if incoming.written.kind() != BoardKind::Document {
9408                let issue = match existing {
9409                    Some(item) => Issue::Existing(item.blocked_by.as_deref()),
9410                    None => Issue::Created,
9411                };
9412                self.reconcile_blocked_by(content_id, native, issue).await?;
9413            }
9414        }
9415        Ok(())
9416    }
9417
9418    /// Whether this source is holding writes back — see [`TaskSource::begin_deferred_writes`].
9419    fn deferring(&self) -> Result<bool, SourceError> {
9420        Ok(self.deferred()?.is_some())
9421    }
9422
9423    /// Whether `content` is an item this command created whose parts are held back.
9424    fn holds_back(&self, content: &NativeId) -> Result<bool, SourceError> {
9425        Ok(self
9426            .deferred()?
9427            .as_ref()
9428            .is_some_and(|deferred| deferred.holds(content)))
9429    }
9430
9431    fn deferred(&self) -> Result<std::sync::MutexGuard<'_, Option<Deferred>>, SourceError> {
9432        self.deferred.lock().map_err(|_| SourceError::Unavailable {
9433            message: "this source's record of the writes it is holding back was left \
9434                      inconsistent by an earlier failure; next: run the command again"
9435                .into(),
9436        })
9437    }
9438
9439    /// Creates one issue filed on the board and under its project by the `createIssue` itself —
9440    /// `projectV2Ids` naming the board and `parentIssueId` the project — which is what a copy
9441    /// creates with: one request where adding it to the board and filing it under its project
9442    /// were two more. GitHub adds the board item after answering, so its id is not known here;
9443    /// [`TaskSource::finish_writes`] reads it.
9444    async fn create_filed_issue(
9445        &self,
9446        board_id: &str,
9447        repository: &RepositoryTarget,
9448        incoming: &Incoming<'_>,
9449        body: &Option<String>,
9450    ) -> Result<Landed, SourceError> {
9451        let repository_id = self.repository_id(repository, incoming).await?;
9452        let data = self
9453            .graphql(
9454                graphql::CREATE_ISSUE,
9455                json!({"input": filed_input(&repository_id, board_id, incoming, body)}),
9456            )
9457            .await?;
9458        let created = data
9459            .pointer("/createIssue/issue")
9460            .filter(|value| !value.is_null())
9461            .ok_or_else(|| SourceError::Malformed {
9462                message: "GitHub issue creation returned no issue".into(),
9463            })?;
9464        self.created_landed(created).await
9465    }
9466
9467    /// Keep the address `createIssue` answered for the issue `content`, for
9468    /// [`TaskSource::written_location`].
9469    fn note_written(&self, content: &NativeId, url: Option<&str>) {
9470        if let Some(url) = url {
9471            self.written
9472                .lock()
9473                .unwrap_or_else(std::sync::PoisonError::into_inner)
9474                .insert(content.clone(), url.to_owned());
9475        }
9476    }
9477
9478    /// What one `createIssue` answered, as the item it created — or, when its number cannot be
9479    /// read, the refusal, after taking the issue back.
9480    async fn created_landed(&self, created: &Value) -> Result<Landed, SourceError> {
9481        let content_id = NativeId(required_nonblank_str(created, "id")?.to_owned());
9482        let url = optional_str(created, "url")?.map(str::to_owned);
9483        let number = match created_issue_number(created) {
9484            Ok(number) => number,
9485            Err(error) => {
9486                let _ = self.delete_issue(&content_id).await;
9487                return Err(error);
9488            }
9489        };
9490        self.note_written(&content_id, url.as_deref());
9491        Ok(Landed {
9492            content_id,
9493            item_id: None,
9494            url,
9495            number,
9496        })
9497    }
9498
9499    /// Hold back everything a write of the created item `content` would send after creating it,
9500    /// for [`TaskSource::finish_writes`]: the same field values, closing and edges
9501    /// `finish_write` sends, the parent excepted, which its create already named. A later write
9502    /// of the same item replaces what it changes.
9503    // 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.
9504    #[allow(clippy::too_many_arguments)]
9505    fn hold_back(
9506        &self,
9507        board_id: &BoardId,
9508        content: &NativeId,
9509        existing: Option<&Resolved>,
9510        origin_field: Option<&str>,
9511        origin: &str,
9512        column: Option<(String, String)>,
9513        status_target: Option<&StatusTarget>,
9514        priority: Option<&PriorityWrite>,
9515        projections: &[ProjectionWrite],
9516        native: &[String],
9517    ) -> Result<(), SourceError> {
9518        let status = column.map(|(field, option)| {
9519            let name = self
9520                .fields_cache()
9521                .ok()
9522                .and_then(|fields| {
9523                    fields.as_ref().and_then(|board| {
9524                        option_name(&board.fields, &field, &option).map(str::to_owned)
9525                    })
9526                })
9527                .unwrap_or_default();
9528            (field, option, name)
9529        });
9530        let (fields, clears) =
9531            field_writes(existing, origin_field, origin, None, priority, projections);
9532        let close = match status_target {
9533            Some(StatusTarget::Terminal(_, reason)) => Some(*reason),
9534            _ => None,
9535        };
9536        let mut deferred = self.deferred()?;
9537        let deferred = deferred.get_or_insert_with(Deferred::default);
9538        if let Deferred::Nothing = deferred {
9539            *deferred = Deferred::Held {
9540                board: board_id.clone(),
9541                created: Vec::new(),
9542            };
9543        }
9544        let Deferred::Held { created, .. } = deferred else {
9545            unreachable!("held back on the line above");
9546        };
9547        let held = match created.iter().position(|held| held.content == *content) {
9548            Some(at) => &mut created[at],
9549            None => {
9550                created.push(HeldBack {
9551                    content: content.clone(),
9552                    fields: Vec::new(),
9553                    status: None,
9554                    clears: Vec::new(),
9555                    close: None,
9556                    edges: Vec::new(),
9557                });
9558                created.last_mut().expect("just held")
9559            }
9560        };
9561        for (field, value) in fields {
9562            held.fields.retain(|(held, _)| *held != field);
9563            held.clears.retain(|held| *held != field);
9564            held.fields.push((field, value));
9565        }
9566        for field in clears {
9567            held.fields.retain(|(held, _)| held != field);
9568            if !held.clears.iter().any(|held| held == field) {
9569                held.clears.push(field.to_owned());
9570            }
9571        }
9572        if status.is_some() {
9573            held.status = status;
9574        }
9575        if close.is_some() {
9576            held.close = close;
9577        }
9578        // Nothing of an item created in this command has been sent but its create, so the edges
9579        // it is to have are exactly the ones its latest write names.
9580        held.edges = native.iter().map(|far| NativeId(far.clone())).collect();
9581        Ok(())
9582    }
9583
9584    /// Create every task of `writes`, together, filed on the board and under each one's
9585    /// project: [`graphql::CREATE_ISSUES`], [`MUTATION_BATCH`] to a request.
9586    ///
9587    /// Each is prepared as [`Self::write_item`] prepares one — every refusal a write owes made
9588    /// before the first request — then created, then completed with its parts held back. A
9589    /// request refused part way leaves the issues it did create, which are taken back, with the
9590    /// rest of this call's, before the refusal is answered.
9591    async fn create_together(
9592        &self,
9593        writes: &[ItemWrite<Task>],
9594    ) -> Result<Vec<NativeId>, SourceError> {
9595        let incomings: Vec<Incoming<'_>> = writes
9596            .iter()
9597            .map(|write| self.task_incoming(write, None))
9598            .collect::<Result<_, _>>()?;
9599        let mut prepared = Vec::with_capacity(writes.len());
9600        for (incoming, write) in incomings.iter().zip(writes) {
9601            if write.target.is_some() {
9602                return Err(SourceError::Malformed {
9603                    message: "a write naming its target was handed to a batch of creates".into(),
9604                });
9605            }
9606            prepared.push(
9607                self.prepare_write(incoming, None, &write.depends_on)
9608                    .await?,
9609            );
9610        }
9611        let mut inputs = Vec::with_capacity(prepared.len());
9612        for (incoming, prepared) in incomings.iter().zip(&prepared) {
9613            let WriteDestination::New(target) = &prepared.destination else {
9614                return Err(SourceError::Malformed {
9615                    message: "an update was decided for a write handed to a batch of creates"
9616                        .into(),
9617                });
9618            };
9619            let repository_id = self.repository_id(target, incoming).await?;
9620            inputs.push(filed_input(
9621                &repository_id,
9622                prepared.board.id.as_str(),
9623                incoming,
9624                &prepared.body,
9625            ));
9626        }
9627        let mut landed: Vec<Landed> = Vec::with_capacity(inputs.len());
9628        let refused = 'create: {
9629            for chunk in inputs.chunks(MUTATION_BATCH) {
9630                let mut variables = serde_json::Map::new();
9631                for slot in 0..MUTATION_BATCH {
9632                    variables.insert(
9633                        format!("create{slot}"),
9634                        chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
9635                    );
9636                    variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
9637                }
9638                let data = match self
9639                    .graphql(graphql::CREATE_ISSUES, Value::Object(variables))
9640                    .await
9641                {
9642                    Ok(data) => data,
9643                    Err(error) => break 'create Some(error),
9644                };
9645                let mut refusal = data.get(ANSWER_ERRORS).map(|errors| {
9646                    let said: Vec<&str> = errors
9647                        .as_array()
9648                        .into_iter()
9649                        .flatten()
9650                        .filter_map(|error| error.get("message").and_then(Value::as_str))
9651                        .collect();
9652                    SourceError::Refused {
9653                        message: if said.is_empty() {
9654                            "GitHub returned GraphQL errors".to_owned()
9655                        } else {
9656                            said.join("; ")
9657                        },
9658                    }
9659                });
9660                for slot in 0..chunk.len() {
9661                    match data
9662                        .pointer(&format!("/create{slot}/issue"))
9663                        .filter(|value| !value.is_null())
9664                    {
9665                        Some(created) => match self.created_landed(created).await {
9666                            Ok(created) => landed.push(created),
9667                            Err(error) => {
9668                                refusal.get_or_insert(error);
9669                            }
9670                        },
9671                        // An alias GitHub refused answers nothing, and the refusal says why.
9672                        None => {
9673                            refusal.get_or_insert(SourceError::Malformed {
9674                                message: "GitHub issue creation returned no issue".into(),
9675                            });
9676                        }
9677                    }
9678                }
9679                if let Some(error) = refusal {
9680                    break 'create Some(error);
9681                }
9682            }
9683            None
9684        };
9685        if let Some(error) = refused {
9686            // None of these ids reaches the caller, so an issue GitHub will not take back is
9687            // named here or by nobody.
9688            let mut left: Vec<&str> = Vec::new();
9689            for created in &landed {
9690                if self.delete_issue(&created.content_id).await.is_err() {
9691                    left.push(created.content_id.0.as_str());
9692                }
9693            }
9694            return Err(left_behind(error, &left));
9695        }
9696        let created: Vec<NativeId> = landed
9697            .iter()
9698            .map(|landed| landed.content_id.clone())
9699            .collect();
9700        let mut ids = Vec::with_capacity(landed.len());
9701        for ((incoming, prepared), landed) in incomings.iter().zip(prepared).zip(landed) {
9702            match self.complete_write(incoming, prepared, landed).await {
9703                Ok(written) => ids.push(written.id),
9704                // 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.
9705                Err(error) => {
9706                    let mut left: Vec<&str> = Vec::new();
9707                    for id in &created {
9708                        if self.delete_issue(id).await.is_err() {
9709                            left.push(id.0.as_str());
9710                        }
9711                    }
9712                    return Err(left_behind(error, &left));
9713                }
9714            }
9715        }
9716        Ok(ids)
9717    }
9718
9719    /// Send everything held back since [`TaskSource::begin_deferred_writes`]: one read of the
9720    /// created items' board items, then their field values, closes and edges, each kind in
9721    /// requests of [`MUTATION_BATCH`]. Anything refused takes back every issue held, and
9722    /// answers the refusal.
9723    async fn send_held_back(&self) -> Result<(), SourceError> {
9724        let Some(Deferred::Held { board, created }) = self.deferred()?.take() else {
9725            return Ok(());
9726        };
9727        if created.is_empty() {
9728            return Ok(());
9729        }
9730        let sent = self.send_deferred(board.as_str(), &created).await;
9731        if let Err(error) = sent {
9732            // Best effort, and the failure is what the caller is told, as a write of one item
9733            // that fails part way takes back what it created: the copy that held these back
9734            // journalled every one, so one this cannot take back its undo tries again, and says
9735            // so when it cannot either.
9736            for held in &created {
9737                let _ = self.delete_issue(&held.content).await;
9738            }
9739            return Err(error);
9740        }
9741        Ok(())
9742    }
9743
9744    /// The requests [`Self::send_held_back`] makes.
9745    async fn send_deferred(&self, board: &str, created: &[HeldBack]) -> Result<(), SourceError> {
9746        let items = self.created_board_items(board, created).await?;
9747        let mut fields: Vec<Value> = Vec::new();
9748        let mut closes: Vec<Value> = Vec::new();
9749        let mut edges: Vec<Value> = Vec::new();
9750        for held in created {
9751            let (item, status) = &items[&held.content];
9752            let mut values = held.fields.clone();
9753            // A status the board already shows — set by a workflow a person turned on to file
9754            // new items at it — is not written again.
9755            if let Some((field, option, name)) = &held.status
9756                && status.as_deref() != Some(name.as_str())
9757            {
9758                values.insert(0, (field.clone(), json!({"singleSelectOptionId": option})));
9759            }
9760            for (field, value) in values {
9761                fields.push(json!({"projectId":board,"itemId":item,"fieldId":field,"value":value}));
9762            }
9763            if let Some(reason) = held.close {
9764                closes.push(json!({"id": held.content.0, "stateInput": closed_input(reason)}));
9765            }
9766            for far in &held.edges {
9767                edges.push(json!({"issueId": held.content.0, "blockingIssueId": far.0}));
9768            }
9769            if !held.clears.is_empty() {
9770                let clears: Vec<&str> = held.clears.iter().map(String::as_str).collect();
9771                self.set_item_fields(board, item, &[], &clears).await?;
9772            }
9773        }
9774        // Each alias answers with what it wrote, which is what its input named: the board item a
9775        // field value is set on, the issue closed, the issue blocked and the one blocking it.
9776        let field_answer: &[(&str, &str)] = &[("projectV2Item", "itemId")];
9777        let close_answer: &[(&str, &str)] = &[("issue", "id")];
9778        let block_answer: &[(&str, &str)] =
9779            &[("issue", "issueId"), ("blockingIssue", "blockingIssueId")];
9780        for (document, prefix, inputs, answers) in [
9781            (graphql::WRITE_FIELD_VALUES, "field", &fields, field_answer),
9782            (graphql::CLOSE_ISSUES, "close", &closes, close_answer),
9783            (graphql::ADD_BLOCKED_BYS, "block", &edges, block_answer),
9784        ] {
9785            for chunk in inputs.chunks(MUTATION_BATCH) {
9786                let mut variables = serde_json::Map::new();
9787                for slot in 0..MUTATION_BATCH {
9788                    variables.insert(
9789                        format!("{prefix}{slot}"),
9790                        chunk.get(slot).cloned().unwrap_or_else(|| chunk[0].clone()),
9791                    );
9792                    variables.insert(format!("send{slot}"), json!(slot < chunk.len()));
9793                }
9794                let data = self.graphql(document, Value::Object(variables)).await?;
9795                for (slot, input) in chunk.iter().enumerate() {
9796                    for (root, named) in answers {
9797                        let answered = data
9798                            .pointer(&format!("/{prefix}{slot}/{root}/id"))
9799                            .and_then(Value::as_str)
9800                            .ok_or_else(|| SourceError::Malformed {
9801                                message: format!(
9802                                    "GitHub answered {} with nothing at {prefix}{slot}",
9803                                    operation_description(document)
9804                                ),
9805                            })?;
9806                        let asked = input.get(named).and_then(Value::as_str).unwrap_or_default();
9807                        if answered != asked {
9808                            return Err(SourceError::Malformed {
9809                                message: format!(
9810                                    "GitHub answered {} at {prefix}{slot} with {answered}, which \
9811                                     is not {asked}, the item it was asked to write",
9812                                    operation_description(document)
9813                                ),
9814                            });
9815                        }
9816                    }
9817                }
9818            }
9819        }
9820        for held in created {
9821            self.learn_item(&held.content, &items[&held.content].0)?;
9822        }
9823        Ok(())
9824    }
9825
9826    /// The board item each of `created` is on this board, with its `Status` as the board set it:
9827    /// [`graphql::CREATED_BOARD_ITEMS`], read again — after a wait of a second, then two, four,
9828    /// eight and sixteen — while GitHub has not filed one yet.
9829    async fn created_board_items(
9830        &self,
9831        board: &str,
9832        created: &[HeldBack],
9833    ) -> Result<BTreeMap<NativeId, (String, Option<String>)>, SourceError> {
9834        let mut found: BTreeMap<NativeId, (String, Option<String>)> = BTreeMap::new();
9835        let mut wait = Duration::from_secs(1);
9836        for attempt in 0..6 {
9837            let unfiled: Vec<&NativeId> = created
9838                .iter()
9839                .map(|held| &held.content)
9840                .filter(|id| !found.contains_key(*id))
9841                .collect();
9842            if unfiled.is_empty() {
9843                return Ok(found);
9844            }
9845            if attempt > 0 {
9846                self.clock
9847                    .sleep_because(wait, pacing::cause::BOARD_FILING)
9848                    .await;
9849                wait = wait.saturating_mul(2);
9850            }
9851            for chunk in unfiled.chunks(COPY_DETAIL_BATCH) {
9852                let mut variables = serde_json::Map::new();
9853                for slot in 0..COPY_DETAIL_BATCH {
9854                    variables.insert(
9855                        format!("id{slot}"),
9856                        json!(chunk.get(slot).unwrap_or(&chunk[0]).0),
9857                    );
9858                    variables.insert(format!("read{slot}"), json!(slot < chunk.len()));
9859                }
9860                variables.insert("boardItems".into(), json!(BOARD_ITEMS_PAGE_SIZE));
9861                variables.insert("statusName".into(), json!(STATUS_FIELD));
9862                let data = self
9863                    .graphql(graphql::CREATED_BOARD_ITEMS, Value::Object(variables))
9864                    .await?;
9865                for (slot, id) in chunk.iter().enumerate() {
9866                    // An answer for another issue than the one asked about says nothing of
9867                    // where that one is filed, and its board item is not this one's.
9868                    let node = data.get(format!("i{slot}")).filter(|node| !node.is_null());
9869                    let named = node.map(|node| node.get("id").and_then(Value::as_str));
9870                    if let Some(named) = named
9871                        && named != Some(id.0.as_str())
9872                    {
9873                        return Err(SourceError::Malformed {
9874                            message: format!(
9875                                "GitHub answered the board items of {} when asked for those of \
9876                                 {}",
9877                                named.unwrap_or("an issue it did not name"),
9878                                id.0
9879                            ),
9880                        });
9881                    }
9882                    // An issue GitHub does not answer yet is not filed yet; one it answers is
9883                    // read whole, its connection, each board item's board and its status, and
9884                    // any of them unreadable is refused rather than read as not filed.
9885                    let Some(node) = node else { continue };
9886                    let malformed = |what: &str| SourceError::Malformed {
9887                        message: format!("GitHub answered the board items of {} with {what}", id.0),
9888                    };
9889                    let nodes = node
9890                        .pointer("/projectItems/nodes")
9891                        .and_then(Value::as_array)
9892                        .ok_or_else(|| malformed("no list of board items"))?;
9893                    let mut item = None;
9894                    for node in nodes {
9895                        let on = node
9896                            .pointer("/project/id")
9897                            .and_then(Value::as_str)
9898                            .ok_or_else(|| malformed("a board item naming no board"))?;
9899                        if on == board {
9900                            item = Some(node);
9901                        }
9902                    }
9903                    if let Some(item) = item {
9904                        let status = match item.get("status") {
9905                            None | Some(Value::Null) => None,
9906                            Some(status) => Some(
9907                                status
9908                                    .get("name")
9909                                    .and_then(Value::as_str)
9910                                    .ok_or_else(|| malformed("a status that names no option"))?
9911                                    .to_owned(),
9912                            ),
9913                        };
9914                        found.insert(
9915                            (*id).clone(),
9916                            (required_nonblank_str(item, "id")?.to_owned(), status),
9917                        );
9918                    }
9919                }
9920            }
9921        }
9922        let unfiled: Vec<&str> = created
9923            .iter()
9924            .map(|held| held.content.0.as_str())
9925            .filter(|id| !found.keys().any(|found| found.0 == *id))
9926            .collect();
9927        // The last read may be the one that finds the rest.
9928        if unfiled.is_empty() {
9929            return Ok(found);
9930        }
9931        Err(SourceError::Unavailable {
9932            message: format!(
9933                "GitHub created {} filed on board {board} and has not added {} to it after 31 \
9934                 seconds: {}; next: run the copy again once GitHub has caught up",
9935                if unfiled.len() == 1 {
9936                    "an issue"
9937                } else {
9938                    "issues"
9939                },
9940                if unfiled.len() == 1 { "it" } else { "them" },
9941                unfiled.join(", ")
9942            ),
9943        })
9944    }
9945
9946    /// The board item id of `content`, which this process created and remembered without one,
9947    /// in every half of its own view.
9948    fn learn_item(&self, content: &NativeId, item: &str) -> Result<(), SourceError> {
9949        if let Some(held) = self.resolved_cache()?.get_mut(content) {
9950            held.item_id = item.to_owned();
9951        }
9952        if let Some(held) = self.created()?.iter_mut().find(|held| held.id == *content) {
9953            held.item_id = item.to_owned();
9954        }
9955        Ok(())
9956    }
9957
9958    /// Ask together which items each of `origins` names, and what creating an item reads; see
9959    /// [`TaskSource::read_ahead_origins_for_copy`]. An origin whose answer runs past its first
9960    /// page is left to its own lookup.
9961    async fn origins_ahead(&self, origins: &[String]) -> Result<(), SourceError> {
9962        let Some(repository) = self.repository.clone() else {
9963            return Ok(());
9964        };
9965        let unasked: Vec<&String> = {
9966            let held = self.narrowed_cache()?;
9967            let mut unasked: Vec<&String> = Vec::new();
9968            for origin in origins {
9969                if !held.contains_key(&Narrowing::Origin(origin.clone()).key())
9970                    && !unasked.contains(&origin)
9971                {
9972                    unasked.push(origin);
9973                }
9974            }
9975            unasked
9976        };
9977        if unasked.is_empty() {
9978            return Ok(());
9979        }
9980        for chunk in unasked.chunks(ORIGIN_BATCH) {
9981            let mut variables = json!({"owner":self.owner,"number":self.project_number,
9982                "type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
9983                "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true,
9984                "repositoryOwner":repository.owner,"repositoryName":repository.name});
9985            for slot in 0..ORIGIN_BATCH {
9986                let origin = chunk.get(slot).unwrap_or(&chunk[0]);
9987                variables[format!("filter{slot}")] =
9988                    json!(format!("{ORIGIN_FIELD}:{}", quoted(origin)));
9989                variables[format!("search{slot}")] = json!(
9990                    self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))))
9991                );
9992                variables[format!("ask{slot}")] = json!(slot < chunk.len());
9993            }
9994            let data = self.graphql(graphql::ORIGIN_LOOKUPS, variables).await?;
9995            self.fields_read(&data)?;
9996            if let Some(node) = data.get("repository").filter(|value| !value.is_null()) {
9997                let id = required_nonblank_str(node, "id")?.to_owned();
9998                self.repository_cache()?.insert(repository.clone(), id);
9999                if let Some(numeric) = node
10000                    .get("databaseId")
10001                    .and_then(Value::as_u64)
10002                    .and_then(std::num::NonZeroU64::new)
10003                {
10004                    self.numeric_repositories
10005                        .lock()
10006                        .await
10007                        .insert(repository.clone(), numeric);
10008                }
10009            }
10010            for (slot, origin) in chunk.iter().enumerate() {
10011                let items = data
10012                    .pointer(&format!("/o{slot}/projectV2/items"))
10013                    .filter(|value| !value.is_null())
10014                    .ok_or_else(|| SourceError::Refused {
10015                        message: format!(
10016                            "GitHub project {}/{} was not found or is not visible to the token",
10017                            self.owner, self.project_number
10018                        ),
10019                    })?;
10020                let searched =
10021                    data.get(format!("s{slot}"))
10022                        .ok_or_else(|| SourceError::Malformed {
10023                            message: "GitHub search response has no search connection".into(),
10024                        })?;
10025                if resumed(items, None)?.has_more() || resumed(searched, None)?.has_more() {
10026                    continue;
10027                }
10028                let mut found: Vec<Resolved> = Vec::new();
10029                for item in optional_nodes(Some(items), "project items")?
10030                    .into_iter()
10031                    .flatten()
10032                {
10033                    if let Some(resolved) = self.resolve(item)?
10034                        && resolved.content_kind == ContentKind::Issue
10035                        && !found.iter().any(|held| held.id == resolved.id)
10036                    {
10037                        found.push(resolved);
10038                    }
10039                }
10040                for node in optional_nodes(Some(searched), "search")?
10041                    .into_iter()
10042                    .flatten()
10043                {
10044                    if let Some(resolved) = self.resolve_issue(node).await?
10045                        && !found.iter().any(|held| held.id == resolved.id)
10046                    {
10047                        found.push(resolved);
10048                    }
10049                }
10050                self.narrowed_cache()?
10051                    .insert(Narrowing::Origin((*origin).clone()).key(), found);
10052            }
10053        }
10054        Ok(())
10055    }
10056
10057    /// Delete one issue, which takes its board item with it.
10058    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
10059        let data = self
10060            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10061            .await?;
10062        data.pointer("/deleteIssue/repository")
10063            .filter(|value| !value.is_null())
10064            .ok_or_else(|| SourceError::Malformed {
10065                message: "GitHub issue deletion returned no repository".into(),
10066            })?;
10067        self.forget(id)?;
10068        Ok(())
10069    }
10070
10071    /// Remove one item this copy created, so a copy that could not finish leaves the board
10072    /// as it found it.
10073    ///
10074    /// Deleting the issue takes its board item with it, so there is no second mutation to
10075    /// keep in step. An id the board does not hold is not an error: the item is already
10076    /// gone, which is the state this asks for. Which that is, is decided by reading the item
10077    /// by its own id — a listing of the board can still be missing an item it holds, and
10078    /// reading that as *already gone* would leave behind the very item this was asked to
10079    /// take back.
10080    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
10081        let Some(item) = self.bound_item(id).await? else {
10082            return Ok(());
10083        };
10084        if item.content_kind == ContentKind::DraftIssue {
10085            return Err(SourceError::Refused {
10086                message: format!(
10087                    "GitHub item {} is a draft, and this source removes an item by deleting \
10088                     its issue; next: remove it from the board by hand",
10089                    id.0
10090                ),
10091            });
10092        }
10093        let data = self
10094            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
10095            .await?;
10096        data.pointer("/deleteIssue/repository")
10097            .filter(|value| !value.is_null())
10098            .ok_or_else(|| SourceError::Malformed {
10099                message: "GitHub issue deletion returned no repository".into(),
10100            })?;
10101        self.forget(id)?;
10102        Ok(())
10103    }
10104
10105    /// The issue a comment call on `task` is about, or `None` when this board holds no such
10106    /// task.
10107    ///
10108    /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
10109    /// read of the task cannot disagree about which ids name one: a project or a document of
10110    /// this board is not a task here either.
10111    ///
10112    /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
10113    /// issues and a draft is not one. It is refused rather than answered with an empty page,
10114    /// which would read as a task nobody has commented on yet.
10115    async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
10116        let cached = self.resolved_cache()?.get(task).cloned();
10117        let Some(item) = (match cached {
10118            Some(item) => Some(item),
10119            None => self.item_by_id(task).await?,
10120        })
10121        .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10122            return Ok(None);
10123        };
10124        if item.content_kind == ContentKind::DraftIssue {
10125            return Err(self.draft_has_no_comments(task));
10126        }
10127        Ok(Some(item.id))
10128    }
10129
10130    /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
10131    /// issues, and a draft is not one.
10132    fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
10133        SourceError::Refused {
10134            message: format!(
10135                "task {} of source {} is a draft item on the board, and GitHub keeps \
10136                 comments on issues alone, so a draft has none to read or write; next: \
10137                 convert the draft to an issue on the board, then comment on the issue it \
10138                 becomes",
10139                task.0, self.name
10140            ),
10141        }
10142    }
10143
10144    /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
10145    /// request — or `None` when this board holds no task by that id.
10146    ///
10147    /// What `task show` and a comment listing read. A draft is a task with no comments, so it
10148    /// is answered with the draft and the refusal, at the price of the draft's own read.
10149    async fn issue_detail(
10150        &self,
10151        id: &NativeId,
10152        page: &PageRequest,
10153    ) -> Result<Option<TaskDetailRead>, SourceError> {
10154        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
10155        let asked = self
10156            .graphql(
10157                graphql::ISSUE_DETAIL,
10158                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
10159                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
10160                       "duplicates":true}),
10161            )
10162            .await;
10163        let data = match asked {
10164            Ok(data) => data,
10165            Err(error) if unresolvable_node(&error) => return Ok(None),
10166            Err(error) => return Err(error),
10167        };
10168        // `node` is null for an id that names nothing, and absent only from an answer this
10169        // source cannot read — never the same thing.
10170        let node = data.get("node").ok_or_else(|| SourceError::Malformed {
10171            message: format!("GitHub answered the read of {} with no node", id.0),
10172        })?;
10173        self.detail_of(id, node, true, after).await
10174    }
10175
10176    /// Several tasks, each with the first page of its comments when `comments` is set, read
10177    /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
10178    /// order.
10179    ///
10180    /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
10181    /// one item at a time, so that id is answered as missing and the others as themselves; any
10182    /// other refusal is every id of that batch's answer.
10183    async fn issue_details(
10184        &self,
10185        ids: &[NativeId],
10186        comments: Option<&PageRequest>,
10187    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10188        let mut read = Vec::with_capacity(ids.len());
10189        for batch in ids.chunks(DETAIL_BATCH) {
10190            match self
10191                .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
10192                .await
10193            {
10194                Ok(data) => {
10195                    for (slot, id) in batch.iter().enumerate() {
10196                        // Every alias asked for is answered, null for an id naming nothing;
10197                        // one missing is an answer this source cannot read.
10198                        let read_one = match data.get(format!("i{slot}")) {
10199                            Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
10200                            None => Err(SourceError::Malformed {
10201                                message: format!(
10202                                    "GitHub answered a batch read with no item for {}",
10203                                    id.0
10204                                ),
10205                            }),
10206                        };
10207                        read.push(read_one);
10208                    }
10209                }
10210                Err(error) if unresolvable_node(&error) => {
10211                    for id in batch {
10212                        read.push(match comments {
10213                            Some(page) => self.issue_detail(id, page).await,
10214                            None => self.task_read(id).await,
10215                        });
10216                    }
10217                }
10218                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
10219            }
10220        }
10221        read
10222    }
10223
10224    /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
10225    async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
10226        Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
10227            task,
10228            comments: None,
10229        }))
10230    }
10231
10232    /// What one node a detail read reached says: the task this board holds by `id`, with the
10233    /// page of comments the node carries when `commented` — or `None` for a node that is no
10234    /// task of this board.
10235    ///
10236    /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
10237    /// and an item this process created answers from this process's own record, which a node
10238    /// read taken moments after the write can still be behind.
10239    async fn detail_of(
10240        &self,
10241        id: &NativeId,
10242        node: &Value,
10243        commented: bool,
10244        after: Option<&str>,
10245    ) -> Result<Option<TaskDetailRead>, SourceError> {
10246        if node.is_null() {
10247            return Ok(None);
10248        }
10249        let draft = optional_str(node, "__typename")? == Some("DraftIssue");
10250        // An issue answered under one id is that id's, or the answer is not one this source
10251        // can report: reporting another issue's task and comments under the qualified id asked
10252        // for would be the one wrong answer here. A draft's own read checks the same.
10253        if !draft
10254            && optional_str(node, "__typename")? == Some("Issue")
10255            && required_str(node, "id")? != id.0
10256        {
10257            return Err(SourceError::Malformed {
10258                message: format!(
10259                    "GitHub answered the read of {} with issue {}",
10260                    id.0,
10261                    required_str(node, "id")?
10262                ),
10263            });
10264        }
10265        let item = if draft {
10266            self.draft_by_id(id).await?
10267        } else {
10268            self.resolve_issue(node).await?
10269        };
10270        // A read that carried the whole of what the issue is blocked by — a copy's — is kept
10271        // for this command, so the dependency read the copy makes next is answered from it.
10272        if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
10273            self.resolved_cache()?.insert(read.id.clone(), read.clone());
10274        }
10275        let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
10276            return Ok(None);
10277        };
10278        let own = self.created()?.iter().find(|own| own.id == *id).cloned();
10279        let task = own.unwrap_or(item).task()?;
10280        let comments = match (commented, draft) {
10281            (false, _) => None,
10282            (true, true) => Some(Err(self.draft_has_no_comments(id))),
10283            (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
10284        };
10285        if commented && !draft && after.is_none() {
10286            self.expected.learn_comments(id, node);
10287        }
10288        Ok(Some(TaskDetailRead { task, comments }))
10289    }
10290
10291    /// Whether the comment `comment` is one of `issue`'s own.
10292    ///
10293    /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
10294    /// comment's id and nothing else: a comment id given against the wrong task would
10295    /// otherwise change a comment on some other issue entirely. An id that names nothing, or
10296    /// names something that is not an issue comment, is a comment this task does not have —
10297    /// which is what GitHub refusing to resolve it means too.
10298    async fn comment_is_on(
10299        &self,
10300        issue: &NativeId,
10301        comment: &NativeId,
10302    ) -> Result<bool, SourceError> {
10303        Ok(self.comment_on(issue, comment).await?.is_some())
10304    }
10305
10306    /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
10307    /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
10308    async fn comment_on(
10309        &self,
10310        issue: &NativeId,
10311        comment: &NativeId,
10312    ) -> Result<Option<String>, SourceError> {
10313        // A write the store's cache expects knows which comments its issue held, and their
10314        // bodies, at the version it expects: what this read would answer.
10315        if let Some(primed) = self.expected.primed_comment(issue, comment) {
10316            return Ok(primed);
10317        }
10318        let asked = self
10319            .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
10320            .await;
10321        let data = match asked {
10322            Ok(data) => data,
10323            Err(error) if unresolvable_node(&error) => return Ok(None),
10324            Err(error) => return Err(error),
10325        };
10326        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
10327            return Ok(None);
10328        };
10329        if optional_str(node, "__typename")? != Some("IssueComment") {
10330            return Ok(None);
10331        }
10332        let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
10333            message: format!("GitHub issue comment {} names no issue", comment.0),
10334        })?;
10335        if required_str(on, "id")? != issue.0 {
10336            return Ok(None);
10337        }
10338        Ok(Some(required_str(node, "body")?.to_owned()))
10339    }
10340
10341    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
10342    async fn partition_edges(
10343        &self,
10344        near_kind: BoardKind,
10345        near_content: ContentKind,
10346        carried: Option<&[Value]>,
10347        depends_on: &[DependencyEdge],
10348    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
10349        let mut native = Vec::new();
10350        let mut fallback = Vec::new();
10351        let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
10352            .iter()
10353            .map(|edge| {
10354                let same_source = edge
10355                    .to
10356                    .source()
10357                    .is_none_or(|source| source == self.name.as_str());
10358                // A qualified id's source segment runs to its *first* colon — `GlobalId` and
10359                // `DependencyEndpoint::source` both read it that way — and a native id may hold
10360                // colons of its own, so the far end is everything after that one separator.
10361                // Splitting at the last would truncate `work:urn:task:7` to `7`.
10362                let far_id = if edge.to.is_qualified() {
10363                    edge.to
10364                        .id()
10365                        .split_once(':')
10366                        .map_or(edge.to.id(), |(_, native)| native)
10367                } else {
10368                    edge.to.id()
10369                };
10370                // One that already blocks the near issue was answered by that issue's own
10371                // read, which carried each of its blockers' kinds — an issue every one — so it
10372                // is not read again.
10373                let blocking = carried.and_then(|nodes| {
10374                    nodes
10375                        .iter()
10376                        .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
10377                });
10378                (edge, far_id, same_source, blocking)
10379            })
10380            .collect();
10381        // Every other same-source far end is read by its own id, exactly as the item it is a
10382        // far end of is: whether this board holds it is that read's answer, never a listing's.
10383        // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
10384        let mut unread: Vec<NativeId> = Vec::new();
10385        for (_, far_id, same_source, blocking) in &far_ends {
10386            let id = NativeId((*far_id).to_owned());
10387            if *same_source && blocking.is_none() && !unread.contains(&id) {
10388                unread.push(id);
10389            }
10390        }
10391        let read: BTreeMap<NativeId, Option<Resolved>> = unread
10392            .iter()
10393            .cloned()
10394            .zip(self.items_by_ids(&unread).await?)
10395            .collect();
10396        for (edge, far_id, same_source, blocking) in far_ends {
10397            let far = match (same_source, blocking) {
10398                (false, _) => None,
10399                (true, Some(node)) => Some(FarEnd {
10400                    kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
10401                        BoardKind::Document
10402                    } else {
10403                        BoardKind::Work(related_kind(node)?)
10404                    },
10405                    content_kind: ContentKind::Issue,
10406                }),
10407                (true, None) => {
10408                    let read = read
10409                        .get(&NativeId(far_id.to_owned()))
10410                        .cloned()
10411                        .flatten()
10412                        .ok_or_else(|| SourceError::Refused {
10413                            message: format!("GitHub dependency item {far_id} was not found"),
10414                        })?;
10415                    Some(FarEnd {
10416                        kind: read.kind,
10417                        content_kind: read.content_kind,
10418                    })
10419                }
10420            };
10421            let far = far.as_ref();
10422            // The caller says which kind the far end is, and this board holds the far end
10423            // itself, so a disagreement is settled here rather than stored: recorded, the
10424            // wrong kind would read back as a cross-level edge that never existed; written
10425            // natively, it would name a relationship of a different level than the caller
10426            // asked for.
10427            //
10428            // A far end this board holds as a *document* fails the same comparison and is
10429            // refused by the same sentence: `ItemKind` has no document variant because
10430            // nothing may point at one, so no caller can name it correctly and the refusal
10431            // is the only honest answer.
10432            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
10433                return Err(SourceError::Refused {
10434                    message: format!(
10435                        "GitHub dependency item {far_id} is a {} of this board, and this item \
10436                         names it as a {}; record the kind it is",
10437                        disagreeing.kind.describes(),
10438                        edge.to.kind.marker()
10439                    ),
10440                });
10441            }
10442            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
10443            // however the far end is spelled — and one classified native here would be
10444            // written nowhere at all, because a draft's native reconciliation never runs.
10445            let native_here = near_content == ContentKind::Issue
10446                && far.is_some_and(|far| {
10447                    far.content_kind == ContentKind::Issue
10448                        && BoardKind::Work(edge.to.kind) == near_kind
10449                });
10450            if native_here {
10451                native.push(far_id.to_owned());
10452            } else {
10453                fallback.push(edge.clone());
10454            }
10455        }
10456        Ok((native, fallback))
10457    }
10458
10459    async fn update_existing(
10460        &self,
10461        item: &Resolved,
10462        incoming: &Incoming<'_>,
10463        body: &Option<String>,
10464        status_target: Option<&StatusTarget>,
10465    ) -> Result<(), SourceError> {
10466        let title = incoming.written_title();
10467        // A terminal status closes the issue here, in the same mutation as its body: its board
10468        // option was selected before this, so a close never lands on an item whose board cannot
10469        // show it.
10470        let mut fields = match item.content_kind {
10471            ContentKind::DraftIssue => json!({"title":title,"body":body}),
10472            ContentKind::Issue => json!({"title":title,"body":body,
10473                                         "stateInput":state_input(status_target)}),
10474        };
10475        // Only the members whose value differs from the item as this write read it are sent,
10476        // and nothing at all when none does — whether or not the store's cache expects a
10477        // version of it. A member left out is one GitHub leaves as it is, so a person's edit
10478        // of it since that read is kept rather than overwritten with what it already held,
10479        // and a re-copy of an unchanged item costs no mutation.
10480        if let Some(members) = fields.as_object_mut() {
10481            if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
10482                members.remove("title");
10483            }
10484            let body_unchanged = match (members.get("body"), item.raw_body.as_deref()) {
10485                (Some(Value::String(sent)), Some(held)) => sent == held,
10486                (Some(Value::Null), held) => held.is_none_or(str::is_empty),
10487                _ => false,
10488            };
10489            if body_unchanged {
10490                members.remove("body");
10491            }
10492            // An issue already in the state the target asks for keeps it: an open one stays
10493            // open, and a closed one whose closure reads as the target's category keeps its
10494            // reason — a duplicate's says more than this write would.
10495            let unchanged_state = match status_target {
10496                Some(StatusTarget::Terminal(_, reason)) => {
10497                    item.closed
10498                        && closed_category(item.reason.as_deref())
10499                            == closed_category(Some(reason.reason()))
10500                }
10501                Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => !item.closed,
10502                None => true,
10503            };
10504            if unchanged_state {
10505                members.remove("stateInput");
10506            }
10507            if members.is_empty() {
10508                return Ok(());
10509            }
10510        }
10511        self.update_content(item.content_kind, &item.id, fields)
10512            .await
10513    }
10514
10515    /// Update one board item's content with exactly `fields` beside its id, through the
10516    /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
10517    /// a draft.
10518    ///
10519    /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
10520    /// is what lets a narrow write carry the one thing it changes and nothing else.
10521    async fn update_content(
10522        &self,
10523        kind: ContentKind,
10524        id: &NativeId,
10525        fields: Value,
10526    ) -> Result<(), SourceError> {
10527        let (operation, id_key, pointer) = match kind {
10528            ContentKind::DraftIssue => (
10529                graphql::UPDATE_DRAFT,
10530                "draftIssueId",
10531                "/updateProjectV2DraftIssue/draftIssue",
10532            ),
10533            ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
10534        };
10535        let mut input = fields;
10536        input[id_key] = json!(id.0);
10537        let data = self.graphql(operation, json!({"input":input})).await?;
10538        let returned = data
10539            .pointer(pointer)
10540            .ok_or_else(|| SourceError::Malformed {
10541                message: "GitHub item update returned no item".into(),
10542            })?;
10543        if required_str(returned, "id")? != id.0 {
10544            return Err(SourceError::Malformed {
10545                message: "GitHub item update returned the wrong item".into(),
10546            });
10547        }
10548        Ok(())
10549    }
10550
10551    /// Creates one issue, files it on the board, and reports what a read of it would say:
10552    /// its content id, its board item id, and the web address GitHub gave it.
10553    ///
10554    /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
10555    /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
10556    /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
10557    /// board item, and the `addProjectV2ItemById` that then had to follow was refused
10558    /// "Content already exists in this project". A terminal status is not written here:
10559    /// `finish_write` selects its option first and closes the issue after, so a close never
10560    /// lands on an item whose board cannot show it.
10561    ///
10562    /// The address and the number come back here because this is the only place either is
10563    /// known before GitHub's own board read catches up — an item this run created answers
10564    /// the reads that follow it out of the record below, and one remembered without them
10565    /// would report no location and no key for the rest of the run.
10566    async fn create_and_file_issue(
10567        &self,
10568        board_id: &str,
10569        repository: &RepositoryTarget,
10570        incoming: &Incoming<'_>,
10571        body: &Option<String>,
10572    ) -> Result<Landed, SourceError> {
10573        let repository_id = self.repository_id(repository, incoming).await?;
10574        let data = self
10575            .graphql(
10576                graphql::CREATE_ISSUE,
10577                json!({"input":{
10578                    "repositoryId":repository_id,"title":incoming.written_title(),"body":body
10579                }}),
10580            )
10581            .await?;
10582        let created = data
10583            .pointer("/createIssue/issue")
10584            .filter(|value| !value.is_null())
10585            .ok_or_else(|| SourceError::Malformed {
10586                message: "GitHub issue creation returned no issue".into(),
10587            })?;
10588        let content_id = NativeId(required_str(created, "id")?.to_owned());
10589        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
10590        // a response without it is not worth failing a landed write over — the item simply
10591        // reports no location until the board read catches up, which is what it did before.
10592        let url = optional_str(created, "url")?.map(str::to_owned);
10593        // The issue exists from here on, so an unreadable number and a refused board
10594        // filing below each try, best effort, to take it back: an issue in the repository
10595        // that is on no board is an item nobody asked for and nothing here would find again.
10596        //
10597        // Its number is optional on the same terms its address is — a landed write is not
10598        // worth failing over a member that came back missing, and such an item reports no
10599        // handle until a board read catches up. A number that is *present* and is not an
10600        // unsigned integer is still a response this source cannot read.
10601        let number = match created_issue_number(created) {
10602            Ok(number) => number,
10603            Err(error) => {
10604                let _ = self.delete_issue(&content_id).await;
10605                return Err(error);
10606            }
10607        };
10608        let added = match self
10609            .graphql(
10610                graphql::ADD_TO_BOARD,
10611                json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
10612            )
10613            .await
10614        {
10615            Ok(added) => added,
10616            Err(error) => {
10617                let _ = self.delete_issue(&content_id).await;
10618                return Err(error);
10619            }
10620        };
10621        let item = added
10622            .pointer("/addProjectV2ItemById/item")
10623            .filter(|value| !value.is_null())
10624            .ok_or_else(|| SourceError::Malformed {
10625                message: "GitHub board addition returned no project item".into(),
10626            })?;
10627        self.note_written(&content_id, url.as_deref());
10628        Ok(Landed {
10629            content_id,
10630            item_id: Some(required_str(item, "id")?.to_owned()),
10631            url,
10632            number,
10633        })
10634    }
10635
10636    /// Move one issue under the project it now belongs to, or out of the one it left.
10637    async fn reparent(
10638        &self,
10639        held: Option<NativeId>,
10640        child: &NativeId,
10641        wanted: Option<&NativeId>,
10642    ) -> Result<(), SourceError> {
10643        if held.as_ref() == wanted {
10644            return Ok(());
10645        }
10646        if let Some(held) = &held {
10647            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
10648                .await?;
10649        }
10650        if let Some(wanted) = wanted {
10651            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
10652                .await?;
10653        }
10654        Ok(())
10655    }
10656
10657    async fn sub_issue(
10658        &self,
10659        operation: &str,
10660        parent: &NativeId,
10661        child: &NativeId,
10662        root: &str,
10663    ) -> Result<(), SourceError> {
10664        let data = self
10665            .graphql(
10666                operation,
10667                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
10668            )
10669            .await?;
10670        let issue =
10671            data.pointer(&format!("/{root}/issue"))
10672                .ok_or_else(|| SourceError::Malformed {
10673                    message: "GitHub sub-issue update returned no issue".into(),
10674                })?;
10675        let sub =
10676            data.pointer(&format!("/{root}/subIssue"))
10677                .ok_or_else(|| SourceError::Malformed {
10678                    message: "GitHub sub-issue update returned no sub-issue".into(),
10679                })?;
10680        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
10681            return Err(SourceError::Malformed {
10682                message: "GitHub sub-issue update returned the wrong issues".into(),
10683            });
10684        }
10685        Ok(())
10686    }
10687
10688    /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
10689    /// whether there was one.
10690    ///
10691    /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
10692    /// relationships are not read: there is nothing a read of them could find.
10693    async fn reconcile_blocked_by(
10694        &self,
10695        content_id: &NativeId,
10696        native: &[String],
10697        issue: Issue<'_>,
10698    ) -> Result<bool, SourceError> {
10699        let current = match issue {
10700            Issue::Created => Vec::new(),
10701            Issue::Existing(Some(held)) => held
10702                .iter()
10703                .map(|far| required_str(far, "id").map(str::to_owned))
10704                .collect::<Result<Vec<_>, _>>()?,
10705            Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
10706        };
10707        let mut changed = false;
10708        for (operation, far_id) in current
10709            .iter()
10710            .filter(|id| !native.contains(id))
10711            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
10712            .chain(
10713                native
10714                    .iter()
10715                    .filter(|id| !current.contains(id))
10716                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
10717            )
10718        {
10719            let data = self
10720                .graphql(
10721                    operation,
10722                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
10723                )
10724                .await?;
10725            let root = if operation == graphql::ADD_BLOCKED_BY {
10726                "addBlockedBy"
10727            } else {
10728                "removeBlockedBy"
10729            };
10730            let issue =
10731                data.pointer(&format!("/{root}/issue"))
10732                    .ok_or_else(|| SourceError::Malformed {
10733                        message: "GitHub dependency update returned no issue".into(),
10734                    })?;
10735            let blocker = data
10736                .pointer(&format!("/{root}/blockingIssue"))
10737                .ok_or_else(|| SourceError::Malformed {
10738                    message: "GitHub dependency update returned no blocking issue".into(),
10739                })?;
10740            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
10741            {
10742                return Err(SourceError::Malformed {
10743                    message: "GitHub dependency update returned the wrong issues".into(),
10744                });
10745            }
10746            changed = true;
10747        }
10748        Ok(changed)
10749    }
10750}
10751
10752/// The board field values one write sends, in the order GitHub runs them, and the fields it
10753/// clears after them: the origin field when it moves, the `Status` option `column` names, the
10754/// priority, then each projected field.
10755fn field_writes<'a>(
10756    existing: Option<&Resolved>,
10757    origin_field: Option<&str>,
10758    origin: &str,
10759    column: Option<(String, String)>,
10760    priority: Option<&'a PriorityWrite>,
10761    projections: &'a [ProjectionWrite],
10762) -> (Vec<(String, Value)>, Vec<&'a str>) {
10763    let mut fields = Vec::new();
10764    if let Some(field_id) = origin_field
10765        && existing.map_or(!origin.is_empty(), |item| {
10766            item.origin.as_deref().unwrap_or("") != origin
10767        })
10768    {
10769        fields.push((field_id.to_owned(), json!({"text":origin})));
10770    }
10771    if let Some((field_id, option_id)) = column {
10772        fields.push((field_id, json!({"singleSelectOptionId":option_id})));
10773    }
10774    let mut clears = Vec::new();
10775    match priority {
10776        Some(PriorityWrite::Select { field, option }) => {
10777            fields.push((field.clone(), json!({"singleSelectOptionId":option})));
10778        }
10779        Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
10780        None => {}
10781    }
10782    for projection in projections {
10783        projection.join(&mut fields, &mut clears);
10784    }
10785    (fields, clears)
10786}
10787
10788/// Everything one write decided before sending anything: see
10789/// [`GitHubProjectsSource::prepare_write`].
10790struct Prepared {
10791    /// What the write lands on.
10792    destination: WriteDestination,
10793    board: BoardFields,
10794    status_target: Option<StatusTarget>,
10795    /// The `Status` field, the option and the option's name the status lands on.
10796    // 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.
10797    column: Option<(String, String, String)>,
10798    priority_write: Option<PriorityWrite>,
10799    projections: Vec<ProjectionWrite>,
10800    projected: BTreeMap<String, String>,
10801    content_kind: ContentKind,
10802    own_repository: Option<Repository>,
10803    /// The far ends of the item's native blocked-by relationships.
10804    // 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.
10805    native: Vec<String>,
10806    slot: BTreeMap<String, Value>,
10807    content: Option<String>,
10808    body: Option<String>,
10809    /// The copy origin the item records, empty for none.
10810    // 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.
10811    origin: String,
10812    origin_field: Option<String>,
10813}
10814
10815/// `error`, which stopped a batch of creates, and — when GitHub refused to take any of them
10816/// back — each issue of `left` it created that is still in the repository, which its caller,
10817/// never handed their ids, could not name.
10818fn left_behind(error: SourceError, left: &[&str]) -> SourceError {
10819    if left.is_empty() {
10820        return error;
10821    }
10822    SourceError::Refused {
10823        message: format!(
10824            "{error}; GitHub then refused to take back {}, which this write created and which \
10825             are still in the repository; next: delete {} before writing again",
10826            left.join(", "),
10827            if left.len() == 1 { "it" } else { "them" }
10828        ),
10829    }
10830}
10831
10832/// What one write lands on: the item it updates, as it stood, or where it creates a new one.
10833enum WriteDestination {
10834    /// The item as it stood, for an update.
10835    Existing(Box<Resolved>),
10836    /// Where a new item is created.
10837    New(RepositoryTarget),
10838}
10839
10840impl WriteDestination {
10841    /// The item an update writes over, as it stood; `None` for a create.
10842    fn existing(&self) -> Option<&Resolved> {
10843        match self {
10844            Self::Existing(item) => Some(item),
10845            Self::New(_) => None,
10846        }
10847    }
10848}
10849
10850/// What a copy has held back of the items it created; see
10851/// [`GitHubProjectsSource::hold_back`].
10852#[derive(Debug, Default)]
10853enum Deferred {
10854    /// Writes are being held back, and nothing has been held yet.
10855    #[default]
10856    Nothing,
10857    /// The items held back, every one of them on `board`.
10858    Held {
10859        /// The board every one of them is on.
10860        board: BoardId,
10861        /// Each created item, in the order it was created.
10862        created: Vec<HeldBack>,
10863    },
10864}
10865
10866impl Deferred {
10867    /// Whether `content` is one of the items held back.
10868    fn holds(&self, content: &NativeId) -> bool {
10869        match self {
10870            Self::Nothing => false,
10871            Self::Held { created, .. } => created.iter().any(|held| held.content == *content),
10872        }
10873    }
10874}
10875
10876/// One created item's parts that land once [`TaskSource::finish_writes`] is called.
10877#[derive(Debug)]
10878struct HeldBack {
10879    /// The issue.
10880    content: NativeId,
10881    /// Its board field values other than its `Status`, in the order they are written.
10882    fields: Vec<(String, Value)>,
10883    /// The `Status` field, and the option and its name that the item's status lands on.
10884    // llmlint: ignore[invalid_states_unrepresentable] The same triple `column_for` returns and `Prepared::column` carries, held back unchanged until it is sent.
10885    status: Option<(String, String, String)>,
10886    /// The fields it clears after those are written.
10887    clears: Vec<String>,
10888    /// The reason it is closed for, once its option is selected; `None` leaves it open.
10889    close: Option<ClosedState>,
10890    /// The issues it is blocked by.
10891    edges: Vec<NativeId>,
10892}
10893
10894/// The member [`answer_with_errors`] carries a response's GraphQL errors under, beside its data:
10895/// no GitHub type has a field of this name.
10896const ANSWER_ERRORS: &str = "onetaskgraphErrors";
10897
10898/// A response's `data` with its GraphQL errors beside it under [`ANSWER_ERRORS`], when it has
10899/// both — or `None`, for an answer [`GitHubProjectsSource::answer`] reads as it always has.
10900///
10901/// What [`graphql::CREATE_ISSUES`] is answered with: GitHub runs a document's mutation fields in
10902/// order and answers each one that ran, so a refusal of one alias leaves the issues the others
10903/// created named in the data beside it — the ids a copy needs to take them back.
10904fn answer_with_errors(body: &str) -> Option<Value> {
10905    let parsed: Value = serde_json::from_str(body).ok()?;
10906    let errors = parsed
10907        .get("errors")?
10908        .as_array()
10909        .filter(|errors| !errors.is_empty())?;
10910    let mut data = parsed.get("data").filter(|data| data.is_object())?.clone();
10911    data[ANSWER_ERRORS] = Value::Array(errors.clone());
10912    Some(data)
10913}
10914
10915/// The `createIssue` input that files the new issue on `board` and, when `incoming` names one,
10916/// under its project — see [`GitHubProjectsSource::create_filed_issue`].
10917fn filed_input(
10918    repository: &str,
10919    board: &str,
10920    incoming: &Incoming<'_>,
10921    body: &Option<String>,
10922) -> Value {
10923    let mut input = json!({"repositoryId":repository,"title":incoming.written_title(),
10924                           "body":body,"projectV2Ids":[board]});
10925    if let Some(parent) = incoming.parent {
10926        input["parentIssueId"] = json!(parent.0);
10927    }
10928    input
10929}
10930
10931/// The name of option `option` of field `field` among a board's `fields`.
10932fn option_name<'a>(fields: &'a Value, field: &str, option: &str) -> Option<&'a str> {
10933    fields
10934        .get("nodes")?
10935        .as_array()?
10936        .iter()
10937        .find(|node| node.get("id").and_then(Value::as_str) == Some(field))?
10938        .get("options")?
10939        .as_array()?
10940        .iter()
10941        .find(|candidate| candidate.get("id").and_then(Value::as_str) == Some(option))?
10942        .get("name")?
10943        .as_str()
10944}
10945
10946/// What a write needs to know of one far end it names: which kind of item it is, and whether
10947/// it is an issue a native relationship can name.
10948struct FarEnd {
10949    kind: BoardKind,
10950    content_kind: ContentKind,
10951}
10952
10953/// Whether the issue one write reconciles was created by that write or was already there.
10954#[derive(Clone, Copy, PartialEq, Eq)]
10955enum Issue<'a> {
10956    /// Created by this write, so it holds no relationships yet.
10957    Created,
10958    /// On the board before this write, holding whatever relationships it holds — the far
10959    /// ends of its whole `blockedBy`, when the read that reached it carried them.
10960    Existing(Option<&'a [Value]>),
10961}
10962
10963/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
10964enum Reached {
10965    /// An issue this board holds, resolved into everything this source reports about it.
10966    Held(Box<Resolved>),
10967    /// Nothing this board holds: no such node, or a node on some other board.
10968    Nothing,
10969    /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
10970    /// again by [`GitHubProjectsSource::draft_by_id`].
10971    Draft,
10972}
10973
10974/// What GitHub says when a string is not a node id it can resolve.
10975///
10976/// Matched because it is the ordinary answer to a project selector naming a project by its
10977/// *name*, and reporting that as a failure would make naming one impossible. It is read
10978/// off the refusal GitHub sent, never guessed from the shape of the string: this source
10979/// does not define the syntax of a GitHub node id and would be wrong about it.
10980const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
10981
10982/// `error` with `note` added to the end of what it says, its kind and every other member
10983/// unchanged — so a caller still branches on the failure that happened, and reads beside it
10984/// what that failure left behind.
10985fn noting(error: SourceError, note: &str) -> SourceError {
10986    match error {
10987        SourceError::Config { message } => SourceError::Config {
10988            message: message + note,
10989        },
10990        SourceError::Auth { message } => SourceError::Auth {
10991            message: message + note,
10992        },
10993        SourceError::Refused { message } => SourceError::Refused {
10994            message: message + note,
10995        },
10996        SourceError::RateLimited {
10997            retry_after_seconds,
10998            message,
10999        } => SourceError::RateLimited {
11000            retry_after_seconds,
11001            message: Some(message.unwrap_or_default() + note),
11002        },
11003        SourceError::Unavailable { message } => SourceError::Unavailable {
11004            message: message + note,
11005        },
11006        SourceError::Malformed { message } => SourceError::Malformed {
11007            message: message + note,
11008        },
11009    }
11010}
11011
11012/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
11013/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
11014fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest, children: bool) -> Value {
11015    let mut variables = serde_json::Map::new();
11016    for slot in 0..COPY_DETAIL_BATCH {
11017        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11018        variables.insert(format!("id{slot}"), json!(id));
11019    }
11020    variables.insert("project".to_owned(), json!(batch.first().map(|id| &id.0)));
11021    variables.insert("children".to_owned(), json!(children));
11022    variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
11023    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11024    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11025    variables.insert("duplicates".to_owned(), json!(true));
11026    Value::Object(variables)
11027}
11028
11029/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
11030/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
11031/// for them.
11032///
11033/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
11034/// is read again at no added price.
11035fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
11036    let mut variables = serde_json::Map::new();
11037    for slot in 0..DETAIL_BATCH {
11038        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
11039        variables.insert(format!("id{slot}"), json!(id));
11040    }
11041    variables.insert(
11042        "first".to_owned(),
11043        json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
11044    );
11045    variables.insert("comments".to_owned(), json!(comments.is_some()));
11046    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
11047    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
11048    variables.insert("duplicates".to_owned(), json!(true));
11049    Value::Object(variables)
11050}
11051
11052/// Whether this refusal is GitHub saying the id names no node at all.
11053fn unresolvable_node(error: &SourceError) -> bool {
11054    matches!(error, SourceError::Refused { message }
11055        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
11056}
11057
11058/// One project name, as a search qualifier which filters on it at the server.
11059///
11060/// Quoted so the whole title is one phrase rather than a bag of words, with the two
11061/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
11062/// the way it documents. A title matched here is still compared for equality afterwards:
11063/// the qualifier narrows what the server sends, and this source decides what it names.
11064fn title_qualifier(name: &str) -> String {
11065    format!("in:title {}", quoted(name))
11066}
11067
11068/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
11069/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
11070/// it documents — so a value holding a qualifier's spelling is searched for rather than
11071/// obeyed.
11072fn quoted(value: &str) -> String {
11073    let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
11074    format!("\"{escaped}\"")
11075}
11076
11077/// The search qualifier for the issues updated at or after `since`.
11078///
11079/// Written to the second, rounded down, which can only widen what the search returns.
11080fn updated_qualifier(since: DateTime<Utc>) -> String {
11081    format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
11082}
11083
11084/// The search terms that narrow a board-scoped issue search to a task query's text and
11085/// metadata predicates, or `None` when it carries neither.
11086///
11087/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
11088/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
11089/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
11090/// matches each in any field the `in:` qualifier names, so a query naming a title search and
11091/// a metadata value searches both fields for both — wider than asked, never narrower, and
11092/// every candidate is confirmed in process afterwards.
11093///
11094/// **This narrows a text search, and that is this source's declared semantics.** GitHub
11095/// matches whole tokens where a substring rule would match inside a word, so an item holding
11096/// the text only inside a longer word is not returned. A text of nothing but whitespace
11097/// matches every item, so it narrows nothing and is not sent.
11098fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
11099    let text = query
11100        .text
11101        .as_ref()
11102        .filter(|text| !text.terms.trim().is_empty());
11103    if text.is_none() && query.metadata.is_empty() {
11104        return None;
11105    }
11106    let (title, body) = match text.map(|text| text.fields) {
11107        None => (false, true),
11108        Some(TextFields::Title) => (true, !query.metadata.is_empty()),
11109        Some(TextFields::Content) => (false, true),
11110        Some(TextFields::TitleOrContent) => (true, true),
11111    };
11112    let fields = match (title, body) {
11113        (true, true) => "in:title,body",
11114        (true, false) => "in:title",
11115        _ => "in:body",
11116    };
11117    let phrases = text
11118        .map(|text| text.terms.clone())
11119        .into_iter()
11120        .chain(
11121            query
11122                .metadata
11123                .iter()
11124                .map(|wanted| as_stored(wanted.value())),
11125        )
11126        .map(|phrase| quoted(&phrase))
11127        .collect::<Vec<_>>();
11128    Some(format!("{fields} {}", phrases.join(" ")))
11129}
11130
11131/// The search terms that narrow a board-scoped issue search to a project or document query's
11132/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
11133/// carrying that text alone is sent as by [`narrowing_qualifiers`].
11134fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
11135    narrowing_qualifiers(&TaskQuery {
11136        text: text.cloned(),
11137        ..TaskQuery::default()
11138    })
11139}
11140
11141/// Refuses a project or document query's text GitHub's issue search cannot find, before
11142/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
11143/// query's.
11144fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
11145    refuse_unsearchable(&TaskQuery {
11146        text: text.cloned(),
11147        ..TaskQuery::default()
11148    })
11149}
11150
11151/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
11152/// before anything is asked of GitHub.
11153///
11154/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
11155/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
11156/// left out, the search is every issue of the board. So this source says it cannot answer
11157/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
11158/// nothing GitHub could search for, and keeps the board read it always had.
11159fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
11160    const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
11161                       letter or digit with a bounded query";
11162    if let Some(text) = &query.text
11163        && !text.terms.trim().is_empty()
11164        && !has_words(&text.terms)
11165    {
11166        return Err(SourceError::Refused {
11167            message: format!(
11168                "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
11169                text.terms
11170            ),
11171        });
11172    }
11173    if let Some(wanted) = query
11174        .metadata
11175        .iter()
11176        .find(|wanted| !has_words(wanted.value()))
11177    {
11178        return Err(SourceError::Refused {
11179            message: format!(
11180                "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
11181                wanted.value(),
11182                std::iter::once(wanted.key())
11183                    .chain(wanted.path().iter().map(String::as_str))
11184                    .collect::<Vec<_>>()
11185                    .join("/"),
11186            ),
11187        });
11188    }
11189    Ok(())
11190}
11191
11192/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
11193fn has_words(phrase: &str) -> bool {
11194    phrase.chars().any(char::is_alphanumeric)
11195}
11196
11197/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
11198///
11199/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
11200/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
11201/// which GitHub's word match would read as different words.
11202fn as_stored(value: &str) -> String {
11203    let encoded = Value::String(value.to_owned()).to_string();
11204    encoded[1..encoded.len() - 1].to_owned()
11205}
11206
11207/// The one narrower question a task query carrying a text, metadata or origin predicate is
11208/// sent as.
11209enum Narrowing {
11210    /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
11211    Origin(String),
11212    /// The board-scoped issue search narrowed by these qualifiers.
11213    Search(String),
11214}
11215
11216impl Narrowing {
11217    /// What this question is remembered under for the length of one command.
11218    fn key(&self) -> String {
11219        match self {
11220            Self::Origin(origin) => format!("origin {origin}"),
11221            Self::Search(also) => format!("search {also}"),
11222        }
11223    }
11224}
11225
11226/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
11227enum Resumed {
11228    /// It reported another page, which starts after this cursor.
11229    More(String),
11230    /// It has ended. Sending this cursor again — the page's own end when it had one, and
11231    /// otherwise the cursor it was reached from — answers an empty page, so the one document
11232    /// can go on walking the other connection.
11233    Ended(Option<String>),
11234}
11235
11236impl Resumed {
11237    /// Whether the connection has another page.
11238    const fn has_more(&self) -> bool {
11239        matches!(self, Self::More(_))
11240    }
11241
11242    /// The cursor to send this connection next.
11243    fn cursor(self) -> Option<String> {
11244        match self {
11245            Self::More(next) => Some(next),
11246            Self::Ended(last) => last,
11247        }
11248    }
11249}
11250
11251/// Where `connection`, reached from `after`, resumes — refused when it reports another page
11252/// with no cursor to it, or from a cursor that does not advance.
11253fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
11254    let info = connection
11255        .get("pageInfo")
11256        .ok_or_else(|| SourceError::Malformed {
11257            message: "GitHub connection has no pageInfo".into(),
11258        })?;
11259    let end = optional_str(info, "endCursor")?;
11260    if required_bool(info, "hasNextPage")? {
11261        let next = end.ok_or_else(|| SourceError::Malformed {
11262            message: "GitHub connection reports another page and no endCursor".into(),
11263        })?;
11264        validate_cursor_progress(after, next)?;
11265        return Ok(Resumed::More(next.to_owned()));
11266    }
11267    Ok(Resumed::Ended(
11268        end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
11269    ))
11270}
11271
11272/// The board, and every item on it this source reports.
11273#[derive(Clone)]
11274struct Board {
11275    id: String,
11276    fields: Value,
11277    items: Vec<Resolved>,
11278}
11279
11280/// What a write needs of the board and nothing more: its node id and its field
11281/// definitions, in the shape a read of the board's own `fields` gives them.
11282///
11283/// Deliberately no items. A write decides which item it writes, which parent it files
11284/// under and which far ends it names by reading each of them by its own id; this is the
11285/// half of the board those reads cannot carry, and holding no item is what keeps it from
11286/// ever being asked whether an item is there.
11287#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
11288struct BoardFields {
11289    id: BoardId,
11290    fields: Value,
11291}
11292
11293/// A board's node id: what a field write and `addProjectV2ItemById` address.
11294///
11295/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
11296/// refused where it is read, and one an item names blank is read as not named at all.
11297#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
11298#[serde(try_from = "String")]
11299struct BoardId(String);
11300
11301impl TryFrom<String> for BoardId {
11302    type Error = String;
11303
11304    /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
11305    fn try_from(id: String) -> Result<Self, Self::Error> {
11306        Self::parse(&id).map_err(|error| error.to_string())
11307    }
11308}
11309
11310/// Where one write left its item, for the record the rest of the command reads it out of.
11311///
11312/// A named record rather than a tuple because the update arm and the create arm each fill
11313/// all four, and two `Option`s of different meaning side by side in a tuple are two
11314/// positions a reader has to count.
11315struct Landed {
11316    /// The issue's own node id, which is the [`NativeId`] this source reports.
11317    content_id: NativeId,
11318    /// The board item's id, which is what a field write addresses — `None` for an issue its
11319    /// create filed on the board itself, whose board item GitHub adds after answering and
11320    /// [`TaskSource::finish_writes`] reads.
11321    // 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.
11322    item_id: Option<String>,
11323    /// The web address GitHub gave the issue, when it gave one.
11324    // 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.
11325    url: Option<String>,
11326    /// The issue's number on its repository, when GitHub reported one.
11327    number: Option<u64>,
11328}
11329
11330impl BoardId {
11331    fn parse(id: &str) -> Result<Self, SourceError> {
11332        if id.trim().is_empty() {
11333            return Err(SourceError::Malformed {
11334                message: "GitHub named a board with a blank node id".into(),
11335            });
11336        }
11337        Ok(Self(id.to_owned()))
11338    }
11339
11340    fn as_str(&self) -> &str {
11341        &self.0
11342    }
11343}
11344
11345impl Board {
11346    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
11347        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
11348        let nodes = fields
11349            .get("nodes")
11350            .and_then(Value::as_array)
11351            .ok_or_else(|| SourceError::Malformed {
11352                message: "GitHub project fields.nodes is not an array".into(),
11353            })?;
11354        Ok(nodes
11355            .iter()
11356            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
11357    }
11358}
11359
11360/// One board item, resolved into everything this source reports about it.
11361///
11362/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
11363/// [`TaskSource::prime_write`] takes back — see [`expected`].
11364#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
11365struct Resolved {
11366    item_id: String,
11367    id: NativeId,
11368    content_kind: ContentKind,
11369    kind: BoardKind,
11370    title: String,
11371    body: Option<String>,
11372    /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
11373    /// that changes the slot alone has to keep byte for byte outside it.
11374    raw_body: Option<String>,
11375    status: Status,
11376    /// The name of the board `Status` option this item sits in, as the board spells it.
11377    option: Option<String>,
11378    /// What its `Priority` field says, read through this instance's mapping.
11379    priority: HeldPriority,
11380    /// Whether this item's issue is closed. A draft has no such state and is never closed.
11381    closed: bool,
11382    /// GitHub's reason for a closed issue's closure, as it spells it — `None` for an open
11383    /// issue, a draft, and an issue closed before GitHub recorded reasons.
11384    #[serde(default)]
11385    reason: Option<String>,
11386    /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
11387    delivers: Vec<TaskRef>,
11388    /// Every task that delivers this one, read out of its slot. Empty for anything not a
11389    /// task.
11390    delivered_by: Vec<TaskRef>,
11391    labels: Vec<Label>,
11392    parent: Option<NativeId>,
11393    // 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.
11394    origin: Option<String>,
11395    /// What each board text field this instance projects metadata onto holds for this item,
11396    /// by field name — only the fields holding a value, an empty text included. What a write compares the
11397    /// item's metadata against, so a field already holding its value is not written again;
11398    /// never read back as metadata, which the slot alone reports.
11399    projected: BTreeMap<String, String>,
11400    /// The issue's own number on its repository, as GitHub reports it.
11401    ///
11402    /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
11403    /// declares none, and a draft is not filed in a repository to be numbered by one — and
11404    /// an issue this run created whose creating mutation answered without one, which is a
11405    /// response GitHub's own schema says cannot happen and which a landed write is not
11406    /// worth failing over. An `Issue` read off the board always has one.
11407    number: Option<u64>,
11408    url: Option<String>,
11409    created_at: Option<DateTime<Utc>>,
11410    updated_at: Option<DateTime<Utc>>,
11411    /// When the board item itself last changed — what a board field write moves — when the
11412    /// read that reached it said.
11413    item_updated_at: Option<DateTime<Utc>>,
11414    /// How many comments the issue holds, when the read that reached it said.
11415    comment_count: Option<u64>,
11416    /// How many relationship events the issue's timeline holds — every dependency, as
11417    /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
11418    /// removed — when the read that reached it said.
11419    relationship_events: Option<u64>,
11420    /// The server's clock on the response that read it — its `Date` header — when the
11421    /// response carried one.
11422    served_at: Option<DateTime<Utc>>,
11423    /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
11424    /// its labels — when the read that reached it carried all three whole.
11425    #[serde(default)]
11426    facets: Option<change_token::FacetsDigest>,
11427    own_repository: Option<Repository>,
11428    repositories: Vec<Repository>,
11429    classification: Classification,
11430    slot: BTreeMap<String, Value>,
11431    /// The node id of the board this item sits on, when the read that reached it said.
11432    board_id: Option<String>,
11433    /// The definition of every board field this item holds a value of, in the shape a read
11434    /// of the board's own `fields` gives one.
11435    ///
11436    /// Only the fields this item has a value in: a field it holds nothing of is not here,
11437    /// which says nothing about whether the board has it.
11438    fields: Vec<Value>,
11439    /// Every field the board this item sits on defines, as its own read of the board's
11440    /// `fields` gives them — when the read that reached the item carried them, which a read
11441    /// of it by its own id does. What a write of it needs of the board, then, needs no read
11442    /// of the board.
11443    board_fields: Option<Value>,
11444    /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
11445    /// selects one — when the read that reached it carried the connection to its end, which a
11446    /// read of it by its own id does for any issue blocked by no more than a page. What a
11447    /// write reconciles that relationship against, and what a read of its forward edges in
11448    /// the same command answers with.
11449    blocked_by: Option<Vec<Value>>,
11450}
11451
11452impl Resolved {
11453    /// The board this item's own read names it on, when that read named one this source can
11454    /// address.
11455    fn named_board(&self) -> Option<BoardId> {
11456        self.board_id
11457            .as_deref()
11458            .and_then(|id| BoardId::parse(id).ok())
11459    }
11460
11461    /// The board's id and every field it defines, when the read that reached this item
11462    /// carried both — which a read of it by its own id does.
11463    fn carried_board(&self) -> Option<BoardFields> {
11464        Some(BoardFields {
11465            id: self.named_board()?,
11466            fields: self.board_fields.clone()?,
11467        })
11468    }
11469
11470    /// Whether this item holds a value of the board field called `name`, and so carries
11471    /// that field's definition. `false` says nothing about whether the board has the field.
11472    fn defines(&self, name: &str) -> bool {
11473        self.fields
11474            .iter()
11475            .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
11476    }
11477
11478    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
11479    /// in a field of its own, and none of the five keys that are only an encoding.
11480    ///
11481    /// The two delivery keys are left out for every kind, not only for a task: they are
11482    /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
11483    /// document carrying one holds nothing a caller's own metadata could mean by it.
11484    fn metadata(&self) -> BTreeMap<String, Value> {
11485        let mut metadata = self.slot.clone();
11486        metadata.remove(Repository::METADATA_KEY);
11487        metadata.remove(DependencyEdge::RECORDED_KEY);
11488        metadata.remove(ItemKind::METADATA_KEY);
11489        metadata.remove(TaskRef::DELIVERS_KEY);
11490        metadata.remove(TaskRef::DELIVERED_BY_KEY);
11491        metadata.remove(Classification::METADATA_KEY);
11492        // The board field is the origin, and the body's copy of it is only a mirror for the
11493        // issue search to find: an item whose field holds none has none, whatever its body
11494        // says, so no reader ever sees two answers.
11495        metadata.remove(ORIGIN_KEY);
11496        if let Some(origin) = &self.origin {
11497            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
11498        }
11499        metadata
11500    }
11501
11502    /// Where this item is, as a link a reader can open.
11503    ///
11504    /// A board is a hosted place and every issue on it has a web address, so that address
11505    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
11506    /// of place it is, so a reader knows to open it rather than to read a file out. It
11507    /// does not replace or derive from `url`: the field goes on reporting exactly what it
11508    /// reported before, and this says what that address *is*.
11509    ///
11510    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
11511    /// rather than a third variant, which is the contract's "the source did not say". An
11512    /// issue this run created is not one of those: its address comes back from the
11513    /// creating mutation, so it is somewhere a reader can open from the moment it exists
11514    /// rather than from whenever the board read catches up.
11515    fn location(&self) -> Option<Location> {
11516        self.url.clone().map(Location::Url)
11517    }
11518
11519    /// The short handle this board's backend shows people for a task: the issue's number
11520    /// alone, as a decimal string.
11521    ///
11522    /// The number alone rather than `owner/repo#1043`, because that is the contract's
11523    /// value for this backend. A draft has no number and so no handle, which is the
11524    /// contract's *absent* rather than a handle of some other shape — and the native
11525    /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
11526    /// derives from.
11527    fn key(&self) -> Option<String> {
11528        self.number.map(|number| number.to_string())
11529    }
11530
11531    /// This item's change token — see [`change_token`] — or `None` when the read that
11532    /// reached it did not carry all six of its parts, which a draft never does, did not say
11533    /// when GitHub served it, or found it changed too recently to have settled.
11534    fn version(&self) -> Option<String> {
11535        match self.content_kind {
11536            ContentKind::Issue => {
11537                let (issue, item) = (self.updated_at?, self.item_updated_at?);
11538                let parts = change_token::Parts {
11539                    issue,
11540                    item,
11541                    comments: self.comment_count?,
11542                    relationships: self.relationship_events?,
11543                    facets: self.facets.clone()?,
11544                    repository: self.own_repository.clone()?,
11545                };
11546                change_token::settled(issue, item, self.served_at?)
11547                    .then(|| change_token::spell(&parts))
11548            }
11549            ContentKind::DraftIssue => None,
11550        }
11551    }
11552
11553    /// Whether its `Priority` field holds a value at all, mapped or not.
11554    fn holds_priority(&self) -> bool {
11555        self.priority != HeldPriority::Read(Priority::None)
11556    }
11557
11558    /// The task this item is.
11559    ///
11560    /// Fails for an item whose `Priority` field holds an option the mapping does not name:
11561    /// reading that as a level would be a guess, and reading it as `none` would let the next
11562    /// copy clear a priority a person set.
11563    fn task(&self) -> Result<Task, SourceError> {
11564        let priority = match &self.priority {
11565            HeldPriority::Read(priority) => *priority,
11566            HeldPriority::Unmapped(option) => {
11567                return Err(SourceError::Malformed {
11568                    message: format!(
11569                        "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
11570                         this source's priority_mapping does not name, so its priority cannot be \
11571                         read; next: name {option:?} under priority_mapping, or move the item to \
11572                         a mapped option",
11573                        self.id,
11574                        self.number
11575                            .map(|number| format!(" (#{number})"))
11576                            .unwrap_or_default()
11577                    ),
11578                });
11579            }
11580        };
11581        Ok(Task {
11582            version: self.version(),
11583            id: self.id.clone(),
11584            key: self.key(),
11585            title: self.title.clone(),
11586            content: self.body.clone(),
11587            status: self.status.clone(),
11588            priority,
11589            labels: self.labels.clone(),
11590            project: self.parent.clone(),
11591            url: self.url.clone(),
11592            location: self.location(),
11593            created_at: self.created_at,
11594            updated_at: self.updated_at,
11595            metadata: self.metadata(),
11596            repositories: self.repositories.clone(),
11597            delivers: self.delivers.clone(),
11598            delivered_by: self.delivered_by.clone(),
11599            classification: self.classification,
11600        })
11601    }
11602
11603    fn project(&self) -> Project {
11604        Project {
11605            version: self.version(),
11606            id: self.id.clone(),
11607            title: self.title.clone(),
11608            content: self.body.clone(),
11609            status: self.status.clone(),
11610            labels: self.labels.clone(),
11611            url: self.url.clone(),
11612            location: self.location(),
11613            created_at: self.created_at,
11614            updated_at: self.updated_at,
11615            metadata: self.metadata(),
11616            repositories: self.repositories.clone(),
11617            classification: self.classification,
11618        }
11619    }
11620
11621    /// The same issue as a document: the project it is filed under, and no status and no
11622    /// dependencies, because a document is not work.
11623    fn document(&self) -> Document {
11624        Document {
11625            version: self.version(),
11626            id: self.id.clone(),
11627            title: self.title.clone(),
11628            content: self.body.clone(),
11629            project: self.parent.clone(),
11630            labels: self.labels.clone(),
11631            url: self.url.clone(),
11632            location: self.location(),
11633            created_at: self.created_at,
11634            updated_at: self.updated_at,
11635            metadata: self.metadata(),
11636            repositories: self.repositories.clone(),
11637            classification: self.classification,
11638        }
11639    }
11640}
11641
11642/// Where one targeted update moves an item's status, and which of its two halves move.
11643struct StatusMove {
11644    /// The board the item's `Status` field is on.
11645    board: BoardId,
11646    /// The `Status` field's id.
11647    field: String,
11648    /// The option's id.
11649    option: String,
11650    /// The option's name, as the board spells it.
11651    name: String,
11652    /// What the status asks of the issue's state.
11653    target: StatusTarget,
11654    /// The status the item reads as once it is there.
11655    landed: Status,
11656    /// Which of the status's two halves differ from what the item holds.
11657    moves: Moves,
11658}
11659
11660/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
11661/// closed state of its issue, or both. A status neither half of which differs is no move at all,
11662/// and is not a value of this type.
11663#[derive(Clone, Copy, PartialEq, Eq)]
11664enum Moves {
11665    /// The option alone.
11666    Option,
11667    /// The issue's state alone: open, closed, or closed with another reason.
11668    State,
11669    /// Both.
11670    Both,
11671}
11672
11673impl Moves {
11674    /// What differs, or `None` when nothing does.
11675    const fn of(option: bool, state: bool) -> Option<Self> {
11676        match (option, state) {
11677            (true, true) => Some(Self::Both),
11678            (true, false) => Some(Self::Option),
11679            (false, true) => Some(Self::State),
11680            (false, false) => None,
11681        }
11682    }
11683
11684    /// Whether the option moves.
11685    const fn option(self) -> bool {
11686        matches!(self, Self::Option | Self::Both)
11687    }
11688
11689    /// Whether the issue's state moves.
11690    const fn state(self) -> bool {
11691        matches!(self, Self::State | Self::Both)
11692    }
11693}
11694
11695/// What one write is, and the status that comes with being it.
11696///
11697/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
11698/// status and a task or a project always has one, so "a document carrying a status" and
11699/// "a task carrying none" are states a write cannot be in rather than states every use
11700/// site below has to defend against.
11701enum Written<'a> {
11702    /// A document, which is not work and so has no status at all.
11703    Document,
11704    /// A task or a project, and the status it is being written with.
11705    Work(ItemKind, &'a Status),
11706}
11707
11708impl Written<'_> {
11709    /// Which of the board's three kinds this write is.
11710    const fn kind(&self) -> BoardKind {
11711        match self {
11712            Self::Document => BoardKind::Document,
11713            Self::Work(kind, _) => BoardKind::Work(*kind),
11714        }
11715    }
11716
11717    /// The status this write carries. A document carries none, so a write of one says
11718    /// nothing about the issue's open or closed state and selects no board `Status`
11719    /// option.
11720    const fn status(&self) -> Option<&Status> {
11721        match self {
11722            Self::Document => None,
11723            Self::Work(_, status) => Some(status),
11724        }
11725    }
11726
11727    /// The status this write carries with the kind whose half of `status_mapping` it is
11728    /// written through.
11729    const fn work_status(&self) -> Option<(ItemKind, &Status)> {
11730        match self {
11731            Self::Document => None,
11732            Self::Work(kind, status) => Some((*kind, status)),
11733        }
11734    }
11735}
11736
11737/// The item being written, in the one shape all three write methods reach.
11738struct Incoming<'a> {
11739    written: Written<'a>,
11740    /// The title a person wrote. A document's goes onto the issue with
11741    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
11742    title: &'a str,
11743    content: Option<&'a str>,
11744    assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
11745    labels: &'a [Label],
11746    metadata: &'a BTreeMap<String, Value>,
11747    repositories: &'a [Repository],
11748    /// Recorded in the slot while private, and nowhere while public.
11749    classification: Classification,
11750    parent: Option<&'a NativeId>,
11751    /// [`Task::delivers`], already checked. Empty for a project or a document, which is
11752    /// what keeps either key out of their slot.
11753    delivers: &'a [TaskRef],
11754    /// [`Task::delivered_by`], already checked. Empty for a project or a document.
11755    delivered_by: &'a [TaskRef],
11756    /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
11757    /// project, a document, and every write to an instance with no `priority_mapping` —
11758    /// which is what keeps such a write's requests exactly what they were before.
11759    priority: Option<Priority>,
11760}
11761
11762/// What one write does to one projected metadata text field, by the field's id.
11763#[derive(Debug, Clone)]
11764enum ProjectionWrite {
11765    /// Write this text.
11766    Set {
11767        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.
11768        text: String,
11769    },
11770    /// Clear the field's value.
11771    Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
11772}
11773
11774impl ProjectionWrite {
11775    /// Add this write to a field write's list of values and list of clears.
11776    fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
11777        match self {
11778            Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
11779            Self::Clear { field } => clears.push(field),
11780        }
11781    }
11782}
11783
11784/// What one write does to an item's `Priority` field.
11785enum PriorityWrite {
11786    /// Select this option of this field.
11787    Select {
11788        /// The `Priority` field's id.
11789        field: String,
11790        /// The mapped option's id.
11791        option: String,
11792    },
11793    /// Clear the field's value, which is what `none` is.
11794    Clear {
11795        /// The `Priority` field's id.
11796        field: String,
11797    },
11798}
11799
11800impl Incoming<'_> {
11801    /// The title this write puts on the issue.
11802    fn written_title(&self) -> String {
11803        match self.written {
11804            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
11805            Written::Work(..) => self.title.to_owned(),
11806        }
11807    }
11808}
11809
11810#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
11811enum ContentKind {
11812    DraftIssue,
11813    Issue,
11814}
11815
11816/// What one board issue is: a document, or the work an [`ItemKind`] names.
11817///
11818/// A type of this source's own rather than an `ItemKind` with a third variant, because
11819/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
11820/// document — the contract keeps a document out of that enum deliberately. Holding the
11821/// board's three answers in one value is what makes every place that asks "which is this?"
11822/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
11823/// two thirds of the board.
11824#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
11825enum BoardKind {
11826    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
11827    Document,
11828    /// Every other issue, and every draft.
11829    Work(ItemKind),
11830}
11831
11832impl BoardKind {
11833    /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
11834    /// sub-issues and carrying the kind marker `marked` in its slot.
11835    ///
11836    /// The design prefix is read *first*, before either of the two rules that separate a
11837    /// project from a task. A document is not work whatever sub-issues it has and whatever
11838    /// marker it carries, and reading the prefix later would make a design issue with none of
11839    /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
11840    /// overrides it: an issue filed under a project is that project's task even when it has
11841    /// sub-issues of its own.
11842    fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
11843        if title.starts_with(DESIGN_TITLE_PREFIX) {
11844            Self::Document
11845        } else if filed {
11846            Self::Work(ItemKind::Task)
11847        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
11848            Self::Work(ItemKind::Project)
11849        } else {
11850            Self::Work(ItemKind::Task)
11851        }
11852    }
11853
11854    /// What the change token's facets call this kind.
11855    const fn facet(self) -> change_token::FacetKind {
11856        match self {
11857            Self::Document => change_token::FacetKind::Document,
11858            Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
11859            Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
11860        }
11861    }
11862
11863    /// Whose half of `status_mapping` an item of this kind reads its status through. A
11864    /// document has no status of its own, so the task half stands in for whatever the issue
11865    /// holds; nothing reports it.
11866    const fn status_kind(self) -> ItemKind {
11867        match self {
11868            Self::Document => ItemKind::Task,
11869            Self::Work(kind) => kind,
11870        }
11871    }
11872
11873    /// How a refusal names this kind to the person reading it.
11874    const fn describes(self) -> &'static str {
11875        match self {
11876            Self::Document => "document",
11877            Self::Work(kind) => kind.marker(),
11878        }
11879    }
11880}
11881
11882/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
11883///
11884/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
11885/// the shared cross-source journeys assert one answer to one question, so two sources
11886/// that disagree about what "carries the label bug" means fail them.
11887fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
11888    let holds = |name: &String| {
11889        labels
11890            .iter()
11891            .any(|label| label.name.eq_ignore_ascii_case(name))
11892    };
11893    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
11894        && filter.all_of.iter().all(holds)
11895        && !filter.none_of.iter().any(holds)
11896}
11897
11898/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
11899/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
11900fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
11901    statuses.is_empty() || statuses.contains(&category)
11902}
11903
11904/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
11905///
11906/// `content` is the item's own prose — the body with this source's trailing metadata
11907/// comment already taken off — so a search never matches an encoding the author of the
11908/// issue never wrote.
11909fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
11910    let terms = query.terms.to_lowercase();
11911    let in_title = title.to_lowercase().contains(&terms);
11912    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
11913    match query.fields {
11914        TextFields::Title => in_title,
11915        TextFields::Content => in_content,
11916        TextFields::TitleOrContent => in_title || in_content,
11917    }
11918}
11919
11920/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
11921///
11922/// The project predicate is passed separately because a read narrowed to one project has
11923/// already answered it by asking *that project* for its own items — and re-applying it
11924/// there would compare the caller's selector, which may be a project's **name**, against
11925/// the id of the project that name resolved to, and keep nothing. Every other read passes
11926/// `query.project` and applies it here, which is what keeps `projects` a predicate this
11927/// source really does apply.
11928fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
11929    labels_match(&task.labels, &query.labels)
11930        && status_matches(task.status.category, &query.statuses)
11931        && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
11932        && match project {
11933            ProjectFilter::Any => true,
11934            ProjectFilter::Orphans => task.project.is_none(),
11935            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
11936        }
11937        && query
11938            .text
11939            .as_ref()
11940            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
11941        // Against the parsed metadata slot, and against the origin field, which is where
11942        // `Resolved::metadata` reads each of them from.
11943        && query.metadata_matches(&task.metadata)
11944        && query.origin_matches(&task.metadata)
11945}
11946
11947fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
11948    labels_match(&project.labels, &query.labels)
11949        && status_matches(project.status.category, &query.statuses)
11950        && query
11951            .text
11952            .as_ref()
11953            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
11954}
11955
11956/// The same three predicates a task query carries, minus the status filter.
11957///
11958/// A document is not work, so it has no status for one to compare against and the query
11959/// type carries none. The project predicate is the same one — a design issue filed under a
11960/// project issue is in that project, and one filed under nothing is in none — so it is
11961/// spelled the same way here rather than answered differently.
11962fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
11963    labels_match(&document.labels, &query.labels)
11964        && match project {
11965            ProjectFilter::Any => true,
11966            ProjectFilter::Orphans => document.project.is_none(),
11967            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
11968        }
11969        && query
11970            .text
11971            .as_ref()
11972            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
11973}
11974
11975/// Why a GitHub item's change token does not vouch for its forward edges.
11976///
11977/// A forward edge names its far end and that far end's kind, and the near item's token moves
11978/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
11979/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
11980/// nothing the near token reads, so an edge kept at the near version could name a far end that
11981/// is no longer what it said.
11982const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
11983     whether it became a document, or its deletion, so its forward edges are always read from \
11984     github";
11985
11986/// The issue-search qualifier that keeps open issues alone, which a status filter naming only
11987/// categories an open issue reads as narrows a board-scoped search with.
11988const OPEN_QUALIFIER: &str = "is:open";
11989
11990/// The board filter `task status reconcile` reads the board through: closed items alone.
11991const CLOSED_FILTER: &str = "is:closed";
11992
11993/// The variables of one [`graphql::ISSUES_BY_REFERENCE`] request for `batch`: each slot names
11994/// one reference, and a slot the batch has none for names its last one again.
11995fn reference_batch(batch: &[IssueRef]) -> Value {
11996    let mut variables = serde_json::Map::new();
11997    for slot in 0..REFERENCE_BATCH {
11998        let issue = batch.get(slot).or(batch.last());
11999        variables.insert(format!("owner{slot}"), json!(issue.map(|i| &i.owner)));
12000        variables.insert(format!("name{slot}"), json!(issue.map(|i| &i.name)));
12001        variables.insert(format!("number{slot}"), json!(issue.map(|i| i.number)));
12002    }
12003    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
12004    variables.insert("statusField".to_owned(), json!(STATUS_FIELD));
12005    variables.insert("duplicates".to_owned(), json!(true));
12006    Value::Object(variables)
12007}
12008
12009/// One issue an issue read by reference answered, in the shape every other read of an issue
12010/// hands its resolver: each board membership's `Status` value, read by name, as the one field
12011/// value that membership carries.
12012///
12013/// A membership's `status` is `null` when its card sits at no option — or on a board with no
12014/// field of that name — and otherwise an option value naming its option; anything else is not
12015/// an answer to this read, and is refused rather than read as a card at no option.
12016fn held_issue(issue: &Value) -> Result<Value, SourceError> {
12017    let malformed = |what: &str| SourceError::Malformed {
12018        message: format!("GitHub answered an issue read by reference with {what}"),
12019    };
12020    let mut issue = issue.clone();
12021    let nodes = issue
12022        .pointer_mut("/projectItems/nodes")
12023        .and_then(Value::as_array_mut)
12024        .ok_or_else(|| malformed("no list of board memberships"))?;
12025    for node in nodes {
12026        let node = node
12027            .as_object_mut()
12028            .ok_or_else(|| malformed("a board membership that is not an object"))?;
12029        let status = match node.get("status") {
12030            None => return Err(malformed("a board membership with no status member")),
12031            Some(Value::Null) => None,
12032            Some(status) if status.get("name").is_some_and(Value::is_string) => {
12033                Some(status.clone())
12034            }
12035            // A single-select value of another field type answers `{}`; only an option value
12036            // carries a name.
12037            Some(status) if status.as_object().is_some_and(serde_json::Map::is_empty) => None,
12038            Some(_) => return Err(malformed("a membership status that names no option")),
12039        };
12040        node.insert(
12041            "fieldValues".to_owned(),
12042            json!({"nodes": status.into_iter().collect::<Vec<_>>(),
12043                   "pageInfo": {"hasNextPage": false}}),
12044        );
12045    }
12046    Ok(issue)
12047}
12048
12049/// The issue one slot of a read by reference answered, `None` when GitHub answered that
12050/// slot's repository or issue `null` — which [`GitHubProjectsSource::answer_unresolved`]
12051/// admitted only beside a `NOT_FOUND` naming that very slot — and a refusal when the answer
12052/// leaves the slot, or its `issue` member, out: an answer missing a member is malformed rather
12053/// than a reference nothing holds.
12054fn referenced_issue(data: &Value, slot: usize) -> Result<Option<&Value>, SourceError> {
12055    let alias = format!("r{slot}");
12056    let repository = data.get(&alias).ok_or_else(|| SourceError::Malformed {
12057        message: format!("GitHub answered a read by reference with no {alias}"),
12058    })?;
12059    if repository.is_null() {
12060        return Ok(None);
12061    }
12062    let issue = repository
12063        .get("issue")
12064        .ok_or_else(|| SourceError::Malformed {
12065            message: format!("GitHub answered {alias} of a read by reference with no issue"),
12066        })?;
12067    if issue.is_null() {
12068        return Ok(None);
12069    }
12070    // `Repository.issue` is typed `Issue`, so anything else is not an answer to this read.
12071    if optional_str(issue, "__typename")? != Some("Issue") {
12072        return Err(SourceError::Malformed {
12073            message: format!(
12074                "GitHub answered {alias} of a read by reference with an issue whose __typename \
12075                 is not Issue"
12076            ),
12077        });
12078    }
12079    Ok(Some(issue))
12080}
12081
12082/// A closure reason as a person reads it: `NOT_PLANNED` as `not planned`.
12083fn spoken_reason(reason: &str) -> String {
12084    reason.to_ascii_lowercase().replace('_', " ")
12085}
12086
12087impl GitHubProjectsSource {
12088    /// Every task and project of this board whose card contradicts its issue's closure, each
12089    /// judged against its own kind's half of `status_mapping`, read through the board's own
12090    /// `is:closed` filter, page by page — never the board's unfiltered `items`.
12091    ///
12092    /// Each item the filter answers is confirmed from its own node: an open one, one whose
12093    /// closure reads `unknown`, and one whose card is already in line are left out whatever
12094    /// the filter let through. The board's fields ride along and are held for the option
12095    /// writes that follow, so those need no read of the board.
12096    async fn contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
12097        let mut after: Option<String> = None;
12098        let mut found = Vec::new();
12099        loop {
12100            let data = self
12101                .graphql(
12102                    graphql::CLOSED_ITEMS,
12103                    json!({"owner":self.owner,"number":self.project_number,
12104                           "first":MAX_PAGE_SIZE,"after":after,"filter":CLOSED_FILTER,
12105                           "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
12106                )
12107                .await?;
12108            let board = data
12109                .pointer("/closedItems/projectV2")
12110                .filter(|value| !value.is_null())
12111                .ok_or_else(|| SourceError::Refused {
12112                    message: format!(
12113                        "GitHub project {}/{} was not found or is not visible to the token",
12114                        self.owner, self.project_number
12115                    ),
12116                })?;
12117            *self.fields_cache()? = Some(BoardFields {
12118                id: BoardId::parse(required_str(board, "id")?)?,
12119                fields: board.get("fields").cloned().unwrap_or(Value::Null),
12120            });
12121            let items = board.get("items").ok_or_else(|| SourceError::Malformed {
12122                message: "GitHub project closed items are missing".into(),
12123            })?;
12124            for item in items
12125                .get("nodes")
12126                .and_then(Value::as_array)
12127                .ok_or_else(|| SourceError::Malformed {
12128                    message: "GitHub project items.nodes is not an array".into(),
12129                })?
12130            {
12131                let Some(resolved) = self.resolve(item)? else {
12132                    continue;
12133                };
12134                let BoardKind::Work(kind) = resolved.kind else {
12135                    continue;
12136                };
12137                let Some((closed_as, name)) = self.statuses.contradicted(
12138                    kind,
12139                    resolved.option.as_deref(),
12140                    resolved.closed,
12141                    resolved.reason.as_deref(),
12142                ) else {
12143                    continue;
12144                };
12145                found.push(StatusContradiction {
12146                    closed_as,
12147                    to: name.to_owned(),
12148                    option: resolved.option.clone(),
12149                    item: match kind {
12150                        ItemKind::Task => ContradictedItem::Task(resolved.task()?),
12151                        ItemKind::Project => ContradictedItem::Project(resolved.project()),
12152                    },
12153                });
12154            }
12155            let info = items
12156                .get("pageInfo")
12157                .ok_or_else(|| SourceError::Malformed {
12158                    message: "GitHub project items have no pageInfo".into(),
12159                })?;
12160            if !required_bool(info, "hasNextPage")? {
12161                return Ok(found);
12162            }
12163            let next = required_str(info, "endCursor")?;
12164            validate_cursor_progress(after.as_deref(), next)?;
12165            after = Some(next.to_owned());
12166        }
12167    }
12168
12169    /// What this board holds for each issue in `issues`, once each in the order given, read
12170    /// [`REFERENCE_BATCH`] references at a time — and nothing else of the board.
12171    ///
12172    /// An issue GitHub cannot find for this source's credential, and one with no item on this
12173    /// board, are both absent. A closure that already says the work will not be done here —
12174    /// not planned, duplicate — is skipped: a landing does not overrule it. An item closed as
12175    /// completed whose card sits at its kind's `done` option is unchanged, and every other item
12176    /// is due a `done` write. A project item — a plan's home card, its issue holding the plan's
12177    /// tasks as sub-issues — is held under exactly these rules, judged against the project
12178    /// kind's half of `status_mapping`.
12179    async fn holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
12180        let mut distinct: Vec<IssueRef> = Vec::with_capacity(issues.len());
12181        for issue in issues {
12182            if !distinct.contains(issue) {
12183                distinct.push(issue.clone());
12184            }
12185        }
12186        let done = |kind: ItemKind| {
12187            self.statuses
12188                .target(kind, StatusCategory::Done)
12189                .option()
12190                .unwrap_or("Done")
12191                .to_owned()
12192        };
12193        // GitHub numbers an issue with a GraphQL `Int`, a signed 32-bit value, so a reference past
12194        // its range names no issue any repository can hold: it is answered absent and never sent,
12195        // where binding it would have GitHub refuse the whole batch.
12196        let readable: Vec<IssueRef> = distinct
12197            .iter()
12198            .filter(|issue| i32::try_from(issue.number).is_ok())
12199            .cloned()
12200            .collect();
12201        let mut holdings = Vec::with_capacity(readable.len());
12202        for batch in readable.chunks(REFERENCE_BATCH) {
12203            let data = self
12204                .graphql(graphql::ISSUES_BY_REFERENCE, reference_batch(batch))
12205                .await?;
12206            for (slot, issue) in batch.iter().enumerate() {
12207                let node = referenced_issue(&data, slot)?;
12208                let resolved = match node {
12209                    Some(node) => self.resolve_issue_as(&held_issue(node)?, false).await?,
12210                    None => None,
12211                };
12212                // A task and a project are both held; a document is not work, so nothing is.
12213                let Some((item, kind)) = resolved.and_then(|item| match item.kind {
12214                    BoardKind::Work(kind) => Some((item, kind)),
12215                    BoardKind::Document => None,
12216                }) else {
12217                    holdings.push(IssueHolding {
12218                        issue: issue.clone(),
12219                        completion: Completion::Absent,
12220                    });
12221                    continue;
12222                };
12223                let task = match kind {
12224                    ItemKind::Task => {
12225                        let held = item.task()?;
12226                        HeldTask {
12227                            id: held.id,
12228                            kind,
12229                            option: item.option.clone(),
12230                            classification: held.classification,
12231                            repositories: held.repositories,
12232                            delivers: held.delivers,
12233                        }
12234                    }
12235                    // A project delivers no task, so what its write keeps in step is nothing.
12236                    ItemKind::Project => {
12237                        let held = item.project();
12238                        HeldTask {
12239                            id: held.id,
12240                            kind,
12241                            option: item.option.clone(),
12242                            classification: held.classification,
12243                            repositories: held.repositories,
12244                            delivers: Vec::new(),
12245                        }
12246                    }
12247                };
12248                let done = done(kind);
12249                let completion = match (item.closed, item.reason.as_deref()) {
12250                    (true, Some(reason @ ("NOT_PLANNED" | "DUPLICATE"))) => Completion::Skipped {
12251                        task,
12252                        reason: format!("closed as {}", spoken_reason(reason)),
12253                    },
12254                    (true, None | Some("COMPLETED"))
12255                        if item
12256                            .option
12257                            .as_deref()
12258                            .is_some_and(|option| option.eq_ignore_ascii_case(&done)) =>
12259                    {
12260                        Completion::Unchanged { task }
12261                    }
12262                    _ => {
12263                        let kept = item.clone();
12264                        self.held_cache
12265                            .lock()
12266                            .map_err(|_| SourceError::Unavailable {
12267                                message: "items read by issue reference were left \
12268                                          inconsistent; run the command again"
12269                                    .into(),
12270                            })?
12271                            .insert(kept.id.clone(), kept);
12272                        Completion::Due {
12273                            task,
12274                            to: done.clone(),
12275                        }
12276                    }
12277                };
12278                holdings.push(IssueHolding {
12279                    issue: issue.clone(),
12280                    completion,
12281                });
12282            }
12283        }
12284        let mut read = holdings.into_iter();
12285        let mut answered = Vec::with_capacity(distinct.len());
12286        for issue in distinct {
12287            if i32::try_from(issue.number).is_ok() {
12288                answered.push(read.next().ok_or_else(|| SourceError::Malformed {
12289                    message: format!("no holding was read for {issue}"),
12290                })?);
12291            } else {
12292                answered.push(IssueHolding {
12293                    issue,
12294                    completion: Completion::Absent,
12295                });
12296            }
12297        }
12298        Ok(answered)
12299    }
12300}
12301
12302#[async_trait::async_trait]
12303impl TaskSource for GitHubProjectsSource {
12304    fn kind(&self) -> &'static str {
12305        KIND
12306    }
12307    fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
12308        change_token::order(a, b)
12309    }
12310    /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
12311    /// member, the change each part of the token moves for. Not its forward edges — see
12312    /// [`FAR_ENDS_UNCOVERED`].
12313    fn version_coverage(&self) -> VersionCoverage {
12314        VersionCoverage {
12315            forward_edges: Coverage::Uncovered {
12316                reason: FAR_ENDS_UNCOVERED.to_owned(),
12317            },
12318            ..VersionCoverage::whole()
12319        }
12320    }
12321    /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
12322    /// and for a far end deleted, so the token sees every change to a task's edges but one — a
12323    /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
12324    /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
12325    /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
12326    // 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.
12327    fn copy_version_coverage(&self) -> VersionCoverage {
12328        VersionCoverage::whole()
12329    }
12330    /// A project's fields, and not its forward edges, for the same reason a task's are not.
12331    fn project_version_coverage(&self) -> ProjectVersionCoverage {
12332        ProjectVersionCoverage {
12333            fields: Coverage::Covered,
12334            forward_edges: Coverage::Uncovered {
12335                reason: FAR_ENDS_UNCOVERED.to_owned(),
12336            },
12337        }
12338    }
12339    /// A document's fields and the manifest of its image assets, which its metadata slot
12340    /// records: both are the issue's body, whose every edit moves the token's issue time.
12341    fn document_version_coverage(&self) -> DocumentVersionCoverage {
12342        DocumentVersionCoverage::whole()
12343    }
12344    fn write_context(&self, id: &NativeId) -> Option<Value> {
12345        self.context_of(id)
12346    }
12347    fn board_context(&self) -> Option<Value> {
12348        self.board_context_held()
12349    }
12350    fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
12351        self.prime(id, context, board)
12352    }
12353    fn unprime(&self, id: &NativeId) {
12354        self.unprime_held(id);
12355    }
12356    fn begin_journal(&self) {
12357        self.expected.begin();
12358    }
12359    fn end_journal(&self) -> Option<WriteJournal> {
12360        Some(self.expected.end())
12361    }
12362    fn capabilities(&self) -> Capabilities {
12363        Capabilities {
12364            projects: Support::Native,
12365            documents: Support::Native,
12366            comments: Support::Native,
12367            assets: Support::Native,
12368            priority: if self.priorities.is_some() {
12369                Support::Native
12370            } else {
12371                Support::Unsupported
12372            },
12373            filter_by_priority: Support::Native,
12374            filter_by_comment_activity: Support::Native,
12375            filter_by_metadata: Support::Native,
12376            filter_by_origin: Support::Native,
12377            reconcile_status: Support::Native,
12378            complete_by_issue: Support::Native,
12379            orphan_tasks: Support::Native,
12380            filter_by_label: Support::Native,
12381            filter_by_status: Support::Native,
12382            search_title: Support::Native,
12383            search_content: Support::Native,
12384            task_dependencies: DependencySupport::BothDirections,
12385            project_dependencies: DependencySupport::BothDirections,
12386            max_page_size: MAX_PAGE_SIZE,
12387        }
12388    }
12389    async fn visibility(
12390        &self,
12391        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
12392    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
12393        self.write_visibility(target).await
12394    }
12395    async fn health(&self) -> Result<Health, SourceError> {
12396        let board = self.board_page(None, 1).await?;
12397        Ok(Health {
12398            reachable: true,
12399            detail: Some(format!(
12400                "reading GitHub project {}/{} ({})",
12401                self.owner,
12402                self.project_number,
12403                required_str(&board, "title")?
12404            )),
12405        })
12406    }
12407    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
12408        self.item_by_id(id)
12409            .await?
12410            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
12411            .map(|item| item.task())
12412            .transpose()
12413    }
12414    async fn task_assets(
12415        &self,
12416        id: &NativeId,
12417    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
12418        self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
12419    }
12420    async fn task_asset(
12421        &self,
12422        id: &NativeId,
12423        name: &onetaskgraph_plugin_api::AssetName,
12424    ) -> Result<Option<Vec<u8>>, SourceError> {
12425        self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
12426            .await
12427    }
12428    async fn set_task_rendering_with_assets(
12429        &self,
12430        id: &NativeId,
12431        content: &str,
12432        provenance: &Value,
12433        _answers: &BTreeMap<String, Value>,
12434        assets: &onetaskgraph_plugin_api::AssetWrite,
12435    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
12436        self.replace_rendering(
12437            id,
12438            BoardKind::Work(ItemKind::Task),
12439            content,
12440            provenance,
12441            Some(assets),
12442        )
12443        .await
12444    }
12445    async fn document_assets(
12446        &self,
12447        id: &NativeId,
12448    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
12449        self.held_assets(id, BoardKind::Document).await
12450    }
12451    async fn document_asset(
12452        &self,
12453        id: &NativeId,
12454        name: &onetaskgraph_plugin_api::AssetName,
12455    ) -> Result<Option<Vec<u8>>, SourceError> {
12456        self.held_asset(id, BoardKind::Document, name).await
12457    }
12458    async fn set_document_rendering_with_assets(
12459        &self,
12460        id: &NativeId,
12461        content: &str,
12462        provenance: &Value,
12463        _answers: &BTreeMap<String, Value>,
12464        assets: &onetaskgraph_plugin_api::AssetWrite,
12465    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
12466        self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
12467            .await
12468    }
12469    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
12470        Ok(self
12471            .item_by_id(id)
12472            .await?
12473            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
12474            .map(|item| item.project()))
12475    }
12476    async fn query_tasks(
12477        &self,
12478        query: &TaskQuery,
12479        page: &PageRequest,
12480    ) -> Result<Page<Task>, SourceError> {
12481        validate_page(page)?;
12482        refuse_unsearchable(query)?;
12483        // A closed issue reads `done`, `cancelled` or — closed for a reason that implies no
12484        // category — `unknown`; every other category reads only off an open issue or a draft. So
12485        // a status filter naming none of those three asks every issue search it sends for open
12486        // issues alone; each candidate's category is still confirmed from its own item below.
12487        let open_only = !query.statuses.is_empty()
12488            && query.statuses.iter().all(|category| {
12489                !matches!(
12490                    category,
12491                    StatusCategory::Done | StatusCategory::Cancelled | StatusCategory::Unknown
12492                )
12493            });
12494        if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
12495            let mut qualifiers: Vec<String> = [
12496                query.commented_since.map(updated_qualifier),
12497                narrowing_qualifiers(query),
12498            ]
12499            .into_iter()
12500            .flatten()
12501            .collect();
12502            // A narrowed search never answers a draft, which is no issue, so narrowing it to
12503            // open issues too leaves out nothing such a search could have answered.
12504            if !qualifiers.is_empty() {
12505                if open_only {
12506                    qualifiers.push(OPEN_QUALIFIER.to_owned());
12507                }
12508                return self.search_tasks(query, page, &qualifiers.join(" ")).await;
12509            }
12510        }
12511
12512        // A read narrowed to one project asks that project for its own tasks, so nothing
12513        // about it costs what the rest of the board holds. A read carrying a text, metadata
12514        // or origin predicate asks GitHub the narrower question those predicates are, and a
12515        // read narrowed to comment activity alone asks the board's own issue search for the
12516        // issues updated since, which is every issue a comment could have been written or
12517        // edited on since. Every other task read is a question about the whole board and is
12518        // answered by reading it.
12519        let (held, membership) = match (&query.project, query.commented_since) {
12520            (ProjectFilter::Is(project), _) => (
12521                match self.held_task_children(project)? {
12522                    // Taken as `project_children` takes the project's own sub-issue read: with
12523                    // every item this process wrote under it since, and none it moved away.
12524                    Some(held) => self.completed_with_written(
12525                        held.into_iter()
12526                            .filter(|child| child.parent.as_ref() == Some(project))
12527                            .collect(),
12528                        |own| own.parent.as_ref() == Some(project),
12529                    )?,
12530                    None => self.project_children(project).await?,
12531                },
12532                // Answered by where these items came from; see `task_matches`.
12533                &ProjectFilter::Any,
12534            ),
12535            (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
12536                match (self.narrowed(query).await?, since) {
12537                    (Some(narrowed), _) => (narrowed, &query.project),
12538                    (None, Some(since)) => (self.updated_since(since).await?, &query.project),
12539                    // The board's own items list every draft; only the search half, which
12540                    // completes them with issues they lag behind on, is narrowed — a closed
12541                    // issue that half leaves out is at no category the filter keeps.
12542                    (None, None) if open_only => (
12543                        self.board_completed_by(Some(OPEN_QUALIFIER)).await?.items,
12544                        &query.project,
12545                    ),
12546                    (None, None) => (self.board().await?.items, &query.project),
12547                }
12548            }
12549        };
12550        // Filtered before paged: a page of a filtered result is a page of the survivors,
12551        // never the survivors of a page.
12552        let mut tasks = Vec::new();
12553        for item in held
12554            .iter()
12555            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
12556        {
12557            let task = item.task()?;
12558            if task_matches(&task, query, membership)
12559                && self.commented_since(item, query.commented_since).await?
12560            {
12561                tasks.push(task);
12562            }
12563        }
12564        Ok(offset_page(
12565            tasks,
12566            numeric_cursor(page.cursor.as_ref())?,
12567            page.limit.min(MAX_PAGE_SIZE) as usize,
12568        ))
12569    }
12570    async fn query_projects(
12571        &self,
12572        query: &ProjectQuery,
12573        page: &PageRequest,
12574    ) -> Result<Page<Project>, SourceError> {
12575        validate_page(page)?;
12576        refuse_unsearchable_text(query.text.as_ref())?;
12577        // The projects a board holds are found by an issue search scoped to that board,
12578        // never by walking the board's own item connection: what tells a project from a
12579        // task is the `parent` each issue carries, which costs nothing to read. A query
12580        // carrying a text asks that search for the text too, so it reads the issues that
12581        // hold it rather than every issue of the board.
12582        let held = match self.text_searched(query.text.as_ref()).await? {
12583            Some(searched) => searched,
12584            None => self.board_issues().await?,
12585        };
12586        let projects = held
12587            .iter()
12588            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
12589            .map(Resolved::project)
12590            .filter(|project| project_matches(project, query))
12591            .collect();
12592        Ok(offset_page(
12593            projects,
12594            numeric_cursor(page.cursor.as_ref())?,
12595            page.limit.min(MAX_PAGE_SIZE) as usize,
12596        ))
12597    }
12598    async fn query_projects_by_origin(
12599        &self,
12600        origin: &str,
12601        page: &PageRequest,
12602    ) -> Result<Page<Project>, SourceError> {
12603        validate_page(page)?;
12604        let projects = self
12605            .copied_from(origin, BoardKind::Work(ItemKind::Project))
12606            .await?
12607            .iter()
12608            .map(Resolved::project)
12609            .collect();
12610        Ok(offset_page(
12611            projects,
12612            numeric_cursor(page.cursor.as_ref())?,
12613            page.limit.min(MAX_PAGE_SIZE) as usize,
12614        ))
12615    }
12616    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
12617        Ok(self
12618            .item_by_id(id)
12619            .await?
12620            .filter(|item| item.kind == BoardKind::Document)
12621            .map(|item| item.document()))
12622    }
12623    async fn query_documents_by_origin(
12624        &self,
12625        origin: &str,
12626        page: &PageRequest,
12627    ) -> Result<Page<Document>, SourceError> {
12628        validate_page(page)?;
12629        let documents = self
12630            .copied_from(origin, BoardKind::Document)
12631            .await?
12632            .iter()
12633            .map(Resolved::document)
12634            .collect();
12635        Ok(offset_page(
12636            documents,
12637            numeric_cursor(page.cursor.as_ref())?,
12638            page.limit.min(MAX_PAGE_SIZE) as usize,
12639        ))
12640    }
12641    async fn query_documents(
12642        &self,
12643        query: &DocumentQuery,
12644        page: &PageRequest,
12645    ) -> Result<Page<Document>, SourceError> {
12646        validate_page(page)?;
12647        // Narrowed to one project, this is the same sub-issue read a task list scoped to
12648        // that project makes — a document filed under a project is a sub-issue of it too,
12649        // and which of them come back is the kind this caller asked for. Unscoped, a query
12650        // carrying a text asks the board-scoped issue search for it, as a task query does,
12651        // and only one carrying none reads the board.
12652        let (held, membership) = match &query.project {
12653            ProjectFilter::Is(project) => (
12654                self.project_children(project).await?,
12655                // Answered by where these items came from; see `task_matches`.
12656                &ProjectFilter::Any,
12657            ),
12658            ProjectFilter::Any | ProjectFilter::Orphans => {
12659                refuse_unsearchable_text(query.text.as_ref())?;
12660                match self.text_searched(query.text.as_ref()).await? {
12661                    Some(searched) => (searched, &query.project),
12662                    None => (self.board().await?.items, &query.project),
12663                }
12664            }
12665        };
12666        // Filtered before paged, exactly as a task read is: a page of a filtered result is
12667        // a page of the survivors, never the survivors of a page.
12668        let documents = held
12669            .iter()
12670            .filter(|item| item.kind == BoardKind::Document)
12671            .map(Resolved::document)
12672            .filter(|document| document_matches(document, query, membership))
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 labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
12681        validate_page(page)?;
12682        let offset = numeric_cursor(page.cursor.as_ref())?;
12683        let mut labels = self
12684            .board()
12685            .await?
12686            .items
12687            .into_iter()
12688            .flat_map(|item| item.labels)
12689            .fold(Vec::new(), |mut all, label| {
12690                if !all.iter().any(|x: &Label| x.id == label.id) {
12691                    all.push(label);
12692                }
12693                all
12694            });
12695        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
12696        Ok(offset_page(
12697            labels,
12698            offset,
12699            page.limit.min(MAX_PAGE_SIZE) as usize,
12700        ))
12701    }
12702    async fn task_dependencies(
12703        &self,
12704        id: &NativeId,
12705        direction: Direction,
12706        page: &PageRequest,
12707    ) -> Result<Page<DependencyEdge>, SourceError> {
12708        self.dependencies(id, ItemKind::Task, direction, page).await
12709    }
12710    async fn project_dependencies(
12711        &self,
12712        id: &NativeId,
12713        direction: Direction,
12714        page: &PageRequest,
12715    ) -> Result<Page<DependencyEdge>, SourceError> {
12716        self.dependencies(id, ItemKind::Project, direction, page)
12717            .await
12718    }
12719
12720    fn writes(&self) -> WriteSupport {
12721        WriteSupport::Supported
12722    }
12723
12724    /// Create or update one task.
12725    ///
12726    /// Its `delivers` and `delivered_by` are checked before anything is read or written —
12727    /// neither may name the task itself or name one task twice — and land in the body's
12728    /// metadata slot under their reserved keys, in place of any caller metadata of those
12729    /// names.
12730    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
12731        self.write_task_assets(write, None)
12732            .await
12733            .map(|written| written.id)
12734    }
12735
12736    async fn read_ahead_origins_for_copy(&self, origins: &[String]) -> Result<(), SourceError> {
12737        self.origins_ahead(origins).await
12738    }
12739
12740    /// The issue's own web address, as the `createIssue` that made it in this command answered
12741    /// it: a task, a project and a document are each an issue here.
12742    fn written_location(
12743        &self,
12744        _record: onetaskgraph_plugin_api::MetadataRecord,
12745        id: &NativeId,
12746    ) -> Option<Location> {
12747        self.written
12748            .lock()
12749            .unwrap_or_else(std::sync::PoisonError::into_inner)
12750            .get(id)
12751            .cloned()
12752            .map(Location::Url)
12753    }
12754
12755    /// [`MUTATION_BATCH`], while writes are held back: [`graphql::CREATE_ISSUES`] creates that many
12756    /// in one request, and a copy's tasks are created together only then, since their fields
12757    /// and edges are what is held back.
12758    fn create_batch_limit(&self) -> usize {
12759        if self.deferring().unwrap_or(false) {
12760            MUTATION_BATCH
12761        } else {
12762            1
12763        }
12764    }
12765
12766    // 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.
12767    async fn write_tasks(&self, writes: &[ItemWrite<Task>]) -> Result<Vec<NativeId>, SourceError> {
12768        if let Some(write) = writes.iter().find(|write| write.target.is_some()) {
12769            return Err(SourceError::Refused {
12770                message: format!(
12771                    "write_tasks creates tasks, and the write of {} names the existing task {}; \
12772                     next: update it with write_task",
12773                    write.item.id,
12774                    write.target.as_ref().map_or("", |target| target.0.as_str())
12775                ),
12776            });
12777        }
12778        if !self.deferring()? {
12779            let mut created = Vec::with_capacity(writes.len());
12780            for write in writes {
12781                match self.write_task(write).await {
12782                    Ok(id) => created.push(id),
12783                    Err(error) => {
12784                        let mut left: Vec<&str> = Vec::new();
12785                        for id in &created {
12786                            if self.delete_issue(id).await.is_err() {
12787                                left.push(id.0.as_str());
12788                            }
12789                        }
12790                        return Err(left_behind(error, &left));
12791                    }
12792                }
12793            }
12794            return Ok(created);
12795        }
12796        self.create_together(writes).await
12797    }
12798
12799    fn begin_deferred_writes(&self) {
12800        if let Ok(mut deferred) = self.deferred.lock() {
12801            deferred.get_or_insert_with(Deferred::default);
12802        }
12803    }
12804
12805    async fn finish_writes(&self) -> Result<(), SourceError> {
12806        self.send_held_back().await
12807    }
12808
12809    async fn write_task_with_assets(
12810        &self,
12811        write: &ItemWrite<Task>,
12812        _answers: Option<&BTreeMap<String, Value>>,
12813        assets: &onetaskgraph_plugin_api::AssetWrite,
12814    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
12815        self.write_task_assets(write, Some(assets)).await
12816    }
12817
12818    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
12819        self.write_item(
12820            &Incoming {
12821                written: Written::Work(ItemKind::Project, &write.item.status),
12822                title: &write.item.title,
12823                content: write.item.content.as_deref(),
12824                assets: None,
12825                labels: &write.item.labels,
12826                metadata: &write.item.metadata,
12827                repositories: &write.item.repositories,
12828                classification: write.item.classification,
12829                parent: None,
12830                delivers: &[],
12831                delivered_by: &[],
12832                priority: None,
12833            },
12834            write.target.as_ref(),
12835            &write.depends_on,
12836        )
12837        .await
12838        .map(|written| written.id)
12839    }
12840
12841    /// Create or update one document, which is one issue titled the way this board spells
12842    /// a document.
12843    ///
12844    /// Everything else is exactly a task write: caller metadata goes to the same canonical
12845    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
12846    /// or a field this board cannot carry is refused by name rather than dropped, a target
12847    /// naming an issue this board does not hold is refused rather than created, and an
12848    /// issue this call created is taken back when the rest of the write fails.
12849    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
12850        self.write_document_assets(write, None)
12851            .await
12852            .map(|written| written.id)
12853    }
12854
12855    async fn write_document_with_assets(
12856        &self,
12857        write: &ItemWrite<Document>,
12858        _answers: Option<&BTreeMap<String, Value>>,
12859        assets: &onetaskgraph_plugin_api::AssetWrite,
12860    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
12861        self.write_document_assets(write, Some(assets)).await
12862    }
12863
12864    /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
12865    /// which reads nothing; then the board's `Status` option. Over an existing item that is
12866    /// read off the item, as the write reads it, and the item is held among this command's
12867    /// resolved records so the write that follows reuses that read rather than repeating it;
12868    /// an item that does not carry the field takes the board's fields, which are held once
12869    /// read. A create is checked against the board's fields only when this command already
12870    /// holds them, because a create reads them together with its repository, in one request,
12871    /// and refuses a missing option before it writes anything.
12872    async fn check_status_write(
12873        &self,
12874        kind: ItemKind,
12875        category: StatusCategory,
12876        target: Option<&NativeId>,
12877    ) -> Result<(), SourceError> {
12878        let status = self.resolved_target(kind, category)?;
12879        if status.option().is_none() {
12880            return Ok(());
12881        }
12882        let fields = match target {
12883            Some(target) => {
12884                // A target this board does not hold is the write's own refusal to make.
12885                let Some(item) = self.bound_item(target).await? else {
12886                    return Ok(());
12887                };
12888                self.resolved_cache()?.insert(target.clone(), item.clone());
12889                self.fields_for(Some(&item), true, false).await?.fields
12890            }
12891            None => {
12892                let held = self
12893                    .board_cache()?
12894                    .as_ref()
12895                    .map(|board| board.fields.clone());
12896                match held.or_else(|| {
12897                    self.fields_cache()
12898                        .ok()
12899                        .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
12900                }) {
12901                    Some(fields) => fields,
12902                    None => return Ok(()),
12903                }
12904            }
12905        };
12906        self.column_for(&fields, kind, category, &status)
12907            .map(|_| ())
12908    }
12909
12910    /// What a write of `status` reads back as: the `Status` option its category maps to for
12911    /// `kind`, and for a closing category the closure that option and its fixed reason read
12912    /// as — exactly what [`BoardStatuses::status`] reports of the item once written. A category
12913    /// this instance has no option for is answered as given, since writing it is refused.
12914    fn status_as_written(&self, kind: ItemKind, status: &Status) -> Status {
12915        match self.statuses.target(kind, status.category) {
12916            StatusTarget::Column(name) => {
12917                self.statuses.status(kind, Some(name.as_str()), false, None)
12918            }
12919            StatusTarget::Terminal(name, reason) => {
12920                self.statuses
12921                    .status(kind, Some(name.as_str()), true, Some(reason.reason()))
12922            }
12923            StatusTarget::Disabled(_) => status.clone(),
12924        }
12925    }
12926
12927    /// Set one task's status alone.
12928    ///
12929    /// An open target reopens a closed issue with an `updateIssue` carrying only its
12930    /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
12931    /// terminal target selects its mapped option, then closes with its fixed reason. No
12932    /// request carries a title, a body or a label. The status
12933    /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
12934    /// what a re-read reports.
12935    async fn set_task_status(
12936        &self,
12937        id: &NativeId,
12938        category: StatusCategory,
12939    ) -> Result<Option<Status>, SourceError> {
12940        self.set_status(id, ItemKind::Task, category).await
12941    }
12942
12943    /// Set one project's status alone, by exactly the paired write a task's status takes, for
12944    /// the project kind: a terminal category selects the project kind's mapped option and then
12945    /// closes the issue with the category's fixed reason — an issue already closed with a
12946    /// reason that reads that category is left as it is — and an open category reopens a closed
12947    /// issue and then selects its option. A board draft is refused a terminal category as a
12948    /// task's is.
12949    async fn set_project_status(
12950        &self,
12951        id: &NativeId,
12952        category: StatusCategory,
12953    ) -> Result<Option<Status>, SourceError> {
12954        self.set_status(id, ItemKind::Project, category).await
12955    }
12956
12957    /// Every task and project whose card contradicts its issue's closure, read through the
12958    /// board's own `is:closed` filter; see [`TaskSource::status_contradictions`].
12959    async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
12960        self.contradictions().await
12961    }
12962
12963    /// What this board holds for each issue reference, one request per fifty of them; see
12964    /// [`TaskSource::issue_holdings`].
12965    async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
12966        self.holdings(issues).await
12967    }
12968
12969    /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
12970    /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
12971    /// for `none`. Refused by an instance with no `priority_mapping`.
12972    async fn set_task_priority(
12973        &self,
12974        id: &NativeId,
12975        priority: Priority,
12976    ) -> Result<Option<Priority>, SourceError> {
12977        self.set_priority(id, priority).await
12978    }
12979
12980    /// Replace one task's content with a single body update that keeps the metadata slot
12981    /// byte for byte.
12982    async fn set_task_content(
12983        &self,
12984        id: &NativeId,
12985        content: &str,
12986    ) -> Result<Option<()>, SourceError> {
12987        self.replace_content(id, content).await
12988    }
12989
12990    /// Replace one task issue's content and its provenance slot entry with a single body
12991    /// update. The answers are not kept: see `replace_rendering`.
12992    async fn set_task_rendering(
12993        &self,
12994        id: &NativeId,
12995        content: &str,
12996        provenance: &Value,
12997        _answers: &BTreeMap<String, Value>,
12998    ) -> Result<Option<()>, SourceError> {
12999        self.replace_rendering(
13000            id,
13001            BoardKind::Work(ItemKind::Task),
13002            content,
13003            provenance,
13004            None,
13005        )
13006        .await
13007        .map(|written| written.map(|_| ()))
13008    }
13009
13010    /// Replace one design-document issue's content and its provenance slot entry, on exactly
13011    /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13012    async fn set_document_rendering(
13013        &self,
13014        id: &NativeId,
13015        content: &str,
13016        provenance: &Value,
13017        _answers: &BTreeMap<String, Value>,
13018    ) -> Result<Option<()>, SourceError> {
13019        self.replace_rendering(id, BoardKind::Document, content, provenance, None)
13020            .await
13021            .map(|written| written.map(|_| ()))
13022    }
13023
13024    /// Replace one project issue's content and its provenance slot entry, on exactly the
13025    /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
13026    async fn set_project_rendering(
13027        &self,
13028        id: &NativeId,
13029        content: &str,
13030        provenance: &Value,
13031        _answers: &BTreeMap<String, Value>,
13032    ) -> Result<Option<()>, SourceError> {
13033        self.replace_rendering(
13034            id,
13035            BoardKind::Work(ItemKind::Project),
13036            content,
13037            provenance,
13038            None,
13039        )
13040        .await
13041        .map(|written| written.map(|_| ()))
13042    }
13043
13044    /// Apply a targeted update with one read of the item and a write only for what differs:
13045    /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
13046    /// and last one `updateIssue` for title, body and state. See `targeted_update`.
13047    async fn update_task(
13048        &self,
13049        id: &NativeId,
13050        update: &TaskUpdate,
13051    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
13052        self.targeted_update(id, update).await
13053    }
13054
13055    /// Replace one task's `delivered_by` with a single body update that changes the
13056    /// metadata slot and nothing outside it.
13057    async fn set_delivered_by(
13058        &self,
13059        id: &NativeId,
13060        delivered_by: &[TaskRef],
13061    ) -> Result<Option<()>, SourceError> {
13062        self.replace_delivered_by(id, delivered_by).await
13063    }
13064
13065    /// Set one key of one task issue's metadata with a single body update that changes the
13066    /// metadata slot and nothing outside it — no title, label, state or board field request —
13067    /// and sends nothing when the task already holds that value under the key.
13068    async fn set_task_metadata(
13069        &self,
13070        id: &NativeId,
13071        key: &MetadataKey,
13072        value: &Value,
13073    ) -> Result<Option<Task>, SourceError> {
13074        Ok(self
13075            .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
13076            .await?
13077            .map(|item| item.task())
13078            .transpose()?)
13079    }
13080
13081    /// Set one key of one project issue's metadata, on exactly the terms of
13082    /// [`set_task_metadata`](TaskSource::set_task_metadata).
13083    async fn set_project_metadata(
13084        &self,
13085        id: &NativeId,
13086        key: &MetadataKey,
13087        value: &Value,
13088    ) -> Result<Option<Project>, SourceError> {
13089        Ok(self
13090            .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
13091            .await?
13092            .map(|item| item.project()))
13093    }
13094
13095    /// Set one key of one design-document issue's metadata, on exactly the terms of
13096    /// [`set_task_metadata`](TaskSource::set_task_metadata).
13097    async fn set_document_metadata(
13098        &self,
13099        id: &NativeId,
13100        key: &MetadataKey,
13101        value: &Value,
13102    ) -> Result<Option<Document>, SourceError> {
13103        Ok(self
13104            .set_slot_key(id, BoardKind::Document, key, value)
13105            .await?
13106            .map(|item| item.document()))
13107    }
13108
13109    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
13110        self.delete_item(id).await
13111    }
13112
13113    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
13114        self.delete_item(id).await
13115    }
13116
13117    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
13118        self.delete_item(id).await
13119    }
13120
13121    /// One page of the task issue's own comments, walked by GitHub's own cursor.
13122    ///
13123    /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
13124    /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
13125    ///
13126    /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
13127    /// board is the read of its comments. A draft this process already resolved is refused
13128    /// without one.
13129    async fn task_comments(
13130        &self,
13131        task: &NativeId,
13132        page: &PageRequest,
13133    ) -> Result<Option<Page<Comment>>, SourceError> {
13134        validate_page(page)?;
13135        let cached = self.resolved_cache()?.get(task).cloned();
13136        if let Some(item) = cached {
13137            if item.kind != BoardKind::Work(ItemKind::Task) {
13138                return Ok(None);
13139            }
13140            if item.content_kind == ContentKind::DraftIssue {
13141                return Err(self.draft_has_no_comments(task));
13142            }
13143        }
13144        match self.issue_detail(task, page).await? {
13145            Some(TaskDetailRead {
13146                comments: Some(comments),
13147                ..
13148            }) => comments,
13149            _ => Ok(None),
13150        }
13151    }
13152
13153    /// Every id's task, with the first page of its comments when `comments` names it:
13154    /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
13155    /// comments in one [`graphql::ISSUE_DETAIL`] request.
13156    async fn get_task_details(
13157        &self,
13158        ids: &[NativeId],
13159        comments: Option<&PageRequest>,
13160    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
13161        if let Some(page) = comments
13162            && let Err(error) = validate_page(page)
13163        {
13164            return ids.iter().map(|_| Err(error.clone())).collect();
13165        }
13166        match (ids, comments) {
13167            ([id], Some(page)) => vec![self.issue_detail(id, page).await],
13168            ([id], None) => vec![self.task_read(id).await],
13169            _ => self.issue_details(ids, comments).await,
13170        }
13171    }
13172
13173    /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
13174    /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
13175    /// request, so the dependency read a copy makes of each next is answered from this one.
13176    async fn get_task_details_for_copy(
13177        &self,
13178        ids: &[NativeId],
13179        comments: &PageRequest,
13180    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
13181        // A page past the first is a page of one task's comments, which the detail read of it
13182        // alone answers.
13183        if comments.cursor.is_some() {
13184            return self.get_task_details(ids, Some(comments)).await;
13185        }
13186        if let Err(error) = validate_page(comments) {
13187            return ids.iter().map(|_| Err(error.clone())).collect();
13188        }
13189        let mut read = Vec::with_capacity(ids.len());
13190        for batch in ids.chunks(COPY_DETAIL_BATCH) {
13191            match self
13192                .graphql(
13193                    graphql::ISSUE_COPY_DETAILS,
13194                    copy_detail_batch(batch, comments, false),
13195                )
13196                .await
13197            {
13198                Ok(data) => {
13199                    for (slot, id) in batch.iter().enumerate() {
13200                        let read_one = match data.get(format!("i{slot}")) {
13201                            Some(node) => self.detail_of(id, node, true, None).await,
13202                            None => Err(SourceError::Malformed {
13203                                message: format!(
13204                                    "GitHub answered a batch read with no item for {}",
13205                                    id.0
13206                                ),
13207                            }),
13208                        };
13209                        read.push(read_one);
13210                    }
13211                }
13212                Err(error) if unresolvable_node(&error) => {
13213                    for id in batch {
13214                        read.push(self.issue_detail(id, comments).await);
13215                    }
13216                }
13217                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
13218            }
13219        }
13220        read
13221    }
13222
13223    /// Every id's item, whatever kind it is, read [`COPY_DETAIL_BATCH`] at a time with
13224    /// [`graphql::ISSUE_COPY_DETAILS`] — the document a copy reads the tasks it carries with —
13225    /// and held for the next read of each by id; see `read_ahead`.
13226    ///
13227    /// What this run wrote is never read: its own record answers, as it does for a read by id.
13228    /// Nor is an item a listing of its project carried whole earlier in this command — see
13229    /// `carried` — which is held in place of reading it.
13230    /// A batch GitHub refuses because one of its ids resolves to no node at all holds nothing,
13231    /// so each of its ids is read on its own and answered as that read answers; a draft, and an
13232    /// answer naming another issue than the one asked for, are left to their own reads too.
13233    async fn read_ahead_for_copy(&self, ids: &[NativeId]) -> Result<(), SourceError> {
13234        let mut unread: Vec<NativeId> = Vec::new();
13235        // Read whole by a listing of its project in this command, which a mutation naming it
13236        // would have dropped: held as a batch would have held it, once every batch is answered.
13237        let mut listed: Vec<NativeId> = Vec::new();
13238        {
13239            let created = self.created()?;
13240            let held = self.read_ahead()?;
13241            let carried = self.carried()?;
13242            for id in ids {
13243                if unread.contains(id)
13244                    || listed.contains(id)
13245                    || held.contains_key(id)
13246                    || created.iter().any(|own| own.id == *id)
13247                {
13248                    continue;
13249                }
13250                if carried.contains_key(id) {
13251                    listed.push(id.clone());
13252                } else {
13253                    unread.push(id.clone());
13254                }
13255            }
13256        }
13257        // Nothing of an item's comments is wanted here: the smallest page keeps the read to
13258        // what the item itself costs.
13259        let comments = PageRequest {
13260            cursor: None,
13261            limit: 1,
13262        };
13263        // Held only once every batch has been answered, so a read-ahead that fails part way
13264        // holds nothing — what a listing carried included, which stays carried — and every
13265        // item is read on its own, as the method promises.
13266        let mut read: Vec<(NativeId, Option<Resolved>)> = Vec::new();
13267        // The issues the first id names as sub-issues, as `ISSUE_COPY_DETAILS`'s `children`
13268        // answered them: what a project copy's orphan check asks, when that first id is its
13269        // project.
13270        let mut children: Option<(NativeId, Vec<(String, String)>)> = None;
13271        for (index, batch) in unread.chunks(COPY_DETAIL_BATCH).enumerate() {
13272            let data = match self
13273                .graphql(
13274                    graphql::ISSUE_COPY_DETAILS,
13275                    copy_detail_batch(batch, &comments, index == 0),
13276                )
13277                .await
13278            {
13279                Ok(data) => data,
13280                Err(error) if unresolvable_node(&error) => continue,
13281                Err(error) => return Err(error),
13282            };
13283            for (slot, id) in batch.iter().enumerate() {
13284                let Some(node) = data.get(format!("i{slot}")) else {
13285                    continue;
13286                };
13287                let held = if node.is_null() {
13288                    None
13289                } else {
13290                    match optional_str(node, "__typename")? {
13291                        Some("DraftIssue") => continue,
13292                        Some("Issue") if required_str(node, "id")? != id.0 => continue,
13293                        _ => {}
13294                    }
13295                    // Remembered below with the rest, so a later batch refused leaves no
13296                    // dependency read answered from this one either.
13297                    self.resolve_issue_as(node, false).await?
13298                };
13299                read.push((id.clone(), held));
13300            }
13301            if index == 0
13302                && let Some(node) = data.get("children").filter(|node| !node.is_null())
13303                && node.get("id").and_then(Value::as_str) == batch.first().map(|id| id.0.as_str())
13304                && node.pointer("/subIssues/pageInfo/hasNextPage") == Some(&Value::Bool(false))
13305            {
13306                // A list GitHub did not answer is not an empty one: it would answer the project's
13307                // task listing with nothing, and the orphan check with no orphans.
13308                let listed = node
13309                    .pointer("/subIssues/nodes")
13310                    .and_then(Value::as_array)
13311                    .ok_or_else(|| SourceError::Malformed {
13312                        message: format!(
13313                            "GitHub answered the sub-issues of project {} with no list of them",
13314                            batch[0].0
13315                        ),
13316                    })?
13317                    .iter()
13318                    .map(|child| {
13319                        Ok((
13320                            required_str(child, "id")?.to_owned(),
13321                            required_str(child, "title")?.to_owned(),
13322                        ))
13323                    })
13324                    .collect::<Result<Vec<_>, SourceError>>()?;
13325                children = Some((batch[0].clone(), listed));
13326            }
13327        }
13328        // The project's tasks are known from this read alone when every child it named is held
13329        // from it, but for a document, which its title says is one and which a task listing
13330        // never answers: then a listing of the project's tasks is answered from them.
13331        if let Some((project, listed)) = children
13332            && read.iter().any(|(id, held)| {
13333                *id == project
13334                    && held
13335                        .as_ref()
13336                        .is_some_and(|held| held.kind == BoardKind::Work(ItemKind::Project))
13337            })
13338        {
13339            let mut tasks = Vec::new();
13340            let mut whole = true;
13341            for (child, title) in &listed {
13342                match read.iter().find(|(id, _)| id.0 == *child) {
13343                    Some((_, Some(held))) if held.parent.as_ref() == Some(&project) => {
13344                        if held.kind == BoardKind::Work(ItemKind::Task) {
13345                            tasks.push(held.clone());
13346                        }
13347                    }
13348                    _ if title.starts_with(DESIGN_TITLE_PREFIX) => {}
13349                    _ => whole = false,
13350                }
13351            }
13352            if whole {
13353                self.task_children()?.insert(project, tasks);
13354            }
13355        }
13356        let mut resolved = self.resolved_cache()?;
13357        let mut ahead = self.read_ahead()?;
13358        let mut carried = self.carried()?;
13359        for item in read.iter().filter_map(|(_, held)| held.as_ref()) {
13360            resolved.insert(item.id.clone(), item.clone());
13361        }
13362        // Taken only if still carried: a mutation naming it while the batches ran dropped it,
13363        // and its next read is then its own.
13364        for id in &listed {
13365            let Some(item) = carried.remove(id) else {
13366                continue;
13367            };
13368            // The whole of what it is blocked by came with it, so the dependency read a copy
13369            // makes next is answered from this one, as a copy's detail read is.
13370            if item.blocked_by.is_some() {
13371                resolved.insert(item.id.clone(), item.clone());
13372            }
13373            read.push((id.clone(), Some(item)));
13374        }
13375        for (id, held) in read {
13376            ahead.insert(id, held);
13377        }
13378        Ok(())
13379    }
13380
13381    /// Add one comment to the task's issue, as the account the token belongs to.
13382    ///
13383    /// The author is refused before anything is sent — not even the task is read — because
13384    /// no answer GitHub could give would make posting under another name than the one asked
13385    /// for the right outcome.
13386    async fn add_comment(
13387        &self,
13388        task: &NativeId,
13389        comment: &NewComment,
13390    ) -> Result<Option<Comment>, SourceError> {
13391        if let Some(author) = &comment.author {
13392            return Err(SourceError::Refused {
13393                message: format!(
13394                    "source {} cannot post a comment as {author:?}: GitHub records the account \
13395                     the token signs in as the author of every comment; next: leave --author \
13396                     out, and the comment is posted as that account",
13397                    self.name
13398                ),
13399            });
13400        }
13401        let Some(issue) = self.commented_issue(task).await? else {
13402            return Ok(None);
13403        };
13404        let data = self
13405            .graphql(
13406                graphql::ADD_COMMENT,
13407                json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
13408            )
13409            .await?;
13410        let subject = data
13411            .pointer("/addComment/subject")
13412            .filter(|value| !value.is_null())
13413            .ok_or_else(|| SourceError::Malformed {
13414                message: "GitHub comment addition returned no subject".into(),
13415            })?;
13416        if required_str(subject, "id")? != issue.0 {
13417            return Err(SourceError::Malformed {
13418                message: "GitHub comment addition answered about another issue".into(),
13419            });
13420        }
13421        let added = data
13422            .pointer("/addComment/commentEdge/node")
13423            .filter(|value| !value.is_null())
13424            .ok_or_else(|| SourceError::Malformed {
13425                message: "GitHub comment addition returned no comment".into(),
13426            })?;
13427        let added = comment_from(added)?;
13428        self.remember_commented(&issue)?;
13429        Ok(Some(added))
13430    }
13431
13432    async fn edit_comment(
13433        &self,
13434        task: &NativeId,
13435        comment: &NativeId,
13436        body: &CommentBody,
13437    ) -> Result<Option<Comment>, SourceError> {
13438        let Some(issue) = self.commented_issue(task).await? else {
13439            return Ok(None);
13440        };
13441        let Some(held) = self.comment_on(&issue, comment).await? else {
13442            return Ok(None);
13443        };
13444        // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
13445        let body = CommentOrigin::kept(&held, body)?;
13446        let data = self
13447            .graphql(
13448                graphql::UPDATE_COMMENT,
13449                json!({"input":{"id":comment.0,"body":body.as_str()}}),
13450            )
13451            .await?;
13452        let edited = data
13453            .pointer("/updateIssueComment/issueComment")
13454            .filter(|value| !value.is_null())
13455            .ok_or_else(|| SourceError::Malformed {
13456                message: "GitHub comment update returned no comment".into(),
13457            })?;
13458        let edited = comment_from(edited)?;
13459        if edited.id != *comment {
13460            return Err(SourceError::Malformed {
13461                message: "GitHub comment update returned the wrong comment".into(),
13462            });
13463        }
13464        self.remember_commented(&issue)?;
13465        Ok(Some(edited))
13466    }
13467
13468    async fn delete_comment(
13469        &self,
13470        task: &NativeId,
13471        comment: &NativeId,
13472    ) -> Result<Option<NativeId>, SourceError> {
13473        let Some(issue) = self.commented_issue(task).await? else {
13474            return Ok(None);
13475        };
13476        if !self.comment_is_on(&issue, comment).await? {
13477            return Ok(None);
13478        }
13479        let data = self
13480            .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
13481            .await?;
13482        // The payload says nothing about the comment it removed, so what is checked is that
13483        // GitHub answered the mutation at all rather than leaving it unanswered.
13484        data.get("deleteIssueComment")
13485            .filter(|value| !value.is_null())
13486            .ok_or_else(|| SourceError::Malformed {
13487                message: "GitHub comment deletion returned no payload".into(),
13488            })?;
13489        Ok(Some(comment.clone()))
13490    }
13491
13492    /// Every request this source has recorded, and what each of GitHub's two budgets was
13493    /// attributed — read off the same accounting the session report is rendered from, so
13494    /// the two cannot count one request two ways.
13495    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
13496        Ok(Some(self.ledger.snapshot().metering()))
13497    }
13498
13499    /// Drop every item, search answer and board read this source holds, so the next command
13500    /// reads the board as a person has since left it.
13501    ///
13502    /// Every one of those is held on the assumption that nothing but this source writes the
13503    /// board while a command runs, which stops being true the moment the command is over: a
13504    /// body a person edited would be overwritten from the record held here, and a card they
13505    /// moved would be read as still where this source left it. The board's own field
13506    /// definitions go too, because a person can add or delete a `Status` option and a write
13507    /// resolved against the held list would not re-read on a miss. What stays is what stays
13508    /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
13509    /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
13510    /// accounting [`metering`](TaskSource::metering) answers from.
13511    ///
13512    /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
13513    /// refused, because clearing it is what puts it right.
13514    async fn end_command(&self) -> Result<(), SourceError> {
13515        fn clear<T: Default>(held: &Mutex<T>) {
13516            *held
13517                .lock()
13518                .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
13519            held.clear_poison();
13520        }
13521        clear(&self.created);
13522        clear(&self.updated);
13523        clear(&self.commented);
13524        clear(&self.board_cache);
13525        clear(&self.search_cache);
13526        clear(&self.narrowed_cache);
13527        clear(&self.search_next);
13528        clear(&self.resolved_cache);
13529        clear(&self.read_ahead);
13530        clear(&self.carried);
13531        clear(&self.held_cache);
13532        clear(&self.children_cache);
13533        clear(&self.task_children);
13534        clear(&self.written);
13535        clear(&self.fields_cache);
13536        self.expected.end_command();
13537        Ok(())
13538    }
13539}
13540
13541/// Each project's sub-issues as one command read them, keyed by the selector they were asked
13542/// for under, beside the project that selector named; see `children_cache`.
13543type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
13544
13545/// One issue comment as the contract carries it.
13546///
13547/// `author` is absent both when GitHub answers `null` for an account that no longer exists
13548/// and when it answers an actor with no login, because either way the source did not say who
13549/// wrote it — which is what an absent author means, rather than an author called nothing.
13550///
13551/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
13552/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
13553/// and is read back with the marker taken out and reported as its origin.
13554fn comment_from(value: &Value) -> Result<Comment, SourceError> {
13555    Ok(Comment {
13556        origin: None,
13557        id: NativeId(required_str(value, "id")?.to_owned()),
13558        author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
13559            .map(str::to_owned),
13560        created_at: optional_time(value, "createdAt")?,
13561        updated_at: optional_time(value, "updatedAt")?,
13562        body: required_str(value, "body")?.to_owned(),
13563        url: optional_str(value, "url")?.map(str::to_owned),
13564    }
13565    .unmarked())
13566}
13567
13568/// The page of comments one issue node carries, resumed from `after`.
13569fn comment_page(
13570    node: &Value,
13571    issue: &str,
13572    after: Option<&str>,
13573) -> Result<Page<Comment>, SourceError> {
13574    let connection = node
13575        .get("comments")
13576        .filter(|value| !value.is_null())
13577        .ok_or_else(|| SourceError::Malformed {
13578            message: format!("GitHub issue {issue} answered with no comments connection"),
13579        })?;
13580    let items = optional_nodes(Some(connection), "issue comments")?
13581        .into_iter()
13582        .flatten()
13583        .map(comment_from)
13584        .collect::<Result<Vec<_>, _>>()?;
13585    let next = next_cursor(connection)?;
13586    if let Some(next) = &next {
13587        validate_cursor_progress(after, &next.0)?;
13588    }
13589    Ok(Page { items, next })
13590}
13591
13592/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
13593/// end — `None` when it carried none, or a page with more past it.
13594fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
13595    let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
13596        return Ok(None);
13597    };
13598    if next_cursor(connection)?.is_some() {
13599        return Ok(None);
13600    }
13601    Ok(Some(
13602        optional_nodes(Some(connection), "blocked-by issues")?
13603            .into_iter()
13604            .flatten()
13605            .cloned()
13606            .collect(),
13607    ))
13608}
13609
13610/// Where the recorded tail of a dependency walk resumes; see
13611/// [`GitHubProjectsSource::recorded_edges`].
13612const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
13613
13614/// The board text field this source keeps a copy's origin in.
13615///
13616/// Named after the key it holds, and held to that name by the guard below rather than by
13617/// a reader noticing.
13618const ORIGIN_FIELD: &str = "onetaskgraph.origin";
13619
13620/// The metadata key that field holds.
13621///
13622/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
13623/// constructs or interprets the qualified id it carries. This source names it only to
13624/// route it — a short, typed value belongs in a typed field rather than in the body slot
13625/// a caller's own prose shares.
13626///
13627/// Restated rather than imported, because no plugin crate may depend on the engine. What
13628/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
13629/// target in `check`: it reads the engine's own literal and fails naming the file and the
13630/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
13631/// that creates a second item every run instead of finding the one it wrote — and that is
13632/// too late to learn it.
13633const ORIGIN_KEY: &str = "onetaskgraph.origin";
13634
13635/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
13636///
13637/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
13638/// is derived from the far end, never written down on the near item — so only a forward
13639/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
13640/// it did not come from, and it is told so rather than answered with an empty page that
13641/// reads as a walk which ended.
13642fn recorded_offset(
13643    cursor: Option<&str>,
13644    direction: Direction,
13645) -> Result<Option<usize>, SourceError> {
13646    cursor
13647        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
13648        .map(|offset| {
13649            if direction != Direction::DependsOn {
13650                return Err(SourceError::Config {
13651                    message: format!(
13652                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
13653                         reverse dependency read never issues; resume it in the direction \
13654                         that reported it"
13655                    ),
13656                });
13657            }
13658            offset.parse().map_err(|_| SourceError::Config {
13659                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
13660            })
13661        })
13662        .transpose()
13663}
13664
13665fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
13666    let mut page = offset_page(edges, offset, limit.max(1));
13667    page.next = page
13668        .next
13669        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
13670    page
13671}
13672
13673/// The kind of one issue reached through a dependency connection.
13674///
13675/// The same questions the board scan asks, over the fields the dependency document
13676/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
13677/// then anything with sub-issues or the marker is a project.
13678///
13679/// # Errors
13680///
13681/// A far end this board holds as a document is refused rather than reported. The two
13682/// answers that are not refusals would both be wrong: reporting it as a task names an id
13683/// no task read of this source can find, and reporting it as a project names one no
13684/// project read can. There is no third value to return — `ItemKind` has no document
13685/// variant, because nothing may point at a document — so the relationship itself is what
13686/// the person is told about.
13687fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
13688    let id = required_str(value, "id")?;
13689    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
13690        return Err(SourceError::Refused {
13691            message: format!(
13692                "GitHub issue {id} is a document of this board — its title begins \
13693                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
13694                 on by one; next: remove that issue's blocking relationship on this board"
13695            ),
13696        });
13697    }
13698    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
13699    if parent.is_some() {
13700        return Ok(ItemKind::Task);
13701    }
13702    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
13703    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
13704        message: format!("GitHub issue {id}: {message}"),
13705    })?;
13706    let sub_issues = sub_issue_total(value)?;
13707    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
13708        ItemKind::Project
13709    } else {
13710        ItemKind::Task
13711    })
13712}
13713
13714/// The `stateInput` that closes an issue for `reason`.
13715fn closed_input(reason: ClosedState) -> Value {
13716    json!({"value":"CLOSED","stateReason":reason.reason()})
13717}
13718
13719/// The `IssueStateUpdateInput` one status target asks for.
13720///
13721/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
13722/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
13723/// a currently-closed issue: without that the item would read back `Unknown` and a copy
13724/// would report a change forever. A document has no status at all, and asks for neither.
13725fn state_input(target: Option<&StatusTarget>) -> Value {
13726    match target {
13727        Some(StatusTarget::Terminal(_, reason)) => closed_input(*reason),
13728        Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
13729        // A document has no status, so a write of one says nothing about the issue's open
13730        // or closed state rather than forcing it open: `stateInput` is what carries that
13731        // instruction, and an explicit null asks for no change to it.
13732        None => Value::Null,
13733    }
13734}
13735
13736/// The metadata one write stores in the item's body slot.
13737///
13738/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
13739/// rather than carried: the kind marker so an empty project stays readable, the
13740/// repository list only when it is not exactly the issue's own repository, and the far
13741/// ends no relationship here can name.
13742///
13743/// The copy origin is the one typed field that is also mirrored here, and only as a
13744/// mirror: it lands in the board's origin field as well, which stays the one every reader
13745/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
13746/// and catches up with a write in seconds rather than minutes — can find the item by it.
13747/// A reader of the release before this one drops the slot's copy and reads the field, so an
13748/// item written here still reads with exactly one origin there.
13749fn slot_metadata(
13750    incoming: &Incoming<'_>,
13751    own_repository: Option<&Repository>,
13752    fallback: &[DependencyEdge],
13753) -> BTreeMap<String, Value> {
13754    let mut metadata = incoming.metadata.clone();
13755    match metadata.remove(ORIGIN_KEY) {
13756        Some(Value::String(origin)) if !origin.is_empty() => {
13757            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
13758        }
13759        _ => {}
13760    }
13761    match incoming.written.kind() {
13762        BoardKind::Work(kind) => metadata.insert(
13763            ItemKind::METADATA_KEY.to_owned(),
13764            Value::String(kind.marker().to_owned()),
13765        ),
13766        // A document is told by its title, so it carries no kind marker: that key names
13767        // what a dependency endpoint points at, and nothing may point at a document.
13768        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
13769    };
13770    incoming.classification.record(&mut metadata);
13771    let derivable = own_repository
13772        .map(|own| incoming.repositories == [own.clone()])
13773        .unwrap_or(incoming.repositories.is_empty());
13774    if derivable {
13775        metadata.remove(Repository::METADATA_KEY);
13776    } else {
13777        metadata.insert(
13778            Repository::METADATA_KEY.to_owned(),
13779            Value::Array(
13780                incoming
13781                    .repositories
13782                    .iter()
13783                    .map(|repository| Value::String(repository.as_str().to_owned()))
13784                    .collect(),
13785            ),
13786        );
13787    }
13788    // The typed lists are what land, whatever the caller's own metadata held under their
13789    // keys: a key of either name travelling beside the field would otherwise be a second
13790    // answer to the same question, and the field is the one the contract names.
13791    for (key, entries) in [
13792        (TaskRef::DELIVERS_KEY, incoming.delivers),
13793        (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
13794    ] {
13795        set_task_list(&mut metadata, key, entries);
13796    }
13797    record_edges(&mut metadata, fallback);
13798    metadata
13799}
13800
13801/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
13802/// one slot's metadata, or no such key when there are none.
13803fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
13804    if fallback.is_empty() {
13805        metadata.remove(DependencyEdge::RECORDED_KEY);
13806    } else {
13807        metadata.insert(
13808            DependencyEdge::RECORDED_KEY.to_owned(),
13809            Value::Array(
13810                fallback
13811                    .iter()
13812                    .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
13813                    .collect(),
13814            ),
13815        );
13816    }
13817}
13818
13819/// Every label one item carries, from its content's own connection and nowhere else.
13820///
13821/// There is no second place to read one from: no document this source sends selects the
13822/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
13823/// cannot carry one at all. The module documentation records the three schema facts that
13824/// settle it.
13825fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
13826    optional_nodes(content.get("labels"), "content labels")?
13827        .into_iter()
13828        .flatten()
13829        .map(|v| {
13830            Ok(Label {
13831                id: NativeId(required_str(v, "id")?.to_owned()),
13832                name: required_str(v, "name")?.to_owned(),
13833                color: optional_str(v, "color")?.map(str::to_owned),
13834            })
13835        })
13836        .collect()
13837}
13838
13839/// Each board field value one item holds, as the change token's facets name it: the field's
13840/// name, with the single-select option's name or the text. A value of a type no read here
13841/// selects, and one naming no field, says nothing a person can rename and is left out.
13842fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
13843    field_values
13844        .iter()
13845        .filter_map(|value| {
13846            let field = value.pointer("/field/name")?.as_str()?;
13847            let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
13848            Some((field.to_owned(), held.to_owned()))
13849        })
13850        .collect()
13851}
13852
13853/// The digest of the facets of the issue `content`, whose board item's values are
13854/// `field_values` — or `None` when the response did not carry, whole, every member they are read
13855/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
13856/// label, and every field value.
13857///
13858/// What a write's own response spells its token from, by exactly the rules a read takes them by.
13859fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
13860    let ended = |connection: &Value| {
13861        connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
13862    };
13863    let labels = content.get("labels").filter(|labels| ended(labels))?;
13864    if !ended(field_values) {
13865        return None;
13866    }
13867    let parent = content.get("parent")?;
13868    let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
13869    let kind = BoardKind::of(
13870        content.get("title")?.as_str()?,
13871        !parent.is_null(),
13872        sub_issue_total(content).ok()?,
13873        ItemKind::from_metadata(&slot).ok()?,
13874    );
13875    let labels = self::labels(&json!({"labels": labels})).ok()?;
13876    let nodes = field_values.get("nodes")?.as_array()?;
13877    Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
13878}
13879
13880/// The definition of each board field one item's values are values of, in the shape a read
13881/// of the board's own `fields` gives one.
13882///
13883/// A value names its field through a fragment on that field's own type, so the type is
13884/// known from which kind of value it is: a single-select value's field is a
13885/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
13886/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
13887fn field_definitions(field_values: &[Value]) -> Vec<Value> {
13888    field_values
13889        .iter()
13890        .filter_map(|value| {
13891            let field = value.get("field")?.as_object()?;
13892            field.get("id")?.as_str().filter(|id| !id.is_empty())?;
13893            let typename = if value.get("text").is_some() {
13894                "ProjectV2Field"
13895            } else if value.get("name").is_some() {
13896                "ProjectV2SingleSelectField"
13897            } else {
13898                return None;
13899            };
13900            let mut defined = field.clone();
13901            defined.insert("__typename".to_owned(), json!(typename));
13902            // Only a text field holds a text value, so the value says the field's type too.
13903            if typename == "ProjectV2Field" {
13904                defined.insert("dataType".to_owned(), json!("TEXT"));
13905            }
13906            Some(Value::Object(defined))
13907        })
13908        .collect()
13909}
13910
13911fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
13912    let Some(node) = field_values
13913        .iter()
13914        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
13915    else {
13916        return Ok(None);
13917    };
13918    Ok(optional_str(node, "text")?.map(str::to_owned))
13919}
13920
13921fn valid_github_owner(owner: &str) -> bool {
13922    !owner.is_empty()
13923        && owner.len() <= 39
13924        && !owner.starts_with('-')
13925        && !owner.ends_with('-')
13926        && !owner.contains("--")
13927        && owner
13928            .bytes()
13929            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
13930}
13931
13932/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
13933/// neither of the two names a path segment already means.
13934fn valid_github_repository_name(name: &str) -> bool {
13935    !name.is_empty()
13936        && name.len() <= 100
13937        && name != "."
13938        && name != ".."
13939        && name
13940            .bytes()
13941            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
13942}
13943
13944fn valid_environment_name(name: &str) -> bool {
13945    let mut bytes = name.bytes();
13946    bytes
13947        .next()
13948        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
13949        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
13950}
13951
13952/// How many sub-issues one issue has.
13953///
13954/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
13955/// absent or non-integer one is a response this source cannot read — and reading it as
13956/// zero would classify a project as a task, which is exactly the mistake the marker
13957/// exists to keep from happening quietly.
13958fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
13959    let summary = issue
13960        .get("subIssuesSummary")
13961        .ok_or_else(|| SourceError::Malformed {
13962            message: "GitHub issue is missing subIssuesSummary".into(),
13963        })?;
13964    summary
13965        .get("total")
13966        .and_then(Value::as_u64)
13967        .ok_or_else(|| SourceError::Malformed {
13968            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
13969        })
13970}
13971
13972/// One issue's own `number`.
13973///
13974/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
13975/// an issue in this module asks for it. So a read of one that comes back without it, or
13976/// with something that is not an unsigned integer, is a response this source cannot read —
13977/// absence here is **not** "this issue has no number". A draft is the content that has
13978/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
13979/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
13980fn issue_number(issue: &Value) -> Result<u64, SourceError> {
13981    issue
13982        .get("number")
13983        .and_then(Value::as_u64)
13984        .ok_or_else(|| SourceError::Malformed {
13985            message: "GitHub issue number is missing or is not an unsigned integer".into(),
13986        })
13987}
13988
13989/// The `number` a creating mutation answered with, and `None` when it answered without one;
13990/// why a missing one is tolerated is at the call in `create_and_file_issue`.
13991fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
13992    match created.get("number") {
13993        None | Some(Value::Null) => Ok(None),
13994        Some(value) => value
13995            .as_u64()
13996            .map(Some)
13997            .ok_or_else(|| SourceError::Malformed {
13998                message: "GitHub created issue number is not an unsigned integer".into(),
13999            }),
14000    }
14001}
14002
14003fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
14004    value
14005        .get(field)
14006        .and_then(Value::as_str)
14007        .ok_or_else(|| SourceError::Malformed {
14008            message: format!("GitHub response is missing string field {field}"),
14009        })
14010}
14011
14012fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
14013    let found = required_str(value, field)?;
14014    if found.trim().is_empty() {
14015        return Err(SourceError::Malformed {
14016            message: format!("GitHub response has blank string field {field}"),
14017        });
14018    }
14019    Ok(found)
14020}
14021
14022/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
14023/// needs one — Linear spells them too, in its own description field.
14024///
14025/// Restated rather than shared, because a plugin crate depends on the contract crate and
14026/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
14027/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
14028/// source round-trips its own writes perfectly well under its own spelling.
14029const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
14030const METADATA_CLOSE: &str = "\n-->";
14031
14032/// What the composer puts between a non-empty visible body and the slot, and the one thing
14033/// the parser takes off the visible body when it takes the slot off — exactly once, so every
14034/// other trailing byte of the body comes back as it was written.
14035// 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.
14036const METADATA_SEPARATOR: &str = "\n\n";
14037
14038/// The visible body and the metadata slot at the end of it.
14039///
14040/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
14041/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
14042/// own content and is left alone. The visible body is everything before the slot less the
14043/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
14044fn metadata_body(
14045    body: Option<String>,
14046) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
14047    let Some(body) = body else {
14048        return Ok((None, BTreeMap::new()));
14049    };
14050    let Some(slot) = slot_span(&body)? else {
14051        return Ok((Some(body), BTreeMap::new()));
14052    };
14053    let metadata =
14054        serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
14055            SourceError::Malformed {
14056                message: format!(
14057                    "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
14058                ),
14059            }
14060        })?;
14061    let before = &body[..slot.start];
14062    let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
14063    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
14064}
14065
14066/// Where the metadata slot sits in one body, as byte offsets into it.
14067struct SlotSpan {
14068    /// Where [`METADATA_OPEN`] begins.
14069    start: usize,
14070    /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
14071    encoded_start: usize,
14072    /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
14073    encoded_end: usize,
14074    /// Just past [`METADATA_CLOSE`].
14075    end: usize,
14076}
14077
14078/// The slot at the very end of `body`, or `None` when it has none.
14079///
14080/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
14081/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
14082/// slot.
14083fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
14084    let Some(start) = body.rfind(METADATA_OPEN) else {
14085        return Ok(None);
14086    };
14087    let encoded_start = start + METADATA_OPEN.len();
14088    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
14089        return Err(SourceError::Malformed {
14090            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
14091        });
14092    };
14093    let encoded_end = encoded_start + relative_end;
14094    let end = encoded_end + METADATA_CLOSE.len();
14095    if !body[end..].trim().is_empty() {
14096        return Ok(None);
14097    }
14098    Ok(Some(SlotSpan {
14099        start,
14100        encoded_start,
14101        encoded_end,
14102        end,
14103    }))
14104}
14105
14106/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
14107/// slot as it was.
14108///
14109/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
14110/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
14111/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
14112/// or alone in an empty body — and a body with no slot that is given no metadata is
14113/// returned as it is.
14114fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
14115    let encoded = if metadata.is_empty() {
14116        None
14117    } else {
14118        Some(
14119            serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
14120                message: error.to_string(),
14121            })?,
14122        )
14123    };
14124    Ok(match (slot_span(body)?, encoded) {
14125        (Some(slot), Some(encoded)) => format!(
14126            "{}{encoded}{}",
14127            &body[..slot.encoded_start],
14128            &body[slot.encoded_end..]
14129        ),
14130        (Some(slot), None) => {
14131            let before = &body[..slot.start];
14132            format!(
14133                "{}{}",
14134                before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
14135                &body[slot.end..]
14136            )
14137        }
14138        (None, None) => body.to_owned(),
14139        (None, Some(encoded)) if body.is_empty() => {
14140            format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14141        }
14142        (None, Some(encoded)) => {
14143            format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14144        }
14145    })
14146}
14147
14148/// `body` with everything before its metadata slot replaced by `content`, and the slot
14149/// itself kept byte for byte.
14150///
14151/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
14152/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
14153/// `content` is empty — so a read of the result reports `content` as the visible body and
14154/// the slot's metadata exactly as it was.
14155fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
14156    let Some(slot) = slot_span(body)? else {
14157        return Ok(content.to_owned());
14158    };
14159    let kept = &body[slot.start..];
14160    Ok(if content.is_empty() {
14161        kept.to_owned()
14162    } else {
14163        format!("{content}{METADATA_SEPARATOR}{kept}")
14164    })
14165}
14166
14167/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
14168fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
14169    if entries.is_empty() {
14170        metadata.remove(key);
14171    } else {
14172        metadata.insert(
14173            key.to_owned(),
14174            Value::Array(
14175                entries
14176                    .iter()
14177                    .map(|entry| Value::String(entry.as_str().to_owned()))
14178                    .collect(),
14179            ),
14180        );
14181    }
14182}
14183
14184fn compose_body(
14185    content: Option<&str>,
14186    metadata: &BTreeMap<String, Value>,
14187) -> Result<Option<String>, SourceError> {
14188    let visible = content.unwrap_or_default();
14189    if metadata.is_empty() {
14190        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
14191    }
14192    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
14193        message: error.to_string(),
14194    })?;
14195    Ok(Some(if visible.is_empty() {
14196        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14197    } else {
14198        format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
14199    }))
14200}
14201
14202fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
14203    value
14204        .get(field)
14205        .and_then(Value::as_bool)
14206        .ok_or_else(|| SourceError::Malformed {
14207            message: format!("GitHub response is missing boolean field {field}"),
14208        })
14209}
14210fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
14211    match value.get(field) {
14212        None | Some(Value::Null) => Ok(None),
14213        Some(value) => value
14214            .as_str()
14215            .map(Some)
14216            .ok_or_else(|| SourceError::Malformed {
14217                message: format!("GitHub response field {field} is not a string or null"),
14218            }),
14219    }
14220}
14221fn optional_nodes<'a>(
14222    connection: Option<&'a Value>,
14223    name: &str,
14224) -> Result<Option<&'a Vec<Value>>, SourceError> {
14225    match connection {
14226        None | Some(Value::Null) => Ok(None),
14227        Some(value) => value
14228            .get("nodes")
14229            .and_then(Value::as_array)
14230            .map(Some)
14231            .ok_or_else(|| SourceError::Malformed {
14232                message: format!("GitHub {name}.nodes is not an array"),
14233            }),
14234    }
14235}
14236fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
14237    let page_info = connection
14238        .get("pageInfo")
14239        .ok_or_else(|| SourceError::Malformed {
14240            message: format!("GitHub {name} has no pageInfo"),
14241        })?;
14242    if required_bool(page_info, "hasNextPage")? {
14243        return Err(SourceError::Malformed {
14244            message: format!(
14245                "GitHub {name} exceeds the supported nested connection size of {size}"
14246            ),
14247        });
14248    }
14249    Ok(())
14250}
14251fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
14252    optional_str(value, field)?
14253        .map(|timestamp| {
14254            timestamp.parse().map_err(|error| SourceError::Malformed {
14255                message: format!("GitHub response field {field} is not a timestamp: {error}"),
14256            })
14257        })
14258        .transpose()
14259}
14260fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
14261    if page.limit == 0 {
14262        Err(SourceError::Config {
14263            message: "page limit must be at least 1".into(),
14264        })
14265    } else {
14266        Ok(())
14267    }
14268}
14269fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
14270    let page = connection
14271        .get("pageInfo")
14272        .filter(|value| value.is_object())
14273        .ok_or_else(|| SourceError::Malformed {
14274            message: "GitHub connection is missing pageInfo".into(),
14275        })?;
14276    if required_bool(page, "hasNextPage")? {
14277        let cursor = required_str(page, "endCursor")?;
14278        validate_cursor_progress(None, cursor)?;
14279        Ok(Some(Cursor(cursor.into())))
14280    } else {
14281        Ok(None)
14282    }
14283}
14284fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
14285    if next.is_empty() || previous == Some(next) {
14286        Err(SourceError::Malformed {
14287            message: "GitHub pagination cursor is empty or did not advance".into(),
14288        })
14289    } else {
14290        Ok(())
14291    }
14292}
14293/// The version of this plugin's opaque narrowing-search cursor.
14294pub const SEARCH_CURSOR_VERSION: u32 = 4;
14295
14296#[derive(Serialize, Deserialize)]
14297#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
14298enum SearchConnection {
14299    Initial {},
14300    Continuing { after: Cursor },
14301    Exhausted {},
14302}
14303impl SearchConnection {
14304    fn after(&self) -> Option<&str> {
14305        match self {
14306            Self::Continuing { after } => Some(&after.0),
14307            _ => None,
14308        }
14309    }
14310    fn exhausted(&self) -> bool {
14311        matches!(self, Self::Exhausted { .. })
14312    }
14313    /// Whether a cursor naming this position, `offset` rows into its page, is one this
14314    /// plugin could have handed out: a page is resumed only part of the way through it — an
14315    /// offset of a whole page or more would skip rows nobody was given — an initial page
14316    /// only once some of it was handed out, and an exhausted connection has no page to be
14317    /// part of the way through.
14318    fn valid_resume(&self, offset: usize) -> bool {
14319        let within = offset < SEARCH_PAGE_SIZE as usize;
14320        match self {
14321            Self::Initial { .. } => offset > 0 && within,
14322            Self::Continuing { after } => !after.0.is_empty() && within,
14323            Self::Exhausted { .. } => offset == 0,
14324        }
14325    }
14326}
14327
14328/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
14329#[derive(Serialize, Deserialize)]
14330#[serde(deny_unknown_fields)]
14331struct SearchPosition {
14332    version: u32,
14333    connection: SearchConnection,
14334    /// How many rows of the page `connection` starts were already handed out.
14335    #[serde(default, skip_serializing_if = "is_zero")]
14336    offset: usize,
14337    #[serde(default, skip_serializing_if = "Vec::is_empty")]
14338    seen: Vec<NativeId>,
14339    #[serde(default, skip_serializing_if = "Vec::is_empty")]
14340    own: Vec<NativeId>,
14341}
14342impl Default for SearchPosition {
14343    fn default() -> Self {
14344        Self {
14345            version: SEARCH_CURSOR_VERSION,
14346            connection: SearchConnection::Initial {},
14347            offset: 0,
14348            seen: Vec::new(),
14349            own: Vec::new(),
14350        }
14351    }
14352}
14353
14354fn is_zero(offset: &usize) -> bool {
14355    *offset == 0
14356}
14357
14358fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
14359    cursor.map_or(Ok(0), |c| {
14360        c.0.parse().map_err(|_| SourceError::Config {
14361            message: "page cursor is invalid".into(),
14362        })
14363    })
14364}
14365fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
14366    if offset > items.len() {
14367        return Page::last(vec![]);
14368    }
14369    let tail = items.split_off(offset);
14370    let mut selected = tail;
14371    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
14372    selected.truncate(limit);
14373    Page {
14374        items: selected,
14375        next,
14376    }
14377}
14378
14379impl GitHubProjectsSource {
14380    async fn write_task_assets(
14381        &self,
14382        write: &ItemWrite<Task>,
14383        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
14384    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14385        self.write_item(
14386            &self.task_incoming(write, assets)?,
14387            write.target.as_ref(),
14388            &write.depends_on,
14389        )
14390        .await
14391    }
14392
14393    /// One task write as [`Self::write_item`] takes it, once what it delivers and its priority
14394    /// are known to be ones this source can write.
14395    fn task_incoming<'a>(
14396        &self,
14397        write: &'a ItemWrite<Task>,
14398        assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
14399    ) -> Result<Incoming<'a>, SourceError> {
14400        let near = write.target.as_ref().unwrap_or(&write.item.id);
14401        for (key, entries) in [
14402            (TaskRef::DELIVERS_KEY, &write.item.delivers),
14403            (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
14404        ] {
14405            TaskRef::listed(key, near, Some(&self.name), entries.clone())
14406                .map_err(|message| SourceError::Refused { message })?;
14407        }
14408        if self.priorities.is_none() && write.item.priority != Priority::None {
14409            return Err(self.holds_no_priority());
14410        }
14411        Ok(Incoming {
14412            written: Written::Work(ItemKind::Task, &write.item.status),
14413            title: &write.item.title,
14414            content: write.item.content.as_deref(),
14415            assets,
14416            labels: &write.item.labels,
14417            metadata: &write.item.metadata,
14418            repositories: &write.item.repositories,
14419            classification: write.item.classification,
14420            parent: write.item.project.as_ref(),
14421            delivers: &write.item.delivers,
14422            delivered_by: &write.item.delivered_by,
14423            priority: self.priorities.as_ref().map(|_| write.item.priority),
14424        })
14425    }
14426    async fn write_document_assets(
14427        &self,
14428        write: &ItemWrite<Document>,
14429        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
14430    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
14431        // A document takes part in no dependency graph, so there is no far end to write
14432        // natively and none to record: a caller naming one is told so rather than having it
14433        // stored under the reserved key, where a later read would report an edge the
14434        // contract says cannot exist.
14435        if !write.depends_on.is_empty() {
14436            return Err(SourceError::Refused {
14437                message: format!(
14438                    "this write names {} dependencies for a document, and a document takes \
14439                     part in no dependency graph; next: put the dependency on the task or \
14440                     project the document is about",
14441                    write.depends_on.len()
14442                ),
14443            });
14444        }
14445        self.write_item(
14446            &Incoming {
14447                written: Written::Document,
14448                title: &write.item.title,
14449                content: write.item.content.as_deref(),
14450                assets,
14451                labels: &write.item.labels,
14452                metadata: &write.item.metadata,
14453                repositories: &write.item.repositories,
14454                classification: write.item.classification,
14455                parent: write.item.project.as_ref(),
14456                delivers: &[],
14457                delivered_by: &[],
14458                priority: None,
14459            },
14460            write.target.as_ref(),
14461            &[],
14462        )
14463        .await
14464    }
14465}
14466
14467/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
14468/// itself, for the two things no journey can observe.
14469///
14470/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
14471/// with and without the call, that a settlement, a board listing and a metadata search each
14472/// read afresh after it — the resolved records, the written-item overlay, the board and its
14473/// search, and the narrowed searches. What they cannot reach is the held field definitions,
14474/// because a status write naming an option a person deleted is refused the same whether or
14475/// not the list is held, and a poisoned lock, because nothing outside the source can panic
14476/// while one of its locks is held. So these assert those directly, and every other holder
14477/// beside them so a holder added later without a clear in the call fails here.
14478#[cfg(test)]
14479mod end_command_tests {
14480    use super::*;
14481
14482    struct Token;
14483
14484    impl SecretResolver for Token {
14485        fn get(&self, var: &str) -> Option<SecretString> {
14486            (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
14487        }
14488    }
14489
14490    fn source() -> GitHubProjectsSource {
14491        let config = serde_json::from_value(json!({
14492            "owner": "octo-org", "project_number": 7, "repository": "acme/work",
14493            // Nothing here is sent: the source is only built and its state inspected.
14494            "endpoint": "http://127.0.0.1:9/graphql",
14495        }))
14496        .expect("a usable configuration");
14497        GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
14498            .expect("the source builds")
14499    }
14500
14501    /// One issue as a board read answers it.
14502    fn resolved(source: &GitHubProjectsSource) -> Resolved {
14503        source
14504            .resolve(&json!({
14505                "id": "ITEM-1",
14506                "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
14507                            "body": "what a person may since have edited", "state": "OPEN",
14508                            "stateReason": null, "url": null, "number": 1,
14509                            "subIssuesSummary": {"total": 0},
14510                            "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
14511                "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
14512            }))
14513            .expect("the item reads")
14514            .expect("an issue")
14515    }
14516
14517    /// Hold something in every holder the call clears, and the repository id it keeps.
14518    fn fill(source: &GitHubProjectsSource) {
14519        let item = resolved(source);
14520        source.created.lock().unwrap().push(item.clone());
14521        source.updated.lock().unwrap().push(item.clone());
14522        *source.board_cache.lock().unwrap() = Some(Board {
14523            id: "PVT-board".into(),
14524            fields: json!({"nodes": []}),
14525            items: vec![item.clone()],
14526        });
14527        *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
14528        source
14529            .narrowed_cache
14530            .lock()
14531            .unwrap()
14532            .insert("status:todo".into(), vec![item.clone()]);
14533        source.children_cache.lock().unwrap().insert(
14534            NativeId("P-1".into()),
14535            (NativeId("P-1".into()), vec![item.clone()]),
14536        );
14537        source
14538            .search_next
14539            .lock()
14540            .unwrap()
14541            .insert("status:todo".into(), Some("cursor".into()));
14542        source
14543            .resolved_cache
14544            .lock()
14545            .unwrap()
14546            .insert(item.id.clone(), item);
14547        *source.fields_cache.lock().unwrap() = Some(BoardFields {
14548            id: BoardId::parse("PVT-board").unwrap(),
14549            fields: json!({"nodes": []}),
14550        });
14551        source
14552            .repository_cache
14553            .lock()
14554            .unwrap()
14555            .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
14556    }
14557
14558    fn assert_dropped(source: &GitHubProjectsSource) {
14559        assert!(source.created().unwrap().is_empty(), "created");
14560        assert!(source.updated().unwrap().is_empty(), "updated");
14561        assert!(source.board_cache().unwrap().is_none(), "board");
14562        assert!(source.search_cache.lock().unwrap().is_none(), "search");
14563        assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
14564        assert!(
14565            source.children_cache.lock().unwrap().is_empty(),
14566            "project children"
14567        );
14568        assert!(
14569            source.search_next.lock().unwrap().is_empty(),
14570            "search paging"
14571        );
14572        assert!(
14573            source.resolved_cache().unwrap().is_empty(),
14574            "resolved records"
14575        );
14576        assert!(source.fields_cache().unwrap().is_none(), "board fields");
14577        assert_eq!(
14578            source.repository_cache().unwrap().len(),
14579            1,
14580            "a repository's node id stays valid and is kept"
14581        );
14582    }
14583
14584    fn end(source: &GitHubProjectsSource) {
14585        tokio::runtime::Builder::new_current_thread()
14586            .build()
14587            .unwrap()
14588            .block_on(source.end_command())
14589            .expect("the command ends");
14590    }
14591
14592    #[test]
14593    fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
14594        let source = source();
14595        fill(&source);
14596        end(&source);
14597        assert_dropped(&source);
14598    }
14599
14600    #[test]
14601    fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
14602        fn poison<T: Send>(held: &Mutex<T>) {
14603            std::thread::scope(|scope| {
14604                let _ = scope
14605                    .spawn(|| {
14606                        let _guard = held.lock().unwrap();
14607                        panic!("a failure while the lock is held");
14608                    })
14609                    .join();
14610            });
14611            assert!(held.is_poisoned());
14612        }
14613        let source = source();
14614        fill(&source);
14615        poison(&source.created);
14616        poison(&source.updated);
14617        poison(&source.board_cache);
14618        poison(&source.search_cache);
14619        poison(&source.narrowed_cache);
14620        poison(&source.search_next);
14621        poison(&source.resolved_cache);
14622        poison(&source.fields_cache);
14623        assert!(
14624            source.resolved_cache().is_err(),
14625            "a poisoned lock is refused before the call"
14626        );
14627        end(&source);
14628        assert_dropped(&source);
14629    }
14630}
14631
14632#[cfg(test)]
14633mod unprojectable_fields_tests {
14634    use super::*;
14635
14636    /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
14637    fn documented() -> std::collections::BTreeSet<String> {
14638        let document = std::fs::read_to_string(
14639            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
14640        )
14641        .expect("docs/metadata.md is readable");
14642        let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
14643        let lead = "a `field` equal, ignoring case, to ";
14644        let start = document.find(lead).expect("the refused-field sentence") + lead.len();
14645        let sentence = &document[start..];
14646        let sentence = &sentence[..sentence
14647            .find("; and two entries naming one field")
14648            .expect("the refused-field sentence ends at the duplicate rule")];
14649        let (own, github) = sentence
14650            .split_once(" or a field GitHub owns on every board (")
14651            .expect("the sentence names GitHub's own fields");
14652        own.split('`')
14653            .skip(1)
14654            .step_by(2)
14655            .chain(
14656                github
14657                    .strip_suffix(')')
14658                    .expect("GitHub's own fields close the sentence")
14659                    .split(", "),
14660            )
14661            .map(str::to_owned)
14662            .collect()
14663    }
14664
14665    #[test]
14666    fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
14667        let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
14668            .iter()
14669            .map(|&name| name.to_owned())
14670            .collect();
14671        assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
14672        let instance = SourceName::new("work").unwrap();
14673        for name in UNPROJECTABLE_FIELDS {
14674            for spelled in [name.to_owned(), name.to_uppercase()] {
14675                let entry = MetadataFieldConfig {
14676                    field: spelled.clone(),
14677                    key: "team.machine".to_owned(),
14678                    path: Vec::new(),
14679                };
14680                let refused = MetadataField::resolve(vec![entry], &instance)
14681                    .expect_err("an unprojectable field is refused");
14682                assert!(
14683                    refused.to_string().contains(&format!("{name:?}")),
14684                    "{spelled}: {refused}"
14685                );
14686            }
14687        }
14688    }
14689}