Skip to main content

onetaskgraph_github_projects/
lib.rs

1//! A stateless onetaskgraph source over one GitHub Projects v2 board.
2//!
3//! **A board is a container of projects, not a project.** Its own `title`,
4//! `shortDescription` and `readme` are never read as an item's fields and are never
5//! written: nothing in this source can rename the board a user configured.
6//!
7//! **A project is an issue and its tasks are that issue's sub-issues.** GitHub's schema
8//! decides that: `Issue` exposes `parent`, `subIssues` and `subIssuesSummary`, and
9//! `DraftIssue` exposes none of them. Creating an issue needs a `repositoryId`, and a
10//! board has none, so a write without [`GitHubProjectsConfig::repository`] is refused
11//! naming the field — but that repository is the *fallback*, not the home of every item.
12//!
13//! <!-- llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] The rule's one
14//! executable source is `GitHubProjectsSource::creation_target`; this is where a reader of
15//! the module meets it, and `tests/plugin.rs` drives every arm below against the loopback
16//! board and asserts on `createIssue`'s own `repositoryId`, so the prose cannot outlive a
17//! change to the rule. -->
18//! **Which repository an issue is created in is decided by the item's own `repositories`
19//! field, under one rule.** Exactly one entry names the repository the issue is created in:
20//! a task issue is where a person finds the work from the repository it changes, and one
21//! filed in a board's nominated repository is invisible from every other. Zero entries, or
22//! two or more, name none, so a task's or a document's issue is created in the repository
23//! its parent project's issue lives in — read from the board, or from this process's own
24//! record of a project it created earlier in the same command — and a project's issue, or
25//! a task or document written with no parent, is created in the configured `repository:`.
26//! What that rule refuses, it refuses before `createIssue`, so no issue is half-created. An
27//! existing issue is never moved: the update path leaves the issue where it is and records
28//! the list in the metadata slot when it differs, so the read side's derivation and the
29//! creation rule agree by construction.
30//! <!-- llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate] -->
31//!
32//! **A document is an ordinary issue whose title begins [`DESIGN_TITLE_PREFIX`].** A
33//! board has no document type and nothing but issues to hold one in, so the title is the
34//! discriminator and it is the whole of it. The title this source *reports* is the one a
35//! person wrote, with the prefix taken off — the same way the metadata slot is taken off
36//! the body so `content` is what the person wrote — and writing a document puts the prefix
37//! back, so a round trip returns the title that went in.
38//!
39//! **Telling a document from a project from a task.** The design prefix is read **first**:
40//! a document is never a project and never a task, whatever sub-issues it has or does not
41//! have. Only then does the rest apply — a board issue is a project when *either* it has
42//! sub-issues *or* it carries [`ItemKind::METADATA_KEY`]; otherwise it is a task. A
43//! sub-issue is always a task, whatever it carries. The marker is sufficient and never
44//! necessary: it is what makes an *empty* project — the state a project copy passes
45//! through between creating the project and filing its first task — readable as a
46//! project, while the sub-issue arm lets a person author a project on the board by hand
47//! with no knowledge of this product's metadata at all. Reading the prefix later than the
48//! sub-issue rule would make a design issue with no sub-issues an empty project, which is
49//! exactly the state that rule exists to catch. Pull requests are neither a project nor a
50//! task nor a document and are ignored.
51//!
52//! **A task's comments are its issue's comments.** They are read off `Issue.comments` and
53//! written with `addComment`, `updateIssueComment` and `deleteIssueComment`, and a comment's
54//! id is GitHub's own node id for the `IssueComment`. Two things GitHub decides are refused
55//! rather than papered over: a board **draft** is not an issue and has no comments at all, so
56//! a comment call on one is refused rather than answered with an empty page; and GitHub signs
57//! every comment as the account the token belongs to, so a comment handed an author of its
58//! own is refused rather than posted under another name. GitHub's comment mutations take the
59//! comment's id and nothing else, so an edit or a delete first reads which issue that comment
60//! is on, and a comment on some other issue is one this task does not have.
61//!
62//! **Where an entity is, is a link.** Every project, task and document this source reports
63//! carries a [`Location::Url`] naming the issue's own web address — the same address the
64//! `url` field already reports, in the shape that says a reader can open it. That is the
65//! contrast the location contract exists for: a reader holding an entity from this source
66//! is handed something to link to and one holding an entity from a folder of Markdown is
67//! handed a path, and neither has to know which plugin answered. It does not replace or
68//! derive from `url`; that field goes on reporting what it always reported.
69//!
70//! **Where metadata lives.** Short typed things go to typed fields and native relations:
71//! status to the board's `Status` single-select and the issue's own state, the copy
72//! origin to a source-owned `onetaskgraph.origin` text field, and dependencies to
73//! `blockedBy` and to sub-issue links. Unbounded caller JSON goes in a trailing
74//! `<!-- onetaskgraph.metadata ... -->` comment at the end of the issue body — the same
75//! encoding `docs/metadata.md` settles for Linear, not a second one. A ProjectV2 text
76//! field is length-bounded and `shortDescription` is capped at 300 characters, which is
77//! why neither can hold a caller's own prose. Setting one caller key on its own — on a task,
78//! a project or a document alike — is one update of the issue body that changes that slot
79//! and not one byte outside it, and it is not sent at all when the key already holds the
80//! value. The link a copy records on an item it copied, `onetaskgraph.copies`, is small and
81//! is kept in that same slot, written by that same update.
82//!
83//! **A caller's value can also be projected onto a board text field**, so the board can filter
84//! on it: each [`GitHubProjectsConfig::metadata_fields`] entry names a text field, a top-level
85//! metadata key and a path inside its value. The slot stays the value's one home — every read
86//! reports metadata from it alone — and the field follows it on every write of an item, a
87//! create, an update and a copy alike: a string is written, and not sent again while the field
88//! holds it — an empty string included, which is written as itself; nothing there or `null`
89//! clears a field holding a value; any other
90//! JSON type is refused before any mutation. It rides the ordered [`graphql::UPDATE_FIELDS`]
91//! write beside `Status`, `Priority` and the origin, so it adds no request; a key set on its
92//! own moves its field first and the body last, as every write of an existing item does. A
93//! board without the field, or with a non-text field of that name, is refused before any
94//! mutation pointing at `sources fields`, whose [`GitHubProjectsSource::fields`] creates it as
95//! a text field and never changes a field that is there.
96//!
97// llmlint: ignore-block[contracts_have_one_source_or_a_drift_gate] This public module documentation is a required user-facing description; the loopback plugin tests and shared live journey drive StatusMapping resolution, both mutations, and observed read-back together.
98//! **Status.** `status_mapping` is per-instance configuration, in the shared grammar
99//! [`onetaskgraph_plugin_api::StatusMapping`] documents, from a status category to an
100//! option of the board's one `Status` field for a task and for a project: a bare name is
101//! the option for both kinds, `null` disables the category for both, and `{task, project}`
102//! names it per kind. A category the mapping does not mention keeps its shipped default for
103//! both kinds; one it mentions is exactly what it configures, so a per-kind object no longer
104//! gets the shipped default for the kind it leaves out. Two categories one kind would read
105//! back from one option are refused as the configuration is read, ignoring case, while one
106//! option may stand for different categories of the two kinds. Writes go by the kind of the
107//! item written: a status that kind has no option for, or whose option the board lacks, is
108//! refused before any mutation, naming the source, the kind, the category and the key
109//! `status_mapping.<category>.<kind>` — there is no fallback. `done` selects its mapped
110//! option and closes the issue as `COMPLETED`; `cancelled` selects its mapped option and
111//! closes it as `NOT_PLANNED`, for either kind. Every open category reopens a closed issue
112//! before selecting its option. Reads give a closed issue's reason precedence over its
113//! option, while an open issue's option decides its category through its own kind's
114//! mapping, and an option that mapping does not name reads as `unknown` under its own name.
115//! The guarded [`GitHubProjectsSource::status_options`] and
116//! [`GitHubProjectsSource::fields`] operations are the one path here that calls
117//! `updateProjectV2Field`: GitHub replaces the whole option list, so they preserve every
118//! existing option id and verify the field and item assignments immediately afterwards.
119//! They ask for both kinds' options, counting a terminal category's mapped option as
120//! configured because a terminal write refuses without it. No ordinary source read or
121//! write calls that mutation, whose
122//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
123//! and a mistake destroys every item's status. A status this board cannot represent is a
124//! refusal naming the status and the instance instead.
125//!
126//! `unknown` has no shipped option because this source cannot preserve an open-ended
127//! status word: it writes an existing board option and never
128//! creates an option. An operator may map `unknown` to one existing option, in which case
129//! every unknown word lands on that option and reads back as `unknown` under the option's
130//! name. This differs from `local-md`, which writes and reads the original word itself.
131//!
132//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
133//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
134//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
135//! finished tasks were only moved to a "Done" column would read 0% complete forever.
136// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
137//!
138//! # What this source declares, field by field
139//!
140//! One verdict per field of [`Capabilities`], and what `Native` means when this source
141//! says it. *Proven* means a shared journey drives it against the real
142//! binary over this source's own row in `crates/onetaskgraph-e2e-support/src/fixtures.rs`, and
143//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
144//! [`capabilities`](TaskSource::capabilities) from parting.
145//!
146//! | Field | Verdict |
147//! | --- | --- |
148//! | `projects` | **Supported and proven,** and the one predicate here that is pushed down rather than applied in process: a task's project is the issue it is a sub-issue of, so a listing scoped to one *asks that issue* for its own sub-issues. This is the field that was declared and then not applied, which silently returned another project's tasks. |
149//! | `documents` | **Supported and proven.** A board holds issues, so a document is one: the issue whose title begins [`DESIGN_TITLE_PREFIX`]. Reads, filters and paging answer on exactly the terms a task read does, and a write puts the prefix back. |
150//! | `comments` | **Supported and proven,** over the task issue's own comment connection, oldest first and paged by GitHub's own cursor; added, edited and removed through GitHub's comment mutations, paced as every other mutation is. A draft item has no comments on GitHub and is refused, and so is an author, because GitHub records the signed-in account as every comment's author. |
151//! | `assets` | **Supported and native.** Image references travel as GitHub user attachments in the repository the task or document issue lives in. Uploads are authenticated and verified before the issue body is written. |
152//! | `priority` | **Supported and proven** by an instance configured with `priority_mapping`, and declared unsupported by one without it, which reports every task's priority as `none` and sends exactly the requests it sent before priorities existed. The priority is the board's single-select `Priority` field: no value is `none`, a mapped option is its level, matched case-insensitively, and an option the mapping does not name fails the read of that task, naming the option. A write selects the mapped option, or clears the value for `none`; a board without the field or the option is refused, pointing at `sources fields`, which is the one thing that creates either. |
153//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
154//! | `filter_by_comment_activity` | **Supported, and exact** for comments created and for comments edited at or after `commented_since`, in every repository — of any owner — the board's items live in. Applied by asking a narrower question rather than by reading the board: GitHub's issue search scoped by `project:<owner>/<number>` alone, with an `updated:>=` qualifier, names the candidates, and each candidate's own comments confirm it, so `ProjectV2.items` is never read. That rests on GitHub moving an issue's `updatedAt` when a comment on it is added **or edited**, which the credentialed journey `an_edited_comment_moves_its_issue_and_is_selected_since` re-takes on every run of this lane. The search is an index that lags a write — the credentialed lane has watched it miss a newly commented issue for thirty seconds — so an issue this process itself commented on in the same command is a candidate whatever the search says, read by its own node if the search did not name it, and has its comments read rather than being ruled out by an `updatedAt` from before the comment. A comment another process wrote is found only once the index has it, so a caller asking again from its last instant should overlap the two generously. |
155//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
156//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
157//! | `filter_by_status` | **Supported and proven,** over the board's `Status` option and the issue's open or closed state, through this instance's own `status_mapping` for the item's kind — a task query by the task half, a project query by the project half, `unknown` included. |
158//! | `filter_by_metadata` | **Supported, and asked of GitHub** — through the body and never through a projected `metadata_fields` text field, which is the board's to filter on and not this source's to read. A query naming metadata values is one board-scoped issue search with each value a quoted phrase `in:body` — GitHub's index covers the metadata comment at the end of the body, which is where caller metadata lives — and every candidate is confirmed against its own parsed metadata comment, so only an item holding that string at that key and path is returned. **A value with no letter or digit is refused** — the empty string, whitespace or punctuation alone — before any request, as a `SourceError::Refused` (wire kind `refused`) naming the value: GitHub's index holds words, so no bounded query can find such a value, and this source neither reads the whole board for it nor answers it as empty. |
159//! | `filter_by_origin` | **Supported, and asked of GitHub without enumerating the board.** The union of three reads, each confirmed by an exact match against the item's own origin field: the board's field filter over the `onetaskgraph.origin` text field, the issue search for the id as a phrase in the body where a write of this release mirrors it, and this process's own writes. See *Where a read-after-write guarantee comes from* for the window the three leave. |
160//! | `search_title` | **Supported, and asked of GitHub for a task,** over `Issue.title`: a task query's text is one board-scoped issue search for it as a phrase `in:title`, every candidate confirmed by the case-insensitive substring rule. GitHub matches whole words, so a task holding the text only inside a longer word is not returned — a narrowing this source declares rather than hides. **A text with no letter or digit that is not blank is refused** — `--` for one — before any request, as the same `refused` error naming the text, for the reason a metadata value like it is; a blank text is not refused, and keeps the board read it always had, confirmed by the same substring rule. A project query's text, and a document query's text when the query is scoped to no project, is that same board-scoped search for the same phrase in the same fields, refused on the same terms, every candidate confirmed by its kind and by the same substring rule, so it narrows exactly as a task's does; a document query scoped to one project sends no search, reads that project's sub-issues and confirms its text over them by the substring rule alone, so it is neither narrowed to whole words nor refused for a text with no letter or digit. A board draft is not an issue, so no text search lists one, a draft titled as a document included. |
161//! | `search_content` | **Supported,** on the same terms, `in:body`, over the visible body — the trailing metadata comment is not part of what the substring rule confirms. |
162//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
163//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
164//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
165//!
166//! Image writes use `POST https://uploads.github.com/user-attachments/assets` with `name`,
167//! `content_type` and the issue repository's numeric `repository_id` as query parameters,
168//! the source token as `Authorization: Bearer`, the image content type as `Content-Type`,
169//! and the raw bytes as the request body. The numeric repository id is read at most once
170//! per repository per source instance. A loopback API endpoint also selects the loopback
171//! uploads host; production needs no assets repository, commits or extra configuration.
172//! Every returned JSON `url` is read with the same token before a body is written: only
173//! a 2xx verification succeeds. A refusal names the asset, URL and HTTP status; an
174//! anonymous 404 does not invalidate an authenticated 200. An upload refusal names the
175//! asset and HTTP status and says the token type may not be accepted by the upload
176//! endpoint. A classic PAT was measured accepted; no acceptance claim is made about
177//! fine-grained PATs or OAuth tokens.
178//! The body keeps the authored alt text while `./<name>` becomes the attachment URL,
179//! and `onetaskgraph.assets` records `{sha256, url}` by name. A matching destination
180//! SHA-256 reuses its URL without an upload or verifying read; changed bytes are uploaded
181//! and verified afresh, in the repository the issue already lives in on an update.
182//! An uploaded image renders for the viewers GitHub lets read it, like the issue text beside it.
183//! Uploads take the same mutation spacing as content writes. Both uploads and verification
184//! reads wait out classified rate limits within the configured per-call retry budget,
185//! on the supplied clock; verifying reads take no mutation slot.
186//!
187//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
188//! source has documents and that its tasks have comments, both of which hold — and the three
189//! facts behind the uniform `Native` on the
190//! predicates beside it are recorded below rather than re-derived, because a reader who
191//! takes `Native` to mean *the remote service filters* will read that uniformity as a
192//! lie.
193//!
194//! First, the plugin contract defines `Support::Native` as *the source applies this
195//! predicate itself*, and says nothing about where it applies it. What the declaration
196//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
197//! — so that the engine may push it down and apply nothing of its own.
198//!
199//! Second, this source can keep that promise for every predicate at no additional API
200//! cost, because whichever of the reads below answers a query has already read every
201//! candidate that query will return before it filters anything. Filtering those items is
202//! in-process work over data already in hand.
203//!
204//! Third, six task predicates are asked of GitHub as a narrower question and the rest are
205//! applied in process over what that question returned. A project filter has a relationship — a
206//! project's tasks are that issue's sub-issues, and asking the issue for them is both cheaper
207//! and exact. Comment activity is the issue search's `updated:` qualifier. A text search, and
208//! a search for metadata values, is the board-scoped issue search carrying the text and each
209//! value as quoted phrases; an origin is the board's own field filter over its origin field
210//! beside the same search for the id. **The text search narrows, and that is this source's
211//! declared semantics:** GitHub matches whole words where the substring rule this source and
212//! the local Markdown source confirm with would match inside one, so an item holding the text
213//! only inside a longer word is never a candidate. Every item returned does contain the text.
214//! A project query's text, and a document query's scoped to no project, is that same search
215//! and narrows on the same terms, its candidates confirmed by their kind as well.
216//! GitHub's issue search offers no qualifier for a label set, a status column or a priority,
217//! so those three are applied in process over the candidates, and a query carrying none of
218//! the six narrowing predicates reads the board. Declaring one `Unsupported` would make the
219//! engine compensate for work this source has already done, and declaring `projects` native
220//! while ignoring the filter (which this source once did) silently returns another project's
221//! tasks, because the engine trusts the declaration and applies nothing locally.
222//!
223//! # The three ways this source reaches an item, and what each costs
224//!
225//! A board read is charged for what its *nested* connections could return rather than for
226//! what was asked, so one whole-board read costs the same whether the question was about
227//! one project or about all of them. That is why a question about one project is never
228//! answered by reading the board:
229//!
230//! | The question | What is sent | What it costs |
231//! | --- | --- | --- |
232//! | one item, by its own id | [`graphql::ISSUE`] — `node(id:)`, carrying the field definitions of the boards it sits on and the far ends of its `blockedBy`, which is what a write of it needs — and, when that node is a board draft, [`graphql::DRAFT`] — the draft and the one board item it is | the item |
233//! | one task with its first page of comments, for `task show` and a comment listing | [`graphql::ISSUE_DETAIL`] — the same `node(id:)` read with the issue's `comments` | the item and a page of its comments |
234//! | several tasks with their comments, for `task show-many` | [`graphql::ISSUE_DETAILS`] — [`DETAIL_BATCH`] aliased `node(id:)` fields per request | each item and a page of its comments |
235//! | the board's own id and field definitions, for a write whose item does not carry them | [`graphql::BOARD_FIELDS`] — the board's `id` and `fields`, and no `items` — or, for a create that needs the repository's id too, [`graphql::CREATION_CONTEXT`], both in one request | the board's fields |
236//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
237//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
238//! | which projects hold a text, or which documents do when no project narrows the question | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text as one quoted phrase, `in:title`, `in:body` or both, as a task's text is sent — walked to its end in pages of twenty | the issues that match |
239//! | which tasks were commented on since an instant | [`graphql::SEARCH_ISSUES`] — the same board-scoped search with an `updated:>=` qualifier — then [`graphql::ISSUE_COMMENTS`] for each candidate it names | the issues updated since, and their comments |
240//! | which tasks hold a text, or a metadata value | [`graphql::SEARCH_ISSUES`] — the board-scoped search with the text and each value as quoted phrases, `in:title`, `in:body` or both, and an `updated:>=` qualifier too when comment activity is asked for — in pages of twenty, only as many as the caller's rows need | the issues that match |
241//! | which tasks were copied from one origin | [`graphql::ORIGIN_LOOKUP`] — the board's own `items` under its field filter on the origin field, and the same board-scoped search for the id `in:body`, in one request, each paged at three | the carriers of that origin, which is one item |
242//! | every task, every document, every label, when nothing above narrows the question | [`graphql::BOARD`] — the board's own `items` — **and** [`graphql::SEARCH_ISSUES`], because neither enumeration of a board is complete alone; see [`GitHubProjectsSource::board`] | the board, twice over |
243//! | which board item one issue is, past the page that came with it | [`graphql::ISSUE_BOARD_ITEMS`] — that issue's own `projectItems` | one issue's memberships |
244//!
245//! The following standalone-ticket requests are pinned by the real CLI fixture journeys
246//! `follow_up_writes_resolve_each_item_once_and_batch_the_copy_fields` and
247//! `a_batched_detail_read_costs_one_request_and_one_point_per_detail_batch`, as request count
248//! equal to declared points equal to the row. They include the origin lookup and the
249//! field/repository discovery a create needs. A bound re-copy changes status, priority,
250//! content and metadata; comment recount means a subsequent detail read. Each request here
251//! costs one declared point. A membership beyond the embedded page can additionally require
252//! the one-point membership recovery described above. A bound re-copy of a task filed under a
253//! project adds one read, the engine confirming that project's link by its own id once per
254//! command; and the same-source far ends a write newly names — those that do not already block
255//! the item, whose own read answered for them — are read together by their own ids,
256//! [`DETAIL_BATCH`] to one [`graphql::ISSUE_DETAILS`] request, each new edge then one
257//! [`graphql::ADD_BLOCKED_BY`]. Both additions are rows of the table below, pinned by
258//! `a_bound_recopy_adds_one_project_read_and_batches_the_dependencies_it_newly_names`.
259//!
260//! **[`DETAIL_BATCH`] is 24**: the largest batch of [`graphql::ISSUE_DETAILS`] the node-count
261//! model prices at one point. Each aliased item is six of GitHub's aggregate, so 24 are 144,
262//! which rounds to one point, and 25 are 150, which rounds to two; `tests/point_cost.rs`
263//! holds both halves.
264//!
265//! **An existing item is written body last.** A bound re-copy and a `task update` send its
266//! board fields first — the `Status` option and the `Priority` together, in one request — then
267//! its parent and its `blockedBy`, and its title, body and state in one `updateIssue` last.
268//! GitHub runs no two requests as one, and runs a document's mutation fields in order without
269//! undoing an earlier field when a later one fails, so that order is what makes a write
270//! refused part-way leave the item's body, and every metadata key in it, exactly as it stood;
271//! the one piece of metadata written before the body, an origin a copy re-points, is put back
272//! when a later write is refused — and when putting it back is refused too, the write's own
273//! refusal names that key, what it now holds and what it held. `crates/onetaskgraph-github-projects-e2e/tests/e2e/write_order.rs` refuses each
274//! of those writes in turn, whole and as one aliased field failing after the one before it.
275//!
276//! **Two facts about GitHub the write rows rest on, each read off GitHub's published schema
277//! artifact <https://docs.github.com/public/fpt/schema.docs.graphql> on 2026-10-01 and pinned
278//! in `tests/fixtures/schema.graphql`, and the first then put to GitHub itself:**
279//!
280//! - **A board is accepted at creation but its item is not answered, so a create still files
281//!   the issue itself: a new copy is 5 requests, and 4 with `--create`.**
282//!   `CreateIssueInput.projectV2Ids: [ID!]` is declared there — "An array of Node IDs for
283//!   Projects V2 associated with this issue", `@possibleTypes(concreteTypes: ["ProjectV2"])`.
284//!   The credentialed journey `real_projects_v2_contract_writes_and_leaves_no_residue` was run
285//!   against a real board on 2026-10-01 with a create sending the board there and reading the
286//!   item off the payload's `Issue.projectItems`: every one of its four creates answered with
287//!   no item on the board, so each went on to [`graphql::ADD_TO_BOARD`], and the fourth was
288//!   refused "Content already exists in this project" — GitHub had filed the issue after
289//!   answering, and refuses a second filing rather than answering with the item it holds. A
290//!   create therefore sends no `projectV2Ids` and files the issue with
291//!   `addProjectV2ItemById`, the one call whose answer names the board item. The saving that is
292//!   real is the read before it: the board's fields and the repository's id together, in
293//!   [`graphql::CREATION_CONTEXT`], at the point the repository is known.
294//! - **A comment still reads its target first, so a comment is 2 requests.**
295//!   `AddCommentInput.subjectId: ID!` is declared there with
296//!   `@possibleTypes(concreteTypes: ["Issue", "PullRequest"], abstractType:
297//!   "IssueOrPullRequest")`. A board draft is no such subject and would be refused, but a
298//!   project's issue, a document's issue, an issue on no board of this source and a pull
299//!   request all are: GitHub writes the comment, so there is no refusal to map into "that is
300//!   not a task of this board". [`graphql::ISSUE`] before [`graphql::ADD_COMMENT`] is what
301//!   refuses those by name.
302//!
303//! | Verb | Requests / points | Documents |
304//! | --- | --- | --- |
305//! | new copy | 5 | ORIGIN_LOOKUP, CREATION_CONTEXT (the board's fields and the repository's id together), CREATE_ISSUE, ADD_TO_BOARD, UPDATE_FIELDS |
306//! | copy --create | 4 | CREATION_CONTEXT, CREATE_ISSUE, ADD_TO_BOARD, UPDATE_FIELDS: the new copy without its ORIGIN_LOOKUP |
307//! | bound copy | 3 | ISSUE (with the board's fields and the issue's `blockedBy`, so no BOARD_FIELDS or ISSUE_DEPENDENCIES), UPDATE_FIELDS, then UPDATE_ISSUE last |
308//! | bound copy, filed under a project | 4 | the bound copy's three, and one ISSUE of the destination project its link names, read once per command |
309//! | bound copy, newly naming n dependencies | + ceil(n / DETAIL_BATCH) + n | ISSUE_DETAILS for the far ends that do not already block the item, DETAIL_BATCH (24) to a request (one alone is ISSUE), then one ADD_BLOCKED_BY each; a far end already blocking it is answered by its own read and costs nothing |
310//! | comment | 2 | ISSUE, ADD_COMMENT: the target is read first, because GitHub accepts a comment on any issue or pull request (see below) |
311//! | detail | 1 | ISSUE_DETAIL: the item and its first page of comments, for `task show` and `task comment list`; `--no-comments` is ISSUE alone |
312//! | batched detail | ceil(n / DETAIL_BATCH) | ISSUE_DETAILS: `task show-many` of `n` items, DETAIL_BATCH (24) at a time, comments included or not |
313//! | copy --comments, n changed tasks | ceil(n / COPY_DETAIL_BATCH) | ISSUE_COPY_DETAILS: each task with its first page of comments and its `blockedBy`, COPY_DETAIL_BATCH (21) at a time; a task whose comments run past that page adds one ISSUE_DETAIL per further page, and a task skipped from its listing costs nothing beyond it |
314//! | recount | 1 | ISSUE_DETAIL |
315//! | status | 2 | ISSUE, UPDATE_FIELD; a terminal status additionally updates issue state |
316//! | priority | 2 | ISSUE, UPDATE_FIELD or CLEAR_FIELD, with stored priority in the mutation response |
317//! | content | 2 | ISSUE, UPDATE_ISSUE |
318//! | metadata | 2 | ISSUE, UPDATE_ISSUE |
319//! | update | 3 | `task update` naming any of title, body, metadata, status and priority — all five included: ISSUE, UPDATE_FIELDS (the status option and the priority together), UPDATE_ISSUE (title, body with its metadata slot, and state) last |
320//! | record only | 1 | ISSUE |
321//!
322//! <!-- github-search-paging:start -->
323//! Board-scoped text, metadata, project-name and comment-activity searches send every
324//! page at `first = 20` (SEARCH_PAGE_SIZE), the SEARCH_ISSUES document's one-point
325//! ceiling. A later page is sent only when `hasNextPage` is true and the caller still
326//! needs rows. A page is never resized to the rows still needed: GitHub orders one
327//! search differently at different page sizes, so one fixed size makes a paged walk
328//! send exactly the requests one whole read sends, and the answer's order is the order
329//! those pages arrive in. A page below twenty would cost the same one point, and GitHub
330//! prices this document by rows, so twenty-row pages cost per row what 100-row pages do.
331//! Project-name lookup continues until an exact match or exhaustion. A task limit bounds
332//! returned and fetched pages: a limit is sliced from the pages it needs, and local
333//! confirmation can require more candidates than matching rows. Walking all pages
334//! returns the whole answer. The opaque version-4 source cursor carries GitHub's page
335//! cursor and how far into that page the last answer stopped, and resumes in the same
336//! process or a new one, without duplicates or gaps. It carries no rows: one process
337//! sends each page's search once, and a new process re-reads only the page it resumes
338//! in, then sends a further page once, never as a re-read, only when its limit still
339//! needs rows. Every request either walk sends is the one a whole read sends for that page. Own writes replace stale index
340//! copies and complete missing rows at exhaustion. Cache entries are whole GitHub pages,
341//! so a small answer cannot truncate a wider question. Origin pages remain three; whole-board sizing is unchanged.
342//! Read-after-write is a per-process guarantee. A cursor resumed in a new process is
343//! not required to include the original process's writes still omitted by the index.
344//! <!-- github-search-paging:end -->
345//!
346//! The board half of an issue — its board item's id, its `Status` option and this
347//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
348//! an item reached any of those ways resolves through the same
349//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
350//! same status, the same labels and the same qualified id. That connection comes back a
351//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
352//! on the page in hand and — only if that page reports more of the connection — in the
353//! last row's read of that one issue's memberships, resumed from the page's own cursor and
354//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
355//! report, which is what keeps an id naming another repository's issue from being answered
356//! as an item of this board; and because the page is where the search starts rather than
357//! where it ends, that answer is one about a connection read to exhaustion and never about
358//! an unread page. Nothing costs the extra read but an issue on more boards than a page
359//! holds: an issue this board really does not hold reports no next page, so its
360//! memberships are already exhausted where they arrived.
361//!
362//! **No document here selects the board's own `Labels` field, and nothing is lost by
363//! that.** An item's labels are read from its content alone, wherever that content is
364//! reached: the three documents above select `Issue.labels` on the fragment, and
365//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
366//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
367//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
368//! create one, and `ProjectV2FieldValue` — the whole of what
369//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
370//! it from the content, for every content type it exists on, and there is nothing it can
371//! hold that the content does not already say: for an `Issue` it *is* that issue's own
372//! labels, so selecting it beside them unions a set with itself.
373//!
374//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
375//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
376//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
377//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
378//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
379//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
380//! label set, and that set is the fixture issue's own, by
381//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
382//! item whose content is a draft reports an empty set, by
383//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
384//! selection is held over [`graphql::DOCUMENTS`] by
385//! `no_document_selects_the_boards_own_labels_field`.
386//!
387//! The whole-board row is still the board's own item connection, and deliberately: a
388//! **draft** board item is not an issue, so no search can list one, and the reads that have
389//! to answer for the whole board are the ones whose cost is the board's size anyway.
390//!
391//! **A question about one item this source already names by id never lists the board.**
392//! Whether that item is on this board, and what its board fields are, is answered by reading
393//! that item — its own `Issue.projectItems`, walked to exhaustion by
394//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
395//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
396//! holds. That covers a write's destination, the project a new item is filed under, a
397//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
398//! and the delete that takes back an item a copy made. What such a write needs of the board
399//! and the item does not carry — the board's id, the `Status` and origin field definitions —
400//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
401//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
402//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
403//! minutes. Scanning this host's 842-item board has refused a document copy and an update
404//! even though the items' own reads named that board. A scan there gives the wrong answer
405//! as well as paying for every page. So a `board.items` lookup does not belong on any of
406//! those paths.
407//!
408//! **What a read may return is capped too, and that cap is on the document rather than on
409//! the board.** GitHub limits the number of nodes **one query may return** to
410//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
411//! an error naming the connection the count crossed at, not a slow or a partial result.
412//! Every board this source reads is refused the same way, so no board is too big for these
413//! documents and none is small enough to save one that is over.
414//!
415//! The count is arithmetic over the document's own text: each connection contributes the
416//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
417//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
418//! restate them — `github-graphql-node-count` implements them, and
419//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
420//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
421//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
422//! same text on every run and fails naming any that reaches the limit — so a connection
423//! added to a shared fragment is caught there rather than by GitHub.
424//!
425//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
426//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
427//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
428//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
429//! effectively squared there, which is why it is the one the limit is most sensitive to.
430//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
431//! page of memberships misses is recovered by one further read rather than refused, so it
432//! buys a bound every read pays for at the price of a request only a multi-board issue
433//! pays.
434//!
435//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
436//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
437//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
438//! `cost` is rate-limit points, metered per hour across everything one credential does; it
439//! is what the two limiters [`Limiter`] tells apart meter, and a document under
440//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
441//! that second number, and `tests/point_cost.rs` pins every document in
442//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
443//! one under, the pin itself is the check. The credentialed lane reconciles both figures
444//! against GitHub's own, off a probe it already sends.
445//!
446//! **What is pinned that way is a per-document price and never a session's.** The record in
447//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
448//! **requests** and **worst-case nodes** — and neither is points. What one whole session
449//! consumes of the hourly point allowance is observable only from a credentialed run's own
450//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
451//! and what `tests/live.rs` prints at the end of every run.
452//!
453//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
454//!
455//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
456//! enumerations of a board can supply one alone.** Resolving a node id is strongly
457//! consistent, so a read by id and a project's own sub-issues are already current. The
458//! other two are not, and they are behind by different amounts and in different directions:
459//!
460//! - GitHub's **issue search** is an index and answers a write made moments ago with the
461//!   value from before it — usually for a second or two.
462//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
463//!   on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
464//!   content withheld, absent, with the connection walked to its own `hasNextPage: false` —
465//!   for *minutes*, while `Issue.projectItems` names the same membership at once.
466//!
467//! That second one is a measurement rather than a caution. This repository's own
468//! credentialed journey writes a project and waits for the board to report it, then writes a
469//! task and waits for the same thing seconds later on the same board: the project wait is
470//! answered through the search and converged in two or three attempts in each of three runs,
471//! and the task wait is answered through `ProjectV2.items` and converged in none of them
472//! inside thirty. Separately, an item added to a second and larger board was read back by
473//! `Issue.projectItems` on that board's own id while every one of that connection's nine
474//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
475//! through the lagging one alone is what had a board read deny an issue that had certainly
476//! landed on it.
477//!
478//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
479//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
480//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
481//! search reports what the projection is behind on. What closes the last
482//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
483//! source answers is completed with what this process itself wrote, so an item created
484//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
485//! nothing is written down, and the record dies with the process. **A wait that has to
486//! observe GitHub's own data cannot be answered from that record** — which is why the
487//! credentialed journey asks through a source built afresh, and why the union above rather
488//! than a longer wait is what makes such a wait converge.
489//!
490//! **A narrowed read is the same bargain, stated for each of the three predicates it
491//! answers.** A read carrying a text, metadata or origin predicate asks GitHub's index rather
492//! than walking the board, and every such answer is completed with what this process wrote —
493//! its [`created`](GitHubProjectsSource::created) record and every existing item it wrote,
494//! each filtered by the same predicates as the rest — so an item this command wrote a moment
495//! ago is returned by a query that matches it whether or not the index has caught up. An item
496//! a caller holds the id of is read by that id, with `node(id:)`, which is strongly
497//! consistent. What is left is stated rather than papered over:
498//!
499//! | Read | Finds | Behind by |
500//! | --- | --- | --- |
501//! | text, metadata | the issue search for the phrases | what another process wrote in the last second or two, until GitHub indexes it |
502//! | origin, first read | the board's field filter over the origin field — every carrier, whichever release wrote it | what `ProjectV2.items` is behind on, which the measurements above put in minutes |
503//! | origin, second read | the issue search for the id in the body, where a write of this release mirrors it | a second or two, as any search |
504//! | origin, third read | this process's own writes | nothing |
505//!
506//! So an origin carrier another process added within the last second or two, before either
507//! index has it, can be missing from an origin query, and one written by the release before
508//! this one — its origin in the field alone — can be missing for as long as the board's own
509//! item connection is behind on it. A copy that must not duplicate its own earlier write
510//! relies on the link it records, not on either index. **A board draft is not an issue**, so
511//! a draft is never returned by a text, metadata or origin query, whatever it holds: no search
512//! lists one, the origin lookup drops any the board's own field filter names, and one this
513//! process wrote is not added back either.
514//!
515//! **The origin lives in the board field, and the body holds a mirror of it.** A write that
516//! carries an origin writes it to the `onetaskgraph.origin` text field and also into the
517//! body's metadata slot, so the issue search can find it in seconds. The field is
518//! authoritative: this source reads an item's origin from the field alone, so a slot that
519//! disagrees with it, or holds one where the field holds none, is never read as a second
520//! origin — and the release before this one reads the slot, drops that key's copy for the
521//! field's, and sees the same one origin.
522//!
523//! Filtering happens before paging, so a page of a filtered result is a page of the
524//! survivors rather than the survivors of a page. Label matching and the substring rule a
525//! text candidate is confirmed by answer the same question the same way the local Markdown
526//! source's do; which candidates a text search has to confirm is GitHub's word match, which
527//! is the one place the two sources can answer the same text differently.
528//!
529//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
530//! has one source, `capabilities`, and the note above is the reasoning behind it rather
531//! than a second copy of it: without the three facts recorded here a reader takes the
532//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
533//! crate's own capabilities test, which pins every field of it against a fully spelled-out
534//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
535//! fails to compile there rather than going unasserted. -->
536//! The fixture-server tests above run wherever this crate is selected; the credentialed
537//! lane runs in the same required check, beside them, and can fail it — it verifies the
538//! current schema, then drives every field of the table above against the real board. It builds its own fixture there — two projects, one task filed under each,
539//! one filed under neither, a label on one of the three and a closed status on another —
540//! because that shape is what tells an honoured predicate from an ignored one: a board
541//! holding a single project answers a project filter the same way whether or not this
542//! source applies it, which is exactly how the defect above went unseen.
543//!
544//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
545//! and the scratch repository `GH_PROJECTS_REPOSITORY` names:
546//! `nickderobertis/onetaskgraph-live-scratch`. It refuses the core repository before any
547//! session or request, independently of the live demand. Fix that variable in the machine's
548//! onetaskgraph `secrets.env` or the environment it pushes from, such as ai-orchestrator's
549//! `.env`. The targets are declared once in `onetaskgraph_github_live`, the lane's own
550//! policy crate; absent credentials or nominations otherwise skip.
551//!
552//! GitHub Actions artifacts carry `ci-<run id>-<attempt>-<micros>`, naming their writing
553//! run and attempt; invalid Actions identity refuses before writing. Other runs retain
554//! `<host>-<process>-<micros>`. Own cleanup matches the whole stamp. Machine residue stays
555//! the owning machine's lock sweep's; Linear always keeps that form and is unaffected.
556//! The hourly janitor is cleanup, never a test lane: scratch CI residue is removed only
557//! after its run reads back as `completed`, immediately before the listed-artifact batch.
558//! Failed or incomplete ownership reads preserve residue. A 24-hour waiting period
559//! is a margin, never the ownership authorisation. Cleanup leaves machine stamps
560//! and the board's `onetaskgraph.origin` field untouched.
561//!
562//! # What a session of requests costs, and where the report is
563//!
564//! This source records **every** request it sends into [`accounting::Accounting`], at
565//! `send_once` — the one place a request leaves this crate, which is why a read path added
566//! later is counted without anybody remembering to count it. That is the whole of what this
567//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
568//! session's spend is arrived at, and what it deliberately does not know are set out.
569//!
570//! What one whole session of the live journey costs, counted that way against this crate's
571//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
572//! reduction it came out of, and with what it does and does not say about rate-limit points.
573//!
574//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
575//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
576//! code path — no environment variable, no feature, no build configuration — because an
577//! instrument nobody switches on measures nothing, and
578//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
579//! source's counts the whole session rather than this source's share. The credentialed lane
580//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
581//! passed or failed.
582//!
583//! **A live session refuses to start unless the account can afford it.** Before it does any
584//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
585//! GitHub documents as not counting against the REST rate limit and which answers both of
586//! its budgets at once — and starts only if, for each of them, what remains minus this
587//! session's estimated cost is still at least
588//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
589//! allowance. A session that cannot **declines**: it did not run, so it is
590//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
591//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
592//! waiting for the budget to come back. The estimate is derived offline from
593//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
594//! which is also where the published rule that model rests on is cited; the accounting
595//! above records the gate's own read like any other request, and
596//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
597//!
598//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
599//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
600//! text, which is what lets it run on every platform and on a pull request from a fork with
601//! no credential — and that is what actually stops a regression merging. But an offline
602//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
603//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
604//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
605//! number of nodes this query may return"* and whose `cost` is what that document would
606//! spend, both for a document **without executing it**, and the lane asks it for every query
607//! document this source sends, under the largest bindings this source sends, and fails when
608//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
609//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
610//! one; the offline pins still cover it. It records what those calls reported about the
611//! account's own allowance, because whether asking is free is a thing to observe rather than
612//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
613//! and `cost` is metered against an hourly allowance the accounting above reads off a
614//! credentialed run's own response headers.
615//!
616//! **GitHub has two rate limiters and this source is refused by both, so nothing here
617//! treats them as one thing.** The primary budget is the hourly allowance `gh api
618//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
619//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
620//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
621//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
622//! one part of.
623#![deny(missing_docs)]
624
625use std::collections::BTreeMap;
626use std::sync::{Arc, Mutex};
627use std::time::Duration;
628
629use chrono::{DateTime, Utc};
630use onetaskgraph_plugin_api::{
631    Capabilities, Classification, Comment, CommentBody, CommentOrigin, Cursor, DependencyEdge,
632    DependencyEndpoint, DependencyKind, DependencySupport, Direction, Document, DocumentQuery,
633    Health, ItemKind, ItemWrite, Label, LabelFilter, Location, MetadataKey, Metering, NativeId,
634    NewComment, Page, PageRequest, Priority, Project, ProjectFilter, ProjectQuery, Repository,
635    SecretResolver, SharedClock, SourceError, SourceName, SourcePlugin, Status, StatusCategory,
636    StatusMapping, Support, Task, TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate,
637    TaskUpdateOutcome, TextFields, TextQuery, UnmappedStatus, UpdatedField, WriteSupport,
638    system_clock,
639};
640use reqwest::{Client, StatusCode, Url};
641use schemars::{Schema, schema_for};
642use secrecy::{ExposeSecret, SecretString};
643use serde::{Deserialize, Serialize};
644use serde_json::{Value, json};
645
646pub mod accounting;
647mod assets;
648pub mod change_token;
649mod visibility;
650
651use accounting::Accounting;
652
653/// The registry name for this plugin.
654pub const KIND: &str = "github-projects";
655/// GitHub's maximum connection page size.
656pub const MAX_PAGE_SIZE: u32 = 100;
657/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
658/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
659/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
660pub const SEARCH_PAGE_SIZE: u32 = 20;
661/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
662/// its comments: the largest batch the node-count model prices at one point.
663///
664/// Each aliased item is resolved once, and what GitHub charges for it is the connections
665/// under it — its labels, its page of board memberships, the field values of each of those
666/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
667/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
668/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
669/// one point and fails if one item more would still be priced at one.
670pub const DETAIL_BATCH: usize = 24;
671/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
672/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
673/// point.
674///
675/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
676/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
677/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
678/// if one item more would still be priced at one.
679pub const COPY_DETAIL_BATCH: usize = 21;
680
681/// The most nodes any one document this source sends may be asked to return.
682///
683/// GitHub's own published per-query ceiling, taken from
684/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
685/// workspace cannot hold a stale copy of somebody else's number. A query above it is
686/// **refused before it is executed**, whoever is asking and whatever board they are
687/// asking about — so this is a bound on the documents rather than a budget that runs out.
688///
689/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
690/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
691/// everything the credential does — two numbers against two limits, and this constant
692/// bounds only the first. The second is computed offline too, per document:
693/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
694/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
695/// lane. There is no constant like this one to hold a price under, because points are an
696/// hourly allowance rather than a per-call bound.
697///
698/// Neither is a session's price. What `session-cost.md` records of a whole session is its
699/// **requests** and its **worst-case nodes**; what a whole session spends in points is
700/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
701/// [`accounting`]. The module section on the three ways this source reaches an item says how
702/// the count is arrived at, and which of the page sizes below decide it.
703pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
704
705/// Nested connection size for the connections that hang off one item.
706///
707/// It multiplies through every document that reaches an item under a page — the count
708/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
709/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
710/// every document under these constants and fails naming any that reaches the limit, so
711/// raising this is caught there rather than by GitHub.
712const NESTED_PAGE_SIZE: u32 = 50;
713/// How many of one issue's board memberships are read when an issue is reached directly.
714///
715/// An issue reached through a search or through its own node id carries its board half in
716/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
717/// under a page of issues, so every point of it multiplies through the whole document and
718/// is paid for whether or not any issue is on a second board — which is why it is
719/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
720///
721/// **Three, because what a page misses is now recovered rather than refused**, and the
722/// recovery is what the value is chosen against. An issue whose entry for this board sits
723/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
724/// that page's own cursor — so the value trades a bound every read pays for a request only
725/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
726/// boards would pay that request *per issue*, which is order N against the one page per
727/// hundred issues a read costs today. At three it is only reached by an issue on four or
728/// more boards at once, which keeps the recovery path exceptional rather than routine for
729/// a plausible deployment.
730const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
731/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
732/// its two connections for.
733///
734/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
735/// second is a duplicate a copy already takes the first of. Both connections are walked to
736/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
737/// never what the answer is. It is small because every point of it is paid on every lookup,
738/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
739/// worst-case nodes than the one whole-board read they replaced.
740const ORIGIN_PAGE_SIZE: u32 = 3;
741
742pub use github_graphql_node_count::{NodeCountError, Variables};
743
744/// The largest value this source can bind to each page-size variable its documents name.
745///
746/// Every `first:` in [`graphql`] reads one of these four, and each is capped at the
747/// constant above it wherever a caller's own limit could reach it — `$first` at
748/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
749/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`. So this is the worst case a caller can drive this source to,
750/// not one configuration of it, which is what makes a bound computed under it a bound on
751/// every read.
752pub fn largest_page_sizes() -> Variables {
753    Variables::from([
754        ("first".to_owned(), MAX_PAGE_SIZE),
755        ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
756        ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
757        ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
758    ])
759}
760
761/// The most nodes `document` could be asked to return, by GitHub's published rules.
762///
763/// Computed offline from the document's own text under [`largest_page_sizes`] — no
764/// network, no credential and no schema — by
765/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
766/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
767/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
768///
769/// # Errors
770///
771/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
772/// no single operation, or binds a page size this source does not name — each of which is
773/// a defect in the document rather than a number.
774pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
775    node_count(document, &largest_page_sizes())
776}
777
778/// The most rate-limit points one call of `document` could spend, by GitHub's published
779/// rules.
780///
781/// Computed offline from the document's own text under [`largest_page_sizes`] — no
782/// network, no credential and no schema — by
783/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
784/// This is `cost`, metered **per hour** against the allowance one credential shares across
785/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
786/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
787/// under, so what `tests/point_cost.rs` does with it is pin every document in
788/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
789/// figures against GitHub's own reported `cost`.
790///
791/// # Errors
792///
793/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
794/// no single operation, or binds a page size this source does not name — each of which is
795/// a defect in the document rather than a number.
796pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
797    github_graphql_node_count::point_cost(document, &largest_page_sizes())
798}
799
800/// The most nodes `document` could be asked to return under `variables`.
801///
802/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
803/// [`accounting`] is this under the bindings one request really sent — one spelling of the
804/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
805/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
806///
807/// # Errors
808///
809/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
810/// no single operation, or binds a page size `variables` does not name.
811pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
812    github_graphql_node_count::node_count(document, variables)
813}
814
815/// The issue-title prefix that makes a board issue a document.
816///
817/// A GitHub Projects board has no document type — it holds issues — so the discriminator
818/// is the title, and this is the whole of it: an issue whose title begins with these bytes
819/// is a document and every other issue is the task or project the sub-issue rule makes it.
820///
821/// It is spelled **once**, here, and read rather than restated everywhere else — including
822/// by the shared journeys, which take it from this constant so a board fixture cannot
823/// drift from what this source reads. `docs/metadata.md` records the two consequences that
824/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
825/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
826/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
827pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
828
829/// Exact GraphQL query documents issued by this plugin.
830///
831/// Keeping the production documents here lets the pinned-schema test validate the same
832/// bytes that are sent to GitHub, rather than a test-only copy which could drift
833/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
834/// field, and its guarded caller always supplies the complete existing option set with ids.
835pub mod graphql {
836    /// The board half of one item: the field values every document here reads it from.
837    ///
838    /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
839    /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
840    /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
841    /// *the same value*, because
842    /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
843    /// one path. Three spellings of it is what would drift, so there is one.
844    ///
845    /// The `Status` option and this source's own origin text field are the whole of it. It
846    /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
847    /// content, so it holds nothing the content's own `labels` do not already say, and it
848    /// would sit a label connection two page sizes deep.
849    macro_rules! board_item_values {
850        () => {
851            r#"fieldValues(first:$nestedFirst){nodes{
852          ... on ProjectV2ItemFieldSingleSelectValue{name field{
853            ... on ProjectV2SingleSelectField{id name options{id name}}
854          }}
855          ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
856        }pageInfo{hasNextPage}}"#
857        };
858    }
859
860    /// The variable every document reading an issue declares for the relationship events its
861    /// change token counts, with its value as the default, so no caller binds it.
862    ///
863    /// A variable rather than a literal list because every argument a production document
864    /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
865    /// its default to the pinned enum. Its default is the six event types GitHub records a
866    /// relationship added or removed with: a dependency on either end, as `blockedBy` and
867    /// `blocking`, so one a person makes from the far end moves this end's count too; and a
868    /// project membership on the task, as its parent, which GitHub records there whichever
869    /// issue the move was made from. A sub-issue's events on its parent are not counted: a
870    /// project carries no task token, and a copy writes nothing of a task its own sub-issues
871    /// decide. [`super::change_token`] says why the count is in the token.
872    macro_rules! relationship_events_variable {
873        () => {
874            ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
875        };
876    }
877
878    /// Everything this source reads about one issue, wherever it reaches that issue.
879    ///
880    /// A macro rather than a constant so the three documents below can `concat!` it: one
881    /// spelling of these fields is what makes an issue read through the board-scoped
882    /// search, through its own node id, and through its project's sub-issue relationship
883    /// resolve to *the same* item, which is the whole of what
884    /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
885    ///
886    /// `projectItems` is what carries the board half of an issue: the board item's own id
887    /// and the [`board_item_values!`] above — the `Status` option and this source's origin
888    /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
889    /// issue rather than on the board, which is what makes the cost of a read proportional
890    /// to what was asked for instead of to the board's size.
891    ///
892    /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
893    /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
894    /// not on that page: a page here is where the search for the entry starts rather than
895    /// where it ends.
896    ///
897    /// It does **not** select the board's `Labels` field value, and that is the whole of
898    /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
899    /// a label connection there sits under `fieldValues` under `projectItems` under a page
900    /// of issues, spending `$nestedFirst` twice down one path, and took
901    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
902    /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
903    /// above, and that connection is where every label this source reports comes from. No
904    /// document in this module selects the board field any longer, [`BOARD`] included; the
905    /// module documentation records why nothing it could have held is lost.
906    macro_rules! board_issue {
907        () => {
908            concat!(
909                r#" fragment BoardIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount}
910      labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
911      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
912        "#,
913                board_item_values!(),
914                r#"}pageInfo{hasNextPage endCursor}}}"#
915            )
916        };
917    }
918
919    /// Every issue of one board, found by a search scoped to that board.
920    ///
921    /// This is how the projects a board holds are listed, and it selects no `items`
922    /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
923    /// container walked page by page, so nothing nested inside a board item is paid for.
924    /// Which of the issues it returns is a project is then read off `parent` — GitHub
925    /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
926    /// discriminator has to be applied to the field, which is a scalar on the issue and
927    /// costs nothing.
928    pub const SEARCH_ISSUES: &str = concat!(
929        r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
930        relationship_events_variable!(),
931        r#"){
932      search(query:$search,type:$type,first:$first,after:$after){
933        pageInfo{hasNextPage endCursor}
934        nodes{__typename ...BoardIssue}
935      }
936    }"#,
937        board_issue!()
938    );
939
940    /// What a dependency read selects of each far end: enough to say which kind of item it
941    /// is, its body included for the kind marker.
942    macro_rules! related_issue {
943        () => {
944            " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
945        };
946    }
947
948    /// One issue by its own node id, which is what a qualified id names here — with what a
949    /// write of it needs and the issue does not carry in `board_issue!`: the field
950    /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
951    ///
952    /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
953    /// answers a write made moments ago with the value from before it, and resolving a node
954    /// id does not.
955    ///
956    /// **Why those two ride here and not on the fragment.** A copy or an update of an item
957    /// reads it by its own id, and with them that one read answers everything the write
958    /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
959    /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
960    /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
961    /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
962    /// they sit under one item, and this read is still one point.
963    pub const ISSUE: &str = concat!(
964        r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
965        relationship_events_variable!(),
966        r#"){
967      node(id:$id){__typename ...BoardIssue ... on Issue{
968        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
969          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
970          ... on ProjectV2Field{__typename id name dataType}
971        }pageInfo{hasNextPage}}}}}
972        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
973      }}
974    }"#,
975        board_issue!(),
976        related_issue!()
977    );
978
979    /// One project's tasks: the sub-issues of the issue that project is, each with a page of
980    /// what blocks it.
981    ///
982    /// The work this costs is the project's own size. Nothing about it grows as the board
983    /// gains projects, or as those projects gain tasks.
984    ///
985    /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
986    /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
987    /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
988    /// costs grow with its tasks rather than with the pages of them. Carried here, a task
989    /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
990    /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
991    /// It adds a connection under each issue of the page — one rate-limit point per page,
992    /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
993    /// answer questions that never read an edge.
994    pub const SUB_ISSUES: &str = concat!(
995        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
996        relationship_events_variable!(),
997        r#"){
998      node(id:$id){__typename
999        ... on Issue{subIssues(first:$first,after:$after){
1000          pageInfo{hasNextPage endCursor}
1001          nodes{__typename ...BoardIssue ... on Issue{
1002            blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1003          }}
1004        }}}
1005    }"#,
1006        board_issue!(),
1007        related_issue!()
1008    );
1009
1010    /// What a read of the board's own `items` selects of each item's content.
1011    ///
1012    /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1013    /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1014    /// content by one spelling.
1015    macro_rules! board_item_content {
1016        () => {
1017            r#" content{
1018        ... on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} commentCount:comments{totalCount} relationshipEvents:timelineItems(itemTypes:$relationshipEvents){totalCount} labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}}
1019        ... on PullRequest{__typename id}
1020        ... on DraftIssue{__typename id title body createdAt updatedAt}
1021      }"#
1022        };
1023    }
1024
1025    /// Reads the board's fields and one page of its items.
1026    pub const BOARD: &str = concat!(
1027        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1028        relationship_events_variable!(),
1029        r#"){
1030      owner:repositoryOwner(login:$owner){
1031        ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1032      }
1033    } fragment Board on ProjectV2 { id title
1034      fields(first:$nestedFirst){nodes{
1035        ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1036        ... on ProjectV2Field{__typename id name dataType}
1037      }pageInfo{hasNextPage}}
1038      items(first:$first,after:$after){nodes{id updatedAt "#,
1039        board_item_values!(),
1040        board_item_content!(),
1041        r#"} pageInfo{hasNextPage endCursor}}
1042    }"#
1043    );
1044
1045    /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1046    /// the board.
1047    ///
1048    /// **`originItems`** is the board's own items narrowed by its own field filter —
1049    /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1050    /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1051    /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1052    /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1053    /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1054    /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1055    /// fresh `addProjectV2ItemById` the way that connection does.
1056    ///
1057    /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1058    /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1059    /// indexes that comment, and the index catches up with a write in a second or two rather
1060    /// than in minutes, so it finds a carrier another process wrote that the first read is
1061    /// still behind on.
1062    ///
1063    /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1064    /// — and resumes from its own cursor; a connection already walked to its end is resumed
1065    /// from its last cursor, which answers an empty page. Every candidate either read returns
1066    /// is confirmed against its own origin field before it is reported, so a token match of
1067    /// the search or anything else the filter admits never is.
1068    ///
1069    /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1070    /// own whole reads counts this one among them.
1071    pub const ORIGIN_LOOKUP: &str = concat!(
1072        r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1073        relationship_events_variable!(),
1074        r#"){
1075      originItems:repositoryOwner(login:$owner){
1076        ... on ProjectV2Owner{projectV2(number:$number){
1077          items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1078        board_item_values!(),
1079        board_item_content!(),
1080        r#"} pageInfo{hasNextPage endCursor}}
1081        }}
1082      }
1083      search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1084        pageInfo{hasNextPage endCursor}
1085        nodes{__typename ...BoardIssue}
1086      }
1087    }"#,
1088        board_issue!()
1089    );
1090
1091    /// The board's own id and field definitions, and not one of its items.
1092    ///
1093    /// What a write needs of the board when the item it writes does not say: the id a field
1094    /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1095    /// origin fields. It selects no `items`, so what it costs is the board's field list
1096    /// however many items the board holds — and it decides nothing about which items those
1097    /// are, which is the question a read of one item by its own id answers instead.
1098    ///
1099    /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1100    /// board's item reads by their root counts this one among them.
1101    pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1102      boardFields:repositoryOwner(login:$owner){
1103        ... on ProjectV2Owner{projectV2(number:$number){id
1104          fields(first:$nestedFirst){nodes{
1105            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1106            ... on ProjectV2Field{__typename id name dataType}
1107          }pageInfo{hasNextPage}}
1108        }}
1109      }
1110    }"#;
1111
1112    /// One board draft by its own node id, with the board item it sits in.
1113    ///
1114    /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1115    /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1116    /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1117    /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1118    /// board listing hands it to, and nothing has to list the board to find one.
1119    pub const DRAFT: &str = concat!(
1120        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1121      node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1122        projectV2Items(first:$boardItems){nodes{id project{id number}
1123        "#,
1124        board_item_values!(),
1125        r#"}pageInfo{hasNextPage endCursor}}}}
1126    }"#
1127    );
1128
1129    /// One issue's board memberships alone, walked past the page a read of it carried.
1130    ///
1131    /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1132    /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1133    /// boards than that page holds may have this board's entry past its end. This asks that
1134    /// one issue for its memberships and nothing else — the caller already holds the issue —
1135    /// so an answer of "this board does not hold it" is only ever given about a connection
1136    /// read to exhaustion.
1137    ///
1138    /// It selects the board item's id, its project number and the same
1139    /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1140    /// very same resolver: an issue recovered this way reports the same title, the same
1141    /// status, the same labels and the same qualified id as one whose entry was on the
1142    /// page.
1143    ///
1144    /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1145    /// multiplies through it and the membership connection can be walked at
1146    /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1147    /// further request for any issue a person really keeps.
1148    pub const ISSUE_BOARD_ITEMS: &str = concat!(
1149        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1150      node(id:$id){
1151        ... on Issue{projectItems(first:$first,after:$after){
1152          nodes{id updatedAt project{id number}
1153        "#,
1154        board_item_values!(),
1155        r#"}
1156          pageInfo{hasNextPage endCursor}}}
1157      }
1158    }"#
1159    );
1160    /// Whether the board's Project is public — half of what decides whether a write here can
1161    /// be read by anybody. Needs the `read:project` scope.
1162    pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1163    /// Resolves the configured repository's node id, which creating an issue requires.
1164    pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1165    /// What creating an issue needs and has not read yet: the board's own id and field
1166    /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1167    /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1168    ///
1169    /// Sent at the point a create knows which repository it is for, when neither half is
1170    /// already known to this process; a create needing only one of them sends that one's own
1171    /// document. Neither half is kept past the process: a field's option ids are re-minted by
1172    /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1173    /// status.
1174    pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1175      boardFields:repositoryOwner(login:$owner){
1176        ... on ProjectV2Owner{projectV2(number:$number){id
1177          fields(first:$nestedFirst){nodes{
1178            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1179            ... on ProjectV2Field{__typename id name dataType}
1180          }pageInfo{hasNextPage}}
1181        }}
1182      }
1183      repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1184    }"#;
1185    /// Reads both dependency directions for one issue, with each far end's own kind — and
1186    /// the issue's own body, which is where an edge to another source is recorded, so that
1187    /// half of a dependency read needs no second read of the issue or of the board.
1188    pub const ISSUE_DEPENDENCIES: &str = concat!(
1189        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1190      ... on Issue{body
1191        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1192        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1193      }}}"#,
1194        related_issue!()
1195    );
1196    /// Creates one issue in the configured repository, on no board: [`ADD_TO_BOARD`] files
1197    /// it. `CreateIssueInput.projectV2Ids` is not sent — see the crate's notes on what GitHub
1198    /// answered when it was.
1199    pub const CREATE_ISSUE: &str =
1200        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1201    /// Puts an existing issue on the configured board.
1202    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1203    /// Updates an issue's visible fields and its open or closed state in one call.
1204    pub const UPDATE_ISSUE: &str =
1205        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1206    /// Updates an existing draft's user-visible fields.
1207    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1208    /// Updates a text or single-select value on one project item.
1209    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}}}}}}}}"#;
1210    /// Writes up to six board fields and clears up to three in one ordered mutation: every
1211    /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1212    /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1213    pub const UPDATE_FIELDS: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$second:UpdateProjectV2ItemFieldValueInput!,$third:UpdateProjectV2ItemFieldValueInput!,$fourth:UpdateProjectV2ItemFieldValueInput!,$fifth:UpdateProjectV2ItemFieldValueInput!,$sixth:UpdateProjectV2ItemFieldValueInput!,$clear:ClearProjectV2ItemFieldValueInput!,$clearSecond:ClearProjectV2ItemFieldValueInput!,$clearThird:ClearProjectV2ItemFieldValueInput!,$writeFirst:Boolean!,$writeSecond:Boolean!,$writeThird:Boolean!,$writeFourth:Boolean!,$writeFifth:Boolean!,$writeSixth:Boolean!,$writeClear:Boolean!,$writeClearSecond:Boolean!,$writeClearThird:Boolean!){updateProjectV2ItemFieldValue(input:$input) @include(if:$writeFirst){projectV2Item{id}} second:updateProjectV2ItemFieldValue(input:$second) @include(if:$writeSecond){projectV2Item{id}} third:updateProjectV2ItemFieldValue(input:$third) @include(if:$writeThird){projectV2Item{id}} fourth:updateProjectV2ItemFieldValue(input:$fourth) @include(if:$writeFourth){projectV2Item{id}} fifth:updateProjectV2ItemFieldValue(input:$fifth) @include(if:$writeFifth){projectV2Item{id}} sixth:updateProjectV2ItemFieldValue(input:$sixth) @include(if:$writeSixth){projectV2Item{id}} cleared:clearProjectV2ItemFieldValue(input:$clear) @include(if:$writeClear){projectV2Item{id}} clearedSecond:clearProjectV2ItemFieldValue(input:$clearSecond) @include(if:$writeClearSecond){projectV2Item{id}} clearedThird:clearProjectV2ItemFieldValue(input:$clearThird) @include(if:$writeClearThird){projectV2Item{id}}}"#;
1214    /// Clears one project item's value of one field, which is what a `none` priority is.
1215    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}}}}}}}}"#;
1216    /// Creates one field: a single-select one with its options, or a text one for a projected
1217    /// metadata value. Only the guarded field setup may use this document, and only for a
1218    /// field the board lacks.
1219    pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1220    /// Replaces a single-select field's options. Only the guarded field setup — the
1221    /// `status-options` and `fields` operations — may use this document, because GitHub
1222    /// treats the input as the complete option list.
1223    pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1224    /// A fresh snapshot of the Status field and every board item's assignment.
1225    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}}}}}}"#;
1226    /// Files one issue under another as a sub-issue, which is what project membership is.
1227    pub const ADD_SUB_ISSUE: &str =
1228        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1229    /// Takes one issue back out of its parent.
1230    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1231    /// Adds GitHub's native issue blocked-by relationship.
1232    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1233    /// Removes one native issue blocked-by relationship.
1234    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1235    /// Deletes one issue, which takes its board item with it.
1236    ///
1237    /// The engine sends this in one situation only: undoing a copy that could not finish,
1238    /// over the items that same copy created. Deleting the issue removes the board item
1239    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1240    pub const DELETE_ISSUE: &str =
1241        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1242
1243    /// Everything this source reads about one issue comment, wherever it reaches one.
1244    ///
1245    /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1246    /// and a comment just edited are handed to one mapper, so they are selected by one
1247    /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1248    /// longer exists, and `login` is the one member every kind of actor carries.
1249    macro_rules! issue_comment {
1250        () => {
1251            "id author{login} createdAt updatedAt body url"
1252        };
1253    }
1254
1255    /// One task's comments: a page of its issue's own `comments` connection.
1256    ///
1257    /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1258    /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1259    /// list every time somebody edited it; left unordered the connection answers in the order
1260    /// the comments were written, which is the order GitHub documents for the same collection
1261    /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1262    /// node count and the caller's own page size is pushed straight down.
1263    pub const ISSUE_COMMENTS: &str = concat!(
1264        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1265        issue_comment!(),
1266        r#"}pageInfo{hasNextPage endCursor}}}}}"#
1267    );
1268    /// One issue by its own node id, with a page of its comments: what `task show` and a
1269    /// comment listing read, in one request.
1270    ///
1271    /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1272    /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1273    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1274    /// connection there would multiply through both of those documents' price, and neither
1275    /// needs one.
1276    pub const ISSUE_DETAIL: &str = concat!(
1277        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1278        relationship_events_variable!(),
1279        r#"){
1280      node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1281        issue_comment!(),
1282        r#"}pageInfo{hasNextPage endCursor}}}}
1283    }"#,
1284        board_issue!()
1285    );
1286
1287    /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1288    /// page of its comments when `$comments` asks for them.
1289    macro_rules! issue_details_alias {
1290        ($n:literal) => {
1291            concat!(
1292                "\n      i",
1293                stringify!($n),
1294                ":node(id:$id",
1295                stringify!($n),
1296                "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1297                issue_comment!(),
1298                "}pageInfo{hasNextPage endCursor}}}}"
1299            )
1300        };
1301    }
1302
1303    /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1304    /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1305    ///
1306    /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1307    /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1308    /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1309    /// neither — so every connection under it would be priced at nothing and the pin in
1310    /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1311    /// one-item read the model already prices, so the batch costs what its aliases cost.
1312    ///
1313    /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1314    /// slots it has no item for to the last item it does, and reads that item again; the
1315    /// price is the document's, whatever its variables, so a short batch costs what a full
1316    /// one does and nothing more.
1317    pub const ISSUE_DETAILS: &str = concat!(
1318        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!"#,
1319        relationship_events_variable!(),
1320        r#"){"#,
1321        issue_details_alias!(0),
1322        issue_details_alias!(1),
1323        issue_details_alias!(2),
1324        issue_details_alias!(3),
1325        issue_details_alias!(4),
1326        issue_details_alias!(5),
1327        issue_details_alias!(6),
1328        issue_details_alias!(7),
1329        issue_details_alias!(8),
1330        issue_details_alias!(9),
1331        issue_details_alias!(10),
1332        issue_details_alias!(11),
1333        issue_details_alias!(12),
1334        issue_details_alias!(13),
1335        issue_details_alias!(14),
1336        issue_details_alias!(15),
1337        issue_details_alias!(16),
1338        issue_details_alias!(17),
1339        issue_details_alias!(18),
1340        issue_details_alias!(19),
1341        issue_details_alias!(20),
1342        issue_details_alias!(21),
1343        issue_details_alias!(22),
1344        issue_details_alias!(23),
1345        "\n    }",
1346        board_issue!()
1347    );
1348
1349    /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1350    /// page of its comments and the issues it is blocked by.
1351    macro_rules! issue_copy_details_alias {
1352        ($n:literal) => {
1353            concat!(
1354                "\n      i",
1355                stringify!($n),
1356                ":node(id:$id",
1357                stringify!($n),
1358                "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1359                issue_comment!(),
1360                "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1361            )
1362        };
1363    }
1364
1365    /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1366    /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1367    /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1368    /// at a time, so a copy of a changed task costs this read and nothing else.
1369    ///
1370    /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1371    /// what `task show-many` is priced by and which reads no edge.
1372    pub const ISSUE_COPY_DETAILS: &str = concat!(
1373        r#"query($id0:ID!,$id1:ID!,$id2:ID!,$id3:ID!,$id4:ID!,$id5:ID!,$id6:ID!,$id7:ID!,$id8:ID!,$id9:ID!,$id10:ID!,$id11:ID!,$id12:ID!,$id13:ID!,$id14:ID!,$id15:ID!,$id16:ID!,$id17:ID!,$id18:ID!,$id19:ID!,$id20:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1374        relationship_events_variable!(),
1375        r#"){"#,
1376        issue_copy_details_alias!(0),
1377        issue_copy_details_alias!(1),
1378        issue_copy_details_alias!(2),
1379        issue_copy_details_alias!(3),
1380        issue_copy_details_alias!(4),
1381        issue_copy_details_alias!(5),
1382        issue_copy_details_alias!(6),
1383        issue_copy_details_alias!(7),
1384        issue_copy_details_alias!(8),
1385        issue_copy_details_alias!(9),
1386        issue_copy_details_alias!(10),
1387        issue_copy_details_alias!(11),
1388        issue_copy_details_alias!(12),
1389        issue_copy_details_alias!(13),
1390        issue_copy_details_alias!(14),
1391        issue_copy_details_alias!(15),
1392        issue_copy_details_alias!(16),
1393        issue_copy_details_alias!(17),
1394        issue_copy_details_alias!(18),
1395        issue_copy_details_alias!(19),
1396        issue_copy_details_alias!(20),
1397        "\n    }",
1398        board_issue!(),
1399        related_issue!()
1400    );
1401
1402    /// Which issue one comment is on, read before that comment is edited or removed.
1403    ///
1404    /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1405    /// comment id given against the wrong task would change a comment on another issue.
1406    pub const COMMENT_ISSUE: &str =
1407        r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1408    /// Adds one comment to an issue, signed as the account the token belongs to.
1409    pub const ADD_COMMENT: &str = concat!(
1410        r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1411        issue_comment!(),
1412        r#"}}}}"#
1413    );
1414    /// Replaces the body of one issue comment.
1415    pub const UPDATE_COMMENT: &str = concat!(
1416        r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
1417        issue_comment!(),
1418        r#"}}}"#
1419    );
1420    /// Removes one issue comment. Its payload carries nothing about the comment it removed.
1421    pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
1422
1423    /// Every document above, with what this source is doing when it sends one.
1424    ///
1425    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
1426    /// name the call that was refused, and a `match` with a catch-all arm would answer a
1427    /// document added later with "talking to GitHub" and never say so.
1428    ///
1429    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
1430    /// const` here that this list omits, so the two cannot part — which is the same guard
1431    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
1432    pub const DOCUMENTS: [(&str, &str); 35] = [
1433        (SEARCH_ISSUES, "searching this board's issues"),
1434        (ISSUE, "reading one issue"),
1435        (
1436            ISSUE_BOARD_ITEMS,
1437            "reading one issue's board memberships past the page it came with",
1438        ),
1439        (SUB_ISSUES, "reading a project's tasks"),
1440        (BOARD, "reading the board"),
1441        (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
1442        (BOARD_FIELDS, "reading the board's fields"),
1443        (DRAFT, "reading one draft"),
1444        (REPOSITORY, "reading the destination repository"),
1445        (
1446            PROJECT_VISIBILITY,
1447            "reading whether the board's project is public",
1448        ),
1449        (
1450            CREATION_CONTEXT,
1451            "reading the board's fields and the destination repository",
1452        ),
1453        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
1454        (CREATE_ISSUE, "creating an issue"),
1455        (ADD_TO_BOARD, "adding an issue to the board"),
1456        (UPDATE_ISSUE, "updating an issue"),
1457        (UPDATE_DRAFT, "updating a draft item"),
1458        (UPDATE_FIELD, "writing a board field"),
1459        (UPDATE_FIELDS, "writing board fields together"),
1460        (CLEAR_FIELD, "clearing a board field"),
1461        (
1462            CREATE_FIELD,
1463            "creating a board single-select field with its options",
1464        ),
1465        (
1466            STATUS_OPTIONS_SNAPSHOT,
1467            "snapshotting board Status options and assignments",
1468        ),
1469        (
1470            STATUS_OPTIONS_UPDATE,
1471            "safely replacing the board Status option list",
1472        ),
1473        (ADD_SUB_ISSUE, "filing an issue under its project"),
1474        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
1475        (ADD_BLOCKED_BY, "recording a dependency"),
1476        (REMOVE_BLOCKED_BY, "removing a dependency"),
1477        (DELETE_ISSUE, "deleting an issue"),
1478        (ISSUE_COMMENTS, "reading a task's comments"),
1479        (ISSUE_DETAIL, "reading one issue with its comments"),
1480        (
1481            ISSUE_DETAILS,
1482            "reading a batch of issues with their comments",
1483        ),
1484        (
1485            ISSUE_COPY_DETAILS,
1486            "reading a batch of issues with their comments and dependencies for a copy",
1487        ),
1488        (COMMENT_ISSUE, "reading which issue a comment is on"),
1489        (ADD_COMMENT, "adding a comment"),
1490        (UPDATE_COMMENT, "editing a comment"),
1491        (DELETE_COMMENT, "deleting a comment"),
1492    ];
1493}
1494
1495/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
1496/// under, the variable carrying its input, and the boolean variable that includes it.
1497#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1498pub struct FieldSlot {
1499    /// The key of the answer's `data` this field's payload is under.
1500    pub alias: &'static str,
1501    /// The variable carrying this field's input.
1502    pub variable: &'static str,
1503    /// The boolean variable whose `true` sends this field.
1504    pub include: &'static str,
1505}
1506
1507/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
1508pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
1509    FieldSlot {
1510        alias: "updateProjectV2ItemFieldValue",
1511        variable: "input",
1512        include: "writeFirst",
1513    },
1514    FieldSlot {
1515        alias: "second",
1516        variable: "second",
1517        include: "writeSecond",
1518    },
1519    FieldSlot {
1520        alias: "third",
1521        variable: "third",
1522        include: "writeThird",
1523    },
1524    FieldSlot {
1525        alias: "fourth",
1526        variable: "fourth",
1527        include: "writeFourth",
1528    },
1529    FieldSlot {
1530        alias: "fifth",
1531        variable: "fifth",
1532        include: "writeFifth",
1533    },
1534    FieldSlot {
1535        alias: "sixth",
1536        variable: "sixth",
1537        include: "writeSixth",
1538    },
1539];
1540
1541/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
1542pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
1543    FieldSlot {
1544        alias: "cleared",
1545        variable: "clear",
1546        include: "writeClear",
1547    },
1548    FieldSlot {
1549        alias: "clearedSecond",
1550        variable: "clearSecond",
1551        include: "writeClearSecond",
1552    },
1553    FieldSlot {
1554        alias: "clearedThird",
1555        variable: "clearThird",
1556        include: "writeClearThird",
1557    },
1558];
1559
1560/// Which of GitHub's two rate limiters refused a request.
1561///
1562/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
1563/// secondary one — so an operator told the wrong one takes the wrong next step, which is
1564/// the whole reason this is carried rather than collapsed into "rate limited".
1565#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1566enum Limiter {
1567    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
1568    Primary,
1569    /// The burst limiter over content-generating requests, which nothing reports.
1570    Secondary,
1571}
1572
1573/// The wordings GitHub answers a secondary rate limit with.
1574///
1575/// It sends them under a forbidden status, under a too-many-requests status, and inside
1576/// the `errors` of a *successful* response, which is why the text is what this matches on
1577/// rather than the status. `abuse detection` is the wording GitHub used before the
1578/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
1579/// what a burst of content creation is refused with.
1580///
1581/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
1582/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
1583/// when, and the drift gate reconciles the two lists both ways. Public for that gate
1584/// alone — a caller has no use for it, and matching on a refusal is this source's job.
1585pub const SECONDARY_WORDINGS: [&str; 5] = [
1586    "secondary rate limit",
1587    "temporarily blocked from content creation",
1588    "abuse detection",
1589    "submitted too quickly",
1590    "exceeded a secondary",
1591];
1592
1593/// The wordings GitHub answers an exhausted primary budget with.
1594///
1595/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
1596/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
1597/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
1598/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
1599/// two phrases is a substring of it, so without it that answer read as a refusal that will
1600/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
1601/// one reason.
1602pub const PRIMARY_WORDINGS: [&str; 4] = [
1603    "api rate limit exceeded",
1604    "api rate limit already exceeded",
1605    "rate limit exceeded",
1606    "rate_limited",
1607];
1608
1609/// What a response *says about itself*, which is the only place a refusal can be read.
1610///
1611/// Deliberately not the whole response body. A board is a place people write about their
1612/// own work, and a task on it titled "the secondary rate limit" would, matched across the
1613/// raw text, turn a perfectly good answer into a refusal this source then waited out and
1614/// reported. So the item data is never read: what is read is GitHub's own REST-style
1615/// `message` envelope, which is what a forbidden status carries, and the `message` and
1616/// `type` of each GraphQL error, which is where a *successful* response says it.
1617///
1618/// A body that is not JSON at all has nothing structured to read, so only a failing
1619/// response's own text is taken — a successful response that is not JSON is malformed
1620/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
1621fn refusal_wording(status: StatusCode, body: &str) -> String {
1622    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
1623        return if status.is_success() {
1624            String::new()
1625        } else {
1626            body.to_owned()
1627        };
1628    };
1629    let mut said: Vec<&str> = parsed
1630        .get("message")
1631        .and_then(Value::as_str)
1632        .into_iter()
1633        .collect();
1634    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
1635        for error in errors {
1636            said.extend(
1637                ["message", "type"]
1638                    .into_iter()
1639                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
1640            );
1641        }
1642    }
1643    said.join("; ")
1644}
1645
1646impl Limiter {
1647    /// Which limiter refused this response, or `None` when none of them did.
1648    ///
1649    /// The wording is read first and the status only decides what carries none of it,
1650    /// because GitHub answers a secondary limit with a forbidden status far more often
1651    /// than with too-many-requests — while a forbidden status saying nothing about a limit
1652    /// really is a credential this token lacks.
1653    ///
1654    /// A response is a refusal because of its status or its own wording. A spent budget
1655    /// only ever explains one; it never turns an answer into a refusal.
1656    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
1657        let normalized = refusal_wording(status, body).to_ascii_lowercase();
1658        if SECONDARY_WORDINGS
1659            .iter()
1660            .any(|wording| normalized.contains(wording))
1661        {
1662            return Some(Self::Secondary);
1663        }
1664        if status == StatusCode::TOO_MANY_REQUESTS {
1665            return Some(Self::Primary);
1666        }
1667        // An exhausted budget *explains* a response that failed; it does not make one that
1668        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
1669        // request the budget allowed as well as on the ones it then refuses, so reading
1670        // the header alone threw away a good answer — and, once refusals were retried,
1671        // replayed a request that had already taken effect.
1672        if !status.is_success() && budget_exhausted {
1673            return Some(Self::Primary);
1674        }
1675        // A successful response saying it: GitHub reports a GraphQL rate limit in the
1676        // `errors` of an HTTP 200, where nothing about the status says so at all.
1677        if status.is_success()
1678            && PRIMARY_WORDINGS
1679                .iter()
1680                .any(|wording| normalized.contains(wording))
1681        {
1682            return Some(Self::Primary);
1683        }
1684        None
1685    }
1686
1687    /// What this limiter is called where an operator can look it up.
1688    const fn name(self) -> &'static str {
1689        match self {
1690            Self::Primary => "GitHub's primary API rate limit",
1691            Self::Secondary => "GitHub's secondary rate limit",
1692        }
1693    }
1694
1695    /// What the endpoint an operator would go and check says about this limiter.
1696    const fn where_to_look(self) -> &'static str {
1697        match self {
1698            Self::Primary => {
1699                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
1700                 comes back."
1701            }
1702            Self::Secondary => {
1703                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
1704                 primary budget and does not report this one, so budget showing there says \
1705                 nothing about this refusal, and every further attempt extends it."
1706            }
1707        }
1708    }
1709
1710    /// The next step this limiter actually calls for.
1711    const fn what_to_do(self) -> &'static str {
1712        match self {
1713            Self::Primary => {
1714                "wait for the reset `gh api rate_limit` reports, then run the command again."
1715            }
1716            Self::Secondary => {
1717                "leave this board alone for a few minutes, then run the command again — or \
1718                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
1719            }
1720        }
1721    }
1722}
1723
1724/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
1725#[derive(Debug, Clone, Copy)]
1726struct Limited {
1727    limiter: Limiter,
1728    hint: Option<u64>,
1729}
1730
1731impl Limited {
1732    /// What the caller is told once this source has waited as long as it may.
1733    ///
1734    /// Both limiters report as [`SourceError::RateLimited`], because that is what
1735    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
1736    /// about *which* limiter it was makes it a different kind of failure. What differs is
1737    /// the operator's next step, and that is what the message carries — a secondary
1738    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
1739    /// budget looks fine, and then back to retry the very burst that was refused.
1740    fn exhausted(
1741        self,
1742        doing: &str,
1743        waits: u32,
1744        waited: Duration,
1745        needed: Duration,
1746        budget: Duration,
1747    ) -> SourceError {
1748        SourceError::RateLimited {
1749            retry_after_seconds: self.hint,
1750            message: Some(format!(
1751                "{} refused this source while {doing}; it waited {} out over {} and was refused \
1752                 again, and the next wait of {} would take it past the {} one call may spend \
1753                 waiting. {} next: {}",
1754                self.limiter.name(),
1755                plural(waits, "refusal"),
1756                seconds(waited),
1757                seconds(needed),
1758                seconds(budget),
1759                self.limiter.where_to_look(),
1760                self.limiter.what_to_do(),
1761            )),
1762        }
1763    }
1764}
1765
1766/// One HTTP attempt's result, with what its response said about the rate limit.
1767///
1768/// The two travel together so the record and the outcome are written from the same place:
1769/// what a response said about the budget is only readable while that response is in hand,
1770/// and what the attempt *meant* is only decidable once its body has been read.
1771struct Attempted {
1772    result: Result<Value, Attempt>,
1773    limits: accounting::RateLimit,
1774    /// GitHub's own reported cost for this call, for a document that asked for it.
1775    reported_cost: Option<u64>,
1776}
1777
1778/// One attempt's outcome: an error to report, or a rate limit to wait out.
1779enum Attempt {
1780    Failed(SourceError),
1781    Limited(Limited),
1782}
1783
1784fn plural(count: u32, thing: &str) -> String {
1785    if count == 1 {
1786        format!("{count} {thing}")
1787    } else {
1788        format!("{count} {thing}s")
1789    }
1790}
1791
1792fn seconds(duration: Duration) -> String {
1793    format!("{:.1}s", duration.as_secs_f64())
1794}
1795
1796/// The member [`stamp_served`] writes onto every node of a response that says when it last
1797/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
1798const SERVED_AT: &str = "onetaskgraphServedAt";
1799
1800/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
1801/// node's time later knows the server's clock on the response that read it — which is what
1802/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
1803fn stamp_served(data: &mut Value, served: &str) {
1804    match data {
1805        Value::Object(members) => {
1806            if members.contains_key("updatedAt") {
1807                members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
1808            }
1809            for member in members.values_mut() {
1810                stamp_served(member, served);
1811            }
1812        }
1813        Value::Array(items) => {
1814            for item in items {
1815                stamp_served(item, served);
1816            }
1817        }
1818        _ => {}
1819    }
1820}
1821
1822/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
1823///
1824/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
1825/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
1826/// header, and neither is what makes a response a refusal — so the whole cost of one this
1827/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
1828/// instead. Refusing the response over the header would turn a readable refusal into an
1829/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
1830fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
1831    value
1832        .and_then(|value| value.to_str().ok())
1833        .and_then(|value| value.trim().parse::<u64>().ok())
1834}
1835
1836/// Every mutation this source sends creates content — an issue, a board item, a field of
1837/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
1838/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
1839/// and what the keyword says are the same set. That is what makes the keyword a sound test
1840/// rather than a convenient one: pacing an edit or a removal the limiter might not have
1841/// counted costs a wait, and not pacing one it did count costs the next fifty minutes.
1842fn is_mutation(query: &str) -> bool {
1843    query.trim_start().starts_with("mutation")
1844}
1845
1846/// What this source was doing, for a diagnostic that has to say so.
1847///
1848/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
1849/// a document added without a description is caught by that list's own gate instead of
1850/// falling through to the vague arm below.
1851fn operation_description(query: &str) -> &'static str {
1852    graphql::DOCUMENTS
1853        .iter()
1854        .find(|(document, _)| *document == query)
1855        .map_or("talking to GitHub", |(_, doing)| *doing)
1856}
1857
1858/// GitHub's published ceiling on content-generating requests, per minute.
1859///
1860/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
1861/// GitHub's number rather than this source's: [`MIN_MUTATION_INTERVAL_MS`] is *derived*
1862/// from it, so a pacing value checked only against itself cannot go stale here.
1863pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
1864/// The same ceiling as GitHub publishes it per hour, which this source does **not** pace
1865/// at. See [`MIN_MUTATION_INTERVAL_MS`] for why the per-minute bound is the one that
1866/// governs; it is pinned beside its sibling so the gate would notice either one moving.
1867pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
1868/// Shortest interval between two content-creating mutations, in milliseconds.
1869///
1870/// GitHub documents two secondary limits on content-generating requests:
1871/// [`CONTENT_CREATION_PER_MINUTE`] and [`CONTENT_CREATION_PER_HOUR`]. 60000/80 is 750, so
1872/// a mutation every 750 ms is the fastest rate that cannot exceed the per-minute bound,
1873/// and that is the bound a copy actually trips: a copy of one plan-sized project is a
1874/// burst of a few dozen mutations inside a few seconds. The hourly bound works out at one
1875/// every 7.2 seconds sustained, which no single copy reaches and which, used as the
1876/// spacing here, would turn an ordinary copy into an hour of waiting — so it is
1877/// deliberately *not* what this paces at. An installation that wants the hourly bound
1878/// honoured for a long sequence of copies says so through
1879/// `pacing.min_mutation_interval_ms`.
1880pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
1881/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
1882///
1883/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
1884/// own advice for a secondary limit — wait, and wait longer each time — without spending
1885/// the first minute of a transient refusal doing nothing.
1886pub const RETRY_BACKOFF_MS: u64 = 1_000;
1887/// Total time one call may spend waiting out rate limits before it reports a failure.
1888///
1889/// Two minutes is long enough to ride out the refusals a paced copy still collects and
1890/// short enough that a command an operator is watching returns. The bound is what makes
1891/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
1892/// the limiter, not in a process nobody can tell from a wedged one.
1893pub const RETRY_BUDGET_MS: u64 = 120_000;
1894
1895fn default_token_env() -> String {
1896    "GH_PROJECTS_TOKEN".to_owned()
1897}
1898fn default_endpoint() -> String {
1899    "https://api.github.com/graphql".to_owned()
1900}
1901
1902/// The name of a `Status` single-select option on the board.
1903///
1904/// Validated on the way in rather than checked later, so a blank option name — which
1905/// nothing on a board can be — is a state this type cannot hold.
1906#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1907#[serde(try_from = "String")]
1908#[schemars(extend("minLength" = 1))]
1909pub struct ColumnName(String);
1910
1911impl ColumnName {
1912    /// The option name, as the board spells it.
1913    fn as_str(&self) -> &str {
1914        &self.0
1915    }
1916}
1917
1918impl TryFrom<String> for ColumnName {
1919    type Error = String;
1920
1921    fn try_from(name: String) -> Result<Self, Self::Error> {
1922        if name.trim().is_empty() {
1923            return Err("a status_mapping option name cannot be blank".to_owned());
1924        }
1925        Ok(Self(name))
1926    }
1927}
1928
1929/// The two closed states this product can mean.
1930///
1931/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which is neither finished
1932/// work nor abandoned work, so nothing here ever writes it.
1933#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
1934#[serde(rename_all = "kebab-case")]
1935pub enum ClosedState {
1936    /// `COMPLETED` — precisely done.
1937    Completed,
1938    /// `NOT_PLANNED` — precisely cancelled.
1939    NotPlanned,
1940}
1941
1942impl ClosedState {
1943    const fn reason(self) -> &'static str {
1944        match self {
1945            Self::Completed => "COMPLETED",
1946            Self::NotPlanned => "NOT_PLANNED",
1947        }
1948    }
1949}
1950
1951/// Configuration for one GitHub Projects v2 board.
1952///
1953/// It serializes to a document this same type reads back as itself, so a caller writing a
1954/// configuration — a consumer filing onto a board it configures — can build one here rather
1955/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
1956#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1957#[serde(default, deny_unknown_fields)]
1958pub struct GitHubProjectsConfig {
1959    /// Login of the user or organization which owns the board.
1960    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
1961    /// The project number shown in the board's GitHub URL.
1962    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
1963    // 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.
1964    /// `owner/name` of the repository this source creates an issue in when the item's own
1965    /// `repositories` field does not decide it.
1966    ///
1967    /// An item naming exactly one repository is created there; a task or a document naming
1968    /// none or several is created in its parent project's repository; and a project, or a
1969    /// task or document with no parent, naming none or several is created here. A board
1970    /// has no repository of its own and `createIssue` requires one, so a write without
1971    /// this is refused naming the field. Reads never need it.
1972    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
1973    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
1974    /// Environment variable containing a fine-grained token with Projects and Issues
1975    /// read/write plus Pull requests read-only access for every repository represented on
1976    /// the board.
1977    #[serde(default = "default_token_env")]
1978    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
1979    /// GraphQL endpoint. GitHub Enterprise installations may override it.
1980    #[serde(default = "default_endpoint")]
1981    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
1982    /// Per-instance mapping from a status category to the option of the board's one
1983    /// `Status` field it lands on, for a task and for a project.
1984    ///
1985    /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
1986    /// `null` to disable the category for both, or `{task, project}` naming it per kind,
1987    /// where a kind left out leaves the category unmapped for that kind. A category this
1988    /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
1989    /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
1990    /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
1991    /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
1992    /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
1993    /// either kind. No two categories may name one option for the same kind, ignoring case.
1994    /// `unknown` may name one existing option; every unknown word then lands on it and
1995    /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
1996    /// each unknown word because it never creates board options.
1997    #[serde(default)]
1998    pub status_mapping: StatusMapping,
1999    /// Per-instance mapping from a task's priority to an option of this board's
2000    /// single-select field named `Priority`.
2001    ///
2002    /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2003    /// other priority is refused before it reaches this board. Present, each of `urgent`,
2004    /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2005    /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2006    /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2007    /// no two levels may name one option. Reads and writes never create the field or an
2008    /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2009    /// the board lacks is refused pointing there.
2010    #[serde(default)]
2011    pub priority_mapping: Option<PriorityMappingConfig>,
2012    /// Caller metadata values this source also writes to board text fields, so the board can
2013    /// filter on them.
2014    ///
2015    /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2016    /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2017    /// value. On every item write — create, update and copy, of a task, a project or a
2018    /// document — a string found there is written to the field, and nothing is sent when the
2019    /// field already holds it; nothing there, or `null`, clears the field when it holds a
2020    /// value; any other JSON type is refused before any mutation. The metadata comment in the
2021    /// issue body stays the one home of the value, and every read reports metadata from it.
2022    /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2023    /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2024    /// non-text field of that name, is refused pointing there.
2025    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2026    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2027    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2028    #[schemars(!skip_serializing_if)]
2029    pub metadata_fields: Vec<MetadataFieldConfig>,
2030    /// How fast this source writes, and how long it waits out a rate-limit refusal.
2031    ///
2032    /// Every field keeps its shipped default when it is absent, and the defaults are
2033    /// GitHub's own published limits rather than taste. See [`Pacing`].
2034    #[serde(default)]
2035    pub pacing: PacingConfig,
2036}
2037
2038/// Which option of the board's `Priority` field each priority lands on.
2039///
2040/// One member per level rather than a map, so a key that is not a level is refused where
2041/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2042/// value in the field, not an option of it.
2043#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2044#[serde(default, deny_unknown_fields)]
2045pub struct PriorityMappingConfig {
2046    /// The option `urgent` lands on; `Urgent` when absent.
2047    pub urgent: Option<PriorityOptionName>,
2048    /// The option `high` lands on; `High` when absent.
2049    pub high: Option<PriorityOptionName>,
2050    /// The option `medium` lands on; `Medium` when absent.
2051    pub medium: Option<PriorityOptionName>,
2052    /// The option `low` lands on; `Low` when absent.
2053    pub low: Option<PriorityOptionName>,
2054}
2055
2056/// The name of an option of the board's `Priority` single-select field.
2057///
2058/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2059/// blank name.
2060#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2061#[serde(try_from = "String")]
2062#[schemars(extend("minLength" = 1))]
2063pub struct PriorityOptionName(String);
2064
2065impl PriorityOptionName {
2066    /// The option name, as the board spells it.
2067    fn as_str(&self) -> &str {
2068        &self.0
2069    }
2070}
2071
2072impl TryFrom<String> for PriorityOptionName {
2073    type Error = String;
2074
2075    fn try_from(name: String) -> Result<Self, Self::Error> {
2076        if name.trim().is_empty() {
2077            return Err("a priority_mapping option name cannot be blank".to_owned());
2078        }
2079        Ok(Self(name))
2080    }
2081}
2082
2083/// The name of the board field a priority is held in.
2084pub const PRIORITY_FIELD: &str = "Priority";
2085
2086/// The four priorities a board option can hold, in the order a new `Priority` field lists
2087/// them. `none` is not among them: it is the field holding no value.
2088///
2089/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2090/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2091/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2092/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2093pub const PRIORITY_LEVELS: [Priority; 4] = [
2094    Priority::Urgent,
2095    Priority::High,
2096    Priority::Medium,
2097    Priority::Low,
2098];
2099
2100/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2101/// see that list for what this pins.
2102#[must_use]
2103pub const fn level_position(priority: Priority) -> Option<usize> {
2104    match priority {
2105        Priority::None => None,
2106        Priority::Urgent => Some(0),
2107        Priority::High => Some(1),
2108        Priority::Medium => Some(2),
2109        Priority::Low => Some(3),
2110    }
2111}
2112
2113/// This instance's complete priority-to-option mapping, read in both directions.
2114///
2115/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2116/// two levels name one option.
2117#[derive(Debug, Clone)]
2118struct PriorityMapping {
2119    options: [PriorityOptionName; 4],
2120}
2121
2122impl PriorityMapping {
2123    fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2124        let shipped = |name: &str| PriorityOptionName(name.to_owned());
2125        let mapping = Self {
2126            options: [
2127                config.urgent.unwrap_or_else(|| shipped("Urgent")),
2128                config.high.unwrap_or_else(|| shipped("High")),
2129                config.medium.unwrap_or_else(|| shipped("Medium")),
2130                config.low.unwrap_or_else(|| shipped("Low")),
2131            ],
2132        };
2133        for (index, option) in mapping.options.iter().enumerate() {
2134            if let Some(earlier) = mapping.options[..index]
2135                .iter()
2136                .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2137            {
2138                return Err(SourceError::Config {
2139                    message: format!(
2140                        "priority_mapping of source {instance} sends both {} and {} to the board \
2141                         option {:?}; one option cannot read back as two priorities",
2142                        PRIORITY_LEVELS[earlier],
2143                        PRIORITY_LEVELS[index],
2144                        option.as_str()
2145                    ),
2146                });
2147            }
2148        }
2149        Ok(mapping)
2150    }
2151
2152    /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2153    fn option(&self, priority: Priority) -> Option<&str> {
2154        level_position(priority).map(|index| self.options[index].as_str())
2155    }
2156
2157    /// The priority a board option name reports, or `None` when nothing maps to it.
2158    fn priority_of(&self, option: &str) -> Option<Priority> {
2159        self.options
2160            .iter()
2161            .position(|name| name.as_str().eq_ignore_ascii_case(option))
2162            .map(|index| PRIORITY_LEVELS[index])
2163    }
2164
2165    /// Every mapped option name, in the order a new `Priority` field lists them.
2166    fn names(&self) -> impl Iterator<Item = &str> {
2167        self.options.iter().map(PriorityOptionName::as_str)
2168    }
2169}
2170
2171/// One metadata value this source projects onto a board text field.
2172#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2173#[serde(deny_unknown_fields)]
2174pub struct MetadataFieldConfig {
2175    /// The name of the board text field the value is written to.
2176    pub field: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `MetadataField::resolve` refuses a blank, reserved, GitHub-owned or repeated name before the private validated entry is built.
2177    /// The top-level metadata key the value is read from. A key under the reserved
2178    /// `onetaskgraph.` prefix is refused.
2179    pub key: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `MetadataField::resolve` refuses a blank or reserved key before the private validated entry is built.
2180    /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2181    /// key's own value is the one projected. Left out of a serialized entry when empty.
2182    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2183    // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
2184    #[schemars(!skip_serializing_if)]
2185    pub path: Vec<String>,
2186}
2187
2188/// The board fields no metadata value may be projected onto, compared ignoring case: the
2189/// three this source writes itself, and those GitHub owns on every board.
2190// llmlint: ignore[contracts_have_one_source_or_a_drift_gate] This list is the configuration contract `metadata_fields`' consumers agreed, refused at read so a mistaken entry is named early — not a mirror GitHub's field set has to stay in step with. What keeps a write off a field GitHub owns is the write's own check, `projection_field`, which refuses any field whose `dataType` is not `TEXT`: every built-in board field reports its own type (TITLE, ASSIGNEES, LABELS, …), and the live contract introspection holds GitHub to `ProjectV2Field.dataType`. So a built-in field GitHub adds later and this list lacks is still never written.
2191const UNPROJECTABLE_FIELDS: [&str; 13] = [
2192    STATUS_FIELD,
2193    PRIORITY_FIELD,
2194    ORIGIN_FIELD,
2195    "Title",
2196    "Assignees",
2197    "Labels",
2198    "Linked pull requests",
2199    "Milestone",
2200    "Repository",
2201    "Reviewers",
2202    "Parent issue",
2203    "Sub-issues progress",
2204    "Type",
2205];
2206
2207/// One validated [`MetadataFieldConfig`] entry.
2208#[derive(Debug, Clone)]
2209struct MetadataField {
2210    field: ProjectedField,
2211    key: ProjectedKey,
2212    path: Vec<String>,
2213}
2214
2215/// The name of a board text field a metadata value is projected onto: never blank, never one
2216/// this source or GitHub writes, and never one another entry of the same source names.
2217#[derive(Debug, Clone, PartialEq, Eq)]
2218struct ProjectedField(String);
2219
2220/// The top-level metadata key a projected value is read from: never blank, and never under
2221/// the namespace this product reserves.
2222#[derive(Debug, Clone, PartialEq, Eq)]
2223struct ProjectedKey(String);
2224
2225impl std::ops::Deref for ProjectedField {
2226    type Target = str;
2227    fn deref(&self) -> &str {
2228        &self.0
2229    }
2230}
2231
2232impl std::ops::Deref for ProjectedKey {
2233    type Target = str;
2234    fn deref(&self) -> &str {
2235        &self.0
2236    }
2237}
2238
2239/// What one item's metadata says one projected field should hold.
2240#[derive(Debug, Clone, PartialEq, Eq)]
2241enum Projected {
2242    /// A string to hold.
2243    Text(String),
2244    /// Nothing: the key or a step of the path is absent, or the value is `null`.
2245    Nothing,
2246}
2247
2248impl MetadataField {
2249    /// Validate every entry of one instance's `metadata_fields`, refusing the first that
2250    /// cannot stand, naming the source and the entry.
2251    fn resolve(
2252        configured: Vec<MetadataFieldConfig>,
2253        instance: &SourceName,
2254    ) -> Result<Vec<Self>, SourceError> {
2255        let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
2256        for (index, entry) in configured.into_iter().enumerate() {
2257            let refuse = |why: String| SourceError::Config {
2258                message: format!(
2259                    "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
2260                    entry.field, entry.key
2261                ),
2262            };
2263            if entry.field.trim().is_empty() {
2264                return Err(refuse("names a blank field".to_owned()));
2265            }
2266            if entry.key.trim().is_empty() {
2267                return Err(refuse("names a blank key".to_owned()));
2268            }
2269            if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
2270                return Err(refuse(format!(
2271                    "names a key under the reserved \"{}.\" prefix, which this product keeps \
2272                     for its own keys; next: project a caller key instead",
2273                    MetadataKey::RESERVED_NAMESPACE
2274                )));
2275            }
2276            if let Some(owned) = UNPROJECTABLE_FIELDS
2277                .iter()
2278                .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
2279            {
2280                return Err(refuse(format!(
2281                    "names the board field {owned:?}, which this source or GitHub already \
2282                     writes; next: name a text field of your own"
2283                )));
2284            }
2285            if let Some(earlier) = resolved
2286                .iter()
2287                .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
2288            {
2289                return Err(refuse(format!(
2290                    "names the same board field as metadata_fields[{earlier}]; one field \
2291                     cannot hold two values"
2292                )));
2293            }
2294            resolved.push(Self {
2295                field: ProjectedField(entry.field),
2296                key: ProjectedKey(entry.key),
2297                path: entry.path,
2298            });
2299        }
2300        Ok(resolved)
2301    }
2302
2303    /// The path as a refusal spells it: `[]` for the key's own value.
2304    fn spelled_path(&self) -> String {
2305        serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
2306    }
2307
2308    /// What `metadata` says this field should hold, or the refusal for a value no text field
2309    /// can hold.
2310    fn projected(
2311        &self,
2312        metadata: &BTreeMap<String, Value>,
2313        instance: &SourceName,
2314    ) -> Result<Projected, SourceError> {
2315        let mut value = metadata.get(&*self.key);
2316        for step in &self.path {
2317            value = value.and_then(|held| held.get(step.as_str()));
2318        }
2319        let found = match value {
2320            None | Some(Value::Null) => return Ok(Projected::Nothing),
2321            Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
2322            Some(Value::Bool(_)) => "a boolean",
2323            Some(Value::Number(_)) => "a number",
2324            Some(Value::Array(_)) => "an array",
2325            Some(Value::Object(_)) => "an object",
2326        };
2327        Err(SourceError::Refused {
2328            message: format!(
2329                "source {instance} projects metadata key {:?} at path {} onto its board text \
2330                 field {:?}, and this item holds {found} there; only a string, null or nothing \
2331                 can be written to a text field; next: store a string there, or remove the \
2332                 value",
2333                &*self.key,
2334                self.spelled_path(),
2335                &*self.field
2336            ),
2337        })
2338    }
2339}
2340
2341/// What one item's `Priority` field says, read through this instance's mapping.
2342#[derive(Debug, Clone, PartialEq, Eq)]
2343enum HeldPriority {
2344    /// A priority this source reports: an option the mapping names, or no value (`none`).
2345    Read(Priority),
2346    /// An option the mapping does not name, which is never read as a level or as `none`.
2347    Unmapped(String),
2348}
2349
2350/// How fast this source writes, and how long it waits out a rate-limit refusal.
2351///
2352/// Configurable because a GitHub Enterprise installation sets its own limits and an
2353/// operator who has already been refused may want to go slower still — not because the
2354/// defaults are guesses.
2355#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2356#[serde(default, deny_unknown_fields)]
2357pub struct PacingConfig {
2358    /// Shortest interval between two content-creating mutations, in milliseconds.
2359    ///
2360    /// Zero sends them as fast as they are asked for, which is what a fixture server on
2361    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
2362    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.
2363    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
2364    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
2365    /// zero while there is a budget to spend, because a schedule of zero-length waits
2366    /// consumes none of it and so never ends.
2367    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.
2368    /// Total time one call may spend waiting out rate limits, in milliseconds.
2369    ///
2370    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
2371    /// the bound is what makes this a wait rather than a hang.
2372    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.
2373}
2374
2375/// The largest any pacing setting may be, in milliseconds.
2376///
2377/// One hour. GitHub's own harshest published bound on content-generating requests works
2378/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
2379/// anything a real limit asks for, and past it the settings stop describing pacing at all:
2380/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
2381/// and an interval beyond it is a command that never sends its second mutation. It also
2382/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
2383/// what a `Duration` can hold on every platform.
2384pub const MAX_PACING_MS: u64 = 3_600_000;
2385
2386/// [`PacingConfig`] with every default resolved and every value checked, which is what the
2387/// source holds.
2388#[derive(Debug, Clone, Copy)]
2389struct Pacing {
2390    min_mutation_interval: Duration,
2391    retry_backoff: Duration,
2392    retry_budget: Duration,
2393}
2394
2395impl Pacing {
2396    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
2397    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2398        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
2399            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
2400                message: format!(
2401                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
2402                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
2403                     GitHub's own harshest published limit"
2404                ),
2405            }),
2406            Some(value) => Ok(Duration::from_millis(value)),
2407            None => Ok(Duration::from_millis(default)),
2408        };
2409        let retry_backoff = bounded(
2410            config.retry_backoff_ms,
2411            RETRY_BACKOFF_MS,
2412            "retry_backoff_ms",
2413        )?;
2414        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
2415        if retry_backoff.is_zero() && !retry_budget.is_zero() {
2416            return Err(SourceError::Config {
2417                message: format!(
2418                    "pacing.retry_backoff_ms of source {instance} is 0 while \
2419                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
2420                     none of that budget, so it would retry a refusal forever. Set a backoff of \
2421                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
2422                     waiting at all",
2423                    retry_budget.as_millis()
2424                ),
2425            });
2426        }
2427        Ok(Self {
2428            min_mutation_interval: bounded(
2429                config.min_mutation_interval_ms,
2430                MIN_MUTATION_INTERVAL_MS,
2431                "min_mutation_interval_ms",
2432            )?,
2433            retry_backoff,
2434            retry_budget,
2435        })
2436    }
2437}
2438
2439/// Factory for [`GitHubProjectsSource`].
2440#[derive(Debug, Clone, Copy, Default)]
2441pub struct Plugin;
2442
2443impl SourcePlugin for Plugin {
2444    fn kind(&self) -> &'static str {
2445        KIND
2446    }
2447    fn config_schema(&self) -> Schema {
2448        schema_for!(GitHubProjectsConfig)
2449    }
2450    fn build(
2451        &self,
2452        name: &SourceName,
2453        config: &Value,
2454        secrets: &dyn SecretResolver,
2455    ) -> Result<Box<dyn TaskSource>, SourceError> {
2456        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
2457    }
2458
2459    fn build_with_clock(
2460        &self,
2461        name: &SourceName,
2462        config: &Value,
2463        secrets: &dyn SecretResolver,
2464        clock: SharedClock,
2465    ) -> Result<Box<dyn TaskSource>, SourceError> {
2466        self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
2467    }
2468}
2469
2470impl Plugin {
2471    /// Build a source recording every request it sends into an accounting the caller holds.
2472    ///
2473    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
2474    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
2475    /// session total rather than two — see [`accounting`] and
2476    /// [`GitHubProjectsSource::recording_into`].
2477    ///
2478    /// # Errors
2479    ///
2480    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
2481    /// [`SourceError::Config`] for configuration this plugin cannot use and
2482    /// [`SourceError::Auth`] for a credential it cannot find.
2483    pub fn build_recording_into(
2484        &self,
2485        name: &SourceName,
2486        config: &Value,
2487        secrets: &dyn SecretResolver,
2488        ledger: Arc<Accounting>,
2489    ) -> Result<Box<dyn TaskSource>, SourceError> {
2490        self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
2491    }
2492
2493    fn build_recording_with_clock(
2494        &self,
2495        name: &SourceName,
2496        config: &Value,
2497        secrets: &dyn SecretResolver,
2498        ledger: Arc<Accounting>,
2499        clock: SharedClock,
2500    ) -> Result<Box<dyn TaskSource>, SourceError> {
2501        let config: GitHubProjectsConfig =
2502            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
2503                message: format!("source {name}: {e}"),
2504            })?;
2505        let prefix = format!("source {name}: ");
2506        let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
2507            .map_err(|error| match error {
2508                // The shared `StatusMapping::distinct` names the source itself.
2509                SourceError::Config { message } if message.starts_with(&prefix) => {
2510                    SourceError::Config { message }
2511                }
2512                SourceError::Config { message } => SourceError::Config {
2513                    message: format!("{prefix}{message}"),
2514                },
2515                SourceError::Auth { message } => SourceError::Auth {
2516                    message: format!("source {name}: {message}"),
2517                },
2518                other => other,
2519            })?;
2520        source.clock = clock;
2521        Ok(Box::new(source))
2522    }
2523}
2524
2525/// Where a status category lands on this board, once configuration is resolved.
2526#[derive(Debug, Clone, PartialEq, Eq)]
2527enum StatusTarget {
2528    /// Not usable against this instance for this kind, and why.
2529    Disabled(UnmappedStatus),
2530    /// The board's `Status` option of this name.
2531    Column(ColumnName),
2532    /// A closed issue, with both its board option and the reason that says which closed it means.
2533    // 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.
2534    Terminal(ColumnName, ClosedState),
2535}
2536
2537/// Every status category, in the order the vocabulary declares them.
2538///
2539/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
2540/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
2541/// added to the shared vocabulary fails to compile until it is named there, and this
2542/// crate's suite reconciles this list against that enum's own derived schema, which is
2543/// generated from the variants rather than written beside them. The schema is what
2544/// catches a list left one short — a list checking only the positions it already holds
2545/// would pass while every mapping indexed by the new position panicked.
2546pub const CATEGORIES: [StatusCategory; 8] = [
2547    StatusCategory::Draft,
2548    StatusCategory::Backlog,
2549    StatusCategory::Todo,
2550    StatusCategory::Queued,
2551    StatusCategory::InProgress,
2552    StatusCategory::Done,
2553    StatusCategory::Cancelled,
2554    StatusCategory::Unknown,
2555];
2556
2557/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
2558#[must_use]
2559pub const fn category_position(category: StatusCategory) -> usize {
2560    match category {
2561        StatusCategory::Draft => 0,
2562        StatusCategory::Backlog => 1,
2563        StatusCategory::Todo => 2,
2564        StatusCategory::Queued => 3,
2565        StatusCategory::InProgress => 4,
2566        StatusCategory::Done => 5,
2567        StatusCategory::Cancelled => 6,
2568        StatusCategory::Unknown => 7,
2569    }
2570}
2571
2572/// The spelling a status category is configured and reported under.
2573fn category_name(category: StatusCategory) -> &'static str {
2574    match category {
2575        StatusCategory::Draft => "draft",
2576        StatusCategory::Backlog => "backlog",
2577        StatusCategory::Todo => "todo",
2578        StatusCategory::Queued => "queued",
2579        StatusCategory::InProgress => "in-progress",
2580        StatusCategory::Done => "done",
2581        StatusCategory::Cancelled => "cancelled",
2582        StatusCategory::Unknown => "unknown",
2583    }
2584}
2585
2586/// A shipped default's option name.
2587///
2588/// The literals below are this file's own and non-blank, and they are validated by the
2589/// one constructor a configured name goes through rather than beside it.
2590fn shipped_column(name: &'static str) -> ColumnName {
2591    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
2592}
2593
2594/// The shipped default for one category this instance's `status_mapping` does not mention,
2595/// for either kind.
2596fn shipped_default(category: StatusCategory) -> StatusTarget {
2597    match category {
2598        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
2599        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
2600        StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
2601        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
2602        StatusCategory::Done => {
2603            StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
2604        }
2605        StatusCategory::Cancelled => {
2606            StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
2607        }
2608        StatusCategory::Draft | StatusCategory::Unknown => {
2609            StatusTarget::Disabled(UnmappedStatus::Unconfigured)
2610        }
2611    }
2612}
2613
2614/// The two kinds a status is written and read for, each with its own half of the mapping.
2615const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
2616
2617/// This instance's complete category-to-target mapping for each kind, read in both
2618/// directions.
2619///
2620/// One target per category per kind, held at that category's own [`category_position`], so
2621/// a category missing from the mapping, named twice in it, or filed out of order is a state
2622/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
2623/// targets are options of the board's one `Status` field.
2624#[derive(Debug, Clone)]
2625struct BoardStatuses {
2626    tasks: [StatusTarget; CATEGORIES.len()],
2627    projects: [StatusTarget; CATEGORIES.len()],
2628}
2629
2630impl BoardStatuses {
2631    /// Resolve `configured` against the shipped defaults, refusing two categories one kind
2632    /// would read back from one option.
2633    ///
2634    /// A category the mapping does not mention keeps its shipped default for both kinds; one
2635    /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
2636    /// omits unmapped rather than defaulted.
2637    fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
2638        let resolve_kind =
2639            |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
2640                // `CATEGORIES[position] == category` for every category — the crate's suite
2641                // asserts it — so mapping the list in order fills each category's own slot.
2642                let mut targets = CATEGORIES.map(shipped_default);
2643                for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
2644                    if !configured.mentions(category) {
2645                        continue;
2646                    }
2647                    *slot = match configured.name_for(category, kind) {
2648                        Err(why) => StatusTarget::Disabled(why),
2649                        Ok(name) => {
2650                            let option = ColumnName::try_from(name.as_str().to_owned())
2651                                .map_err(|message| SourceError::Config { message })?;
2652                            match category {
2653                                StatusCategory::Done => {
2654                                    StatusTarget::Terminal(option, ClosedState::Completed)
2655                                }
2656                                StatusCategory::Cancelled => {
2657                                    StatusTarget::Terminal(option, ClosedState::NotPlanned)
2658                                }
2659                                _ => StatusTarget::Column(option),
2660                            }
2661                        }
2662                    };
2663                }
2664                StatusMapping::distinct(
2665                    instance,
2666                    kind,
2667                    CATEGORIES
2668                        .iter()
2669                        .zip(&targets)
2670                        .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
2671                )?;
2672                Ok(targets)
2673            };
2674        Ok(Self {
2675            tasks: resolve_kind(ItemKind::Task)?,
2676            projects: resolve_kind(ItemKind::Project)?,
2677        })
2678    }
2679
2680    /// Every category's target for `kind`, in category order.
2681    const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
2682        match kind {
2683            ItemKind::Task => &self.tasks,
2684            ItemKind::Project => &self.projects,
2685        }
2686    }
2687
2688    fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
2689        &self.targets(kind)[category_position(category)]
2690    }
2691
2692    /// The category a board option name reports for `kind`, or `None` when nothing of that
2693    /// kind maps to it.
2694    fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
2695        CATEGORIES.into_iter().find(|category| {
2696            self.target(kind, *category)
2697                .option()
2698                .is_some_and(|name| name.eq_ignore_ascii_case(option))
2699        })
2700    }
2701
2702    /// Every option name either kind maps a category to, each once ignoring case, in
2703    /// category order with a task's name before a project's — what the guarded setup asks
2704    /// the `Status` field to hold.
2705    fn wanted(&self) -> Vec<String> {
2706        let mut wanted: Vec<String> = Vec::new();
2707        for category in CATEGORIES {
2708            for kind in STATUS_KINDS {
2709                if let Some(name) = self.target(kind, category).option()
2710                    && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
2711                {
2712                    wanted.push(name.to_owned());
2713                }
2714            }
2715        }
2716        wanted
2717    }
2718
2719    /// The status an item of `kind` reports, from the three things a read of it says: its
2720    /// board `Status` option, whether its issue is closed, and the reason it was closed with.
2721    ///
2722    /// The closed state decides the category and the `Status` option decides the name, so
2723    /// a closed issue sitting in a "Shipped" column reports `done` named `Shipped`, whatever
2724    /// its kind. A closed issue whose reason is `DUPLICATE` or `REOPENED` reports `Unknown`:
2725    /// a duplicate is not finished work, and calling it done is a lie the next copy would
2726    /// write back. `REOPENED`-while-closed is a state this source can never produce, so
2727    /// it is read permissively rather than refused — reads are faithful, and refusals
2728    /// belong on writes. An open item's option reads through its own kind's mapping, and an
2729    /// option that mapping does not name reads as `Unknown` under its own name.
2730    ///
2731    /// One function of those three rather than of a response, so a narrow status write can
2732    /// answer what a re-read would report by applying it to the state it has just written.
2733    fn status(
2734        &self,
2735        kind: ItemKind,
2736        option: Option<&str>,
2737        closed: bool,
2738        reason: Option<&str>,
2739    ) -> Status {
2740        if closed {
2741            let category = match reason {
2742                None | Some("COMPLETED") => StatusCategory::Done,
2743                Some("NOT_PLANNED") => StatusCategory::Cancelled,
2744                Some(_) => StatusCategory::Unknown,
2745            };
2746            let fallback = match category {
2747                StatusCategory::Done => "Done",
2748                StatusCategory::Cancelled => "Cancelled",
2749                _ => "Closed",
2750            };
2751            return Status {
2752                category,
2753                name: option.unwrap_or(fallback).to_owned(),
2754            };
2755        }
2756        let name = option.unwrap_or("Open").to_owned();
2757        Status {
2758            category: self
2759                .category_of(kind, &name)
2760                .unwrap_or(StatusCategory::Unknown),
2761            name,
2762        }
2763    }
2764}
2765
2766impl BoardStatuses {
2767    /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
2768    /// case; a kind lacking none is left out.
2769    fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
2770        STATUS_KINDS
2771            .into_iter()
2772            .filter_map(|kind| {
2773                let missing: Vec<String> = self
2774                    .targets(kind)
2775                    .iter()
2776                    .filter_map(StatusTarget::option)
2777                    .filter(|wanted| {
2778                        !existing
2779                            .iter()
2780                            .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2781                    })
2782                    .map(str::to_owned)
2783                    .collect();
2784                (!missing.is_empty()).then_some(KindMissing { kind, missing })
2785            })
2786            .collect()
2787    }
2788}
2789
2790impl StatusTarget {
2791    /// The board option this target selects, or `None` for an unmapped one.
2792    fn option(&self) -> Option<&str> {
2793        match self {
2794            Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
2795            Self::Disabled(_) => None,
2796        }
2797    }
2798}
2799
2800// 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.
2801/// One repository this source can create an issue in, as `owner/name`.
2802///
2803/// Every `createIssue` this source sends names one of these: the item's own single
2804/// `repositories` entry, else its parent project issue's repository, else the configured
2805/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
2806/// that choice and says what it refuses before `createIssue`.
2807// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
2808#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
2809struct RepositoryTarget {
2810    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
2811    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
2812}
2813
2814impl RepositoryTarget {
2815    fn parse(value: &str) -> Result<Self, SourceError> {
2816        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
2817            message: format!(
2818                "repository must be spelled owner/name; {value:?} names no repository"
2819            ),
2820        })?;
2821        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
2822            return Err(SourceError::Config {
2823                message: format!(
2824                    "repository must be spelled owner/name with a GitHub login and one \
2825                     repository name; {value:?} is not"
2826                ),
2827            });
2828        }
2829        Ok(Self {
2830            owner: owner.to_owned(),
2831            name: name.to_owned(),
2832        })
2833    }
2834
2835    /// The one host whose repositories this source creates issues in, spelled once: it is
2836    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
2837    const HOST: &str = "github.com";
2838
2839    fn origin(&self) -> String {
2840        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
2841    }
2842
2843    /// The repository a normalized origin names, or why it is none this source can create
2844    /// an issue in: another host, or more or fewer than `owner/name` under this one.
2845    fn from_origin(origin: &Repository) -> Result<Self, String> {
2846        let not_here = || {
2847            format!(
2848                "{} is not a {}/owner/name repository",
2849                origin.as_str(),
2850                Self::HOST
2851            )
2852        };
2853        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
2854        if host != Self::HOST {
2855            return Err(not_here());
2856        }
2857        Self::parse(rest).map_err(|_| not_here())
2858    }
2859
2860    fn slug(&self) -> String {
2861        format!("{}/{}", self.owner, self.name)
2862    }
2863}
2864
2865/// A source which reads GitHub afresh for every operation.
2866pub struct GitHubProjectsSource {
2867    /// This source's configured name, used both to tell a far end naming this source
2868    /// from one naming a system it knows nothing about, and to name the instance a
2869    /// status refusal is about.
2870    name: SourceName,
2871    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
2872    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
2873    repository: Option<RepositoryTarget>,
2874    endpoint: Url,
2875    token: SecretString,
2876    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
2877    statuses: BoardStatuses,
2878    /// Where each priority lands on this board, or `None` when this instance holds none.
2879    priorities: Option<PriorityMapping>,
2880    /// The metadata values this instance projects onto board text fields, in configured order.
2881    metadata_fields: Vec<MetadataField>,
2882    client: Client,
2883    asset_client: Client,
2884    /// Every item this source has created in this command, in the order it created them —
2885    /// dropped by [`TaskSource::end_command`].
2886    ///
2887    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
2888    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
2889    /// a copy resolving a dependency on an item it had just created refused it as not
2890    /// found. A board read is completed from this — an item remembered here and absent from
2891    /// the read is added back, because the board really does hold it and only the read is
2892    /// behind.
2893    ///
2894    /// It is not a cache of a user's work: nothing is remembered that this process did not
2895    /// itself just write, it lives and dies with the process, and it is never consulted for
2896    /// an item this source did not create.
2897    created: Mutex<Vec<Resolved>>,
2898    /// Every item that already existed and that this source has written in this command, as
2899    /// it wrote it — dropped by [`TaskSource::end_command`].
2900    ///
2901    /// The other half of [`Self::created`], held on the same terms and for the reason a
2902    /// narrowed read needs it: an answer from GitHub's search or from the board's own field
2903    /// filter is an index behind a write this process made moments ago, so a query matching
2904    /// what this process just wrote onto an existing item would otherwise miss it. Nothing
2905    /// is remembered that this process did not itself just write.
2906    updated: Mutex<Vec<Resolved>>,
2907    /// Every issue this source has added a comment to or edited a comment of in this command
2908    /// — dropped by [`TaskSource::end_command`].
2909    ///
2910    /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
2911    /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
2912    /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
2913    /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
2914    /// from before — until the index caught up. Each id here is a candidate of every such
2915    /// search-narrowed read, and wherever it is a candidate its comments are read rather than
2916    /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
2917    /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
2918    /// process wrote is still found only once the index has it.
2919    commented: Mutex<Vec<NativeId>>,
2920    /// How fast this source writes, and how long it waits out a refusal.
2921    pacing: Pacing,
2922    /// When the last content-creating mutation finished, or the moment the furthest-out
2923    /// reserved slot releases the next one, whichever is later — so the one after it can be
2924    /// spaced from that. See [`MIN_MUTATION_INTERVAL_MS`] for the interval and
2925    /// [`GitHubProjectsSource::finish_mutation`] for why completion rather than release is
2926    /// what it is measured from.
2927    last_mutation: Mutex<Option<Duration>>,
2928    clock: SharedClock,
2929    numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
2930    /// The board as this process last read it, for the length of one command — dropped by
2931    /// [`TaskSource::end_command`].
2932    ///
2933    /// A copy of a project used to re-read the whole board, paged, before writing each of
2934    /// its items, which is by far the largest part of a copy's request count and none of
2935    /// its work. Nothing else changes this board while a command runs — this source's own
2936    /// writes are the only writer — so one read answers them all.
2937    ///
2938    /// It is not a store of a user's work and it is not the cache the no-persistence
2939    /// invariant forbids: it lives and dies with the process exactly as `created` does,
2940    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
2941    /// an item this command created and then depends on resolves whether or not GitHub's
2942    /// own eventually-consistent read has caught up. A write to an item already on the
2943    /// board updates the entry here too, so what this holds is the last read plus this
2944    /// process's own writes rather than a snapshot taken before them.
2945    board_cache: Mutex<Option<Board>>,
2946    /// Every issue this board's own search reported, for the length of one command — dropped
2947    /// by [`TaskSource::end_command`].
2948    ///
2949    /// The second half of a board read, and cached for the same reason and on the same
2950    /// terms as the first: it lives and dies with the process, nothing is written down, and
2951    /// a write this process makes updates the entry here exactly as it updates the one in
2952    /// [`Self::board_cache`]. One read answers every question a command asks, so a command
2953    /// that lists this board's projects and its tasks pays for one search rather than two.
2954    search_cache: Mutex<Option<Vec<Resolved>>>,
2955    /// What each narrowed question GitHub was asked answered, keyed by that question, for
2956    /// the length of one command — dropped by [`TaskSource::end_command`].
2957    ///
2958    /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
2959    /// and dies with the process, nothing is written down, a write this process makes
2960    /// updates the entry here as it updates the other two, and every answer is completed
2961    /// with this process's own writes each time it is given. A command that asks the same
2962    /// narrowed question twice — a wait polling for its own items, a listing repeated after a
2963    /// write — pays for it once, which is what the whole-board read it replaced gave it.
2964    narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
2965    search_next: Mutex<BTreeMap<String, Option<String>>>,
2966    /// Records already resolved in this command, reused by writes and for comment identity.
2967    /// Explicit item reads still reach GitHub. Nothing is persisted, and
2968    /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
2969    /// its item as a person has since left it.
2970    resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
2971    /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
2972    /// asked for under, with the project that selector named — for the length of one command,
2973    /// dropped by [`TaskSource::end_command`].
2974    ///
2975    /// A caller pages through a project's tasks one engine page at a time, and every page
2976    /// is cut from the whole list of them, so without this each page walked every
2977    /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
2978    /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
2979    /// dies with the process, nothing is written down, a write this process makes updates or
2980    /// removes the entry here as it does there, and every answer is completed with this
2981    /// process's own writes each time it is given.
2982    children_cache: Mutex<ProjectChildren>,
2983    /// The board's own id and field definitions as this process last read them on their
2984    /// own, for the length of one command — dropped by [`TaskSource::end_command`].
2985    ///
2986    /// What a write needs of the board and its item does not say, read once per command
2987    /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
2988    /// lives and dies with the process and nothing is written down. It holds no item and so
2989    /// can answer no question about one — see [`Self::board_fields`].
2990    fields_cache: Mutex<Option<BoardFields>>,
2991    /// Each destination repository's node id, resolved once per repository
2992    /// rather than per issue created.
2993    ///
2994    /// A repository's node id does not change, and re-reading it for every issue of a copy
2995    /// spent one request per item on an answer this source already had. It is a map rather
2996    /// than one entry because a copy files each item in the repository its own
2997    /// `repositories` field names, so a plan across five repositories asks GitHub five
2998    /// times and not once per item.
2999    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3000    /// What every request this source sends is recorded into.
3001    ///
3002    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3003    /// a request leaves this crate, so nothing has to be switched on for a session to be
3004    /// counted. It is shared rather than owned so a caller accounting for a whole session —
3005    /// its own schema verification, board lookups, residue sweep and cleanup beside this
3006    /// source's reads and writes — adds up one accounting instead of two. See
3007    /// [`accounting`] for what a record carries and what a session's spend is and is not.
3008    ledger: Arc<Accounting>,
3009}
3010
3011/// GitHub's closed single-select color vocabulary.
3012#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3013#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
3014pub enum StatusOptionColor {
3015    /// Gray.
3016    Gray,
3017    /// Blue.
3018    Blue,
3019    /// Green.
3020    Green,
3021    /// Yellow.
3022    Yellow,
3023    /// Purple.
3024    Purple,
3025    /// Red.
3026    Red,
3027    /// Orange.
3028    Orange,
3029    /// Pink.
3030    Pink,
3031}
3032
3033/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
3034/// applies its additions.
3035#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3036pub enum SetupMode {
3037    /// Read without mutation.
3038    Plan,
3039    /// Apply and verify.
3040    Apply,
3041}
3042
3043/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
3044/// against it goes on compiling.
3045pub type StatusOptionsMode = SetupMode;
3046
3047/// The explicit result of the requested operation.
3048#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3049#[serde(rename_all = "kebab-case")]
3050pub enum StatusOptionsOutcome {
3051    /// A read-only plan.
3052    Planned,
3053    /// Apply found nothing missing.
3054    Unchanged,
3055    /// Additions were applied and verified.
3056    Applied,
3057}
3058
3059/// A GitHub single-select option's opaque GraphQL node identifier.
3060#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3061#[serde(transparent)]
3062pub struct StatusOptionId(#[schemars(length(min = 1))] String);
3063
3064impl TryFrom<String> for StatusOptionId {
3065    type Error = String;
3066
3067    fn try_from(id: String) -> Result<Self, Self::Error> {
3068        if id.trim().is_empty() {
3069            return Err("a GitHub Status option id cannot be blank".to_owned());
3070        }
3071        Ok(Self(id))
3072    }
3073}
3074
3075/// One existing or proposed option in a guarded Status-field update.
3076#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3077pub struct StatusOption {
3078    /// GitHub's stable id.
3079    pub id: StatusOptionId,
3080    /// The visible option name.
3081    pub name: ColumnName,
3082    /// GitHub's single-select color token.
3083    pub color: StatusOptionColor,
3084    /// The option description, including an empty one.
3085    pub description: String,
3086}
3087
3088/// One board item's Status assignment, retained as recovery data.
3089#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3090pub struct StatusAssignment {
3091    /// The project item id whose assignment this is.
3092    // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
3093    // carried verbatim as operator recovery data; introducing a semantic type would claim
3094    // validation rules GitHub does not publish and no operation here interprets.
3095    pub item_id: String,
3096    /// The selected option, absent when the item has no status.
3097    #[serde(skip_serializing_if = "Option::is_none")]
3098    pub option: Option<AssignedStatusOption>,
3099}
3100
3101/// The inseparable id and name of an assigned option.
3102#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3103pub struct AssignedStatusOption {
3104    /// GitHub's stable id.
3105    pub id: StatusOptionId,
3106    /// The visible name.
3107    pub name: ColumnName,
3108}
3109
3110/// The plan and verified outcome of reconciling configured Status options.
3111#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3112pub struct StatusOptionsReport {
3113    /// The configured source name.
3114    pub source: SourceName,
3115    /// Configured option names absent before the operation.
3116    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
3117    // `ColumnName` and has therefore already passed its nonblank validation; retaining the
3118    // serialized string here preserves the report's intentionally simple public contract.
3119    pub missing: Vec<String>,
3120    /// What the requested operation did.
3121    pub outcome: StatusOptionsOutcome,
3122    /// The complete option list observed before any mutation.
3123    pub existing: Vec<StatusOption>,
3124}
3125
3126#[derive(Debug, Clone, PartialEq, Eq)]
3127struct StatusSnapshot {
3128    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3129    // passed back as the mutation's project identity; a newtype could enforce no stronger
3130    // invariant because GitHub publishes no grammar for it.
3131    board_id: String,
3132    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3133    // passed back as the mutation's field identity; a newtype could enforce no stronger
3134    // invariant because GitHub publishes no grammar for it.
3135    field_id: String,
3136    options: Vec<StatusOption>,
3137    assignments: Vec<StatusAssignment>,
3138}
3139
3140/// The name of the board field a status is held in.
3141const STATUS_FIELD: &str = "Status";
3142
3143/// Every item's value of each field `report` names, as it stood before the setup wrote
3144/// anything — what a person puts back when the setup is refused part way.
3145fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
3146    let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
3147        .fields
3148        .iter()
3149        .map(|field| (field.field.name(), before.assignments(field.field)))
3150        .collect();
3151    serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
3152        message: format!("cannot render the pre-write field recovery snapshot: {error}"),
3153    })
3154}
3155
3156/// One board field the guarded setup reads and writes — every one it reads, and the only
3157/// ones it writes.
3158#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
3159pub enum BoardField {
3160    /// The single-select `Status` field every instance's `status_mapping` resolves into.
3161    Status,
3162    /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
3163    Priority,
3164}
3165
3166impl BoardField {
3167    /// The field's name on the board.
3168    #[must_use]
3169    pub const fn name(self) -> &'static str {
3170        match self {
3171            Self::Status => STATUS_FIELD,
3172            Self::Priority => PRIORITY_FIELD,
3173        }
3174    }
3175
3176    /// The field a board calls `name`, or `None` for one this setup does not own.
3177    fn named(name: &str) -> Option<Self> {
3178        [Self::Status, Self::Priority]
3179            .into_iter()
3180            .find(|field| field.name() == name)
3181    }
3182}
3183
3184/// What the guarded setup did to one field.
3185#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3186#[serde(rename_all = "kebab-case")]
3187pub enum FieldOutcome {
3188    /// A read-only plan.
3189    Planned,
3190    /// Apply found the field there with every configured option.
3191    Unchanged,
3192    /// Missing options were added to the field that was there, and verified.
3193    Applied,
3194    /// The field was not there; it was created holding the configured options, and verified.
3195    Created,
3196}
3197
3198/// One field's plan, or its verified outcome.
3199#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3200pub struct FieldReport {
3201    /// Which field.
3202    pub field: BoardField,
3203    /// Whether the board had the field before the operation.
3204    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
3205    // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
3206    // "outcome", "existing"}` — so folding one into the other would change a published JSON
3207    // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
3208    // one constructor, and it derives `outcome` from `exists` in one match.
3209    pub exists: bool,
3210    /// Configured option names the field lacked before the operation — every one of them,
3211    /// in the order a new field lists them, when the field was not there at all.
3212    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3213    // mapping name and has therefore already passed its nonblank validation; the serialized
3214    // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
3215    pub missing: Vec<String>,
3216    /// For the `Status` field, which item kind each missing name is configured for: one
3217    /// entry per kind `status_mapping` names a missing option for, task before project, each
3218    /// listing that kind's missing names in category order. A name both kinds use is in
3219    /// both. Empty — and left out of the JSON — when nothing is missing, and always for
3220    /// `Priority`, which only a task holds.
3221    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3222    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3223    // both SDKs model an absent `kinds` as an empty list rather than as `null`.
3224    #[schemars(!skip_serializing_if)]
3225    pub kinds: Vec<KindMissing>,
3226    /// What the requested operation did.
3227    pub outcome: FieldOutcome,
3228    /// The field's complete option list observed before any mutation; empty when the field
3229    /// was not there.
3230    pub existing: Vec<StatusOption>,
3231}
3232
3233/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
3234#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3235pub struct KindMissing {
3236    /// The kind these names are configured for.
3237    pub kind: ItemKind,
3238    /// The names that kind maps a category to and the field lacked, in category order.
3239    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3240    // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
3241    // intentionally simple public contract.
3242    pub missing: Vec<String>,
3243}
3244
3245/// The plan and verified outcome of setting up every field a source's configuration names.
3246#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3247pub struct FieldsReport {
3248    /// The configured source name.
3249    pub source: SourceName,
3250    /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
3251    // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
3252    // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
3253    // per field would change a published JSON shape. The states the list could hold and the
3254    // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
3255    // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
3256    pub fields: Vec<FieldReport>,
3257    /// Each board text field the source's `metadata_fields` projects a value onto, in
3258    /// configured order. Empty — and left out of the JSON — when it projects none.
3259    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3260    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3261    // both SDKs model an absent list as an empty one rather than as `null`.
3262    #[schemars(!skip_serializing_if)]
3263    pub metadata_fields: Vec<MetadataFieldReport>,
3264}
3265
3266/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
3267/// its GraphQL type when it has none — which is what a projected field's conflict reports.
3268///
3269/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
3270/// no conflict.
3271// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
3272// refuses exactly what `try_from` below does.
3273#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3274#[serde(transparent)]
3275pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
3276
3277/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
3278/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
3279/// neither SDK's validator can be relied on to support.
3280const NON_TEXT_FIELD_TYPE_PATTERN: &str =
3281    r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
3282
3283impl TryFrom<String> for NonTextFieldType {
3284    type Error = String;
3285
3286    fn try_from(kind: String) -> Result<Self, Self::Error> {
3287        if kind.is_empty() || kind.starts_with(char::is_whitespace) {
3288            return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
3289        }
3290        if kind == "TEXT" {
3291            return Err("a TEXT field is no conflict for a projected text field".to_owned());
3292        }
3293        Ok(Self(kind))
3294    }
3295}
3296
3297impl std::fmt::Display for NonTextFieldType {
3298    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3299        formatter.write_str(&self.0)
3300    }
3301}
3302
3303/// One projected metadata text field's plan, or its verified outcome.
3304///
3305/// The setup creates a missing one as a text field and never alters a field that is there:
3306/// a same-named field of another type is reported in `conflict` and left as it is.
3307#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3308pub struct MetadataFieldReport {
3309    /// The board field's name.
3310    pub field: String, // llmlint: ignore[invalid_states_unrepresentable] Copied from a configuration entry `MetadataField::resolve` already validated; the serialized string is the report's intentionally simple public contract, as `FieldReport::missing`'s names are.
3311    /// The metadata key the value is read from.
3312    pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
3313    /// The object keys walked inside that key's value; empty for the key's own value.
3314    pub path: Vec<String>,
3315    /// Whether the board had a text field of that name before the operation.
3316    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` mirrors `FieldReport`'s published shape; `GitHubProjectsSource::fields` is the one constructor and derives `outcome` from `exists` and `conflict` in one match.
3317    pub exists: bool,
3318    /// The type of a field of that name that is not a text field, which the setup leaves as
3319    /// it is; absent — and left out of the JSON — when there is none.
3320    #[serde(default, skip_serializing_if = "Option::is_none")]
3321    pub conflict: Option<NonTextFieldType>,
3322    /// What the requested operation did: `planned`; `created` for a field that was not
3323    /// there; `unchanged` for one that was, and for a conflict, which is never changed.
3324    pub outcome: FieldOutcome,
3325}
3326
3327/// Which options one field is configured with, in the order a new field would list them.
3328struct FieldPlan {
3329    field: BoardField,
3330    wanted: Vec<String>,
3331}
3332
3333/// One single-select field as the guarded setup snapshots it.
3334#[derive(Debug, Clone, PartialEq, Eq)]
3335struct SnapshotField {
3336    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3337    // passed back as the mutation's field identity; a newtype could enforce no stronger
3338    // invariant because GitHub publishes no grammar for it.
3339    field_id: String,
3340    options: Vec<StatusOption>,
3341}
3342
3343/// Every single-select field of a board and every item's value of each.
3344#[derive(Debug, Clone, PartialEq, Eq)]
3345struct BoardSnapshot {
3346    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3347    // passed back as the mutation's project identity; a newtype could enforce no stronger
3348    // invariant because GitHub publishes no grammar for it.
3349    board_id: String,
3350    fields: BTreeMap<BoardField, SnapshotField>,
3351    /// Each board item's id, and its value of each field this setup owns that it holds one of.
3352    items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
3353}
3354
3355impl BoardSnapshot {
3356    /// Every item's value of `field`, in board order — the recovery data a drift refusal
3357    /// carries.
3358    fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
3359        self.items
3360            .iter()
3361            .map(|(item_id, values)| StatusAssignment {
3362                item_id: item_id.clone(),
3363                option: values.get(&field).cloned(),
3364            })
3365            .collect()
3366    }
3367}
3368
3369impl GitHubProjectsSource {
3370    /// Report missing configured Status options and, when `apply` is true, add them with
3371    /// a whole-list mutation that preserves every existing id and verifies the result.
3372    ///
3373    /// # Errors
3374    ///
3375    /// Refuses a board without a single-select `Status` field. A post-write difference in
3376    /// any pre-existing option id or item assignment is refused with the complete pre-write
3377    /// assignment snapshot in the diagnostic for recovery.
3378    // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
3379    // successful mutation, both drift refusals, source selection, missing Status, casing,
3380    // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
3381    // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
3382    // responses from entering the defensive malformed-response branches below.
3383    pub async fn status_options(
3384        &self,
3385        mode: StatusOptionsMode,
3386    ) -> Result<StatusOptionsReport, SourceError> {
3387        let before = self.status_snapshot().await?;
3388        // A terminal category's option is as configured as an open one's: a terminal
3389        // write validates it before closing and refuses when the board lacks it. Both
3390        // kinds' names are options of the one field, so both are asked for.
3391        let missing = self
3392            .statuses
3393            .wanted()
3394            .into_iter()
3395            .filter(|wanted| {
3396                !before
3397                    .options
3398                    .iter()
3399                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3400            })
3401            .collect::<Vec<_>>();
3402        let report = StatusOptionsReport {
3403            source: self.name.clone(),
3404            missing: missing.clone(),
3405            outcome: match (mode, missing.is_empty()) {
3406                (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
3407                (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
3408                (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
3409            },
3410            existing: before.options.clone(),
3411        };
3412        if mode == StatusOptionsMode::Plan || missing.is_empty() {
3413            return Ok(report);
3414        }
3415        let mut options = before
3416            .options
3417            .iter()
3418            .map(|option| {
3419                json!({
3420                    "id": option.id, "name": option.name, "color": option.color,
3421                    "description": option.description,
3422                })
3423            })
3424            .collect::<Vec<_>>();
3425        options.extend(missing.iter().map(|name| {
3426            json!({
3427                "name": name, "color": "GRAY", "description": ""
3428            })
3429        }));
3430        self.graphql(
3431            graphql::STATUS_OPTIONS_UPDATE,
3432            json!({"input": {
3433                "projectId": before.board_id, "fieldId": before.field_id,
3434                "singleSelectOptions": options,
3435            }}),
3436        )
3437        .await?;
3438        let after = self.status_snapshot().await?;
3439        let options_preserved = before
3440            .options
3441            .iter()
3442            .all(|old| after.options.iter().any(|new| new == old));
3443        let additions_present = missing.iter().all(|wanted| {
3444            after
3445                .options
3446                .iter()
3447                .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3448        });
3449        if !options_preserved || !additions_present || after.assignments != before.assignments {
3450            let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
3451                SourceError::Malformed {
3452                    message: format!("cannot render pre-write Status recovery snapshot: {error}"),
3453                }
3454            })?;
3455            return Err(SourceError::Refused {
3456                message: format!(
3457                    "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}"
3458                ),
3459            });
3460        }
3461        Ok(report)
3462    }
3463
3464    /// A fresh snapshot of the Status field and every board item's assignment of it.
3465    ///
3466    /// # Errors
3467    ///
3468    /// Refuses a board without a single-select `Status` field, and one the token cannot see.
3469    async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
3470        // Status alone, as this operation has always read it: a `Priority` field is another
3471        // operation's, so nothing about it can refuse this one.
3472        let mut board = self.board_snapshot(&[BoardField::Status]).await?;
3473        let field = board
3474            .fields
3475            .remove(&BoardField::Status)
3476            .ok_or_else(|| self.no_status_field())?;
3477        Ok(StatusSnapshot {
3478            assignments: board.assignments(BoardField::Status),
3479            board_id: board.board_id,
3480            field_id: field.field_id,
3481            options: field.options,
3482        })
3483    }
3484
3485    /// The refusal a board with no `Status` field is answered with by the guarded setup.
3486    fn no_status_field(&self) -> SourceError {
3487        SourceError::Refused {
3488            message: format!("source {} board has no Status field", self.name),
3489        }
3490    }
3491
3492    // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
3493    // the real CLI loopback journey, including pagination. The individual malformed guards
3494    // are defensive validation of a schema-pinned third-party response, not separate user
3495    // journeys; drift and missing-field failures cover the operation's recovery behavior.
3496    /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
3497    /// every board item's value of each, walked to the end of the board's items. A field not
3498    /// in `owned` is read past whatever it holds.
3499    async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
3500        let mut after: Option<String> = None;
3501        let mut snapshot: Option<BoardSnapshot> = None;
3502        loop {
3503            let data = self
3504                .graphql(
3505                    graphql::STATUS_OPTIONS_SNAPSHOT,
3506                    json!({
3507                        "owner": self.owner, "number": self.project_number,
3508                        "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
3509                    }),
3510                )
3511                .await?;
3512            let board = data
3513                .pointer("/owner/projectV2")
3514                .filter(|board| board.is_object())
3515                .ok_or_else(|| SourceError::Refused {
3516                    message: format!(
3517                        "source {} has no accessible GitHub Projects board",
3518                        self.name
3519                    ),
3520                })?;
3521            if board
3522                .pointer("/fields/pageInfo/hasNextPage")
3523                .and_then(Value::as_bool)
3524                != Some(false)
3525            {
3526                return Err(SourceError::Malformed {
3527                    message:
3528                        "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
3529                            .into(),
3530                });
3531            }
3532            let mut fields = BTreeMap::new();
3533            // Only the fields this setup owns, by name: a node the single-select fragment did not
3534            // match carries no name, and a person's own single-select field — a `Size`, a
3535            // `Team` — is none of this setup's business, so nothing about it can refuse one. A
3536            // `Status` or `Priority` field without its options is malformed, not absent.
3537            // 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.
3538            for (owned, field) in board
3539                .pointer("/fields/nodes")
3540                .and_then(Value::as_array)
3541                .ok_or_else(|| SourceError::Malformed {
3542                    message: "GitHub project fields.nodes is not an array".into(),
3543                })?
3544                .iter()
3545                .filter_map(|field| {
3546                    let named = BoardField::named(field.get("name")?.as_str()?)?;
3547                    owned.contains(&named).then_some((named, field))
3548                })
3549            {
3550                let options = field
3551                    .get("options")
3552                    .and_then(Value::as_array)
3553                    .ok_or_else(|| SourceError::Malformed {
3554                        message: "GitHub single-select field options is not an array".into(),
3555                    })?
3556                    .iter()
3557                    .map(|option| {
3558                        Ok(StatusOption {
3559                            id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
3560                                .map_err(|message| SourceError::Malformed { message })?,
3561                            name: ColumnName::try_from(required_str(option, "name")?.to_owned())
3562                                .map_err(|message| SourceError::Malformed {
3563                                    message: format!(
3564                                        "GitHub single-select option name is invalid: {message}"
3565                                    ),
3566                                })?,
3567                            color: serde_json::from_value(
3568                                option.get("color").cloned().unwrap_or(Value::Null),
3569                            )
3570                            .map_err(|error| {
3571                                SourceError::Malformed {
3572                                    message: format!(
3573                                        "GitHub single-select option color is invalid: {error}"
3574                                    ),
3575                                }
3576                            })?,
3577                            description: optional_str(option, "description")?
3578                                .unwrap_or_default()
3579                                .to_owned(),
3580                        })
3581                    })
3582                    .collect::<Result<Vec<_>, SourceError>>()?;
3583                let snapshot = SnapshotField {
3584                    field_id: required_nonblank_str(field, "id")?.to_owned(),
3585                    options,
3586                };
3587                // A board's field names are unique, so a second one is an answer that cannot
3588                // say which field the setup would act on — refused rather than one chosen.
3589                if fields.insert(owned, snapshot).is_some() {
3590                    return Err(SourceError::Malformed {
3591                        message: format!(
3592                            "GitHub answered two {} fields for this board",
3593                            owned.name()
3594                        ),
3595                    });
3596                }
3597            }
3598            let board_id = required_nonblank_str(board, "id")?.to_owned();
3599            let current = snapshot.get_or_insert_with(|| BoardSnapshot {
3600                board_id,
3601                fields,
3602                items: Vec::new(),
3603            });
3604            let items = board
3605                .pointer("/items/nodes")
3606                .and_then(Value::as_array)
3607                .ok_or_else(|| SourceError::Malformed {
3608                    message: "GitHub project items.nodes is not an array".into(),
3609                })?;
3610            for item in items {
3611                let field_values =
3612                    item.get("fieldValues")
3613                        .ok_or_else(|| SourceError::Malformed {
3614                            message: "GitHub project item is missing fieldValues".into(),
3615                        })?;
3616                if field_values
3617                    .pointer("/pageInfo/hasNextPage")
3618                    .and_then(Value::as_bool)
3619                    != Some(false)
3620                {
3621                    return Err(SourceError::Malformed {
3622                        message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
3623                    });
3624                }
3625                let values = item
3626                    .pointer("/fieldValues/nodes")
3627                    .and_then(Value::as_array)
3628                    .ok_or_else(|| SourceError::Malformed {
3629                        message: "GitHub project item fieldValues.nodes is not an array".into(),
3630                    })?;
3631                let item_id = required_nonblank_str(item, "id")?;
3632                let mut assigned = BTreeMap::new();
3633                for value in values {
3634                    let Some(field) = value
3635                        .pointer("/field/name")
3636                        .and_then(Value::as_str)
3637                        .and_then(BoardField::named)
3638                        .filter(|field| owned.contains(field))
3639                    else {
3640                        continue;
3641                    };
3642                    let held = assigned.insert(
3643                        field,
3644                        AssignedStatusOption {
3645                            id: StatusOptionId::try_from(
3646                                required_str(value, "optionId")?.to_owned(),
3647                            )
3648                            .map_err(|message| SourceError::Malformed { message })?,
3649                            name: ColumnName::try_from(required_str(value, "name")?.to_owned())
3650                                .map_err(|message| SourceError::Malformed {
3651                                    message: format!(
3652                                        "GitHub assigned {} name is invalid: {message}",
3653                                        field.name()
3654                                    ),
3655                                })?,
3656                        },
3657                    );
3658                    // An item holds one value of a field, so a second one leaves no way to
3659                    // tell which it holds — and a verification or recovery built on either
3660                    // could restore the wrong one.
3661                    if held.is_some() {
3662                        return Err(SourceError::Malformed {
3663                            message: format!(
3664                                "GitHub answered two {} values for board item {item_id}",
3665                                field.name()
3666                            ),
3667                        });
3668                    }
3669                }
3670                current.items.push((item_id.to_owned(), assigned));
3671            }
3672            let page = board.get("items").ok_or_else(|| SourceError::Malformed {
3673                message: "GitHub project is missing items".into(),
3674            })?;
3675            let has_next = page
3676                .pointer("/pageInfo/hasNextPage")
3677                .and_then(Value::as_bool)
3678                .ok_or_else(|| SourceError::Malformed {
3679                    message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
3680                })?;
3681            if !has_next {
3682                break;
3683            }
3684            let next =
3685                required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
3686            validate_cursor_progress(after.as_deref(), next)?;
3687            after = Some(next.to_owned());
3688        }
3689        snapshot.ok_or_else(|| SourceError::Malformed {
3690            message: "GitHub returned no board field snapshot".into(),
3691        })
3692    }
3693    // llmlint: ignore-end[changed_behavior_has_e2e]
3694
3695    /// Report every board field this source's configuration names and, with
3696    /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
3697    /// the `Priority` field when the board has none.
3698    ///
3699    /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
3700    /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
3701    /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
3702    /// color and description: the whole option list goes back with every existing id, because
3703    /// a re-minted id clears every item's value.
3704    ///
3705    /// # Errors
3706    ///
3707    /// Refuses a board without a single-select `Status` field. After an apply the board is
3708    /// read again, and a pre-existing option or any item's value of either field that moved is
3709    /// refused with the complete pre-write assignments in the diagnostic, for recovery.
3710    // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
3711    // unchanged apply, a created field, an added option to each field, drift refusal, a board
3712    // with no Status field and a non-github-projects source through the compiled CLI against
3713    // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
3714    pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
3715        let owned: Vec<BoardField> = if self.priorities.is_some() {
3716            vec![BoardField::Status, BoardField::Priority]
3717        } else {
3718            vec![BoardField::Status]
3719        };
3720        let before = self.board_snapshot(&owned).await?;
3721        let mut plans = vec![FieldPlan {
3722            field: BoardField::Status,
3723            wanted: self.statuses.wanted(),
3724        }];
3725        if !before.fields.contains_key(&BoardField::Status) {
3726            return Err(self.no_status_field());
3727        }
3728        if let Some(mapping) = &self.priorities {
3729            plans.push(FieldPlan {
3730                field: BoardField::Priority,
3731                wanted: mapping.names().map(str::to_owned).collect(),
3732            });
3733        }
3734        // The snapshot reads single-select fields alone, so a field it did not find may still
3735        // be on the board under the name, of another type: creating one beside it would fail
3736        // part way, or leave two fields of one name. Asked of the board's own field list, and
3737        // only when a field is missing.
3738        if plans
3739            .iter()
3740            .any(|plan| !before.fields.contains_key(&plan.field))
3741        {
3742            let board = self.board_fields().await?;
3743            for plan in plans
3744                .iter()
3745                .filter(|plan| !before.fields.contains_key(&plan.field))
3746            {
3747                if let Some(field) = Board::field(&board.fields, plan.field.name())? {
3748                    return Err(SourceError::Refused {
3749                        message: format!(
3750                            "source {}'s board has a {} field that is not a single-select field \
3751                             (it is a {}), so it cannot hold this source's options; next: rename \
3752                             or remove that field, then run this again",
3753                            self.name,
3754                            plan.field.name(),
3755                            optional_str(field, "__typename")?.unwrap_or("field of another type")
3756                        ),
3757                    });
3758                }
3759            }
3760        }
3761        let mut reports = Vec::new();
3762        for plan in &plans {
3763            let held = before.fields.get(&plan.field);
3764            let existing = held.map(|field| field.options.clone()).unwrap_or_default();
3765            let mut missing: Vec<String> = Vec::new();
3766            for wanted in &plan.wanted {
3767                let present = existing
3768                    .iter()
3769                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3770                    || missing
3771                        .iter()
3772                        .any(|named| named.eq_ignore_ascii_case(wanted));
3773                if !present {
3774                    missing.push(wanted.clone());
3775                }
3776            }
3777            let kinds = match plan.field {
3778                BoardField::Status => self.statuses.missing_by_kind(&existing),
3779                BoardField::Priority => Vec::new(),
3780            };
3781            reports.push(FieldReport {
3782                field: plan.field,
3783                exists: held.is_some(),
3784                kinds,
3785                outcome: match (mode, held.is_some(), missing.is_empty()) {
3786                    (SetupMode::Plan, _, _) => FieldOutcome::Planned,
3787                    (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
3788                    (SetupMode::Apply, true, false) => FieldOutcome::Applied,
3789                    (SetupMode::Apply, false, _) => FieldOutcome::Created,
3790                },
3791                missing,
3792                existing,
3793            });
3794        }
3795        // A projected field is a text field, which the single-select snapshot cannot see, so
3796        // it is asked of the board's own field list — and only when one is configured.
3797        let mut metadata_fields = Vec::new();
3798        if !self.metadata_fields.is_empty() {
3799            let board = self.board_fields().await?;
3800            for projection in &self.metadata_fields {
3801                let conflict = match Board::field(&board.fields, &projection.field)? {
3802                    None => None,
3803                    Some(field) => {
3804                        let typename = optional_str(field, "__typename")?;
3805                        let data_type = optional_str(field, "dataType")?;
3806                        if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
3807                            None
3808                        } else {
3809                            let named = data_type
3810                                .filter(|kind| *kind != "TEXT")
3811                                .or(typename)
3812                                .unwrap_or("field of another type");
3813                            Some(NonTextFieldType::try_from(named.to_owned()).map_err(
3814                                |message| SourceError::Malformed {
3815                                    message: format!(
3816                                        "GitHub answered the board's {} field with a type this \
3817                                         setup cannot read: {message}",
3818                                        &*projection.field
3819                                    ),
3820                                },
3821                            )?)
3822                        }
3823                    }
3824                };
3825                let exists =
3826                    conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
3827                metadata_fields.push(MetadataFieldReport {
3828                    field: projection.field.to_string(),
3829                    key: projection.key.to_string(),
3830                    path: projection.path.clone(),
3831                    exists,
3832                    outcome: match (mode, exists || conflict.is_some()) {
3833                        (SetupMode::Plan, _) => FieldOutcome::Planned,
3834                        (SetupMode::Apply, true) => FieldOutcome::Unchanged,
3835                        (SetupMode::Apply, false) => FieldOutcome::Created,
3836                    },
3837                    conflict,
3838                });
3839            }
3840        }
3841        let report = FieldsReport {
3842            source: self.name.clone(),
3843            fields: reports,
3844            metadata_fields,
3845        };
3846        let writes: Vec<&FieldReport> = report
3847            .fields
3848            .iter()
3849            .filter(|field| !field.missing.is_empty() || !field.exists)
3850            .collect();
3851        let creates: Vec<&MetadataFieldReport> = report
3852            .metadata_fields
3853            .iter()
3854            .filter(|field| field.outcome == FieldOutcome::Created)
3855            .collect();
3856        if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
3857            return Ok(report);
3858        }
3859        let mut landed: Vec<&str> = Vec::new();
3860        for field in &writes {
3861            let added = field
3862                .missing
3863                .iter()
3864                .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
3865            let sent = match before.fields.get(&field.field) {
3866                Some(held) => {
3867                    let mut options = held
3868                        .options
3869                        .iter()
3870                        .map(|option| {
3871                            json!({
3872                                "id": option.id, "name": option.name, "color": option.color,
3873                                "description": option.description,
3874                            })
3875                        })
3876                        .collect::<Vec<_>>();
3877                    options.extend(added);
3878                    self.graphql(
3879                        graphql::STATUS_OPTIONS_UPDATE,
3880                        json!({"input": {
3881                            "projectId": before.board_id, "fieldId": held.field_id,
3882                            "singleSelectOptions": options,
3883                        }}),
3884                    )
3885                    .await
3886                }
3887                None => {
3888                    self.graphql(
3889                        graphql::CREATE_FIELD,
3890                        json!({"input": {
3891                            "projectId": before.board_id, "dataType": "SINGLE_SELECT",
3892                            "name": field.field.name(),
3893                            "singleSelectOptions": added.collect::<Vec<_>>(),
3894                        }}),
3895                    )
3896                    .await
3897                }
3898            };
3899            // A mutation that failed does not establish that GitHub left its field as it was,
3900            // so every failure from here on carries the recovery data a drift refusal does.
3901            match sent {
3902                Ok(_) => landed.push(field.field.name()),
3903                Err(error) => {
3904                    let changed = if landed.is_empty() {
3905                        String::new()
3906                    } else {
3907                        format!("changed the {} field and then ", landed.join(" and "))
3908                    };
3909                    return Err(SourceError::Refused {
3910                        message: format!(
3911                            "the guarded field setup {changed}failed on the {} field, which it may \
3912                             have changed part way: {error}; the pre-write item assignments \
3913                             are:\n{}",
3914                            field.field.name(),
3915                            recovery(&report, &before)?
3916                        ),
3917                    });
3918                }
3919            }
3920        }
3921        for field in &creates {
3922            let sent = self
3923                .graphql(
3924                    graphql::CREATE_FIELD,
3925                    json!({"input": {
3926                        "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
3927                    }}),
3928                )
3929                .await;
3930            if let Err(error) = sent {
3931                let changed = if landed.is_empty() {
3932                    String::new()
3933                } else {
3934                    format!("changed the {} field and then ", landed.join(" and "))
3935                };
3936                return Err(SourceError::Refused {
3937                    message: format!(
3938                        "the guarded field setup {changed}failed creating the {} text field: \
3939                         {error}; run it again to create what is still missing",
3940                        field.field
3941                    ),
3942                });
3943            }
3944            landed.push(&field.field);
3945        }
3946        // The board has been written, so a verification read that fails leaves it unverified
3947        // rather than unchanged, and says what to put back.
3948        let after = match self.board_snapshot(&owned).await {
3949            Ok(after) => after,
3950            Err(error) => {
3951                return Err(SourceError::Refused {
3952                    message: format!(
3953                        "the guarded field setup changed the {} field and then could not read the \
3954                         board back to verify it: {error}; the pre-write item assignments are:\n{}",
3955                        landed.join(" and "),
3956                        recovery(&report, &before)?
3957                    ),
3958                });
3959            }
3960        };
3961        let mut moved = Vec::new();
3962        for field in &report.fields {
3963            let name = field.field.name();
3964            let now = after
3965                .fields
3966                .get(&field.field)
3967                .map(|held| held.options.as_slice())
3968                .unwrap_or_default();
3969            if !field.existing.iter().all(|old| now.contains(old)) {
3970                moved.push(format!(
3971                    "a pre-existing {name} option id, name, color or description"
3972                ));
3973            }
3974            if !field.missing.iter().all(|wanted| {
3975                now.iter()
3976                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3977            }) {
3978                moved.push(format!("an added {name} option"));
3979            }
3980            if after.assignments(field.field) != before.assignments(field.field) {
3981                moved.push(format!("an item's {name} value"));
3982            }
3983        }
3984        if !creates.is_empty() {
3985            // Read afresh: the view held for this command is the one from before the creates.
3986            *self.fields_cache()? = None;
3987            let board = match self.board_fields().await {
3988                Ok(board) => board,
3989                Err(error) => {
3990                    return Err(SourceError::Refused {
3991                        message: format!(
3992                            "the guarded field setup changed the {} field and then could not \
3993                             read the board back to verify it: {error}; run it again to verify \
3994                             it, which creates nothing a second time",
3995                            landed.join(" and ")
3996                        ),
3997                    });
3998                }
3999            };
4000            for field in &creates {
4001                let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
4002                    held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
4003                        && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
4004                });
4005                if !text {
4006                    moved.push(format!("the created {} text field", field.field));
4007                }
4008            }
4009        }
4010        if !moved.is_empty() {
4011            return Err(SourceError::Refused {
4012                message: format!(
4013                    "GitHub changed {} after the guarded field setup; the pre-write item \
4014                     assignments are:\n{}",
4015                    moved.join(", "),
4016                    recovery(&report, &before)?
4017                ),
4018            });
4019        }
4020        Ok(report)
4021    }
4022
4023    /// Validate configuration and capture the named credential without exposing it.
4024    ///
4025    /// # Errors
4026    ///
4027    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
4028    /// [`SourceError::Auth`] when the named credential is missing or empty.
4029    pub fn new(
4030        name: &SourceName,
4031        config: GitHubProjectsConfig,
4032        secrets: &dyn SecretResolver,
4033    ) -> Result<Self, SourceError> {
4034        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
4035    }
4036
4037    /// The same, recording every request it sends into an accounting the caller holds too.
4038    ///
4039    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
4040    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
4041    /// up — passes the one it records those into, so the session total accounts for the
4042    /// whole session rather than for this source's share of it.
4043    ///
4044    /// # Errors
4045    ///
4046    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
4047    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
4048    pub fn recording_into(
4049        name: &SourceName,
4050        config: GitHubProjectsConfig,
4051        secrets: &dyn SecretResolver,
4052        ledger: Arc<Accounting>,
4053    ) -> Result<Self, SourceError> {
4054        if !valid_github_owner(&config.owner) {
4055            return Err(SourceError::Config {
4056                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
4057            });
4058        }
4059        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
4060            return Err(SourceError::Config {
4061                message: format!("project_number must be between 1 and {}", i32::MAX),
4062            });
4063        }
4064        if !valid_environment_name(&config.token_env) {
4065            return Err(SourceError::Config {
4066                message: "token_env must be a valid environment-variable name".into(),
4067            });
4068        }
4069        let repository = config
4070            .repository
4071            .as_deref()
4072            .map(RepositoryTarget::parse)
4073            .transpose()?;
4074        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
4075            message: format!("endpoint is not a valid URL: {e}"),
4076        })?;
4077        if endpoint.scheme() != "https"
4078            && !(endpoint.scheme() == "http"
4079                && endpoint
4080                    .host_str()
4081                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
4082        {
4083            return Err(SourceError::Config {
4084                message:
4085                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
4086                        .into(),
4087            });
4088        }
4089        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
4090            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),
4091        })?;
4092        Ok(Self {
4093            name: name.clone(),
4094            owner: config.owner,
4095            project_number: config.project_number,
4096            repository,
4097            asset_client: assets::client(&endpoint)?,
4098            endpoint,
4099            token,
4100            credential_name: config.token_env,
4101            statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
4102            priorities: config
4103                .priority_mapping
4104                .map(|mapping| PriorityMapping::resolve(mapping, name))
4105                .transpose()?,
4106            metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
4107            client: Client::builder()
4108                .user_agent("onetaskgraph")
4109                .build()
4110                .map_err(|e| SourceError::Config {
4111                    message: format!("cannot build HTTP client: {e}"),
4112                })?,
4113            created: Mutex::new(Vec::new()),
4114            updated: Mutex::new(Vec::new()),
4115            commented: Mutex::new(Vec::new()),
4116            pacing: Pacing::resolve(config.pacing, name)?,
4117            last_mutation: Mutex::new(None),
4118            clock: system_clock(),
4119            numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
4120            board_cache: Mutex::new(None),
4121            search_cache: Mutex::new(None),
4122            narrowed_cache: Mutex::new(BTreeMap::new()),
4123            resolved_cache: Mutex::new(BTreeMap::new()),
4124            children_cache: Mutex::new(BTreeMap::new()),
4125            search_next: Mutex::new(BTreeMap::new()),
4126            fields_cache: Mutex::new(None),
4127            repository_cache: Mutex::new(BTreeMap::new()),
4128            ledger,
4129        })
4130    }
4131
4132    /// A snapshot of every request this source has sent, and what each cost.
4133    ///
4134    /// A value to hold and compare rather than a borrow of the accounting itself, so two
4135    /// of them can sit side by side. When this source was built with
4136    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
4137    /// point of building it that way.
4138    #[must_use]
4139    pub fn accounting(&self) -> accounting::Session {
4140        self.ledger.snapshot()
4141    }
4142
4143    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
4144    /// rate limit rather than handing it straight back as an error.
4145    ///
4146    /// Retrying is safe for every document here, including the mutations, and the reason
4147    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
4148    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
4149    /// this replays has already taken effect. An outcome this source cannot know — the
4150    /// send failed, or the body could not be read, so the mutation may well have landed —
4151    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
4152    /// attempt. A duplicate write would come from replaying one of those, and none is
4153    /// replayed.
4154    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4155        if is_mutation(query)
4156            && ![
4157                graphql::ADD_COMMENT,
4158                graphql::UPDATE_COMMENT,
4159                graphql::DELETE_COMMENT,
4160            ]
4161            .contains(&query)
4162        {
4163            let mut cache = self.resolved_cache()?;
4164            for argument in ["input", "second", "third", "clear"] {
4165                if let Some(input) = variables.get(argument) {
4166                    cache.retain(|id, item| {
4167                        !["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
4168                            input
4169                                .get(key)
4170                                .and_then(Value::as_str)
4171                                .is_some_and(|value| value == id.0 || value == item.item_id)
4172                        })
4173                    });
4174                }
4175            }
4176        }
4177        let doing = operation_description(query);
4178        let mut waited = Duration::ZERO;
4179        let mut waits = 0_u32;
4180        let mut backoff = self.pacing.retry_backoff;
4181        loop {
4182            if is_mutation(query) {
4183                let spacing = self.reserve_mutation_slot();
4184                if !spacing.is_zero() {
4185                    self.clock.sleep(spacing).await;
4186                }
4187            }
4188            let attempt = self.send_once(query, &variables).await;
4189            if is_mutation(query) {
4190                self.finish_mutation();
4191            }
4192            let limited = match attempt {
4193                Ok(data) => return Ok(data),
4194                Err(Attempt::Failed(error)) => return Err(error),
4195                Err(Attempt::Limited(limited)) => limited,
4196            };
4197            // GitHub really does send `retry-after: 0`, and retrying at once is the one
4198            // move that extends a secondary limit, so a hint below the schedule's own next
4199            // wait is raised to it.
4200            let wait = match limited.hint {
4201                Some(hint) => Duration::from_secs(hint).max(backoff),
4202                None => backoff,
4203            };
4204            let remaining = self.pacing.retry_budget.saturating_sub(waited);
4205            // A wait of nothing spends none of the budget, so it is exhaustion rather
4206            // than a retry. `Pacing::resolve` rules out every way of configuring one
4207            // except a budget of zero, where reporting the first refusal is the ask.
4208            if wait.is_zero() || wait > remaining {
4209                return Err(limited.exhausted(
4210                    doing,
4211                    waits,
4212                    waited,
4213                    wait,
4214                    self.pacing.retry_budget,
4215                ));
4216            }
4217            self.clock.sleep(wait).await;
4218            waited += wait;
4219            waits += 1;
4220            backoff = backoff.saturating_mul(2);
4221        }
4222    }
4223
4224    /// The next moment a content-creating mutation may leave this source, as a wait from
4225    /// now.
4226    ///
4227    /// The slot is reserved under the lock and the waiting happens outside it, so two
4228    /// callers take two slots rather than the same one — and no lock is held across an
4229    /// await.
4230    ///
4231    /// The moment it is spaced from is the previous mutation's *completion*, which
4232    /// [`Self::finish_mutation`] records. See that method for why the release moment on its
4233    /// own is the wrong thing to measure from.
4234    fn reserve_mutation_slot(&self) -> Duration {
4235        if self.pacing.min_mutation_interval.is_zero() {
4236            return Duration::ZERO;
4237        }
4238        // A poisoned lock here costs pacing, not correctness, and refusing the write over
4239        // it would turn an earlier failure into a second one for no gain.
4240        let mut last = self
4241            .last_mutation
4242            .lock()
4243            .unwrap_or_else(std::sync::PoisonError::into_inner);
4244        let now = self.clock.now();
4245        // `checked_add` rather than `+`: adding durations can panic on overflow, and
4246        // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
4247        let at = last.map_or(now, |previous| {
4248            previous
4249                .checked_add(self.pacing.min_mutation_interval)
4250                .map_or(now, |earliest| earliest.max(now))
4251        });
4252        *last = Some(at);
4253        at.saturating_sub(now)
4254    }
4255
4256    /// Record that a content-creating mutation has finished, so the next one is spaced
4257    /// from here rather than from the moment this one was released.
4258    ///
4259    /// This source can only choose when a request *departs*; the limiter counts when it
4260    /// *arrives*, and the two differ by whatever the request spent in transit. Spacing one
4261    /// departure from the last therefore hands the limiter a gap of the interval less that
4262    /// transit, so a source pacing at 750 ms can still be seen arriving faster — which is
4263    /// exactly how a copy paced well inside a board's threshold was refused by it on a
4264    /// slower machine while passing on a quick one.
4265    ///
4266    /// Spacing from completion removes the subtraction rather than budgeting for it. The
4267    /// previous request had already arrived before its response came back, so its arrival
4268    /// is no later than this moment, and the next mutation is released at least the
4269    /// interval after this moment and arrives no earlier than it is released: the gap the
4270    /// limiter measures is therefore at least the interval, whatever transit costs and on
4271    /// whatever platform. The price is that a mutation's own round trip no longer counts
4272    /// towards its spacing, which makes this source slightly slower than the configured
4273    /// rate rather than slightly faster — the safe side of a limit that punishes being
4274    /// wrong by refusing reads for the next fifty minutes.
4275    ///
4276    /// A failed attempt is recorded too: a request refused by the limiter still arrived,
4277    /// and one that never left costs only a wait nobody needed.
4278    fn finish_mutation(&self) {
4279        if self.pacing.min_mutation_interval.is_zero() {
4280            return;
4281        }
4282        // A poisoned lock here costs pacing, not correctness, exactly as in the reservation.
4283        let mut last = self
4284            .last_mutation
4285            .lock()
4286            .unwrap_or_else(std::sync::PoisonError::into_inner);
4287        let now = self.clock.now();
4288        // `max` rather than an assignment: a concurrent caller may already have reserved a
4289        // slot further out, and completing this request must never pull that slot back in.
4290        *last = Some(last.map_or(now, |reserved| reserved.max(now)));
4291    }
4292
4293    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
4294    /// failure that waiting cannot help — and recorded, whichever of the three it was.
4295    ///
4296    /// This is the one place a request leaves this crate, which is why the accounting is
4297    /// here rather than at each of the callers: a read path added later is counted without
4298    /// anybody remembering to count it, and
4299    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
4300    /// when one is not.
4301    async fn send_once(&self, query: &str, variables: &Value) -> Result<Value, Attempt> {
4302        let Attempted {
4303            result,
4304            limits,
4305            reported_cost,
4306        } = self.attempt(query, variables).await;
4307        // No `otherwise` name: every document this source sends is one of its own, and the
4308        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
4309        let sending = accounting::Request::graphql(query, variables, None, reported_cost);
4310        let outcome = match &result {
4311            Ok(_) => accounting::Outcome::Answered,
4312            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
4313            Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
4314        };
4315        self.ledger.record(sending.finished(outcome, limits));
4316        result
4317    }
4318
4319    /// The attempt itself, with what its response said about the rate limit alongside.
4320    ///
4321    /// The two are returned together rather than recorded here because every one of the
4322    /// early exits below is a different outcome, and a record written at each of them is a
4323    /// record one of them can be added without.
4324    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
4325        let mut limits = accounting::RateLimit::default();
4326        let mut reported_cost = None;
4327        let result = self
4328            .attempted(query, variables, &mut limits, &mut reported_cost)
4329            .await;
4330        Attempted {
4331            result,
4332            limits,
4333            reported_cost,
4334        }
4335    }
4336
4337    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
4338    async fn attempted(
4339        &self,
4340        query: &str,
4341        variables: &Value,
4342        limits: &mut accounting::RateLimit,
4343        reported_cost: &mut Option<u64>,
4344    ) -> Result<Value, Attempt> {
4345        let response = self
4346            .client
4347            .post(self.endpoint.clone())
4348            .bearer_auth(self.token.expose_secret())
4349            .json(&json!({"query": query, "variables": variables}))
4350            .send()
4351            .await
4352            .map_err(|e| {
4353                Attempt::Failed(SourceError::Unavailable {
4354                    message: format!("GitHub GraphQL request failed: {e}"),
4355                })
4356            })?;
4357        let status = response.status();
4358        let header = |name: &str| whole_seconds(response.headers().get(name));
4359        let served = response
4360            .headers()
4361            .get(reqwest::header::DATE)
4362            .and_then(|value| value.to_str().ok())
4363            .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
4364            .map(|at| at.with_timezone(&Utc));
4365        *limits = accounting::RateLimit::read(|name| {
4366            response
4367                .headers()
4368                .get(name)
4369                .and_then(|value| value.to_str().ok())
4370                .map(str::to_owned)
4371        });
4372        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
4373        // that are not text at all — is "not known to be exhausted". This never makes a
4374        // response a refusal on its own: it says which limiter a refusal is attributed to
4375        // and where its hint comes from, so a value this cannot read costs a hint rather
4376        // than an answer.
4377        let exhausted = response
4378            .headers()
4379            .get("x-ratelimit-remaining")
4380            .and_then(|value| value.to_str().ok())
4381            == Some("0");
4382        // `retry-after` is what GitHub asks for when it asks; when it does not and the
4383        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
4384        // which is the same question answered as an absolute time. Nothing else here is a
4385        // hint, and a schedule is what answers a refusal that carries none.
4386        let hint = header("retry-after").or_else(|| {
4387            exhausted
4388                .then(|| header("x-ratelimit-reset"))
4389                .flatten()
4390                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
4391        });
4392        // Read before it is parsed, because the evidence which tells a secondary rate
4393        // limit from a rejected credential is in the body of a response whose status says
4394        // only "forbidden" — and a non-success response was never parsed at all.
4395        let body = response.text().await.map_err(|e| {
4396            Attempt::Failed(SourceError::Unavailable {
4397                message: format!("GitHub GraphQL response could not be read: {e}"),
4398            })
4399        })?;
4400        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
4401            return Err(Attempt::Limited(Limited { limiter, hint }));
4402        }
4403        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
4404            return Err(Attempt::Failed(SourceError::Auth {
4405                message: format!(
4406                    "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"
4407                ),
4408            }));
4409        }
4410        if !status.is_success() {
4411            return Err(Attempt::Failed(SourceError::Unavailable {
4412                message: format!("GitHub GraphQL returned HTTP {status}"),
4413            }));
4414        }
4415        // GitHub reports what a call cost only when the document asked it to, and no
4416        // document this source sends does — so this is `None` here and carries the figure
4417        // for a caller whose own document selects `rateLimit { cost }`. What it must never
4418        // pick up is a `dryRun` probe's cost, which is some other document's.
4419        *reported_cost = serde_json::from_str::<Value>(&body)
4420            .ok()
4421            .as_ref()
4422            .and_then(|body| body.pointer("/data/rateLimit/cost"))
4423            .and_then(Value::as_u64);
4424        let mut data = self.answer(&body).map_err(Attempt::Failed)?;
4425        if let Some(served) = served {
4426            stamp_served(&mut data, &served.to_rfc3339());
4427        }
4428        Ok(data)
4429    }
4430
4431    /// What one successful HTTP response says, once its GraphQL errors are read.
4432    fn answer(&self, body: &str) -> Result<Value, SourceError> {
4433        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
4434            message: format!("GitHub returned invalid JSON: {e}"),
4435        })?;
4436        let errors = body
4437            .get("errors")
4438            .map(|value| {
4439                value.as_array().ok_or_else(|| SourceError::Malformed {
4440                    message: "GitHub response errors is not an array".into(),
4441                })
4442            })
4443            .transpose()?;
4444        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
4445            let messages = errors
4446                .iter()
4447                .filter_map(|e| e.get("message").and_then(Value::as_str))
4448                .collect::<Vec<_>>()
4449                .join("; ");
4450            let message = if messages.is_empty() {
4451                "GitHub returned GraphQL errors".into()
4452            } else {
4453                messages
4454            };
4455            let normalized = message.to_ascii_lowercase();
4456            if normalized.contains("resource not accessible") || normalized.contains("scope") {
4457                return Err(SourceError::Auth {
4458                    message: format!(
4459                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
4460                        self.credential_name
4461                    ),
4462                });
4463            }
4464            return Err(SourceError::Refused { message });
4465        }
4466        body.get("data")
4467            .filter(|data| data.is_object())
4468            .cloned()
4469            .ok_or_else(|| SourceError::Malformed {
4470                message: "GitHub response has no data object".into(),
4471            })
4472    }
4473
4474    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
4475    // GraphQL cannot independently page them inside the outer item page. This source page is
4476    // deliberately bounded at that published maximum; the live drift journey exercises it.
4477    async fn board_page(
4478        &self,
4479        items_after: Option<&str>,
4480        items_first: u32,
4481    ) -> Result<Value, SourceError> {
4482        let data = self
4483            .graphql(
4484                graphql::BOARD,
4485                json!({"owner":self.owner,"number":self.project_number,
4486                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
4487                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
4488            )
4489            .await?;
4490        data.pointer("/owner/projectV2")
4491            .filter(|v| !v.is_null())
4492            .cloned()
4493            .ok_or_else(|| SourceError::Refused {
4494                message: format!(
4495                    "GitHub project {}/{} was not found or is not visible to the token",
4496                    self.owner, self.project_number
4497                ),
4498            })
4499    }
4500
4501    /// The search that finds the issues of this board, narrowed by `also` when it is
4502    /// given.
4503    ///
4504    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
4505    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
4506    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
4507    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
4508    /// from a task by the `parent` field each issue carries rather than by the search.
4509    fn board_search(&self, also: Option<&str>) -> String {
4510        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
4511        match also {
4512            Some(also) => format!("{scope} {also}"),
4513            None => scope,
4514        }
4515    }
4516
4517    /// One issue this source reached directly, as the board item a read of the board would
4518    /// have produced — or `None` when this board does not hold it.
4519    ///
4520    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
4521    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
4522    /// item's own id, that item's field values, and the issue as its content. One resolver
4523    /// for both routes is what makes an issue read through a search, through its own node
4524    /// id, or through its project's sub-issues report the same title, the same status, the
4525    /// same labels and the same qualified id.
4526    ///
4527    /// An issue with no entry for *this* board is not this source's to report, which is
4528    /// what keeps an id naming some other repository's issue from being answered as an item
4529    /// of this board. That answer is given about an **exhausted** connection and never
4530    /// about an unread page: the entry is looked for on the page in hand, and only if that
4531    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
4532    /// rest of it.
4533    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
4534        if optional_str(issue, "__typename")? != Some("Issue") {
4535            return Ok(None);
4536        }
4537        let memberships = issue
4538            .get("projectItems")
4539            .ok_or_else(|| SourceError::Malformed {
4540                message: "GitHub issue is missing projectItems".into(),
4541            })?;
4542        let nodes = memberships
4543            .get("nodes")
4544            .and_then(Value::as_array)
4545            .ok_or_else(|| SourceError::Malformed {
4546                message: "GitHub issue projectItems.nodes is not an array".into(),
4547            })?;
4548        let held = match self.board_entry(nodes) {
4549            Some(held) => held.clone(),
4550            None => {
4551                let info = memberships
4552                    .get("pageInfo")
4553                    .ok_or_else(|| SourceError::Malformed {
4554                        message: "GitHub issue projectItems has no pageInfo".into(),
4555                    })?;
4556                // The page held no entry for this board. Whether that means the issue is
4557                // not on it is a question about the rest of the connection, and only a
4558                // connection with no rest answers it here.
4559                if !required_bool(info, "hasNextPage")? {
4560                    return Ok(None);
4561                }
4562                let cursor = required_str(info, "endCursor")?;
4563                validate_cursor_progress(None, cursor)?;
4564                let issue_id = required_str(issue, "id")?;
4565                match self.board_membership(issue_id, cursor).await? {
4566                    Some(held) => held,
4567                    None => return Ok(None),
4568                }
4569            }
4570        };
4571        // The membership's own `updatedAt` rides along: it is the board half of the item's change
4572        // token, which a field change moves where the issue's own time does not.
4573        let item = json!({
4574            "id": required_str(&held, "id")?,
4575            "updatedAt": held.get("updatedAt"),
4576            "project": held.get("project"),
4577            "fieldValues": held.get("fieldValues"),
4578            "content": issue,
4579        });
4580        self.resolve(&item)
4581    }
4582
4583    /// This board's own entry among one page of an issue's `Issue.projectItems`.
4584    ///
4585    /// One spelling of *which membership is this board's*, so the page a read carries and
4586    /// the pages [`Self::board_membership`] walks are searched by the same rule.
4587    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
4588        nodes.iter().find(|node| {
4589            node.pointer("/project/number").and_then(Value::as_u64)
4590                == Some(u64::from(self.project_number))
4591        })
4592    }
4593
4594    /// The rest of one issue's board memberships, from `after`, for this board's entry.
4595    ///
4596    /// The recovery read: a page of memberships that holds no entry for this board says
4597    /// nothing about the memberships past it, so the connection is walked to exhaustion
4598    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
4599    /// positive answer — the whole connection was read and no entry named this board —
4600    /// rather than a failure, and the walk is held to
4601    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
4602    /// with a cursor that does not advance is refused instead of spun on.
4603    async fn board_membership(
4604        &self,
4605        issue: &str,
4606        after: &str,
4607    ) -> Result<Option<Value>, SourceError> {
4608        let mut after = after.to_owned();
4609        loop {
4610            let data = self
4611                .graphql(
4612                    graphql::ISSUE_BOARD_ITEMS,
4613                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
4614                           "nestedFirst":NESTED_PAGE_SIZE}),
4615                )
4616                .await?;
4617            let Some(connection) = data
4618                .pointer("/node/projectItems")
4619                .filter(|value| !value.is_null())
4620            else {
4621                // The id resolved to nothing, or to something with no memberships to walk —
4622                // which is the same answer as a connection holding no entry for this board.
4623                return Ok(None);
4624            };
4625            let nodes = connection
4626                .get("nodes")
4627                .and_then(Value::as_array)
4628                .ok_or_else(|| SourceError::Malformed {
4629                    message: "GitHub issue projectItems.nodes is not an array".into(),
4630                })?;
4631            if let Some(held) = self.board_entry(nodes) {
4632                return Ok(Some(held.clone()));
4633            }
4634            let info = connection
4635                .get("pageInfo")
4636                .ok_or_else(|| SourceError::Malformed {
4637                    message: "GitHub issue projectItems has no pageInfo".into(),
4638                })?;
4639            let next = required_bool(info, "hasNextPage")?
4640                .then(|| required_str(info, "endCursor"))
4641                .transpose()?;
4642            match next {
4643                Some(next) => {
4644                    validate_cursor_progress(Some(&after), next)?;
4645                    after = next.to_owned();
4646                }
4647                None => return Ok(None),
4648            }
4649        }
4650    }
4651
4652    /// One page of a board-scoped issue search, and where the next page resumes.
4653    async fn search_page(
4654        &self,
4655        search: &str,
4656        first: u32,
4657        after: Option<&str>,
4658    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
4659        let data = self
4660            .graphql(
4661                graphql::SEARCH_ISSUES,
4662                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
4663                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
4664                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4665            )
4666            .await?;
4667        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
4668            message: "GitHub search response has no search connection".into(),
4669        })?;
4670        let mut found = Vec::new();
4671        for node in connection
4672            .get("nodes")
4673            .and_then(Value::as_array)
4674            .ok_or_else(|| SourceError::Malformed {
4675                message: "GitHub search nodes is not an array".into(),
4676            })?
4677        {
4678            if let Some(resolved) = self.resolve_issue(node).await? {
4679                found.push(resolved);
4680            }
4681        }
4682        let info = connection
4683            .get("pageInfo")
4684            .ok_or_else(|| SourceError::Malformed {
4685                message: "GitHub search connection has no pageInfo".into(),
4686            })?;
4687        let next = required_bool(info, "hasNextPage")?
4688            .then(|| required_str(info, "endCursor"))
4689            .transpose()?
4690            .map(str::to_owned);
4691        if let Some(next) = &next {
4692            validate_cursor_progress(after, next)?;
4693        }
4694        Ok((found, next))
4695    }
4696
4697    /// Every issue this board holds, completed with what this run wrote.
4698    ///
4699    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
4700    /// is an index and is eventually consistent, so an issue this run created seconds ago
4701    /// can be absent from it, and a project listed straight after being written would
4702    /// otherwise be missing from its own board. What is added back is only what this
4703    /// process itself wrote, out of [`Self::created`], which lives and dies with the
4704    /// process.
4705    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4706        let found = self.searched_issues().await?;
4707        self.completed_with_written(found, |_| true)
4708    }
4709
4710    /// Every issue this board's own search reports, walked to exhaustion, read once per
4711    /// source.
4712    ///
4713    /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
4714    /// needs it too and the two would otherwise walk the same search twice in one command.
4715    /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
4716    /// is.
4717    async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4718        let cached = self.search_cache()?.clone();
4719        if let Some(held) = cached {
4720            return Ok(held);
4721        }
4722        let mut after: Option<String> = None;
4723        let mut found = Vec::new();
4724        let search = self.board_search(None);
4725        loop {
4726            let (page, next) = self
4727                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
4728                .await?;
4729            found.extend(page);
4730            match next {
4731                Some(next) => after = Some(next),
4732                None => break,
4733            }
4734        }
4735        *self.search_cache()? = Some(found.clone());
4736        Ok(found)
4737    }
4738
4739    /// This process's own view of the board's issues, or the refusal a poisoned lock is.
4740    fn search_cache(
4741        &self,
4742    ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
4743        self.search_cache
4744            .lock()
4745            .map_err(|_| SourceError::Unavailable {
4746                message: "this source's view of the board's issues was left inconsistent by an \
4747                      earlier failure; next: run the command again"
4748                    .into(),
4749            })
4750    }
4751
4752    /// `found`, with everything this run wrote that `keep` accepts and the read did not
4753    /// report.
4754    ///
4755    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
4756    /// at all: the search index is behind, and a node read of an item filed moments ago can
4757    /// be too.
4758    fn completed_with_written(
4759        &self,
4760        mut found: Vec<Resolved>,
4761        keep: impl Fn(&Resolved) -> bool,
4762    ) -> Result<Vec<Resolved>, SourceError> {
4763        for own in self.created()?.iter().filter(|own| keep(own)) {
4764            if !found.iter().any(|item| item.id == own.id) {
4765                found.push(own.clone());
4766            }
4767        }
4768        Ok(found)
4769    }
4770
4771    /// What resolving one node id reached.
4772    ///
4773    /// Three answers rather than an `Option`, because a board *draft* is none of the other
4774    /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
4775    /// is completed by a read of the draft itself rather than reported as nothing.
4776    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
4777        let asked = self
4778            .graphql(
4779                graphql::ISSUE,
4780                json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
4781                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4782            )
4783            .await;
4784        let data = match asked {
4785            Ok(data) => data,
4786            // A string that is not a node id at all is not a failure to report: it is an id
4787            // this board does not hold, which is what every read of one already answers.
4788            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
4789            Err(error) => return Err(error),
4790        };
4791        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
4792            return Ok(Reached::Nothing);
4793        };
4794        if optional_str(node, "__typename")? == Some("DraftIssue") {
4795            return Ok(Reached::Draft);
4796        }
4797        Ok(match self.resolve_issue(node).await? {
4798            Some(item) => Reached::Held(Box::new(item)),
4799            None => Reached::Nothing,
4800        })
4801    }
4802
4803    /// One item of this board by its own id, whatever kind it is.
4804    ///
4805    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
4806    /// run wrote is read first, because a node read of an item created moments ago can
4807    /// still be behind the board field values written onto it — see [`Self::created`].
4808    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4809        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
4810            return Ok(Some(own.clone()));
4811        }
4812        match self.reach(id).await? {
4813            Reached::Held(item) => Ok(Some(*item)),
4814            Reached::Nothing => Ok(None),
4815            Reached::Draft => self.draft_by_id(id).await,
4816        }
4817    }
4818
4819    /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
4820    /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
4821    /// than one request per id.
4822    ///
4823    /// What this run wrote answers first, as it does there, and only the rest is read. One id
4824    /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
4825    /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
4826    /// id at a time, so that id is answered as not held and the others as themselves; a draft
4827    /// is completed by a read of the draft, exactly as there.
4828    async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
4829        let mut found: Vec<Option<Option<Resolved>>> = {
4830            let created = self.created()?;
4831            ids.iter()
4832                .map(|id| {
4833                    created
4834                        .iter()
4835                        .find(|own| own.id == *id)
4836                        .map(|own| Some(own.clone()))
4837                })
4838                .collect()
4839        };
4840        let unread: Vec<NativeId> = ids
4841            .iter()
4842            .zip(&found)
4843            .filter(|(_, found)| found.is_none())
4844            .map(|(id, _)| id.clone())
4845            .collect();
4846        let mut read = Vec::with_capacity(unread.len());
4847        if let [one] = unread.as_slice() {
4848            read.push(self.item_by_id(one).await?);
4849        } else {
4850            for batch in unread.chunks(DETAIL_BATCH) {
4851                let data = match self
4852                    .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
4853                    .await
4854                {
4855                    Ok(data) => data,
4856                    Err(error) if unresolvable_node(&error) => {
4857                        for id in batch {
4858                            read.push(self.item_by_id(id).await?);
4859                        }
4860                        continue;
4861                    }
4862                    Err(error) => return Err(error),
4863                };
4864                for (slot, id) in batch.iter().enumerate() {
4865                    let node =
4866                        data.get(format!("i{slot}"))
4867                            .ok_or_else(|| SourceError::Malformed {
4868                                message: format!(
4869                                    "GitHub answered a batch read with no item for {}",
4870                                    id.0
4871                                ),
4872                            })?;
4873                    read.push(if node.is_null() {
4874                        None
4875                    } else if optional_str(node, "__typename")? == Some("DraftIssue") {
4876                        self.draft_by_id(id).await?
4877                    } else {
4878                        if optional_str(node, "__typename")? == Some("Issue")
4879                            && required_str(node, "id")? != id.0
4880                        {
4881                            return Err(SourceError::Malformed {
4882                                message: format!(
4883                                    "GitHub answered the read of {} with issue {}",
4884                                    id.0,
4885                                    required_str(node, "id")?
4886                                ),
4887                            });
4888                        }
4889                        self.resolve_issue(node).await?
4890                    });
4891                }
4892            }
4893        }
4894        let mut read = read.into_iter();
4895        Ok(found
4896            .iter_mut()
4897            .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
4898            .collect())
4899    }
4900
4901    fn resolved_cache(
4902        &self,
4903    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
4904        self.resolved_cache
4905            .lock()
4906            .map_err(|_| SourceError::Unavailable {
4907                message: "resolved item records were left inconsistent; run the command again"
4908                    .into(),
4909            })
4910    }
4911
4912    /// Reuse a record this invocation already resolved. The mutation sender invalidates
4913    /// it before writing, so a partial failure cannot leave a pre-write binding behind.
4914    async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4915        let cached = self.resolved_cache()?.get(id).cloned();
4916        match cached {
4917            Some(item) => Ok(Some(item)),
4918            None => self.item_by_id(id).await,
4919        }
4920    }
4921
4922    /// One board draft by its own id, with the board item it sits in — or `None` when no
4923    /// item of this board is that draft's.
4924    ///
4925    /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
4926    /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
4927    /// links a draft to one board item, so the page this read carries is the whole of that
4928    /// connection, and a page that reports more than it holds is refused rather than read
4929    /// as an answer about memberships nobody read.
4930    async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4931        let data = self
4932            .graphql(
4933                graphql::DRAFT,
4934                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
4935                       "boardItems":BOARD_ITEMS_PAGE_SIZE}),
4936            )
4937            .await?;
4938        // Gone between the two reads is an answer — the draft is no longer there. Anything
4939        // else than the draft [`Self::reach`] was just told this id is, is not one.
4940        let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
4941            return Ok(None);
4942        };
4943        if optional_str(draft, "__typename")? != Some("DraftIssue") {
4944            return Err(SourceError::Malformed {
4945                message: format!(
4946                    "GitHub answered {} as a draft and then as something else",
4947                    id.0
4948                ),
4949            });
4950        }
4951        if required_str(draft, "id")? != id.0 {
4952            return Err(SourceError::Malformed {
4953                message: format!("GitHub answered a different draft for {}", id.0),
4954            });
4955        }
4956        let memberships = draft
4957            .get("projectV2Items")
4958            .ok_or_else(|| SourceError::Malformed {
4959                message: format!("GitHub draft {} is missing projectV2Items", id.0),
4960            })?;
4961        let nodes = memberships
4962            .get("nodes")
4963            .and_then(Value::as_array)
4964            .ok_or_else(|| SourceError::Malformed {
4965                message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
4966            })?;
4967        let info = memberships
4968            .get("pageInfo")
4969            .ok_or_else(|| SourceError::Malformed {
4970                message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
4971            })?;
4972        // Read whether or not this board's entry is on the page: a page claiming more than
4973        // the one item GitHub links a draft to is a malformed answer either way.
4974        if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
4975            return Err(SourceError::Malformed {
4976                message: format!(
4977                    "GitHub draft {} reports more board items than the one GitHub links a draft \
4978                     to",
4979                    id.0
4980                ),
4981            });
4982        }
4983        if let Some(node) = nodes.first()
4984            && node
4985                .pointer("/project/number")
4986                .and_then(Value::as_u64)
4987                .is_none()
4988        {
4989            return Err(SourceError::Malformed {
4990                message: format!(
4991                    "GitHub draft {} board item has no numeric project number",
4992                    id.0
4993                ),
4994            });
4995        }
4996        let Some(held) = self.board_entry(nodes) else {
4997            return Ok(None);
4998        };
4999        if required_str(
5000            held.get("project").ok_or_else(|| SourceError::Malformed {
5001                message: format!("GitHub draft {} board item has no project", id.0),
5002            })?,
5003            "id",
5004        )? != self.board_fields().await?.id.as_str()
5005        {
5006            return Ok(None);
5007        }
5008        let item = json!({
5009            "id": required_str(held, "id")?,
5010            "project": held.get("project"),
5011            "fieldValues": held.get("fieldValues"),
5012            "content": draft,
5013        });
5014        self.resolve(&item)
5015    }
5016
5017    /// The board's own id and field definitions, for a write whose item does not carry
5018    /// them — never its items.
5019    ///
5020    /// A board this command has already listed supplies them, since it read them beside its
5021    /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
5022    /// is consulted about which items the board holds: see the module documentation for
5023    /// why a question about one known item is answered by reading that item.
5024    async fn board_fields(&self) -> Result<BoardFields, SourceError> {
5025        if let Some(board) = self.board_cache()?.as_ref() {
5026            return Ok(BoardFields {
5027                id: BoardId::parse(&board.id)?,
5028                fields: board.fields.clone(),
5029            });
5030        }
5031        if let Some(held) = self.fields_cache()?.clone() {
5032            return Ok(held);
5033        }
5034        let data = self
5035            .graphql(
5036                graphql::BOARD_FIELDS,
5037                json!({"owner":self.owner,"number":self.project_number,
5038                       "nestedFirst":NESTED_PAGE_SIZE}),
5039            )
5040            .await?;
5041        self.fields_read(&data)
5042    }
5043
5044    /// The board's id and fields out of an answer carrying the `boardFields` root, held for
5045    /// the rest of this command.
5046    fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
5047        let board = data
5048            .pointer("/boardFields/projectV2")
5049            .filter(|value| !value.is_null())
5050            .ok_or_else(|| SourceError::Refused {
5051                message: format!(
5052                    "GitHub project {}/{} was not found or is not visible to the token",
5053                    self.owner, self.project_number
5054                ),
5055            })?;
5056        let read = BoardFields {
5057            id: BoardId::parse(required_str(board, "id")?)?,
5058            fields: board.get("fields").cloned().unwrap_or(Value::Null),
5059        };
5060        *self.fields_cache()? = Some(read.clone());
5061        Ok(read)
5062    }
5063
5064    /// Read what creating an issue in `repository` needs and this command has not read yet —
5065    /// the board's fields and the repository's node id — in one request when it needs both.
5066    ///
5067    /// When either is already known this sends nothing, and the other is read by its own
5068    /// document where it is asked for, so no create reads anything twice.
5069    async fn creation_context(
5070        &self,
5071        repository: &RepositoryTarget,
5072        incoming: &Incoming<'_>,
5073    ) -> Result<(), SourceError> {
5074        let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
5075        if fields_known || self.repository_cache()?.contains_key(repository) {
5076            return Ok(());
5077        }
5078        let data = self
5079            .graphql(
5080                graphql::CREATION_CONTEXT,
5081                json!({"owner":self.owner,"number":self.project_number,
5082                       "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
5083                       "repositoryName":repository.name}),
5084            )
5085            .await?;
5086        self.fields_read(&data)?;
5087        self.repository_read(&data, repository, incoming)?;
5088        Ok(())
5089    }
5090
5091    /// This process's own view of the board's fields, or the refusal a poisoned lock is.
5092    fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
5093        self.fields_cache
5094            .lock()
5095            .map_err(|_| SourceError::Unavailable {
5096                message: "this source's view of the board's fields was left inconsistent by an \
5097                      earlier failure; next: run the command again"
5098                    .into(),
5099            })
5100    }
5101
5102    /// What a write to `item` needs of the board, read off that item when it says enough and
5103    /// off [`Self::board_fields`] when it does not.
5104    ///
5105    /// A node read of an item names its board and carries the definition of every field it
5106    /// holds a value of — so an item naming its board, holding a value of the origin field,
5107    /// and, when the write carries a status, holding a `Status` value, needs no read of the
5108    /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
5109    /// of may still be on the board, and a view reading it as absent would refuse a write the
5110    /// board can take or skip a field write the board needs, so such an item — and a create,
5111    /// which has no item yet — takes the board's fields from their own read instead.
5112    async fn fields_for(
5113        &self,
5114        item: Option<&Resolved>,
5115        writes_status: bool,
5116        selects_priority: bool,
5117    ) -> Result<BoardFields, SourceError> {
5118        if let Some(board) = item.and_then(Resolved::carried_board) {
5119            return Ok(board);
5120        }
5121        if let Some(item) = item
5122            && let Some(board_id) = item.named_board()
5123            && item.defines(ORIGIN_FIELD)
5124            && (!writes_status || item.defines("Status"))
5125            && (!selects_priority || item.defines(PRIORITY_FIELD))
5126            && self
5127                .metadata_fields
5128                .iter()
5129                .all(|projection| item.defines(&projection.field))
5130        {
5131            return Ok(BoardFields {
5132                id: board_id,
5133                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
5134            });
5135        }
5136        self.board_fields().await
5137    }
5138
5139    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
5140    /// when that id names nothing here with a sub-issue relationship to walk.
5141    ///
5142    /// `None` and an empty answer are different: `None` is *this is not an issue of this
5143    /// GitHub*, which is what sends a project selector on to be read as a name, and an
5144    /// empty vector is a project that holds nothing.
5145    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
5146        let mut after: Option<String> = None;
5147        let mut children = Vec::new();
5148        loop {
5149            let asked = self
5150                .graphql(
5151                    graphql::SUB_ISSUES,
5152                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
5153                           "nestedFirst":NESTED_PAGE_SIZE,
5154                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5155                )
5156                .await;
5157            let data = match asked {
5158                Ok(data) => data,
5159                // A string that is not a node id at all is not a failure to report: it is
5160                // the ordinary answer to a selector naming a project by its name.
5161                Err(error) if unresolvable_node(&error) => return Ok(None),
5162                Err(error) => return Err(error),
5163            };
5164            let Some(connection) = data
5165                .pointer("/node/subIssues")
5166                .filter(|value| !value.is_null())
5167            else {
5168                // No such node, or one with no sub-issue relationship — a board draft is
5169                // the one this board can really hold.
5170                return Ok(None);
5171            };
5172            for node in connection
5173                .get("nodes")
5174                .and_then(Value::as_array)
5175                .ok_or_else(|| SourceError::Malformed {
5176                    message: "GitHub subIssues.nodes is not an array".into(),
5177                })?
5178            {
5179                if let Some(resolved) = self.resolve_issue(node).await? {
5180                    children.push(resolved);
5181                }
5182            }
5183            let info = connection
5184                .get("pageInfo")
5185                .ok_or_else(|| SourceError::Malformed {
5186                    message: "GitHub subIssues connection has no pageInfo".into(),
5187                })?;
5188            let next = required_bool(info, "hasNextPage")?
5189                .then(|| required_str(info, "endCursor"))
5190                .transpose()?;
5191            match next {
5192                Some(next) => {
5193                    validate_cursor_progress(after.as_deref(), next)?;
5194                    after = Some(next.to_owned());
5195                }
5196                None => return Ok(Some(children)),
5197            }
5198        }
5199    }
5200
5201    /// Which issue of this board a project *name* is, or `None` when none is.
5202    ///
5203    /// One bounded query which filters on that name at the server, rather than a walk of
5204    /// every issue the board holds. The name is compared again here: the qualifier narrows
5205    /// what GitHub sends, and this source decides what it names.
5206    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
5207        let search = self.board_search(Some(&title_qualifier(name)));
5208        let mut after = None;
5209        loop {
5210            let (candidates, next) = self
5211                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
5212                .await?;
5213            if let Some(item) = candidates.into_iter().find(|item| {
5214                item.kind == BoardKind::Work(ItemKind::Project)
5215                    && item.title.eq_ignore_ascii_case(name)
5216            }) {
5217                return Ok(Some(item.id));
5218            }
5219            match next {
5220                Some(next) => after = Some(next),
5221                None => return Ok(None),
5222            }
5223        }
5224    }
5225
5226    /// Everything filed under one project of this board: the sub-issues of the issue that
5227    /// project is.
5228    ///
5229    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
5230    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
5231    /// gains projects, or as another project gains tasks.
5232    ///
5233    /// A qualified id names the issue and is asked for its sub-issues directly: one
5234    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
5235    /// read as a project *name*, which costs the one bounded search
5236    /// [`Self::project_by_name`] makes.
5237    ///
5238    /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
5239    /// — and each answer is still cut to the items whose parent is this project, so one this
5240    /// process has since filed elsewhere is not reported here, and completed with what this
5241    /// process filed under it.
5242    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
5243        let held = self.children_cache()?.get(selector).cloned();
5244        let (project, children) = match held {
5245            Some(held) => held,
5246            None => {
5247                let answered = match self.sub_issues(selector).await? {
5248                    Some(children) => (selector.clone(), children),
5249                    None => match self.project_by_name(&selector.0).await? {
5250                        Some(project) => {
5251                            let children = self.sub_issues(&project).await?.unwrap_or_default();
5252                            (project, children)
5253                        }
5254                        None => return Ok(Vec::new()),
5255                    },
5256                };
5257                self.children_cache()?
5258                    .insert(selector.clone(), answered.clone());
5259                answered
5260            }
5261        };
5262        let mut children: Vec<Resolved> = children
5263            .into_iter()
5264            .filter(|child| child.parent.as_ref() == Some(&project))
5265            .collect();
5266        for own in self.updated()?.iter() {
5267            if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
5268                children.push(own.clone());
5269            }
5270        }
5271        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
5272    }
5273
5274    /// Every issue of this board GitHub's issue search reports updated at or after `since`,
5275    /// completed with what this run wrote — the candidates a comment-activity read confirms.
5276    ///
5277    /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
5278    /// on the board whatever repository, and whatever owner, it lives in, so no repository or
5279    /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
5280    /// sufficient is a fact about GitHub rather than about this source: a comment written on an
5281    /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
5282    /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
5283    /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
5284    /// rather than silently narrowing a caller's answer.
5285    ///
5286    /// The instant is written to the second, rounded down, which can only widen what the
5287    /// search returns; confirmation against each candidate's own comments is what makes the
5288    /// answer exact. The search is an index that lags a write by a second or two — the module
5289    /// documentation records it — so a caller that asks again from its last instant should
5290    /// overlap the two by more than that.
5291    async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
5292        let found = self.searched(&updated_qualifier(since)).await?;
5293        self.completed_with_written(found, |_| true)
5294    }
5295
5296    /// Every issue of this board GitHub's issue search reports for the board-scoped search
5297    /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
5298    ///
5299    /// Uncompleted: what this process wrote is added by the caller, which knows whether its
5300    /// own record is the fresher of the two.
5301    async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5302        let search = self.board_search(Some(also));
5303        let mut after: Option<String> = None;
5304        let mut found = Vec::new();
5305        loop {
5306            let (page, next) = self
5307                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
5308                .await?;
5309            found.extend(page);
5310            match next {
5311                Some(next) => after = Some(next),
5312                None => return Ok(found),
5313            }
5314        }
5315    }
5316
5317    /// A bounded task answer; the versioned cursor carries the connection position, how
5318    /// many rows of the page starting there were already handed out, and the own-write ids
5319    /// already observed, including across a new source instance.
5320    ///
5321    /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
5322    /// sliced from the pages it needs; why is the module documentation's paging contract.
5323    async fn search_tasks(
5324        &self,
5325        query: &TaskQuery,
5326        page: &PageRequest,
5327        also: &str,
5328    ) -> Result<Page<Task>, SourceError> {
5329        let mut position = match &page.cursor {
5330            None => SearchPosition::default(),
5331            Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
5332                .ok()
5333                .filter(|position| {
5334                    position.version == SEARCH_CURSOR_VERSION
5335                        && position.connection.valid_resume(position.offset)
5336                })
5337                .ok_or_else(|| SourceError::Config {
5338                    message: "page cursor is invalid".into(),
5339                })?,
5340        };
5341        let search = self.board_search(Some(also));
5342        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
5343        let own = self.with_own_writes(Vec::new())?;
5344        // An issue this process commented on is a candidate of a comment-activity read
5345        // whether or not the search has caught up with the comment; see `Self::commented`.
5346        let commented = match query.commented_since {
5347            Some(_) => self.commented()?.clone(),
5348            None => Vec::new(),
5349        };
5350        for id in own.iter().map(|item| &item.id).chain(&commented) {
5351            if !position.own.contains(id) {
5352                position.own.push(id.clone());
5353            }
5354        }
5355        let mut tasks = Vec::new();
5356        while !position.connection.exhausted() && tasks.len() < limit {
5357            let first = SEARCH_PAGE_SIZE;
5358            // Page size is part of the key: a short cached answer cannot answer a wider ask.
5359            let key =
5360                serde_json::to_string(&("page", &search, &position.connection.after(), first))
5361                    .expect("search page key is serializable");
5362            let cached = if query.commented_since.is_none() {
5363                self.narrowed_cache()?.get(&key).cloned()
5364            } else {
5365                None
5366            };
5367            let (found, next) = match cached {
5368                Some(found) => {
5369                    let next = self
5370                        .search_next
5371                        .lock()
5372                        .map_err(|_| SourceError::Unavailable {
5373                            message:
5374                                "search pagination was left inconsistent; run the command again"
5375                                    .into(),
5376                        })?
5377                        .get(&key)
5378                        .cloned()
5379                        .flatten();
5380                    (found, next)
5381                }
5382                None => {
5383                    let (found, next) = self
5384                        .search_page(&search, first, position.connection.after())
5385                        .await?;
5386                    if query.commented_since.is_none() {
5387                        self.search_next
5388                            .lock()
5389                            .map_err(|_| SourceError::Unavailable {
5390                                message:
5391                                    "search pagination was left inconsistent; run the command again"
5392                                        .into(),
5393                            })?
5394                            .insert(key.clone(), next.clone());
5395                        self.narrowed_cache()?.insert(key, found.clone());
5396                    }
5397                    (found, next)
5398                }
5399            };
5400            let rows = found.len();
5401            for mut item in found.into_iter().skip(position.offset) {
5402                if tasks.len() == limit {
5403                    break;
5404                }
5405                position.offset += 1;
5406                if position.own.contains(&item.id) {
5407                    if position.seen.contains(&item.id) {
5408                        continue;
5409                    }
5410                    position.seen.push(item.id.clone());
5411                    // The search's own copy of an issue this process only commented on is as
5412                    // good as a node read of it, since its comments are read either way.
5413                    let only_commented = commented.contains(&item.id)
5414                        && !own.iter().any(|written| written.id == item.id);
5415                    if !only_commented {
5416                        let updated_at = item.updated_at;
5417                        let Some(written) = self.search_written(&own, &item.id).await? else {
5418                            continue;
5419                        };
5420                        item = written;
5421                        item.updated_at = item.updated_at.max(updated_at);
5422                        self.resolved_cache()?.insert(item.id.clone(), item.clone());
5423                    }
5424                }
5425                if item.kind == BoardKind::Work(ItemKind::Task) {
5426                    let task = item.task()?;
5427                    if task_matches(&task, query, &query.project)
5428                        && self.commented_since(&item, query.commented_since).await?
5429                    {
5430                        tasks.push(task);
5431                    }
5432                }
5433            }
5434            if position.offset < rows {
5435                continue;
5436            }
5437            position.offset = 0;
5438            position.connection = match next {
5439                Some(after) => SearchConnection::Continuing {
5440                    after: Cursor(after),
5441                },
5442                None => SearchConnection::Exhausted {},
5443            };
5444        }
5445        if position.connection.exhausted() {
5446            for id in position.own.clone() {
5447                if position.seen.contains(&id) {
5448                    continue;
5449                }
5450                if tasks.len() == limit {
5451                    break;
5452                }
5453                position.seen.push(id.clone());
5454                let Some(item) = self.search_written(&own, &id).await? else {
5455                    continue;
5456                };
5457                if item.kind == BoardKind::Work(ItemKind::Task) {
5458                    let task = item.task()?;
5459                    if task_matches(&task, query, &query.project)
5460                        && self.commented_since(&item, query.commented_since).await?
5461                    {
5462                        tasks.push(task);
5463                    }
5464                }
5465            }
5466        }
5467        let more = !position.connection.exhausted()
5468            || position.own.iter().any(|id| !position.seen.contains(id));
5469        Ok(Page {
5470            items: tasks,
5471            next: more.then(|| {
5472                Cursor(serde_json::to_string(&position).expect("search position is serializable"))
5473            }),
5474        })
5475    }
5476
5477    /// A resumed process has the ids but no write records; resolve only a record the
5478    /// current page needs, by its uncached node read rather than the lagging search index.
5479    async fn search_written(
5480        &self,
5481        own: &[Resolved],
5482        id: &NativeId,
5483    ) -> Result<Option<Resolved>, SourceError> {
5484        match own.iter().find(|item| item.id == *id) {
5485            Some(item) => Ok(Some(item.clone())),
5486            None => self.item_by_id(id).await,
5487        }
5488    }
5489
5490    /// The candidates for a task query carrying a text, metadata or origin predicate, read
5491    /// without enumerating the board — or `None` for a query carrying none of the three, which
5492    /// keeps the reads it always had.
5493    ///
5494    /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
5495    /// because it names at most a handful of items. Text and metadata are answered by one
5496    /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
5497    /// further by `updated:>=` when the query also asks for comment activity, since both
5498    /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
5499    /// process afterwards by the same predicates [`task_matches`] applies to every read.
5500    ///
5501    /// Completed with what this process wrote, its own record winning over the index's copy
5502    /// of the same item: see [`Self::with_own_writes`].
5503    async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
5504        let asked = match (&query.origin, narrowing_qualifiers(query)) {
5505            (Some(origin), _) => Narrowing::Origin(origin.clone()),
5506            (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
5507                Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
5508                None => qualifiers,
5509            }),
5510            (None, None) => return Ok(None),
5511        };
5512        // A question about comment activity is asked afresh every time, as it always was: it
5513        // is the one a caller polls from one source while waiting for the index, and an
5514        // answer held from the first poll would be the answer to every later one.
5515        let key = query.commented_since.is_none().then(|| asked.key());
5516        let cached = match &key {
5517            Some(key) => self.narrowed_cache()?.get(key).cloned(),
5518            None => None,
5519        };
5520        let found = match cached {
5521            Some(found) => found,
5522            None => {
5523                let found = match &asked {
5524                    Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
5525                    Narrowing::Search(also) => self.searched(also).await?,
5526                };
5527                if let Some(key) = key {
5528                    self.narrowed_cache()?.insert(key, found.clone());
5529                }
5530                found
5531            }
5532        };
5533        self.with_own_writes(found).map(Some)
5534    }
5535
5536    /// The candidates for a project or unscoped document query carrying a searchable text,
5537    /// read without enumerating the board — or `None` for a query with no text or a blank one,
5538    /// which keeps the read it always had.
5539    ///
5540    /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
5541    /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
5542    /// costs is the issues that match and never the board. Its answer is held for the command
5543    /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
5544    /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
5545    /// substring rule, exactly as an item of the wider read was, and is completed with what this
5546    /// process wrote: see [`Self::with_own_writes`].
5547    async fn text_searched(
5548        &self,
5549        text: Option<&TextQuery>,
5550    ) -> Result<Option<Vec<Resolved>>, SourceError> {
5551        let Some(also) = text_qualifiers(text) else {
5552            return Ok(None);
5553        };
5554        let key = Narrowing::Search(also.clone()).key();
5555        let cached = self.narrowed_cache()?.get(&key).cloned();
5556        let found = match cached {
5557            Some(found) => found,
5558            None => {
5559                let found = self.searched(&also).await?;
5560                self.narrowed_cache()?.insert(key, found.clone());
5561                found
5562            }
5563        };
5564        self.with_own_writes(found).map(Some)
5565    }
5566
5567    /// Every item of this board that may carry `origin` — a superset of those that do — found
5568    /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
5569    ///
5570    /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
5571    /// which reads the field every carrier holds, whichever release wrote it — and the
5572    /// board-scoped issue search for the same id as a phrase in the body, where this source
5573    /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
5574    /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
5575    /// query's, exactly.
5576    ///
5577    /// Both connections are walked to exhaustion, each from its own cursor. One that has
5578    /// already ended is sent its last cursor again, which answers an empty page, so the one
5579    /// document serves every page of either. What the two leave is stated in the module
5580    /// documentation: a carrier another process added within the last second or two, before
5581    /// either index has it.
5582    async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
5583        let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
5584        let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
5585        let mut items_after: Option<String> = None;
5586        let mut search_after: Option<String> = None;
5587        let mut found: Vec<Resolved> = Vec::new();
5588        let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
5589            if !found.iter().any(|held| held.id == resolved.id) {
5590                found.push(resolved);
5591            }
5592        };
5593        loop {
5594            let data = self
5595                .graphql(
5596                    graphql::ORIGIN_LOOKUP,
5597                    json!({"owner":self.owner,"number":self.project_number,"filter":filter,
5598                           "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
5599                           "itemsAfter":items_after,"searchAfter":search_after,
5600                           "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
5601                           "duplicates":true}),
5602                )
5603                .await?;
5604            let items = data
5605                .pointer("/originItems/projectV2/items")
5606                .filter(|value| !value.is_null())
5607                .ok_or_else(|| SourceError::Refused {
5608                    message: format!(
5609                        "GitHub project {}/{} was not found or is not visible to the token",
5610                        self.owner, self.project_number
5611                    ),
5612                })?;
5613            for item in optional_nodes(Some(items), "project items")?
5614                .into_iter()
5615                .flatten()
5616            {
5617                // The board's own items list its drafts too, and a draft is not an issue: no
5618                // narrowed read answers with one, whatever its origin field holds.
5619                if let Some(resolved) = self.resolve(item)?
5620                    && resolved.content_kind == ContentKind::Issue
5621                {
5622                    keep(resolved, &mut found);
5623                }
5624            }
5625            let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
5626                message: "GitHub search response has no search connection".into(),
5627            })?;
5628            for node in optional_nodes(Some(searched), "search")?
5629                .into_iter()
5630                .flatten()
5631            {
5632                if let Some(resolved) = self.resolve_issue(node).await? {
5633                    keep(resolved, &mut found);
5634                }
5635            }
5636            let items_next = resumed(items, items_after.as_deref())?;
5637            let search_next = resumed(searched, search_after.as_deref())?;
5638            if !items_next.has_more() && !search_next.has_more() {
5639                return Ok(found);
5640            }
5641            items_after = items_next.cursor();
5642            search_after = search_next.cursor();
5643        }
5644    }
5645
5646    /// `found`, with every item this process created or wrote in its place, and every one of
5647    /// them the read did not report added.
5648    ///
5649    /// This process's own record wins over the read's copy of the same item, because a read
5650    /// of an item written moments ago can still be behind what was written onto it — the
5651    /// origin field included, which is the one a narrowed read is confirmed against — and a
5652    /// read that still names an item under a predicate this process's write moved it out of
5653    /// must not return it. The one thing the read knows that the record cannot is when GitHub
5654    /// last saw the item change, which is what a comment-activity read rules a candidate out
5655    /// by, so the read's `updatedAt` is kept when the record has none of its own. See
5656    /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
5657    fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
5658        // A board draft is not an issue, so no narrowed read returns one, and this process
5659        // having written one does not make it an answer either.
5660        let own: Vec<Resolved> = self
5661            .created()?
5662            .iter()
5663            .chain(self.updated()?.iter())
5664            .filter(|own| own.content_kind == ContentKind::Issue)
5665            .cloned()
5666            .collect();
5667        for mut own in own {
5668            self.resolved_cache()?.insert(own.id.clone(), own.clone());
5669            match found.iter_mut().find(|read| read.id == own.id) {
5670                Some(read) => {
5671                    own.updated_at = own.updated_at.max(read.updated_at);
5672                    *read = own;
5673                }
5674                None => found.push(own),
5675            }
5676        }
5677        Ok(found)
5678    }
5679
5680    /// Whether `item` has a comment created or last edited at or after `since` — always, when
5681    /// there is no instant to hold it to.
5682    ///
5683    /// The candidate's own `updatedAt` is read first, because a comment written or edited at
5684    /// or after the instant moved it there: an issue not updated since holds no such comment,
5685    /// and its comments are never asked for — unless this process commented on it in this
5686    /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
5687    /// only as far as the first that matches. A board draft is not an issue and has no
5688    /// comments, so it never matches.
5689    async fn commented_since(
5690        &self,
5691        item: &Resolved,
5692        since: Option<DateTime<Utc>>,
5693    ) -> Result<bool, SourceError> {
5694        let Some(since) = since else {
5695            return Ok(true);
5696        };
5697        if item.content_kind == ContentKind::DraftIssue {
5698            return Ok(false);
5699        }
5700        // An `updatedAt` this process's own record or a lagging index holds can predate a
5701        // comment this process wrote since, so only an issue it did not comment on is ruled
5702        // out by one.
5703        if item.updated_at.is_some_and(|updated| updated < since)
5704            && !self.commented()?.contains(&item.id)
5705        {
5706            return Ok(false);
5707        }
5708        let query = TaskQuery {
5709            commented_since: Some(since),
5710            ..TaskQuery::default()
5711        };
5712        let mut after: Option<String> = None;
5713        loop {
5714            let data = self
5715                .graphql(
5716                    graphql::ISSUE_COMMENTS,
5717                    json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
5718                )
5719                .await?;
5720            let Some(connection) = data
5721                .get("node")
5722                .filter(|value| !value.is_null())
5723                .and_then(|node| node.get("comments"))
5724                .filter(|value| !value.is_null())
5725            else {
5726                // Removed since the search reported it: no longer an issue with comments.
5727                return Ok(false);
5728            };
5729            let comments = optional_nodes(Some(connection), "issue comments")?
5730                .into_iter()
5731                .flatten()
5732                .map(comment_from)
5733                .collect::<Result<Vec<_>, _>>()?;
5734            if query.comments_match(&comments) {
5735                return Ok(true);
5736            }
5737            match next_cursor(connection)? {
5738                Some(next) => {
5739                    validate_cursor_progress(after.as_deref(), &next.0)?;
5740                    after = Some(next.0);
5741                }
5742                None => return Ok(false),
5743            }
5744        }
5745    }
5746
5747    /// Every item on the board: the union of both enumerations GitHub offers of one.
5748    ///
5749    /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
5750    /// board **draft** and reads the board's own fields beside its items, and only the search
5751    /// reports an item that connection is behind on. The module documentation is where the lag and the
5752    /// measurements behind it are written down.
5753    ///
5754    /// A search result is admitted on the same terms as any other issue this source reaches
5755    /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
5756    /// names *this* board — so an issue the index still believes is here after it was taken
5757    /// off is refused rather than reported.
5758    ///
5759    /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
5760    /// which is what the cache could otherwise have broken.
5761    async fn board(&self) -> Result<Board, SourceError> {
5762        let cached = self.board_cache()?.clone();
5763        let mut board = match cached {
5764            Some(board) => board,
5765            None => {
5766                let read = self.read_board().await?;
5767                *self.board_cache()? = Some(read.clone());
5768                read
5769            }
5770        };
5771        for held in self.searched_issues().await? {
5772            if !board.items.iter().any(|item| item.id == held.id) {
5773                board.items.push(held);
5774            }
5775        }
5776        for own in self.created()?.iter() {
5777            if !board.items.iter().any(|item| item.id == own.id) {
5778                board.items.push(own.clone());
5779            }
5780        }
5781        Ok(board)
5782    }
5783
5784    /// This process's own view of the board, or the refusal a poisoned lock is.
5785    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
5786        self.board_cache
5787            .lock()
5788            .map_err(|_| SourceError::Unavailable {
5789                message: "this source's view of the board was left inconsistent by an earlier \
5790                      failure; next: run the command again"
5791                    .into(),
5792            })
5793    }
5794
5795    /// Bring this process's own view of the board up to an item it has just written.
5796    ///
5797    /// A created item goes to `created`, which is what completes a board read GitHub's own
5798    /// eventual consistency has left behind. An item that was already there is replaced
5799    /// where it sits, so a second write of it in the same command reads its real parent
5800    /// rather than the one it had before the first write.
5801    ///
5802    /// "Where it sits" is three places, and missing an earlier one leaves a stale record
5803    /// that wins: an item this same run created is held in `created` and not in the cached
5804    /// board, and `board` completes the cached board *from* `created`, so replacing only
5805    /// the cached copy of such an item replaces nothing and the read still reports the
5806    /// title it was created with. The search is the third, and it is the one an item the
5807    /// board's own projection is behind on sits in *alone* — which is exactly the item this
5808    /// source is least able to re-read, so leaving it out would put the stale title back on
5809    /// the only items the completion in [`Self::board`] exists for.
5810    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
5811        self.resolved_cache()?.insert(item.id.clone(), item.clone());
5812        if created {
5813            self.created()?.push(item);
5814            return Ok(());
5815        }
5816        {
5817            let mut own = self.created()?;
5818            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
5819                *held = item;
5820                return Ok(());
5821            }
5822        }
5823        {
5824            let mut own = self.updated()?;
5825            match own.iter_mut().find(|held| held.id == item.id) {
5826                Some(held) => *held = item.clone(),
5827                None => own.push(item.clone()),
5828            }
5829        }
5830        if let Some(board) = self.board_cache()?.as_mut()
5831            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
5832        {
5833            *held = item.clone();
5834        }
5835        if let Some(found) = self.search_cache()?.as_mut()
5836            && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
5837        {
5838            *held = item.clone();
5839        }
5840        for found in self.narrowed_cache()?.values_mut() {
5841            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
5842                *held = item.clone();
5843            }
5844        }
5845        for (_, found) in self.children_cache()?.values_mut() {
5846            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
5847                *held = item.clone();
5848            }
5849        }
5850        Ok(())
5851    }
5852
5853    /// Forget one item this process has just deleted, from every half of its own view.
5854    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
5855        self.resolved_cache()?.remove(id);
5856        self.created()?.retain(|own| own.id != *id);
5857        self.updated()?.retain(|own| own.id != *id);
5858        self.commented()?.retain(|own| own != id);
5859        if let Some(board) = self.board_cache()?.as_mut() {
5860            board.items.retain(|item| item.id != *id);
5861        }
5862        if let Some(found) = self.search_cache()?.as_mut() {
5863            found.retain(|item| item.id != *id);
5864        }
5865        for found in self.narrowed_cache()?.values_mut() {
5866            found.retain(|item| item.id != *id);
5867        }
5868        for (_, found) in self.children_cache()?.values_mut() {
5869            found.retain(|item| item.id != *id);
5870        }
5871        Ok(())
5872    }
5873
5874    /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
5875    fn narrowed_cache(
5876        &self,
5877    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
5878        self.narrowed_cache
5879            .lock()
5880            .map_err(|_| SourceError::Unavailable {
5881                message: "this source's view of a narrowed read was left inconsistent by an \
5882                      earlier failure; next: run the command again"
5883                    .into(),
5884            })
5885    }
5886
5887    /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
5888    /// is.
5889    fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
5890        self.children_cache
5891            .lock()
5892            .map_err(|_| SourceError::Unavailable {
5893                message: "this source's view of a project's tasks was left inconsistent by an \
5894                      earlier failure; next: run the command again"
5895                    .into(),
5896            })
5897    }
5898
5899    /// Every page of the board, read from GitHub.
5900    async fn read_board(&self) -> Result<Board, SourceError> {
5901        let mut after: Option<String> = None;
5902        let mut items = Vec::new();
5903        let mut board;
5904        loop {
5905            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
5906            for item in page
5907                .pointer("/items/nodes")
5908                .and_then(Value::as_array)
5909                .ok_or_else(|| SourceError::Malformed {
5910                    message: "GitHub project items.nodes is not an array".into(),
5911                })?
5912            {
5913                if let Some(resolved) = self.resolve(item)? {
5914                    items.push(resolved);
5915                }
5916            }
5917            let info = page
5918                .pointer("/items/pageInfo")
5919                .ok_or_else(|| SourceError::Malformed {
5920                    message: "GitHub project items have no pageInfo".into(),
5921                })?;
5922            let has_next = required_bool(info, "hasNextPage")?;
5923            let next = has_next
5924                .then(|| required_str(info, "endCursor"))
5925                .transpose()?;
5926            board = page.clone();
5927            match next {
5928                Some(next) => {
5929                    validate_cursor_progress(after.as_deref(), next)?;
5930                    after = Some(next.to_owned());
5931                }
5932                None => break,
5933            }
5934        }
5935        Ok(Board {
5936            id: required_str(&board, "id")?.to_owned(),
5937            fields: board.get("fields").cloned().unwrap_or(Value::Null),
5938            items,
5939        })
5940    }
5941
5942    /// The existing items this source has written, for completing a narrowed read that is
5943    /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
5944    fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
5945        self.updated.lock().map_err(|_| SourceError::Unavailable {
5946            message: "this source's record of what it wrote in this run was left inconsistent \
5947                      by an earlier failure; next: run the command again"
5948                .into(),
5949        })
5950    }
5951
5952    /// The issues this source has commented on in this command; see
5953    /// [`Self::commented`](GitHubProjectsSource::commented).
5954    fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
5955        self.commented.lock().map_err(|_| SourceError::Unavailable {
5956            message: "this source's record of what it commented on in this run was left \
5957                      inconsistent by an earlier failure; next: run the command again"
5958                .into(),
5959        })
5960    }
5961
5962    /// Called only once GitHub has answered the comment write, so an issue whose comment
5963    /// failed is never made a candidate a later read would pay a node read for.
5964    fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
5965        let mut commented = self.commented()?;
5966        if !commented.contains(issue) {
5967            commented.push(issue.clone());
5968        }
5969        Ok(())
5970    }
5971
5972    /// The items this source has created, for completing a board read that is behind.
5973    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
5974        self.created.lock().map_err(|_| SourceError::Unavailable {
5975            message: "this source's record of what it created in this run was left \
5976                      inconsistent by an earlier failure; next: run the command again"
5977                .into(),
5978        })
5979    }
5980
5981    /// One board item as this source reports it, or `None` for content it ignores.
5982    ///
5983    /// A pull request is neither a project nor a task — it is somebody's change, not a
5984    /// unit of plan — and an item whose content the token cannot see has nothing to
5985    /// report at all.
5986    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
5987        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
5988            message: "GitHub project item is missing content".into(),
5989        })?;
5990        if content.is_null() {
5991            return Ok(None);
5992        }
5993        let content_kind = match required_str(content, "__typename")? {
5994            "Issue" => ContentKind::Issue,
5995            "DraftIssue" => ContentKind::DraftIssue,
5996            _ => return Ok(None),
5997        };
5998        let field_values = item
5999            .get("fieldValues")
6000            .ok_or_else(|| SourceError::Malformed {
6001                message: "GitHub project item is missing fieldValues".into(),
6002            })?;
6003        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
6004        let nodes = field_values
6005            .get("nodes")
6006            .and_then(Value::as_array)
6007            .ok_or_else(|| SourceError::Malformed {
6008                message: "GitHub project item fieldValues.nodes is not an array".into(),
6009            })?;
6010        if let Some(labels) = content.get("labels") {
6011            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
6012        }
6013        let raw_body = optional_str(content, "body")?.map(str::to_owned);
6014        let (body, slot) = metadata_body(raw_body.clone())?;
6015        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
6016            .map(|id| NativeId(id.to_owned()));
6017        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
6018        // to read one from; it is a task, and never a project.
6019        let sub_issues = match content_kind {
6020            ContentKind::Issue => sub_issue_total(content)?,
6021            ContentKind::DraftIssue => 0,
6022        };
6023        let content_id = required_str(content, "id")?;
6024        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
6025            message: format!("GitHub issue {content_id}: {message}"),
6026        })?;
6027        let raw_title = required_str(content, "title")?;
6028        // The design prefix is read *first*, before either of the two rules that separate
6029        // a project from a task. A document is not work whatever sub-issues it has and
6030        // whatever marker it carries, and reading the prefix later would make a design
6031        // issue with none of either an empty project.
6032        let kind = if raw_title.starts_with(DESIGN_TITLE_PREFIX) {
6033            BoardKind::Document
6034        } else if parent.is_some() {
6035            // Being a sub-issue wins outright, and no marker overrides it: an issue filed
6036            // under a project is that project's task even when it has sub-issues of its
6037            // own.
6038            BoardKind::Work(ItemKind::Task)
6039        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
6040            BoardKind::Work(ItemKind::Project)
6041        } else {
6042            BoardKind::Work(ItemKind::Task)
6043        };
6044        // The title a person wrote, which for a document is the one without the prefix —
6045        // the same way `content` above is the body without this source's metadata slot.
6046        let title = match kind {
6047            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
6048            BoardKind::Work(_) => raw_title.to_owned(),
6049        };
6050        let own_repository = content
6051            .pointer("/repository/nameWithOwner")
6052            .and_then(Value::as_str)
6053            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
6054            .transpose()
6055            .map_err(|message| SourceError::Malformed { message })?;
6056        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
6057            Repository::from_metadata(&slot)
6058                .map_err(|message| SourceError::Malformed { message })?
6059        } else {
6060            own_repository.clone().into_iter().collect()
6061        };
6062        let classification = Classification::from_metadata(&slot)
6063            .map_err(|message| SourceError::Malformed { message })?;
6064        let id = NativeId(content_id.to_owned());
6065        // Read only for a task, because only a task has either list: a project or a
6066        // document holding one of these keys holds nothing this source reports, and the
6067        // keys are left out of its caller-visible metadata all the same.
6068        let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
6069            let listed = |key: &str| {
6070                TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
6071                    .map_err(|message| SourceError::Malformed { message })
6072            };
6073            (
6074                listed(TaskRef::DELIVERS_KEY)?,
6075                listed(TaskRef::DELIVERED_BY_KEY)?,
6076            )
6077        } else {
6078            (Vec::new(), Vec::new())
6079        };
6080        let (option, closed, reason) = Self::status_parts(nodes, content)?;
6081        let priority = self.held_priority(nodes)?;
6082        // Present when the item was reached through its own issue, whose board entry
6083        // names the board; a read of the board's own items has the board already. An
6084        // empty id names nothing a field write could address, so it is read as absent and
6085        // the write goes back to reading the board.
6086        let board_id = item
6087            .pointer("/project/id")
6088            .and_then(Value::as_str)
6089            .filter(|id| !id.is_empty());
6090        let resolved = Resolved {
6091            item_id: required_str(item, "id")?.to_owned(),
6092            id,
6093            content_kind,
6094            kind,
6095            title,
6096            body: body.filter(|value| !value.is_empty()),
6097            raw_body,
6098            status: self
6099                .statuses
6100                .status(kind.status_kind(), option, closed, reason),
6101            option: option.map(str::to_owned),
6102            priority,
6103            closed,
6104            delivers,
6105            delivered_by,
6106            labels: labels(content)?,
6107            parent,
6108            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
6109            projected: self.held_projections(nodes)?,
6110            number: match content_kind {
6111                ContentKind::Issue => Some(issue_number(content)?),
6112                // A draft is filed in no repository, so nothing ever numbered it:
6113                // `DraftIssue` declares no `number` at all, exactly as it declares no
6114                // `subIssuesSummary` the branch above reads.
6115                ContentKind::DraftIssue => None,
6116            },
6117            url: optional_str(content, "url")?.map(str::to_owned),
6118            created_at: optional_time(content, "createdAt")?,
6119            updated_at: optional_time(content, "updatedAt")?,
6120            item_updated_at: optional_time(item, "updatedAt")?,
6121            comment_count: content
6122                .pointer("/commentCount/totalCount")
6123                .and_then(Value::as_u64),
6124            relationship_events: content
6125                .pointer("/relationshipEvents/totalCount")
6126                .and_then(Value::as_u64),
6127            // The issue node is stamped wherever it was read; a board item a read of the issue
6128            // put together from its memberships is not.
6129            served_at: match optional_time(content, SERVED_AT)? {
6130                Some(served) => Some(served),
6131                None => optional_time(item, SERVED_AT)?,
6132            },
6133            own_repository,
6134            repositories,
6135            classification,
6136            slot,
6137            board_id: board_id.map(str::to_owned),
6138            fields: field_definitions(nodes),
6139            board_fields: Self::carried_board_fields(content, board_id)?,
6140            blocked_by: carried_blocked_by(content)?,
6141        };
6142        self.resolved_cache()?
6143            .insert(resolved.id.clone(), resolved.clone());
6144        Ok(Some(resolved))
6145    }
6146
6147    /// The field definitions of the board `board_id` names — the project this issue's own
6148    /// board item is on — off the `boards` page a read of an issue by its own id carries, or
6149    /// `None` when the read carried none, carried no entry for that board, or the board item
6150    /// named no board, which a write then answers by reading the board's fields itself.
6151    ///
6152    /// Matched by the board's node id and never by its number alone: a project number is
6153    /// unique only within its owner, so another owner's board numbered alike can sit on the
6154    /// same page, and its field and option ids address nothing on this one.
6155    fn carried_board_fields(
6156        content: &Value,
6157        board_id: Option<&str>,
6158    ) -> Result<Option<Value>, SourceError> {
6159        let (Some(nodes), Some(board_id)) = (
6160            content.pointer("/boards/nodes").and_then(Value::as_array),
6161            board_id,
6162        ) else {
6163            return Ok(None);
6164        };
6165        let Some(board) = nodes.iter().find_map(|node| {
6166            let project = node.get("project")?;
6167            (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
6168        }) else {
6169            return Ok(None);
6170        };
6171        let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
6172            return Ok(None);
6173        };
6174        complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
6175        Ok(Some(fields.clone()))
6176    }
6177
6178    /// What one board item's `Priority` field says, through this instance's mapping.
6179    ///
6180    /// An instance with no mapping holds no priority, so every item reads as `none` whatever
6181    /// its board holds. With one, no value is `none`, a mapped option is its level, and an
6182    /// option the mapping does not name is kept as itself — never read as a level or as
6183    /// `none` — for a read of the task to report by name.
6184    fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
6185        let Some(mapping) = &self.priorities else {
6186            return Ok(HeldPriority::Read(Priority::None));
6187        };
6188        // A value of the field that names no option — a text field someone called `Priority` —
6189        // is malformed rather than `none`: reading it as no priority would let the next copy
6190        // clear one a person set.
6191        let Some(option) = field_values
6192            .iter()
6193            .find(|value| {
6194                value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
6195            })
6196            .map(|value| required_str(value, "name"))
6197            .transpose()?
6198        else {
6199            return Ok(HeldPriority::Read(Priority::None));
6200        };
6201        Ok(mapping.priority_of(option).map_or_else(
6202            || HeldPriority::Unmapped(option.to_owned()),
6203            HeldPriority::Read,
6204        ))
6205    }
6206
6207    /// What each projected metadata field holds for one board item, by field name — every text
6208    /// value GitHub answers, an empty one included.
6209    fn held_projections(
6210        &self,
6211        field_values: &[Value],
6212    ) -> Result<BTreeMap<String, String>, SourceError> {
6213        let mut held = BTreeMap::new();
6214        for projection in &self.metadata_fields {
6215            if let Some(text) = text_field(field_values, &projection.field)? {
6216                held.insert(projection.field.to_string(), text);
6217            }
6218        }
6219        Ok(held)
6220    }
6221
6222    /// What one board item's status is read from: its `Status` option, whether its issue
6223    /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
6224    /// three into the status it reports.
6225    fn status_parts<'a>(
6226        field_values: &'a [Value],
6227        content: &'a Value,
6228    ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
6229        let option = field_values
6230            .iter()
6231            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
6232            .map(|value| required_str(value, "name"))
6233            .transpose()?;
6234        let closed = optional_str(content, "state")? == Some("CLOSED");
6235        Ok((option, closed, optional_str(content, "stateReason")?))
6236    }
6237
6238    /// The board Status option this write selects, or the refusal that says why not.
6239    ///
6240    /// The mapped option is required for both open and terminal targets. A terminal write
6241    /// validates it before changing either representation, so it can never fall back to
6242    /// closing an issue whose board cannot display the matching status.
6243    ///
6244    /// Answers the field's id, the option's id, and the option's name as the board spells
6245    /// it — which is the name a read of the item reports once it sits there.
6246    fn column_for(
6247        &self,
6248        fields: &Value,
6249        kind: ItemKind,
6250        category: StatusCategory,
6251        target: &StatusTarget,
6252    ) -> Result<Option<(String, String, String)>, SourceError> {
6253        let Some(wanted) = target.option() else {
6254            return Ok(None);
6255        };
6256        let missing = |detail: &str| SourceError::Refused {
6257            message: format!(
6258                "{} status {} of source {} needs the board Status option {wanted:?}, and \
6259                 {detail}; next: add that option to the board, which `onetaskgraph sources \
6260                 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
6261                 it has",
6262                kind.marker(),
6263                category_name(category),
6264                self.name,
6265                self.name,
6266                category_name(category),
6267                kind.marker()
6268            ),
6269        };
6270        let Some(field) = Board::field(fields, "Status")? else {
6271            return Err(missing("this board has no Status field"));
6272        };
6273        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
6274            return Err(missing(
6275                "this board's Status field is not a single-select field",
6276            ));
6277        }
6278        let option = field
6279            .get("options")
6280            .and_then(Value::as_array)
6281            .and_then(|options| {
6282                options.iter().find(|option| {
6283                    option
6284                        .get("name")
6285                        .and_then(Value::as_str)
6286                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
6287                })
6288            });
6289        match option {
6290            None => Err(missing("this board does not have it")),
6291            Some(option) => Ok(Some((
6292                required_str(field, "id")?.to_owned(),
6293                required_str(option, "id")?.to_owned(),
6294                required_str(option, "name")?.to_owned(),
6295            ))),
6296        }
6297    }
6298
6299    /// The refusal a status that closes an issue is answered with over a board draft.
6300    fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
6301        SourceError::Refused {
6302            message: format!(
6303                "status {} of source {} closes the item's issue, and GitHub draft items have \
6304                 no open or closed state",
6305                category_name(category),
6306                self.name
6307            ),
6308        }
6309    }
6310
6311    /// What a status write to one item needs of the board: the board's id and the
6312    /// definition of its `Status` field, read off the item when the item says both.
6313    ///
6314    /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
6315    /// and its `Status` value carries that field's definition, options and all. An item that
6316    /// does not say — no board id, or no `Status` value to read the field off — takes them
6317    /// from [`Self::board_fields`], which reads no item.
6318    async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
6319        if let Some(board) = item.carried_board() {
6320            return Ok(board);
6321        }
6322        if item.defines("Status")
6323            && let Some(board_id) = item.named_board()
6324        {
6325            return Ok(BoardFields {
6326                id: board_id,
6327                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6328            });
6329        }
6330        self.board_fields().await
6331    }
6332
6333    /// Set one task's status and nothing else; see [`TaskSource::set_task_status`].
6334    async fn set_status(
6335        &self,
6336        id: &NativeId,
6337        category: StatusCategory,
6338    ) -> Result<Option<Status>, SourceError> {
6339        // Refused before anything is read, in the words a write of the same status is.
6340        let target = self.resolved_target(ItemKind::Task, category)?;
6341        let Some(mut item) = self
6342            .bound_item(id)
6343            .await?
6344            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6345        else {
6346            return Ok(None);
6347        };
6348        let board = self.status_board(&item).await?;
6349        let (field, option, name) = self
6350            .column_for(&board.fields, ItemKind::Task, category, &target)?
6351            .ok_or_else(|| SourceError::Malformed {
6352                message: format!(
6353                    "status {} of source {} names no board Status option",
6354                    category_name(category),
6355                    self.name
6356                ),
6357            })?;
6358        if item.status.category == category && item.option.as_deref() == Some(&name) {
6359            return Ok(Some(item.status));
6360        }
6361        match &target {
6362            StatusTarget::Terminal(_, reason) => {
6363                if item.content_kind == ContentKind::DraftIssue {
6364                    return Err(self.closes_a_draft(category));
6365                }
6366                self.set_item_field(
6367                    board.id.as_str(),
6368                    &item.item_id,
6369                    &field,
6370                    json!({"singleSelectOptionId": option}),
6371                )
6372                .await?;
6373                self.update_content(
6374                    ContentKind::Issue,
6375                    &item.id,
6376                    json!({"stateInput": state_input(Some(&target))}),
6377                )
6378                .await?;
6379                item.closed = true;
6380                item.status =
6381                    self.statuses
6382                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()));
6383                item.option = Some(name);
6384            }
6385            StatusTarget::Column(_) => {
6386                // An option is what an open item's status is, so a closed issue is reopened
6387                // first — sitting closed in the column, it would read back as closed. A draft has
6388                // no state to reopen.
6389                if item.content_kind == ContentKind::Issue && item.closed {
6390                    self.update_content(
6391                        ContentKind::Issue,
6392                        &item.id,
6393                        json!({"stateInput": state_input(Some(&target))}),
6394                    )
6395                    .await?;
6396                    item.closed = false;
6397                }
6398                self.set_item_field(
6399                    board.id.as_str(),
6400                    &item.item_id,
6401                    &field,
6402                    json!({"singleSelectOptionId": option}),
6403                )
6404                .await?;
6405                item.status = self
6406                    .statuses
6407                    .status(ItemKind::Task, Some(&name), false, None);
6408                item.option = Some(name);
6409            }
6410            StatusTarget::Disabled(_) => {
6411                unreachable!("resolved_target refused a disabled status")
6412            }
6413        }
6414        let status = item.status.clone();
6415        self.remember_written(item, false)?;
6416        Ok(Some(status))
6417    }
6418
6419    /// Replace one task's `delivered_by` and nothing else; see
6420    /// [`TaskSource::set_delivered_by`].
6421    ///
6422    /// One update of the body, which differs from the body GitHub holds only inside the
6423    /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
6424    async fn replace_delivered_by(
6425        &self,
6426        id: &NativeId,
6427        delivered_by: &[TaskRef],
6428    ) -> Result<Option<()>, SourceError> {
6429        let entries = TaskRef::listed(
6430            TaskRef::DELIVERED_BY_KEY,
6431            id,
6432            Some(&self.name),
6433            delivered_by.to_vec(),
6434        )
6435        .map_err(|message| SourceError::Refused { message })?;
6436        let Some(mut item) = self
6437            .bound_item(id)
6438            .await?
6439            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6440        else {
6441            return Ok(None);
6442        };
6443        let mut slot = item.slot.clone();
6444        set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
6445        self.write_slot(&mut item, &slot).await?;
6446        item.delivered_by = entries;
6447        self.remember_written(item, false)?;
6448        Ok(Some(()))
6449    }
6450
6451    /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
6452    /// see [`TaskSource::set_task_metadata`].
6453    ///
6454    /// `None` when this board holds no item by that id, or holds one of another kind. The
6455    /// answer is the item as this source now reads it, so what a caller is told the key
6456    /// holds is what the slot holds.
6457    ///
6458    /// A key already holding the value is answered without a write, compared as JSON rather
6459    /// than as the body's bytes: a slot a person spelled with other whitespace would
6460    /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
6461    async fn set_slot_key(
6462        &self,
6463        id: &NativeId,
6464        kind: BoardKind,
6465        key: &MetadataKey,
6466        value: &Value,
6467    ) -> Result<Option<Resolved>, SourceError> {
6468        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
6469            return Ok(None);
6470        };
6471        if item.slot.get(key.as_str()) == Some(value) {
6472            return Ok(Some(item));
6473        }
6474        let mut slot = item.slot.clone();
6475        slot.insert(key.as_str().to_owned(), value.clone());
6476        // A key a board text field projects moves that field first, so the body — the value's
6477        // one authoritative home — is written last, as every write of an existing item is.
6478        if self
6479            .metadata_fields
6480            .iter()
6481            .any(|projection| &*projection.key == key.as_str())
6482        {
6483            let board = self.projection_board(&item).await?;
6484            let (writes, projected) = self.projection_writes(
6485                &board.fields,
6486                Some(&item.projected),
6487                &slot,
6488                Some(&[key.as_str()]),
6489            )?;
6490            let (mut values, mut clears) = (Vec::new(), Vec::new());
6491            for write in &writes {
6492                write.join(&mut values, &mut clears);
6493            }
6494            self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
6495                .await?;
6496            item.projected = projected;
6497        }
6498        self.write_slot(&mut item, &slot).await?;
6499        self.remember_written(item.clone(), false)?;
6500        Ok(Some(item))
6501    }
6502
6503    /// What a projection write to `item` needs of the board, read off the item when it says
6504    /// enough, as [`Self::fields_for`] reads it.
6505    async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
6506        if let Some(board) = item.carried_board() {
6507            return Ok(board);
6508        }
6509        if let Some(board_id) = item.named_board()
6510            && self
6511                .metadata_fields
6512                .iter()
6513                .all(|projection| item.defines(&projection.field))
6514        {
6515            return Ok(BoardFields {
6516                id: board_id,
6517                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6518            });
6519        }
6520        self.board_fields().await
6521    }
6522
6523    /// Put `slot` in one item's metadata slot with a single update of its body, and bring
6524    /// `item` up to what that write left.
6525    ///
6526    /// The body sent differs from the body GitHub holds only inside the slot — see
6527    /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
6528    /// the mutation the item's content takes, so a board draft's body is written with
6529    /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
6530    async fn write_slot(
6531        &self,
6532        item: &mut Resolved,
6533        slot: &BTreeMap<String, Value>,
6534    ) -> Result<(), SourceError> {
6535        let held = item.raw_body.clone().unwrap_or_default();
6536        let body = with_slot(&held, slot)?;
6537        if body != held {
6538            self.update_content(item.content_kind, &item.id, json!({"body": body}))
6539                .await?;
6540        }
6541        let (visible, slot) = metadata_body(Some(body.clone()))?;
6542        item.body = visible.filter(|value| !value.is_empty());
6543        item.raw_body = Some(body);
6544        item.slot = slot;
6545        Ok(())
6546    }
6547
6548    /// This instance's target for a category written to an item of `kind`, refusing one
6549    /// that kind has no option for — before anything is read or written.
6550    ///
6551    /// Nothing here mutates the board's option set to make room for a status. GitHub
6552    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
6553    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
6554    /// field and every item's status.
6555    fn resolved_target(
6556        &self,
6557        kind: ItemKind,
6558        category: StatusCategory,
6559    ) -> Result<StatusTarget, SourceError> {
6560        let target = self.statuses.target(kind, category).clone();
6561        let StatusTarget::Disabled(why) = target else {
6562            return Ok(target);
6563        };
6564        let refusal = why.refusal(&self.name, category, kind);
6565        // Why there is no shipped default, which is the question a person meeting this
6566        // refusal on a source that never mentioned the category asks.
6567        let shipped_none = match category {
6568            StatusCategory::Draft => Some(
6569                "draft has no shipped default because GitHub draft issues cannot have \
6570                 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
6571            ),
6572            StatusCategory::Unknown => Some(
6573                "unknown has no shipped default because this board keeps no open-ended status \
6574                 word: every word classified unknown is written to the one board Status option \
6575                 status_mapping.unknown names",
6576            ),
6577            _ => None,
6578        };
6579        Err(match (refusal, shipped_none, why) {
6580            (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
6581                SourceError::Refused {
6582                    message: format!("{message}; {note}"),
6583                }
6584            }
6585            (refusal, _, _) => refusal,
6586        })
6587    }
6588
6589    /// What writing `priority` does to one item's `Priority` field on this board, or the
6590    /// refusal naming what the board lacks.
6591    ///
6592    /// `none` is no value, so it clears the field — and asks nothing of an item that holds
6593    /// none already, or of an item not created yet. Every other priority selects the option
6594    /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
6595    /// without that option, is refused rather than given one: reads and writes never create
6596    /// a field or an option.
6597    fn priority_write(
6598        &self,
6599        fields: &Value,
6600        existing: Option<&Resolved>,
6601        priority: Priority,
6602    ) -> Result<Option<PriorityWrite>, SourceError> {
6603        let Some(mapping) = &self.priorities else {
6604            return Err(self.holds_no_priority());
6605        };
6606        let Some(wanted) = mapping.option(priority) else {
6607            if !existing.is_some_and(Resolved::holds_priority) {
6608                return Ok(None);
6609            }
6610            let field =
6611                Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
6612                    message: format!(
6613                        "an item holding a {PRIORITY_FIELD} value was read without that field"
6614                    ),
6615                })?;
6616            return Ok(Some(PriorityWrite::Clear {
6617                field: required_str(field, "id")?.to_owned(),
6618            }));
6619        };
6620        let missing = |detail: &str| SourceError::Refused {
6621            message: format!(
6622                "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
6623                 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
6624                 it, or point priority_mapping.{priority} of this source at an option the board \
6625                 has",
6626                self.name, self.name
6627            ),
6628        };
6629        let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
6630            return Err(missing(&format!(
6631                "this board has no {PRIORITY_FIELD} field"
6632            )));
6633        };
6634        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
6635            return Err(missing(&format!(
6636                "this board's {PRIORITY_FIELD} field is not a single-select field"
6637            )));
6638        }
6639        // An options list that is absent or not a list is an answer this source cannot read,
6640        // not a board lacking the option: `sources fields --apply` is no remedy for it.
6641        let option = field
6642            .get("options")
6643            .and_then(Value::as_array)
6644            .ok_or_else(|| SourceError::Malformed {
6645                message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
6646            })?
6647            .iter()
6648            .find(|option| {
6649                option
6650                    .get("name")
6651                    .and_then(Value::as_str)
6652                    .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
6653            })
6654            .ok_or_else(|| missing("this board does not have it"))?;
6655        Ok(Some(PriorityWrite::Select {
6656            field: required_str(field, "id")?.to_owned(),
6657            option: required_str(option, "id")?.to_owned(),
6658        }))
6659    }
6660
6661    /// What writing `metadata` does to each projected text field of one item, and what those
6662    /// fields hold after it — or the refusal, before anything is sent.
6663    ///
6664    /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
6665    /// narrows the work to the entries projecting one of those metadata keys, for a write that
6666    /// touches only them; `None` is every entry. A string the field already holds, and nothing
6667    /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
6668    /// and so is a board without the field, or with a field of that name that is not a text
6669    /// field: reads and writes never create a field.
6670    fn projection_writes(
6671        &self,
6672        fields: &Value,
6673        held: Option<&BTreeMap<String, String>>,
6674        metadata: &BTreeMap<String, Value>,
6675        keys: Option<&[&str]>,
6676    ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
6677        let mut projected = held.cloned().unwrap_or_default();
6678        let mut writes = Vec::new();
6679        for projection in self
6680            .metadata_fields
6681            .iter()
6682            .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
6683        {
6684            let wanted = projection.projected(metadata, &self.name)?;
6685            let field = self.projection_field(fields, projection)?;
6686            let holding = projected.get(&*projection.field);
6687            match wanted {
6688                Projected::Text(text) if holding != Some(&text) => {
6689                    writes.push(ProjectionWrite::Set {
6690                        field,
6691                        text: text.clone(),
6692                    });
6693                    projected.insert(projection.field.to_string(), text);
6694                }
6695                Projected::Nothing if holding.is_some() => {
6696                    writes.push(ProjectionWrite::Clear { field });
6697                    projected.remove(&*projection.field);
6698                }
6699                Projected::Text(_) | Projected::Nothing => {}
6700            }
6701        }
6702        Ok((writes, projected))
6703    }
6704
6705    /// The id of the board text field one projection writes, or the refusal naming what the
6706    /// board has instead.
6707    fn projection_field(
6708        &self,
6709        fields: &Value,
6710        projection: &MetadataField,
6711    ) -> Result<String, SourceError> {
6712        let refuse = |detail: String| SourceError::Refused {
6713            message: format!(
6714                "source {} projects metadata key {:?} onto the board text field {:?}, and \
6715                 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
6716                 text field, or name another field in metadata_fields",
6717                self.name, &*projection.key, &*projection.field, self.name
6718            ),
6719        };
6720        let Some(field) = Board::field(fields, &projection.field)? else {
6721            return Err(refuse("this board has no field of that name".to_owned()));
6722        };
6723        let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
6724        let data_type = optional_str(field, "dataType")?;
6725        if typename != "ProjectV2Field" || data_type != Some("TEXT") {
6726            return Err(refuse(format!(
6727                "this board's field of that name is not a text field (it is a {}), so it is \
6728                 left as it is; rename or remove that field first",
6729                data_type.unwrap_or(typename)
6730            )));
6731        }
6732        Ok(required_str(field, "id")?.to_owned())
6733    }
6734
6735    /// Apply one priority write to one board item.
6736    async fn write_priority(
6737        &self,
6738        board_id: &str,
6739        item_id: &str,
6740        write: &PriorityWrite,
6741    ) -> Result<(), SourceError> {
6742        match write {
6743            PriorityWrite::Select { field, option } => {
6744                self.set_item_field(
6745                    board_id,
6746                    item_id,
6747                    field,
6748                    json!({"singleSelectOptionId": option}),
6749                )
6750                .await
6751            }
6752            PriorityWrite::Clear { field } => {
6753                let data = self
6754                    .graphql(
6755                        graphql::CLEAR_FIELD,
6756                        json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
6757                            "readPriority":false,"priorityName":PRIORITY_FIELD}),
6758                    )
6759                    .await?;
6760                let returned = data
6761                    .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
6762                    .ok_or_else(|| SourceError::Malformed {
6763                        message: "GitHub field clear returned no project item".into(),
6764                    })?;
6765                if required_str(returned, "id")? != item_id {
6766                    return Err(SourceError::Malformed {
6767                        message: "GitHub field clear returned the wrong project item".into(),
6768                    });
6769                }
6770                Ok(())
6771            }
6772        }
6773    }
6774
6775    /// The refusal a priority is answered with by an instance configured with no
6776    /// `priority_mapping`, which holds none.
6777    fn holds_no_priority(&self) -> SourceError {
6778        SourceError::Refused {
6779            message: format!(
6780                "source {} holds no task priority: its configuration sets no priority_mapping; \
6781                 next: set priority_mapping on this source, then run `onetaskgraph sources \
6782                 fields {} --apply` to set its board up",
6783                self.name, self.name
6784            ),
6785        }
6786    }
6787
6788    /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
6789    ///
6790    /// One field write — a select, or a clear for `none` — and no title, body, label, state
6791    /// or `Status` request. Clearing a priority an item does not hold sends nothing.
6792    async fn set_priority(
6793        &self,
6794        id: &NativeId,
6795        priority: Priority,
6796    ) -> Result<Option<Priority>, SourceError> {
6797        if self.priorities.is_none() {
6798            return Err(self.holds_no_priority());
6799        }
6800        let Some(mut item) = self
6801            .bound_item(id)
6802            .await?
6803            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6804        else {
6805            return Ok(None);
6806        };
6807        if priority == Priority::None && !item.holds_priority() {
6808            return Ok(Some(priority));
6809        }
6810        // The item's own read carries the field's definition whenever it holds a value of
6811        // it, which a clear always does; a select onto an item holding none reads the board.
6812        let board = match (item.carried_board(), item.named_board()) {
6813            (Some(board), _) => board,
6814            (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
6815                id,
6816                fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
6817            },
6818            _ => self.board_fields().await?,
6819        };
6820        let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
6821            return Ok(Some(priority));
6822        };
6823        let (document, root, input) = match write {
6824            PriorityWrite::Select { field, option } => (
6825                graphql::UPDATE_FIELD,
6826                "updateProjectV2ItemFieldValue",
6827                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
6828            ),
6829            PriorityWrite::Clear { field } => (
6830                graphql::CLEAR_FIELD,
6831                "clearProjectV2ItemFieldValue",
6832                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
6833            ),
6834        };
6835        let data = self
6836            .graphql(
6837                document,
6838                json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
6839            )
6840            .await?;
6841        let returned = data
6842            .get(root)
6843            .and_then(|value| value.get("projectV2Item"))
6844            .ok_or_else(|| SourceError::Malformed {
6845                message: "GitHub priority write returned no project item".into(),
6846            })?;
6847        if required_str(returned, "id")? != item.item_id {
6848            return Err(SourceError::Malformed {
6849                message: "GitHub priority write returned the wrong project item".into(),
6850            });
6851        }
6852        let value = returned
6853            .get("fieldValueByName")
6854            .ok_or_else(|| SourceError::Malformed {
6855                message: "GitHub priority write returned no priority read-back".into(),
6856            })?;
6857        if !value.is_null()
6858            && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
6859        {
6860            return Err(SourceError::Malformed {
6861                message: "GitHub priority read-back is not a Priority field value".into(),
6862            });
6863        }
6864        let values = if value.is_null() {
6865            Vec::new()
6866        } else {
6867            vec![value.clone()]
6868        };
6869        item.priority = self.held_priority(&values)?;
6870        let answer = item.task()?.priority;
6871        self.remember_written(item, false)?;
6872        Ok(Some(answer))
6873    }
6874
6875    /// Replace one task's visible body and nothing else; see
6876    /// [`TaskSource::set_task_content`].
6877    ///
6878    /// One update of the body, which differs from the body GitHub holds only outside the
6879    /// metadata slot — the slot is kept byte for byte, so every caller key and every list
6880    /// this source keeps there reads back as it was. A body that would not change is not
6881    /// sent at all.
6882    async fn replace_content(
6883        &self,
6884        id: &NativeId,
6885        content: &str,
6886    ) -> Result<Option<()>, SourceError> {
6887        let Some(mut item) = self
6888            .bound_item(id)
6889            .await?
6890            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6891        else {
6892            return Ok(None);
6893        };
6894        let held = item.raw_body.clone().unwrap_or_default();
6895        let body = with_content(&held, content)?;
6896        // Checked before anything is sent: content ending in what this source reads as its own
6897        // metadata slot would read back as metadata rather than as the content it was.
6898        let (visible, slot) = metadata_body(Some(body.clone()))?;
6899        if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
6900            return Err(SourceError::Refused {
6901                message: format!(
6902                    "this content ends in what source {} reads as its own metadata slot \
6903                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
6904                     as content; next: remove that trailing block from the content",
6905                    self.name
6906                ),
6907            });
6908        }
6909        if body != held {
6910            self.update_content(item.content_kind, &item.id, json!({"body": body}))
6911                .await?;
6912        }
6913        item.body = visible.filter(|value| !value.is_empty());
6914        item.raw_body = Some(body);
6915        item.slot = slot;
6916        self.remember_written(item, false)?;
6917        Ok(Some(()))
6918    }
6919
6920    /// Apply one targeted update to one task; see [`TaskSource::update_task`].
6921    ///
6922    /// One read of the item — which carries the board's field definitions and the issue's
6923    /// `blockedBy`, so neither is read again — and then only what differs from it: the
6924    /// `Status` option and the `Priority` field together in one request, the `blockedBy`
6925    /// additions and removals the named edges differ by, and last one `updateIssue` carrying
6926    /// the title, the body — visible content and metadata slot together — and a state change.
6927    /// So an update naming any of title, body, metadata, status and priority is one read and
6928    /// at most two writes. The body goes last so that a write refused part-way leaves it, and
6929    /// the metadata in it, as it stood. A terminal status selects its option and then closes,
6930    /// as a whole write does; an open one selects its option and then reopens. The origin
6931    /// field is never written: an update is of an item that already exists, whose origin is
6932    /// what it is.
6933    ///
6934    /// The task answered is the item as those writes left it, built from the read and what was
6935    /// sent rather than read again — the same record a later read in this run answers from.
6936    async fn targeted_update(
6937        &self,
6938        id: &NativeId,
6939        update: &TaskUpdate,
6940    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
6941        // Everything this source can refuse without reading the item is refused first, in the
6942        // words a whole write of the same fields is refused with.
6943        update.consistent()?;
6944        if update
6945            .title
6946            .as_deref()
6947            .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
6948        {
6949            return Err(SourceError::Refused {
6950                message: format!(
6951                    "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
6952                     spells a document, so it would read back as one rather than as a task; \
6953                     retitle it",
6954                    self.name
6955                ),
6956            });
6957        }
6958        if let Some(delivers) = &update.delivers {
6959            TaskRef::listed(
6960                TaskRef::DELIVERS_KEY,
6961                id,
6962                Some(&self.name),
6963                delivers.clone(),
6964            )
6965            .map_err(|message| SourceError::Refused { message })?;
6966        }
6967        if self.priorities.is_none()
6968            && update
6969                .priority
6970                .is_some_and(|priority| priority != Priority::None)
6971        {
6972            return Err(self.holds_no_priority());
6973        }
6974        let target = update
6975            .status
6976            .as_ref()
6977            .map(|status| self.resolved_target(ItemKind::Task, status.category))
6978            .transpose()?;
6979        let Some(mut item) = self
6980            .bound_item(id)
6981            .await?
6982            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6983        else {
6984            return Ok(None);
6985        };
6986        let before = item.task()?;
6987
6988        let mut status_move = None;
6989        if let (Some(status), Some(target)) = (&update.status, target) {
6990            let board = self.status_board(&item).await?;
6991            let (field, option, name) = self
6992                .column_for(&board.fields, ItemKind::Task, status.category, &target)?
6993                .ok_or_else(|| SourceError::Malformed {
6994                    message: format!(
6995                        "status {} of source {} names no board Status option",
6996                        category_name(status.category),
6997                        self.name
6998                    ),
6999                })?;
7000            let terminal = matches!(target, StatusTarget::Terminal(_, _));
7001            if terminal && item.content_kind == ContentKind::DraftIssue {
7002                return Err(self.closes_a_draft(status.category));
7003            }
7004            let landed = match &target {
7005                StatusTarget::Terminal(_, reason) => {
7006                    self.statuses
7007                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
7008                }
7009                _ => self
7010                    .statuses
7011                    .status(ItemKind::Task, Some(&name), false, None),
7012            };
7013            let option_moves = item
7014                .option
7015                .as_deref()
7016                .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
7017            let state_moves = item.content_kind == ContentKind::Issue
7018                && (item.closed != terminal || (terminal && item.status != landed));
7019            if let Some(moves) = Moves::of(option_moves, state_moves) {
7020                status_move = Some(StatusMove {
7021                    board: board.id,
7022                    field,
7023                    option,
7024                    name,
7025                    target,
7026                    landed,
7027                    moves,
7028                });
7029            }
7030        }
7031
7032        let mut priority_move = None;
7033        if let Some(priority) = update.priority
7034            && self.priorities.is_some()
7035            && item.priority != HeldPriority::Read(priority)
7036        {
7037            let board = match (item.carried_board(), item.named_board()) {
7038                (Some(board), _) => board,
7039                (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
7040                    id: board,
7041                    fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7042                },
7043                _ => self.board_fields().await?,
7044            };
7045            if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
7046                priority_move = Some((board.id, write, priority));
7047            }
7048        }
7049
7050        // The metadata keys this update names that a board text field projects, and what
7051        // that does to those fields — refused, as a whole write refuses it, before anything
7052        // is sent.
7053        let touched: Vec<&str> = update
7054            .metadata_set
7055            .keys()
7056            .chain(update.metadata_remove.iter())
7057            .map(MetadataKey::as_str)
7058            .collect();
7059        let mut projection_move = None;
7060        if self
7061            .metadata_fields
7062            .iter()
7063            .any(|projection| touched.contains(&&*projection.key))
7064        {
7065            let mut metadata = item.slot.clone();
7066            for (key, value) in &update.metadata_set {
7067                metadata.insert(key.as_str().to_owned(), value.clone());
7068            }
7069            for key in &update.metadata_remove {
7070                metadata.remove(key.as_str());
7071            }
7072            let board = self.projection_board(&item).await?;
7073            let (writes, projected) = self.projection_writes(
7074                &board.fields,
7075                Some(&item.projected),
7076                &metadata,
7077                Some(&touched),
7078            )?;
7079            projection_move = Some((board.id, writes, projected));
7080        }
7081
7082        // Resolved before the body is composed, because a far end `blockedBy` cannot name is
7083        // recorded in the slot, and the slot travels in the one body update below.
7084        let edges = match &update.depends_on {
7085            Some(edges) => Some(
7086                self.partition_edges(
7087                    BoardKind::Work(ItemKind::Task),
7088                    item.content_kind,
7089                    item.blocked_by.as_deref(),
7090                    edges,
7091                )
7092                .await?,
7093            ),
7094            None => None,
7095        };
7096
7097        let mut slot = item.slot.clone();
7098        for (key, value) in &update.metadata_set {
7099            slot.insert(key.as_str().to_owned(), value.clone());
7100        }
7101        for key in &update.metadata_remove {
7102            slot.remove(key.as_str());
7103        }
7104        if let Some(delivers) = &update.delivers {
7105            set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
7106        }
7107        if let Some((_, recorded)) = &edges {
7108            record_edges(&mut slot, recorded);
7109        }
7110        let held = item.raw_body.clone().unwrap_or_default();
7111        let content = match &update.content {
7112            Some(content) => with_content(&held, content)?,
7113            None => held.clone(),
7114        };
7115        // A slot holding what it held is kept byte for byte, compared as JSON rather than as
7116        // the body's bytes, as a metadata write compares it: a slot a person spelled with
7117        // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
7118        let body = if slot == item.slot {
7119            content
7120        } else {
7121            with_slot(&content, &slot)?
7122        };
7123        // Checked before anything is sent, as a content write checks it: content ending in
7124        // what this source reads as its own slot would read back as metadata.
7125        let (visible, read) = metadata_body(Some(body.clone()))?;
7126        let wanted = update.content.as_deref().or(item.body.as_deref());
7127        if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
7128            return Err(SourceError::Refused {
7129                message: format!(
7130                    "this content ends in what source {} reads as its own metadata slot \
7131                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7132                     as content; next: remove that trailing block from the content",
7133                    self.name
7134                ),
7135            });
7136        }
7137        let recorded_moves =
7138            slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
7139
7140        // One `updateIssue` carries all three, because every mutation spends the secondary
7141        // limiter and the title, body and state are one mutation's inputs.
7142        let mut fields = serde_json::Map::new();
7143        if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
7144            fields.insert("title".to_owned(), json!(title));
7145        }
7146        if body != held {
7147            fields.insert("body".to_owned(), json!(body));
7148        }
7149        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
7150            fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
7151        }
7152        // **The body is written last, and that is the guarantee a refusal part-way keeps.**
7153        // GitHub runs no two requests as one, and runs one document's mutation fields in order
7154        // without undoing an earlier field when a later one fails — so a body written before a
7155        // board field the board then refused would be left changed. Written after every other
7156        // write has landed, a refusal anywhere leaves the item's body, and every metadata key
7157        // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
7158        // first, together in one request — a terminal option selected before the issue
7159        // closes, as a whole write does — then the `blockedBy` difference, then the body.
7160        let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
7161        let mut clears: Vec<(&BoardId, &str)> = Vec::new();
7162        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
7163            board_writes.push((
7164                &moving.board,
7165                (
7166                    moving.field.clone(),
7167                    json!({"singleSelectOptionId": moving.option}),
7168                ),
7169            ));
7170        }
7171        match &priority_move {
7172            Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
7173                board,
7174                (field.clone(), json!({"singleSelectOptionId": option})),
7175            )),
7176            Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
7177            None => {}
7178        }
7179        if let Some((board, writes, _)) = &projection_move {
7180            for write in writes {
7181                let (mut values, mut cleared) = (Vec::new(), Vec::new());
7182                write.join(&mut values, &mut cleared);
7183                board_writes.extend(values.into_iter().map(|value| (board, value)));
7184                clears.extend(cleared.into_iter().map(|field| (board, field)));
7185            }
7186        }
7187        let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
7188        boards.extend(clears.iter().map(|(board, _)| *board));
7189        boards.dedup_by(|one, other| one.as_str() == other.as_str());
7190        for board in boards {
7191            let writes = board_writes
7192                .iter()
7193                .filter(|(on, _)| on.as_str() == board.as_str())
7194                .map(|(_, write)| write.clone())
7195                .collect::<Vec<_>>();
7196            let cleared = clears
7197                .iter()
7198                .filter(|(on, _)| on.as_str() == board.as_str())
7199                .map(|(_, field)| *field)
7200                .collect::<Vec<_>>();
7201            self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
7202                .await?;
7203        }
7204        let mut blocked_by_moved = false;
7205        if let Some((native, _)) = &edges
7206            && item.content_kind == ContentKind::Issue
7207        {
7208            blocked_by_moved = self
7209                .reconcile_blocked_by(
7210                    &item.id,
7211                    native,
7212                    Issue::Existing(item.blocked_by.as_deref()),
7213                )
7214                .await?;
7215        }
7216        if !fields.is_empty() {
7217            self.update_content(item.content_kind, &item.id, Value::Object(fields))
7218                .await?;
7219        }
7220
7221        // The `blockedBy` this item was read with is what the write just changed, so it is no
7222        // answer any more: a later write in this command would reconcile against it — re-adding
7223        // what it kept and never removing what it dropped — and a forward-edge read would
7224        // answer with it. Left unknown, each asks GitHub instead.
7225        if blocked_by_moved {
7226            item.blocked_by = None;
7227        }
7228        if let Some(title) = &update.title {
7229            item.title.clone_from(title);
7230        }
7231        item.body = visible.filter(|value| !value.is_empty());
7232        item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
7233        item.slot = slot;
7234        if let Some(delivers) = &update.delivers {
7235            item.delivers.clone_from(delivers);
7236        }
7237        if let Some(moving) = status_move {
7238            item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
7239                && item.content_kind == ContentKind::Issue;
7240            item.status = moving.landed;
7241            item.option = Some(moving.name);
7242        }
7243        if let Some((_, _, priority)) = priority_move {
7244            item.priority = HeldPriority::Read(priority);
7245        }
7246        if let Some((_, _, projected)) = projection_move {
7247            item.projected = projected;
7248        }
7249        let task = item.task()?;
7250        let mut written = update.changed(&before, &task);
7251        if blocked_by_moved || recorded_moves {
7252            written.insert(UpdatedField::DependsOn);
7253        }
7254        self.remember_written(item, false)?;
7255        Ok(Some(TaskUpdateOutcome {
7256            task,
7257            written,
7258            delivers_before: before.delivers,
7259        }))
7260    }
7261
7262    /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
7263    /// together, and nothing else; see [`TaskSource::set_task_rendering`].
7264    ///
7265    /// One update of the body: the content outside the slot, and inside it that one entry,
7266    /// every other entry kept as it was. This source keeps no template answers — an issue has
7267    /// no room beside itself that is not its body, and answers written there would duplicate
7268    /// what the content already says and count against GitHub's body limit — so `answers`
7269    /// reaches nothing here. A body that would not change is not sent at all.
7270    async fn replace_rendering(
7271        &self,
7272        id: &NativeId,
7273        kind: BoardKind,
7274        content: &str,
7275        provenance: &Value,
7276        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
7277    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
7278        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7279            return Ok(None);
7280        };
7281        let held = item.raw_body.clone().unwrap_or_default();
7282        let mut slot = item.slot.clone();
7283        slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
7284        let rewritten;
7285        let content = if let Some(assets) = assets {
7286            let uploads = self
7287                .upload_assets(item.own_repository.as_ref(), assets)
7288                .await?;
7289            rewritten =
7290                onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
7291            rewritten.as_str()
7292        } else {
7293            content
7294        };
7295        let body = with_slot(&with_content(&held, content)?, &slot)?;
7296        // Checked before anything is sent, as a content write checks it.
7297        let (visible, read) = metadata_body(Some(body.clone()))?;
7298        if visible.as_deref().unwrap_or_default() != content || read != slot {
7299            return Err(SourceError::Refused {
7300                message: format!(
7301                    "this content ends in what source {} reads as its own metadata slot \
7302                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7303                     as content; next: remove that trailing block from the template",
7304                    self.name
7305                ),
7306            });
7307        }
7308        if body != held {
7309            self.update_content(item.content_kind, &item.id, json!({"body": body}))
7310                .await?;
7311        }
7312        item.body = visible.filter(|value| !value.is_empty());
7313        item.raw_body = Some(body);
7314        item.slot = read;
7315        self.remember_written(item, false)?;
7316        Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
7317            id: id.clone(),
7318            content: Some(content.to_owned()),
7319        }))
7320    }
7321
7322    async fn set_item_field(
7323        &self,
7324        board_id: &str,
7325        item_id: &str,
7326        field_id: &str,
7327        value: Value,
7328    ) -> Result<(), SourceError> {
7329        let data = self
7330            .graphql(
7331                graphql::UPDATE_FIELD,
7332                json!({"input":{
7333                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
7334                },"readPriority":false,"priorityName":PRIORITY_FIELD}),
7335            )
7336            .await?;
7337        let returned = data
7338            .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
7339            .ok_or_else(|| SourceError::Malformed {
7340                message: "GitHub field update returned no project item".into(),
7341            })?;
7342        if required_str(returned, "id")? != item_id {
7343            return Err(SourceError::Malformed {
7344                message: "GitHub field update returned the wrong project item".into(),
7345            });
7346        }
7347        Ok(())
7348    }
7349
7350    /// GitHub accepts one value per field mutation; aliases combine those mutations in
7351    /// one request — writes in the order given, then clears. A lone write is
7352    /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
7353    /// request's slots hold go in further requests of the same document, in order. Every
7354    /// returned item id is checked, including optional aliases.
7355    async fn set_item_fields(
7356        &self,
7357        board: &str,
7358        item: &str,
7359        fields: &[(String, Value)],
7360        clears: &[&str],
7361    ) -> Result<(), SourceError> {
7362        match (fields, clears) {
7363            ([], []) => return Ok(()),
7364            ([(field, value)], []) => {
7365                return self.set_item_field(board, item, field, value.clone()).await;
7366            }
7367            ([], [field]) => {
7368                return self
7369                    .write_priority(
7370                        board,
7371                        item,
7372                        &PriorityWrite::Clear {
7373                            field: (*field).to_owned(),
7374                        },
7375                    )
7376                    .await;
7377            }
7378            _ => {}
7379        }
7380        // Any field of the request stands in for an input a slot leaves out, which GitHub
7381        // still requires to be well formed although it never runs it.
7382        let filler = fields
7383            .first()
7384            .map_or_else(|| clears[0], |(field, _)| field.as_str());
7385        let mut writes = fields.iter();
7386        let mut cleared = clears.iter();
7387        loop {
7388            let batch: Vec<&(String, Value)> =
7389                writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
7390            let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
7391            if batch.is_empty() && batch_clears.is_empty() {
7392                return Ok(());
7393            }
7394            let mut variables = serde_json::Map::new();
7395            for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
7396                let input = match batch.get(index) {
7397                    Some((field, value)) => {
7398                        json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
7399                    }
7400                    None => json!({"projectId":board,"itemId":item,"fieldId":filler,
7401                                   "value":{"text":""}}),
7402                };
7403                variables.insert(slot.variable.to_owned(), input);
7404                variables.insert(slot.include.to_owned(), json!(index < batch.len()));
7405            }
7406            for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
7407                let field = batch_clears.get(index).map_or(filler, |field| **field);
7408                variables.insert(
7409                    slot.variable.to_owned(),
7410                    json!({"projectId":board,"itemId":item,"fieldId":field}),
7411                );
7412                variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
7413            }
7414            let data = self
7415                .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
7416                .await?;
7417            let sent = FIELD_WRITE_SLOTS[..batch.len()]
7418                .iter()
7419                .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
7420            for slot in sent {
7421                let alias = slot.alias;
7422                let returned = data
7423                    .get(alias)
7424                    .and_then(|value| value.get("projectV2Item"))
7425                    .ok_or_else(|| SourceError::Malformed {
7426                        message: format!("GitHub field update {alias} returned no project item"),
7427                    })?;
7428                if required_str(returned, "id")? != item {
7429                    return Err(SourceError::Malformed {
7430                        message: format!(
7431                            "GitHub field update {alias} returned the wrong project item"
7432                        ),
7433                    });
7434                }
7435            }
7436        }
7437    }
7438
7439    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
7440        let mut after: Option<String> = None;
7441        let mut ids = Vec::new();
7442        loop {
7443            let data = self
7444                .graphql(
7445                    graphql::ISSUE_DEPENDENCIES,
7446                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
7447                )
7448                .await?;
7449            let connection =
7450                data.pointer("/node/blockedBy")
7451                    .ok_or_else(|| SourceError::Malformed {
7452                        message: "GitHub dependency response has no blockedBy connection".into(),
7453                    })?;
7454            ids.extend(
7455                connection
7456                    .get("nodes")
7457                    .and_then(Value::as_array)
7458                    .ok_or_else(|| SourceError::Malformed {
7459                        message: "GitHub dependency response nodes is not an array".into(),
7460                    })?
7461                    .iter()
7462                    .map(|value| required_str(value, "id").map(str::to_owned))
7463                    .collect::<Result<Vec<_>, _>>()?,
7464            );
7465            let next = next_cursor(connection)?;
7466            if let Some(next) = &next {
7467                validate_cursor_progress(after.as_deref(), &next.0)?;
7468            }
7469            after = next.map(|cursor| cursor.0);
7470            if after.is_none() {
7471                return Ok(ids);
7472            }
7473        }
7474    }
7475
7476    async fn dependencies(
7477        &self,
7478        id: &NativeId,
7479        near_kind: ItemKind,
7480        direction: Direction,
7481        page: &PageRequest,
7482    ) -> Result<Page<DependencyEdge>, SourceError> {
7483        validate_page(page)?;
7484        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
7485        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
7486        let recorded = recorded_offset(cursor, direction)?;
7487        // What this issue is blocked by, when a read of it by its own id in this command
7488        // already carried the whole connection — a copy reads the item it writes before it
7489        // reads its edges — and the page asked for is the whole of it, or the recorded tail
7490        // after it. Answered from that read, in the shape the dependency read answers in;
7491        // anything else is asked of GitHub.
7492        let carried = match direction {
7493            Direction::DependsOn => self
7494                .resolved_cache()?
7495                .get(id)
7496                .filter(|item| item.content_kind == ContentKind::Issue)
7497                .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
7498            Direction::DependedOnBy => None,
7499        }
7500        .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
7501        // Asked for even in the recorded phase, whose page reads nothing from the
7502        // connection: `__typename` is what says whether this item has a native
7503        // relationship at all, and that is what decides which far ends the reserved key is
7504        // allowed to hold.
7505        let data = match carried {
7506            Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
7507                "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
7508            None => {
7509                self.graphql(
7510                    graphql::ISSUE_DEPENDENCIES,
7511                    json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
7512                           "after":if recorded.is_some() {None} else {cursor}}),
7513                )
7514                .await?
7515            }
7516        };
7517        let node =
7518            data.get("node")
7519                .filter(|v| !v.is_null())
7520                .ok_or_else(|| SourceError::Refused {
7521                    message: format!(
7522                        "GitHub item {} was not found or does not support dependencies",
7523                        id.0
7524                    ),
7525                })?;
7526        let connection_name = match direction {
7527            Direction::DependsOn => "blockedBy",
7528            Direction::DependedOnBy => "blocking",
7529        };
7530        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
7531        // named natively and the reserved key may hold any far end. An issue's connections
7532        // hold issues, and this source reads them at the near item's own level.
7533        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
7534        if let Some(offset) = recorded {
7535            return Ok(recorded_page(
7536                self.recorded_edges(id, near_kind, direction, natively_names, node)
7537                    .await?,
7538                offset,
7539                limit,
7540            ));
7541        }
7542        if natively_names.is_none() {
7543            return Ok(recorded_page(
7544                self.recorded_edges(id, near_kind, direction, natively_names, node)
7545                    .await?,
7546                0,
7547                limit,
7548            ));
7549        }
7550        let connection = node
7551            .get(connection_name)
7552            .ok_or_else(|| SourceError::Malformed {
7553                message: "GitHub dependency response is missing its connection".into(),
7554            })?;
7555        let nodes = connection
7556            .get("nodes")
7557            .and_then(Value::as_array)
7558            .ok_or_else(|| SourceError::Malformed {
7559                message: "GitHub dependency response nodes is not an array".into(),
7560            })?;
7561        // `from` depends on `to`, always. GitHub spells the same relationship from either
7562        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
7563        // it — so the near item is `from` in one direction and `to` in the other.
7564        let items = nodes
7565            .iter()
7566            .map(|value| {
7567                let related = NativeId(required_str(value, "id")?.into());
7568                let related_kind = related_kind(value)?;
7569                let (from, to) = match direction {
7570                    Direction::DependsOn => (
7571                        DependencyEndpoint::from_native(id.clone(), near_kind),
7572                        DependencyEndpoint::from_native(related, related_kind),
7573                    ),
7574                    Direction::DependedOnBy => (
7575                        DependencyEndpoint::from_native(related, related_kind),
7576                        DependencyEndpoint::from_native(id.clone(), near_kind),
7577                    ),
7578                };
7579                Ok(DependencyEdge {
7580                    from,
7581                    to,
7582                    kind: DependencyKind::Blocks,
7583                })
7584            })
7585            .collect::<Result<Vec<_>, SourceError>>()?;
7586        let mut next = next_cursor(connection)?;
7587        if let Some(next) = &next {
7588            validate_cursor_progress(cursor, &next.0)?;
7589        }
7590        if next.is_none()
7591            && !self
7592                .recorded_edges(id, near_kind, direction, natively_names, node)
7593                .await?
7594                .is_empty()
7595        {
7596            next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
7597        }
7598        Ok(Page { items, next })
7599    }
7600
7601    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
7602    /// a far end in another source has to live: no GitHub issue relationship can name one.
7603    ///
7604    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
7605    /// source never writes one down.
7606    ///
7607    /// The metadata lives in the item's own body slot, and `node` is the dependency read's
7608    /// own answer, which carries an issue's body — so an issue's recorded edges cost no
7609    /// request beyond the read already made, and reading the board for them would be a
7610    /// walk of every item for one field of one. A draft has no body in that answer, because
7611    /// a draft is not an issue, so a draft's are read off its own read by id — never off a
7612    /// listing of the board, which can be behind on the very item asked about.
7613    async fn recorded_edges(
7614        &self,
7615        id: &NativeId,
7616        near_kind: ItemKind,
7617        direction: Direction,
7618        natively_names: Option<ItemKind>,
7619        node: &Value,
7620    ) -> Result<Vec<DependencyEdge>, SourceError> {
7621        if direction != Direction::DependsOn {
7622            return Ok(Vec::new());
7623        }
7624        let slot = match node.get("body") {
7625            Some(body) if natively_names.is_some() => {
7626                metadata_body(body.as_str().map(str::to_owned))?.1
7627            }
7628            _ => {
7629                let Some(item) = self.bound_item(id).await? else {
7630                    return Ok(Vec::new());
7631                };
7632                item.slot
7633            }
7634        };
7635        DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
7636            .map_err(|message| SourceError::Malformed { message })
7637    }
7638
7639    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
7640        self.repository
7641            .as_ref()
7642            .ok_or_else(|| SourceError::Refused {
7643                message: format!(
7644                    "source {} has no repository configured, and a GitHub Projects board has no \
7645                 repository of its own to create an issue in; set repository: owner/name on \
7646                 this source",
7647                    self.name
7648                ),
7649            })
7650    }
7651
7652    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
7653    /// states.
7654    ///
7655    /// The fallback is demanded first, whichever arm answers: a write without a configured
7656    /// repository is refused naming the field exactly as it was before the rule existed,
7657    /// so a source that could not write before cannot write now, rather than writing for
7658    /// the one item whose own field happens to decide it.
7659    ///
7660    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
7661    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
7662    /// entry owned by someone other than the owner of the parent issue's repository —
7663    /// GitHub accepts a sub-issue from another repository of the same owner and from no
7664    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
7665    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
7666    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
7667    /// and is visible to the token is checked where its node id is resolved, still before
7668    /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
7669    /// looked up in a listing of the board, which can be minutes behind an issue its own
7670    /// `projectItems` already places on it — and that read answers first from this process's
7671    /// own record, so a project created moments ago in this command answers though GitHub
7672    /// has not caught up.
7673    async fn creation_target(
7674        &self,
7675        incoming: &Incoming<'_>,
7676    ) -> Result<RepositoryTarget, SourceError> {
7677        let fallback = self.configured_repository()?;
7678        let what = |incoming: &Incoming<'_>| {
7679            format!(
7680                "{} {:?}",
7681                incoming.written.kind().describes(),
7682                incoming.title
7683            )
7684        };
7685        let parent = match incoming.parent {
7686            Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
7687                SourceError::Refused {
7688                    message: format!(
7689                        "GitHub project issue {} was not found on the board of source {}, so {} \
7690                         cannot be filed under it",
7691                        parent.0,
7692                        self.name,
7693                        what(incoming)
7694                    ),
7695                }
7696            })?),
7697            None => None,
7698        };
7699        let parents_repository = parent
7700            .as_ref()
7701            .map(|parent| {
7702                // A draft is on the board and so is found, but it has no repository to
7703                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
7704                // would refuse the task only once `createIssue` had made it.
7705                if parent.content_kind == ContentKind::DraftIssue {
7706                    return Err(SourceError::Refused {
7707                        message: format!(
7708                            "GitHub project item {} on the board of source {} is a draft, \
7709                             which cannot have sub-issues, so {} cannot be filed under it",
7710                            parent.id.0,
7711                            self.name,
7712                            what(incoming)
7713                        ),
7714                    });
7715                }
7716                // An issue's repository is where a sub-issue is placed and whose owner it
7717                // is compared against, so a parent whose repository this source cannot
7718                // spell as `owner/name` — GitHub's login grammar is wider than this
7719                // source's floor — is one nothing can be filed under.
7720                parent
7721                    .own_repository
7722                    .as_ref()
7723                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
7724                    .ok_or_else(|| SourceError::Malformed {
7725                        message: format!(
7726                            "GitHub project issue {} on the board of source {} is in {}, which \
7727                             is not a {}/owner/name repository this source can place {} in",
7728                            parent.id.0,
7729                            self.name,
7730                            parent
7731                                .own_repository
7732                                .as_ref()
7733                                .map_or("no repository", Repository::as_str),
7734                            RepositoryTarget::HOST,
7735                            what(incoming)
7736                        ),
7737                    })
7738            })
7739            .transpose()?;
7740        match incoming.repositories {
7741            [named] => {
7742                let target =
7743                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
7744                        message: format!(
7745                            "{} names repository {}, which is not a {}/owner/name repository \
7746                             source {} can create an issue in; name one that is, or name none",
7747                            what(incoming),
7748                            named.as_str(),
7749                            RepositoryTarget::HOST,
7750                            self.name
7751                        ),
7752                    })?;
7753                if let Some(parents) = &parents_repository
7754                    && parents.owner != target.owner
7755                {
7756                    return Err(SourceError::Refused {
7757                        message: format!(
7758                            "{} names repository {}, owned by {}, but its project's issue is in \
7759                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
7760                             of the same owner as its parent issue; name a repository of {}, or \
7761                             name none",
7762                            what(incoming),
7763                            target.slug(),
7764                            target.owner,
7765                            parents.slug(),
7766                            parents.owner,
7767                            parents.owner
7768                        ),
7769                    });
7770                }
7771                Ok(target)
7772            }
7773            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
7774        }
7775    }
7776
7777    /// The node id of the repository `incoming` is being created in, or the refusal naming
7778    /// the item and the repository the token cannot see.
7779    ///
7780    /// Resolved once per command per repository; see [`Self::repository_cache`].
7781    async fn repository_id(
7782        &self,
7783        repository: &RepositoryTarget,
7784        incoming: &Incoming<'_>,
7785    ) -> Result<String, SourceError> {
7786        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
7787            return Ok(id);
7788        }
7789        let data = self
7790            .graphql(
7791                graphql::REPOSITORY,
7792                json!({"owner":repository.owner,"name":repository.name}),
7793            )
7794            .await?;
7795        self.repository_read(&data, repository, incoming)
7796    }
7797
7798    /// The repository's node id out of an answer carrying the `repository` root, held for
7799    /// the rest of this command, or the refusal naming the item that cannot be created in it.
7800    fn repository_read(
7801        &self,
7802        data: &Value,
7803        repository: &RepositoryTarget,
7804        incoming: &Incoming<'_>,
7805    ) -> Result<String, SourceError> {
7806        let node = data
7807            .get("repository")
7808            .filter(|value| !value.is_null())
7809            .ok_or_else(|| SourceError::Refused {
7810                message: format!(
7811                    "GitHub repository {} was not found or is not visible to the token, so {} \
7812                     {:?} cannot be created in it",
7813                    repository.slug(),
7814                    incoming.written.kind().describes(),
7815                    incoming.title
7816                ),
7817            })?;
7818        let id = required_str(node, "id")?.to_owned();
7819        self.repository_cache()?
7820            .insert(repository.clone(), id.clone());
7821        Ok(id)
7822    }
7823
7824    fn repository_cache(
7825        &self,
7826    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
7827        self.repository_cache
7828            .lock()
7829            .map_err(|_| SourceError::Unavailable {
7830                message: "this source's record of the destination repository was left \
7831                          inconsistent by an earlier failure; next: run the command again"
7832                    .into(),
7833            })
7834    }
7835
7836    /// Create or update one board item, whichever kind it is.
7837    async fn write_item(
7838        &self,
7839        incoming: &Incoming<'_>,
7840        target: Option<&NativeId>,
7841        depends_on: &[DependencyEdge],
7842    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
7843        // Refused before anything is read or written: a task or a project titled the way
7844        // this board spells a document would land as an issue this same source reads back
7845        // as a document, so the field this destination cannot carry is named rather than
7846        // written and silently reclassified.
7847        if let Written::Work(kind, _) = incoming.written
7848            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
7849        {
7850            return Err(SourceError::Refused {
7851                message: format!(
7852                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
7853                     spells a document, so it would read back as one rather than as a {}; \
7854                     retitle it, or copy it as a document",
7855                    kind.marker(),
7856                    self.name,
7857                    kind.marker()
7858                ),
7859            });
7860        }
7861        // The destination is read by its own id, and whether this board holds it is decided
7862        // by that read — its own `projectItems` — rather than by whether a listing of the
7863        // board happens to include it yet. See the module documentation.
7864        let existing = match target {
7865            Some(target) => {
7866                Some(
7867                    self.bound_item(target)
7868                        .await?
7869                        .ok_or_else(|| SourceError::Refused {
7870                            message: format!("GitHub destination item {} was not found", target.0),
7871                        })?,
7872                )
7873            }
7874            None => None,
7875        };
7876        let existing = existing.as_ref();
7877        // An existing issue is never moved; a new one is created where the rule says — and
7878        // knowing where is what lets the board's fields and that repository's id be read
7879        // together, before anything below needs either.
7880        let creation_target = match existing {
7881            Some(_) => None,
7882            None => {
7883                let target = self.creation_target(incoming).await?;
7884                self.creation_context(&target, incoming).await?;
7885                Some(target)
7886            }
7887        };
7888        let board = self
7889            .fields_for(
7890                existing,
7891                incoming.written.status().is_some(),
7892                incoming
7893                    .priority
7894                    .is_some_and(|priority| priority != Priority::None),
7895            )
7896            .await?;
7897        let status_target = incoming
7898            .written
7899            .work_status()
7900            .map(|(kind, status)| self.resolved_target(kind, status.category))
7901            .transpose()?;
7902        let column = match (incoming.written.work_status(), status_target.as_ref()) {
7903            (Some((kind, status)), Some(target)) => {
7904                self.column_for(&board.fields, kind, status.category, target)?
7905            }
7906            _ => None,
7907        };
7908        // Resolved before anything is created, for the reason the column above is: a
7909        // priority this board has no option for is refused while nothing has been written.
7910        let priority_write = match incoming.priority {
7911            Some(priority) => self.priority_write(&board.fields, existing, priority)?,
7912            None => None,
7913        };
7914        // And for the reason the priority is: a value no projected field can hold, or a board
7915        // that cannot hold the field, is refused while nothing has been written.
7916        let (projections, projected) = self.projection_writes(
7917            &board.fields,
7918            existing.map(|item| &item.projected),
7919            incoming.metadata,
7920            None,
7921        )?;
7922        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
7923        if content_kind == ContentKind::DraftIssue {
7924            if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
7925                (status_target.as_ref(), incoming.written.status())
7926            {
7927                return Err(self.closes_a_draft(status.category));
7928            }
7929            if incoming.parent.is_some() {
7930                return Err(SourceError::Refused {
7931                    message: "GitHub draft items cannot be a project's sub-issue".into(),
7932                });
7933            }
7934        }
7935        match existing {
7936            Some(item) if content_kind == ContentKind::Issue => {
7937                if item.labels != incoming.labels {
7938                    return Err(SourceError::Refused {
7939                        message: "GitHub issue labels differ from the labels being written".into(),
7940                    });
7941                }
7942            }
7943            _ => {
7944                if !incoming.labels.is_empty() {
7945                    return Err(SourceError::Refused {
7946                        message: "GitHub items created by this destination carry no labels".into(),
7947                    });
7948                }
7949            }
7950        }
7951
7952        // The repository the issue really lives in is what the slot below is written against,
7953        // so a single entry that is where the issue is created travels as no key at all, and
7954        // the read side derives it back from the issue.
7955        let own_repository = match (existing, &creation_target) {
7956            (Some(item), _) => item.own_repository.clone(),
7957            (None, Some(target)) => Some(
7958                Repository::try_from(target.origin())
7959                    .map_err(|message| SourceError::Config { message })?,
7960            ),
7961            (None, None) => None,
7962        };
7963        let (native, fallback) = self
7964            .partition_edges(
7965                incoming.written.kind(),
7966                content_kind,
7967                existing.and_then(|item| item.blocked_by.as_deref()),
7968                depends_on,
7969            )
7970            .await?;
7971        let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
7972        let content = match incoming.assets {
7973            Some(assets) => {
7974                let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
7975                let rewritten = onetaskgraph_plugin_api::serve_asset_references(
7976                    incoming.content.unwrap_or_default(),
7977                    &mut slot,
7978                    &uploads,
7979                );
7980                incoming.content.map(|_| rewritten)
7981            }
7982            None => incoming.content.map(str::to_owned),
7983        };
7984        let body = compose_body(content.as_deref(), &slot)?;
7985        // Read before anything is created, for the reason the field below is: a value
7986        // this destination cannot store has to refuse, and refusing after `createIssue`
7987        // would leave an issue behind that nothing asked for. The engine writes a
7988        // qualified id here; a caller handing this key anything else is told so rather
7989        // than having it silently stored as no origin at all.
7990        // 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.
7991        let origin = match incoming.metadata.get(ORIGIN_KEY) {
7992            None => "",
7993            Some(Value::String(origin)) => origin.as_str(),
7994            Some(other) => {
7995                return Err(SourceError::Refused {
7996                    message: format!(
7997                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
7998                         is {other}"
7999                    ),
8000                });
8001            }
8002        };
8003        // Resolved before anything is created: a board that cannot carry the copy origin
8004        // has to refuse the write, and refusing it after `createIssue` would leave an
8005        // issue behind that nothing asked for.
8006        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
8007            Some(field) => {
8008                if required_str(field, "__typename")? != "ProjectV2Field" {
8009                    return Err(SourceError::Refused {
8010                        message: format!(
8011                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
8012                        ),
8013                    });
8014                }
8015                Some(required_str(field, "id")?.to_owned())
8016            }
8017            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
8018                return Err(SourceError::Refused {
8019                    message: format!(
8020                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
8021                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
8022                         the board"
8023                    ),
8024                });
8025            }
8026            None => None,
8027        };
8028
8029        let Landed {
8030            content_id,
8031            item_id,
8032            url,
8033            number,
8034        } = match existing {
8035            // Its content is written last, below, once everything else has landed.
8036            Some(item) => Landed {
8037                content_id: item.id.clone(),
8038                item_id: item.item_id.clone(),
8039                url: item.url.clone(),
8040                number: item.number,
8041            },
8042            None => {
8043                let target = creation_target
8044                    .as_ref()
8045                    .ok_or_else(|| SourceError::Malformed {
8046                        message: "a new item was decided without a repository to create it in"
8047                            .into(),
8048                    })?;
8049                self.create_and_file_issue(board.id.as_str(), target, incoming, &body)
8050                    .await?
8051            }
8052        };
8053
8054        let written_option = column.as_ref().map(|(_, _, name)| name.clone());
8055        let column = column
8056            .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
8057            .map(|(field, option, _)| (field, option));
8058        // Creating an item here is several calls — `createIssue`, which files it on the
8059        // board, then its board fields, the parent and the dependencies — and GitHub can fail
8060        // at any of them. Everything this source can refuse *before* the first of those is
8061        // already checked above, so what is left is GitHub itself failing part way. When it
8062        // does over an item this call created, the issue is taken back: a write that
8063        // refused must not leave an item behind that nobody asked for, and one that does
8064        // makes the retry create a second.
8065        // Whether the board-field write carrying a moved origin was answered as landing whole.
8066        // When it was refused, GitHub does not say which of its fields ran before the one that
8067        // failed, so the origin may or may not have moved.
8068        let mut origin_landed = false;
8069        let landed = self
8070            .finish_write(
8071                board.id.as_str(),
8072                incoming,
8073                &content_id,
8074                &item_id,
8075                content_kind,
8076                existing,
8077                origin_field.as_deref(),
8078                origin,
8079                column,
8080                status_target.as_ref(),
8081                priority_write.as_ref(),
8082                &projections,
8083                &native,
8084                &mut origin_landed,
8085            )
8086            .await;
8087        // An existing item's title, body and state go last, in one `updateIssue`, once its board
8088        // fields and its relationships have landed: a refusal of any of those then leaves its
8089        // body — and the metadata slot inside it — exactly as it stood.
8090        let landed = match (landed, existing) {
8091            (Ok(()), Some(item)) => {
8092                self.update_existing(item, incoming, &body, status_target.as_ref())
8093                    .await
8094            }
8095            (landed, _) => landed,
8096        };
8097        if let Err(error) = landed {
8098            match existing {
8099                // Best effort, and the write's own failure is what the caller is told: a
8100                // refusal naming the tidy-up would hide why the write failed at all.
8101                None => {
8102                    let _ = self.delete_issue(&content_id).await;
8103                }
8104                // The origin field is the one piece of an existing item's metadata written
8105                // before its body, so a write refused after it puts it back as it was. When
8106                // that is refused too, the write's own failure is still what the caller is
8107                // told — with what it left behind added, because the item's metadata is then
8108                // not as it stood and a caller retrying has to know which key moved.
8109                Some(item) => {
8110                    let before = item.origin.as_deref().unwrap_or("");
8111                    if let Some(field) = origin_field.as_deref()
8112                        && before != origin
8113                        && let Err(restore) = self
8114                            .set_item_field(
8115                                board.id.as_str(),
8116                                &item.item_id,
8117                                field,
8118                                json!({"text": before}),
8119                            )
8120                            .await
8121                    {
8122                        let left = if origin_landed {
8123                            format!(
8124                                "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
8125                                 not be put back to {before:?} ({restore}), so item {} still \
8126                                 holds {origin:?} there",
8127                                item.id.0
8128                            )
8129                        } else {
8130                            format!(
8131                                "the refused write carried its {ORIGIN_KEY} from {before:?} to \
8132                                 {origin:?}, GitHub does not say whether that part of it ran, \
8133                                 and putting it back to {before:?} was refused ({restore}), so \
8134                                 item {} holds {origin:?} or {before:?} there",
8135                                item.id.0
8136                            )
8137                        };
8138                        return Err(noting(
8139                            error,
8140                            &format!(
8141                                "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
8142                                 run the write again"
8143                            ),
8144                        ));
8145                    }
8146                }
8147            }
8148            return Err(error);
8149        }
8150
8151        let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
8152            (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
8153                self.statuses
8154                    .status(kind, written_option.as_deref(), true, Some(reason.reason()))
8155            }
8156            (Some((kind, _)), Some(StatusTarget::Column(_))) => {
8157                self.statuses
8158                    .status(kind, written_option.as_deref(), false, None)
8159            }
8160            (Some((_, status)), _) => status.clone(),
8161            (None, _) => Status {
8162                category: StatusCategory::Unknown,
8163                name: "Open".to_owned(),
8164            },
8165        };
8166
8167        // So the rest of this command reads what it just did rather than what the board
8168        // said before it. See `remember_written` for which half takes it.
8169        let remembered = Resolved {
8170            item_id,
8171            id: content_id.clone(),
8172            content_kind,
8173            kind: incoming.written.kind(),
8174            title: incoming.title.to_owned(),
8175            // The visible half of the body this write composed, split back off it the
8176            // way a read splits it — so what this record reports is what a read of the
8177            // same issue reports, rather than the person's text with the metadata slot
8178            // still on the end of it.
8179            body: metadata_body(body.clone())?.0,
8180            raw_body: body.clone(),
8181            // A document has no status of its own; what it reads back as is whatever
8182            // the issue's own state says, which is what a re-read reports.
8183            status: written_status,
8184            option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
8185            priority: match incoming.priority {
8186                Some(priority) => HeldPriority::Read(priority),
8187                None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
8188                    item.priority.clone()
8189                }),
8190            },
8191            // What `state_input` asked for: closed for a terminal target, open for any other
8192            // status, and the issue's own state left as it was by a document write.
8193            closed: content_kind == ContentKind::Issue
8194                && match status_target.as_ref() {
8195                    Some(StatusTarget::Terminal(_, _)) => true,
8196                    Some(_) => false,
8197                    None => existing.is_some_and(|item| item.closed),
8198                },
8199            delivers: incoming.delivers.to_vec(),
8200            delivered_by: incoming.delivered_by.to_vec(),
8201            labels: incoming.labels.to_vec(),
8202            parent: incoming.parent.cloned(),
8203            origin: (!origin.is_empty()).then(|| origin.to_owned()),
8204            projected,
8205            number,
8206            // In the update path this is the item's own url, read off `existing` where the
8207            // record above was bound, so one expression serves both halves.
8208            url,
8209            created_at: existing.and_then(|item| item.created_at),
8210            updated_at: existing.and_then(|item| item.updated_at),
8211            // What this write moved is GitHub's to say, so a record of it gives no token until
8212            // it is read again.
8213            item_updated_at: None,
8214            comment_count: None,
8215            relationship_events: None,
8216            served_at: None,
8217            own_repository,
8218            repositories: incoming.repositories.to_vec(),
8219            classification: incoming.classification,
8220            slot,
8221            board_id: Some(board.id.as_str().to_owned()),
8222            fields: board
8223                .fields
8224                .get("nodes")
8225                .and_then(Value::as_array)
8226                .cloned()
8227                .unwrap_or_default(),
8228            board_fields: Some(board.fields.clone()),
8229            // What this write left the relationship holding is known by id alone, and a
8230            // later read of its edges needs each far end's kind, so it reads them again.
8231            blocked_by: None,
8232        };
8233        self.remember_written(remembered, existing.is_none())?;
8234        Ok(onetaskgraph_plugin_api::AssetsWritten {
8235            id: content_id,
8236            content,
8237        })
8238    }
8239
8240    /// Everything a write does after the item exists: its board fields, its parent, and
8241    /// its dependencies.
8242    ///
8243    /// Split out of `write_item` so there is one place a failure past the point of no
8244    /// return is caught, rather than a tidy-up repeated at each `?` above.
8245    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
8246    // so there is one place a failure past the point of no return is caught, and its
8247    // arguments are exactly the values that tail already had in scope. Bundling them into a
8248    // struct would describe no concept — it would be "the arguments of this function" — and
8249    // would put the whole of `write_item`'s locals behind one more indirection.
8250    #[allow(clippy::too_many_arguments)]
8251    async fn finish_write(
8252        &self,
8253        board_id: &str,
8254        incoming: &Incoming<'_>,
8255        content_id: &NativeId,
8256        item_id: &str,
8257        content_kind: ContentKind,
8258        existing: Option<&Resolved>,
8259        origin_field: Option<&str>,
8260        origin: &str,
8261        column: Option<(String, String)>,
8262        status_target: Option<&StatusTarget>,
8263        priority: Option<&PriorityWrite>,
8264        projections: &[ProjectionWrite],
8265        native: &[String],
8266        origin_landed: &mut bool,
8267    ) -> Result<(), SourceError> {
8268        let mut fields = Vec::new();
8269        if let Some(field_id) = origin_field
8270            && existing.map_or(!origin.is_empty(), |item| {
8271                item.origin.as_deref().unwrap_or("") != origin
8272            })
8273        {
8274            fields.push((field_id.to_owned(), json!({"text":origin})));
8275        }
8276        if let Some((field_id, option_id)) = column {
8277            fields.push((field_id, json!({"singleSelectOptionId":option_id})));
8278        }
8279        let mut clears = Vec::new();
8280        match priority {
8281            Some(PriorityWrite::Select { field, option }) => {
8282                fields.push((field.clone(), json!({"singleSelectOptionId":option})));
8283            }
8284            Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
8285            None => {}
8286        }
8287        for projection in projections {
8288            projection.join(&mut fields, &mut clears);
8289        }
8290        self.set_item_fields(board_id, item_id, &fields, &clears)
8291            .await?;
8292        *origin_landed = true;
8293
8294        // An existing issue closes in the `updateIssue` its write ends with; one created just
8295        // now closes here, once its option is selected.
8296        if existing.is_none()
8297            && content_kind == ContentKind::Issue
8298            && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
8299        {
8300            self.update_content(
8301                ContentKind::Issue,
8302                content_id,
8303                json!({"stateInput":state_input(status_target)}),
8304            )
8305            .await?;
8306        }
8307
8308        if content_kind == ContentKind::Issue {
8309            self.reparent(
8310                existing.and_then(|item| item.parent.clone()),
8311                content_id,
8312                incoming.parent,
8313            )
8314            .await?;
8315            // A document takes part in no dependency graph, so writing one neither reads
8316            // nor changes the issue's own `blockedBy` relationships. Reconciling them
8317            // against the empty list a document write carries would *delete* whatever
8318            // relationships a person had made on that issue, which is a write nobody
8319            // asked for.
8320            if incoming.written.kind() != BoardKind::Document {
8321                let issue = match existing {
8322                    Some(item) => Issue::Existing(item.blocked_by.as_deref()),
8323                    None => Issue::Created,
8324                };
8325                self.reconcile_blocked_by(content_id, native, issue).await?;
8326            }
8327        }
8328        Ok(())
8329    }
8330
8331    /// Delete one issue, which takes its board item with it.
8332    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
8333        let data = self
8334            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
8335            .await?;
8336        data.pointer("/deleteIssue/repository")
8337            .filter(|value| !value.is_null())
8338            .ok_or_else(|| SourceError::Malformed {
8339                message: "GitHub issue deletion returned no repository".into(),
8340            })?;
8341        self.forget(id)?;
8342        Ok(())
8343    }
8344
8345    /// Remove one item this copy created, so a copy that could not finish leaves the board
8346    /// as it found it.
8347    ///
8348    /// Deleting the issue takes its board item with it, so there is no second mutation to
8349    /// keep in step. An id the board does not hold is not an error: the item is already
8350    /// gone, which is the state this asks for. Which that is, is decided by reading the item
8351    /// by its own id — a listing of the board can still be missing an item it holds, and
8352    /// reading that as *already gone* would leave behind the very item this was asked to
8353    /// take back.
8354    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
8355        let Some(item) = self.bound_item(id).await? else {
8356            return Ok(());
8357        };
8358        if item.content_kind == ContentKind::DraftIssue {
8359            return Err(SourceError::Refused {
8360                message: format!(
8361                    "GitHub item {} is a draft, and this source removes an item by deleting \
8362                     its issue; next: remove it from the board by hand",
8363                    id.0
8364                ),
8365            });
8366        }
8367        let data = self
8368            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
8369            .await?;
8370        data.pointer("/deleteIssue/repository")
8371            .filter(|value| !value.is_null())
8372            .ok_or_else(|| SourceError::Malformed {
8373                message: "GitHub issue deletion returned no repository".into(),
8374            })?;
8375        self.forget(id)?;
8376        Ok(())
8377    }
8378
8379    /// The issue a comment call on `task` is about, or `None` when this board holds no such
8380    /// task.
8381    ///
8382    /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
8383    /// read of the task cannot disagree about which ids name one: a project or a document of
8384    /// this board is not a task here either.
8385    ///
8386    /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
8387    /// issues and a draft is not one. It is refused rather than answered with an empty page,
8388    /// which would read as a task nobody has commented on yet.
8389    async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
8390        let cached = self.resolved_cache()?.get(task).cloned();
8391        let Some(item) = (match cached {
8392            Some(item) => Some(item),
8393            None => self.item_by_id(task).await?,
8394        })
8395        .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
8396            return Ok(None);
8397        };
8398        if item.content_kind == ContentKind::DraftIssue {
8399            return Err(self.draft_has_no_comments(task));
8400        }
8401        Ok(Some(item.id))
8402    }
8403
8404    /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
8405    /// issues, and a draft is not one.
8406    fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
8407        SourceError::Refused {
8408            message: format!(
8409                "task {} of source {} is a draft item on the board, and GitHub keeps \
8410                 comments on issues alone, so a draft has none to read or write; next: \
8411                 convert the draft to an issue on the board, then comment on the issue it \
8412                 becomes",
8413                task.0, self.name
8414            ),
8415        }
8416    }
8417
8418    /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
8419    /// request — or `None` when this board holds no task by that id.
8420    ///
8421    /// What `task show` and a comment listing read. A draft is a task with no comments, so it
8422    /// is answered with the draft and the refusal, at the price of the draft's own read.
8423    async fn issue_detail(
8424        &self,
8425        id: &NativeId,
8426        page: &PageRequest,
8427    ) -> Result<Option<TaskDetailRead>, SourceError> {
8428        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
8429        let asked = self
8430            .graphql(
8431                graphql::ISSUE_DETAIL,
8432                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
8433                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
8434                       "duplicates":true}),
8435            )
8436            .await;
8437        let data = match asked {
8438            Ok(data) => data,
8439            Err(error) if unresolvable_node(&error) => return Ok(None),
8440            Err(error) => return Err(error),
8441        };
8442        // `node` is null for an id that names nothing, and absent only from an answer this
8443        // source cannot read — never the same thing.
8444        let node = data.get("node").ok_or_else(|| SourceError::Malformed {
8445            message: format!("GitHub answered the read of {} with no node", id.0),
8446        })?;
8447        self.detail_of(id, node, true, after).await
8448    }
8449
8450    /// Several tasks, each with the first page of its comments when `comments` is set, read
8451    /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
8452    /// order.
8453    ///
8454    /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
8455    /// one item at a time, so that id is answered as missing and the others as themselves; any
8456    /// other refusal is every id of that batch's answer.
8457    async fn issue_details(
8458        &self,
8459        ids: &[NativeId],
8460        comments: Option<&PageRequest>,
8461    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
8462        let mut read = Vec::with_capacity(ids.len());
8463        for batch in ids.chunks(DETAIL_BATCH) {
8464            match self
8465                .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
8466                .await
8467            {
8468                Ok(data) => {
8469                    for (slot, id) in batch.iter().enumerate() {
8470                        // Every alias asked for is answered, null for an id naming nothing;
8471                        // one missing is an answer this source cannot read.
8472                        let read_one = match data.get(format!("i{slot}")) {
8473                            Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
8474                            None => Err(SourceError::Malformed {
8475                                message: format!(
8476                                    "GitHub answered a batch read with no item for {}",
8477                                    id.0
8478                                ),
8479                            }),
8480                        };
8481                        read.push(read_one);
8482                    }
8483                }
8484                Err(error) if unresolvable_node(&error) => {
8485                    for id in batch {
8486                        read.push(match comments {
8487                            Some(page) => self.issue_detail(id, page).await,
8488                            None => self.task_read(id).await,
8489                        });
8490                    }
8491                }
8492                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
8493            }
8494        }
8495        read
8496    }
8497
8498    /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
8499    async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
8500        Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
8501            task,
8502            comments: None,
8503        }))
8504    }
8505
8506    /// What one node a detail read reached says: the task this board holds by `id`, with the
8507    /// page of comments the node carries when `commented` — or `None` for a node that is no
8508    /// task of this board.
8509    ///
8510    /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
8511    /// and an item this process created answers from this process's own record, which a node
8512    /// read taken moments after the write can still be behind.
8513    async fn detail_of(
8514        &self,
8515        id: &NativeId,
8516        node: &Value,
8517        commented: bool,
8518        after: Option<&str>,
8519    ) -> Result<Option<TaskDetailRead>, SourceError> {
8520        if node.is_null() {
8521            return Ok(None);
8522        }
8523        let draft = optional_str(node, "__typename")? == Some("DraftIssue");
8524        // An issue answered under one id is that id's, or the answer is not one this source
8525        // can report: reporting another issue's task and comments under the qualified id asked
8526        // for would be the one wrong answer here. A draft's own read checks the same.
8527        if !draft
8528            && optional_str(node, "__typename")? == Some("Issue")
8529            && required_str(node, "id")? != id.0
8530        {
8531            return Err(SourceError::Malformed {
8532                message: format!(
8533                    "GitHub answered the read of {} with issue {}",
8534                    id.0,
8535                    required_str(node, "id")?
8536                ),
8537            });
8538        }
8539        let item = if draft {
8540            self.draft_by_id(id).await?
8541        } else {
8542            self.resolve_issue(node).await?
8543        };
8544        // A read that carried the whole of what the issue is blocked by — a copy's — is kept
8545        // for this command, so the dependency read the copy makes next is answered from it.
8546        if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
8547            self.resolved_cache()?.insert(read.id.clone(), read.clone());
8548        }
8549        let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
8550            return Ok(None);
8551        };
8552        let own = self.created()?.iter().find(|own| own.id == *id).cloned();
8553        let task = own.unwrap_or(item).task()?;
8554        let comments = match (commented, draft) {
8555            (false, _) => None,
8556            (true, true) => Some(Err(self.draft_has_no_comments(id))),
8557            (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
8558        };
8559        Ok(Some(TaskDetailRead { task, comments }))
8560    }
8561
8562    /// Whether the comment `comment` is one of `issue`'s own.
8563    ///
8564    /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
8565    /// comment's id and nothing else: a comment id given against the wrong task would
8566    /// otherwise change a comment on some other issue entirely. An id that names nothing, or
8567    /// names something that is not an issue comment, is a comment this task does not have —
8568    /// which is what GitHub refusing to resolve it means too.
8569    async fn comment_is_on(
8570        &self,
8571        issue: &NativeId,
8572        comment: &NativeId,
8573    ) -> Result<bool, SourceError> {
8574        Ok(self.comment_on(issue, comment).await?.is_some())
8575    }
8576
8577    /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
8578    /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
8579    async fn comment_on(
8580        &self,
8581        issue: &NativeId,
8582        comment: &NativeId,
8583    ) -> Result<Option<String>, SourceError> {
8584        let asked = self
8585            .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
8586            .await;
8587        let data = match asked {
8588            Ok(data) => data,
8589            Err(error) if unresolvable_node(&error) => return Ok(None),
8590            Err(error) => return Err(error),
8591        };
8592        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
8593            return Ok(None);
8594        };
8595        if optional_str(node, "__typename")? != Some("IssueComment") {
8596            return Ok(None);
8597        }
8598        let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
8599            message: format!("GitHub issue comment {} names no issue", comment.0),
8600        })?;
8601        if required_str(on, "id")? != issue.0 {
8602            return Ok(None);
8603        }
8604        Ok(Some(required_str(node, "body")?.to_owned()))
8605    }
8606
8607    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
8608    async fn partition_edges(
8609        &self,
8610        near_kind: BoardKind,
8611        near_content: ContentKind,
8612        carried: Option<&[Value]>,
8613        depends_on: &[DependencyEdge],
8614    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
8615        let mut native = Vec::new();
8616        let mut fallback = Vec::new();
8617        let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
8618            .iter()
8619            .map(|edge| {
8620                let same_source = edge
8621                    .to
8622                    .source()
8623                    .is_none_or(|source| source == self.name.as_str());
8624                // A qualified id's source segment runs to its *first* colon — `GlobalId` and
8625                // `DependencyEndpoint::source` both read it that way — and a native id may hold
8626                // colons of its own, so the far end is everything after that one separator.
8627                // Splitting at the last would truncate `work:urn:task:7` to `7`.
8628                let far_id = if edge.to.is_qualified() {
8629                    edge.to
8630                        .id()
8631                        .split_once(':')
8632                        .map_or(edge.to.id(), |(_, native)| native)
8633                } else {
8634                    edge.to.id()
8635                };
8636                // One that already blocks the near issue was answered by that issue's own
8637                // read, which carried each of its blockers' kinds — an issue every one — so it
8638                // is not read again.
8639                let blocking = carried.and_then(|nodes| {
8640                    nodes
8641                        .iter()
8642                        .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
8643                });
8644                (edge, far_id, same_source, blocking)
8645            })
8646            .collect();
8647        // Every other same-source far end is read by its own id, exactly as the item it is a
8648        // far end of is: whether this board holds it is that read's answer, never a listing's.
8649        // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
8650        let mut unread: Vec<NativeId> = Vec::new();
8651        for (_, far_id, same_source, blocking) in &far_ends {
8652            let id = NativeId((*far_id).to_owned());
8653            if *same_source && blocking.is_none() && !unread.contains(&id) {
8654                unread.push(id);
8655            }
8656        }
8657        let read: BTreeMap<NativeId, Option<Resolved>> = unread
8658            .iter()
8659            .cloned()
8660            .zip(self.items_by_ids(&unread).await?)
8661            .collect();
8662        for (edge, far_id, same_source, blocking) in far_ends {
8663            let far = match (same_source, blocking) {
8664                (false, _) => None,
8665                (true, Some(node)) => Some(FarEnd {
8666                    kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
8667                        BoardKind::Document
8668                    } else {
8669                        BoardKind::Work(related_kind(node)?)
8670                    },
8671                    content_kind: ContentKind::Issue,
8672                }),
8673                (true, None) => {
8674                    let read = read
8675                        .get(&NativeId(far_id.to_owned()))
8676                        .cloned()
8677                        .flatten()
8678                        .ok_or_else(|| SourceError::Refused {
8679                            message: format!("GitHub dependency item {far_id} was not found"),
8680                        })?;
8681                    Some(FarEnd {
8682                        kind: read.kind,
8683                        content_kind: read.content_kind,
8684                    })
8685                }
8686            };
8687            let far = far.as_ref();
8688            // The caller says which kind the far end is, and this board holds the far end
8689            // itself, so a disagreement is settled here rather than stored: recorded, the
8690            // wrong kind would read back as a cross-level edge that never existed; written
8691            // natively, it would name a relationship of a different level than the caller
8692            // asked for.
8693            //
8694            // A far end this board holds as a *document* fails the same comparison and is
8695            // refused by the same sentence: `ItemKind` has no document variant because
8696            // nothing may point at one, so no caller can name it correctly and the refusal
8697            // is the only honest answer.
8698            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
8699                return Err(SourceError::Refused {
8700                    message: format!(
8701                        "GitHub dependency item {far_id} is a {} of this board, and this item \
8702                         names it as a {}; record the kind it is",
8703                        disagreeing.kind.describes(),
8704                        edge.to.kind.marker()
8705                    ),
8706                });
8707            }
8708            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
8709            // however the far end is spelled — and one classified native here would be
8710            // written nowhere at all, because a draft's native reconciliation never runs.
8711            let native_here = near_content == ContentKind::Issue
8712                && far.is_some_and(|far| {
8713                    far.content_kind == ContentKind::Issue
8714                        && BoardKind::Work(edge.to.kind) == near_kind
8715                });
8716            if native_here {
8717                native.push(far_id.to_owned());
8718            } else {
8719                fallback.push(edge.clone());
8720            }
8721        }
8722        Ok((native, fallback))
8723    }
8724
8725    async fn update_existing(
8726        &self,
8727        item: &Resolved,
8728        incoming: &Incoming<'_>,
8729        body: &Option<String>,
8730        status_target: Option<&StatusTarget>,
8731    ) -> Result<(), SourceError> {
8732        let title = incoming.written_title();
8733        // A terminal status closes the issue here, in the same mutation as its body: its board
8734        // option was selected before this, so a close never lands on an item whose board cannot
8735        // show it.
8736        let fields = match item.content_kind {
8737            ContentKind::DraftIssue => json!({"title":title,"body":body}),
8738            ContentKind::Issue => json!({"title":title,"body":body,
8739                                         "stateInput":state_input(status_target)}),
8740        };
8741        self.update_content(item.content_kind, &item.id, fields)
8742            .await
8743    }
8744
8745    /// Update one board item's content with exactly `fields` beside its id, through the
8746    /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
8747    /// a draft.
8748    ///
8749    /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
8750    /// is what lets a narrow write carry the one thing it changes and nothing else.
8751    async fn update_content(
8752        &self,
8753        kind: ContentKind,
8754        id: &NativeId,
8755        fields: Value,
8756    ) -> Result<(), SourceError> {
8757        let (operation, id_key, pointer) = match kind {
8758            ContentKind::DraftIssue => (
8759                graphql::UPDATE_DRAFT,
8760                "draftIssueId",
8761                "/updateProjectV2DraftIssue/draftIssue",
8762            ),
8763            ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
8764        };
8765        let mut input = fields;
8766        input[id_key] = json!(id.0);
8767        let data = self.graphql(operation, json!({"input":input})).await?;
8768        let returned = data
8769            .pointer(pointer)
8770            .ok_or_else(|| SourceError::Malformed {
8771                message: "GitHub item update returned no item".into(),
8772            })?;
8773        if required_str(returned, "id")? != id.0 {
8774            return Err(SourceError::Malformed {
8775                message: "GitHub item update returned the wrong item".into(),
8776            });
8777        }
8778        Ok(())
8779    }
8780
8781    /// Creates one issue, files it on the board, and reports what a read of it would say:
8782    /// its content id, its board item id, and the web address GitHub gave it.
8783    ///
8784    /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
8785    /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
8786    /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
8787    /// board item, and the `addProjectV2ItemById` that then had to follow was refused
8788    /// "Content already exists in this project". A terminal status is not written here:
8789    /// `finish_write` selects its option first and closes the issue after, so a close never
8790    /// lands on an item whose board cannot show it.
8791    ///
8792    /// The address and the number come back here because this is the only place either is
8793    /// known before GitHub's own board read catches up — an item this run created answers
8794    /// the reads that follow it out of the record below, and one remembered without them
8795    /// would report no location and no key for the rest of the run.
8796    async fn create_and_file_issue(
8797        &self,
8798        board_id: &str,
8799        repository: &RepositoryTarget,
8800        incoming: &Incoming<'_>,
8801        body: &Option<String>,
8802    ) -> Result<Landed, SourceError> {
8803        let repository_id = self.repository_id(repository, incoming).await?;
8804        let data = self
8805            .graphql(
8806                graphql::CREATE_ISSUE,
8807                json!({"input":{
8808                    "repositoryId":repository_id,"title":incoming.written_title(),"body":body
8809                }}),
8810            )
8811            .await?;
8812        let created = data
8813            .pointer("/createIssue/issue")
8814            .filter(|value| !value.is_null())
8815            .ok_or_else(|| SourceError::Malformed {
8816                message: "GitHub issue creation returned no issue".into(),
8817            })?;
8818        let content_id = NativeId(required_str(created, "id")?.to_owned());
8819        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
8820        // a response without it is not worth failing a landed write over — the item simply
8821        // reports no location until the board read catches up, which is what it did before.
8822        let url = optional_str(created, "url")?.map(str::to_owned);
8823        // The issue exists from here on, so an unreadable number and a refused board
8824        // filing below each try, best effort, to take it back: an issue in the repository
8825        // that is on no board is an item nobody asked for and nothing here would find again.
8826        //
8827        // Its number is optional on the same terms its address is — a landed write is not
8828        // worth failing over a member that came back missing, and such an item reports no
8829        // handle until a board read catches up. A number that is *present* and is not an
8830        // unsigned integer is still a response this source cannot read.
8831        let number = match created_issue_number(created) {
8832            Ok(number) => number,
8833            Err(error) => {
8834                let _ = self.delete_issue(&content_id).await;
8835                return Err(error);
8836            }
8837        };
8838        let added = match self
8839            .graphql(
8840                graphql::ADD_TO_BOARD,
8841                json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
8842            )
8843            .await
8844        {
8845            Ok(added) => added,
8846            Err(error) => {
8847                let _ = self.delete_issue(&content_id).await;
8848                return Err(error);
8849            }
8850        };
8851        let item = added
8852            .pointer("/addProjectV2ItemById/item")
8853            .filter(|value| !value.is_null())
8854            .ok_or_else(|| SourceError::Malformed {
8855                message: "GitHub board addition returned no project item".into(),
8856            })?;
8857        Ok(Landed {
8858            content_id,
8859            item_id: required_str(item, "id")?.to_owned(),
8860            url,
8861            number,
8862        })
8863    }
8864
8865    /// Move one issue under the project it now belongs to, or out of the one it left.
8866    async fn reparent(
8867        &self,
8868        held: Option<NativeId>,
8869        child: &NativeId,
8870        wanted: Option<&NativeId>,
8871    ) -> Result<(), SourceError> {
8872        if held.as_ref() == wanted {
8873            return Ok(());
8874        }
8875        if let Some(held) = &held {
8876            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
8877                .await?;
8878        }
8879        if let Some(wanted) = wanted {
8880            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
8881                .await?;
8882        }
8883        Ok(())
8884    }
8885
8886    async fn sub_issue(
8887        &self,
8888        operation: &str,
8889        parent: &NativeId,
8890        child: &NativeId,
8891        root: &str,
8892    ) -> Result<(), SourceError> {
8893        let data = self
8894            .graphql(
8895                operation,
8896                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
8897            )
8898            .await?;
8899        let issue =
8900            data.pointer(&format!("/{root}/issue"))
8901                .ok_or_else(|| SourceError::Malformed {
8902                    message: "GitHub sub-issue update returned no issue".into(),
8903                })?;
8904        let sub =
8905            data.pointer(&format!("/{root}/subIssue"))
8906                .ok_or_else(|| SourceError::Malformed {
8907                    message: "GitHub sub-issue update returned no sub-issue".into(),
8908                })?;
8909        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
8910            return Err(SourceError::Malformed {
8911                message: "GitHub sub-issue update returned the wrong issues".into(),
8912            });
8913        }
8914        Ok(())
8915    }
8916
8917    /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
8918    /// whether there was one.
8919    ///
8920    /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
8921    /// relationships are not read: there is nothing a read of them could find.
8922    async fn reconcile_blocked_by(
8923        &self,
8924        content_id: &NativeId,
8925        native: &[String],
8926        issue: Issue<'_>,
8927    ) -> Result<bool, SourceError> {
8928        let current = match issue {
8929            Issue::Created => Vec::new(),
8930            Issue::Existing(Some(held)) => held
8931                .iter()
8932                .map(|far| required_str(far, "id").map(str::to_owned))
8933                .collect::<Result<Vec<_>, _>>()?,
8934            Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
8935        };
8936        let mut changed = false;
8937        for (operation, far_id) in current
8938            .iter()
8939            .filter(|id| !native.contains(id))
8940            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
8941            .chain(
8942                native
8943                    .iter()
8944                    .filter(|id| !current.contains(id))
8945                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
8946            )
8947        {
8948            let data = self
8949                .graphql(
8950                    operation,
8951                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
8952                )
8953                .await?;
8954            let root = if operation == graphql::ADD_BLOCKED_BY {
8955                "addBlockedBy"
8956            } else {
8957                "removeBlockedBy"
8958            };
8959            let issue =
8960                data.pointer(&format!("/{root}/issue"))
8961                    .ok_or_else(|| SourceError::Malformed {
8962                        message: "GitHub dependency update returned no issue".into(),
8963                    })?;
8964            let blocker = data
8965                .pointer(&format!("/{root}/blockingIssue"))
8966                .ok_or_else(|| SourceError::Malformed {
8967                    message: "GitHub dependency update returned no blocking issue".into(),
8968                })?;
8969            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
8970            {
8971                return Err(SourceError::Malformed {
8972                    message: "GitHub dependency update returned the wrong issues".into(),
8973                });
8974            }
8975            changed = true;
8976        }
8977        Ok(changed)
8978    }
8979}
8980
8981/// What a write needs to know of one far end it names: which kind of item it is, and whether
8982/// it is an issue a native relationship can name.
8983struct FarEnd {
8984    kind: BoardKind,
8985    content_kind: ContentKind,
8986}
8987
8988/// Whether the issue one write reconciles was created by that write or was already there.
8989#[derive(Clone, Copy, PartialEq, Eq)]
8990enum Issue<'a> {
8991    /// Created by this write, so it holds no relationships yet.
8992    Created,
8993    /// On the board before this write, holding whatever relationships it holds — the far
8994    /// ends of its whole `blockedBy`, when the read that reached it carried them.
8995    Existing(Option<&'a [Value]>),
8996}
8997
8998/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
8999enum Reached {
9000    /// An issue this board holds, resolved into everything this source reports about it.
9001    Held(Box<Resolved>),
9002    /// Nothing this board holds: no such node, or a node on some other board.
9003    Nothing,
9004    /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
9005    /// again by [`GitHubProjectsSource::draft_by_id`].
9006    Draft,
9007}
9008
9009/// What GitHub says when a string is not a node id it can resolve.
9010///
9011/// Matched because it is the ordinary answer to a project selector naming a project by its
9012/// *name*, and reporting that as a failure would make naming one impossible. It is read
9013/// off the refusal GitHub sent, never guessed from the shape of the string: this source
9014/// does not define the syntax of a GitHub node id and would be wrong about it.
9015const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
9016
9017/// `error` with `note` added to the end of what it says, its kind and every other member
9018/// unchanged — so a caller still branches on the failure that happened, and reads beside it
9019/// what that failure left behind.
9020fn noting(error: SourceError, note: &str) -> SourceError {
9021    match error {
9022        SourceError::Config { message } => SourceError::Config {
9023            message: message + note,
9024        },
9025        SourceError::Auth { message } => SourceError::Auth {
9026            message: message + note,
9027        },
9028        SourceError::Refused { message } => SourceError::Refused {
9029            message: message + note,
9030        },
9031        SourceError::RateLimited {
9032            retry_after_seconds,
9033            message,
9034        } => SourceError::RateLimited {
9035            retry_after_seconds,
9036            message: Some(message.unwrap_or_default() + note),
9037        },
9038        SourceError::Unavailable { message } => SourceError::Unavailable {
9039            message: message + note,
9040        },
9041        SourceError::Malformed { message } => SourceError::Malformed {
9042            message: message + note,
9043        },
9044    }
9045}
9046
9047/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
9048/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
9049fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest) -> Value {
9050    let mut variables = serde_json::Map::new();
9051    for slot in 0..COPY_DETAIL_BATCH {
9052        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
9053        variables.insert(format!("id{slot}"), json!(id));
9054    }
9055    variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
9056    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
9057    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
9058    variables.insert("duplicates".to_owned(), json!(true));
9059    Value::Object(variables)
9060}
9061
9062/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
9063/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
9064/// for them.
9065///
9066/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
9067/// is read again at no added price.
9068fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
9069    let mut variables = serde_json::Map::new();
9070    for slot in 0..DETAIL_BATCH {
9071        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
9072        variables.insert(format!("id{slot}"), json!(id));
9073    }
9074    variables.insert(
9075        "first".to_owned(),
9076        json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
9077    );
9078    variables.insert("comments".to_owned(), json!(comments.is_some()));
9079    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
9080    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
9081    variables.insert("duplicates".to_owned(), json!(true));
9082    Value::Object(variables)
9083}
9084
9085/// Whether this refusal is GitHub saying the id names no node at all.
9086fn unresolvable_node(error: &SourceError) -> bool {
9087    matches!(error, SourceError::Refused { message }
9088        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
9089}
9090
9091/// One project name, as a search qualifier which filters on it at the server.
9092///
9093/// Quoted so the whole title is one phrase rather than a bag of words, with the two
9094/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
9095/// the way it documents. A title matched here is still compared for equality afterwards:
9096/// the qualifier narrows what the server sends, and this source decides what it names.
9097fn title_qualifier(name: &str) -> String {
9098    format!("in:title {}", quoted(name))
9099}
9100
9101/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
9102/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
9103/// it documents — so a value holding a qualifier's spelling is searched for rather than
9104/// obeyed.
9105fn quoted(value: &str) -> String {
9106    let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
9107    format!("\"{escaped}\"")
9108}
9109
9110/// The search qualifier for the issues updated at or after `since`.
9111///
9112/// Written to the second, rounded down, which can only widen what the search returns.
9113fn updated_qualifier(since: DateTime<Utc>) -> String {
9114    format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
9115}
9116
9117/// The search terms that narrow a board-scoped issue search to a task query's text and
9118/// metadata predicates, or `None` when it carries neither.
9119///
9120/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
9121/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
9122/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
9123/// matches each in any field the `in:` qualifier names, so a query naming a title search and
9124/// a metadata value searches both fields for both — wider than asked, never narrower, and
9125/// every candidate is confirmed in process afterwards.
9126///
9127/// **This narrows a text search, and that is this source's declared semantics.** GitHub
9128/// matches whole tokens where a substring rule would match inside a word, so an item holding
9129/// the text only inside a longer word is not returned. A text of nothing but whitespace
9130/// matches every item, so it narrows nothing and is not sent.
9131fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
9132    let text = query
9133        .text
9134        .as_ref()
9135        .filter(|text| !text.terms.trim().is_empty());
9136    if text.is_none() && query.metadata.is_empty() {
9137        return None;
9138    }
9139    let (title, body) = match text.map(|text| text.fields) {
9140        None => (false, true),
9141        Some(TextFields::Title) => (true, !query.metadata.is_empty()),
9142        Some(TextFields::Content) => (false, true),
9143        Some(TextFields::TitleOrContent) => (true, true),
9144    };
9145    let fields = match (title, body) {
9146        (true, true) => "in:title,body",
9147        (true, false) => "in:title",
9148        _ => "in:body",
9149    };
9150    let phrases = text
9151        .map(|text| text.terms.clone())
9152        .into_iter()
9153        .chain(
9154            query
9155                .metadata
9156                .iter()
9157                .map(|wanted| as_stored(wanted.value())),
9158        )
9159        .map(|phrase| quoted(&phrase))
9160        .collect::<Vec<_>>();
9161    Some(format!("{fields} {}", phrases.join(" ")))
9162}
9163
9164/// The search terms that narrow a board-scoped issue search to a project or document query's
9165/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
9166/// carrying that text alone is sent as by [`narrowing_qualifiers`].
9167fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
9168    narrowing_qualifiers(&TaskQuery {
9169        text: text.cloned(),
9170        ..TaskQuery::default()
9171    })
9172}
9173
9174/// Refuses a project or document query's text GitHub's issue search cannot find, before
9175/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
9176/// query's.
9177fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
9178    refuse_unsearchable(&TaskQuery {
9179        text: text.cloned(),
9180        ..TaskQuery::default()
9181    })
9182}
9183
9184/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
9185/// before anything is asked of GitHub.
9186///
9187/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
9188/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
9189/// left out, the search is every issue of the board. So this source says it cannot answer
9190/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
9191/// nothing GitHub could search for, and keeps the board read it always had.
9192fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
9193    const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
9194                       letter or digit with a bounded query";
9195    if let Some(text) = &query.text
9196        && !text.terms.trim().is_empty()
9197        && !has_words(&text.terms)
9198    {
9199        return Err(SourceError::Refused {
9200            message: format!(
9201                "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
9202                text.terms
9203            ),
9204        });
9205    }
9206    if let Some(wanted) = query
9207        .metadata
9208        .iter()
9209        .find(|wanted| !has_words(wanted.value()))
9210    {
9211        return Err(SourceError::Refused {
9212            message: format!(
9213                "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
9214                wanted.value(),
9215                std::iter::once(wanted.key())
9216                    .chain(wanted.path().iter().map(String::as_str))
9217                    .collect::<Vec<_>>()
9218                    .join("/"),
9219            ),
9220        });
9221    }
9222    Ok(())
9223}
9224
9225/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
9226fn has_words(phrase: &str) -> bool {
9227    phrase.chars().any(char::is_alphanumeric)
9228}
9229
9230/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
9231///
9232/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
9233/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
9234/// which GitHub's word match would read as different words.
9235fn as_stored(value: &str) -> String {
9236    let encoded = Value::String(value.to_owned()).to_string();
9237    encoded[1..encoded.len() - 1].to_owned()
9238}
9239
9240/// The one narrower question a task query carrying a text, metadata or origin predicate is
9241/// sent as.
9242enum Narrowing {
9243    /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
9244    Origin(String),
9245    /// The board-scoped issue search narrowed by these qualifiers.
9246    Search(String),
9247}
9248
9249impl Narrowing {
9250    /// What this question is remembered under for the length of one command.
9251    fn key(&self) -> String {
9252        match self {
9253            Self::Origin(origin) => format!("origin {origin}"),
9254            Self::Search(also) => format!("search {also}"),
9255        }
9256    }
9257}
9258
9259/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
9260enum Resumed {
9261    /// It reported another page, which starts after this cursor.
9262    More(String),
9263    /// It has ended. Sending this cursor again — the page's own end when it had one, and
9264    /// otherwise the cursor it was reached from — answers an empty page, so the one document
9265    /// can go on walking the other connection.
9266    Ended(Option<String>),
9267}
9268
9269impl Resumed {
9270    /// Whether the connection has another page.
9271    const fn has_more(&self) -> bool {
9272        matches!(self, Self::More(_))
9273    }
9274
9275    /// The cursor to send this connection next.
9276    fn cursor(self) -> Option<String> {
9277        match self {
9278            Self::More(next) => Some(next),
9279            Self::Ended(last) => last,
9280        }
9281    }
9282}
9283
9284/// Where `connection`, reached from `after`, resumes — refused when it reports another page
9285/// with no cursor to it, or from a cursor that does not advance.
9286fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
9287    let info = connection
9288        .get("pageInfo")
9289        .ok_or_else(|| SourceError::Malformed {
9290            message: "GitHub connection has no pageInfo".into(),
9291        })?;
9292    let end = optional_str(info, "endCursor")?;
9293    if required_bool(info, "hasNextPage")? {
9294        let next = end.ok_or_else(|| SourceError::Malformed {
9295            message: "GitHub connection reports another page and no endCursor".into(),
9296        })?;
9297        validate_cursor_progress(after, next)?;
9298        return Ok(Resumed::More(next.to_owned()));
9299    }
9300    Ok(Resumed::Ended(
9301        end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
9302    ))
9303}
9304
9305/// The board, and every item on it this source reports.
9306#[derive(Clone)]
9307struct Board {
9308    id: String,
9309    fields: Value,
9310    items: Vec<Resolved>,
9311}
9312
9313/// What a write needs of the board and nothing more: its node id and its field
9314/// definitions, in the shape a read of the board's own `fields` gives them.
9315///
9316/// Deliberately no items. A write decides which item it writes, which parent it files
9317/// under and which far ends it names by reading each of them by its own id; this is the
9318/// half of the board those reads cannot carry, and holding no item is what keeps it from
9319/// ever being asked whether an item is there.
9320#[derive(Clone)]
9321struct BoardFields {
9322    id: BoardId,
9323    fields: Value,
9324}
9325
9326/// A board's node id: what a field write and `addProjectV2ItemById` address.
9327///
9328/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
9329/// refused where it is read, and one an item names blank is read as not named at all.
9330#[derive(Clone)]
9331struct BoardId(String);
9332
9333/// Where one write left its item, for the record the rest of the command reads it out of.
9334///
9335/// A named record rather than a tuple because the update arm and the create arm each fill
9336/// all four, and two `Option`s of different meaning side by side in a tuple are two
9337/// positions a reader has to count.
9338struct Landed {
9339    /// The issue's own node id, which is the [`NativeId`] this source reports.
9340    content_id: NativeId,
9341    /// The board item's id, which is what a field write addresses.
9342    // 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.
9343    item_id: String,
9344    /// The web address GitHub gave the issue, when it gave one.
9345    // 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.
9346    url: Option<String>,
9347    /// The issue's number on its repository, when GitHub reported one.
9348    number: Option<u64>,
9349}
9350
9351impl BoardId {
9352    fn parse(id: &str) -> Result<Self, SourceError> {
9353        if id.trim().is_empty() {
9354            return Err(SourceError::Malformed {
9355                message: "GitHub named a board with a blank node id".into(),
9356            });
9357        }
9358        Ok(Self(id.to_owned()))
9359    }
9360
9361    fn as_str(&self) -> &str {
9362        &self.0
9363    }
9364}
9365
9366impl Board {
9367    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
9368        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
9369        let nodes = fields
9370            .get("nodes")
9371            .and_then(Value::as_array)
9372            .ok_or_else(|| SourceError::Malformed {
9373                message: "GitHub project fields.nodes is not an array".into(),
9374            })?;
9375        Ok(nodes
9376            .iter()
9377            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
9378    }
9379}
9380
9381/// One board item, resolved into everything this source reports about it.
9382#[derive(Clone)]
9383struct Resolved {
9384    item_id: String,
9385    id: NativeId,
9386    content_kind: ContentKind,
9387    kind: BoardKind,
9388    title: String,
9389    body: Option<String>,
9390    /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
9391    /// that changes the slot alone has to keep byte for byte outside it.
9392    raw_body: Option<String>,
9393    status: Status,
9394    /// The name of the board `Status` option this item sits in, as the board spells it.
9395    option: Option<String>,
9396    /// What its `Priority` field says, read through this instance's mapping.
9397    priority: HeldPriority,
9398    /// Whether this item's issue is closed. A draft has no such state and is never closed.
9399    closed: bool,
9400    /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
9401    delivers: Vec<TaskRef>,
9402    /// Every task that delivers this one, read out of its slot. Empty for anything not a
9403    /// task.
9404    delivered_by: Vec<TaskRef>,
9405    labels: Vec<Label>,
9406    parent: Option<NativeId>,
9407    // 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.
9408    origin: Option<String>,
9409    /// What each board text field this instance projects metadata onto holds for this item,
9410    /// by field name — only the fields holding a value, an empty text included. What a write compares the
9411    /// item's metadata against, so a field already holding its value is not written again;
9412    /// never read back as metadata, which the slot alone reports.
9413    projected: BTreeMap<String, String>,
9414    /// The issue's own number on its repository, as GitHub reports it.
9415    ///
9416    /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
9417    /// declares none, and a draft is not filed in a repository to be numbered by one — and
9418    /// an issue this run created whose creating mutation answered without one, which is a
9419    /// response GitHub's own schema says cannot happen and which a landed write is not
9420    /// worth failing over. An `Issue` read off the board always has one.
9421    number: Option<u64>,
9422    url: Option<String>,
9423    created_at: Option<DateTime<Utc>>,
9424    updated_at: Option<DateTime<Utc>>,
9425    /// When the board item itself last changed — what a board field write moves — when the
9426    /// read that reached it said.
9427    item_updated_at: Option<DateTime<Utc>>,
9428    /// How many comments the issue holds, when the read that reached it said.
9429    comment_count: Option<u64>,
9430    /// How many relationship events the issue's timeline holds — every dependency, as
9431    /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
9432    /// removed — when the read that reached it said.
9433    relationship_events: Option<u64>,
9434    /// The server's clock on the response that read it — its `Date` header — when the
9435    /// response carried one.
9436    served_at: Option<DateTime<Utc>>,
9437    own_repository: Option<Repository>,
9438    repositories: Vec<Repository>,
9439    classification: Classification,
9440    slot: BTreeMap<String, Value>,
9441    /// The node id of the board this item sits on, when the read that reached it said.
9442    board_id: Option<String>,
9443    /// The definition of every board field this item holds a value of, in the shape a read
9444    /// of the board's own `fields` gives one.
9445    ///
9446    /// Only the fields this item has a value in: a field it holds nothing of is not here,
9447    /// which says nothing about whether the board has it.
9448    fields: Vec<Value>,
9449    /// Every field the board this item sits on defines, as its own read of the board's
9450    /// `fields` gives them — when the read that reached the item carried them, which a read
9451    /// of it by its own id does. What a write of it needs of the board, then, needs no read
9452    /// of the board.
9453    board_fields: Option<Value>,
9454    /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
9455    /// selects one — when the read that reached it carried the connection to its end, which a
9456    /// read of it by its own id does for any issue blocked by no more than a page. What a
9457    /// write reconciles that relationship against, and what a read of its forward edges in
9458    /// the same command answers with.
9459    blocked_by: Option<Vec<Value>>,
9460}
9461
9462impl Resolved {
9463    /// The board this item's own read names it on, when that read named one this source can
9464    /// address.
9465    fn named_board(&self) -> Option<BoardId> {
9466        self.board_id
9467            .as_deref()
9468            .and_then(|id| BoardId::parse(id).ok())
9469    }
9470
9471    /// The board's id and every field it defines, when the read that reached this item
9472    /// carried both — which a read of it by its own id does.
9473    fn carried_board(&self) -> Option<BoardFields> {
9474        Some(BoardFields {
9475            id: self.named_board()?,
9476            fields: self.board_fields.clone()?,
9477        })
9478    }
9479
9480    /// Whether this item holds a value of the board field called `name`, and so carries
9481    /// that field's definition. `false` says nothing about whether the board has the field.
9482    fn defines(&self, name: &str) -> bool {
9483        self.fields
9484            .iter()
9485            .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
9486    }
9487
9488    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
9489    /// in a field of its own, and none of the five keys that are only an encoding.
9490    ///
9491    /// The two delivery keys are left out for every kind, not only for a task: they are
9492    /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
9493    /// document carrying one holds nothing a caller's own metadata could mean by it.
9494    fn metadata(&self) -> BTreeMap<String, Value> {
9495        let mut metadata = self.slot.clone();
9496        metadata.remove(Repository::METADATA_KEY);
9497        metadata.remove(DependencyEdge::RECORDED_KEY);
9498        metadata.remove(ItemKind::METADATA_KEY);
9499        metadata.remove(TaskRef::DELIVERS_KEY);
9500        metadata.remove(TaskRef::DELIVERED_BY_KEY);
9501        metadata.remove(Classification::METADATA_KEY);
9502        // The board field is the origin, and the body's copy of it is only a mirror for the
9503        // issue search to find: an item whose field holds none has none, whatever its body
9504        // says, so no reader ever sees two answers.
9505        metadata.remove(ORIGIN_KEY);
9506        if let Some(origin) = &self.origin {
9507            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
9508        }
9509        metadata
9510    }
9511
9512    /// Where this item is, as a link a reader can open.
9513    ///
9514    /// A board is a hosted place and every issue on it has a web address, so that address
9515    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
9516    /// of place it is, so a reader knows to open it rather than to read a file out. It
9517    /// does not replace or derive from `url`: the field goes on reporting exactly what it
9518    /// reported before, and this says what that address *is*.
9519    ///
9520    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
9521    /// rather than a third variant, which is the contract's "the source did not say". An
9522    /// issue this run created is not one of those: its address comes back from the
9523    /// creating mutation, so it is somewhere a reader can open from the moment it exists
9524    /// rather than from whenever the board read catches up.
9525    fn location(&self) -> Option<Location> {
9526        self.url.clone().map(Location::Url)
9527    }
9528
9529    /// The short handle this board's backend shows people for a task: the issue's number
9530    /// alone, as a decimal string.
9531    ///
9532    /// The number alone rather than `owner/repo#1043`, because that is the contract's
9533    /// value for this backend. A draft has no number and so no handle, which is the
9534    /// contract's *absent* rather than a handle of some other shape — and the native
9535    /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
9536    /// derives from.
9537    fn key(&self) -> Option<String> {
9538        self.number.map(|number| number.to_string())
9539    }
9540
9541    /// This item's change token — see [`change_token`] — or `None` when the read that
9542    /// reached it did not carry all five of its parts, which a draft never does, did not say
9543    /// when GitHub served it, or found it changed too recently to have settled.
9544    fn version(&self) -> Option<String> {
9545        match self.content_kind {
9546            ContentKind::Issue => {
9547                let (issue, item) = (self.updated_at?, self.item_updated_at?);
9548                let parts = change_token::Parts {
9549                    issue,
9550                    item,
9551                    comments: self.comment_count?,
9552                    relationships: self.relationship_events?,
9553                    repository: self.own_repository.clone()?,
9554                };
9555                change_token::settled(issue, item, self.served_at?)
9556                    .then(|| change_token::spell(&parts))
9557            }
9558            ContentKind::DraftIssue => None,
9559        }
9560    }
9561
9562    /// Whether its `Priority` field holds a value at all, mapped or not.
9563    fn holds_priority(&self) -> bool {
9564        self.priority != HeldPriority::Read(Priority::None)
9565    }
9566
9567    /// The task this item is.
9568    ///
9569    /// Fails for an item whose `Priority` field holds an option the mapping does not name:
9570    /// reading that as a level would be a guess, and reading it as `none` would let the next
9571    /// copy clear a priority a person set.
9572    fn task(&self) -> Result<Task, SourceError> {
9573        let priority = match &self.priority {
9574            HeldPriority::Read(priority) => *priority,
9575            HeldPriority::Unmapped(option) => {
9576                return Err(SourceError::Malformed {
9577                    message: format!(
9578                        "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
9579                         this source's priority_mapping does not name, so its priority cannot be \
9580                         read; next: name {option:?} under priority_mapping, or move the item to \
9581                         a mapped option",
9582                        self.id,
9583                        self.number
9584                            .map(|number| format!(" (#{number})"))
9585                            .unwrap_or_default()
9586                    ),
9587                });
9588            }
9589        };
9590        Ok(Task {
9591            version: self.version(),
9592            id: self.id.clone(),
9593            key: self.key(),
9594            title: self.title.clone(),
9595            content: self.body.clone(),
9596            status: self.status.clone(),
9597            priority,
9598            labels: self.labels.clone(),
9599            project: self.parent.clone(),
9600            url: self.url.clone(),
9601            location: self.location(),
9602            created_at: self.created_at,
9603            updated_at: self.updated_at,
9604            metadata: self.metadata(),
9605            repositories: self.repositories.clone(),
9606            delivers: self.delivers.clone(),
9607            delivered_by: self.delivered_by.clone(),
9608            classification: self.classification,
9609        })
9610    }
9611
9612    fn project(&self) -> Project {
9613        Project {
9614            id: self.id.clone(),
9615            title: self.title.clone(),
9616            content: self.body.clone(),
9617            status: self.status.clone(),
9618            labels: self.labels.clone(),
9619            url: self.url.clone(),
9620            location: self.location(),
9621            created_at: self.created_at,
9622            updated_at: self.updated_at,
9623            metadata: self.metadata(),
9624            repositories: self.repositories.clone(),
9625            classification: self.classification,
9626        }
9627    }
9628
9629    /// The same issue as a document: the project it is filed under, and no status and no
9630    /// dependencies, because a document is not work.
9631    fn document(&self) -> Document {
9632        Document {
9633            id: self.id.clone(),
9634            title: self.title.clone(),
9635            content: self.body.clone(),
9636            project: self.parent.clone(),
9637            labels: self.labels.clone(),
9638            url: self.url.clone(),
9639            location: self.location(),
9640            created_at: self.created_at,
9641            updated_at: self.updated_at,
9642            metadata: self.metadata(),
9643            repositories: self.repositories.clone(),
9644            classification: self.classification,
9645        }
9646    }
9647}
9648
9649/// Where one targeted update moves an item's status, and which of its two halves move.
9650struct StatusMove {
9651    /// The board the item's `Status` field is on.
9652    board: BoardId,
9653    /// The `Status` field's id.
9654    field: String,
9655    /// The option's id.
9656    option: String,
9657    /// The option's name, as the board spells it.
9658    name: String,
9659    /// What the status asks of the issue's state.
9660    target: StatusTarget,
9661    /// The status the item reads as once it is there.
9662    landed: Status,
9663    /// Which of the status's two halves differ from what the item holds.
9664    moves: Moves,
9665}
9666
9667/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
9668/// closed state of its issue, or both. A status neither half of which differs is no move at all,
9669/// and is not a value of this type.
9670#[derive(Clone, Copy, PartialEq, Eq)]
9671enum Moves {
9672    /// The option alone.
9673    Option,
9674    /// The issue's state alone: open, closed, or closed with another reason.
9675    State,
9676    /// Both.
9677    Both,
9678}
9679
9680impl Moves {
9681    /// What differs, or `None` when nothing does.
9682    const fn of(option: bool, state: bool) -> Option<Self> {
9683        match (option, state) {
9684            (true, true) => Some(Self::Both),
9685            (true, false) => Some(Self::Option),
9686            (false, true) => Some(Self::State),
9687            (false, false) => None,
9688        }
9689    }
9690
9691    /// Whether the option moves.
9692    const fn option(self) -> bool {
9693        matches!(self, Self::Option | Self::Both)
9694    }
9695
9696    /// Whether the issue's state moves.
9697    const fn state(self) -> bool {
9698        matches!(self, Self::State | Self::Both)
9699    }
9700}
9701
9702/// What one write is, and the status that comes with being it.
9703///
9704/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
9705/// status and a task or a project always has one, so "a document carrying a status" and
9706/// "a task carrying none" are states a write cannot be in rather than states every use
9707/// site below has to defend against.
9708enum Written<'a> {
9709    /// A document, which is not work and so has no status at all.
9710    Document,
9711    /// A task or a project, and the status it is being written with.
9712    Work(ItemKind, &'a Status),
9713}
9714
9715impl Written<'_> {
9716    /// Which of the board's three kinds this write is.
9717    const fn kind(&self) -> BoardKind {
9718        match self {
9719            Self::Document => BoardKind::Document,
9720            Self::Work(kind, _) => BoardKind::Work(*kind),
9721        }
9722    }
9723
9724    /// The status this write carries. A document carries none, so a write of one says
9725    /// nothing about the issue's open or closed state and selects no board `Status`
9726    /// option.
9727    const fn status(&self) -> Option<&Status> {
9728        match self {
9729            Self::Document => None,
9730            Self::Work(_, status) => Some(status),
9731        }
9732    }
9733
9734    /// The status this write carries with the kind whose half of `status_mapping` it is
9735    /// written through.
9736    const fn work_status(&self) -> Option<(ItemKind, &Status)> {
9737        match self {
9738            Self::Document => None,
9739            Self::Work(kind, status) => Some((*kind, status)),
9740        }
9741    }
9742}
9743
9744/// The item being written, in the one shape all three write methods reach.
9745struct Incoming<'a> {
9746    written: Written<'a>,
9747    /// The title a person wrote. A document's goes onto the issue with
9748    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
9749    title: &'a str,
9750    content: Option<&'a str>,
9751    assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
9752    labels: &'a [Label],
9753    metadata: &'a BTreeMap<String, Value>,
9754    repositories: &'a [Repository],
9755    /// Recorded in the slot while private, and nowhere while public.
9756    classification: Classification,
9757    parent: Option<&'a NativeId>,
9758    /// [`Task::delivers`], already checked. Empty for a project or a document, which is
9759    /// what keeps either key out of their slot.
9760    delivers: &'a [TaskRef],
9761    /// [`Task::delivered_by`], already checked. Empty for a project or a document.
9762    delivered_by: &'a [TaskRef],
9763    /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
9764    /// project, a document, and every write to an instance with no `priority_mapping` —
9765    /// which is what keeps such a write's requests exactly what they were before.
9766    priority: Option<Priority>,
9767}
9768
9769/// What one write does to one projected metadata text field, by the field's id.
9770#[derive(Debug, Clone)]
9771enum ProjectionWrite {
9772    /// Write this text.
9773    Set {
9774        field: String, // llmlint: ignore[invalid_states_unrepresentable] A private opaque GraphQL field id passed straight back to GitHub, as `PriorityWrite`'s are; GitHub publishes no grammar for it.
9775        text: String,
9776    },
9777    /// Clear the field's value.
9778    Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
9779}
9780
9781impl ProjectionWrite {
9782    /// Add this write to a field write's list of values and list of clears.
9783    fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
9784        match self {
9785            Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
9786            Self::Clear { field } => clears.push(field),
9787        }
9788    }
9789}
9790
9791/// What one write does to an item's `Priority` field.
9792enum PriorityWrite {
9793    /// Select this option of this field.
9794    Select {
9795        /// The `Priority` field's id.
9796        field: String,
9797        /// The mapped option's id.
9798        option: String,
9799    },
9800    /// Clear the field's value, which is what `none` is.
9801    Clear {
9802        /// The `Priority` field's id.
9803        field: String,
9804    },
9805}
9806
9807impl Incoming<'_> {
9808    /// The title this write puts on the issue.
9809    fn written_title(&self) -> String {
9810        match self.written {
9811            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
9812            Written::Work(..) => self.title.to_owned(),
9813        }
9814    }
9815}
9816
9817#[derive(Clone, Copy, PartialEq, Eq)]
9818enum ContentKind {
9819    DraftIssue,
9820    Issue,
9821}
9822
9823/// What one board issue is: a document, or the work an [`ItemKind`] names.
9824///
9825/// A type of this source's own rather than an `ItemKind` with a third variant, because
9826/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
9827/// document — the contract keeps a document out of that enum deliberately. Holding the
9828/// board's three answers in one value is what makes every place that asks "which is this?"
9829/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
9830/// two thirds of the board.
9831#[derive(Clone, Copy, PartialEq, Eq)]
9832enum BoardKind {
9833    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
9834    Document,
9835    /// Every other issue, and every draft.
9836    Work(ItemKind),
9837}
9838
9839impl BoardKind {
9840    /// Whose half of `status_mapping` an item of this kind reads its status through. A
9841    /// document has no status of its own, so the task half stands in for whatever the issue
9842    /// holds; nothing reports it.
9843    const fn status_kind(self) -> ItemKind {
9844        match self {
9845            Self::Document => ItemKind::Task,
9846            Self::Work(kind) => kind,
9847        }
9848    }
9849
9850    /// How a refusal names this kind to the person reading it.
9851    const fn describes(self) -> &'static str {
9852        match self {
9853            Self::Document => "document",
9854            Self::Work(kind) => kind.marker(),
9855        }
9856    }
9857}
9858
9859/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
9860///
9861/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
9862/// the shared cross-source journeys assert one answer to one question, so two sources
9863/// that disagree about what "carries the label bug" means fail them.
9864fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
9865    let holds = |name: &String| {
9866        labels
9867            .iter()
9868            .any(|label| label.name.eq_ignore_ascii_case(name))
9869    };
9870    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
9871        && filter.all_of.iter().all(holds)
9872        && !filter.none_of.iter().any(holds)
9873}
9874
9875/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
9876/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
9877fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
9878    statuses.is_empty() || statuses.contains(&category)
9879}
9880
9881/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
9882///
9883/// `content` is the item's own prose — the body with this source's trailing metadata
9884/// comment already taken off — so a search never matches an encoding the author of the
9885/// issue never wrote.
9886fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
9887    let terms = query.terms.to_lowercase();
9888    let in_title = title.to_lowercase().contains(&terms);
9889    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
9890    match query.fields {
9891        TextFields::Title => in_title,
9892        TextFields::Content => in_content,
9893        TextFields::TitleOrContent => in_title || in_content,
9894    }
9895}
9896
9897/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
9898///
9899/// The project predicate is passed separately because a read narrowed to one project has
9900/// already answered it by asking *that project* for its own items — and re-applying it
9901/// there would compare the caller's selector, which may be a project's **name**, against
9902/// the id of the project that name resolved to, and keep nothing. Every other read passes
9903/// `query.project` and applies it here, which is what keeps `projects` a predicate this
9904/// source really does apply.
9905fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
9906    labels_match(&task.labels, &query.labels)
9907        && status_matches(task.status.category, &query.statuses)
9908        && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
9909        && match project {
9910            ProjectFilter::Any => true,
9911            ProjectFilter::Orphans => task.project.is_none(),
9912            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
9913        }
9914        && query
9915            .text
9916            .as_ref()
9917            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
9918        // Against the parsed metadata slot, and against the origin field, which is where
9919        // `Resolved::metadata` reads each of them from.
9920        && query.metadata_matches(&task.metadata)
9921        && query.origin_matches(&task.metadata)
9922}
9923
9924fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
9925    labels_match(&project.labels, &query.labels)
9926        && status_matches(project.status.category, &query.statuses)
9927        && query
9928            .text
9929            .as_ref()
9930            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
9931}
9932
9933/// The same three predicates a task query carries, minus the status filter.
9934///
9935/// A document is not work, so it has no status for one to compare against and the query
9936/// type carries none. The project predicate is the same one — a design issue filed under a
9937/// project issue is in that project, and one filed under nothing is in none — so it is
9938/// spelled the same way here rather than answered differently.
9939fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
9940    labels_match(&document.labels, &query.labels)
9941        && match project {
9942            ProjectFilter::Any => true,
9943            ProjectFilter::Orphans => document.project.is_none(),
9944            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
9945        }
9946        && query
9947            .text
9948            .as_ref()
9949            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
9950}
9951
9952#[async_trait::async_trait]
9953impl TaskSource for GitHubProjectsSource {
9954    fn kind(&self) -> &'static str {
9955        KIND
9956    }
9957    fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
9958        change_token::order(a, b)
9959    }
9960    fn capabilities(&self) -> Capabilities {
9961        Capabilities {
9962            projects: Support::Native,
9963            documents: Support::Native,
9964            comments: Support::Native,
9965            assets: Support::Native,
9966            priority: if self.priorities.is_some() {
9967                Support::Native
9968            } else {
9969                Support::Unsupported
9970            },
9971            filter_by_priority: Support::Native,
9972            filter_by_comment_activity: Support::Native,
9973            filter_by_metadata: Support::Native,
9974            filter_by_origin: Support::Native,
9975            orphan_tasks: Support::Native,
9976            filter_by_label: Support::Native,
9977            filter_by_status: Support::Native,
9978            search_title: Support::Native,
9979            search_content: Support::Native,
9980            task_dependencies: DependencySupport::BothDirections,
9981            project_dependencies: DependencySupport::BothDirections,
9982            max_page_size: MAX_PAGE_SIZE,
9983        }
9984    }
9985    async fn visibility(
9986        &self,
9987        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
9988    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
9989        self.write_visibility(target).await
9990    }
9991    async fn health(&self) -> Result<Health, SourceError> {
9992        let board = self.board_page(None, 1).await?;
9993        Ok(Health {
9994            reachable: true,
9995            detail: Some(format!(
9996                "reading GitHub project {}/{} ({})",
9997                self.owner,
9998                self.project_number,
9999                required_str(&board, "title")?
10000            )),
10001        })
10002    }
10003    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
10004        self.item_by_id(id)
10005            .await?
10006            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
10007            .map(|item| item.task())
10008            .transpose()
10009    }
10010    async fn task_assets(
10011        &self,
10012        id: &NativeId,
10013    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
10014        self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
10015    }
10016    async fn task_asset(
10017        &self,
10018        id: &NativeId,
10019        name: &onetaskgraph_plugin_api::AssetName,
10020    ) -> Result<Option<Vec<u8>>, SourceError> {
10021        self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
10022            .await
10023    }
10024    async fn set_task_rendering_with_assets(
10025        &self,
10026        id: &NativeId,
10027        content: &str,
10028        provenance: &Value,
10029        _answers: &BTreeMap<String, Value>,
10030        assets: &onetaskgraph_plugin_api::AssetWrite,
10031    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
10032        self.replace_rendering(
10033            id,
10034            BoardKind::Work(ItemKind::Task),
10035            content,
10036            provenance,
10037            Some(assets),
10038        )
10039        .await
10040    }
10041    async fn document_assets(
10042        &self,
10043        id: &NativeId,
10044    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
10045        self.held_assets(id, BoardKind::Document).await
10046    }
10047    async fn document_asset(
10048        &self,
10049        id: &NativeId,
10050        name: &onetaskgraph_plugin_api::AssetName,
10051    ) -> Result<Option<Vec<u8>>, SourceError> {
10052        self.held_asset(id, BoardKind::Document, name).await
10053    }
10054    async fn set_document_rendering_with_assets(
10055        &self,
10056        id: &NativeId,
10057        content: &str,
10058        provenance: &Value,
10059        _answers: &BTreeMap<String, Value>,
10060        assets: &onetaskgraph_plugin_api::AssetWrite,
10061    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
10062        self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
10063            .await
10064    }
10065    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
10066        Ok(self
10067            .item_by_id(id)
10068            .await?
10069            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
10070            .map(|item| item.project()))
10071    }
10072    async fn query_tasks(
10073        &self,
10074        query: &TaskQuery,
10075        page: &PageRequest,
10076    ) -> Result<Page<Task>, SourceError> {
10077        validate_page(page)?;
10078        refuse_unsearchable(query)?;
10079        if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
10080            let qualifiers = match (narrowing_qualifiers(query), query.commented_since) {
10081                (Some(also), Some(since)) => Some(format!("{} {also}", updated_qualifier(since))),
10082                (Some(also), None) => Some(also),
10083                (None, Some(since)) => Some(updated_qualifier(since)),
10084                (None, None) => None,
10085            };
10086            if let Some(also) = qualifiers {
10087                return self.search_tasks(query, page, &also).await;
10088            }
10089        }
10090
10091        // A read narrowed to one project asks that project for its own tasks, so nothing
10092        // about it costs what the rest of the board holds. A read carrying a text, metadata
10093        // or origin predicate asks GitHub the narrower question those predicates are, and a
10094        // read narrowed to comment activity alone asks the board's own issue search for the
10095        // issues updated since, which is every issue a comment could have been written or
10096        // edited on since. Every other task read is a question about the whole board and is
10097        // answered by reading it.
10098        let (held, membership) = match (&query.project, query.commented_since) {
10099            (ProjectFilter::Is(project), _) => (
10100                self.project_children(project).await?,
10101                // Answered by where these items came from; see `task_matches`.
10102                &ProjectFilter::Any,
10103            ),
10104            (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
10105                match (self.narrowed(query).await?, since) {
10106                    (Some(narrowed), _) => (narrowed, &query.project),
10107                    (None, Some(since)) => (self.updated_since(since).await?, &query.project),
10108                    (None, None) => (self.board().await?.items, &query.project),
10109                }
10110            }
10111        };
10112        // Filtered before paged: a page of a filtered result is a page of the survivors,
10113        // never the survivors of a page.
10114        let mut tasks = Vec::new();
10115        for item in held
10116            .iter()
10117            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
10118        {
10119            let task = item.task()?;
10120            if task_matches(&task, query, membership)
10121                && self.commented_since(item, query.commented_since).await?
10122            {
10123                tasks.push(task);
10124            }
10125        }
10126        Ok(offset_page(
10127            tasks,
10128            numeric_cursor(page.cursor.as_ref())?,
10129            page.limit.min(MAX_PAGE_SIZE) as usize,
10130        ))
10131    }
10132    async fn query_projects(
10133        &self,
10134        query: &ProjectQuery,
10135        page: &PageRequest,
10136    ) -> Result<Page<Project>, SourceError> {
10137        validate_page(page)?;
10138        refuse_unsearchable_text(query.text.as_ref())?;
10139        // The projects a board holds are found by an issue search scoped to that board,
10140        // never by walking the board's own item connection: what tells a project from a
10141        // task is the `parent` each issue carries, which costs nothing to read. A query
10142        // carrying a text asks that search for the text too, so it reads the issues that
10143        // hold it rather than every issue of the board.
10144        let held = match self.text_searched(query.text.as_ref()).await? {
10145            Some(searched) => searched,
10146            None => self.board_issues().await?,
10147        };
10148        let projects = held
10149            .iter()
10150            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
10151            .map(Resolved::project)
10152            .filter(|project| project_matches(project, query))
10153            .collect();
10154        Ok(offset_page(
10155            projects,
10156            numeric_cursor(page.cursor.as_ref())?,
10157            page.limit.min(MAX_PAGE_SIZE) as usize,
10158        ))
10159    }
10160    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
10161        Ok(self
10162            .item_by_id(id)
10163            .await?
10164            .filter(|item| item.kind == BoardKind::Document)
10165            .map(|item| item.document()))
10166    }
10167    async fn query_documents(
10168        &self,
10169        query: &DocumentQuery,
10170        page: &PageRequest,
10171    ) -> Result<Page<Document>, SourceError> {
10172        validate_page(page)?;
10173        // Narrowed to one project, this is the same sub-issue read a task list scoped to
10174        // that project makes — a document filed under a project is a sub-issue of it too,
10175        // and which of them come back is the kind this caller asked for. Unscoped, a query
10176        // carrying a text asks the board-scoped issue search for it, as a task query does,
10177        // and only one carrying none reads the board.
10178        let (held, membership) = match &query.project {
10179            ProjectFilter::Is(project) => (
10180                self.project_children(project).await?,
10181                // Answered by where these items came from; see `task_matches`.
10182                &ProjectFilter::Any,
10183            ),
10184            ProjectFilter::Any | ProjectFilter::Orphans => {
10185                refuse_unsearchable_text(query.text.as_ref())?;
10186                match self.text_searched(query.text.as_ref()).await? {
10187                    Some(searched) => (searched, &query.project),
10188                    None => (self.board().await?.items, &query.project),
10189                }
10190            }
10191        };
10192        // Filtered before paged, exactly as a task read is: a page of a filtered result is
10193        // a page of the survivors, never the survivors of a page.
10194        let documents = held
10195            .iter()
10196            .filter(|item| item.kind == BoardKind::Document)
10197            .map(Resolved::document)
10198            .filter(|document| document_matches(document, query, membership))
10199            .collect();
10200        Ok(offset_page(
10201            documents,
10202            numeric_cursor(page.cursor.as_ref())?,
10203            page.limit.min(MAX_PAGE_SIZE) as usize,
10204        ))
10205    }
10206    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
10207        validate_page(page)?;
10208        let offset = numeric_cursor(page.cursor.as_ref())?;
10209        let mut labels = self
10210            .board()
10211            .await?
10212            .items
10213            .into_iter()
10214            .flat_map(|item| item.labels)
10215            .fold(Vec::new(), |mut all, label| {
10216                if !all.iter().any(|x: &Label| x.id == label.id) {
10217                    all.push(label);
10218                }
10219                all
10220            });
10221        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
10222        Ok(offset_page(
10223            labels,
10224            offset,
10225            page.limit.min(MAX_PAGE_SIZE) as usize,
10226        ))
10227    }
10228    async fn task_dependencies(
10229        &self,
10230        id: &NativeId,
10231        direction: Direction,
10232        page: &PageRequest,
10233    ) -> Result<Page<DependencyEdge>, SourceError> {
10234        self.dependencies(id, ItemKind::Task, direction, page).await
10235    }
10236    async fn project_dependencies(
10237        &self,
10238        id: &NativeId,
10239        direction: Direction,
10240        page: &PageRequest,
10241    ) -> Result<Page<DependencyEdge>, SourceError> {
10242        self.dependencies(id, ItemKind::Project, direction, page)
10243            .await
10244    }
10245
10246    fn writes(&self) -> WriteSupport {
10247        WriteSupport::Supported
10248    }
10249
10250    /// Create or update one task.
10251    ///
10252    /// Its `delivers` and `delivered_by` are checked before anything is read or written —
10253    /// neither may name the task itself or name one task twice — and land in the body's
10254    /// metadata slot under their reserved keys, in place of any caller metadata of those
10255    /// names.
10256    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
10257        self.write_task_assets(write, None)
10258            .await
10259            .map(|written| written.id)
10260    }
10261
10262    async fn write_task_with_assets(
10263        &self,
10264        write: &ItemWrite<Task>,
10265        _answers: Option<&BTreeMap<String, Value>>,
10266        assets: &onetaskgraph_plugin_api::AssetWrite,
10267    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
10268        self.write_task_assets(write, Some(assets)).await
10269    }
10270
10271    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
10272        self.write_item(
10273            &Incoming {
10274                written: Written::Work(ItemKind::Project, &write.item.status),
10275                title: &write.item.title,
10276                content: write.item.content.as_deref(),
10277                assets: None,
10278                labels: &write.item.labels,
10279                metadata: &write.item.metadata,
10280                repositories: &write.item.repositories,
10281                classification: write.item.classification,
10282                parent: None,
10283                delivers: &[],
10284                delivered_by: &[],
10285                priority: None,
10286            },
10287            write.target.as_ref(),
10288            &write.depends_on,
10289        )
10290        .await
10291        .map(|written| written.id)
10292    }
10293
10294    /// Create or update one document, which is one issue titled the way this board spells
10295    /// a document.
10296    ///
10297    /// Everything else is exactly a task write: caller metadata goes to the same canonical
10298    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
10299    /// or a field this board cannot carry is refused by name rather than dropped, a target
10300    /// naming an issue this board does not hold is refused rather than created, and an
10301    /// issue this call created is taken back when the rest of the write fails.
10302    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
10303        self.write_document_assets(write, None)
10304            .await
10305            .map(|written| written.id)
10306    }
10307
10308    async fn write_document_with_assets(
10309        &self,
10310        write: &ItemWrite<Document>,
10311        _answers: Option<&BTreeMap<String, Value>>,
10312        assets: &onetaskgraph_plugin_api::AssetWrite,
10313    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
10314        self.write_document_assets(write, Some(assets)).await
10315    }
10316
10317    /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
10318    /// which reads nothing; then the board's `Status` option. Over an existing item that is
10319    /// read off the item, as the write reads it, and the item is held among this command's
10320    /// resolved records so the write that follows reuses that read rather than repeating it;
10321    /// an item that does not carry the field takes the board's fields, which are held once
10322    /// read. A create is checked against the board's fields only when this command already
10323    /// holds them, because a create reads them together with its repository, in one request,
10324    /// and refuses a missing option before it writes anything.
10325    async fn check_status_write(
10326        &self,
10327        kind: ItemKind,
10328        category: StatusCategory,
10329        target: Option<&NativeId>,
10330    ) -> Result<(), SourceError> {
10331        let status = self.resolved_target(kind, category)?;
10332        if status.option().is_none() {
10333            return Ok(());
10334        }
10335        let fields = match target {
10336            Some(target) => {
10337                // A target this board does not hold is the write's own refusal to make.
10338                let Some(item) = self.bound_item(target).await? else {
10339                    return Ok(());
10340                };
10341                self.resolved_cache()?.insert(target.clone(), item.clone());
10342                self.fields_for(Some(&item), true, false).await?.fields
10343            }
10344            None => {
10345                let held = self
10346                    .board_cache()?
10347                    .as_ref()
10348                    .map(|board| board.fields.clone());
10349                match held.or_else(|| {
10350                    self.fields_cache()
10351                        .ok()
10352                        .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
10353                }) {
10354                    Some(fields) => fields,
10355                    None => return Ok(()),
10356                }
10357            }
10358        };
10359        self.column_for(&fields, kind, category, &status)
10360            .map(|_| ())
10361    }
10362
10363    /// Set one task's status alone.
10364    ///
10365    /// An open target reopens a closed issue with an `updateIssue` carrying only its
10366    /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
10367    /// terminal target selects its mapped option, then closes with its fixed reason. No
10368    /// request carries a title, a body or a label. The status
10369    /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
10370    /// what a re-read reports.
10371    async fn set_task_status(
10372        &self,
10373        id: &NativeId,
10374        category: StatusCategory,
10375    ) -> Result<Option<Status>, SourceError> {
10376        self.set_status(id, category).await
10377    }
10378
10379    /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
10380    /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
10381    /// for `none`. Refused by an instance with no `priority_mapping`.
10382    async fn set_task_priority(
10383        &self,
10384        id: &NativeId,
10385        priority: Priority,
10386    ) -> Result<Option<Priority>, SourceError> {
10387        self.set_priority(id, priority).await
10388    }
10389
10390    /// Replace one task's content with a single body update that keeps the metadata slot
10391    /// byte for byte.
10392    async fn set_task_content(
10393        &self,
10394        id: &NativeId,
10395        content: &str,
10396    ) -> Result<Option<()>, SourceError> {
10397        self.replace_content(id, content).await
10398    }
10399
10400    /// Replace one task issue's content and its provenance slot entry with a single body
10401    /// update. The answers are not kept: see `replace_rendering`.
10402    async fn set_task_rendering(
10403        &self,
10404        id: &NativeId,
10405        content: &str,
10406        provenance: &Value,
10407        _answers: &BTreeMap<String, Value>,
10408    ) -> Result<Option<()>, SourceError> {
10409        self.replace_rendering(
10410            id,
10411            BoardKind::Work(ItemKind::Task),
10412            content,
10413            provenance,
10414            None,
10415        )
10416        .await
10417        .map(|written| written.map(|_| ()))
10418    }
10419
10420    /// Replace one design-document issue's content and its provenance slot entry, on exactly
10421    /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
10422    async fn set_document_rendering(
10423        &self,
10424        id: &NativeId,
10425        content: &str,
10426        provenance: &Value,
10427        _answers: &BTreeMap<String, Value>,
10428    ) -> Result<Option<()>, SourceError> {
10429        self.replace_rendering(id, BoardKind::Document, content, provenance, None)
10430            .await
10431            .map(|written| written.map(|_| ()))
10432    }
10433
10434    /// Replace one project issue's content and its provenance slot entry, on exactly the
10435    /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
10436    async fn set_project_rendering(
10437        &self,
10438        id: &NativeId,
10439        content: &str,
10440        provenance: &Value,
10441        _answers: &BTreeMap<String, Value>,
10442    ) -> Result<Option<()>, SourceError> {
10443        self.replace_rendering(
10444            id,
10445            BoardKind::Work(ItemKind::Project),
10446            content,
10447            provenance,
10448            None,
10449        )
10450        .await
10451        .map(|written| written.map(|_| ()))
10452    }
10453
10454    /// Apply a targeted update with one read of the item and a write only for what differs:
10455    /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
10456    /// and last one `updateIssue` for title, body and state. See `targeted_update`.
10457    async fn update_task(
10458        &self,
10459        id: &NativeId,
10460        update: &TaskUpdate,
10461    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
10462        self.targeted_update(id, update).await
10463    }
10464
10465    /// Replace one task's `delivered_by` with a single body update that changes the
10466    /// metadata slot and nothing outside it.
10467    async fn set_delivered_by(
10468        &self,
10469        id: &NativeId,
10470        delivered_by: &[TaskRef],
10471    ) -> Result<Option<()>, SourceError> {
10472        self.replace_delivered_by(id, delivered_by).await
10473    }
10474
10475    /// Set one key of one task issue's metadata with a single body update that changes the
10476    /// metadata slot and nothing outside it — no title, label, state or board field request —
10477    /// and sends nothing when the task already holds that value under the key.
10478    async fn set_task_metadata(
10479        &self,
10480        id: &NativeId,
10481        key: &MetadataKey,
10482        value: &Value,
10483    ) -> Result<Option<Task>, SourceError> {
10484        Ok(self
10485            .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
10486            .await?
10487            .map(|item| item.task())
10488            .transpose()?)
10489    }
10490
10491    /// Set one key of one project issue's metadata, on exactly the terms of
10492    /// [`set_task_metadata`](TaskSource::set_task_metadata).
10493    async fn set_project_metadata(
10494        &self,
10495        id: &NativeId,
10496        key: &MetadataKey,
10497        value: &Value,
10498    ) -> Result<Option<Project>, SourceError> {
10499        Ok(self
10500            .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
10501            .await?
10502            .map(|item| item.project()))
10503    }
10504
10505    /// Set one key of one design-document issue's metadata, on exactly the terms of
10506    /// [`set_task_metadata`](TaskSource::set_task_metadata).
10507    async fn set_document_metadata(
10508        &self,
10509        id: &NativeId,
10510        key: &MetadataKey,
10511        value: &Value,
10512    ) -> Result<Option<Document>, SourceError> {
10513        Ok(self
10514            .set_slot_key(id, BoardKind::Document, key, value)
10515            .await?
10516            .map(|item| item.document()))
10517    }
10518
10519    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
10520        self.delete_item(id).await
10521    }
10522
10523    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
10524        self.delete_item(id).await
10525    }
10526
10527    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
10528        self.delete_item(id).await
10529    }
10530
10531    /// One page of the task issue's own comments, walked by GitHub's own cursor.
10532    ///
10533    /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
10534    /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
10535    ///
10536    /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
10537    /// board is the read of its comments. A draft this process already resolved is refused
10538    /// without one.
10539    async fn task_comments(
10540        &self,
10541        task: &NativeId,
10542        page: &PageRequest,
10543    ) -> Result<Option<Page<Comment>>, SourceError> {
10544        validate_page(page)?;
10545        let cached = self.resolved_cache()?.get(task).cloned();
10546        if let Some(item) = cached {
10547            if item.kind != BoardKind::Work(ItemKind::Task) {
10548                return Ok(None);
10549            }
10550            if item.content_kind == ContentKind::DraftIssue {
10551                return Err(self.draft_has_no_comments(task));
10552            }
10553        }
10554        match self.issue_detail(task, page).await? {
10555            Some(TaskDetailRead {
10556                comments: Some(comments),
10557                ..
10558            }) => comments,
10559            _ => Ok(None),
10560        }
10561    }
10562
10563    /// Every id's task, with the first page of its comments when `comments` names it:
10564    /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
10565    /// comments in one [`graphql::ISSUE_DETAIL`] request.
10566    async fn get_task_details(
10567        &self,
10568        ids: &[NativeId],
10569        comments: Option<&PageRequest>,
10570    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10571        if let Some(page) = comments
10572            && let Err(error) = validate_page(page)
10573        {
10574            return ids.iter().map(|_| Err(error.clone())).collect();
10575        }
10576        match (ids, comments) {
10577            ([id], Some(page)) => vec![self.issue_detail(id, page).await],
10578            ([id], None) => vec![self.task_read(id).await],
10579            _ => self.issue_details(ids, comments).await,
10580        }
10581    }
10582
10583    /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
10584    /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
10585    /// request, so the dependency read a copy makes of each next is answered from this one.
10586    async fn get_task_details_for_copy(
10587        &self,
10588        ids: &[NativeId],
10589        comments: &PageRequest,
10590    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10591        // A page past the first is a page of one task's comments, which the detail read of it
10592        // alone answers.
10593        if comments.cursor.is_some() {
10594            return self.get_task_details(ids, Some(comments)).await;
10595        }
10596        if let Err(error) = validate_page(comments) {
10597            return ids.iter().map(|_| Err(error.clone())).collect();
10598        }
10599        let mut read = Vec::with_capacity(ids.len());
10600        for batch in ids.chunks(COPY_DETAIL_BATCH) {
10601            match self
10602                .graphql(
10603                    graphql::ISSUE_COPY_DETAILS,
10604                    copy_detail_batch(batch, comments),
10605                )
10606                .await
10607            {
10608                Ok(data) => {
10609                    for (slot, id) in batch.iter().enumerate() {
10610                        let read_one = match data.get(format!("i{slot}")) {
10611                            Some(node) => self.detail_of(id, node, true, None).await,
10612                            None => Err(SourceError::Malformed {
10613                                message: format!(
10614                                    "GitHub answered a batch read with no item for {}",
10615                                    id.0
10616                                ),
10617                            }),
10618                        };
10619                        read.push(read_one);
10620                    }
10621                }
10622                Err(error) if unresolvable_node(&error) => {
10623                    for id in batch {
10624                        read.push(self.issue_detail(id, comments).await);
10625                    }
10626                }
10627                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
10628            }
10629        }
10630        read
10631    }
10632
10633    /// Add one comment to the task's issue, as the account the token belongs to.
10634    ///
10635    /// The author is refused before anything is sent — not even the task is read — because
10636    /// no answer GitHub could give would make posting under another name than the one asked
10637    /// for the right outcome.
10638    async fn add_comment(
10639        &self,
10640        task: &NativeId,
10641        comment: &NewComment,
10642    ) -> Result<Option<Comment>, SourceError> {
10643        if let Some(author) = &comment.author {
10644            return Err(SourceError::Refused {
10645                message: format!(
10646                    "source {} cannot post a comment as {author:?}: GitHub records the account \
10647                     the token signs in as the author of every comment; next: leave --author \
10648                     out, and the comment is posted as that account",
10649                    self.name
10650                ),
10651            });
10652        }
10653        let Some(issue) = self.commented_issue(task).await? else {
10654            return Ok(None);
10655        };
10656        let data = self
10657            .graphql(
10658                graphql::ADD_COMMENT,
10659                json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
10660            )
10661            .await?;
10662        let subject = data
10663            .pointer("/addComment/subject")
10664            .filter(|value| !value.is_null())
10665            .ok_or_else(|| SourceError::Malformed {
10666                message: "GitHub comment addition returned no subject".into(),
10667            })?;
10668        if required_str(subject, "id")? != issue.0 {
10669            return Err(SourceError::Malformed {
10670                message: "GitHub comment addition answered about another issue".into(),
10671            });
10672        }
10673        let added = data
10674            .pointer("/addComment/commentEdge/node")
10675            .filter(|value| !value.is_null())
10676            .ok_or_else(|| SourceError::Malformed {
10677                message: "GitHub comment addition returned no comment".into(),
10678            })?;
10679        let added = comment_from(added)?;
10680        self.remember_commented(&issue)?;
10681        Ok(Some(added))
10682    }
10683
10684    async fn edit_comment(
10685        &self,
10686        task: &NativeId,
10687        comment: &NativeId,
10688        body: &CommentBody,
10689    ) -> Result<Option<Comment>, SourceError> {
10690        let Some(issue) = self.commented_issue(task).await? else {
10691            return Ok(None);
10692        };
10693        let Some(held) = self.comment_on(&issue, comment).await? else {
10694            return Ok(None);
10695        };
10696        // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
10697        let body = CommentOrigin::kept(&held, body)?;
10698        let data = self
10699            .graphql(
10700                graphql::UPDATE_COMMENT,
10701                json!({"input":{"id":comment.0,"body":body.as_str()}}),
10702            )
10703            .await?;
10704        let edited = data
10705            .pointer("/updateIssueComment/issueComment")
10706            .filter(|value| !value.is_null())
10707            .ok_or_else(|| SourceError::Malformed {
10708                message: "GitHub comment update returned no comment".into(),
10709            })?;
10710        let edited = comment_from(edited)?;
10711        if edited.id != *comment {
10712            return Err(SourceError::Malformed {
10713                message: "GitHub comment update returned the wrong comment".into(),
10714            });
10715        }
10716        self.remember_commented(&issue)?;
10717        Ok(Some(edited))
10718    }
10719
10720    async fn delete_comment(
10721        &self,
10722        task: &NativeId,
10723        comment: &NativeId,
10724    ) -> Result<Option<NativeId>, SourceError> {
10725        let Some(issue) = self.commented_issue(task).await? else {
10726            return Ok(None);
10727        };
10728        if !self.comment_is_on(&issue, comment).await? {
10729            return Ok(None);
10730        }
10731        let data = self
10732            .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
10733            .await?;
10734        // The payload says nothing about the comment it removed, so what is checked is that
10735        // GitHub answered the mutation at all rather than leaving it unanswered.
10736        data.get("deleteIssueComment")
10737            .filter(|value| !value.is_null())
10738            .ok_or_else(|| SourceError::Malformed {
10739                message: "GitHub comment deletion returned no payload".into(),
10740            })?;
10741        Ok(Some(comment.clone()))
10742    }
10743
10744    /// Every request this source has recorded, and what each of GitHub's two budgets was
10745    /// attributed — read off the same accounting the session report is rendered from, so
10746    /// the two cannot count one request two ways.
10747    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
10748        Ok(Some(self.ledger.snapshot().metering()))
10749    }
10750
10751    /// Drop every item, search answer and board read this source holds, so the next command
10752    /// reads the board as a person has since left it.
10753    ///
10754    /// Every one of those is held on the assumption that nothing but this source writes the
10755    /// board while a command runs, which stops being true the moment the command is over: a
10756    /// body a person edited would be overwritten from the record held here, and a card they
10757    /// moved would be read as still where this source left it. The board's own field
10758    /// definitions go too, because a person can add or delete a `Status` option and a write
10759    /// resolved against the held list would not re-read on a miss. What stays is what stays
10760    /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
10761    /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
10762    /// accounting [`metering`](TaskSource::metering) answers from.
10763    ///
10764    /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
10765    /// refused, because clearing it is what puts it right.
10766    async fn end_command(&self) -> Result<(), SourceError> {
10767        fn clear<T: Default>(held: &Mutex<T>) {
10768            *held
10769                .lock()
10770                .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
10771            held.clear_poison();
10772        }
10773        clear(&self.created);
10774        clear(&self.updated);
10775        clear(&self.commented);
10776        clear(&self.board_cache);
10777        clear(&self.search_cache);
10778        clear(&self.narrowed_cache);
10779        clear(&self.search_next);
10780        clear(&self.resolved_cache);
10781        clear(&self.children_cache);
10782        clear(&self.fields_cache);
10783        Ok(())
10784    }
10785}
10786
10787/// Each project's sub-issues as one command read them, keyed by the selector they were asked
10788/// for under, beside the project that selector named; see `children_cache`.
10789type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
10790
10791/// One issue comment as the contract carries it.
10792///
10793/// `author` is absent both when GitHub answers `null` for an account that no longer exists
10794/// and when it answers an actor with no login, because either way the source did not say who
10795/// wrote it — which is what an absent author means, rather than an author called nothing.
10796///
10797/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
10798/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
10799/// and is read back with the marker taken out and reported as its origin.
10800fn comment_from(value: &Value) -> Result<Comment, SourceError> {
10801    Ok(Comment {
10802        origin: None,
10803        id: NativeId(required_str(value, "id")?.to_owned()),
10804        author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
10805            .map(str::to_owned),
10806        created_at: optional_time(value, "createdAt")?,
10807        updated_at: optional_time(value, "updatedAt")?,
10808        body: required_str(value, "body")?.to_owned(),
10809        url: optional_str(value, "url")?.map(str::to_owned),
10810    }
10811    .unmarked())
10812}
10813
10814/// The page of comments one issue node carries, resumed from `after`.
10815fn comment_page(
10816    node: &Value,
10817    issue: &str,
10818    after: Option<&str>,
10819) -> Result<Page<Comment>, SourceError> {
10820    let connection = node
10821        .get("comments")
10822        .filter(|value| !value.is_null())
10823        .ok_or_else(|| SourceError::Malformed {
10824            message: format!("GitHub issue {issue} answered with no comments connection"),
10825        })?;
10826    let items = optional_nodes(Some(connection), "issue comments")?
10827        .into_iter()
10828        .flatten()
10829        .map(comment_from)
10830        .collect::<Result<Vec<_>, _>>()?;
10831    let next = next_cursor(connection)?;
10832    if let Some(next) = &next {
10833        validate_cursor_progress(after, &next.0)?;
10834    }
10835    Ok(Page { items, next })
10836}
10837
10838/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
10839/// end — `None` when it carried none, or a page with more past it.
10840fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
10841    let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
10842        return Ok(None);
10843    };
10844    if next_cursor(connection)?.is_some() {
10845        return Ok(None);
10846    }
10847    Ok(Some(
10848        optional_nodes(Some(connection), "blocked-by issues")?
10849            .into_iter()
10850            .flatten()
10851            .cloned()
10852            .collect(),
10853    ))
10854}
10855
10856/// Where the recorded tail of a dependency walk resumes; see
10857/// [`GitHubProjectsSource::recorded_edges`].
10858const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
10859
10860/// The board text field this source keeps a copy's origin in.
10861///
10862/// Named after the key it holds, and held to that name by the guard below rather than by
10863/// a reader noticing.
10864const ORIGIN_FIELD: &str = "onetaskgraph.origin";
10865
10866/// The metadata key that field holds.
10867///
10868/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
10869/// constructs or interprets the qualified id it carries. This source names it only to
10870/// route it — a short, typed value belongs in a typed field rather than in the body slot
10871/// a caller's own prose shares.
10872///
10873/// Restated rather than imported, because no plugin crate may depend on the engine. What
10874/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
10875/// target in `check`: it reads the engine's own literal and fails naming the file and the
10876/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
10877/// that creates a second item every run instead of finding the one it wrote — and that is
10878/// too late to learn it.
10879const ORIGIN_KEY: &str = "onetaskgraph.origin";
10880
10881/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
10882///
10883/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
10884/// is derived from the far end, never written down on the near item — so only a forward
10885/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
10886/// it did not come from, and it is told so rather than answered with an empty page that
10887/// reads as a walk which ended.
10888fn recorded_offset(
10889    cursor: Option<&str>,
10890    direction: Direction,
10891) -> Result<Option<usize>, SourceError> {
10892    cursor
10893        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
10894        .map(|offset| {
10895            if direction != Direction::DependsOn {
10896                return Err(SourceError::Config {
10897                    message: format!(
10898                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
10899                         reverse dependency read never issues; resume it in the direction \
10900                         that reported it"
10901                    ),
10902                });
10903            }
10904            offset.parse().map_err(|_| SourceError::Config {
10905                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
10906            })
10907        })
10908        .transpose()
10909}
10910
10911fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
10912    let mut page = offset_page(edges, offset, limit.max(1));
10913    page.next = page
10914        .next
10915        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
10916    page
10917}
10918
10919/// The kind of one issue reached through a dependency connection.
10920///
10921/// The same questions the board scan asks, over the fields the dependency document
10922/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
10923/// then anything with sub-issues or the marker is a project.
10924///
10925/// # Errors
10926///
10927/// A far end this board holds as a document is refused rather than reported. The two
10928/// answers that are not refusals would both be wrong: reporting it as a task names an id
10929/// no task read of this source can find, and reporting it as a project names one no
10930/// project read can. There is no third value to return — `ItemKind` has no document
10931/// variant, because nothing may point at a document — so the relationship itself is what
10932/// the person is told about.
10933fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
10934    let id = required_str(value, "id")?;
10935    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
10936        return Err(SourceError::Refused {
10937            message: format!(
10938                "GitHub issue {id} is a document of this board — its title begins \
10939                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
10940                 on by one; next: remove that issue's blocking relationship on this board"
10941            ),
10942        });
10943    }
10944    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
10945    if parent.is_some() {
10946        return Ok(ItemKind::Task);
10947    }
10948    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
10949    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
10950        message: format!("GitHub issue {id}: {message}"),
10951    })?;
10952    let sub_issues = sub_issue_total(value)?;
10953    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
10954        ItemKind::Project
10955    } else {
10956        ItemKind::Task
10957    })
10958}
10959
10960/// The `IssueStateUpdateInput` one status target asks for.
10961///
10962/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
10963/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
10964/// a currently-closed issue: without that the item would read back `Unknown` and a copy
10965/// would report a change forever. A document has no status at all, and asks for neither.
10966fn state_input(target: Option<&StatusTarget>) -> Value {
10967    match target {
10968        Some(StatusTarget::Terminal(_, reason)) => {
10969            json!({"value":"CLOSED","stateReason":reason.reason()})
10970        }
10971        Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
10972        // A document has no status, so a write of one says nothing about the issue's open
10973        // or closed state rather than forcing it open: `stateInput` is what carries that
10974        // instruction, and an explicit null asks for no change to it.
10975        None => Value::Null,
10976    }
10977}
10978
10979/// The metadata one write stores in the item's body slot.
10980///
10981/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
10982/// rather than carried: the kind marker so an empty project stays readable, the
10983/// repository list only when it is not exactly the issue's own repository, and the far
10984/// ends no relationship here can name.
10985///
10986/// The copy origin is the one typed field that is also mirrored here, and only as a
10987/// mirror: it lands in the board's origin field as well, which stays the one every reader
10988/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
10989/// and catches up with a write in seconds rather than minutes — can find the item by it.
10990/// A reader of the release before this one drops the slot's copy and reads the field, so an
10991/// item written here still reads with exactly one origin there.
10992fn slot_metadata(
10993    incoming: &Incoming<'_>,
10994    own_repository: Option<&Repository>,
10995    fallback: &[DependencyEdge],
10996) -> BTreeMap<String, Value> {
10997    let mut metadata = incoming.metadata.clone();
10998    match metadata.remove(ORIGIN_KEY) {
10999        Some(Value::String(origin)) if !origin.is_empty() => {
11000            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
11001        }
11002        _ => {}
11003    }
11004    match incoming.written.kind() {
11005        BoardKind::Work(kind) => metadata.insert(
11006            ItemKind::METADATA_KEY.to_owned(),
11007            Value::String(kind.marker().to_owned()),
11008        ),
11009        // A document is told by its title, so it carries no kind marker: that key names
11010        // what a dependency endpoint points at, and nothing may point at a document.
11011        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
11012    };
11013    incoming.classification.record(&mut metadata);
11014    let derivable = own_repository
11015        .map(|own| incoming.repositories == [own.clone()])
11016        .unwrap_or(incoming.repositories.is_empty());
11017    if derivable {
11018        metadata.remove(Repository::METADATA_KEY);
11019    } else {
11020        metadata.insert(
11021            Repository::METADATA_KEY.to_owned(),
11022            Value::Array(
11023                incoming
11024                    .repositories
11025                    .iter()
11026                    .map(|repository| Value::String(repository.as_str().to_owned()))
11027                    .collect(),
11028            ),
11029        );
11030    }
11031    // The typed lists are what land, whatever the caller's own metadata held under their
11032    // keys: a key of either name travelling beside the field would otherwise be a second
11033    // answer to the same question, and the field is the one the contract names.
11034    for (key, entries) in [
11035        (TaskRef::DELIVERS_KEY, incoming.delivers),
11036        (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
11037    ] {
11038        set_task_list(&mut metadata, key, entries);
11039    }
11040    record_edges(&mut metadata, fallback);
11041    metadata
11042}
11043
11044/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
11045/// one slot's metadata, or no such key when there are none.
11046fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
11047    if fallback.is_empty() {
11048        metadata.remove(DependencyEdge::RECORDED_KEY);
11049    } else {
11050        metadata.insert(
11051            DependencyEdge::RECORDED_KEY.to_owned(),
11052            Value::Array(
11053                fallback
11054                    .iter()
11055                    .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
11056                    .collect(),
11057            ),
11058        );
11059    }
11060}
11061
11062/// Every label one item carries, from its content's own connection and nowhere else.
11063///
11064/// There is no second place to read one from: no document this source sends selects the
11065/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
11066/// cannot carry one at all. The module documentation records the three schema facts that
11067/// settle it.
11068fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
11069    optional_nodes(content.get("labels"), "content labels")?
11070        .into_iter()
11071        .flatten()
11072        .map(|v| {
11073            Ok(Label {
11074                id: NativeId(required_str(v, "id")?.to_owned()),
11075                name: required_str(v, "name")?.to_owned(),
11076                color: optional_str(v, "color")?.map(str::to_owned),
11077            })
11078        })
11079        .collect()
11080}
11081
11082/// The definition of each board field one item's values are values of, in the shape a read
11083/// of the board's own `fields` gives one.
11084///
11085/// A value names its field through a fragment on that field's own type, so the type is
11086/// known from which kind of value it is: a single-select value's field is a
11087/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
11088/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
11089fn field_definitions(field_values: &[Value]) -> Vec<Value> {
11090    field_values
11091        .iter()
11092        .filter_map(|value| {
11093            let field = value.get("field")?.as_object()?;
11094            field.get("id")?.as_str().filter(|id| !id.is_empty())?;
11095            let typename = if value.get("text").is_some() {
11096                "ProjectV2Field"
11097            } else if value.get("name").is_some() {
11098                "ProjectV2SingleSelectField"
11099            } else {
11100                return None;
11101            };
11102            let mut defined = field.clone();
11103            defined.insert("__typename".to_owned(), json!(typename));
11104            // Only a text field holds a text value, so the value says the field's type too.
11105            if typename == "ProjectV2Field" {
11106                defined.insert("dataType".to_owned(), json!("TEXT"));
11107            }
11108            Some(Value::Object(defined))
11109        })
11110        .collect()
11111}
11112
11113fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
11114    let Some(node) = field_values
11115        .iter()
11116        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
11117    else {
11118        return Ok(None);
11119    };
11120    Ok(optional_str(node, "text")?.map(str::to_owned))
11121}
11122
11123fn valid_github_owner(owner: &str) -> bool {
11124    !owner.is_empty()
11125        && owner.len() <= 39
11126        && !owner.starts_with('-')
11127        && !owner.ends_with('-')
11128        && !owner.contains("--")
11129        && owner
11130            .bytes()
11131            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
11132}
11133
11134/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
11135/// neither of the two names a path segment already means.
11136fn valid_github_repository_name(name: &str) -> bool {
11137    !name.is_empty()
11138        && name.len() <= 100
11139        && name != "."
11140        && name != ".."
11141        && name
11142            .bytes()
11143            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
11144}
11145
11146fn valid_environment_name(name: &str) -> bool {
11147    let mut bytes = name.bytes();
11148    bytes
11149        .next()
11150        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
11151        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
11152}
11153
11154/// How many sub-issues one issue has.
11155///
11156/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
11157/// absent or non-integer one is a response this source cannot read — and reading it as
11158/// zero would classify a project as a task, which is exactly the mistake the marker
11159/// exists to keep from happening quietly.
11160fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
11161    let summary = issue
11162        .get("subIssuesSummary")
11163        .ok_or_else(|| SourceError::Malformed {
11164            message: "GitHub issue is missing subIssuesSummary".into(),
11165        })?;
11166    summary
11167        .get("total")
11168        .and_then(Value::as_u64)
11169        .ok_or_else(|| SourceError::Malformed {
11170            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
11171        })
11172}
11173
11174/// One issue's own `number`.
11175///
11176/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
11177/// an issue in this module asks for it. So a read of one that comes back without it, or
11178/// with something that is not an unsigned integer, is a response this source cannot read —
11179/// absence here is **not** "this issue has no number". A draft is the content that has
11180/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
11181/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
11182fn issue_number(issue: &Value) -> Result<u64, SourceError> {
11183    issue
11184        .get("number")
11185        .and_then(Value::as_u64)
11186        .ok_or_else(|| SourceError::Malformed {
11187            message: "GitHub issue number is missing or is not an unsigned integer".into(),
11188        })
11189}
11190
11191/// The `number` a creating mutation answered with, and `None` when it answered without one;
11192/// why a missing one is tolerated is at the call in `create_and_file_issue`.
11193fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
11194    match created.get("number") {
11195        None | Some(Value::Null) => Ok(None),
11196        Some(value) => value
11197            .as_u64()
11198            .map(Some)
11199            .ok_or_else(|| SourceError::Malformed {
11200                message: "GitHub created issue number is not an unsigned integer".into(),
11201            }),
11202    }
11203}
11204
11205fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
11206    value
11207        .get(field)
11208        .and_then(Value::as_str)
11209        .ok_or_else(|| SourceError::Malformed {
11210            message: format!("GitHub response is missing string field {field}"),
11211        })
11212}
11213
11214fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
11215    let found = required_str(value, field)?;
11216    if found.trim().is_empty() {
11217        return Err(SourceError::Malformed {
11218            message: format!("GitHub response has blank string field {field}"),
11219        });
11220    }
11221    Ok(found)
11222}
11223
11224/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
11225/// needs one — Linear spells them too, in its own description field.
11226///
11227/// Restated rather than shared, because a plugin crate depends on the contract crate and
11228/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
11229/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
11230/// source round-trips its own writes perfectly well under its own spelling.
11231const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
11232const METADATA_CLOSE: &str = "\n-->";
11233
11234/// What the composer puts between a non-empty visible body and the slot, and the one thing
11235/// the parser takes off the visible body when it takes the slot off — exactly once, so every
11236/// other trailing byte of the body comes back as it was written.
11237// 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.
11238const METADATA_SEPARATOR: &str = "\n\n";
11239
11240/// The visible body and the metadata slot at the end of it.
11241///
11242/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
11243/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
11244/// own content and is left alone. The visible body is everything before the slot less the
11245/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
11246fn metadata_body(
11247    body: Option<String>,
11248) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
11249    let Some(body) = body else {
11250        return Ok((None, BTreeMap::new()));
11251    };
11252    let Some(slot) = slot_span(&body)? else {
11253        return Ok((Some(body), BTreeMap::new()));
11254    };
11255    let metadata =
11256        serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
11257            SourceError::Malformed {
11258                message: format!(
11259                    "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
11260                ),
11261            }
11262        })?;
11263    let before = &body[..slot.start];
11264    let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
11265    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
11266}
11267
11268/// Where the metadata slot sits in one body, as byte offsets into it.
11269struct SlotSpan {
11270    /// Where [`METADATA_OPEN`] begins.
11271    start: usize,
11272    /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
11273    encoded_start: usize,
11274    /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
11275    encoded_end: usize,
11276    /// Just past [`METADATA_CLOSE`].
11277    end: usize,
11278}
11279
11280/// The slot at the very end of `body`, or `None` when it has none.
11281///
11282/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
11283/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
11284/// slot.
11285fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
11286    let Some(start) = body.rfind(METADATA_OPEN) else {
11287        return Ok(None);
11288    };
11289    let encoded_start = start + METADATA_OPEN.len();
11290    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
11291        return Err(SourceError::Malformed {
11292            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
11293        });
11294    };
11295    let encoded_end = encoded_start + relative_end;
11296    let end = encoded_end + METADATA_CLOSE.len();
11297    if !body[end..].trim().is_empty() {
11298        return Ok(None);
11299    }
11300    Ok(Some(SlotSpan {
11301        start,
11302        encoded_start,
11303        encoded_end,
11304        end,
11305    }))
11306}
11307
11308/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
11309/// slot as it was.
11310///
11311/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
11312/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
11313/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
11314/// or alone in an empty body — and a body with no slot that is given no metadata is
11315/// returned as it is.
11316fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
11317    let encoded = if metadata.is_empty() {
11318        None
11319    } else {
11320        Some(
11321            serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
11322                message: error.to_string(),
11323            })?,
11324        )
11325    };
11326    Ok(match (slot_span(body)?, encoded) {
11327        (Some(slot), Some(encoded)) => format!(
11328            "{}{encoded}{}",
11329            &body[..slot.encoded_start],
11330            &body[slot.encoded_end..]
11331        ),
11332        (Some(slot), None) => {
11333            let before = &body[..slot.start];
11334            format!(
11335                "{}{}",
11336                before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
11337                &body[slot.end..]
11338            )
11339        }
11340        (None, None) => body.to_owned(),
11341        (None, Some(encoded)) if body.is_empty() => {
11342            format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11343        }
11344        (None, Some(encoded)) => {
11345            format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11346        }
11347    })
11348}
11349
11350/// `body` with everything before its metadata slot replaced by `content`, and the slot
11351/// itself kept byte for byte.
11352///
11353/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
11354/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
11355/// `content` is empty — so a read of the result reports `content` as the visible body and
11356/// the slot's metadata exactly as it was.
11357fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
11358    let Some(slot) = slot_span(body)? else {
11359        return Ok(content.to_owned());
11360    };
11361    let kept = &body[slot.start..];
11362    Ok(if content.is_empty() {
11363        kept.to_owned()
11364    } else {
11365        format!("{content}{METADATA_SEPARATOR}{kept}")
11366    })
11367}
11368
11369/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
11370fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
11371    if entries.is_empty() {
11372        metadata.remove(key);
11373    } else {
11374        metadata.insert(
11375            key.to_owned(),
11376            Value::Array(
11377                entries
11378                    .iter()
11379                    .map(|entry| Value::String(entry.as_str().to_owned()))
11380                    .collect(),
11381            ),
11382        );
11383    }
11384}
11385
11386fn compose_body(
11387    content: Option<&str>,
11388    metadata: &BTreeMap<String, Value>,
11389) -> Result<Option<String>, SourceError> {
11390    let visible = content.unwrap_or_default();
11391    if metadata.is_empty() {
11392        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
11393    }
11394    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
11395        message: error.to_string(),
11396    })?;
11397    Ok(Some(if visible.is_empty() {
11398        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11399    } else {
11400        format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11401    }))
11402}
11403
11404fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
11405    value
11406        .get(field)
11407        .and_then(Value::as_bool)
11408        .ok_or_else(|| SourceError::Malformed {
11409            message: format!("GitHub response is missing boolean field {field}"),
11410        })
11411}
11412fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
11413    match value.get(field) {
11414        None | Some(Value::Null) => Ok(None),
11415        Some(value) => value
11416            .as_str()
11417            .map(Some)
11418            .ok_or_else(|| SourceError::Malformed {
11419                message: format!("GitHub response field {field} is not a string or null"),
11420            }),
11421    }
11422}
11423fn optional_nodes<'a>(
11424    connection: Option<&'a Value>,
11425    name: &str,
11426) -> Result<Option<&'a Vec<Value>>, SourceError> {
11427    match connection {
11428        None | Some(Value::Null) => Ok(None),
11429        Some(value) => value
11430            .get("nodes")
11431            .and_then(Value::as_array)
11432            .map(Some)
11433            .ok_or_else(|| SourceError::Malformed {
11434                message: format!("GitHub {name}.nodes is not an array"),
11435            }),
11436    }
11437}
11438fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
11439    let page_info = connection
11440        .get("pageInfo")
11441        .ok_or_else(|| SourceError::Malformed {
11442            message: format!("GitHub {name} has no pageInfo"),
11443        })?;
11444    if required_bool(page_info, "hasNextPage")? {
11445        return Err(SourceError::Malformed {
11446            message: format!(
11447                "GitHub {name} exceeds the supported nested connection size of {size}"
11448            ),
11449        });
11450    }
11451    Ok(())
11452}
11453fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
11454    optional_str(value, field)?
11455        .map(|timestamp| {
11456            timestamp.parse().map_err(|error| SourceError::Malformed {
11457                message: format!("GitHub response field {field} is not a timestamp: {error}"),
11458            })
11459        })
11460        .transpose()
11461}
11462fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
11463    if page.limit == 0 {
11464        Err(SourceError::Config {
11465            message: "page limit must be at least 1".into(),
11466        })
11467    } else {
11468        Ok(())
11469    }
11470}
11471fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
11472    let page = connection
11473        .get("pageInfo")
11474        .filter(|value| value.is_object())
11475        .ok_or_else(|| SourceError::Malformed {
11476            message: "GitHub connection is missing pageInfo".into(),
11477        })?;
11478    if required_bool(page, "hasNextPage")? {
11479        let cursor = required_str(page, "endCursor")?;
11480        validate_cursor_progress(None, cursor)?;
11481        Ok(Some(Cursor(cursor.into())))
11482    } else {
11483        Ok(None)
11484    }
11485}
11486fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
11487    if next.is_empty() || previous == Some(next) {
11488        Err(SourceError::Malformed {
11489            message: "GitHub pagination cursor is empty or did not advance".into(),
11490        })
11491    } else {
11492        Ok(())
11493    }
11494}
11495/// The version of this plugin's opaque narrowing-search cursor.
11496pub const SEARCH_CURSOR_VERSION: u32 = 4;
11497
11498#[derive(Serialize, Deserialize)]
11499#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
11500enum SearchConnection {
11501    Initial {},
11502    Continuing { after: Cursor },
11503    Exhausted {},
11504}
11505impl SearchConnection {
11506    fn after(&self) -> Option<&str> {
11507        match self {
11508            Self::Continuing { after } => Some(&after.0),
11509            _ => None,
11510        }
11511    }
11512    fn exhausted(&self) -> bool {
11513        matches!(self, Self::Exhausted { .. })
11514    }
11515    /// Whether a cursor naming this position, `offset` rows into its page, is one this
11516    /// plugin could have handed out: a page is resumed only part of the way through it — an
11517    /// offset of a whole page or more would skip rows nobody was given — an initial page
11518    /// only once some of it was handed out, and an exhausted connection has no page to be
11519    /// part of the way through.
11520    fn valid_resume(&self, offset: usize) -> bool {
11521        let within = offset < SEARCH_PAGE_SIZE as usize;
11522        match self {
11523            Self::Initial { .. } => offset > 0 && within,
11524            Self::Continuing { after } => !after.0.is_empty() && within,
11525            Self::Exhausted { .. } => offset == 0,
11526        }
11527    }
11528}
11529
11530/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
11531#[derive(Serialize, Deserialize)]
11532#[serde(deny_unknown_fields)]
11533struct SearchPosition {
11534    version: u32,
11535    connection: SearchConnection,
11536    /// How many rows of the page `connection` starts were already handed out.
11537    #[serde(default, skip_serializing_if = "is_zero")]
11538    offset: usize,
11539    #[serde(default, skip_serializing_if = "Vec::is_empty")]
11540    seen: Vec<NativeId>,
11541    #[serde(default, skip_serializing_if = "Vec::is_empty")]
11542    own: Vec<NativeId>,
11543}
11544impl Default for SearchPosition {
11545    fn default() -> Self {
11546        Self {
11547            version: SEARCH_CURSOR_VERSION,
11548            connection: SearchConnection::Initial {},
11549            offset: 0,
11550            seen: Vec::new(),
11551            own: Vec::new(),
11552        }
11553    }
11554}
11555
11556fn is_zero(offset: &usize) -> bool {
11557    *offset == 0
11558}
11559
11560fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
11561    cursor.map_or(Ok(0), |c| {
11562        c.0.parse().map_err(|_| SourceError::Config {
11563            message: "page cursor is invalid".into(),
11564        })
11565    })
11566}
11567fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
11568    if offset > items.len() {
11569        return Page::last(vec![]);
11570    }
11571    let tail = items.split_off(offset);
11572    let mut selected = tail;
11573    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
11574    selected.truncate(limit);
11575    Page {
11576        items: selected,
11577        next,
11578    }
11579}
11580
11581impl GitHubProjectsSource {
11582    async fn write_task_assets(
11583        &self,
11584        write: &ItemWrite<Task>,
11585        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
11586    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
11587        let near = write.target.as_ref().unwrap_or(&write.item.id);
11588        for (key, entries) in [
11589            (TaskRef::DELIVERS_KEY, &write.item.delivers),
11590            (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
11591        ] {
11592            TaskRef::listed(key, near, Some(&self.name), entries.clone())
11593                .map_err(|message| SourceError::Refused { message })?;
11594        }
11595        if self.priorities.is_none() && write.item.priority != Priority::None {
11596            return Err(self.holds_no_priority());
11597        }
11598        self.write_item(
11599            &Incoming {
11600                written: Written::Work(ItemKind::Task, &write.item.status),
11601                title: &write.item.title,
11602                content: write.item.content.as_deref(),
11603                assets,
11604                labels: &write.item.labels,
11605                metadata: &write.item.metadata,
11606                repositories: &write.item.repositories,
11607                classification: write.item.classification,
11608                parent: write.item.project.as_ref(),
11609                delivers: &write.item.delivers,
11610                delivered_by: &write.item.delivered_by,
11611                priority: self.priorities.as_ref().map(|_| write.item.priority),
11612            },
11613            write.target.as_ref(),
11614            &write.depends_on,
11615        )
11616        .await
11617    }
11618    async fn write_document_assets(
11619        &self,
11620        write: &ItemWrite<Document>,
11621        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
11622    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
11623        // A document takes part in no dependency graph, so there is no far end to write
11624        // natively and none to record: a caller naming one is told so rather than having it
11625        // stored under the reserved key, where a later read would report an edge the
11626        // contract says cannot exist.
11627        if !write.depends_on.is_empty() {
11628            return Err(SourceError::Refused {
11629                message: format!(
11630                    "this write names {} dependencies for a document, and a document takes \
11631                     part in no dependency graph; next: put the dependency on the task or \
11632                     project the document is about",
11633                    write.depends_on.len()
11634                ),
11635            });
11636        }
11637        self.write_item(
11638            &Incoming {
11639                written: Written::Document,
11640                title: &write.item.title,
11641                content: write.item.content.as_deref(),
11642                assets,
11643                labels: &write.item.labels,
11644                metadata: &write.item.metadata,
11645                repositories: &write.item.repositories,
11646                classification: write.item.classification,
11647                parent: write.item.project.as_ref(),
11648                delivers: &[],
11649                delivered_by: &[],
11650                priority: None,
11651            },
11652            write.target.as_ref(),
11653            &[],
11654        )
11655        .await
11656    }
11657}
11658
11659/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
11660/// itself, for the two things no journey can observe.
11661///
11662/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
11663/// with and without the call, that a settlement, a board listing and a metadata search each
11664/// read afresh after it — the resolved records, the written-item overlay, the board and its
11665/// search, and the narrowed searches. What they cannot reach is the held field definitions,
11666/// because a status write naming an option a person deleted is refused the same whether or
11667/// not the list is held, and a poisoned lock, because nothing outside the source can panic
11668/// while one of its locks is held. So these assert those directly, and every other holder
11669/// beside them so a holder added later without a clear in the call fails here.
11670#[cfg(test)]
11671mod end_command_tests {
11672    use super::*;
11673
11674    struct Token;
11675
11676    impl SecretResolver for Token {
11677        fn get(&self, var: &str) -> Option<SecretString> {
11678            (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
11679        }
11680    }
11681
11682    fn source() -> GitHubProjectsSource {
11683        let config = serde_json::from_value(json!({
11684            "owner": "octo-org", "project_number": 7, "repository": "acme/work",
11685            // Nothing here is sent: the source is only built and its state inspected.
11686            "endpoint": "http://127.0.0.1:9/graphql",
11687        }))
11688        .expect("a usable configuration");
11689        GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
11690            .expect("the source builds")
11691    }
11692
11693    /// One issue as a board read answers it.
11694    fn resolved(source: &GitHubProjectsSource) -> Resolved {
11695        source
11696            .resolve(&json!({
11697                "id": "ITEM-1",
11698                "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
11699                            "body": "what a person may since have edited", "state": "OPEN",
11700                            "stateReason": null, "url": null, "number": 1,
11701                            "subIssuesSummary": {"total": 0},
11702                            "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
11703                "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
11704            }))
11705            .expect("the item reads")
11706            .expect("an issue")
11707    }
11708
11709    /// Hold something in every holder the call clears, and the repository id it keeps.
11710    fn fill(source: &GitHubProjectsSource) {
11711        let item = resolved(source);
11712        source.created.lock().unwrap().push(item.clone());
11713        source.updated.lock().unwrap().push(item.clone());
11714        *source.board_cache.lock().unwrap() = Some(Board {
11715            id: "PVT-board".into(),
11716            fields: json!({"nodes": []}),
11717            items: vec![item.clone()],
11718        });
11719        *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
11720        source
11721            .narrowed_cache
11722            .lock()
11723            .unwrap()
11724            .insert("status:todo".into(), vec![item.clone()]);
11725        source.children_cache.lock().unwrap().insert(
11726            NativeId("P-1".into()),
11727            (NativeId("P-1".into()), vec![item.clone()]),
11728        );
11729        source
11730            .search_next
11731            .lock()
11732            .unwrap()
11733            .insert("status:todo".into(), Some("cursor".into()));
11734        source
11735            .resolved_cache
11736            .lock()
11737            .unwrap()
11738            .insert(item.id.clone(), item);
11739        *source.fields_cache.lock().unwrap() = Some(BoardFields {
11740            id: BoardId::parse("PVT-board").unwrap(),
11741            fields: json!({"nodes": []}),
11742        });
11743        source
11744            .repository_cache
11745            .lock()
11746            .unwrap()
11747            .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
11748    }
11749
11750    fn assert_dropped(source: &GitHubProjectsSource) {
11751        assert!(source.created().unwrap().is_empty(), "created");
11752        assert!(source.updated().unwrap().is_empty(), "updated");
11753        assert!(source.board_cache().unwrap().is_none(), "board");
11754        assert!(source.search_cache.lock().unwrap().is_none(), "search");
11755        assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
11756        assert!(
11757            source.children_cache.lock().unwrap().is_empty(),
11758            "project children"
11759        );
11760        assert!(
11761            source.search_next.lock().unwrap().is_empty(),
11762            "search paging"
11763        );
11764        assert!(
11765            source.resolved_cache().unwrap().is_empty(),
11766            "resolved records"
11767        );
11768        assert!(source.fields_cache().unwrap().is_none(), "board fields");
11769        assert_eq!(
11770            source.repository_cache().unwrap().len(),
11771            1,
11772            "a repository's node id stays valid and is kept"
11773        );
11774    }
11775
11776    fn end(source: &GitHubProjectsSource) {
11777        tokio::runtime::Builder::new_current_thread()
11778            .build()
11779            .unwrap()
11780            .block_on(source.end_command())
11781            .expect("the command ends");
11782    }
11783
11784    #[test]
11785    fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
11786        let source = source();
11787        fill(&source);
11788        end(&source);
11789        assert_dropped(&source);
11790    }
11791
11792    #[test]
11793    fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
11794        fn poison<T: Send>(held: &Mutex<T>) {
11795            std::thread::scope(|scope| {
11796                let _ = scope
11797                    .spawn(|| {
11798                        let _guard = held.lock().unwrap();
11799                        panic!("a failure while the lock is held");
11800                    })
11801                    .join();
11802            });
11803            assert!(held.is_poisoned());
11804        }
11805        let source = source();
11806        fill(&source);
11807        poison(&source.created);
11808        poison(&source.updated);
11809        poison(&source.board_cache);
11810        poison(&source.search_cache);
11811        poison(&source.narrowed_cache);
11812        poison(&source.search_next);
11813        poison(&source.resolved_cache);
11814        poison(&source.fields_cache);
11815        assert!(
11816            source.resolved_cache().is_err(),
11817            "a poisoned lock is refused before the call"
11818        );
11819        end(&source);
11820        assert_dropped(&source);
11821    }
11822}
11823
11824#[cfg(test)]
11825mod unprojectable_fields_tests {
11826    use super::*;
11827
11828    /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
11829    fn documented() -> std::collections::BTreeSet<String> {
11830        let document = std::fs::read_to_string(
11831            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
11832        )
11833        .expect("docs/metadata.md is readable");
11834        let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
11835        let lead = "a `field` equal, ignoring case, to ";
11836        let start = document.find(lead).expect("the refused-field sentence") + lead.len();
11837        let sentence = &document[start..];
11838        let sentence = &sentence[..sentence
11839            .find("; and two entries naming one field")
11840            .expect("the refused-field sentence ends at the duplicate rule")];
11841        let (own, github) = sentence
11842            .split_once(" or a field GitHub owns on every board (")
11843            .expect("the sentence names GitHub's own fields");
11844        own.split('`')
11845            .skip(1)
11846            .step_by(2)
11847            .chain(
11848                github
11849                    .strip_suffix(')')
11850                    .expect("GitHub's own fields close the sentence")
11851                    .split(", "),
11852            )
11853            .map(str::to_owned)
11854            .collect()
11855    }
11856
11857    #[test]
11858    fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
11859        let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
11860            .iter()
11861            .map(|&name| name.to_owned())
11862            .collect();
11863        assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
11864        let instance = SourceName::new("work").unwrap();
11865        for name in UNPROJECTABLE_FIELDS {
11866            for spelled in [name.to_owned(), name.to_uppercase()] {
11867                let entry = MetadataFieldConfig {
11868                    field: spelled.clone(),
11869                    key: "team.machine".to_owned(),
11870                    path: Vec::new(),
11871                };
11872                let refused = MetadataField::resolve(vec![entry], &instance)
11873                    .expect_err("an unprojectable field is refused");
11874                assert!(
11875                    refused.to_string().contains(&format!("{name:?}")),
11876                    "{spelled}: {refused}"
11877                );
11878            }
11879        }
11880    }
11881}