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// 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.
84//! **Status.** `status_mapping` is per-instance configuration, in the shared grammar
85//! [`onetaskgraph_plugin_api::StatusMapping`] documents, from a status category to an
86//! option of the board's one `Status` field for a task and for a project: a bare name is
87//! the option for both kinds, `null` disables the category for both, and `{task, project}`
88//! names it per kind. A category the mapping does not mention keeps its shipped default for
89//! both kinds; one it mentions is exactly what it configures, so a per-kind object no longer
90//! gets the shipped default for the kind it leaves out. Two categories one kind would read
91//! back from one option are refused as the configuration is read, ignoring case, while one
92//! option may stand for different categories of the two kinds. Writes go by the kind of the
93//! item written: a status that kind has no option for, or whose option the board lacks, is
94//! refused before any mutation, naming the source, the kind, the category and the key
95//! `status_mapping.<category>.<kind>` — there is no fallback. `done` selects its mapped
96//! option and closes the issue as `COMPLETED`; `cancelled` selects its mapped option and
97//! closes it as `NOT_PLANNED`, for either kind. Every open category reopens a closed issue
98//! before selecting its option. Reads give a closed issue's reason precedence over its
99//! option, while an open issue's option decides its category through its own kind's
100//! mapping, and an option that mapping does not name reads as `unknown` under its own name.
101//! The guarded [`GitHubProjectsSource::status_options`] and
102//! [`GitHubProjectsSource::fields`] operations are the one path here that calls
103//! `updateProjectV2Field`: GitHub replaces the whole option list, so they preserve every
104//! existing option id and verify the field and item assignments immediately afterwards.
105//! They ask for both kinds' options, counting a terminal category's mapped option as
106//! configured because a terminal write refuses without it. No ordinary source read or
107//! write calls that mutation, whose
108//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
109//! and a mistake destroys every item's status. A status this board cannot represent is a
110//! refusal naming the status and the instance instead.
111//!
112//! `unknown` has no shipped option because this source cannot preserve an open-ended
113//! status word: it writes an existing board option and never
114//! creates an option. An operator may map `unknown` to one existing option, in which case
115//! every unknown word lands on that option and reads back as `unknown` under the option's
116//! name. This differs from `local-md`, which writes and reads the original word itself.
117//!
118//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
119//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
120//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
121//! finished tasks were only moved to a "Done" column would read 0% complete forever.
122// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
123//!
124//! # What this source declares, field by field
125//!
126//! One verdict per field of [`Capabilities`], and what `Native` means when this source
127//! says it. *Proven* means a shared journey drives it against the real
128//! binary over this source's own row in `crates/onetaskgraph-e2e-support/src/fixtures.rs`, and
129//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
130//! [`capabilities`](TaskSource::capabilities) from parting.
131//!
132//! | Field | Verdict |
133//! | --- | --- |
134//! | `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. |
135//! | `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. |
136//! | `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. |
137//! | `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. |
138//! | `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. |
139//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
140//! | `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. |
141//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
142//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
143//! | `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. |
144//! | `filter_by_metadata` | **Supported, and asked of GitHub.** 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. |
145//! | `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. |
146//! | `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. |
147//! | `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. |
148//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
149//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
150//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
151//!
152//! Image writes use `POST https://uploads.github.com/user-attachments/assets` with `name`,
153//! `content_type` and the issue repository's numeric `repository_id` as query parameters,
154//! the source token as `Authorization: Bearer`, the image content type as `Content-Type`,
155//! and the raw bytes as the request body. The numeric repository id is read at most once
156//! per repository per source instance. A loopback API endpoint also selects the loopback
157//! uploads host; production needs no assets repository, commits or extra configuration.
158//! Every returned JSON `url` is read with the same token before a body is written: only
159//! a 2xx verification succeeds. A refusal names the asset, URL and HTTP status; an
160//! anonymous 404 does not invalidate an authenticated 200. An upload refusal names the
161//! asset and HTTP status and says the token type may not be accepted by the upload
162//! endpoint. A classic PAT was measured accepted; no acceptance claim is made about
163//! fine-grained PATs or OAuth tokens.
164//! The body keeps the authored alt text while `./<name>` becomes the attachment URL,
165//! and `onetaskgraph.assets` records `{sha256, url}` by name. A matching destination
166//! SHA-256 reuses its URL without an upload or verifying read; changed bytes are uploaded
167//! and verified afresh, in the repository the issue already lives in on an update.
168//! An uploaded image renders for the viewers GitHub lets read it, like the issue text beside it.
169//! Uploads take the same mutation spacing as content writes. Both uploads and verification
170//! reads wait out classified rate limits within the configured per-call retry budget,
171//! on the supplied clock; verifying reads take no mutation slot.
172//!
173//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
174//! source has documents and that its tasks have comments, both of which hold — and the three
175//! facts behind the uniform `Native` on the
176//! predicates beside it are recorded below rather than re-derived, because a reader who
177//! takes `Native` to mean *the remote service filters* will read that uniformity as a
178//! lie.
179//!
180//! First, the plugin contract defines `Support::Native` as *the source applies this
181//! predicate itself*, and says nothing about where it applies it. What the declaration
182//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
183//! — so that the engine may push it down and apply nothing of its own.
184//!
185//! Second, this source can keep that promise for every predicate at no additional API
186//! cost, because whichever of the reads below answers a query has already read every
187//! candidate that query will return before it filters anything. Filtering those items is
188//! in-process work over data already in hand.
189//!
190//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
191//! applied in process over what that question returned. A project filter has a relationship — a
192//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
193//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
194//! a search for metadata values, is the board-scoped issue search carrying the text and each
195//! value as quoted phrases; an origin is the board's own field filter over its origin field
196//! beside the same search for the id. **The text search narrows, and that is this source's
197//! declared semantics:** GitHub matches whole words where the substring rule this source and
198//! the local Markdown source confirm with would match inside one, so an item holding the text
199//! only inside a longer word is never a candidate. Every item returned does contain the text.
200//! A project query's text, and a document query's scoped to no project, is that same search
201//! and narrows on the same terms, its candidates confirmed by their kind as well.
202//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
203//! so those three are applied in process over the candidates, and a query carrying none of
204//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
205//! engine compensate for work this source has already done, and declaring `projects` native
206//! while ignoring the filter (which this source once did) silently returns another project's
207//! tasks, because the engine trusts the declaration and applies nothing locally.
208//!
209//! # The three ways this source reaches an item, and what each costs
210//!
211//! A board read is charged for what its *nested* connections could return rather than for
212//! what was asked, so one whole-board read costs the same whether the question was about
213//! one project or about all of them. That is why a question about one project is never
214//! answered by reading the board:
215//!
216//! | The question | What is sent | What it costs |
217//! | --- | --- | --- |
218//! | 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 |
219//! | 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 |
220//! | 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 |
221//! | 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 |
222//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
223//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
224//! | 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 |
225//! | 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 |
226//! | 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 |
227//! | 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 |
228//! | 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 |
229//! | 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 |
230//!
231//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
232//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
233//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
234//! equal to declared points equal to the row. They include the origin lookup and the
235//! field/repository discovery a create needs. A bound re-copy changes status, priority,
236//! content and metadata; comment recount means a subsequent detail read. Each request here
237//! costs one declared point. A membership beyond the embedded page can additionally require
238//! the one-point membership recovery described above. A bound re-copy of a task filed under a
239//! project adds one read, the engine confirming that project's link by its own id once per
240//! command; and the same-source far ends a write newly names — those that do not already block
241//! the item, whose own read answered for them — are read together by their own ids,
242//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
243//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
244//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
245//!
246//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
247//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
248//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
249//! holds both halves.
250//!
251//! **An existing item is written body last.** A bound re-copy and a `task update` send its
252//! board fields first — the `Status` option and the `Priority` together, in one request — then
253//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
254//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
255//! undoing an earlier field when a later one fails, so that order is what makes a write
256//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
257//! the one piece of metadata written before the body, an origin a copy re-points, is put back
258//! when a later write is refused — and when putting it back is refused too, the write's own
259//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
260//! of those writes in turn, whole and as one aliased field failing after the one before it.
261//!
262//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
263//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
264//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
265//!
266//! - **A board is accepted at creation but its item is not answered, so a create still files
267//!   the issue itself: a new copy is 5 requests, and 4 with `--create`.**
268//!   `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
269//!   Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
270//!   The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
271//!   against a real board on 2026-10-01 with a create sending the board there and reading the
272//!   item off the payload's `Issue.projectItems`: every one of its four creates answered with
273//!   no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
274//!   refused "Content already exists in this project" — GitHub had filed the issue after
275//!   answering, and refuses a second filing rather than answering with the item it holds. A
276//!   create therefore sends no `projectV2Ids` and files the issue with
277//!   `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
278//!   real is the read before it: the board's fields and the repository's id together, in
279//!   [`graphql::CREATION_CONTEXT`], at the point the repository is known.
280//! - **A comment still reads its target first, so a comment is 2 requests.**
281//!   `AddCommentInput.subjectId: ID!` is declared there with
282//!   `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
283//!   "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
284//!   project's issue, a document's issue, an issue on no board of this source and a pull
285//!   request all are: GitHub writes the comment, so there is no refusal to map into "that is
286//!   not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
287//!   refuses those by name.
288//!
289//! | Verb | Requests / points | Documents |
290//! | --- | --- | --- |
291//! | new copy | 5 | ORIGIN_LOOKUP, CREATION_CONTEXT (the board's fields and the repository's id together), CREATE_ISSUE, ADD_TO_BOARD, UPDATE_FIELDS |
292//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, ADD_TO_BOARD, UPDATE_FIELDS: the new copy without its ORIGIN_LOOKUP |
293//! | 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 |
294//! | 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 |
295//! | 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 |
296//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
297//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
298//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
299//! | recount | 1 | ISSUE_DETAIL |
300//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
301//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
302//! | content | 2 | ISSUE, UPDATE_ISSUE |
303//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
304//! | 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 |
305//! | record only | 1 | ISSUE |
306//!
307//! <!-- github-search-paging:start -->
308//! Board-scoped text, metadata, project-name and comment-activity searches send every
309//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
310//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
311//! needs rows. A page is never resized to the rows still needed: GitHub orders one
312//! search differently at different page sizes, so one fixed size makes a paged walk
313//! send exactly the requests one whole read sends, and the answer's order is the order
314//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
315//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
316//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
317//! returned and fetched pages: a limit is sliced from the pages it needs, and local
318//! confirmation can require more candidates than matching rows. Walking all pages
319//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
320//! cursor and how far into that page the last answer stopped, and resumes in the same
321//! process or a new one, without duplicates or gaps. It carries no rows: one process
322//! sends each page's search once, and a new process re-reads only the page it resumes
323//! in, then sends a further page once, never as a re-read, only when its limit still
324//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
325//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
326//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
327//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
328//! not required to include the original process's writes still omitted by the index.
329//! <!-- github-search-paging:end -->
330//!
331//! The board half of an issue — its board item's id, its `Status` option and this
332//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
333//! an item reached any of those ways resolves through the same
334//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
335//! same status, the same labels and the same qualified id. That connection comes back a
336//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
337//! on the page in hand and — only if that page reports more of the connection — in the
338//! last row's read of that one issue's memberships, resumed from the page's own cursor and
339//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
340//! report, which is what keeps an id naming another repository's issue from being answered
341//! as an item of this board; and because the page is where the search starts rather than
342//! where it ends, that answer is one about a connection read to exhaustion and never about
343//! an unread page. Nothing costs the extra read but an issue on more boards than a page
344//! holds: an issue this board really does not hold reports no next page, so its
345//! memberships are already exhausted where they arrived.
346//!
347//! **No document here selects the board's own `Labels` field, and nothing is lost by
348//! that.** An item's labels are read from its content alone, wherever that content is
349//! reached: the three documents above select `Issue.labels` on the fragment, and
350//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
351//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
352//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
353//! create one, and `ProjectV2FieldValue` — the whole of what
354//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
355//! it from the content, for every content type it exists on, and there is nothing it can
356//! hold that the content does not already say: for an `Issue` it *is* that issue's own
357//! labels, so selecting it beside them unions a set with itself.
358//!
359//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
360//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
361//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
362//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
363//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
364//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
365//! label set, and that set is the fixture issue's own, by
366//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
367//! item whose content is a draft reports an empty set, by
368//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
369//! selection is held over [`graphql::DOCUMENTS`] by
370//! `no_document_selects_the_boards_own_labels_field`.
371//!
372//! The whole-board row is still the board's own item connection, and deliberately: a
373//! **draft** board item is not an issue, so no search can list one, and the reads that have
374//! to answer for the whole board are the ones whose cost is the board's size anyway.
375//!
376//! **A question about one item this source already names by id never lists the board.**
377//! Whether that item is on this board, and what its board fields are, is answered by reading
378//! that item — its own `Issue.projectItems`, walked to exhaustion by
379//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
380//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
381//! holds. That covers a write's destination, the project a new item is filed under, a
382//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
383//! and the delete that takes back an item a copy made. What such a write needs of the board
384//! and the item does not carry — the board's id, the `Status` and origin field definitions —
385//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
386//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
387//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
388//! minutes. Scanning this host's 842-item board has refused a document copy and an update
389//! even though the items' own reads named that board. A scan there gives the wrong answer
390//! as well as paying for every page. So a `board.items` lookup does not belong on any of
391//! those paths.
392//!
393//! **What a read may return is capped too, and that cap is on the document rather than on
394//! the board.** GitHub limits the number of nodes **one query may return** to
395//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
396//! an error naming the connection the count crossed at, not a slow or a partial result.
397//! Every board this source reads is refused the same way, so no board is too big for these
398//! documents and none is small enough to save one that is over.
399//!
400//! The count is arithmetic over the document's own text: each connection contributes the
401//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
402//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
403//! restate them — `github-graphql-node-count` implements them, and
404//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
405//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
406//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
407//! same text on every run and fails naming any that reaches the limit — so a connection
408//! added to a shared fragment is caught there rather than by GitHub.
409//!
410//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
411//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
412//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
413//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
414//! effectively squared there, which is why it is the one the limit is most sensitive to.
415//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
416//! page of memberships misses is recovered by one further read rather than refused, so it
417//! buys a bound every read pays for at the price of a request only a multi-board issue
418//! pays.
419//!
420//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
421//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
422//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
423//! `cost` is rate-limit points, metered per hour across everything one credential does; it
424//! is what the two limiters [`Limiter`] tells apart meter, and a document under
425//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
426//! that second number, and `tests/point_cost.rs` pins every document in
427//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
428//! one under, the pin itself is the check. The credentialed lane reconciles both figures
429//! against GitHub's own, off a probe it already sends.
430//!
431//! **What is pinned that way is a per-document price and never a session's.** The record in
432//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
433//! **requests** and **worst-case nodes** — and neither is points. What one whole session
434//! consumes of the hourly point allowance is observable only from a credentialed run's own
435//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
436//! and what `tests/live.rs` prints at the end of every run.
437//!
438//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
439//!
440//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
441//! enumerations of a board can supply one alone.** Resolving a node id is strongly
442//! consistent, so a read by id and a project's own sub-issues are already current. The
443//! other two are not, and they are behind by different amounts and in different directions:
444//!
445//! - GitHub's **issue search** is an index and answers a write made moments ago with the
446//!   value from before it — usually for a second or two.
447//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
448//!   on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
449//!   content withheld, absent, with the connection walked to its own `hasNextPage: false` —
450//!   for *minutes*, while `Issue.projectItems` names the same membership at once.
451//!
452//! That second one is a measurement rather than a caution. This repository's own
453//! credentialed journey writes a project and waits for the board to report it, then writes a
454//! task and waits for the same thing seconds later on the same board: the project wait is
455//! answered through the search and converged in two or three attempts in each of three runs,
456//! and the task wait is answered through `ProjectV2.items` and converged in none of them
457//! inside thirty. Separately, an item added to a second and larger board was read back by
458//! `Issue.projectItems` on that board's own id while every one of that connection's nine
459//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
460//! through the lagging one alone is what had a board read deny an issue that had certainly
461//! landed on it.
462//!
463//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
464//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
465//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
466//! search reports what the projection is behind on. What closes the last
467//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
468//! source answers is completed with what this process itself wrote, so an item created
469//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
470//! nothing is written down, and the record dies with the process. **A wait that has to
471//! observe GitHub's own data cannot be answered from that record** — which is why the
472//! credentialed journey asks through a source built afresh, and why the union above rather
473//! than a longer wait is what makes such a wait converge.
474//!
475//! **A narrowed read is the same bargain, stated for each of the three predicates it
476//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
477//! than walking the board, and every such answer is completed with what this process wrote —
478//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
479//! each filtered by the same predicates as the rest — so an item this command wrote a moment
480//! ago is returned by a query that matches it whether or not the index has caught up. An item
481//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
482//! consistent. What is left is stated rather than papered over:
483//!
484//! | Read | Finds | Behind by |
485//! | --- | --- | --- |
486//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
487//! | 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 |
488//! | 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 |
489//! | origin, third read | this process's own writes | nothing |
490//!
491//! So an origin carrier another process added within the last second or two, before either
492//! index has it, can be missing from an origin query, and one written by the release before
493//! this one — its origin in the field alone — can be missing for as long as the board's own
494//! item connection is behind on it. A copy that must not duplicate its own earlier write
495//! relies on the link it records, not on either index. **A board draft is not an issue**, so
496//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
497//! lists one, the origin lookup drops any the board's own field filter names, and one this
498//! process wrote is not added back either.
499//!
500//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
501//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
502//! body's metadata slot, so the issue search can find it in seconds. The field is
503//! authoritative: this source reads an item's origin from the field alone, so a slot that
504//! disagrees with it, or holds one where the field holds none, is never read as a second
505//! origin — and the release before this one reads the slot, drops that key's copy for the
506//! field's, and sees the same one origin.
507//!
508//! Filtering happens before paging, so a page of a filtered result is a page of the
509//! survivors rather than the survivors of a page. Label matching and the substring rule a
510//! text candidate is confirmed by answer the same question the same way the local Markdown
511//! source's do; which candidates a text search has to confirm is GitHub's word match, which
512//! is the one place the two sources can answer the same text differently.
513//!
514//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
515//! has one source, `capabilities`, and the note above is the reasoning behind it rather
516//! than a second copy of it: without the three facts recorded here a reader takes the
517//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
518//! crate's own capabilities test, which pins every field of it against a fully spelled-out
519//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
520//! fails to compile there rather than going unasserted. -->
521//! The fixture-server tests above run wherever this crate is selected; the credentialed
522//! lane runs in the same required check, beside them, and can fail it — it verifies the
523//! 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,
524//! one filed under neither, a label on one of the three and a closed status on another —
525//! because that shape is what tells an honoured predicate from an ignored one: a board
526//! holding a single project answers a project filter the same way whether or not this
527//! source applies it, which is exactly how the defect above went unseen.
528//!
529//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
530//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
531//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
532//! session or request, independently of the live demand. Fix that variable in the machine's
533//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
534//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
535//! policy crate; absent credentials or nominations otherwise skip.
536//!
537//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
538//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
539//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
540//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
541//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
542//! after its run reads back as `completed`, immediately before the listed-artifact batch.
543//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
544//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
545//! and the board's `onetaskgraph.origin` field untouched.
546//!
547//! # What a session of requests costs, and where the report is
548//!
549//! This source records **every** request it sends into [`accounting::Accounting`], at
550//! `send_once` — the one place a request leaves this crate, which is why a read path added
551//! later is counted without anybody remembering to count it. That is the whole of what this
552//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
553//! session's spend is arrived at, and what it deliberately does not know are set out.
554//!
555//! What one whole session of the live journey costs, counted that way against this crate's
556//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
557//! reduction it came out of, and with what it does and does not say about rate-limit points.
558//!
559//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
560//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
561//! code path — no environment variable, no feature, no build configuration — because an
562//! instrument nobody switches on measures nothing, and
563//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
564//! source's counts the whole session rather than this source's share. The credentialed lane
565//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
566//! passed or failed.
567//!
568//! **A live session refuses to start unless the account can afford it.** Before it does any
569//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
570//! GitHub documents as not counting against the REST rate limit and which answers both of
571//! its budgets at once — and starts only if, for each of them, what remains minus this
572//! session's estimated cost is still at least
573//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
574//! allowance. A session that cannot **declines**: it did not run, so it is
575//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
576//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
577//! waiting for the budget to come back. The estimate is derived offline from
578//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
579//! which is also where the published rule that model rests on is cited; the accounting
580//! above records the gate's own read like any other request, and
581//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
582//!
583//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
584//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
585//! text, which is what lets it run on every platform and on a pull request from a fork with
586//! no credential — and that is what actually stops a regression merging. But an offline
587//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
588//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
589//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
590//! number of nodes this query may return"* and whose `cost` is what that document would
591//! spend, both for a document **without executing it**, and the lane asks it for every query
592//! document this source sends, under the largest bindings this source sends, and fails when
593//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
594//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
595//! one; the offline pins still cover it. It records what those calls reported about the
596//! account's own allowance, because whether asking is free is a thing to observe rather than
597//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
598//! and `cost` is metered against an hourly allowance the accounting above reads off a
599//! credentialed run's own response headers.
600//!
601//! **GitHub has two rate limiters and this source is refused by both, so nothing here
602//! treats them as one thing.** The primary budget is the hourly allowance `gh api
603//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
604//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
605//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
606//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
607//! one part of.
608#![deny(missing_docs)]
609
610use std::collections::BTreeMap;
611use std::sync::{Arc, Mutex};
612use std::time::Duration;
613
614use chrono::{DateTime, Utc};
615use onetaskgraph_plugin_api::{
616    Capabilities, Comment, CommentBody, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
617    DependencySupport, Direction, Document, DocumentQuery, Health, ItemKind, ItemWrite, Label,
618    LabelFilter, Location, MetadataKey, Metering, NativeId, NewComment, Page, PageRequest,
619    Priority, Project, ProjectFilter, ProjectQuery, Repository, SecretResolver, SharedClock,
620    SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusMapping, Support, Task,
621    TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate, TaskUpdateOutcome, TextFields,
622    TextQuery, UnmappedStatus, UpdatedField, WriteSupport, system_clock,
623};
624use reqwest::{Client, StatusCode, Url};
625use schemars::{Schema, schema_for};
626use secrecy::{ExposeSecret, SecretString};
627use serde::{Deserialize, Serialize};
628use serde_json::{Value, json};
629
630pub mod accounting;
631mod assets;
632
633use accounting::Accounting;
634
635/// The registry name for this plugin.
636pub const KIND: &str = "github-projects";
637/// GitHub's maximum connection page size.
638pub const MAX_PAGE_SIZE: u32 = 100;
639/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
640/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
641/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
642pub const SEARCH_PAGE_SIZE: u32 = 20;
643/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
644/// its comments: the largest batch the node-count model prices at one point.
645///
646/// Each aliased item is resolved once, and what GitHub charges for it is the connections
647/// under it — its labels, its page of board memberships, the field values of each of those
648/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
649/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
650/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
651/// one point and fails if one item more would still be priced at one.
652pub const DETAIL_BATCH: usize = 24;
653
654/// The most nodes any one document this source sends may be asked to return.
655///
656/// GitHub's own published per-query ceiling, taken from
657/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
658/// workspace cannot hold a stale copy of somebody else's number. A query above it is
659/// **refused before it is executed**, whoever is asking and whatever board they are
660/// asking about — so this is a bound on the documents rather than a budget that runs out.
661///
662/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
663/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
664/// everything the credential does — two numbers against two limits, and this constant
665/// bounds only the first. The second is computed offline too, per document:
666/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
667/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
668/// lane. There is no constant like this one to hold a price under, because points are an
669/// hourly allowance rather than a per-call bound.
670///
671/// Neither is a session's price. What `session-cost.md` records of a whole session is its
672/// **requests** and its **worst-case nodes**; what a whole session spends in points is
673/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
674/// [`accounting`]. The module section on the three ways this source reaches an item says how
675/// the count is arrived at, and which of the page sizes below decide it.
676pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
677
678/// Nested connection size for the connections that hang off one item.
679///
680/// It multiplies through every document that reaches an item under a page — the count
681/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
682/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
683/// every document under these constants and fails naming any that reaches the limit, so
684/// raising this is caught there rather than by GitHub.
685const NESTED_PAGE_SIZE: u32 = 50;
686/// How many of one issue's board memberships are read when an issue is reached directly.
687///
688/// An issue reached through a search or through its own node id carries its board half in
689/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
690/// under a page of issues, so every point of it multiplies through the whole document and
691/// is paid for whether or not any issue is on a second board — which is why it is
692/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
693///
694/// **Three, because what a page misses is now recovered rather than refused**, and the
695/// recovery is what the value is chosen against. An issue whose entry for this board sits
696/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
697/// that page's own cursor — so the value trades a bound every read pays for a request only
698/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
699/// boards would pay that request *per issue*, which is order N against the one page per
700/// hundred issues a read costs today. At three it is only reached by an issue on four or
701/// more boards at once, which keeps the recovery path exceptional rather than routine for
702/// a plausible deployment.
703const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
704/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
705/// its two connections for.
706///
707/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
708/// second is a duplicate a copy already takes the first of. Both connections are walked to
709/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
710/// never what the answer is. It is small because every point of it is paid on every lookup,
711/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
712/// worst-case nodes than the one whole-board read they replaced.
713const ORIGIN_PAGE_SIZE: u32 = 3;
714
715pub use github_graphql_node_count::{NodeCountError, Variables};
716
717/// The largest value this source can bind to each page-size variable its documents name.
718///
719/// Every `first:` in [`graphql`] reads one of these four, and each is capped at the
720/// constant above it wherever a caller's own limit could reach it — `$first` at
721/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
722/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`. So this is the worst case a caller can drive this source to,
723/// not one configuration of it, which is what makes a bound computed under it a bound on
724/// every read.
725pub fn largest_page_sizes() -> Variables {
726    Variables::from([
727        ("first".to_owned(), MAX_PAGE_SIZE),
728        ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
729        ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
730        ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
731    ])
732}
733
734/// The most nodes `document` could be asked to return, by GitHub's published rules.
735///
736/// Computed offline from the document's own text under [`largest_page_sizes`] — no
737/// network, no credential and no schema — by
738/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
739/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
740/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
741///
742/// # Errors
743///
744/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
745/// no single operation, or binds a page size this source does not name — each of which is
746/// a defect in the document rather than a number.
747pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
748    node_count(document, &largest_page_sizes())
749}
750
751/// The most rate-limit points one call of `document` could spend, by GitHub's published
752/// rules.
753///
754/// Computed offline from the document's own text under [`largest_page_sizes`] — no
755/// network, no credential and no schema — by
756/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
757/// This is `cost`, metered **per hour** against the allowance one credential shares across
758/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
759/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
760/// under, so what `tests/point_cost.rs` does with it is pin every document in
761/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
762/// figures against GitHub's own reported `cost`.
763///
764/// # Errors
765///
766/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
767/// no single operation, or binds a page size this source does not name — each of which is
768/// a defect in the document rather than a number.
769pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
770    github_graphql_node_count::point_cost(document, &largest_page_sizes())
771}
772
773/// The most nodes `document` could be asked to return under `variables`.
774///
775/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
776/// [`accounting`] is this under the bindings one request really sent — one spelling of the
777/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
778/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
779///
780/// # Errors
781///
782/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
783/// no single operation, or binds a page size `variables` does not name.
784pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
785    github_graphql_node_count::node_count(document, variables)
786}
787
788/// The issue-title prefix that makes a board issue a document.
789///
790/// A GitHub Projects board has no document type — it holds issues — so the discriminator
791/// is the title, and this is the whole of it: an issue whose title begins with these bytes
792/// is a document and every other issue is the task or project the sub-issue rule makes it.
793///
794/// It is spelled **once**, here, and read rather than restated everywhere else — including
795/// by the shared journeys, which take it from this constant so a board fixture cannot
796/// drift from what this source reads. `docs/metadata.md` records the two consequences that
797/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
798/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
799/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
800pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
801
802/// Exact GraphQL query documents issued by this plugin.
803///
804/// Keeping the production documents here lets the pinned-schema test validate the same
805/// bytes that are sent to GitHub, rather than a test-only copy which could drift
806/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
807/// field, and its guarded caller always supplies the complete existing option set with ids.
808pub mod graphql {
809    /// The board half of one item: the field values every document here reads it from.
810    ///
811    /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
812    /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
813    /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
814    /// *the same value*, because
815    /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
816    /// one path. Three spellings of it is what would drift, so there is one.
817    ///
818    /// The `Status` option and this source's own origin text field are the whole of it. It
819    /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
820    /// content, so it holds nothing the content's own `labels` do not already say, and it
821    /// would sit a label connection two page sizes deep.
822    macro_rules! board_item_values {
823        () => {
824            r#"fieldValues(first:$nestedFirst){nodes{
825          ... on ProjectV2ItemFieldSingleSelectValue{name field{
826            ... on ProjectV2SingleSelectField{id name options{id name}}
827          }}
828          ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
829        }pageInfo{hasNextPage}}"#
830        };
831    }
832
833    /// Everything this source reads about one issue, wherever it reaches that issue.
834    ///
835    /// A macro rather than a constant so the three documents below can `concat!` it: one
836    /// spelling of these fields is what makes an issue read through the board-scoped
837    /// search, through its own node id, and through its project's sub-issue relationship
838    /// resolve to *the same* item, which is the whole of what
839    /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
840    ///
841    /// `projectItems` is what carries the board half of an issue: the board item's own id
842    /// and the [`board_item_values!`] above — the `Status` option and this source's origin
843    /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
844    /// issue rather than on the board, which is what makes the cost of a read proportional
845    /// to what was asked for instead of to the board's size.
846    ///
847    /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
848    /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
849    /// not on that page: a page here is where the search for the entry starts rather than
850    /// where it ends.
851    ///
852    /// It does **not** select the board's `Labels` field value, and that is the whole of
853    /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
854    /// a label connection there sits under `fieldValues` under `projectItems` under a page
855    /// of issues, spending `$nestedFirst` twice down one path, and took
856    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
857    /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
858    /// above, and that connection is where every label this source reports comes from. No
859    /// document in this module selects the board field any longer, [`BOARD`] included; the
860    /// module documentation records why nothing it could have held is lost.
861    macro_rules! board_issue {
862        () => {
863            concat!(
864                r#" fragment BoardIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total}
865      labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
866      projectItems(first:$boardItems){nodes{id project{id number}
867        "#,
868                board_item_values!(),
869                r#"}pageInfo{hasNextPage endCursor}}}"#
870            )
871        };
872    }
873
874    /// Every issue of one board, found by a search scoped to that board.
875    ///
876    /// This is how the projects a board holds are listed, and it selects no `items`
877    /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
878    /// container walked page by page, so nothing nested inside a board item is paid for.
879    /// Which of the issues it returns is a project is then read off `parent` — GitHub
880    /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
881    /// discriminator has to be applied to the field, which is a scalar on the issue and
882    /// costs nothing.
883    pub const SEARCH_ISSUES: &str = concat!(
884        r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
885      search(query:$search,type:$type,first:$first,after:$after){
886        pageInfo{hasNextPage endCursor}
887        nodes{__typename ...BoardIssue}
888      }
889    }"#,
890        board_issue!()
891    );
892
893    /// What a dependency read selects of each far end: enough to say which kind of item it
894    /// is, its body included for the kind marker.
895    macro_rules! related_issue {
896        () => {
897            " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
898        };
899    }
900
901    /// One issue by its own node id, which is what a qualified id names here — with what a
902    /// write of it needs and the issue does not carry in `board_issue!`: the field
903    /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
904    ///
905    /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
906    /// answers a write made moments ago with the value from before it, and resolving a node
907    /// id does not.
908    ///
909    /// **Why those two ride here and not on the fragment.** A copy or an update of an item
910    /// reads it by its own id, and with them that one read answers everything the write
911    /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
912    /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
913    /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
914    /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
915    /// they sit under one item, and this read is still one point.
916    pub const ISSUE: &str = concat!(
917        r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
918      node(id:$id){__typename ...BoardIssue ... on Issue{
919        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
920          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
921          ... on ProjectV2Field{__typename id name}
922        }pageInfo{hasNextPage}}}}}
923        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
924      }}
925    }"#,
926        board_issue!(),
927        related_issue!()
928    );
929
930    /// One project's tasks: the sub-issues of the issue that project is, each with a page of
931    /// what blocks it.
932    ///
933    /// The work this costs is the project's own size. Nothing about it grows as the board
934    /// gains projects, or as those projects gain tasks.
935    ///
936    /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
937    /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
938    /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
939    /// costs grow with its tasks rather than with the pages of them. Carried here, a task
940    /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
941    /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
942    /// It adds a connection under each issue of the page — one rate-limit point per page,
943    /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
944    /// answer questions that never read an edge.
945    pub const SUB_ISSUES: &str = concat!(
946        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
947      node(id:$id){__typename
948        ... on Issue{subIssues(first:$first,after:$after){
949          pageInfo{hasNextPage endCursor}
950          nodes{__typename ...BoardIssue ... on Issue{
951            blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
952          }}
953        }}}
954    }"#,
955        board_issue!(),
956        related_issue!()
957    );
958
959    /// What a read of the board's own `items` selects of each item's content.
960    ///
961    /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
962    /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
963    /// content by one spelling.
964    macro_rules! board_item_content {
965        () => {
966            r#" content{
967        ... on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}}
968        ... on PullRequest{__typename id}
969        ... on DraftIssue{__typename id title body createdAt updatedAt}
970      }"#
971        };
972    }
973
974    /// Reads the board's fields and one page of its items.
975    pub const BOARD: &str = concat!(
976        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!){
977      owner:repositoryOwner(login:$owner){
978        ... on ProjectV2Owner{projectV2(number:$number){...Board}}
979      }
980    } fragment Board on ProjectV2 { id title
981      fields(first:$nestedFirst){nodes{
982        ... on ProjectV2SingleSelectField{__typename id name options{id name}}
983        ... on ProjectV2Field{__typename id name}
984      }pageInfo{hasNextPage}}
985      items(first:$first,after:$after){nodes{id "#,
986        board_item_values!(),
987        board_item_content!(),
988        r#"} pageInfo{hasNextPage endCursor}}
989    }"#
990    );
991
992    /// Every carrier of one copy origin, by two reads in one request, and nothing else of
993    /// the board.
994    ///
995    /// **`originItems`** is the board's own items narrowed by its own field filter —
996    /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
997    /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
998    /// qualified id, quoted. It reads the field every carrier already holds, whichever release
999    /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1000    /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1001    /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1002    /// fresh `addProjectV2ItemById` the way that connection does.
1003    ///
1004    /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1005    /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1006    /// indexes that comment, and the index catches up with a write in a second or two rather
1007    /// than in minutes, so it finds a carrier another process wrote that the first read is
1008    /// still behind on.
1009    ///
1010    /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1011    /// — and resumes from its own cursor; a connection already walked to its end is resumed
1012    /// from its last cursor, which answers an empty page. Every candidate either read returns
1013    /// is confirmed against its own origin field before it is reported, so a token match of
1014    /// the search or anything else the filter admits never is.
1015    ///
1016    /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1017    /// own whole reads counts this one among them.
1018    pub const ORIGIN_LOOKUP: &str = concat!(
1019        r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
1020      originItems:repositoryOwner(login:$owner){
1021        ... on ProjectV2Owner{projectV2(number:$number){
1022          items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id "#,
1023        board_item_values!(),
1024        board_item_content!(),
1025        r#"} pageInfo{hasNextPage endCursor}}
1026        }}
1027      }
1028      search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1029        pageInfo{hasNextPage endCursor}
1030        nodes{__typename ...BoardIssue}
1031      }
1032    }"#,
1033        board_issue!()
1034    );
1035
1036    /// The board's own id and field definitions, and not one of its items.
1037    ///
1038    /// What a write needs of the board when the item it writes does not say: the id a field
1039    /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1040    /// origin fields. It selects no `items`, so what it costs is the board's field list
1041    /// however many items the board holds — and it decides nothing about which items those
1042    /// are, which is the question a read of one item by its own id answers instead.
1043    ///
1044    /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1045    /// board's item reads by their root counts this one among them.
1046    pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1047      boardFields:repositoryOwner(login:$owner){
1048        ... on ProjectV2Owner{projectV2(number:$number){id
1049          fields(first:$nestedFirst){nodes{
1050            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1051            ... on ProjectV2Field{__typename id name}
1052          }pageInfo{hasNextPage}}
1053        }}
1054      }
1055    }"#;
1056
1057    /// One board draft by its own node id, with the board item it sits in.
1058    ///
1059    /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1060    /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1061    /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1062    /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1063    /// board listing hands it to, and nothing has to list the board to find one.
1064    pub const DRAFT: &str = concat!(
1065        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1066      node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1067        projectV2Items(first:$boardItems){nodes{id project{id number}
1068        "#,
1069        board_item_values!(),
1070        r#"}pageInfo{hasNextPage endCursor}}}}
1071    }"#
1072    );
1073
1074    /// One issue's board memberships alone, walked past the page a read of it carried.
1075    ///
1076    /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1077    /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1078    /// boards than that page holds may have this board's entry past its end. This asks that
1079    /// one issue for its memberships and nothing else — the caller already holds the issue —
1080    /// so an answer of "this board does not hold it" is only ever given about a connection
1081    /// read to exhaustion.
1082    ///
1083    /// It selects the board item's id, its project number and the same
1084    /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1085    /// very same resolver: an issue recovered this way reports the same title, the same
1086    /// status, the same labels and the same qualified id as one whose entry was on the
1087    /// page.
1088    ///
1089    /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1090    /// multiplies through it and the membership connection can be walked at
1091    /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1092    /// further request for any issue a person really keeps.
1093    pub const ISSUE_BOARD_ITEMS: &str = concat!(
1094        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1095      node(id:$id){
1096        ... on Issue{projectItems(first:$first,after:$after){
1097          nodes{id project{id number}
1098        "#,
1099        board_item_values!(),
1100        r#"}
1101          pageInfo{hasNextPage endCursor}}}
1102      }
1103    }"#
1104    );
1105    /// Resolves the configured repository's node id, which creating an issue requires.
1106    pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1107    /// What creating an issue needs and has not read yet: the board's own id and field
1108    /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1109    /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1110    ///
1111    /// Sent at the point a create knows which repository it is for, when neither half is
1112    /// already known to this process; a create needing only one of them sends that one's own
1113    /// document. Neither half is kept past the process: a field's option ids are re-minted by
1114    /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1115    /// status.
1116    pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1117      boardFields:repositoryOwner(login:$owner){
1118        ... on ProjectV2Owner{projectV2(number:$number){id
1119          fields(first:$nestedFirst){nodes{
1120            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1121            ... on ProjectV2Field{__typename id name}
1122          }pageInfo{hasNextPage}}
1123        }}
1124      }
1125      repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1126    }"#;
1127    /// Reads both dependency directions for one issue, with each far end's own kind — and
1128    /// the issue's own body, which is where an edge to another source is recorded, so that
1129    /// half of a dependency read needs no second read of the issue or of the board.
1130    pub const ISSUE_DEPENDENCIES: &str = concat!(
1131        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1132      ... on Issue{body
1133        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1134        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1135      }}}"#,
1136        related_issue!()
1137    );
1138    /// Creates one issue in the configured repository, on no board: [`ADD_TO_BOARD`] files
1139    /// it. `CreateIssueInput.projectV2Ids` is not sent — see the crate's notes on what GitHub
1140    /// answered when it was.
1141    pub const CREATE_ISSUE: &str =
1142        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1143    /// Puts an existing issue on the configured board.
1144    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1145    /// Updates an issue's visible fields and its open or closed state in one call.
1146    pub const UPDATE_ISSUE: &str =
1147        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1148    /// Updates an existing draft's user-visible fields.
1149    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1150    /// Updates a text or single-select value on one project item.
1151    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}}}}}}}}"#;
1152    /// Writes up to three board fields and an optional clear in one ordered mutation.
1153    pub const UPDATE_FIELDS: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$second:UpdateProjectV2ItemFieldValueInput!,$third:UpdateProjectV2ItemFieldValueInput!,$clear:ClearProjectV2ItemFieldValueInput!,$writeSecond:Boolean!,$writeThird:Boolean!,$writeClear:Boolean!){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id}} second:updateProjectV2ItemFieldValue(input:$second) @include(if:$writeSecond){projectV2Item{id}} third:updateProjectV2ItemFieldValue(input:$third) @include(if:$writeThird){projectV2Item{id}} cleared:clearProjectV2ItemFieldValue(input:$clear) @include(if:$writeClear){projectV2Item{id}}}"#;
1154    /// Clears one project item's value of one field, which is what a `none` priority is.
1155    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}}}}}}}}"#;
1156    /// Creates one single-select field with its options. Only the guarded field setup may use
1157    /// this document, and only for a field the board lacks.
1158    pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1159    /// Replaces a single-select field's options. Only the guarded field setup — the
1160    /// `status-options` and `fields` operations — may use this document, because GitHub
1161    /// treats the input as the complete option list.
1162    pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1163    /// A fresh snapshot of the Status field and every board item's assignment.
1164    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}}}}}}"#;
1165    /// Files one issue under another as a sub-issue, which is what project membership is.
1166    pub const ADD_SUB_ISSUE: &str =
1167        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1168    /// Takes one issue back out of its parent.
1169    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1170    /// Adds GitHub's native issue blocked-by relationship.
1171    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1172    /// Removes one native issue blocked-by relationship.
1173    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1174    /// Deletes one issue, which takes its board item with it.
1175    ///
1176    /// The engine sends this in one situation only: undoing a copy that could not finish,
1177    /// over the items that same copy created. Deleting the issue removes the board item
1178    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1179    pub const DELETE_ISSUE: &str =
1180        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1181
1182    /// Everything this source reads about one issue comment, wherever it reaches one.
1183    ///
1184    /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1185    /// and a comment just edited are handed to one mapper, so they are selected by one
1186    /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1187    /// longer exists, and `login` is the one member every kind of actor carries.
1188    macro_rules! issue_comment {
1189        () => {
1190            "id author{login} createdAt updatedAt body url"
1191        };
1192    }
1193
1194    /// One task's comments: a page of its issue's own `comments` connection.
1195    ///
1196    /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1197    /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1198    /// list every time somebody edited it; left unordered the connection answers in the order
1199    /// the comments were written, which is the order GitHub documents for the same collection
1200    /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1201    /// node count and the caller's own page size is pushed straight down.
1202    pub const ISSUE_COMMENTS: &str = concat!(
1203        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1204        issue_comment!(),
1205        r#"}pageInfo{hasNextPage endCursor}}}}}"#
1206    );
1207    /// One issue by its own node id, with a page of its comments: what `task show` and a
1208    /// comment listing read, in one request.
1209    ///
1210    /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1211    /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1212    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1213    /// connection there would multiply through both of those documents' price, and neither
1214    /// needs one.
1215    pub const ISSUE_DETAIL: &str = concat!(
1216        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
1217      node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1218        issue_comment!(),
1219        r#"}pageInfo{hasNextPage endCursor}}}}
1220    }"#,
1221        board_issue!()
1222    );
1223
1224    /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1225    /// page of its comments when `$comments` asks for them.
1226    macro_rules! issue_details_alias {
1227        ($n:literal) => {
1228            concat!(
1229                "\n      i",
1230                stringify!($n),
1231                ":node(id:$id",
1232                stringify!($n),
1233                "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1234                issue_comment!(),
1235                "}pageInfo{hasNextPage endCursor}}}}"
1236            )
1237        };
1238    }
1239
1240    /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1241    /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1242    ///
1243    /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1244    /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1245    /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1246    /// neither — so every connection under it would be priced at nothing and the pin in
1247    /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1248    /// one-item read the model already prices, so the batch costs what its aliases cost.
1249    ///
1250    /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1251    /// slots it has no item for to the last item it does, and reads that item again; the
1252    /// price is the document's, whatever its variables, so a short batch costs what a full
1253    /// one does and nothing more.
1254    pub const ISSUE_DETAILS: &str = concat!(
1255        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!){"#,
1256        issue_details_alias!(0),
1257        issue_details_alias!(1),
1258        issue_details_alias!(2),
1259        issue_details_alias!(3),
1260        issue_details_alias!(4),
1261        issue_details_alias!(5),
1262        issue_details_alias!(6),
1263        issue_details_alias!(7),
1264        issue_details_alias!(8),
1265        issue_details_alias!(9),
1266        issue_details_alias!(10),
1267        issue_details_alias!(11),
1268        issue_details_alias!(12),
1269        issue_details_alias!(13),
1270        issue_details_alias!(14),
1271        issue_details_alias!(15),
1272        issue_details_alias!(16),
1273        issue_details_alias!(17),
1274        issue_details_alias!(18),
1275        issue_details_alias!(19),
1276        issue_details_alias!(20),
1277        issue_details_alias!(21),
1278        issue_details_alias!(22),
1279        issue_details_alias!(23),
1280        "\n    }",
1281        board_issue!()
1282    );
1283
1284    /// Which issue one comment is on, read before that comment is edited or removed.
1285    ///
1286    /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1287    /// comment id given against the wrong task would change a comment on another issue.
1288    pub const COMMENT_ISSUE: &str =
1289        r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id issue{id}}}}"#;
1290    /// Adds one comment to an issue, signed as the account the token belongs to.
1291    pub const ADD_COMMENT: &str = concat!(
1292        r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1293        issue_comment!(),
1294        r#"}}}}"#
1295    );
1296    /// Replaces the body of one issue comment.
1297    pub const UPDATE_COMMENT: &str = concat!(
1298        r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
1299        issue_comment!(),
1300        r#"}}}"#
1301    );
1302    /// Removes one issue comment. Its payload carries nothing about the comment it removed.
1303    pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
1304
1305    /// Every document above, with what this source is doing when it sends one.
1306    ///
1307    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
1308    /// name the call that was refused, and a `match` with a catch-all arm would answer a
1309    /// document added later with "talking to GitHub" and never say so.
1310    ///
1311    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
1312    /// const` here that this list omits, so the two cannot part — which is the same guard
1313    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
1314    pub const DOCUMENTS: [(&str, &str); 33] = [
1315        (SEARCH_ISSUES, "searching this board's issues"),
1316        (ISSUE, "reading one issue"),
1317        (
1318            ISSUE_BOARD_ITEMS,
1319            "reading one issue's board memberships past the page it came with",
1320        ),
1321        (SUB_ISSUES, "reading a project's tasks"),
1322        (BOARD, "reading the board"),
1323        (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
1324        (BOARD_FIELDS, "reading the board's fields"),
1325        (DRAFT, "reading one draft"),
1326        (REPOSITORY, "reading the destination repository"),
1327        (
1328            CREATION_CONTEXT,
1329            "reading the board's fields and the destination repository",
1330        ),
1331        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
1332        (CREATE_ISSUE, "creating an issue"),
1333        (ADD_TO_BOARD, "adding an issue to the board"),
1334        (UPDATE_ISSUE, "updating an issue"),
1335        (UPDATE_DRAFT, "updating a draft item"),
1336        (UPDATE_FIELD, "writing a board field"),
1337        (UPDATE_FIELDS, "writing board fields together"),
1338        (CLEAR_FIELD, "clearing a board field"),
1339        (
1340            CREATE_FIELD,
1341            "creating a board single-select field with its options",
1342        ),
1343        (
1344            STATUS_OPTIONS_SNAPSHOT,
1345            "snapshotting board Status options and assignments",
1346        ),
1347        (
1348            STATUS_OPTIONS_UPDATE,
1349            "safely replacing the board Status option list",
1350        ),
1351        (ADD_SUB_ISSUE, "filing an issue under its project"),
1352        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
1353        (ADD_BLOCKED_BY, "recording a dependency"),
1354        (REMOVE_BLOCKED_BY, "removing a dependency"),
1355        (DELETE_ISSUE, "deleting an issue"),
1356        (ISSUE_COMMENTS, "reading a task's comments"),
1357        (ISSUE_DETAIL, "reading one issue with its comments"),
1358        (
1359            ISSUE_DETAILS,
1360            "reading a batch of issues with their comments",
1361        ),
1362        (COMMENT_ISSUE, "reading which issue a comment is on"),
1363        (ADD_COMMENT, "adding a comment"),
1364        (UPDATE_COMMENT, "editing a comment"),
1365        (DELETE_COMMENT, "deleting a comment"),
1366    ];
1367}
1368
1369/// Which of GitHub's two rate limiters refused a request.
1370///
1371/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
1372/// secondary one — so an operator told the wrong one takes the wrong next step, which is
1373/// the whole reason this is carried rather than collapsed into "rate limited".
1374#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1375enum Limiter {
1376    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
1377    Primary,
1378    /// The burst limiter over content-generating requests, which nothing reports.
1379    Secondary,
1380}
1381
1382/// The wordings GitHub answers a secondary rate limit with.
1383///
1384/// It sends them under a forbidden status, under a too-many-requests status, and inside
1385/// the `errors` of a *successful* response, which is why the text is what this matches on
1386/// rather than the status. `abuse detection` is the wording GitHub used before the
1387/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
1388/// what a burst of content creation is refused with.
1389///
1390/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
1391/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
1392/// when, and the drift gate reconciles the two lists both ways. Public for that gate
1393/// alone — a caller has no use for it, and matching on a refusal is this source's job.
1394pub const SECONDARY_WORDINGS: [&str; 5] = [
1395    "secondary rate limit",
1396    "temporarily blocked from content creation",
1397    "abuse detection",
1398    "submitted too quickly",
1399    "exceeded a secondary",
1400];
1401
1402/// The wordings GitHub answers an exhausted primary budget with.
1403///
1404/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
1405/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
1406/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
1407/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
1408/// two phrases is a substring of it, so without it that answer read as a refusal that will
1409/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
1410/// one reason.
1411pub const PRIMARY_WORDINGS: [&str; 4] = [
1412    "api rate limit exceeded",
1413    "api rate limit already exceeded",
1414    "rate limit exceeded",
1415    "rate_limited",
1416];
1417
1418/// What a response *says about itself*, which is the only place a refusal can be read.
1419///
1420/// Deliberately not the whole response body. A board is a place people write about their
1421/// own work, and a task on it titled "the secondary rate limit" would, matched across the
1422/// raw text, turn a perfectly good answer into a refusal this source then waited out and
1423/// reported. So the item data is never read: what is read is GitHub's own REST-style
1424/// `message` envelope, which is what a forbidden status carries, and the `message` and
1425/// `type` of each GraphQL error, which is where a *successful* response says it.
1426///
1427/// A body that is not JSON at all has nothing structured to read, so only a failing
1428/// response's own text is taken — a successful response that is not JSON is malformed
1429/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
1430fn refusal_wording(status: StatusCode, body: &str) -> String {
1431    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
1432        return if status.is_success() {
1433            String::new()
1434        } else {
1435            body.to_owned()
1436        };
1437    };
1438    let mut said: Vec<&str> = parsed
1439        .get("message")
1440        .and_then(Value::as_str)
1441        .into_iter()
1442        .collect();
1443    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
1444        for error in errors {
1445            said.extend(
1446                ["message", "type"]
1447                    .into_iter()
1448                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
1449            );
1450        }
1451    }
1452    said.join("; ")
1453}
1454
1455impl Limiter {
1456    /// Which limiter refused this response, or `None` when none of them did.
1457    ///
1458    /// The wording is read first and the status only decides what carries none of it,
1459    /// because GitHub answers a secondary limit with a forbidden status far more often
1460    /// than with too-many-requests — while a forbidden status saying nothing about a limit
1461    /// really is a credential this token lacks.
1462    ///
1463    /// A response is a refusal because of its status or its own wording. A spent budget
1464    /// only ever explains one; it never turns an answer into a refusal.
1465    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
1466        let normalized = refusal_wording(status, body).to_ascii_lowercase();
1467        if SECONDARY_WORDINGS
1468            .iter()
1469            .any(|wording| normalized.contains(wording))
1470        {
1471            return Some(Self::Secondary);
1472        }
1473        if status == StatusCode::TOO_MANY_REQUESTS {
1474            return Some(Self::Primary);
1475        }
1476        // An exhausted budget *explains* a response that failed; it does not make one that
1477        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
1478        // request the budget allowed as well as on the ones it then refuses, so reading
1479        // the header alone threw away a good answer — and, once refusals were retried,
1480        // replayed a request that had already taken effect.
1481        if !status.is_success() && budget_exhausted {
1482            return Some(Self::Primary);
1483        }
1484        // A successful response saying it: GitHub reports a GraphQL rate limit in the
1485        // `errors` of an HTTP 200, where nothing about the status says so at all.
1486        if status.is_success()
1487            && PRIMARY_WORDINGS
1488                .iter()
1489                .any(|wording| normalized.contains(wording))
1490        {
1491            return Some(Self::Primary);
1492        }
1493        None
1494    }
1495
1496    /// What this limiter is called where an operator can look it up.
1497    const fn name(self) -> &'static str {
1498        match self {
1499            Self::Primary => "GitHub's primary API rate limit",
1500            Self::Secondary => "GitHub's secondary rate limit",
1501        }
1502    }
1503
1504    /// What the endpoint an operator would go and check says about this limiter.
1505    const fn where_to_look(self) -> &'static str {
1506        match self {
1507            Self::Primary => {
1508                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
1509                 comes back."
1510            }
1511            Self::Secondary => {
1512                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
1513                 primary budget and does not report this one, so budget showing there says \
1514                 nothing about this refusal, and every further attempt extends it."
1515            }
1516        }
1517    }
1518
1519    /// The next step this limiter actually calls for.
1520    const fn what_to_do(self) -> &'static str {
1521        match self {
1522            Self::Primary => {
1523                "wait for the reset `gh api rate_limit` reports, then run the command again."
1524            }
1525            Self::Secondary => {
1526                "leave this board alone for a few minutes, then run the command again — or \
1527                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
1528            }
1529        }
1530    }
1531}
1532
1533/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
1534#[derive(Debug, Clone, Copy)]
1535struct Limited {
1536    limiter: Limiter,
1537    hint: Option<u64>,
1538}
1539
1540impl Limited {
1541    /// What the caller is told once this source has waited as long as it may.
1542    ///
1543    /// Both limiters report as [`SourceError::RateLimited`], because that is what
1544    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
1545    /// about *which* limiter it was makes it a different kind of failure. What differs is
1546    /// the operator's next step, and that is what the message carries — a secondary
1547    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
1548    /// budget looks fine, and then back to retry the very burst that was refused.
1549    fn exhausted(
1550        self,
1551        doing: &str,
1552        waits: u32,
1553        waited: Duration,
1554        needed: Duration,
1555        budget: Duration,
1556    ) -> SourceError {
1557        SourceError::RateLimited {
1558            retry_after_seconds: self.hint,
1559            message: Some(format!(
1560                "{} refused this source while {doing}; it waited {} out over {} and was refused \
1561                 again, and the next wait of {} would take it past the {} one call may spend \
1562                 waiting. {} next: {}",
1563                self.limiter.name(),
1564                plural(waits, "refusal"),
1565                seconds(waited),
1566                seconds(needed),
1567                seconds(budget),
1568                self.limiter.where_to_look(),
1569                self.limiter.what_to_do(),
1570            )),
1571        }
1572    }
1573}
1574
1575/// One HTTP attempt's result, with what its response said about the rate limit.
1576///
1577/// The two travel together so the record and the outcome are written from the same place:
1578/// what a response said about the budget is only readable while that response is in hand,
1579/// and what the attempt *meant* is only decidable once its body has been read.
1580struct Attempted {
1581    result: Result<Value, Attempt>,
1582    limits: accounting::RateLimit,
1583    /// GitHub's own reported cost for this call, for a document that asked for it.
1584    reported_cost: Option<u64>,
1585}
1586
1587/// One attempt's outcome: an error to report, or a rate limit to wait out.
1588enum Attempt {
1589    Failed(SourceError),
1590    Limited(Limited),
1591}
1592
1593fn plural(count: u32, thing: &str) -> String {
1594    if count == 1 {
1595        format!("{count} {thing}")
1596    } else {
1597        format!("{count} {thing}s")
1598    }
1599}
1600
1601fn seconds(duration: Duration) -> String {
1602    format!("{:.1}s", duration.as_secs_f64())
1603}
1604
1605/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
1606///
1607/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
1608/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
1609/// header, and neither is what makes a response a refusal — so the whole cost of one this
1610/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
1611/// instead. Refusing the response over the header would turn a readable refusal into an
1612/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
1613fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
1614    value
1615        .and_then(|value| value.to_str().ok())
1616        .and_then(|value| value.trim().parse::<u64>().ok())
1617}
1618
1619/// Every mutation this source sends creates content — an issue, a board item, a field of
1620/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
1621/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
1622/// and what the keyword says are the same set. That is what makes the keyword a sound test
1623/// rather than a convenient one: pacing an edit or a removal the limiter might not have
1624/// counted costs a wait, and not pacing one it did count costs the next fifty minutes.
1625fn is_mutation(query: &str) -> bool {
1626    query.trim_start().starts_with("mutation")
1627}
1628
1629/// What this source was doing, for a diagnostic that has to say so.
1630///
1631/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
1632/// a document added without a description is caught by that list's own gate instead of
1633/// falling through to the vague arm below.
1634fn operation_description(query: &str) -> &'static str {
1635    graphql::DOCUMENTS
1636        .iter()
1637        .find(|(document, _)| *document == query)
1638        .map_or("talking to GitHub", |(_, doing)| *doing)
1639}
1640
1641/// GitHub's published ceiling on content-generating requests, per minute.
1642///
1643/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
1644/// GitHub's number rather than this source's: [`MIN_MUTATION_INTERVAL_MS`] is *derived*
1645/// from it, so a pacing value checked only against itself cannot go stale here.
1646pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
1647/// The same ceiling as GitHub publishes it per hour, which this source does **not** pace
1648/// at. See [`MIN_MUTATION_INTERVAL_MS`] for why the per-minute bound is the one that
1649/// governs; it is pinned beside its sibling so the gate would notice either one moving.
1650pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
1651/// Shortest interval between two content-creating mutations, in milliseconds.
1652///
1653/// GitHub documents two secondary limits on content-generating requests:
1654/// [`CONTENT_CREATION_PER_MINUTE`] and [`CONTENT_CREATION_PER_HOUR`]. 60000/80 is 750, so
1655/// a mutation every 750 ms is the fastest rate that cannot exceed the per-minute bound,
1656/// and that is the bound a copy actually trips: a copy of one plan-sized project is a
1657/// burst of a few dozen mutations inside a few seconds. The hourly bound works out at one
1658/// every 7.2 seconds sustained, which no single copy reaches and which, used as the
1659/// spacing here, would turn an ordinary copy into an hour of waiting — so it is
1660/// deliberately *not* what this paces at. An installation that wants the hourly bound
1661/// honoured for a long sequence of copies says so through
1662/// `pacing.min_mutation_interval_ms`.
1663pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
1664/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
1665///
1666/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
1667/// own advice for a secondary limit — wait, and wait longer each time — without spending
1668/// the first minute of a transient refusal doing nothing.
1669pub const RETRY_BACKOFF_MS: u64 = 1_000;
1670/// Total time one call may spend waiting out rate limits before it reports a failure.
1671///
1672/// Two minutes is long enough to ride out the refusals a paced copy still collects and
1673/// short enough that a command an operator is watching returns. The bound is what makes
1674/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
1675/// the limiter, not in a process nobody can tell from a wedged one.
1676pub const RETRY_BUDGET_MS: u64 = 120_000;
1677
1678fn default_token_env() -> String {
1679    "GH_PROJECTS_TOKEN".to_owned()
1680}
1681fn default_endpoint() -> String {
1682    "https://api.github.com/graphql".to_owned()
1683}
1684
1685/// The name of a `Status` single-select option on the board.
1686///
1687/// Validated on the way in rather than checked later, so a blank option name — which
1688/// nothing on a board can be — is a state this type cannot hold.
1689#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1690#[serde(try_from = "String")]
1691#[schemars(extend("minLength" = 1))]
1692pub struct ColumnName(String);
1693
1694impl ColumnName {
1695    /// The option name, as the board spells it.
1696    fn as_str(&self) -> &str {
1697        &self.0
1698    }
1699}
1700
1701impl TryFrom<String> for ColumnName {
1702    type Error = String;
1703
1704    fn try_from(name: String) -> Result<Self, Self::Error> {
1705        if name.trim().is_empty() {
1706            return Err("a status_mapping option name cannot be blank".to_owned());
1707        }
1708        Ok(Self(name))
1709    }
1710}
1711
1712/// The two closed states this product can mean.
1713///
1714/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which is neither finished
1715/// work nor abandoned work, so nothing here ever writes it.
1716#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
1717#[serde(rename_all = "kebab-case")]
1718pub enum ClosedState {
1719    /// `COMPLETED` — precisely done.
1720    Completed,
1721    /// `NOT_PLANNED` — precisely cancelled.
1722    NotPlanned,
1723}
1724
1725impl ClosedState {
1726    const fn reason(self) -> &'static str {
1727        match self {
1728            Self::Completed => "COMPLETED",
1729            Self::NotPlanned => "NOT_PLANNED",
1730        }
1731    }
1732}
1733
1734/// Configuration for one GitHub Projects v2 board.
1735#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1736#[serde(default, deny_unknown_fields)]
1737pub struct GitHubProjectsConfig {
1738    /// Login of the user or organization which owns the board.
1739    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
1740    /// The project number shown in the board's GitHub URL.
1741    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
1742    // 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.
1743    /// `owner/name` of the repository this source creates an issue in when the item's own
1744    /// `repositories` field does not decide it.
1745    ///
1746    /// An item naming exactly one repository is created there; a task or a document naming
1747    /// none or several is created in its parent project's repository; and a project, or a
1748    /// task or document with no parent, naming none or several is created here. A board
1749    /// has no repository of its own and `createIssue` requires one, so a write without
1750    /// this is refused naming the field. Reads never need it.
1751    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
1752    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
1753    /// Environment variable containing a fine-grained token with Projects and Issues
1754    /// read/write plus Pull requests read-only access for every repository represented on
1755    /// the board.
1756    #[serde(default = "default_token_env")]
1757    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
1758    /// GraphQL endpoint. GitHub Enterprise installations may override it.
1759    #[serde(default = "default_endpoint")]
1760    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
1761    /// Per-instance mapping from a status category to the option of the board's one
1762    /// `Status` field it lands on, for a task and for a project.
1763    ///
1764    /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
1765    /// `null` to disable the category for both, or `{task, project}` naming it per kind,
1766    /// where a kind left out leaves the category unmapped for that kind. A category this
1767    /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
1768    /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
1769    /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
1770    /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
1771    /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
1772    /// either kind. No two categories may name one option for the same kind, ignoring case.
1773    /// `unknown` may name one existing option; every unknown word then lands on it and
1774    /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
1775    /// each unknown word because it never creates board options.
1776    #[serde(default)]
1777    pub status_mapping: StatusMapping,
1778    /// Per-instance mapping from a task's priority to an option of this board's
1779    /// single-select field named `Priority`.
1780    ///
1781    /// Absent, this source holds no priority: every task reads as `none`, and a write of any
1782    /// other priority is refused before it reaches this board. Present, each of `urgent`,
1783    /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
1784    /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
1785    /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
1786    /// no two levels may name one option. Reads and writes never create the field or an
1787    /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
1788    /// the board lacks is refused pointing there.
1789    #[serde(default)]
1790    pub priority_mapping: Option<PriorityMappingConfig>,
1791    /// How fast this source writes, and how long it waits out a rate-limit refusal.
1792    ///
1793    /// Every field keeps its shipped default when it is absent, and the defaults are
1794    /// GitHub's own published limits rather than taste. See [`Pacing`].
1795    #[serde(default)]
1796    pub pacing: PacingConfig,
1797}
1798
1799/// Which option of the board's `Priority` field each priority lands on.
1800///
1801/// One member per level rather than a map, so a key that is not a level is refused where
1802/// the configuration is read, naming the levels there are. `none` is not a member: it is no
1803/// value in the field, not an option of it.
1804#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1805#[serde(default, deny_unknown_fields)]
1806pub struct PriorityMappingConfig {
1807    /// The option `urgent` lands on; `Urgent` when absent.
1808    pub urgent: Option<PriorityOptionName>,
1809    /// The option `high` lands on; `High` when absent.
1810    pub high: Option<PriorityOptionName>,
1811    /// The option `medium` lands on; `Medium` when absent.
1812    pub medium: Option<PriorityOptionName>,
1813    /// The option `low` lands on; `Low` when absent.
1814    pub low: Option<PriorityOptionName>,
1815}
1816
1817/// The name of an option of the board's `Priority` single-select field.
1818///
1819/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
1820/// blank name.
1821#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1822#[serde(try_from = "String")]
1823#[schemars(extend("minLength" = 1))]
1824pub struct PriorityOptionName(String);
1825
1826impl PriorityOptionName {
1827    /// The option name, as the board spells it.
1828    fn as_str(&self) -> &str {
1829        &self.0
1830    }
1831}
1832
1833impl TryFrom<String> for PriorityOptionName {
1834    type Error = String;
1835
1836    fn try_from(name: String) -> Result<Self, Self::Error> {
1837        if name.trim().is_empty() {
1838            return Err("a priority_mapping option name cannot be blank".to_owned());
1839        }
1840        Ok(Self(name))
1841    }
1842}
1843
1844/// The name of the board field a priority is held in.
1845pub const PRIORITY_FIELD: &str = "Priority";
1846
1847/// The four priorities a board option can hold, in the order a new `Priority` field lists
1848/// them. `none` is not among them: it is the field holding no value.
1849///
1850/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
1851/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
1852/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
1853/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
1854pub const PRIORITY_LEVELS: [Priority; 4] = [
1855    Priority::Urgent,
1856    Priority::High,
1857    Priority::Medium,
1858    Priority::Low,
1859];
1860
1861/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
1862/// see that list for what this pins.
1863#[must_use]
1864pub const fn level_position(priority: Priority) -> Option<usize> {
1865    match priority {
1866        Priority::None => None,
1867        Priority::Urgent => Some(0),
1868        Priority::High => Some(1),
1869        Priority::Medium => Some(2),
1870        Priority::Low => Some(3),
1871    }
1872}
1873
1874/// This instance's complete priority-to-option mapping, read in both directions.
1875///
1876/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
1877/// two levels name one option.
1878#[derive(Debug, Clone)]
1879struct PriorityMapping {
1880    options: [PriorityOptionName; 4],
1881}
1882
1883impl PriorityMapping {
1884    fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
1885        let shipped = |name: &str| PriorityOptionName(name.to_owned());
1886        let mapping = Self {
1887            options: [
1888                config.urgent.unwrap_or_else(|| shipped("Urgent")),
1889                config.high.unwrap_or_else(|| shipped("High")),
1890                config.medium.unwrap_or_else(|| shipped("Medium")),
1891                config.low.unwrap_or_else(|| shipped("Low")),
1892            ],
1893        };
1894        for (index, option) in mapping.options.iter().enumerate() {
1895            if let Some(earlier) = mapping.options[..index]
1896                .iter()
1897                .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
1898            {
1899                return Err(SourceError::Config {
1900                    message: format!(
1901                        "priority_mapping of source {instance} sends both {} and {} to the board \
1902                         option {:?}; one option cannot read back as two priorities",
1903                        PRIORITY_LEVELS[earlier],
1904                        PRIORITY_LEVELS[index],
1905                        option.as_str()
1906                    ),
1907                });
1908            }
1909        }
1910        Ok(mapping)
1911    }
1912
1913    /// The option `priority` lands on, or `None` for `none`, which is no option at all.
1914    fn option(&self, priority: Priority) -> Option<&str> {
1915        level_position(priority).map(|index| self.options[index].as_str())
1916    }
1917
1918    /// The priority a board option name reports, or `None` when nothing maps to it.
1919    fn priority_of(&self, option: &str) -> Option<Priority> {
1920        self.options
1921            .iter()
1922            .position(|name| name.as_str().eq_ignore_ascii_case(option))
1923            .map(|index| PRIORITY_LEVELS[index])
1924    }
1925
1926    /// Every mapped option name, in the order a new `Priority` field lists them.
1927    fn names(&self) -> impl Iterator<Item = &str> {
1928        self.options.iter().map(PriorityOptionName::as_str)
1929    }
1930}
1931
1932/// What one item's `Priority` field says, read through this instance's mapping.
1933#[derive(Debug, Clone, PartialEq, Eq)]
1934enum HeldPriority {
1935    /// A priority this source reports: an option the mapping names, or no value (`none`).
1936    Read(Priority),
1937    /// An option the mapping does not name, which is never read as a level or as `none`.
1938    Unmapped(String),
1939}
1940
1941/// How fast this source writes, and how long it waits out a rate-limit refusal.
1942///
1943/// Configurable because a GitHub Enterprise installation sets its own limits and an
1944/// operator who has already been refused may want to go slower still — not because the
1945/// defaults are guesses.
1946#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1947#[serde(default, deny_unknown_fields)]
1948pub struct PacingConfig {
1949    /// Shortest interval between two content-creating mutations, in milliseconds.
1950    ///
1951    /// Zero sends them as fast as they are asked for, which is what a fixture server on
1952    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
1953    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.
1954    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
1955    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
1956    /// zero while there is a budget to spend, because a schedule of zero-length waits
1957    /// consumes none of it and so never ends.
1958    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.
1959    /// Total time one call may spend waiting out rate limits, in milliseconds.
1960    ///
1961    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
1962    /// the bound is what makes this a wait rather than a hang.
1963    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.
1964}
1965
1966/// The largest any pacing setting may be, in milliseconds.
1967///
1968/// One hour. GitHub's own harshest published bound on content-generating requests works
1969/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
1970/// anything a real limit asks for, and past it the settings stop describing pacing at all:
1971/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
1972/// and an interval beyond it is a command that never sends its second mutation. It also
1973/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
1974/// what a `Duration` can hold on every platform.
1975pub const MAX_PACING_MS: u64 = 3_600_000;
1976
1977/// [`PacingConfig`] with every default resolved and every value checked, which is what the
1978/// source holds.
1979#[derive(Debug, Clone, Copy)]
1980struct Pacing {
1981    min_mutation_interval: Duration,
1982    retry_backoff: Duration,
1983    retry_budget: Duration,
1984}
1985
1986impl Pacing {
1987    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
1988    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
1989        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
1990            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
1991                message: format!(
1992                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
1993                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
1994                     GitHub's own harshest published limit"
1995                ),
1996            }),
1997            Some(value) => Ok(Duration::from_millis(value)),
1998            None => Ok(Duration::from_millis(default)),
1999        };
2000        let retry_backoff = bounded(
2001            config.retry_backoff_ms,
2002            RETRY_BACKOFF_MS,
2003            "retry_backoff_ms",
2004        )?;
2005        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
2006        if retry_backoff.is_zero() && !retry_budget.is_zero() {
2007            return Err(SourceError::Config {
2008                message: format!(
2009                    "pacing.retry_backoff_ms of source {instance} is 0 while \
2010                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
2011                     none of that budget, so it would retry a refusal forever. Set a backoff of \
2012                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
2013                     waiting at all",
2014                    retry_budget.as_millis()
2015                ),
2016            });
2017        }
2018        Ok(Self {
2019            min_mutation_interval: bounded(
2020                config.min_mutation_interval_ms,
2021                MIN_MUTATION_INTERVAL_MS,
2022                "min_mutation_interval_ms",
2023            )?,
2024            retry_backoff,
2025            retry_budget,
2026        })
2027    }
2028}
2029
2030/// Factory for [`GitHubProjectsSource`].
2031#[derive(Debug, Clone, Copy, Default)]
2032pub struct Plugin;
2033
2034impl SourcePlugin for Plugin {
2035    fn kind(&self) -> &'static str {
2036        KIND
2037    }
2038    fn config_schema(&self) -> Schema {
2039        schema_for!(GitHubProjectsConfig)
2040    }
2041    fn build(
2042        &self,
2043        name: &SourceName,
2044        config: &Value,
2045        secrets: &dyn SecretResolver,
2046    ) -> Result<Box<dyn TaskSource>, SourceError> {
2047        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
2048    }
2049
2050    fn build_with_clock(
2051        &self,
2052        name: &SourceName,
2053        config: &Value,
2054        secrets: &dyn SecretResolver,
2055        clock: SharedClock,
2056    ) -> Result<Box<dyn TaskSource>, SourceError> {
2057        self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
2058    }
2059}
2060
2061impl Plugin {
2062    /// Build a source recording every request it sends into an accounting the caller holds.
2063    ///
2064    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
2065    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
2066    /// session total rather than two — see [`accounting`] and
2067    /// [`GitHubProjectsSource::recording_into`].
2068    ///
2069    /// # Errors
2070    ///
2071    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
2072    /// [`SourceError::Config`] for configuration this plugin cannot use and
2073    /// [`SourceError::Auth`] for a credential it cannot find.
2074    pub fn build_recording_into(
2075        &self,
2076        name: &SourceName,
2077        config: &Value,
2078        secrets: &dyn SecretResolver,
2079        ledger: Arc<Accounting>,
2080    ) -> Result<Box<dyn TaskSource>, SourceError> {
2081        self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
2082    }
2083
2084    fn build_recording_with_clock(
2085        &self,
2086        name: &SourceName,
2087        config: &Value,
2088        secrets: &dyn SecretResolver,
2089        ledger: Arc<Accounting>,
2090        clock: SharedClock,
2091    ) -> Result<Box<dyn TaskSource>, SourceError> {
2092        let config: GitHubProjectsConfig =
2093            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
2094                message: format!("source {name}: {e}"),
2095            })?;
2096        let prefix = format!("source {name}: ");
2097        let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
2098            .map_err(|error| match error {
2099                // The shared `StatusMapping::distinct` names the source itself.
2100                SourceError::Config { message } if message.starts_with(&prefix) => {
2101                    SourceError::Config { message }
2102                }
2103                SourceError::Config { message } => SourceError::Config {
2104                    message: format!("{prefix}{message}"),
2105                },
2106                SourceError::Auth { message } => SourceError::Auth {
2107                    message: format!("source {name}: {message}"),
2108                },
2109                other => other,
2110            })?;
2111        source.clock = clock;
2112        Ok(Box::new(source))
2113    }
2114}
2115
2116/// Where a status category lands on this board, once configuration is resolved.
2117#[derive(Debug, Clone, PartialEq, Eq)]
2118enum StatusTarget {
2119    /// Not usable against this instance for this kind, and why.
2120    Disabled(UnmappedStatus),
2121    /// The board's `Status` option of this name.
2122    Column(ColumnName),
2123    /// A closed issue, with both its board option and the reason that says which closed it means.
2124    // 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.
2125    Terminal(ColumnName, ClosedState),
2126}
2127
2128/// Every status category, in the order the vocabulary declares them.
2129///
2130/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
2131/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
2132/// added to the shared vocabulary fails to compile until it is named there, and this
2133/// crate's suite reconciles this list against that enum's own derived schema, which is
2134/// generated from the variants rather than written beside them. The schema is what
2135/// catches a list left one short — a list checking only the positions it already holds
2136/// would pass while every mapping indexed by the new position panicked.
2137pub const CATEGORIES: [StatusCategory; 8] = [
2138    StatusCategory::Draft,
2139    StatusCategory::Backlog,
2140    StatusCategory::Todo,
2141    StatusCategory::Queued,
2142    StatusCategory::InProgress,
2143    StatusCategory::Done,
2144    StatusCategory::Cancelled,
2145    StatusCategory::Unknown,
2146];
2147
2148/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
2149#[must_use]
2150pub const fn category_position(category: StatusCategory) -> usize {
2151    match category {
2152        StatusCategory::Draft => 0,
2153        StatusCategory::Backlog => 1,
2154        StatusCategory::Todo => 2,
2155        StatusCategory::Queued => 3,
2156        StatusCategory::InProgress => 4,
2157        StatusCategory::Done => 5,
2158        StatusCategory::Cancelled => 6,
2159        StatusCategory::Unknown => 7,
2160    }
2161}
2162
2163/// The spelling a status category is configured and reported under.
2164fn category_name(category: StatusCategory) -> &'static str {
2165    match category {
2166        StatusCategory::Draft => "draft",
2167        StatusCategory::Backlog => "backlog",
2168        StatusCategory::Todo => "todo",
2169        StatusCategory::Queued => "queued",
2170        StatusCategory::InProgress => "in-progress",
2171        StatusCategory::Done => "done",
2172        StatusCategory::Cancelled => "cancelled",
2173        StatusCategory::Unknown => "unknown",
2174    }
2175}
2176
2177/// A shipped default's option name.
2178///
2179/// The literals below are this file's own and non-blank, and they are validated by the
2180/// one constructor a configured name goes through rather than beside it.
2181fn shipped_column(name: &'static str) -> ColumnName {
2182    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
2183}
2184
2185/// The shipped default for one category this instance's `status_mapping` does not mention,
2186/// for either kind.
2187fn shipped_default(category: StatusCategory) -> StatusTarget {
2188    match category {
2189        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
2190        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
2191        StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
2192        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
2193        StatusCategory::Done => {
2194            StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
2195        }
2196        StatusCategory::Cancelled => {
2197            StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
2198        }
2199        StatusCategory::Draft | StatusCategory::Unknown => {
2200            StatusTarget::Disabled(UnmappedStatus::Unconfigured)
2201        }
2202    }
2203}
2204
2205/// The two kinds a status is written and read for, each with its own half of the mapping.
2206const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
2207
2208/// This instance's complete category-to-target mapping for each kind, read in both
2209/// directions.
2210///
2211/// One target per category per kind, held at that category's own [`category_position`], so
2212/// a category missing from the mapping, named twice in it, or filed out of order is a state
2213/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
2214/// targets are options of the board's one `Status` field.
2215#[derive(Debug, Clone)]
2216struct BoardStatuses {
2217    tasks: [StatusTarget; CATEGORIES.len()],
2218    projects: [StatusTarget; CATEGORIES.len()],
2219}
2220
2221impl BoardStatuses {
2222    /// Resolve `configured` against the shipped defaults, refusing two categories one kind
2223    /// would read back from one option.
2224    ///
2225    /// A category the mapping does not mention keeps its shipped default for both kinds; one
2226    /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
2227    /// omits unmapped rather than defaulted.
2228    fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
2229        let resolve_kind =
2230            |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
2231                // `CATEGORIES[position] == category` for every category — the crate's suite
2232                // asserts it — so mapping the list in order fills each category's own slot.
2233                let mut targets = CATEGORIES.map(shipped_default);
2234                for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
2235                    if !configured.mentions(category) {
2236                        continue;
2237                    }
2238                    *slot = match configured.name_for(category, kind) {
2239                        Err(why) => StatusTarget::Disabled(why),
2240                        Ok(name) => {
2241                            let option = ColumnName::try_from(name.as_str().to_owned())
2242                                .map_err(|message| SourceError::Config { message })?;
2243                            match category {
2244                                StatusCategory::Done => {
2245                                    StatusTarget::Terminal(option, ClosedState::Completed)
2246                                }
2247                                StatusCategory::Cancelled => {
2248                                    StatusTarget::Terminal(option, ClosedState::NotPlanned)
2249                                }
2250                                _ => StatusTarget::Column(option),
2251                            }
2252                        }
2253                    };
2254                }
2255                StatusMapping::distinct(
2256                    instance,
2257                    kind,
2258                    CATEGORIES
2259                        .iter()
2260                        .zip(&targets)
2261                        .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
2262                )?;
2263                Ok(targets)
2264            };
2265        Ok(Self {
2266            tasks: resolve_kind(ItemKind::Task)?,
2267            projects: resolve_kind(ItemKind::Project)?,
2268        })
2269    }
2270
2271    /// Every category's target for `kind`, in category order.
2272    const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
2273        match kind {
2274            ItemKind::Task => &self.tasks,
2275            ItemKind::Project => &self.projects,
2276        }
2277    }
2278
2279    fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
2280        &self.targets(kind)[category_position(category)]
2281    }
2282
2283    /// The category a board option name reports for `kind`, or `None` when nothing of that
2284    /// kind maps to it.
2285    fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
2286        CATEGORIES.into_iter().find(|category| {
2287            self.target(kind, *category)
2288                .option()
2289                .is_some_and(|name| name.eq_ignore_ascii_case(option))
2290        })
2291    }
2292
2293    /// Every option name either kind maps a category to, each once ignoring case, in
2294    /// category order with a task's name before a project's — what the guarded setup asks
2295    /// the `Status` field to hold.
2296    fn wanted(&self) -> Vec<String> {
2297        let mut wanted: Vec<String> = Vec::new();
2298        for category in CATEGORIES {
2299            for kind in STATUS_KINDS {
2300                if let Some(name) = self.target(kind, category).option()
2301                    && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
2302                {
2303                    wanted.push(name.to_owned());
2304                }
2305            }
2306        }
2307        wanted
2308    }
2309
2310    /// The status an item of `kind` reports, from the three things a read of it says: its
2311    /// board `Status` option, whether its issue is closed, and the reason it was closed with.
2312    ///
2313    /// The closed state decides the category and the `Status` option decides the name, so
2314    /// a closed issue sitting in a "Shipped" column reports `done` named `Shipped`, whatever
2315    /// its kind. A closed issue whose reason is `DUPLICATE` or `REOPENED` reports `Unknown`:
2316    /// a duplicate is not finished work, and calling it done is a lie the next copy would
2317    /// write back. `REOPENED`-while-closed is a state this source can never produce, so
2318    /// it is read permissively rather than refused — reads are faithful, and refusals
2319    /// belong on writes. An open item's option reads through its own kind's mapping, and an
2320    /// option that mapping does not name reads as `Unknown` under its own name.
2321    ///
2322    /// One function of those three rather than of a response, so a narrow status write can
2323    /// answer what a re-read would report by applying it to the state it has just written.
2324    fn status(
2325        &self,
2326        kind: ItemKind,
2327        option: Option<&str>,
2328        closed: bool,
2329        reason: Option<&str>,
2330    ) -> Status {
2331        if closed {
2332            let category = match reason {
2333                None | Some("COMPLETED") => StatusCategory::Done,
2334                Some("NOT_PLANNED") => StatusCategory::Cancelled,
2335                Some(_) => StatusCategory::Unknown,
2336            };
2337            let fallback = match category {
2338                StatusCategory::Done => "Done",
2339                StatusCategory::Cancelled => "Cancelled",
2340                _ => "Closed",
2341            };
2342            return Status {
2343                category,
2344                name: option.unwrap_or(fallback).to_owned(),
2345            };
2346        }
2347        let name = option.unwrap_or("Open").to_owned();
2348        Status {
2349            category: self
2350                .category_of(kind, &name)
2351                .unwrap_or(StatusCategory::Unknown),
2352            name,
2353        }
2354    }
2355}
2356
2357impl BoardStatuses {
2358    /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
2359    /// case; a kind lacking none is left out.
2360    fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
2361        STATUS_KINDS
2362            .into_iter()
2363            .filter_map(|kind| {
2364                let missing: Vec<String> = self
2365                    .targets(kind)
2366                    .iter()
2367                    .filter_map(StatusTarget::option)
2368                    .filter(|wanted| {
2369                        !existing
2370                            .iter()
2371                            .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2372                    })
2373                    .map(str::to_owned)
2374                    .collect();
2375                (!missing.is_empty()).then_some(KindMissing { kind, missing })
2376            })
2377            .collect()
2378    }
2379}
2380
2381impl StatusTarget {
2382    /// The board option this target selects, or `None` for an unmapped one.
2383    fn option(&self) -> Option<&str> {
2384        match self {
2385            Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
2386            Self::Disabled(_) => None,
2387        }
2388    }
2389}
2390
2391// 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.
2392/// One repository this source can create an issue in, as `owner/name`.
2393///
2394/// Every `createIssue` this source sends names one of these: the item's own single
2395/// `repositories` entry, else its parent project issue's repository, else the configured
2396/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
2397/// that choice and says what it refuses before `createIssue`.
2398// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
2399#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
2400struct RepositoryTarget {
2401    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
2402    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
2403}
2404
2405impl RepositoryTarget {
2406    fn parse(value: &str) -> Result<Self, SourceError> {
2407        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
2408            message: format!(
2409                "repository must be spelled owner/name; {value:?} names no repository"
2410            ),
2411        })?;
2412        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
2413            return Err(SourceError::Config {
2414                message: format!(
2415                    "repository must be spelled owner/name with a GitHub login and one \
2416                     repository name; {value:?} is not"
2417                ),
2418            });
2419        }
2420        Ok(Self {
2421            owner: owner.to_owned(),
2422            name: name.to_owned(),
2423        })
2424    }
2425
2426    /// The one host whose repositories this source creates issues in, spelled once: it is
2427    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
2428    const HOST: &str = "github.com";
2429
2430    fn origin(&self) -> String {
2431        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
2432    }
2433
2434    /// The repository a normalized origin names, or why it is none this source can create
2435    /// an issue in: another host, or more or fewer than `owner/name` under this one.
2436    fn from_origin(origin: &Repository) -> Result<Self, String> {
2437        let not_here = || {
2438            format!(
2439                "{} is not a {}/owner/name repository",
2440                origin.as_str(),
2441                Self::HOST
2442            )
2443        };
2444        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
2445        if host != Self::HOST {
2446            return Err(not_here());
2447        }
2448        Self::parse(rest).map_err(|_| not_here())
2449    }
2450
2451    fn slug(&self) -> String {
2452        format!("{}/{}", self.owner, self.name)
2453    }
2454}
2455
2456/// A source which reads GitHub afresh for every operation.
2457pub struct GitHubProjectsSource {
2458    /// This source's configured name, used both to tell a far end naming this source
2459    /// from one naming a system it knows nothing about, and to name the instance a
2460    /// status refusal is about.
2461    name: SourceName,
2462    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
2463    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
2464    repository: Option<RepositoryTarget>,
2465    endpoint: Url,
2466    token: SecretString,
2467    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
2468    statuses: BoardStatuses,
2469    /// Where each priority lands on this board, or `None` when this instance holds none.
2470    priorities: Option<PriorityMapping>,
2471    client: Client,
2472    asset_client: Client,
2473    /// Every item this source has created in this command, in the order it created them —
2474    /// dropped by [`TaskSource::end_command`].
2475    ///
2476    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
2477    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
2478    /// a copy resolving a dependency on an item it had just created refused it as not
2479    /// found. A board read is completed from this — an item remembered here and absent from
2480    /// the read is added back, because the board really does hold it and only the read is
2481    /// behind.
2482    ///
2483    /// It is not a cache of a user's work: nothing is remembered that this process did not
2484    /// itself just write, it lives and dies with the process, and it is never consulted for
2485    /// an item this source did not create.
2486    created: Mutex<Vec<Resolved>>,
2487    /// Every item that already existed and that this source has written in this command, as
2488    /// it wrote it — dropped by [`TaskSource::end_command`].
2489    ///
2490    /// The other half of [`Self::created`], held on the same terms and for the reason a
2491    /// narrowed read needs it: an answer from GitHub's search or from the board's own field
2492    /// filter is an index behind a write this process made moments ago, so a query matching
2493    /// what this process just wrote onto an existing item would otherwise miss it. Nothing
2494    /// is remembered that this process did not itself just write.
2495    updated: Mutex<Vec<Resolved>>,
2496    /// Every issue this source has added a comment to or edited a comment of in this command
2497    /// — dropped by [`TaskSource::end_command`].
2498    ///
2499    /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
2500    /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
2501    /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
2502    /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
2503    /// from before — until the index caught up. Each id here is a candidate of every such
2504    /// search-narrowed read, and wherever it is a candidate its comments are read rather than
2505    /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
2506    /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
2507    /// process wrote is still found only once the index has it.
2508    commented: Mutex<Vec<NativeId>>,
2509    /// How fast this source writes, and how long it waits out a refusal.
2510    pacing: Pacing,
2511    /// When the last content-creating mutation finished, or the moment the furthest-out
2512    /// reserved slot releases the next one, whichever is later — so the one after it can be
2513    /// spaced from that. See [`MIN_MUTATION_INTERVAL_MS`] for the interval and
2514    /// [`GitHubProjectsSource::finish_mutation`] for why completion rather than release is
2515    /// what it is measured from.
2516    last_mutation: Mutex<Option<Duration>>,
2517    clock: SharedClock,
2518    numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
2519    /// The board as this process last read it, for the length of one command — dropped by
2520    /// [`TaskSource::end_command`].
2521    ///
2522    /// A copy of a project used to re-read the whole board, paged, before writing each of
2523    /// its items, which is by far the largest part of a copy's request count and none of
2524    /// its work. Nothing else changes this board while a command runs — this source's own
2525    /// writes are the only writer — so one read answers them all.
2526    ///
2527    /// It is not a store of a user's work and it is not the cache the no-persistence
2528    /// invariant forbids: it lives and dies with the process exactly as `created` does,
2529    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
2530    /// an item this command created and then depends on resolves whether or not GitHub's
2531    /// own eventually-consistent read has caught up. A write to an item already on the
2532    /// board updates the entry here too, so what this holds is the last read plus this
2533    /// process's own writes rather than a snapshot taken before them.
2534    board_cache: Mutex<Option<Board>>,
2535    /// Every issue this board's own search reported, for the length of one command — dropped
2536    /// by [`TaskSource::end_command`].
2537    ///
2538    /// The second half of a board read, and cached for the same reason and on the same
2539    /// terms as the first: it lives and dies with the process, nothing is written down, and
2540    /// a write this process makes updates the entry here exactly as it updates the one in
2541    /// [`Self::board_cache`]. One read answers every question a command asks, so a command
2542    /// that lists this board's projects and its tasks pays for one search rather than two.
2543    search_cache: Mutex<Option<Vec<Resolved>>>,
2544    /// What each narrowed question GitHub was asked answered, keyed by that question, for
2545    /// the length of one command — dropped by [`TaskSource::end_command`].
2546    ///
2547    /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
2548    /// and dies with the process, nothing is written down, a write this process makes
2549    /// updates the entry here as it updates the other two, and every answer is completed
2550    /// with this process's own writes each time it is given. A command that asks the same
2551    /// narrowed question twice — a wait polling for its own items, a listing repeated after a
2552    /// write — pays for it once, which is what the whole-board read it replaced gave it.
2553    narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
2554    search_next: Mutex<BTreeMap<String, Option<String>>>,
2555    /// Records already resolved in this command, reused by writes and for comment identity.
2556    /// Explicit item reads still reach GitHub. Nothing is persisted, and
2557    /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
2558    /// its item as a person has since left it.
2559    resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
2560    /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
2561    /// asked for under, with the project that selector named — for the length of one command,
2562    /// dropped by [`TaskSource::end_command`].
2563    ///
2564    /// A caller pages through a project's tasks one engine page at a time, and every page
2565    /// is cut from the whole list of them, so without this each page walked every
2566    /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
2567    /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
2568    /// dies with the process, nothing is written down, a write this process makes updates or
2569    /// removes the entry here as it does there, and every answer is completed with this
2570    /// process's own writes each time it is given.
2571    children_cache: Mutex<ProjectChildren>,
2572    /// The board's own id and field definitions as this process last read them on their
2573    /// own, for the length of one command — dropped by [`TaskSource::end_command`].
2574    ///
2575    /// What a write needs of the board and its item does not say, read once per command
2576    /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
2577    /// lives and dies with the process and nothing is written down. It holds no item and so
2578    /// can answer no question about one — see [`Self::board_fields`].
2579    fields_cache: Mutex<Option<BoardFields>>,
2580    /// Each destination repository's node id, resolved once per repository
2581    /// rather than per issue created.
2582    ///
2583    /// A repository's node id does not change, and re-reading it for every issue of a copy
2584    /// spent one request per item on an answer this source already had. It is a map rather
2585    /// than one entry because a copy files each item in the repository its own
2586    /// `repositories` field names, so a plan across five repositories asks GitHub five
2587    /// times and not once per item.
2588    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
2589    /// What every request this source sends is recorded into.
2590    ///
2591    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
2592    /// a request leaves this crate, so nothing has to be switched on for a session to be
2593    /// counted. It is shared rather than owned so a caller accounting for a whole session —
2594    /// its own schema verification, board lookups, residue sweep and cleanup beside this
2595    /// source's reads and writes — adds up one accounting instead of two. See
2596    /// [`accounting`] for what a record carries and what a session's spend is and is not.
2597    ledger: Arc<Accounting>,
2598}
2599
2600/// GitHub's closed single-select color vocabulary.
2601#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2602#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
2603pub enum StatusOptionColor {
2604    /// Gray.
2605    Gray,
2606    /// Blue.
2607    Blue,
2608    /// Green.
2609    Green,
2610    /// Yellow.
2611    Yellow,
2612    /// Purple.
2613    Purple,
2614    /// Red.
2615    Red,
2616    /// Orange.
2617    Orange,
2618    /// Pink.
2619    Pink,
2620}
2621
2622/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
2623/// applies its additions.
2624#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2625pub enum SetupMode {
2626    /// Read without mutation.
2627    Plan,
2628    /// Apply and verify.
2629    Apply,
2630}
2631
2632/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
2633/// against it goes on compiling.
2634pub type StatusOptionsMode = SetupMode;
2635
2636/// The explicit result of the requested operation.
2637#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2638#[serde(rename_all = "kebab-case")]
2639pub enum StatusOptionsOutcome {
2640    /// A read-only plan.
2641    Planned,
2642    /// Apply found nothing missing.
2643    Unchanged,
2644    /// Additions were applied and verified.
2645    Applied,
2646}
2647
2648/// A GitHub single-select option's opaque GraphQL node identifier.
2649#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2650#[serde(transparent)]
2651pub struct StatusOptionId(#[schemars(length(min = 1))] String);
2652
2653impl TryFrom<String> for StatusOptionId {
2654    type Error = String;
2655
2656    fn try_from(id: String) -> Result<Self, Self::Error> {
2657        if id.trim().is_empty() {
2658            return Err("a GitHub Status option id cannot be blank".to_owned());
2659        }
2660        Ok(Self(id))
2661    }
2662}
2663
2664/// One existing or proposed option in a guarded Status-field update.
2665#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2666pub struct StatusOption {
2667    /// GitHub's stable id.
2668    pub id: StatusOptionId,
2669    /// The visible option name.
2670    pub name: ColumnName,
2671    /// GitHub's single-select color token.
2672    pub color: StatusOptionColor,
2673    /// The option description, including an empty one.
2674    pub description: String,
2675}
2676
2677/// One board item's Status assignment, retained as recovery data.
2678#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2679pub struct StatusAssignment {
2680    /// The project item id whose assignment this is.
2681    // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
2682    // carried verbatim as operator recovery data; introducing a semantic type would claim
2683    // validation rules GitHub does not publish and no operation here interprets.
2684    pub item_id: String,
2685    /// The selected option, absent when the item has no status.
2686    #[serde(skip_serializing_if = "Option::is_none")]
2687    pub option: Option<AssignedStatusOption>,
2688}
2689
2690/// The inseparable id and name of an assigned option.
2691#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2692pub struct AssignedStatusOption {
2693    /// GitHub's stable id.
2694    pub id: StatusOptionId,
2695    /// The visible name.
2696    pub name: ColumnName,
2697}
2698
2699/// The plan and verified outcome of reconciling configured Status options.
2700#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2701pub struct StatusOptionsReport {
2702    /// The configured source name.
2703    pub source: SourceName,
2704    /// Configured option names absent before the operation.
2705    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
2706    // `ColumnName` and has therefore already passed its nonblank validation; retaining the
2707    // serialized string here preserves the report's intentionally simple public contract.
2708    pub missing: Vec<String>,
2709    /// What the requested operation did.
2710    pub outcome: StatusOptionsOutcome,
2711    /// The complete option list observed before any mutation.
2712    pub existing: Vec<StatusOption>,
2713}
2714
2715#[derive(Debug, Clone, PartialEq, Eq)]
2716struct StatusSnapshot {
2717    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2718    // passed back as the mutation's project identity; a newtype could enforce no stronger
2719    // invariant because GitHub publishes no grammar for it.
2720    board_id: String,
2721    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2722    // passed back as the mutation's field identity; a newtype could enforce no stronger
2723    // invariant because GitHub publishes no grammar for it.
2724    field_id: String,
2725    options: Vec<StatusOption>,
2726    assignments: Vec<StatusAssignment>,
2727}
2728
2729/// The name of the board field a status is held in.
2730const STATUS_FIELD: &str = "Status";
2731
2732/// Every item's value of each field `report` names, as it stood before the setup wrote
2733/// anything — what a person puts back when the setup is refused part way.
2734fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
2735    let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
2736        .fields
2737        .iter()
2738        .map(|field| (field.field.name(), before.assignments(field.field)))
2739        .collect();
2740    serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
2741        message: format!("cannot render the pre-write field recovery snapshot: {error}"),
2742    })
2743}
2744
2745/// One board field the guarded setup reads and writes — every one it reads, and the only
2746/// ones it writes.
2747#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
2748pub enum BoardField {
2749    /// The single-select `Status` field every instance's `status_mapping` resolves into.
2750    Status,
2751    /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
2752    Priority,
2753}
2754
2755impl BoardField {
2756    /// The field's name on the board.
2757    #[must_use]
2758    pub const fn name(self) -> &'static str {
2759        match self {
2760            Self::Status => STATUS_FIELD,
2761            Self::Priority => PRIORITY_FIELD,
2762        }
2763    }
2764
2765    /// The field a board calls `name`, or `None` for one this setup does not own.
2766    fn named(name: &str) -> Option<Self> {
2767        [Self::Status, Self::Priority]
2768            .into_iter()
2769            .find(|field| field.name() == name)
2770    }
2771}
2772
2773/// What the guarded setup did to one field.
2774#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2775#[serde(rename_all = "kebab-case")]
2776pub enum FieldOutcome {
2777    /// A read-only plan.
2778    Planned,
2779    /// Apply found the field there with every configured option.
2780    Unchanged,
2781    /// Missing options were added to the field that was there, and verified.
2782    Applied,
2783    /// The field was not there; it was created holding the configured options, and verified.
2784    Created,
2785}
2786
2787/// One field's plan, or its verified outcome.
2788#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2789pub struct FieldReport {
2790    /// Which field.
2791    pub field: BoardField,
2792    /// Whether the board had the field before the operation.
2793    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
2794    // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
2795    // "outcome", "existing"}` — so folding one into the other would change a published JSON
2796    // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
2797    // one constructor, and it derives `outcome` from `exists` in one match.
2798    pub exists: bool,
2799    /// Configured option names the field lacked before the operation — every one of them,
2800    /// in the order a new field lists them, when the field was not there at all.
2801    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
2802    // mapping name and has therefore already passed its nonblank validation; the serialized
2803    // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
2804    pub missing: Vec<String>,
2805    /// For the `Status` field, which item kind each missing name is configured for: one
2806    /// entry per kind `status_mapping` names a missing option for, task before project, each
2807    /// listing that kind's missing names in category order. A name both kinds use is in
2808    /// both. Empty — and left out of the JSON — when nothing is missing, and always for
2809    /// `Priority`, which only a task holds.
2810    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2811    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
2812    // both SDKs model an absent `kinds` as an empty list rather than as `null`.
2813    #[schemars(!skip_serializing_if)]
2814    pub kinds: Vec<KindMissing>,
2815    /// What the requested operation did.
2816    pub outcome: FieldOutcome,
2817    /// The field's complete option list observed before any mutation; empty when the field
2818    /// was not there.
2819    pub existing: Vec<StatusOption>,
2820}
2821
2822/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
2823#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2824pub struct KindMissing {
2825    /// The kind these names are configured for.
2826    pub kind: ItemKind,
2827    /// The names that kind maps a category to and the field lacked, in category order.
2828    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
2829    // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
2830    // intentionally simple public contract.
2831    pub missing: Vec<String>,
2832}
2833
2834/// The plan and verified outcome of setting up every field a source's configuration names.
2835#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2836pub struct FieldsReport {
2837    /// The configured source name.
2838    pub source: SourceName,
2839    /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
2840    // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
2841    // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
2842    // per field would change a published JSON shape. The states the list could hold and the
2843    // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
2844    // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
2845    pub fields: Vec<FieldReport>,
2846}
2847
2848/// Which options one field is configured with, in the order a new field would list them.
2849struct FieldPlan {
2850    field: BoardField,
2851    wanted: Vec<String>,
2852}
2853
2854/// One single-select field as the guarded setup snapshots it.
2855#[derive(Debug, Clone, PartialEq, Eq)]
2856struct SnapshotField {
2857    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2858    // passed back as the mutation's field identity; a newtype could enforce no stronger
2859    // invariant because GitHub publishes no grammar for it.
2860    field_id: String,
2861    options: Vec<StatusOption>,
2862}
2863
2864/// Every single-select field of a board and every item's value of each.
2865#[derive(Debug, Clone, PartialEq, Eq)]
2866struct BoardSnapshot {
2867    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2868    // passed back as the mutation's project identity; a newtype could enforce no stronger
2869    // invariant because GitHub publishes no grammar for it.
2870    board_id: String,
2871    fields: BTreeMap<BoardField, SnapshotField>,
2872    /// Each board item's id, and its value of each field this setup owns that it holds one of.
2873    items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
2874}
2875
2876impl BoardSnapshot {
2877    /// Every item's value of `field`, in board order — the recovery data a drift refusal
2878    /// carries.
2879    fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
2880        self.items
2881            .iter()
2882            .map(|(item_id, values)| StatusAssignment {
2883                item_id: item_id.clone(),
2884                option: values.get(&field).cloned(),
2885            })
2886            .collect()
2887    }
2888}
2889
2890impl GitHubProjectsSource {
2891    /// Report missing configured Status options and, when `apply` is true, add them with
2892    /// a whole-list mutation that preserves every existing id and verifies the result.
2893    ///
2894    /// # Errors
2895    ///
2896    /// Refuses a board without a single-select `Status` field. A post-write difference in
2897    /// any pre-existing option id or item assignment is refused with the complete pre-write
2898    /// assignment snapshot in the diagnostic for recovery.
2899    // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
2900    // successful mutation, both drift refusals, source selection, missing Status, casing,
2901    // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
2902    // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
2903    // responses from entering the defensive malformed-response branches below.
2904    pub async fn status_options(
2905        &self,
2906        mode: StatusOptionsMode,
2907    ) -> Result<StatusOptionsReport, SourceError> {
2908        let before = self.status_snapshot().await?;
2909        // A terminal category's option is as configured as an open one's: a terminal
2910        // write validates it before closing and refuses when the board lacks it. Both
2911        // kinds' names are options of the one field, so both are asked for.
2912        let missing = self
2913            .statuses
2914            .wanted()
2915            .into_iter()
2916            .filter(|wanted| {
2917                !before
2918                    .options
2919                    .iter()
2920                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2921            })
2922            .collect::<Vec<_>>();
2923        let report = StatusOptionsReport {
2924            source: self.name.clone(),
2925            missing: missing.clone(),
2926            outcome: match (mode, missing.is_empty()) {
2927                (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
2928                (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
2929                (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
2930            },
2931            existing: before.options.clone(),
2932        };
2933        if mode == StatusOptionsMode::Plan || missing.is_empty() {
2934            return Ok(report);
2935        }
2936        let mut options = before
2937            .options
2938            .iter()
2939            .map(|option| {
2940                json!({
2941                    "id": option.id, "name": option.name, "color": option.color,
2942                    "description": option.description,
2943                })
2944            })
2945            .collect::<Vec<_>>();
2946        options.extend(missing.iter().map(|name| {
2947            json!({
2948                "name": name, "color": "GRAY", "description": ""
2949            })
2950        }));
2951        self.graphql(
2952            graphql::STATUS_OPTIONS_UPDATE,
2953            json!({"input": {
2954                "projectId": before.board_id, "fieldId": before.field_id,
2955                "singleSelectOptions": options,
2956            }}),
2957        )
2958        .await?;
2959        let after = self.status_snapshot().await?;
2960        let options_preserved = before
2961            .options
2962            .iter()
2963            .all(|old| after.options.iter().any(|new| new == old));
2964        let additions_present = missing.iter().all(|wanted| {
2965            after
2966                .options
2967                .iter()
2968                .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2969        });
2970        if !options_preserved || !additions_present || after.assignments != before.assignments {
2971            let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
2972                SourceError::Malformed {
2973                    message: format!("cannot render pre-write Status recovery snapshot: {error}"),
2974                }
2975            })?;
2976            return Err(SourceError::Refused {
2977                message: format!(
2978                    "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}"
2979                ),
2980            });
2981        }
2982        Ok(report)
2983    }
2984
2985    /// A fresh snapshot of the Status field and every board item's assignment of it.
2986    ///
2987    /// # Errors
2988    ///
2989    /// Refuses a board without a single-select `Status` field, and one the token cannot see.
2990    async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
2991        // Status alone, as this operation has always read it: a `Priority` field is another
2992        // operation's, so nothing about it can refuse this one.
2993        let mut board = self.board_snapshot(&[BoardField::Status]).await?;
2994        let field = board
2995            .fields
2996            .remove(&BoardField::Status)
2997            .ok_or_else(|| self.no_status_field())?;
2998        Ok(StatusSnapshot {
2999            assignments: board.assignments(BoardField::Status),
3000            board_id: board.board_id,
3001            field_id: field.field_id,
3002            options: field.options,
3003        })
3004    }
3005
3006    /// The refusal a board with no `Status` field is answered with by the guarded setup.
3007    fn no_status_field(&self) -> SourceError {
3008        SourceError::Refused {
3009            message: format!("source {} board has no Status field", self.name),
3010        }
3011    }
3012
3013    // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
3014    // the real CLI loopback journey, including pagination. The individual malformed guards
3015    // are defensive validation of a schema-pinned third-party response, not separate user
3016    // journeys; drift and missing-field failures cover the operation's recovery behavior.
3017    /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
3018    /// every board item's value of each, walked to the end of the board's items. A field not
3019    /// in `owned` is read past whatever it holds.
3020    async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
3021        let mut after: Option<String> = None;
3022        let mut snapshot: Option<BoardSnapshot> = None;
3023        loop {
3024            let data = self
3025                .graphql(
3026                    graphql::STATUS_OPTIONS_SNAPSHOT,
3027                    json!({
3028                        "owner": self.owner, "number": self.project_number,
3029                        "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
3030                    }),
3031                )
3032                .await?;
3033            let board = data
3034                .pointer("/owner/projectV2")
3035                .filter(|board| board.is_object())
3036                .ok_or_else(|| SourceError::Refused {
3037                    message: format!(
3038                        "source {} has no accessible GitHub Projects board",
3039                        self.name
3040                    ),
3041                })?;
3042            if board
3043                .pointer("/fields/pageInfo/hasNextPage")
3044                .and_then(Value::as_bool)
3045                != Some(false)
3046            {
3047                return Err(SourceError::Malformed {
3048                    message:
3049                        "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
3050                            .into(),
3051                });
3052            }
3053            let mut fields = BTreeMap::new();
3054            // Only the fields this setup owns, by name: a node the single-select fragment did not
3055            // match carries no name, and a person's own single-select field — a `Size`, a
3056            // `Team` — is none of this setup's business, so nothing about it can refuse one. A
3057            // `Status` or `Priority` field without its options is malformed, not absent.
3058            // 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.
3059            for (owned, field) in board
3060                .pointer("/fields/nodes")
3061                .and_then(Value::as_array)
3062                .ok_or_else(|| SourceError::Malformed {
3063                    message: "GitHub project fields.nodes is not an array".into(),
3064                })?
3065                .iter()
3066                .filter_map(|field| {
3067                    let named = BoardField::named(field.get("name")?.as_str()?)?;
3068                    owned.contains(&named).then_some((named, field))
3069                })
3070            {
3071                let options = field
3072                    .get("options")
3073                    .and_then(Value::as_array)
3074                    .ok_or_else(|| SourceError::Malformed {
3075                        message: "GitHub single-select field options is not an array".into(),
3076                    })?
3077                    .iter()
3078                    .map(|option| {
3079                        Ok(StatusOption {
3080                            id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
3081                                .map_err(|message| SourceError::Malformed { message })?,
3082                            name: ColumnName::try_from(required_str(option, "name")?.to_owned())
3083                                .map_err(|message| SourceError::Malformed {
3084                                    message: format!(
3085                                        "GitHub single-select option name is invalid: {message}"
3086                                    ),
3087                                })?,
3088                            color: serde_json::from_value(
3089                                option.get("color").cloned().unwrap_or(Value::Null),
3090                            )
3091                            .map_err(|error| {
3092                                SourceError::Malformed {
3093                                    message: format!(
3094                                        "GitHub single-select option color is invalid: {error}"
3095                                    ),
3096                                }
3097                            })?,
3098                            description: optional_str(option, "description")?
3099                                .unwrap_or_default()
3100                                .to_owned(),
3101                        })
3102                    })
3103                    .collect::<Result<Vec<_>, SourceError>>()?;
3104                let snapshot = SnapshotField {
3105                    field_id: required_nonblank_str(field, "id")?.to_owned(),
3106                    options,
3107                };
3108                // A board's field names are unique, so a second one is an answer that cannot
3109                // say which field the setup would act on — refused rather than one chosen.
3110                if fields.insert(owned, snapshot).is_some() {
3111                    return Err(SourceError::Malformed {
3112                        message: format!(
3113                            "GitHub answered two {} fields for this board",
3114                            owned.name()
3115                        ),
3116                    });
3117                }
3118            }
3119            let board_id = required_nonblank_str(board, "id")?.to_owned();
3120            let current = snapshot.get_or_insert_with(|| BoardSnapshot {
3121                board_id,
3122                fields,
3123                items: Vec::new(),
3124            });
3125            let items = board
3126                .pointer("/items/nodes")
3127                .and_then(Value::as_array)
3128                .ok_or_else(|| SourceError::Malformed {
3129                    message: "GitHub project items.nodes is not an array".into(),
3130                })?;
3131            for item in items {
3132                let field_values =
3133                    item.get("fieldValues")
3134                        .ok_or_else(|| SourceError::Malformed {
3135                            message: "GitHub project item is missing fieldValues".into(),
3136                        })?;
3137                if field_values
3138                    .pointer("/pageInfo/hasNextPage")
3139                    .and_then(Value::as_bool)
3140                    != Some(false)
3141                {
3142                    return Err(SourceError::Malformed {
3143                        message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
3144                    });
3145                }
3146                let values = item
3147                    .pointer("/fieldValues/nodes")
3148                    .and_then(Value::as_array)
3149                    .ok_or_else(|| SourceError::Malformed {
3150                        message: "GitHub project item fieldValues.nodes is not an array".into(),
3151                    })?;
3152                let item_id = required_nonblank_str(item, "id")?;
3153                let mut assigned = BTreeMap::new();
3154                for value in values {
3155                    let Some(field) = value
3156                        .pointer("/field/name")
3157                        .and_then(Value::as_str)
3158                        .and_then(BoardField::named)
3159                        .filter(|field| owned.contains(field))
3160                    else {
3161                        continue;
3162                    };
3163                    let held = assigned.insert(
3164                        field,
3165                        AssignedStatusOption {
3166                            id: StatusOptionId::try_from(
3167                                required_str(value, "optionId")?.to_owned(),
3168                            )
3169                            .map_err(|message| SourceError::Malformed { message })?,
3170                            name: ColumnName::try_from(required_str(value, "name")?.to_owned())
3171                                .map_err(|message| SourceError::Malformed {
3172                                    message: format!(
3173                                        "GitHub assigned {} name is invalid: {message}",
3174                                        field.name()
3175                                    ),
3176                                })?,
3177                        },
3178                    );
3179                    // An item holds one value of a field, so a second one leaves no way to
3180                    // tell which it holds — and a verification or recovery built on either
3181                    // could restore the wrong one.
3182                    if held.is_some() {
3183                        return Err(SourceError::Malformed {
3184                            message: format!(
3185                                "GitHub answered two {} values for board item {item_id}",
3186                                field.name()
3187                            ),
3188                        });
3189                    }
3190                }
3191                current.items.push((item_id.to_owned(), assigned));
3192            }
3193            let page = board.get("items").ok_or_else(|| SourceError::Malformed {
3194                message: "GitHub project is missing items".into(),
3195            })?;
3196            let has_next = page
3197                .pointer("/pageInfo/hasNextPage")
3198                .and_then(Value::as_bool)
3199                .ok_or_else(|| SourceError::Malformed {
3200                    message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
3201                })?;
3202            if !has_next {
3203                break;
3204            }
3205            let next =
3206                required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
3207            validate_cursor_progress(after.as_deref(), next)?;
3208            after = Some(next.to_owned());
3209        }
3210        snapshot.ok_or_else(|| SourceError::Malformed {
3211            message: "GitHub returned no board field snapshot".into(),
3212        })
3213    }
3214    // llmlint: ignore-end[changed_behavior_has_e2e]
3215
3216    /// Report every board field this source's configuration names and, with
3217    /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
3218    /// the `Priority` field when the board has none.
3219    ///
3220    /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
3221    /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
3222    /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
3223    /// color and description: the whole option list goes back with every existing id, because
3224    /// a re-minted id clears every item's value.
3225    ///
3226    /// # Errors
3227    ///
3228    /// Refuses a board without a single-select `Status` field. After an apply the board is
3229    /// read again, and a pre-existing option or any item's value of either field that moved is
3230    /// refused with the complete pre-write assignments in the diagnostic, for recovery.
3231    // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
3232    // unchanged apply, a created field, an added option to each field, drift refusal, a board
3233    // with no Status field and a non-github-projects source through the compiled CLI against
3234    // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
3235    pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
3236        let owned: Vec<BoardField> = if self.priorities.is_some() {
3237            vec![BoardField::Status, BoardField::Priority]
3238        } else {
3239            vec![BoardField::Status]
3240        };
3241        let before = self.board_snapshot(&owned).await?;
3242        let mut plans = vec![FieldPlan {
3243            field: BoardField::Status,
3244            wanted: self.statuses.wanted(),
3245        }];
3246        if !before.fields.contains_key(&BoardField::Status) {
3247            return Err(self.no_status_field());
3248        }
3249        if let Some(mapping) = &self.priorities {
3250            plans.push(FieldPlan {
3251                field: BoardField::Priority,
3252                wanted: mapping.names().map(str::to_owned).collect(),
3253            });
3254        }
3255        // The snapshot reads single-select fields alone, so a field it did not find may still
3256        // be on the board under the name, of another type: creating one beside it would fail
3257        // part way, or leave two fields of one name. Asked of the board's own field list, and
3258        // only when a field is missing.
3259        if plans
3260            .iter()
3261            .any(|plan| !before.fields.contains_key(&plan.field))
3262        {
3263            let board = self.board_fields().await?;
3264            for plan in plans
3265                .iter()
3266                .filter(|plan| !before.fields.contains_key(&plan.field))
3267            {
3268                if let Some(field) = Board::field(&board.fields, plan.field.name())? {
3269                    return Err(SourceError::Refused {
3270                        message: format!(
3271                            "source {}'s board has a {} field that is not a single-select field \
3272                             (it is a {}), so it cannot hold this source's options; next: rename \
3273                             or remove that field, then run this again",
3274                            self.name,
3275                            plan.field.name(),
3276                            optional_str(field, "__typename")?.unwrap_or("field of another type")
3277                        ),
3278                    });
3279                }
3280            }
3281        }
3282        let mut reports = Vec::new();
3283        for plan in &plans {
3284            let held = before.fields.get(&plan.field);
3285            let existing = held.map(|field| field.options.clone()).unwrap_or_default();
3286            let mut missing: Vec<String> = Vec::new();
3287            for wanted in &plan.wanted {
3288                let present = existing
3289                    .iter()
3290                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3291                    || missing
3292                        .iter()
3293                        .any(|named| named.eq_ignore_ascii_case(wanted));
3294                if !present {
3295                    missing.push(wanted.clone());
3296                }
3297            }
3298            let kinds = match plan.field {
3299                BoardField::Status => self.statuses.missing_by_kind(&existing),
3300                BoardField::Priority => Vec::new(),
3301            };
3302            reports.push(FieldReport {
3303                field: plan.field,
3304                exists: held.is_some(),
3305                kinds,
3306                outcome: match (mode, held.is_some(), missing.is_empty()) {
3307                    (SetupMode::Plan, _, _) => FieldOutcome::Planned,
3308                    (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
3309                    (SetupMode::Apply, true, false) => FieldOutcome::Applied,
3310                    (SetupMode::Apply, false, _) => FieldOutcome::Created,
3311                },
3312                missing,
3313                existing,
3314            });
3315        }
3316        let report = FieldsReport {
3317            source: self.name.clone(),
3318            fields: reports,
3319        };
3320        let writes: Vec<&FieldReport> = report
3321            .fields
3322            .iter()
3323            .filter(|field| !field.missing.is_empty() || !field.exists)
3324            .collect();
3325        if mode == SetupMode::Plan || writes.is_empty() {
3326            return Ok(report);
3327        }
3328        let mut landed: Vec<&str> = Vec::new();
3329        for field in &writes {
3330            let added = field
3331                .missing
3332                .iter()
3333                .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
3334            let sent = match before.fields.get(&field.field) {
3335                Some(held) => {
3336                    let mut options = held
3337                        .options
3338                        .iter()
3339                        .map(|option| {
3340                            json!({
3341                                "id": option.id, "name": option.name, "color": option.color,
3342                                "description": option.description,
3343                            })
3344                        })
3345                        .collect::<Vec<_>>();
3346                    options.extend(added);
3347                    self.graphql(
3348                        graphql::STATUS_OPTIONS_UPDATE,
3349                        json!({"input": {
3350                            "projectId": before.board_id, "fieldId": held.field_id,
3351                            "singleSelectOptions": options,
3352                        }}),
3353                    )
3354                    .await
3355                }
3356                None => {
3357                    self.graphql(
3358                        graphql::CREATE_FIELD,
3359                        json!({"input": {
3360                            "projectId": before.board_id, "dataType": "SINGLE_SELECT",
3361                            "name": field.field.name(),
3362                            "singleSelectOptions": added.collect::<Vec<_>>(),
3363                        }}),
3364                    )
3365                    .await
3366                }
3367            };
3368            // A mutation that failed does not establish that GitHub left its field as it was,
3369            // so every failure from here on carries the recovery data a drift refusal does.
3370            match sent {
3371                Ok(_) => landed.push(field.field.name()),
3372                Err(error) => {
3373                    let changed = if landed.is_empty() {
3374                        String::new()
3375                    } else {
3376                        format!("changed the {} field and then ", landed.join(" and "))
3377                    };
3378                    return Err(SourceError::Refused {
3379                        message: format!(
3380                            "the guarded field setup {changed}failed on the {} field, which it may \
3381                             have changed part way: {error}; the pre-write item assignments \
3382                             are:\n{}",
3383                            field.field.name(),
3384                            recovery(&report, &before)?
3385                        ),
3386                    });
3387                }
3388            }
3389        }
3390        // The board has been written, so a verification read that fails leaves it unverified
3391        // rather than unchanged, and says what to put back.
3392        let after = match self.board_snapshot(&owned).await {
3393            Ok(after) => after,
3394            Err(error) => {
3395                return Err(SourceError::Refused {
3396                    message: format!(
3397                        "the guarded field setup changed the {} field and then could not read the \
3398                         board back to verify it: {error}; the pre-write item assignments are:\n{}",
3399                        landed.join(" and "),
3400                        recovery(&report, &before)?
3401                    ),
3402                });
3403            }
3404        };
3405        let mut moved = Vec::new();
3406        for field in &report.fields {
3407            let name = field.field.name();
3408            let now = after
3409                .fields
3410                .get(&field.field)
3411                .map(|held| held.options.as_slice())
3412                .unwrap_or_default();
3413            if !field.existing.iter().all(|old| now.contains(old)) {
3414                moved.push(format!(
3415                    "a pre-existing {name} option id, name, color or description"
3416                ));
3417            }
3418            if !field.missing.iter().all(|wanted| {
3419                now.iter()
3420                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3421            }) {
3422                moved.push(format!("an added {name} option"));
3423            }
3424            if after.assignments(field.field) != before.assignments(field.field) {
3425                moved.push(format!("an item's {name} value"));
3426            }
3427        }
3428        if !moved.is_empty() {
3429            return Err(SourceError::Refused {
3430                message: format!(
3431                    "GitHub changed {} after the guarded field setup; the pre-write item \
3432                     assignments are:\n{}",
3433                    moved.join(", "),
3434                    recovery(&report, &before)?
3435                ),
3436            });
3437        }
3438        Ok(report)
3439    }
3440
3441    /// Validate configuration and capture the named credential without exposing it.
3442    ///
3443    /// # Errors
3444    ///
3445    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
3446    /// [`SourceError::Auth`] when the named credential is missing or empty.
3447    pub fn new(
3448        name: &SourceName,
3449        config: GitHubProjectsConfig,
3450        secrets: &dyn SecretResolver,
3451    ) -> Result<Self, SourceError> {
3452        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
3453    }
3454
3455    /// The same, recording every request it sends into an accounting the caller holds too.
3456    ///
3457    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
3458    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
3459    /// up — passes the one it records those into, so the session total accounts for the
3460    /// whole session rather than for this source's share of it.
3461    ///
3462    /// # Errors
3463    ///
3464    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
3465    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
3466    pub fn recording_into(
3467        name: &SourceName,
3468        config: GitHubProjectsConfig,
3469        secrets: &dyn SecretResolver,
3470        ledger: Arc<Accounting>,
3471    ) -> Result<Self, SourceError> {
3472        if !valid_github_owner(&config.owner) {
3473            return Err(SourceError::Config {
3474                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
3475            });
3476        }
3477        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
3478            return Err(SourceError::Config {
3479                message: format!("project_number must be between 1 and {}", i32::MAX),
3480            });
3481        }
3482        if !valid_environment_name(&config.token_env) {
3483            return Err(SourceError::Config {
3484                message: "token_env must be a valid environment-variable name".into(),
3485            });
3486        }
3487        let repository = config
3488            .repository
3489            .as_deref()
3490            .map(RepositoryTarget::parse)
3491            .transpose()?;
3492        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
3493            message: format!("endpoint is not a valid URL: {e}"),
3494        })?;
3495        if endpoint.scheme() != "https"
3496            && !(endpoint.scheme() == "http"
3497                && endpoint
3498                    .host_str()
3499                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
3500        {
3501            return Err(SourceError::Config {
3502                message:
3503                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
3504                        .into(),
3505            });
3506        }
3507        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
3508            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),
3509        })?;
3510        Ok(Self {
3511            name: name.clone(),
3512            owner: config.owner,
3513            project_number: config.project_number,
3514            repository,
3515            asset_client: assets::client(&endpoint)?,
3516            endpoint,
3517            token,
3518            credential_name: config.token_env,
3519            statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
3520            priorities: config
3521                .priority_mapping
3522                .map(|mapping| PriorityMapping::resolve(mapping, name))
3523                .transpose()?,
3524            client: Client::builder()
3525                .user_agent("onetaskgraph")
3526                .build()
3527                .map_err(|e| SourceError::Config {
3528                    message: format!("cannot build HTTP client: {e}"),
3529                })?,
3530            created: Mutex::new(Vec::new()),
3531            updated: Mutex::new(Vec::new()),
3532            commented: Mutex::new(Vec::new()),
3533            pacing: Pacing::resolve(config.pacing, name)?,
3534            last_mutation: Mutex::new(None),
3535            clock: system_clock(),
3536            numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
3537            board_cache: Mutex::new(None),
3538            search_cache: Mutex::new(None),
3539            narrowed_cache: Mutex::new(BTreeMap::new()),
3540            resolved_cache: Mutex::new(BTreeMap::new()),
3541            children_cache: Mutex::new(BTreeMap::new()),
3542            search_next: Mutex::new(BTreeMap::new()),
3543            fields_cache: Mutex::new(None),
3544            repository_cache: Mutex::new(BTreeMap::new()),
3545            ledger,
3546        })
3547    }
3548
3549    /// A snapshot of every request this source has sent, and what each cost.
3550    ///
3551    /// A value to hold and compare rather than a borrow of the accounting itself, so two
3552    /// of them can sit side by side. When this source was built with
3553    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
3554    /// point of building it that way.
3555    #[must_use]
3556    pub fn accounting(&self) -> accounting::Session {
3557        self.ledger.snapshot()
3558    }
3559
3560    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
3561    /// rate limit rather than handing it straight back as an error.
3562    ///
3563    /// Retrying is safe for every document here, including the mutations, and the reason
3564    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
3565    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
3566    /// this replays has already taken effect. An outcome this source cannot know — the
3567    /// send failed, or the body could not be read, so the mutation may well have landed —
3568    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
3569    /// attempt. A duplicate write would come from replaying one of those, and none is
3570    /// replayed.
3571    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
3572        if is_mutation(query)
3573            && ![
3574                graphql::ADD_COMMENT,
3575                graphql::UPDATE_COMMENT,
3576                graphql::DELETE_COMMENT,
3577            ]
3578            .contains(&query)
3579        {
3580            let mut cache = self.resolved_cache()?;
3581            for argument in ["input", "second", "third", "clear"] {
3582                if let Some(input) = variables.get(argument) {
3583                    cache.retain(|id, item| {
3584                        !["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
3585                            input
3586                                .get(key)
3587                                .and_then(Value::as_str)
3588                                .is_some_and(|value| value == id.0 || value == item.item_id)
3589                        })
3590                    });
3591                }
3592            }
3593        }
3594        let doing = operation_description(query);
3595        let mut waited = Duration::ZERO;
3596        let mut waits = 0_u32;
3597        let mut backoff = self.pacing.retry_backoff;
3598        loop {
3599            if is_mutation(query) {
3600                let spacing = self.reserve_mutation_slot();
3601                if !spacing.is_zero() {
3602                    self.clock.sleep(spacing).await;
3603                }
3604            }
3605            let attempt = self.send_once(query, &variables).await;
3606            if is_mutation(query) {
3607                self.finish_mutation();
3608            }
3609            let limited = match attempt {
3610                Ok(data) => return Ok(data),
3611                Err(Attempt::Failed(error)) => return Err(error),
3612                Err(Attempt::Limited(limited)) => limited,
3613            };
3614            // GitHub really does send `retry-after: 0`, and retrying at once is the one
3615            // move that extends a secondary limit, so a hint below the schedule's own next
3616            // wait is raised to it.
3617            let wait = match limited.hint {
3618                Some(hint) => Duration::from_secs(hint).max(backoff),
3619                None => backoff,
3620            };
3621            let remaining = self.pacing.retry_budget.saturating_sub(waited);
3622            // A wait of nothing spends none of the budget, so it is exhaustion rather
3623            // than a retry. `Pacing::resolve` rules out every way of configuring one
3624            // except a budget of zero, where reporting the first refusal is the ask.
3625            if wait.is_zero() || wait > remaining {
3626                return Err(limited.exhausted(
3627                    doing,
3628                    waits,
3629                    waited,
3630                    wait,
3631                    self.pacing.retry_budget,
3632                ));
3633            }
3634            self.clock.sleep(wait).await;
3635            waited += wait;
3636            waits += 1;
3637            backoff = backoff.saturating_mul(2);
3638        }
3639    }
3640
3641    /// The next moment a content-creating mutation may leave this source, as a wait from
3642    /// now.
3643    ///
3644    /// The slot is reserved under the lock and the waiting happens outside it, so two
3645    /// callers take two slots rather than the same one — and no lock is held across an
3646    /// await.
3647    ///
3648    /// The moment it is spaced from is the previous mutation's *completion*, which
3649    /// [`Self::finish_mutation`] records. See that method for why the release moment on its
3650    /// own is the wrong thing to measure from.
3651    fn reserve_mutation_slot(&self) -> Duration {
3652        if self.pacing.min_mutation_interval.is_zero() {
3653            return Duration::ZERO;
3654        }
3655        // A poisoned lock here costs pacing, not correctness, and refusing the write over
3656        // it would turn an earlier failure into a second one for no gain.
3657        let mut last = self
3658            .last_mutation
3659            .lock()
3660            .unwrap_or_else(std::sync::PoisonError::into_inner);
3661        let now = self.clock.now();
3662        // `checked_add` rather than `+`: adding durations can panic on overflow, and
3663        // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
3664        let at = last.map_or(now, |previous| {
3665            previous
3666                .checked_add(self.pacing.min_mutation_interval)
3667                .map_or(now, |earliest| earliest.max(now))
3668        });
3669        *last = Some(at);
3670        at.saturating_sub(now)
3671    }
3672
3673    /// Record that a content-creating mutation has finished, so the next one is spaced
3674    /// from here rather than from the moment this one was released.
3675    ///
3676    /// This source can only choose when a request *departs*; the limiter counts when it
3677    /// *arrives*, and the two differ by whatever the request spent in transit. Spacing one
3678    /// departure from the last therefore hands the limiter a gap of the interval less that
3679    /// transit, so a source pacing at 750 ms can still be seen arriving faster — which is
3680    /// exactly how a copy paced well inside a board's threshold was refused by it on a
3681    /// slower machine while passing on a quick one.
3682    ///
3683    /// Spacing from completion removes the subtraction rather than budgeting for it. The
3684    /// previous request had already arrived before its response came back, so its arrival
3685    /// is no later than this moment, and the next mutation is released at least the
3686    /// interval after this moment and arrives no earlier than it is released: the gap the
3687    /// limiter measures is therefore at least the interval, whatever transit costs and on
3688    /// whatever platform. The price is that a mutation's own round trip no longer counts
3689    /// towards its spacing, which makes this source slightly slower than the configured
3690    /// rate rather than slightly faster — the safe side of a limit that punishes being
3691    /// wrong by refusing reads for the next fifty minutes.
3692    ///
3693    /// A failed attempt is recorded too: a request refused by the limiter still arrived,
3694    /// and one that never left costs only a wait nobody needed.
3695    fn finish_mutation(&self) {
3696        if self.pacing.min_mutation_interval.is_zero() {
3697            return;
3698        }
3699        // A poisoned lock here costs pacing, not correctness, exactly as in the reservation.
3700        let mut last = self
3701            .last_mutation
3702            .lock()
3703            .unwrap_or_else(std::sync::PoisonError::into_inner);
3704        let now = self.clock.now();
3705        // `max` rather than an assignment: a concurrent caller may already have reserved a
3706        // slot further out, and completing this request must never pull that slot back in.
3707        *last = Some(last.map_or(now, |reserved| reserved.max(now)));
3708    }
3709
3710    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
3711    /// failure that waiting cannot help — and recorded, whichever of the three it was.
3712    ///
3713    /// This is the one place a request leaves this crate, which is why the accounting is
3714    /// here rather than at each of the callers: a read path added later is counted without
3715    /// anybody remembering to count it, and
3716    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
3717    /// when one is not.
3718    async fn send_once(&self, query: &str, variables: &Value) -> Result<Value, Attempt> {
3719        let Attempted {
3720            result,
3721            limits,
3722            reported_cost,
3723        } = self.attempt(query, variables).await;
3724        // No `otherwise` name: every document this source sends is one of its own, and the
3725        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
3726        let sending = accounting::Request::graphql(query, variables, None, reported_cost);
3727        let outcome = match &result {
3728            Ok(_) => accounting::Outcome::Answered,
3729            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
3730            Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
3731        };
3732        self.ledger.record(sending.finished(outcome, limits));
3733        result
3734    }
3735
3736    /// The attempt itself, with what its response said about the rate limit alongside.
3737    ///
3738    /// The two are returned together rather than recorded here because every one of the
3739    /// early exits below is a different outcome, and a record written at each of them is a
3740    /// record one of them can be added without.
3741    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
3742        let mut limits = accounting::RateLimit::default();
3743        let mut reported_cost = None;
3744        let result = self
3745            .attempted(query, variables, &mut limits, &mut reported_cost)
3746            .await;
3747        Attempted {
3748            result,
3749            limits,
3750            reported_cost,
3751        }
3752    }
3753
3754    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
3755    async fn attempted(
3756        &self,
3757        query: &str,
3758        variables: &Value,
3759        limits: &mut accounting::RateLimit,
3760        reported_cost: &mut Option<u64>,
3761    ) -> Result<Value, Attempt> {
3762        let response = self
3763            .client
3764            .post(self.endpoint.clone())
3765            .bearer_auth(self.token.expose_secret())
3766            .json(&json!({"query": query, "variables": variables}))
3767            .send()
3768            .await
3769            .map_err(|e| {
3770                Attempt::Failed(SourceError::Unavailable {
3771                    message: format!("GitHub GraphQL request failed: {e}"),
3772                })
3773            })?;
3774        let status = response.status();
3775        let header = |name: &str| whole_seconds(response.headers().get(name));
3776        *limits = accounting::RateLimit::read(|name| {
3777            response
3778                .headers()
3779                .get(name)
3780                .and_then(|value| value.to_str().ok())
3781                .map(str::to_owned)
3782        });
3783        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
3784        // that are not text at all — is "not known to be exhausted". This never makes a
3785        // response a refusal on its own: it says which limiter a refusal is attributed to
3786        // and where its hint comes from, so a value this cannot read costs a hint rather
3787        // than an answer.
3788        let exhausted = response
3789            .headers()
3790            .get("x-ratelimit-remaining")
3791            .and_then(|value| value.to_str().ok())
3792            == Some("0");
3793        // `retry-after` is what GitHub asks for when it asks; when it does not and the
3794        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
3795        // which is the same question answered as an absolute time. Nothing else here is a
3796        // hint, and a schedule is what answers a refusal that carries none.
3797        let hint = header("retry-after").or_else(|| {
3798            exhausted
3799                .then(|| header("x-ratelimit-reset"))
3800                .flatten()
3801                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
3802        });
3803        // Read before it is parsed, because the evidence which tells a secondary rate
3804        // limit from a rejected credential is in the body of a response whose status says
3805        // only "forbidden" — and a non-success response was never parsed at all.
3806        let body = response.text().await.map_err(|e| {
3807            Attempt::Failed(SourceError::Unavailable {
3808                message: format!("GitHub GraphQL response could not be read: {e}"),
3809            })
3810        })?;
3811        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
3812            return Err(Attempt::Limited(Limited { limiter, hint }));
3813        }
3814        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
3815            return Err(Attempt::Failed(SourceError::Auth {
3816                message: format!(
3817                    "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"
3818                ),
3819            }));
3820        }
3821        if !status.is_success() {
3822            return Err(Attempt::Failed(SourceError::Unavailable {
3823                message: format!("GitHub GraphQL returned HTTP {status}"),
3824            }));
3825        }
3826        // GitHub reports what a call cost only when the document asked it to, and no
3827        // document this source sends does — so this is `None` here and carries the figure
3828        // for a caller whose own document selects `rateLimit { cost }`. What it must never
3829        // pick up is a `dryRun` probe's cost, which is some other document's.
3830        *reported_cost = serde_json::from_str::<Value>(&body)
3831            .ok()
3832            .as_ref()
3833            .and_then(|body| body.pointer("/data/rateLimit/cost"))
3834            .and_then(Value::as_u64);
3835        self.answer(&body).map_err(Attempt::Failed)
3836    }
3837
3838    /// What one successful HTTP response says, once its GraphQL errors are read.
3839    fn answer(&self, body: &str) -> Result<Value, SourceError> {
3840        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
3841            message: format!("GitHub returned invalid JSON: {e}"),
3842        })?;
3843        let errors = body
3844            .get("errors")
3845            .map(|value| {
3846                value.as_array().ok_or_else(|| SourceError::Malformed {
3847                    message: "GitHub response errors is not an array".into(),
3848                })
3849            })
3850            .transpose()?;
3851        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
3852            let messages = errors
3853                .iter()
3854                .filter_map(|e| e.get("message").and_then(Value::as_str))
3855                .collect::<Vec<_>>()
3856                .join("; ");
3857            let message = if messages.is_empty() {
3858                "GitHub returned GraphQL errors".into()
3859            } else {
3860                messages
3861            };
3862            let normalized = message.to_ascii_lowercase();
3863            if normalized.contains("resource not accessible") || normalized.contains("scope") {
3864                return Err(SourceError::Auth {
3865                    message: format!(
3866                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
3867                        self.credential_name
3868                    ),
3869                });
3870            }
3871            return Err(SourceError::Refused { message });
3872        }
3873        body.get("data")
3874            .filter(|data| data.is_object())
3875            .cloned()
3876            .ok_or_else(|| SourceError::Malformed {
3877                message: "GitHub response has no data object".into(),
3878            })
3879    }
3880
3881    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
3882    // GraphQL cannot independently page them inside the outer item page. This source page is
3883    // deliberately bounded at that published maximum; the live drift journey exercises it.
3884    async fn board_page(
3885        &self,
3886        items_after: Option<&str>,
3887        items_first: u32,
3888    ) -> Result<Value, SourceError> {
3889        let data = self
3890            .graphql(
3891                graphql::BOARD,
3892                json!({"owner":self.owner,"number":self.project_number,
3893                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
3894                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
3895            )
3896            .await?;
3897        data.pointer("/owner/projectV2")
3898            .filter(|v| !v.is_null())
3899            .cloned()
3900            .ok_or_else(|| SourceError::Refused {
3901                message: format!(
3902                    "GitHub project {}/{} was not found or is not visible to the token",
3903                    self.owner, self.project_number
3904                ),
3905            })
3906    }
3907
3908    /// The search that finds the issues of this board, narrowed by `also` when it is
3909    /// given.
3910    ///
3911    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
3912    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
3913    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
3914    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
3915    /// from a task by the `parent` field each issue carries rather than by the search.
3916    fn board_search(&self, also: Option<&str>) -> String {
3917        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
3918        match also {
3919            Some(also) => format!("{scope} {also}"),
3920            None => scope,
3921        }
3922    }
3923
3924    /// One issue this source reached directly, as the board item a read of the board would
3925    /// have produced — or `None` when this board does not hold it.
3926    ///
3927    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
3928    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
3929    /// item's own id, that item's field values, and the issue as its content. One resolver
3930    /// for both routes is what makes an issue read through a search, through its own node
3931    /// id, or through its project's sub-issues report the same title, the same status, the
3932    /// same labels and the same qualified id.
3933    ///
3934    /// An issue with no entry for *this* board is not this source's to report, which is
3935    /// what keeps an id naming some other repository's issue from being answered as an item
3936    /// of this board. That answer is given about an **exhausted** connection and never
3937    /// about an unread page: the entry is looked for on the page in hand, and only if that
3938    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
3939    /// rest of it.
3940    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
3941        if optional_str(issue, "__typename")? != Some("Issue") {
3942            return Ok(None);
3943        }
3944        let memberships = issue
3945            .get("projectItems")
3946            .ok_or_else(|| SourceError::Malformed {
3947                message: "GitHub issue is missing projectItems".into(),
3948            })?;
3949        let nodes = memberships
3950            .get("nodes")
3951            .and_then(Value::as_array)
3952            .ok_or_else(|| SourceError::Malformed {
3953                message: "GitHub issue projectItems.nodes is not an array".into(),
3954            })?;
3955        let held = match self.board_entry(nodes) {
3956            Some(held) => held.clone(),
3957            None => {
3958                let info = memberships
3959                    .get("pageInfo")
3960                    .ok_or_else(|| SourceError::Malformed {
3961                        message: "GitHub issue projectItems has no pageInfo".into(),
3962                    })?;
3963                // The page held no entry for this board. Whether that means the issue is
3964                // not on it is a question about the rest of the connection, and only a
3965                // connection with no rest answers it here.
3966                if !required_bool(info, "hasNextPage")? {
3967                    return Ok(None);
3968                }
3969                let cursor = required_str(info, "endCursor")?;
3970                validate_cursor_progress(None, cursor)?;
3971                let issue_id = required_str(issue, "id")?;
3972                match self.board_membership(issue_id, cursor).await? {
3973                    Some(held) => held,
3974                    None => return Ok(None),
3975                }
3976            }
3977        };
3978        let item = json!({
3979            "id": required_str(&held, "id")?,
3980            "project": held.get("project"),
3981            "fieldValues": held.get("fieldValues"),
3982            "content": issue,
3983        });
3984        self.resolve(&item)
3985    }
3986
3987    /// This board's own entry among one page of an issue's `Issue.projectItems`.
3988    ///
3989    /// One spelling of *which membership is this board's*, so the page a read carries and
3990    /// the pages [`Self::board_membership`] walks are searched by the same rule.
3991    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
3992        nodes.iter().find(|node| {
3993            node.pointer("/project/number").and_then(Value::as_u64)
3994                == Some(u64::from(self.project_number))
3995        })
3996    }
3997
3998    /// The rest of one issue's board memberships, from `after`, for this board's entry.
3999    ///
4000    /// The recovery read: a page of memberships that holds no entry for this board says
4001    /// nothing about the memberships past it, so the connection is walked to exhaustion
4002    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
4003    /// positive answer — the whole connection was read and no entry named this board —
4004    /// rather than a failure, and the walk is held to
4005    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
4006    /// with a cursor that does not advance is refused instead of spun on.
4007    async fn board_membership(
4008        &self,
4009        issue: &str,
4010        after: &str,
4011    ) -> Result<Option<Value>, SourceError> {
4012        let mut after = after.to_owned();
4013        loop {
4014            let data = self
4015                .graphql(
4016                    graphql::ISSUE_BOARD_ITEMS,
4017                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
4018                           "nestedFirst":NESTED_PAGE_SIZE}),
4019                )
4020                .await?;
4021            let Some(connection) = data
4022                .pointer("/node/projectItems")
4023                .filter(|value| !value.is_null())
4024            else {
4025                // The id resolved to nothing, or to something with no memberships to walk —
4026                // which is the same answer as a connection holding no entry for this board.
4027                return Ok(None);
4028            };
4029            let nodes = connection
4030                .get("nodes")
4031                .and_then(Value::as_array)
4032                .ok_or_else(|| SourceError::Malformed {
4033                    message: "GitHub issue projectItems.nodes is not an array".into(),
4034                })?;
4035            if let Some(held) = self.board_entry(nodes) {
4036                return Ok(Some(held.clone()));
4037            }
4038            let info = connection
4039                .get("pageInfo")
4040                .ok_or_else(|| SourceError::Malformed {
4041                    message: "GitHub issue projectItems has no pageInfo".into(),
4042                })?;
4043            let next = required_bool(info, "hasNextPage")?
4044                .then(|| required_str(info, "endCursor"))
4045                .transpose()?;
4046            match next {
4047                Some(next) => {
4048                    validate_cursor_progress(Some(&after), next)?;
4049                    after = next.to_owned();
4050                }
4051                None => return Ok(None),
4052            }
4053        }
4054    }
4055
4056    /// One page of a board-scoped issue search, and where the next page resumes.
4057    async fn search_page(
4058        &self,
4059        search: &str,
4060        first: u32,
4061        after: Option<&str>,
4062    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
4063        let data = self
4064            .graphql(
4065                graphql::SEARCH_ISSUES,
4066                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
4067                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
4068                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4069            )
4070            .await?;
4071        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
4072            message: "GitHub search response has no search connection".into(),
4073        })?;
4074        let mut found = Vec::new();
4075        for node in connection
4076            .get("nodes")
4077            .and_then(Value::as_array)
4078            .ok_or_else(|| SourceError::Malformed {
4079                message: "GitHub search nodes is not an array".into(),
4080            })?
4081        {
4082            if let Some(resolved) = self.resolve_issue(node).await? {
4083                found.push(resolved);
4084            }
4085        }
4086        let info = connection
4087            .get("pageInfo")
4088            .ok_or_else(|| SourceError::Malformed {
4089                message: "GitHub search connection has no pageInfo".into(),
4090            })?;
4091        let next = required_bool(info, "hasNextPage")?
4092            .then(|| required_str(info, "endCursor"))
4093            .transpose()?
4094            .map(str::to_owned);
4095        if let Some(next) = &next {
4096            validate_cursor_progress(after, next)?;
4097        }
4098        Ok((found, next))
4099    }
4100
4101    /// Every issue this board holds, completed with what this run wrote.
4102    ///
4103    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
4104    /// is an index and is eventually consistent, so an issue this run created seconds ago
4105    /// can be absent from it, and a project listed straight after being written would
4106    /// otherwise be missing from its own board. What is added back is only what this
4107    /// process itself wrote, out of [`Self::created`], which lives and dies with the
4108    /// process.
4109    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4110        let found = self.searched_issues().await?;
4111        self.completed_with_written(found, |_| true)
4112    }
4113
4114    /// Every issue this board's own search reports, walked to exhaustion, read once per
4115    /// source.
4116    ///
4117    /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
4118    /// needs it too and the two would otherwise walk the same search twice in one command.
4119    /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
4120    /// is.
4121    async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4122        let cached = self.search_cache()?.clone();
4123        if let Some(held) = cached {
4124            return Ok(held);
4125        }
4126        let mut after: Option<String> = None;
4127        let mut found = Vec::new();
4128        let search = self.board_search(None);
4129        loop {
4130            let (page, next) = self
4131                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
4132                .await?;
4133            found.extend(page);
4134            match next {
4135                Some(next) => after = Some(next),
4136                None => break,
4137            }
4138        }
4139        *self.search_cache()? = Some(found.clone());
4140        Ok(found)
4141    }
4142
4143    /// This process's own view of the board's issues, or the refusal a poisoned lock is.
4144    fn search_cache(
4145        &self,
4146    ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
4147        self.search_cache
4148            .lock()
4149            .map_err(|_| SourceError::Unavailable {
4150                message: "this source's view of the board's issues was left inconsistent by an \
4151                      earlier failure; next: run the command again"
4152                    .into(),
4153            })
4154    }
4155
4156    /// `found`, with everything this run wrote that `keep` accepts and the read did not
4157    /// report.
4158    ///
4159    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
4160    /// at all: the search index is behind, and a node read of an item filed moments ago can
4161    /// be too.
4162    fn completed_with_written(
4163        &self,
4164        mut found: Vec<Resolved>,
4165        keep: impl Fn(&Resolved) -> bool,
4166    ) -> Result<Vec<Resolved>, SourceError> {
4167        for own in self.created()?.iter().filter(|own| keep(own)) {
4168            if !found.iter().any(|item| item.id == own.id) {
4169                found.push(own.clone());
4170            }
4171        }
4172        Ok(found)
4173    }
4174
4175    /// What resolving one node id reached.
4176    ///
4177    /// Three answers rather than an `Option`, because a board *draft* is none of the other
4178    /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
4179    /// is completed by a read of the draft itself rather than reported as nothing.
4180    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
4181        let asked = self
4182            .graphql(
4183                graphql::ISSUE,
4184                json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
4185                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4186            )
4187            .await;
4188        let data = match asked {
4189            Ok(data) => data,
4190            // A string that is not a node id at all is not a failure to report: it is an id
4191            // this board does not hold, which is what every read of one already answers.
4192            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
4193            Err(error) => return Err(error),
4194        };
4195        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
4196            return Ok(Reached::Nothing);
4197        };
4198        if optional_str(node, "__typename")? == Some("DraftIssue") {
4199            return Ok(Reached::Draft);
4200        }
4201        Ok(match self.resolve_issue(node).await? {
4202            Some(item) => Reached::Held(Box::new(item)),
4203            None => Reached::Nothing,
4204        })
4205    }
4206
4207    /// One item of this board by its own id, whatever kind it is.
4208    ///
4209    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
4210    /// run wrote is read first, because a node read of an item created moments ago can
4211    /// still be behind the board field values written onto it — see [`Self::created`].
4212    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4213        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
4214            return Ok(Some(own.clone()));
4215        }
4216        match self.reach(id).await? {
4217            Reached::Held(item) => Ok(Some(*item)),
4218            Reached::Nothing => Ok(None),
4219            Reached::Draft => self.draft_by_id(id).await,
4220        }
4221    }
4222
4223    /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
4224    /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
4225    /// than one request per id.
4226    ///
4227    /// What this run wrote answers first, as it does there, and only the rest is read. One id
4228    /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
4229    /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
4230    /// id at a time, so that id is answered as not held and the others as themselves; a draft
4231    /// is completed by a read of the draft, exactly as there.
4232    async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
4233        let mut found: Vec<Option<Option<Resolved>>> = {
4234            let created = self.created()?;
4235            ids.iter()
4236                .map(|id| {
4237                    created
4238                        .iter()
4239                        .find(|own| own.id == *id)
4240                        .map(|own| Some(own.clone()))
4241                })
4242                .collect()
4243        };
4244        let unread: Vec<NativeId> = ids
4245            .iter()
4246            .zip(&found)
4247            .filter(|(_, found)| found.is_none())
4248            .map(|(id, _)| id.clone())
4249            .collect();
4250        let mut read = Vec::with_capacity(unread.len());
4251        if let [one] = unread.as_slice() {
4252            read.push(self.item_by_id(one).await?);
4253        } else {
4254            for batch in unread.chunks(DETAIL_BATCH) {
4255                let data = match self
4256                    .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
4257                    .await
4258                {
4259                    Ok(data) => data,
4260                    Err(error) if unresolvable_node(&error) => {
4261                        for id in batch {
4262                            read.push(self.item_by_id(id).await?);
4263                        }
4264                        continue;
4265                    }
4266                    Err(error) => return Err(error),
4267                };
4268                for (slot, id) in batch.iter().enumerate() {
4269                    let node =
4270                        data.get(format!("i{slot}"))
4271                            .ok_or_else(|| SourceError::Malformed {
4272                                message: format!(
4273                                    "GitHub answered a batch read with no item for {}",
4274                                    id.0
4275                                ),
4276                            })?;
4277                    read.push(if node.is_null() {
4278                        None
4279                    } else if optional_str(node, "__typename")? == Some("DraftIssue") {
4280                        self.draft_by_id(id).await?
4281                    } else {
4282                        if optional_str(node, "__typename")? == Some("Issue")
4283                            && required_str(node, "id")? != id.0
4284                        {
4285                            return Err(SourceError::Malformed {
4286                                message: format!(
4287                                    "GitHub answered the read of {} with issue {}",
4288                                    id.0,
4289                                    required_str(node, "id")?
4290                                ),
4291                            });
4292                        }
4293                        self.resolve_issue(node).await?
4294                    });
4295                }
4296            }
4297        }
4298        let mut read = read.into_iter();
4299        Ok(found
4300            .iter_mut()
4301            .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
4302            .collect())
4303    }
4304
4305    fn resolved_cache(
4306        &self,
4307    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
4308        self.resolved_cache
4309            .lock()
4310            .map_err(|_| SourceError::Unavailable {
4311                message: "resolved item records were left inconsistent; run the command again"
4312                    .into(),
4313            })
4314    }
4315
4316    /// Reuse a record this invocation already resolved. The mutation sender invalidates
4317    /// it before writing, so a partial failure cannot leave a pre-write binding behind.
4318    async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4319        let cached = self.resolved_cache()?.get(id).cloned();
4320        match cached {
4321            Some(item) => Ok(Some(item)),
4322            None => self.item_by_id(id).await,
4323        }
4324    }
4325
4326    /// One board draft by its own id, with the board item it sits in — or `None` when no
4327    /// item of this board is that draft's.
4328    ///
4329    /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
4330    /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
4331    /// links a draft to one board item, so the page this read carries is the whole of that
4332    /// connection, and a page that reports more than it holds is refused rather than read
4333    /// as an answer about memberships nobody read.
4334    async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4335        let data = self
4336            .graphql(
4337                graphql::DRAFT,
4338                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
4339                       "boardItems":BOARD_ITEMS_PAGE_SIZE}),
4340            )
4341            .await?;
4342        // Gone between the two reads is an answer — the draft is no longer there. Anything
4343        // else than the draft [`Self::reach`] was just told this id is, is not one.
4344        let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
4345            return Ok(None);
4346        };
4347        if optional_str(draft, "__typename")? != Some("DraftIssue") {
4348            return Err(SourceError::Malformed {
4349                message: format!(
4350                    "GitHub answered {} as a draft and then as something else",
4351                    id.0
4352                ),
4353            });
4354        }
4355        if required_str(draft, "id")? != id.0 {
4356            return Err(SourceError::Malformed {
4357                message: format!("GitHub answered a different draft for {}", id.0),
4358            });
4359        }
4360        let memberships = draft
4361            .get("projectV2Items")
4362            .ok_or_else(|| SourceError::Malformed {
4363                message: format!("GitHub draft {} is missing projectV2Items", id.0),
4364            })?;
4365        let nodes = memberships
4366            .get("nodes")
4367            .and_then(Value::as_array)
4368            .ok_or_else(|| SourceError::Malformed {
4369                message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
4370            })?;
4371        let info = memberships
4372            .get("pageInfo")
4373            .ok_or_else(|| SourceError::Malformed {
4374                message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
4375            })?;
4376        // Read whether or not this board's entry is on the page: a page claiming more than
4377        // the one item GitHub links a draft to is a malformed answer either way.
4378        if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
4379            return Err(SourceError::Malformed {
4380                message: format!(
4381                    "GitHub draft {} reports more board items than the one GitHub links a draft \
4382                     to",
4383                    id.0
4384                ),
4385            });
4386        }
4387        if let Some(node) = nodes.first()
4388            && node
4389                .pointer("/project/number")
4390                .and_then(Value::as_u64)
4391                .is_none()
4392        {
4393            return Err(SourceError::Malformed {
4394                message: format!(
4395                    "GitHub draft {} board item has no numeric project number",
4396                    id.0
4397                ),
4398            });
4399        }
4400        let Some(held) = self.board_entry(nodes) else {
4401            return Ok(None);
4402        };
4403        if required_str(
4404            held.get("project").ok_or_else(|| SourceError::Malformed {
4405                message: format!("GitHub draft {} board item has no project", id.0),
4406            })?,
4407            "id",
4408        )? != self.board_fields().await?.id.as_str()
4409        {
4410            return Ok(None);
4411        }
4412        let item = json!({
4413            "id": required_str(held, "id")?,
4414            "project": held.get("project"),
4415            "fieldValues": held.get("fieldValues"),
4416            "content": draft,
4417        });
4418        self.resolve(&item)
4419    }
4420
4421    /// The board's own id and field definitions, for a write whose item does not carry
4422    /// them — never its items.
4423    ///
4424    /// A board this command has already listed supplies them, since it read them beside its
4425    /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
4426    /// is consulted about which items the board holds: see the module documentation for
4427    /// why a question about one known item is answered by reading that item.
4428    async fn board_fields(&self) -> Result<BoardFields, SourceError> {
4429        if let Some(board) = self.board_cache()?.as_ref() {
4430            return Ok(BoardFields {
4431                id: BoardId::parse(&board.id)?,
4432                fields: board.fields.clone(),
4433            });
4434        }
4435        if let Some(held) = self.fields_cache()?.clone() {
4436            return Ok(held);
4437        }
4438        let data = self
4439            .graphql(
4440                graphql::BOARD_FIELDS,
4441                json!({"owner":self.owner,"number":self.project_number,
4442                       "nestedFirst":NESTED_PAGE_SIZE}),
4443            )
4444            .await?;
4445        self.fields_read(&data)
4446    }
4447
4448    /// The board's id and fields out of an answer carrying the `boardFields` root, held for
4449    /// the rest of this command.
4450    fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
4451        let board = data
4452            .pointer("/boardFields/projectV2")
4453            .filter(|value| !value.is_null())
4454            .ok_or_else(|| SourceError::Refused {
4455                message: format!(
4456                    "GitHub project {}/{} was not found or is not visible to the token",
4457                    self.owner, self.project_number
4458                ),
4459            })?;
4460        let read = BoardFields {
4461            id: BoardId::parse(required_str(board, "id")?)?,
4462            fields: board.get("fields").cloned().unwrap_or(Value::Null),
4463        };
4464        *self.fields_cache()? = Some(read.clone());
4465        Ok(read)
4466    }
4467
4468    /// Read what creating an issue in `repository` needs and this command has not read yet —
4469    /// the board's fields and the repository's node id — in one request when it needs both.
4470    ///
4471    /// When either is already known this sends nothing, and the other is read by its own
4472    /// document where it is asked for, so no create reads anything twice.
4473    async fn creation_context(
4474        &self,
4475        repository: &RepositoryTarget,
4476        incoming: &Incoming<'_>,
4477    ) -> Result<(), SourceError> {
4478        let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
4479        if fields_known || self.repository_cache()?.contains_key(repository) {
4480            return Ok(());
4481        }
4482        let data = self
4483            .graphql(
4484                graphql::CREATION_CONTEXT,
4485                json!({"owner":self.owner,"number":self.project_number,
4486                       "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
4487                       "repositoryName":repository.name}),
4488            )
4489            .await?;
4490        self.fields_read(&data)?;
4491        self.repository_read(&data, repository, incoming)?;
4492        Ok(())
4493    }
4494
4495    /// This process's own view of the board's fields, or the refusal a poisoned lock is.
4496    fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
4497        self.fields_cache
4498            .lock()
4499            .map_err(|_| SourceError::Unavailable {
4500                message: "this source's view of the board's fields was left inconsistent by an \
4501                      earlier failure; next: run the command again"
4502                    .into(),
4503            })
4504    }
4505
4506    /// What a write to `item` needs of the board, read off that item when it says enough and
4507    /// off [`Self::board_fields`] when it does not.
4508    ///
4509    /// A node read of an item names its board and carries the definition of every field it
4510    /// holds a value of — so an item naming its board, holding a value of the origin field,
4511    /// and, when the write carries a status, holding a `Status` value, needs no read of the
4512    /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
4513    /// of may still be on the board, and a view reading it as absent would refuse a write the
4514    /// board can take or skip a field write the board needs, so such an item — and a create,
4515    /// which has no item yet — takes the board's fields from their own read instead.
4516    async fn fields_for(
4517        &self,
4518        item: Option<&Resolved>,
4519        writes_status: bool,
4520        selects_priority: bool,
4521    ) -> Result<BoardFields, SourceError> {
4522        if let Some(board) = item.and_then(Resolved::carried_board) {
4523            return Ok(board);
4524        }
4525        if let Some(item) = item
4526            && let Some(board_id) = item.named_board()
4527            && item.defines(ORIGIN_FIELD)
4528            && (!writes_status || item.defines("Status"))
4529            && (!selects_priority || item.defines(PRIORITY_FIELD))
4530        {
4531            return Ok(BoardFields {
4532                id: board_id,
4533                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
4534            });
4535        }
4536        self.board_fields().await
4537    }
4538
4539    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
4540    /// when that id names nothing here with a sub-issue relationship to walk.
4541    ///
4542    /// `None` and an empty answer are different: `None` is *this is not an issue of this
4543    /// GitHub*, which is what sends a project selector on to be read as a name, and an
4544    /// empty vector is a project that holds nothing.
4545    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
4546        let mut after: Option<String> = None;
4547        let mut children = Vec::new();
4548        loop {
4549            let asked = self
4550                .graphql(
4551                    graphql::SUB_ISSUES,
4552                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
4553                           "nestedFirst":NESTED_PAGE_SIZE,
4554                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4555                )
4556                .await;
4557            let data = match asked {
4558                Ok(data) => data,
4559                // A string that is not a node id at all is not a failure to report: it is
4560                // the ordinary answer to a selector naming a project by its name.
4561                Err(error) if unresolvable_node(&error) => return Ok(None),
4562                Err(error) => return Err(error),
4563            };
4564            let Some(connection) = data
4565                .pointer("/node/subIssues")
4566                .filter(|value| !value.is_null())
4567            else {
4568                // No such node, or one with no sub-issue relationship — a board draft is
4569                // the one this board can really hold.
4570                return Ok(None);
4571            };
4572            for node in connection
4573                .get("nodes")
4574                .and_then(Value::as_array)
4575                .ok_or_else(|| SourceError::Malformed {
4576                    message: "GitHub subIssues.nodes is not an array".into(),
4577                })?
4578            {
4579                if let Some(resolved) = self.resolve_issue(node).await? {
4580                    children.push(resolved);
4581                }
4582            }
4583            let info = connection
4584                .get("pageInfo")
4585                .ok_or_else(|| SourceError::Malformed {
4586                    message: "GitHub subIssues connection has no pageInfo".into(),
4587                })?;
4588            let next = required_bool(info, "hasNextPage")?
4589                .then(|| required_str(info, "endCursor"))
4590                .transpose()?;
4591            match next {
4592                Some(next) => {
4593                    validate_cursor_progress(after.as_deref(), next)?;
4594                    after = Some(next.to_owned());
4595                }
4596                None => return Ok(Some(children)),
4597            }
4598        }
4599    }
4600
4601    /// Which issue of this board a project *name* is, or `None` when none is.
4602    ///
4603    /// One bounded query which filters on that name at the server, rather than a walk of
4604    /// every issue the board holds. The name is compared again here: the qualifier narrows
4605    /// what GitHub sends, and this source decides what it names.
4606    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
4607        let search = self.board_search(Some(&title_qualifier(name)));
4608        let mut after = None;
4609        loop {
4610            let (candidates, next) = self
4611                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
4612                .await?;
4613            if let Some(item) = candidates.into_iter().find(|item| {
4614                item.kind == BoardKind::Work(ItemKind::Project)
4615                    && item.title.eq_ignore_ascii_case(name)
4616            }) {
4617                return Ok(Some(item.id));
4618            }
4619            match next {
4620                Some(next) => after = Some(next),
4621                None => return Ok(None),
4622            }
4623        }
4624    }
4625
4626    /// Everything filed under one project of this board: the sub-issues of the issue that
4627    /// project is.
4628    ///
4629    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
4630    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
4631    /// gains projects, or as another project gains tasks.
4632    ///
4633    /// A qualified id names the issue and is asked for its sub-issues directly: one
4634    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
4635    /// read as a project *name*, which costs the one bounded search
4636    /// [`Self::project_by_name`] makes.
4637    ///
4638    /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
4639    /// — and each answer is still cut to the items whose parent is this project, so one this
4640    /// process has since filed elsewhere is not reported here, and completed with what this
4641    /// process filed under it.
4642    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
4643        let held = self.children_cache()?.get(selector).cloned();
4644        let (project, children) = match held {
4645            Some(held) => held,
4646            None => {
4647                let answered = match self.sub_issues(selector).await? {
4648                    Some(children) => (selector.clone(), children),
4649                    None => match self.project_by_name(&selector.0).await? {
4650                        Some(project) => {
4651                            let children = self.sub_issues(&project).await?.unwrap_or_default();
4652                            (project, children)
4653                        }
4654                        None => return Ok(Vec::new()),
4655                    },
4656                };
4657                self.children_cache()?
4658                    .insert(selector.clone(), answered.clone());
4659                answered
4660            }
4661        };
4662        let mut children: Vec<Resolved> = children
4663            .into_iter()
4664            .filter(|child| child.parent.as_ref() == Some(&project))
4665            .collect();
4666        for own in self.updated()?.iter() {
4667            if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
4668                children.push(own.clone());
4669            }
4670        }
4671        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
4672    }
4673
4674    /// Every issue of this board GitHub's issue search reports updated at or after `since`,
4675    /// completed with what this run wrote — the candidates a comment-activity read confirms.
4676    ///
4677    /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
4678    /// on the board whatever repository, and whatever owner, it lives in, so no repository or
4679    /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
4680    /// sufficient is a fact about GitHub rather than about this source: a comment written on an
4681    /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
4682    /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
4683    /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
4684    /// rather than silently narrowing a caller's answer.
4685    ///
4686    /// The instant is written to the second, rounded down, which can only widen what the
4687    /// search returns; confirmation against each candidate's own comments is what makes the
4688    /// answer exact. The search is an index that lags a write by a second or two — the module
4689    /// documentation records it — so a caller that asks again from its last instant should
4690    /// overlap the two by more than that.
4691    async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
4692        let found = self.searched(&updated_qualifier(since)).await?;
4693        self.completed_with_written(found, |_| true)
4694    }
4695
4696    /// Every issue of this board GitHub's issue search reports for the board-scoped search
4697    /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
4698    ///
4699    /// Uncompleted: what this process wrote is added by the caller, which knows whether its
4700    /// own record is the fresher of the two.
4701    async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
4702        let search = self.board_search(Some(also));
4703        let mut after: Option<String> = None;
4704        let mut found = Vec::new();
4705        loop {
4706            let (page, next) = self
4707                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
4708                .await?;
4709            found.extend(page);
4710            match next {
4711                Some(next) => after = Some(next),
4712                None => return Ok(found),
4713            }
4714        }
4715    }
4716
4717    /// A bounded task answer; the versioned cursor carries the connection position, how
4718    /// many rows of the page starting there were already handed out, and the own-write ids
4719    /// already observed, including across a new source instance.
4720    ///
4721    /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
4722    /// sliced from the pages it needs; why is the module documentation's paging contract.
4723    async fn search_tasks(
4724        &self,
4725        query: &TaskQuery,
4726        page: &PageRequest,
4727        also: &str,
4728    ) -> Result<Page<Task>, SourceError> {
4729        let mut position = match &page.cursor {
4730            None => SearchPosition::default(),
4731            Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
4732                .ok()
4733                .filter(|position| {
4734                    position.version == SEARCH_CURSOR_VERSION
4735                        && position.connection.valid_resume(position.offset)
4736                })
4737                .ok_or_else(|| SourceError::Config {
4738                    message: "page cursor is invalid".into(),
4739                })?,
4740        };
4741        let search = self.board_search(Some(also));
4742        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
4743        let own = self.with_own_writes(Vec::new())?;
4744        // An issue this process commented on is a candidate of a comment-activity read
4745        // whether or not the search has caught up with the comment; see `Self::commented`.
4746        let commented = match query.commented_since {
4747            Some(_) => self.commented()?.clone(),
4748            None => Vec::new(),
4749        };
4750        for id in own.iter().map(|item| &item.id).chain(&commented) {
4751            if !position.own.contains(id) {
4752                position.own.push(id.clone());
4753            }
4754        }
4755        let mut tasks = Vec::new();
4756        while !position.connection.exhausted() && tasks.len() < limit {
4757            let first = SEARCH_PAGE_SIZE;
4758            // Page size is part of the key: a short cached answer cannot answer a wider ask.
4759            let key =
4760                serde_json::to_string(&("page", &search, &position.connection.after(), first))
4761                    .expect("search page key is serializable");
4762            let cached = if query.commented_since.is_none() {
4763                self.narrowed_cache()?.get(&key).cloned()
4764            } else {
4765                None
4766            };
4767            let (found, next) = match cached {
4768                Some(found) => {
4769                    let next = self
4770                        .search_next
4771                        .lock()
4772                        .map_err(|_| SourceError::Unavailable {
4773                            message:
4774                                "search pagination was left inconsistent; run the command again"
4775                                    .into(),
4776                        })?
4777                        .get(&key)
4778                        .cloned()
4779                        .flatten();
4780                    (found, next)
4781                }
4782                None => {
4783                    let (found, next) = self
4784                        .search_page(&search, first, position.connection.after())
4785                        .await?;
4786                    if query.commented_since.is_none() {
4787                        self.search_next
4788                            .lock()
4789                            .map_err(|_| SourceError::Unavailable {
4790                                message:
4791                                    "search pagination was left inconsistent; run the command again"
4792                                        .into(),
4793                            })?
4794                            .insert(key.clone(), next.clone());
4795                        self.narrowed_cache()?.insert(key, found.clone());
4796                    }
4797                    (found, next)
4798                }
4799            };
4800            let rows = found.len();
4801            for mut item in found.into_iter().skip(position.offset) {
4802                if tasks.len() == limit {
4803                    break;
4804                }
4805                position.offset += 1;
4806                if position.own.contains(&item.id) {
4807                    if position.seen.contains(&item.id) {
4808                        continue;
4809                    }
4810                    position.seen.push(item.id.clone());
4811                    // The search's own copy of an issue this process only commented on is as
4812                    // good as a node read of it, since its comments are read either way.
4813                    let only_commented = commented.contains(&item.id)
4814                        && !own.iter().any(|written| written.id == item.id);
4815                    if !only_commented {
4816                        let updated_at = item.updated_at;
4817                        let Some(written) = self.search_written(&own, &item.id).await? else {
4818                            continue;
4819                        };
4820                        item = written;
4821                        item.updated_at = item.updated_at.max(updated_at);
4822                        self.resolved_cache()?.insert(item.id.clone(), item.clone());
4823                    }
4824                }
4825                if item.kind == BoardKind::Work(ItemKind::Task) {
4826                    let task = item.task()?;
4827                    if task_matches(&task, query, &query.project)
4828                        && self.commented_since(&item, query.commented_since).await?
4829                    {
4830                        tasks.push(task);
4831                    }
4832                }
4833            }
4834            if position.offset < rows {
4835                continue;
4836            }
4837            position.offset = 0;
4838            position.connection = match next {
4839                Some(after) => SearchConnection::Continuing {
4840                    after: Cursor(after),
4841                },
4842                None => SearchConnection::Exhausted {},
4843            };
4844        }
4845        if position.connection.exhausted() {
4846            for id in position.own.clone() {
4847                if position.seen.contains(&id) {
4848                    continue;
4849                }
4850                if tasks.len() == limit {
4851                    break;
4852                }
4853                position.seen.push(id.clone());
4854                let Some(item) = self.search_written(&own, &id).await? else {
4855                    continue;
4856                };
4857                if item.kind == BoardKind::Work(ItemKind::Task) {
4858                    let task = item.task()?;
4859                    if task_matches(&task, query, &query.project)
4860                        && self.commented_since(&item, query.commented_since).await?
4861                    {
4862                        tasks.push(task);
4863                    }
4864                }
4865            }
4866        }
4867        let more = !position.connection.exhausted()
4868            || position.own.iter().any(|id| !position.seen.contains(id));
4869        Ok(Page {
4870            items: tasks,
4871            next: more.then(|| {
4872                Cursor(serde_json::to_string(&position).expect("search position is serializable"))
4873            }),
4874        })
4875    }
4876
4877    /// A resumed process has the ids but no write records; resolve only a record the
4878    /// current page needs, by its uncached node read rather than the lagging search index.
4879    async fn search_written(
4880        &self,
4881        own: &[Resolved],
4882        id: &NativeId,
4883    ) -> Result<Option<Resolved>, SourceError> {
4884        match own.iter().find(|item| item.id == *id) {
4885            Some(item) => Ok(Some(item.clone())),
4886            None => self.item_by_id(id).await,
4887        }
4888    }
4889
4890    /// The candidates for a task query carrying a text, metadata or origin predicate, read
4891    /// without enumerating the board — or `None` for a query carrying none of the three, which
4892    /// keeps the reads it always had.
4893    ///
4894    /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
4895    /// because it names at most a handful of items. Text and metadata are answered by one
4896    /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
4897    /// further by `updated:>=` when the query also asks for comment activity, since both
4898    /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
4899    /// process afterwards by the same predicates [`task_matches`] applies to every read.
4900    ///
4901    /// Completed with what this process wrote, its own record winning over the index's copy
4902    /// of the same item: see [`Self::with_own_writes`].
4903    async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
4904        let asked = match (&query.origin, narrowing_qualifiers(query)) {
4905            (Some(origin), _) => Narrowing::Origin(origin.clone()),
4906            (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
4907                Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
4908                None => qualifiers,
4909            }),
4910            (None, None) => return Ok(None),
4911        };
4912        // A question about comment activity is asked afresh every time, as it always was: it
4913        // is the one a caller polls from one source while waiting for the index, and an
4914        // answer held from the first poll would be the answer to every later one.
4915        let key = query.commented_since.is_none().then(|| asked.key());
4916        let cached = match &key {
4917            Some(key) => self.narrowed_cache()?.get(key).cloned(),
4918            None => None,
4919        };
4920        let found = match cached {
4921            Some(found) => found,
4922            None => {
4923                let found = match &asked {
4924                    Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
4925                    Narrowing::Search(also) => self.searched(also).await?,
4926                };
4927                if let Some(key) = key {
4928                    self.narrowed_cache()?.insert(key, found.clone());
4929                }
4930                found
4931            }
4932        };
4933        self.with_own_writes(found).map(Some)
4934    }
4935
4936    /// The candidates for a project or unscoped document query carrying a searchable text,
4937    /// read without enumerating the board — or `None` for a query with no text or a blank one,
4938    /// which keeps the read it always had.
4939    ///
4940    /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
4941    /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
4942    /// costs is the issues that match and never the board. Its answer is held for the command
4943    /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
4944    /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
4945    /// substring rule, exactly as an item of the wider read was, and is completed with what this
4946    /// process wrote: see [`Self::with_own_writes`].
4947    async fn text_searched(
4948        &self,
4949        text: Option<&TextQuery>,
4950    ) -> Result<Option<Vec<Resolved>>, SourceError> {
4951        let Some(also) = text_qualifiers(text) else {
4952            return Ok(None);
4953        };
4954        let key = Narrowing::Search(also.clone()).key();
4955        let cached = self.narrowed_cache()?.get(&key).cloned();
4956        let found = match cached {
4957            Some(found) => found,
4958            None => {
4959                let found = self.searched(&also).await?;
4960                self.narrowed_cache()?.insert(key, found.clone());
4961                found
4962            }
4963        };
4964        self.with_own_writes(found).map(Some)
4965    }
4966
4967    /// Every item of this board that may carry `origin` — a superset of those that do — found
4968    /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
4969    ///
4970    /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
4971    /// which reads the field every carrier holds, whichever release wrote it — and the
4972    /// board-scoped issue search for the same id as a phrase in the body, where this source
4973    /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
4974    /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
4975    /// query's, exactly.
4976    ///
4977    /// Both connections are walked to exhaustion, each from its own cursor. One that has
4978    /// already ended is sent its last cursor again, which answers an empty page, so the one
4979    /// document serves every page of either. What the two leave is stated in the module
4980    /// documentation: a carrier another process added within the last second or two, before
4981    /// either index has it.
4982    async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
4983        let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
4984        let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
4985        let mut items_after: Option<String> = None;
4986        let mut search_after: Option<String> = None;
4987        let mut found: Vec<Resolved> = Vec::new();
4988        let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
4989            if !found.iter().any(|held| held.id == resolved.id) {
4990                found.push(resolved);
4991            }
4992        };
4993        loop {
4994            let data = self
4995                .graphql(
4996                    graphql::ORIGIN_LOOKUP,
4997                    json!({"owner":self.owner,"number":self.project_number,"filter":filter,
4998                           "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
4999                           "itemsAfter":items_after,"searchAfter":search_after,
5000                           "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
5001                           "duplicates":true}),
5002                )
5003                .await?;
5004            let items = data
5005                .pointer("/originItems/projectV2/items")
5006                .filter(|value| !value.is_null())
5007                .ok_or_else(|| SourceError::Refused {
5008                    message: format!(
5009                        "GitHub project {}/{} was not found or is not visible to the token",
5010                        self.owner, self.project_number
5011                    ),
5012                })?;
5013            for item in optional_nodes(Some(items), "project items")?
5014                .into_iter()
5015                .flatten()
5016            {
5017                // The board's own items list its drafts too, and a draft is not an issue: no
5018                // narrowed read answers with one, whatever its origin field holds.
5019                if let Some(resolved) = self.resolve(item)?
5020                    && resolved.content_kind == ContentKind::Issue
5021                {
5022                    keep(resolved, &mut found);
5023                }
5024            }
5025            let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
5026                message: "GitHub search response has no search connection".into(),
5027            })?;
5028            for node in optional_nodes(Some(searched), "search")?
5029                .into_iter()
5030                .flatten()
5031            {
5032                if let Some(resolved) = self.resolve_issue(node).await? {
5033                    keep(resolved, &mut found);
5034                }
5035            }
5036            let items_next = resumed(items, items_after.as_deref())?;
5037            let search_next = resumed(searched, search_after.as_deref())?;
5038            if !items_next.has_more() && !search_next.has_more() {
5039                return Ok(found);
5040            }
5041            items_after = items_next.cursor();
5042            search_after = search_next.cursor();
5043        }
5044    }
5045
5046    /// `found`, with every item this process created or wrote in its place, and every one of
5047    /// them the read did not report added.
5048    ///
5049    /// This process's own record wins over the read's copy of the same item, because a read
5050    /// of an item written moments ago can still be behind what was written onto it — the
5051    /// origin field included, which is the one a narrowed read is confirmed against — and a
5052    /// read that still names an item under a predicate this process's write moved it out of
5053    /// must not return it. The one thing the read knows that the record cannot is when GitHub
5054    /// last saw the item change, which is what a comment-activity read rules a candidate out
5055    /// by, so the read's `updatedAt` is kept when the record has none of its own. See
5056    /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
5057    fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
5058        // A board draft is not an issue, so no narrowed read returns one, and this process
5059        // having written one does not make it an answer either.
5060        let own: Vec<Resolved> = self
5061            .created()?
5062            .iter()
5063            .chain(self.updated()?.iter())
5064            .filter(|own| own.content_kind == ContentKind::Issue)
5065            .cloned()
5066            .collect();
5067        for mut own in own {
5068            self.resolved_cache()?.insert(own.id.clone(), own.clone());
5069            match found.iter_mut().find(|read| read.id == own.id) {
5070                Some(read) => {
5071                    own.updated_at = own.updated_at.max(read.updated_at);
5072                    *read = own;
5073                }
5074                None => found.push(own),
5075            }
5076        }
5077        Ok(found)
5078    }
5079
5080    /// Whether `item` has a comment created or last edited at or after `since` — always, when
5081    /// there is no instant to hold it to.
5082    ///
5083    /// The candidate's own `updatedAt` is read first, because a comment written or edited at
5084    /// or after the instant moved it there: an issue not updated since holds no such comment,
5085    /// and its comments are never asked for — unless this process commented on it in this
5086    /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
5087    /// only as far as the first that matches. A board draft is not an issue and has no
5088    /// comments, so it never matches.
5089    async fn commented_since(
5090        &self,
5091        item: &Resolved,
5092        since: Option<DateTime<Utc>>,
5093    ) -> Result<bool, SourceError> {
5094        let Some(since) = since else {
5095            return Ok(true);
5096        };
5097        if item.content_kind == ContentKind::DraftIssue {
5098            return Ok(false);
5099        }
5100        // An `updatedAt` this process's own record or a lagging index holds can predate a
5101        // comment this process wrote since, so only an issue it did not comment on is ruled
5102        // out by one.
5103        if item.updated_at.is_some_and(|updated| updated < since)
5104            && !self.commented()?.contains(&item.id)
5105        {
5106            return Ok(false);
5107        }
5108        let query = TaskQuery {
5109            commented_since: Some(since),
5110            ..TaskQuery::default()
5111        };
5112        let mut after: Option<String> = None;
5113        loop {
5114            let data = self
5115                .graphql(
5116                    graphql::ISSUE_COMMENTS,
5117                    json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
5118                )
5119                .await?;
5120            let Some(connection) = data
5121                .get("node")
5122                .filter(|value| !value.is_null())
5123                .and_then(|node| node.get("comments"))
5124                .filter(|value| !value.is_null())
5125            else {
5126                // Removed since the search reported it: no longer an issue with comments.
5127                return Ok(false);
5128            };
5129            let comments = optional_nodes(Some(connection), "issue comments")?
5130                .into_iter()
5131                .flatten()
5132                .map(comment_from)
5133                .collect::<Result<Vec<_>, _>>()?;
5134            if query.comments_match(&comments) {
5135                return Ok(true);
5136            }
5137            match next_cursor(connection)? {
5138                Some(next) => {
5139                    validate_cursor_progress(after.as_deref(), &next.0)?;
5140                    after = Some(next.0);
5141                }
5142                None => return Ok(false),
5143            }
5144        }
5145    }
5146
5147    /// Every item on the board: the union of both enumerations GitHub offers of one.
5148    ///
5149    /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
5150    /// board **draft** and reads the board's own fields beside its items, and only the search
5151    /// reports an item that connection is behind on. The module documentation is where the lag and the
5152    /// measurements behind it are written down.
5153    ///
5154    /// A search result is admitted on the same terms as any other issue this source reaches
5155    /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
5156    /// names *this* board — so an issue the index still believes is here after it was taken
5157    /// off is refused rather than reported.
5158    ///
5159    /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
5160    /// which is what the cache could otherwise have broken.
5161    async fn board(&self) -> Result<Board, SourceError> {
5162        let cached = self.board_cache()?.clone();
5163        let mut board = match cached {
5164            Some(board) => board,
5165            None => {
5166                let read = self.read_board().await?;
5167                *self.board_cache()? = Some(read.clone());
5168                read
5169            }
5170        };
5171        for held in self.searched_issues().await? {
5172            if !board.items.iter().any(|item| item.id == held.id) {
5173                board.items.push(held);
5174            }
5175        }
5176        for own in self.created()?.iter() {
5177            if !board.items.iter().any(|item| item.id == own.id) {
5178                board.items.push(own.clone());
5179            }
5180        }
5181        Ok(board)
5182    }
5183
5184    /// This process's own view of the board, or the refusal a poisoned lock is.
5185    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
5186        self.board_cache
5187            .lock()
5188            .map_err(|_| SourceError::Unavailable {
5189                message: "this source's view of the board was left inconsistent by an earlier \
5190                      failure; next: run the command again"
5191                    .into(),
5192            })
5193    }
5194
5195    /// Bring this process's own view of the board up to an item it has just written.
5196    ///
5197    /// A created item goes to `created`, which is what completes a board read GitHub's own
5198    /// eventual consistency has left behind. An item that was already there is replaced
5199    /// where it sits, so a second write of it in the same command reads its real parent
5200    /// rather than the one it had before the first write.
5201    ///
5202    /// "Where it sits" is three places, and missing an earlier one leaves a stale record
5203    /// that wins: an item this same run created is held in `created` and not in the cached
5204    /// board, and `board` completes the cached board *from* `created`, so replacing only
5205    /// the cached copy of such an item replaces nothing and the read still reports the
5206    /// title it was created with. The search is the third, and it is the one an item the
5207    /// board's own projection is behind on sits in *alone* — which is exactly the item this
5208    /// source is least able to re-read, so leaving it out would put the stale title back on
5209    /// the only items the completion in [`Self::board`] exists for.
5210    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
5211        self.resolved_cache()?.insert(item.id.clone(), item.clone());
5212        if created {
5213            self.created()?.push(item);
5214            return Ok(());
5215        }
5216        {
5217            let mut own = self.created()?;
5218            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
5219                *held = item;
5220                return Ok(());
5221            }
5222        }
5223        {
5224            let mut own = self.updated()?;
5225            match own.iter_mut().find(|held| held.id == item.id) {
5226                Some(held) => *held = item.clone(),
5227                None => own.push(item.clone()),
5228            }
5229        }
5230        if let Some(board) = self.board_cache()?.as_mut()
5231            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
5232        {
5233            *held = item.clone();
5234        }
5235        if let Some(found) = self.search_cache()?.as_mut()
5236            && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
5237        {
5238            *held = item.clone();
5239        }
5240        for found in self.narrowed_cache()?.values_mut() {
5241            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
5242                *held = item.clone();
5243            }
5244        }
5245        for (_, found) in self.children_cache()?.values_mut() {
5246            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
5247                *held = item.clone();
5248            }
5249        }
5250        Ok(())
5251    }
5252
5253    /// Forget one item this process has just deleted, from every half of its own view.
5254    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
5255        self.resolved_cache()?.remove(id);
5256        self.created()?.retain(|own| own.id != *id);
5257        self.updated()?.retain(|own| own.id != *id);
5258        self.commented()?.retain(|own| own != id);
5259        if let Some(board) = self.board_cache()?.as_mut() {
5260            board.items.retain(|item| item.id != *id);
5261        }
5262        if let Some(found) = self.search_cache()?.as_mut() {
5263            found.retain(|item| item.id != *id);
5264        }
5265        for found in self.narrowed_cache()?.values_mut() {
5266            found.retain(|item| item.id != *id);
5267        }
5268        for (_, found) in self.children_cache()?.values_mut() {
5269            found.retain(|item| item.id != *id);
5270        }
5271        Ok(())
5272    }
5273
5274    /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
5275    fn narrowed_cache(
5276        &self,
5277    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
5278        self.narrowed_cache
5279            .lock()
5280            .map_err(|_| SourceError::Unavailable {
5281                message: "this source's view of a narrowed read was left inconsistent by an \
5282                      earlier failure; next: run the command again"
5283                    .into(),
5284            })
5285    }
5286
5287    /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
5288    /// is.
5289    fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
5290        self.children_cache
5291            .lock()
5292            .map_err(|_| SourceError::Unavailable {
5293                message: "this source's view of a project's tasks was left inconsistent by an \
5294                      earlier failure; next: run the command again"
5295                    .into(),
5296            })
5297    }
5298
5299    /// Every page of the board, read from GitHub.
5300    async fn read_board(&self) -> Result<Board, SourceError> {
5301        let mut after: Option<String> = None;
5302        let mut items = Vec::new();
5303        let mut board;
5304        loop {
5305            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
5306            for item in page
5307                .pointer("/items/nodes")
5308                .and_then(Value::as_array)
5309                .ok_or_else(|| SourceError::Malformed {
5310                    message: "GitHub project items.nodes is not an array".into(),
5311                })?
5312            {
5313                if let Some(resolved) = self.resolve(item)? {
5314                    items.push(resolved);
5315                }
5316            }
5317            let info = page
5318                .pointer("/items/pageInfo")
5319                .ok_or_else(|| SourceError::Malformed {
5320                    message: "GitHub project items have no pageInfo".into(),
5321                })?;
5322            let has_next = required_bool(info, "hasNextPage")?;
5323            let next = has_next
5324                .then(|| required_str(info, "endCursor"))
5325                .transpose()?;
5326            board = page.clone();
5327            match next {
5328                Some(next) => {
5329                    validate_cursor_progress(after.as_deref(), next)?;
5330                    after = Some(next.to_owned());
5331                }
5332                None => break,
5333            }
5334        }
5335        Ok(Board {
5336            id: required_str(&board, "id")?.to_owned(),
5337            fields: board.get("fields").cloned().unwrap_or(Value::Null),
5338            items,
5339        })
5340    }
5341
5342    /// The existing items this source has written, for completing a narrowed read that is
5343    /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
5344    fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
5345        self.updated.lock().map_err(|_| SourceError::Unavailable {
5346            message: "this source's record of what it wrote in this run was left inconsistent \
5347                      by an earlier failure; next: run the command again"
5348                .into(),
5349        })
5350    }
5351
5352    /// The issues this source has commented on in this command; see
5353    /// [`Self::commented`](GitHubProjectsSource::commented).
5354    fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
5355        self.commented.lock().map_err(|_| SourceError::Unavailable {
5356            message: "this source's record of what it commented on in this run was left \
5357                      inconsistent by an earlier failure; next: run the command again"
5358                .into(),
5359        })
5360    }
5361
5362    /// Called only once GitHub has answered the comment write, so an issue whose comment
5363    /// failed is never made a candidate a later read would pay a node read for.
5364    fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
5365        let mut commented = self.commented()?;
5366        if !commented.contains(issue) {
5367            commented.push(issue.clone());
5368        }
5369        Ok(())
5370    }
5371
5372    /// The items this source has created, for completing a board read that is behind.
5373    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
5374        self.created.lock().map_err(|_| SourceError::Unavailable {
5375            message: "this source's record of what it created in this run was left \
5376                      inconsistent by an earlier failure; next: run the command again"
5377                .into(),
5378        })
5379    }
5380
5381    /// One board item as this source reports it, or `None` for content it ignores.
5382    ///
5383    /// A pull request is neither a project nor a task — it is somebody's change, not a
5384    /// unit of plan — and an item whose content the token cannot see has nothing to
5385    /// report at all.
5386    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
5387        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
5388            message: "GitHub project item is missing content".into(),
5389        })?;
5390        if content.is_null() {
5391            return Ok(None);
5392        }
5393        let content_kind = match required_str(content, "__typename")? {
5394            "Issue" => ContentKind::Issue,
5395            "DraftIssue" => ContentKind::DraftIssue,
5396            _ => return Ok(None),
5397        };
5398        let field_values = item
5399            .get("fieldValues")
5400            .ok_or_else(|| SourceError::Malformed {
5401                message: "GitHub project item is missing fieldValues".into(),
5402            })?;
5403        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
5404        let nodes = field_values
5405            .get("nodes")
5406            .and_then(Value::as_array)
5407            .ok_or_else(|| SourceError::Malformed {
5408                message: "GitHub project item fieldValues.nodes is not an array".into(),
5409            })?;
5410        if let Some(labels) = content.get("labels") {
5411            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
5412        }
5413        let raw_body = optional_str(content, "body")?.map(str::to_owned);
5414        let (body, slot) = metadata_body(raw_body.clone())?;
5415        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
5416            .map(|id| NativeId(id.to_owned()));
5417        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
5418        // to read one from; it is a task, and never a project.
5419        let sub_issues = match content_kind {
5420            ContentKind::Issue => sub_issue_total(content)?,
5421            ContentKind::DraftIssue => 0,
5422        };
5423        let content_id = required_str(content, "id")?;
5424        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
5425            message: format!("GitHub issue {content_id}: {message}"),
5426        })?;
5427        let raw_title = required_str(content, "title")?;
5428        // The design prefix is read *first*, before either of the two rules that separate
5429        // a project from a task. A document is not work whatever sub-issues it has and
5430        // whatever marker it carries, and reading the prefix later would make a design
5431        // issue with none of either an empty project.
5432        let kind = if raw_title.starts_with(DESIGN_TITLE_PREFIX) {
5433            BoardKind::Document
5434        } else if parent.is_some() {
5435            // Being a sub-issue wins outright, and no marker overrides it: an issue filed
5436            // under a project is that project's task even when it has sub-issues of its
5437            // own.
5438            BoardKind::Work(ItemKind::Task)
5439        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
5440            BoardKind::Work(ItemKind::Project)
5441        } else {
5442            BoardKind::Work(ItemKind::Task)
5443        };
5444        // The title a person wrote, which for a document is the one without the prefix —
5445        // the same way `content` above is the body without this source's metadata slot.
5446        let title = match kind {
5447            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
5448            BoardKind::Work(_) => raw_title.to_owned(),
5449        };
5450        let own_repository = content
5451            .pointer("/repository/nameWithOwner")
5452            .and_then(Value::as_str)
5453            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
5454            .transpose()
5455            .map_err(|message| SourceError::Malformed { message })?;
5456        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
5457            Repository::from_metadata(&slot)
5458                .map_err(|message| SourceError::Malformed { message })?
5459        } else {
5460            own_repository.clone().into_iter().collect()
5461        };
5462        let id = NativeId(content_id.to_owned());
5463        // Read only for a task, because only a task has either list: a project or a
5464        // document holding one of these keys holds nothing this source reports, and the
5465        // keys are left out of its caller-visible metadata all the same.
5466        let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
5467            let listed = |key: &str| {
5468                TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
5469                    .map_err(|message| SourceError::Malformed { message })
5470            };
5471            (
5472                listed(TaskRef::DELIVERS_KEY)?,
5473                listed(TaskRef::DELIVERED_BY_KEY)?,
5474            )
5475        } else {
5476            (Vec::new(), Vec::new())
5477        };
5478        let (option, closed, reason) = Self::status_parts(nodes, content)?;
5479        let priority = self.held_priority(nodes)?;
5480        // Present when the item was reached through its own issue, whose board entry
5481        // names the board; a read of the board's own items has the board already. An
5482        // empty id names nothing a field write could address, so it is read as absent and
5483        // the write goes back to reading the board.
5484        let board_id = item
5485            .pointer("/project/id")
5486            .and_then(Value::as_str)
5487            .filter(|id| !id.is_empty());
5488        let resolved = Resolved {
5489            item_id: required_str(item, "id")?.to_owned(),
5490            id,
5491            content_kind,
5492            kind,
5493            title,
5494            body: body.filter(|value| !value.is_empty()),
5495            raw_body,
5496            status: self
5497                .statuses
5498                .status(kind.status_kind(), option, closed, reason),
5499            option: option.map(str::to_owned),
5500            priority,
5501            closed,
5502            delivers,
5503            delivered_by,
5504            labels: labels(content)?,
5505            parent,
5506            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
5507            number: match content_kind {
5508                ContentKind::Issue => Some(issue_number(content)?),
5509                // A draft is filed in no repository, so nothing ever numbered it:
5510                // `DraftIssue` declares no `number` at all, exactly as it declares no
5511                // `subIssuesSummary` the branch above reads.
5512                ContentKind::DraftIssue => None,
5513            },
5514            url: optional_str(content, "url")?.map(str::to_owned),
5515            created_at: optional_time(content, "createdAt")?,
5516            updated_at: optional_time(content, "updatedAt")?,
5517            own_repository,
5518            repositories,
5519            slot,
5520            board_id: board_id.map(str::to_owned),
5521            fields: field_definitions(nodes),
5522            board_fields: Self::carried_board_fields(content, board_id)?,
5523            blocked_by: carried_blocked_by(content)?,
5524        };
5525        self.resolved_cache()?
5526            .insert(resolved.id.clone(), resolved.clone());
5527        Ok(Some(resolved))
5528    }
5529
5530    /// The field definitions of the board `board_id` names — the project this issue's own
5531    /// board item is on — off the `boards` page a read of an issue by its own id carries, or
5532    /// `None` when the read carried none, carried no entry for that board, or the board item
5533    /// named no board, which a write then answers by reading the board's fields itself.
5534    ///
5535    /// Matched by the board's node id and never by its number alone: a project number is
5536    /// unique only within its owner, so another owner's board numbered alike can sit on the
5537    /// same page, and its field and option ids address nothing on this one.
5538    fn carried_board_fields(
5539        content: &Value,
5540        board_id: Option<&str>,
5541    ) -> Result<Option<Value>, SourceError> {
5542        let (Some(nodes), Some(board_id)) = (
5543            content.pointer("/boards/nodes").and_then(Value::as_array),
5544            board_id,
5545        ) else {
5546            return Ok(None);
5547        };
5548        let Some(board) = nodes.iter().find_map(|node| {
5549            let project = node.get("project")?;
5550            (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
5551        }) else {
5552            return Ok(None);
5553        };
5554        let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
5555            return Ok(None);
5556        };
5557        complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
5558        Ok(Some(fields.clone()))
5559    }
5560
5561    /// What one board item's `Priority` field says, through this instance's mapping.
5562    ///
5563    /// An instance with no mapping holds no priority, so every item reads as `none` whatever
5564    /// its board holds. With one, no value is `none`, a mapped option is its level, and an
5565    /// option the mapping does not name is kept as itself — never read as a level or as
5566    /// `none` — for a read of the task to report by name.
5567    fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
5568        let Some(mapping) = &self.priorities else {
5569            return Ok(HeldPriority::Read(Priority::None));
5570        };
5571        // A value of the field that names no option — a text field someone called `Priority` —
5572        // is malformed rather than `none`: reading it as no priority would let the next copy
5573        // clear one a person set.
5574        let Some(option) = field_values
5575            .iter()
5576            .find(|value| {
5577                value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
5578            })
5579            .map(|value| required_str(value, "name"))
5580            .transpose()?
5581        else {
5582            return Ok(HeldPriority::Read(Priority::None));
5583        };
5584        Ok(mapping.priority_of(option).map_or_else(
5585            || HeldPriority::Unmapped(option.to_owned()),
5586            HeldPriority::Read,
5587        ))
5588    }
5589
5590    /// What one board item's status is read from: its `Status` option, whether its issue
5591    /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
5592    /// three into the status it reports.
5593    fn status_parts<'a>(
5594        field_values: &'a [Value],
5595        content: &'a Value,
5596    ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
5597        let option = field_values
5598            .iter()
5599            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
5600            .map(|value| required_str(value, "name"))
5601            .transpose()?;
5602        let closed = optional_str(content, "state")? == Some("CLOSED");
5603        Ok((option, closed, optional_str(content, "stateReason")?))
5604    }
5605
5606    /// The board Status option this write selects, or the refusal that says why not.
5607    ///
5608    /// The mapped option is required for both open and terminal targets. A terminal write
5609    /// validates it before changing either representation, so it can never fall back to
5610    /// closing an issue whose board cannot display the matching status.
5611    ///
5612    /// Answers the field's id, the option's id, and the option's name as the board spells
5613    /// it — which is the name a read of the item reports once it sits there.
5614    fn column_for(
5615        &self,
5616        fields: &Value,
5617        kind: ItemKind,
5618        category: StatusCategory,
5619        target: &StatusTarget,
5620    ) -> Result<Option<(String, String, String)>, SourceError> {
5621        let Some(wanted) = target.option() else {
5622            return Ok(None);
5623        };
5624        let missing = |detail: &str| SourceError::Refused {
5625            message: format!(
5626                "{} status {} of source {} needs the board Status option {wanted:?}, and \
5627                 {detail}; next: add that option to the board, which `onetaskgraph sources \
5628                 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
5629                 it has",
5630                kind.marker(),
5631                category_name(category),
5632                self.name,
5633                self.name,
5634                category_name(category),
5635                kind.marker()
5636            ),
5637        };
5638        let Some(field) = Board::field(fields, "Status")? else {
5639            return Err(missing("this board has no Status field"));
5640        };
5641        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
5642            return Err(missing(
5643                "this board's Status field is not a single-select field",
5644            ));
5645        }
5646        let option = field
5647            .get("options")
5648            .and_then(Value::as_array)
5649            .and_then(|options| {
5650                options.iter().find(|option| {
5651                    option
5652                        .get("name")
5653                        .and_then(Value::as_str)
5654                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
5655                })
5656            });
5657        match option {
5658            None => Err(missing("this board does not have it")),
5659            Some(option) => Ok(Some((
5660                required_str(field, "id")?.to_owned(),
5661                required_str(option, "id")?.to_owned(),
5662                required_str(option, "name")?.to_owned(),
5663            ))),
5664        }
5665    }
5666
5667    /// The refusal a status that closes an issue is answered with over a board draft.
5668    fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
5669        SourceError::Refused {
5670            message: format!(
5671                "status {} of source {} closes the item's issue, and GitHub draft items have \
5672                 no open or closed state",
5673                category_name(category),
5674                self.name
5675            ),
5676        }
5677    }
5678
5679    /// What a status write to one item needs of the board: the board's id and the
5680    /// definition of its `Status` field, read off the item when the item says both.
5681    ///
5682    /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
5683    /// and its `Status` value carries that field's definition, options and all. An item that
5684    /// does not say — no board id, or no `Status` value to read the field off — takes them
5685    /// from [`Self::board_fields`], which reads no item.
5686    async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
5687        if let Some(board) = item.carried_board() {
5688            return Ok(board);
5689        }
5690        if item.defines("Status")
5691            && let Some(board_id) = item.named_board()
5692        {
5693            return Ok(BoardFields {
5694                id: board_id,
5695                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
5696            });
5697        }
5698        self.board_fields().await
5699    }
5700
5701    /// Set one task's status and nothing else; see [`TaskSource::set_task_status`].
5702    async fn set_status(
5703        &self,
5704        id: &NativeId,
5705        category: StatusCategory,
5706    ) -> Result<Option<Status>, SourceError> {
5707        // Refused before anything is read, in the words a write of the same status is.
5708        let target = self.resolved_target(ItemKind::Task, category)?;
5709        let Some(mut item) = self
5710            .bound_item(id)
5711            .await?
5712            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
5713        else {
5714            return Ok(None);
5715        };
5716        let board = self.status_board(&item).await?;
5717        let (field, option, name) = self
5718            .column_for(&board.fields, ItemKind::Task, category, &target)?
5719            .ok_or_else(|| SourceError::Malformed {
5720                message: format!(
5721                    "status {} of source {} names no board Status option",
5722                    category_name(category),
5723                    self.name
5724                ),
5725            })?;
5726        if item.status.category == category && item.option.as_deref() == Some(&name) {
5727            return Ok(Some(item.status));
5728        }
5729        match &target {
5730            StatusTarget::Terminal(_, reason) => {
5731                if item.content_kind == ContentKind::DraftIssue {
5732                    return Err(self.closes_a_draft(category));
5733                }
5734                self.set_item_field(
5735                    board.id.as_str(),
5736                    &item.item_id,
5737                    &field,
5738                    json!({"singleSelectOptionId": option}),
5739                )
5740                .await?;
5741                self.update_content(
5742                    ContentKind::Issue,
5743                    &item.id,
5744                    json!({"stateInput": state_input(Some(&target))}),
5745                )
5746                .await?;
5747                item.closed = true;
5748                item.status =
5749                    self.statuses
5750                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()));
5751                item.option = Some(name);
5752            }
5753            StatusTarget::Column(_) => {
5754                // An option is what an open item's status is, so a closed issue is reopened
5755                // first — sitting closed in the column, it would read back as closed. A draft has
5756                // no state to reopen.
5757                if item.content_kind == ContentKind::Issue && item.closed {
5758                    self.update_content(
5759                        ContentKind::Issue,
5760                        &item.id,
5761                        json!({"stateInput": state_input(Some(&target))}),
5762                    )
5763                    .await?;
5764                    item.closed = false;
5765                }
5766                self.set_item_field(
5767                    board.id.as_str(),
5768                    &item.item_id,
5769                    &field,
5770                    json!({"singleSelectOptionId": option}),
5771                )
5772                .await?;
5773                item.status = self
5774                    .statuses
5775                    .status(ItemKind::Task, Some(&name), false, None);
5776                item.option = Some(name);
5777            }
5778            StatusTarget::Disabled(_) => {
5779                unreachable!("resolved_target refused a disabled status")
5780            }
5781        }
5782        let status = item.status.clone();
5783        self.remember_written(item, false)?;
5784        Ok(Some(status))
5785    }
5786
5787    /// Replace one task's `delivered_by` and nothing else; see
5788    /// [`TaskSource::set_delivered_by`].
5789    ///
5790    /// One update of the body, which differs from the body GitHub holds only inside the
5791    /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
5792    async fn replace_delivered_by(
5793        &self,
5794        id: &NativeId,
5795        delivered_by: &[TaskRef],
5796    ) -> Result<Option<()>, SourceError> {
5797        let entries = TaskRef::listed(
5798            TaskRef::DELIVERED_BY_KEY,
5799            id,
5800            Some(&self.name),
5801            delivered_by.to_vec(),
5802        )
5803        .map_err(|message| SourceError::Refused { message })?;
5804        let Some(mut item) = self
5805            .bound_item(id)
5806            .await?
5807            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
5808        else {
5809            return Ok(None);
5810        };
5811        let mut slot = item.slot.clone();
5812        set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
5813        self.write_slot(&mut item, &slot).await?;
5814        item.delivered_by = entries;
5815        self.remember_written(item, false)?;
5816        Ok(Some(()))
5817    }
5818
5819    /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
5820    /// see [`TaskSource::set_task_metadata`].
5821    ///
5822    /// `None` when this board holds no item by that id, or holds one of another kind. The
5823    /// answer is the item as this source now reads it, so what a caller is told the key
5824    /// holds is what the slot holds.
5825    ///
5826    /// A key already holding the value is answered without a write, compared as JSON rather
5827    /// than as the body's bytes: a slot a person spelled with other whitespace would
5828    /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
5829    async fn set_slot_key(
5830        &self,
5831        id: &NativeId,
5832        kind: BoardKind,
5833        key: &MetadataKey,
5834        value: &Value,
5835    ) -> Result<Option<Resolved>, SourceError> {
5836        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
5837            return Ok(None);
5838        };
5839        if item.slot.get(key.as_str()) == Some(value) {
5840            return Ok(Some(item));
5841        }
5842        let mut slot = item.slot.clone();
5843        slot.insert(key.as_str().to_owned(), value.clone());
5844        self.write_slot(&mut item, &slot).await?;
5845        self.remember_written(item.clone(), false)?;
5846        Ok(Some(item))
5847    }
5848
5849    /// Put `slot` in one item's metadata slot with a single update of its body, and bring
5850    /// `item` up to what that write left.
5851    ///
5852    /// The body sent differs from the body GitHub holds only inside the slot — see
5853    /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
5854    /// the mutation the item's content takes, so a board draft's body is written with
5855    /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
5856    async fn write_slot(
5857        &self,
5858        item: &mut Resolved,
5859        slot: &BTreeMap<String, Value>,
5860    ) -> Result<(), SourceError> {
5861        let held = item.raw_body.clone().unwrap_or_default();
5862        let body = with_slot(&held, slot)?;
5863        if body != held {
5864            self.update_content(item.content_kind, &item.id, json!({"body": body}))
5865                .await?;
5866        }
5867        let (visible, slot) = metadata_body(Some(body.clone()))?;
5868        item.body = visible.filter(|value| !value.is_empty());
5869        item.raw_body = Some(body);
5870        item.slot = slot;
5871        Ok(())
5872    }
5873
5874    /// This instance's target for a category written to an item of `kind`, refusing one
5875    /// that kind has no option for — before anything is read or written.
5876    ///
5877    /// Nothing here mutates the board's option set to make room for a status. GitHub
5878    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
5879    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
5880    /// field and every item's status.
5881    fn resolved_target(
5882        &self,
5883        kind: ItemKind,
5884        category: StatusCategory,
5885    ) -> Result<StatusTarget, SourceError> {
5886        let target = self.statuses.target(kind, category).clone();
5887        let StatusTarget::Disabled(why) = target else {
5888            return Ok(target);
5889        };
5890        let refusal = why.refusal(&self.name, category, kind);
5891        // Why there is no shipped default, which is the question a person meeting this
5892        // refusal on a source that never mentioned the category asks.
5893        let shipped_none = match category {
5894            StatusCategory::Draft => Some(
5895                "draft has no shipped default because GitHub draft issues cannot have \
5896                 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
5897            ),
5898            StatusCategory::Unknown => Some(
5899                "unknown has no shipped default because this board keeps no open-ended status \
5900                 word: every word classified unknown is written to the one board Status option \
5901                 status_mapping.unknown names",
5902            ),
5903            _ => None,
5904        };
5905        Err(match (refusal, shipped_none, why) {
5906            (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
5907                SourceError::Refused {
5908                    message: format!("{message}; {note}"),
5909                }
5910            }
5911            (refusal, _, _) => refusal,
5912        })
5913    }
5914
5915    /// What writing `priority` does to one item's `Priority` field on this board, or the
5916    /// refusal naming what the board lacks.
5917    ///
5918    /// `none` is no value, so it clears the field — and asks nothing of an item that holds
5919    /// none already, or of an item not created yet. Every other priority selects the option
5920    /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
5921    /// without that option, is refused rather than given one: reads and writes never create
5922    /// a field or an option.
5923    fn priority_write(
5924        &self,
5925        fields: &Value,
5926        existing: Option<&Resolved>,
5927        priority: Priority,
5928    ) -> Result<Option<PriorityWrite>, SourceError> {
5929        let Some(mapping) = &self.priorities else {
5930            return Err(self.holds_no_priority());
5931        };
5932        let Some(wanted) = mapping.option(priority) else {
5933            if !existing.is_some_and(Resolved::holds_priority) {
5934                return Ok(None);
5935            }
5936            let field =
5937                Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
5938                    message: format!(
5939                        "an item holding a {PRIORITY_FIELD} value was read without that field"
5940                    ),
5941                })?;
5942            return Ok(Some(PriorityWrite::Clear {
5943                field: required_str(field, "id")?.to_owned(),
5944            }));
5945        };
5946        let missing = |detail: &str| SourceError::Refused {
5947            message: format!(
5948                "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
5949                 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
5950                 it, or point priority_mapping.{priority} of this source at an option the board \
5951                 has",
5952                self.name, self.name
5953            ),
5954        };
5955        let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
5956            return Err(missing(&format!(
5957                "this board has no {PRIORITY_FIELD} field"
5958            )));
5959        };
5960        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
5961            return Err(missing(&format!(
5962                "this board's {PRIORITY_FIELD} field is not a single-select field"
5963            )));
5964        }
5965        // An options list that is absent or not a list is an answer this source cannot read,
5966        // not a board lacking the option: `sources fields --apply` is no remedy for it.
5967        let option = field
5968            .get("options")
5969            .and_then(Value::as_array)
5970            .ok_or_else(|| SourceError::Malformed {
5971                message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
5972            })?
5973            .iter()
5974            .find(|option| {
5975                option
5976                    .get("name")
5977                    .and_then(Value::as_str)
5978                    .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
5979            })
5980            .ok_or_else(|| missing("this board does not have it"))?;
5981        Ok(Some(PriorityWrite::Select {
5982            field: required_str(field, "id")?.to_owned(),
5983            option: required_str(option, "id")?.to_owned(),
5984        }))
5985    }
5986
5987    /// Apply one priority write to one board item.
5988    async fn write_priority(
5989        &self,
5990        board_id: &str,
5991        item_id: &str,
5992        write: &PriorityWrite,
5993    ) -> Result<(), SourceError> {
5994        match write {
5995            PriorityWrite::Select { field, option } => {
5996                self.set_item_field(
5997                    board_id,
5998                    item_id,
5999                    field,
6000                    json!({"singleSelectOptionId": option}),
6001                )
6002                .await
6003            }
6004            PriorityWrite::Clear { field } => {
6005                let data = self
6006                    .graphql(
6007                        graphql::CLEAR_FIELD,
6008                        json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
6009                            "readPriority":false,"priorityName":PRIORITY_FIELD}),
6010                    )
6011                    .await?;
6012                let returned = data
6013                    .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
6014                    .ok_or_else(|| SourceError::Malformed {
6015                        message: "GitHub field clear returned no project item".into(),
6016                    })?;
6017                if required_str(returned, "id")? != item_id {
6018                    return Err(SourceError::Malformed {
6019                        message: "GitHub field clear returned the wrong project item".into(),
6020                    });
6021                }
6022                Ok(())
6023            }
6024        }
6025    }
6026
6027    /// The refusal a priority is answered with by an instance configured with no
6028    /// `priority_mapping`, which holds none.
6029    fn holds_no_priority(&self) -> SourceError {
6030        SourceError::Refused {
6031            message: format!(
6032                "source {} holds no task priority: its configuration sets no priority_mapping; \
6033                 next: set priority_mapping on this source, then run `onetaskgraph sources \
6034                 fields {} --apply` to set its board up",
6035                self.name, self.name
6036            ),
6037        }
6038    }
6039
6040    /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
6041    ///
6042    /// One field write — a select, or a clear for `none` — and no title, body, label, state
6043    /// or `Status` request. Clearing a priority an item does not hold sends nothing.
6044    async fn set_priority(
6045        &self,
6046        id: &NativeId,
6047        priority: Priority,
6048    ) -> Result<Option<Priority>, SourceError> {
6049        if self.priorities.is_none() {
6050            return Err(self.holds_no_priority());
6051        }
6052        let Some(mut item) = self
6053            .bound_item(id)
6054            .await?
6055            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6056        else {
6057            return Ok(None);
6058        };
6059        if priority == Priority::None && !item.holds_priority() {
6060            return Ok(Some(priority));
6061        }
6062        // The item's own read carries the field's definition whenever it holds a value of
6063        // it, which a clear always does; a select onto an item holding none reads the board.
6064        let board = match (item.carried_board(), item.named_board()) {
6065            (Some(board), _) => board,
6066            (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
6067                id,
6068                fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
6069            },
6070            _ => self.board_fields().await?,
6071        };
6072        let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
6073            return Ok(Some(priority));
6074        };
6075        let (document, root, input) = match write {
6076            PriorityWrite::Select { field, option } => (
6077                graphql::UPDATE_FIELD,
6078                "updateProjectV2ItemFieldValue",
6079                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
6080            ),
6081            PriorityWrite::Clear { field } => (
6082                graphql::CLEAR_FIELD,
6083                "clearProjectV2ItemFieldValue",
6084                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
6085            ),
6086        };
6087        let data = self
6088            .graphql(
6089                document,
6090                json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
6091            )
6092            .await?;
6093        let returned = data
6094            .get(root)
6095            .and_then(|value| value.get("projectV2Item"))
6096            .ok_or_else(|| SourceError::Malformed {
6097                message: "GitHub priority write returned no project item".into(),
6098            })?;
6099        if required_str(returned, "id")? != item.item_id {
6100            return Err(SourceError::Malformed {
6101                message: "GitHub priority write returned the wrong project item".into(),
6102            });
6103        }
6104        let value = returned
6105            .get("fieldValueByName")
6106            .ok_or_else(|| SourceError::Malformed {
6107                message: "GitHub priority write returned no priority read-back".into(),
6108            })?;
6109        if !value.is_null()
6110            && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
6111        {
6112            return Err(SourceError::Malformed {
6113                message: "GitHub priority read-back is not a Priority field value".into(),
6114            });
6115        }
6116        let values = if value.is_null() {
6117            Vec::new()
6118        } else {
6119            vec![value.clone()]
6120        };
6121        item.priority = self.held_priority(&values)?;
6122        let answer = item.task()?.priority;
6123        self.remember_written(item, false)?;
6124        Ok(Some(answer))
6125    }
6126
6127    /// Replace one task's visible body and nothing else; see
6128    /// [`TaskSource::set_task_content`].
6129    ///
6130    /// One update of the body, which differs from the body GitHub holds only outside the
6131    /// metadata slot — the slot is kept byte for byte, so every caller key and every list
6132    /// this source keeps there reads back as it was. A body that would not change is not
6133    /// sent at all.
6134    async fn replace_content(
6135        &self,
6136        id: &NativeId,
6137        content: &str,
6138    ) -> Result<Option<()>, SourceError> {
6139        let Some(mut item) = self
6140            .bound_item(id)
6141            .await?
6142            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6143        else {
6144            return Ok(None);
6145        };
6146        let held = item.raw_body.clone().unwrap_or_default();
6147        let body = with_content(&held, content)?;
6148        // Checked before anything is sent: content ending in what this source reads as its own
6149        // metadata slot would read back as metadata rather than as the content it was.
6150        let (visible, slot) = metadata_body(Some(body.clone()))?;
6151        if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
6152            return Err(SourceError::Refused {
6153                message: format!(
6154                    "this content ends in what source {} reads as its own metadata slot \
6155                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
6156                     as content; next: remove that trailing block from the content",
6157                    self.name
6158                ),
6159            });
6160        }
6161        if body != held {
6162            self.update_content(item.content_kind, &item.id, json!({"body": body}))
6163                .await?;
6164        }
6165        item.body = visible.filter(|value| !value.is_empty());
6166        item.raw_body = Some(body);
6167        item.slot = slot;
6168        self.remember_written(item, false)?;
6169        Ok(Some(()))
6170    }
6171
6172    /// Apply one targeted update to one task; see [`TaskSource::update_task`].
6173    ///
6174    /// One read of the item — which carries the board's field definitions and the issue's
6175    /// `blockedBy`, so neither is read again — and then only what differs from it: the
6176    /// `Status` option and the `Priority` field together in one request, the `blockedBy`
6177    /// additions and removals the named edges differ by, and last one `updateIssue` carrying
6178    /// the title, the body — visible content and metadata slot together — and a state change.
6179    /// So an update naming any of title, body, metadata, status and priority is one read and
6180    /// at most two writes. The body goes last so that a write refused part-way leaves it, and
6181    /// the metadata in it, as it stood. A terminal status selects its option and then closes,
6182    /// as a whole write does; an open one selects its option and then reopens. The origin
6183    /// field is never written: an update is of an item that already exists, whose origin is
6184    /// what it is.
6185    ///
6186    /// The task answered is the item as those writes left it, built from the read and what was
6187    /// sent rather than read again — the same record a later read in this run answers from.
6188    async fn targeted_update(
6189        &self,
6190        id: &NativeId,
6191        update: &TaskUpdate,
6192    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
6193        // Everything this source can refuse without reading the item is refused first, in the
6194        // words a whole write of the same fields is refused with.
6195        update.consistent()?;
6196        if update
6197            .title
6198            .as_deref()
6199            .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
6200        {
6201            return Err(SourceError::Refused {
6202                message: format!(
6203                    "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
6204                     spells a document, so it would read back as one rather than as a task; \
6205                     retitle it",
6206                    self.name
6207                ),
6208            });
6209        }
6210        if let Some(delivers) = &update.delivers {
6211            TaskRef::listed(
6212                TaskRef::DELIVERS_KEY,
6213                id,
6214                Some(&self.name),
6215                delivers.clone(),
6216            )
6217            .map_err(|message| SourceError::Refused { message })?;
6218        }
6219        if self.priorities.is_none()
6220            && update
6221                .priority
6222                .is_some_and(|priority| priority != Priority::None)
6223        {
6224            return Err(self.holds_no_priority());
6225        }
6226        let target = update
6227            .status
6228            .as_ref()
6229            .map(|status| self.resolved_target(ItemKind::Task, status.category))
6230            .transpose()?;
6231        let Some(mut item) = self
6232            .bound_item(id)
6233            .await?
6234            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6235        else {
6236            return Ok(None);
6237        };
6238        let before = item.task()?;
6239
6240        let mut status_move = None;
6241        if let (Some(status), Some(target)) = (&update.status, target) {
6242            let board = self.status_board(&item).await?;
6243            let (field, option, name) = self
6244                .column_for(&board.fields, ItemKind::Task, status.category, &target)?
6245                .ok_or_else(|| SourceError::Malformed {
6246                    message: format!(
6247                        "status {} of source {} names no board Status option",
6248                        category_name(status.category),
6249                        self.name
6250                    ),
6251                })?;
6252            let terminal = matches!(target, StatusTarget::Terminal(_, _));
6253            if terminal && item.content_kind == ContentKind::DraftIssue {
6254                return Err(self.closes_a_draft(status.category));
6255            }
6256            let landed = match &target {
6257                StatusTarget::Terminal(_, reason) => {
6258                    self.statuses
6259                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
6260                }
6261                _ => self
6262                    .statuses
6263                    .status(ItemKind::Task, Some(&name), false, None),
6264            };
6265            let option_moves = item
6266                .option
6267                .as_deref()
6268                .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
6269            let state_moves = item.content_kind == ContentKind::Issue
6270                && (item.closed != terminal || (terminal && item.status != landed));
6271            if let Some(moves) = Moves::of(option_moves, state_moves) {
6272                status_move = Some(StatusMove {
6273                    board: board.id,
6274                    field,
6275                    option,
6276                    name,
6277                    target,
6278                    landed,
6279                    moves,
6280                });
6281            }
6282        }
6283
6284        let mut priority_move = None;
6285        if let Some(priority) = update.priority
6286            && self.priorities.is_some()
6287            && item.priority != HeldPriority::Read(priority)
6288        {
6289            let board = match (item.carried_board(), item.named_board()) {
6290                (Some(board), _) => board,
6291                (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
6292                    id: board,
6293                    fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6294                },
6295                _ => self.board_fields().await?,
6296            };
6297            if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
6298                priority_move = Some((board.id, write, priority));
6299            }
6300        }
6301
6302        // Resolved before the body is composed, because a far end `blockedBy` cannot name is
6303        // recorded in the slot, and the slot travels in the one body update below.
6304        let edges = match &update.depends_on {
6305            Some(edges) => Some(
6306                self.partition_edges(
6307                    BoardKind::Work(ItemKind::Task),
6308                    item.content_kind,
6309                    item.blocked_by.as_deref(),
6310                    edges,
6311                )
6312                .await?,
6313            ),
6314            None => None,
6315        };
6316
6317        let mut slot = item.slot.clone();
6318        for (key, value) in &update.metadata_set {
6319            slot.insert(key.as_str().to_owned(), value.clone());
6320        }
6321        for key in &update.metadata_remove {
6322            slot.remove(key.as_str());
6323        }
6324        if let Some(delivers) = &update.delivers {
6325            set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
6326        }
6327        if let Some((_, recorded)) = &edges {
6328            record_edges(&mut slot, recorded);
6329        }
6330        let held = item.raw_body.clone().unwrap_or_default();
6331        let content = match &update.content {
6332            Some(content) => with_content(&held, content)?,
6333            None => held.clone(),
6334        };
6335        // A slot holding what it held is kept byte for byte, compared as JSON rather than as
6336        // the body's bytes, as a metadata write compares it: a slot a person spelled with
6337        // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
6338        let body = if slot == item.slot {
6339            content
6340        } else {
6341            with_slot(&content, &slot)?
6342        };
6343        // Checked before anything is sent, as a content write checks it: content ending in
6344        // what this source reads as its own slot would read back as metadata.
6345        let (visible, read) = metadata_body(Some(body.clone()))?;
6346        let wanted = update.content.as_deref().or(item.body.as_deref());
6347        if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
6348            return Err(SourceError::Refused {
6349                message: format!(
6350                    "this content ends in what source {} reads as its own metadata slot \
6351                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
6352                     as content; next: remove that trailing block from the content",
6353                    self.name
6354                ),
6355            });
6356        }
6357        let recorded_moves =
6358            slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
6359
6360        // One `updateIssue` carries all three, because every mutation spends the secondary
6361        // limiter and the title, body and state are one mutation's inputs.
6362        let mut fields = serde_json::Map::new();
6363        if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
6364            fields.insert("title".to_owned(), json!(title));
6365        }
6366        if body != held {
6367            fields.insert("body".to_owned(), json!(body));
6368        }
6369        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
6370            fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
6371        }
6372        // **The body is written last, and that is the guarantee a refusal part-way keeps.**
6373        // GitHub runs no two requests as one, and runs one document's mutation fields in order
6374        // without undoing an earlier field when a later one fails — so a body written before a
6375        // board field the board then refused would be left changed. Written after every other
6376        // write has landed, a refusal anywhere leaves the item's body, and every metadata key
6377        // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
6378        // first, together in one request — a terminal option selected before the issue
6379        // closes, as a whole write does — then the `blockedBy` difference, then the body.
6380        let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
6381        let mut clear: Option<(&BoardId, &str)> = None;
6382        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
6383            board_writes.push((
6384                &moving.board,
6385                (
6386                    moving.field.clone(),
6387                    json!({"singleSelectOptionId": moving.option}),
6388                ),
6389            ));
6390        }
6391        match &priority_move {
6392            Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
6393                board,
6394                (field.clone(), json!({"singleSelectOptionId": option})),
6395            )),
6396            Some((board, PriorityWrite::Clear { field }, _)) => clear = Some((board, field)),
6397            None => {}
6398        }
6399        let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
6400        boards.extend(clear.map(|(board, _)| board));
6401        boards.dedup_by(|one, other| one.as_str() == other.as_str());
6402        for board in boards {
6403            let writes = board_writes
6404                .iter()
6405                .filter(|(on, _)| on.as_str() == board.as_str())
6406                .map(|(_, write)| write.clone())
6407                .collect::<Vec<_>>();
6408            let cleared = clear
6409                .filter(|(on, _)| on.as_str() == board.as_str())
6410                .map(|(_, field)| field);
6411            self.set_item_fields(board.as_str(), &item.item_id, &writes, cleared)
6412                .await?;
6413        }
6414        let mut blocked_by_moved = false;
6415        if let Some((native, _)) = &edges
6416            && item.content_kind == ContentKind::Issue
6417        {
6418            blocked_by_moved = self
6419                .reconcile_blocked_by(
6420                    &item.id,
6421                    native,
6422                    Issue::Existing(item.blocked_by.as_deref()),
6423                )
6424                .await?;
6425        }
6426        if !fields.is_empty() {
6427            self.update_content(item.content_kind, &item.id, Value::Object(fields))
6428                .await?;
6429        }
6430
6431        if let Some(title) = &update.title {
6432            item.title.clone_from(title);
6433        }
6434        item.body = visible.filter(|value| !value.is_empty());
6435        item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
6436        item.slot = slot;
6437        if let Some(delivers) = &update.delivers {
6438            item.delivers.clone_from(delivers);
6439        }
6440        if let Some(moving) = status_move {
6441            item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
6442                && item.content_kind == ContentKind::Issue;
6443            item.status = moving.landed;
6444            item.option = Some(moving.name);
6445        }
6446        if let Some((_, _, priority)) = priority_move {
6447            item.priority = HeldPriority::Read(priority);
6448        }
6449        let task = item.task()?;
6450        let mut written = update.changed(&before, &task);
6451        if blocked_by_moved || recorded_moves {
6452            written.insert(UpdatedField::DependsOn);
6453        }
6454        self.remember_written(item, false)?;
6455        Ok(Some(TaskUpdateOutcome {
6456            task,
6457            written,
6458            delivers_before: before.delivers,
6459        }))
6460    }
6461
6462    /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
6463    /// together, and nothing else; see [`TaskSource::set_task_rendering`].
6464    ///
6465    /// One update of the body: the content outside the slot, and inside it that one entry,
6466    /// every other entry kept as it was. This source keeps no template answers — an issue has
6467    /// no room beside itself that is not its body, and answers written there would duplicate
6468    /// what the content already says and count against GitHub's body limit — so `answers`
6469    /// reaches nothing here. A body that would not change is not sent at all.
6470    async fn replace_rendering(
6471        &self,
6472        id: &NativeId,
6473        kind: BoardKind,
6474        content: &str,
6475        provenance: &Value,
6476        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
6477    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
6478        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
6479            return Ok(None);
6480        };
6481        let held = item.raw_body.clone().unwrap_or_default();
6482        let mut slot = item.slot.clone();
6483        slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
6484        let rewritten;
6485        let content = if let Some(assets) = assets {
6486            let uploads = self
6487                .upload_assets(item.own_repository.as_ref(), assets)
6488                .await?;
6489            rewritten =
6490                onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
6491            rewritten.as_str()
6492        } else {
6493            content
6494        };
6495        let body = with_slot(&with_content(&held, content)?, &slot)?;
6496        // Checked before anything is sent, as a content write checks it.
6497        let (visible, read) = metadata_body(Some(body.clone()))?;
6498        if visible.as_deref().unwrap_or_default() != content || read != slot {
6499            return Err(SourceError::Refused {
6500                message: format!(
6501                    "this content ends in what source {} reads as its own metadata slot \
6502                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
6503                     as content; next: remove that trailing block from the template",
6504                    self.name
6505                ),
6506            });
6507        }
6508        if body != held {
6509            self.update_content(item.content_kind, &item.id, json!({"body": body}))
6510                .await?;
6511        }
6512        item.body = visible.filter(|value| !value.is_empty());
6513        item.raw_body = Some(body);
6514        item.slot = read;
6515        self.remember_written(item, false)?;
6516        Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
6517            id: id.clone(),
6518            content: Some(content.to_owned()),
6519        }))
6520    }
6521
6522    async fn set_item_field(
6523        &self,
6524        board_id: &str,
6525        item_id: &str,
6526        field_id: &str,
6527        value: Value,
6528    ) -> Result<(), SourceError> {
6529        let data = self
6530            .graphql(
6531                graphql::UPDATE_FIELD,
6532                json!({"input":{
6533                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
6534                },"readPriority":false,"priorityName":PRIORITY_FIELD}),
6535            )
6536            .await?;
6537        let returned = data
6538            .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
6539            .ok_or_else(|| SourceError::Malformed {
6540                message: "GitHub field update returned no project item".into(),
6541            })?;
6542        if required_str(returned, "id")? != item_id {
6543            return Err(SourceError::Malformed {
6544                message: "GitHub field update returned the wrong project item".into(),
6545            });
6546        }
6547        Ok(())
6548    }
6549
6550    /// GitHub accepts one value per field mutation; aliases combine those mutations in
6551    /// one request. Every returned item id is checked, including optional aliases.
6552    async fn set_item_fields(
6553        &self,
6554        board: &str,
6555        item: &str,
6556        fields: &[(String, Value)],
6557        clear: Option<&str>,
6558    ) -> Result<(), SourceError> {
6559        if fields.len() <= 1 && clear.is_none() {
6560            if let Some((field, value)) = fields.first() {
6561                self.set_item_field(board, item, field, value.clone())
6562                    .await?;
6563            }
6564            return Ok(());
6565        }
6566        if fields.is_empty() {
6567            if let Some(field) = clear {
6568                self.write_priority(
6569                    board,
6570                    item,
6571                    &PriorityWrite::Clear {
6572                        field: field.to_owned(),
6573                    },
6574                )
6575                .await?;
6576            }
6577            return Ok(());
6578        }
6579        let input = |index: usize| {
6580            let (field, value) = fields.get(index).unwrap_or(&fields[0]);
6581            json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
6582        };
6583        let data = self.graphql(graphql::UPDATE_FIELDS, json!({
6584            "input":input(0),"second":input(1),"third":input(2),
6585            "writeSecond":fields.len()>1,"writeThird":fields.len()>2,"writeClear":clear.is_some(),
6586            "clear":{"projectId":board,"itemId":item,"fieldId":clear.unwrap_or(&fields[0].0)}
6587        })).await?;
6588        for alias in [
6589            Some("updateProjectV2ItemFieldValue"),
6590            (fields.len() > 1).then_some("second"),
6591            (fields.len() > 2).then_some("third"),
6592            clear.map(|_| "cleared"),
6593        ]
6594        .into_iter()
6595        .flatten()
6596        {
6597            let returned = data
6598                .get(alias)
6599                .and_then(|value| value.get("projectV2Item"))
6600                .ok_or_else(|| SourceError::Malformed {
6601                    message: format!("GitHub field update {alias} returned no project item"),
6602                })?;
6603            if required_str(returned, "id")? != item {
6604                return Err(SourceError::Malformed {
6605                    message: format!("GitHub field update {alias} returned the wrong project item"),
6606                });
6607            }
6608        }
6609        Ok(())
6610    }
6611
6612    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
6613        let mut after: Option<String> = None;
6614        let mut ids = Vec::new();
6615        loop {
6616            let data = self
6617                .graphql(
6618                    graphql::ISSUE_DEPENDENCIES,
6619                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
6620                )
6621                .await?;
6622            let connection =
6623                data.pointer("/node/blockedBy")
6624                    .ok_or_else(|| SourceError::Malformed {
6625                        message: "GitHub dependency response has no blockedBy connection".into(),
6626                    })?;
6627            ids.extend(
6628                connection
6629                    .get("nodes")
6630                    .and_then(Value::as_array)
6631                    .ok_or_else(|| SourceError::Malformed {
6632                        message: "GitHub dependency response nodes is not an array".into(),
6633                    })?
6634                    .iter()
6635                    .map(|value| required_str(value, "id").map(str::to_owned))
6636                    .collect::<Result<Vec<_>, _>>()?,
6637            );
6638            let next = next_cursor(connection)?;
6639            if let Some(next) = &next {
6640                validate_cursor_progress(after.as_deref(), &next.0)?;
6641            }
6642            after = next.map(|cursor| cursor.0);
6643            if after.is_none() {
6644                return Ok(ids);
6645            }
6646        }
6647    }
6648
6649    async fn dependencies(
6650        &self,
6651        id: &NativeId,
6652        near_kind: ItemKind,
6653        direction: Direction,
6654        page: &PageRequest,
6655    ) -> Result<Page<DependencyEdge>, SourceError> {
6656        validate_page(page)?;
6657        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
6658        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
6659        let recorded = recorded_offset(cursor, direction)?;
6660        // What this issue is blocked by, when a read of it by its own id in this command
6661        // already carried the whole connection — a copy reads the item it writes before it
6662        // reads its edges — and the page asked for is the whole of it, or the recorded tail
6663        // after it. Answered from that read, in the shape the dependency read answers in;
6664        // anything else is asked of GitHub.
6665        let carried = match direction {
6666            Direction::DependsOn => self
6667                .resolved_cache()?
6668                .get(id)
6669                .filter(|item| item.content_kind == ContentKind::Issue)
6670                .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
6671            Direction::DependedOnBy => None,
6672        }
6673        .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
6674        // Asked for even in the recorded phase, whose page reads nothing from the
6675        // connection: `__typename` is what says whether this item has a native
6676        // relationship at all, and that is what decides which far ends the reserved key is
6677        // allowed to hold.
6678        let data = match carried {
6679            Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
6680                "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
6681            None => {
6682                self.graphql(
6683                    graphql::ISSUE_DEPENDENCIES,
6684                    json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
6685                           "after":if recorded.is_some() {None} else {cursor}}),
6686                )
6687                .await?
6688            }
6689        };
6690        let node =
6691            data.get("node")
6692                .filter(|v| !v.is_null())
6693                .ok_or_else(|| SourceError::Refused {
6694                    message: format!(
6695                        "GitHub item {} was not found or does not support dependencies",
6696                        id.0
6697                    ),
6698                })?;
6699        let connection_name = match direction {
6700            Direction::DependsOn => "blockedBy",
6701            Direction::DependedOnBy => "blocking",
6702        };
6703        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
6704        // named natively and the reserved key may hold any far end. An issue's connections
6705        // hold issues, and this source reads them at the near item's own level.
6706        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
6707        if let Some(offset) = recorded {
6708            return Ok(recorded_page(
6709                self.recorded_edges(id, near_kind, direction, natively_names, node)
6710                    .await?,
6711                offset,
6712                limit,
6713            ));
6714        }
6715        if natively_names.is_none() {
6716            return Ok(recorded_page(
6717                self.recorded_edges(id, near_kind, direction, natively_names, node)
6718                    .await?,
6719                0,
6720                limit,
6721            ));
6722        }
6723        let connection = node
6724            .get(connection_name)
6725            .ok_or_else(|| SourceError::Malformed {
6726                message: "GitHub dependency response is missing its connection".into(),
6727            })?;
6728        let nodes = connection
6729            .get("nodes")
6730            .and_then(Value::as_array)
6731            .ok_or_else(|| SourceError::Malformed {
6732                message: "GitHub dependency response nodes is not an array".into(),
6733            })?;
6734        // `from` depends on `to`, always. GitHub spells the same relationship from either
6735        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
6736        // it — so the near item is `from` in one direction and `to` in the other.
6737        let items = nodes
6738            .iter()
6739            .map(|value| {
6740                let related = NativeId(required_str(value, "id")?.into());
6741                let related_kind = related_kind(value)?;
6742                let (from, to) = match direction {
6743                    Direction::DependsOn => (
6744                        DependencyEndpoint::from_native(id.clone(), near_kind),
6745                        DependencyEndpoint::from_native(related, related_kind),
6746                    ),
6747                    Direction::DependedOnBy => (
6748                        DependencyEndpoint::from_native(related, related_kind),
6749                        DependencyEndpoint::from_native(id.clone(), near_kind),
6750                    ),
6751                };
6752                Ok(DependencyEdge {
6753                    from,
6754                    to,
6755                    kind: DependencyKind::Blocks,
6756                })
6757            })
6758            .collect::<Result<Vec<_>, SourceError>>()?;
6759        let mut next = next_cursor(connection)?;
6760        if let Some(next) = &next {
6761            validate_cursor_progress(cursor, &next.0)?;
6762        }
6763        if next.is_none()
6764            && !self
6765                .recorded_edges(id, near_kind, direction, natively_names, node)
6766                .await?
6767                .is_empty()
6768        {
6769            next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
6770        }
6771        Ok(Page { items, next })
6772    }
6773
6774    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
6775    /// a far end in another source has to live: no GitHub issue relationship can name one.
6776    ///
6777    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
6778    /// source never writes one down.
6779    ///
6780    /// The metadata lives in the item's own body slot, and `node` is the dependency read's
6781    /// own answer, which carries an issue's body — so an issue's recorded edges cost no
6782    /// request beyond the read already made, and reading the board for them would be a
6783    /// walk of every item for one field of one. A draft has no body in that answer, because
6784    /// a draft is not an issue, so a draft's are read off its own read by id — never off a
6785    /// listing of the board, which can be behind on the very item asked about.
6786    async fn recorded_edges(
6787        &self,
6788        id: &NativeId,
6789        near_kind: ItemKind,
6790        direction: Direction,
6791        natively_names: Option<ItemKind>,
6792        node: &Value,
6793    ) -> Result<Vec<DependencyEdge>, SourceError> {
6794        if direction != Direction::DependsOn {
6795            return Ok(Vec::new());
6796        }
6797        let slot = match node.get("body") {
6798            Some(body) if natively_names.is_some() => {
6799                metadata_body(body.as_str().map(str::to_owned))?.1
6800            }
6801            _ => {
6802                let Some(item) = self.bound_item(id).await? else {
6803                    return Ok(Vec::new());
6804                };
6805                item.slot
6806            }
6807        };
6808        DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
6809            .map_err(|message| SourceError::Malformed { message })
6810    }
6811
6812    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
6813        self.repository
6814            .as_ref()
6815            .ok_or_else(|| SourceError::Refused {
6816                message: format!(
6817                    "source {} has no repository configured, and a GitHub Projects board has no \
6818                 repository of its own to create an issue in; set repository: owner/name on \
6819                 this source",
6820                    self.name
6821                ),
6822            })
6823    }
6824
6825    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
6826    /// states.
6827    ///
6828    /// The fallback is demanded first, whichever arm answers: a write without a configured
6829    /// repository is refused naming the field exactly as it was before the rule existed,
6830    /// so a source that could not write before cannot write now, rather than writing for
6831    /// the one item whose own field happens to decide it.
6832    ///
6833    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
6834    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
6835    /// entry owned by someone other than the owner of the parent issue's repository —
6836    /// GitHub accepts a sub-issue from another repository of the same owner and from no
6837    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
6838    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
6839    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
6840    /// and is visible to the token is checked where its node id is resolved, still before
6841    /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
6842    /// looked up in a listing of the board, which can be minutes behind an issue its own
6843    /// `projectItems` already places on it — and that read answers first from this process's
6844    /// own record, so a project created moments ago in this command answers though GitHub
6845    /// has not caught up.
6846    async fn creation_target(
6847        &self,
6848        incoming: &Incoming<'_>,
6849    ) -> Result<RepositoryTarget, SourceError> {
6850        let fallback = self.configured_repository()?;
6851        let what = |incoming: &Incoming<'_>| {
6852            format!(
6853                "{} {:?}",
6854                incoming.written.kind().describes(),
6855                incoming.title
6856            )
6857        };
6858        let parent = match incoming.parent {
6859            Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
6860                SourceError::Refused {
6861                    message: format!(
6862                        "GitHub project issue {} was not found on the board of source {}, so {} \
6863                         cannot be filed under it",
6864                        parent.0,
6865                        self.name,
6866                        what(incoming)
6867                    ),
6868                }
6869            })?),
6870            None => None,
6871        };
6872        let parents_repository = parent
6873            .as_ref()
6874            .map(|parent| {
6875                // A draft is on the board and so is found, but it has no repository to
6876                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
6877                // would refuse the task only once `createIssue` had made it.
6878                if parent.content_kind == ContentKind::DraftIssue {
6879                    return Err(SourceError::Refused {
6880                        message: format!(
6881                            "GitHub project item {} on the board of source {} is a draft, \
6882                             which cannot have sub-issues, so {} cannot be filed under it",
6883                            parent.id.0,
6884                            self.name,
6885                            what(incoming)
6886                        ),
6887                    });
6888                }
6889                // An issue's repository is where a sub-issue is placed and whose owner it
6890                // is compared against, so a parent whose repository this source cannot
6891                // spell as `owner/name` — GitHub's login grammar is wider than this
6892                // source's floor — is one nothing can be filed under.
6893                parent
6894                    .own_repository
6895                    .as_ref()
6896                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
6897                    .ok_or_else(|| SourceError::Malformed {
6898                        message: format!(
6899                            "GitHub project issue {} on the board of source {} is in {}, which \
6900                             is not a {}/owner/name repository this source can place {} in",
6901                            parent.id.0,
6902                            self.name,
6903                            parent
6904                                .own_repository
6905                                .as_ref()
6906                                .map_or("no repository", Repository::as_str),
6907                            RepositoryTarget::HOST,
6908                            what(incoming)
6909                        ),
6910                    })
6911            })
6912            .transpose()?;
6913        match incoming.repositories {
6914            [named] => {
6915                let target =
6916                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
6917                        message: format!(
6918                            "{} names repository {}, which is not a {}/owner/name repository \
6919                             source {} can create an issue in; name one that is, or name none",
6920                            what(incoming),
6921                            named.as_str(),
6922                            RepositoryTarget::HOST,
6923                            self.name
6924                        ),
6925                    })?;
6926                if let Some(parents) = &parents_repository
6927                    && parents.owner != target.owner
6928                {
6929                    return Err(SourceError::Refused {
6930                        message: format!(
6931                            "{} names repository {}, owned by {}, but its project's issue is in \
6932                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
6933                             of the same owner as its parent issue; name a repository of {}, or \
6934                             name none",
6935                            what(incoming),
6936                            target.slug(),
6937                            target.owner,
6938                            parents.slug(),
6939                            parents.owner,
6940                            parents.owner
6941                        ),
6942                    });
6943                }
6944                Ok(target)
6945            }
6946            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
6947        }
6948    }
6949
6950    /// The node id of the repository `incoming` is being created in, or the refusal naming
6951    /// the item and the repository the token cannot see.
6952    ///
6953    /// Resolved once per command per repository; see [`Self::repository_cache`].
6954    async fn repository_id(
6955        &self,
6956        repository: &RepositoryTarget,
6957        incoming: &Incoming<'_>,
6958    ) -> Result<String, SourceError> {
6959        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
6960            return Ok(id);
6961        }
6962        let data = self
6963            .graphql(
6964                graphql::REPOSITORY,
6965                json!({"owner":repository.owner,"name":repository.name}),
6966            )
6967            .await?;
6968        self.repository_read(&data, repository, incoming)
6969    }
6970
6971    /// The repository's node id out of an answer carrying the `repository` root, held for
6972    /// the rest of this command, or the refusal naming the item that cannot be created in it.
6973    fn repository_read(
6974        &self,
6975        data: &Value,
6976        repository: &RepositoryTarget,
6977        incoming: &Incoming<'_>,
6978    ) -> Result<String, SourceError> {
6979        let node = data
6980            .get("repository")
6981            .filter(|value| !value.is_null())
6982            .ok_or_else(|| SourceError::Refused {
6983                message: format!(
6984                    "GitHub repository {} was not found or is not visible to the token, so {} \
6985                     {:?} cannot be created in it",
6986                    repository.slug(),
6987                    incoming.written.kind().describes(),
6988                    incoming.title
6989                ),
6990            })?;
6991        let id = required_str(node, "id")?.to_owned();
6992        self.repository_cache()?
6993            .insert(repository.clone(), id.clone());
6994        Ok(id)
6995    }
6996
6997    fn repository_cache(
6998        &self,
6999    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
7000        self.repository_cache
7001            .lock()
7002            .map_err(|_| SourceError::Unavailable {
7003                message: "this source's record of the destination repository was left \
7004                          inconsistent by an earlier failure; next: run the command again"
7005                    .into(),
7006            })
7007    }
7008
7009    /// Create or update one board item, whichever kind it is.
7010    async fn write_item(
7011        &self,
7012        incoming: &Incoming<'_>,
7013        target: Option<&NativeId>,
7014        depends_on: &[DependencyEdge],
7015    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
7016        // Refused before anything is read or written: a task or a project titled the way
7017        // this board spells a document would land as an issue this same source reads back
7018        // as a document, so the field this destination cannot carry is named rather than
7019        // written and silently reclassified.
7020        if let Written::Work(kind, _) = incoming.written
7021            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
7022        {
7023            return Err(SourceError::Refused {
7024                message: format!(
7025                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
7026                     spells a document, so it would read back as one rather than as a {}; \
7027                     retitle it, or copy it as a document",
7028                    kind.marker(),
7029                    self.name,
7030                    kind.marker()
7031                ),
7032            });
7033        }
7034        // The destination is read by its own id, and whether this board holds it is decided
7035        // by that read — its own `projectItems` — rather than by whether a listing of the
7036        // board happens to include it yet. See the module documentation.
7037        let existing = match target {
7038            Some(target) => {
7039                Some(
7040                    self.bound_item(target)
7041                        .await?
7042                        .ok_or_else(|| SourceError::Refused {
7043                            message: format!("GitHub destination item {} was not found", target.0),
7044                        })?,
7045                )
7046            }
7047            None => None,
7048        };
7049        let existing = existing.as_ref();
7050        // An existing issue is never moved; a new one is created where the rule says — and
7051        // knowing where is what lets the board's fields and that repository's id be read
7052        // together, before anything below needs either.
7053        let creation_target = match existing {
7054            Some(_) => None,
7055            None => {
7056                let target = self.creation_target(incoming).await?;
7057                self.creation_context(&target, incoming).await?;
7058                Some(target)
7059            }
7060        };
7061        let board = self
7062            .fields_for(
7063                existing,
7064                incoming.written.status().is_some(),
7065                incoming
7066                    .priority
7067                    .is_some_and(|priority| priority != Priority::None),
7068            )
7069            .await?;
7070        let status_target = incoming
7071            .written
7072            .work_status()
7073            .map(|(kind, status)| self.resolved_target(kind, status.category))
7074            .transpose()?;
7075        let column = match (incoming.written.work_status(), status_target.as_ref()) {
7076            (Some((kind, status)), Some(target)) => {
7077                self.column_for(&board.fields, kind, status.category, target)?
7078            }
7079            _ => None,
7080        };
7081        // Resolved before anything is created, for the reason the column above is: a
7082        // priority this board has no option for is refused while nothing has been written.
7083        let priority_write = match incoming.priority {
7084            Some(priority) => self.priority_write(&board.fields, existing, priority)?,
7085            None => None,
7086        };
7087        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
7088        if content_kind == ContentKind::DraftIssue {
7089            if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
7090                (status_target.as_ref(), incoming.written.status())
7091            {
7092                return Err(self.closes_a_draft(status.category));
7093            }
7094            if incoming.parent.is_some() {
7095                return Err(SourceError::Refused {
7096                    message: "GitHub draft items cannot be a project's sub-issue".into(),
7097                });
7098            }
7099        }
7100        match existing {
7101            Some(item) if content_kind == ContentKind::Issue => {
7102                if item.labels != incoming.labels {
7103                    return Err(SourceError::Refused {
7104                        message: "GitHub issue labels differ from the labels being written".into(),
7105                    });
7106                }
7107            }
7108            _ => {
7109                if !incoming.labels.is_empty() {
7110                    return Err(SourceError::Refused {
7111                        message: "GitHub items created by this destination carry no labels".into(),
7112                    });
7113                }
7114            }
7115        }
7116
7117        // The repository the issue really lives in is what the slot below is written against,
7118        // so a single entry that is where the issue is created travels as no key at all, and
7119        // the read side derives it back from the issue.
7120        let own_repository = match (existing, &creation_target) {
7121            (Some(item), _) => item.own_repository.clone(),
7122            (None, Some(target)) => Some(
7123                Repository::try_from(target.origin())
7124                    .map_err(|message| SourceError::Config { message })?,
7125            ),
7126            (None, None) => None,
7127        };
7128        let (native, fallback) = self
7129            .partition_edges(
7130                incoming.written.kind(),
7131                content_kind,
7132                existing.and_then(|item| item.blocked_by.as_deref()),
7133                depends_on,
7134            )
7135            .await?;
7136        let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
7137        let content = match incoming.assets {
7138            Some(assets) => {
7139                let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
7140                let rewritten = onetaskgraph_plugin_api::serve_asset_references(
7141                    incoming.content.unwrap_or_default(),
7142                    &mut slot,
7143                    &uploads,
7144                );
7145                incoming.content.map(|_| rewritten)
7146            }
7147            None => incoming.content.map(str::to_owned),
7148        };
7149        let body = compose_body(content.as_deref(), &slot)?;
7150        // Read before anything is created, for the reason the field below is: a value
7151        // this destination cannot store has to refuse, and refusing after `createIssue`
7152        // would leave an issue behind that nothing asked for. The engine writes a
7153        // qualified id here; a caller handing this key anything else is told so rather
7154        // than having it silently stored as no origin at all.
7155        // 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.
7156        let origin = match incoming.metadata.get(ORIGIN_KEY) {
7157            None => "",
7158            Some(Value::String(origin)) => origin.as_str(),
7159            Some(other) => {
7160                return Err(SourceError::Refused {
7161                    message: format!(
7162                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
7163                         is {other}"
7164                    ),
7165                });
7166            }
7167        };
7168        // Resolved before anything is created: a board that cannot carry the copy origin
7169        // has to refuse the write, and refusing it after `createIssue` would leave an
7170        // issue behind that nothing asked for.
7171        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
7172            Some(field) => {
7173                if required_str(field, "__typename")? != "ProjectV2Field" {
7174                    return Err(SourceError::Refused {
7175                        message: format!(
7176                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
7177                        ),
7178                    });
7179                }
7180                Some(required_str(field, "id")?.to_owned())
7181            }
7182            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
7183                return Err(SourceError::Refused {
7184                    message: format!(
7185                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
7186                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
7187                         the board"
7188                    ),
7189                });
7190            }
7191            None => None,
7192        };
7193
7194        let Landed {
7195            content_id,
7196            item_id,
7197            url,
7198            number,
7199        } = match existing {
7200            // Its content is written last, below, once everything else has landed.
7201            Some(item) => Landed {
7202                content_id: item.id.clone(),
7203                item_id: item.item_id.clone(),
7204                url: item.url.clone(),
7205                number: item.number,
7206            },
7207            None => {
7208                let target = creation_target
7209                    .as_ref()
7210                    .ok_or_else(|| SourceError::Malformed {
7211                        message: "a new item was decided without a repository to create it in"
7212                            .into(),
7213                    })?;
7214                self.create_and_file_issue(board.id.as_str(), target, incoming, &body)
7215                    .await?
7216            }
7217        };
7218
7219        let written_option = column.as_ref().map(|(_, _, name)| name.clone());
7220        let column = column
7221            .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
7222            .map(|(field, option, _)| (field, option));
7223        // Creating an item here is several calls — `createIssue`, which files it on the
7224        // board, then its board fields, the parent and the dependencies — and GitHub can fail
7225        // at any of them. Everything this source can refuse *before* the first of those is
7226        // already checked above, so what is left is GitHub itself failing part way. When it
7227        // does over an item this call created, the issue is taken back: a write that
7228        // refused must not leave an item behind that nobody asked for, and one that does
7229        // makes the retry create a second.
7230        // Whether the board-field write carrying a moved origin was answered as landing whole.
7231        // When it was refused, GitHub does not say which of its fields ran before the one that
7232        // failed, so the origin may or may not have moved.
7233        let mut origin_landed = false;
7234        let landed = self
7235            .finish_write(
7236                board.id.as_str(),
7237                incoming,
7238                &content_id,
7239                &item_id,
7240                content_kind,
7241                existing,
7242                origin_field.as_deref(),
7243                origin,
7244                column,
7245                status_target.as_ref(),
7246                priority_write.as_ref(),
7247                &native,
7248                &mut origin_landed,
7249            )
7250            .await;
7251        // An existing item's title, body and state go last, in one `updateIssue`, once its board
7252        // fields and its relationships have landed: a refusal of any of those then leaves its
7253        // body — and the metadata slot inside it — exactly as it stood.
7254        let landed = match (landed, existing) {
7255            (Ok(()), Some(item)) => {
7256                self.update_existing(item, incoming, &body, status_target.as_ref())
7257                    .await
7258            }
7259            (landed, _) => landed,
7260        };
7261        if let Err(error) = landed {
7262            match existing {
7263                // Best effort, and the write's own failure is what the caller is told: a
7264                // refusal naming the tidy-up would hide why the write failed at all.
7265                None => {
7266                    let _ = self.delete_issue(&content_id).await;
7267                }
7268                // The origin field is the one piece of an existing item's metadata written
7269                // before its body, so a write refused after it puts it back as it was. When
7270                // that is refused too, the write's own failure is still what the caller is
7271                // told — with what it left behind added, because the item's metadata is then
7272                // not as it stood and a caller retrying has to know which key moved.
7273                Some(item) => {
7274                    let before = item.origin.as_deref().unwrap_or("");
7275                    if let Some(field) = origin_field.as_deref()
7276                        && before != origin
7277                        && let Err(restore) = self
7278                            .set_item_field(
7279                                board.id.as_str(),
7280                                &item.item_id,
7281                                field,
7282                                json!({"text": before}),
7283                            )
7284                            .await
7285                    {
7286                        let left = if origin_landed {
7287                            format!(
7288                                "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
7289                                 not be put back to {before:?} ({restore}), so item {} still \
7290                                 holds {origin:?} there",
7291                                item.id.0
7292                            )
7293                        } else {
7294                            format!(
7295                                "the refused write carried its {ORIGIN_KEY} from {before:?} to \
7296                                 {origin:?}, GitHub does not say whether that part of it ran, \
7297                                 and putting it back to {before:?} was refused ({restore}), so \
7298                                 item {} holds {origin:?} or {before:?} there",
7299                                item.id.0
7300                            )
7301                        };
7302                        return Err(noting(
7303                            error,
7304                            &format!(
7305                                "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
7306                                 run the write again"
7307                            ),
7308                        ));
7309                    }
7310                }
7311            }
7312            return Err(error);
7313        }
7314
7315        let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
7316            (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
7317                self.statuses
7318                    .status(kind, written_option.as_deref(), true, Some(reason.reason()))
7319            }
7320            (Some((kind, _)), Some(StatusTarget::Column(_))) => {
7321                self.statuses
7322                    .status(kind, written_option.as_deref(), false, None)
7323            }
7324            (Some((_, status)), _) => status.clone(),
7325            (None, _) => Status {
7326                category: StatusCategory::Unknown,
7327                name: "Open".to_owned(),
7328            },
7329        };
7330
7331        // So the rest of this command reads what it just did rather than what the board
7332        // said before it. See `remember_written` for which half takes it.
7333        let remembered = Resolved {
7334            item_id,
7335            id: content_id.clone(),
7336            content_kind,
7337            kind: incoming.written.kind(),
7338            title: incoming.title.to_owned(),
7339            // The visible half of the body this write composed, split back off it the
7340            // way a read splits it — so what this record reports is what a read of the
7341            // same issue reports, rather than the person's text with the metadata slot
7342            // still on the end of it.
7343            body: metadata_body(body.clone())?.0,
7344            raw_body: body.clone(),
7345            // A document has no status of its own; what it reads back as is whatever
7346            // the issue's own state says, which is what a re-read reports.
7347            status: written_status,
7348            option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
7349            priority: match incoming.priority {
7350                Some(priority) => HeldPriority::Read(priority),
7351                None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
7352                    item.priority.clone()
7353                }),
7354            },
7355            // What `state_input` asked for: closed for a terminal target, open for any other
7356            // status, and the issue's own state left as it was by a document write.
7357            closed: content_kind == ContentKind::Issue
7358                && match status_target.as_ref() {
7359                    Some(StatusTarget::Terminal(_, _)) => true,
7360                    Some(_) => false,
7361                    None => existing.is_some_and(|item| item.closed),
7362                },
7363            delivers: incoming.delivers.to_vec(),
7364            delivered_by: incoming.delivered_by.to_vec(),
7365            labels: incoming.labels.to_vec(),
7366            parent: incoming.parent.cloned(),
7367            origin: (!origin.is_empty()).then(|| origin.to_owned()),
7368            number,
7369            // In the update path this is the item's own url, read off `existing` where the
7370            // record above was bound, so one expression serves both halves.
7371            url,
7372            created_at: existing.and_then(|item| item.created_at),
7373            updated_at: existing.and_then(|item| item.updated_at),
7374            own_repository,
7375            repositories: incoming.repositories.to_vec(),
7376            slot,
7377            board_id: Some(board.id.as_str().to_owned()),
7378            fields: board
7379                .fields
7380                .get("nodes")
7381                .and_then(Value::as_array)
7382                .cloned()
7383                .unwrap_or_default(),
7384            board_fields: Some(board.fields.clone()),
7385            // What this write left the relationship holding is known by id alone, and a
7386            // later read of its edges needs each far end's kind, so it reads them again.
7387            blocked_by: None,
7388        };
7389        self.remember_written(remembered, existing.is_none())?;
7390        Ok(onetaskgraph_plugin_api::AssetsWritten {
7391            id: content_id,
7392            content,
7393        })
7394    }
7395
7396    /// Everything a write does after the item exists: its board fields, its parent, and
7397    /// its dependencies.
7398    ///
7399    /// Split out of `write_item` so there is one place a failure past the point of no
7400    /// return is caught, rather than a tidy-up repeated at each `?` above.
7401    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
7402    // so there is one place a failure past the point of no return is caught, and its
7403    // arguments are exactly the values that tail already had in scope. Bundling them into a
7404    // struct would describe no concept — it would be "the arguments of this function" — and
7405    // would put the whole of `write_item`'s locals behind one more indirection.
7406    #[allow(clippy::too_many_arguments)]
7407    async fn finish_write(
7408        &self,
7409        board_id: &str,
7410        incoming: &Incoming<'_>,
7411        content_id: &NativeId,
7412        item_id: &str,
7413        content_kind: ContentKind,
7414        existing: Option<&Resolved>,
7415        origin_field: Option<&str>,
7416        origin: &str,
7417        column: Option<(String, String)>,
7418        status_target: Option<&StatusTarget>,
7419        priority: Option<&PriorityWrite>,
7420        native: &[String],
7421        origin_landed: &mut bool,
7422    ) -> Result<(), SourceError> {
7423        let mut fields = Vec::new();
7424        if let Some(field_id) = origin_field
7425            && existing.map_or(!origin.is_empty(), |item| {
7426                item.origin.as_deref().unwrap_or("") != origin
7427            })
7428        {
7429            fields.push((field_id.to_owned(), json!({"text":origin})));
7430        }
7431        if let Some((field_id, option_id)) = column {
7432            fields.push((field_id, json!({"singleSelectOptionId":option_id})));
7433        }
7434        let clear = match priority {
7435            Some(PriorityWrite::Select { field, option }) => {
7436                fields.push((field.clone(), json!({"singleSelectOptionId":option})));
7437                None
7438            }
7439            Some(PriorityWrite::Clear { field }) => Some(field.as_str()),
7440            None => None,
7441        };
7442        self.set_item_fields(board_id, item_id, &fields, clear)
7443            .await?;
7444        *origin_landed = true;
7445
7446        // An existing issue closes in the `updateIssue` its write ends with; one created just
7447        // now closes here, once its option is selected.
7448        if existing.is_none()
7449            && content_kind == ContentKind::Issue
7450            && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
7451        {
7452            self.update_content(
7453                ContentKind::Issue,
7454                content_id,
7455                json!({"stateInput":state_input(status_target)}),
7456            )
7457            .await?;
7458        }
7459
7460        if content_kind == ContentKind::Issue {
7461            self.reparent(
7462                existing.and_then(|item| item.parent.clone()),
7463                content_id,
7464                incoming.parent,
7465            )
7466            .await?;
7467            // A document takes part in no dependency graph, so writing one neither reads
7468            // nor changes the issue's own `blockedBy` relationships. Reconciling them
7469            // against the empty list a document write carries would *delete* whatever
7470            // relationships a person had made on that issue, which is a write nobody
7471            // asked for.
7472            if incoming.written.kind() != BoardKind::Document {
7473                let issue = match existing {
7474                    Some(item) => Issue::Existing(item.blocked_by.as_deref()),
7475                    None => Issue::Created,
7476                };
7477                self.reconcile_blocked_by(content_id, native, issue).await?;
7478            }
7479        }
7480        Ok(())
7481    }
7482
7483    /// Delete one issue, which takes its board item with it.
7484    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
7485        let data = self
7486            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
7487            .await?;
7488        data.pointer("/deleteIssue/repository")
7489            .filter(|value| !value.is_null())
7490            .ok_or_else(|| SourceError::Malformed {
7491                message: "GitHub issue deletion returned no repository".into(),
7492            })?;
7493        self.forget(id)?;
7494        Ok(())
7495    }
7496
7497    /// Remove one item this copy created, so a copy that could not finish leaves the board
7498    /// as it found it.
7499    ///
7500    /// Deleting the issue takes its board item with it, so there is no second mutation to
7501    /// keep in step. An id the board does not hold is not an error: the item is already
7502    /// gone, which is the state this asks for. Which that is, is decided by reading the item
7503    /// by its own id — a listing of the board can still be missing an item it holds, and
7504    /// reading that as *already gone* would leave behind the very item this was asked to
7505    /// take back.
7506    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
7507        let Some(item) = self.bound_item(id).await? else {
7508            return Ok(());
7509        };
7510        if item.content_kind == ContentKind::DraftIssue {
7511            return Err(SourceError::Refused {
7512                message: format!(
7513                    "GitHub item {} is a draft, and this source removes an item by deleting \
7514                     its issue; next: remove it from the board by hand",
7515                    id.0
7516                ),
7517            });
7518        }
7519        let data = self
7520            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
7521            .await?;
7522        data.pointer("/deleteIssue/repository")
7523            .filter(|value| !value.is_null())
7524            .ok_or_else(|| SourceError::Malformed {
7525                message: "GitHub issue deletion returned no repository".into(),
7526            })?;
7527        self.forget(id)?;
7528        Ok(())
7529    }
7530
7531    /// The issue a comment call on `task` is about, or `None` when this board holds no such
7532    /// task.
7533    ///
7534    /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
7535    /// read of the task cannot disagree about which ids name one: a project or a document of
7536    /// this board is not a task here either.
7537    ///
7538    /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
7539    /// issues and a draft is not one. It is refused rather than answered with an empty page,
7540    /// which would read as a task nobody has commented on yet.
7541    async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
7542        let cached = self.resolved_cache()?.get(task).cloned();
7543        let Some(item) = (match cached {
7544            Some(item) => Some(item),
7545            None => self.item_by_id(task).await?,
7546        })
7547        .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
7548            return Ok(None);
7549        };
7550        if item.content_kind == ContentKind::DraftIssue {
7551            return Err(self.draft_has_no_comments(task));
7552        }
7553        Ok(Some(item.id))
7554    }
7555
7556    /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
7557    /// issues, and a draft is not one.
7558    fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
7559        SourceError::Refused {
7560            message: format!(
7561                "task {} of source {} is a draft item on the board, and GitHub keeps \
7562                 comments on issues alone, so a draft has none to read or write; next: \
7563                 convert the draft to an issue on the board, then comment on the issue it \
7564                 becomes",
7565                task.0, self.name
7566            ),
7567        }
7568    }
7569
7570    /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
7571    /// request — or `None` when this board holds no task by that id.
7572    ///
7573    /// What `task show` and a comment listing read. A draft is a task with no comments, so it
7574    /// is answered with the draft and the refusal, at the price of the draft's own read.
7575    async fn issue_detail(
7576        &self,
7577        id: &NativeId,
7578        page: &PageRequest,
7579    ) -> Result<Option<TaskDetailRead>, SourceError> {
7580        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
7581        let asked = self
7582            .graphql(
7583                graphql::ISSUE_DETAIL,
7584                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
7585                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
7586                       "duplicates":true}),
7587            )
7588            .await;
7589        let data = match asked {
7590            Ok(data) => data,
7591            Err(error) if unresolvable_node(&error) => return Ok(None),
7592            Err(error) => return Err(error),
7593        };
7594        // `node` is null for an id that names nothing, and absent only from an answer this
7595        // source cannot read — never the same thing.
7596        let node = data.get("node").ok_or_else(|| SourceError::Malformed {
7597            message: format!("GitHub answered the read of {} with no node", id.0),
7598        })?;
7599        self.detail_of(id, node, true, after).await
7600    }
7601
7602    /// Several tasks, each with the first page of its comments when `comments` is set, read
7603    /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
7604    /// order.
7605    ///
7606    /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
7607    /// one item at a time, so that id is answered as missing and the others as themselves; any
7608    /// other refusal is every id of that batch's answer.
7609    async fn issue_details(
7610        &self,
7611        ids: &[NativeId],
7612        comments: Option<&PageRequest>,
7613    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
7614        let mut read = Vec::with_capacity(ids.len());
7615        for batch in ids.chunks(DETAIL_BATCH) {
7616            match self
7617                .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
7618                .await
7619            {
7620                Ok(data) => {
7621                    for (slot, id) in batch.iter().enumerate() {
7622                        // Every alias asked for is answered, null for an id naming nothing;
7623                        // one missing is an answer this source cannot read.
7624                        let read_one = match data.get(format!("i{slot}")) {
7625                            Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
7626                            None => Err(SourceError::Malformed {
7627                                message: format!(
7628                                    "GitHub answered a batch read with no item for {}",
7629                                    id.0
7630                                ),
7631                            }),
7632                        };
7633                        read.push(read_one);
7634                    }
7635                }
7636                Err(error) if unresolvable_node(&error) => {
7637                    for id in batch {
7638                        read.push(match comments {
7639                            Some(page) => self.issue_detail(id, page).await,
7640                            None => self.task_read(id).await,
7641                        });
7642                    }
7643                }
7644                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
7645            }
7646        }
7647        read
7648    }
7649
7650    /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
7651    async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
7652        Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
7653            task,
7654            comments: None,
7655        }))
7656    }
7657
7658    /// What one node a detail read reached says: the task this board holds by `id`, with the
7659    /// page of comments the node carries when `commented` — or `None` for a node that is no
7660    /// task of this board.
7661    ///
7662    /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
7663    /// and an item this process created answers from this process's own record, which a node
7664    /// read taken moments after the write can still be behind.
7665    async fn detail_of(
7666        &self,
7667        id: &NativeId,
7668        node: &Value,
7669        commented: bool,
7670        after: Option<&str>,
7671    ) -> Result<Option<TaskDetailRead>, SourceError> {
7672        if node.is_null() {
7673            return Ok(None);
7674        }
7675        let draft = optional_str(node, "__typename")? == Some("DraftIssue");
7676        // An issue answered under one id is that id's, or the answer is not one this source
7677        // can report: reporting another issue's task and comments under the qualified id asked
7678        // for would be the one wrong answer here. A draft's own read checks the same.
7679        if !draft
7680            && optional_str(node, "__typename")? == Some("Issue")
7681            && required_str(node, "id")? != id.0
7682        {
7683            return Err(SourceError::Malformed {
7684                message: format!(
7685                    "GitHub answered the read of {} with issue {}",
7686                    id.0,
7687                    required_str(node, "id")?
7688                ),
7689            });
7690        }
7691        let item = if draft {
7692            self.draft_by_id(id).await?
7693        } else {
7694            self.resolve_issue(node).await?
7695        };
7696        let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
7697            return Ok(None);
7698        };
7699        let own = self.created()?.iter().find(|own| own.id == *id).cloned();
7700        let task = own.unwrap_or(item).task()?;
7701        let comments = match (commented, draft) {
7702            (false, _) => None,
7703            (true, true) => Some(Err(self.draft_has_no_comments(id))),
7704            (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
7705        };
7706        Ok(Some(TaskDetailRead { task, comments }))
7707    }
7708
7709    /// Whether the comment `comment` is one of `issue`'s own.
7710    ///
7711    /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
7712    /// comment's id and nothing else: a comment id given against the wrong task would
7713    /// otherwise change a comment on some other issue entirely. An id that names nothing, or
7714    /// names something that is not an issue comment, is a comment this task does not have —
7715    /// which is what GitHub refusing to resolve it means too.
7716    async fn comment_is_on(
7717        &self,
7718        issue: &NativeId,
7719        comment: &NativeId,
7720    ) -> Result<bool, SourceError> {
7721        let asked = self
7722            .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
7723            .await;
7724        let data = match asked {
7725            Ok(data) => data,
7726            Err(error) if unresolvable_node(&error) => return Ok(false),
7727            Err(error) => return Err(error),
7728        };
7729        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
7730            return Ok(false);
7731        };
7732        if optional_str(node, "__typename")? != Some("IssueComment") {
7733            return Ok(false);
7734        }
7735        let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
7736            message: format!("GitHub issue comment {} names no issue", comment.0),
7737        })?;
7738        Ok(required_str(on, "id")? == issue.0)
7739    }
7740
7741    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
7742    async fn partition_edges(
7743        &self,
7744        near_kind: BoardKind,
7745        near_content: ContentKind,
7746        carried: Option<&[Value]>,
7747        depends_on: &[DependencyEdge],
7748    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
7749        let mut native = Vec::new();
7750        let mut fallback = Vec::new();
7751        let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
7752            .iter()
7753            .map(|edge| {
7754                let same_source = edge
7755                    .to
7756                    .source()
7757                    .is_none_or(|source| source == self.name.as_str());
7758                // A qualified id's source segment runs to its *first* colon — `GlobalId` and
7759                // `DependencyEndpoint::source` both read it that way — and a native id may hold
7760                // colons of its own, so the far end is everything after that one separator.
7761                // Splitting at the last would truncate `work:urn:task:7` to `7`.
7762                let far_id = if edge.to.is_qualified() {
7763                    edge.to
7764                        .id()
7765                        .split_once(':')
7766                        .map_or(edge.to.id(), |(_, native)| native)
7767                } else {
7768                    edge.to.id()
7769                };
7770                // One that already blocks the near issue was answered by that issue's own
7771                // read, which carried each of its blockers' kinds — an issue every one — so it
7772                // is not read again.
7773                let blocking = carried.and_then(|nodes| {
7774                    nodes
7775                        .iter()
7776                        .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
7777                });
7778                (edge, far_id, same_source, blocking)
7779            })
7780            .collect();
7781        // Every other same-source far end is read by its own id, exactly as the item it is a
7782        // far end of is: whether this board holds it is that read's answer, never a listing's.
7783        // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
7784        let mut unread: Vec<NativeId> = Vec::new();
7785        for (_, far_id, same_source, blocking) in &far_ends {
7786            let id = NativeId((*far_id).to_owned());
7787            if *same_source && blocking.is_none() && !unread.contains(&id) {
7788                unread.push(id);
7789            }
7790        }
7791        let read: BTreeMap<NativeId, Option<Resolved>> = unread
7792            .iter()
7793            .cloned()
7794            .zip(self.items_by_ids(&unread).await?)
7795            .collect();
7796        for (edge, far_id, same_source, blocking) in far_ends {
7797            let far = match (same_source, blocking) {
7798                (false, _) => None,
7799                (true, Some(node)) => Some(FarEnd {
7800                    kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
7801                        BoardKind::Document
7802                    } else {
7803                        BoardKind::Work(related_kind(node)?)
7804                    },
7805                    content_kind: ContentKind::Issue,
7806                }),
7807                (true, None) => {
7808                    let read = read
7809                        .get(&NativeId(far_id.to_owned()))
7810                        .cloned()
7811                        .flatten()
7812                        .ok_or_else(|| SourceError::Refused {
7813                            message: format!("GitHub dependency item {far_id} was not found"),
7814                        })?;
7815                    Some(FarEnd {
7816                        kind: read.kind,
7817                        content_kind: read.content_kind,
7818                    })
7819                }
7820            };
7821            let far = far.as_ref();
7822            // The caller says which kind the far end is, and this board holds the far end
7823            // itself, so a disagreement is settled here rather than stored: recorded, the
7824            // wrong kind would read back as a cross-level edge that never existed; written
7825            // natively, it would name a relationship of a different level than the caller
7826            // asked for.
7827            //
7828            // A far end this board holds as a *document* fails the same comparison and is
7829            // refused by the same sentence: `ItemKind` has no document variant because
7830            // nothing may point at one, so no caller can name it correctly and the refusal
7831            // is the only honest answer.
7832            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
7833                return Err(SourceError::Refused {
7834                    message: format!(
7835                        "GitHub dependency item {far_id} is a {} of this board, and this item \
7836                         names it as a {}; record the kind it is",
7837                        disagreeing.kind.describes(),
7838                        edge.to.kind.marker()
7839                    ),
7840                });
7841            }
7842            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
7843            // however the far end is spelled — and one classified native here would be
7844            // written nowhere at all, because a draft's native reconciliation never runs.
7845            let native_here = near_content == ContentKind::Issue
7846                && far.is_some_and(|far| {
7847                    far.content_kind == ContentKind::Issue
7848                        && BoardKind::Work(edge.to.kind) == near_kind
7849                });
7850            if native_here {
7851                native.push(far_id.to_owned());
7852            } else {
7853                fallback.push(edge.clone());
7854            }
7855        }
7856        Ok((native, fallback))
7857    }
7858
7859    async fn update_existing(
7860        &self,
7861        item: &Resolved,
7862        incoming: &Incoming<'_>,
7863        body: &Option<String>,
7864        status_target: Option<&StatusTarget>,
7865    ) -> Result<(), SourceError> {
7866        let title = incoming.written_title();
7867        // A terminal status closes the issue here, in the same mutation as its body: its board
7868        // option was selected before this, so a close never lands on an item whose board cannot
7869        // show it.
7870        let fields = match item.content_kind {
7871            ContentKind::DraftIssue => json!({"title":title,"body":body}),
7872            ContentKind::Issue => json!({"title":title,"body":body,
7873                                         "stateInput":state_input(status_target)}),
7874        };
7875        self.update_content(item.content_kind, &item.id, fields)
7876            .await
7877    }
7878
7879    /// Update one board item's content with exactly `fields` beside its id, through the
7880    /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
7881    /// a draft.
7882    ///
7883    /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
7884    /// is what lets a narrow write carry the one thing it changes and nothing else.
7885    async fn update_content(
7886        &self,
7887        kind: ContentKind,
7888        id: &NativeId,
7889        fields: Value,
7890    ) -> Result<(), SourceError> {
7891        let (operation, id_key, pointer) = match kind {
7892            ContentKind::DraftIssue => (
7893                graphql::UPDATE_DRAFT,
7894                "draftIssueId",
7895                "/updateProjectV2DraftIssue/draftIssue",
7896            ),
7897            ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
7898        };
7899        let mut input = fields;
7900        input[id_key] = json!(id.0);
7901        let data = self.graphql(operation, json!({"input":input})).await?;
7902        let returned = data
7903            .pointer(pointer)
7904            .ok_or_else(|| SourceError::Malformed {
7905                message: "GitHub item update returned no item".into(),
7906            })?;
7907        if required_str(returned, "id")? != id.0 {
7908            return Err(SourceError::Malformed {
7909                message: "GitHub item update returned the wrong item".into(),
7910            });
7911        }
7912        Ok(())
7913    }
7914
7915    /// Creates one issue, files it on the board, and reports what a read of it would say:
7916    /// its content id, its board item id, and the web address GitHub gave it.
7917    ///
7918    /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
7919    /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
7920    /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
7921    /// board item, and the `addProjectV2ItemById` that then had to follow was refused
7922    /// "Content already exists in this project". A terminal status is not written here:
7923    /// `finish_write` selects its option first and closes the issue after, so a close never
7924    /// lands on an item whose board cannot show it.
7925    ///
7926    /// The address and the number come back here because this is the only place either is
7927    /// known before GitHub's own board read catches up — an item this run created answers
7928    /// the reads that follow it out of the record below, and one remembered without them
7929    /// would report no location and no key for the rest of the run.
7930    async fn create_and_file_issue(
7931        &self,
7932        board_id: &str,
7933        repository: &RepositoryTarget,
7934        incoming: &Incoming<'_>,
7935        body: &Option<String>,
7936    ) -> Result<Landed, SourceError> {
7937        let repository_id = self.repository_id(repository, incoming).await?;
7938        let data = self
7939            .graphql(
7940                graphql::CREATE_ISSUE,
7941                json!({"input":{
7942                    "repositoryId":repository_id,"title":incoming.written_title(),"body":body
7943                }}),
7944            )
7945            .await?;
7946        let created = data
7947            .pointer("/createIssue/issue")
7948            .filter(|value| !value.is_null())
7949            .ok_or_else(|| SourceError::Malformed {
7950                message: "GitHub issue creation returned no issue".into(),
7951            })?;
7952        let content_id = NativeId(required_str(created, "id")?.to_owned());
7953        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
7954        // a response without it is not worth failing a landed write over — the item simply
7955        // reports no location until the board read catches up, which is what it did before.
7956        let url = optional_str(created, "url")?.map(str::to_owned);
7957        // The issue exists from here on, so an unreadable number and a refused board
7958        // filing below each try, best effort, to take it back: an issue in the repository
7959        // that is on no board is an item nobody asked for and nothing here would find again.
7960        //
7961        // Its number is optional on the same terms its address is — a landed write is not
7962        // worth failing over a member that came back missing, and such an item reports no
7963        // handle until a board read catches up. A number that is *present* and is not an
7964        // unsigned integer is still a response this source cannot read.
7965        let number = match created_issue_number(created) {
7966            Ok(number) => number,
7967            Err(error) => {
7968                let _ = self.delete_issue(&content_id).await;
7969                return Err(error);
7970            }
7971        };
7972        let added = match self
7973            .graphql(
7974                graphql::ADD_TO_BOARD,
7975                json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
7976            )
7977            .await
7978        {
7979            Ok(added) => added,
7980            Err(error) => {
7981                let _ = self.delete_issue(&content_id).await;
7982                return Err(error);
7983            }
7984        };
7985        let item = added
7986            .pointer("/addProjectV2ItemById/item")
7987            .filter(|value| !value.is_null())
7988            .ok_or_else(|| SourceError::Malformed {
7989                message: "GitHub board addition returned no project item".into(),
7990            })?;
7991        Ok(Landed {
7992            content_id,
7993            item_id: required_str(item, "id")?.to_owned(),
7994            url,
7995            number,
7996        })
7997    }
7998
7999    /// Move one issue under the project it now belongs to, or out of the one it left.
8000    async fn reparent(
8001        &self,
8002        held: Option<NativeId>,
8003        child: &NativeId,
8004        wanted: Option<&NativeId>,
8005    ) -> Result<(), SourceError> {
8006        if held.as_ref() == wanted {
8007            return Ok(());
8008        }
8009        if let Some(held) = &held {
8010            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
8011                .await?;
8012        }
8013        if let Some(wanted) = wanted {
8014            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
8015                .await?;
8016        }
8017        Ok(())
8018    }
8019
8020    async fn sub_issue(
8021        &self,
8022        operation: &str,
8023        parent: &NativeId,
8024        child: &NativeId,
8025        root: &str,
8026    ) -> Result<(), SourceError> {
8027        let data = self
8028            .graphql(
8029                operation,
8030                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
8031            )
8032            .await?;
8033        let issue =
8034            data.pointer(&format!("/{root}/issue"))
8035                .ok_or_else(|| SourceError::Malformed {
8036                    message: "GitHub sub-issue update returned no issue".into(),
8037                })?;
8038        let sub =
8039            data.pointer(&format!("/{root}/subIssue"))
8040                .ok_or_else(|| SourceError::Malformed {
8041                    message: "GitHub sub-issue update returned no sub-issue".into(),
8042                })?;
8043        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
8044            return Err(SourceError::Malformed {
8045                message: "GitHub sub-issue update returned the wrong issues".into(),
8046            });
8047        }
8048        Ok(())
8049    }
8050
8051    /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
8052    /// whether there was one.
8053    ///
8054    /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
8055    /// relationships are not read: there is nothing a read of them could find.
8056    async fn reconcile_blocked_by(
8057        &self,
8058        content_id: &NativeId,
8059        native: &[String],
8060        issue: Issue<'_>,
8061    ) -> Result<bool, SourceError> {
8062        let current = match issue {
8063            Issue::Created => Vec::new(),
8064            Issue::Existing(Some(held)) => held
8065                .iter()
8066                .map(|far| required_str(far, "id").map(str::to_owned))
8067                .collect::<Result<Vec<_>, _>>()?,
8068            Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
8069        };
8070        let mut changed = false;
8071        for (operation, far_id) in current
8072            .iter()
8073            .filter(|id| !native.contains(id))
8074            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
8075            .chain(
8076                native
8077                    .iter()
8078                    .filter(|id| !current.contains(id))
8079                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
8080            )
8081        {
8082            let data = self
8083                .graphql(
8084                    operation,
8085                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
8086                )
8087                .await?;
8088            let root = if operation == graphql::ADD_BLOCKED_BY {
8089                "addBlockedBy"
8090            } else {
8091                "removeBlockedBy"
8092            };
8093            let issue =
8094                data.pointer(&format!("/{root}/issue"))
8095                    .ok_or_else(|| SourceError::Malformed {
8096                        message: "GitHub dependency update returned no issue".into(),
8097                    })?;
8098            let blocker = data
8099                .pointer(&format!("/{root}/blockingIssue"))
8100                .ok_or_else(|| SourceError::Malformed {
8101                    message: "GitHub dependency update returned no blocking issue".into(),
8102                })?;
8103            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
8104            {
8105                return Err(SourceError::Malformed {
8106                    message: "GitHub dependency update returned the wrong issues".into(),
8107                });
8108            }
8109            changed = true;
8110        }
8111        Ok(changed)
8112    }
8113}
8114
8115/// What a write needs to know of one far end it names: which kind of item it is, and whether
8116/// it is an issue a native relationship can name.
8117struct FarEnd {
8118    kind: BoardKind,
8119    content_kind: ContentKind,
8120}
8121
8122/// Whether the issue one write reconciles was created by that write or was already there.
8123#[derive(Clone, Copy, PartialEq, Eq)]
8124enum Issue<'a> {
8125    /// Created by this write, so it holds no relationships yet.
8126    Created,
8127    /// On the board before this write, holding whatever relationships it holds — the far
8128    /// ends of its whole `blockedBy`, when the read that reached it carried them.
8129    Existing(Option<&'a [Value]>),
8130}
8131
8132/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
8133enum Reached {
8134    /// An issue this board holds, resolved into everything this source reports about it.
8135    Held(Box<Resolved>),
8136    /// Nothing this board holds: no such node, or a node on some other board.
8137    Nothing,
8138    /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
8139    /// again by [`GitHubProjectsSource::draft_by_id`].
8140    Draft,
8141}
8142
8143/// What GitHub says when a string is not a node id it can resolve.
8144///
8145/// Matched because it is the ordinary answer to a project selector naming a project by its
8146/// *name*, and reporting that as a failure would make naming one impossible. It is read
8147/// off the refusal GitHub sent, never guessed from the shape of the string: this source
8148/// does not define the syntax of a GitHub node id and would be wrong about it.
8149const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
8150
8151/// `error` with `note` added to the end of what it says, its kind and every other member
8152/// unchanged — so a caller still branches on the failure that happened, and reads beside it
8153/// what that failure left behind.
8154fn noting(error: SourceError, note: &str) -> SourceError {
8155    match error {
8156        SourceError::Config { message } => SourceError::Config {
8157            message: message + note,
8158        },
8159        SourceError::Auth { message } => SourceError::Auth {
8160            message: message + note,
8161        },
8162        SourceError::Refused { message } => SourceError::Refused {
8163            message: message + note,
8164        },
8165        SourceError::RateLimited {
8166            retry_after_seconds,
8167            message,
8168        } => SourceError::RateLimited {
8169            retry_after_seconds,
8170            message: Some(message.unwrap_or_default() + note),
8171        },
8172        SourceError::Unavailable { message } => SourceError::Unavailable {
8173            message: message + note,
8174        },
8175        SourceError::Malformed { message } => SourceError::Malformed {
8176            message: message + note,
8177        },
8178    }
8179}
8180
8181/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
8182/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
8183/// for them.
8184///
8185/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
8186/// is read again at no added price.
8187fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
8188    let mut variables = serde_json::Map::new();
8189    for slot in 0..DETAIL_BATCH {
8190        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
8191        variables.insert(format!("id{slot}"), json!(id));
8192    }
8193    variables.insert(
8194        "first".to_owned(),
8195        json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
8196    );
8197    variables.insert("comments".to_owned(), json!(comments.is_some()));
8198    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
8199    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
8200    variables.insert("duplicates".to_owned(), json!(true));
8201    Value::Object(variables)
8202}
8203
8204/// Whether this refusal is GitHub saying the id names no node at all.
8205fn unresolvable_node(error: &SourceError) -> bool {
8206    matches!(error, SourceError::Refused { message }
8207        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
8208}
8209
8210/// One project name, as a search qualifier which filters on it at the server.
8211///
8212/// Quoted so the whole title is one phrase rather than a bag of words, with the two
8213/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
8214/// the way it documents. A title matched here is still compared for equality afterwards:
8215/// the qualifier narrows what the server sends, and this source decides what it names.
8216fn title_qualifier(name: &str) -> String {
8217    format!("in:title {}", quoted(name))
8218}
8219
8220/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
8221/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
8222/// it documents — so a value holding a qualifier's spelling is searched for rather than
8223/// obeyed.
8224fn quoted(value: &str) -> String {
8225    let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
8226    format!("\"{escaped}\"")
8227}
8228
8229/// The search qualifier for the issues updated at or after `since`.
8230///
8231/// Written to the second, rounded down, which can only widen what the search returns.
8232fn updated_qualifier(since: DateTime<Utc>) -> String {
8233    format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
8234}
8235
8236/// The search terms that narrow a board-scoped issue search to a task query's text and
8237/// metadata predicates, or `None` when it carries neither.
8238///
8239/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
8240/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
8241/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
8242/// matches each in any field the `in:` qualifier names, so a query naming a title search and
8243/// a metadata value searches both fields for both — wider than asked, never narrower, and
8244/// every candidate is confirmed in process afterwards.
8245///
8246/// **This narrows a text search, and that is this source's declared semantics.** GitHub
8247/// matches whole tokens where a substring rule would match inside a word, so an item holding
8248/// the text only inside a longer word is not returned. A text of nothing but whitespace
8249/// matches every item, so it narrows nothing and is not sent.
8250fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
8251    let text = query
8252        .text
8253        .as_ref()
8254        .filter(|text| !text.terms.trim().is_empty());
8255    if text.is_none() && query.metadata.is_empty() {
8256        return None;
8257    }
8258    let (title, body) = match text.map(|text| text.fields) {
8259        None => (false, true),
8260        Some(TextFields::Title) => (true, !query.metadata.is_empty()),
8261        Some(TextFields::Content) => (false, true),
8262        Some(TextFields::TitleOrContent) => (true, true),
8263    };
8264    let fields = match (title, body) {
8265        (true, true) => "in:title,body",
8266        (true, false) => "in:title",
8267        _ => "in:body",
8268    };
8269    let phrases = text
8270        .map(|text| text.terms.clone())
8271        .into_iter()
8272        .chain(
8273            query
8274                .metadata
8275                .iter()
8276                .map(|wanted| as_stored(wanted.value())),
8277        )
8278        .map(|phrase| quoted(&phrase))
8279        .collect::<Vec<_>>();
8280    Some(format!("{fields} {}", phrases.join(" ")))
8281}
8282
8283/// The search terms that narrow a board-scoped issue search to a project or document query's
8284/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
8285/// carrying that text alone is sent as by [`narrowing_qualifiers`].
8286fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
8287    narrowing_qualifiers(&TaskQuery {
8288        text: text.cloned(),
8289        ..TaskQuery::default()
8290    })
8291}
8292
8293/// Refuses a project or document query's text GitHub's issue search cannot find, before
8294/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
8295/// query's.
8296fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
8297    refuse_unsearchable(&TaskQuery {
8298        text: text.cloned(),
8299        ..TaskQuery::default()
8300    })
8301}
8302
8303/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
8304/// before anything is asked of GitHub.
8305///
8306/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
8307/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
8308/// left out, the search is every issue of the board. So this source says it cannot answer
8309/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
8310/// nothing GitHub could search for, and keeps the board read it always had.
8311fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
8312    const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
8313                       letter or digit with a bounded query";
8314    if let Some(text) = &query.text
8315        && !text.terms.trim().is_empty()
8316        && !has_words(&text.terms)
8317    {
8318        return Err(SourceError::Refused {
8319            message: format!(
8320                "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
8321                text.terms
8322            ),
8323        });
8324    }
8325    if let Some(wanted) = query
8326        .metadata
8327        .iter()
8328        .find(|wanted| !has_words(wanted.value()))
8329    {
8330        return Err(SourceError::Refused {
8331            message: format!(
8332                "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
8333                wanted.value(),
8334                std::iter::once(wanted.key())
8335                    .chain(wanted.path().iter().map(String::as_str))
8336                    .collect::<Vec<_>>()
8337                    .join("/"),
8338            ),
8339        });
8340    }
8341    Ok(())
8342}
8343
8344/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
8345fn has_words(phrase: &str) -> bool {
8346    phrase.chars().any(char::is_alphanumeric)
8347}
8348
8349/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
8350///
8351/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
8352/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
8353/// which GitHub's word match would read as different words.
8354fn as_stored(value: &str) -> String {
8355    let encoded = Value::String(value.to_owned()).to_string();
8356    encoded[1..encoded.len() - 1].to_owned()
8357}
8358
8359/// The one narrower question a task query carrying a text, metadata or origin predicate is
8360/// sent as.
8361enum Narrowing {
8362    /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
8363    Origin(String),
8364    /// The board-scoped issue search narrowed by these qualifiers.
8365    Search(String),
8366}
8367
8368impl Narrowing {
8369    /// What this question is remembered under for the length of one command.
8370    fn key(&self) -> String {
8371        match self {
8372            Self::Origin(origin) => format!("origin {origin}"),
8373            Self::Search(also) => format!("search {also}"),
8374        }
8375    }
8376}
8377
8378/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
8379enum Resumed {
8380    /// It reported another page, which starts after this cursor.
8381    More(String),
8382    /// It has ended. Sending this cursor again — the page's own end when it had one, and
8383    /// otherwise the cursor it was reached from — answers an empty page, so the one document
8384    /// can go on walking the other connection.
8385    Ended(Option<String>),
8386}
8387
8388impl Resumed {
8389    /// Whether the connection has another page.
8390    const fn has_more(&self) -> bool {
8391        matches!(self, Self::More(_))
8392    }
8393
8394    /// The cursor to send this connection next.
8395    fn cursor(self) -> Option<String> {
8396        match self {
8397            Self::More(next) => Some(next),
8398            Self::Ended(last) => last,
8399        }
8400    }
8401}
8402
8403/// Where `connection`, reached from `after`, resumes — refused when it reports another page
8404/// with no cursor to it, or from a cursor that does not advance.
8405fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
8406    let info = connection
8407        .get("pageInfo")
8408        .ok_or_else(|| SourceError::Malformed {
8409            message: "GitHub connection has no pageInfo".into(),
8410        })?;
8411    let end = optional_str(info, "endCursor")?;
8412    if required_bool(info, "hasNextPage")? {
8413        let next = end.ok_or_else(|| SourceError::Malformed {
8414            message: "GitHub connection reports another page and no endCursor".into(),
8415        })?;
8416        validate_cursor_progress(after, next)?;
8417        return Ok(Resumed::More(next.to_owned()));
8418    }
8419    Ok(Resumed::Ended(
8420        end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
8421    ))
8422}
8423
8424/// The board, and every item on it this source reports.
8425#[derive(Clone)]
8426struct Board {
8427    id: String,
8428    fields: Value,
8429    items: Vec<Resolved>,
8430}
8431
8432/// What a write needs of the board and nothing more: its node id and its field
8433/// definitions, in the shape a read of the board's own `fields` gives them.
8434///
8435/// Deliberately no items. A write decides which item it writes, which parent it files
8436/// under and which far ends it names by reading each of them by its own id; this is the
8437/// half of the board those reads cannot carry, and holding no item is what keeps it from
8438/// ever being asked whether an item is there.
8439#[derive(Clone)]
8440struct BoardFields {
8441    id: BoardId,
8442    fields: Value,
8443}
8444
8445/// A board's node id: what a field write and `addProjectV2ItemById` address.
8446///
8447/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
8448/// refused where it is read, and one an item names blank is read as not named at all.
8449#[derive(Clone)]
8450struct BoardId(String);
8451
8452/// Where one write left its item, for the record the rest of the command reads it out of.
8453///
8454/// A named record rather than a tuple because the update arm and the create arm each fill
8455/// all four, and two `Option`s of different meaning side by side in a tuple are two
8456/// positions a reader has to count.
8457struct Landed {
8458    /// The issue's own node id, which is the [`NativeId`] this source reports.
8459    content_id: NativeId,
8460    /// The board item's id, which is what a field write addresses.
8461    // 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.
8462    item_id: String,
8463    /// The web address GitHub gave the issue, when it gave one.
8464    // 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.
8465    url: Option<String>,
8466    /// The issue's number on its repository, when GitHub reported one.
8467    number: Option<u64>,
8468}
8469
8470impl BoardId {
8471    fn parse(id: &str) -> Result<Self, SourceError> {
8472        if id.trim().is_empty() {
8473            return Err(SourceError::Malformed {
8474                message: "GitHub named a board with a blank node id".into(),
8475            });
8476        }
8477        Ok(Self(id.to_owned()))
8478    }
8479
8480    fn as_str(&self) -> &str {
8481        &self.0
8482    }
8483}
8484
8485impl Board {
8486    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
8487        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
8488        let nodes = fields
8489            .get("nodes")
8490            .and_then(Value::as_array)
8491            .ok_or_else(|| SourceError::Malformed {
8492                message: "GitHub project fields.nodes is not an array".into(),
8493            })?;
8494        Ok(nodes
8495            .iter()
8496            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
8497    }
8498}
8499
8500/// One board item, resolved into everything this source reports about it.
8501#[derive(Clone)]
8502struct Resolved {
8503    item_id: String,
8504    id: NativeId,
8505    content_kind: ContentKind,
8506    kind: BoardKind,
8507    title: String,
8508    body: Option<String>,
8509    /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
8510    /// that changes the slot alone has to keep byte for byte outside it.
8511    raw_body: Option<String>,
8512    status: Status,
8513    /// The name of the board `Status` option this item sits in, as the board spells it.
8514    option: Option<String>,
8515    /// What its `Priority` field says, read through this instance's mapping.
8516    priority: HeldPriority,
8517    /// Whether this item's issue is closed. A draft has no such state and is never closed.
8518    closed: bool,
8519    /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
8520    delivers: Vec<TaskRef>,
8521    /// Every task that delivers this one, read out of its slot. Empty for anything not a
8522    /// task.
8523    delivered_by: Vec<TaskRef>,
8524    labels: Vec<Label>,
8525    parent: Option<NativeId>,
8526    // 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.
8527    origin: Option<String>,
8528    /// The issue's own number on its repository, as GitHub reports it.
8529    ///
8530    /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
8531    /// declares none, and a draft is not filed in a repository to be numbered by one — and
8532    /// an issue this run created whose creating mutation answered without one, which is a
8533    /// response GitHub's own schema says cannot happen and which a landed write is not
8534    /// worth failing over. An `Issue` read off the board always has one.
8535    number: Option<u64>,
8536    url: Option<String>,
8537    created_at: Option<DateTime<Utc>>,
8538    updated_at: Option<DateTime<Utc>>,
8539    own_repository: Option<Repository>,
8540    repositories: Vec<Repository>,
8541    slot: BTreeMap<String, Value>,
8542    /// The node id of the board this item sits on, when the read that reached it said.
8543    board_id: Option<String>,
8544    /// The definition of every board field this item holds a value of, in the shape a read
8545    /// of the board's own `fields` gives one.
8546    ///
8547    /// Only the fields this item has a value in: a field it holds nothing of is not here,
8548    /// which says nothing about whether the board has it.
8549    fields: Vec<Value>,
8550    /// Every field the board this item sits on defines, as its own read of the board's
8551    /// `fields` gives them — when the read that reached the item carried them, which a read
8552    /// of it by its own id does. What a write of it needs of the board, then, needs no read
8553    /// of the board.
8554    board_fields: Option<Value>,
8555    /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
8556    /// selects one — when the read that reached it carried the connection to its end, which a
8557    /// read of it by its own id does for any issue blocked by no more than a page. What a
8558    /// write reconciles that relationship against, and what a read of its forward edges in
8559    /// the same command answers with.
8560    blocked_by: Option<Vec<Value>>,
8561}
8562
8563impl Resolved {
8564    /// The board this item's own read names it on, when that read named one this source can
8565    /// address.
8566    fn named_board(&self) -> Option<BoardId> {
8567        self.board_id
8568            .as_deref()
8569            .and_then(|id| BoardId::parse(id).ok())
8570    }
8571
8572    /// The board's id and every field it defines, when the read that reached this item
8573    /// carried both — which a read of it by its own id does.
8574    fn carried_board(&self) -> Option<BoardFields> {
8575        Some(BoardFields {
8576            id: self.named_board()?,
8577            fields: self.board_fields.clone()?,
8578        })
8579    }
8580
8581    /// Whether this item holds a value of the board field called `name`, and so carries
8582    /// that field's definition. `false` says nothing about whether the board has the field.
8583    fn defines(&self, name: &str) -> bool {
8584        self.fields
8585            .iter()
8586            .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
8587    }
8588
8589    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
8590    /// in a field of its own, and none of the five keys that are only an encoding.
8591    ///
8592    /// The two delivery keys are left out for every kind, not only for a task: they are
8593    /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
8594    /// document carrying one holds nothing a caller's own metadata could mean by it.
8595    fn metadata(&self) -> BTreeMap<String, Value> {
8596        let mut metadata = self.slot.clone();
8597        metadata.remove(Repository::METADATA_KEY);
8598        metadata.remove(DependencyEdge::RECORDED_KEY);
8599        metadata.remove(ItemKind::METADATA_KEY);
8600        metadata.remove(TaskRef::DELIVERS_KEY);
8601        metadata.remove(TaskRef::DELIVERED_BY_KEY);
8602        // The board field is the origin, and the body's copy of it is only a mirror for the
8603        // issue search to find: an item whose field holds none has none, whatever its body
8604        // says, so no reader ever sees two answers.
8605        metadata.remove(ORIGIN_KEY);
8606        if let Some(origin) = &self.origin {
8607            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
8608        }
8609        metadata
8610    }
8611
8612    /// Where this item is, as a link a reader can open.
8613    ///
8614    /// A board is a hosted place and every issue on it has a web address, so that address
8615    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
8616    /// of place it is, so a reader knows to open it rather than to read a file out. It
8617    /// does not replace or derive from `url`: the field goes on reporting exactly what it
8618    /// reported before, and this says what that address *is*.
8619    ///
8620    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
8621    /// rather than a third variant, which is the contract's "the source did not say". An
8622    /// issue this run created is not one of those: its address comes back from the
8623    /// creating mutation, so it is somewhere a reader can open from the moment it exists
8624    /// rather than from whenever the board read catches up.
8625    fn location(&self) -> Option<Location> {
8626        self.url.clone().map(Location::Url)
8627    }
8628
8629    /// The short handle this board's backend shows people for a task: the issue's number
8630    /// alone, as a decimal string.
8631    ///
8632    /// The number alone rather than `owner/repo#1043`, because that is the contract's
8633    /// value for this backend. A draft has no number and so no handle, which is the
8634    /// contract's *absent* rather than a handle of some other shape — and the native
8635    /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
8636    /// derives from.
8637    fn key(&self) -> Option<String> {
8638        self.number.map(|number| number.to_string())
8639    }
8640
8641    /// Whether its `Priority` field holds a value at all, mapped or not.
8642    fn holds_priority(&self) -> bool {
8643        self.priority != HeldPriority::Read(Priority::None)
8644    }
8645
8646    /// The task this item is.
8647    ///
8648    /// Fails for an item whose `Priority` field holds an option the mapping does not name:
8649    /// reading that as a level would be a guess, and reading it as `none` would let the next
8650    /// copy clear a priority a person set.
8651    fn task(&self) -> Result<Task, SourceError> {
8652        let priority = match &self.priority {
8653            HeldPriority::Read(priority) => *priority,
8654            HeldPriority::Unmapped(option) => {
8655                return Err(SourceError::Malformed {
8656                    message: format!(
8657                        "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
8658                         this source's priority_mapping does not name, so its priority cannot be \
8659                         read; next: name {option:?} under priority_mapping, or move the item to \
8660                         a mapped option",
8661                        self.id,
8662                        self.number
8663                            .map(|number| format!(" (#{number})"))
8664                            .unwrap_or_default()
8665                    ),
8666                });
8667            }
8668        };
8669        Ok(Task {
8670            id: self.id.clone(),
8671            key: self.key(),
8672            title: self.title.clone(),
8673            content: self.body.clone(),
8674            status: self.status.clone(),
8675            priority,
8676            labels: self.labels.clone(),
8677            project: self.parent.clone(),
8678            url: self.url.clone(),
8679            location: self.location(),
8680            created_at: self.created_at,
8681            updated_at: self.updated_at,
8682            metadata: self.metadata(),
8683            repositories: self.repositories.clone(),
8684            delivers: self.delivers.clone(),
8685            delivered_by: self.delivered_by.clone(),
8686        })
8687    }
8688
8689    fn project(&self) -> Project {
8690        Project {
8691            id: self.id.clone(),
8692            title: self.title.clone(),
8693            content: self.body.clone(),
8694            status: self.status.clone(),
8695            labels: self.labels.clone(),
8696            url: self.url.clone(),
8697            location: self.location(),
8698            created_at: self.created_at,
8699            updated_at: self.updated_at,
8700            metadata: self.metadata(),
8701            repositories: self.repositories.clone(),
8702        }
8703    }
8704
8705    /// The same issue as a document: the project it is filed under, and no status and no
8706    /// dependencies, because a document is not work.
8707    fn document(&self) -> Document {
8708        Document {
8709            id: self.id.clone(),
8710            title: self.title.clone(),
8711            content: self.body.clone(),
8712            project: self.parent.clone(),
8713            labels: self.labels.clone(),
8714            url: self.url.clone(),
8715            location: self.location(),
8716            created_at: self.created_at,
8717            updated_at: self.updated_at,
8718            metadata: self.metadata(),
8719            repositories: self.repositories.clone(),
8720        }
8721    }
8722}
8723
8724/// Where one targeted update moves an item's status, and which of its two halves move.
8725struct StatusMove {
8726    /// The board the item's `Status` field is on.
8727    board: BoardId,
8728    /// The `Status` field's id.
8729    field: String,
8730    /// The option's id.
8731    option: String,
8732    /// The option's name, as the board spells it.
8733    name: String,
8734    /// What the status asks of the issue's state.
8735    target: StatusTarget,
8736    /// The status the item reads as once it is there.
8737    landed: Status,
8738    /// Which of the status's two halves differ from what the item holds.
8739    moves: Moves,
8740}
8741
8742/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
8743/// closed state of its issue, or both. A status neither half of which differs is no move at all,
8744/// and is not a value of this type.
8745#[derive(Clone, Copy, PartialEq, Eq)]
8746enum Moves {
8747    /// The option alone.
8748    Option,
8749    /// The issue's state alone: open, closed, or closed with another reason.
8750    State,
8751    /// Both.
8752    Both,
8753}
8754
8755impl Moves {
8756    /// What differs, or `None` when nothing does.
8757    const fn of(option: bool, state: bool) -> Option<Self> {
8758        match (option, state) {
8759            (true, true) => Some(Self::Both),
8760            (true, false) => Some(Self::Option),
8761            (false, true) => Some(Self::State),
8762            (false, false) => None,
8763        }
8764    }
8765
8766    /// Whether the option moves.
8767    const fn option(self) -> bool {
8768        matches!(self, Self::Option | Self::Both)
8769    }
8770
8771    /// Whether the issue's state moves.
8772    const fn state(self) -> bool {
8773        matches!(self, Self::State | Self::Both)
8774    }
8775}
8776
8777/// What one write is, and the status that comes with being it.
8778///
8779/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
8780/// status and a task or a project always has one, so "a document carrying a status" and
8781/// "a task carrying none" are states a write cannot be in rather than states every use
8782/// site below has to defend against.
8783enum Written<'a> {
8784    /// A document, which is not work and so has no status at all.
8785    Document,
8786    /// A task or a project, and the status it is being written with.
8787    Work(ItemKind, &'a Status),
8788}
8789
8790impl Written<'_> {
8791    /// Which of the board's three kinds this write is.
8792    const fn kind(&self) -> BoardKind {
8793        match self {
8794            Self::Document => BoardKind::Document,
8795            Self::Work(kind, _) => BoardKind::Work(*kind),
8796        }
8797    }
8798
8799    /// The status this write carries. A document carries none, so a write of one says
8800    /// nothing about the issue's open or closed state and selects no board `Status`
8801    /// option.
8802    const fn status(&self) -> Option<&Status> {
8803        match self {
8804            Self::Document => None,
8805            Self::Work(_, status) => Some(status),
8806        }
8807    }
8808
8809    /// The status this write carries with the kind whose half of `status_mapping` it is
8810    /// written through.
8811    const fn work_status(&self) -> Option<(ItemKind, &Status)> {
8812        match self {
8813            Self::Document => None,
8814            Self::Work(kind, status) => Some((*kind, status)),
8815        }
8816    }
8817}
8818
8819/// The item being written, in the one shape all three write methods reach.
8820struct Incoming<'a> {
8821    written: Written<'a>,
8822    /// The title a person wrote. A document's goes onto the issue with
8823    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
8824    title: &'a str,
8825    content: Option<&'a str>,
8826    assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
8827    labels: &'a [Label],
8828    metadata: &'a BTreeMap<String, Value>,
8829    repositories: &'a [Repository],
8830    parent: Option<&'a NativeId>,
8831    /// [`Task::delivers`], already checked. Empty for a project or a document, which is
8832    /// what keeps either key out of their slot.
8833    delivers: &'a [TaskRef],
8834    /// [`Task::delivered_by`], already checked. Empty for a project or a document.
8835    delivered_by: &'a [TaskRef],
8836    /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
8837    /// project, a document, and every write to an instance with no `priority_mapping` —
8838    /// which is what keeps such a write's requests exactly what they were before.
8839    priority: Option<Priority>,
8840}
8841
8842/// What one write does to an item's `Priority` field.
8843enum PriorityWrite {
8844    /// Select this option of this field.
8845    Select {
8846        /// The `Priority` field's id.
8847        field: String,
8848        /// The mapped option's id.
8849        option: String,
8850    },
8851    /// Clear the field's value, which is what `none` is.
8852    Clear {
8853        /// The `Priority` field's id.
8854        field: String,
8855    },
8856}
8857
8858impl Incoming<'_> {
8859    /// The title this write puts on the issue.
8860    fn written_title(&self) -> String {
8861        match self.written {
8862            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
8863            Written::Work(..) => self.title.to_owned(),
8864        }
8865    }
8866}
8867
8868#[derive(Clone, Copy, PartialEq, Eq)]
8869enum ContentKind {
8870    DraftIssue,
8871    Issue,
8872}
8873
8874/// What one board issue is: a document, or the work an [`ItemKind`] names.
8875///
8876/// A type of this source's own rather than an `ItemKind` with a third variant, because
8877/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
8878/// document — the contract keeps a document out of that enum deliberately. Holding the
8879/// board's three answers in one value is what makes every place that asks "which is this?"
8880/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
8881/// two thirds of the board.
8882#[derive(Clone, Copy, PartialEq, Eq)]
8883enum BoardKind {
8884    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
8885    Document,
8886    /// Every other issue, and every draft.
8887    Work(ItemKind),
8888}
8889
8890impl BoardKind {
8891    /// Whose half of `status_mapping` an item of this kind reads its status through. A
8892    /// document has no status of its own, so the task half stands in for whatever the issue
8893    /// holds; nothing reports it.
8894    const fn status_kind(self) -> ItemKind {
8895        match self {
8896            Self::Document => ItemKind::Task,
8897            Self::Work(kind) => kind,
8898        }
8899    }
8900
8901    /// How a refusal names this kind to the person reading it.
8902    const fn describes(self) -> &'static str {
8903        match self {
8904            Self::Document => "document",
8905            Self::Work(kind) => kind.marker(),
8906        }
8907    }
8908}
8909
8910/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
8911///
8912/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
8913/// the shared cross-source journeys assert one answer to one question, so two sources
8914/// that disagree about what "carries the label bug" means fail them.
8915fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
8916    let holds = |name: &String| {
8917        labels
8918            .iter()
8919            .any(|label| label.name.eq_ignore_ascii_case(name))
8920    };
8921    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
8922        && filter.all_of.iter().all(holds)
8923        && !filter.none_of.iter().any(holds)
8924}
8925
8926/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
8927/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
8928fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
8929    statuses.is_empty() || statuses.contains(&category)
8930}
8931
8932/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
8933///
8934/// `content` is the item's own prose — the body with this source's trailing metadata
8935/// comment already taken off — so a search never matches an encoding the author of the
8936/// issue never wrote.
8937fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
8938    let terms = query.terms.to_lowercase();
8939    let in_title = title.to_lowercase().contains(&terms);
8940    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
8941    match query.fields {
8942        TextFields::Title => in_title,
8943        TextFields::Content => in_content,
8944        TextFields::TitleOrContent => in_title || in_content,
8945    }
8946}
8947
8948/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
8949///
8950/// The project predicate is passed separately because a read narrowed to one project has
8951/// already answered it by asking *that project* for its own items — and re-applying it
8952/// there would compare the caller's selector, which may be a project's **name**, against
8953/// the id of the project that name resolved to, and keep nothing. Every other read passes
8954/// `query.project` and applies it here, which is what keeps `projects` a predicate this
8955/// source really does apply.
8956fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
8957    labels_match(&task.labels, &query.labels)
8958        && status_matches(task.status.category, &query.statuses)
8959        && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
8960        && match project {
8961            ProjectFilter::Any => true,
8962            ProjectFilter::Orphans => task.project.is_none(),
8963            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
8964        }
8965        && query
8966            .text
8967            .as_ref()
8968            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
8969        // Against the parsed metadata slot, and against the origin field, which is where
8970        // `Resolved::metadata` reads each of them from.
8971        && query.metadata_matches(&task.metadata)
8972        && query.origin_matches(&task.metadata)
8973}
8974
8975fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
8976    labels_match(&project.labels, &query.labels)
8977        && status_matches(project.status.category, &query.statuses)
8978        && query
8979            .text
8980            .as_ref()
8981            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
8982}
8983
8984/// The same three predicates a task query carries, minus the status filter.
8985///
8986/// A document is not work, so it has no status for one to compare against and the query
8987/// type carries none. The project predicate is the same one — a design issue filed under a
8988/// project issue is in that project, and one filed under nothing is in none — so it is
8989/// spelled the same way here rather than answered differently.
8990fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
8991    labels_match(&document.labels, &query.labels)
8992        && match project {
8993            ProjectFilter::Any => true,
8994            ProjectFilter::Orphans => document.project.is_none(),
8995            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
8996        }
8997        && query
8998            .text
8999            .as_ref()
9000            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
9001}
9002
9003#[async_trait::async_trait]
9004impl TaskSource for GitHubProjectsSource {
9005    fn kind(&self) -> &'static str {
9006        KIND
9007    }
9008    fn capabilities(&self) -> Capabilities {
9009        Capabilities {
9010            projects: Support::Native,
9011            documents: Support::Native,
9012            comments: Support::Native,
9013            assets: Support::Native,
9014            priority: if self.priorities.is_some() {
9015                Support::Native
9016            } else {
9017                Support::Unsupported
9018            },
9019            filter_by_priority: Support::Native,
9020            filter_by_comment_activity: Support::Native,
9021            filter_by_metadata: Support::Native,
9022            filter_by_origin: Support::Native,
9023            orphan_tasks: Support::Native,
9024            filter_by_label: Support::Native,
9025            filter_by_status: Support::Native,
9026            search_title: Support::Native,
9027            search_content: Support::Native,
9028            task_dependencies: DependencySupport::BothDirections,
9029            project_dependencies: DependencySupport::BothDirections,
9030            max_page_size: MAX_PAGE_SIZE,
9031        }
9032    }
9033    async fn health(&self) -> Result<Health, SourceError> {
9034        let board = self.board_page(None, 1).await?;
9035        Ok(Health {
9036            reachable: true,
9037            detail: Some(format!(
9038                "reading GitHub project {}/{} ({})",
9039                self.owner,
9040                self.project_number,
9041                required_str(&board, "title")?
9042            )),
9043        })
9044    }
9045    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
9046        self.item_by_id(id)
9047            .await?
9048            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
9049            .map(|item| item.task())
9050            .transpose()
9051    }
9052    async fn task_assets(
9053        &self,
9054        id: &NativeId,
9055    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
9056        self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
9057    }
9058    async fn task_asset(
9059        &self,
9060        id: &NativeId,
9061        name: &onetaskgraph_plugin_api::AssetName,
9062    ) -> Result<Option<Vec<u8>>, SourceError> {
9063        self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
9064            .await
9065    }
9066    async fn set_task_rendering_with_assets(
9067        &self,
9068        id: &NativeId,
9069        content: &str,
9070        provenance: &Value,
9071        _answers: &BTreeMap<String, Value>,
9072        assets: &onetaskgraph_plugin_api::AssetWrite,
9073    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
9074        self.replace_rendering(
9075            id,
9076            BoardKind::Work(ItemKind::Task),
9077            content,
9078            provenance,
9079            Some(assets),
9080        )
9081        .await
9082    }
9083    async fn document_assets(
9084        &self,
9085        id: &NativeId,
9086    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
9087        self.held_assets(id, BoardKind::Document).await
9088    }
9089    async fn document_asset(
9090        &self,
9091        id: &NativeId,
9092        name: &onetaskgraph_plugin_api::AssetName,
9093    ) -> Result<Option<Vec<u8>>, SourceError> {
9094        self.held_asset(id, BoardKind::Document, name).await
9095    }
9096    async fn set_document_rendering_with_assets(
9097        &self,
9098        id: &NativeId,
9099        content: &str,
9100        provenance: &Value,
9101        _answers: &BTreeMap<String, Value>,
9102        assets: &onetaskgraph_plugin_api::AssetWrite,
9103    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
9104        self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
9105            .await
9106    }
9107    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
9108        Ok(self
9109            .item_by_id(id)
9110            .await?
9111            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
9112            .map(|item| item.project()))
9113    }
9114    async fn query_tasks(
9115        &self,
9116        query: &TaskQuery,
9117        page: &PageRequest,
9118    ) -> Result<Page<Task>, SourceError> {
9119        validate_page(page)?;
9120        refuse_unsearchable(query)?;
9121        if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
9122            let qualifiers = match (narrowing_qualifiers(query), query.commented_since) {
9123                (Some(also), Some(since)) => Some(format!("{} {also}", updated_qualifier(since))),
9124                (Some(also), None) => Some(also),
9125                (None, Some(since)) => Some(updated_qualifier(since)),
9126                (None, None) => None,
9127            };
9128            if let Some(also) = qualifiers {
9129                return self.search_tasks(query, page, &also).await;
9130            }
9131        }
9132
9133        // A read narrowed to one project asks that project for its own tasks, so nothing
9134        // about it costs what the rest of the board holds. A read carrying a text, metadata
9135        // or origin predicate asks GitHub the narrower question those predicates are, and a
9136        // read narrowed to comment activity alone asks the board's own issue search for the
9137        // issues updated since, which is every issue a comment could have been written or
9138        // edited on since. Every other task read is a question about the whole board and is
9139        // answered by reading it.
9140        let (held, membership) = match (&query.project, query.commented_since) {
9141            (ProjectFilter::Is(project), _) => (
9142                self.project_children(project).await?,
9143                // Answered by where these items came from; see `task_matches`.
9144                &ProjectFilter::Any,
9145            ),
9146            (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
9147                match (self.narrowed(query).await?, since) {
9148                    (Some(narrowed), _) => (narrowed, &query.project),
9149                    (None, Some(since)) => (self.updated_since(since).await?, &query.project),
9150                    (None, None) => (self.board().await?.items, &query.project),
9151                }
9152            }
9153        };
9154        // Filtered before paged: a page of a filtered result is a page of the survivors,
9155        // never the survivors of a page.
9156        let mut tasks = Vec::new();
9157        for item in held
9158            .iter()
9159            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
9160        {
9161            let task = item.task()?;
9162            if task_matches(&task, query, membership)
9163                && self.commented_since(item, query.commented_since).await?
9164            {
9165                tasks.push(task);
9166            }
9167        }
9168        Ok(offset_page(
9169            tasks,
9170            numeric_cursor(page.cursor.as_ref())?,
9171            page.limit.min(MAX_PAGE_SIZE) as usize,
9172        ))
9173    }
9174    async fn query_projects(
9175        &self,
9176        query: &ProjectQuery,
9177        page: &PageRequest,
9178    ) -> Result<Page<Project>, SourceError> {
9179        validate_page(page)?;
9180        refuse_unsearchable_text(query.text.as_ref())?;
9181        // The projects a board holds are found by an issue search scoped to that board,
9182        // never by walking the board's own item connection: what tells a project from a
9183        // task is the `parent` each issue carries, which costs nothing to read. A query
9184        // carrying a text asks that search for the text too, so it reads the issues that
9185        // hold it rather than every issue of the board.
9186        let held = match self.text_searched(query.text.as_ref()).await? {
9187            Some(searched) => searched,
9188            None => self.board_issues().await?,
9189        };
9190        let projects = held
9191            .iter()
9192            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
9193            .map(Resolved::project)
9194            .filter(|project| project_matches(project, query))
9195            .collect();
9196        Ok(offset_page(
9197            projects,
9198            numeric_cursor(page.cursor.as_ref())?,
9199            page.limit.min(MAX_PAGE_SIZE) as usize,
9200        ))
9201    }
9202    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
9203        Ok(self
9204            .item_by_id(id)
9205            .await?
9206            .filter(|item| item.kind == BoardKind::Document)
9207            .map(|item| item.document()))
9208    }
9209    async fn query_documents(
9210        &self,
9211        query: &DocumentQuery,
9212        page: &PageRequest,
9213    ) -> Result<Page<Document>, SourceError> {
9214        validate_page(page)?;
9215        // Narrowed to one project, this is the same sub-issue read a task list scoped to
9216        // that project makes — a document filed under a project is a sub-issue of it too,
9217        // and which of them come back is the kind this caller asked for. Unscoped, a query
9218        // carrying a text asks the board-scoped issue search for it, as a task query does,
9219        // and only one carrying none reads the board.
9220        let (held, membership) = match &query.project {
9221            ProjectFilter::Is(project) => (
9222                self.project_children(project).await?,
9223                // Answered by where these items came from; see `task_matches`.
9224                &ProjectFilter::Any,
9225            ),
9226            ProjectFilter::Any | ProjectFilter::Orphans => {
9227                refuse_unsearchable_text(query.text.as_ref())?;
9228                match self.text_searched(query.text.as_ref()).await? {
9229                    Some(searched) => (searched, &query.project),
9230                    None => (self.board().await?.items, &query.project),
9231                }
9232            }
9233        };
9234        // Filtered before paged, exactly as a task read is: a page of a filtered result is
9235        // a page of the survivors, never the survivors of a page.
9236        let documents = held
9237            .iter()
9238            .filter(|item| item.kind == BoardKind::Document)
9239            .map(Resolved::document)
9240            .filter(|document| document_matches(document, query, membership))
9241            .collect();
9242        Ok(offset_page(
9243            documents,
9244            numeric_cursor(page.cursor.as_ref())?,
9245            page.limit.min(MAX_PAGE_SIZE) as usize,
9246        ))
9247    }
9248    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
9249        validate_page(page)?;
9250        let offset = numeric_cursor(page.cursor.as_ref())?;
9251        let mut labels = self
9252            .board()
9253            .await?
9254            .items
9255            .into_iter()
9256            .flat_map(|item| item.labels)
9257            .fold(Vec::new(), |mut all, label| {
9258                if !all.iter().any(|x: &Label| x.id == label.id) {
9259                    all.push(label);
9260                }
9261                all
9262            });
9263        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
9264        Ok(offset_page(
9265            labels,
9266            offset,
9267            page.limit.min(MAX_PAGE_SIZE) as usize,
9268        ))
9269    }
9270    async fn task_dependencies(
9271        &self,
9272        id: &NativeId,
9273        direction: Direction,
9274        page: &PageRequest,
9275    ) -> Result<Page<DependencyEdge>, SourceError> {
9276        self.dependencies(id, ItemKind::Task, direction, page).await
9277    }
9278    async fn project_dependencies(
9279        &self,
9280        id: &NativeId,
9281        direction: Direction,
9282        page: &PageRequest,
9283    ) -> Result<Page<DependencyEdge>, SourceError> {
9284        self.dependencies(id, ItemKind::Project, direction, page)
9285            .await
9286    }
9287
9288    fn writes(&self) -> WriteSupport {
9289        WriteSupport::Supported
9290    }
9291
9292    /// Create or update one task.
9293    ///
9294    /// Its `delivers` and `delivered_by` are checked before anything is read or written —
9295    /// neither may name the task itself or name one task twice — and land in the body's
9296    /// metadata slot under their reserved keys, in place of any caller metadata of those
9297    /// names.
9298    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
9299        self.write_task_assets(write, None)
9300            .await
9301            .map(|written| written.id)
9302    }
9303
9304    async fn write_task_with_assets(
9305        &self,
9306        write: &ItemWrite<Task>,
9307        _answers: Option<&BTreeMap<String, Value>>,
9308        assets: &onetaskgraph_plugin_api::AssetWrite,
9309    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9310        self.write_task_assets(write, Some(assets)).await
9311    }
9312
9313    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
9314        self.write_item(
9315            &Incoming {
9316                written: Written::Work(ItemKind::Project, &write.item.status),
9317                title: &write.item.title,
9318                content: write.item.content.as_deref(),
9319                assets: None,
9320                labels: &write.item.labels,
9321                metadata: &write.item.metadata,
9322                repositories: &write.item.repositories,
9323                parent: None,
9324                delivers: &[],
9325                delivered_by: &[],
9326                priority: None,
9327            },
9328            write.target.as_ref(),
9329            &write.depends_on,
9330        )
9331        .await
9332        .map(|written| written.id)
9333    }
9334
9335    /// Create or update one document, which is one issue titled the way this board spells
9336    /// a document.
9337    ///
9338    /// Everything else is exactly a task write: caller metadata goes to the same canonical
9339    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
9340    /// or a field this board cannot carry is refused by name rather than dropped, a target
9341    /// naming an issue this board does not hold is refused rather than created, and an
9342    /// issue this call created is taken back when the rest of the write fails.
9343    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
9344        self.write_document_assets(write, None)
9345            .await
9346            .map(|written| written.id)
9347    }
9348
9349    async fn write_document_with_assets(
9350        &self,
9351        write: &ItemWrite<Document>,
9352        _answers: Option<&BTreeMap<String, Value>>,
9353        assets: &onetaskgraph_plugin_api::AssetWrite,
9354    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
9355        self.write_document_assets(write, Some(assets)).await
9356    }
9357
9358    /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
9359    /// which reads nothing; then the board's `Status` option. Over an existing item that is
9360    /// read off the item, as the write reads it, and the item is held among this command's
9361    /// resolved records so the write that follows reuses that read rather than repeating it;
9362    /// an item that does not carry the field takes the board's fields, which are held once
9363    /// read. A create is checked against the board's fields only when this command already
9364    /// holds them, because a create reads them together with its repository, in one request,
9365    /// and refuses a missing option before it writes anything.
9366    async fn check_status_write(
9367        &self,
9368        kind: ItemKind,
9369        category: StatusCategory,
9370        target: Option<&NativeId>,
9371    ) -> Result<(), SourceError> {
9372        let status = self.resolved_target(kind, category)?;
9373        if status.option().is_none() {
9374            return Ok(());
9375        }
9376        let fields = match target {
9377            Some(target) => {
9378                // A target this board does not hold is the write's own refusal to make.
9379                let Some(item) = self.bound_item(target).await? else {
9380                    return Ok(());
9381                };
9382                self.resolved_cache()?.insert(target.clone(), item.clone());
9383                self.fields_for(Some(&item), true, false).await?.fields
9384            }
9385            None => {
9386                let held = self
9387                    .board_cache()?
9388                    .as_ref()
9389                    .map(|board| board.fields.clone());
9390                match held.or_else(|| {
9391                    self.fields_cache()
9392                        .ok()
9393                        .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
9394                }) {
9395                    Some(fields) => fields,
9396                    None => return Ok(()),
9397                }
9398            }
9399        };
9400        self.column_for(&fields, kind, category, &status)
9401            .map(|_| ())
9402    }
9403
9404    /// Set one task's status alone.
9405    ///
9406    /// An open target reopens a closed issue with an `updateIssue` carrying only its
9407    /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
9408    /// terminal target selects its mapped option, then closes with its fixed reason. No
9409    /// request carries a title, a body or a label. The status
9410    /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
9411    /// what a re-read reports.
9412    async fn set_task_status(
9413        &self,
9414        id: &NativeId,
9415        category: StatusCategory,
9416    ) -> Result<Option<Status>, SourceError> {
9417        self.set_status(id, category).await
9418    }
9419
9420    /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
9421    /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
9422    /// for `none`. Refused by an instance with no `priority_mapping`.
9423    async fn set_task_priority(
9424        &self,
9425        id: &NativeId,
9426        priority: Priority,
9427    ) -> Result<Option<Priority>, SourceError> {
9428        self.set_priority(id, priority).await
9429    }
9430
9431    /// Replace one task's content with a single body update that keeps the metadata slot
9432    /// byte for byte.
9433    async fn set_task_content(
9434        &self,
9435        id: &NativeId,
9436        content: &str,
9437    ) -> Result<Option<()>, SourceError> {
9438        self.replace_content(id, content).await
9439    }
9440
9441    /// Replace one task issue's content and its provenance slot entry with a single body
9442    /// update. The answers are not kept: see `replace_rendering`.
9443    async fn set_task_rendering(
9444        &self,
9445        id: &NativeId,
9446        content: &str,
9447        provenance: &Value,
9448        _answers: &BTreeMap<String, Value>,
9449    ) -> Result<Option<()>, SourceError> {
9450        self.replace_rendering(
9451            id,
9452            BoardKind::Work(ItemKind::Task),
9453            content,
9454            provenance,
9455            None,
9456        )
9457        .await
9458        .map(|written| written.map(|_| ()))
9459    }
9460
9461    /// Replace one design-document issue's content and its provenance slot entry, on exactly
9462    /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
9463    async fn set_document_rendering(
9464        &self,
9465        id: &NativeId,
9466        content: &str,
9467        provenance: &Value,
9468        _answers: &BTreeMap<String, Value>,
9469    ) -> Result<Option<()>, SourceError> {
9470        self.replace_rendering(id, BoardKind::Document, content, provenance, None)
9471            .await
9472            .map(|written| written.map(|_| ()))
9473    }
9474
9475    /// Replace one project issue's content and its provenance slot entry, on exactly the
9476    /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
9477    async fn set_project_rendering(
9478        &self,
9479        id: &NativeId,
9480        content: &str,
9481        provenance: &Value,
9482        _answers: &BTreeMap<String, Value>,
9483    ) -> Result<Option<()>, SourceError> {
9484        self.replace_rendering(
9485            id,
9486            BoardKind::Work(ItemKind::Project),
9487            content,
9488            provenance,
9489            None,
9490        )
9491        .await
9492        .map(|written| written.map(|_| ()))
9493    }
9494
9495    /// Apply a targeted update with one read of the item and a write only for what differs:
9496    /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
9497    /// and last one `updateIssue` for title, body and state. See `targeted_update`.
9498    async fn update_task(
9499        &self,
9500        id: &NativeId,
9501        update: &TaskUpdate,
9502    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
9503        self.targeted_update(id, update).await
9504    }
9505
9506    /// Replace one task's `delivered_by` with a single body update that changes the
9507    /// metadata slot and nothing outside it.
9508    async fn set_delivered_by(
9509        &self,
9510        id: &NativeId,
9511        delivered_by: &[TaskRef],
9512    ) -> Result<Option<()>, SourceError> {
9513        self.replace_delivered_by(id, delivered_by).await
9514    }
9515
9516    /// Set one key of one task issue's metadata with a single body update that changes the
9517    /// metadata slot and nothing outside it — no title, label, state or board field request —
9518    /// and sends nothing when the task already holds that value under the key.
9519    async fn set_task_metadata(
9520        &self,
9521        id: &NativeId,
9522        key: &MetadataKey,
9523        value: &Value,
9524    ) -> Result<Option<Task>, SourceError> {
9525        Ok(self
9526            .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
9527            .await?
9528            .map(|item| item.task())
9529            .transpose()?)
9530    }
9531
9532    /// Set one key of one project issue's metadata, on exactly the terms of
9533    /// [`set_task_metadata`](TaskSource::set_task_metadata).
9534    async fn set_project_metadata(
9535        &self,
9536        id: &NativeId,
9537        key: &MetadataKey,
9538        value: &Value,
9539    ) -> Result<Option<Project>, SourceError> {
9540        Ok(self
9541            .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
9542            .await?
9543            .map(|item| item.project()))
9544    }
9545
9546    /// Set one key of one design-document issue's metadata, on exactly the terms of
9547    /// [`set_task_metadata`](TaskSource::set_task_metadata).
9548    async fn set_document_metadata(
9549        &self,
9550        id: &NativeId,
9551        key: &MetadataKey,
9552        value: &Value,
9553    ) -> Result<Option<Document>, SourceError> {
9554        Ok(self
9555            .set_slot_key(id, BoardKind::Document, key, value)
9556            .await?
9557            .map(|item| item.document()))
9558    }
9559
9560    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
9561        self.delete_item(id).await
9562    }
9563
9564    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
9565        self.delete_item(id).await
9566    }
9567
9568    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
9569        self.delete_item(id).await
9570    }
9571
9572    /// One page of the task issue's own comments, walked by GitHub's own cursor.
9573    ///
9574    /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
9575    /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
9576    ///
9577    /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
9578    /// board is the read of its comments. A draft this process already resolved is refused
9579    /// without one.
9580    async fn task_comments(
9581        &self,
9582        task: &NativeId,
9583        page: &PageRequest,
9584    ) -> Result<Option<Page<Comment>>, SourceError> {
9585        validate_page(page)?;
9586        let cached = self.resolved_cache()?.get(task).cloned();
9587        if let Some(item) = cached {
9588            if item.kind != BoardKind::Work(ItemKind::Task) {
9589                return Ok(None);
9590            }
9591            if item.content_kind == ContentKind::DraftIssue {
9592                return Err(self.draft_has_no_comments(task));
9593            }
9594        }
9595        match self.issue_detail(task, page).await? {
9596            Some(TaskDetailRead {
9597                comments: Some(comments),
9598                ..
9599            }) => comments,
9600            _ => Ok(None),
9601        }
9602    }
9603
9604    /// Every id's task, with the first page of its comments when `comments` names it:
9605    /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
9606    /// comments in one [`graphql::ISSUE_DETAIL`] request.
9607    async fn get_task_details(
9608        &self,
9609        ids: &[NativeId],
9610        comments: Option<&PageRequest>,
9611    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
9612        if let Some(page) = comments
9613            && let Err(error) = validate_page(page)
9614        {
9615            return ids.iter().map(|_| Err(error.clone())).collect();
9616        }
9617        match (ids, comments) {
9618            ([id], Some(page)) => vec![self.issue_detail(id, page).await],
9619            ([id], None) => vec![self.task_read(id).await],
9620            _ => self.issue_details(ids, comments).await,
9621        }
9622    }
9623
9624    /// Add one comment to the task's issue, as the account the token belongs to.
9625    ///
9626    /// The author is refused before anything is sent — not even the task is read — because
9627    /// no answer GitHub could give would make posting under another name than the one asked
9628    /// for the right outcome.
9629    async fn add_comment(
9630        &self,
9631        task: &NativeId,
9632        comment: &NewComment,
9633    ) -> Result<Option<Comment>, SourceError> {
9634        if let Some(author) = &comment.author {
9635            return Err(SourceError::Refused {
9636                message: format!(
9637                    "source {} cannot post a comment as {author:?}: GitHub records the account \
9638                     the token signs in as the author of every comment; next: leave --author \
9639                     out, and the comment is posted as that account",
9640                    self.name
9641                ),
9642            });
9643        }
9644        let Some(issue) = self.commented_issue(task).await? else {
9645            return Ok(None);
9646        };
9647        let data = self
9648            .graphql(
9649                graphql::ADD_COMMENT,
9650                json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
9651            )
9652            .await?;
9653        let subject = data
9654            .pointer("/addComment/subject")
9655            .filter(|value| !value.is_null())
9656            .ok_or_else(|| SourceError::Malformed {
9657                message: "GitHub comment addition returned no subject".into(),
9658            })?;
9659        if required_str(subject, "id")? != issue.0 {
9660            return Err(SourceError::Malformed {
9661                message: "GitHub comment addition answered about another issue".into(),
9662            });
9663        }
9664        let added = data
9665            .pointer("/addComment/commentEdge/node")
9666            .filter(|value| !value.is_null())
9667            .ok_or_else(|| SourceError::Malformed {
9668                message: "GitHub comment addition returned no comment".into(),
9669            })?;
9670        let added = comment_from(added)?;
9671        self.remember_commented(&issue)?;
9672        Ok(Some(added))
9673    }
9674
9675    async fn edit_comment(
9676        &self,
9677        task: &NativeId,
9678        comment: &NativeId,
9679        body: &CommentBody,
9680    ) -> Result<Option<Comment>, SourceError> {
9681        let Some(issue) = self.commented_issue(task).await? else {
9682            return Ok(None);
9683        };
9684        if !self.comment_is_on(&issue, comment).await? {
9685            return Ok(None);
9686        }
9687        let data = self
9688            .graphql(
9689                graphql::UPDATE_COMMENT,
9690                json!({"input":{"id":comment.0,"body":body.as_str()}}),
9691            )
9692            .await?;
9693        let edited = data
9694            .pointer("/updateIssueComment/issueComment")
9695            .filter(|value| !value.is_null())
9696            .ok_or_else(|| SourceError::Malformed {
9697                message: "GitHub comment update returned no comment".into(),
9698            })?;
9699        let edited = comment_from(edited)?;
9700        if edited.id != *comment {
9701            return Err(SourceError::Malformed {
9702                message: "GitHub comment update returned the wrong comment".into(),
9703            });
9704        }
9705        self.remember_commented(&issue)?;
9706        Ok(Some(edited))
9707    }
9708
9709    async fn delete_comment(
9710        &self,
9711        task: &NativeId,
9712        comment: &NativeId,
9713    ) -> Result<Option<NativeId>, SourceError> {
9714        let Some(issue) = self.commented_issue(task).await? else {
9715            return Ok(None);
9716        };
9717        if !self.comment_is_on(&issue, comment).await? {
9718            return Ok(None);
9719        }
9720        let data = self
9721            .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
9722            .await?;
9723        // The payload says nothing about the comment it removed, so what is checked is that
9724        // GitHub answered the mutation at all rather than leaving it unanswered.
9725        data.get("deleteIssueComment")
9726            .filter(|value| !value.is_null())
9727            .ok_or_else(|| SourceError::Malformed {
9728                message: "GitHub comment deletion returned no payload".into(),
9729            })?;
9730        Ok(Some(comment.clone()))
9731    }
9732
9733    /// Every request this source has recorded, and what each of GitHub's two budgets was
9734    /// attributed — read off the same accounting the session report is rendered from, so
9735    /// the two cannot count one request two ways.
9736    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
9737        Ok(Some(self.ledger.snapshot().metering()))
9738    }
9739
9740    /// Drop every item, search answer and board read this source holds, so the next command
9741    /// reads the board as a person has since left it.
9742    ///
9743    /// Every one of those is held on the assumption that nothing but this source writes the
9744    /// board while a command runs, which stops being true the moment the command is over: a
9745    /// body a person edited would be overwritten from the record held here, and a card they
9746    /// moved would be read as still where this source left it. The board's own field
9747    /// definitions go too, because a person can add or delete a `Status` option and a write
9748    /// resolved against the held list would not re-read on a miss. What stays is what stays
9749    /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
9750    /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
9751    /// accounting [`metering`](TaskSource::metering) answers from.
9752    ///
9753    /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
9754    /// refused, because clearing it is what puts it right.
9755    async fn end_command(&self) -> Result<(), SourceError> {
9756        fn clear<T: Default>(held: &Mutex<T>) {
9757            *held
9758                .lock()
9759                .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
9760            held.clear_poison();
9761        }
9762        clear(&self.created);
9763        clear(&self.updated);
9764        clear(&self.commented);
9765        clear(&self.board_cache);
9766        clear(&self.search_cache);
9767        clear(&self.narrowed_cache);
9768        clear(&self.search_next);
9769        clear(&self.resolved_cache);
9770        clear(&self.children_cache);
9771        clear(&self.fields_cache);
9772        Ok(())
9773    }
9774}
9775
9776/// Each project's sub-issues as one command read them, keyed by the selector they were asked
9777/// for under, beside the project that selector named; see `children_cache`.
9778type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
9779
9780/// One issue comment as the contract carries it.
9781///
9782/// `author` is absent both when GitHub answers `null` for an account that no longer exists
9783/// and when it answers an actor with no login, because either way the source did not say who
9784/// wrote it — which is what an absent author means, rather than an author called nothing.
9785fn comment_from(value: &Value) -> Result<Comment, SourceError> {
9786    Ok(Comment {
9787        id: NativeId(required_str(value, "id")?.to_owned()),
9788        author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
9789            .map(str::to_owned),
9790        created_at: optional_time(value, "createdAt")?,
9791        updated_at: optional_time(value, "updatedAt")?,
9792        body: required_str(value, "body")?.to_owned(),
9793        url: optional_str(value, "url")?.map(str::to_owned),
9794    })
9795}
9796
9797/// The page of comments one issue node carries, resumed from `after`.
9798fn comment_page(
9799    node: &Value,
9800    issue: &str,
9801    after: Option<&str>,
9802) -> Result<Page<Comment>, SourceError> {
9803    let connection = node
9804        .get("comments")
9805        .filter(|value| !value.is_null())
9806        .ok_or_else(|| SourceError::Malformed {
9807            message: format!("GitHub issue {issue} answered with no comments connection"),
9808        })?;
9809    let items = optional_nodes(Some(connection), "issue comments")?
9810        .into_iter()
9811        .flatten()
9812        .map(comment_from)
9813        .collect::<Result<Vec<_>, _>>()?;
9814    let next = next_cursor(connection)?;
9815    if let Some(next) = &next {
9816        validate_cursor_progress(after, &next.0)?;
9817    }
9818    Ok(Page { items, next })
9819}
9820
9821/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
9822/// end — `None` when it carried none, or a page with more past it.
9823fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
9824    let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
9825        return Ok(None);
9826    };
9827    if next_cursor(connection)?.is_some() {
9828        return Ok(None);
9829    }
9830    Ok(Some(
9831        optional_nodes(Some(connection), "blocked-by issues")?
9832            .into_iter()
9833            .flatten()
9834            .cloned()
9835            .collect(),
9836    ))
9837}
9838
9839/// Where the recorded tail of a dependency walk resumes; see
9840/// [`GitHubProjectsSource::recorded_edges`].
9841const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
9842
9843/// The board text field this source keeps a copy's origin in.
9844///
9845/// Named after the key it holds, and held to that name by the guard below rather than by
9846/// a reader noticing.
9847const ORIGIN_FIELD: &str = "onetaskgraph.origin";
9848
9849/// The metadata key that field holds.
9850///
9851/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
9852/// constructs or interprets the qualified id it carries. This source names it only to
9853/// route it — a short, typed value belongs in a typed field rather than in the body slot
9854/// a caller's own prose shares.
9855///
9856/// Restated rather than imported, because no plugin crate may depend on the engine. What
9857/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
9858/// target in `check`: it reads the engine's own literal and fails naming the file and the
9859/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
9860/// that creates a second item every run instead of finding the one it wrote — and that is
9861/// too late to learn it.
9862const ORIGIN_KEY: &str = "onetaskgraph.origin";
9863
9864/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
9865///
9866/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
9867/// is derived from the far end, never written down on the near item — so only a forward
9868/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
9869/// it did not come from, and it is told so rather than answered with an empty page that
9870/// reads as a walk which ended.
9871fn recorded_offset(
9872    cursor: Option<&str>,
9873    direction: Direction,
9874) -> Result<Option<usize>, SourceError> {
9875    cursor
9876        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
9877        .map(|offset| {
9878            if direction != Direction::DependsOn {
9879                return Err(SourceError::Config {
9880                    message: format!(
9881                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
9882                         reverse dependency read never issues; resume it in the direction \
9883                         that reported it"
9884                    ),
9885                });
9886            }
9887            offset.parse().map_err(|_| SourceError::Config {
9888                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
9889            })
9890        })
9891        .transpose()
9892}
9893
9894fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
9895    let mut page = offset_page(edges, offset, limit.max(1));
9896    page.next = page
9897        .next
9898        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
9899    page
9900}
9901
9902/// The kind of one issue reached through a dependency connection.
9903///
9904/// The same questions the board scan asks, over the fields the dependency document
9905/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
9906/// then anything with sub-issues or the marker is a project.
9907///
9908/// # Errors
9909///
9910/// A far end this board holds as a document is refused rather than reported. The two
9911/// answers that are not refusals would both be wrong: reporting it as a task names an id
9912/// no task read of this source can find, and reporting it as a project names one no
9913/// project read can. There is no third value to return — `ItemKind` has no document
9914/// variant, because nothing may point at a document — so the relationship itself is what
9915/// the person is told about.
9916fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
9917    let id = required_str(value, "id")?;
9918    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
9919        return Err(SourceError::Refused {
9920            message: format!(
9921                "GitHub issue {id} is a document of this board — its title begins \
9922                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
9923                 on by one; next: remove that issue's blocking relationship on this board"
9924            ),
9925        });
9926    }
9927    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
9928    if parent.is_some() {
9929        return Ok(ItemKind::Task);
9930    }
9931    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
9932    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
9933        message: format!("GitHub issue {id}: {message}"),
9934    })?;
9935    let sub_issues = sub_issue_total(value)?;
9936    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
9937        ItemKind::Project
9938    } else {
9939        ItemKind::Task
9940    })
9941}
9942
9943/// The `IssueStateUpdateInput` one status target asks for.
9944///
9945/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
9946/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
9947/// a currently-closed issue: without that the item would read back `Unknown` and a copy
9948/// would report a change forever. A document has no status at all, and asks for neither.
9949fn state_input(target: Option<&StatusTarget>) -> Value {
9950    match target {
9951        Some(StatusTarget::Terminal(_, reason)) => {
9952            json!({"value":"CLOSED","stateReason":reason.reason()})
9953        }
9954        Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
9955        // A document has no status, so a write of one says nothing about the issue's open
9956        // or closed state rather than forcing it open: `stateInput` is what carries that
9957        // instruction, and an explicit null asks for no change to it.
9958        None => Value::Null,
9959    }
9960}
9961
9962/// The metadata one write stores in the item's body slot.
9963///
9964/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
9965/// rather than carried: the kind marker so an empty project stays readable, the
9966/// repository list only when it is not exactly the issue's own repository, and the far
9967/// ends no relationship here can name.
9968///
9969/// The copy origin is the one typed field that is also mirrored here, and only as a
9970/// mirror: it lands in the board's origin field as well, which stays the one every reader
9971/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
9972/// and catches up with a write in seconds rather than minutes — can find the item by it.
9973/// A reader of the release before this one drops the slot's copy and reads the field, so an
9974/// item written here still reads with exactly one origin there.
9975fn slot_metadata(
9976    incoming: &Incoming<'_>,
9977    own_repository: Option<&Repository>,
9978    fallback: &[DependencyEdge],
9979) -> BTreeMap<String, Value> {
9980    let mut metadata = incoming.metadata.clone();
9981    match metadata.remove(ORIGIN_KEY) {
9982        Some(Value::String(origin)) if !origin.is_empty() => {
9983            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
9984        }
9985        _ => {}
9986    }
9987    match incoming.written.kind() {
9988        BoardKind::Work(kind) => metadata.insert(
9989            ItemKind::METADATA_KEY.to_owned(),
9990            Value::String(kind.marker().to_owned()),
9991        ),
9992        // A document is told by its title, so it carries no kind marker: that key names
9993        // what a dependency endpoint points at, and nothing may point at a document.
9994        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
9995    };
9996    let derivable = own_repository
9997        .map(|own| incoming.repositories == [own.clone()])
9998        .unwrap_or(incoming.repositories.is_empty());
9999    if derivable {
10000        metadata.remove(Repository::METADATA_KEY);
10001    } else {
10002        metadata.insert(
10003            Repository::METADATA_KEY.to_owned(),
10004            Value::Array(
10005                incoming
10006                    .repositories
10007                    .iter()
10008                    .map(|repository| Value::String(repository.as_str().to_owned()))
10009                    .collect(),
10010            ),
10011        );
10012    }
10013    // The typed lists are what land, whatever the caller's own metadata held under their
10014    // keys: a key of either name travelling beside the field would otherwise be a second
10015    // answer to the same question, and the field is the one the contract names.
10016    for (key, entries) in [
10017        (TaskRef::DELIVERS_KEY, incoming.delivers),
10018        (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
10019    ] {
10020        set_task_list(&mut metadata, key, entries);
10021    }
10022    record_edges(&mut metadata, fallback);
10023    metadata
10024}
10025
10026/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
10027/// one slot's metadata, or no such key when there are none.
10028fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
10029    if fallback.is_empty() {
10030        metadata.remove(DependencyEdge::RECORDED_KEY);
10031    } else {
10032        metadata.insert(
10033            DependencyEdge::RECORDED_KEY.to_owned(),
10034            Value::Array(
10035                fallback
10036                    .iter()
10037                    .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
10038                    .collect(),
10039            ),
10040        );
10041    }
10042}
10043
10044/// Every label one item carries, from its content's own connection and nowhere else.
10045///
10046/// There is no second place to read one from: no document this source sends selects the
10047/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
10048/// cannot carry one at all. The module documentation records the three schema facts that
10049/// settle it.
10050fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
10051    optional_nodes(content.get("labels"), "content labels")?
10052        .into_iter()
10053        .flatten()
10054        .map(|v| {
10055            Ok(Label {
10056                id: NativeId(required_str(v, "id")?.to_owned()),
10057                name: required_str(v, "name")?.to_owned(),
10058                color: optional_str(v, "color")?.map(str::to_owned),
10059            })
10060        })
10061        .collect()
10062}
10063
10064/// The definition of each board field one item's values are values of, in the shape a read
10065/// of the board's own `fields` gives one.
10066///
10067/// A value names its field through a fragment on that field's own type, so the type is
10068/// known from which kind of value it is: a single-select value's field is a
10069/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
10070/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
10071fn field_definitions(field_values: &[Value]) -> Vec<Value> {
10072    field_values
10073        .iter()
10074        .filter_map(|value| {
10075            let field = value.get("field")?.as_object()?;
10076            field.get("id")?.as_str().filter(|id| !id.is_empty())?;
10077            let typename = if value.get("text").is_some() {
10078                "ProjectV2Field"
10079            } else if value.get("name").is_some() {
10080                "ProjectV2SingleSelectField"
10081            } else {
10082                return None;
10083            };
10084            let mut defined = field.clone();
10085            defined.insert("__typename".to_owned(), json!(typename));
10086            Some(Value::Object(defined))
10087        })
10088        .collect()
10089}
10090
10091fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
10092    let Some(node) = field_values
10093        .iter()
10094        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
10095    else {
10096        return Ok(None);
10097    };
10098    Ok(optional_str(node, "text")?.map(str::to_owned))
10099}
10100
10101fn valid_github_owner(owner: &str) -> bool {
10102    !owner.is_empty()
10103        && owner.len() <= 39
10104        && !owner.starts_with('-')
10105        && !owner.ends_with('-')
10106        && !owner.contains("--")
10107        && owner
10108            .bytes()
10109            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
10110}
10111
10112/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
10113/// neither of the two names a path segment already means.
10114fn valid_github_repository_name(name: &str) -> bool {
10115    !name.is_empty()
10116        && name.len() <= 100
10117        && name != "."
10118        && name != ".."
10119        && name
10120            .bytes()
10121            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
10122}
10123
10124fn valid_environment_name(name: &str) -> bool {
10125    let mut bytes = name.bytes();
10126    bytes
10127        .next()
10128        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
10129        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
10130}
10131
10132/// How many sub-issues one issue has.
10133///
10134/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
10135/// absent or non-integer one is a response this source cannot read — and reading it as
10136/// zero would classify a project as a task, which is exactly the mistake the marker
10137/// exists to keep from happening quietly.
10138fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
10139    let summary = issue
10140        .get("subIssuesSummary")
10141        .ok_or_else(|| SourceError::Malformed {
10142            message: "GitHub issue is missing subIssuesSummary".into(),
10143        })?;
10144    summary
10145        .get("total")
10146        .and_then(Value::as_u64)
10147        .ok_or_else(|| SourceError::Malformed {
10148            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
10149        })
10150}
10151
10152/// One issue's own `number`.
10153///
10154/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
10155/// an issue in this module asks for it. So a read of one that comes back without it, or
10156/// with something that is not an unsigned integer, is a response this source cannot read —
10157/// absence here is **not** "this issue has no number". A draft is the content that has
10158/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
10159/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
10160fn issue_number(issue: &Value) -> Result<u64, SourceError> {
10161    issue
10162        .get("number")
10163        .and_then(Value::as_u64)
10164        .ok_or_else(|| SourceError::Malformed {
10165            message: "GitHub issue number is missing or is not an unsigned integer".into(),
10166        })
10167}
10168
10169/// The `number` a creating mutation answered with, and `None` when it answered without one;
10170/// why a missing one is tolerated is at the call in `create_and_file_issue`.
10171fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
10172    match created.get("number") {
10173        None | Some(Value::Null) => Ok(None),
10174        Some(value) => value
10175            .as_u64()
10176            .map(Some)
10177            .ok_or_else(|| SourceError::Malformed {
10178                message: "GitHub created issue number is not an unsigned integer".into(),
10179            }),
10180    }
10181}
10182
10183fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
10184    value
10185        .get(field)
10186        .and_then(Value::as_str)
10187        .ok_or_else(|| SourceError::Malformed {
10188            message: format!("GitHub response is missing string field {field}"),
10189        })
10190}
10191
10192fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
10193    let found = required_str(value, field)?;
10194    if found.trim().is_empty() {
10195        return Err(SourceError::Malformed {
10196            message: format!("GitHub response has blank string field {field}"),
10197        });
10198    }
10199    Ok(found)
10200}
10201
10202/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
10203/// needs one — Linear spells them too, in its own description field.
10204///
10205/// Restated rather than shared, because a plugin crate depends on the contract crate and
10206/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
10207/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
10208/// source round-trips its own writes perfectly well under its own spelling.
10209const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
10210const METADATA_CLOSE: &str = "\n-->";
10211
10212/// What the composer puts between a non-empty visible body and the slot, and the one thing
10213/// the parser takes off the visible body when it takes the slot off — exactly once, so every
10214/// other trailing byte of the body comes back as it was written.
10215// 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.
10216const METADATA_SEPARATOR: &str = "\n\n";
10217
10218/// The visible body and the metadata slot at the end of it.
10219///
10220/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
10221/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
10222/// own content and is left alone. The visible body is everything before the slot less the
10223/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
10224fn metadata_body(
10225    body: Option<String>,
10226) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
10227    let Some(body) = body else {
10228        return Ok((None, BTreeMap::new()));
10229    };
10230    let Some(slot) = slot_span(&body)? else {
10231        return Ok((Some(body), BTreeMap::new()));
10232    };
10233    let metadata =
10234        serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
10235            SourceError::Malformed {
10236                message: format!(
10237                    "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
10238                ),
10239            }
10240        })?;
10241    let before = &body[..slot.start];
10242    let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
10243    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
10244}
10245
10246/// Where the metadata slot sits in one body, as byte offsets into it.
10247struct SlotSpan {
10248    /// Where [`METADATA_OPEN`] begins.
10249    start: usize,
10250    /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
10251    encoded_start: usize,
10252    /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
10253    encoded_end: usize,
10254    /// Just past [`METADATA_CLOSE`].
10255    end: usize,
10256}
10257
10258/// The slot at the very end of `body`, or `None` when it has none.
10259///
10260/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
10261/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
10262/// slot.
10263fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
10264    let Some(start) = body.rfind(METADATA_OPEN) else {
10265        return Ok(None);
10266    };
10267    let encoded_start = start + METADATA_OPEN.len();
10268    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
10269        return Err(SourceError::Malformed {
10270            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
10271        });
10272    };
10273    let encoded_end = encoded_start + relative_end;
10274    let end = encoded_end + METADATA_CLOSE.len();
10275    if !body[end..].trim().is_empty() {
10276        return Ok(None);
10277    }
10278    Ok(Some(SlotSpan {
10279        start,
10280        encoded_start,
10281        encoded_end,
10282        end,
10283    }))
10284}
10285
10286/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
10287/// slot as it was.
10288///
10289/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
10290/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
10291/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
10292/// or alone in an empty body — and a body with no slot that is given no metadata is
10293/// returned as it is.
10294fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
10295    let encoded = if metadata.is_empty() {
10296        None
10297    } else {
10298        Some(
10299            serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
10300                message: error.to_string(),
10301            })?,
10302        )
10303    };
10304    Ok(match (slot_span(body)?, encoded) {
10305        (Some(slot), Some(encoded)) => format!(
10306            "{}{encoded}{}",
10307            &body[..slot.encoded_start],
10308            &body[slot.encoded_end..]
10309        ),
10310        (Some(slot), None) => {
10311            let before = &body[..slot.start];
10312            format!(
10313                "{}{}",
10314                before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
10315                &body[slot.end..]
10316            )
10317        }
10318        (None, None) => body.to_owned(),
10319        (None, Some(encoded)) if body.is_empty() => {
10320            format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
10321        }
10322        (None, Some(encoded)) => {
10323            format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
10324        }
10325    })
10326}
10327
10328/// `body` with everything before its metadata slot replaced by `content`, and the slot
10329/// itself kept byte for byte.
10330///
10331/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
10332/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
10333/// `content` is empty — so a read of the result reports `content` as the visible body and
10334/// the slot's metadata exactly as it was.
10335fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
10336    let Some(slot) = slot_span(body)? else {
10337        return Ok(content.to_owned());
10338    };
10339    let kept = &body[slot.start..];
10340    Ok(if content.is_empty() {
10341        kept.to_owned()
10342    } else {
10343        format!("{content}{METADATA_SEPARATOR}{kept}")
10344    })
10345}
10346
10347/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
10348fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
10349    if entries.is_empty() {
10350        metadata.remove(key);
10351    } else {
10352        metadata.insert(
10353            key.to_owned(),
10354            Value::Array(
10355                entries
10356                    .iter()
10357                    .map(|entry| Value::String(entry.as_str().to_owned()))
10358                    .collect(),
10359            ),
10360        );
10361    }
10362}
10363
10364fn compose_body(
10365    content: Option<&str>,
10366    metadata: &BTreeMap<String, Value>,
10367) -> Result<Option<String>, SourceError> {
10368    let visible = content.unwrap_or_default();
10369    if metadata.is_empty() {
10370        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
10371    }
10372    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
10373        message: error.to_string(),
10374    })?;
10375    Ok(Some(if visible.is_empty() {
10376        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
10377    } else {
10378        format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
10379    }))
10380}
10381
10382fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
10383    value
10384        .get(field)
10385        .and_then(Value::as_bool)
10386        .ok_or_else(|| SourceError::Malformed {
10387            message: format!("GitHub response is missing boolean field {field}"),
10388        })
10389}
10390fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
10391    match value.get(field) {
10392        None | Some(Value::Null) => Ok(None),
10393        Some(value) => value
10394            .as_str()
10395            .map(Some)
10396            .ok_or_else(|| SourceError::Malformed {
10397                message: format!("GitHub response field {field} is not a string or null"),
10398            }),
10399    }
10400}
10401fn optional_nodes<'a>(
10402    connection: Option<&'a Value>,
10403    name: &str,
10404) -> Result<Option<&'a Vec<Value>>, SourceError> {
10405    match connection {
10406        None | Some(Value::Null) => Ok(None),
10407        Some(value) => value
10408            .get("nodes")
10409            .and_then(Value::as_array)
10410            .map(Some)
10411            .ok_or_else(|| SourceError::Malformed {
10412                message: format!("GitHub {name}.nodes is not an array"),
10413            }),
10414    }
10415}
10416fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
10417    let page_info = connection
10418        .get("pageInfo")
10419        .ok_or_else(|| SourceError::Malformed {
10420            message: format!("GitHub {name} has no pageInfo"),
10421        })?;
10422    if required_bool(page_info, "hasNextPage")? {
10423        return Err(SourceError::Malformed {
10424            message: format!(
10425                "GitHub {name} exceeds the supported nested connection size of {size}"
10426            ),
10427        });
10428    }
10429    Ok(())
10430}
10431fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
10432    optional_str(value, field)?
10433        .map(|timestamp| {
10434            timestamp.parse().map_err(|error| SourceError::Malformed {
10435                message: format!("GitHub response field {field} is not a timestamp: {error}"),
10436            })
10437        })
10438        .transpose()
10439}
10440fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
10441    if page.limit == 0 {
10442        Err(SourceError::Config {
10443            message: "page limit must be at least 1".into(),
10444        })
10445    } else {
10446        Ok(())
10447    }
10448}
10449fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
10450    let page = connection
10451        .get("pageInfo")
10452        .filter(|value| value.is_object())
10453        .ok_or_else(|| SourceError::Malformed {
10454            message: "GitHub connection is missing pageInfo".into(),
10455        })?;
10456    if required_bool(page, "hasNextPage")? {
10457        let cursor = required_str(page, "endCursor")?;
10458        validate_cursor_progress(None, cursor)?;
10459        Ok(Some(Cursor(cursor.into())))
10460    } else {
10461        Ok(None)
10462    }
10463}
10464fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
10465    if next.is_empty() || previous == Some(next) {
10466        Err(SourceError::Malformed {
10467            message: "GitHub pagination cursor is empty or did not advance".into(),
10468        })
10469    } else {
10470        Ok(())
10471    }
10472}
10473/// The version of this plugin's opaque narrowing-search cursor.
10474pub const SEARCH_CURSOR_VERSION: u32 = 4;
10475
10476#[derive(Serialize, Deserialize)]
10477#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
10478enum SearchConnection {
10479    Initial {},
10480    Continuing { after: Cursor },
10481    Exhausted {},
10482}
10483impl SearchConnection {
10484    fn after(&self) -> Option<&str> {
10485        match self {
10486            Self::Continuing { after } => Some(&after.0),
10487            _ => None,
10488        }
10489    }
10490    fn exhausted(&self) -> bool {
10491        matches!(self, Self::Exhausted { .. })
10492    }
10493    /// Whether a cursor naming this position, `offset` rows into its page, is one this
10494    /// plugin could have handed out: a page is resumed only part of the way through it — an
10495    /// offset of a whole page or more would skip rows nobody was given — an initial page
10496    /// only once some of it was handed out, and an exhausted connection has no page to be
10497    /// part of the way through.
10498    fn valid_resume(&self, offset: usize) -> bool {
10499        let within = offset < SEARCH_PAGE_SIZE as usize;
10500        match self {
10501            Self::Initial { .. } => offset > 0 && within,
10502            Self::Continuing { after } => !after.0.is_empty() && within,
10503            Self::Exhausted { .. } => offset == 0,
10504        }
10505    }
10506}
10507
10508/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
10509#[derive(Serialize, Deserialize)]
10510#[serde(deny_unknown_fields)]
10511struct SearchPosition {
10512    version: u32,
10513    connection: SearchConnection,
10514    /// How many rows of the page `connection` starts were already handed out.
10515    #[serde(default, skip_serializing_if = "is_zero")]
10516    offset: usize,
10517    #[serde(default, skip_serializing_if = "Vec::is_empty")]
10518    seen: Vec<NativeId>,
10519    #[serde(default, skip_serializing_if = "Vec::is_empty")]
10520    own: Vec<NativeId>,
10521}
10522impl Default for SearchPosition {
10523    fn default() -> Self {
10524        Self {
10525            version: SEARCH_CURSOR_VERSION,
10526            connection: SearchConnection::Initial {},
10527            offset: 0,
10528            seen: Vec::new(),
10529            own: Vec::new(),
10530        }
10531    }
10532}
10533
10534fn is_zero(offset: &usize) -> bool {
10535    *offset == 0
10536}
10537
10538fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
10539    cursor.map_or(Ok(0), |c| {
10540        c.0.parse().map_err(|_| SourceError::Config {
10541            message: "page cursor is invalid".into(),
10542        })
10543    })
10544}
10545fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
10546    if offset > items.len() {
10547        return Page::last(vec![]);
10548    }
10549    let tail = items.split_off(offset);
10550    let mut selected = tail;
10551    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
10552    selected.truncate(limit);
10553    Page {
10554        items: selected,
10555        next,
10556    }
10557}
10558
10559impl GitHubProjectsSource {
10560    async fn write_task_assets(
10561        &self,
10562        write: &ItemWrite<Task>,
10563        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
10564    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
10565        let near = write.target.as_ref().unwrap_or(&write.item.id);
10566        for (key, entries) in [
10567            (TaskRef::DELIVERS_KEY, &write.item.delivers),
10568            (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
10569        ] {
10570            TaskRef::listed(key, near, Some(&self.name), entries.clone())
10571                .map_err(|message| SourceError::Refused { message })?;
10572        }
10573        if self.priorities.is_none() && write.item.priority != Priority::None {
10574            return Err(self.holds_no_priority());
10575        }
10576        self.write_item(
10577            &Incoming {
10578                written: Written::Work(ItemKind::Task, &write.item.status),
10579                title: &write.item.title,
10580                content: write.item.content.as_deref(),
10581                assets,
10582                labels: &write.item.labels,
10583                metadata: &write.item.metadata,
10584                repositories: &write.item.repositories,
10585                parent: write.item.project.as_ref(),
10586                delivers: &write.item.delivers,
10587                delivered_by: &write.item.delivered_by,
10588                priority: self.priorities.as_ref().map(|_| write.item.priority),
10589            },
10590            write.target.as_ref(),
10591            &write.depends_on,
10592        )
10593        .await
10594    }
10595    async fn write_document_assets(
10596        &self,
10597        write: &ItemWrite<Document>,
10598        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
10599    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
10600        // A document takes part in no dependency graph, so there is no far end to write
10601        // natively and none to record: a caller naming one is told so rather than having it
10602        // stored under the reserved key, where a later read would report an edge the
10603        // contract says cannot exist.
10604        if !write.depends_on.is_empty() {
10605            return Err(SourceError::Refused {
10606                message: format!(
10607                    "this write names {} dependencies for a document, and a document takes \
10608                     part in no dependency graph; next: put the dependency on the task or \
10609                     project the document is about",
10610                    write.depends_on.len()
10611                ),
10612            });
10613        }
10614        self.write_item(
10615            &Incoming {
10616                written: Written::Document,
10617                title: &write.item.title,
10618                content: write.item.content.as_deref(),
10619                assets,
10620                labels: &write.item.labels,
10621                metadata: &write.item.metadata,
10622                repositories: &write.item.repositories,
10623                parent: write.item.project.as_ref(),
10624                delivers: &[],
10625                delivered_by: &[],
10626                priority: None,
10627            },
10628            write.target.as_ref(),
10629            &[],
10630        )
10631        .await
10632    }
10633}
10634
10635/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
10636/// itself, for the two things no journey can observe.
10637///
10638/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
10639/// with and without the call, that a settlement, a board listing and a metadata search each
10640/// read afresh after it — the resolved records, the written-item overlay, the board and its
10641/// search, and the narrowed searches. What they cannot reach is the held field definitions,
10642/// because a status write naming an option a person deleted is refused the same whether or
10643/// not the list is held, and a poisoned lock, because nothing outside the source can panic
10644/// while one of its locks is held. So these assert those directly, and every other holder
10645/// beside them so a holder added later without a clear in the call fails here.
10646#[cfg(test)]
10647mod end_command_tests {
10648    use super::*;
10649
10650    struct Token;
10651
10652    impl SecretResolver for Token {
10653        fn get(&self, var: &str) -> Option<SecretString> {
10654            (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
10655        }
10656    }
10657
10658    fn source() -> GitHubProjectsSource {
10659        let config = serde_json::from_value(json!({
10660            "owner": "octo-org", "project_number": 7, "repository": "acme/work",
10661            // Nothing here is sent: the source is only built and its state inspected.
10662            "endpoint": "http://127.0.0.1:9/graphql",
10663        }))
10664        .expect("a usable configuration");
10665        GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
10666            .expect("the source builds")
10667    }
10668
10669    /// One issue as a board read answers it.
10670    fn resolved(source: &GitHubProjectsSource) -> Resolved {
10671        source
10672            .resolve(&json!({
10673                "id": "ITEM-1",
10674                "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
10675                            "body": "what a person may since have edited", "state": "OPEN",
10676                            "stateReason": null, "url": null, "number": 1,
10677                            "subIssuesSummary": {"total": 0},
10678                            "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
10679                "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
10680            }))
10681            .expect("the item reads")
10682            .expect("an issue")
10683    }
10684
10685    /// Hold something in every holder the call clears, and the repository id it keeps.
10686    fn fill(source: &GitHubProjectsSource) {
10687        let item = resolved(source);
10688        source.created.lock().unwrap().push(item.clone());
10689        source.updated.lock().unwrap().push(item.clone());
10690        *source.board_cache.lock().unwrap() = Some(Board {
10691            id: "PVT-board".into(),
10692            fields: json!({"nodes": []}),
10693            items: vec![item.clone()],
10694        });
10695        *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
10696        source
10697            .narrowed_cache
10698            .lock()
10699            .unwrap()
10700            .insert("status:todo".into(), vec![item.clone()]);
10701        source.children_cache.lock().unwrap().insert(
10702            NativeId("P-1".into()),
10703            (NativeId("P-1".into()), vec![item.clone()]),
10704        );
10705        source
10706            .search_next
10707            .lock()
10708            .unwrap()
10709            .insert("status:todo".into(), Some("cursor".into()));
10710        source
10711            .resolved_cache
10712            .lock()
10713            .unwrap()
10714            .insert(item.id.clone(), item);
10715        *source.fields_cache.lock().unwrap() = Some(BoardFields {
10716            id: BoardId::parse("PVT-board").unwrap(),
10717            fields: json!({"nodes": []}),
10718        });
10719        source
10720            .repository_cache
10721            .lock()
10722            .unwrap()
10723            .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
10724    }
10725
10726    fn assert_dropped(source: &GitHubProjectsSource) {
10727        assert!(source.created().unwrap().is_empty(), "created");
10728        assert!(source.updated().unwrap().is_empty(), "updated");
10729        assert!(source.board_cache().unwrap().is_none(), "board");
10730        assert!(source.search_cache.lock().unwrap().is_none(), "search");
10731        assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
10732        assert!(
10733            source.children_cache.lock().unwrap().is_empty(),
10734            "project children"
10735        );
10736        assert!(
10737            source.search_next.lock().unwrap().is_empty(),
10738            "search paging"
10739        );
10740        assert!(
10741            source.resolved_cache().unwrap().is_empty(),
10742            "resolved records"
10743        );
10744        assert!(source.fields_cache().unwrap().is_none(), "board fields");
10745        assert_eq!(
10746            source.repository_cache().unwrap().len(),
10747            1,
10748            "a repository's node id stays valid and is kept"
10749        );
10750    }
10751
10752    fn end(source: &GitHubProjectsSource) {
10753        tokio::runtime::Builder::new_current_thread()
10754            .build()
10755            .unwrap()
10756            .block_on(source.end_command())
10757            .expect("the command ends");
10758    }
10759
10760    #[test]
10761    fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
10762        let source = source();
10763        fill(&source);
10764        end(&source);
10765        assert_dropped(&source);
10766    }
10767
10768    #[test]
10769    fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
10770        fn poison<T: Send>(held: &Mutex<T>) {
10771            std::thread::scope(|scope| {
10772                let _ = scope
10773                    .spawn(|| {
10774                        let _guard = held.lock().unwrap();
10775                        panic!("a failure while the lock is held");
10776                    })
10777                    .join();
10778            });
10779            assert!(held.is_poisoned());
10780        }
10781        let source = source();
10782        fill(&source);
10783        poison(&source.created);
10784        poison(&source.updated);
10785        poison(&source.board_cache);
10786        poison(&source.search_cache);
10787        poison(&source.narrowed_cache);
10788        poison(&source.search_next);
10789        poison(&source.resolved_cache);
10790        poison(&source.fields_cache);
10791        assert!(
10792            source.resolved_cache().is_err(),
10793            "a poisoned lock is refused before the call"
10794        );
10795        end(&source);
10796        assert_dropped(&source);
10797    }
10798}