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//! | `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. |
161//! | `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. |
162//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
163//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
164//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
165//!
166//! Image writes use `POST https://uploads.github.com/user-attachments/assets` with `name`,
167//! `content_type` and the issue repository's numeric `repository_id` as query parameters,
168//! the source token as `Authorization: Bearer`, the image content type as `Content-Type`,
169//! and the raw bytes as the request body. The numeric repository id is read at most once
170//! per repository per source instance. A loopback API endpoint also selects the loopback
171//! uploads host; production needs no assets repository, commits or extra configuration.
172//! Every returned JSON `url` is read with the same token before a body is written: only
173//! a 2xx verification succeeds. A refusal names the asset, URL and HTTP status; an
174//! anonymous 404 does not invalidate an authenticated 200. An upload refusal names the
175//! asset and HTTP status and says the token type may not be accepted by the upload
176//! endpoint. A classic PAT was measured accepted; no acceptance claim is made about
177//! fine-grained PATs or OAuth tokens.
178//! The body keeps the authored alt text while `./<name>` becomes the attachment URL,
179//! and `onetaskgraph.assets` records `{sha256, url}` by name. A matching destination
180//! SHA-256 reuses its URL without an upload or verifying read; changed bytes are uploaded
181//! and verified afresh, in the repository the issue already lives in on an update.
182//! An uploaded image renders for the viewers GitHub lets read it, like the issue text beside it.
183//! Uploads take the same mutation spacing as content writes. Both uploads and verification
184//! reads wait out classified rate limits within the configured per-call retry budget,
185//! on the supplied clock; verifying reads take no mutation slot.
186//!
187//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
188//! source has documents and that its tasks have comments, both of which hold — and the three
189//! facts behind the uniform `Native` on the
190//! predicates beside it are recorded below rather than re-derived, because a reader who
191//! takes `Native` to mean *the remote service filters* will read that uniformity as a
192//! lie.
193//!
194//! First, the plugin contract defines `Support::Native` as *the source applies this
195//! predicate itself*, and says nothing about where it applies it. What the declaration
196//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
197//! — so that the engine may push it down and apply nothing of its own.
198//!
199//! Second, this source can keep that promise for every predicate at no additional API
200//! cost, because whichever of the reads below answers a query has already read every
201//! candidate that query will return before it filters anything. Filtering those items is
202//! in-process work over data already in hand.
203//!
204//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
205//! applied in process over what that question returned. A project filter has a relationship — a
206//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
207//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
208//! a search for metadata values, is the board-scoped issue search carrying the text and each
209//! value as quoted phrases; an origin is the board's own field filter over its origin field
210//! beside the same search for the id. **The text search narrows, and that is this source's
211//! declared semantics:** GitHub matches whole words where the substring rule this source and
212//! the local Markdown source confirm with would match inside one, so an item holding the text
213//! only inside a longer word is never a candidate. Every item returned does contain the text.
214//! A project query's text, and a document query's scoped to no project, is that same search
215//! and narrows on the same terms, its candidates confirmed by their kind as well.
216//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
217//! so those three are applied in process over the candidates, and a query carrying none of
218//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
219//! engine compensate for work this source has already done, and declaring `projects` native
220//! while ignoring the filter (which this source once did) silently returns another project's
221//! tasks, because the engine trusts the declaration and applies nothing locally.
222//!
223//! # The three ways this source reaches an item, and what each costs
224//!
225//! A board read is charged for what its *nested* connections could return rather than for
226//! what was asked, so one whole-board read costs the same whether the question was about
227//! one project or about all of them. That is why a question about one project is never
228//! answered by reading the board:
229//!
230//! | The question | What is sent | What it costs |
231//! | --- | --- | --- |
232//! | 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 |
233//! | 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 |
234//! | 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 |
235//! | 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 |
236//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
237//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
238//! | 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 |
239//! | 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 |
240//! | 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 |
241//! | 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 |
242//! | 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 |
243//! | 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 |
244//!
245//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
246//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
247//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
248//! equal to declared points equal to the row. They include the origin lookup and the
249//! field/repository discovery a create needs. A bound re-copy changes status, priority,
250//! content and metadata; comment recount means a subsequent detail read. Each request here
251//! costs one declared point. A membership beyond the embedded page can additionally require
252//! the one-point membership recovery described above. A bound re-copy of a task filed under a
253//! project adds one read, the engine confirming that project's link by its own id once per
254//! command; and the same-source far ends a write newly names — those that do not already block
255//! the item, whose own read answered for them — are read together by their own ids,
256//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
257//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
258//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
259//!
260//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
261//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
262//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
263//! holds both halves.
264//!
265//! **An existing item is written body last.** A bound re-copy and a `task update` send its
266//! board fields first — the `Status` option and the `Priority` together, in one request — then
267//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
268//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
269//! undoing an earlier field when a later one fails, so that order is what makes a write
270//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
271//! the one piece of metadata written before the body, an origin a copy re-points, is put back
272//! when a later write is refused — and when putting it back is refused too, the write's own
273//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
274//! of those writes in turn, whole and as one aliased field failing after the one before it.
275//!
276//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
277//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
278//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
279//!
280//! - **A board is accepted at creation but its item is not answered, so a create still files
281//!   the issue itself: a new copy is 5 requests, and 4 with `--create`.**
282//!   `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
283//!   Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
284//!   The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
285//!   against a real board on 2026-10-01 with a create sending the board there and reading the
286//!   item off the payload's `Issue.projectItems`: every one of its four creates answered with
287//!   no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
288//!   refused "Content already exists in this project" — GitHub had filed the issue after
289//!   answering, and refuses a second filing rather than answering with the item it holds. A
290//!   create therefore sends no `projectV2Ids` and files the issue with
291//!   `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
292//!   real is the read before it: the board's fields and the repository's id together, in
293//!   [`graphql::CREATION_CONTEXT`], at the point the repository is known.
294//! - **A comment still reads its target first, so a comment is 2 requests.**
295//!   `AddCommentInput.subjectId: ID!` is declared there with
296//!   `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
297//!   "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
298//!   project's issue, a document's issue, an issue on no board of this source and a pull
299//!   request all are: GitHub writes the comment, so there is no refusal to map into "that is
300//!   not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
301//!   refuses those by name.
302//!
303//! | Verb | Requests / points | Documents |
304//! | --- | --- | --- |
305//! | new copy | 5 | ORIGIN_LOOKUP, CREATION_CONTEXT (the board's fields and the repository's id together), CREATE_ISSUE, ADD_TO_BOARD, UPDATE_FIELDS |
306//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, ADD_TO_BOARD, UPDATE_FIELDS: the new copy without its ORIGIN_LOOKUP |
307//! | bound copy | 3 | ISSUE (with the board's fields and the issue's `blockedBy`, so no BOARD_FIELDS or ISSUE_DEPENDENCIES), UPDATE_FIELDS, then UPDATE_ISSUE last |
308//! | 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 |
309//! | 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 |
310//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
311//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
312//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
313//! | 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 |
314//! | recount | 1 | ISSUE_DETAIL |
315//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
316//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
317//! | content | 2 | ISSUE, UPDATE_ISSUE |
318//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
319//! | 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 |
320//! | record only | 1 | ISSUE |
321//!
322//! <!-- github-search-paging:start -->
323//! Board-scoped text, metadata, project-name and comment-activity searches send every
324//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
325//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
326//! needs rows. A page is never resized to the rows still needed: GitHub orders one
327//! search differently at different page sizes, so one fixed size makes a paged walk
328//! send exactly the requests one whole read sends, and the answer's order is the order
329//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
330//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
331//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
332//! returned and fetched pages: a limit is sliced from the pages it needs, and local
333//! confirmation can require more candidates than matching rows. Walking all pages
334//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
335//! cursor and how far into that page the last answer stopped, and resumes in the same
336//! process or a new one, without duplicates or gaps. It carries no rows: one process
337//! sends each page's search once, and a new process re-reads only the page it resumes
338//! in, then sends a further page once, never as a re-read, only when its limit still
339//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
340//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
341//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
342//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
343//! not required to include the original process's writes still omitted by the index.
344//! <!-- github-search-paging:end -->
345//!
346//! The board half of an issue — its board item's id, its `Status` option and this
347//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
348//! an item reached any of those ways resolves through the same
349//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
350//! same status, the same labels and the same qualified id. That connection comes back a
351//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
352//! on the page in hand and — only if that page reports more of the connection — in the
353//! last row's read of that one issue's memberships, resumed from the page's own cursor and
354//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
355//! report, which is what keeps an id naming another repository's issue from being answered
356//! as an item of this board; and because the page is where the search starts rather than
357//! where it ends, that answer is one about a connection read to exhaustion and never about
358//! an unread page. Nothing costs the extra read but an issue on more boards than a page
359//! holds: an issue this board really does not hold reports no next page, so its
360//! memberships are already exhausted where they arrived.
361//!
362//! **No document here selects the board's own `Labels` field, and nothing is lost by
363//! that.** An item's labels are read from its content alone, wherever that content is
364//! reached: the three documents above select `Issue.labels` on the fragment, and
365//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
366//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
367//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
368//! create one, and `ProjectV2FieldValue` — the whole of what
369//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
370//! it from the content, for every content type it exists on, and there is nothing it can
371//! hold that the content does not already say: for an `Issue` it *is* that issue's own
372//! labels, so selecting it beside them unions a set with itself.
373//!
374//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
375//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
376//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
377//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
378//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
379//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
380//! label set, and that set is the fixture issue's own, by
381//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
382//! item whose content is a draft reports an empty set, by
383//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
384//! selection is held over [`graphql::DOCUMENTS`] by
385//! `no_document_selects_the_boards_own_labels_field`.
386//!
387//! The whole-board row is still the board's own item connection, and deliberately: a
388//! **draft** board item is not an issue, so no search can list one, and the reads that have
389//! to answer for the whole board are the ones whose cost is the board's size anyway.
390//!
391//! **A question about one item this source already names by id never lists the board.**
392//! Whether that item is on this board, and what its board fields are, is answered by reading
393//! that item — its own `Issue.projectItems`, walked to exhaustion by
394//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
395//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
396//! holds. That covers a write's destination, the project a new item is filed under, a
397//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
398//! and the delete that takes back an item a copy made. What such a write needs of the board
399//! and the item does not carry — the board's id, the `Status` and origin field definitions —
400//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
401//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
402//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
403//! minutes. Scanning this host's 842-item board has refused a document copy and an update
404//! even though the items' own reads named that board. A scan there gives the wrong answer
405//! as well as paying for every page. So a `board.items` lookup does not belong on any of
406//! those paths.
407//!
408//! **What a read may return is capped too, and that cap is on the document rather than on
409//! the board.** GitHub limits the number of nodes **one query may return** to
410//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
411//! an error naming the connection the count crossed at, not a slow or a partial result.
412//! Every board this source reads is refused the same way, so no board is too big for these
413//! documents and none is small enough to save one that is over.
414//!
415//! The count is arithmetic over the document's own text: each connection contributes the
416//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
417//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
418//! restate them — `github-graphql-node-count` implements them, and
419//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
420//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
421//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
422//! same text on every run and fails naming any that reaches the limit — so a connection
423//! added to a shared fragment is caught there rather than by GitHub.
424//!
425//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
426//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
427//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
428//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
429//! effectively squared there, which is why it is the one the limit is most sensitive to.
430//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
431//! page of memberships misses is recovered by one further read rather than refused, so it
432//! buys a bound every read pays for at the price of a request only a multi-board issue
433//! pays.
434//!
435//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
436//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
437//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
438//! `cost` is rate-limit points, metered per hour across everything one credential does; it
439//! is what the two limiters [`Limiter`] tells apart meter, and a document under
440//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
441//! that second number, and `tests/point_cost.rs` pins every document in
442//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
443//! one under, the pin itself is the check. The credentialed lane reconciles both figures
444//! against GitHub's own, off a probe it already sends.
445//!
446//! **What is pinned that way is a per-document price and never a session's.** The record in
447//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
448//! **requests** and **worst-case nodes** — and neither is points. What one whole session
449//! consumes of the hourly point allowance is observable only from a credentialed run's own
450//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
451//! and what `tests/live.rs` prints at the end of every run.
452//!
453//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
454//!
455//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
456//! enumerations of a board can supply one alone.** Resolving a node id is strongly
457//! consistent, so a read by id and a project's own sub-issues are already current. The
458//! other two are not, and they are behind by different amounts and in different directions:
459//!
460//! - GitHub's **issue search** is an index and answers a write made moments ago with the
461//!   value from before it — usually for a second or two.
462//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
463//!   on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
464//!   content withheld, absent, with the connection walked to its own `hasNextPage: false` —
465//!   for *minutes*, while `Issue.projectItems` names the same membership at once.
466//!
467//! That second one is a measurement rather than a caution. This repository's own
468//! credentialed journey writes a project and waits for the board to report it, then writes a
469//! task and waits for the same thing seconds later on the same board: the project wait is
470//! answered through the search and converged in two or three attempts in each of three runs,
471//! and the task wait is answered through `ProjectV2.items` and converged in none of them
472//! inside thirty. Separately, an item added to a second and larger board was read back by
473//! `Issue.projectItems` on that board's own id while every one of that connection's nine
474//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
475//! through the lagging one alone is what had a board read deny an issue that had certainly
476//! landed on it.
477//!
478//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
479//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
480//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
481//! search reports what the projection is behind on. What closes the last
482//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
483//! source answers is completed with what this process itself wrote, so an item created
484//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
485//! nothing is written down, and the record dies with the process. **A wait that has to
486//! observe GitHub's own data cannot be answered from that record** — which is why the
487//! credentialed journey asks through a source built afresh, and why the union above rather
488//! than a longer wait is what makes such a wait converge.
489//!
490//! **A narrowed read is the same bargain, stated for each of the three predicates it
491//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
492//! than walking the board, and every such answer is completed with what this process wrote —
493//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
494//! each filtered by the same predicates as the rest — so an item this command wrote a moment
495//! ago is returned by a query that matches it whether or not the index has caught up. An item
496//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
497//! consistent. What is left is stated rather than papered over:
498//!
499//! | Read | Finds | Behind by |
500//! | --- | --- | --- |
501//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
502//! | 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 |
503//! | 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 |
504//! | origin, third read | this process's own writes | nothing |
505//!
506//! So an origin carrier another process added within the last second or two, before either
507//! index has it, can be missing from an origin query, and one written by the release before
508//! this one — its origin in the field alone — can be missing for as long as the board's own
509//! item connection is behind on it. A copy that must not duplicate its own earlier write
510//! relies on the link it records, not on either index. **A board draft is not an issue**, so
511//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
512//! lists one, the origin lookup drops any the board's own field filter names, and one this
513//! process wrote is not added back either.
514//!
515//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
516//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
517//! body's metadata slot, so the issue search can find it in seconds. The field is
518//! authoritative: this source reads an item's origin from the field alone, so a slot that
519//! disagrees with it, or holds one where the field holds none, is never read as a second
520//! origin — and the release before this one reads the slot, drops that key's copy for the
521//! field's, and sees the same one origin.
522//!
523//! Filtering happens before paging, so a page of a filtered result is a page of the
524//! survivors rather than the survivors of a page. Label matching and the substring rule a
525//! text candidate is confirmed by answer the same question the same way the local Markdown
526//! source's do; which candidates a text search has to confirm is GitHub's word match, which
527//! is the one place the two sources can answer the same text differently.
528//!
529//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
530//! has one source, `capabilities`, and the note above is the reasoning behind it rather
531//! than a second copy of it: without the three facts recorded here a reader takes the
532//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
533//! crate's own capabilities test, which pins every field of it against a fully spelled-out
534//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
535//! fails to compile there rather than going unasserted. -->
536//! The fixture-server tests above run wherever this crate is selected; the credentialed
537//! lane runs in the same required check, beside them, and can fail it — it verifies the
538//! 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,
539//! one filed under neither, a label on one of the three and a closed status on another —
540//! because that shape is what tells an honoured predicate from an ignored one: a board
541//! holding a single project answers a project filter the same way whether or not this
542//! source applies it, which is exactly how the defect above went unseen.
543//!
544//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
545//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
546//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
547//! session or request, independently of the live demand. Fix that variable in the machine's
548//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
549//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
550//! policy crate; absent credentials or nominations otherwise skip.
551//!
552//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
553//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
554//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
555//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
556//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
557//! after its run reads back as `completed`, immediately before the listed-artifact batch.
558//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
559//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
560//! and the board's `onetaskgraph.origin` field untouched.
561//!
562//! # What a session of requests costs, and where the report is
563//!
564//! This source records **every** request it sends into [`accounting::Accounting`], at
565//! `send_once` — the one place a request leaves this crate, which is why a read path added
566//! later is counted without anybody remembering to count it. That is the whole of what this
567//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
568//! session's spend is arrived at, and what it deliberately does not know are set out.
569//!
570//! What one whole session of the live journey costs, counted that way against this crate's
571//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
572//! reduction it came out of, and with what it does and does not say about rate-limit points.
573//!
574//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
575//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
576//! code path — no environment variable, no feature, no build configuration — because an
577//! instrument nobody switches on measures nothing, and
578//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
579//! source's counts the whole session rather than this source's share. The credentialed lane
580//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
581//! passed or failed.
582//!
583//! **A live session refuses to start unless the account can afford it.** Before it does any
584//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
585//! GitHub documents as not counting against the REST rate limit and which answers both of
586//! its budgets at once — and starts only if, for each of them, what remains minus this
587//! session's estimated cost is still at least
588//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
589//! allowance. A session that cannot **declines**: it did not run, so it is
590//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
591//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
592//! waiting for the budget to come back. The estimate is derived offline from
593//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
594//! which is also where the published rule that model rests on is cited; the accounting
595//! above records the gate's own read like any other request, and
596//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
597//!
598//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
599//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
600//! text, which is what lets it run on every platform and on a pull request from a fork with
601//! no credential — and that is what actually stops a regression merging. But an offline
602//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
603//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
604//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
605//! number of nodes this query may return"* and whose `cost` is what that document would
606//! spend, both for a document **without executing it**, and the lane asks it for every query
607//! document this source sends, under the largest bindings this source sends, and fails when
608//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
609//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
610//! one; the offline pins still cover it. It records what those calls reported about the
611//! account's own allowance, because whether asking is free is a thing to observe rather than
612//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
613//! and `cost` is metered against an hourly allowance the accounting above reads off a
614//! credentialed run's own response headers.
615//!
616//! **GitHub has two rate limiters and this source is refused by both, so nothing here
617//! treats them as one thing.** The primary budget is the hourly allowance `gh api
618//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
619//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
620//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
621//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
622//! one part of.
623#![deny(missing_docs)]
624
625use std::collections::BTreeMap;
626use std::sync::{Arc, Mutex};
627use std::time::Duration;
628
629use chrono::{DateTime, Utc};
630use onetaskgraph_plugin_api::{
631    Capabilities, Classification, Comment, CommentBody, CommentOrigin, Cursor, DependencyEdge,
632    DependencyEndpoint, DependencyKind, DependencySupport, Direction, Document, DocumentQuery,
633    Health, ItemKind, ItemWrite, Label, LabelFilter, Location, MetadataKey, Metering, NativeId,
634    NewComment, Page, PageRequest, Priority, Project, ProjectFilter, ProjectQuery, Repository,
635    SecretResolver, SharedClock, SourceError, SourceName, SourcePlugin, Status, StatusCategory,
636    StatusMapping, Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate,
637    TaskUpdateOutcome, TextFields, TextQuery, UnmappedStatus, UpdatedField, VersionCoverage,
638    WriteJournal, WriteSupport, system_clock,
639};
640use reqwest::{Client, StatusCode, Url};
641use schemars::{Schema, schema_for};
642use secrecy::{ExposeSecret, SecretString};
643use serde::{Deserialize, Serialize};
644use serde_json::{Value, json};
645
646pub mod accounting;
647mod assets;
648pub mod change_token;
649mod expected;
650pub use expected::{CONTEXT_VERSION, WHOLE_MEMBERS, context_shapes};
651mod visibility;
652
653use accounting::Accounting;
654
655/// The registry name for this plugin.
656pub const KIND: &str = "github-projects";
657/// GitHub's maximum connection page size.
658pub const MAX_PAGE_SIZE: u32 = 100;
659/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
660/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
661/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
662pub const SEARCH_PAGE_SIZE: u32 = 20;
663/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
664/// its comments: the largest batch the node-count model prices at one point.
665///
666/// Each aliased item is resolved once, and what GitHub charges for it is the connections
667/// under it — its labels, its page of board memberships, the field values of each of those
668/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
669/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
670/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
671/// one point and fails if one item more would still be priced at one.
672pub const DETAIL_BATCH: usize = 24;
673/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
674/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
675/// point.
676///
677/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
678/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
679/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
680/// if one item more would still be priced at one.
681pub const COPY_DETAIL_BATCH: usize = 21;
682
683/// The most nodes any one document this source sends may be asked to return.
684///
685/// GitHub's own published per-query ceiling, taken from
686/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
687/// workspace cannot hold a stale copy of somebody else's number. A query above it is
688/// **refused before it is executed**, whoever is asking and whatever board they are
689/// asking about — so this is a bound on the documents rather than a budget that runs out.
690///
691/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
692/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
693/// everything the credential does — two numbers against two limits, and this constant
694/// bounds only the first. The second is computed offline too, per document:
695/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
696/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
697/// lane. There is no constant like this one to hold a price under, because points are an
698/// hourly allowance rather than a per-call bound.
699///
700/// Neither is a session's price. What `session-cost.md` records of a whole session is its
701/// **requests** and its **worst-case nodes**; what a whole session spends in points is
702/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
703/// [`accounting`]. The module section on the three ways this source reaches an item says how
704/// the count is arrived at, and which of the page sizes below decide it.
705pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
706
707/// Nested connection size for the connections that hang off one item.
708///
709/// It multiplies through every document that reaches an item under a page — the count
710/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
711/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
712/// every document under these constants and fails naming any that reaches the limit, so
713/// raising this is caught there rather than by GitHub.
714const NESTED_PAGE_SIZE: u32 = 50;
715/// How many of one issue's board memberships are read when an issue is reached directly.
716///
717/// An issue reached through a search or through its own node id carries its board half in
718/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
719/// under a page of issues, so every point of it multiplies through the whole document and
720/// is paid for whether or not any issue is on a second board — which is why it is
721/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
722///
723/// **Three, because what a page misses is now recovered rather than refused**, and the
724/// recovery is what the value is chosen against. An issue whose entry for this board sits
725/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
726/// that page's own cursor — so the value trades a bound every read pays for a request only
727/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
728/// boards would pay that request *per issue*, which is order N against the one page per
729/// hundred issues a read costs today. At three it is only reached by an issue on four or
730/// more boards at once, which keeps the recovery path exceptional rather than routine for
731/// a plausible deployment.
732const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
733/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
734/// its two connections for.
735///
736/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
737/// second is a duplicate a copy already takes the first of. Both connections are walked to
738/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
739/// never what the answer is. It is small because every point of it is paid on every lookup,
740/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
741/// worst-case nodes than the one whole-board read they replaced.
742const ORIGIN_PAGE_SIZE: u32 = 3;
743
744pub use github_graphql_node_count::{NodeCountError, Variables};
745
746/// The largest value this source can bind to each page-size variable its documents name.
747///
748/// Every `first:` in [`graphql`] reads one of these four, and each is capped at the
749/// constant above it wherever a caller's own limit could reach it — `$first` at
750/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
751/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`. So this is the worst case a caller can drive this source to,
752/// not one configuration of it, which is what makes a bound computed under it a bound on
753/// every read.
754pub fn largest_page_sizes() -> Variables {
755    Variables::from([
756        ("first".to_owned(), MAX_PAGE_SIZE),
757        ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
758        ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
759        ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
760    ])
761}
762
763/// The most nodes `document` could be asked to return, by GitHub's published rules.
764///
765/// Computed offline from the document's own text under [`largest_page_sizes`] — no
766/// network, no credential and no schema — by
767/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
768/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
769/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
770///
771/// # Errors
772///
773/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
774/// no single operation, or binds a page size this source does not name — each of which is
775/// a defect in the document rather than a number.
776pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
777    node_count(document, &largest_page_sizes())
778}
779
780/// The most rate-limit points one call of `document` could spend, by GitHub's published
781/// rules.
782///
783/// Computed offline from the document's own text under [`largest_page_sizes`] — no
784/// network, no credential and no schema — by
785/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
786/// This is `cost`, metered **per hour** against the allowance one credential shares across
787/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
788/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
789/// under, so what `tests/point_cost.rs` does with it is pin every document in
790/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
791/// figures against GitHub's own reported `cost`.
792///
793/// # Errors
794///
795/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
796/// no single operation, or binds a page size this source does not name — each of which is
797/// a defect in the document rather than a number.
798pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
799    github_graphql_node_count::point_cost(document, &largest_page_sizes())
800}
801
802/// The most nodes `document` could be asked to return under `variables`.
803///
804/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
805/// [`accounting`] is this under the bindings one request really sent — one spelling of the
806/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
807/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
808///
809/// # Errors
810///
811/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
812/// no single operation, or binds a page size `variables` does not name.
813pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
814    github_graphql_node_count::node_count(document, variables)
815}
816
817/// The issue-title prefix that makes a board issue a document.
818///
819/// A GitHub Projects board has no document type — it holds issues — so the discriminator
820/// is the title, and this is the whole of it: an issue whose title begins with these bytes
821/// is a document and every other issue is the task or project the sub-issue rule makes it.
822///
823/// It is spelled **once**, here, and read rather than restated everywhere else — including
824/// by the shared journeys, which take it from this constant so a board fixture cannot
825/// drift from what this source reads. `docs/metadata.md` records the two consequences that
826/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
827/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
828/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
829pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
830
831/// Exact GraphQL query documents issued by this plugin.
832///
833/// Keeping the production documents here lets the pinned-schema test validate the same
834/// bytes that are sent to GitHub, rather than a test-only copy which could drift
835/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
836/// field, and its guarded caller always supplies the complete existing option set with ids.
837pub mod graphql {
838    /// The board half of one item: the field values every document here reads it from.
839    ///
840    /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
841    /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
842    /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
843    /// *the same value*, because
844    /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
845    /// one path. Three spellings of it is what would drift, so there is one.
846    ///
847    /// The `Status` option and this source's own origin text field are the whole of it. It
848    /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
849    /// content, so it holds nothing the content's own `labels` do not already say, and it
850    /// would sit a label connection two page sizes deep.
851    macro_rules! board_item_values {
852        () => {
853            r#"fieldValues(first:$nestedFirst){nodes{
854          ... on ProjectV2ItemFieldSingleSelectValue{name field{
855            ... on ProjectV2SingleSelectField{id name options{id name}}
856          }}
857          ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
858        }pageInfo{hasNextPage}}"#
859        };
860    }
861
862    /// The variable every document reading an issue declares for the relationship events its
863    /// change token counts, with its value as the default, so no caller binds it.
864    ///
865    /// A variable rather than a literal list because every argument a production document
866    /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
867    /// its default to the pinned enum. Its default is the six event types GitHub records a
868    /// relationship added or removed with: a dependency on either end, as `blockedBy` and
869    /// `blocking`, so one a person makes from the far end moves this end's count too; and a
870    /// project membership on the task, as its parent, which GitHub records there whichever
871    /// issue the move was made from. A sub-issue's events on its parent are not counted: a
872    /// project carries no task token, and a copy writes nothing of a task its own sub-issues
873    /// decide. [`super::change_token`] says why the count is in the token.
874    macro_rules! relationship_events_variable {
875        () => {
876            ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
877        };
878    }
879
880    /// Everything this source reads about one issue, wherever it reaches that issue.
881    ///
882    /// A macro rather than a constant so the three documents below can `concat!` it: one
883    /// spelling of these fields is what makes an issue read through the board-scoped
884    /// search, through its own node id, and through its project's sub-issue relationship
885    /// resolve to *the same* item, which is the whole of what
886    /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
887    ///
888    /// `projectItems` is what carries the board half of an issue: the board item's own id
889    /// and the [`board_item_values!`] above — the `Status` option and this source's origin
890    /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
891    /// issue rather than on the board, which is what makes the cost of a read proportional
892    /// to what was asked for instead of to the board's size.
893    ///
894    /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
895    /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
896    /// not on that page: a page here is where the search for the entry starts rather than
897    /// where it ends.
898    ///
899    /// It does **not** select the board's `Labels` field value, and that is the whole of
900    /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
901    /// a label connection there sits under `fieldValues` under `projectItems` under a page
902    /// of issues, spending `$nestedFirst` twice down one path, and took
903    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
904    /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
905    /// above, and that connection is where every label this source reports comes from. No
906    /// document in this module selects the board field any longer, [`BOARD`] included; the
907    /// module documentation records why nothing it could have held is lost.
908    macro_rules! board_issue {
909        () => {
910            concat!(
911                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}
912      labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
913      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
914        "#,
915                board_item_values!(),
916                r#"}pageInfo{hasNextPage endCursor}}}"#
917            )
918        };
919    }
920
921    /// Every issue of one board, found by a search scoped to that board.
922    ///
923    /// This is how the projects a board holds are listed, and it selects no `items`
924    /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
925    /// container walked page by page, so nothing nested inside a board item is paid for.
926    /// Which of the issues it returns is a project is then read off `parent` — GitHub
927    /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
928    /// discriminator has to be applied to the field, which is a scalar on the issue and
929    /// costs nothing.
930    pub const SEARCH_ISSUES: &str = concat!(
931        r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
932        relationship_events_variable!(),
933        r#"){
934      search(query:$search,type:$type,first:$first,after:$after){
935        pageInfo{hasNextPage endCursor}
936        nodes{__typename ...BoardIssue}
937      }
938    }"#,
939        board_issue!()
940    );
941
942    /// What a dependency read selects of each far end: enough to say which kind of item it
943    /// is, its body included for the kind marker.
944    macro_rules! related_issue {
945        () => {
946            " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
947        };
948    }
949
950    /// One issue by its own node id, which is what a qualified id names here — with what a
951    /// write of it needs and the issue does not carry in `board_issue!`: the field
952    /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
953    ///
954    /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
955    /// answers a write made moments ago with the value from before it, and resolving a node
956    /// id does not.
957    ///
958    /// **Why those two ride here and not on the fragment.** A copy or an update of an item
959    /// reads it by its own id, and with them that one read answers everything the write
960    /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
961    /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
962    /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
963    /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
964    /// they sit under one item, and this read is still one point.
965    pub const ISSUE: &str = concat!(
966        r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
967        relationship_events_variable!(),
968        r#"){
969      node(id:$id){__typename ...BoardIssue ... on Issue{
970        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
971          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
972          ... on ProjectV2Field{__typename id name dataType}
973        }pageInfo{hasNextPage}}}}}
974        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
975      }}
976    }"#,
977        board_issue!(),
978        related_issue!()
979    );
980
981    /// One project's tasks: the sub-issues of the issue that project is, each with a page of
982    /// what blocks it.
983    ///
984    /// The work this costs is the project's own size. Nothing about it grows as the board
985    /// gains projects, or as those projects gain tasks.
986    ///
987    /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
988    /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
989    /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
990    /// costs grow with its tasks rather than with the pages of them. Carried here, a task
991    /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
992    /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
993    /// It adds a connection under each issue of the page — one rate-limit point per page,
994    /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
995    /// answer questions that never read an edge.
996    pub const SUB_ISSUES: &str = concat!(
997        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
998        relationship_events_variable!(),
999        r#"){
1000      node(id:$id){__typename
1001        ... on Issue{subIssues(first:$first,after:$after){
1002          pageInfo{hasNextPage endCursor}
1003          nodes{__typename ...BoardIssue ... on Issue{
1004            blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1005          }}
1006        }}}
1007    }"#,
1008        board_issue!(),
1009        related_issue!()
1010    );
1011
1012    /// What a read of the board's own `items` selects of each item's content.
1013    ///
1014    /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1015    /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1016    /// content by one spelling.
1017    macro_rules! board_item_content {
1018        () => {
1019            r#" content{
1020        ... 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}}}
1021        ... on PullRequest{__typename id}
1022        ... on DraftIssue{__typename id title body createdAt updatedAt}
1023      }"#
1024        };
1025    }
1026
1027    /// Reads the board's fields and one page of its items.
1028    pub const BOARD: &str = concat!(
1029        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1030        relationship_events_variable!(),
1031        r#"){
1032      owner:repositoryOwner(login:$owner){
1033        ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1034      }
1035    } fragment Board on ProjectV2 { id title
1036      fields(first:$nestedFirst){nodes{
1037        ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1038        ... on ProjectV2Field{__typename id name dataType}
1039      }pageInfo{hasNextPage}}
1040      items(first:$first,after:$after){nodes{id updatedAt "#,
1041        board_item_values!(),
1042        board_item_content!(),
1043        r#"} pageInfo{hasNextPage endCursor}}
1044    }"#
1045    );
1046
1047    /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1048    /// the board.
1049    ///
1050    /// **`originItems`** is the board's own items narrowed by its own field filter —
1051    /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1052    /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1053    /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1054    /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1055    /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1056    /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1057    /// fresh `addProjectV2ItemById` the way that connection does.
1058    ///
1059    /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1060    /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1061    /// indexes that comment, and the index catches up with a write in a second or two rather
1062    /// than in minutes, so it finds a carrier another process wrote that the first read is
1063    /// still behind on.
1064    ///
1065    /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1066    /// — and resumes from its own cursor; a connection already walked to its end is resumed
1067    /// from its last cursor, which answers an empty page. Every candidate either read returns
1068    /// is confirmed against its own origin field before it is reported, so a token match of
1069    /// the search or anything else the filter admits never is.
1070    ///
1071    /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1072    /// own whole reads counts this one among them.
1073    pub const ORIGIN_LOOKUP: &str = concat!(
1074        r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1075        relationship_events_variable!(),
1076        r#"){
1077      originItems:repositoryOwner(login:$owner){
1078        ... on ProjectV2Owner{projectV2(number:$number){
1079          items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1080        board_item_values!(),
1081        board_item_content!(),
1082        r#"} pageInfo{hasNextPage endCursor}}
1083        }}
1084      }
1085      search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1086        pageInfo{hasNextPage endCursor}
1087        nodes{__typename ...BoardIssue}
1088      }
1089    }"#,
1090        board_issue!()
1091    );
1092
1093    /// The board's own id and field definitions, and not one of its items.
1094    ///
1095    /// What a write needs of the board when the item it writes does not say: the id a field
1096    /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1097    /// origin fields. It selects no `items`, so what it costs is the board's field list
1098    /// however many items the board holds — and it decides nothing about which items those
1099    /// are, which is the question a read of one item by its own id answers instead.
1100    ///
1101    /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1102    /// board's item reads by their root counts this one among them.
1103    pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1104      boardFields:repositoryOwner(login:$owner){
1105        ... on ProjectV2Owner{projectV2(number:$number){id
1106          fields(first:$nestedFirst){nodes{
1107            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1108            ... on ProjectV2Field{__typename id name dataType}
1109          }pageInfo{hasNextPage}}
1110        }}
1111      }
1112    }"#;
1113
1114    /// One board draft by its own node id, with the board item it sits in.
1115    ///
1116    /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1117    /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1118    /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1119    /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1120    /// board listing hands it to, and nothing has to list the board to find one.
1121    pub const DRAFT: &str = concat!(
1122        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1123      node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1124        projectV2Items(first:$boardItems){nodes{id project{id number}
1125        "#,
1126        board_item_values!(),
1127        r#"}pageInfo{hasNextPage endCursor}}}}
1128    }"#
1129    );
1130
1131    /// One issue's board memberships alone, walked past the page a read of it carried.
1132    ///
1133    /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1134    /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1135    /// boards than that page holds may have this board's entry past its end. This asks that
1136    /// one issue for its memberships and nothing else — the caller already holds the issue —
1137    /// so an answer of "this board does not hold it" is only ever given about a connection
1138    /// read to exhaustion.
1139    ///
1140    /// It selects the board item's id, its project number and the same
1141    /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1142    /// very same resolver: an issue recovered this way reports the same title, the same
1143    /// status, the same labels and the same qualified id as one whose entry was on the
1144    /// page.
1145    ///
1146    /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1147    /// multiplies through it and the membership connection can be walked at
1148    /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1149    /// further request for any issue a person really keeps.
1150    pub const ISSUE_BOARD_ITEMS: &str = concat!(
1151        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1152      node(id:$id){
1153        ... on Issue{projectItems(first:$first,after:$after){
1154          nodes{id updatedAt project{id number}
1155        "#,
1156        board_item_values!(),
1157        r#"}
1158          pageInfo{hasNextPage endCursor}}}
1159      }
1160    }"#
1161    );
1162    /// Whether the board's Project is public — half of what decides whether a write here can
1163    /// be read by anybody. Needs the `read:project` scope.
1164    pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1165    /// Resolves the configured repository's node id, which creating an issue requires.
1166    pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1167    /// What creating an issue needs and has not read yet: the board's own id and field
1168    /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1169    /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1170    ///
1171    /// Sent at the point a create knows which repository it is for, when neither half is
1172    /// already known to this process; a create needing only one of them sends that one's own
1173    /// document. Neither half is kept past the process: a field's option ids are re-minted by
1174    /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1175    /// status.
1176    pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1177      boardFields:repositoryOwner(login:$owner){
1178        ... on ProjectV2Owner{projectV2(number:$number){id
1179          fields(first:$nestedFirst){nodes{
1180            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1181            ... on ProjectV2Field{__typename id name dataType}
1182          }pageInfo{hasNextPage}}
1183        }}
1184      }
1185      repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1186    }"#;
1187    /// Reads both dependency directions for one issue, with each far end's own kind — and
1188    /// the issue's own body, which is where an edge to another source is recorded, so that
1189    /// half of a dependency read needs no second read of the issue or of the board.
1190    pub const ISSUE_DEPENDENCIES: &str = concat!(
1191        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1192      ... on Issue{body
1193        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1194        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1195      }}}"#,
1196        related_issue!()
1197    );
1198    /// Creates one issue in the configured repository, on no board: [`ADD_TO_BOARD`] files
1199    /// it. `CreateIssueInput.projectV2Ids` is not sent — see the crate's notes on what GitHub
1200    /// answered when it was.
1201    pub const CREATE_ISSUE: &str =
1202        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1203    /// Puts an existing issue on the configured board.
1204    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1205    /// Updates an issue's visible fields and its open or closed state in one call.
1206    pub const UPDATE_ISSUE: &str =
1207        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1208    /// Updates an existing draft's user-visible fields.
1209    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1210    /// Updates a text or single-select value on one project item.
1211    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}}}}}}}}"#;
1212    /// Writes up to six board fields and clears up to three in one ordered mutation: every
1213    /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1214    /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1215    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}}}"#;
1216    /// Clears one project item's value of one field, which is what a `none` priority is.
1217    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}}}}}}}}"#;
1218    /// Creates one field: a single-select one with its options, or a text one for a projected
1219    /// metadata value. Only the guarded field setup may use this document, and only for a
1220    /// field the board lacks.
1221    pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1222    /// Replaces a single-select field's options. Only the guarded field setup — the
1223    /// `status-options` and `fields` operations — may use this document, because GitHub
1224    /// treats the input as the complete option list.
1225    pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1226    /// A fresh snapshot of the Status field and every board item's assignment.
1227    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}}}}}}"#;
1228    /// Files one issue under another as a sub-issue, which is what project membership is.
1229    pub const ADD_SUB_ISSUE: &str =
1230        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1231    /// Takes one issue back out of its parent.
1232    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1233    /// Adds GitHub's native issue blocked-by relationship.
1234    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1235    /// Removes one native issue blocked-by relationship.
1236    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1237    /// Deletes one issue, which takes its board item with it.
1238    ///
1239    /// The engine sends this in one situation only: undoing a copy that could not finish,
1240    /// over the items that same copy created. Deleting the issue removes the board item
1241    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1242    pub const DELETE_ISSUE: &str =
1243        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1244
1245    /// Everything this source reads about one issue comment, wherever it reaches one.
1246    ///
1247    /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1248    /// and a comment just edited are handed to one mapper, so they are selected by one
1249    /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1250    /// longer exists, and `login` is the one member every kind of actor carries.
1251    macro_rules! issue_comment {
1252        () => {
1253            "id author{login} createdAt updatedAt body url"
1254        };
1255    }
1256
1257    /// One task's comments: a page of its issue's own `comments` connection.
1258    ///
1259    /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1260    /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1261    /// list every time somebody edited it; left unordered the connection answers in the order
1262    /// the comments were written, which is the order GitHub documents for the same collection
1263    /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1264    /// node count and the caller's own page size is pushed straight down.
1265    pub const ISSUE_COMMENTS: &str = concat!(
1266        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1267        issue_comment!(),
1268        r#"}pageInfo{hasNextPage endCursor}}}}}"#
1269    );
1270    /// One issue by its own node id, with a page of its comments: what `task show` and a
1271    /// comment listing read, in one request.
1272    ///
1273    /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1274    /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1275    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1276    /// connection there would multiply through both of those documents' price, and neither
1277    /// needs one.
1278    pub const ISSUE_DETAIL: &str = concat!(
1279        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1280        relationship_events_variable!(),
1281        r#"){
1282      node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1283        issue_comment!(),
1284        r#"}pageInfo{hasNextPage endCursor}}}}
1285    }"#,
1286        board_issue!()
1287    );
1288
1289    /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1290    /// page of its comments when `$comments` asks for them.
1291    macro_rules! issue_details_alias {
1292        ($n:literal) => {
1293            concat!(
1294                "\n      i",
1295                stringify!($n),
1296                ":node(id:$id",
1297                stringify!($n),
1298                "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1299                issue_comment!(),
1300                "}pageInfo{hasNextPage endCursor}}}}"
1301            )
1302        };
1303    }
1304
1305    /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1306    /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1307    ///
1308    /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1309    /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1310    /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1311    /// neither — so every connection under it would be priced at nothing and the pin in
1312    /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1313    /// one-item read the model already prices, so the batch costs what its aliases cost.
1314    ///
1315    /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1316    /// slots it has no item for to the last item it does, and reads that item again; the
1317    /// price is the document's, whatever its variables, so a short batch costs what a full
1318    /// one does and nothing more.
1319    pub const ISSUE_DETAILS: &str = concat!(
1320        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!"#,
1321        relationship_events_variable!(),
1322        r#"){"#,
1323        issue_details_alias!(0),
1324        issue_details_alias!(1),
1325        issue_details_alias!(2),
1326        issue_details_alias!(3),
1327        issue_details_alias!(4),
1328        issue_details_alias!(5),
1329        issue_details_alias!(6),
1330        issue_details_alias!(7),
1331        issue_details_alias!(8),
1332        issue_details_alias!(9),
1333        issue_details_alias!(10),
1334        issue_details_alias!(11),
1335        issue_details_alias!(12),
1336        issue_details_alias!(13),
1337        issue_details_alias!(14),
1338        issue_details_alias!(15),
1339        issue_details_alias!(16),
1340        issue_details_alias!(17),
1341        issue_details_alias!(18),
1342        issue_details_alias!(19),
1343        issue_details_alias!(20),
1344        issue_details_alias!(21),
1345        issue_details_alias!(22),
1346        issue_details_alias!(23),
1347        "\n    }",
1348        board_issue!()
1349    );
1350
1351    /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1352    /// page of its comments and the issues it is blocked by.
1353    macro_rules! issue_copy_details_alias {
1354        ($n:literal) => {
1355            concat!(
1356                "\n      i",
1357                stringify!($n),
1358                ":node(id:$id",
1359                stringify!($n),
1360                "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1361                issue_comment!(),
1362                "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1363            )
1364        };
1365    }
1366
1367    /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1368    /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1369    /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1370    /// at a time, so a copy of a changed task costs this read and nothing else.
1371    ///
1372    /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1373    /// what `task show-many` is priced by and which reads no edge.
1374    pub const ISSUE_COPY_DETAILS: &str = concat!(
1375        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!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1376        relationship_events_variable!(),
1377        r#"){"#,
1378        issue_copy_details_alias!(0),
1379        issue_copy_details_alias!(1),
1380        issue_copy_details_alias!(2),
1381        issue_copy_details_alias!(3),
1382        issue_copy_details_alias!(4),
1383        issue_copy_details_alias!(5),
1384        issue_copy_details_alias!(6),
1385        issue_copy_details_alias!(7),
1386        issue_copy_details_alias!(8),
1387        issue_copy_details_alias!(9),
1388        issue_copy_details_alias!(10),
1389        issue_copy_details_alias!(11),
1390        issue_copy_details_alias!(12),
1391        issue_copy_details_alias!(13),
1392        issue_copy_details_alias!(14),
1393        issue_copy_details_alias!(15),
1394        issue_copy_details_alias!(16),
1395        issue_copy_details_alias!(17),
1396        issue_copy_details_alias!(18),
1397        issue_copy_details_alias!(19),
1398        issue_copy_details_alias!(20),
1399        "\n    }",
1400        board_issue!(),
1401        related_issue!()
1402    );
1403
1404    /// Which issue one comment is on, read before that comment is edited or removed.
1405    ///
1406    /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1407    /// comment id given against the wrong task would change a comment on another issue.
1408    pub const COMMENT_ISSUE: &str =
1409        r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1410    /// Adds one comment to an issue, signed as the account the token belongs to.
1411    pub const ADD_COMMENT: &str = concat!(
1412        r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1413        issue_comment!(),
1414        r#"}}}}"#
1415    );
1416    /// Replaces the body of one issue comment.
1417    pub const UPDATE_COMMENT: &str = concat!(
1418        r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
1419        issue_comment!(),
1420        r#"}}}"#
1421    );
1422    /// Removes one issue comment. Its payload carries nothing about the comment it removed.
1423    pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
1424
1425    /// What a mutation of a write the store's cache expects selects of the issue it wrote:
1426    /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
1427    /// change token's every part and the first page of its comments.
1428    ///
1429    /// Sent **only** while that write runs, in place of the document named beside each
1430    /// below, so every other write's documents, prices and request counts are what they
1431    /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
1432    /// mutation's response is stamped seconds after the change it made, so against GitHub
1433    /// itself a response rarely determines a version; the cache is then left unserveable
1434    /// for the task, and its next read refreshes it.
1435    macro_rules! whole_issue {
1436        () => {
1437            concat!(
1438                r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
1439                issue_comment!(),
1440                r#"}pageInfo{hasNextPage endCursor}}}"#
1441            )
1442        };
1443    }
1444
1445    /// The variables every document of `whole_issue!` declares beside its own.
1446    macro_rules! whole_variables {
1447        () => {
1448            concat!(
1449                r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1450                relationship_events_variable!()
1451            )
1452        };
1453    }
1454
1455    /// [`ADD_COMMENT`], selecting the whole issue commented on.
1456    pub const ADD_COMMENT_WHOLE: &str = concat!(
1457        r#"mutation($input:AddCommentInput!"#,
1458        whole_variables!(),
1459        r#"){addComment(input:$input){subject{id "#,
1460        whole_issue!(),
1461        r#"} commentEdge{node{"#,
1462        issue_comment!(),
1463        r#"}}}}"#,
1464        board_issue!()
1465    );
1466    /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
1467    pub const UPDATE_COMMENT_WHOLE: &str = concat!(
1468        r#"mutation($input:UpdateIssueCommentInput!"#,
1469        whole_variables!(),
1470        r#"){updateIssueComment(input:$input){issueComment{"#,
1471        issue_comment!(),
1472        r#" issue{"#,
1473        whole_issue!(),
1474        r#"}}}}"#,
1475        board_issue!()
1476    );
1477    /// [`UPDATE_ISSUE`], selecting the whole issue updated.
1478    pub const UPDATE_ISSUE_WHOLE: &str = concat!(
1479        r#"mutation($input:UpdateIssueInput!"#,
1480        whole_variables!(),
1481        r#"){updateIssue(input:$input){issue{id "#,
1482        whole_issue!(),
1483        r#"}}}"#,
1484        board_issue!()
1485    );
1486    /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
1487    pub const UPDATE_FIELD_WHOLE: &str = concat!(
1488        r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
1489        whole_variables!(),
1490        r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
1491        whole_issue!(),
1492        r#"}}}}"#,
1493        board_issue!()
1494    );
1495    /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
1496    pub const CLEAR_FIELD_WHOLE: &str = concat!(
1497        r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
1498        whole_variables!(),
1499        r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
1500        whole_issue!(),
1501        r#"}}}}"#,
1502        board_issue!()
1503    );
1504
1505    /// The document sent in `document`'s place while a write the store's cache expects runs,
1506    /// when it has one.
1507    #[must_use]
1508    pub fn whole_twin(document: &str) -> Option<&'static str> {
1509        [
1510            (ADD_COMMENT, ADD_COMMENT_WHOLE),
1511            (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
1512            (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
1513            (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
1514            (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
1515        ]
1516        .into_iter()
1517        .find(|(plain, _)| *plain == document)
1518        .map(|(_, whole)| whole)
1519    }
1520
1521    /// Every document above, with what this source is doing when it sends one.
1522    ///
1523    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
1524    /// name the call that was refused, and a `match` with a catch-all arm would answer a
1525    /// document added later with "talking to GitHub" and never say so.
1526    ///
1527    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
1528    /// const` here that this list omits, so the two cannot part — which is the same guard
1529    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
1530    pub const DOCUMENTS: [(&str, &str); 40] = [
1531        (SEARCH_ISSUES, "searching this board's issues"),
1532        (ISSUE, "reading one issue"),
1533        (
1534            ISSUE_BOARD_ITEMS,
1535            "reading one issue's board memberships past the page it came with",
1536        ),
1537        (SUB_ISSUES, "reading a project's tasks"),
1538        (BOARD, "reading the board"),
1539        (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
1540        (BOARD_FIELDS, "reading the board's fields"),
1541        (DRAFT, "reading one draft"),
1542        (REPOSITORY, "reading the destination repository"),
1543        (
1544            PROJECT_VISIBILITY,
1545            "reading whether the board's project is public",
1546        ),
1547        (
1548            CREATION_CONTEXT,
1549            "reading the board's fields and the destination repository",
1550        ),
1551        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
1552        (CREATE_ISSUE, "creating an issue"),
1553        (ADD_TO_BOARD, "adding an issue to the board"),
1554        (UPDATE_ISSUE, "updating an issue"),
1555        (UPDATE_DRAFT, "updating a draft item"),
1556        (UPDATE_FIELD, "writing a board field"),
1557        (UPDATE_FIELDS, "writing board fields together"),
1558        (CLEAR_FIELD, "clearing a board field"),
1559        (
1560            CREATE_FIELD,
1561            "creating a board single-select field with its options",
1562        ),
1563        (
1564            STATUS_OPTIONS_SNAPSHOT,
1565            "snapshotting board Status options and assignments",
1566        ),
1567        (
1568            STATUS_OPTIONS_UPDATE,
1569            "safely replacing the board Status option list",
1570        ),
1571        (ADD_SUB_ISSUE, "filing an issue under its project"),
1572        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
1573        (ADD_BLOCKED_BY, "recording a dependency"),
1574        (REMOVE_BLOCKED_BY, "removing a dependency"),
1575        (DELETE_ISSUE, "deleting an issue"),
1576        (ISSUE_COMMENTS, "reading a task's comments"),
1577        (ISSUE_DETAIL, "reading one issue with its comments"),
1578        (
1579            ISSUE_DETAILS,
1580            "reading a batch of issues with their comments",
1581        ),
1582        (
1583            ISSUE_COPY_DETAILS,
1584            "reading a batch of issues with their comments and dependencies for a copy",
1585        ),
1586        (COMMENT_ISSUE, "reading which issue a comment is on"),
1587        (ADD_COMMENT, "adding a comment"),
1588        (UPDATE_COMMENT, "editing a comment"),
1589        (DELETE_COMMENT, "deleting a comment"),
1590        (
1591            ADD_COMMENT_WHOLE,
1592            "adding a comment, answered with the whole issue",
1593        ),
1594        (
1595            UPDATE_COMMENT_WHOLE,
1596            "editing a comment, answered with the whole issue",
1597        ),
1598        (
1599            UPDATE_ISSUE_WHOLE,
1600            "updating an issue, answered with it whole",
1601        ),
1602        (
1603            UPDATE_FIELD_WHOLE,
1604            "writing a board field, answered with the whole issue",
1605        ),
1606        (
1607            CLEAR_FIELD_WHOLE,
1608            "clearing a board field, answered with the whole issue",
1609        ),
1610    ];
1611}
1612
1613/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
1614/// under, the variable carrying its input, and the boolean variable that includes it.
1615#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1616pub struct FieldSlot {
1617    /// The key of the answer's `data` this field's payload is under.
1618    pub alias: &'static str,
1619    /// The variable carrying this field's input.
1620    pub variable: &'static str,
1621    /// The boolean variable whose `true` sends this field.
1622    pub include: &'static str,
1623}
1624
1625/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
1626pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
1627    FieldSlot {
1628        alias: "updateProjectV2ItemFieldValue",
1629        variable: "input",
1630        include: "writeFirst",
1631    },
1632    FieldSlot {
1633        alias: "second",
1634        variable: "second",
1635        include: "writeSecond",
1636    },
1637    FieldSlot {
1638        alias: "third",
1639        variable: "third",
1640        include: "writeThird",
1641    },
1642    FieldSlot {
1643        alias: "fourth",
1644        variable: "fourth",
1645        include: "writeFourth",
1646    },
1647    FieldSlot {
1648        alias: "fifth",
1649        variable: "fifth",
1650        include: "writeFifth",
1651    },
1652    FieldSlot {
1653        alias: "sixth",
1654        variable: "sixth",
1655        include: "writeSixth",
1656    },
1657];
1658
1659/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
1660pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
1661    FieldSlot {
1662        alias: "cleared",
1663        variable: "clear",
1664        include: "writeClear",
1665    },
1666    FieldSlot {
1667        alias: "clearedSecond",
1668        variable: "clearSecond",
1669        include: "writeClearSecond",
1670    },
1671    FieldSlot {
1672        alias: "clearedThird",
1673        variable: "clearThird",
1674        include: "writeClearThird",
1675    },
1676];
1677
1678/// Which of GitHub's two rate limiters refused a request.
1679///
1680/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
1681/// secondary one — so an operator told the wrong one takes the wrong next step, which is
1682/// the whole reason this is carried rather than collapsed into "rate limited".
1683#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1684enum Limiter {
1685    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
1686    Primary,
1687    /// The burst limiter over content-generating requests, which nothing reports.
1688    Secondary,
1689}
1690
1691/// The wordings GitHub answers a secondary rate limit with.
1692///
1693/// It sends them under a forbidden status, under a too-many-requests status, and inside
1694/// the `errors` of a *successful* response, which is why the text is what this matches on
1695/// rather than the status. `abuse detection` is the wording GitHub used before the
1696/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
1697/// what a burst of content creation is refused with.
1698///
1699/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
1700/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
1701/// when, and the drift gate reconciles the two lists both ways. Public for that gate
1702/// alone — a caller has no use for it, and matching on a refusal is this source's job.
1703pub const SECONDARY_WORDINGS: [&str; 5] = [
1704    "secondary rate limit",
1705    "temporarily blocked from content creation",
1706    "abuse detection",
1707    "submitted too quickly",
1708    "exceeded a secondary",
1709];
1710
1711/// The wordings GitHub answers an exhausted primary budget with.
1712///
1713/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
1714/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
1715/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
1716/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
1717/// two phrases is a substring of it, so without it that answer read as a refusal that will
1718/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
1719/// one reason.
1720pub const PRIMARY_WORDINGS: [&str; 4] = [
1721    "api rate limit exceeded",
1722    "api rate limit already exceeded",
1723    "rate limit exceeded",
1724    "rate_limited",
1725];
1726
1727/// What a response *says about itself*, which is the only place a refusal can be read.
1728///
1729/// Deliberately not the whole response body. A board is a place people write about their
1730/// own work, and a task on it titled "the secondary rate limit" would, matched across the
1731/// raw text, turn a perfectly good answer into a refusal this source then waited out and
1732/// reported. So the item data is never read: what is read is GitHub's own REST-style
1733/// `message` envelope, which is what a forbidden status carries, and the `message` and
1734/// `type` of each GraphQL error, which is where a *successful* response says it.
1735///
1736/// A body that is not JSON at all has nothing structured to read, so only a failing
1737/// response's own text is taken — a successful response that is not JSON is malformed
1738/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
1739fn refusal_wording(status: StatusCode, body: &str) -> String {
1740    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
1741        return if status.is_success() {
1742            String::new()
1743        } else {
1744            body.to_owned()
1745        };
1746    };
1747    let mut said: Vec<&str> = parsed
1748        .get("message")
1749        .and_then(Value::as_str)
1750        .into_iter()
1751        .collect();
1752    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
1753        for error in errors {
1754            said.extend(
1755                ["message", "type"]
1756                    .into_iter()
1757                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
1758            );
1759        }
1760    }
1761    said.join("; ")
1762}
1763
1764impl Limiter {
1765    /// Which limiter refused this response, or `None` when none of them did.
1766    ///
1767    /// The wording is read first and the status only decides what carries none of it,
1768    /// because GitHub answers a secondary limit with a forbidden status far more often
1769    /// than with too-many-requests — while a forbidden status saying nothing about a limit
1770    /// really is a credential this token lacks.
1771    ///
1772    /// A response is a refusal because of its status or its own wording. A spent budget
1773    /// only ever explains one; it never turns an answer into a refusal.
1774    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
1775        let normalized = refusal_wording(status, body).to_ascii_lowercase();
1776        if SECONDARY_WORDINGS
1777            .iter()
1778            .any(|wording| normalized.contains(wording))
1779        {
1780            return Some(Self::Secondary);
1781        }
1782        if status == StatusCode::TOO_MANY_REQUESTS {
1783            return Some(Self::Primary);
1784        }
1785        // An exhausted budget *explains* a response that failed; it does not make one that
1786        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
1787        // request the budget allowed as well as on the ones it then refuses, so reading
1788        // the header alone threw away a good answer — and, once refusals were retried,
1789        // replayed a request that had already taken effect.
1790        if !status.is_success() && budget_exhausted {
1791            return Some(Self::Primary);
1792        }
1793        // A successful response saying it: GitHub reports a GraphQL rate limit in the
1794        // `errors` of an HTTP 200, where nothing about the status says so at all.
1795        if status.is_success()
1796            && PRIMARY_WORDINGS
1797                .iter()
1798                .any(|wording| normalized.contains(wording))
1799        {
1800            return Some(Self::Primary);
1801        }
1802        None
1803    }
1804
1805    /// What this limiter is called where an operator can look it up.
1806    const fn name(self) -> &'static str {
1807        match self {
1808            Self::Primary => "GitHub's primary API rate limit",
1809            Self::Secondary => "GitHub's secondary rate limit",
1810        }
1811    }
1812
1813    /// What the endpoint an operator would go and check says about this limiter.
1814    const fn where_to_look(self) -> &'static str {
1815        match self {
1816            Self::Primary => {
1817                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
1818                 comes back."
1819            }
1820            Self::Secondary => {
1821                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
1822                 primary budget and does not report this one, so budget showing there says \
1823                 nothing about this refusal, and every further attempt extends it."
1824            }
1825        }
1826    }
1827
1828    /// The next step this limiter actually calls for.
1829    const fn what_to_do(self) -> &'static str {
1830        match self {
1831            Self::Primary => {
1832                "wait for the reset `gh api rate_limit` reports, then run the command again."
1833            }
1834            Self::Secondary => {
1835                "leave this board alone for a few minutes, then run the command again — or \
1836                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
1837            }
1838        }
1839    }
1840}
1841
1842/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
1843#[derive(Debug, Clone, Copy)]
1844struct Limited {
1845    limiter: Limiter,
1846    hint: Option<u64>,
1847}
1848
1849impl Limited {
1850    /// What the caller is told once this source has waited as long as it may.
1851    ///
1852    /// Both limiters report as [`SourceError::RateLimited`], because that is what
1853    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
1854    /// about *which* limiter it was makes it a different kind of failure. What differs is
1855    /// the operator's next step, and that is what the message carries — a secondary
1856    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
1857    /// budget looks fine, and then back to retry the very burst that was refused.
1858    fn exhausted(
1859        self,
1860        doing: &str,
1861        waits: u32,
1862        waited: Duration,
1863        needed: Duration,
1864        budget: Duration,
1865    ) -> SourceError {
1866        SourceError::RateLimited {
1867            retry_after_seconds: self.hint,
1868            message: Some(format!(
1869                "{} refused this source while {doing}; it waited {} out over {} and was refused \
1870                 again, and the next wait of {} would take it past the {} one call may spend \
1871                 waiting. {} next: {}",
1872                self.limiter.name(),
1873                plural(waits, "refusal"),
1874                seconds(waited),
1875                seconds(needed),
1876                seconds(budget),
1877                self.limiter.where_to_look(),
1878                self.limiter.what_to_do(),
1879            )),
1880        }
1881    }
1882}
1883
1884/// One HTTP attempt's result, with what its response said about the rate limit.
1885///
1886/// The two travel together so the record and the outcome are written from the same place:
1887/// what a response said about the budget is only readable while that response is in hand,
1888/// and what the attempt *meant* is only decidable once its body has been read.
1889struct Attempted {
1890    result: Result<Value, Attempt>,
1891    limits: accounting::RateLimit,
1892    /// GitHub's own reported cost for this call, for a document that asked for it.
1893    reported_cost: Option<u64>,
1894}
1895
1896/// One attempt's outcome: an error to report, or a rate limit to wait out.
1897enum Attempt {
1898    Failed(SourceError),
1899    Limited(Limited),
1900}
1901
1902fn plural(count: u32, thing: &str) -> String {
1903    if count == 1 {
1904        format!("{count} {thing}")
1905    } else {
1906        format!("{count} {thing}s")
1907    }
1908}
1909
1910fn seconds(duration: Duration) -> String {
1911    format!("{:.1}s", duration.as_secs_f64())
1912}
1913
1914/// The member [`stamp_served`] writes onto every node of a response that says when it last
1915/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
1916const SERVED_AT: &str = "onetaskgraphServedAt";
1917
1918/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
1919/// node's time later knows the server's clock on the response that read it — which is what
1920/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
1921fn stamp_served(data: &mut Value, served: &str) {
1922    match data {
1923        Value::Object(members) => {
1924            if members.contains_key("updatedAt") {
1925                members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
1926            }
1927            for member in members.values_mut() {
1928                stamp_served(member, served);
1929            }
1930        }
1931        Value::Array(items) => {
1932            for item in items {
1933                stamp_served(item, served);
1934            }
1935        }
1936        _ => {}
1937    }
1938}
1939
1940/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
1941///
1942/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
1943/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
1944/// header, and neither is what makes a response a refusal — so the whole cost of one this
1945/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
1946/// instead. Refusing the response over the header would turn a readable refusal into an
1947/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
1948fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
1949    value
1950        .and_then(|value| value.to_str().ok())
1951        .and_then(|value| value.trim().parse::<u64>().ok())
1952}
1953
1954/// Every mutation this source sends creates content — an issue, a board item, a field of
1955/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
1956/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
1957/// and what the keyword says are the same set. That is what makes the keyword a sound test
1958/// rather than a convenient one: pacing an edit or a removal the limiter might not have
1959/// counted costs a wait, and not pacing one it did count costs the next fifty minutes.
1960fn is_mutation(query: &str) -> bool {
1961    query.trim_start().starts_with("mutation")
1962}
1963
1964/// What this source was doing, for a diagnostic that has to say so.
1965///
1966/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
1967/// a document added without a description is caught by that list's own gate instead of
1968/// falling through to the vague arm below.
1969fn operation_description(query: &str) -> &'static str {
1970    graphql::DOCUMENTS
1971        .iter()
1972        .find(|(document, _)| *document == query)
1973        .map_or("talking to GitHub", |(_, doing)| *doing)
1974}
1975
1976/// GitHub's published ceiling on content-generating requests, per minute.
1977///
1978/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
1979/// GitHub's number rather than this source's: [`MIN_MUTATION_INTERVAL_MS`] is *derived*
1980/// from it, so a pacing value checked only against itself cannot go stale here.
1981pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
1982/// The same ceiling as GitHub publishes it per hour, which this source does **not** pace
1983/// at. See [`MIN_MUTATION_INTERVAL_MS`] for why the per-minute bound is the one that
1984/// governs; it is pinned beside its sibling so the gate would notice either one moving.
1985pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
1986/// Shortest interval between two content-creating mutations, in milliseconds.
1987///
1988/// GitHub documents two secondary limits on content-generating requests:
1989/// [`CONTENT_CREATION_PER_MINUTE`] and [`CONTENT_CREATION_PER_HOUR`]. 60000/80 is 750, so
1990/// a mutation every 750 ms is the fastest rate that cannot exceed the per-minute bound,
1991/// and that is the bound a copy actually trips: a copy of one plan-sized project is a
1992/// burst of a few dozen mutations inside a few seconds. The hourly bound works out at one
1993/// every 7.2 seconds sustained, which no single copy reaches and which, used as the
1994/// spacing here, would turn an ordinary copy into an hour of waiting — so it is
1995/// deliberately *not* what this paces at. An installation that wants the hourly bound
1996/// honoured for a long sequence of copies says so through
1997/// `pacing.min_mutation_interval_ms`.
1998pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
1999/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2000///
2001/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2002/// own advice for a secondary limit — wait, and wait longer each time — without spending
2003/// the first minute of a transient refusal doing nothing.
2004pub const RETRY_BACKOFF_MS: u64 = 1_000;
2005/// Total time one call may spend waiting out rate limits before it reports a failure.
2006///
2007/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2008/// short enough that a command an operator is watching returns. The bound is what makes
2009/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2010/// the limiter, not in a process nobody can tell from a wedged one.
2011pub const RETRY_BUDGET_MS: u64 = 120_000;
2012
2013fn default_token_env() -> String {
2014    "GH_PROJECTS_TOKEN".to_owned()
2015}
2016fn default_endpoint() -> String {
2017    "https://api.github.com/graphql".to_owned()
2018}
2019
2020/// The name of a `Status` single-select option on the board.
2021///
2022/// Validated on the way in rather than checked later, so a blank option name — which
2023/// nothing on a board can be — is a state this type cannot hold.
2024#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2025#[serde(try_from = "String")]
2026#[schemars(extend("minLength" = 1))]
2027pub struct ColumnName(String);
2028
2029impl ColumnName {
2030    /// The option name, as the board spells it.
2031    fn as_str(&self) -> &str {
2032        &self.0
2033    }
2034}
2035
2036impl TryFrom<String> for ColumnName {
2037    type Error = String;
2038
2039    fn try_from(name: String) -> Result<Self, Self::Error> {
2040        if name.trim().is_empty() {
2041            return Err("a status_mapping option name cannot be blank".to_owned());
2042        }
2043        Ok(Self(name))
2044    }
2045}
2046
2047/// The two closed states this product can mean.
2048///
2049/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which is neither finished
2050/// work nor abandoned work, so nothing here ever writes it.
2051#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2052#[serde(rename_all = "kebab-case")]
2053pub enum ClosedState {
2054    /// `COMPLETED` — precisely done.
2055    Completed,
2056    /// `NOT_PLANNED` — precisely cancelled.
2057    NotPlanned,
2058}
2059
2060impl ClosedState {
2061    const fn reason(self) -> &'static str {
2062        match self {
2063            Self::Completed => "COMPLETED",
2064            Self::NotPlanned => "NOT_PLANNED",
2065        }
2066    }
2067}
2068
2069/// Configuration for one GitHub Projects v2 board.
2070///
2071/// It serializes to a document this same type reads back as itself, so a caller writing a
2072/// configuration — a consumer filing onto a board it configures — can build one here rather
2073/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2074#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2075#[serde(default, deny_unknown_fields)]
2076pub struct GitHubProjectsConfig {
2077    /// Login of the user or organization which owns the board.
2078    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2079    /// The project number shown in the board's GitHub URL.
2080    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2081    // 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.
2082    /// `owner/name` of the repository this source creates an issue in when the item's own
2083    /// `repositories` field does not decide it.
2084    ///
2085    /// An item naming exactly one repository is created there; a task or a document naming
2086    /// none or several is created in its parent project's repository; and a project, or a
2087    /// task or document with no parent, naming none or several is created here. A board
2088    /// has no repository of its own and `createIssue` requires one, so a write without
2089    /// this is refused naming the field. Reads never need it.
2090    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2091    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2092    /// Environment variable containing a fine-grained token with Projects and Issues
2093    /// read/write plus Pull requests read-only access for every repository represented on
2094    /// the board.
2095    #[serde(default = "default_token_env")]
2096    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2097    /// GraphQL endpoint. GitHub Enterprise installations may override it.
2098    #[serde(default = "default_endpoint")]
2099    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2100    /// Per-instance mapping from a status category to the option of the board's one
2101    /// `Status` field it lands on, for a task and for a project.
2102    ///
2103    /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2104    /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2105    /// where a kind left out leaves the category unmapped for that kind. A category this
2106    /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2107    /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2108    /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2109    /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2110    /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2111    /// either kind. No two categories may name one option for the same kind, ignoring case.
2112    /// `unknown` may name one existing option; every unknown word then lands on it and
2113    /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2114    /// each unknown word because it never creates board options.
2115    #[serde(default)]
2116    pub status_mapping: StatusMapping,
2117    /// Per-instance mapping from a task's priority to an option of this board's
2118    /// single-select field named `Priority`.
2119    ///
2120    /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2121    /// other priority is refused before it reaches this board. Present, each of `urgent`,
2122    /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2123    /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2124    /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2125    /// no two levels may name one option. Reads and writes never create the field or an
2126    /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2127    /// the board lacks is refused pointing there.
2128    #[serde(default)]
2129    pub priority_mapping: Option<PriorityMappingConfig>,
2130    /// Caller metadata values this source also writes to board text fields, so the board can
2131    /// filter on them.
2132    ///
2133    /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2134    /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2135    /// value. On every item write — create, update and copy, of a task, a project or a
2136    /// document — a string found there is written to the field, and nothing is sent when the
2137    /// field already holds it; nothing there, or `null`, clears the field when it holds a
2138    /// value; any other JSON type is refused before any mutation. The metadata comment in the
2139    /// issue body stays the one home of the value, and every read reports metadata from it.
2140    /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2141    /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2142    /// non-text field of that name, is refused pointing there.
2143    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2144    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2145    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2146    #[schemars(!skip_serializing_if)]
2147    pub metadata_fields: Vec<MetadataFieldConfig>,
2148    /// How fast this source writes, and how long it waits out a rate-limit refusal.
2149    ///
2150    /// Every field keeps its shipped default when it is absent, and the defaults are
2151    /// GitHub's own published limits rather than taste. See [`Pacing`].
2152    #[serde(default)]
2153    pub pacing: PacingConfig,
2154}
2155
2156/// Which option of the board's `Priority` field each priority lands on.
2157///
2158/// One member per level rather than a map, so a key that is not a level is refused where
2159/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2160/// value in the field, not an option of it.
2161#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2162#[serde(default, deny_unknown_fields)]
2163pub struct PriorityMappingConfig {
2164    /// The option `urgent` lands on; `Urgent` when absent.
2165    pub urgent: Option<PriorityOptionName>,
2166    /// The option `high` lands on; `High` when absent.
2167    pub high: Option<PriorityOptionName>,
2168    /// The option `medium` lands on; `Medium` when absent.
2169    pub medium: Option<PriorityOptionName>,
2170    /// The option `low` lands on; `Low` when absent.
2171    pub low: Option<PriorityOptionName>,
2172}
2173
2174/// The name of an option of the board's `Priority` single-select field.
2175///
2176/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2177/// blank name.
2178#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2179#[serde(try_from = "String")]
2180#[schemars(extend("minLength" = 1))]
2181pub struct PriorityOptionName(String);
2182
2183impl PriorityOptionName {
2184    /// The option name, as the board spells it.
2185    fn as_str(&self) -> &str {
2186        &self.0
2187    }
2188}
2189
2190impl TryFrom<String> for PriorityOptionName {
2191    type Error = String;
2192
2193    fn try_from(name: String) -> Result<Self, Self::Error> {
2194        if name.trim().is_empty() {
2195            return Err("a priority_mapping option name cannot be blank".to_owned());
2196        }
2197        Ok(Self(name))
2198    }
2199}
2200
2201/// The name of the board field a priority is held in.
2202pub const PRIORITY_FIELD: &str = "Priority";
2203
2204/// The four priorities a board option can hold, in the order a new `Priority` field lists
2205/// them. `none` is not among them: it is the field holding no value.
2206///
2207/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2208/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2209/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2210/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2211pub const PRIORITY_LEVELS: [Priority; 4] = [
2212    Priority::Urgent,
2213    Priority::High,
2214    Priority::Medium,
2215    Priority::Low,
2216];
2217
2218/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2219/// see that list for what this pins.
2220#[must_use]
2221pub const fn level_position(priority: Priority) -> Option<usize> {
2222    match priority {
2223        Priority::None => None,
2224        Priority::Urgent => Some(0),
2225        Priority::High => Some(1),
2226        Priority::Medium => Some(2),
2227        Priority::Low => Some(3),
2228    }
2229}
2230
2231/// This instance's complete priority-to-option mapping, read in both directions.
2232///
2233/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2234/// two levels name one option.
2235#[derive(Debug, Clone)]
2236struct PriorityMapping {
2237    options: [PriorityOptionName; 4],
2238}
2239
2240impl PriorityMapping {
2241    fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2242        let shipped = |name: &str| PriorityOptionName(name.to_owned());
2243        let mapping = Self {
2244            options: [
2245                config.urgent.unwrap_or_else(|| shipped("Urgent")),
2246                config.high.unwrap_or_else(|| shipped("High")),
2247                config.medium.unwrap_or_else(|| shipped("Medium")),
2248                config.low.unwrap_or_else(|| shipped("Low")),
2249            ],
2250        };
2251        for (index, option) in mapping.options.iter().enumerate() {
2252            if let Some(earlier) = mapping.options[..index]
2253                .iter()
2254                .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2255            {
2256                return Err(SourceError::Config {
2257                    message: format!(
2258                        "priority_mapping of source {instance} sends both {} and {} to the board \
2259                         option {:?}; one option cannot read back as two priorities",
2260                        PRIORITY_LEVELS[earlier],
2261                        PRIORITY_LEVELS[index],
2262                        option.as_str()
2263                    ),
2264                });
2265            }
2266        }
2267        Ok(mapping)
2268    }
2269
2270    /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2271    fn option(&self, priority: Priority) -> Option<&str> {
2272        level_position(priority).map(|index| self.options[index].as_str())
2273    }
2274
2275    /// The priority a board option name reports, or `None` when nothing maps to it.
2276    fn priority_of(&self, option: &str) -> Option<Priority> {
2277        self.options
2278            .iter()
2279            .position(|name| name.as_str().eq_ignore_ascii_case(option))
2280            .map(|index| PRIORITY_LEVELS[index])
2281    }
2282
2283    /// Every mapped option name, in the order a new `Priority` field lists them.
2284    fn names(&self) -> impl Iterator<Item = &str> {
2285        self.options.iter().map(PriorityOptionName::as_str)
2286    }
2287}
2288
2289/// One metadata value this source projects onto a board text field.
2290#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2291#[serde(deny_unknown_fields)]
2292pub struct MetadataFieldConfig {
2293    /// The name of the board text field the value is written to.
2294    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.
2295    /// The top-level metadata key the value is read from. A key under the reserved
2296    /// `onetaskgraph.` prefix is refused.
2297    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.
2298    /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2299    /// key's own value is the one projected. Left out of a serialized entry when empty.
2300    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2301    // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
2302    #[schemars(!skip_serializing_if)]
2303    pub path: Vec<String>,
2304}
2305
2306/// The board fields no metadata value may be projected onto, compared ignoring case: the
2307/// three this source writes itself, and those GitHub owns on every board.
2308// 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.
2309const UNPROJECTABLE_FIELDS: [&str; 13] = [
2310    STATUS_FIELD,
2311    PRIORITY_FIELD,
2312    ORIGIN_FIELD,
2313    "Title",
2314    "Assignees",
2315    "Labels",
2316    "Linked pull requests",
2317    "Milestone",
2318    "Repository",
2319    "Reviewers",
2320    "Parent issue",
2321    "Sub-issues progress",
2322    "Type",
2323];
2324
2325/// One validated [`MetadataFieldConfig`] entry.
2326#[derive(Debug, Clone)]
2327struct MetadataField {
2328    field: ProjectedField,
2329    key: ProjectedKey,
2330    path: Vec<String>,
2331}
2332
2333/// The name of a board text field a metadata value is projected onto: never blank, never one
2334/// this source or GitHub writes, and never one another entry of the same source names.
2335#[derive(Debug, Clone, PartialEq, Eq)]
2336struct ProjectedField(String);
2337
2338/// The top-level metadata key a projected value is read from: never blank, and never under
2339/// the namespace this product reserves.
2340#[derive(Debug, Clone, PartialEq, Eq)]
2341struct ProjectedKey(String);
2342
2343impl std::ops::Deref for ProjectedField {
2344    type Target = str;
2345    fn deref(&self) -> &str {
2346        &self.0
2347    }
2348}
2349
2350impl std::ops::Deref for ProjectedKey {
2351    type Target = str;
2352    fn deref(&self) -> &str {
2353        &self.0
2354    }
2355}
2356
2357/// What one item's metadata says one projected field should hold.
2358#[derive(Debug, Clone, PartialEq, Eq)]
2359enum Projected {
2360    /// A string to hold.
2361    Text(String),
2362    /// Nothing: the key or a step of the path is absent, or the value is `null`.
2363    Nothing,
2364}
2365
2366impl MetadataField {
2367    /// Validate every entry of one instance's `metadata_fields`, refusing the first that
2368    /// cannot stand, naming the source and the entry.
2369    fn resolve(
2370        configured: Vec<MetadataFieldConfig>,
2371        instance: &SourceName,
2372    ) -> Result<Vec<Self>, SourceError> {
2373        let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
2374        for (index, entry) in configured.into_iter().enumerate() {
2375            let refuse = |why: String| SourceError::Config {
2376                message: format!(
2377                    "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
2378                    entry.field, entry.key
2379                ),
2380            };
2381            if entry.field.trim().is_empty() {
2382                return Err(refuse("names a blank field".to_owned()));
2383            }
2384            if entry.key.trim().is_empty() {
2385                return Err(refuse("names a blank key".to_owned()));
2386            }
2387            if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
2388                return Err(refuse(format!(
2389                    "names a key under the reserved \"{}.\" prefix, which this product keeps \
2390                     for its own keys; next: project a caller key instead",
2391                    MetadataKey::RESERVED_NAMESPACE
2392                )));
2393            }
2394            if let Some(owned) = UNPROJECTABLE_FIELDS
2395                .iter()
2396                .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
2397            {
2398                return Err(refuse(format!(
2399                    "names the board field {owned:?}, which this source or GitHub already \
2400                     writes; next: name a text field of your own"
2401                )));
2402            }
2403            if let Some(earlier) = resolved
2404                .iter()
2405                .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
2406            {
2407                return Err(refuse(format!(
2408                    "names the same board field as metadata_fields[{earlier}]; one field \
2409                     cannot hold two values"
2410                )));
2411            }
2412            resolved.push(Self {
2413                field: ProjectedField(entry.field),
2414                key: ProjectedKey(entry.key),
2415                path: entry.path,
2416            });
2417        }
2418        Ok(resolved)
2419    }
2420
2421    /// The path as a refusal spells it: `[]` for the key's own value.
2422    fn spelled_path(&self) -> String {
2423        serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
2424    }
2425
2426    /// What `metadata` says this field should hold, or the refusal for a value no text field
2427    /// can hold.
2428    fn projected(
2429        &self,
2430        metadata: &BTreeMap<String, Value>,
2431        instance: &SourceName,
2432    ) -> Result<Projected, SourceError> {
2433        let mut value = metadata.get(&*self.key);
2434        for step in &self.path {
2435            value = value.and_then(|held| held.get(step.as_str()));
2436        }
2437        let found = match value {
2438            None | Some(Value::Null) => return Ok(Projected::Nothing),
2439            Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
2440            Some(Value::Bool(_)) => "a boolean",
2441            Some(Value::Number(_)) => "a number",
2442            Some(Value::Array(_)) => "an array",
2443            Some(Value::Object(_)) => "an object",
2444        };
2445        Err(SourceError::Refused {
2446            message: format!(
2447                "source {instance} projects metadata key {:?} at path {} onto its board text \
2448                 field {:?}, and this item holds {found} there; only a string, null or nothing \
2449                 can be written to a text field; next: store a string there, or remove the \
2450                 value",
2451                &*self.key,
2452                self.spelled_path(),
2453                &*self.field
2454            ),
2455        })
2456    }
2457}
2458
2459/// What one item's `Priority` field says, read through this instance's mapping.
2460#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
2461enum HeldPriority {
2462    /// A priority this source reports: an option the mapping names, or no value (`none`).
2463    Read(Priority),
2464    /// An option the mapping does not name, which is never read as a level or as `none`.
2465    Unmapped(String),
2466}
2467
2468/// How fast this source writes, and how long it waits out a rate-limit refusal.
2469///
2470/// Configurable because a GitHub Enterprise installation sets its own limits and an
2471/// operator who has already been refused may want to go slower still — not because the
2472/// defaults are guesses.
2473#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2474#[serde(default, deny_unknown_fields)]
2475pub struct PacingConfig {
2476    /// Shortest interval between two content-creating mutations, in milliseconds.
2477    ///
2478    /// Zero sends them as fast as they are asked for, which is what a fixture server on
2479    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
2480    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.
2481    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
2482    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
2483    /// zero while there is a budget to spend, because a schedule of zero-length waits
2484    /// consumes none of it and so never ends.
2485    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.
2486    /// Total time one call may spend waiting out rate limits, in milliseconds.
2487    ///
2488    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
2489    /// the bound is what makes this a wait rather than a hang.
2490    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.
2491}
2492
2493/// The largest any pacing setting may be, in milliseconds.
2494///
2495/// One hour. GitHub's own harshest published bound on content-generating requests works
2496/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
2497/// anything a real limit asks for, and past it the settings stop describing pacing at all:
2498/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
2499/// and an interval beyond it is a command that never sends its second mutation. It also
2500/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
2501/// what a `Duration` can hold on every platform.
2502pub const MAX_PACING_MS: u64 = 3_600_000;
2503
2504/// [`PacingConfig`] with every default resolved and every value checked, which is what the
2505/// source holds.
2506#[derive(Debug, Clone, Copy)]
2507struct Pacing {
2508    min_mutation_interval: Duration,
2509    retry_backoff: Duration,
2510    retry_budget: Duration,
2511}
2512
2513impl Pacing {
2514    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
2515    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2516        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
2517            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
2518                message: format!(
2519                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
2520                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
2521                     GitHub's own harshest published limit"
2522                ),
2523            }),
2524            Some(value) => Ok(Duration::from_millis(value)),
2525            None => Ok(Duration::from_millis(default)),
2526        };
2527        let retry_backoff = bounded(
2528            config.retry_backoff_ms,
2529            RETRY_BACKOFF_MS,
2530            "retry_backoff_ms",
2531        )?;
2532        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
2533        if retry_backoff.is_zero() && !retry_budget.is_zero() {
2534            return Err(SourceError::Config {
2535                message: format!(
2536                    "pacing.retry_backoff_ms of source {instance} is 0 while \
2537                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
2538                     none of that budget, so it would retry a refusal forever. Set a backoff of \
2539                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
2540                     waiting at all",
2541                    retry_budget.as_millis()
2542                ),
2543            });
2544        }
2545        Ok(Self {
2546            min_mutation_interval: bounded(
2547                config.min_mutation_interval_ms,
2548                MIN_MUTATION_INTERVAL_MS,
2549                "min_mutation_interval_ms",
2550            )?,
2551            retry_backoff,
2552            retry_budget,
2553        })
2554    }
2555}
2556
2557/// Factory for [`GitHubProjectsSource`].
2558#[derive(Debug, Clone, Copy, Default)]
2559pub struct Plugin;
2560
2561impl SourcePlugin for Plugin {
2562    fn kind(&self) -> &'static str {
2563        KIND
2564    }
2565    /// The endpoint, the owner and the board's number: what names one board.
2566    fn remote_identity(&self, config: &Value) -> Option<String> {
2567        let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
2568        Some(format!(
2569            "{}\n{}\n{}",
2570            config.endpoint, config.owner, config.project_number
2571        ))
2572    }
2573    fn config_schema(&self) -> Schema {
2574        schema_for!(GitHubProjectsConfig)
2575    }
2576    fn build(
2577        &self,
2578        name: &SourceName,
2579        config: &Value,
2580        secrets: &dyn SecretResolver,
2581    ) -> Result<Box<dyn TaskSource>, SourceError> {
2582        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
2583    }
2584
2585    fn build_with_clock(
2586        &self,
2587        name: &SourceName,
2588        config: &Value,
2589        secrets: &dyn SecretResolver,
2590        clock: SharedClock,
2591    ) -> Result<Box<dyn TaskSource>, SourceError> {
2592        self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
2593    }
2594}
2595
2596impl Plugin {
2597    /// Build a source recording every request it sends into an accounting the caller holds.
2598    ///
2599    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
2600    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
2601    /// session total rather than two — see [`accounting`] and
2602    /// [`GitHubProjectsSource::recording_into`].
2603    ///
2604    /// # Errors
2605    ///
2606    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
2607    /// [`SourceError::Config`] for configuration this plugin cannot use and
2608    /// [`SourceError::Auth`] for a credential it cannot find.
2609    pub fn build_recording_into(
2610        &self,
2611        name: &SourceName,
2612        config: &Value,
2613        secrets: &dyn SecretResolver,
2614        ledger: Arc<Accounting>,
2615    ) -> Result<Box<dyn TaskSource>, SourceError> {
2616        self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
2617    }
2618
2619    fn build_recording_with_clock(
2620        &self,
2621        name: &SourceName,
2622        config: &Value,
2623        secrets: &dyn SecretResolver,
2624        ledger: Arc<Accounting>,
2625        clock: SharedClock,
2626    ) -> Result<Box<dyn TaskSource>, SourceError> {
2627        let config: GitHubProjectsConfig =
2628            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
2629                message: format!("source {name}: {e}"),
2630            })?;
2631        let prefix = format!("source {name}: ");
2632        let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
2633            .map_err(|error| match error {
2634                // The shared `StatusMapping::distinct` names the source itself.
2635                SourceError::Config { message } if message.starts_with(&prefix) => {
2636                    SourceError::Config { message }
2637                }
2638                SourceError::Config { message } => SourceError::Config {
2639                    message: format!("{prefix}{message}"),
2640                },
2641                SourceError::Auth { message } => SourceError::Auth {
2642                    message: format!("source {name}: {message}"),
2643                },
2644                other => other,
2645            })?;
2646        source.clock = clock;
2647        Ok(Box::new(source))
2648    }
2649}
2650
2651/// Where a status category lands on this board, once configuration is resolved.
2652#[derive(Debug, Clone, PartialEq, Eq)]
2653enum StatusTarget {
2654    /// Not usable against this instance for this kind, and why.
2655    Disabled(UnmappedStatus),
2656    /// The board's `Status` option of this name.
2657    Column(ColumnName),
2658    /// A closed issue, with both its board option and the reason that says which closed it means.
2659    // 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.
2660    Terminal(ColumnName, ClosedState),
2661}
2662
2663/// Every status category, in the order the vocabulary declares them.
2664///
2665/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
2666/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
2667/// added to the shared vocabulary fails to compile until it is named there, and this
2668/// crate's suite reconciles this list against that enum's own derived schema, which is
2669/// generated from the variants rather than written beside them. The schema is what
2670/// catches a list left one short — a list checking only the positions it already holds
2671/// would pass while every mapping indexed by the new position panicked.
2672pub const CATEGORIES: [StatusCategory; 8] = [
2673    StatusCategory::Draft,
2674    StatusCategory::Backlog,
2675    StatusCategory::Todo,
2676    StatusCategory::Queued,
2677    StatusCategory::InProgress,
2678    StatusCategory::Done,
2679    StatusCategory::Cancelled,
2680    StatusCategory::Unknown,
2681];
2682
2683/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
2684#[must_use]
2685pub const fn category_position(category: StatusCategory) -> usize {
2686    match category {
2687        StatusCategory::Draft => 0,
2688        StatusCategory::Backlog => 1,
2689        StatusCategory::Todo => 2,
2690        StatusCategory::Queued => 3,
2691        StatusCategory::InProgress => 4,
2692        StatusCategory::Done => 5,
2693        StatusCategory::Cancelled => 6,
2694        StatusCategory::Unknown => 7,
2695    }
2696}
2697
2698/// The spelling a status category is configured and reported under.
2699fn category_name(category: StatusCategory) -> &'static str {
2700    match category {
2701        StatusCategory::Draft => "draft",
2702        StatusCategory::Backlog => "backlog",
2703        StatusCategory::Todo => "todo",
2704        StatusCategory::Queued => "queued",
2705        StatusCategory::InProgress => "in-progress",
2706        StatusCategory::Done => "done",
2707        StatusCategory::Cancelled => "cancelled",
2708        StatusCategory::Unknown => "unknown",
2709    }
2710}
2711
2712/// A shipped default's option name.
2713///
2714/// The literals below are this file's own and non-blank, and they are validated by the
2715/// one constructor a configured name goes through rather than beside it.
2716fn shipped_column(name: &'static str) -> ColumnName {
2717    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
2718}
2719
2720/// The shipped default for one category this instance's `status_mapping` does not mention,
2721/// for either kind.
2722fn shipped_default(category: StatusCategory) -> StatusTarget {
2723    match category {
2724        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
2725        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
2726        StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
2727        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
2728        StatusCategory::Done => {
2729            StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
2730        }
2731        StatusCategory::Cancelled => {
2732            StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
2733        }
2734        StatusCategory::Draft | StatusCategory::Unknown => {
2735            StatusTarget::Disabled(UnmappedStatus::Unconfigured)
2736        }
2737    }
2738}
2739
2740/// The two kinds a status is written and read for, each with its own half of the mapping.
2741const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
2742
2743/// This instance's complete category-to-target mapping for each kind, read in both
2744/// directions.
2745///
2746/// One target per category per kind, held at that category's own [`category_position`], so
2747/// a category missing from the mapping, named twice in it, or filed out of order is a state
2748/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
2749/// targets are options of the board's one `Status` field.
2750#[derive(Debug, Clone)]
2751struct BoardStatuses {
2752    tasks: [StatusTarget; CATEGORIES.len()],
2753    projects: [StatusTarget; CATEGORIES.len()],
2754}
2755
2756impl BoardStatuses {
2757    /// Resolve `configured` against the shipped defaults, refusing two categories one kind
2758    /// would read back from one option.
2759    ///
2760    /// A category the mapping does not mention keeps its shipped default for both kinds; one
2761    /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
2762    /// omits unmapped rather than defaulted.
2763    fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
2764        let resolve_kind =
2765            |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
2766                // `CATEGORIES[position] == category` for every category — the crate's suite
2767                // asserts it — so mapping the list in order fills each category's own slot.
2768                let mut targets = CATEGORIES.map(shipped_default);
2769                for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
2770                    if !configured.mentions(category) {
2771                        continue;
2772                    }
2773                    *slot = match configured.name_for(category, kind) {
2774                        Err(why) => StatusTarget::Disabled(why),
2775                        Ok(name) => {
2776                            let option = ColumnName::try_from(name.as_str().to_owned())
2777                                .map_err(|message| SourceError::Config { message })?;
2778                            match category {
2779                                StatusCategory::Done => {
2780                                    StatusTarget::Terminal(option, ClosedState::Completed)
2781                                }
2782                                StatusCategory::Cancelled => {
2783                                    StatusTarget::Terminal(option, ClosedState::NotPlanned)
2784                                }
2785                                _ => StatusTarget::Column(option),
2786                            }
2787                        }
2788                    };
2789                }
2790                StatusMapping::distinct(
2791                    instance,
2792                    kind,
2793                    CATEGORIES
2794                        .iter()
2795                        .zip(&targets)
2796                        .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
2797                )?;
2798                Ok(targets)
2799            };
2800        Ok(Self {
2801            tasks: resolve_kind(ItemKind::Task)?,
2802            projects: resolve_kind(ItemKind::Project)?,
2803        })
2804    }
2805
2806    /// Every category's target for `kind`, in category order.
2807    const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
2808        match kind {
2809            ItemKind::Task => &self.tasks,
2810            ItemKind::Project => &self.projects,
2811        }
2812    }
2813
2814    fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
2815        &self.targets(kind)[category_position(category)]
2816    }
2817
2818    /// The category a board option name reports for `kind`, or `None` when nothing of that
2819    /// kind maps to it.
2820    fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
2821        CATEGORIES.into_iter().find(|category| {
2822            self.target(kind, *category)
2823                .option()
2824                .is_some_and(|name| name.eq_ignore_ascii_case(option))
2825        })
2826    }
2827
2828    /// Every option name either kind maps a category to, each once ignoring case, in
2829    /// category order with a task's name before a project's — what the guarded setup asks
2830    /// the `Status` field to hold.
2831    fn wanted(&self) -> Vec<String> {
2832        let mut wanted: Vec<String> = Vec::new();
2833        for category in CATEGORIES {
2834            for kind in STATUS_KINDS {
2835                if let Some(name) = self.target(kind, category).option()
2836                    && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
2837                {
2838                    wanted.push(name.to_owned());
2839                }
2840            }
2841        }
2842        wanted
2843    }
2844
2845    /// The status an item of `kind` reports, from the three things a read of it says: its
2846    /// board `Status` option, whether its issue is closed, and the reason it was closed with.
2847    ///
2848    /// The closed state decides the category and the `Status` option decides the name, so
2849    /// a closed issue sitting in a "Shipped" column reports `done` named `Shipped`, whatever
2850    /// its kind. A closed issue whose reason is `DUPLICATE` or `REOPENED` reports `Unknown`:
2851    /// a duplicate is not finished work, and calling it done is a lie the next copy would
2852    /// write back. `REOPENED`-while-closed is a state this source can never produce, so
2853    /// it is read permissively rather than refused — reads are faithful, and refusals
2854    /// belong on writes. An open item's option reads through its own kind's mapping, and an
2855    /// option that mapping does not name reads as `Unknown` under its own name.
2856    ///
2857    /// One function of those three rather than of a response, so a narrow status write can
2858    /// answer what a re-read would report by applying it to the state it has just written.
2859    fn status(
2860        &self,
2861        kind: ItemKind,
2862        option: Option<&str>,
2863        closed: bool,
2864        reason: Option<&str>,
2865    ) -> Status {
2866        if closed {
2867            let category = match reason {
2868                None | Some("COMPLETED") => StatusCategory::Done,
2869                Some("NOT_PLANNED") => StatusCategory::Cancelled,
2870                Some(_) => StatusCategory::Unknown,
2871            };
2872            let fallback = match category {
2873                StatusCategory::Done => "Done",
2874                StatusCategory::Cancelled => "Cancelled",
2875                _ => "Closed",
2876            };
2877            return Status {
2878                category,
2879                name: option.unwrap_or(fallback).to_owned(),
2880            };
2881        }
2882        let name = option.unwrap_or("Open").to_owned();
2883        Status {
2884            category: self
2885                .category_of(kind, &name)
2886                .unwrap_or(StatusCategory::Unknown),
2887            name,
2888        }
2889    }
2890}
2891
2892impl BoardStatuses {
2893    /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
2894    /// case; a kind lacking none is left out.
2895    fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
2896        STATUS_KINDS
2897            .into_iter()
2898            .filter_map(|kind| {
2899                let missing: Vec<String> = self
2900                    .targets(kind)
2901                    .iter()
2902                    .filter_map(StatusTarget::option)
2903                    .filter(|wanted| {
2904                        !existing
2905                            .iter()
2906                            .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2907                    })
2908                    .map(str::to_owned)
2909                    .collect();
2910                (!missing.is_empty()).then_some(KindMissing { kind, missing })
2911            })
2912            .collect()
2913    }
2914}
2915
2916impl StatusTarget {
2917    /// The board option this target selects, or `None` for an unmapped one.
2918    fn option(&self) -> Option<&str> {
2919        match self {
2920            Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
2921            Self::Disabled(_) => None,
2922        }
2923    }
2924}
2925
2926// 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.
2927/// One repository this source can create an issue in, as `owner/name`.
2928///
2929/// Every `createIssue` this source sends names one of these: the item's own single
2930/// `repositories` entry, else its parent project issue's repository, else the configured
2931/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
2932/// that choice and says what it refuses before `createIssue`.
2933// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
2934#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
2935struct RepositoryTarget {
2936    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
2937    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
2938}
2939
2940impl RepositoryTarget {
2941    fn parse(value: &str) -> Result<Self, SourceError> {
2942        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
2943            message: format!(
2944                "repository must be spelled owner/name; {value:?} names no repository"
2945            ),
2946        })?;
2947        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
2948            return Err(SourceError::Config {
2949                message: format!(
2950                    "repository must be spelled owner/name with a GitHub login and one \
2951                     repository name; {value:?} is not"
2952                ),
2953            });
2954        }
2955        Ok(Self {
2956            owner: owner.to_owned(),
2957            name: name.to_owned(),
2958        })
2959    }
2960
2961    /// The one host whose repositories this source creates issues in, spelled once: it is
2962    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
2963    const HOST: &str = "github.com";
2964
2965    fn origin(&self) -> String {
2966        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
2967    }
2968
2969    /// The repository a normalized origin names, or why it is none this source can create
2970    /// an issue in: another host, or more or fewer than `owner/name` under this one.
2971    fn from_origin(origin: &Repository) -> Result<Self, String> {
2972        let not_here = || {
2973            format!(
2974                "{} is not a {}/owner/name repository",
2975                origin.as_str(),
2976                Self::HOST
2977            )
2978        };
2979        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
2980        if host != Self::HOST {
2981            return Err(not_here());
2982        }
2983        Self::parse(rest).map_err(|_| not_here())
2984    }
2985
2986    fn slug(&self) -> String {
2987        format!("{}/{}", self.owner, self.name)
2988    }
2989}
2990
2991/// A source which reads GitHub afresh for every operation.
2992pub struct GitHubProjectsSource {
2993    /// This source's configured name, used both to tell a far end naming this source
2994    /// from one naming a system it knows nothing about, and to name the instance a
2995    /// status refusal is about.
2996    name: SourceName,
2997    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
2998    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
2999    repository: Option<RepositoryTarget>,
3000    endpoint: Url,
3001    token: SecretString,
3002    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3003    statuses: BoardStatuses,
3004    /// Where each priority lands on this board, or `None` when this instance holds none.
3005    priorities: Option<PriorityMapping>,
3006    /// The metadata values this instance projects onto board text fields, in configured order.
3007    metadata_fields: Vec<MetadataField>,
3008    client: Client,
3009    asset_client: Client,
3010    /// Every item this source has created in this command, in the order it created them —
3011    /// dropped by [`TaskSource::end_command`].
3012    ///
3013    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3014    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3015    /// a copy resolving a dependency on an item it had just created refused it as not
3016    /// found. A board read is completed from this — an item remembered here and absent from
3017    /// the read is added back, because the board really does hold it and only the read is
3018    /// behind.
3019    ///
3020    /// It is not a cache of a user's work: nothing is remembered that this process did not
3021    /// itself just write, it lives and dies with the process, and it is never consulted for
3022    /// an item this source did not create.
3023    created: Mutex<Vec<Resolved>>,
3024    /// Every item that already existed and that this source has written in this command, as
3025    /// it wrote it — dropped by [`TaskSource::end_command`].
3026    ///
3027    /// The other half of [`Self::created`], held on the same terms and for the reason a
3028    /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3029    /// filter is an index behind a write this process made moments ago, so a query matching
3030    /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3031    /// is remembered that this process did not itself just write.
3032    updated: Mutex<Vec<Resolved>>,
3033    /// Every issue this source has added a comment to or edited a comment of in this command
3034    /// — dropped by [`TaskSource::end_command`].
3035    ///
3036    /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3037    /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3038    /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3039    /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3040    /// from before — until the index caught up. Each id here is a candidate of every such
3041    /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3042    /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3043    /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3044    /// process wrote is still found only once the index has it.
3045    commented: Mutex<Vec<NativeId>>,
3046    /// How fast this source writes, and how long it waits out a refusal.
3047    pacing: Pacing,
3048    /// When the last content-creating mutation finished, or the moment the furthest-out
3049    /// reserved slot releases the next one, whichever is later — so the one after it can be
3050    /// spaced from that. See [`MIN_MUTATION_INTERVAL_MS`] for the interval and
3051    /// [`GitHubProjectsSource::finish_mutation`] for why completion rather than release is
3052    /// what it is measured from.
3053    last_mutation: Mutex<Option<Duration>>,
3054    clock: SharedClock,
3055    numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3056    /// The board as this process last read it, for the length of one command — dropped by
3057    /// [`TaskSource::end_command`].
3058    ///
3059    /// A copy of a project used to re-read the whole board, paged, before writing each of
3060    /// its items, which is by far the largest part of a copy's request count and none of
3061    /// its work. Nothing else changes this board while a command runs — this source's own
3062    /// writes are the only writer — so one read answers them all.
3063    ///
3064    /// It is not a store of a user's work and it is not the cache the no-persistence
3065    /// invariant forbids: it lives and dies with the process exactly as `created` does,
3066    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3067    /// an item this command created and then depends on resolves whether or not GitHub's
3068    /// own eventually-consistent read has caught up. A write to an item already on the
3069    /// board updates the entry here too, so what this holds is the last read plus this
3070    /// process's own writes rather than a snapshot taken before them.
3071    board_cache: Mutex<Option<Board>>,
3072    /// Every issue this board's own search reported, for the length of one command — dropped
3073    /// by [`TaskSource::end_command`].
3074    ///
3075    /// The second half of a board read, and cached for the same reason and on the same
3076    /// terms as the first: it lives and dies with the process, nothing is written down, and
3077    /// a write this process makes updates the entry here exactly as it updates the one in
3078    /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3079    /// that lists this board's projects and its tasks pays for one search rather than two.
3080    search_cache: Mutex<Option<Vec<Resolved>>>,
3081    /// What each narrowed question GitHub was asked answered, keyed by that question, for
3082    /// the length of one command — dropped by [`TaskSource::end_command`].
3083    ///
3084    /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3085    /// and dies with the process, nothing is written down, a write this process makes
3086    /// updates the entry here as it updates the other two, and every answer is completed
3087    /// with this process's own writes each time it is given. A command that asks the same
3088    /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3089    /// write — pays for it once, which is what the whole-board read it replaced gave it.
3090    narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3091    search_next: Mutex<BTreeMap<String, Option<String>>>,
3092    /// Records already resolved in this command, reused by writes and for comment identity.
3093    /// Explicit item reads still reach GitHub. Nothing is persisted, and
3094    /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3095    /// its item as a person has since left it.
3096    resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3097    /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3098    /// asked for under, with the project that selector named — for the length of one command,
3099    /// dropped by [`TaskSource::end_command`].
3100    ///
3101    /// A caller pages through a project's tasks one engine page at a time, and every page
3102    /// is cut from the whole list of them, so without this each page walked every
3103    /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3104    /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3105    /// dies with the process, nothing is written down, a write this process makes updates or
3106    /// removes the entry here as it does there, and every answer is completed with this
3107    /// process's own writes each time it is given.
3108    children_cache: Mutex<ProjectChildren>,
3109    /// The board's own id and field definitions as this process last read them on their
3110    /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3111    ///
3112    /// What a write needs of the board and its item does not say, read once per command
3113    /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3114    /// lives and dies with the process and nothing is written down. It holds no item and so
3115    /// can answer no question about one — see [`Self::board_fields`].
3116    fields_cache: Mutex<Option<BoardFields>>,
3117    /// Each destination repository's node id, resolved once per repository
3118    /// rather than per issue created.
3119    ///
3120    /// A repository's node id does not change, and re-reading it for every issue of a copy
3121    /// spent one request per item on an answer this source already had. It is a map rather
3122    /// than one entry because a copy files each item in the repository its own
3123    /// `repositories` field names, so a plan across five repositories asks GitHub five
3124    /// times and not once per item.
3125    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3126    /// What every request this source sends is recorded into.
3127    ///
3128    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3129    /// a request leaves this crate, so nothing has to be switched on for a session to be
3130    /// counted. It is shared rather than owned so a caller accounting for a whole session —
3131    /// its own schema verification, board lookups, residue sweep and cleanup beside this
3132    /// source's reads and writes — adds up one accounting instead of two. See
3133    /// [`accounting`] for what a record carries and what a session's spend is and is not.
3134    ledger: Arc<Accounting>,
3135    /// What [`expected`] holds for a write the store's cache expects: the journal while one is
3136    /// open, the issues primed from the cache, and the comments known on each.
3137    expected: expected::Held,
3138}
3139
3140/// GitHub's closed single-select color vocabulary.
3141#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3142#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
3143pub enum StatusOptionColor {
3144    /// Gray.
3145    Gray,
3146    /// Blue.
3147    Blue,
3148    /// Green.
3149    Green,
3150    /// Yellow.
3151    Yellow,
3152    /// Purple.
3153    Purple,
3154    /// Red.
3155    Red,
3156    /// Orange.
3157    Orange,
3158    /// Pink.
3159    Pink,
3160}
3161
3162/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
3163/// applies its additions.
3164#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3165pub enum SetupMode {
3166    /// Read without mutation.
3167    Plan,
3168    /// Apply and verify.
3169    Apply,
3170}
3171
3172/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
3173/// against it goes on compiling.
3174pub type StatusOptionsMode = SetupMode;
3175
3176/// The explicit result of the requested operation.
3177#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3178#[serde(rename_all = "kebab-case")]
3179pub enum StatusOptionsOutcome {
3180    /// A read-only plan.
3181    Planned,
3182    /// Apply found nothing missing.
3183    Unchanged,
3184    /// Additions were applied and verified.
3185    Applied,
3186}
3187
3188/// A GitHub single-select option's opaque GraphQL node identifier.
3189#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3190#[serde(transparent)]
3191pub struct StatusOptionId(#[schemars(length(min = 1))] String);
3192
3193impl TryFrom<String> for StatusOptionId {
3194    type Error = String;
3195
3196    fn try_from(id: String) -> Result<Self, Self::Error> {
3197        if id.trim().is_empty() {
3198            return Err("a GitHub Status option id cannot be blank".to_owned());
3199        }
3200        Ok(Self(id))
3201    }
3202}
3203
3204/// One existing or proposed option in a guarded Status-field update.
3205#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3206pub struct StatusOption {
3207    /// GitHub's stable id.
3208    pub id: StatusOptionId,
3209    /// The visible option name.
3210    pub name: ColumnName,
3211    /// GitHub's single-select color token.
3212    pub color: StatusOptionColor,
3213    /// The option description, including an empty one.
3214    pub description: String,
3215}
3216
3217/// One board item's Status assignment, retained as recovery data.
3218#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3219pub struct StatusAssignment {
3220    /// The project item id whose assignment this is.
3221    // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
3222    // carried verbatim as operator recovery data; introducing a semantic type would claim
3223    // validation rules GitHub does not publish and no operation here interprets.
3224    pub item_id: String,
3225    /// The selected option, absent when the item has no status.
3226    #[serde(skip_serializing_if = "Option::is_none")]
3227    pub option: Option<AssignedStatusOption>,
3228}
3229
3230/// The inseparable id and name of an assigned option.
3231#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3232pub struct AssignedStatusOption {
3233    /// GitHub's stable id.
3234    pub id: StatusOptionId,
3235    /// The visible name.
3236    pub name: ColumnName,
3237}
3238
3239/// The plan and verified outcome of reconciling configured Status options.
3240#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3241pub struct StatusOptionsReport {
3242    /// The configured source name.
3243    pub source: SourceName,
3244    /// Configured option names absent before the operation.
3245    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
3246    // `ColumnName` and has therefore already passed its nonblank validation; retaining the
3247    // serialized string here preserves the report's intentionally simple public contract.
3248    pub missing: Vec<String>,
3249    /// What the requested operation did.
3250    pub outcome: StatusOptionsOutcome,
3251    /// The complete option list observed before any mutation.
3252    pub existing: Vec<StatusOption>,
3253}
3254
3255#[derive(Debug, Clone, PartialEq, Eq)]
3256struct StatusSnapshot {
3257    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3258    // passed back as the mutation's project identity; a newtype could enforce no stronger
3259    // invariant because GitHub publishes no grammar for it.
3260    board_id: String,
3261    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3262    // passed back as the mutation's field identity; a newtype could enforce no stronger
3263    // invariant because GitHub publishes no grammar for it.
3264    field_id: String,
3265    options: Vec<StatusOption>,
3266    assignments: Vec<StatusAssignment>,
3267}
3268
3269/// The name of the board field a status is held in.
3270const STATUS_FIELD: &str = "Status";
3271
3272/// Every item's value of each field `report` names, as it stood before the setup wrote
3273/// anything — what a person puts back when the setup is refused part way.
3274fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
3275    let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
3276        .fields
3277        .iter()
3278        .map(|field| (field.field.name(), before.assignments(field.field)))
3279        .collect();
3280    serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
3281        message: format!("cannot render the pre-write field recovery snapshot: {error}"),
3282    })
3283}
3284
3285/// One board field the guarded setup reads and writes — every one it reads, and the only
3286/// ones it writes.
3287#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
3288pub enum BoardField {
3289    /// The single-select `Status` field every instance's `status_mapping` resolves into.
3290    Status,
3291    /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
3292    Priority,
3293}
3294
3295impl BoardField {
3296    /// The field's name on the board.
3297    #[must_use]
3298    pub const fn name(self) -> &'static str {
3299        match self {
3300            Self::Status => STATUS_FIELD,
3301            Self::Priority => PRIORITY_FIELD,
3302        }
3303    }
3304
3305    /// The field a board calls `name`, or `None` for one this setup does not own.
3306    fn named(name: &str) -> Option<Self> {
3307        [Self::Status, Self::Priority]
3308            .into_iter()
3309            .find(|field| field.name() == name)
3310    }
3311}
3312
3313/// What the guarded setup did to one field.
3314#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3315#[serde(rename_all = "kebab-case")]
3316pub enum FieldOutcome {
3317    /// A read-only plan.
3318    Planned,
3319    /// Apply found the field there with every configured option.
3320    Unchanged,
3321    /// Missing options were added to the field that was there, and verified.
3322    Applied,
3323    /// The field was not there; it was created holding the configured options, and verified.
3324    Created,
3325}
3326
3327/// One field's plan, or its verified outcome.
3328#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3329pub struct FieldReport {
3330    /// Which field.
3331    pub field: BoardField,
3332    /// Whether the board had the field before the operation.
3333    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
3334    // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
3335    // "outcome", "existing"}` — so folding one into the other would change a published JSON
3336    // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
3337    // one constructor, and it derives `outcome` from `exists` in one match.
3338    pub exists: bool,
3339    /// Configured option names the field lacked before the operation — every one of them,
3340    /// in the order a new field lists them, when the field was not there at all.
3341    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3342    // mapping name and has therefore already passed its nonblank validation; the serialized
3343    // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
3344    pub missing: Vec<String>,
3345    /// For the `Status` field, which item kind each missing name is configured for: one
3346    /// entry per kind `status_mapping` names a missing option for, task before project, each
3347    /// listing that kind's missing names in category order. A name both kinds use is in
3348    /// both. Empty — and left out of the JSON — when nothing is missing, and always for
3349    /// `Priority`, which only a task holds.
3350    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3351    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3352    // both SDKs model an absent `kinds` as an empty list rather than as `null`.
3353    #[schemars(!skip_serializing_if)]
3354    pub kinds: Vec<KindMissing>,
3355    /// What the requested operation did.
3356    pub outcome: FieldOutcome,
3357    /// The field's complete option list observed before any mutation; empty when the field
3358    /// was not there.
3359    pub existing: Vec<StatusOption>,
3360}
3361
3362/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
3363#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3364pub struct KindMissing {
3365    /// The kind these names are configured for.
3366    pub kind: ItemKind,
3367    /// The names that kind maps a category to and the field lacked, in category order.
3368    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3369    // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
3370    // intentionally simple public contract.
3371    pub missing: Vec<String>,
3372}
3373
3374/// The plan and verified outcome of setting up every field a source's configuration names.
3375#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3376pub struct FieldsReport {
3377    /// The configured source name.
3378    pub source: SourceName,
3379    /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
3380    // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
3381    // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
3382    // per field would change a published JSON shape. The states the list could hold and the
3383    // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
3384    // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
3385    pub fields: Vec<FieldReport>,
3386    /// Each board text field the source's `metadata_fields` projects a value onto, in
3387    /// configured order. Empty — and left out of the JSON — when it projects none.
3388    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3389    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3390    // both SDKs model an absent list as an empty one rather than as `null`.
3391    #[schemars(!skip_serializing_if)]
3392    pub metadata_fields: Vec<MetadataFieldReport>,
3393}
3394
3395/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
3396/// its GraphQL type when it has none — which is what a projected field's conflict reports.
3397///
3398/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
3399/// no conflict.
3400// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
3401// refuses exactly what `try_from` below does.
3402#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3403#[serde(transparent)]
3404pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
3405
3406/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
3407/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
3408/// neither SDK's validator can be relied on to support.
3409const NON_TEXT_FIELD_TYPE_PATTERN: &str =
3410    r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
3411
3412impl TryFrom<String> for NonTextFieldType {
3413    type Error = String;
3414
3415    fn try_from(kind: String) -> Result<Self, Self::Error> {
3416        if kind.is_empty() || kind.starts_with(char::is_whitespace) {
3417            return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
3418        }
3419        if kind == "TEXT" {
3420            return Err("a TEXT field is no conflict for a projected text field".to_owned());
3421        }
3422        Ok(Self(kind))
3423    }
3424}
3425
3426impl std::fmt::Display for NonTextFieldType {
3427    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3428        formatter.write_str(&self.0)
3429    }
3430}
3431
3432/// One projected metadata text field's plan, or its verified outcome.
3433///
3434/// The setup creates a missing one as a text field and never alters a field that is there:
3435/// a same-named field of another type is reported in `conflict` and left as it is.
3436#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3437pub struct MetadataFieldReport {
3438    /// The board field's name.
3439    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.
3440    /// The metadata key the value is read from.
3441    pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
3442    /// The object keys walked inside that key's value; empty for the key's own value.
3443    pub path: Vec<String>,
3444    /// Whether the board had a text field of that name before the operation.
3445    // 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.
3446    pub exists: bool,
3447    /// The type of a field of that name that is not a text field, which the setup leaves as
3448    /// it is; absent — and left out of the JSON — when there is none.
3449    #[serde(default, skip_serializing_if = "Option::is_none")]
3450    pub conflict: Option<NonTextFieldType>,
3451    /// What the requested operation did: `planned`; `created` for a field that was not
3452    /// there; `unchanged` for one that was, and for a conflict, which is never changed.
3453    pub outcome: FieldOutcome,
3454}
3455
3456/// Which options one field is configured with, in the order a new field would list them.
3457struct FieldPlan {
3458    field: BoardField,
3459    wanted: Vec<String>,
3460}
3461
3462/// One single-select field as the guarded setup snapshots it.
3463#[derive(Debug, Clone, PartialEq, Eq)]
3464struct SnapshotField {
3465    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3466    // passed back as the mutation's field identity; a newtype could enforce no stronger
3467    // invariant because GitHub publishes no grammar for it.
3468    field_id: String,
3469    options: Vec<StatusOption>,
3470}
3471
3472/// Every single-select field of a board and every item's value of each.
3473#[derive(Debug, Clone, PartialEq, Eq)]
3474struct BoardSnapshot {
3475    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3476    // passed back as the mutation's project identity; a newtype could enforce no stronger
3477    // invariant because GitHub publishes no grammar for it.
3478    board_id: String,
3479    fields: BTreeMap<BoardField, SnapshotField>,
3480    /// Each board item's id, and its value of each field this setup owns that it holds one of.
3481    items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
3482}
3483
3484impl BoardSnapshot {
3485    /// Every item's value of `field`, in board order — the recovery data a drift refusal
3486    /// carries.
3487    fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
3488        self.items
3489            .iter()
3490            .map(|(item_id, values)| StatusAssignment {
3491                item_id: item_id.clone(),
3492                option: values.get(&field).cloned(),
3493            })
3494            .collect()
3495    }
3496}
3497
3498impl GitHubProjectsSource {
3499    /// Report missing configured Status options and, when `apply` is true, add them with
3500    /// a whole-list mutation that preserves every existing id and verifies the result.
3501    ///
3502    /// # Errors
3503    ///
3504    /// Refuses a board without a single-select `Status` field. A post-write difference in
3505    /// any pre-existing option id or item assignment is refused with the complete pre-write
3506    /// assignment snapshot in the diagnostic for recovery.
3507    // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
3508    // successful mutation, both drift refusals, source selection, missing Status, casing,
3509    // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
3510    // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
3511    // responses from entering the defensive malformed-response branches below.
3512    pub async fn status_options(
3513        &self,
3514        mode: StatusOptionsMode,
3515    ) -> Result<StatusOptionsReport, SourceError> {
3516        let before = self.status_snapshot().await?;
3517        // A terminal category's option is as configured as an open one's: a terminal
3518        // write validates it before closing and refuses when the board lacks it. Both
3519        // kinds' names are options of the one field, so both are asked for.
3520        let missing = self
3521            .statuses
3522            .wanted()
3523            .into_iter()
3524            .filter(|wanted| {
3525                !before
3526                    .options
3527                    .iter()
3528                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3529            })
3530            .collect::<Vec<_>>();
3531        let report = StatusOptionsReport {
3532            source: self.name.clone(),
3533            missing: missing.clone(),
3534            outcome: match (mode, missing.is_empty()) {
3535                (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
3536                (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
3537                (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
3538            },
3539            existing: before.options.clone(),
3540        };
3541        if mode == StatusOptionsMode::Plan || missing.is_empty() {
3542            return Ok(report);
3543        }
3544        let mut options = before
3545            .options
3546            .iter()
3547            .map(|option| {
3548                json!({
3549                    "id": option.id, "name": option.name, "color": option.color,
3550                    "description": option.description,
3551                })
3552            })
3553            .collect::<Vec<_>>();
3554        options.extend(missing.iter().map(|name| {
3555            json!({
3556                "name": name, "color": "GRAY", "description": ""
3557            })
3558        }));
3559        self.graphql(
3560            graphql::STATUS_OPTIONS_UPDATE,
3561            json!({"input": {
3562                "projectId": before.board_id, "fieldId": before.field_id,
3563                "singleSelectOptions": options,
3564            }}),
3565        )
3566        .await?;
3567        let after = self.status_snapshot().await?;
3568        let options_preserved = before
3569            .options
3570            .iter()
3571            .all(|old| after.options.iter().any(|new| new == old));
3572        let additions_present = missing.iter().all(|wanted| {
3573            after
3574                .options
3575                .iter()
3576                .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3577        });
3578        if !options_preserved || !additions_present || after.assignments != before.assignments {
3579            let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
3580                SourceError::Malformed {
3581                    message: format!("cannot render pre-write Status recovery snapshot: {error}"),
3582                }
3583            })?;
3584            return Err(SourceError::Refused {
3585                message: format!(
3586                    "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}"
3587                ),
3588            });
3589        }
3590        Ok(report)
3591    }
3592
3593    /// A fresh snapshot of the Status field and every board item's assignment of it.
3594    ///
3595    /// # Errors
3596    ///
3597    /// Refuses a board without a single-select `Status` field, and one the token cannot see.
3598    async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
3599        // Status alone, as this operation has always read it: a `Priority` field is another
3600        // operation's, so nothing about it can refuse this one.
3601        let mut board = self.board_snapshot(&[BoardField::Status]).await?;
3602        let field = board
3603            .fields
3604            .remove(&BoardField::Status)
3605            .ok_or_else(|| self.no_status_field())?;
3606        Ok(StatusSnapshot {
3607            assignments: board.assignments(BoardField::Status),
3608            board_id: board.board_id,
3609            field_id: field.field_id,
3610            options: field.options,
3611        })
3612    }
3613
3614    /// The refusal a board with no `Status` field is answered with by the guarded setup.
3615    fn no_status_field(&self) -> SourceError {
3616        SourceError::Refused {
3617            message: format!("source {} board has no Status field", self.name),
3618        }
3619    }
3620
3621    // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
3622    // the real CLI loopback journey, including pagination. The individual malformed guards
3623    // are defensive validation of a schema-pinned third-party response, not separate user
3624    // journeys; drift and missing-field failures cover the operation's recovery behavior.
3625    /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
3626    /// every board item's value of each, walked to the end of the board's items. A field not
3627    /// in `owned` is read past whatever it holds.
3628    async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
3629        let mut after: Option<String> = None;
3630        let mut snapshot: Option<BoardSnapshot> = None;
3631        loop {
3632            let data = self
3633                .graphql(
3634                    graphql::STATUS_OPTIONS_SNAPSHOT,
3635                    json!({
3636                        "owner": self.owner, "number": self.project_number,
3637                        "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
3638                    }),
3639                )
3640                .await?;
3641            let board = data
3642                .pointer("/owner/projectV2")
3643                .filter(|board| board.is_object())
3644                .ok_or_else(|| SourceError::Refused {
3645                    message: format!(
3646                        "source {} has no accessible GitHub Projects board",
3647                        self.name
3648                    ),
3649                })?;
3650            if board
3651                .pointer("/fields/pageInfo/hasNextPage")
3652                .and_then(Value::as_bool)
3653                != Some(false)
3654            {
3655                return Err(SourceError::Malformed {
3656                    message:
3657                        "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
3658                            .into(),
3659                });
3660            }
3661            let mut fields = BTreeMap::new();
3662            // Only the fields this setup owns, by name: a node the single-select fragment did not
3663            // match carries no name, and a person's own single-select field — a `Size`, a
3664            // `Team` — is none of this setup's business, so nothing about it can refuse one. A
3665            // `Status` or `Priority` field without its options is malformed, not absent.
3666            // 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.
3667            for (owned, field) in board
3668                .pointer("/fields/nodes")
3669                .and_then(Value::as_array)
3670                .ok_or_else(|| SourceError::Malformed {
3671                    message: "GitHub project fields.nodes is not an array".into(),
3672                })?
3673                .iter()
3674                .filter_map(|field| {
3675                    let named = BoardField::named(field.get("name")?.as_str()?)?;
3676                    owned.contains(&named).then_some((named, field))
3677                })
3678            {
3679                let options = field
3680                    .get("options")
3681                    .and_then(Value::as_array)
3682                    .ok_or_else(|| SourceError::Malformed {
3683                        message: "GitHub single-select field options is not an array".into(),
3684                    })?
3685                    .iter()
3686                    .map(|option| {
3687                        Ok(StatusOption {
3688                            id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
3689                                .map_err(|message| SourceError::Malformed { message })?,
3690                            name: ColumnName::try_from(required_str(option, "name")?.to_owned())
3691                                .map_err(|message| SourceError::Malformed {
3692                                    message: format!(
3693                                        "GitHub single-select option name is invalid: {message}"
3694                                    ),
3695                                })?,
3696                            color: serde_json::from_value(
3697                                option.get("color").cloned().unwrap_or(Value::Null),
3698                            )
3699                            .map_err(|error| {
3700                                SourceError::Malformed {
3701                                    message: format!(
3702                                        "GitHub single-select option color is invalid: {error}"
3703                                    ),
3704                                }
3705                            })?,
3706                            description: optional_str(option, "description")?
3707                                .unwrap_or_default()
3708                                .to_owned(),
3709                        })
3710                    })
3711                    .collect::<Result<Vec<_>, SourceError>>()?;
3712                let snapshot = SnapshotField {
3713                    field_id: required_nonblank_str(field, "id")?.to_owned(),
3714                    options,
3715                };
3716                // A board's field names are unique, so a second one is an answer that cannot
3717                // say which field the setup would act on — refused rather than one chosen.
3718                if fields.insert(owned, snapshot).is_some() {
3719                    return Err(SourceError::Malformed {
3720                        message: format!(
3721                            "GitHub answered two {} fields for this board",
3722                            owned.name()
3723                        ),
3724                    });
3725                }
3726            }
3727            let board_id = required_nonblank_str(board, "id")?.to_owned();
3728            let current = snapshot.get_or_insert_with(|| BoardSnapshot {
3729                board_id,
3730                fields,
3731                items: Vec::new(),
3732            });
3733            let items = board
3734                .pointer("/items/nodes")
3735                .and_then(Value::as_array)
3736                .ok_or_else(|| SourceError::Malformed {
3737                    message: "GitHub project items.nodes is not an array".into(),
3738                })?;
3739            for item in items {
3740                let field_values =
3741                    item.get("fieldValues")
3742                        .ok_or_else(|| SourceError::Malformed {
3743                            message: "GitHub project item is missing fieldValues".into(),
3744                        })?;
3745                if field_values
3746                    .pointer("/pageInfo/hasNextPage")
3747                    .and_then(Value::as_bool)
3748                    != Some(false)
3749                {
3750                    return Err(SourceError::Malformed {
3751                        message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
3752                    });
3753                }
3754                let values = item
3755                    .pointer("/fieldValues/nodes")
3756                    .and_then(Value::as_array)
3757                    .ok_or_else(|| SourceError::Malformed {
3758                        message: "GitHub project item fieldValues.nodes is not an array".into(),
3759                    })?;
3760                let item_id = required_nonblank_str(item, "id")?;
3761                let mut assigned = BTreeMap::new();
3762                for value in values {
3763                    let Some(field) = value
3764                        .pointer("/field/name")
3765                        .and_then(Value::as_str)
3766                        .and_then(BoardField::named)
3767                        .filter(|field| owned.contains(field))
3768                    else {
3769                        continue;
3770                    };
3771                    let held = assigned.insert(
3772                        field,
3773                        AssignedStatusOption {
3774                            id: StatusOptionId::try_from(
3775                                required_str(value, "optionId")?.to_owned(),
3776                            )
3777                            .map_err(|message| SourceError::Malformed { message })?,
3778                            name: ColumnName::try_from(required_str(value, "name")?.to_owned())
3779                                .map_err(|message| SourceError::Malformed {
3780                                    message: format!(
3781                                        "GitHub assigned {} name is invalid: {message}",
3782                                        field.name()
3783                                    ),
3784                                })?,
3785                        },
3786                    );
3787                    // An item holds one value of a field, so a second one leaves no way to
3788                    // tell which it holds — and a verification or recovery built on either
3789                    // could restore the wrong one.
3790                    if held.is_some() {
3791                        return Err(SourceError::Malformed {
3792                            message: format!(
3793                                "GitHub answered two {} values for board item {item_id}",
3794                                field.name()
3795                            ),
3796                        });
3797                    }
3798                }
3799                current.items.push((item_id.to_owned(), assigned));
3800            }
3801            let page = board.get("items").ok_or_else(|| SourceError::Malformed {
3802                message: "GitHub project is missing items".into(),
3803            })?;
3804            let has_next = page
3805                .pointer("/pageInfo/hasNextPage")
3806                .and_then(Value::as_bool)
3807                .ok_or_else(|| SourceError::Malformed {
3808                    message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
3809                })?;
3810            if !has_next {
3811                break;
3812            }
3813            let next =
3814                required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
3815            validate_cursor_progress(after.as_deref(), next)?;
3816            after = Some(next.to_owned());
3817        }
3818        snapshot.ok_or_else(|| SourceError::Malformed {
3819            message: "GitHub returned no board field snapshot".into(),
3820        })
3821    }
3822    // llmlint: ignore-end[changed_behavior_has_e2e]
3823
3824    /// Report every board field this source's configuration names and, with
3825    /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
3826    /// the `Priority` field when the board has none.
3827    ///
3828    /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
3829    /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
3830    /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
3831    /// color and description: the whole option list goes back with every existing id, because
3832    /// a re-minted id clears every item's value.
3833    ///
3834    /// # Errors
3835    ///
3836    /// Refuses a board without a single-select `Status` field. After an apply the board is
3837    /// read again, and a pre-existing option or any item's value of either field that moved is
3838    /// refused with the complete pre-write assignments in the diagnostic, for recovery.
3839    // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
3840    // unchanged apply, a created field, an added option to each field, drift refusal, a board
3841    // with no Status field and a non-github-projects source through the compiled CLI against
3842    // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
3843    pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
3844        let owned: Vec<BoardField> = if self.priorities.is_some() {
3845            vec![BoardField::Status, BoardField::Priority]
3846        } else {
3847            vec![BoardField::Status]
3848        };
3849        let before = self.board_snapshot(&owned).await?;
3850        let mut plans = vec![FieldPlan {
3851            field: BoardField::Status,
3852            wanted: self.statuses.wanted(),
3853        }];
3854        if !before.fields.contains_key(&BoardField::Status) {
3855            return Err(self.no_status_field());
3856        }
3857        if let Some(mapping) = &self.priorities {
3858            plans.push(FieldPlan {
3859                field: BoardField::Priority,
3860                wanted: mapping.names().map(str::to_owned).collect(),
3861            });
3862        }
3863        // The snapshot reads single-select fields alone, so a field it did not find may still
3864        // be on the board under the name, of another type: creating one beside it would fail
3865        // part way, or leave two fields of one name. Asked of the board's own field list, and
3866        // only when a field is missing.
3867        if plans
3868            .iter()
3869            .any(|plan| !before.fields.contains_key(&plan.field))
3870        {
3871            let board = self.board_fields().await?;
3872            for plan in plans
3873                .iter()
3874                .filter(|plan| !before.fields.contains_key(&plan.field))
3875            {
3876                if let Some(field) = Board::field(&board.fields, plan.field.name())? {
3877                    return Err(SourceError::Refused {
3878                        message: format!(
3879                            "source {}'s board has a {} field that is not a single-select field \
3880                             (it is a {}), so it cannot hold this source's options; next: rename \
3881                             or remove that field, then run this again",
3882                            self.name,
3883                            plan.field.name(),
3884                            optional_str(field, "__typename")?.unwrap_or("field of another type")
3885                        ),
3886                    });
3887                }
3888            }
3889        }
3890        let mut reports = Vec::new();
3891        for plan in &plans {
3892            let held = before.fields.get(&plan.field);
3893            let existing = held.map(|field| field.options.clone()).unwrap_or_default();
3894            let mut missing: Vec<String> = Vec::new();
3895            for wanted in &plan.wanted {
3896                let present = existing
3897                    .iter()
3898                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3899                    || missing
3900                        .iter()
3901                        .any(|named| named.eq_ignore_ascii_case(wanted));
3902                if !present {
3903                    missing.push(wanted.clone());
3904                }
3905            }
3906            let kinds = match plan.field {
3907                BoardField::Status => self.statuses.missing_by_kind(&existing),
3908                BoardField::Priority => Vec::new(),
3909            };
3910            reports.push(FieldReport {
3911                field: plan.field,
3912                exists: held.is_some(),
3913                kinds,
3914                outcome: match (mode, held.is_some(), missing.is_empty()) {
3915                    (SetupMode::Plan, _, _) => FieldOutcome::Planned,
3916                    (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
3917                    (SetupMode::Apply, true, false) => FieldOutcome::Applied,
3918                    (SetupMode::Apply, false, _) => FieldOutcome::Created,
3919                },
3920                missing,
3921                existing,
3922            });
3923        }
3924        // A projected field is a text field, which the single-select snapshot cannot see, so
3925        // it is asked of the board's own field list — and only when one is configured.
3926        let mut metadata_fields = Vec::new();
3927        if !self.metadata_fields.is_empty() {
3928            let board = self.board_fields().await?;
3929            for projection in &self.metadata_fields {
3930                let conflict = match Board::field(&board.fields, &projection.field)? {
3931                    None => None,
3932                    Some(field) => {
3933                        let typename = optional_str(field, "__typename")?;
3934                        let data_type = optional_str(field, "dataType")?;
3935                        if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
3936                            None
3937                        } else {
3938                            let named = data_type
3939                                .filter(|kind| *kind != "TEXT")
3940                                .or(typename)
3941                                .unwrap_or("field of another type");
3942                            Some(NonTextFieldType::try_from(named.to_owned()).map_err(
3943                                |message| SourceError::Malformed {
3944                                    message: format!(
3945                                        "GitHub answered the board's {} field with a type this \
3946                                         setup cannot read: {message}",
3947                                        &*projection.field
3948                                    ),
3949                                },
3950                            )?)
3951                        }
3952                    }
3953                };
3954                let exists =
3955                    conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
3956                metadata_fields.push(MetadataFieldReport {
3957                    field: projection.field.to_string(),
3958                    key: projection.key.to_string(),
3959                    path: projection.path.clone(),
3960                    exists,
3961                    outcome: match (mode, exists || conflict.is_some()) {
3962                        (SetupMode::Plan, _) => FieldOutcome::Planned,
3963                        (SetupMode::Apply, true) => FieldOutcome::Unchanged,
3964                        (SetupMode::Apply, false) => FieldOutcome::Created,
3965                    },
3966                    conflict,
3967                });
3968            }
3969        }
3970        let report = FieldsReport {
3971            source: self.name.clone(),
3972            fields: reports,
3973            metadata_fields,
3974        };
3975        let writes: Vec<&FieldReport> = report
3976            .fields
3977            .iter()
3978            .filter(|field| !field.missing.is_empty() || !field.exists)
3979            .collect();
3980        let creates: Vec<&MetadataFieldReport> = report
3981            .metadata_fields
3982            .iter()
3983            .filter(|field| field.outcome == FieldOutcome::Created)
3984            .collect();
3985        if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
3986            return Ok(report);
3987        }
3988        let mut landed: Vec<&str> = Vec::new();
3989        for field in &writes {
3990            let added = field
3991                .missing
3992                .iter()
3993                .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
3994            let sent = match before.fields.get(&field.field) {
3995                Some(held) => {
3996                    let mut options = held
3997                        .options
3998                        .iter()
3999                        .map(|option| {
4000                            json!({
4001                                "id": option.id, "name": option.name, "color": option.color,
4002                                "description": option.description,
4003                            })
4004                        })
4005                        .collect::<Vec<_>>();
4006                    options.extend(added);
4007                    self.graphql(
4008                        graphql::STATUS_OPTIONS_UPDATE,
4009                        json!({"input": {
4010                            "projectId": before.board_id, "fieldId": held.field_id,
4011                            "singleSelectOptions": options,
4012                        }}),
4013                    )
4014                    .await
4015                }
4016                None => {
4017                    self.graphql(
4018                        graphql::CREATE_FIELD,
4019                        json!({"input": {
4020                            "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4021                            "name": field.field.name(),
4022                            "singleSelectOptions": added.collect::<Vec<_>>(),
4023                        }}),
4024                    )
4025                    .await
4026                }
4027            };
4028            // A mutation that failed does not establish that GitHub left its field as it was,
4029            // so every failure from here on carries the recovery data a drift refusal does.
4030            match sent {
4031                Ok(_) => landed.push(field.field.name()),
4032                Err(error) => {
4033                    let changed = if landed.is_empty() {
4034                        String::new()
4035                    } else {
4036                        format!("changed the {} field and then ", landed.join(" and "))
4037                    };
4038                    return Err(SourceError::Refused {
4039                        message: format!(
4040                            "the guarded field setup {changed}failed on the {} field, which it may \
4041                             have changed part way: {error}; the pre-write item assignments \
4042                             are:\n{}",
4043                            field.field.name(),
4044                            recovery(&report, &before)?
4045                        ),
4046                    });
4047                }
4048            }
4049        }
4050        for field in &creates {
4051            let sent = self
4052                .graphql(
4053                    graphql::CREATE_FIELD,
4054                    json!({"input": {
4055                        "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4056                    }}),
4057                )
4058                .await;
4059            if let Err(error) = sent {
4060                let changed = if landed.is_empty() {
4061                    String::new()
4062                } else {
4063                    format!("changed the {} field and then ", landed.join(" and "))
4064                };
4065                return Err(SourceError::Refused {
4066                    message: format!(
4067                        "the guarded field setup {changed}failed creating the {} text field: \
4068                         {error}; run it again to create what is still missing",
4069                        field.field
4070                    ),
4071                });
4072            }
4073            landed.push(&field.field);
4074        }
4075        // The board has been written, so a verification read that fails leaves it unverified
4076        // rather than unchanged, and says what to put back.
4077        let after = match self.board_snapshot(&owned).await {
4078            Ok(after) => after,
4079            Err(error) => {
4080                return Err(SourceError::Refused {
4081                    message: format!(
4082                        "the guarded field setup changed the {} field and then could not read the \
4083                         board back to verify it: {error}; the pre-write item assignments are:\n{}",
4084                        landed.join(" and "),
4085                        recovery(&report, &before)?
4086                    ),
4087                });
4088            }
4089        };
4090        let mut moved = Vec::new();
4091        for field in &report.fields {
4092            let name = field.field.name();
4093            let now = after
4094                .fields
4095                .get(&field.field)
4096                .map(|held| held.options.as_slice())
4097                .unwrap_or_default();
4098            if !field.existing.iter().all(|old| now.contains(old)) {
4099                moved.push(format!(
4100                    "a pre-existing {name} option id, name, color or description"
4101                ));
4102            }
4103            if !field.missing.iter().all(|wanted| {
4104                now.iter()
4105                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4106            }) {
4107                moved.push(format!("an added {name} option"));
4108            }
4109            if after.assignments(field.field) != before.assignments(field.field) {
4110                moved.push(format!("an item's {name} value"));
4111            }
4112        }
4113        if !creates.is_empty() {
4114            // Read afresh: the view held for this command is the one from before the creates.
4115            *self.fields_cache()? = None;
4116            let board = match self.board_fields().await {
4117                Ok(board) => board,
4118                Err(error) => {
4119                    return Err(SourceError::Refused {
4120                        message: format!(
4121                            "the guarded field setup changed the {} field and then could not \
4122                             read the board back to verify it: {error}; run it again to verify \
4123                             it, which creates nothing a second time",
4124                            landed.join(" and ")
4125                        ),
4126                    });
4127                }
4128            };
4129            for field in &creates {
4130                let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
4131                    held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
4132                        && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
4133                });
4134                if !text {
4135                    moved.push(format!("the created {} text field", field.field));
4136                }
4137            }
4138        }
4139        if !moved.is_empty() {
4140            return Err(SourceError::Refused {
4141                message: format!(
4142                    "GitHub changed {} after the guarded field setup; the pre-write item \
4143                     assignments are:\n{}",
4144                    moved.join(", "),
4145                    recovery(&report, &before)?
4146                ),
4147            });
4148        }
4149        Ok(report)
4150    }
4151
4152    /// Validate configuration and capture the named credential without exposing it.
4153    ///
4154    /// # Errors
4155    ///
4156    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
4157    /// [`SourceError::Auth`] when the named credential is missing or empty.
4158    pub fn new(
4159        name: &SourceName,
4160        config: GitHubProjectsConfig,
4161        secrets: &dyn SecretResolver,
4162    ) -> Result<Self, SourceError> {
4163        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
4164    }
4165
4166    /// The same, recording every request it sends into an accounting the caller holds too.
4167    ///
4168    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
4169    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
4170    /// up — passes the one it records those into, so the session total accounts for the
4171    /// whole session rather than for this source's share of it.
4172    ///
4173    /// # Errors
4174    ///
4175    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
4176    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
4177    pub fn recording_into(
4178        name: &SourceName,
4179        config: GitHubProjectsConfig,
4180        secrets: &dyn SecretResolver,
4181        ledger: Arc<Accounting>,
4182    ) -> Result<Self, SourceError> {
4183        if !valid_github_owner(&config.owner) {
4184            return Err(SourceError::Config {
4185                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
4186            });
4187        }
4188        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
4189            return Err(SourceError::Config {
4190                message: format!("project_number must be between 1 and {}", i32::MAX),
4191            });
4192        }
4193        if !valid_environment_name(&config.token_env) {
4194            return Err(SourceError::Config {
4195                message: "token_env must be a valid environment-variable name".into(),
4196            });
4197        }
4198        let repository = config
4199            .repository
4200            .as_deref()
4201            .map(RepositoryTarget::parse)
4202            .transpose()?;
4203        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
4204            message: format!("endpoint is not a valid URL: {e}"),
4205        })?;
4206        if endpoint.scheme() != "https"
4207            && !(endpoint.scheme() == "http"
4208                && endpoint
4209                    .host_str()
4210                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
4211        {
4212            return Err(SourceError::Config {
4213                message:
4214                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
4215                        .into(),
4216            });
4217        }
4218        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
4219            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),
4220        })?;
4221        Ok(Self {
4222            name: name.clone(),
4223            owner: config.owner,
4224            project_number: config.project_number,
4225            repository,
4226            asset_client: assets::client(&endpoint)?,
4227            endpoint,
4228            token,
4229            credential_name: config.token_env,
4230            statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
4231            priorities: config
4232                .priority_mapping
4233                .map(|mapping| PriorityMapping::resolve(mapping, name))
4234                .transpose()?,
4235            metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
4236            client: Client::builder()
4237                .user_agent("onetaskgraph")
4238                .build()
4239                .map_err(|e| SourceError::Config {
4240                    message: format!("cannot build HTTP client: {e}"),
4241                })?,
4242            created: Mutex::new(Vec::new()),
4243            updated: Mutex::new(Vec::new()),
4244            commented: Mutex::new(Vec::new()),
4245            pacing: Pacing::resolve(config.pacing, name)?,
4246            last_mutation: Mutex::new(None),
4247            clock: system_clock(),
4248            numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
4249            board_cache: Mutex::new(None),
4250            search_cache: Mutex::new(None),
4251            narrowed_cache: Mutex::new(BTreeMap::new()),
4252            resolved_cache: Mutex::new(BTreeMap::new()),
4253            children_cache: Mutex::new(BTreeMap::new()),
4254            search_next: Mutex::new(BTreeMap::new()),
4255            fields_cache: Mutex::new(None),
4256            repository_cache: Mutex::new(BTreeMap::new()),
4257            ledger,
4258            expected: expected::Held::default(),
4259        })
4260    }
4261
4262    /// A snapshot of every request this source has sent, and what each cost.
4263    ///
4264    /// A value to hold and compare rather than a borrow of the accounting itself, so two
4265    /// of them can sit side by side. When this source was built with
4266    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
4267    /// point of building it that way.
4268    #[must_use]
4269    pub fn accounting(&self) -> accounting::Session {
4270        self.ledger.snapshot()
4271    }
4272
4273    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
4274    /// rate limit rather than handing it straight back as an error.
4275    ///
4276    /// Retrying is safe for every document here, including the mutations, and the reason
4277    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
4278    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
4279    /// this replays has already taken effect. An outcome this source cannot know — the
4280    /// send failed, or the body could not be read, so the mutation may well have landed —
4281    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
4282    /// attempt. A duplicate write would come from replaying one of those, and none is
4283    /// replayed.
4284    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4285        if !self.expected.journaling() {
4286            return self.graphql_sent(query, variables).await;
4287        }
4288        // While a write the store's cache expects runs, its mutations select the whole issue
4289        // they leave behind, so the response vouches for what the cache keeps — and every
4290        // request is recorded, so the write's parts are reported from their own responses.
4291        let (query, variables) = match graphql::whole_twin(query) {
4292            Some(twin) => (twin, expected::whole_variables(variables)),
4293            None => (query, variables),
4294        };
4295        let sent = self.graphql_sent(query, variables.clone()).await;
4296        self.journal(query, &variables, &sent).await;
4297        sent
4298    }
4299
4300    /// One document sent, retried while GitHub asks for a wait, exactly as asked.
4301    async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4302        if is_mutation(query)
4303            && ![
4304                graphql::ADD_COMMENT,
4305                graphql::UPDATE_COMMENT,
4306                graphql::DELETE_COMMENT,
4307                graphql::ADD_COMMENT_WHOLE,
4308                graphql::UPDATE_COMMENT_WHOLE,
4309            ]
4310            .contains(&query)
4311        {
4312            let mut cache = self.resolved_cache()?;
4313            for argument in ["input", "second", "third", "clear"] {
4314                if let Some(input) = variables.get(argument) {
4315                    cache.retain(|id, item| {
4316                        !["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
4317                            input
4318                                .get(key)
4319                                .and_then(Value::as_str)
4320                                .is_some_and(|value| value == id.0 || value == item.item_id)
4321                        })
4322                    });
4323                }
4324            }
4325        }
4326        let doing = operation_description(query);
4327        let mut waited = Duration::ZERO;
4328        let mut waits = 0_u32;
4329        let mut backoff = self.pacing.retry_backoff;
4330        loop {
4331            if is_mutation(query) {
4332                let spacing = self.reserve_mutation_slot();
4333                if !spacing.is_zero() {
4334                    self.clock.sleep(spacing).await;
4335                }
4336            }
4337            let attempt = self.send_once(query, &variables).await;
4338            if is_mutation(query) {
4339                self.finish_mutation();
4340            }
4341            let limited = match attempt {
4342                Ok(data) => return Ok(data),
4343                Err(Attempt::Failed(error)) => return Err(error),
4344                Err(Attempt::Limited(limited)) => limited,
4345            };
4346            // GitHub really does send `retry-after: 0`, and retrying at once is the one
4347            // move that extends a secondary limit, so a hint below the schedule's own next
4348            // wait is raised to it.
4349            let wait = match limited.hint {
4350                Some(hint) => Duration::from_secs(hint).max(backoff),
4351                None => backoff,
4352            };
4353            let remaining = self.pacing.retry_budget.saturating_sub(waited);
4354            // A wait of nothing spends none of the budget, so it is exhaustion rather
4355            // than a retry. `Pacing::resolve` rules out every way of configuring one
4356            // except a budget of zero, where reporting the first refusal is the ask.
4357            if wait.is_zero() || wait > remaining {
4358                return Err(limited.exhausted(
4359                    doing,
4360                    waits,
4361                    waited,
4362                    wait,
4363                    self.pacing.retry_budget,
4364                ));
4365            }
4366            self.clock.sleep(wait).await;
4367            waited += wait;
4368            waits += 1;
4369            backoff = backoff.saturating_mul(2);
4370        }
4371    }
4372
4373    /// The next moment a content-creating mutation may leave this source, as a wait from
4374    /// now.
4375    ///
4376    /// The slot is reserved under the lock and the waiting happens outside it, so two
4377    /// callers take two slots rather than the same one — and no lock is held across an
4378    /// await.
4379    ///
4380    /// The moment it is spaced from is the previous mutation's *completion*, which
4381    /// [`Self::finish_mutation`] records. See that method for why the release moment on its
4382    /// own is the wrong thing to measure from.
4383    fn reserve_mutation_slot(&self) -> Duration {
4384        if self.pacing.min_mutation_interval.is_zero() {
4385            return Duration::ZERO;
4386        }
4387        // A poisoned lock here costs pacing, not correctness, and refusing the write over
4388        // it would turn an earlier failure into a second one for no gain.
4389        let mut last = self
4390            .last_mutation
4391            .lock()
4392            .unwrap_or_else(std::sync::PoisonError::into_inner);
4393        let now = self.clock.now();
4394        // `checked_add` rather than `+`: adding durations can panic on overflow, and
4395        // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
4396        let at = last.map_or(now, |previous| {
4397            previous
4398                .checked_add(self.pacing.min_mutation_interval)
4399                .map_or(now, |earliest| earliest.max(now))
4400        });
4401        *last = Some(at);
4402        at.saturating_sub(now)
4403    }
4404
4405    /// Record that a content-creating mutation has finished, so the next one is spaced
4406    /// from here rather than from the moment this one was released.
4407    ///
4408    /// This source can only choose when a request *departs*; the limiter counts when it
4409    /// *arrives*, and the two differ by whatever the request spent in transit. Spacing one
4410    /// departure from the last therefore hands the limiter a gap of the interval less that
4411    /// transit, so a source pacing at 750 ms can still be seen arriving faster — which is
4412    /// exactly how a copy paced well inside a board's threshold was refused by it on a
4413    /// slower machine while passing on a quick one.
4414    ///
4415    /// Spacing from completion removes the subtraction rather than budgeting for it. The
4416    /// previous request had already arrived before its response came back, so its arrival
4417    /// is no later than this moment, and the next mutation is released at least the
4418    /// interval after this moment and arrives no earlier than it is released: the gap the
4419    /// limiter measures is therefore at least the interval, whatever transit costs and on
4420    /// whatever platform. The price is that a mutation's own round trip no longer counts
4421    /// towards its spacing, which makes this source slightly slower than the configured
4422    /// rate rather than slightly faster — the safe side of a limit that punishes being
4423    /// wrong by refusing reads for the next fifty minutes.
4424    ///
4425    /// A failed attempt is recorded too: a request refused by the limiter still arrived,
4426    /// and one that never left costs only a wait nobody needed.
4427    fn finish_mutation(&self) {
4428        if self.pacing.min_mutation_interval.is_zero() {
4429            return;
4430        }
4431        // A poisoned lock here costs pacing, not correctness, exactly as in the reservation.
4432        let mut last = self
4433            .last_mutation
4434            .lock()
4435            .unwrap_or_else(std::sync::PoisonError::into_inner);
4436        let now = self.clock.now();
4437        // `max` rather than an assignment: a concurrent caller may already have reserved a
4438        // slot further out, and completing this request must never pull that slot back in.
4439        *last = Some(last.map_or(now, |reserved| reserved.max(now)));
4440    }
4441
4442    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
4443    /// failure that waiting cannot help — and recorded, whichever of the three it was.
4444    ///
4445    /// This is the one place a request leaves this crate, which is why the accounting is
4446    /// here rather than at each of the callers: a read path added later is counted without
4447    /// anybody remembering to count it, and
4448    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
4449    /// when one is not.
4450    async fn send_once(&self, query: &str, variables: &Value) -> Result<Value, Attempt> {
4451        let Attempted {
4452            result,
4453            limits,
4454            reported_cost,
4455        } = self.attempt(query, variables).await;
4456        // No `otherwise` name: every document this source sends is one of its own, and the
4457        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
4458        let sending = accounting::Request::graphql(query, variables, None, reported_cost);
4459        let outcome = match &result {
4460            Ok(_) => accounting::Outcome::Answered,
4461            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
4462            Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
4463        };
4464        self.ledger.record(sending.finished(outcome, limits));
4465        result
4466    }
4467
4468    /// The attempt itself, with what its response said about the rate limit alongside.
4469    ///
4470    /// The two are returned together rather than recorded here because every one of the
4471    /// early exits below is a different outcome, and a record written at each of them is a
4472    /// record one of them can be added without.
4473    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
4474        let mut limits = accounting::RateLimit::default();
4475        let mut reported_cost = None;
4476        let result = self
4477            .attempted(query, variables, &mut limits, &mut reported_cost)
4478            .await;
4479        Attempted {
4480            result,
4481            limits,
4482            reported_cost,
4483        }
4484    }
4485
4486    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
4487    async fn attempted(
4488        &self,
4489        query: &str,
4490        variables: &Value,
4491        limits: &mut accounting::RateLimit,
4492        reported_cost: &mut Option<u64>,
4493    ) -> Result<Value, Attempt> {
4494        let response = self
4495            .client
4496            .post(self.endpoint.clone())
4497            .bearer_auth(self.token.expose_secret())
4498            .json(&json!({"query": query, "variables": variables}))
4499            .send()
4500            .await
4501            .map_err(|e| {
4502                Attempt::Failed(SourceError::Unavailable {
4503                    message: format!("GitHub GraphQL request failed: {e}"),
4504                })
4505            })?;
4506        let status = response.status();
4507        let header = |name: &str| whole_seconds(response.headers().get(name));
4508        let served = response
4509            .headers()
4510            .get(reqwest::header::DATE)
4511            .and_then(|value| value.to_str().ok())
4512            .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
4513            .map(|at| at.with_timezone(&Utc));
4514        *limits = accounting::RateLimit::read(|name| {
4515            response
4516                .headers()
4517                .get(name)
4518                .and_then(|value| value.to_str().ok())
4519                .map(str::to_owned)
4520        });
4521        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
4522        // that are not text at all — is "not known to be exhausted". This never makes a
4523        // response a refusal on its own: it says which limiter a refusal is attributed to
4524        // and where its hint comes from, so a value this cannot read costs a hint rather
4525        // than an answer.
4526        let exhausted = response
4527            .headers()
4528            .get("x-ratelimit-remaining")
4529            .and_then(|value| value.to_str().ok())
4530            == Some("0");
4531        // `retry-after` is what GitHub asks for when it asks; when it does not and the
4532        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
4533        // which is the same question answered as an absolute time. Nothing else here is a
4534        // hint, and a schedule is what answers a refusal that carries none.
4535        let hint = header("retry-after").or_else(|| {
4536            exhausted
4537                .then(|| header("x-ratelimit-reset"))
4538                .flatten()
4539                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
4540        });
4541        // Read before it is parsed, because the evidence which tells a secondary rate
4542        // limit from a rejected credential is in the body of a response whose status says
4543        // only "forbidden" — and a non-success response was never parsed at all.
4544        let body = response.text().await.map_err(|e| {
4545            Attempt::Failed(SourceError::Unavailable {
4546                message: format!("GitHub GraphQL response could not be read: {e}"),
4547            })
4548        })?;
4549        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
4550            return Err(Attempt::Limited(Limited { limiter, hint }));
4551        }
4552        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
4553            return Err(Attempt::Failed(SourceError::Auth {
4554                message: format!(
4555                    "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"
4556                ),
4557            }));
4558        }
4559        if !status.is_success() {
4560            return Err(Attempt::Failed(SourceError::Unavailable {
4561                message: format!("GitHub GraphQL returned HTTP {status}"),
4562            }));
4563        }
4564        // GitHub reports what a call cost only when the document asked it to, and no
4565        // document this source sends does — so this is `None` here and carries the figure
4566        // for a caller whose own document selects `rateLimit { cost }`. What it must never
4567        // pick up is a `dryRun` probe's cost, which is some other document's.
4568        *reported_cost = serde_json::from_str::<Value>(&body)
4569            .ok()
4570            .as_ref()
4571            .and_then(|body| body.pointer("/data/rateLimit/cost"))
4572            .and_then(Value::as_u64);
4573        let mut data = self.answer(&body).map_err(Attempt::Failed)?;
4574        if let Some(served) = served {
4575            stamp_served(&mut data, &served.to_rfc3339());
4576        }
4577        Ok(data)
4578    }
4579
4580    /// What one successful HTTP response says, once its GraphQL errors are read.
4581    fn answer(&self, body: &str) -> Result<Value, SourceError> {
4582        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
4583            message: format!("GitHub returned invalid JSON: {e}"),
4584        })?;
4585        let errors = body
4586            .get("errors")
4587            .map(|value| {
4588                value.as_array().ok_or_else(|| SourceError::Malformed {
4589                    message: "GitHub response errors is not an array".into(),
4590                })
4591            })
4592            .transpose()?;
4593        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
4594            let messages = errors
4595                .iter()
4596                .filter_map(|e| e.get("message").and_then(Value::as_str))
4597                .collect::<Vec<_>>()
4598                .join("; ");
4599            let message = if messages.is_empty() {
4600                "GitHub returned GraphQL errors".into()
4601            } else {
4602                messages
4603            };
4604            let normalized = message.to_ascii_lowercase();
4605            if normalized.contains("resource not accessible") || normalized.contains("scope") {
4606                return Err(SourceError::Auth {
4607                    message: format!(
4608                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
4609                        self.credential_name
4610                    ),
4611                });
4612            }
4613            return Err(SourceError::Refused { message });
4614        }
4615        body.get("data")
4616            .filter(|data| data.is_object())
4617            .cloned()
4618            .ok_or_else(|| SourceError::Malformed {
4619                message: "GitHub response has no data object".into(),
4620            })
4621    }
4622
4623    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
4624    // GraphQL cannot independently page them inside the outer item page. This source page is
4625    // deliberately bounded at that published maximum; the live drift journey exercises it.
4626    async fn board_page(
4627        &self,
4628        items_after: Option<&str>,
4629        items_first: u32,
4630    ) -> Result<Value, SourceError> {
4631        let data = self
4632            .graphql(
4633                graphql::BOARD,
4634                json!({"owner":self.owner,"number":self.project_number,
4635                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
4636                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
4637            )
4638            .await?;
4639        data.pointer("/owner/projectV2")
4640            .filter(|v| !v.is_null())
4641            .cloned()
4642            .ok_or_else(|| SourceError::Refused {
4643                message: format!(
4644                    "GitHub project {}/{} was not found or is not visible to the token",
4645                    self.owner, self.project_number
4646                ),
4647            })
4648    }
4649
4650    /// The search that finds the issues of this board, narrowed by `also` when it is
4651    /// given.
4652    ///
4653    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
4654    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
4655    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
4656    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
4657    /// from a task by the `parent` field each issue carries rather than by the search.
4658    fn board_search(&self, also: Option<&str>) -> String {
4659        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
4660        match also {
4661            Some(also) => format!("{scope} {also}"),
4662            None => scope,
4663        }
4664    }
4665
4666    /// One issue this source reached directly, as the board item a read of the board would
4667    /// have produced — or `None` when this board does not hold it.
4668    ///
4669    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
4670    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
4671    /// item's own id, that item's field values, and the issue as its content. One resolver
4672    /// for both routes is what makes an issue read through a search, through its own node
4673    /// id, or through its project's sub-issues report the same title, the same status, the
4674    /// same labels and the same qualified id.
4675    ///
4676    /// An issue with no entry for *this* board is not this source's to report, which is
4677    /// what keeps an id naming some other repository's issue from being answered as an item
4678    /// of this board. That answer is given about an **exhausted** connection and never
4679    /// about an unread page: the entry is looked for on the page in hand, and only if that
4680    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
4681    /// rest of it.
4682    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
4683        if optional_str(issue, "__typename")? != Some("Issue") {
4684            return Ok(None);
4685        }
4686        let memberships = issue
4687            .get("projectItems")
4688            .ok_or_else(|| SourceError::Malformed {
4689                message: "GitHub issue is missing projectItems".into(),
4690            })?;
4691        let nodes = memberships
4692            .get("nodes")
4693            .and_then(Value::as_array)
4694            .ok_or_else(|| SourceError::Malformed {
4695                message: "GitHub issue projectItems.nodes is not an array".into(),
4696            })?;
4697        let held = match self.board_entry(nodes) {
4698            Some(held) => held.clone(),
4699            None => {
4700                let info = memberships
4701                    .get("pageInfo")
4702                    .ok_or_else(|| SourceError::Malformed {
4703                        message: "GitHub issue projectItems has no pageInfo".into(),
4704                    })?;
4705                // The page held no entry for this board. Whether that means the issue is
4706                // not on it is a question about the rest of the connection, and only a
4707                // connection with no rest answers it here.
4708                if !required_bool(info, "hasNextPage")? {
4709                    return Ok(None);
4710                }
4711                let cursor = required_str(info, "endCursor")?;
4712                validate_cursor_progress(None, cursor)?;
4713                let issue_id = required_str(issue, "id")?;
4714                match self.board_membership(issue_id, cursor).await? {
4715                    Some(held) => held,
4716                    None => return Ok(None),
4717                }
4718            }
4719        };
4720        // The membership's own `updatedAt` rides along: it is the board half of the item's change
4721        // token, which a field change moves where the issue's own time does not.
4722        let item = json!({
4723            "id": required_str(&held, "id")?,
4724            "updatedAt": held.get("updatedAt"),
4725            "project": held.get("project"),
4726            "fieldValues": held.get("fieldValues"),
4727            "content": issue,
4728        });
4729        self.resolve(&item)
4730    }
4731
4732    /// This board's own entry among one page of an issue's `Issue.projectItems`.
4733    ///
4734    /// One spelling of *which membership is this board's*, so the page a read carries and
4735    /// the pages [`Self::board_membership`] walks are searched by the same rule.
4736    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
4737        nodes.iter().find(|node| {
4738            node.pointer("/project/number").and_then(Value::as_u64)
4739                == Some(u64::from(self.project_number))
4740        })
4741    }
4742
4743    /// The rest of one issue's board memberships, from `after`, for this board's entry.
4744    ///
4745    /// The recovery read: a page of memberships that holds no entry for this board says
4746    /// nothing about the memberships past it, so the connection is walked to exhaustion
4747    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
4748    /// positive answer — the whole connection was read and no entry named this board —
4749    /// rather than a failure, and the walk is held to
4750    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
4751    /// with a cursor that does not advance is refused instead of spun on.
4752    async fn board_membership(
4753        &self,
4754        issue: &str,
4755        after: &str,
4756    ) -> Result<Option<Value>, SourceError> {
4757        let mut after = after.to_owned();
4758        loop {
4759            let data = self
4760                .graphql(
4761                    graphql::ISSUE_BOARD_ITEMS,
4762                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
4763                           "nestedFirst":NESTED_PAGE_SIZE}),
4764                )
4765                .await?;
4766            let Some(connection) = data
4767                .pointer("/node/projectItems")
4768                .filter(|value| !value.is_null())
4769            else {
4770                // The id resolved to nothing, or to something with no memberships to walk —
4771                // which is the same answer as a connection holding no entry for this board.
4772                return Ok(None);
4773            };
4774            let nodes = connection
4775                .get("nodes")
4776                .and_then(Value::as_array)
4777                .ok_or_else(|| SourceError::Malformed {
4778                    message: "GitHub issue projectItems.nodes is not an array".into(),
4779                })?;
4780            if let Some(held) = self.board_entry(nodes) {
4781                return Ok(Some(held.clone()));
4782            }
4783            let info = connection
4784                .get("pageInfo")
4785                .ok_or_else(|| SourceError::Malformed {
4786                    message: "GitHub issue projectItems has no pageInfo".into(),
4787                })?;
4788            let next = required_bool(info, "hasNextPage")?
4789                .then(|| required_str(info, "endCursor"))
4790                .transpose()?;
4791            match next {
4792                Some(next) => {
4793                    validate_cursor_progress(Some(&after), next)?;
4794                    after = next.to_owned();
4795                }
4796                None => return Ok(None),
4797            }
4798        }
4799    }
4800
4801    /// One page of a board-scoped issue search, and where the next page resumes.
4802    async fn search_page(
4803        &self,
4804        search: &str,
4805        first: u32,
4806        after: Option<&str>,
4807    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
4808        let data = self
4809            .graphql(
4810                graphql::SEARCH_ISSUES,
4811                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
4812                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
4813                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4814            )
4815            .await?;
4816        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
4817            message: "GitHub search response has no search connection".into(),
4818        })?;
4819        let mut found = Vec::new();
4820        for node in connection
4821            .get("nodes")
4822            .and_then(Value::as_array)
4823            .ok_or_else(|| SourceError::Malformed {
4824                message: "GitHub search nodes is not an array".into(),
4825            })?
4826        {
4827            if let Some(resolved) = self.resolve_issue(node).await? {
4828                found.push(resolved);
4829            }
4830        }
4831        let info = connection
4832            .get("pageInfo")
4833            .ok_or_else(|| SourceError::Malformed {
4834                message: "GitHub search connection has no pageInfo".into(),
4835            })?;
4836        let next = required_bool(info, "hasNextPage")?
4837            .then(|| required_str(info, "endCursor"))
4838            .transpose()?
4839            .map(str::to_owned);
4840        if let Some(next) = &next {
4841            validate_cursor_progress(after, next)?;
4842        }
4843        Ok((found, next))
4844    }
4845
4846    /// Every issue this board holds, completed with what this run wrote.
4847    ///
4848    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
4849    /// is an index and is eventually consistent, so an issue this run created seconds ago
4850    /// can be absent from it, and a project listed straight after being written would
4851    /// otherwise be missing from its own board. What is added back is only what this
4852    /// process itself wrote, out of [`Self::created`], which lives and dies with the
4853    /// process.
4854    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4855        let found = self.searched_issues().await?;
4856        self.completed_with_written(found, |_| true)
4857    }
4858
4859    /// Every issue this board's own search reports, walked to exhaustion, read once per
4860    /// source.
4861    ///
4862    /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
4863    /// needs it too and the two would otherwise walk the same search twice in one command.
4864    /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
4865    /// is.
4866    async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4867        let cached = self.search_cache()?.clone();
4868        if let Some(held) = cached {
4869            return Ok(held);
4870        }
4871        let mut after: Option<String> = None;
4872        let mut found = Vec::new();
4873        let search = self.board_search(None);
4874        loop {
4875            let (page, next) = self
4876                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
4877                .await?;
4878            found.extend(page);
4879            match next {
4880                Some(next) => after = Some(next),
4881                None => break,
4882            }
4883        }
4884        *self.search_cache()? = Some(found.clone());
4885        Ok(found)
4886    }
4887
4888    /// This process's own view of the board's issues, or the refusal a poisoned lock is.
4889    fn search_cache(
4890        &self,
4891    ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
4892        self.search_cache
4893            .lock()
4894            .map_err(|_| SourceError::Unavailable {
4895                message: "this source's view of the board's issues was left inconsistent by an \
4896                      earlier failure; next: run the command again"
4897                    .into(),
4898            })
4899    }
4900
4901    /// `found`, with everything this run wrote that `keep` accepts and the read did not
4902    /// report.
4903    ///
4904    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
4905    /// at all: the search index is behind, and a node read of an item filed moments ago can
4906    /// be too.
4907    fn completed_with_written(
4908        &self,
4909        mut found: Vec<Resolved>,
4910        keep: impl Fn(&Resolved) -> bool,
4911    ) -> Result<Vec<Resolved>, SourceError> {
4912        for own in self.created()?.iter().filter(|own| keep(own)) {
4913            if !found.iter().any(|item| item.id == own.id) {
4914                found.push(own.clone());
4915            }
4916        }
4917        Ok(found)
4918    }
4919
4920    /// What resolving one node id reached.
4921    ///
4922    /// Three answers rather than an `Option`, because a board *draft* is none of the other
4923    /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
4924    /// is completed by a read of the draft itself rather than reported as nothing.
4925    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
4926        let asked = self
4927            .graphql(
4928                graphql::ISSUE,
4929                json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
4930                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4931            )
4932            .await;
4933        let data = match asked {
4934            Ok(data) => data,
4935            // A string that is not a node id at all is not a failure to report: it is an id
4936            // this board does not hold, which is what every read of one already answers.
4937            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
4938            Err(error) => return Err(error),
4939        };
4940        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
4941            return Ok(Reached::Nothing);
4942        };
4943        if optional_str(node, "__typename")? == Some("DraftIssue") {
4944            return Ok(Reached::Draft);
4945        }
4946        Ok(match self.resolve_issue(node).await? {
4947            Some(item) => Reached::Held(Box::new(item)),
4948            None => Reached::Nothing,
4949        })
4950    }
4951
4952    /// One item of this board by its own id, whatever kind it is.
4953    ///
4954    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
4955    /// run wrote is read first, because a node read of an item created moments ago can
4956    /// still be behind the board field values written onto it — see [`Self::created`].
4957    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4958        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
4959            return Ok(Some(own.clone()));
4960        }
4961        match self.reach(id).await? {
4962            Reached::Held(item) => Ok(Some(*item)),
4963            Reached::Nothing => Ok(None),
4964            Reached::Draft => self.draft_by_id(id).await,
4965        }
4966    }
4967
4968    /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
4969    /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
4970    /// than one request per id.
4971    ///
4972    /// What this run wrote answers first, as it does there, and only the rest is read. One id
4973    /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
4974    /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
4975    /// id at a time, so that id is answered as not held and the others as themselves; a draft
4976    /// is completed by a read of the draft, exactly as there.
4977    async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
4978        let mut found: Vec<Option<Option<Resolved>>> = {
4979            let created = self.created()?;
4980            ids.iter()
4981                .map(|id| {
4982                    created
4983                        .iter()
4984                        .find(|own| own.id == *id)
4985                        .map(|own| Some(own.clone()))
4986                })
4987                .collect()
4988        };
4989        let unread: Vec<NativeId> = ids
4990            .iter()
4991            .zip(&found)
4992            .filter(|(_, found)| found.is_none())
4993            .map(|(id, _)| id.clone())
4994            .collect();
4995        let mut read = Vec::with_capacity(unread.len());
4996        if let [one] = unread.as_slice() {
4997            read.push(self.item_by_id(one).await?);
4998        } else {
4999            for batch in unread.chunks(DETAIL_BATCH) {
5000                let data = match self
5001                    .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
5002                    .await
5003                {
5004                    Ok(data) => data,
5005                    Err(error) if unresolvable_node(&error) => {
5006                        for id in batch {
5007                            read.push(self.item_by_id(id).await?);
5008                        }
5009                        continue;
5010                    }
5011                    Err(error) => return Err(error),
5012                };
5013                for (slot, id) in batch.iter().enumerate() {
5014                    let node =
5015                        data.get(format!("i{slot}"))
5016                            .ok_or_else(|| SourceError::Malformed {
5017                                message: format!(
5018                                    "GitHub answered a batch read with no item for {}",
5019                                    id.0
5020                                ),
5021                            })?;
5022                    read.push(if node.is_null() {
5023                        None
5024                    } else if optional_str(node, "__typename")? == Some("DraftIssue") {
5025                        self.draft_by_id(id).await?
5026                    } else {
5027                        if optional_str(node, "__typename")? == Some("Issue")
5028                            && required_str(node, "id")? != id.0
5029                        {
5030                            return Err(SourceError::Malformed {
5031                                message: format!(
5032                                    "GitHub answered the read of {} with issue {}",
5033                                    id.0,
5034                                    required_str(node, "id")?
5035                                ),
5036                            });
5037                        }
5038                        self.resolve_issue(node).await?
5039                    });
5040                }
5041            }
5042        }
5043        let mut read = read.into_iter();
5044        Ok(found
5045            .iter_mut()
5046            .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
5047            .collect())
5048    }
5049
5050    fn resolved_cache(
5051        &self,
5052    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
5053        self.resolved_cache
5054            .lock()
5055            .map_err(|_| SourceError::Unavailable {
5056                message: "resolved item records were left inconsistent; run the command again"
5057                    .into(),
5058            })
5059    }
5060
5061    /// Reuse a record this invocation already resolved. The mutation sender invalidates
5062    /// it before writing, so a partial failure cannot leave a pre-write binding behind.
5063    async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5064        let cached = self.resolved_cache()?.get(id).cloned();
5065        match cached {
5066            Some(item) => Ok(Some(item)),
5067            None => self.item_by_id(id).await,
5068        }
5069    }
5070
5071    /// One board draft by its own id, with the board item it sits in — or `None` when no
5072    /// item of this board is that draft's.
5073    ///
5074    /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
5075    /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
5076    /// links a draft to one board item, so the page this read carries is the whole of that
5077    /// connection, and a page that reports more than it holds is refused rather than read
5078    /// as an answer about memberships nobody read.
5079    async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5080        let data = self
5081            .graphql(
5082                graphql::DRAFT,
5083                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
5084                       "boardItems":BOARD_ITEMS_PAGE_SIZE}),
5085            )
5086            .await?;
5087        // Gone between the two reads is an answer — the draft is no longer there. Anything
5088        // else than the draft [`Self::reach`] was just told this id is, is not one.
5089        let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
5090            return Ok(None);
5091        };
5092        if optional_str(draft, "__typename")? != Some("DraftIssue") {
5093            return Err(SourceError::Malformed {
5094                message: format!(
5095                    "GitHub answered {} as a draft and then as something else",
5096                    id.0
5097                ),
5098            });
5099        }
5100        if required_str(draft, "id")? != id.0 {
5101            return Err(SourceError::Malformed {
5102                message: format!("GitHub answered a different draft for {}", id.0),
5103            });
5104        }
5105        let memberships = draft
5106            .get("projectV2Items")
5107            .ok_or_else(|| SourceError::Malformed {
5108                message: format!("GitHub draft {} is missing projectV2Items", id.0),
5109            })?;
5110        let nodes = memberships
5111            .get("nodes")
5112            .and_then(Value::as_array)
5113            .ok_or_else(|| SourceError::Malformed {
5114                message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
5115            })?;
5116        let info = memberships
5117            .get("pageInfo")
5118            .ok_or_else(|| SourceError::Malformed {
5119                message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
5120            })?;
5121        // Read whether or not this board's entry is on the page: a page claiming more than
5122        // the one item GitHub links a draft to is a malformed answer either way.
5123        if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
5124            return Err(SourceError::Malformed {
5125                message: format!(
5126                    "GitHub draft {} reports more board items than the one GitHub links a draft \
5127                     to",
5128                    id.0
5129                ),
5130            });
5131        }
5132        if let Some(node) = nodes.first()
5133            && node
5134                .pointer("/project/number")
5135                .and_then(Value::as_u64)
5136                .is_none()
5137        {
5138            return Err(SourceError::Malformed {
5139                message: format!(
5140                    "GitHub draft {} board item has no numeric project number",
5141                    id.0
5142                ),
5143            });
5144        }
5145        let Some(held) = self.board_entry(nodes) else {
5146            return Ok(None);
5147        };
5148        if required_str(
5149            held.get("project").ok_or_else(|| SourceError::Malformed {
5150                message: format!("GitHub draft {} board item has no project", id.0),
5151            })?,
5152            "id",
5153        )? != self.board_fields().await?.id.as_str()
5154        {
5155            return Ok(None);
5156        }
5157        let item = json!({
5158            "id": required_str(held, "id")?,
5159            "project": held.get("project"),
5160            "fieldValues": held.get("fieldValues"),
5161            "content": draft,
5162        });
5163        self.resolve(&item)
5164    }
5165
5166    /// The board's own id and field definitions, for a write whose item does not carry
5167    /// them — never its items.
5168    ///
5169    /// A board this command has already listed supplies them, since it read them beside its
5170    /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
5171    /// is consulted about which items the board holds: see the module documentation for
5172    /// why a question about one known item is answered by reading that item.
5173    async fn board_fields(&self) -> Result<BoardFields, SourceError> {
5174        if let Some(board) = self.board_cache()?.as_ref() {
5175            return Ok(BoardFields {
5176                id: BoardId::parse(&board.id)?,
5177                fields: board.fields.clone(),
5178            });
5179        }
5180        if let Some(held) = self.fields_cache()?.clone() {
5181            return Ok(held);
5182        }
5183        let data = self
5184            .graphql(
5185                graphql::BOARD_FIELDS,
5186                json!({"owner":self.owner,"number":self.project_number,
5187                       "nestedFirst":NESTED_PAGE_SIZE}),
5188            )
5189            .await?;
5190        self.fields_read(&data)
5191    }
5192
5193    /// The board's id and fields out of an answer carrying the `boardFields` root, held for
5194    /// the rest of this command.
5195    fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
5196        let board = data
5197            .pointer("/boardFields/projectV2")
5198            .filter(|value| !value.is_null())
5199            .ok_or_else(|| SourceError::Refused {
5200                message: format!(
5201                    "GitHub project {}/{} was not found or is not visible to the token",
5202                    self.owner, self.project_number
5203                ),
5204            })?;
5205        let read = BoardFields {
5206            id: BoardId::parse(required_str(board, "id")?)?,
5207            fields: board.get("fields").cloned().unwrap_or(Value::Null),
5208        };
5209        *self.fields_cache()? = Some(read.clone());
5210        Ok(read)
5211    }
5212
5213    /// Read what creating an issue in `repository` needs and this command has not read yet —
5214    /// the board's fields and the repository's node id — in one request when it needs both.
5215    ///
5216    /// When either is already known this sends nothing, and the other is read by its own
5217    /// document where it is asked for, so no create reads anything twice.
5218    async fn creation_context(
5219        &self,
5220        repository: &RepositoryTarget,
5221        incoming: &Incoming<'_>,
5222    ) -> Result<(), SourceError> {
5223        let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
5224        if fields_known || self.repository_cache()?.contains_key(repository) {
5225            return Ok(());
5226        }
5227        let data = self
5228            .graphql(
5229                graphql::CREATION_CONTEXT,
5230                json!({"owner":self.owner,"number":self.project_number,
5231                       "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
5232                       "repositoryName":repository.name}),
5233            )
5234            .await?;
5235        self.fields_read(&data)?;
5236        self.repository_read(&data, repository, incoming)?;
5237        Ok(())
5238    }
5239
5240    /// This process's own view of the board's fields, or the refusal a poisoned lock is.
5241    fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
5242        self.fields_cache
5243            .lock()
5244            .map_err(|_| SourceError::Unavailable {
5245                message: "this source's view of the board's fields was left inconsistent by an \
5246                      earlier failure; next: run the command again"
5247                    .into(),
5248            })
5249    }
5250
5251    /// What a write to `item` needs of the board, read off that item when it says enough and
5252    /// off [`Self::board_fields`] when it does not.
5253    ///
5254    /// A node read of an item names its board and carries the definition of every field it
5255    /// holds a value of — so an item naming its board, holding a value of the origin field,
5256    /// and, when the write carries a status, holding a `Status` value, needs no read of the
5257    /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
5258    /// of may still be on the board, and a view reading it as absent would refuse a write the
5259    /// board can take or skip a field write the board needs, so such an item — and a create,
5260    /// which has no item yet — takes the board's fields from their own read instead.
5261    async fn fields_for(
5262        &self,
5263        item: Option<&Resolved>,
5264        writes_status: bool,
5265        selects_priority: bool,
5266    ) -> Result<BoardFields, SourceError> {
5267        if let Some(board) = item.and_then(Resolved::carried_board) {
5268            return Ok(board);
5269        }
5270        if let Some(item) = item
5271            && let Some(board_id) = item.named_board()
5272            && item.defines(ORIGIN_FIELD)
5273            && (!writes_status || item.defines("Status"))
5274            && (!selects_priority || item.defines(PRIORITY_FIELD))
5275            && self
5276                .metadata_fields
5277                .iter()
5278                .all(|projection| item.defines(&projection.field))
5279        {
5280            return Ok(BoardFields {
5281                id: board_id,
5282                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
5283            });
5284        }
5285        self.board_fields().await
5286    }
5287
5288    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
5289    /// when that id names nothing here with a sub-issue relationship to walk.
5290    ///
5291    /// `None` and an empty answer are different: `None` is *this is not an issue of this
5292    /// GitHub*, which is what sends a project selector on to be read as a name, and an
5293    /// empty vector is a project that holds nothing.
5294    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
5295        let mut after: Option<String> = None;
5296        let mut children = Vec::new();
5297        loop {
5298            let asked = self
5299                .graphql(
5300                    graphql::SUB_ISSUES,
5301                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
5302                           "nestedFirst":NESTED_PAGE_SIZE,
5303                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5304                )
5305                .await;
5306            let data = match asked {
5307                Ok(data) => data,
5308                // A string that is not a node id at all is not a failure to report: it is
5309                // the ordinary answer to a selector naming a project by its name.
5310                Err(error) if unresolvable_node(&error) => return Ok(None),
5311                Err(error) => return Err(error),
5312            };
5313            let Some(connection) = data
5314                .pointer("/node/subIssues")
5315                .filter(|value| !value.is_null())
5316            else {
5317                // No such node, or one with no sub-issue relationship — a board draft is
5318                // the one this board can really hold.
5319                return Ok(None);
5320            };
5321            for node in connection
5322                .get("nodes")
5323                .and_then(Value::as_array)
5324                .ok_or_else(|| SourceError::Malformed {
5325                    message: "GitHub subIssues.nodes is not an array".into(),
5326                })?
5327            {
5328                if let Some(resolved) = self.resolve_issue(node).await? {
5329                    children.push(resolved);
5330                }
5331            }
5332            let info = connection
5333                .get("pageInfo")
5334                .ok_or_else(|| SourceError::Malformed {
5335                    message: "GitHub subIssues connection has no pageInfo".into(),
5336                })?;
5337            let next = required_bool(info, "hasNextPage")?
5338                .then(|| required_str(info, "endCursor"))
5339                .transpose()?;
5340            match next {
5341                Some(next) => {
5342                    validate_cursor_progress(after.as_deref(), next)?;
5343                    after = Some(next.to_owned());
5344                }
5345                None => return Ok(Some(children)),
5346            }
5347        }
5348    }
5349
5350    /// Which issue of this board a project *name* is, or `None` when none is.
5351    ///
5352    /// One bounded query which filters on that name at the server, rather than a walk of
5353    /// every issue the board holds. The name is compared again here: the qualifier narrows
5354    /// what GitHub sends, and this source decides what it names.
5355    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
5356        let search = self.board_search(Some(&title_qualifier(name)));
5357        let mut after = None;
5358        loop {
5359            let (candidates, next) = self
5360                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
5361                .await?;
5362            if let Some(item) = candidates.into_iter().find(|item| {
5363                item.kind == BoardKind::Work(ItemKind::Project)
5364                    && item.title.eq_ignore_ascii_case(name)
5365            }) {
5366                return Ok(Some(item.id));
5367            }
5368            match next {
5369                Some(next) => after = Some(next),
5370                None => return Ok(None),
5371            }
5372        }
5373    }
5374
5375    /// Everything filed under one project of this board: the sub-issues of the issue that
5376    /// project is.
5377    ///
5378    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
5379    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
5380    /// gains projects, or as another project gains tasks.
5381    ///
5382    /// A qualified id names the issue and is asked for its sub-issues directly: one
5383    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
5384    /// read as a project *name*, which costs the one bounded search
5385    /// [`Self::project_by_name`] makes.
5386    ///
5387    /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
5388    /// — and each answer is still cut to the items whose parent is this project, so one this
5389    /// process has since filed elsewhere is not reported here, and completed with what this
5390    /// process filed under it.
5391    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
5392        let held = self.children_cache()?.get(selector).cloned();
5393        let (project, children) = match held {
5394            Some(held) => held,
5395            None => {
5396                let answered = match self.sub_issues(selector).await? {
5397                    Some(children) => (selector.clone(), children),
5398                    None => match self.project_by_name(&selector.0).await? {
5399                        Some(project) => {
5400                            let children = self.sub_issues(&project).await?.unwrap_or_default();
5401                            (project, children)
5402                        }
5403                        None => return Ok(Vec::new()),
5404                    },
5405                };
5406                self.children_cache()?
5407                    .insert(selector.clone(), answered.clone());
5408                answered
5409            }
5410        };
5411        let mut children: Vec<Resolved> = children
5412            .into_iter()
5413            .filter(|child| child.parent.as_ref() == Some(&project))
5414            .collect();
5415        for own in self.updated()?.iter() {
5416            if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
5417                children.push(own.clone());
5418            }
5419        }
5420        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
5421    }
5422
5423    /// Every issue of this board GitHub's issue search reports updated at or after `since`,
5424    /// completed with what this run wrote — the candidates a comment-activity read confirms.
5425    ///
5426    /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
5427    /// on the board whatever repository, and whatever owner, it lives in, so no repository or
5428    /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
5429    /// sufficient is a fact about GitHub rather than about this source: a comment written on an
5430    /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
5431    /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
5432    /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
5433    /// rather than silently narrowing a caller's answer.
5434    ///
5435    /// The instant is written to the second, rounded down, which can only widen what the
5436    /// search returns; confirmation against each candidate's own comments is what makes the
5437    /// answer exact. The search is an index that lags a write by a second or two — the module
5438    /// documentation records it — so a caller that asks again from its last instant should
5439    /// overlap the two by more than that.
5440    async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
5441        let found = self.searched(&updated_qualifier(since)).await?;
5442        self.completed_with_written(found, |_| true)
5443    }
5444
5445    /// Every issue of this board GitHub's issue search reports for the board-scoped search
5446    /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
5447    ///
5448    /// Uncompleted: what this process wrote is added by the caller, which knows whether its
5449    /// own record is the fresher of the two.
5450    async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5451        let search = self.board_search(Some(also));
5452        let mut after: Option<String> = None;
5453        let mut found = Vec::new();
5454        loop {
5455            let (page, next) = self
5456                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
5457                .await?;
5458            found.extend(page);
5459            match next {
5460                Some(next) => after = Some(next),
5461                None => return Ok(found),
5462            }
5463        }
5464    }
5465
5466    /// A bounded task answer; the versioned cursor carries the connection position, how
5467    /// many rows of the page starting there were already handed out, and the own-write ids
5468    /// already observed, including across a new source instance.
5469    ///
5470    /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
5471    /// sliced from the pages it needs; why is the module documentation's paging contract.
5472    async fn search_tasks(
5473        &self,
5474        query: &TaskQuery,
5475        page: &PageRequest,
5476        also: &str,
5477    ) -> Result<Page<Task>, SourceError> {
5478        let mut position = match &page.cursor {
5479            None => SearchPosition::default(),
5480            Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
5481                .ok()
5482                .filter(|position| {
5483                    position.version == SEARCH_CURSOR_VERSION
5484                        && position.connection.valid_resume(position.offset)
5485                })
5486                .ok_or_else(|| SourceError::Config {
5487                    message: "page cursor is invalid".into(),
5488                })?,
5489        };
5490        let search = self.board_search(Some(also));
5491        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
5492        let own = self.with_own_writes(Vec::new())?;
5493        // An issue this process commented on is a candidate of a comment-activity read
5494        // whether or not the search has caught up with the comment; see `Self::commented`.
5495        let commented = match query.commented_since {
5496            Some(_) => self.commented()?.clone(),
5497            None => Vec::new(),
5498        };
5499        for id in own.iter().map(|item| &item.id).chain(&commented) {
5500            if !position.own.contains(id) {
5501                position.own.push(id.clone());
5502            }
5503        }
5504        let mut tasks = Vec::new();
5505        while !position.connection.exhausted() && tasks.len() < limit {
5506            let first = SEARCH_PAGE_SIZE;
5507            // Page size is part of the key: a short cached answer cannot answer a wider ask.
5508            let key =
5509                serde_json::to_string(&("page", &search, &position.connection.after(), first))
5510                    .expect("search page key is serializable");
5511            let cached = if query.commented_since.is_none() {
5512                self.narrowed_cache()?.get(&key).cloned()
5513            } else {
5514                None
5515            };
5516            let (found, next) = match cached {
5517                Some(found) => {
5518                    let next = self
5519                        .search_next
5520                        .lock()
5521                        .map_err(|_| SourceError::Unavailable {
5522                            message:
5523                                "search pagination was left inconsistent; run the command again"
5524                                    .into(),
5525                        })?
5526                        .get(&key)
5527                        .cloned()
5528                        .flatten();
5529                    (found, next)
5530                }
5531                None => {
5532                    let (found, next) = self
5533                        .search_page(&search, first, position.connection.after())
5534                        .await?;
5535                    if query.commented_since.is_none() {
5536                        self.search_next
5537                            .lock()
5538                            .map_err(|_| SourceError::Unavailable {
5539                                message:
5540                                    "search pagination was left inconsistent; run the command again"
5541                                        .into(),
5542                            })?
5543                            .insert(key.clone(), next.clone());
5544                        self.narrowed_cache()?.insert(key, found.clone());
5545                    }
5546                    (found, next)
5547                }
5548            };
5549            let rows = found.len();
5550            for mut item in found.into_iter().skip(position.offset) {
5551                if tasks.len() == limit {
5552                    break;
5553                }
5554                position.offset += 1;
5555                if position.own.contains(&item.id) {
5556                    if position.seen.contains(&item.id) {
5557                        continue;
5558                    }
5559                    position.seen.push(item.id.clone());
5560                    // The search's own copy of an issue this process only commented on is as
5561                    // good as a node read of it, since its comments are read either way.
5562                    let only_commented = commented.contains(&item.id)
5563                        && !own.iter().any(|written| written.id == item.id);
5564                    if !only_commented {
5565                        let updated_at = item.updated_at;
5566                        let Some(written) = self.search_written(&own, &item.id).await? else {
5567                            continue;
5568                        };
5569                        item = written;
5570                        item.updated_at = item.updated_at.max(updated_at);
5571                        self.resolved_cache()?.insert(item.id.clone(), item.clone());
5572                    }
5573                }
5574                if item.kind == BoardKind::Work(ItemKind::Task) {
5575                    let task = item.task()?;
5576                    if task_matches(&task, query, &query.project)
5577                        && self.commented_since(&item, query.commented_since).await?
5578                    {
5579                        tasks.push(task);
5580                    }
5581                }
5582            }
5583            if position.offset < rows {
5584                continue;
5585            }
5586            position.offset = 0;
5587            position.connection = match next {
5588                Some(after) => SearchConnection::Continuing {
5589                    after: Cursor(after),
5590                },
5591                None => SearchConnection::Exhausted {},
5592            };
5593        }
5594        if position.connection.exhausted() {
5595            for id in position.own.clone() {
5596                if position.seen.contains(&id) {
5597                    continue;
5598                }
5599                if tasks.len() == limit {
5600                    break;
5601                }
5602                position.seen.push(id.clone());
5603                let Some(item) = self.search_written(&own, &id).await? else {
5604                    continue;
5605                };
5606                if item.kind == BoardKind::Work(ItemKind::Task) {
5607                    let task = item.task()?;
5608                    if task_matches(&task, query, &query.project)
5609                        && self.commented_since(&item, query.commented_since).await?
5610                    {
5611                        tasks.push(task);
5612                    }
5613                }
5614            }
5615        }
5616        let more = !position.connection.exhausted()
5617            || position.own.iter().any(|id| !position.seen.contains(id));
5618        Ok(Page {
5619            items: tasks,
5620            next: more.then(|| {
5621                Cursor(serde_json::to_string(&position).expect("search position is serializable"))
5622            }),
5623        })
5624    }
5625
5626    /// A resumed process has the ids but no write records; resolve only a record the
5627    /// current page needs, by its uncached node read rather than the lagging search index.
5628    async fn search_written(
5629        &self,
5630        own: &[Resolved],
5631        id: &NativeId,
5632    ) -> Result<Option<Resolved>, SourceError> {
5633        match own.iter().find(|item| item.id == *id) {
5634            Some(item) => Ok(Some(item.clone())),
5635            None => self.item_by_id(id).await,
5636        }
5637    }
5638
5639    /// The candidates for a task query carrying a text, metadata or origin predicate, read
5640    /// without enumerating the board — or `None` for a query carrying none of the three, which
5641    /// keeps the reads it always had.
5642    ///
5643    /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
5644    /// because it names at most a handful of items. Text and metadata are answered by one
5645    /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
5646    /// further by `updated:>=` when the query also asks for comment activity, since both
5647    /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
5648    /// process afterwards by the same predicates [`task_matches`] applies to every read.
5649    ///
5650    /// Completed with what this process wrote, its own record winning over the index's copy
5651    /// of the same item: see [`Self::with_own_writes`].
5652    async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
5653        let asked = match (&query.origin, narrowing_qualifiers(query)) {
5654            (Some(origin), _) => Narrowing::Origin(origin.clone()),
5655            (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
5656                Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
5657                None => qualifiers,
5658            }),
5659            (None, None) => return Ok(None),
5660        };
5661        // A question about comment activity is asked afresh every time, as it always was: it
5662        // is the one a caller polls from one source while waiting for the index, and an
5663        // answer held from the first poll would be the answer to every later one.
5664        let key = query.commented_since.is_none().then(|| asked.key());
5665        let cached = match &key {
5666            Some(key) => self.narrowed_cache()?.get(key).cloned(),
5667            None => None,
5668        };
5669        let found = match cached {
5670            Some(found) => found,
5671            None => {
5672                let found = match &asked {
5673                    Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
5674                    Narrowing::Search(also) => self.searched(also).await?,
5675                };
5676                if let Some(key) = key {
5677                    self.narrowed_cache()?.insert(key, found.clone());
5678                }
5679                found
5680            }
5681        };
5682        self.with_own_writes(found).map(Some)
5683    }
5684
5685    /// The candidates for a project or unscoped document query carrying a searchable text,
5686    /// read without enumerating the board — or `None` for a query with no text or a blank one,
5687    /// which keeps the read it always had.
5688    ///
5689    /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
5690    /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
5691    /// costs is the issues that match and never the board. Its answer is held for the command
5692    /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
5693    /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
5694    /// substring rule, exactly as an item of the wider read was, and is completed with what this
5695    /// process wrote: see [`Self::with_own_writes`].
5696    async fn text_searched(
5697        &self,
5698        text: Option<&TextQuery>,
5699    ) -> Result<Option<Vec<Resolved>>, SourceError> {
5700        let Some(also) = text_qualifiers(text) else {
5701            return Ok(None);
5702        };
5703        let key = Narrowing::Search(also.clone()).key();
5704        let cached = self.narrowed_cache()?.get(&key).cloned();
5705        let found = match cached {
5706            Some(found) => found,
5707            None => {
5708                let found = self.searched(&also).await?;
5709                self.narrowed_cache()?.insert(key, found.clone());
5710                found
5711            }
5712        };
5713        self.with_own_writes(found).map(Some)
5714    }
5715
5716    /// Every item of this board that may carry `origin` — a superset of those that do — found
5717    /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
5718    ///
5719    /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
5720    /// which reads the field every carrier holds, whichever release wrote it — and the
5721    /// board-scoped issue search for the same id as a phrase in the body, where this source
5722    /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
5723    /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
5724    /// query's, exactly.
5725    ///
5726    /// Both connections are walked to exhaustion, each from its own cursor. One that has
5727    /// already ended is sent its last cursor again, which answers an empty page, so the one
5728    /// document serves every page of either. What the two leave is stated in the module
5729    /// documentation: a carrier another process added within the last second or two, before
5730    /// either index has it.
5731    async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
5732        let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
5733        let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
5734        let mut items_after: Option<String> = None;
5735        let mut search_after: Option<String> = None;
5736        let mut found: Vec<Resolved> = Vec::new();
5737        let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
5738            if !found.iter().any(|held| held.id == resolved.id) {
5739                found.push(resolved);
5740            }
5741        };
5742        loop {
5743            let data = self
5744                .graphql(
5745                    graphql::ORIGIN_LOOKUP,
5746                    json!({"owner":self.owner,"number":self.project_number,"filter":filter,
5747                           "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
5748                           "itemsAfter":items_after,"searchAfter":search_after,
5749                           "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
5750                           "duplicates":true}),
5751                )
5752                .await?;
5753            let items = data
5754                .pointer("/originItems/projectV2/items")
5755                .filter(|value| !value.is_null())
5756                .ok_or_else(|| SourceError::Refused {
5757                    message: format!(
5758                        "GitHub project {}/{} was not found or is not visible to the token",
5759                        self.owner, self.project_number
5760                    ),
5761                })?;
5762            for item in optional_nodes(Some(items), "project items")?
5763                .into_iter()
5764                .flatten()
5765            {
5766                // The board's own items list its drafts too, and a draft is not an issue: no
5767                // narrowed read answers with one, whatever its origin field holds.
5768                if let Some(resolved) = self.resolve(item)?
5769                    && resolved.content_kind == ContentKind::Issue
5770                {
5771                    keep(resolved, &mut found);
5772                }
5773            }
5774            let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
5775                message: "GitHub search response has no search connection".into(),
5776            })?;
5777            for node in optional_nodes(Some(searched), "search")?
5778                .into_iter()
5779                .flatten()
5780            {
5781                if let Some(resolved) = self.resolve_issue(node).await? {
5782                    keep(resolved, &mut found);
5783                }
5784            }
5785            let items_next = resumed(items, items_after.as_deref())?;
5786            let search_next = resumed(searched, search_after.as_deref())?;
5787            if !items_next.has_more() && !search_next.has_more() {
5788                return Ok(found);
5789            }
5790            items_after = items_next.cursor();
5791            search_after = search_next.cursor();
5792        }
5793    }
5794
5795    /// `found`, with every item this process created or wrote in its place, and every one of
5796    /// them the read did not report added.
5797    ///
5798    /// This process's own record wins over the read's copy of the same item, because a read
5799    /// of an item written moments ago can still be behind what was written onto it — the
5800    /// origin field included, which is the one a narrowed read is confirmed against — and a
5801    /// read that still names an item under a predicate this process's write moved it out of
5802    /// must not return it. The one thing the read knows that the record cannot is when GitHub
5803    /// last saw the item change, which is what a comment-activity read rules a candidate out
5804    /// by, so the read's `updatedAt` is kept when the record has none of its own. See
5805    /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
5806    fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
5807        // A board draft is not an issue, so no narrowed read returns one, and this process
5808        // having written one does not make it an answer either.
5809        let own: Vec<Resolved> = self
5810            .created()?
5811            .iter()
5812            .chain(self.updated()?.iter())
5813            .filter(|own| own.content_kind == ContentKind::Issue)
5814            .cloned()
5815            .collect();
5816        for mut own in own {
5817            self.resolved_cache()?.insert(own.id.clone(), own.clone());
5818            match found.iter_mut().find(|read| read.id == own.id) {
5819                Some(read) => {
5820                    own.updated_at = own.updated_at.max(read.updated_at);
5821                    *read = own;
5822                }
5823                None => found.push(own),
5824            }
5825        }
5826        Ok(found)
5827    }
5828
5829    /// Whether `item` has a comment created or last edited at or after `since` — always, when
5830    /// there is no instant to hold it to.
5831    ///
5832    /// The candidate's own `updatedAt` is read first, because a comment written or edited at
5833    /// or after the instant moved it there: an issue not updated since holds no such comment,
5834    /// and its comments are never asked for — unless this process commented on it in this
5835    /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
5836    /// only as far as the first that matches. A board draft is not an issue and has no
5837    /// comments, so it never matches.
5838    async fn commented_since(
5839        &self,
5840        item: &Resolved,
5841        since: Option<DateTime<Utc>>,
5842    ) -> Result<bool, SourceError> {
5843        let Some(since) = since else {
5844            return Ok(true);
5845        };
5846        if item.content_kind == ContentKind::DraftIssue {
5847            return Ok(false);
5848        }
5849        // An `updatedAt` this process's own record or a lagging index holds can predate a
5850        // comment this process wrote since, so only an issue it did not comment on is ruled
5851        // out by one.
5852        if item.updated_at.is_some_and(|updated| updated < since)
5853            && !self.commented()?.contains(&item.id)
5854        {
5855            return Ok(false);
5856        }
5857        let query = TaskQuery {
5858            commented_since: Some(since),
5859            ..TaskQuery::default()
5860        };
5861        let mut after: Option<String> = None;
5862        loop {
5863            let data = self
5864                .graphql(
5865                    graphql::ISSUE_COMMENTS,
5866                    json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
5867                )
5868                .await?;
5869            let Some(connection) = data
5870                .get("node")
5871                .filter(|value| !value.is_null())
5872                .and_then(|node| node.get("comments"))
5873                .filter(|value| !value.is_null())
5874            else {
5875                // Removed since the search reported it: no longer an issue with comments.
5876                return Ok(false);
5877            };
5878            let comments = optional_nodes(Some(connection), "issue comments")?
5879                .into_iter()
5880                .flatten()
5881                .map(comment_from)
5882                .collect::<Result<Vec<_>, _>>()?;
5883            if query.comments_match(&comments) {
5884                return Ok(true);
5885            }
5886            match next_cursor(connection)? {
5887                Some(next) => {
5888                    validate_cursor_progress(after.as_deref(), &next.0)?;
5889                    after = Some(next.0);
5890                }
5891                None => return Ok(false),
5892            }
5893        }
5894    }
5895
5896    /// Every item on the board: the union of both enumerations GitHub offers of one.
5897    ///
5898    /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
5899    /// board **draft** and reads the board's own fields beside its items, and only the search
5900    /// reports an item that connection is behind on. The module documentation is where the lag and the
5901    /// measurements behind it are written down.
5902    ///
5903    /// A search result is admitted on the same terms as any other issue this source reaches
5904    /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
5905    /// names *this* board — so an issue the index still believes is here after it was taken
5906    /// off is refused rather than reported.
5907    ///
5908    /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
5909    /// which is what the cache could otherwise have broken.
5910    async fn board(&self) -> Result<Board, SourceError> {
5911        let cached = self.board_cache()?.clone();
5912        let mut board = match cached {
5913            Some(board) => board,
5914            None => {
5915                let read = self.read_board().await?;
5916                *self.board_cache()? = Some(read.clone());
5917                read
5918            }
5919        };
5920        for held in self.searched_issues().await? {
5921            if !board.items.iter().any(|item| item.id == held.id) {
5922                board.items.push(held);
5923            }
5924        }
5925        for own in self.created()?.iter() {
5926            if !board.items.iter().any(|item| item.id == own.id) {
5927                board.items.push(own.clone());
5928            }
5929        }
5930        Ok(board)
5931    }
5932
5933    /// This process's own view of the board, or the refusal a poisoned lock is.
5934    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
5935        self.board_cache
5936            .lock()
5937            .map_err(|_| SourceError::Unavailable {
5938                message: "this source's view of the board was left inconsistent by an earlier \
5939                      failure; next: run the command again"
5940                    .into(),
5941            })
5942    }
5943
5944    /// Bring this process's own view of the board up to an item it has just written.
5945    ///
5946    /// A created item goes to `created`, which is what completes a board read GitHub's own
5947    /// eventual consistency has left behind. An item that was already there is replaced
5948    /// where it sits, so a second write of it in the same command reads its real parent
5949    /// rather than the one it had before the first write.
5950    ///
5951    /// "Where it sits" is three places, and missing an earlier one leaves a stale record
5952    /// that wins: an item this same run created is held in `created` and not in the cached
5953    /// board, and `board` completes the cached board *from* `created`, so replacing only
5954    /// the cached copy of such an item replaces nothing and the read still reports the
5955    /// title it was created with. The search is the third, and it is the one an item the
5956    /// board's own projection is behind on sits in *alone* — which is exactly the item this
5957    /// source is least able to re-read, so leaving it out would put the stale title back on
5958    /// the only items the completion in [`Self::board`] exists for.
5959    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
5960        self.resolved_cache()?.insert(item.id.clone(), item.clone());
5961        if created {
5962            self.created()?.push(item);
5963            return Ok(());
5964        }
5965        {
5966            let mut own = self.created()?;
5967            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
5968                *held = item;
5969                return Ok(());
5970            }
5971        }
5972        {
5973            let mut own = self.updated()?;
5974            match own.iter_mut().find(|held| held.id == item.id) {
5975                Some(held) => *held = item.clone(),
5976                None => own.push(item.clone()),
5977            }
5978        }
5979        if let Some(board) = self.board_cache()?.as_mut()
5980            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
5981        {
5982            *held = item.clone();
5983        }
5984        if let Some(found) = self.search_cache()?.as_mut()
5985            && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
5986        {
5987            *held = item.clone();
5988        }
5989        for found in self.narrowed_cache()?.values_mut() {
5990            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
5991                *held = item.clone();
5992            }
5993        }
5994        for (_, found) in self.children_cache()?.values_mut() {
5995            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
5996                *held = item.clone();
5997            }
5998        }
5999        Ok(())
6000    }
6001
6002    /// Forget one item this process has just deleted, from every half of its own view.
6003    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
6004        self.resolved_cache()?.remove(id);
6005        self.created()?.retain(|own| own.id != *id);
6006        self.updated()?.retain(|own| own.id != *id);
6007        self.commented()?.retain(|own| own != id);
6008        if let Some(board) = self.board_cache()?.as_mut() {
6009            board.items.retain(|item| item.id != *id);
6010        }
6011        if let Some(found) = self.search_cache()?.as_mut() {
6012            found.retain(|item| item.id != *id);
6013        }
6014        for found in self.narrowed_cache()?.values_mut() {
6015            found.retain(|item| item.id != *id);
6016        }
6017        for (_, found) in self.children_cache()?.values_mut() {
6018            found.retain(|item| item.id != *id);
6019        }
6020        Ok(())
6021    }
6022
6023    /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
6024    fn narrowed_cache(
6025        &self,
6026    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
6027        self.narrowed_cache
6028            .lock()
6029            .map_err(|_| SourceError::Unavailable {
6030                message: "this source's view of a narrowed read was left inconsistent by an \
6031                      earlier failure; next: run the command again"
6032                    .into(),
6033            })
6034    }
6035
6036    /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
6037    /// is.
6038    fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
6039        self.children_cache
6040            .lock()
6041            .map_err(|_| SourceError::Unavailable {
6042                message: "this source's view of a project's tasks was left inconsistent by an \
6043                      earlier failure; next: run the command again"
6044                    .into(),
6045            })
6046    }
6047
6048    /// Every page of the board, read from GitHub.
6049    async fn read_board(&self) -> Result<Board, SourceError> {
6050        let mut after: Option<String> = None;
6051        let mut items = Vec::new();
6052        let mut board;
6053        loop {
6054            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
6055            for item in page
6056                .pointer("/items/nodes")
6057                .and_then(Value::as_array)
6058                .ok_or_else(|| SourceError::Malformed {
6059                    message: "GitHub project items.nodes is not an array".into(),
6060                })?
6061            {
6062                if let Some(resolved) = self.resolve(item)? {
6063                    items.push(resolved);
6064                }
6065            }
6066            let info = page
6067                .pointer("/items/pageInfo")
6068                .ok_or_else(|| SourceError::Malformed {
6069                    message: "GitHub project items have no pageInfo".into(),
6070                })?;
6071            let has_next = required_bool(info, "hasNextPage")?;
6072            let next = has_next
6073                .then(|| required_str(info, "endCursor"))
6074                .transpose()?;
6075            board = page.clone();
6076            match next {
6077                Some(next) => {
6078                    validate_cursor_progress(after.as_deref(), next)?;
6079                    after = Some(next.to_owned());
6080                }
6081                None => break,
6082            }
6083        }
6084        Ok(Board {
6085            id: required_str(&board, "id")?.to_owned(),
6086            fields: board.get("fields").cloned().unwrap_or(Value::Null),
6087            items,
6088        })
6089    }
6090
6091    /// The existing items this source has written, for completing a narrowed read that is
6092    /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
6093    fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
6094        self.updated.lock().map_err(|_| SourceError::Unavailable {
6095            message: "this source's record of what it wrote in this run was left inconsistent \
6096                      by an earlier failure; next: run the command again"
6097                .into(),
6098        })
6099    }
6100
6101    /// The issues this source has commented on in this command; see
6102    /// [`Self::commented`](GitHubProjectsSource::commented).
6103    fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
6104        self.commented.lock().map_err(|_| SourceError::Unavailable {
6105            message: "this source's record of what it commented on in this run was left \
6106                      inconsistent by an earlier failure; next: run the command again"
6107                .into(),
6108        })
6109    }
6110
6111    /// Called only once GitHub has answered the comment write, so an issue whose comment
6112    /// failed is never made a candidate a later read would pay a node read for.
6113    fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
6114        let mut commented = self.commented()?;
6115        if !commented.contains(issue) {
6116            commented.push(issue.clone());
6117        }
6118        Ok(())
6119    }
6120
6121    /// The items this source has created, for completing a board read that is behind.
6122    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
6123        self.created.lock().map_err(|_| SourceError::Unavailable {
6124            message: "this source's record of what it created in this run was left \
6125                      inconsistent by an earlier failure; next: run the command again"
6126                .into(),
6127        })
6128    }
6129
6130    /// One board item as this source reports it, or `None` for content it ignores.
6131    ///
6132    /// A pull request is neither a project nor a task — it is somebody's change, not a
6133    /// unit of plan — and an item whose content the token cannot see has nothing to
6134    /// report at all.
6135    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
6136        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
6137            message: "GitHub project item is missing content".into(),
6138        })?;
6139        if content.is_null() {
6140            return Ok(None);
6141        }
6142        let content_kind = match required_str(content, "__typename")? {
6143            "Issue" => ContentKind::Issue,
6144            "DraftIssue" => ContentKind::DraftIssue,
6145            _ => return Ok(None),
6146        };
6147        let field_values = item
6148            .get("fieldValues")
6149            .ok_or_else(|| SourceError::Malformed {
6150                message: "GitHub project item is missing fieldValues".into(),
6151            })?;
6152        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
6153        let nodes = field_values
6154            .get("nodes")
6155            .and_then(Value::as_array)
6156            .ok_or_else(|| SourceError::Malformed {
6157                message: "GitHub project item fieldValues.nodes is not an array".into(),
6158            })?;
6159        if let Some(labels) = content.get("labels") {
6160            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
6161        }
6162        let raw_body = optional_str(content, "body")?.map(str::to_owned);
6163        let (body, slot) = metadata_body(raw_body.clone())?;
6164        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
6165            .map(|id| NativeId(id.to_owned()));
6166        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
6167        // to read one from; it is a task, and never a project.
6168        let sub_issues = match content_kind {
6169            ContentKind::Issue => sub_issue_total(content)?,
6170            ContentKind::DraftIssue => 0,
6171        };
6172        let content_id = required_str(content, "id")?;
6173        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
6174            message: format!("GitHub issue {content_id}: {message}"),
6175        })?;
6176        let raw_title = required_str(content, "title")?;
6177        // The design prefix is read *first*, before either of the two rules that separate
6178        // a project from a task. A document is not work whatever sub-issues it has and
6179        // whatever marker it carries, and reading the prefix later would make a design
6180        // issue with none of either an empty project.
6181        let kind = if raw_title.starts_with(DESIGN_TITLE_PREFIX) {
6182            BoardKind::Document
6183        } else if parent.is_some() {
6184            // Being a sub-issue wins outright, and no marker overrides it: an issue filed
6185            // under a project is that project's task even when it has sub-issues of its
6186            // own.
6187            BoardKind::Work(ItemKind::Task)
6188        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
6189            BoardKind::Work(ItemKind::Project)
6190        } else {
6191            BoardKind::Work(ItemKind::Task)
6192        };
6193        // The title a person wrote, which for a document is the one without the prefix —
6194        // the same way `content` above is the body without this source's metadata slot.
6195        let title = match kind {
6196            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
6197            BoardKind::Work(_) => raw_title.to_owned(),
6198        };
6199        let own_repository = content
6200            .pointer("/repository/nameWithOwner")
6201            .and_then(Value::as_str)
6202            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
6203            .transpose()
6204            .map_err(|message| SourceError::Malformed { message })?;
6205        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
6206            Repository::from_metadata(&slot)
6207                .map_err(|message| SourceError::Malformed { message })?
6208        } else {
6209            own_repository.clone().into_iter().collect()
6210        };
6211        let classification = Classification::from_metadata(&slot)
6212            .map_err(|message| SourceError::Malformed { message })?;
6213        let id = NativeId(content_id.to_owned());
6214        // Read only for a task, because only a task has either list: a project or a
6215        // document holding one of these keys holds nothing this source reports, and the
6216        // keys are left out of its caller-visible metadata all the same.
6217        let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
6218            let listed = |key: &str| {
6219                TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
6220                    .map_err(|message| SourceError::Malformed { message })
6221            };
6222            (
6223                listed(TaskRef::DELIVERS_KEY)?,
6224                listed(TaskRef::DELIVERED_BY_KEY)?,
6225            )
6226        } else {
6227            (Vec::new(), Vec::new())
6228        };
6229        let (option, closed, reason) = Self::status_parts(nodes, content)?;
6230        let priority = self.held_priority(nodes)?;
6231        // Present when the item was reached through its own issue, whose board entry
6232        // names the board; a read of the board's own items has the board already. An
6233        // empty id names nothing a field write could address, so it is read as absent and
6234        // the write goes back to reading the board.
6235        let board_id = item
6236            .pointer("/project/id")
6237            .and_then(Value::as_str)
6238            .filter(|id| !id.is_empty());
6239        let resolved = Resolved {
6240            item_id: required_str(item, "id")?.to_owned(),
6241            id,
6242            content_kind,
6243            kind,
6244            title,
6245            body: body.filter(|value| !value.is_empty()),
6246            raw_body,
6247            status: self
6248                .statuses
6249                .status(kind.status_kind(), option, closed, reason),
6250            option: option.map(str::to_owned),
6251            priority,
6252            closed,
6253            delivers,
6254            delivered_by,
6255            labels: labels(content)?,
6256            parent,
6257            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
6258            projected: self.held_projections(nodes)?,
6259            number: match content_kind {
6260                ContentKind::Issue => Some(issue_number(content)?),
6261                // A draft is filed in no repository, so nothing ever numbered it:
6262                // `DraftIssue` declares no `number` at all, exactly as it declares no
6263                // `subIssuesSummary` the branch above reads.
6264                ContentKind::DraftIssue => None,
6265            },
6266            url: optional_str(content, "url")?.map(str::to_owned),
6267            created_at: optional_time(content, "createdAt")?,
6268            updated_at: optional_time(content, "updatedAt")?,
6269            item_updated_at: optional_time(item, "updatedAt")?,
6270            comment_count: content
6271                .pointer("/commentCount/totalCount")
6272                .and_then(Value::as_u64),
6273            relationship_events: content
6274                .pointer("/relationshipEvents/totalCount")
6275                .and_then(Value::as_u64),
6276            // The issue node is stamped wherever it was read; a board item a read of the issue
6277            // put together from its memberships is not.
6278            served_at: match optional_time(content, SERVED_AT)? {
6279                Some(served) => Some(served),
6280                None => optional_time(item, SERVED_AT)?,
6281            },
6282            own_repository,
6283            repositories,
6284            classification,
6285            slot,
6286            board_id: board_id.map(str::to_owned),
6287            fields: field_definitions(nodes),
6288            board_fields: Self::carried_board_fields(content, board_id)?,
6289            blocked_by: carried_blocked_by(content)?,
6290        };
6291        self.resolved_cache()?
6292            .insert(resolved.id.clone(), resolved.clone());
6293        Ok(Some(resolved))
6294    }
6295
6296    /// The field definitions of the board `board_id` names — the project this issue's own
6297    /// board item is on — off the `boards` page a read of an issue by its own id carries, or
6298    /// `None` when the read carried none, carried no entry for that board, or the board item
6299    /// named no board, which a write then answers by reading the board's fields itself.
6300    ///
6301    /// Matched by the board's node id and never by its number alone: a project number is
6302    /// unique only within its owner, so another owner's board numbered alike can sit on the
6303    /// same page, and its field and option ids address nothing on this one.
6304    fn carried_board_fields(
6305        content: &Value,
6306        board_id: Option<&str>,
6307    ) -> Result<Option<Value>, SourceError> {
6308        let (Some(nodes), Some(board_id)) = (
6309            content.pointer("/boards/nodes").and_then(Value::as_array),
6310            board_id,
6311        ) else {
6312            return Ok(None);
6313        };
6314        let Some(board) = nodes.iter().find_map(|node| {
6315            let project = node.get("project")?;
6316            (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
6317        }) else {
6318            return Ok(None);
6319        };
6320        let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
6321            return Ok(None);
6322        };
6323        complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
6324        Ok(Some(fields.clone()))
6325    }
6326
6327    /// What one board item's `Priority` field says, through this instance's mapping.
6328    ///
6329    /// An instance with no mapping holds no priority, so every item reads as `none` whatever
6330    /// its board holds. With one, no value is `none`, a mapped option is its level, and an
6331    /// option the mapping does not name is kept as itself — never read as a level or as
6332    /// `none` — for a read of the task to report by name.
6333    fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
6334        let Some(mapping) = &self.priorities else {
6335            return Ok(HeldPriority::Read(Priority::None));
6336        };
6337        // A value of the field that names no option — a text field someone called `Priority` —
6338        // is malformed rather than `none`: reading it as no priority would let the next copy
6339        // clear one a person set.
6340        let Some(option) = field_values
6341            .iter()
6342            .find(|value| {
6343                value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
6344            })
6345            .map(|value| required_str(value, "name"))
6346            .transpose()?
6347        else {
6348            return Ok(HeldPriority::Read(Priority::None));
6349        };
6350        Ok(mapping.priority_of(option).map_or_else(
6351            || HeldPriority::Unmapped(option.to_owned()),
6352            HeldPriority::Read,
6353        ))
6354    }
6355
6356    /// What each projected metadata field holds for one board item, by field name — every text
6357    /// value GitHub answers, an empty one included.
6358    fn held_projections(
6359        &self,
6360        field_values: &[Value],
6361    ) -> Result<BTreeMap<String, String>, SourceError> {
6362        let mut held = BTreeMap::new();
6363        for projection in &self.metadata_fields {
6364            if let Some(text) = text_field(field_values, &projection.field)? {
6365                held.insert(projection.field.to_string(), text);
6366            }
6367        }
6368        Ok(held)
6369    }
6370
6371    /// What one board item's status is read from: its `Status` option, whether its issue
6372    /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
6373    /// three into the status it reports.
6374    fn status_parts<'a>(
6375        field_values: &'a [Value],
6376        content: &'a Value,
6377    ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
6378        let option = field_values
6379            .iter()
6380            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
6381            .map(|value| required_str(value, "name"))
6382            .transpose()?;
6383        let closed = optional_str(content, "state")? == Some("CLOSED");
6384        Ok((option, closed, optional_str(content, "stateReason")?))
6385    }
6386
6387    /// The board Status option this write selects, or the refusal that says why not.
6388    ///
6389    /// The mapped option is required for both open and terminal targets. A terminal write
6390    /// validates it before changing either representation, so it can never fall back to
6391    /// closing an issue whose board cannot display the matching status.
6392    ///
6393    /// Answers the field's id, the option's id, and the option's name as the board spells
6394    /// it — which is the name a read of the item reports once it sits there.
6395    fn column_for(
6396        &self,
6397        fields: &Value,
6398        kind: ItemKind,
6399        category: StatusCategory,
6400        target: &StatusTarget,
6401    ) -> Result<Option<(String, String, String)>, SourceError> {
6402        let Some(wanted) = target.option() else {
6403            return Ok(None);
6404        };
6405        let missing = |detail: &str| SourceError::Refused {
6406            message: format!(
6407                "{} status {} of source {} needs the board Status option {wanted:?}, and \
6408                 {detail}; next: add that option to the board, which `onetaskgraph sources \
6409                 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
6410                 it has",
6411                kind.marker(),
6412                category_name(category),
6413                self.name,
6414                self.name,
6415                category_name(category),
6416                kind.marker()
6417            ),
6418        };
6419        let Some(field) = Board::field(fields, "Status")? else {
6420            return Err(missing("this board has no Status field"));
6421        };
6422        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
6423            return Err(missing(
6424                "this board's Status field is not a single-select field",
6425            ));
6426        }
6427        let option = field
6428            .get("options")
6429            .and_then(Value::as_array)
6430            .and_then(|options| {
6431                options.iter().find(|option| {
6432                    option
6433                        .get("name")
6434                        .and_then(Value::as_str)
6435                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
6436                })
6437            });
6438        match option {
6439            None => Err(missing("this board does not have it")),
6440            Some(option) => Ok(Some((
6441                required_str(field, "id")?.to_owned(),
6442                required_str(option, "id")?.to_owned(),
6443                required_str(option, "name")?.to_owned(),
6444            ))),
6445        }
6446    }
6447
6448    /// The refusal a status that closes an issue is answered with over a board draft.
6449    fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
6450        SourceError::Refused {
6451            message: format!(
6452                "status {} of source {} closes the item's issue, and GitHub draft items have \
6453                 no open or closed state",
6454                category_name(category),
6455                self.name
6456            ),
6457        }
6458    }
6459
6460    /// What a status write to one item needs of the board: the board's id and the
6461    /// definition of its `Status` field, read off the item when the item says both.
6462    ///
6463    /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
6464    /// and its `Status` value carries that field's definition, options and all. An item that
6465    /// does not say — no board id, or no `Status` value to read the field off — takes them
6466    /// from [`Self::board_fields`], which reads no item.
6467    async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
6468        if let Some(board) = item.carried_board() {
6469            return Ok(board);
6470        }
6471        if item.defines("Status")
6472            && let Some(board_id) = item.named_board()
6473        {
6474            return Ok(BoardFields {
6475                id: board_id,
6476                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6477            });
6478        }
6479        self.board_fields().await
6480    }
6481
6482    /// Set one task's status and nothing else; see [`TaskSource::set_task_status`].
6483    async fn set_status(
6484        &self,
6485        id: &NativeId,
6486        category: StatusCategory,
6487    ) -> Result<Option<Status>, SourceError> {
6488        // Refused before anything is read, in the words a write of the same status is.
6489        let target = self.resolved_target(ItemKind::Task, category)?;
6490        let Some(mut item) = self
6491            .bound_item(id)
6492            .await?
6493            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6494        else {
6495            return Ok(None);
6496        };
6497        let board = self.status_board(&item).await?;
6498        let (field, option, name) = self
6499            .column_for(&board.fields, ItemKind::Task, category, &target)?
6500            .ok_or_else(|| SourceError::Malformed {
6501                message: format!(
6502                    "status {} of source {} names no board Status option",
6503                    category_name(category),
6504                    self.name
6505                ),
6506            })?;
6507        if item.status.category == category && item.option.as_deref() == Some(&name) {
6508            return Ok(Some(item.status));
6509        }
6510        match &target {
6511            StatusTarget::Terminal(_, reason) => {
6512                if item.content_kind == ContentKind::DraftIssue {
6513                    return Err(self.closes_a_draft(category));
6514                }
6515                self.set_item_field(
6516                    board.id.as_str(),
6517                    &item.item_id,
6518                    &field,
6519                    json!({"singleSelectOptionId": option}),
6520                )
6521                .await?;
6522                self.update_content(
6523                    ContentKind::Issue,
6524                    &item.id,
6525                    json!({"stateInput": state_input(Some(&target))}),
6526                )
6527                .await?;
6528                item.closed = true;
6529                item.status =
6530                    self.statuses
6531                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()));
6532                item.option = Some(name);
6533            }
6534            StatusTarget::Column(_) => {
6535                // An option is what an open item's status is, so a closed issue is reopened
6536                // first — sitting closed in the column, it would read back as closed. A draft has
6537                // no state to reopen.
6538                if item.content_kind == ContentKind::Issue && item.closed {
6539                    self.update_content(
6540                        ContentKind::Issue,
6541                        &item.id,
6542                        json!({"stateInput": state_input(Some(&target))}),
6543                    )
6544                    .await?;
6545                    item.closed = false;
6546                }
6547                self.set_item_field(
6548                    board.id.as_str(),
6549                    &item.item_id,
6550                    &field,
6551                    json!({"singleSelectOptionId": option}),
6552                )
6553                .await?;
6554                item.status = self
6555                    .statuses
6556                    .status(ItemKind::Task, Some(&name), false, None);
6557                item.option = Some(name);
6558            }
6559            StatusTarget::Disabled(_) => {
6560                unreachable!("resolved_target refused a disabled status")
6561            }
6562        }
6563        let status = item.status.clone();
6564        self.remember_written(item, false)?;
6565        Ok(Some(status))
6566    }
6567
6568    /// Replace one task's `delivered_by` and nothing else; see
6569    /// [`TaskSource::set_delivered_by`].
6570    ///
6571    /// One update of the body, which differs from the body GitHub holds only inside the
6572    /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
6573    async fn replace_delivered_by(
6574        &self,
6575        id: &NativeId,
6576        delivered_by: &[TaskRef],
6577    ) -> Result<Option<()>, SourceError> {
6578        let entries = TaskRef::listed(
6579            TaskRef::DELIVERED_BY_KEY,
6580            id,
6581            Some(&self.name),
6582            delivered_by.to_vec(),
6583        )
6584        .map_err(|message| SourceError::Refused { message })?;
6585        let Some(mut item) = self
6586            .bound_item(id)
6587            .await?
6588            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6589        else {
6590            return Ok(None);
6591        };
6592        let mut slot = item.slot.clone();
6593        set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
6594        self.write_slot(&mut item, &slot).await?;
6595        item.delivered_by = entries;
6596        self.remember_written(item, false)?;
6597        Ok(Some(()))
6598    }
6599
6600    /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
6601    /// see [`TaskSource::set_task_metadata`].
6602    ///
6603    /// `None` when this board holds no item by that id, or holds one of another kind. The
6604    /// answer is the item as this source now reads it, so what a caller is told the key
6605    /// holds is what the slot holds.
6606    ///
6607    /// A key already holding the value is answered without a write, compared as JSON rather
6608    /// than as the body's bytes: a slot a person spelled with other whitespace would
6609    /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
6610    async fn set_slot_key(
6611        &self,
6612        id: &NativeId,
6613        kind: BoardKind,
6614        key: &MetadataKey,
6615        value: &Value,
6616    ) -> Result<Option<Resolved>, SourceError> {
6617        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
6618            return Ok(None);
6619        };
6620        if item.slot.get(key.as_str()) == Some(value) {
6621            return Ok(Some(item));
6622        }
6623        let mut slot = item.slot.clone();
6624        slot.insert(key.as_str().to_owned(), value.clone());
6625        // A key a board text field projects moves that field first, so the body — the value's
6626        // one authoritative home — is written last, as every write of an existing item is.
6627        if self
6628            .metadata_fields
6629            .iter()
6630            .any(|projection| &*projection.key == key.as_str())
6631        {
6632            let board = self.projection_board(&item).await?;
6633            let (writes, projected) = self.projection_writes(
6634                &board.fields,
6635                Some(&item.projected),
6636                &slot,
6637                Some(&[key.as_str()]),
6638            )?;
6639            let (mut values, mut clears) = (Vec::new(), Vec::new());
6640            for write in &writes {
6641                write.join(&mut values, &mut clears);
6642            }
6643            self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
6644                .await?;
6645            item.projected = projected;
6646        }
6647        self.write_slot(&mut item, &slot).await?;
6648        self.remember_written(item.clone(), false)?;
6649        Ok(Some(item))
6650    }
6651
6652    /// What a projection write to `item` needs of the board, read off the item when it says
6653    /// enough, as [`Self::fields_for`] reads it.
6654    async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
6655        if let Some(board) = item.carried_board() {
6656            return Ok(board);
6657        }
6658        if let Some(board_id) = item.named_board()
6659            && self
6660                .metadata_fields
6661                .iter()
6662                .all(|projection| item.defines(&projection.field))
6663        {
6664            return Ok(BoardFields {
6665                id: board_id,
6666                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6667            });
6668        }
6669        self.board_fields().await
6670    }
6671
6672    /// Put `slot` in one item's metadata slot with a single update of its body, and bring
6673    /// `item` up to what that write left.
6674    ///
6675    /// The body sent differs from the body GitHub holds only inside the slot — see
6676    /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
6677    /// the mutation the item's content takes, so a board draft's body is written with
6678    /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
6679    async fn write_slot(
6680        &self,
6681        item: &mut Resolved,
6682        slot: &BTreeMap<String, Value>,
6683    ) -> Result<(), SourceError> {
6684        let held = item.raw_body.clone().unwrap_or_default();
6685        let body = with_slot(&held, slot)?;
6686        if body != held {
6687            self.update_content(item.content_kind, &item.id, json!({"body": body}))
6688                .await?;
6689        }
6690        let (visible, slot) = metadata_body(Some(body.clone()))?;
6691        item.body = visible.filter(|value| !value.is_empty());
6692        item.raw_body = Some(body);
6693        item.slot = slot;
6694        Ok(())
6695    }
6696
6697    /// This instance's target for a category written to an item of `kind`, refusing one
6698    /// that kind has no option for — before anything is read or written.
6699    ///
6700    /// Nothing here mutates the board's option set to make room for a status. GitHub
6701    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
6702    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
6703    /// field and every item's status.
6704    fn resolved_target(
6705        &self,
6706        kind: ItemKind,
6707        category: StatusCategory,
6708    ) -> Result<StatusTarget, SourceError> {
6709        let target = self.statuses.target(kind, category).clone();
6710        let StatusTarget::Disabled(why) = target else {
6711            return Ok(target);
6712        };
6713        let refusal = why.refusal(&self.name, category, kind);
6714        // Why there is no shipped default, which is the question a person meeting this
6715        // refusal on a source that never mentioned the category asks.
6716        let shipped_none = match category {
6717            StatusCategory::Draft => Some(
6718                "draft has no shipped default because GitHub draft issues cannot have \
6719                 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
6720            ),
6721            StatusCategory::Unknown => Some(
6722                "unknown has no shipped default because this board keeps no open-ended status \
6723                 word: every word classified unknown is written to the one board Status option \
6724                 status_mapping.unknown names",
6725            ),
6726            _ => None,
6727        };
6728        Err(match (refusal, shipped_none, why) {
6729            (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
6730                SourceError::Refused {
6731                    message: format!("{message}; {note}"),
6732                }
6733            }
6734            (refusal, _, _) => refusal,
6735        })
6736    }
6737
6738    /// What writing `priority` does to one item's `Priority` field on this board, or the
6739    /// refusal naming what the board lacks.
6740    ///
6741    /// `none` is no value, so it clears the field — and asks nothing of an item that holds
6742    /// none already, or of an item not created yet. Every other priority selects the option
6743    /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
6744    /// without that option, is refused rather than given one: reads and writes never create
6745    /// a field or an option.
6746    fn priority_write(
6747        &self,
6748        fields: &Value,
6749        existing: Option<&Resolved>,
6750        priority: Priority,
6751    ) -> Result<Option<PriorityWrite>, SourceError> {
6752        let Some(mapping) = &self.priorities else {
6753            return Err(self.holds_no_priority());
6754        };
6755        let Some(wanted) = mapping.option(priority) else {
6756            if !existing.is_some_and(Resolved::holds_priority) {
6757                return Ok(None);
6758            }
6759            let field =
6760                Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
6761                    message: format!(
6762                        "an item holding a {PRIORITY_FIELD} value was read without that field"
6763                    ),
6764                })?;
6765            return Ok(Some(PriorityWrite::Clear {
6766                field: required_str(field, "id")?.to_owned(),
6767            }));
6768        };
6769        let missing = |detail: &str| SourceError::Refused {
6770            message: format!(
6771                "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
6772                 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
6773                 it, or point priority_mapping.{priority} of this source at an option the board \
6774                 has",
6775                self.name, self.name
6776            ),
6777        };
6778        let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
6779            return Err(missing(&format!(
6780                "this board has no {PRIORITY_FIELD} field"
6781            )));
6782        };
6783        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
6784            return Err(missing(&format!(
6785                "this board's {PRIORITY_FIELD} field is not a single-select field"
6786            )));
6787        }
6788        // An options list that is absent or not a list is an answer this source cannot read,
6789        // not a board lacking the option: `sources fields --apply` is no remedy for it.
6790        let option = field
6791            .get("options")
6792            .and_then(Value::as_array)
6793            .ok_or_else(|| SourceError::Malformed {
6794                message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
6795            })?
6796            .iter()
6797            .find(|option| {
6798                option
6799                    .get("name")
6800                    .and_then(Value::as_str)
6801                    .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
6802            })
6803            .ok_or_else(|| missing("this board does not have it"))?;
6804        Ok(Some(PriorityWrite::Select {
6805            field: required_str(field, "id")?.to_owned(),
6806            option: required_str(option, "id")?.to_owned(),
6807        }))
6808    }
6809
6810    /// What writing `metadata` does to each projected text field of one item, and what those
6811    /// fields hold after it — or the refusal, before anything is sent.
6812    ///
6813    /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
6814    /// narrows the work to the entries projecting one of those metadata keys, for a write that
6815    /// touches only them; `None` is every entry. A string the field already holds, and nothing
6816    /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
6817    /// and so is a board without the field, or with a field of that name that is not a text
6818    /// field: reads and writes never create a field.
6819    fn projection_writes(
6820        &self,
6821        fields: &Value,
6822        held: Option<&BTreeMap<String, String>>,
6823        metadata: &BTreeMap<String, Value>,
6824        keys: Option<&[&str]>,
6825    ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
6826        let mut projected = held.cloned().unwrap_or_default();
6827        let mut writes = Vec::new();
6828        for projection in self
6829            .metadata_fields
6830            .iter()
6831            .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
6832        {
6833            let wanted = projection.projected(metadata, &self.name)?;
6834            let field = self.projection_field(fields, projection)?;
6835            let holding = projected.get(&*projection.field);
6836            match wanted {
6837                Projected::Text(text) if holding != Some(&text) => {
6838                    writes.push(ProjectionWrite::Set {
6839                        field,
6840                        text: text.clone(),
6841                    });
6842                    projected.insert(projection.field.to_string(), text);
6843                }
6844                Projected::Nothing if holding.is_some() => {
6845                    writes.push(ProjectionWrite::Clear { field });
6846                    projected.remove(&*projection.field);
6847                }
6848                Projected::Text(_) | Projected::Nothing => {}
6849            }
6850        }
6851        Ok((writes, projected))
6852    }
6853
6854    /// The id of the board text field one projection writes, or the refusal naming what the
6855    /// board has instead.
6856    fn projection_field(
6857        &self,
6858        fields: &Value,
6859        projection: &MetadataField,
6860    ) -> Result<String, SourceError> {
6861        let refuse = |detail: String| SourceError::Refused {
6862            message: format!(
6863                "source {} projects metadata key {:?} onto the board text field {:?}, and \
6864                 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
6865                 text field, or name another field in metadata_fields",
6866                self.name, &*projection.key, &*projection.field, self.name
6867            ),
6868        };
6869        let Some(field) = Board::field(fields, &projection.field)? else {
6870            return Err(refuse("this board has no field of that name".to_owned()));
6871        };
6872        let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
6873        let data_type = optional_str(field, "dataType")?;
6874        if typename != "ProjectV2Field" || data_type != Some("TEXT") {
6875            return Err(refuse(format!(
6876                "this board's field of that name is not a text field (it is a {}), so it is \
6877                 left as it is; rename or remove that field first",
6878                data_type.unwrap_or(typename)
6879            )));
6880        }
6881        Ok(required_str(field, "id")?.to_owned())
6882    }
6883
6884    /// Apply one priority write to one board item.
6885    async fn write_priority(
6886        &self,
6887        board_id: &str,
6888        item_id: &str,
6889        write: &PriorityWrite,
6890    ) -> Result<(), SourceError> {
6891        match write {
6892            PriorityWrite::Select { field, option } => {
6893                self.set_item_field(
6894                    board_id,
6895                    item_id,
6896                    field,
6897                    json!({"singleSelectOptionId": option}),
6898                )
6899                .await
6900            }
6901            PriorityWrite::Clear { field } => {
6902                let data = self
6903                    .graphql(
6904                        graphql::CLEAR_FIELD,
6905                        json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
6906                            "readPriority":false,"priorityName":PRIORITY_FIELD}),
6907                    )
6908                    .await?;
6909                let returned = data
6910                    .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
6911                    .ok_or_else(|| SourceError::Malformed {
6912                        message: "GitHub field clear returned no project item".into(),
6913                    })?;
6914                if required_str(returned, "id")? != item_id {
6915                    return Err(SourceError::Malformed {
6916                        message: "GitHub field clear returned the wrong project item".into(),
6917                    });
6918                }
6919                Ok(())
6920            }
6921        }
6922    }
6923
6924    /// The refusal a priority is answered with by an instance configured with no
6925    /// `priority_mapping`, which holds none.
6926    fn holds_no_priority(&self) -> SourceError {
6927        SourceError::Refused {
6928            message: format!(
6929                "source {} holds no task priority: its configuration sets no priority_mapping; \
6930                 next: set priority_mapping on this source, then run `onetaskgraph sources \
6931                 fields {} --apply` to set its board up",
6932                self.name, self.name
6933            ),
6934        }
6935    }
6936
6937    /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
6938    ///
6939    /// One field write — a select, or a clear for `none` — and no title, body, label, state
6940    /// or `Status` request. Clearing a priority an item does not hold sends nothing.
6941    async fn set_priority(
6942        &self,
6943        id: &NativeId,
6944        priority: Priority,
6945    ) -> Result<Option<Priority>, SourceError> {
6946        if self.priorities.is_none() {
6947            return Err(self.holds_no_priority());
6948        }
6949        let Some(mut item) = self
6950            .bound_item(id)
6951            .await?
6952            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6953        else {
6954            return Ok(None);
6955        };
6956        if priority == Priority::None && !item.holds_priority() {
6957            return Ok(Some(priority));
6958        }
6959        // The item's own read carries the field's definition whenever it holds a value of
6960        // it, which a clear always does; a select onto an item holding none reads the board.
6961        let board = match (item.carried_board(), item.named_board()) {
6962            (Some(board), _) => board,
6963            (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
6964                id,
6965                fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
6966            },
6967            _ => self.board_fields().await?,
6968        };
6969        let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
6970            return Ok(Some(priority));
6971        };
6972        let (document, root, input) = match write {
6973            PriorityWrite::Select { field, option } => (
6974                graphql::UPDATE_FIELD,
6975                "updateProjectV2ItemFieldValue",
6976                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
6977            ),
6978            PriorityWrite::Clear { field } => (
6979                graphql::CLEAR_FIELD,
6980                "clearProjectV2ItemFieldValue",
6981                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
6982            ),
6983        };
6984        let data = self
6985            .graphql(
6986                document,
6987                json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
6988            )
6989            .await?;
6990        let returned = data
6991            .get(root)
6992            .and_then(|value| value.get("projectV2Item"))
6993            .ok_or_else(|| SourceError::Malformed {
6994                message: "GitHub priority write returned no project item".into(),
6995            })?;
6996        if required_str(returned, "id")? != item.item_id {
6997            return Err(SourceError::Malformed {
6998                message: "GitHub priority write returned the wrong project item".into(),
6999            });
7000        }
7001        let value = returned
7002            .get("fieldValueByName")
7003            .ok_or_else(|| SourceError::Malformed {
7004                message: "GitHub priority write returned no priority read-back".into(),
7005            })?;
7006        if !value.is_null()
7007            && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
7008        {
7009            return Err(SourceError::Malformed {
7010                message: "GitHub priority read-back is not a Priority field value".into(),
7011            });
7012        }
7013        let values = if value.is_null() {
7014            Vec::new()
7015        } else {
7016            vec![value.clone()]
7017        };
7018        item.priority = self.held_priority(&values)?;
7019        let answer = item.task()?.priority;
7020        self.remember_written(item, false)?;
7021        Ok(Some(answer))
7022    }
7023
7024    /// Replace one task's visible body and nothing else; see
7025    /// [`TaskSource::set_task_content`].
7026    ///
7027    /// One update of the body, which differs from the body GitHub holds only outside the
7028    /// metadata slot — the slot is kept byte for byte, so every caller key and every list
7029    /// this source keeps there reads back as it was. A body that would not change is not
7030    /// sent at all.
7031    async fn replace_content(
7032        &self,
7033        id: &NativeId,
7034        content: &str,
7035    ) -> Result<Option<()>, SourceError> {
7036        let Some(mut item) = self
7037            .bound_item(id)
7038            .await?
7039            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7040        else {
7041            return Ok(None);
7042        };
7043        let held = item.raw_body.clone().unwrap_or_default();
7044        let body = with_content(&held, content)?;
7045        // Checked before anything is sent: content ending in what this source reads as its own
7046        // metadata slot would read back as metadata rather than as the content it was.
7047        let (visible, slot) = metadata_body(Some(body.clone()))?;
7048        if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
7049            return Err(SourceError::Refused {
7050                message: format!(
7051                    "this content ends in what source {} reads as its own metadata slot \
7052                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7053                     as content; next: remove that trailing block from the content",
7054                    self.name
7055                ),
7056            });
7057        }
7058        if body != held {
7059            self.update_content(item.content_kind, &item.id, json!({"body": body}))
7060                .await?;
7061        }
7062        item.body = visible.filter(|value| !value.is_empty());
7063        item.raw_body = Some(body);
7064        item.slot = slot;
7065        self.remember_written(item, false)?;
7066        Ok(Some(()))
7067    }
7068
7069    /// Apply one targeted update to one task; see [`TaskSource::update_task`].
7070    ///
7071    /// One read of the item — which carries the board's field definitions and the issue's
7072    /// `blockedBy`, so neither is read again — and then only what differs from it: the
7073    /// `Status` option and the `Priority` field together in one request, the `blockedBy`
7074    /// additions and removals the named edges differ by, and last one `updateIssue` carrying
7075    /// the title, the body — visible content and metadata slot together — and a state change.
7076    /// So an update naming any of title, body, metadata, status and priority is one read and
7077    /// at most two writes. The body goes last so that a write refused part-way leaves it, and
7078    /// the metadata in it, as it stood. A terminal status selects its option and then closes,
7079    /// as a whole write does; an open one selects its option and then reopens. The origin
7080    /// field is never written: an update is of an item that already exists, whose origin is
7081    /// what it is.
7082    ///
7083    /// The task answered is the item as those writes left it, built from the read and what was
7084    /// sent rather than read again — the same record a later read in this run answers from.
7085    async fn targeted_update(
7086        &self,
7087        id: &NativeId,
7088        update: &TaskUpdate,
7089    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
7090        // Everything this source can refuse without reading the item is refused first, in the
7091        // words a whole write of the same fields is refused with.
7092        update.consistent()?;
7093        if update
7094            .title
7095            .as_deref()
7096            .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
7097        {
7098            return Err(SourceError::Refused {
7099                message: format!(
7100                    "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
7101                     spells a document, so it would read back as one rather than as a task; \
7102                     retitle it",
7103                    self.name
7104                ),
7105            });
7106        }
7107        if let Some(delivers) = &update.delivers {
7108            TaskRef::listed(
7109                TaskRef::DELIVERS_KEY,
7110                id,
7111                Some(&self.name),
7112                delivers.clone(),
7113            )
7114            .map_err(|message| SourceError::Refused { message })?;
7115        }
7116        if self.priorities.is_none()
7117            && update
7118                .priority
7119                .is_some_and(|priority| priority != Priority::None)
7120        {
7121            return Err(self.holds_no_priority());
7122        }
7123        let target = update
7124            .status
7125            .as_ref()
7126            .map(|status| self.resolved_target(ItemKind::Task, status.category))
7127            .transpose()?;
7128        let Some(mut item) = self
7129            .bound_item(id)
7130            .await?
7131            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7132        else {
7133            return Ok(None);
7134        };
7135        let before = item.task()?;
7136
7137        let mut status_move = None;
7138        if let (Some(status), Some(target)) = (&update.status, target) {
7139            let board = self.status_board(&item).await?;
7140            let (field, option, name) = self
7141                .column_for(&board.fields, ItemKind::Task, status.category, &target)?
7142                .ok_or_else(|| SourceError::Malformed {
7143                    message: format!(
7144                        "status {} of source {} names no board Status option",
7145                        category_name(status.category),
7146                        self.name
7147                    ),
7148                })?;
7149            let terminal = matches!(target, StatusTarget::Terminal(_, _));
7150            if terminal && item.content_kind == ContentKind::DraftIssue {
7151                return Err(self.closes_a_draft(status.category));
7152            }
7153            let landed = match &target {
7154                StatusTarget::Terminal(_, reason) => {
7155                    self.statuses
7156                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
7157                }
7158                _ => self
7159                    .statuses
7160                    .status(ItemKind::Task, Some(&name), false, None),
7161            };
7162            let option_moves = item
7163                .option
7164                .as_deref()
7165                .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
7166            let state_moves = item.content_kind == ContentKind::Issue
7167                && (item.closed != terminal || (terminal && item.status != landed));
7168            if let Some(moves) = Moves::of(option_moves, state_moves) {
7169                status_move = Some(StatusMove {
7170                    board: board.id,
7171                    field,
7172                    option,
7173                    name,
7174                    target,
7175                    landed,
7176                    moves,
7177                });
7178            }
7179        }
7180
7181        let mut priority_move = None;
7182        if let Some(priority) = update.priority
7183            && self.priorities.is_some()
7184            && item.priority != HeldPriority::Read(priority)
7185        {
7186            let board = match (item.carried_board(), item.named_board()) {
7187                (Some(board), _) => board,
7188                (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
7189                    id: board,
7190                    fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7191                },
7192                _ => self.board_fields().await?,
7193            };
7194            if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
7195                priority_move = Some((board.id, write, priority));
7196            }
7197        }
7198
7199        // The metadata keys this update names that a board text field projects, and what
7200        // that does to those fields — refused, as a whole write refuses it, before anything
7201        // is sent.
7202        let touched: Vec<&str> = update
7203            .metadata_set
7204            .keys()
7205            .chain(update.metadata_remove.iter())
7206            .map(MetadataKey::as_str)
7207            .collect();
7208        let mut projection_move = None;
7209        if self
7210            .metadata_fields
7211            .iter()
7212            .any(|projection| touched.contains(&&*projection.key))
7213        {
7214            let mut metadata = item.slot.clone();
7215            for (key, value) in &update.metadata_set {
7216                metadata.insert(key.as_str().to_owned(), value.clone());
7217            }
7218            for key in &update.metadata_remove {
7219                metadata.remove(key.as_str());
7220            }
7221            let board = self.projection_board(&item).await?;
7222            let (writes, projected) = self.projection_writes(
7223                &board.fields,
7224                Some(&item.projected),
7225                &metadata,
7226                Some(&touched),
7227            )?;
7228            projection_move = Some((board.id, writes, projected));
7229        }
7230
7231        // Resolved before the body is composed, because a far end `blockedBy` cannot name is
7232        // recorded in the slot, and the slot travels in the one body update below.
7233        let edges = match &update.depends_on {
7234            Some(edges) => Some(
7235                self.partition_edges(
7236                    BoardKind::Work(ItemKind::Task),
7237                    item.content_kind,
7238                    item.blocked_by.as_deref(),
7239                    edges,
7240                )
7241                .await?,
7242            ),
7243            None => None,
7244        };
7245
7246        let mut slot = item.slot.clone();
7247        for (key, value) in &update.metadata_set {
7248            slot.insert(key.as_str().to_owned(), value.clone());
7249        }
7250        for key in &update.metadata_remove {
7251            slot.remove(key.as_str());
7252        }
7253        if let Some(delivers) = &update.delivers {
7254            set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
7255        }
7256        if let Some((_, recorded)) = &edges {
7257            record_edges(&mut slot, recorded);
7258        }
7259        let held = item.raw_body.clone().unwrap_or_default();
7260        let content = match &update.content {
7261            Some(content) => with_content(&held, content)?,
7262            None => held.clone(),
7263        };
7264        // A slot holding what it held is kept byte for byte, compared as JSON rather than as
7265        // the body's bytes, as a metadata write compares it: a slot a person spelled with
7266        // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
7267        let body = if slot == item.slot {
7268            content
7269        } else {
7270            with_slot(&content, &slot)?
7271        };
7272        // Checked before anything is sent, as a content write checks it: content ending in
7273        // what this source reads as its own slot would read back as metadata.
7274        let (visible, read) = metadata_body(Some(body.clone()))?;
7275        let wanted = update.content.as_deref().or(item.body.as_deref());
7276        if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
7277            return Err(SourceError::Refused {
7278                message: format!(
7279                    "this content ends in what source {} reads as its own metadata slot \
7280                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7281                     as content; next: remove that trailing block from the content",
7282                    self.name
7283                ),
7284            });
7285        }
7286        let recorded_moves =
7287            slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
7288
7289        // One `updateIssue` carries all three, because every mutation spends the secondary
7290        // limiter and the title, body and state are one mutation's inputs.
7291        let mut fields = serde_json::Map::new();
7292        if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
7293            fields.insert("title".to_owned(), json!(title));
7294        }
7295        if body != held {
7296            fields.insert("body".to_owned(), json!(body));
7297        }
7298        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
7299            fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
7300        }
7301        // **The body is written last, and that is the guarantee a refusal part-way keeps.**
7302        // GitHub runs no two requests as one, and runs one document's mutation fields in order
7303        // without undoing an earlier field when a later one fails — so a body written before a
7304        // board field the board then refused would be left changed. Written after every other
7305        // write has landed, a refusal anywhere leaves the item's body, and every metadata key
7306        // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
7307        // first, together in one request — a terminal option selected before the issue
7308        // closes, as a whole write does — then the `blockedBy` difference, then the body.
7309        let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
7310        let mut clears: Vec<(&BoardId, &str)> = Vec::new();
7311        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
7312            board_writes.push((
7313                &moving.board,
7314                (
7315                    moving.field.clone(),
7316                    json!({"singleSelectOptionId": moving.option}),
7317                ),
7318            ));
7319        }
7320        match &priority_move {
7321            Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
7322                board,
7323                (field.clone(), json!({"singleSelectOptionId": option})),
7324            )),
7325            Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
7326            None => {}
7327        }
7328        if let Some((board, writes, _)) = &projection_move {
7329            for write in writes {
7330                let (mut values, mut cleared) = (Vec::new(), Vec::new());
7331                write.join(&mut values, &mut cleared);
7332                board_writes.extend(values.into_iter().map(|value| (board, value)));
7333                clears.extend(cleared.into_iter().map(|field| (board, field)));
7334            }
7335        }
7336        let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
7337        boards.extend(clears.iter().map(|(board, _)| *board));
7338        boards.dedup_by(|one, other| one.as_str() == other.as_str());
7339        for board in boards {
7340            let writes = board_writes
7341                .iter()
7342                .filter(|(on, _)| on.as_str() == board.as_str())
7343                .map(|(_, write)| write.clone())
7344                .collect::<Vec<_>>();
7345            let cleared = clears
7346                .iter()
7347                .filter(|(on, _)| on.as_str() == board.as_str())
7348                .map(|(_, field)| *field)
7349                .collect::<Vec<_>>();
7350            self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
7351                .await?;
7352        }
7353        let mut blocked_by_moved = false;
7354        if let Some((native, _)) = &edges
7355            && item.content_kind == ContentKind::Issue
7356        {
7357            blocked_by_moved = self
7358                .reconcile_blocked_by(
7359                    &item.id,
7360                    native,
7361                    Issue::Existing(item.blocked_by.as_deref()),
7362                )
7363                .await?;
7364        }
7365        if !fields.is_empty() {
7366            self.update_content(item.content_kind, &item.id, Value::Object(fields))
7367                .await?;
7368        }
7369
7370        // The `blockedBy` this item was read with is what the write just changed, so it is no
7371        // answer any more: a later write in this command would reconcile against it — re-adding
7372        // what it kept and never removing what it dropped — and a forward-edge read would
7373        // answer with it. Left unknown, each asks GitHub instead.
7374        if blocked_by_moved {
7375            item.blocked_by = None;
7376        }
7377        if let Some(title) = &update.title {
7378            item.title.clone_from(title);
7379        }
7380        item.body = visible.filter(|value| !value.is_empty());
7381        item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
7382        item.slot = slot;
7383        if let Some(delivers) = &update.delivers {
7384            item.delivers.clone_from(delivers);
7385        }
7386        if let Some(moving) = status_move {
7387            item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
7388                && item.content_kind == ContentKind::Issue;
7389            item.status = moving.landed;
7390            item.option = Some(moving.name);
7391        }
7392        if let Some((_, _, priority)) = priority_move {
7393            item.priority = HeldPriority::Read(priority);
7394        }
7395        if let Some((_, _, projected)) = projection_move {
7396            item.projected = projected;
7397        }
7398        let task = item.task()?;
7399        let mut written = update.changed(&before, &task);
7400        if blocked_by_moved || recorded_moves {
7401            written.insert(UpdatedField::DependsOn);
7402        }
7403        self.remember_written(item, false)?;
7404        Ok(Some(TaskUpdateOutcome {
7405            task,
7406            written,
7407            delivers_before: before.delivers,
7408        }))
7409    }
7410
7411    /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
7412    /// together, and nothing else; see [`TaskSource::set_task_rendering`].
7413    ///
7414    /// One update of the body: the content outside the slot, and inside it that one entry,
7415    /// every other entry kept as it was. This source keeps no template answers — an issue has
7416    /// no room beside itself that is not its body, and answers written there would duplicate
7417    /// what the content already says and count against GitHub's body limit — so `answers`
7418    /// reaches nothing here. A body that would not change is not sent at all.
7419    async fn replace_rendering(
7420        &self,
7421        id: &NativeId,
7422        kind: BoardKind,
7423        content: &str,
7424        provenance: &Value,
7425        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
7426    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
7427        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7428            return Ok(None);
7429        };
7430        let held = item.raw_body.clone().unwrap_or_default();
7431        let mut slot = item.slot.clone();
7432        slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
7433        let rewritten;
7434        let content = if let Some(assets) = assets {
7435            let uploads = self
7436                .upload_assets(item.own_repository.as_ref(), assets)
7437                .await?;
7438            rewritten =
7439                onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
7440            rewritten.as_str()
7441        } else {
7442            content
7443        };
7444        let body = with_slot(&with_content(&held, content)?, &slot)?;
7445        // Checked before anything is sent, as a content write checks it.
7446        let (visible, read) = metadata_body(Some(body.clone()))?;
7447        if visible.as_deref().unwrap_or_default() != content || read != slot {
7448            return Err(SourceError::Refused {
7449                message: format!(
7450                    "this content ends in what source {} reads as its own metadata slot \
7451                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7452                     as content; next: remove that trailing block from the template",
7453                    self.name
7454                ),
7455            });
7456        }
7457        if body != held {
7458            self.update_content(item.content_kind, &item.id, json!({"body": body}))
7459                .await?;
7460        }
7461        item.body = visible.filter(|value| !value.is_empty());
7462        item.raw_body = Some(body);
7463        item.slot = read;
7464        self.remember_written(item, false)?;
7465        Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
7466            id: id.clone(),
7467            content: Some(content.to_owned()),
7468        }))
7469    }
7470
7471    async fn set_item_field(
7472        &self,
7473        board_id: &str,
7474        item_id: &str,
7475        field_id: &str,
7476        value: Value,
7477    ) -> Result<(), SourceError> {
7478        let data = self
7479            .graphql(
7480                graphql::UPDATE_FIELD,
7481                json!({"input":{
7482                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
7483                },"readPriority":false,"priorityName":PRIORITY_FIELD}),
7484            )
7485            .await?;
7486        let returned = data
7487            .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
7488            .ok_or_else(|| SourceError::Malformed {
7489                message: "GitHub field update returned no project item".into(),
7490            })?;
7491        if required_str(returned, "id")? != item_id {
7492            return Err(SourceError::Malformed {
7493                message: "GitHub field update returned the wrong project item".into(),
7494            });
7495        }
7496        Ok(())
7497    }
7498
7499    /// GitHub accepts one value per field mutation; aliases combine those mutations in
7500    /// one request — writes in the order given, then clears. A lone write is
7501    /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
7502    /// request's slots hold go in further requests of the same document, in order. Every
7503    /// returned item id is checked, including optional aliases.
7504    async fn set_item_fields(
7505        &self,
7506        board: &str,
7507        item: &str,
7508        fields: &[(String, Value)],
7509        clears: &[&str],
7510    ) -> Result<(), SourceError> {
7511        match (fields, clears) {
7512            ([], []) => return Ok(()),
7513            ([(field, value)], []) => {
7514                return self.set_item_field(board, item, field, value.clone()).await;
7515            }
7516            ([], [field]) => {
7517                return self
7518                    .write_priority(
7519                        board,
7520                        item,
7521                        &PriorityWrite::Clear {
7522                            field: (*field).to_owned(),
7523                        },
7524                    )
7525                    .await;
7526            }
7527            _ => {}
7528        }
7529        // Any field of the request stands in for an input a slot leaves out, which GitHub
7530        // still requires to be well formed although it never runs it.
7531        let filler = fields
7532            .first()
7533            .map_or_else(|| clears[0], |(field, _)| field.as_str());
7534        let mut writes = fields.iter();
7535        let mut cleared = clears.iter();
7536        loop {
7537            let batch: Vec<&(String, Value)> =
7538                writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
7539            let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
7540            if batch.is_empty() && batch_clears.is_empty() {
7541                return Ok(());
7542            }
7543            let mut variables = serde_json::Map::new();
7544            for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
7545                let input = match batch.get(index) {
7546                    Some((field, value)) => {
7547                        json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
7548                    }
7549                    None => json!({"projectId":board,"itemId":item,"fieldId":filler,
7550                                   "value":{"text":""}}),
7551                };
7552                variables.insert(slot.variable.to_owned(), input);
7553                variables.insert(slot.include.to_owned(), json!(index < batch.len()));
7554            }
7555            for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
7556                let field = batch_clears.get(index).map_or(filler, |field| **field);
7557                variables.insert(
7558                    slot.variable.to_owned(),
7559                    json!({"projectId":board,"itemId":item,"fieldId":field}),
7560                );
7561                variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
7562            }
7563            let data = self
7564                .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
7565                .await?;
7566            let sent = FIELD_WRITE_SLOTS[..batch.len()]
7567                .iter()
7568                .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
7569            for slot in sent {
7570                let alias = slot.alias;
7571                let returned = data
7572                    .get(alias)
7573                    .and_then(|value| value.get("projectV2Item"))
7574                    .ok_or_else(|| SourceError::Malformed {
7575                        message: format!("GitHub field update {alias} returned no project item"),
7576                    })?;
7577                if required_str(returned, "id")? != item {
7578                    return Err(SourceError::Malformed {
7579                        message: format!(
7580                            "GitHub field update {alias} returned the wrong project item"
7581                        ),
7582                    });
7583                }
7584            }
7585        }
7586    }
7587
7588    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
7589        let mut after: Option<String> = None;
7590        let mut ids = Vec::new();
7591        loop {
7592            let data = self
7593                .graphql(
7594                    graphql::ISSUE_DEPENDENCIES,
7595                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
7596                )
7597                .await?;
7598            let connection =
7599                data.pointer("/node/blockedBy")
7600                    .ok_or_else(|| SourceError::Malformed {
7601                        message: "GitHub dependency response has no blockedBy connection".into(),
7602                    })?;
7603            ids.extend(
7604                connection
7605                    .get("nodes")
7606                    .and_then(Value::as_array)
7607                    .ok_or_else(|| SourceError::Malformed {
7608                        message: "GitHub dependency response nodes is not an array".into(),
7609                    })?
7610                    .iter()
7611                    .map(|value| required_str(value, "id").map(str::to_owned))
7612                    .collect::<Result<Vec<_>, _>>()?,
7613            );
7614            let next = next_cursor(connection)?;
7615            if let Some(next) = &next {
7616                validate_cursor_progress(after.as_deref(), &next.0)?;
7617            }
7618            after = next.map(|cursor| cursor.0);
7619            if after.is_none() {
7620                return Ok(ids);
7621            }
7622        }
7623    }
7624
7625    async fn dependencies(
7626        &self,
7627        id: &NativeId,
7628        near_kind: ItemKind,
7629        direction: Direction,
7630        page: &PageRequest,
7631    ) -> Result<Page<DependencyEdge>, SourceError> {
7632        validate_page(page)?;
7633        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
7634        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
7635        let recorded = recorded_offset(cursor, direction)?;
7636        // What this issue is blocked by, when a read of it by its own id in this command
7637        // already carried the whole connection — a copy reads the item it writes before it
7638        // reads its edges — and the page asked for is the whole of it, or the recorded tail
7639        // after it. Answered from that read, in the shape the dependency read answers in;
7640        // anything else is asked of GitHub.
7641        let carried = match direction {
7642            Direction::DependsOn => self
7643                .resolved_cache()?
7644                .get(id)
7645                .filter(|item| item.content_kind == ContentKind::Issue)
7646                .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
7647            Direction::DependedOnBy => None,
7648        }
7649        .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
7650        // Asked for even in the recorded phase, whose page reads nothing from the
7651        // connection: `__typename` is what says whether this item has a native
7652        // relationship at all, and that is what decides which far ends the reserved key is
7653        // allowed to hold.
7654        let data = match carried {
7655            Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
7656                "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
7657            None => {
7658                self.graphql(
7659                    graphql::ISSUE_DEPENDENCIES,
7660                    json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
7661                           "after":if recorded.is_some() {None} else {cursor}}),
7662                )
7663                .await?
7664            }
7665        };
7666        let node =
7667            data.get("node")
7668                .filter(|v| !v.is_null())
7669                .ok_or_else(|| SourceError::Refused {
7670                    message: format!(
7671                        "GitHub item {} was not found or does not support dependencies",
7672                        id.0
7673                    ),
7674                })?;
7675        let connection_name = match direction {
7676            Direction::DependsOn => "blockedBy",
7677            Direction::DependedOnBy => "blocking",
7678        };
7679        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
7680        // named natively and the reserved key may hold any far end. An issue's connections
7681        // hold issues, and this source reads them at the near item's own level.
7682        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
7683        if let Some(offset) = recorded {
7684            return Ok(recorded_page(
7685                self.recorded_edges(id, near_kind, direction, natively_names, node)
7686                    .await?,
7687                offset,
7688                limit,
7689            ));
7690        }
7691        if natively_names.is_none() {
7692            return Ok(recorded_page(
7693                self.recorded_edges(id, near_kind, direction, natively_names, node)
7694                    .await?,
7695                0,
7696                limit,
7697            ));
7698        }
7699        let connection = node
7700            .get(connection_name)
7701            .ok_or_else(|| SourceError::Malformed {
7702                message: "GitHub dependency response is missing its connection".into(),
7703            })?;
7704        let nodes = connection
7705            .get("nodes")
7706            .and_then(Value::as_array)
7707            .ok_or_else(|| SourceError::Malformed {
7708                message: "GitHub dependency response nodes is not an array".into(),
7709            })?;
7710        // `from` depends on `to`, always. GitHub spells the same relationship from either
7711        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
7712        // it — so the near item is `from` in one direction and `to` in the other.
7713        let items = nodes
7714            .iter()
7715            .map(|value| {
7716                let related = NativeId(required_str(value, "id")?.into());
7717                let related_kind = related_kind(value)?;
7718                let (from, to) = match direction {
7719                    Direction::DependsOn => (
7720                        DependencyEndpoint::from_native(id.clone(), near_kind),
7721                        DependencyEndpoint::from_native(related, related_kind),
7722                    ),
7723                    Direction::DependedOnBy => (
7724                        DependencyEndpoint::from_native(related, related_kind),
7725                        DependencyEndpoint::from_native(id.clone(), near_kind),
7726                    ),
7727                };
7728                Ok(DependencyEdge {
7729                    from,
7730                    to,
7731                    kind: DependencyKind::Blocks,
7732                })
7733            })
7734            .collect::<Result<Vec<_>, SourceError>>()?;
7735        let mut next = next_cursor(connection)?;
7736        if let Some(next) = &next {
7737            validate_cursor_progress(cursor, &next.0)?;
7738        }
7739        if next.is_none()
7740            && !self
7741                .recorded_edges(id, near_kind, direction, natively_names, node)
7742                .await?
7743                .is_empty()
7744        {
7745            next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
7746        }
7747        Ok(Page { items, next })
7748    }
7749
7750    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
7751    /// a far end in another source has to live: no GitHub issue relationship can name one.
7752    ///
7753    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
7754    /// source never writes one down.
7755    ///
7756    /// The metadata lives in the item's own body slot, and `node` is the dependency read's
7757    /// own answer, which carries an issue's body — so an issue's recorded edges cost no
7758    /// request beyond the read already made, and reading the board for them would be a
7759    /// walk of every item for one field of one. A draft has no body in that answer, because
7760    /// a draft is not an issue, so a draft's are read off its own read by id — never off a
7761    /// listing of the board, which can be behind on the very item asked about.
7762    async fn recorded_edges(
7763        &self,
7764        id: &NativeId,
7765        near_kind: ItemKind,
7766        direction: Direction,
7767        natively_names: Option<ItemKind>,
7768        node: &Value,
7769    ) -> Result<Vec<DependencyEdge>, SourceError> {
7770        if direction != Direction::DependsOn {
7771            return Ok(Vec::new());
7772        }
7773        let slot = match node.get("body") {
7774            Some(body) if natively_names.is_some() => {
7775                metadata_body(body.as_str().map(str::to_owned))?.1
7776            }
7777            _ => {
7778                let Some(item) = self.bound_item(id).await? else {
7779                    return Ok(Vec::new());
7780                };
7781                item.slot
7782            }
7783        };
7784        DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
7785            .map_err(|message| SourceError::Malformed { message })
7786    }
7787
7788    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
7789        self.repository
7790            .as_ref()
7791            .ok_or_else(|| SourceError::Refused {
7792                message: format!(
7793                    "source {} has no repository configured, and a GitHub Projects board has no \
7794                 repository of its own to create an issue in; set repository: owner/name on \
7795                 this source",
7796                    self.name
7797                ),
7798            })
7799    }
7800
7801    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
7802    /// states.
7803    ///
7804    /// The fallback is demanded first, whichever arm answers: a write without a configured
7805    /// repository is refused naming the field exactly as it was before the rule existed,
7806    /// so a source that could not write before cannot write now, rather than writing for
7807    /// the one item whose own field happens to decide it.
7808    ///
7809    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
7810    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
7811    /// entry owned by someone other than the owner of the parent issue's repository —
7812    /// GitHub accepts a sub-issue from another repository of the same owner and from no
7813    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
7814    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
7815    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
7816    /// and is visible to the token is checked where its node id is resolved, still before
7817    /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
7818    /// looked up in a listing of the board, which can be minutes behind an issue its own
7819    /// `projectItems` already places on it — and that read answers first from this process's
7820    /// own record, so a project created moments ago in this command answers though GitHub
7821    /// has not caught up.
7822    async fn creation_target(
7823        &self,
7824        incoming: &Incoming<'_>,
7825    ) -> Result<RepositoryTarget, SourceError> {
7826        let fallback = self.configured_repository()?;
7827        let what = |incoming: &Incoming<'_>| {
7828            format!(
7829                "{} {:?}",
7830                incoming.written.kind().describes(),
7831                incoming.title
7832            )
7833        };
7834        let parent = match incoming.parent {
7835            Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
7836                SourceError::Refused {
7837                    message: format!(
7838                        "GitHub project issue {} was not found on the board of source {}, so {} \
7839                         cannot be filed under it",
7840                        parent.0,
7841                        self.name,
7842                        what(incoming)
7843                    ),
7844                }
7845            })?),
7846            None => None,
7847        };
7848        let parents_repository = parent
7849            .as_ref()
7850            .map(|parent| {
7851                // A draft is on the board and so is found, but it has no repository to
7852                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
7853                // would refuse the task only once `createIssue` had made it.
7854                if parent.content_kind == ContentKind::DraftIssue {
7855                    return Err(SourceError::Refused {
7856                        message: format!(
7857                            "GitHub project item {} on the board of source {} is a draft, \
7858                             which cannot have sub-issues, so {} cannot be filed under it",
7859                            parent.id.0,
7860                            self.name,
7861                            what(incoming)
7862                        ),
7863                    });
7864                }
7865                // An issue's repository is where a sub-issue is placed and whose owner it
7866                // is compared against, so a parent whose repository this source cannot
7867                // spell as `owner/name` — GitHub's login grammar is wider than this
7868                // source's floor — is one nothing can be filed under.
7869                parent
7870                    .own_repository
7871                    .as_ref()
7872                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
7873                    .ok_or_else(|| SourceError::Malformed {
7874                        message: format!(
7875                            "GitHub project issue {} on the board of source {} is in {}, which \
7876                             is not a {}/owner/name repository this source can place {} in",
7877                            parent.id.0,
7878                            self.name,
7879                            parent
7880                                .own_repository
7881                                .as_ref()
7882                                .map_or("no repository", Repository::as_str),
7883                            RepositoryTarget::HOST,
7884                            what(incoming)
7885                        ),
7886                    })
7887            })
7888            .transpose()?;
7889        match incoming.repositories {
7890            [named] => {
7891                let target =
7892                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
7893                        message: format!(
7894                            "{} names repository {}, which is not a {}/owner/name repository \
7895                             source {} can create an issue in; name one that is, or name none",
7896                            what(incoming),
7897                            named.as_str(),
7898                            RepositoryTarget::HOST,
7899                            self.name
7900                        ),
7901                    })?;
7902                if let Some(parents) = &parents_repository
7903                    && parents.owner != target.owner
7904                {
7905                    return Err(SourceError::Refused {
7906                        message: format!(
7907                            "{} names repository {}, owned by {}, but its project's issue is in \
7908                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
7909                             of the same owner as its parent issue; name a repository of {}, or \
7910                             name none",
7911                            what(incoming),
7912                            target.slug(),
7913                            target.owner,
7914                            parents.slug(),
7915                            parents.owner,
7916                            parents.owner
7917                        ),
7918                    });
7919                }
7920                Ok(target)
7921            }
7922            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
7923        }
7924    }
7925
7926    /// The node id of the repository `incoming` is being created in, or the refusal naming
7927    /// the item and the repository the token cannot see.
7928    ///
7929    /// Resolved once per command per repository; see [`Self::repository_cache`].
7930    async fn repository_id(
7931        &self,
7932        repository: &RepositoryTarget,
7933        incoming: &Incoming<'_>,
7934    ) -> Result<String, SourceError> {
7935        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
7936            return Ok(id);
7937        }
7938        let data = self
7939            .graphql(
7940                graphql::REPOSITORY,
7941                json!({"owner":repository.owner,"name":repository.name}),
7942            )
7943            .await?;
7944        self.repository_read(&data, repository, incoming)
7945    }
7946
7947    /// The repository's node id out of an answer carrying the `repository` root, held for
7948    /// the rest of this command, or the refusal naming the item that cannot be created in it.
7949    fn repository_read(
7950        &self,
7951        data: &Value,
7952        repository: &RepositoryTarget,
7953        incoming: &Incoming<'_>,
7954    ) -> Result<String, SourceError> {
7955        let node = data
7956            .get("repository")
7957            .filter(|value| !value.is_null())
7958            .ok_or_else(|| SourceError::Refused {
7959                message: format!(
7960                    "GitHub repository {} was not found or is not visible to the token, so {} \
7961                     {:?} cannot be created in it",
7962                    repository.slug(),
7963                    incoming.written.kind().describes(),
7964                    incoming.title
7965                ),
7966            })?;
7967        let id = required_str(node, "id")?.to_owned();
7968        self.repository_cache()?
7969            .insert(repository.clone(), id.clone());
7970        Ok(id)
7971    }
7972
7973    fn repository_cache(
7974        &self,
7975    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
7976        self.repository_cache
7977            .lock()
7978            .map_err(|_| SourceError::Unavailable {
7979                message: "this source's record of the destination repository was left \
7980                          inconsistent by an earlier failure; next: run the command again"
7981                    .into(),
7982            })
7983    }
7984
7985    /// Create or update one board item, whichever kind it is.
7986    async fn write_item(
7987        &self,
7988        incoming: &Incoming<'_>,
7989        target: Option<&NativeId>,
7990        depends_on: &[DependencyEdge],
7991    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
7992        // Refused before anything is read or written: a task or a project titled the way
7993        // this board spells a document would land as an issue this same source reads back
7994        // as a document, so the field this destination cannot carry is named rather than
7995        // written and silently reclassified.
7996        if let Written::Work(kind, _) = incoming.written
7997            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
7998        {
7999            return Err(SourceError::Refused {
8000                message: format!(
8001                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8002                     spells a document, so it would read back as one rather than as a {}; \
8003                     retitle it, or copy it as a document",
8004                    kind.marker(),
8005                    self.name,
8006                    kind.marker()
8007                ),
8008            });
8009        }
8010        // The destination is read by its own id, and whether this board holds it is decided
8011        // by that read — its own `projectItems` — rather than by whether a listing of the
8012        // board happens to include it yet. See the module documentation.
8013        let existing = match target {
8014            Some(target) => {
8015                Some(
8016                    self.bound_item(target)
8017                        .await?
8018                        .ok_or_else(|| SourceError::Refused {
8019                            message: format!("GitHub destination item {} was not found", target.0),
8020                        })?,
8021                )
8022            }
8023            None => None,
8024        };
8025        let existing = existing.as_ref();
8026        // An existing issue is never moved; a new one is created where the rule says — and
8027        // knowing where is what lets the board's fields and that repository's id be read
8028        // together, before anything below needs either.
8029        let creation_target = match existing {
8030            Some(_) => None,
8031            None => {
8032                let target = self.creation_target(incoming).await?;
8033                self.creation_context(&target, incoming).await?;
8034                Some(target)
8035            }
8036        };
8037        let board = self
8038            .fields_for(
8039                existing,
8040                incoming.written.status().is_some(),
8041                incoming
8042                    .priority
8043                    .is_some_and(|priority| priority != Priority::None),
8044            )
8045            .await?;
8046        let status_target = incoming
8047            .written
8048            .work_status()
8049            .map(|(kind, status)| self.resolved_target(kind, status.category))
8050            .transpose()?;
8051        let column = match (incoming.written.work_status(), status_target.as_ref()) {
8052            (Some((kind, status)), Some(target)) => {
8053                self.column_for(&board.fields, kind, status.category, target)?
8054            }
8055            _ => None,
8056        };
8057        // Resolved before anything is created, for the reason the column above is: a
8058        // priority this board has no option for is refused while nothing has been written.
8059        let priority_write = match incoming.priority {
8060            Some(priority) => self.priority_write(&board.fields, existing, priority)?,
8061            None => None,
8062        };
8063        // And for the reason the priority is: a value no projected field can hold, or a board
8064        // that cannot hold the field, is refused while nothing has been written.
8065        let (projections, projected) = self.projection_writes(
8066            &board.fields,
8067            existing.map(|item| &item.projected),
8068            incoming.metadata,
8069            None,
8070        )?;
8071        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
8072        if content_kind == ContentKind::DraftIssue {
8073            if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
8074                (status_target.as_ref(), incoming.written.status())
8075            {
8076                return Err(self.closes_a_draft(status.category));
8077            }
8078            if incoming.parent.is_some() {
8079                return Err(SourceError::Refused {
8080                    message: "GitHub draft items cannot be a project's sub-issue".into(),
8081                });
8082            }
8083        }
8084        match existing {
8085            Some(item) if content_kind == ContentKind::Issue => {
8086                if item.labels != incoming.labels {
8087                    return Err(SourceError::Refused {
8088                        message: "GitHub issue labels differ from the labels being written".into(),
8089                    });
8090                }
8091            }
8092            _ => {
8093                if !incoming.labels.is_empty() {
8094                    return Err(SourceError::Refused {
8095                        message: "GitHub items created by this destination carry no labels".into(),
8096                    });
8097                }
8098            }
8099        }
8100
8101        // The repository the issue really lives in is what the slot below is written against,
8102        // so a single entry that is where the issue is created travels as no key at all, and
8103        // the read side derives it back from the issue.
8104        let own_repository = match (existing, &creation_target) {
8105            (Some(item), _) => item.own_repository.clone(),
8106            (None, Some(target)) => Some(
8107                Repository::try_from(target.origin())
8108                    .map_err(|message| SourceError::Config { message })?,
8109            ),
8110            (None, None) => None,
8111        };
8112        let (native, fallback) = self
8113            .partition_edges(
8114                incoming.written.kind(),
8115                content_kind,
8116                existing.and_then(|item| item.blocked_by.as_deref()),
8117                depends_on,
8118            )
8119            .await?;
8120        let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
8121        let content = match incoming.assets {
8122            Some(assets) => {
8123                let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
8124                let rewritten = onetaskgraph_plugin_api::serve_asset_references(
8125                    incoming.content.unwrap_or_default(),
8126                    &mut slot,
8127                    &uploads,
8128                );
8129                incoming.content.map(|_| rewritten)
8130            }
8131            None => incoming.content.map(str::to_owned),
8132        };
8133        let body = compose_body(content.as_deref(), &slot)?;
8134        // Read before anything is created, for the reason the field below is: a value
8135        // this destination cannot store has to refuse, and refusing after `createIssue`
8136        // would leave an issue behind that nothing asked for. The engine writes a
8137        // qualified id here; a caller handing this key anything else is told so rather
8138        // than having it silently stored as no origin at all.
8139        // 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.
8140        let origin = match incoming.metadata.get(ORIGIN_KEY) {
8141            None => "",
8142            Some(Value::String(origin)) => origin.as_str(),
8143            Some(other) => {
8144                return Err(SourceError::Refused {
8145                    message: format!(
8146                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
8147                         is {other}"
8148                    ),
8149                });
8150            }
8151        };
8152        // Resolved before anything is created: a board that cannot carry the copy origin
8153        // has to refuse the write, and refusing it after `createIssue` would leave an
8154        // issue behind that nothing asked for.
8155        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
8156            Some(field) => {
8157                if required_str(field, "__typename")? != "ProjectV2Field" {
8158                    return Err(SourceError::Refused {
8159                        message: format!(
8160                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
8161                        ),
8162                    });
8163                }
8164                Some(required_str(field, "id")?.to_owned())
8165            }
8166            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
8167                return Err(SourceError::Refused {
8168                    message: format!(
8169                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
8170                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
8171                         the board"
8172                    ),
8173                });
8174            }
8175            None => None,
8176        };
8177
8178        let Landed {
8179            content_id,
8180            item_id,
8181            url,
8182            number,
8183        } = match existing {
8184            // Its content is written last, below, once everything else has landed.
8185            Some(item) => Landed {
8186                content_id: item.id.clone(),
8187                item_id: item.item_id.clone(),
8188                url: item.url.clone(),
8189                number: item.number,
8190            },
8191            None => {
8192                let target = creation_target
8193                    .as_ref()
8194                    .ok_or_else(|| SourceError::Malformed {
8195                        message: "a new item was decided without a repository to create it in"
8196                            .into(),
8197                    })?;
8198                self.create_and_file_issue(board.id.as_str(), target, incoming, &body)
8199                    .await?
8200            }
8201        };
8202
8203        // A write the store's cache expects sends only what differs from the version it expects:
8204        // a priority the item already holds there is not sent, so a person's edit of it since is
8205        // left alone. Every other write sends it, as its pinned cost says.
8206        let priority_write = priority_write.filter(|_| {
8207            !existing.is_some_and(|item| {
8208                self.expected.is_primed(&item.id)
8209                    && incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
8210            })
8211        });
8212        let written_option = column.as_ref().map(|(_, _, name)| name.clone());
8213        let column = column
8214            .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
8215            .map(|(field, option, _)| (field, option));
8216        // Creating an item here is several calls — `createIssue`, which files it on the
8217        // board, then its board fields, the parent and the dependencies — and GitHub can fail
8218        // at any of them. Everything this source can refuse *before* the first of those is
8219        // already checked above, so what is left is GitHub itself failing part way. When it
8220        // does over an item this call created, the issue is taken back: a write that
8221        // refused must not leave an item behind that nobody asked for, and one that does
8222        // makes the retry create a second.
8223        // Whether the board-field write carrying a moved origin was answered as landing whole.
8224        // When it was refused, GitHub does not say which of its fields ran before the one that
8225        // failed, so the origin may or may not have moved.
8226        let mut origin_landed = false;
8227        let landed = self
8228            .finish_write(
8229                board.id.as_str(),
8230                incoming,
8231                &content_id,
8232                &item_id,
8233                content_kind,
8234                existing,
8235                origin_field.as_deref(),
8236                origin,
8237                column,
8238                status_target.as_ref(),
8239                priority_write.as_ref(),
8240                &projections,
8241                &native,
8242                &mut origin_landed,
8243            )
8244            .await;
8245        // An existing item's title, body and state go last, in one `updateIssue`, once its board
8246        // fields and its relationships have landed: a refusal of any of those then leaves its
8247        // body — and the metadata slot inside it — exactly as it stood.
8248        let landed = match (landed, existing) {
8249            (Ok(()), Some(item)) => {
8250                self.update_existing(item, incoming, &body, status_target.as_ref())
8251                    .await
8252            }
8253            (landed, _) => landed,
8254        };
8255        if let Err(error) = landed {
8256            match existing {
8257                // Best effort, and the write's own failure is what the caller is told: a
8258                // refusal naming the tidy-up would hide why the write failed at all.
8259                None => {
8260                    let _ = self.delete_issue(&content_id).await;
8261                }
8262                // The origin field is the one piece of an existing item's metadata written
8263                // before its body, so a write refused after it puts it back as it was. When
8264                // that is refused too, the write's own failure is still what the caller is
8265                // told — with what it left behind added, because the item's metadata is then
8266                // not as it stood and a caller retrying has to know which key moved.
8267                Some(item) => {
8268                    let before = item.origin.as_deref().unwrap_or("");
8269                    if let Some(field) = origin_field.as_deref()
8270                        && before != origin
8271                        && let Err(restore) = self
8272                            .set_item_field(
8273                                board.id.as_str(),
8274                                &item.item_id,
8275                                field,
8276                                json!({"text": before}),
8277                            )
8278                            .await
8279                    {
8280                        let left = if origin_landed {
8281                            format!(
8282                                "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
8283                                 not be put back to {before:?} ({restore}), so item {} still \
8284                                 holds {origin:?} there",
8285                                item.id.0
8286                            )
8287                        } else {
8288                            format!(
8289                                "the refused write carried its {ORIGIN_KEY} from {before:?} to \
8290                                 {origin:?}, GitHub does not say whether that part of it ran, \
8291                                 and putting it back to {before:?} was refused ({restore}), so \
8292                                 item {} holds {origin:?} or {before:?} there",
8293                                item.id.0
8294                            )
8295                        };
8296                        return Err(noting(
8297                            error,
8298                            &format!(
8299                                "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
8300                                 run the write again"
8301                            ),
8302                        ));
8303                    }
8304                }
8305            }
8306            return Err(error);
8307        }
8308
8309        let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
8310            (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
8311                self.statuses
8312                    .status(kind, written_option.as_deref(), true, Some(reason.reason()))
8313            }
8314            (Some((kind, _)), Some(StatusTarget::Column(_))) => {
8315                self.statuses
8316                    .status(kind, written_option.as_deref(), false, None)
8317            }
8318            (Some((_, status)), _) => status.clone(),
8319            (None, _) => Status {
8320                category: StatusCategory::Unknown,
8321                name: "Open".to_owned(),
8322            },
8323        };
8324
8325        // So the rest of this command reads what it just did rather than what the board
8326        // said before it. See `remember_written` for which half takes it.
8327        let remembered = Resolved {
8328            item_id,
8329            id: content_id.clone(),
8330            content_kind,
8331            kind: incoming.written.kind(),
8332            title: incoming.title.to_owned(),
8333            // The visible half of the body this write composed, split back off it the
8334            // way a read splits it — so what this record reports is what a read of the
8335            // same issue reports, rather than the person's text with the metadata slot
8336            // still on the end of it.
8337            body: metadata_body(body.clone())?.0,
8338            raw_body: body.clone(),
8339            // A document has no status of its own; what it reads back as is whatever
8340            // the issue's own state says, which is what a re-read reports.
8341            status: written_status,
8342            option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
8343            priority: match incoming.priority {
8344                Some(priority) => HeldPriority::Read(priority),
8345                None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
8346                    item.priority.clone()
8347                }),
8348            },
8349            // What `state_input` asked for: closed for a terminal target, open for any other
8350            // status, and the issue's own state left as it was by a document write.
8351            closed: content_kind == ContentKind::Issue
8352                && match status_target.as_ref() {
8353                    Some(StatusTarget::Terminal(_, _)) => true,
8354                    Some(_) => false,
8355                    None => existing.is_some_and(|item| item.closed),
8356                },
8357            delivers: incoming.delivers.to_vec(),
8358            delivered_by: incoming.delivered_by.to_vec(),
8359            labels: incoming.labels.to_vec(),
8360            parent: incoming.parent.cloned(),
8361            origin: (!origin.is_empty()).then(|| origin.to_owned()),
8362            projected,
8363            number,
8364            // In the update path this is the item's own url, read off `existing` where the
8365            // record above was bound, so one expression serves both halves.
8366            url,
8367            created_at: existing.and_then(|item| item.created_at),
8368            updated_at: existing.and_then(|item| item.updated_at),
8369            // What this write moved is GitHub's to say, so a record of it gives no token until
8370            // it is read again.
8371            item_updated_at: None,
8372            comment_count: None,
8373            relationship_events: None,
8374            served_at: None,
8375            own_repository,
8376            repositories: incoming.repositories.to_vec(),
8377            classification: incoming.classification,
8378            slot,
8379            board_id: Some(board.id.as_str().to_owned()),
8380            fields: board
8381                .fields
8382                .get("nodes")
8383                .and_then(Value::as_array)
8384                .cloned()
8385                .unwrap_or_default(),
8386            board_fields: Some(board.fields.clone()),
8387            // What this write left the relationship holding is known by id alone, and a
8388            // later read of its edges needs each far end's kind, so it reads them again.
8389            blocked_by: None,
8390        };
8391        self.remember_written(remembered, existing.is_none())?;
8392        Ok(onetaskgraph_plugin_api::AssetsWritten {
8393            id: content_id,
8394            content,
8395        })
8396    }
8397
8398    /// Everything a write does after the item exists: its board fields, its parent, and
8399    /// its dependencies.
8400    ///
8401    /// Split out of `write_item` so there is one place a failure past the point of no
8402    /// return is caught, rather than a tidy-up repeated at each `?` above.
8403    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
8404    // so there is one place a failure past the point of no return is caught, and its
8405    // arguments are exactly the values that tail already had in scope. Bundling them into a
8406    // struct would describe no concept — it would be "the arguments of this function" — and
8407    // would put the whole of `write_item`'s locals behind one more indirection.
8408    #[allow(clippy::too_many_arguments)]
8409    async fn finish_write(
8410        &self,
8411        board_id: &str,
8412        incoming: &Incoming<'_>,
8413        content_id: &NativeId,
8414        item_id: &str,
8415        content_kind: ContentKind,
8416        existing: Option<&Resolved>,
8417        origin_field: Option<&str>,
8418        origin: &str,
8419        column: Option<(String, String)>,
8420        status_target: Option<&StatusTarget>,
8421        priority: Option<&PriorityWrite>,
8422        projections: &[ProjectionWrite],
8423        native: &[String],
8424        origin_landed: &mut bool,
8425    ) -> Result<(), SourceError> {
8426        let mut fields = Vec::new();
8427        if let Some(field_id) = origin_field
8428            && existing.map_or(!origin.is_empty(), |item| {
8429                item.origin.as_deref().unwrap_or("") != origin
8430            })
8431        {
8432            fields.push((field_id.to_owned(), json!({"text":origin})));
8433        }
8434        if let Some((field_id, option_id)) = column {
8435            fields.push((field_id, json!({"singleSelectOptionId":option_id})));
8436        }
8437        let mut clears = Vec::new();
8438        match priority {
8439            Some(PriorityWrite::Select { field, option }) => {
8440                fields.push((field.clone(), json!({"singleSelectOptionId":option})));
8441            }
8442            Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
8443            None => {}
8444        }
8445        for projection in projections {
8446            projection.join(&mut fields, &mut clears);
8447        }
8448        self.set_item_fields(board_id, item_id, &fields, &clears)
8449            .await?;
8450        *origin_landed = true;
8451
8452        // An existing issue closes in the `updateIssue` its write ends with; one created just
8453        // now closes here, once its option is selected.
8454        if existing.is_none()
8455            && content_kind == ContentKind::Issue
8456            && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
8457        {
8458            self.update_content(
8459                ContentKind::Issue,
8460                content_id,
8461                json!({"stateInput":state_input(status_target)}),
8462            )
8463            .await?;
8464        }
8465
8466        if content_kind == ContentKind::Issue {
8467            self.reparent(
8468                existing.and_then(|item| item.parent.clone()),
8469                content_id,
8470                incoming.parent,
8471            )
8472            .await?;
8473            // A document takes part in no dependency graph, so writing one neither reads
8474            // nor changes the issue's own `blockedBy` relationships. Reconciling them
8475            // against the empty list a document write carries would *delete* whatever
8476            // relationships a person had made on that issue, which is a write nobody
8477            // asked for.
8478            if incoming.written.kind() != BoardKind::Document {
8479                let issue = match existing {
8480                    Some(item) => Issue::Existing(item.blocked_by.as_deref()),
8481                    None => Issue::Created,
8482                };
8483                self.reconcile_blocked_by(content_id, native, issue).await?;
8484            }
8485        }
8486        Ok(())
8487    }
8488
8489    /// Delete one issue, which takes its board item with it.
8490    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
8491        let data = self
8492            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
8493            .await?;
8494        data.pointer("/deleteIssue/repository")
8495            .filter(|value| !value.is_null())
8496            .ok_or_else(|| SourceError::Malformed {
8497                message: "GitHub issue deletion returned no repository".into(),
8498            })?;
8499        self.forget(id)?;
8500        Ok(())
8501    }
8502
8503    /// Remove one item this copy created, so a copy that could not finish leaves the board
8504    /// as it found it.
8505    ///
8506    /// Deleting the issue takes its board item with it, so there is no second mutation to
8507    /// keep in step. An id the board does not hold is not an error: the item is already
8508    /// gone, which is the state this asks for. Which that is, is decided by reading the item
8509    /// by its own id — a listing of the board can still be missing an item it holds, and
8510    /// reading that as *already gone* would leave behind the very item this was asked to
8511    /// take back.
8512    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
8513        let Some(item) = self.bound_item(id).await? else {
8514            return Ok(());
8515        };
8516        if item.content_kind == ContentKind::DraftIssue {
8517            return Err(SourceError::Refused {
8518                message: format!(
8519                    "GitHub item {} is a draft, and this source removes an item by deleting \
8520                     its issue; next: remove it from the board by hand",
8521                    id.0
8522                ),
8523            });
8524        }
8525        let data = self
8526            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
8527            .await?;
8528        data.pointer("/deleteIssue/repository")
8529            .filter(|value| !value.is_null())
8530            .ok_or_else(|| SourceError::Malformed {
8531                message: "GitHub issue deletion returned no repository".into(),
8532            })?;
8533        self.forget(id)?;
8534        Ok(())
8535    }
8536
8537    /// The issue a comment call on `task` is about, or `None` when this board holds no such
8538    /// task.
8539    ///
8540    /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
8541    /// read of the task cannot disagree about which ids name one: a project or a document of
8542    /// this board is not a task here either.
8543    ///
8544    /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
8545    /// issues and a draft is not one. It is refused rather than answered with an empty page,
8546    /// which would read as a task nobody has commented on yet.
8547    async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
8548        let cached = self.resolved_cache()?.get(task).cloned();
8549        let Some(item) = (match cached {
8550            Some(item) => Some(item),
8551            None => self.item_by_id(task).await?,
8552        })
8553        .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
8554            return Ok(None);
8555        };
8556        if item.content_kind == ContentKind::DraftIssue {
8557            return Err(self.draft_has_no_comments(task));
8558        }
8559        Ok(Some(item.id))
8560    }
8561
8562    /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
8563    /// issues, and a draft is not one.
8564    fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
8565        SourceError::Refused {
8566            message: format!(
8567                "task {} of source {} is a draft item on the board, and GitHub keeps \
8568                 comments on issues alone, so a draft has none to read or write; next: \
8569                 convert the draft to an issue on the board, then comment on the issue it \
8570                 becomes",
8571                task.0, self.name
8572            ),
8573        }
8574    }
8575
8576    /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
8577    /// request — or `None` when this board holds no task by that id.
8578    ///
8579    /// What `task show` and a comment listing read. A draft is a task with no comments, so it
8580    /// is answered with the draft and the refusal, at the price of the draft's own read.
8581    async fn issue_detail(
8582        &self,
8583        id: &NativeId,
8584        page: &PageRequest,
8585    ) -> Result<Option<TaskDetailRead>, SourceError> {
8586        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
8587        let asked = self
8588            .graphql(
8589                graphql::ISSUE_DETAIL,
8590                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
8591                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
8592                       "duplicates":true}),
8593            )
8594            .await;
8595        let data = match asked {
8596            Ok(data) => data,
8597            Err(error) if unresolvable_node(&error) => return Ok(None),
8598            Err(error) => return Err(error),
8599        };
8600        // `node` is null for an id that names nothing, and absent only from an answer this
8601        // source cannot read — never the same thing.
8602        let node = data.get("node").ok_or_else(|| SourceError::Malformed {
8603            message: format!("GitHub answered the read of {} with no node", id.0),
8604        })?;
8605        self.detail_of(id, node, true, after).await
8606    }
8607
8608    /// Several tasks, each with the first page of its comments when `comments` is set, read
8609    /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
8610    /// order.
8611    ///
8612    /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
8613    /// one item at a time, so that id is answered as missing and the others as themselves; any
8614    /// other refusal is every id of that batch's answer.
8615    async fn issue_details(
8616        &self,
8617        ids: &[NativeId],
8618        comments: Option<&PageRequest>,
8619    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
8620        let mut read = Vec::with_capacity(ids.len());
8621        for batch in ids.chunks(DETAIL_BATCH) {
8622            match self
8623                .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
8624                .await
8625            {
8626                Ok(data) => {
8627                    for (slot, id) in batch.iter().enumerate() {
8628                        // Every alias asked for is answered, null for an id naming nothing;
8629                        // one missing is an answer this source cannot read.
8630                        let read_one = match data.get(format!("i{slot}")) {
8631                            Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
8632                            None => Err(SourceError::Malformed {
8633                                message: format!(
8634                                    "GitHub answered a batch read with no item for {}",
8635                                    id.0
8636                                ),
8637                            }),
8638                        };
8639                        read.push(read_one);
8640                    }
8641                }
8642                Err(error) if unresolvable_node(&error) => {
8643                    for id in batch {
8644                        read.push(match comments {
8645                            Some(page) => self.issue_detail(id, page).await,
8646                            None => self.task_read(id).await,
8647                        });
8648                    }
8649                }
8650                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
8651            }
8652        }
8653        read
8654    }
8655
8656    /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
8657    async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
8658        Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
8659            task,
8660            comments: None,
8661        }))
8662    }
8663
8664    /// What one node a detail read reached says: the task this board holds by `id`, with the
8665    /// page of comments the node carries when `commented` — or `None` for a node that is no
8666    /// task of this board.
8667    ///
8668    /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
8669    /// and an item this process created answers from this process's own record, which a node
8670    /// read taken moments after the write can still be behind.
8671    async fn detail_of(
8672        &self,
8673        id: &NativeId,
8674        node: &Value,
8675        commented: bool,
8676        after: Option<&str>,
8677    ) -> Result<Option<TaskDetailRead>, SourceError> {
8678        if node.is_null() {
8679            return Ok(None);
8680        }
8681        let draft = optional_str(node, "__typename")? == Some("DraftIssue");
8682        // An issue answered under one id is that id's, or the answer is not one this source
8683        // can report: reporting another issue's task and comments under the qualified id asked
8684        // for would be the one wrong answer here. A draft's own read checks the same.
8685        if !draft
8686            && optional_str(node, "__typename")? == Some("Issue")
8687            && required_str(node, "id")? != id.0
8688        {
8689            return Err(SourceError::Malformed {
8690                message: format!(
8691                    "GitHub answered the read of {} with issue {}",
8692                    id.0,
8693                    required_str(node, "id")?
8694                ),
8695            });
8696        }
8697        let item = if draft {
8698            self.draft_by_id(id).await?
8699        } else {
8700            self.resolve_issue(node).await?
8701        };
8702        // A read that carried the whole of what the issue is blocked by — a copy's — is kept
8703        // for this command, so the dependency read the copy makes next is answered from it.
8704        if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
8705            self.resolved_cache()?.insert(read.id.clone(), read.clone());
8706        }
8707        let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
8708            return Ok(None);
8709        };
8710        let own = self.created()?.iter().find(|own| own.id == *id).cloned();
8711        let task = own.unwrap_or(item).task()?;
8712        let comments = match (commented, draft) {
8713            (false, _) => None,
8714            (true, true) => Some(Err(self.draft_has_no_comments(id))),
8715            (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
8716        };
8717        if commented && !draft && after.is_none() {
8718            self.expected.learn_comments(id, node);
8719        }
8720        Ok(Some(TaskDetailRead { task, comments }))
8721    }
8722
8723    /// Whether the comment `comment` is one of `issue`'s own.
8724    ///
8725    /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
8726    /// comment's id and nothing else: a comment id given against the wrong task would
8727    /// otherwise change a comment on some other issue entirely. An id that names nothing, or
8728    /// names something that is not an issue comment, is a comment this task does not have —
8729    /// which is what GitHub refusing to resolve it means too.
8730    async fn comment_is_on(
8731        &self,
8732        issue: &NativeId,
8733        comment: &NativeId,
8734    ) -> Result<bool, SourceError> {
8735        Ok(self.comment_on(issue, comment).await?.is_some())
8736    }
8737
8738    /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
8739    /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
8740    async fn comment_on(
8741        &self,
8742        issue: &NativeId,
8743        comment: &NativeId,
8744    ) -> Result<Option<String>, SourceError> {
8745        // A write the store's cache expects knows which comments its issue held, and their
8746        // bodies, at the version it expects: what this read would answer.
8747        if let Some(primed) = self.expected.primed_comment(issue, comment) {
8748            return Ok(primed);
8749        }
8750        let asked = self
8751            .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
8752            .await;
8753        let data = match asked {
8754            Ok(data) => data,
8755            Err(error) if unresolvable_node(&error) => return Ok(None),
8756            Err(error) => return Err(error),
8757        };
8758        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
8759            return Ok(None);
8760        };
8761        if optional_str(node, "__typename")? != Some("IssueComment") {
8762            return Ok(None);
8763        }
8764        let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
8765            message: format!("GitHub issue comment {} names no issue", comment.0),
8766        })?;
8767        if required_str(on, "id")? != issue.0 {
8768            return Ok(None);
8769        }
8770        Ok(Some(required_str(node, "body")?.to_owned()))
8771    }
8772
8773    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
8774    async fn partition_edges(
8775        &self,
8776        near_kind: BoardKind,
8777        near_content: ContentKind,
8778        carried: Option<&[Value]>,
8779        depends_on: &[DependencyEdge],
8780    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
8781        let mut native = Vec::new();
8782        let mut fallback = Vec::new();
8783        let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
8784            .iter()
8785            .map(|edge| {
8786                let same_source = edge
8787                    .to
8788                    .source()
8789                    .is_none_or(|source| source == self.name.as_str());
8790                // A qualified id's source segment runs to its *first* colon — `GlobalId` and
8791                // `DependencyEndpoint::source` both read it that way — and a native id may hold
8792                // colons of its own, so the far end is everything after that one separator.
8793                // Splitting at the last would truncate `work:urn:task:7` to `7`.
8794                let far_id = if edge.to.is_qualified() {
8795                    edge.to
8796                        .id()
8797                        .split_once(':')
8798                        .map_or(edge.to.id(), |(_, native)| native)
8799                } else {
8800                    edge.to.id()
8801                };
8802                // One that already blocks the near issue was answered by that issue's own
8803                // read, which carried each of its blockers' kinds — an issue every one — so it
8804                // is not read again.
8805                let blocking = carried.and_then(|nodes| {
8806                    nodes
8807                        .iter()
8808                        .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
8809                });
8810                (edge, far_id, same_source, blocking)
8811            })
8812            .collect();
8813        // Every other same-source far end is read by its own id, exactly as the item it is a
8814        // far end of is: whether this board holds it is that read's answer, never a listing's.
8815        // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
8816        let mut unread: Vec<NativeId> = Vec::new();
8817        for (_, far_id, same_source, blocking) in &far_ends {
8818            let id = NativeId((*far_id).to_owned());
8819            if *same_source && blocking.is_none() && !unread.contains(&id) {
8820                unread.push(id);
8821            }
8822        }
8823        let read: BTreeMap<NativeId, Option<Resolved>> = unread
8824            .iter()
8825            .cloned()
8826            .zip(self.items_by_ids(&unread).await?)
8827            .collect();
8828        for (edge, far_id, same_source, blocking) in far_ends {
8829            let far = match (same_source, blocking) {
8830                (false, _) => None,
8831                (true, Some(node)) => Some(FarEnd {
8832                    kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
8833                        BoardKind::Document
8834                    } else {
8835                        BoardKind::Work(related_kind(node)?)
8836                    },
8837                    content_kind: ContentKind::Issue,
8838                }),
8839                (true, None) => {
8840                    let read = read
8841                        .get(&NativeId(far_id.to_owned()))
8842                        .cloned()
8843                        .flatten()
8844                        .ok_or_else(|| SourceError::Refused {
8845                            message: format!("GitHub dependency item {far_id} was not found"),
8846                        })?;
8847                    Some(FarEnd {
8848                        kind: read.kind,
8849                        content_kind: read.content_kind,
8850                    })
8851                }
8852            };
8853            let far = far.as_ref();
8854            // The caller says which kind the far end is, and this board holds the far end
8855            // itself, so a disagreement is settled here rather than stored: recorded, the
8856            // wrong kind would read back as a cross-level edge that never existed; written
8857            // natively, it would name a relationship of a different level than the caller
8858            // asked for.
8859            //
8860            // A far end this board holds as a *document* fails the same comparison and is
8861            // refused by the same sentence: `ItemKind` has no document variant because
8862            // nothing may point at one, so no caller can name it correctly and the refusal
8863            // is the only honest answer.
8864            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
8865                return Err(SourceError::Refused {
8866                    message: format!(
8867                        "GitHub dependency item {far_id} is a {} of this board, and this item \
8868                         names it as a {}; record the kind it is",
8869                        disagreeing.kind.describes(),
8870                        edge.to.kind.marker()
8871                    ),
8872                });
8873            }
8874            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
8875            // however the far end is spelled — and one classified native here would be
8876            // written nowhere at all, because a draft's native reconciliation never runs.
8877            let native_here = near_content == ContentKind::Issue
8878                && far.is_some_and(|far| {
8879                    far.content_kind == ContentKind::Issue
8880                        && BoardKind::Work(edge.to.kind) == near_kind
8881                });
8882            if native_here {
8883                native.push(far_id.to_owned());
8884            } else {
8885                fallback.push(edge.clone());
8886            }
8887        }
8888        Ok((native, fallback))
8889    }
8890
8891    async fn update_existing(
8892        &self,
8893        item: &Resolved,
8894        incoming: &Incoming<'_>,
8895        body: &Option<String>,
8896        status_target: Option<&StatusTarget>,
8897    ) -> Result<(), SourceError> {
8898        let title = incoming.written_title();
8899        // A terminal status closes the issue here, in the same mutation as its body: its board
8900        // option was selected before this, so a close never lands on an item whose board cannot
8901        // show it.
8902        let mut fields = match item.content_kind {
8903            ContentKind::DraftIssue => json!({"title":title,"body":body}),
8904            ContentKind::Issue => json!({"title":title,"body":body,
8905                                         "stateInput":state_input(status_target)}),
8906        };
8907        // A write the store's cache expects sends only the members whose value differs from the
8908        // version it expects, and nothing at all when none does.
8909        if self.expected.is_primed(&item.id)
8910            && let Some(members) = fields.as_object_mut()
8911        {
8912            if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
8913                members.remove("title");
8914            }
8915            if members.get("body").and_then(Value::as_str)
8916                == Some(item.raw_body.as_deref().unwrap_or_default())
8917            {
8918                members.remove("body");
8919            }
8920            let terminal = matches!(status_target, Some(StatusTarget::Terminal(_, _)));
8921            if members.contains_key("stateInput") && terminal == item.closed {
8922                members.remove("stateInput");
8923            }
8924            if members.is_empty() {
8925                return Ok(());
8926            }
8927        }
8928        self.update_content(item.content_kind, &item.id, fields)
8929            .await
8930    }
8931
8932    /// Update one board item's content with exactly `fields` beside its id, through the
8933    /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
8934    /// a draft.
8935    ///
8936    /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
8937    /// is what lets a narrow write carry the one thing it changes and nothing else.
8938    async fn update_content(
8939        &self,
8940        kind: ContentKind,
8941        id: &NativeId,
8942        fields: Value,
8943    ) -> Result<(), SourceError> {
8944        let (operation, id_key, pointer) = match kind {
8945            ContentKind::DraftIssue => (
8946                graphql::UPDATE_DRAFT,
8947                "draftIssueId",
8948                "/updateProjectV2DraftIssue/draftIssue",
8949            ),
8950            ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
8951        };
8952        let mut input = fields;
8953        input[id_key] = json!(id.0);
8954        let data = self.graphql(operation, json!({"input":input})).await?;
8955        let returned = data
8956            .pointer(pointer)
8957            .ok_or_else(|| SourceError::Malformed {
8958                message: "GitHub item update returned no item".into(),
8959            })?;
8960        if required_str(returned, "id")? != id.0 {
8961            return Err(SourceError::Malformed {
8962                message: "GitHub item update returned the wrong item".into(),
8963            });
8964        }
8965        Ok(())
8966    }
8967
8968    /// Creates one issue, files it on the board, and reports what a read of it would say:
8969    /// its content id, its board item id, and the web address GitHub gave it.
8970    ///
8971    /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
8972    /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
8973    /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
8974    /// board item, and the `addProjectV2ItemById` that then had to follow was refused
8975    /// "Content already exists in this project". A terminal status is not written here:
8976    /// `finish_write` selects its option first and closes the issue after, so a close never
8977    /// lands on an item whose board cannot show it.
8978    ///
8979    /// The address and the number come back here because this is the only place either is
8980    /// known before GitHub's own board read catches up — an item this run created answers
8981    /// the reads that follow it out of the record below, and one remembered without them
8982    /// would report no location and no key for the rest of the run.
8983    async fn create_and_file_issue(
8984        &self,
8985        board_id: &str,
8986        repository: &RepositoryTarget,
8987        incoming: &Incoming<'_>,
8988        body: &Option<String>,
8989    ) -> Result<Landed, SourceError> {
8990        let repository_id = self.repository_id(repository, incoming).await?;
8991        let data = self
8992            .graphql(
8993                graphql::CREATE_ISSUE,
8994                json!({"input":{
8995                    "repositoryId":repository_id,"title":incoming.written_title(),"body":body
8996                }}),
8997            )
8998            .await?;
8999        let created = data
9000            .pointer("/createIssue/issue")
9001            .filter(|value| !value.is_null())
9002            .ok_or_else(|| SourceError::Malformed {
9003                message: "GitHub issue creation returned no issue".into(),
9004            })?;
9005        let content_id = NativeId(required_str(created, "id")?.to_owned());
9006        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
9007        // a response without it is not worth failing a landed write over — the item simply
9008        // reports no location until the board read catches up, which is what it did before.
9009        let url = optional_str(created, "url")?.map(str::to_owned);
9010        // The issue exists from here on, so an unreadable number and a refused board
9011        // filing below each try, best effort, to take it back: an issue in the repository
9012        // that is on no board is an item nobody asked for and nothing here would find again.
9013        //
9014        // Its number is optional on the same terms its address is — a landed write is not
9015        // worth failing over a member that came back missing, and such an item reports no
9016        // handle until a board read catches up. A number that is *present* and is not an
9017        // unsigned integer is still a response this source cannot read.
9018        let number = match created_issue_number(created) {
9019            Ok(number) => number,
9020            Err(error) => {
9021                let _ = self.delete_issue(&content_id).await;
9022                return Err(error);
9023            }
9024        };
9025        let added = match self
9026            .graphql(
9027                graphql::ADD_TO_BOARD,
9028                json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
9029            )
9030            .await
9031        {
9032            Ok(added) => added,
9033            Err(error) => {
9034                let _ = self.delete_issue(&content_id).await;
9035                return Err(error);
9036            }
9037        };
9038        let item = added
9039            .pointer("/addProjectV2ItemById/item")
9040            .filter(|value| !value.is_null())
9041            .ok_or_else(|| SourceError::Malformed {
9042                message: "GitHub board addition returned no project item".into(),
9043            })?;
9044        Ok(Landed {
9045            content_id,
9046            item_id: required_str(item, "id")?.to_owned(),
9047            url,
9048            number,
9049        })
9050    }
9051
9052    /// Move one issue under the project it now belongs to, or out of the one it left.
9053    async fn reparent(
9054        &self,
9055        held: Option<NativeId>,
9056        child: &NativeId,
9057        wanted: Option<&NativeId>,
9058    ) -> Result<(), SourceError> {
9059        if held.as_ref() == wanted {
9060            return Ok(());
9061        }
9062        if let Some(held) = &held {
9063            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
9064                .await?;
9065        }
9066        if let Some(wanted) = wanted {
9067            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
9068                .await?;
9069        }
9070        Ok(())
9071    }
9072
9073    async fn sub_issue(
9074        &self,
9075        operation: &str,
9076        parent: &NativeId,
9077        child: &NativeId,
9078        root: &str,
9079    ) -> Result<(), SourceError> {
9080        let data = self
9081            .graphql(
9082                operation,
9083                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
9084            )
9085            .await?;
9086        let issue =
9087            data.pointer(&format!("/{root}/issue"))
9088                .ok_or_else(|| SourceError::Malformed {
9089                    message: "GitHub sub-issue update returned no issue".into(),
9090                })?;
9091        let sub =
9092            data.pointer(&format!("/{root}/subIssue"))
9093                .ok_or_else(|| SourceError::Malformed {
9094                    message: "GitHub sub-issue update returned no sub-issue".into(),
9095                })?;
9096        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
9097            return Err(SourceError::Malformed {
9098                message: "GitHub sub-issue update returned the wrong issues".into(),
9099            });
9100        }
9101        Ok(())
9102    }
9103
9104    /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
9105    /// whether there was one.
9106    ///
9107    /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
9108    /// relationships are not read: there is nothing a read of them could find.
9109    async fn reconcile_blocked_by(
9110        &self,
9111        content_id: &NativeId,
9112        native: &[String],
9113        issue: Issue<'_>,
9114    ) -> Result<bool, SourceError> {
9115        let current = match issue {
9116            Issue::Created => Vec::new(),
9117            Issue::Existing(Some(held)) => held
9118                .iter()
9119                .map(|far| required_str(far, "id").map(str::to_owned))
9120                .collect::<Result<Vec<_>, _>>()?,
9121            Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
9122        };
9123        let mut changed = false;
9124        for (operation, far_id) in current
9125            .iter()
9126            .filter(|id| !native.contains(id))
9127            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
9128            .chain(
9129                native
9130                    .iter()
9131                    .filter(|id| !current.contains(id))
9132                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
9133            )
9134        {
9135            let data = self
9136                .graphql(
9137                    operation,
9138                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
9139                )
9140                .await?;
9141            let root = if operation == graphql::ADD_BLOCKED_BY {
9142                "addBlockedBy"
9143            } else {
9144                "removeBlockedBy"
9145            };
9146            let issue =
9147                data.pointer(&format!("/{root}/issue"))
9148                    .ok_or_else(|| SourceError::Malformed {
9149                        message: "GitHub dependency update returned no issue".into(),
9150                    })?;
9151            let blocker = data
9152                .pointer(&format!("/{root}/blockingIssue"))
9153                .ok_or_else(|| SourceError::Malformed {
9154                    message: "GitHub dependency update returned no blocking issue".into(),
9155                })?;
9156            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
9157            {
9158                return Err(SourceError::Malformed {
9159                    message: "GitHub dependency update returned the wrong issues".into(),
9160                });
9161            }
9162            changed = true;
9163        }
9164        Ok(changed)
9165    }
9166}
9167
9168/// What a write needs to know of one far end it names: which kind of item it is, and whether
9169/// it is an issue a native relationship can name.
9170struct FarEnd {
9171    kind: BoardKind,
9172    content_kind: ContentKind,
9173}
9174
9175/// Whether the issue one write reconciles was created by that write or was already there.
9176#[derive(Clone, Copy, PartialEq, Eq)]
9177enum Issue<'a> {
9178    /// Created by this write, so it holds no relationships yet.
9179    Created,
9180    /// On the board before this write, holding whatever relationships it holds — the far
9181    /// ends of its whole `blockedBy`, when the read that reached it carried them.
9182    Existing(Option<&'a [Value]>),
9183}
9184
9185/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
9186enum Reached {
9187    /// An issue this board holds, resolved into everything this source reports about it.
9188    Held(Box<Resolved>),
9189    /// Nothing this board holds: no such node, or a node on some other board.
9190    Nothing,
9191    /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
9192    /// again by [`GitHubProjectsSource::draft_by_id`].
9193    Draft,
9194}
9195
9196/// What GitHub says when a string is not a node id it can resolve.
9197///
9198/// Matched because it is the ordinary answer to a project selector naming a project by its
9199/// *name*, and reporting that as a failure would make naming one impossible. It is read
9200/// off the refusal GitHub sent, never guessed from the shape of the string: this source
9201/// does not define the syntax of a GitHub node id and would be wrong about it.
9202const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
9203
9204/// `error` with `note` added to the end of what it says, its kind and every other member
9205/// unchanged — so a caller still branches on the failure that happened, and reads beside it
9206/// what that failure left behind.
9207fn noting(error: SourceError, note: &str) -> SourceError {
9208    match error {
9209        SourceError::Config { message } => SourceError::Config {
9210            message: message + note,
9211        },
9212        SourceError::Auth { message } => SourceError::Auth {
9213            message: message + note,
9214        },
9215        SourceError::Refused { message } => SourceError::Refused {
9216            message: message + note,
9217        },
9218        SourceError::RateLimited {
9219            retry_after_seconds,
9220            message,
9221        } => SourceError::RateLimited {
9222            retry_after_seconds,
9223            message: Some(message.unwrap_or_default() + note),
9224        },
9225        SourceError::Unavailable { message } => SourceError::Unavailable {
9226            message: message + note,
9227        },
9228        SourceError::Malformed { message } => SourceError::Malformed {
9229            message: message + note,
9230        },
9231    }
9232}
9233
9234/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
9235/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
9236fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest) -> Value {
9237    let mut variables = serde_json::Map::new();
9238    for slot in 0..COPY_DETAIL_BATCH {
9239        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
9240        variables.insert(format!("id{slot}"), json!(id));
9241    }
9242    variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
9243    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
9244    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
9245    variables.insert("duplicates".to_owned(), json!(true));
9246    Value::Object(variables)
9247}
9248
9249/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
9250/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
9251/// for them.
9252///
9253/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
9254/// is read again at no added price.
9255fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
9256    let mut variables = serde_json::Map::new();
9257    for slot in 0..DETAIL_BATCH {
9258        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
9259        variables.insert(format!("id{slot}"), json!(id));
9260    }
9261    variables.insert(
9262        "first".to_owned(),
9263        json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
9264    );
9265    variables.insert("comments".to_owned(), json!(comments.is_some()));
9266    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
9267    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
9268    variables.insert("duplicates".to_owned(), json!(true));
9269    Value::Object(variables)
9270}
9271
9272/// Whether this refusal is GitHub saying the id names no node at all.
9273fn unresolvable_node(error: &SourceError) -> bool {
9274    matches!(error, SourceError::Refused { message }
9275        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
9276}
9277
9278/// One project name, as a search qualifier which filters on it at the server.
9279///
9280/// Quoted so the whole title is one phrase rather than a bag of words, with the two
9281/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
9282/// the way it documents. A title matched here is still compared for equality afterwards:
9283/// the qualifier narrows what the server sends, and this source decides what it names.
9284fn title_qualifier(name: &str) -> String {
9285    format!("in:title {}", quoted(name))
9286}
9287
9288/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
9289/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
9290/// it documents — so a value holding a qualifier's spelling is searched for rather than
9291/// obeyed.
9292fn quoted(value: &str) -> String {
9293    let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
9294    format!("\"{escaped}\"")
9295}
9296
9297/// The search qualifier for the issues updated at or after `since`.
9298///
9299/// Written to the second, rounded down, which can only widen what the search returns.
9300fn updated_qualifier(since: DateTime<Utc>) -> String {
9301    format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
9302}
9303
9304/// The search terms that narrow a board-scoped issue search to a task query's text and
9305/// metadata predicates, or `None` when it carries neither.
9306///
9307/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
9308/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
9309/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
9310/// matches each in any field the `in:` qualifier names, so a query naming a title search and
9311/// a metadata value searches both fields for both — wider than asked, never narrower, and
9312/// every candidate is confirmed in process afterwards.
9313///
9314/// **This narrows a text search, and that is this source's declared semantics.** GitHub
9315/// matches whole tokens where a substring rule would match inside a word, so an item holding
9316/// the text only inside a longer word is not returned. A text of nothing but whitespace
9317/// matches every item, so it narrows nothing and is not sent.
9318fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
9319    let text = query
9320        .text
9321        .as_ref()
9322        .filter(|text| !text.terms.trim().is_empty());
9323    if text.is_none() && query.metadata.is_empty() {
9324        return None;
9325    }
9326    let (title, body) = match text.map(|text| text.fields) {
9327        None => (false, true),
9328        Some(TextFields::Title) => (true, !query.metadata.is_empty()),
9329        Some(TextFields::Content) => (false, true),
9330        Some(TextFields::TitleOrContent) => (true, true),
9331    };
9332    let fields = match (title, body) {
9333        (true, true) => "in:title,body",
9334        (true, false) => "in:title",
9335        _ => "in:body",
9336    };
9337    let phrases = text
9338        .map(|text| text.terms.clone())
9339        .into_iter()
9340        .chain(
9341            query
9342                .metadata
9343                .iter()
9344                .map(|wanted| as_stored(wanted.value())),
9345        )
9346        .map(|phrase| quoted(&phrase))
9347        .collect::<Vec<_>>();
9348    Some(format!("{fields} {}", phrases.join(" ")))
9349}
9350
9351/// The search terms that narrow a board-scoped issue search to a project or document query's
9352/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
9353/// carrying that text alone is sent as by [`narrowing_qualifiers`].
9354fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
9355    narrowing_qualifiers(&TaskQuery {
9356        text: text.cloned(),
9357        ..TaskQuery::default()
9358    })
9359}
9360
9361/// Refuses a project or document query's text GitHub's issue search cannot find, before
9362/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
9363/// query's.
9364fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
9365    refuse_unsearchable(&TaskQuery {
9366        text: text.cloned(),
9367        ..TaskQuery::default()
9368    })
9369}
9370
9371/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
9372/// before anything is asked of GitHub.
9373///
9374/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
9375/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
9376/// left out, the search is every issue of the board. So this source says it cannot answer
9377/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
9378/// nothing GitHub could search for, and keeps the board read it always had.
9379fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
9380    const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
9381                       letter or digit with a bounded query";
9382    if let Some(text) = &query.text
9383        && !text.terms.trim().is_empty()
9384        && !has_words(&text.terms)
9385    {
9386        return Err(SourceError::Refused {
9387            message: format!(
9388                "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
9389                text.terms
9390            ),
9391        });
9392    }
9393    if let Some(wanted) = query
9394        .metadata
9395        .iter()
9396        .find(|wanted| !has_words(wanted.value()))
9397    {
9398        return Err(SourceError::Refused {
9399            message: format!(
9400                "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
9401                wanted.value(),
9402                std::iter::once(wanted.key())
9403                    .chain(wanted.path().iter().map(String::as_str))
9404                    .collect::<Vec<_>>()
9405                    .join("/"),
9406            ),
9407        });
9408    }
9409    Ok(())
9410}
9411
9412/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
9413fn has_words(phrase: &str) -> bool {
9414    phrase.chars().any(char::is_alphanumeric)
9415}
9416
9417/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
9418///
9419/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
9420/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
9421/// which GitHub's word match would read as different words.
9422fn as_stored(value: &str) -> String {
9423    let encoded = Value::String(value.to_owned()).to_string();
9424    encoded[1..encoded.len() - 1].to_owned()
9425}
9426
9427/// The one narrower question a task query carrying a text, metadata or origin predicate is
9428/// sent as.
9429enum Narrowing {
9430    /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
9431    Origin(String),
9432    /// The board-scoped issue search narrowed by these qualifiers.
9433    Search(String),
9434}
9435
9436impl Narrowing {
9437    /// What this question is remembered under for the length of one command.
9438    fn key(&self) -> String {
9439        match self {
9440            Self::Origin(origin) => format!("origin {origin}"),
9441            Self::Search(also) => format!("search {also}"),
9442        }
9443    }
9444}
9445
9446/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
9447enum Resumed {
9448    /// It reported another page, which starts after this cursor.
9449    More(String),
9450    /// It has ended. Sending this cursor again — the page's own end when it had one, and
9451    /// otherwise the cursor it was reached from — answers an empty page, so the one document
9452    /// can go on walking the other connection.
9453    Ended(Option<String>),
9454}
9455
9456impl Resumed {
9457    /// Whether the connection has another page.
9458    const fn has_more(&self) -> bool {
9459        matches!(self, Self::More(_))
9460    }
9461
9462    /// The cursor to send this connection next.
9463    fn cursor(self) -> Option<String> {
9464        match self {
9465            Self::More(next) => Some(next),
9466            Self::Ended(last) => last,
9467        }
9468    }
9469}
9470
9471/// Where `connection`, reached from `after`, resumes — refused when it reports another page
9472/// with no cursor to it, or from a cursor that does not advance.
9473fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
9474    let info = connection
9475        .get("pageInfo")
9476        .ok_or_else(|| SourceError::Malformed {
9477            message: "GitHub connection has no pageInfo".into(),
9478        })?;
9479    let end = optional_str(info, "endCursor")?;
9480    if required_bool(info, "hasNextPage")? {
9481        let next = end.ok_or_else(|| SourceError::Malformed {
9482            message: "GitHub connection reports another page and no endCursor".into(),
9483        })?;
9484        validate_cursor_progress(after, next)?;
9485        return Ok(Resumed::More(next.to_owned()));
9486    }
9487    Ok(Resumed::Ended(
9488        end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
9489    ))
9490}
9491
9492/// The board, and every item on it this source reports.
9493#[derive(Clone)]
9494struct Board {
9495    id: String,
9496    fields: Value,
9497    items: Vec<Resolved>,
9498}
9499
9500/// What a write needs of the board and nothing more: its node id and its field
9501/// definitions, in the shape a read of the board's own `fields` gives them.
9502///
9503/// Deliberately no items. A write decides which item it writes, which parent it files
9504/// under and which far ends it names by reading each of them by its own id; this is the
9505/// half of the board those reads cannot carry, and holding no item is what keeps it from
9506/// ever being asked whether an item is there.
9507#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
9508struct BoardFields {
9509    id: BoardId,
9510    fields: Value,
9511}
9512
9513/// A board's node id: what a field write and `addProjectV2ItemById` address.
9514///
9515/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
9516/// refused where it is read, and one an item names blank is read as not named at all.
9517#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
9518#[serde(try_from = "String")]
9519struct BoardId(String);
9520
9521impl TryFrom<String> for BoardId {
9522    type Error = String;
9523
9524    /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
9525    fn try_from(id: String) -> Result<Self, Self::Error> {
9526        Self::parse(&id).map_err(|error| error.to_string())
9527    }
9528}
9529
9530/// Where one write left its item, for the record the rest of the command reads it out of.
9531///
9532/// A named record rather than a tuple because the update arm and the create arm each fill
9533/// all four, and two `Option`s of different meaning side by side in a tuple are two
9534/// positions a reader has to count.
9535struct Landed {
9536    /// The issue's own node id, which is the [`NativeId`] this source reports.
9537    content_id: NativeId,
9538    /// The board item's id, which is what a field write addresses.
9539    // 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.
9540    item_id: String,
9541    /// The web address GitHub gave the issue, when it gave one.
9542    // 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.
9543    url: Option<String>,
9544    /// The issue's number on its repository, when GitHub reported one.
9545    number: Option<u64>,
9546}
9547
9548impl BoardId {
9549    fn parse(id: &str) -> Result<Self, SourceError> {
9550        if id.trim().is_empty() {
9551            return Err(SourceError::Malformed {
9552                message: "GitHub named a board with a blank node id".into(),
9553            });
9554        }
9555        Ok(Self(id.to_owned()))
9556    }
9557
9558    fn as_str(&self) -> &str {
9559        &self.0
9560    }
9561}
9562
9563impl Board {
9564    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
9565        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
9566        let nodes = fields
9567            .get("nodes")
9568            .and_then(Value::as_array)
9569            .ok_or_else(|| SourceError::Malformed {
9570                message: "GitHub project fields.nodes is not an array".into(),
9571            })?;
9572        Ok(nodes
9573            .iter()
9574            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
9575    }
9576}
9577
9578/// One board item, resolved into everything this source reports about it.
9579///
9580/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
9581/// [`TaskSource::prime_write`] takes back — see [`expected`].
9582#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
9583struct Resolved {
9584    item_id: String,
9585    id: NativeId,
9586    content_kind: ContentKind,
9587    kind: BoardKind,
9588    title: String,
9589    body: Option<String>,
9590    /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
9591    /// that changes the slot alone has to keep byte for byte outside it.
9592    raw_body: Option<String>,
9593    status: Status,
9594    /// The name of the board `Status` option this item sits in, as the board spells it.
9595    option: Option<String>,
9596    /// What its `Priority` field says, read through this instance's mapping.
9597    priority: HeldPriority,
9598    /// Whether this item's issue is closed. A draft has no such state and is never closed.
9599    closed: bool,
9600    /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
9601    delivers: Vec<TaskRef>,
9602    /// Every task that delivers this one, read out of its slot. Empty for anything not a
9603    /// task.
9604    delivered_by: Vec<TaskRef>,
9605    labels: Vec<Label>,
9606    parent: Option<NativeId>,
9607    // 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.
9608    origin: Option<String>,
9609    /// What each board text field this instance projects metadata onto holds for this item,
9610    /// by field name — only the fields holding a value, an empty text included. What a write compares the
9611    /// item's metadata against, so a field already holding its value is not written again;
9612    /// never read back as metadata, which the slot alone reports.
9613    projected: BTreeMap<String, String>,
9614    /// The issue's own number on its repository, as GitHub reports it.
9615    ///
9616    /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
9617    /// declares none, and a draft is not filed in a repository to be numbered by one — and
9618    /// an issue this run created whose creating mutation answered without one, which is a
9619    /// response GitHub's own schema says cannot happen and which a landed write is not
9620    /// worth failing over. An `Issue` read off the board always has one.
9621    number: Option<u64>,
9622    url: Option<String>,
9623    created_at: Option<DateTime<Utc>>,
9624    updated_at: Option<DateTime<Utc>>,
9625    /// When the board item itself last changed — what a board field write moves — when the
9626    /// read that reached it said.
9627    item_updated_at: Option<DateTime<Utc>>,
9628    /// How many comments the issue holds, when the read that reached it said.
9629    comment_count: Option<u64>,
9630    /// How many relationship events the issue's timeline holds — every dependency, as
9631    /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
9632    /// removed — when the read that reached it said.
9633    relationship_events: Option<u64>,
9634    /// The server's clock on the response that read it — its `Date` header — when the
9635    /// response carried one.
9636    served_at: Option<DateTime<Utc>>,
9637    own_repository: Option<Repository>,
9638    repositories: Vec<Repository>,
9639    classification: Classification,
9640    slot: BTreeMap<String, Value>,
9641    /// The node id of the board this item sits on, when the read that reached it said.
9642    board_id: Option<String>,
9643    /// The definition of every board field this item holds a value of, in the shape a read
9644    /// of the board's own `fields` gives one.
9645    ///
9646    /// Only the fields this item has a value in: a field it holds nothing of is not here,
9647    /// which says nothing about whether the board has it.
9648    fields: Vec<Value>,
9649    /// Every field the board this item sits on defines, as its own read of the board's
9650    /// `fields` gives them — when the read that reached the item carried them, which a read
9651    /// of it by its own id does. What a write of it needs of the board, then, needs no read
9652    /// of the board.
9653    board_fields: Option<Value>,
9654    /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
9655    /// selects one — when the read that reached it carried the connection to its end, which a
9656    /// read of it by its own id does for any issue blocked by no more than a page. What a
9657    /// write reconciles that relationship against, and what a read of its forward edges in
9658    /// the same command answers with.
9659    blocked_by: Option<Vec<Value>>,
9660}
9661
9662impl Resolved {
9663    /// The board this item's own read names it on, when that read named one this source can
9664    /// address.
9665    fn named_board(&self) -> Option<BoardId> {
9666        self.board_id
9667            .as_deref()
9668            .and_then(|id| BoardId::parse(id).ok())
9669    }
9670
9671    /// The board's id and every field it defines, when the read that reached this item
9672    /// carried both — which a read of it by its own id does.
9673    fn carried_board(&self) -> Option<BoardFields> {
9674        Some(BoardFields {
9675            id: self.named_board()?,
9676            fields: self.board_fields.clone()?,
9677        })
9678    }
9679
9680    /// Whether this item holds a value of the board field called `name`, and so carries
9681    /// that field's definition. `false` says nothing about whether the board has the field.
9682    fn defines(&self, name: &str) -> bool {
9683        self.fields
9684            .iter()
9685            .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
9686    }
9687
9688    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
9689    /// in a field of its own, and none of the five keys that are only an encoding.
9690    ///
9691    /// The two delivery keys are left out for every kind, not only for a task: they are
9692    /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
9693    /// document carrying one holds nothing a caller's own metadata could mean by it.
9694    fn metadata(&self) -> BTreeMap<String, Value> {
9695        let mut metadata = self.slot.clone();
9696        metadata.remove(Repository::METADATA_KEY);
9697        metadata.remove(DependencyEdge::RECORDED_KEY);
9698        metadata.remove(ItemKind::METADATA_KEY);
9699        metadata.remove(TaskRef::DELIVERS_KEY);
9700        metadata.remove(TaskRef::DELIVERED_BY_KEY);
9701        metadata.remove(Classification::METADATA_KEY);
9702        // The board field is the origin, and the body's copy of it is only a mirror for the
9703        // issue search to find: an item whose field holds none has none, whatever its body
9704        // says, so no reader ever sees two answers.
9705        metadata.remove(ORIGIN_KEY);
9706        if let Some(origin) = &self.origin {
9707            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
9708        }
9709        metadata
9710    }
9711
9712    /// Where this item is, as a link a reader can open.
9713    ///
9714    /// A board is a hosted place and every issue on it has a web address, so that address
9715    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
9716    /// of place it is, so a reader knows to open it rather than to read a file out. It
9717    /// does not replace or derive from `url`: the field goes on reporting exactly what it
9718    /// reported before, and this says what that address *is*.
9719    ///
9720    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
9721    /// rather than a third variant, which is the contract's "the source did not say". An
9722    /// issue this run created is not one of those: its address comes back from the
9723    /// creating mutation, so it is somewhere a reader can open from the moment it exists
9724    /// rather than from whenever the board read catches up.
9725    fn location(&self) -> Option<Location> {
9726        self.url.clone().map(Location::Url)
9727    }
9728
9729    /// The short handle this board's backend shows people for a task: the issue's number
9730    /// alone, as a decimal string.
9731    ///
9732    /// The number alone rather than `owner/repo#1043`, because that is the contract's
9733    /// value for this backend. A draft has no number and so no handle, which is the
9734    /// contract's *absent* rather than a handle of some other shape — and the native
9735    /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
9736    /// derives from.
9737    fn key(&self) -> Option<String> {
9738        self.number.map(|number| number.to_string())
9739    }
9740
9741    /// This item's change token — see [`change_token`] — or `None` when the read that
9742    /// reached it did not carry all five of its parts, which a draft never does, did not say
9743    /// when GitHub served it, or found it changed too recently to have settled.
9744    fn version(&self) -> Option<String> {
9745        match self.content_kind {
9746            ContentKind::Issue => {
9747                let (issue, item) = (self.updated_at?, self.item_updated_at?);
9748                let parts = change_token::Parts {
9749                    issue,
9750                    item,
9751                    comments: self.comment_count?,
9752                    relationships: self.relationship_events?,
9753                    repository: self.own_repository.clone()?,
9754                };
9755                change_token::settled(issue, item, self.served_at?)
9756                    .then(|| change_token::spell(&parts))
9757            }
9758            ContentKind::DraftIssue => None,
9759        }
9760    }
9761
9762    /// Whether its `Priority` field holds a value at all, mapped or not.
9763    fn holds_priority(&self) -> bool {
9764        self.priority != HeldPriority::Read(Priority::None)
9765    }
9766
9767    /// The task this item is.
9768    ///
9769    /// Fails for an item whose `Priority` field holds an option the mapping does not name:
9770    /// reading that as a level would be a guess, and reading it as `none` would let the next
9771    /// copy clear a priority a person set.
9772    fn task(&self) -> Result<Task, SourceError> {
9773        let priority = match &self.priority {
9774            HeldPriority::Read(priority) => *priority,
9775            HeldPriority::Unmapped(option) => {
9776                return Err(SourceError::Malformed {
9777                    message: format!(
9778                        "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
9779                         this source's priority_mapping does not name, so its priority cannot be \
9780                         read; next: name {option:?} under priority_mapping, or move the item to \
9781                         a mapped option",
9782                        self.id,
9783                        self.number
9784                            .map(|number| format!(" (#{number})"))
9785                            .unwrap_or_default()
9786                    ),
9787                });
9788            }
9789        };
9790        Ok(Task {
9791            version: self.version(),
9792            id: self.id.clone(),
9793            key: self.key(),
9794            title: self.title.clone(),
9795            content: self.body.clone(),
9796            status: self.status.clone(),
9797            priority,
9798            labels: self.labels.clone(),
9799            project: self.parent.clone(),
9800            url: self.url.clone(),
9801            location: self.location(),
9802            created_at: self.created_at,
9803            updated_at: self.updated_at,
9804            metadata: self.metadata(),
9805            repositories: self.repositories.clone(),
9806            delivers: self.delivers.clone(),
9807            delivered_by: self.delivered_by.clone(),
9808            classification: self.classification,
9809        })
9810    }
9811
9812    fn project(&self) -> Project {
9813        Project {
9814            id: self.id.clone(),
9815            title: self.title.clone(),
9816            content: self.body.clone(),
9817            status: self.status.clone(),
9818            labels: self.labels.clone(),
9819            url: self.url.clone(),
9820            location: self.location(),
9821            created_at: self.created_at,
9822            updated_at: self.updated_at,
9823            metadata: self.metadata(),
9824            repositories: self.repositories.clone(),
9825            classification: self.classification,
9826        }
9827    }
9828
9829    /// The same issue as a document: the project it is filed under, and no status and no
9830    /// dependencies, because a document is not work.
9831    fn document(&self) -> Document {
9832        Document {
9833            id: self.id.clone(),
9834            title: self.title.clone(),
9835            content: self.body.clone(),
9836            project: self.parent.clone(),
9837            labels: self.labels.clone(),
9838            url: self.url.clone(),
9839            location: self.location(),
9840            created_at: self.created_at,
9841            updated_at: self.updated_at,
9842            metadata: self.metadata(),
9843            repositories: self.repositories.clone(),
9844            classification: self.classification,
9845        }
9846    }
9847}
9848
9849/// Where one targeted update moves an item's status, and which of its two halves move.
9850struct StatusMove {
9851    /// The board the item's `Status` field is on.
9852    board: BoardId,
9853    /// The `Status` field's id.
9854    field: String,
9855    /// The option's id.
9856    option: String,
9857    /// The option's name, as the board spells it.
9858    name: String,
9859    /// What the status asks of the issue's state.
9860    target: StatusTarget,
9861    /// The status the item reads as once it is there.
9862    landed: Status,
9863    /// Which of the status's two halves differ from what the item holds.
9864    moves: Moves,
9865}
9866
9867/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
9868/// closed state of its issue, or both. A status neither half of which differs is no move at all,
9869/// and is not a value of this type.
9870#[derive(Clone, Copy, PartialEq, Eq)]
9871enum Moves {
9872    /// The option alone.
9873    Option,
9874    /// The issue's state alone: open, closed, or closed with another reason.
9875    State,
9876    /// Both.
9877    Both,
9878}
9879
9880impl Moves {
9881    /// What differs, or `None` when nothing does.
9882    const fn of(option: bool, state: bool) -> Option<Self> {
9883        match (option, state) {
9884            (true, true) => Some(Self::Both),
9885            (true, false) => Some(Self::Option),
9886            (false, true) => Some(Self::State),
9887            (false, false) => None,
9888        }
9889    }
9890
9891    /// Whether the option moves.
9892    const fn option(self) -> bool {
9893        matches!(self, Self::Option | Self::Both)
9894    }
9895
9896    /// Whether the issue's state moves.
9897    const fn state(self) -> bool {
9898        matches!(self, Self::State | Self::Both)
9899    }
9900}
9901
9902/// What one write is, and the status that comes with being it.
9903///
9904/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
9905/// status and a task or a project always has one, so "a document carrying a status" and
9906/// "a task carrying none" are states a write cannot be in rather than states every use
9907/// site below has to defend against.
9908enum Written<'a> {
9909    /// A document, which is not work and so has no status at all.
9910    Document,
9911    /// A task or a project, and the status it is being written with.
9912    Work(ItemKind, &'a Status),
9913}
9914
9915impl Written<'_> {
9916    /// Which of the board's three kinds this write is.
9917    const fn kind(&self) -> BoardKind {
9918        match self {
9919            Self::Document => BoardKind::Document,
9920            Self::Work(kind, _) => BoardKind::Work(*kind),
9921        }
9922    }
9923
9924    /// The status this write carries. A document carries none, so a write of one says
9925    /// nothing about the issue's open or closed state and selects no board `Status`
9926    /// option.
9927    const fn status(&self) -> Option<&Status> {
9928        match self {
9929            Self::Document => None,
9930            Self::Work(_, status) => Some(status),
9931        }
9932    }
9933
9934    /// The status this write carries with the kind whose half of `status_mapping` it is
9935    /// written through.
9936    const fn work_status(&self) -> Option<(ItemKind, &Status)> {
9937        match self {
9938            Self::Document => None,
9939            Self::Work(kind, status) => Some((*kind, status)),
9940        }
9941    }
9942}
9943
9944/// The item being written, in the one shape all three write methods reach.
9945struct Incoming<'a> {
9946    written: Written<'a>,
9947    /// The title a person wrote. A document's goes onto the issue with
9948    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
9949    title: &'a str,
9950    content: Option<&'a str>,
9951    assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
9952    labels: &'a [Label],
9953    metadata: &'a BTreeMap<String, Value>,
9954    repositories: &'a [Repository],
9955    /// Recorded in the slot while private, and nowhere while public.
9956    classification: Classification,
9957    parent: Option<&'a NativeId>,
9958    /// [`Task::delivers`], already checked. Empty for a project or a document, which is
9959    /// what keeps either key out of their slot.
9960    delivers: &'a [TaskRef],
9961    /// [`Task::delivered_by`], already checked. Empty for a project or a document.
9962    delivered_by: &'a [TaskRef],
9963    /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
9964    /// project, a document, and every write to an instance with no `priority_mapping` —
9965    /// which is what keeps such a write's requests exactly what they were before.
9966    priority: Option<Priority>,
9967}
9968
9969/// What one write does to one projected metadata text field, by the field's id.
9970#[derive(Debug, Clone)]
9971enum ProjectionWrite {
9972    /// Write this text.
9973    Set {
9974        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.
9975        text: String,
9976    },
9977    /// Clear the field's value.
9978    Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
9979}
9980
9981impl ProjectionWrite {
9982    /// Add this write to a field write's list of values and list of clears.
9983    fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
9984        match self {
9985            Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
9986            Self::Clear { field } => clears.push(field),
9987        }
9988    }
9989}
9990
9991/// What one write does to an item's `Priority` field.
9992enum PriorityWrite {
9993    /// Select this option of this field.
9994    Select {
9995        /// The `Priority` field's id.
9996        field: String,
9997        /// The mapped option's id.
9998        option: String,
9999    },
10000    /// Clear the field's value, which is what `none` is.
10001    Clear {
10002        /// The `Priority` field's id.
10003        field: String,
10004    },
10005}
10006
10007impl Incoming<'_> {
10008    /// The title this write puts on the issue.
10009    fn written_title(&self) -> String {
10010        match self.written {
10011            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
10012            Written::Work(..) => self.title.to_owned(),
10013        }
10014    }
10015}
10016
10017#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
10018enum ContentKind {
10019    DraftIssue,
10020    Issue,
10021}
10022
10023/// What one board issue is: a document, or the work an [`ItemKind`] names.
10024///
10025/// A type of this source's own rather than an `ItemKind` with a third variant, because
10026/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
10027/// document — the contract keeps a document out of that enum deliberately. Holding the
10028/// board's three answers in one value is what makes every place that asks "which is this?"
10029/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
10030/// two thirds of the board.
10031#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
10032enum BoardKind {
10033    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
10034    Document,
10035    /// Every other issue, and every draft.
10036    Work(ItemKind),
10037}
10038
10039impl BoardKind {
10040    /// Whose half of `status_mapping` an item of this kind reads its status through. A
10041    /// document has no status of its own, so the task half stands in for whatever the issue
10042    /// holds; nothing reports it.
10043    const fn status_kind(self) -> ItemKind {
10044        match self {
10045            Self::Document => ItemKind::Task,
10046            Self::Work(kind) => kind,
10047        }
10048    }
10049
10050    /// How a refusal names this kind to the person reading it.
10051    const fn describes(self) -> &'static str {
10052        match self {
10053            Self::Document => "document",
10054            Self::Work(kind) => kind.marker(),
10055        }
10056    }
10057}
10058
10059/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
10060///
10061/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
10062/// the shared cross-source journeys assert one answer to one question, so two sources
10063/// that disagree about what "carries the label bug" means fail them.
10064fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
10065    let holds = |name: &String| {
10066        labels
10067            .iter()
10068            .any(|label| label.name.eq_ignore_ascii_case(name))
10069    };
10070    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
10071        && filter.all_of.iter().all(holds)
10072        && !filter.none_of.iter().any(holds)
10073}
10074
10075/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
10076/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
10077fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
10078    statuses.is_empty() || statuses.contains(&category)
10079}
10080
10081/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
10082///
10083/// `content` is the item's own prose — the body with this source's trailing metadata
10084/// comment already taken off — so a search never matches an encoding the author of the
10085/// issue never wrote.
10086fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
10087    let terms = query.terms.to_lowercase();
10088    let in_title = title.to_lowercase().contains(&terms);
10089    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
10090    match query.fields {
10091        TextFields::Title => in_title,
10092        TextFields::Content => in_content,
10093        TextFields::TitleOrContent => in_title || in_content,
10094    }
10095}
10096
10097/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
10098///
10099/// The project predicate is passed separately because a read narrowed to one project has
10100/// already answered it by asking *that project* for its own items — and re-applying it
10101/// there would compare the caller's selector, which may be a project's **name**, against
10102/// the id of the project that name resolved to, and keep nothing. Every other read passes
10103/// `query.project` and applies it here, which is what keeps `projects` a predicate this
10104/// source really does apply.
10105fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
10106    labels_match(&task.labels, &query.labels)
10107        && status_matches(task.status.category, &query.statuses)
10108        && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
10109        && match project {
10110            ProjectFilter::Any => true,
10111            ProjectFilter::Orphans => task.project.is_none(),
10112            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
10113        }
10114        && query
10115            .text
10116            .as_ref()
10117            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
10118        // Against the parsed metadata slot, and against the origin field, which is where
10119        // `Resolved::metadata` reads each of them from.
10120        && query.metadata_matches(&task.metadata)
10121        && query.origin_matches(&task.metadata)
10122}
10123
10124fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
10125    labels_match(&project.labels, &query.labels)
10126        && status_matches(project.status.category, &query.statuses)
10127        && query
10128            .text
10129            .as_ref()
10130            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
10131}
10132
10133/// The same three predicates a task query carries, minus the status filter.
10134///
10135/// A document is not work, so it has no status for one to compare against and the query
10136/// type carries none. The project predicate is the same one — a design issue filed under a
10137/// project issue is in that project, and one filed under nothing is in none — so it is
10138/// spelled the same way here rather than answered differently.
10139fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
10140    labels_match(&document.labels, &query.labels)
10141        && match project {
10142            ProjectFilter::Any => true,
10143            ProjectFilter::Orphans => document.project.is_none(),
10144            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
10145        }
10146        && query
10147            .text
10148            .as_ref()
10149            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
10150}
10151
10152#[async_trait::async_trait]
10153impl TaskSource for GitHubProjectsSource {
10154    fn kind(&self) -> &'static str {
10155        KIND
10156    }
10157    fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
10158        change_token::order(a, b)
10159    }
10160    /// Every part: §4.13d of `docs/plugin-protocol.md` states, member by member, the change
10161    /// each part of the token moves for, comments included.
10162    fn version_coverage(&self) -> VersionCoverage {
10163        VersionCoverage::whole()
10164    }
10165    fn write_context(&self, id: &NativeId) -> Option<Value> {
10166        self.context_of(id)
10167    }
10168    fn board_context(&self) -> Option<Value> {
10169        self.board_context_held()
10170    }
10171    fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
10172        self.prime(id, context, board)
10173    }
10174    fn unprime(&self, id: &NativeId) {
10175        self.unprime_held(id);
10176    }
10177    fn begin_journal(&self) {
10178        self.expected.begin();
10179    }
10180    fn end_journal(&self) -> Option<WriteJournal> {
10181        Some(self.expected.end())
10182    }
10183    fn capabilities(&self) -> Capabilities {
10184        Capabilities {
10185            projects: Support::Native,
10186            documents: Support::Native,
10187            comments: Support::Native,
10188            assets: Support::Native,
10189            priority: if self.priorities.is_some() {
10190                Support::Native
10191            } else {
10192                Support::Unsupported
10193            },
10194            filter_by_priority: Support::Native,
10195            filter_by_comment_activity: Support::Native,
10196            filter_by_metadata: Support::Native,
10197            filter_by_origin: Support::Native,
10198            orphan_tasks: Support::Native,
10199            filter_by_label: Support::Native,
10200            filter_by_status: Support::Native,
10201            search_title: Support::Native,
10202            search_content: Support::Native,
10203            task_dependencies: DependencySupport::BothDirections,
10204            project_dependencies: DependencySupport::BothDirections,
10205            max_page_size: MAX_PAGE_SIZE,
10206        }
10207    }
10208    async fn visibility(
10209        &self,
10210        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
10211    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
10212        self.write_visibility(target).await
10213    }
10214    async fn health(&self) -> Result<Health, SourceError> {
10215        let board = self.board_page(None, 1).await?;
10216        Ok(Health {
10217            reachable: true,
10218            detail: Some(format!(
10219                "reading GitHub project {}/{} ({})",
10220                self.owner,
10221                self.project_number,
10222                required_str(&board, "title")?
10223            )),
10224        })
10225    }
10226    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
10227        self.item_by_id(id)
10228            .await?
10229            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
10230            .map(|item| item.task())
10231            .transpose()
10232    }
10233    async fn task_assets(
10234        &self,
10235        id: &NativeId,
10236    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
10237        self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
10238    }
10239    async fn task_asset(
10240        &self,
10241        id: &NativeId,
10242        name: &onetaskgraph_plugin_api::AssetName,
10243    ) -> Result<Option<Vec<u8>>, SourceError> {
10244        self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
10245            .await
10246    }
10247    async fn set_task_rendering_with_assets(
10248        &self,
10249        id: &NativeId,
10250        content: &str,
10251        provenance: &Value,
10252        _answers: &BTreeMap<String, Value>,
10253        assets: &onetaskgraph_plugin_api::AssetWrite,
10254    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
10255        self.replace_rendering(
10256            id,
10257            BoardKind::Work(ItemKind::Task),
10258            content,
10259            provenance,
10260            Some(assets),
10261        )
10262        .await
10263    }
10264    async fn document_assets(
10265        &self,
10266        id: &NativeId,
10267    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
10268        self.held_assets(id, BoardKind::Document).await
10269    }
10270    async fn document_asset(
10271        &self,
10272        id: &NativeId,
10273        name: &onetaskgraph_plugin_api::AssetName,
10274    ) -> Result<Option<Vec<u8>>, SourceError> {
10275        self.held_asset(id, BoardKind::Document, name).await
10276    }
10277    async fn set_document_rendering_with_assets(
10278        &self,
10279        id: &NativeId,
10280        content: &str,
10281        provenance: &Value,
10282        _answers: &BTreeMap<String, Value>,
10283        assets: &onetaskgraph_plugin_api::AssetWrite,
10284    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
10285        self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
10286            .await
10287    }
10288    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
10289        Ok(self
10290            .item_by_id(id)
10291            .await?
10292            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
10293            .map(|item| item.project()))
10294    }
10295    async fn query_tasks(
10296        &self,
10297        query: &TaskQuery,
10298        page: &PageRequest,
10299    ) -> Result<Page<Task>, SourceError> {
10300        validate_page(page)?;
10301        refuse_unsearchable(query)?;
10302        if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
10303            let qualifiers = match (narrowing_qualifiers(query), query.commented_since) {
10304                (Some(also), Some(since)) => Some(format!("{} {also}", updated_qualifier(since))),
10305                (Some(also), None) => Some(also),
10306                (None, Some(since)) => Some(updated_qualifier(since)),
10307                (None, None) => None,
10308            };
10309            if let Some(also) = qualifiers {
10310                return self.search_tasks(query, page, &also).await;
10311            }
10312        }
10313
10314        // A read narrowed to one project asks that project for its own tasks, so nothing
10315        // about it costs what the rest of the board holds. A read carrying a text, metadata
10316        // or origin predicate asks GitHub the narrower question those predicates are, and a
10317        // read narrowed to comment activity alone asks the board's own issue search for the
10318        // issues updated since, which is every issue a comment could have been written or
10319        // edited on since. Every other task read is a question about the whole board and is
10320        // answered by reading it.
10321        let (held, membership) = match (&query.project, query.commented_since) {
10322            (ProjectFilter::Is(project), _) => (
10323                self.project_children(project).await?,
10324                // Answered by where these items came from; see `task_matches`.
10325                &ProjectFilter::Any,
10326            ),
10327            (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
10328                match (self.narrowed(query).await?, since) {
10329                    (Some(narrowed), _) => (narrowed, &query.project),
10330                    (None, Some(since)) => (self.updated_since(since).await?, &query.project),
10331                    (None, None) => (self.board().await?.items, &query.project),
10332                }
10333            }
10334        };
10335        // Filtered before paged: a page of a filtered result is a page of the survivors,
10336        // never the survivors of a page.
10337        let mut tasks = Vec::new();
10338        for item in held
10339            .iter()
10340            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
10341        {
10342            let task = item.task()?;
10343            if task_matches(&task, query, membership)
10344                && self.commented_since(item, query.commented_since).await?
10345            {
10346                tasks.push(task);
10347            }
10348        }
10349        Ok(offset_page(
10350            tasks,
10351            numeric_cursor(page.cursor.as_ref())?,
10352            page.limit.min(MAX_PAGE_SIZE) as usize,
10353        ))
10354    }
10355    async fn query_projects(
10356        &self,
10357        query: &ProjectQuery,
10358        page: &PageRequest,
10359    ) -> Result<Page<Project>, SourceError> {
10360        validate_page(page)?;
10361        refuse_unsearchable_text(query.text.as_ref())?;
10362        // The projects a board holds are found by an issue search scoped to that board,
10363        // never by walking the board's own item connection: what tells a project from a
10364        // task is the `parent` each issue carries, which costs nothing to read. A query
10365        // carrying a text asks that search for the text too, so it reads the issues that
10366        // hold it rather than every issue of the board.
10367        let held = match self.text_searched(query.text.as_ref()).await? {
10368            Some(searched) => searched,
10369            None => self.board_issues().await?,
10370        };
10371        let projects = held
10372            .iter()
10373            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
10374            .map(Resolved::project)
10375            .filter(|project| project_matches(project, query))
10376            .collect();
10377        Ok(offset_page(
10378            projects,
10379            numeric_cursor(page.cursor.as_ref())?,
10380            page.limit.min(MAX_PAGE_SIZE) as usize,
10381        ))
10382    }
10383    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
10384        Ok(self
10385            .item_by_id(id)
10386            .await?
10387            .filter(|item| item.kind == BoardKind::Document)
10388            .map(|item| item.document()))
10389    }
10390    async fn query_documents(
10391        &self,
10392        query: &DocumentQuery,
10393        page: &PageRequest,
10394    ) -> Result<Page<Document>, SourceError> {
10395        validate_page(page)?;
10396        // Narrowed to one project, this is the same sub-issue read a task list scoped to
10397        // that project makes — a document filed under a project is a sub-issue of it too,
10398        // and which of them come back is the kind this caller asked for. Unscoped, a query
10399        // carrying a text asks the board-scoped issue search for it, as a task query does,
10400        // and only one carrying none reads the board.
10401        let (held, membership) = match &query.project {
10402            ProjectFilter::Is(project) => (
10403                self.project_children(project).await?,
10404                // Answered by where these items came from; see `task_matches`.
10405                &ProjectFilter::Any,
10406            ),
10407            ProjectFilter::Any | ProjectFilter::Orphans => {
10408                refuse_unsearchable_text(query.text.as_ref())?;
10409                match self.text_searched(query.text.as_ref()).await? {
10410                    Some(searched) => (searched, &query.project),
10411                    None => (self.board().await?.items, &query.project),
10412                }
10413            }
10414        };
10415        // Filtered before paged, exactly as a task read is: a page of a filtered result is
10416        // a page of the survivors, never the survivors of a page.
10417        let documents = held
10418            .iter()
10419            .filter(|item| item.kind == BoardKind::Document)
10420            .map(Resolved::document)
10421            .filter(|document| document_matches(document, query, membership))
10422            .collect();
10423        Ok(offset_page(
10424            documents,
10425            numeric_cursor(page.cursor.as_ref())?,
10426            page.limit.min(MAX_PAGE_SIZE) as usize,
10427        ))
10428    }
10429    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
10430        validate_page(page)?;
10431        let offset = numeric_cursor(page.cursor.as_ref())?;
10432        let mut labels = self
10433            .board()
10434            .await?
10435            .items
10436            .into_iter()
10437            .flat_map(|item| item.labels)
10438            .fold(Vec::new(), |mut all, label| {
10439                if !all.iter().any(|x: &Label| x.id == label.id) {
10440                    all.push(label);
10441                }
10442                all
10443            });
10444        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
10445        Ok(offset_page(
10446            labels,
10447            offset,
10448            page.limit.min(MAX_PAGE_SIZE) as usize,
10449        ))
10450    }
10451    async fn task_dependencies(
10452        &self,
10453        id: &NativeId,
10454        direction: Direction,
10455        page: &PageRequest,
10456    ) -> Result<Page<DependencyEdge>, SourceError> {
10457        self.dependencies(id, ItemKind::Task, direction, page).await
10458    }
10459    async fn project_dependencies(
10460        &self,
10461        id: &NativeId,
10462        direction: Direction,
10463        page: &PageRequest,
10464    ) -> Result<Page<DependencyEdge>, SourceError> {
10465        self.dependencies(id, ItemKind::Project, direction, page)
10466            .await
10467    }
10468
10469    fn writes(&self) -> WriteSupport {
10470        WriteSupport::Supported
10471    }
10472
10473    /// Create or update one task.
10474    ///
10475    /// Its `delivers` and `delivered_by` are checked before anything is read or written —
10476    /// neither may name the task itself or name one task twice — and land in the body's
10477    /// metadata slot under their reserved keys, in place of any caller metadata of those
10478    /// names.
10479    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
10480        self.write_task_assets(write, None)
10481            .await
10482            .map(|written| written.id)
10483    }
10484
10485    async fn write_task_with_assets(
10486        &self,
10487        write: &ItemWrite<Task>,
10488        _answers: Option<&BTreeMap<String, Value>>,
10489        assets: &onetaskgraph_plugin_api::AssetWrite,
10490    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
10491        self.write_task_assets(write, Some(assets)).await
10492    }
10493
10494    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
10495        self.write_item(
10496            &Incoming {
10497                written: Written::Work(ItemKind::Project, &write.item.status),
10498                title: &write.item.title,
10499                content: write.item.content.as_deref(),
10500                assets: None,
10501                labels: &write.item.labels,
10502                metadata: &write.item.metadata,
10503                repositories: &write.item.repositories,
10504                classification: write.item.classification,
10505                parent: None,
10506                delivers: &[],
10507                delivered_by: &[],
10508                priority: None,
10509            },
10510            write.target.as_ref(),
10511            &write.depends_on,
10512        )
10513        .await
10514        .map(|written| written.id)
10515    }
10516
10517    /// Create or update one document, which is one issue titled the way this board spells
10518    /// a document.
10519    ///
10520    /// Everything else is exactly a task write: caller metadata goes to the same canonical
10521    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
10522    /// or a field this board cannot carry is refused by name rather than dropped, a target
10523    /// naming an issue this board does not hold is refused rather than created, and an
10524    /// issue this call created is taken back when the rest of the write fails.
10525    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
10526        self.write_document_assets(write, None)
10527            .await
10528            .map(|written| written.id)
10529    }
10530
10531    async fn write_document_with_assets(
10532        &self,
10533        write: &ItemWrite<Document>,
10534        _answers: Option<&BTreeMap<String, Value>>,
10535        assets: &onetaskgraph_plugin_api::AssetWrite,
10536    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
10537        self.write_document_assets(write, Some(assets)).await
10538    }
10539
10540    /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
10541    /// which reads nothing; then the board's `Status` option. Over an existing item that is
10542    /// read off the item, as the write reads it, and the item is held among this command's
10543    /// resolved records so the write that follows reuses that read rather than repeating it;
10544    /// an item that does not carry the field takes the board's fields, which are held once
10545    /// read. A create is checked against the board's fields only when this command already
10546    /// holds them, because a create reads them together with its repository, in one request,
10547    /// and refuses a missing option before it writes anything.
10548    async fn check_status_write(
10549        &self,
10550        kind: ItemKind,
10551        category: StatusCategory,
10552        target: Option<&NativeId>,
10553    ) -> Result<(), SourceError> {
10554        let status = self.resolved_target(kind, category)?;
10555        if status.option().is_none() {
10556            return Ok(());
10557        }
10558        let fields = match target {
10559            Some(target) => {
10560                // A target this board does not hold is the write's own refusal to make.
10561                let Some(item) = self.bound_item(target).await? else {
10562                    return Ok(());
10563                };
10564                self.resolved_cache()?.insert(target.clone(), item.clone());
10565                self.fields_for(Some(&item), true, false).await?.fields
10566            }
10567            None => {
10568                let held = self
10569                    .board_cache()?
10570                    .as_ref()
10571                    .map(|board| board.fields.clone());
10572                match held.or_else(|| {
10573                    self.fields_cache()
10574                        .ok()
10575                        .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
10576                }) {
10577                    Some(fields) => fields,
10578                    None => return Ok(()),
10579                }
10580            }
10581        };
10582        self.column_for(&fields, kind, category, &status)
10583            .map(|_| ())
10584    }
10585
10586    /// Set one task's status alone.
10587    ///
10588    /// An open target reopens a closed issue with an `updateIssue` carrying only its
10589    /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
10590    /// terminal target selects its mapped option, then closes with its fixed reason. No
10591    /// request carries a title, a body or a label. The status
10592    /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
10593    /// what a re-read reports.
10594    async fn set_task_status(
10595        &self,
10596        id: &NativeId,
10597        category: StatusCategory,
10598    ) -> Result<Option<Status>, SourceError> {
10599        self.set_status(id, category).await
10600    }
10601
10602    /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
10603    /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
10604    /// for `none`. Refused by an instance with no `priority_mapping`.
10605    async fn set_task_priority(
10606        &self,
10607        id: &NativeId,
10608        priority: Priority,
10609    ) -> Result<Option<Priority>, SourceError> {
10610        self.set_priority(id, priority).await
10611    }
10612
10613    /// Replace one task's content with a single body update that keeps the metadata slot
10614    /// byte for byte.
10615    async fn set_task_content(
10616        &self,
10617        id: &NativeId,
10618        content: &str,
10619    ) -> Result<Option<()>, SourceError> {
10620        self.replace_content(id, content).await
10621    }
10622
10623    /// Replace one task issue's content and its provenance slot entry with a single body
10624    /// update. The answers are not kept: see `replace_rendering`.
10625    async fn set_task_rendering(
10626        &self,
10627        id: &NativeId,
10628        content: &str,
10629        provenance: &Value,
10630        _answers: &BTreeMap<String, Value>,
10631    ) -> Result<Option<()>, SourceError> {
10632        self.replace_rendering(
10633            id,
10634            BoardKind::Work(ItemKind::Task),
10635            content,
10636            provenance,
10637            None,
10638        )
10639        .await
10640        .map(|written| written.map(|_| ()))
10641    }
10642
10643    /// Replace one design-document issue's content and its provenance slot entry, on exactly
10644    /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
10645    async fn set_document_rendering(
10646        &self,
10647        id: &NativeId,
10648        content: &str,
10649        provenance: &Value,
10650        _answers: &BTreeMap<String, Value>,
10651    ) -> Result<Option<()>, SourceError> {
10652        self.replace_rendering(id, BoardKind::Document, content, provenance, None)
10653            .await
10654            .map(|written| written.map(|_| ()))
10655    }
10656
10657    /// Replace one project issue's content and its provenance slot entry, on exactly the
10658    /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
10659    async fn set_project_rendering(
10660        &self,
10661        id: &NativeId,
10662        content: &str,
10663        provenance: &Value,
10664        _answers: &BTreeMap<String, Value>,
10665    ) -> Result<Option<()>, SourceError> {
10666        self.replace_rendering(
10667            id,
10668            BoardKind::Work(ItemKind::Project),
10669            content,
10670            provenance,
10671            None,
10672        )
10673        .await
10674        .map(|written| written.map(|_| ()))
10675    }
10676
10677    /// Apply a targeted update with one read of the item and a write only for what differs:
10678    /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
10679    /// and last one `updateIssue` for title, body and state. See `targeted_update`.
10680    async fn update_task(
10681        &self,
10682        id: &NativeId,
10683        update: &TaskUpdate,
10684    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
10685        self.targeted_update(id, update).await
10686    }
10687
10688    /// Replace one task's `delivered_by` with a single body update that changes the
10689    /// metadata slot and nothing outside it.
10690    async fn set_delivered_by(
10691        &self,
10692        id: &NativeId,
10693        delivered_by: &[TaskRef],
10694    ) -> Result<Option<()>, SourceError> {
10695        self.replace_delivered_by(id, delivered_by).await
10696    }
10697
10698    /// Set one key of one task issue's metadata with a single body update that changes the
10699    /// metadata slot and nothing outside it — no title, label, state or board field request —
10700    /// and sends nothing when the task already holds that value under the key.
10701    async fn set_task_metadata(
10702        &self,
10703        id: &NativeId,
10704        key: &MetadataKey,
10705        value: &Value,
10706    ) -> Result<Option<Task>, SourceError> {
10707        Ok(self
10708            .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
10709            .await?
10710            .map(|item| item.task())
10711            .transpose()?)
10712    }
10713
10714    /// Set one key of one project issue's metadata, on exactly the terms of
10715    /// [`set_task_metadata`](TaskSource::set_task_metadata).
10716    async fn set_project_metadata(
10717        &self,
10718        id: &NativeId,
10719        key: &MetadataKey,
10720        value: &Value,
10721    ) -> Result<Option<Project>, SourceError> {
10722        Ok(self
10723            .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
10724            .await?
10725            .map(|item| item.project()))
10726    }
10727
10728    /// Set one key of one design-document issue's metadata, on exactly the terms of
10729    /// [`set_task_metadata`](TaskSource::set_task_metadata).
10730    async fn set_document_metadata(
10731        &self,
10732        id: &NativeId,
10733        key: &MetadataKey,
10734        value: &Value,
10735    ) -> Result<Option<Document>, SourceError> {
10736        Ok(self
10737            .set_slot_key(id, BoardKind::Document, key, value)
10738            .await?
10739            .map(|item| item.document()))
10740    }
10741
10742    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
10743        self.delete_item(id).await
10744    }
10745
10746    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
10747        self.delete_item(id).await
10748    }
10749
10750    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
10751        self.delete_item(id).await
10752    }
10753
10754    /// One page of the task issue's own comments, walked by GitHub's own cursor.
10755    ///
10756    /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
10757    /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
10758    ///
10759    /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
10760    /// board is the read of its comments. A draft this process already resolved is refused
10761    /// without one.
10762    async fn task_comments(
10763        &self,
10764        task: &NativeId,
10765        page: &PageRequest,
10766    ) -> Result<Option<Page<Comment>>, SourceError> {
10767        validate_page(page)?;
10768        let cached = self.resolved_cache()?.get(task).cloned();
10769        if let Some(item) = cached {
10770            if item.kind != BoardKind::Work(ItemKind::Task) {
10771                return Ok(None);
10772            }
10773            if item.content_kind == ContentKind::DraftIssue {
10774                return Err(self.draft_has_no_comments(task));
10775            }
10776        }
10777        match self.issue_detail(task, page).await? {
10778            Some(TaskDetailRead {
10779                comments: Some(comments),
10780                ..
10781            }) => comments,
10782            _ => Ok(None),
10783        }
10784    }
10785
10786    /// Every id's task, with the first page of its comments when `comments` names it:
10787    /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
10788    /// comments in one [`graphql::ISSUE_DETAIL`] request.
10789    async fn get_task_details(
10790        &self,
10791        ids: &[NativeId],
10792        comments: Option<&PageRequest>,
10793    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10794        if let Some(page) = comments
10795            && let Err(error) = validate_page(page)
10796        {
10797            return ids.iter().map(|_| Err(error.clone())).collect();
10798        }
10799        match (ids, comments) {
10800            ([id], Some(page)) => vec![self.issue_detail(id, page).await],
10801            ([id], None) => vec![self.task_read(id).await],
10802            _ => self.issue_details(ids, comments).await,
10803        }
10804    }
10805
10806    /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
10807    /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
10808    /// request, so the dependency read a copy makes of each next is answered from this one.
10809    async fn get_task_details_for_copy(
10810        &self,
10811        ids: &[NativeId],
10812        comments: &PageRequest,
10813    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10814        // A page past the first is a page of one task's comments, which the detail read of it
10815        // alone answers.
10816        if comments.cursor.is_some() {
10817            return self.get_task_details(ids, Some(comments)).await;
10818        }
10819        if let Err(error) = validate_page(comments) {
10820            return ids.iter().map(|_| Err(error.clone())).collect();
10821        }
10822        let mut read = Vec::with_capacity(ids.len());
10823        for batch in ids.chunks(COPY_DETAIL_BATCH) {
10824            match self
10825                .graphql(
10826                    graphql::ISSUE_COPY_DETAILS,
10827                    copy_detail_batch(batch, comments),
10828                )
10829                .await
10830            {
10831                Ok(data) => {
10832                    for (slot, id) in batch.iter().enumerate() {
10833                        let read_one = match data.get(format!("i{slot}")) {
10834                            Some(node) => self.detail_of(id, node, true, None).await,
10835                            None => Err(SourceError::Malformed {
10836                                message: format!(
10837                                    "GitHub answered a batch read with no item for {}",
10838                                    id.0
10839                                ),
10840                            }),
10841                        };
10842                        read.push(read_one);
10843                    }
10844                }
10845                Err(error) if unresolvable_node(&error) => {
10846                    for id in batch {
10847                        read.push(self.issue_detail(id, comments).await);
10848                    }
10849                }
10850                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
10851            }
10852        }
10853        read
10854    }
10855
10856    /// Add one comment to the task's issue, as the account the token belongs to.
10857    ///
10858    /// The author is refused before anything is sent — not even the task is read — because
10859    /// no answer GitHub could give would make posting under another name than the one asked
10860    /// for the right outcome.
10861    async fn add_comment(
10862        &self,
10863        task: &NativeId,
10864        comment: &NewComment,
10865    ) -> Result<Option<Comment>, SourceError> {
10866        if let Some(author) = &comment.author {
10867            return Err(SourceError::Refused {
10868                message: format!(
10869                    "source {} cannot post a comment as {author:?}: GitHub records the account \
10870                     the token signs in as the author of every comment; next: leave --author \
10871                     out, and the comment is posted as that account",
10872                    self.name
10873                ),
10874            });
10875        }
10876        let Some(issue) = self.commented_issue(task).await? else {
10877            return Ok(None);
10878        };
10879        let data = self
10880            .graphql(
10881                graphql::ADD_COMMENT,
10882                json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
10883            )
10884            .await?;
10885        let subject = data
10886            .pointer("/addComment/subject")
10887            .filter(|value| !value.is_null())
10888            .ok_or_else(|| SourceError::Malformed {
10889                message: "GitHub comment addition returned no subject".into(),
10890            })?;
10891        if required_str(subject, "id")? != issue.0 {
10892            return Err(SourceError::Malformed {
10893                message: "GitHub comment addition answered about another issue".into(),
10894            });
10895        }
10896        let added = data
10897            .pointer("/addComment/commentEdge/node")
10898            .filter(|value| !value.is_null())
10899            .ok_or_else(|| SourceError::Malformed {
10900                message: "GitHub comment addition returned no comment".into(),
10901            })?;
10902        let added = comment_from(added)?;
10903        self.remember_commented(&issue)?;
10904        Ok(Some(added))
10905    }
10906
10907    async fn edit_comment(
10908        &self,
10909        task: &NativeId,
10910        comment: &NativeId,
10911        body: &CommentBody,
10912    ) -> Result<Option<Comment>, SourceError> {
10913        let Some(issue) = self.commented_issue(task).await? else {
10914            return Ok(None);
10915        };
10916        let Some(held) = self.comment_on(&issue, comment).await? else {
10917            return Ok(None);
10918        };
10919        // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
10920        let body = CommentOrigin::kept(&held, body)?;
10921        let data = self
10922            .graphql(
10923                graphql::UPDATE_COMMENT,
10924                json!({"input":{"id":comment.0,"body":body.as_str()}}),
10925            )
10926            .await?;
10927        let edited = data
10928            .pointer("/updateIssueComment/issueComment")
10929            .filter(|value| !value.is_null())
10930            .ok_or_else(|| SourceError::Malformed {
10931                message: "GitHub comment update returned no comment".into(),
10932            })?;
10933        let edited = comment_from(edited)?;
10934        if edited.id != *comment {
10935            return Err(SourceError::Malformed {
10936                message: "GitHub comment update returned the wrong comment".into(),
10937            });
10938        }
10939        self.remember_commented(&issue)?;
10940        Ok(Some(edited))
10941    }
10942
10943    async fn delete_comment(
10944        &self,
10945        task: &NativeId,
10946        comment: &NativeId,
10947    ) -> Result<Option<NativeId>, SourceError> {
10948        let Some(issue) = self.commented_issue(task).await? else {
10949            return Ok(None);
10950        };
10951        if !self.comment_is_on(&issue, comment).await? {
10952            return Ok(None);
10953        }
10954        let data = self
10955            .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
10956            .await?;
10957        // The payload says nothing about the comment it removed, so what is checked is that
10958        // GitHub answered the mutation at all rather than leaving it unanswered.
10959        data.get("deleteIssueComment")
10960            .filter(|value| !value.is_null())
10961            .ok_or_else(|| SourceError::Malformed {
10962                message: "GitHub comment deletion returned no payload".into(),
10963            })?;
10964        Ok(Some(comment.clone()))
10965    }
10966
10967    /// Every request this source has recorded, and what each of GitHub's two budgets was
10968    /// attributed — read off the same accounting the session report is rendered from, so
10969    /// the two cannot count one request two ways.
10970    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
10971        Ok(Some(self.ledger.snapshot().metering()))
10972    }
10973
10974    /// Drop every item, search answer and board read this source holds, so the next command
10975    /// reads the board as a person has since left it.
10976    ///
10977    /// Every one of those is held on the assumption that nothing but this source writes the
10978    /// board while a command runs, which stops being true the moment the command is over: a
10979    /// body a person edited would be overwritten from the record held here, and a card they
10980    /// moved would be read as still where this source left it. The board's own field
10981    /// definitions go too, because a person can add or delete a `Status` option and a write
10982    /// resolved against the held list would not re-read on a miss. What stays is what stays
10983    /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
10984    /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
10985    /// accounting [`metering`](TaskSource::metering) answers from.
10986    ///
10987    /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
10988    /// refused, because clearing it is what puts it right.
10989    async fn end_command(&self) -> Result<(), SourceError> {
10990        fn clear<T: Default>(held: &Mutex<T>) {
10991            *held
10992                .lock()
10993                .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
10994            held.clear_poison();
10995        }
10996        clear(&self.created);
10997        clear(&self.updated);
10998        clear(&self.commented);
10999        clear(&self.board_cache);
11000        clear(&self.search_cache);
11001        clear(&self.narrowed_cache);
11002        clear(&self.search_next);
11003        clear(&self.resolved_cache);
11004        clear(&self.children_cache);
11005        clear(&self.fields_cache);
11006        self.expected.end_command();
11007        Ok(())
11008    }
11009}
11010
11011/// Each project's sub-issues as one command read them, keyed by the selector they were asked
11012/// for under, beside the project that selector named; see `children_cache`.
11013type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
11014
11015/// One issue comment as the contract carries it.
11016///
11017/// `author` is absent both when GitHub answers `null` for an account that no longer exists
11018/// and when it answers an actor with no login, because either way the source did not say who
11019/// wrote it — which is what an absent author means, rather than an author called nothing.
11020///
11021/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
11022/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
11023/// and is read back with the marker taken out and reported as its origin.
11024fn comment_from(value: &Value) -> Result<Comment, SourceError> {
11025    Ok(Comment {
11026        origin: None,
11027        id: NativeId(required_str(value, "id")?.to_owned()),
11028        author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
11029            .map(str::to_owned),
11030        created_at: optional_time(value, "createdAt")?,
11031        updated_at: optional_time(value, "updatedAt")?,
11032        body: required_str(value, "body")?.to_owned(),
11033        url: optional_str(value, "url")?.map(str::to_owned),
11034    }
11035    .unmarked())
11036}
11037
11038/// The page of comments one issue node carries, resumed from `after`.
11039fn comment_page(
11040    node: &Value,
11041    issue: &str,
11042    after: Option<&str>,
11043) -> Result<Page<Comment>, SourceError> {
11044    let connection = node
11045        .get("comments")
11046        .filter(|value| !value.is_null())
11047        .ok_or_else(|| SourceError::Malformed {
11048            message: format!("GitHub issue {issue} answered with no comments connection"),
11049        })?;
11050    let items = optional_nodes(Some(connection), "issue comments")?
11051        .into_iter()
11052        .flatten()
11053        .map(comment_from)
11054        .collect::<Result<Vec<_>, _>>()?;
11055    let next = next_cursor(connection)?;
11056    if let Some(next) = &next {
11057        validate_cursor_progress(after, &next.0)?;
11058    }
11059    Ok(Page { items, next })
11060}
11061
11062/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
11063/// end — `None` when it carried none, or a page with more past it.
11064fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
11065    let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
11066        return Ok(None);
11067    };
11068    if next_cursor(connection)?.is_some() {
11069        return Ok(None);
11070    }
11071    Ok(Some(
11072        optional_nodes(Some(connection), "blocked-by issues")?
11073            .into_iter()
11074            .flatten()
11075            .cloned()
11076            .collect(),
11077    ))
11078}
11079
11080/// Where the recorded tail of a dependency walk resumes; see
11081/// [`GitHubProjectsSource::recorded_edges`].
11082const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
11083
11084/// The board text field this source keeps a copy's origin in.
11085///
11086/// Named after the key it holds, and held to that name by the guard below rather than by
11087/// a reader noticing.
11088const ORIGIN_FIELD: &str = "onetaskgraph.origin";
11089
11090/// The metadata key that field holds.
11091///
11092/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
11093/// constructs or interprets the qualified id it carries. This source names it only to
11094/// route it — a short, typed value belongs in a typed field rather than in the body slot
11095/// a caller's own prose shares.
11096///
11097/// Restated rather than imported, because no plugin crate may depend on the engine. What
11098/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
11099/// target in `check`: it reads the engine's own literal and fails naming the file and the
11100/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
11101/// that creates a second item every run instead of finding the one it wrote — and that is
11102/// too late to learn it.
11103const ORIGIN_KEY: &str = "onetaskgraph.origin";
11104
11105/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
11106///
11107/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
11108/// is derived from the far end, never written down on the near item — so only a forward
11109/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
11110/// it did not come from, and it is told so rather than answered with an empty page that
11111/// reads as a walk which ended.
11112fn recorded_offset(
11113    cursor: Option<&str>,
11114    direction: Direction,
11115) -> Result<Option<usize>, SourceError> {
11116    cursor
11117        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
11118        .map(|offset| {
11119            if direction != Direction::DependsOn {
11120                return Err(SourceError::Config {
11121                    message: format!(
11122                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
11123                         reverse dependency read never issues; resume it in the direction \
11124                         that reported it"
11125                    ),
11126                });
11127            }
11128            offset.parse().map_err(|_| SourceError::Config {
11129                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
11130            })
11131        })
11132        .transpose()
11133}
11134
11135fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
11136    let mut page = offset_page(edges, offset, limit.max(1));
11137    page.next = page
11138        .next
11139        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
11140    page
11141}
11142
11143/// The kind of one issue reached through a dependency connection.
11144///
11145/// The same questions the board scan asks, over the fields the dependency document
11146/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
11147/// then anything with sub-issues or the marker is a project.
11148///
11149/// # Errors
11150///
11151/// A far end this board holds as a document is refused rather than reported. The two
11152/// answers that are not refusals would both be wrong: reporting it as a task names an id
11153/// no task read of this source can find, and reporting it as a project names one no
11154/// project read can. There is no third value to return — `ItemKind` has no document
11155/// variant, because nothing may point at a document — so the relationship itself is what
11156/// the person is told about.
11157fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
11158    let id = required_str(value, "id")?;
11159    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
11160        return Err(SourceError::Refused {
11161            message: format!(
11162                "GitHub issue {id} is a document of this board — its title begins \
11163                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
11164                 on by one; next: remove that issue's blocking relationship on this board"
11165            ),
11166        });
11167    }
11168    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
11169    if parent.is_some() {
11170        return Ok(ItemKind::Task);
11171    }
11172    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
11173    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
11174        message: format!("GitHub issue {id}: {message}"),
11175    })?;
11176    let sub_issues = sub_issue_total(value)?;
11177    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
11178        ItemKind::Project
11179    } else {
11180        ItemKind::Task
11181    })
11182}
11183
11184/// The `IssueStateUpdateInput` one status target asks for.
11185///
11186/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
11187/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
11188/// a currently-closed issue: without that the item would read back `Unknown` and a copy
11189/// would report a change forever. A document has no status at all, and asks for neither.
11190fn state_input(target: Option<&StatusTarget>) -> Value {
11191    match target {
11192        Some(StatusTarget::Terminal(_, reason)) => {
11193            json!({"value":"CLOSED","stateReason":reason.reason()})
11194        }
11195        Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
11196        // A document has no status, so a write of one says nothing about the issue's open
11197        // or closed state rather than forcing it open: `stateInput` is what carries that
11198        // instruction, and an explicit null asks for no change to it.
11199        None => Value::Null,
11200    }
11201}
11202
11203/// The metadata one write stores in the item's body slot.
11204///
11205/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
11206/// rather than carried: the kind marker so an empty project stays readable, the
11207/// repository list only when it is not exactly the issue's own repository, and the far
11208/// ends no relationship here can name.
11209///
11210/// The copy origin is the one typed field that is also mirrored here, and only as a
11211/// mirror: it lands in the board's origin field as well, which stays the one every reader
11212/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
11213/// and catches up with a write in seconds rather than minutes — can find the item by it.
11214/// A reader of the release before this one drops the slot's copy and reads the field, so an
11215/// item written here still reads with exactly one origin there.
11216fn slot_metadata(
11217    incoming: &Incoming<'_>,
11218    own_repository: Option<&Repository>,
11219    fallback: &[DependencyEdge],
11220) -> BTreeMap<String, Value> {
11221    let mut metadata = incoming.metadata.clone();
11222    match metadata.remove(ORIGIN_KEY) {
11223        Some(Value::String(origin)) if !origin.is_empty() => {
11224            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
11225        }
11226        _ => {}
11227    }
11228    match incoming.written.kind() {
11229        BoardKind::Work(kind) => metadata.insert(
11230            ItemKind::METADATA_KEY.to_owned(),
11231            Value::String(kind.marker().to_owned()),
11232        ),
11233        // A document is told by its title, so it carries no kind marker: that key names
11234        // what a dependency endpoint points at, and nothing may point at a document.
11235        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
11236    };
11237    incoming.classification.record(&mut metadata);
11238    let derivable = own_repository
11239        .map(|own| incoming.repositories == [own.clone()])
11240        .unwrap_or(incoming.repositories.is_empty());
11241    if derivable {
11242        metadata.remove(Repository::METADATA_KEY);
11243    } else {
11244        metadata.insert(
11245            Repository::METADATA_KEY.to_owned(),
11246            Value::Array(
11247                incoming
11248                    .repositories
11249                    .iter()
11250                    .map(|repository| Value::String(repository.as_str().to_owned()))
11251                    .collect(),
11252            ),
11253        );
11254    }
11255    // The typed lists are what land, whatever the caller's own metadata held under their
11256    // keys: a key of either name travelling beside the field would otherwise be a second
11257    // answer to the same question, and the field is the one the contract names.
11258    for (key, entries) in [
11259        (TaskRef::DELIVERS_KEY, incoming.delivers),
11260        (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
11261    ] {
11262        set_task_list(&mut metadata, key, entries);
11263    }
11264    record_edges(&mut metadata, fallback);
11265    metadata
11266}
11267
11268/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
11269/// one slot's metadata, or no such key when there are none.
11270fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
11271    if fallback.is_empty() {
11272        metadata.remove(DependencyEdge::RECORDED_KEY);
11273    } else {
11274        metadata.insert(
11275            DependencyEdge::RECORDED_KEY.to_owned(),
11276            Value::Array(
11277                fallback
11278                    .iter()
11279                    .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
11280                    .collect(),
11281            ),
11282        );
11283    }
11284}
11285
11286/// Every label one item carries, from its content's own connection and nowhere else.
11287///
11288/// There is no second place to read one from: no document this source sends selects the
11289/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
11290/// cannot carry one at all. The module documentation records the three schema facts that
11291/// settle it.
11292fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
11293    optional_nodes(content.get("labels"), "content labels")?
11294        .into_iter()
11295        .flatten()
11296        .map(|v| {
11297            Ok(Label {
11298                id: NativeId(required_str(v, "id")?.to_owned()),
11299                name: required_str(v, "name")?.to_owned(),
11300                color: optional_str(v, "color")?.map(str::to_owned),
11301            })
11302        })
11303        .collect()
11304}
11305
11306/// The definition of each board field one item's values are values of, in the shape a read
11307/// of the board's own `fields` gives one.
11308///
11309/// A value names its field through a fragment on that field's own type, so the type is
11310/// known from which kind of value it is: a single-select value's field is a
11311/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
11312/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
11313fn field_definitions(field_values: &[Value]) -> Vec<Value> {
11314    field_values
11315        .iter()
11316        .filter_map(|value| {
11317            let field = value.get("field")?.as_object()?;
11318            field.get("id")?.as_str().filter(|id| !id.is_empty())?;
11319            let typename = if value.get("text").is_some() {
11320                "ProjectV2Field"
11321            } else if value.get("name").is_some() {
11322                "ProjectV2SingleSelectField"
11323            } else {
11324                return None;
11325            };
11326            let mut defined = field.clone();
11327            defined.insert("__typename".to_owned(), json!(typename));
11328            // Only a text field holds a text value, so the value says the field's type too.
11329            if typename == "ProjectV2Field" {
11330                defined.insert("dataType".to_owned(), json!("TEXT"));
11331            }
11332            Some(Value::Object(defined))
11333        })
11334        .collect()
11335}
11336
11337fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
11338    let Some(node) = field_values
11339        .iter()
11340        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
11341    else {
11342        return Ok(None);
11343    };
11344    Ok(optional_str(node, "text")?.map(str::to_owned))
11345}
11346
11347fn valid_github_owner(owner: &str) -> bool {
11348    !owner.is_empty()
11349        && owner.len() <= 39
11350        && !owner.starts_with('-')
11351        && !owner.ends_with('-')
11352        && !owner.contains("--")
11353        && owner
11354            .bytes()
11355            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
11356}
11357
11358/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
11359/// neither of the two names a path segment already means.
11360fn valid_github_repository_name(name: &str) -> bool {
11361    !name.is_empty()
11362        && name.len() <= 100
11363        && name != "."
11364        && name != ".."
11365        && name
11366            .bytes()
11367            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
11368}
11369
11370fn valid_environment_name(name: &str) -> bool {
11371    let mut bytes = name.bytes();
11372    bytes
11373        .next()
11374        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
11375        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
11376}
11377
11378/// How many sub-issues one issue has.
11379///
11380/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
11381/// absent or non-integer one is a response this source cannot read — and reading it as
11382/// zero would classify a project as a task, which is exactly the mistake the marker
11383/// exists to keep from happening quietly.
11384fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
11385    let summary = issue
11386        .get("subIssuesSummary")
11387        .ok_or_else(|| SourceError::Malformed {
11388            message: "GitHub issue is missing subIssuesSummary".into(),
11389        })?;
11390    summary
11391        .get("total")
11392        .and_then(Value::as_u64)
11393        .ok_or_else(|| SourceError::Malformed {
11394            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
11395        })
11396}
11397
11398/// One issue's own `number`.
11399///
11400/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
11401/// an issue in this module asks for it. So a read of one that comes back without it, or
11402/// with something that is not an unsigned integer, is a response this source cannot read —
11403/// absence here is **not** "this issue has no number". A draft is the content that has
11404/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
11405/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
11406fn issue_number(issue: &Value) -> Result<u64, SourceError> {
11407    issue
11408        .get("number")
11409        .and_then(Value::as_u64)
11410        .ok_or_else(|| SourceError::Malformed {
11411            message: "GitHub issue number is missing or is not an unsigned integer".into(),
11412        })
11413}
11414
11415/// The `number` a creating mutation answered with, and `None` when it answered without one;
11416/// why a missing one is tolerated is at the call in `create_and_file_issue`.
11417fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
11418    match created.get("number") {
11419        None | Some(Value::Null) => Ok(None),
11420        Some(value) => value
11421            .as_u64()
11422            .map(Some)
11423            .ok_or_else(|| SourceError::Malformed {
11424                message: "GitHub created issue number is not an unsigned integer".into(),
11425            }),
11426    }
11427}
11428
11429fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
11430    value
11431        .get(field)
11432        .and_then(Value::as_str)
11433        .ok_or_else(|| SourceError::Malformed {
11434            message: format!("GitHub response is missing string field {field}"),
11435        })
11436}
11437
11438fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
11439    let found = required_str(value, field)?;
11440    if found.trim().is_empty() {
11441        return Err(SourceError::Malformed {
11442            message: format!("GitHub response has blank string field {field}"),
11443        });
11444    }
11445    Ok(found)
11446}
11447
11448/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
11449/// needs one — Linear spells them too, in its own description field.
11450///
11451/// Restated rather than shared, because a plugin crate depends on the contract crate and
11452/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
11453/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
11454/// source round-trips its own writes perfectly well under its own spelling.
11455const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
11456const METADATA_CLOSE: &str = "\n-->";
11457
11458/// What the composer puts between a non-empty visible body and the slot, and the one thing
11459/// the parser takes off the visible body when it takes the slot off — exactly once, so every
11460/// other trailing byte of the body comes back as it was written.
11461// 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.
11462const METADATA_SEPARATOR: &str = "\n\n";
11463
11464/// The visible body and the metadata slot at the end of it.
11465///
11466/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
11467/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
11468/// own content and is left alone. The visible body is everything before the slot less the
11469/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
11470fn metadata_body(
11471    body: Option<String>,
11472) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
11473    let Some(body) = body else {
11474        return Ok((None, BTreeMap::new()));
11475    };
11476    let Some(slot) = slot_span(&body)? else {
11477        return Ok((Some(body), BTreeMap::new()));
11478    };
11479    let metadata =
11480        serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
11481            SourceError::Malformed {
11482                message: format!(
11483                    "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
11484                ),
11485            }
11486        })?;
11487    let before = &body[..slot.start];
11488    let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
11489    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
11490}
11491
11492/// Where the metadata slot sits in one body, as byte offsets into it.
11493struct SlotSpan {
11494    /// Where [`METADATA_OPEN`] begins.
11495    start: usize,
11496    /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
11497    encoded_start: usize,
11498    /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
11499    encoded_end: usize,
11500    /// Just past [`METADATA_CLOSE`].
11501    end: usize,
11502}
11503
11504/// The slot at the very end of `body`, or `None` when it has none.
11505///
11506/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
11507/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
11508/// slot.
11509fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
11510    let Some(start) = body.rfind(METADATA_OPEN) else {
11511        return Ok(None);
11512    };
11513    let encoded_start = start + METADATA_OPEN.len();
11514    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
11515        return Err(SourceError::Malformed {
11516            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
11517        });
11518    };
11519    let encoded_end = encoded_start + relative_end;
11520    let end = encoded_end + METADATA_CLOSE.len();
11521    if !body[end..].trim().is_empty() {
11522        return Ok(None);
11523    }
11524    Ok(Some(SlotSpan {
11525        start,
11526        encoded_start,
11527        encoded_end,
11528        end,
11529    }))
11530}
11531
11532/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
11533/// slot as it was.
11534///
11535/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
11536/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
11537/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
11538/// or alone in an empty body — and a body with no slot that is given no metadata is
11539/// returned as it is.
11540fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
11541    let encoded = if metadata.is_empty() {
11542        None
11543    } else {
11544        Some(
11545            serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
11546                message: error.to_string(),
11547            })?,
11548        )
11549    };
11550    Ok(match (slot_span(body)?, encoded) {
11551        (Some(slot), Some(encoded)) => format!(
11552            "{}{encoded}{}",
11553            &body[..slot.encoded_start],
11554            &body[slot.encoded_end..]
11555        ),
11556        (Some(slot), None) => {
11557            let before = &body[..slot.start];
11558            format!(
11559                "{}{}",
11560                before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
11561                &body[slot.end..]
11562            )
11563        }
11564        (None, None) => body.to_owned(),
11565        (None, Some(encoded)) if body.is_empty() => {
11566            format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11567        }
11568        (None, Some(encoded)) => {
11569            format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11570        }
11571    })
11572}
11573
11574/// `body` with everything before its metadata slot replaced by `content`, and the slot
11575/// itself kept byte for byte.
11576///
11577/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
11578/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
11579/// `content` is empty — so a read of the result reports `content` as the visible body and
11580/// the slot's metadata exactly as it was.
11581fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
11582    let Some(slot) = slot_span(body)? else {
11583        return Ok(content.to_owned());
11584    };
11585    let kept = &body[slot.start..];
11586    Ok(if content.is_empty() {
11587        kept.to_owned()
11588    } else {
11589        format!("{content}{METADATA_SEPARATOR}{kept}")
11590    })
11591}
11592
11593/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
11594fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
11595    if entries.is_empty() {
11596        metadata.remove(key);
11597    } else {
11598        metadata.insert(
11599            key.to_owned(),
11600            Value::Array(
11601                entries
11602                    .iter()
11603                    .map(|entry| Value::String(entry.as_str().to_owned()))
11604                    .collect(),
11605            ),
11606        );
11607    }
11608}
11609
11610fn compose_body(
11611    content: Option<&str>,
11612    metadata: &BTreeMap<String, Value>,
11613) -> Result<Option<String>, SourceError> {
11614    let visible = content.unwrap_or_default();
11615    if metadata.is_empty() {
11616        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
11617    }
11618    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
11619        message: error.to_string(),
11620    })?;
11621    Ok(Some(if visible.is_empty() {
11622        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11623    } else {
11624        format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11625    }))
11626}
11627
11628fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
11629    value
11630        .get(field)
11631        .and_then(Value::as_bool)
11632        .ok_or_else(|| SourceError::Malformed {
11633            message: format!("GitHub response is missing boolean field {field}"),
11634        })
11635}
11636fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
11637    match value.get(field) {
11638        None | Some(Value::Null) => Ok(None),
11639        Some(value) => value
11640            .as_str()
11641            .map(Some)
11642            .ok_or_else(|| SourceError::Malformed {
11643                message: format!("GitHub response field {field} is not a string or null"),
11644            }),
11645    }
11646}
11647fn optional_nodes<'a>(
11648    connection: Option<&'a Value>,
11649    name: &str,
11650) -> Result<Option<&'a Vec<Value>>, SourceError> {
11651    match connection {
11652        None | Some(Value::Null) => Ok(None),
11653        Some(value) => value
11654            .get("nodes")
11655            .and_then(Value::as_array)
11656            .map(Some)
11657            .ok_or_else(|| SourceError::Malformed {
11658                message: format!("GitHub {name}.nodes is not an array"),
11659            }),
11660    }
11661}
11662fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
11663    let page_info = connection
11664        .get("pageInfo")
11665        .ok_or_else(|| SourceError::Malformed {
11666            message: format!("GitHub {name} has no pageInfo"),
11667        })?;
11668    if required_bool(page_info, "hasNextPage")? {
11669        return Err(SourceError::Malformed {
11670            message: format!(
11671                "GitHub {name} exceeds the supported nested connection size of {size}"
11672            ),
11673        });
11674    }
11675    Ok(())
11676}
11677fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
11678    optional_str(value, field)?
11679        .map(|timestamp| {
11680            timestamp.parse().map_err(|error| SourceError::Malformed {
11681                message: format!("GitHub response field {field} is not a timestamp: {error}"),
11682            })
11683        })
11684        .transpose()
11685}
11686fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
11687    if page.limit == 0 {
11688        Err(SourceError::Config {
11689            message: "page limit must be at least 1".into(),
11690        })
11691    } else {
11692        Ok(())
11693    }
11694}
11695fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
11696    let page = connection
11697        .get("pageInfo")
11698        .filter(|value| value.is_object())
11699        .ok_or_else(|| SourceError::Malformed {
11700            message: "GitHub connection is missing pageInfo".into(),
11701        })?;
11702    if required_bool(page, "hasNextPage")? {
11703        let cursor = required_str(page, "endCursor")?;
11704        validate_cursor_progress(None, cursor)?;
11705        Ok(Some(Cursor(cursor.into())))
11706    } else {
11707        Ok(None)
11708    }
11709}
11710fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
11711    if next.is_empty() || previous == Some(next) {
11712        Err(SourceError::Malformed {
11713            message: "GitHub pagination cursor is empty or did not advance".into(),
11714        })
11715    } else {
11716        Ok(())
11717    }
11718}
11719/// The version of this plugin's opaque narrowing-search cursor.
11720pub const SEARCH_CURSOR_VERSION: u32 = 4;
11721
11722#[derive(Serialize, Deserialize)]
11723#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
11724enum SearchConnection {
11725    Initial {},
11726    Continuing { after: Cursor },
11727    Exhausted {},
11728}
11729impl SearchConnection {
11730    fn after(&self) -> Option<&str> {
11731        match self {
11732            Self::Continuing { after } => Some(&after.0),
11733            _ => None,
11734        }
11735    }
11736    fn exhausted(&self) -> bool {
11737        matches!(self, Self::Exhausted { .. })
11738    }
11739    /// Whether a cursor naming this position, `offset` rows into its page, is one this
11740    /// plugin could have handed out: a page is resumed only part of the way through it — an
11741    /// offset of a whole page or more would skip rows nobody was given — an initial page
11742    /// only once some of it was handed out, and an exhausted connection has no page to be
11743    /// part of the way through.
11744    fn valid_resume(&self, offset: usize) -> bool {
11745        let within = offset < SEARCH_PAGE_SIZE as usize;
11746        match self {
11747            Self::Initial { .. } => offset > 0 && within,
11748            Self::Continuing { after } => !after.0.is_empty() && within,
11749            Self::Exhausted { .. } => offset == 0,
11750        }
11751    }
11752}
11753
11754/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
11755#[derive(Serialize, Deserialize)]
11756#[serde(deny_unknown_fields)]
11757struct SearchPosition {
11758    version: u32,
11759    connection: SearchConnection,
11760    /// How many rows of the page `connection` starts were already handed out.
11761    #[serde(default, skip_serializing_if = "is_zero")]
11762    offset: usize,
11763    #[serde(default, skip_serializing_if = "Vec::is_empty")]
11764    seen: Vec<NativeId>,
11765    #[serde(default, skip_serializing_if = "Vec::is_empty")]
11766    own: Vec<NativeId>,
11767}
11768impl Default for SearchPosition {
11769    fn default() -> Self {
11770        Self {
11771            version: SEARCH_CURSOR_VERSION,
11772            connection: SearchConnection::Initial {},
11773            offset: 0,
11774            seen: Vec::new(),
11775            own: Vec::new(),
11776        }
11777    }
11778}
11779
11780fn is_zero(offset: &usize) -> bool {
11781    *offset == 0
11782}
11783
11784fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
11785    cursor.map_or(Ok(0), |c| {
11786        c.0.parse().map_err(|_| SourceError::Config {
11787            message: "page cursor is invalid".into(),
11788        })
11789    })
11790}
11791fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
11792    if offset > items.len() {
11793        return Page::last(vec![]);
11794    }
11795    let tail = items.split_off(offset);
11796    let mut selected = tail;
11797    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
11798    selected.truncate(limit);
11799    Page {
11800        items: selected,
11801        next,
11802    }
11803}
11804
11805impl GitHubProjectsSource {
11806    async fn write_task_assets(
11807        &self,
11808        write: &ItemWrite<Task>,
11809        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
11810    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
11811        let near = write.target.as_ref().unwrap_or(&write.item.id);
11812        for (key, entries) in [
11813            (TaskRef::DELIVERS_KEY, &write.item.delivers),
11814            (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
11815        ] {
11816            TaskRef::listed(key, near, Some(&self.name), entries.clone())
11817                .map_err(|message| SourceError::Refused { message })?;
11818        }
11819        if self.priorities.is_none() && write.item.priority != Priority::None {
11820            return Err(self.holds_no_priority());
11821        }
11822        self.write_item(
11823            &Incoming {
11824                written: Written::Work(ItemKind::Task, &write.item.status),
11825                title: &write.item.title,
11826                content: write.item.content.as_deref(),
11827                assets,
11828                labels: &write.item.labels,
11829                metadata: &write.item.metadata,
11830                repositories: &write.item.repositories,
11831                classification: write.item.classification,
11832                parent: write.item.project.as_ref(),
11833                delivers: &write.item.delivers,
11834                delivered_by: &write.item.delivered_by,
11835                priority: self.priorities.as_ref().map(|_| write.item.priority),
11836            },
11837            write.target.as_ref(),
11838            &write.depends_on,
11839        )
11840        .await
11841    }
11842    async fn write_document_assets(
11843        &self,
11844        write: &ItemWrite<Document>,
11845        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
11846    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
11847        // A document takes part in no dependency graph, so there is no far end to write
11848        // natively and none to record: a caller naming one is told so rather than having it
11849        // stored under the reserved key, where a later read would report an edge the
11850        // contract says cannot exist.
11851        if !write.depends_on.is_empty() {
11852            return Err(SourceError::Refused {
11853                message: format!(
11854                    "this write names {} dependencies for a document, and a document takes \
11855                     part in no dependency graph; next: put the dependency on the task or \
11856                     project the document is about",
11857                    write.depends_on.len()
11858                ),
11859            });
11860        }
11861        self.write_item(
11862            &Incoming {
11863                written: Written::Document,
11864                title: &write.item.title,
11865                content: write.item.content.as_deref(),
11866                assets,
11867                labels: &write.item.labels,
11868                metadata: &write.item.metadata,
11869                repositories: &write.item.repositories,
11870                classification: write.item.classification,
11871                parent: write.item.project.as_ref(),
11872                delivers: &[],
11873                delivered_by: &[],
11874                priority: None,
11875            },
11876            write.target.as_ref(),
11877            &[],
11878        )
11879        .await
11880    }
11881}
11882
11883/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
11884/// itself, for the two things no journey can observe.
11885///
11886/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
11887/// with and without the call, that a settlement, a board listing and a metadata search each
11888/// read afresh after it — the resolved records, the written-item overlay, the board and its
11889/// search, and the narrowed searches. What they cannot reach is the held field definitions,
11890/// because a status write naming an option a person deleted is refused the same whether or
11891/// not the list is held, and a poisoned lock, because nothing outside the source can panic
11892/// while one of its locks is held. So these assert those directly, and every other holder
11893/// beside them so a holder added later without a clear in the call fails here.
11894#[cfg(test)]
11895mod end_command_tests {
11896    use super::*;
11897
11898    struct Token;
11899
11900    impl SecretResolver for Token {
11901        fn get(&self, var: &str) -> Option<SecretString> {
11902            (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
11903        }
11904    }
11905
11906    fn source() -> GitHubProjectsSource {
11907        let config = serde_json::from_value(json!({
11908            "owner": "octo-org", "project_number": 7, "repository": "acme/work",
11909            // Nothing here is sent: the source is only built and its state inspected.
11910            "endpoint": "http://127.0.0.1:9/graphql",
11911        }))
11912        .expect("a usable configuration");
11913        GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
11914            .expect("the source builds")
11915    }
11916
11917    /// One issue as a board read answers it.
11918    fn resolved(source: &GitHubProjectsSource) -> Resolved {
11919        source
11920            .resolve(&json!({
11921                "id": "ITEM-1",
11922                "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
11923                            "body": "what a person may since have edited", "state": "OPEN",
11924                            "stateReason": null, "url": null, "number": 1,
11925                            "subIssuesSummary": {"total": 0},
11926                            "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
11927                "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
11928            }))
11929            .expect("the item reads")
11930            .expect("an issue")
11931    }
11932
11933    /// Hold something in every holder the call clears, and the repository id it keeps.
11934    fn fill(source: &GitHubProjectsSource) {
11935        let item = resolved(source);
11936        source.created.lock().unwrap().push(item.clone());
11937        source.updated.lock().unwrap().push(item.clone());
11938        *source.board_cache.lock().unwrap() = Some(Board {
11939            id: "PVT-board".into(),
11940            fields: json!({"nodes": []}),
11941            items: vec![item.clone()],
11942        });
11943        *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
11944        source
11945            .narrowed_cache
11946            .lock()
11947            .unwrap()
11948            .insert("status:todo".into(), vec![item.clone()]);
11949        source.children_cache.lock().unwrap().insert(
11950            NativeId("P-1".into()),
11951            (NativeId("P-1".into()), vec![item.clone()]),
11952        );
11953        source
11954            .search_next
11955            .lock()
11956            .unwrap()
11957            .insert("status:todo".into(), Some("cursor".into()));
11958        source
11959            .resolved_cache
11960            .lock()
11961            .unwrap()
11962            .insert(item.id.clone(), item);
11963        *source.fields_cache.lock().unwrap() = Some(BoardFields {
11964            id: BoardId::parse("PVT-board").unwrap(),
11965            fields: json!({"nodes": []}),
11966        });
11967        source
11968            .repository_cache
11969            .lock()
11970            .unwrap()
11971            .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
11972    }
11973
11974    fn assert_dropped(source: &GitHubProjectsSource) {
11975        assert!(source.created().unwrap().is_empty(), "created");
11976        assert!(source.updated().unwrap().is_empty(), "updated");
11977        assert!(source.board_cache().unwrap().is_none(), "board");
11978        assert!(source.search_cache.lock().unwrap().is_none(), "search");
11979        assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
11980        assert!(
11981            source.children_cache.lock().unwrap().is_empty(),
11982            "project children"
11983        );
11984        assert!(
11985            source.search_next.lock().unwrap().is_empty(),
11986            "search paging"
11987        );
11988        assert!(
11989            source.resolved_cache().unwrap().is_empty(),
11990            "resolved records"
11991        );
11992        assert!(source.fields_cache().unwrap().is_none(), "board fields");
11993        assert_eq!(
11994            source.repository_cache().unwrap().len(),
11995            1,
11996            "a repository's node id stays valid and is kept"
11997        );
11998    }
11999
12000    fn end(source: &GitHubProjectsSource) {
12001        tokio::runtime::Builder::new_current_thread()
12002            .build()
12003            .unwrap()
12004            .block_on(source.end_command())
12005            .expect("the command ends");
12006    }
12007
12008    #[test]
12009    fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
12010        let source = source();
12011        fill(&source);
12012        end(&source);
12013        assert_dropped(&source);
12014    }
12015
12016    #[test]
12017    fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
12018        fn poison<T: Send>(held: &Mutex<T>) {
12019            std::thread::scope(|scope| {
12020                let _ = scope
12021                    .spawn(|| {
12022                        let _guard = held.lock().unwrap();
12023                        panic!("a failure while the lock is held");
12024                    })
12025                    .join();
12026            });
12027            assert!(held.is_poisoned());
12028        }
12029        let source = source();
12030        fill(&source);
12031        poison(&source.created);
12032        poison(&source.updated);
12033        poison(&source.board_cache);
12034        poison(&source.search_cache);
12035        poison(&source.narrowed_cache);
12036        poison(&source.search_next);
12037        poison(&source.resolved_cache);
12038        poison(&source.fields_cache);
12039        assert!(
12040            source.resolved_cache().is_err(),
12041            "a poisoned lock is refused before the call"
12042        );
12043        end(&source);
12044        assert_dropped(&source);
12045    }
12046}
12047
12048#[cfg(test)]
12049mod unprojectable_fields_tests {
12050    use super::*;
12051
12052    /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
12053    fn documented() -> std::collections::BTreeSet<String> {
12054        let document = std::fs::read_to_string(
12055            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
12056        )
12057        .expect("docs/metadata.md is readable");
12058        let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
12059        let lead = "a `field` equal, ignoring case, to ";
12060        let start = document.find(lead).expect("the refused-field sentence") + lead.len();
12061        let sentence = &document[start..];
12062        let sentence = &sentence[..sentence
12063            .find("; and two entries naming one field")
12064            .expect("the refused-field sentence ends at the duplicate rule")];
12065        let (own, github) = sentence
12066            .split_once(" or a field GitHub owns on every board (")
12067            .expect("the sentence names GitHub's own fields");
12068        own.split('`')
12069            .skip(1)
12070            .step_by(2)
12071            .chain(
12072                github
12073                    .strip_suffix(')')
12074                    .expect("GitHub's own fields close the sentence")
12075                    .split(", "),
12076            )
12077            .map(str::to_owned)
12078            .collect()
12079    }
12080
12081    #[test]
12082    fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
12083        let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
12084            .iter()
12085            .map(|&name| name.to_owned())
12086            .collect();
12087        assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
12088        let instance = SourceName::new("work").unwrap();
12089        for name in UNPROJECTABLE_FIELDS {
12090            for spelled in [name.to_owned(), name.to_uppercase()] {
12091                let entry = MetadataFieldConfig {
12092                    field: spelled.clone(),
12093                    key: "team.machine".to_owned(),
12094                    path: Vec::new(),
12095                };
12096                let refused = MetadataField::resolve(vec![entry], &instance)
12097                    .expect_err("an unprojectable field is refused");
12098                assert!(
12099                    refused.to_string().contains(&format!("{name:?}")),
12100                    "{spelled}: {refused}"
12101                );
12102            }
12103        }
12104    }
12105}