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, Coverage, Cursor,
632    DependencyEdge, DependencyEndpoint, DependencyKind, DependencySupport, Direction, Document,
633    DocumentQuery, DocumentVersionCoverage, Health, ItemKind, ItemWrite, Label, LabelFilter,
634    Location, MetadataKey, Metering, NativeId, NewComment, Page, PageRequest, Priority, Project,
635    ProjectFilter, ProjectQuery, ProjectVersionCoverage, Repository, SecretResolver, SharedClock,
636    SourceError, SourceName, SourcePlugin, Status, StatusCategory, StatusMapping, Support, Task,
637    TaskDetailRead, TaskQuery, TaskRef, TaskSource, TaskUpdate, TaskUpdateOutcome, TextFields,
638    TextQuery, UnmappedStatus, UpdatedField, VersionCoverage, WriteJournal, WriteSupport,
639    system_clock,
640};
641use reqwest::{Client, StatusCode, Url};
642use schemars::{Schema, schema_for};
643use secrecy::{ExposeSecret, SecretString};
644use serde::{Deserialize, Serialize};
645use serde_json::{Value, json};
646
647pub mod accounting;
648mod assets;
649pub mod change_token;
650mod expected;
651pub use expected::{CONTEXT_VERSION, WHOLE_MEMBERS, context_shapes};
652mod visibility;
653
654use accounting::Accounting;
655
656/// The registry name for this plugin.
657pub const KIND: &str = "github-projects";
658/// GitHub's maximum connection page size.
659pub const MAX_PAGE_SIZE: u32 = 100;
660/// Every page of a board-scoped narrowing search: 20 rows, one point of SEARCH_ISSUES, the
661/// most one point buys. GitHub prices that document by rows, so pages of 20 cost what pages
662/// of 100 cost per row, and a page of fewer than 20 costs the same one point.
663pub const SEARCH_PAGE_SIZE: u32 = 20;
664/// How many items one [`graphql::ISSUE_DETAILS`] request reads, each with the first page of
665/// its comments: the largest batch the node-count model prices at one point.
666///
667/// Each aliased item is resolved once, and what GitHub charges for it is the connections
668/// under it — its labels, its page of board memberships, the field values of each of those
669/// three memberships, and its comments: six requests' worth of the aggregate GitHub divides
670/// by a hundred and rounds. Twenty-four items come to 144, which rounds to one point;
671/// twenty-five come to 150, which rounds to two. `tests/point_cost.rs` prices the document at
672/// one point and fails if one item more would still be priced at one.
673pub const DETAIL_BATCH: usize = 24;
674/// How many items one [`graphql::ISSUE_COPY_DETAILS`] request reads: each with the first page
675/// of its comments and its `blockedBy`, the largest batch the node-count model prices at one
676/// point.
677///
678/// The six requests' worth an item of [`DETAIL_BATCH`] costs, and its `blockedBy` connection
679/// besides: seven. Twenty-one items come to 147, which rounds to one point; twenty-two come to
680/// 154, which rounds to two. `tests/point_cost.rs` prices the document at one point and fails
681/// if one item more would still be priced at one.
682pub const COPY_DETAIL_BATCH: usize = 21;
683
684/// The most nodes any one document this source sends may be asked to return.
685///
686/// GitHub's own published per-query ceiling, taken from
687/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
688/// workspace cannot hold a stale copy of somebody else's number. A query above it is
689/// **refused before it is executed**, whoever is asking and whatever board they are
690/// asking about — so this is a bound on the documents rather than a budget that runs out.
691///
692/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
693/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
694/// everything the credential does — two numbers against two limits, and this constant
695/// bounds only the first. The second is computed offline too, per document:
696/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
697/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
698/// lane. There is no constant like this one to hold a price under, because points are an
699/// hourly allowance rather than a per-call bound.
700///
701/// Neither is a session's price. What `session-cost.md` records of a whole session is its
702/// **requests** and its **worst-case nodes**; what a whole session spends in points is
703/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
704/// [`accounting`]. The module section on the three ways this source reaches an item says how
705/// the count is arrived at, and which of the page sizes below decide it.
706pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
707
708/// Nested connection size for the connections that hang off one item.
709///
710/// It multiplies through every document that reaches an item under a page — the count
711/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
712/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
713/// every document under these constants and fails naming any that reaches the limit, so
714/// raising this is caught there rather than by GitHub.
715const NESTED_PAGE_SIZE: u32 = 50;
716/// How many of one issue's board memberships are read when an issue is reached directly.
717///
718/// An issue reached through a search or through its own node id carries its board half in
719/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
720/// under a page of issues, so every point of it multiplies through the whole document and
721/// is paid for whether or not any issue is on a second board — which is why it is
722/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
723///
724/// **Three, because what a page misses is now recovered rather than refused**, and the
725/// recovery is what the value is chosen against. An issue whose entry for this board sits
726/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
727/// that page's own cursor — so the value trades a bound every read pays for a request only
728/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
729/// boards would pay that request *per issue*, which is order N against the one page per
730/// hundred issues a read costs today. At three it is only reached by an issue on four or
731/// more boards at once, which keeps the recovery path exceptional rather than routine for
732/// a plausible deployment.
733const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
734/// How many carriers of one copy origin one page of [`graphql::ORIGIN_LOOKUP`] asks each of
735/// its two connections for.
736///
737/// An origin names one item, so the answer an origin lookup expects is one carrier, and a
738/// second is a duplicate a copy already takes the first of. Both connections are walked to
739/// exhaustion whatever this is, so it decides how many requests an unusual answer costs and
740/// never what the answer is. It is small because every point of it is paid on every lookup,
741/// and a copy makes one lookup per item it has no link for: at three, ten lookups cost fewer
742/// worst-case nodes than the one whole-board read they replaced.
743const ORIGIN_PAGE_SIZE: u32 = 3;
744
745pub use github_graphql_node_count::{NodeCountError, Variables};
746
747/// The largest value this source can bind to each page-size variable its documents name.
748///
749/// Every `first:` in [`graphql`] reads one of these four, and each is capped at the
750/// constant above it wherever a caller's own limit could reach it — `$first` at
751/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
752/// `BOARD_ITEMS_PAGE_SIZE`, `$originFirst` at `ORIGIN_PAGE_SIZE`. So this is the worst case a caller can drive this source to,
753/// not one configuration of it, which is what makes a bound computed under it a bound on
754/// every read.
755pub fn largest_page_sizes() -> Variables {
756    Variables::from([
757        ("first".to_owned(), MAX_PAGE_SIZE),
758        ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
759        ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
760        ("originFirst".to_owned(), ORIGIN_PAGE_SIZE),
761    ])
762}
763
764/// The most nodes `document` could be asked to return, by GitHub's published rules.
765///
766/// Computed offline from the document's own text under [`largest_page_sizes`] — no
767/// network, no credential and no schema — by
768/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
769/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
770/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
771///
772/// # Errors
773///
774/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
775/// no single operation, or binds a page size this source does not name — each of which is
776/// a defect in the document rather than a number.
777pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
778    node_count(document, &largest_page_sizes())
779}
780
781/// The most rate-limit points one call of `document` could spend, by GitHub's published
782/// rules.
783///
784/// Computed offline from the document's own text under [`largest_page_sizes`] — no
785/// network, no credential and no schema — by
786/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
787/// This is `cost`, metered **per hour** against the allowance one credential shares across
788/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
789/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
790/// under, so what `tests/point_cost.rs` does with it is pin every document in
791/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
792/// figures against GitHub's own reported `cost`.
793///
794/// # Errors
795///
796/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
797/// no single operation, or binds a page size this source does not name — each of which is
798/// a defect in the document rather than a number.
799pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
800    github_graphql_node_count::point_cost(document, &largest_page_sizes())
801}
802
803/// The most nodes `document` could be asked to return under `variables`.
804///
805/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
806/// [`accounting`] is this under the bindings one request really sent — one spelling of the
807/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
808/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
809///
810/// # Errors
811///
812/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
813/// no single operation, or binds a page size `variables` does not name.
814pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
815    github_graphql_node_count::node_count(document, variables)
816}
817
818/// The issue-title prefix that makes a board issue a document.
819///
820/// A GitHub Projects board has no document type — it holds issues — so the discriminator
821/// is the title, and this is the whole of it: an issue whose title begins with these bytes
822/// is a document and every other issue is the task or project the sub-issue rule makes it.
823///
824/// It is spelled **once**, here, and read rather than restated everywhere else — including
825/// by the shared journeys, which take it from this constant so a board fixture cannot
826/// drift from what this source reads. `docs/metadata.md` records the two consequences that
827/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
828/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
829/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
830pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
831
832/// Exact GraphQL query documents issued by this plugin.
833///
834/// Keeping the production documents here lets the pinned-schema test validate the same
835/// bytes that are sent to GitHub, rather than a test-only copy which could drift
836/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
837/// field, and its guarded caller always supplies the complete existing option set with ids.
838pub mod graphql {
839    /// The board half of one item: the field values every document here reads it from.
840    ///
841    /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
842    /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
843    /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
844    /// *the same value*, because
845    /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
846    /// one path. Three spellings of it is what would drift, so there is one.
847    ///
848    /// The `Status` option and this source's own origin text field are the whole of it. It
849    /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
850    /// content, so it holds nothing the content's own `labels` do not already say, and it
851    /// would sit a label connection two page sizes deep.
852    macro_rules! board_item_values {
853        () => {
854            r#"fieldValues(first:$nestedFirst){nodes{
855          ... on ProjectV2ItemFieldSingleSelectValue{name field{
856            ... on ProjectV2SingleSelectField{id name options{id name}}
857          }}
858          ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
859        }pageInfo{hasNextPage}}"#
860        };
861    }
862
863    /// The variable every document reading an issue declares for the relationship events its
864    /// change token counts, with its value as the default, so no caller binds it.
865    ///
866    /// A variable rather than a literal list because every argument a production document
867    /// passes is a declared variable, which `tests/schema.rs` holds them to, and every member of
868    /// its default to the pinned enum. Its default is the six event types GitHub records a
869    /// relationship added or removed with: a dependency on either end, as `blockedBy` and
870    /// `blocking`, so one a person makes from the far end moves this end's count too; and a
871    /// project membership on the task, as its parent, which GitHub records there whichever
872    /// issue the move was made from. A sub-issue's events on its parent are not counted: a
873    /// project carries no task token, and a copy writes nothing of a task its own sub-issues
874    /// decide. [`super::change_token`] says why the count is in the token.
875    macro_rules! relationship_events_variable {
876        () => {
877            ",$relationshipEvents:[IssueTimelineItemsItemType!]=[BLOCKED_BY_ADDED_EVENT,BLOCKED_BY_REMOVED_EVENT,BLOCKING_ADDED_EVENT,BLOCKING_REMOVED_EVENT,PARENT_ISSUE_ADDED_EVENT,PARENT_ISSUE_REMOVED_EVENT]"
878        };
879    }
880
881    /// Everything this source reads about one issue, wherever it reaches that issue.
882    ///
883    /// A macro rather than a constant so the three documents below can `concat!` it: one
884    /// spelling of these fields is what makes an issue read through the board-scoped
885    /// search, through its own node id, and through its project's sub-issue relationship
886    /// resolve to *the same* item, which is the whole of what
887    /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
888    ///
889    /// `projectItems` is what carries the board half of an issue: the board item's own id
890    /// and the [`board_item_values!`] above — the `Status` option and this source's origin
891    /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
892    /// issue rather than on the board, which is what makes the cost of a read proportional
893    /// to what was asked for instead of to the board's size.
894    ///
895    /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
896    /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
897    /// not on that page: a page here is where the search for the entry starts rather than
898    /// where it ends.
899    ///
900    /// It does **not** select the board's `Labels` field value, and that is the whole of
901    /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
902    /// a label connection there sits under `fieldValues` under `projectItems` under a page
903    /// of issues, spending `$nestedFirst` twice down one path, and took
904    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
905    /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
906    /// above, and that connection is where every label this source reports comes from. No
907    /// document in this module selects the board field any longer, [`BOARD`] included; the
908    /// module documentation records why nothing it could have held is lost.
909    macro_rules! board_issue {
910        () => {
911            concat!(
912                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}
913      labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
914      projectItems(first:$boardItems){nodes{id updatedAt project{id number}
915        "#,
916                board_item_values!(),
917                r#"}pageInfo{hasNextPage endCursor}}}"#
918            )
919        };
920    }
921
922    /// Every issue of one board, found by a search scoped to that board.
923    ///
924    /// This is how the projects a board holds are listed, and it selects no `items`
925    /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
926    /// container walked page by page, so nothing nested inside a board item is paid for.
927    /// Which of the issues it returns is a project is then read off `parent` — GitHub
928    /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
929    /// discriminator has to be applied to the field, which is a scalar on the issue and
930    /// costs nothing.
931    pub const SEARCH_ISSUES: &str = concat!(
932        r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
933        relationship_events_variable!(),
934        r#"){
935      search(query:$search,type:$type,first:$first,after:$after){
936        pageInfo{hasNextPage endCursor}
937        nodes{__typename ...BoardIssue}
938      }
939    }"#,
940        board_issue!()
941    );
942
943    /// What a dependency read selects of each far end: enough to say which kind of item it
944    /// is, its body included for the kind marker.
945    macro_rules! related_issue {
946        () => {
947            " fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"
948        };
949    }
950
951    /// One issue by its own node id, which is what a qualified id names here — with what a
952    /// write of it needs and the issue does not carry in `board_issue!`: the field
953    /// definitions of the boards it sits on, and the far ends of its `blockedBy`.
954    ///
955    /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
956    /// answers a write made moments ago with the value from before it, and resolving a node
957    /// id does not.
958    ///
959    /// **Why those two ride here and not on the fragment.** A copy or an update of an item
960    /// reads it by its own id, and with them that one read answers everything the write
961    /// needs: which option ids the board's `Status` and `Priority` fields hold — so no
962    /// [`BOARD_FIELDS`] — and which issues block it, with each one's kind — so no
963    /// [`ISSUE_DEPENDENCIES`]. On `board_issue!` they would sit under the hundred-issue
964    /// pages of [`SEARCH_ISSUES`] and [`SUB_ISSUES`], multiplying both documents' price. Here
965    /// they sit under one item, and this read is still one point.
966    pub const ISSUE: &str = concat!(
967        r#"query($id:ID!,$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
968        relationship_events_variable!(),
969        r#"){
970      node(id:$id){__typename ...BoardIssue ... on Issue{
971        boards:projectItems(first:$boardItems){nodes{project{id number fields(first:$nestedFirst){nodes{
972          ... on ProjectV2SingleSelectField{__typename id name options{id name}}
973          ... on ProjectV2Field{__typename id name dataType}
974        }pageInfo{hasNextPage}}}}}
975        blockedBy(first:$first){nodes{...Related}pageInfo{hasNextPage endCursor}}
976      }}
977    }"#,
978        board_issue!(),
979        related_issue!()
980    );
981
982    /// One project's tasks: the sub-issues of the issue that project is, each with a page of
983    /// what blocks it.
984    ///
985    /// The work this costs is the project's own size. Nothing about it grows as the board
986    /// gains projects, or as those projects gain tasks.
987    ///
988    /// **Why `blockedBy` rides here and on no other page of issues.** What reads a project's
989    /// tasks reads their edges next — `project graph` draws them, a copy carries them — and
990    /// without them here that is one [`ISSUE_DEPENDENCIES`] per task, so the requests a graph
991    /// costs grow with its tasks rather than with the pages of them. Carried here, a task
992    /// blocked by no more than `$nestedFirst` issues answers its forward edges from this read,
993    /// exactly as an [`ISSUE`] read of it does, and only one blocked by more is asked again.
994    /// It adds a connection under each issue of the page — one rate-limit point per page,
995    /// and `$nestedFirst` nodes per issue — and is kept off [`SEARCH_ISSUES`], whose pages
996    /// answer questions that never read an edge.
997    pub const SUB_ISSUES: &str = concat!(
998        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
999        relationship_events_variable!(),
1000        r#"){
1001      node(id:$id){__typename
1002        ... on Issue{subIssues(first:$first,after:$after){
1003          pageInfo{hasNextPage endCursor}
1004          nodes{__typename ...BoardIssue ... on Issue{
1005            blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}
1006          }}
1007        }}}
1008    }"#,
1009        board_issue!(),
1010        related_issue!()
1011    );
1012
1013    /// What a read of the board's own `items` selects of each item's content.
1014    ///
1015    /// A macro for the reason [`board_item_values!`] is one: [`BOARD`] and [`ORIGIN_LOOKUP`]
1016    /// both walk `ProjectV2.items` and hand each item to one resolver, so they select its
1017    /// content by one spelling.
1018    macro_rules! board_item_content {
1019        () => {
1020            r#" content{
1021        ... 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}}}
1022        ... on PullRequest{__typename id}
1023        ... on DraftIssue{__typename id title body createdAt updatedAt}
1024      }"#
1025        };
1026    }
1027
1028    /// Reads the board's fields and one page of its items.
1029    pub const BOARD: &str = concat!(
1030        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!"#,
1031        relationship_events_variable!(),
1032        r#"){
1033      owner:repositoryOwner(login:$owner){
1034        ... on ProjectV2Owner{projectV2(number:$number){...Board}}
1035      }
1036    } fragment Board on ProjectV2 { id title
1037      fields(first:$nestedFirst){nodes{
1038        ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1039        ... on ProjectV2Field{__typename id name dataType}
1040      }pageInfo{hasNextPage}}
1041      items(first:$first,after:$after){nodes{id updatedAt "#,
1042        board_item_values!(),
1043        board_item_content!(),
1044        r#"} pageInfo{hasNextPage endCursor}}
1045    }"#
1046    );
1047
1048    /// Every carrier of one copy origin, by two reads in one request, and nothing else of
1049    /// the board.
1050    ///
1051    /// **`originItems`** is the board's own items narrowed by its own field filter —
1052    /// `ProjectV2.items(query:)`, which GitHub's schema declares as "Search query for
1053    /// filtering items" — to those whose `onetaskgraph.origin` text field holds the
1054    /// qualified id, quoted. It reads the field every carrier already holds, whichever release
1055    /// wrote it, and matches it exactly: measured on 2026-09-29 against a 394-item board,
1056    /// the quoted, the unquoted and the bare-value spellings each returned exactly the one
1057    /// carrier and a prefix of the value returned none. It is `ProjectV2.items`, so it lags a
1058    /// fresh `addProjectV2ItemById` the way that connection does.
1059    ///
1060    /// **`search`** is the board-scoped issue search for the same id as a quoted phrase in
1061    /// the body, which is where this source mirrors the origin into its metadata slot. GitHub
1062    /// indexes that comment, and the index catches up with a write in a second or two rather
1063    /// than in minutes, so it finds a carrier another process wrote that the first read is
1064    /// still behind on.
1065    ///
1066    /// Each connection pages at `$originFirst`, its own small size — see `ORIGIN_PAGE_SIZE`
1067    /// — and resumes from its own cursor; a connection already walked to its end is resumed
1068    /// from its last cursor, which answers an empty page. Every candidate either read returns
1069    /// is confirmed against its own origin field before it is reported, so a token match of
1070    /// the search or anything else the filter admits never is.
1071    ///
1072    /// The root is aliased `originItems` rather than `owner`, so nothing counting the board's
1073    /// own whole reads counts this one among them.
1074    pub const ORIGIN_LOOKUP: &str = concat!(
1075        r#"query($owner:String!,$number:Int!,$filter:String!,$search:String!,$type:SearchType!,$originFirst:Int!,$itemsAfter:String,$searchAfter:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1076        relationship_events_variable!(),
1077        r#"){
1078      originItems:repositoryOwner(login:$owner){
1079        ... on ProjectV2Owner{projectV2(number:$number){
1080          items(first:$originFirst,after:$itemsAfter,query:$filter){nodes{id updatedAt "#,
1081        board_item_values!(),
1082        board_item_content!(),
1083        r#"} pageInfo{hasNextPage endCursor}}
1084        }}
1085      }
1086      search(query:$search,type:$type,first:$originFirst,after:$searchAfter){
1087        pageInfo{hasNextPage endCursor}
1088        nodes{__typename ...BoardIssue}
1089      }
1090    }"#,
1091        board_issue!()
1092    );
1093
1094    /// The board's own id and field definitions, and not one of its items.
1095    ///
1096    /// What a write needs of the board when the item it writes does not say: the id a field
1097    /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
1098    /// origin fields. It selects no `items`, so what it costs is the board's field list
1099    /// however many items the board holds — and it decides nothing about which items those
1100    /// are, which is the question a read of one item by its own id answers instead.
1101    ///
1102    /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
1103    /// board's item reads by their root counts this one among them.
1104    pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
1105      boardFields:repositoryOwner(login:$owner){
1106        ... on ProjectV2Owner{projectV2(number:$number){id
1107          fields(first:$nestedFirst){nodes{
1108            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1109            ... on ProjectV2Field{__typename id name dataType}
1110          }pageInfo{hasNextPage}}
1111        }}
1112      }
1113    }"#;
1114
1115    /// One board draft by its own node id, with the board item it sits in.
1116    ///
1117    /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
1118    /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
1119    /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
1120    /// issue fragment reads, so a draft reached by id resolves through the same resolver a
1121    /// board listing hands it to, and nothing has to list the board to find one.
1122    pub const DRAFT: &str = concat!(
1123        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
1124      node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
1125        projectV2Items(first:$boardItems){nodes{id project{id number}
1126        "#,
1127        board_item_values!(),
1128        r#"}pageInfo{hasNextPage endCursor}}}}
1129    }"#
1130    );
1131
1132    /// One issue's board memberships alone, walked past the page a read of it carried.
1133    ///
1134    /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
1135    /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
1136    /// boards than that page holds may have this board's entry past its end. This asks that
1137    /// one issue for its memberships and nothing else — the caller already holds the issue —
1138    /// so an answer of "this board does not hold it" is only ever given about a connection
1139    /// read to exhaustion.
1140    ///
1141    /// It selects the board item's id, its project number and the same
1142    /// [`board_item_values!`] the fragment does, because what it produces is handed to the
1143    /// very same resolver: an issue recovered this way reports the same title, the same
1144    /// status, the same labels and the same qualified id as one whose entry was on the
1145    /// page.
1146    ///
1147    /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
1148    /// multiplies through it and the membership connection can be walked at
1149    /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
1150    /// further request for any issue a person really keeps.
1151    pub const ISSUE_BOARD_ITEMS: &str = concat!(
1152        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
1153      node(id:$id){
1154        ... on Issue{projectItems(first:$first,after:$after){
1155          nodes{id updatedAt project{id number}
1156        "#,
1157        board_item_values!(),
1158        r#"}
1159          pageInfo{hasNextPage endCursor}}}
1160      }
1161    }"#
1162    );
1163    /// Whether the board's Project is public — half of what decides whether a write here can
1164    /// be read by anybody. Needs the `read:project` scope.
1165    pub const PROJECT_VISIBILITY: &str = r#"query($owner:String!,$number:Int!){visibility:repositoryOwner(login:$owner){... on ProjectV2Owner{projectV2(number:$number){public}}}}"#;
1166    /// Resolves the configured repository's node id, which creating an issue requires.
1167    pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
1168    /// What creating an issue needs and has not read yet: the board's own id and field
1169    /// definitions, as [`BOARD_FIELDS`] reads them, and the node id of the repository the
1170    /// issue is created in, as [`REPOSITORY`] reads it — in one request.
1171    ///
1172    /// Sent at the point a create knows which repository it is for, when neither half is
1173    /// already known to this process; a create needing only one of them sends that one's own
1174    /// document. Neither half is kept past the process: a field's option ids are re-minted by
1175    /// `sources fields --apply`, so a copy of them held between runs would write the wrong
1176    /// status.
1177    pub const CREATION_CONTEXT: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!,$repositoryOwner:String!,$repositoryName:String!){
1178      boardFields:repositoryOwner(login:$owner){
1179        ... on ProjectV2Owner{projectV2(number:$number){id
1180          fields(first:$nestedFirst){nodes{
1181            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
1182            ... on ProjectV2Field{__typename id name dataType}
1183          }pageInfo{hasNextPage}}
1184        }}
1185      }
1186      repository(owner:$repositoryOwner,name:$repositoryName){id nameWithOwner}
1187    }"#;
1188    /// Reads both dependency directions for one issue, with each far end's own kind — and
1189    /// the issue's own body, which is where an edge to another source is recorded, so that
1190    /// half of a dependency read needs no second read of the issue or of the board.
1191    pub const ISSUE_DEPENDENCIES: &str = concat!(
1192        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
1193      ... on Issue{body
1194        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1195        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
1196      }}}"#,
1197        related_issue!()
1198    );
1199    /// Creates one issue in the configured repository, on no board: [`ADD_TO_BOARD`] files
1200    /// it. `CreateIssueInput.projectV2Ids` is not sent — see the crate's notes on what GitHub
1201    /// answered when it was.
1202    pub const CREATE_ISSUE: &str =
1203        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
1204    /// Puts an existing issue on the configured board.
1205    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
1206    /// Updates an issue's visible fields and its open or closed state in one call.
1207    pub const UPDATE_ISSUE: &str =
1208        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
1209    /// Updates an existing draft's user-visible fields.
1210    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
1211    /// Updates a text or single-select value on one project item.
1212    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}}}}}}}}"#;
1213    /// Writes up to six board fields and clears up to three in one ordered mutation: every
1214    /// aliased field is included by its own boolean, as [`FIELD_WRITE_SLOTS`](super::FIELD_WRITE_SLOTS)
1215    /// and [`FIELD_CLEAR_SLOTS`](super::FIELD_CLEAR_SLOTS) name them, writes before clears.
1216    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}}}"#;
1217    /// Clears one project item's value of one field, which is what a `none` priority is.
1218    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}}}}}}}}"#;
1219    /// Creates one field: a single-select one with its options, or a text one for a projected
1220    /// metadata value. Only the guarded field setup may use this document, and only for a
1221    /// field the board lacks.
1222    pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
1223    /// Replaces a single-select field's options. Only the guarded field setup — the
1224    /// `status-options` and `fields` operations — may use this document, because GitHub
1225    /// treats the input as the complete option list.
1226    pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
1227    /// A fresh snapshot of the Status field and every board item's assignment.
1228    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}}}}}}"#;
1229    /// Files one issue under another as a sub-issue, which is what project membership is.
1230    pub const ADD_SUB_ISSUE: &str =
1231        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1232    /// Takes one issue back out of its parent.
1233    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
1234    /// Adds GitHub's native issue blocked-by relationship.
1235    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1236    /// Removes one native issue blocked-by relationship.
1237    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
1238    /// Deletes one issue, which takes its board item with it.
1239    ///
1240    /// The engine sends this in one situation only: undoing a copy that could not finish,
1241    /// over the items that same copy created. Deleting the issue removes the board item
1242    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
1243    pub const DELETE_ISSUE: &str =
1244        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
1245
1246    /// Everything this source reads about one issue comment, wherever it reaches one.
1247    ///
1248    /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
1249    /// and a comment just edited are handed to one mapper, so they are selected by one
1250    /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
1251    /// longer exists, and `login` is the one member every kind of actor carries.
1252    macro_rules! issue_comment {
1253        () => {
1254            "id author{login} createdAt updatedAt body url"
1255        };
1256    }
1257
1258    /// One task's comments: a page of its issue's own `comments` connection.
1259    ///
1260    /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
1261    /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
1262    /// list every time somebody edited it; left unordered the connection answers in the order
1263    /// the comments were written, which is the order GitHub documents for the same collection
1264    /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
1265    /// node count and the caller's own page size is pushed straight down.
1266    pub const ISSUE_COMMENTS: &str = concat!(
1267        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
1268        issue_comment!(),
1269        r#"}pageInfo{hasNextPage endCursor}}}}}"#
1270    );
1271    /// One issue by its own node id, with a page of its comments: what `task show` and a
1272    /// comment listing read, in one request.
1273    ///
1274    /// [`ISSUE`] and [`ISSUE_COMMENTS`] in one document, rather than one then the other. The
1275    /// comments are selected here and **not** on the shared `board_issue!` fragment, which
1276    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] nest under a page of a hundred issues: a comment
1277    /// connection there would multiply through both of those documents' price, and neither
1278    /// needs one.
1279    pub const ISSUE_DETAIL: &str = concat!(
1280        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1281        relationship_events_variable!(),
1282        r#"){
1283      node(id:$id){__typename ...BoardIssue ... on Issue{comments(first:$first,after:$after){nodes{"#,
1284        issue_comment!(),
1285        r#"}pageInfo{hasNextPage endCursor}}}}
1286    }"#,
1287        board_issue!()
1288    );
1289
1290    /// One alias of [`ISSUE_DETAILS`]: the item a batch's `$id<n>` names, with the first
1291    /// page of its comments when `$comments` asks for them.
1292    macro_rules! issue_details_alias {
1293        ($n:literal) => {
1294            concat!(
1295                "\n      i",
1296                stringify!($n),
1297                ":node(id:$id",
1298                stringify!($n),
1299                "){__typename ...BoardIssue ... on Issue{comments(first:$first) @include(if:$comments){nodes{",
1300                issue_comment!(),
1301                "}pageInfo{hasNextPage endCursor}}}}"
1302            )
1303        };
1304    }
1305
1306    /// [`ISSUE_DETAIL`] for [`DETAIL_BATCH`](super::DETAIL_BATCH) items at once, each by its
1307    /// own node id, as one fixed-size document of aliased `node(id:)` fields.
1308    ///
1309    /// **Aliased `node(id:)` rather than `nodes(ids:)`, and that is what keeps its price
1310    /// honest.** The `github-graphql-node-count` model this workspace prices with treats a
1311    /// field that supplies neither `first` nor `last` as free, and `nodes(ids:)` supplies
1312    /// neither — so every connection under it would be priced at nothing and the pin in
1313    /// `tests/point_cost.rs` would understate what GitHub charges. Each alias here is the
1314    /// one-item read the model already prices, so the batch costs what its aliases cost.
1315    ///
1316    /// **Fixed-size, so there is one document to price.** A batch of fewer items binds the
1317    /// slots it has no item for to the last item it does, and reads that item again; the
1318    /// price is the document's, whatever its variables, so a short batch costs what a full
1319    /// one does and nothing more.
1320    pub const ISSUE_DETAILS: &str = concat!(
1321        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!"#,
1322        relationship_events_variable!(),
1323        r#"){"#,
1324        issue_details_alias!(0),
1325        issue_details_alias!(1),
1326        issue_details_alias!(2),
1327        issue_details_alias!(3),
1328        issue_details_alias!(4),
1329        issue_details_alias!(5),
1330        issue_details_alias!(6),
1331        issue_details_alias!(7),
1332        issue_details_alias!(8),
1333        issue_details_alias!(9),
1334        issue_details_alias!(10),
1335        issue_details_alias!(11),
1336        issue_details_alias!(12),
1337        issue_details_alias!(13),
1338        issue_details_alias!(14),
1339        issue_details_alias!(15),
1340        issue_details_alias!(16),
1341        issue_details_alias!(17),
1342        issue_details_alias!(18),
1343        issue_details_alias!(19),
1344        issue_details_alias!(20),
1345        issue_details_alias!(21),
1346        issue_details_alias!(22),
1347        issue_details_alias!(23),
1348        "\n    }",
1349        board_issue!()
1350    );
1351
1352    /// One alias of [`ISSUE_COPY_DETAILS`]: the item a batch's `$id<n>` names, with the first
1353    /// page of its comments and the issues it is blocked by.
1354    macro_rules! issue_copy_details_alias {
1355        ($n:literal) => {
1356            concat!(
1357                "\n      i",
1358                stringify!($n),
1359                ":node(id:$id",
1360                stringify!($n),
1361                "){__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{",
1362                issue_comment!(),
1363                "}pageInfo{hasNextPage endCursor}} blockedBy(first:$nestedFirst){nodes{...Related}pageInfo{hasNextPage endCursor}}}}"
1364            )
1365        };
1366    }
1367
1368    /// [`ISSUE_DETAILS`] as a copy reads the tasks it carries: each item with the first page of
1369    /// its comments and its `blockedBy` — the edges a copy reads of every task next, which
1370    /// [`ISSUE`] carries and [`ISSUE_DETAILS`] does not — [`COPY_DETAIL_BATCH`](super::COPY_DETAIL_BATCH)
1371    /// at a time, so a copy of a changed task costs this read and nothing else.
1372    ///
1373    /// A document of its own rather than a widening of [`ISSUE_DETAILS`], whose batch size is
1374    /// what `task show-many` is priced by and which reads no edge.
1375    pub const ISSUE_COPY_DETAILS: &str = concat!(
1376        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!"#,
1377        relationship_events_variable!(),
1378        r#"){"#,
1379        issue_copy_details_alias!(0),
1380        issue_copy_details_alias!(1),
1381        issue_copy_details_alias!(2),
1382        issue_copy_details_alias!(3),
1383        issue_copy_details_alias!(4),
1384        issue_copy_details_alias!(5),
1385        issue_copy_details_alias!(6),
1386        issue_copy_details_alias!(7),
1387        issue_copy_details_alias!(8),
1388        issue_copy_details_alias!(9),
1389        issue_copy_details_alias!(10),
1390        issue_copy_details_alias!(11),
1391        issue_copy_details_alias!(12),
1392        issue_copy_details_alias!(13),
1393        issue_copy_details_alias!(14),
1394        issue_copy_details_alias!(15),
1395        issue_copy_details_alias!(16),
1396        issue_copy_details_alias!(17),
1397        issue_copy_details_alias!(18),
1398        issue_copy_details_alias!(19),
1399        issue_copy_details_alias!(20),
1400        "\n    }",
1401        board_issue!(),
1402        related_issue!()
1403    );
1404
1405    /// Which issue one comment is on, read before that comment is edited or removed.
1406    ///
1407    /// GitHub's comment mutations take the comment's id and nothing else, so without this a
1408    /// comment id given against the wrong task would change a comment on another issue.
1409    pub const COMMENT_ISSUE: &str =
1410        r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id body issue{id}}}}"#;
1411    /// Adds one comment to an issue, signed as the account the token belongs to.
1412    pub const ADD_COMMENT: &str = concat!(
1413        r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
1414        issue_comment!(),
1415        r#"}}}}"#
1416    );
1417    /// Replaces the body of one issue comment.
1418    pub const UPDATE_COMMENT: &str = concat!(
1419        r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
1420        issue_comment!(),
1421        r#"}}}"#
1422    );
1423    /// Removes one issue comment. Its payload carries nothing about the comment it removed.
1424    pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
1425
1426    /// What a mutation of a write the store's cache expects selects of the issue it wrote:
1427    /// everything [`ISSUE_DETAIL`] reads of it, so its response shows the task whole — its
1428    /// change token's every part and the first page of its comments.
1429    ///
1430    /// Sent **only** while that write runs, in place of the document named beside each
1431    /// below, so every other write's documents, prices and request counts are what they
1432    /// were. GitHub's token waits for a task to settle (`change_token::SETTLED`), and a
1433    /// mutation's response is stamped seconds after the change it made, so against GitHub
1434    /// itself a response rarely determines a version; the cache is then left unserveable
1435    /// for the task, and its next read refreshes it.
1436    macro_rules! whole_issue {
1437        () => {
1438            concat!(
1439                r#"__typename ...BoardIssue ... on Issue{comments(first:$first){nodes{"#,
1440                issue_comment!(),
1441                r#"}pageInfo{hasNextPage endCursor}}}"#
1442            )
1443        };
1444    }
1445
1446    /// The variables every document of `whole_issue!` declares beside its own.
1447    macro_rules! whole_variables {
1448        () => {
1449            concat!(
1450                r#",$first:Int!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!"#,
1451                relationship_events_variable!()
1452            )
1453        };
1454    }
1455
1456    /// [`ADD_COMMENT`], selecting the whole issue commented on.
1457    pub const ADD_COMMENT_WHOLE: &str = concat!(
1458        r#"mutation($input:AddCommentInput!"#,
1459        whole_variables!(),
1460        r#"){addComment(input:$input){subject{id "#,
1461        whole_issue!(),
1462        r#"} commentEdge{node{"#,
1463        issue_comment!(),
1464        r#"}}}}"#,
1465        board_issue!()
1466    );
1467    /// [`UPDATE_COMMENT`], selecting the whole issue the comment is on.
1468    pub const UPDATE_COMMENT_WHOLE: &str = concat!(
1469        r#"mutation($input:UpdateIssueCommentInput!"#,
1470        whole_variables!(),
1471        r#"){updateIssueComment(input:$input){issueComment{"#,
1472        issue_comment!(),
1473        r#" issue{"#,
1474        whole_issue!(),
1475        r#"}}}}"#,
1476        board_issue!()
1477    );
1478    /// [`UPDATE_ISSUE`], selecting the whole issue updated.
1479    pub const UPDATE_ISSUE_WHOLE: &str = concat!(
1480        r#"mutation($input:UpdateIssueInput!"#,
1481        whole_variables!(),
1482        r#"){updateIssue(input:$input){issue{id "#,
1483        whole_issue!(),
1484        r#"}}}"#,
1485        board_issue!()
1486    );
1487    /// [`UPDATE_FIELD`], selecting the whole issue behind the board item written.
1488    pub const UPDATE_FIELD_WHOLE: &str = concat!(
1489        r#"mutation($input:UpdateProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
1490        whole_variables!(),
1491        r#"){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
1492        whole_issue!(),
1493        r#"}}}}"#,
1494        board_issue!()
1495    );
1496    /// [`CLEAR_FIELD`], selecting the whole issue behind the board item cleared.
1497    pub const CLEAR_FIELD_WHOLE: &str = concat!(
1498        r#"mutation($input:ClearProjectV2ItemFieldValueInput!,$readPriority:Boolean!,$priorityName:String!"#,
1499        whole_variables!(),
1500        r#"){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id fieldValueByName(name:$priorityName) @include(if:$readPriority){... on ProjectV2ItemFieldSingleSelectValue{name field{... on ProjectV2SingleSelectField{id name options{id name}}}}} content{"#,
1501        whole_issue!(),
1502        r#"}}}}"#,
1503        board_issue!()
1504    );
1505
1506    /// The document sent in `document`'s place while a write the store's cache expects runs,
1507    /// when it has one.
1508    #[must_use]
1509    pub fn whole_twin(document: &str) -> Option<&'static str> {
1510        [
1511            (ADD_COMMENT, ADD_COMMENT_WHOLE),
1512            (UPDATE_COMMENT, UPDATE_COMMENT_WHOLE),
1513            (UPDATE_ISSUE, UPDATE_ISSUE_WHOLE),
1514            (UPDATE_FIELD, UPDATE_FIELD_WHOLE),
1515            (CLEAR_FIELD, CLEAR_FIELD_WHOLE),
1516        ]
1517        .into_iter()
1518        .find(|(plain, _)| *plain == document)
1519        .map(|(_, whole)| whole)
1520    }
1521
1522    /// Every document above, with what this source is doing when it sends one.
1523    ///
1524    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
1525    /// name the call that was refused, and a `match` with a catch-all arm would answer a
1526    /// document added later with "talking to GitHub" and never say so.
1527    ///
1528    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
1529    /// const` here that this list omits, so the two cannot part — which is the same guard
1530    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
1531    pub const DOCUMENTS: [(&str, &str); 40] = [
1532        (SEARCH_ISSUES, "searching this board's issues"),
1533        (ISSUE, "reading one issue"),
1534        (
1535            ISSUE_BOARD_ITEMS,
1536            "reading one issue's board memberships past the page it came with",
1537        ),
1538        (SUB_ISSUES, "reading a project's tasks"),
1539        (BOARD, "reading the board"),
1540        (ORIGIN_LOOKUP, "looking up the items copied from one origin"),
1541        (BOARD_FIELDS, "reading the board's fields"),
1542        (DRAFT, "reading one draft"),
1543        (REPOSITORY, "reading the destination repository"),
1544        (
1545            PROJECT_VISIBILITY,
1546            "reading whether the board's project is public",
1547        ),
1548        (
1549            CREATION_CONTEXT,
1550            "reading the board's fields and the destination repository",
1551        ),
1552        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
1553        (CREATE_ISSUE, "creating an issue"),
1554        (ADD_TO_BOARD, "adding an issue to the board"),
1555        (UPDATE_ISSUE, "updating an issue"),
1556        (UPDATE_DRAFT, "updating a draft item"),
1557        (UPDATE_FIELD, "writing a board field"),
1558        (UPDATE_FIELDS, "writing board fields together"),
1559        (CLEAR_FIELD, "clearing a board field"),
1560        (
1561            CREATE_FIELD,
1562            "creating a board single-select field with its options",
1563        ),
1564        (
1565            STATUS_OPTIONS_SNAPSHOT,
1566            "snapshotting board Status options and assignments",
1567        ),
1568        (
1569            STATUS_OPTIONS_UPDATE,
1570            "safely replacing the board Status option list",
1571        ),
1572        (ADD_SUB_ISSUE, "filing an issue under its project"),
1573        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
1574        (ADD_BLOCKED_BY, "recording a dependency"),
1575        (REMOVE_BLOCKED_BY, "removing a dependency"),
1576        (DELETE_ISSUE, "deleting an issue"),
1577        (ISSUE_COMMENTS, "reading a task's comments"),
1578        (ISSUE_DETAIL, "reading one issue with its comments"),
1579        (
1580            ISSUE_DETAILS,
1581            "reading a batch of issues with their comments",
1582        ),
1583        (
1584            ISSUE_COPY_DETAILS,
1585            "reading a batch of issues with their comments and dependencies for a copy",
1586        ),
1587        (COMMENT_ISSUE, "reading which issue a comment is on"),
1588        (ADD_COMMENT, "adding a comment"),
1589        (UPDATE_COMMENT, "editing a comment"),
1590        (DELETE_COMMENT, "deleting a comment"),
1591        (
1592            ADD_COMMENT_WHOLE,
1593            "adding a comment, answered with the whole issue",
1594        ),
1595        (
1596            UPDATE_COMMENT_WHOLE,
1597            "editing a comment, answered with the whole issue",
1598        ),
1599        (
1600            UPDATE_ISSUE_WHOLE,
1601            "updating an issue, answered with it whole",
1602        ),
1603        (
1604            UPDATE_FIELD_WHOLE,
1605            "writing a board field, answered with the whole issue",
1606        ),
1607        (
1608            CLEAR_FIELD_WHOLE,
1609            "clearing a board field, answered with the whole issue",
1610        ),
1611    ];
1612}
1613
1614/// One aliased mutation field of [`graphql::UPDATE_FIELDS`]: the key its answer comes back
1615/// under, the variable carrying its input, and the boolean variable that includes it.
1616#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1617pub struct FieldSlot {
1618    /// The key of the answer's `data` this field's payload is under.
1619    pub alias: &'static str,
1620    /// The variable carrying this field's input.
1621    pub variable: &'static str,
1622    /// The boolean variable whose `true` sends this field.
1623    pub include: &'static str,
1624}
1625
1626/// The value writes [`graphql::UPDATE_FIELDS`] carries, in the order GitHub runs them.
1627pub const FIELD_WRITE_SLOTS: [FieldSlot; 6] = [
1628    FieldSlot {
1629        alias: "updateProjectV2ItemFieldValue",
1630        variable: "input",
1631        include: "writeFirst",
1632    },
1633    FieldSlot {
1634        alias: "second",
1635        variable: "second",
1636        include: "writeSecond",
1637    },
1638    FieldSlot {
1639        alias: "third",
1640        variable: "third",
1641        include: "writeThird",
1642    },
1643    FieldSlot {
1644        alias: "fourth",
1645        variable: "fourth",
1646        include: "writeFourth",
1647    },
1648    FieldSlot {
1649        alias: "fifth",
1650        variable: "fifth",
1651        include: "writeFifth",
1652    },
1653    FieldSlot {
1654        alias: "sixth",
1655        variable: "sixth",
1656        include: "writeSixth",
1657    },
1658];
1659
1660/// The clears [`graphql::UPDATE_FIELDS`] carries, run after every write of the same request.
1661pub const FIELD_CLEAR_SLOTS: [FieldSlot; 3] = [
1662    FieldSlot {
1663        alias: "cleared",
1664        variable: "clear",
1665        include: "writeClear",
1666    },
1667    FieldSlot {
1668        alias: "clearedSecond",
1669        variable: "clearSecond",
1670        include: "writeClearSecond",
1671    },
1672    FieldSlot {
1673        alias: "clearedThird",
1674        variable: "clearThird",
1675        include: "writeClearThird",
1676    },
1677];
1678
1679/// Which of GitHub's two rate limiters refused a request.
1680///
1681/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
1682/// secondary one — so an operator told the wrong one takes the wrong next step, which is
1683/// the whole reason this is carried rather than collapsed into "rate limited".
1684#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1685enum Limiter {
1686    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
1687    Primary,
1688    /// The burst limiter over content-generating requests, which nothing reports.
1689    Secondary,
1690}
1691
1692/// The wordings GitHub answers a secondary rate limit with.
1693///
1694/// It sends them under a forbidden status, under a too-many-requests status, and inside
1695/// the `errors` of a *successful* response, which is why the text is what this matches on
1696/// rather than the status. `abuse detection` is the wording GitHub used before the
1697/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
1698/// what a burst of content creation is refused with.
1699///
1700/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
1701/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
1702/// when, and the drift gate reconciles the two lists both ways. Public for that gate
1703/// alone — a caller has no use for it, and matching on a refusal is this source's job.
1704pub const SECONDARY_WORDINGS: [&str; 5] = [
1705    "secondary rate limit",
1706    "temporarily blocked from content creation",
1707    "abuse detection",
1708    "submitted too quickly",
1709    "exceeded a secondary",
1710];
1711
1712/// The wordings GitHub answers an exhausted primary budget with.
1713///
1714/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
1715/// than looked for in the response text. `api rate limit already exceeded` is what GraphQL
1716/// answers a request made once the hour's budget is spent — "API rate limit already exceeded
1717/// for user ID …" in the `errors` of an HTTP 200, with no `type` — and neither of the other
1718/// two phrases is a substring of it, so without it that answer read as a refusal that will
1719/// never lift. Pinned and gated exactly as [`SECONDARY_WORDINGS`] is, and public for the same
1720/// one reason.
1721pub const PRIMARY_WORDINGS: [&str; 4] = [
1722    "api rate limit exceeded",
1723    "api rate limit already exceeded",
1724    "rate limit exceeded",
1725    "rate_limited",
1726];
1727
1728/// What a response *says about itself*, which is the only place a refusal can be read.
1729///
1730/// Deliberately not the whole response body. A board is a place people write about their
1731/// own work, and a task on it titled "the secondary rate limit" would, matched across the
1732/// raw text, turn a perfectly good answer into a refusal this source then waited out and
1733/// reported. So the item data is never read: what is read is GitHub's own REST-style
1734/// `message` envelope, which is what a forbidden status carries, and the `message` and
1735/// `type` of each GraphQL error, which is where a *successful* response says it.
1736///
1737/// A body that is not JSON at all has nothing structured to read, so only a failing
1738/// response's own text is taken — a successful response that is not JSON is malformed
1739/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
1740fn refusal_wording(status: StatusCode, body: &str) -> String {
1741    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
1742        return if status.is_success() {
1743            String::new()
1744        } else {
1745            body.to_owned()
1746        };
1747    };
1748    let mut said: Vec<&str> = parsed
1749        .get("message")
1750        .and_then(Value::as_str)
1751        .into_iter()
1752        .collect();
1753    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
1754        for error in errors {
1755            said.extend(
1756                ["message", "type"]
1757                    .into_iter()
1758                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
1759            );
1760        }
1761    }
1762    said.join("; ")
1763}
1764
1765impl Limiter {
1766    /// Which limiter refused this response, or `None` when none of them did.
1767    ///
1768    /// The wording is read first and the status only decides what carries none of it,
1769    /// because GitHub answers a secondary limit with a forbidden status far more often
1770    /// than with too-many-requests — while a forbidden status saying nothing about a limit
1771    /// really is a credential this token lacks.
1772    ///
1773    /// A response is a refusal because of its status or its own wording. A spent budget
1774    /// only ever explains one; it never turns an answer into a refusal.
1775    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
1776        let normalized = refusal_wording(status, body).to_ascii_lowercase();
1777        if SECONDARY_WORDINGS
1778            .iter()
1779            .any(|wording| normalized.contains(wording))
1780        {
1781            return Some(Self::Secondary);
1782        }
1783        if status == StatusCode::TOO_MANY_REQUESTS {
1784            return Some(Self::Primary);
1785        }
1786        // An exhausted budget *explains* a response that failed; it does not make one that
1787        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
1788        // request the budget allowed as well as on the ones it then refuses, so reading
1789        // the header alone threw away a good answer — and, once refusals were retried,
1790        // replayed a request that had already taken effect.
1791        if !status.is_success() && budget_exhausted {
1792            return Some(Self::Primary);
1793        }
1794        // A successful response saying it: GitHub reports a GraphQL rate limit in the
1795        // `errors` of an HTTP 200, where nothing about the status says so at all.
1796        if status.is_success()
1797            && PRIMARY_WORDINGS
1798                .iter()
1799                .any(|wording| normalized.contains(wording))
1800        {
1801            return Some(Self::Primary);
1802        }
1803        None
1804    }
1805
1806    /// What this limiter is called where an operator can look it up.
1807    const fn name(self) -> &'static str {
1808        match self {
1809            Self::Primary => "GitHub's primary API rate limit",
1810            Self::Secondary => "GitHub's secondary rate limit",
1811        }
1812    }
1813
1814    /// What the endpoint an operator would go and check says about this limiter.
1815    const fn where_to_look(self) -> &'static str {
1816        match self {
1817            Self::Primary => {
1818                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
1819                 comes back."
1820            }
1821            Self::Secondary => {
1822                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
1823                 primary budget and does not report this one, so budget showing there says \
1824                 nothing about this refusal, and every further attempt extends it."
1825            }
1826        }
1827    }
1828
1829    /// The next step this limiter actually calls for.
1830    const fn what_to_do(self) -> &'static str {
1831        match self {
1832            Self::Primary => {
1833                "wait for the reset `gh api rate_limit` reports, then run the command again."
1834            }
1835            Self::Secondary => {
1836                "leave this board alone for a few minutes, then run the command again — or \
1837                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
1838            }
1839        }
1840    }
1841}
1842
1843/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
1844#[derive(Debug, Clone, Copy)]
1845struct Limited {
1846    limiter: Limiter,
1847    hint: Option<u64>,
1848}
1849
1850impl Limited {
1851    /// What the caller is told once this source has waited as long as it may.
1852    ///
1853    /// Both limiters report as [`SourceError::RateLimited`], because that is what
1854    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
1855    /// about *which* limiter it was makes it a different kind of failure. What differs is
1856    /// the operator's next step, and that is what the message carries — a secondary
1857    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
1858    /// budget looks fine, and then back to retry the very burst that was refused.
1859    fn exhausted(
1860        self,
1861        doing: &str,
1862        waits: u32,
1863        waited: Duration,
1864        needed: Duration,
1865        budget: Duration,
1866    ) -> SourceError {
1867        SourceError::RateLimited {
1868            retry_after_seconds: self.hint,
1869            message: Some(format!(
1870                "{} refused this source while {doing}; it waited {} out over {} and was refused \
1871                 again, and the next wait of {} would take it past the {} one call may spend \
1872                 waiting. {} next: {}",
1873                self.limiter.name(),
1874                plural(waits, "refusal"),
1875                seconds(waited),
1876                seconds(needed),
1877                seconds(budget),
1878                self.limiter.where_to_look(),
1879                self.limiter.what_to_do(),
1880            )),
1881        }
1882    }
1883}
1884
1885/// One HTTP attempt's result, with what its response said about the rate limit.
1886///
1887/// The two travel together so the record and the outcome are written from the same place:
1888/// what a response said about the budget is only readable while that response is in hand,
1889/// and what the attempt *meant* is only decidable once its body has been read.
1890struct Attempted {
1891    result: Result<Value, Attempt>,
1892    limits: accounting::RateLimit,
1893    /// GitHub's own reported cost for this call, for a document that asked for it.
1894    reported_cost: Option<u64>,
1895    response_outcome: accounting::ResponseOutcome,
1896}
1897
1898/// One attempt's outcome: an error to report, or a rate limit to wait out.
1899enum Attempt {
1900    Failed(SourceError),
1901    Limited(Limited),
1902}
1903
1904fn plural(count: u32, thing: &str) -> String {
1905    if count == 1 {
1906        format!("{count} {thing}")
1907    } else {
1908        format!("{count} {thing}s")
1909    }
1910}
1911
1912fn seconds(duration: Duration) -> String {
1913    format!("{:.1}s", duration.as_secs_f64())
1914}
1915
1916/// The member [`stamp_served`] writes onto every node of a response that says when it last
1917/// changed: the `Date` that response was served at. No GitHub type has a field of this name.
1918const SERVED_AT: &str = "onetaskgraphServedAt";
1919
1920/// Write `served` onto every object of `data` carrying an `updatedAt`, so whatever reads a
1921/// node's time later knows the server's clock on the response that read it — which is what
1922/// decides whether that node's change token is given (see [`change_token::SETTLED`]).
1923fn stamp_served(data: &mut Value, served: &str) {
1924    match data {
1925        Value::Object(members) => {
1926            if members.contains_key("updatedAt") {
1927                members.insert(SERVED_AT.to_owned(), Value::String(served.to_owned()));
1928            }
1929            for member in members.values_mut() {
1930                stamp_served(member, served);
1931            }
1932        }
1933        Value::Array(items) => {
1934            for item in items {
1935                stamp_served(item, served);
1936            }
1937        }
1938        _ => {}
1939    }
1940}
1941
1942/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
1943///
1944/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
1945/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
1946/// header, and neither is what makes a response a refusal — so the whole cost of one this
1947/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
1948/// instead. Refusing the response over the header would turn a readable refusal into an
1949/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
1950fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
1951    value
1952        .and_then(|value| value.to_str().ok())
1953        .and_then(|value| value.trim().parse::<u64>().ok())
1954}
1955
1956/// Every mutation this source sends creates content — an issue, a board item, a field of
1957/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
1958/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
1959/// and what the keyword says are the same set. That is what makes the keyword a sound test
1960/// rather than a convenient one: pacing an edit or a removal the limiter might not have
1961/// counted costs a wait, and not pacing one it did count costs the next fifty minutes.
1962fn is_mutation(query: &str) -> bool {
1963    query.trim_start().starts_with("mutation")
1964}
1965
1966/// What this source was doing, for a diagnostic that has to say so.
1967///
1968/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
1969/// a document added without a description is caught by that list's own gate instead of
1970/// falling through to the vague arm below.
1971fn operation_description(query: &str) -> &'static str {
1972    graphql::DOCUMENTS
1973        .iter()
1974        .find(|(document, _)| *document == query)
1975        .map_or("talking to GitHub", |(_, doing)| *doing)
1976}
1977
1978/// GitHub's published ceiling on content-generating requests, per minute.
1979///
1980/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
1981/// GitHub's number rather than this source's: [`MIN_MUTATION_INTERVAL_MS`] is *derived*
1982/// from it, so a pacing value checked only against itself cannot go stale here.
1983pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
1984/// The same ceiling as GitHub publishes it per hour, which this source does **not** pace
1985/// at. See [`MIN_MUTATION_INTERVAL_MS`] for why the per-minute bound is the one that
1986/// governs; it is pinned beside its sibling so the gate would notice either one moving.
1987pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
1988/// Shortest interval between two content-creating mutations, in milliseconds.
1989///
1990/// GitHub documents two secondary limits on content-generating requests:
1991/// [`CONTENT_CREATION_PER_MINUTE`] and [`CONTENT_CREATION_PER_HOUR`]. 60000/80 is 750, so
1992/// a mutation every 750 ms is the fastest rate that cannot exceed the per-minute bound,
1993/// and that is the bound a copy actually trips: a copy of one plan-sized project is a
1994/// burst of a few dozen mutations inside a few seconds. The hourly bound works out at one
1995/// every 7.2 seconds sustained, which no single copy reaches and which, used as the
1996/// spacing here, would turn an ordinary copy into an hour of waiting — so it is
1997/// deliberately *not* what this paces at. An installation that wants the hourly bound
1998/// honoured for a long sequence of copies says so through
1999/// `pacing.min_mutation_interval_ms`.
2000pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
2001/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
2002///
2003/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
2004/// own advice for a secondary limit — wait, and wait longer each time — without spending
2005/// the first minute of a transient refusal doing nothing.
2006pub const RETRY_BACKOFF_MS: u64 = 1_000;
2007/// Total time one call may spend waiting out rate limits before it reports a failure.
2008///
2009/// Two minutes is long enough to ride out the refusals a paced copy still collects and
2010/// short enough that a command an operator is watching returns. The bound is what makes
2011/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
2012/// the limiter, not in a process nobody can tell from a wedged one.
2013pub const RETRY_BUDGET_MS: u64 = 120_000;
2014
2015fn default_token_env() -> String {
2016    "GH_PROJECTS_TOKEN".to_owned()
2017}
2018fn default_endpoint() -> String {
2019    "https://api.github.com/graphql".to_owned()
2020}
2021
2022/// The name of a `Status` single-select option on the board.
2023///
2024/// Validated on the way in rather than checked later, so a blank option name — which
2025/// nothing on a board can be — is a state this type cannot hold.
2026#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2027#[serde(try_from = "String")]
2028#[schemars(extend("minLength" = 1))]
2029pub struct ColumnName(String);
2030
2031impl ColumnName {
2032    /// The option name, as the board spells it.
2033    fn as_str(&self) -> &str {
2034        &self.0
2035    }
2036}
2037
2038impl TryFrom<String> for ColumnName {
2039    type Error = String;
2040
2041    fn try_from(name: String) -> Result<Self, Self::Error> {
2042        if name.trim().is_empty() {
2043            return Err("a status_mapping option name cannot be blank".to_owned());
2044        }
2045        Ok(Self(name))
2046    }
2047}
2048
2049/// The two closed states this product can mean.
2050///
2051/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which is neither finished
2052/// work nor abandoned work, so nothing here ever writes it.
2053#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
2054#[serde(rename_all = "kebab-case")]
2055pub enum ClosedState {
2056    /// `COMPLETED` — precisely done.
2057    Completed,
2058    /// `NOT_PLANNED` — precisely cancelled.
2059    NotPlanned,
2060}
2061
2062impl ClosedState {
2063    const fn reason(self) -> &'static str {
2064        match self {
2065            Self::Completed => "COMPLETED",
2066            Self::NotPlanned => "NOT_PLANNED",
2067        }
2068    }
2069}
2070
2071/// Configuration for one GitHub Projects v2 board.
2072///
2073/// It serializes to a document this same type reads back as itself, so a caller writing a
2074/// configuration — a consumer filing onto a board it configures — can build one here rather
2075/// than spell the shape again; an empty `metadata_fields` is left out, as one never set.
2076#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2077#[serde(default, deny_unknown_fields)]
2078pub struct GitHubProjectsConfig {
2079    /// Login of the user or organization which owns the board.
2080    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
2081    /// The project number shown in the board's GitHub URL.
2082    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
2083    // 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.
2084    /// `owner/name` of the repository this source creates an issue in when the item's own
2085    /// `repositories` field does not decide it.
2086    ///
2087    /// An item naming exactly one repository is created there; a task or a document naming
2088    /// none or several is created in its parent project's repository; and a project, or a
2089    /// task or document with no parent, naming none or several is created here. A board
2090    /// has no repository of its own and `createIssue` requires one, so a write without
2091    /// this is refused naming the field. Reads never need it.
2092    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
2093    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
2094    /// Environment variable containing a fine-grained token with Projects and Issues
2095    /// read/write plus Pull requests read-only access for every repository represented on
2096    /// the board.
2097    #[serde(default = "default_token_env")]
2098    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
2099    /// GraphQL endpoint. GitHub Enterprise installations may override it.
2100    #[serde(default = "default_endpoint")]
2101    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
2102    /// Per-instance mapping from a status category to the option of the board's one
2103    /// `Status` field it lands on, for a task and for a project.
2104    ///
2105    /// The shared `StatusMapping` grammar: each value is one option name for both kinds,
2106    /// `null` to disable the category for both, or `{task, project}` naming it per kind,
2107    /// where a kind left out leaves the category unmapped for that kind. A category this
2108    /// does not mention keeps its shipped default for both kinds: `backlog` to "Backlog",
2109    /// `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress", `done` to
2110    /// "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed as not
2111    /// planned, and `draft` and `unknown` unmapped. A category it does mention gets no
2112    /// shipped default for a kind it leaves out. `done` and `cancelled` close the issue for
2113    /// either kind. No two categories may name one option for the same kind, ignoring case.
2114    /// `unknown` may name one existing option; every unknown word then lands on it and
2115    /// reads back as `unknown` under its name. Unlike `local-md`, this source cannot keep
2116    /// each unknown word because it never creates board options.
2117    #[serde(default)]
2118    pub status_mapping: StatusMapping,
2119    /// Per-instance mapping from a task's priority to an option of this board's
2120    /// single-select field named `Priority`.
2121    ///
2122    /// Absent, this source holds no priority: every task reads as `none`, and a write of any
2123    /// other priority is refused before it reaches this board. Present, each of `urgent`,
2124    /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
2125    /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
2126    /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
2127    /// no two levels may name one option. Reads and writes never create the field or an
2128    /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
2129    /// the board lacks is refused pointing there.
2130    #[serde(default)]
2131    pub priority_mapping: Option<PriorityMappingConfig>,
2132    /// Caller metadata values this source also writes to board text fields, so the board can
2133    /// filter on them.
2134    ///
2135    /// Absent or empty, nothing is projected. Each entry names a board text `field`, a
2136    /// top-level metadata `key` and an optional `path` of object keys walked inside that key's
2137    /// value. On every item write — create, update and copy, of a task, a project or a
2138    /// document — a string found there is written to the field, and nothing is sent when the
2139    /// field already holds it; nothing there, or `null`, clears the field when it holds a
2140    /// value; any other JSON type is refused before any mutation. The metadata comment in the
2141    /// issue body stays the one home of the value, and every read reports metadata from it.
2142    /// Reads and writes never create the field: `onetaskgraph sources fields <source>
2143    /// --apply` does, as a text field, and a write to a board lacking it, or holding a
2144    /// non-text field of that name, is refused pointing there.
2145    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2146    // Kept in the schema as `"default": []` although a serialized configuration leaves an empty
2147    // list out, so both SDKs model an absent list as an empty one rather than as `null`.
2148    #[schemars(!skip_serializing_if)]
2149    pub metadata_fields: Vec<MetadataFieldConfig>,
2150    /// How fast this source writes, and how long it waits out a rate-limit refusal.
2151    ///
2152    /// Every field keeps its shipped default when it is absent, and the defaults are
2153    /// GitHub's own published limits rather than taste. See [`Pacing`].
2154    #[serde(default)]
2155    pub pacing: PacingConfig,
2156}
2157
2158/// Which option of the board's `Priority` field each priority lands on.
2159///
2160/// One member per level rather than a map, so a key that is not a level is refused where
2161/// the configuration is read, naming the levels there are. `none` is not a member: it is no
2162/// value in the field, not an option of it.
2163#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2164#[serde(default, deny_unknown_fields)]
2165pub struct PriorityMappingConfig {
2166    /// The option `urgent` lands on; `Urgent` when absent.
2167    pub urgent: Option<PriorityOptionName>,
2168    /// The option `high` lands on; `High` when absent.
2169    pub high: Option<PriorityOptionName>,
2170    /// The option `medium` lands on; `Medium` when absent.
2171    pub medium: Option<PriorityOptionName>,
2172    /// The option `low` lands on; `Low` when absent.
2173    pub low: Option<PriorityOptionName>,
2174}
2175
2176/// The name of an option of the board's `Priority` single-select field.
2177///
2178/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
2179/// blank name.
2180#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2181#[serde(try_from = "String")]
2182#[schemars(extend("minLength" = 1))]
2183pub struct PriorityOptionName(String);
2184
2185impl PriorityOptionName {
2186    /// The option name, as the board spells it.
2187    fn as_str(&self) -> &str {
2188        &self.0
2189    }
2190}
2191
2192impl TryFrom<String> for PriorityOptionName {
2193    type Error = String;
2194
2195    fn try_from(name: String) -> Result<Self, Self::Error> {
2196        if name.trim().is_empty() {
2197            return Err("a priority_mapping option name cannot be blank".to_owned());
2198        }
2199        Ok(Self(name))
2200    }
2201}
2202
2203/// The name of the board field a priority is held in.
2204pub const PRIORITY_FIELD: &str = "Priority";
2205
2206/// The four priorities a board option can hold, in the order a new `Priority` field lists
2207/// them. `none` is not among them: it is the field holding no value.
2208///
2209/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
2210/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
2211/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
2212/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
2213pub const PRIORITY_LEVELS: [Priority; 4] = [
2214    Priority::Urgent,
2215    Priority::High,
2216    Priority::Medium,
2217    Priority::Low,
2218];
2219
2220/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
2221/// see that list for what this pins.
2222#[must_use]
2223pub const fn level_position(priority: Priority) -> Option<usize> {
2224    match priority {
2225        Priority::None => None,
2226        Priority::Urgent => Some(0),
2227        Priority::High => Some(1),
2228        Priority::Medium => Some(2),
2229        Priority::Low => Some(3),
2230    }
2231}
2232
2233/// This instance's complete priority-to-option mapping, read in both directions.
2234///
2235/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
2236/// two levels name one option.
2237#[derive(Debug, Clone)]
2238struct PriorityMapping {
2239    options: [PriorityOptionName; 4],
2240}
2241
2242impl PriorityMapping {
2243    fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2244        let shipped = |name: &str| PriorityOptionName(name.to_owned());
2245        let mapping = Self {
2246            options: [
2247                config.urgent.unwrap_or_else(|| shipped("Urgent")),
2248                config.high.unwrap_or_else(|| shipped("High")),
2249                config.medium.unwrap_or_else(|| shipped("Medium")),
2250                config.low.unwrap_or_else(|| shipped("Low")),
2251            ],
2252        };
2253        for (index, option) in mapping.options.iter().enumerate() {
2254            if let Some(earlier) = mapping.options[..index]
2255                .iter()
2256                .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
2257            {
2258                return Err(SourceError::Config {
2259                    message: format!(
2260                        "priority_mapping of source {instance} sends both {} and {} to the board \
2261                         option {:?}; one option cannot read back as two priorities",
2262                        PRIORITY_LEVELS[earlier],
2263                        PRIORITY_LEVELS[index],
2264                        option.as_str()
2265                    ),
2266                });
2267            }
2268        }
2269        Ok(mapping)
2270    }
2271
2272    /// The option `priority` lands on, or `None` for `none`, which is no option at all.
2273    fn option(&self, priority: Priority) -> Option<&str> {
2274        level_position(priority).map(|index| self.options[index].as_str())
2275    }
2276
2277    /// The priority a board option name reports, or `None` when nothing maps to it.
2278    fn priority_of(&self, option: &str) -> Option<Priority> {
2279        self.options
2280            .iter()
2281            .position(|name| name.as_str().eq_ignore_ascii_case(option))
2282            .map(|index| PRIORITY_LEVELS[index])
2283    }
2284
2285    /// Every mapped option name, in the order a new `Priority` field lists them.
2286    fn names(&self) -> impl Iterator<Item = &str> {
2287        self.options.iter().map(PriorityOptionName::as_str)
2288    }
2289}
2290
2291/// One metadata value this source projects onto a board text field.
2292#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2293#[serde(deny_unknown_fields)]
2294pub struct MetadataFieldConfig {
2295    /// The name of the board text field the value is written to.
2296    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.
2297    /// The top-level metadata key the value is read from. A key under the reserved
2298    /// `onetaskgraph.` prefix is refused.
2299    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.
2300    /// Object keys walked inside that key's value, outermost first. Absent or empty, the
2301    /// key's own value is the one projected. Left out of a serialized entry when empty.
2302    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2303    // Kept in the schema as `"default": []`, for the reason `metadata_fields`' is.
2304    #[schemars(!skip_serializing_if)]
2305    pub path: Vec<String>,
2306}
2307
2308/// The board fields no metadata value may be projected onto, compared ignoring case: the
2309/// three this source writes itself, and those GitHub owns on every board.
2310// 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.
2311const UNPROJECTABLE_FIELDS: [&str; 13] = [
2312    STATUS_FIELD,
2313    PRIORITY_FIELD,
2314    ORIGIN_FIELD,
2315    "Title",
2316    "Assignees",
2317    "Labels",
2318    "Linked pull requests",
2319    "Milestone",
2320    "Repository",
2321    "Reviewers",
2322    "Parent issue",
2323    "Sub-issues progress",
2324    "Type",
2325];
2326
2327/// One validated [`MetadataFieldConfig`] entry.
2328#[derive(Debug, Clone)]
2329struct MetadataField {
2330    field: ProjectedField,
2331    key: ProjectedKey,
2332    path: Vec<String>,
2333}
2334
2335/// The name of a board text field a metadata value is projected onto: never blank, never one
2336/// this source or GitHub writes, and never one another entry of the same source names.
2337#[derive(Debug, Clone, PartialEq, Eq)]
2338struct ProjectedField(String);
2339
2340/// The top-level metadata key a projected value is read from: never blank, and never under
2341/// the namespace this product reserves.
2342#[derive(Debug, Clone, PartialEq, Eq)]
2343struct ProjectedKey(String);
2344
2345impl std::ops::Deref for ProjectedField {
2346    type Target = str;
2347    fn deref(&self) -> &str {
2348        &self.0
2349    }
2350}
2351
2352impl std::ops::Deref for ProjectedKey {
2353    type Target = str;
2354    fn deref(&self) -> &str {
2355        &self.0
2356    }
2357}
2358
2359/// What one item's metadata says one projected field should hold.
2360#[derive(Debug, Clone, PartialEq, Eq)]
2361enum Projected {
2362    /// A string to hold.
2363    Text(String),
2364    /// Nothing: the key or a step of the path is absent, or the value is `null`.
2365    Nothing,
2366}
2367
2368impl MetadataField {
2369    /// Validate every entry of one instance's `metadata_fields`, refusing the first that
2370    /// cannot stand, naming the source and the entry.
2371    fn resolve(
2372        configured: Vec<MetadataFieldConfig>,
2373        instance: &SourceName,
2374    ) -> Result<Vec<Self>, SourceError> {
2375        let mut resolved: Vec<Self> = Vec::with_capacity(configured.len());
2376        for (index, entry) in configured.into_iter().enumerate() {
2377            let refuse = |why: String| SourceError::Config {
2378                message: format!(
2379                    "metadata_fields[{index}] of source {instance} (field {:?}, key {:?}) {why}",
2380                    entry.field, entry.key
2381                ),
2382            };
2383            if entry.field.trim().is_empty() {
2384                return Err(refuse("names a blank field".to_owned()));
2385            }
2386            if entry.key.trim().is_empty() {
2387                return Err(refuse("names a blank key".to_owned()));
2388            }
2389            if entry.key.split('.').next() == Some(MetadataKey::RESERVED_NAMESPACE) {
2390                return Err(refuse(format!(
2391                    "names a key under the reserved \"{}.\" prefix, which this product keeps \
2392                     for its own keys; next: project a caller key instead",
2393                    MetadataKey::RESERVED_NAMESPACE
2394                )));
2395            }
2396            if let Some(owned) = UNPROJECTABLE_FIELDS
2397                .iter()
2398                .find(|owned| owned.eq_ignore_ascii_case(&entry.field))
2399            {
2400                return Err(refuse(format!(
2401                    "names the board field {owned:?}, which this source or GitHub already \
2402                     writes; next: name a text field of your own"
2403                )));
2404            }
2405            if let Some(earlier) = resolved
2406                .iter()
2407                .position(|other| other.field.eq_ignore_ascii_case(&entry.field))
2408            {
2409                return Err(refuse(format!(
2410                    "names the same board field as metadata_fields[{earlier}]; one field \
2411                     cannot hold two values"
2412                )));
2413            }
2414            resolved.push(Self {
2415                field: ProjectedField(entry.field),
2416                key: ProjectedKey(entry.key),
2417                path: entry.path,
2418            });
2419        }
2420        Ok(resolved)
2421    }
2422
2423    /// The path as a refusal spells it: `[]` for the key's own value.
2424    fn spelled_path(&self) -> String {
2425        serde_json::to_string(&self.path).unwrap_or_else(|_| format!("{:?}", self.path))
2426    }
2427
2428    /// What `metadata` says this field should hold, or the refusal for a value no text field
2429    /// can hold.
2430    fn projected(
2431        &self,
2432        metadata: &BTreeMap<String, Value>,
2433        instance: &SourceName,
2434    ) -> Result<Projected, SourceError> {
2435        let mut value = metadata.get(&*self.key);
2436        for step in &self.path {
2437            value = value.and_then(|held| held.get(step.as_str()));
2438        }
2439        let found = match value {
2440            None | Some(Value::Null) => return Ok(Projected::Nothing),
2441            Some(Value::String(text)) => return Ok(Projected::Text(text.clone())),
2442            Some(Value::Bool(_)) => "a boolean",
2443            Some(Value::Number(_)) => "a number",
2444            Some(Value::Array(_)) => "an array",
2445            Some(Value::Object(_)) => "an object",
2446        };
2447        Err(SourceError::Refused {
2448            message: format!(
2449                "source {instance} projects metadata key {:?} at path {} onto its board text \
2450                 field {:?}, and this item holds {found} there; only a string, null or nothing \
2451                 can be written to a text field; next: store a string there, or remove the \
2452                 value",
2453                &*self.key,
2454                self.spelled_path(),
2455                &*self.field
2456            ),
2457        })
2458    }
2459}
2460
2461/// What one item's `Priority` field says, read through this instance's mapping.
2462#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
2463enum HeldPriority {
2464    /// A priority this source reports: an option the mapping names, or no value (`none`).
2465    Read(Priority),
2466    /// An option the mapping does not name, which is never read as a level or as `none`.
2467    Unmapped(String),
2468}
2469
2470/// How fast this source writes, and how long it waits out a rate-limit refusal.
2471///
2472/// Configurable because a GitHub Enterprise installation sets its own limits and an
2473/// operator who has already been refused may want to go slower still — not because the
2474/// defaults are guesses.
2475#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2476#[serde(default, deny_unknown_fields)]
2477pub struct PacingConfig {
2478    /// Shortest interval between two content-creating mutations, in milliseconds.
2479    ///
2480    /// Zero sends them as fast as they are asked for, which is what a fixture server on
2481    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
2482    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.
2483    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
2484    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
2485    /// zero while there is a budget to spend, because a schedule of zero-length waits
2486    /// consumes none of it and so never ends.
2487    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.
2488    /// Total time one call may spend waiting out rate limits, in milliseconds.
2489    ///
2490    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
2491    /// the bound is what makes this a wait rather than a hang.
2492    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.
2493}
2494
2495/// The largest any pacing setting may be, in milliseconds.
2496///
2497/// One hour. GitHub's own harshest published bound on content-generating requests works
2498/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
2499/// anything a real limit asks for, and past it the settings stop describing pacing at all:
2500/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
2501/// and an interval beyond it is a command that never sends its second mutation. It also
2502/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
2503/// what a `Duration` can hold on every platform.
2504pub const MAX_PACING_MS: u64 = 3_600_000;
2505
2506/// [`PacingConfig`] with every default resolved and every value checked, which is what the
2507/// source holds.
2508#[derive(Debug, Clone, Copy)]
2509struct Pacing {
2510    min_mutation_interval: Duration,
2511    retry_backoff: Duration,
2512    retry_budget: Duration,
2513}
2514
2515impl Pacing {
2516    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
2517    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
2518        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
2519            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
2520                message: format!(
2521                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
2522                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
2523                     GitHub's own harshest published limit"
2524                ),
2525            }),
2526            Some(value) => Ok(Duration::from_millis(value)),
2527            None => Ok(Duration::from_millis(default)),
2528        };
2529        let retry_backoff = bounded(
2530            config.retry_backoff_ms,
2531            RETRY_BACKOFF_MS,
2532            "retry_backoff_ms",
2533        )?;
2534        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
2535        if retry_backoff.is_zero() && !retry_budget.is_zero() {
2536            return Err(SourceError::Config {
2537                message: format!(
2538                    "pacing.retry_backoff_ms of source {instance} is 0 while \
2539                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
2540                     none of that budget, so it would retry a refusal forever. Set a backoff of \
2541                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
2542                     waiting at all",
2543                    retry_budget.as_millis()
2544                ),
2545            });
2546        }
2547        Ok(Self {
2548            min_mutation_interval: bounded(
2549                config.min_mutation_interval_ms,
2550                MIN_MUTATION_INTERVAL_MS,
2551                "min_mutation_interval_ms",
2552            )?,
2553            retry_backoff,
2554            retry_budget,
2555        })
2556    }
2557}
2558
2559/// Factory for [`GitHubProjectsSource`].
2560#[derive(Debug, Clone, Copy, Default)]
2561pub struct Plugin;
2562
2563impl SourcePlugin for Plugin {
2564    fn kind(&self) -> &'static str {
2565        KIND
2566    }
2567    /// The endpoint, the owner and the board's number: what names one board.
2568    fn remote_identity(&self, config: &Value) -> Option<String> {
2569        let config: GitHubProjectsConfig = serde_json::from_value(config.clone()).ok()?;
2570        Some(format!(
2571            "{}\n{}\n{}",
2572            config.endpoint, config.owner, config.project_number
2573        ))
2574    }
2575    fn config_schema(&self) -> Schema {
2576        schema_for!(GitHubProjectsConfig)
2577    }
2578    fn build(
2579        &self,
2580        name: &SourceName,
2581        config: &Value,
2582        secrets: &dyn SecretResolver,
2583    ) -> Result<Box<dyn TaskSource>, SourceError> {
2584        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
2585    }
2586
2587    fn build_with_clock(
2588        &self,
2589        name: &SourceName,
2590        config: &Value,
2591        secrets: &dyn SecretResolver,
2592        clock: SharedClock,
2593    ) -> Result<Box<dyn TaskSource>, SourceError> {
2594        self.build_recording_with_clock(name, config, secrets, Arc::new(Accounting::new()), clock)
2595    }
2596}
2597
2598impl Plugin {
2599    /// Build a source recording every request it sends into an accounting the caller holds.
2600    ///
2601    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
2602    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
2603    /// session total rather than two — see [`accounting`] and
2604    /// [`GitHubProjectsSource::recording_into`].
2605    ///
2606    /// # Errors
2607    ///
2608    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
2609    /// [`SourceError::Config`] for configuration this plugin cannot use and
2610    /// [`SourceError::Auth`] for a credential it cannot find.
2611    pub fn build_recording_into(
2612        &self,
2613        name: &SourceName,
2614        config: &Value,
2615        secrets: &dyn SecretResolver,
2616        ledger: Arc<Accounting>,
2617    ) -> Result<Box<dyn TaskSource>, SourceError> {
2618        self.build_recording_with_clock(name, config, secrets, ledger, system_clock())
2619    }
2620
2621    fn build_recording_with_clock(
2622        &self,
2623        name: &SourceName,
2624        config: &Value,
2625        secrets: &dyn SecretResolver,
2626        ledger: Arc<Accounting>,
2627        clock: SharedClock,
2628    ) -> Result<Box<dyn TaskSource>, SourceError> {
2629        let config: GitHubProjectsConfig =
2630            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
2631                message: format!("source {name}: {e}"),
2632            })?;
2633        let prefix = format!("source {name}: ");
2634        let mut source = GitHubProjectsSource::recording_into(name, config, secrets, ledger)
2635            .map_err(|error| match error {
2636                // The shared `StatusMapping::distinct` names the source itself.
2637                SourceError::Config { message } if message.starts_with(&prefix) => {
2638                    SourceError::Config { message }
2639                }
2640                SourceError::Config { message } => SourceError::Config {
2641                    message: format!("{prefix}{message}"),
2642                },
2643                SourceError::Auth { message } => SourceError::Auth {
2644                    message: format!("source {name}: {message}"),
2645                },
2646                other => other,
2647            })?;
2648        source.clock = clock;
2649        Ok(Box::new(source))
2650    }
2651}
2652
2653/// Where a status category lands on this board, once configuration is resolved.
2654#[derive(Debug, Clone, PartialEq, Eq)]
2655enum StatusTarget {
2656    /// Not usable against this instance for this kind, and why.
2657    Disabled(UnmappedStatus),
2658    /// The board's `Status` option of this name.
2659    Column(ColumnName),
2660    /// A closed issue, with both its board option and the reason that says which closed it means.
2661    // 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.
2662    Terminal(ColumnName, ClosedState),
2663}
2664
2665/// Every status category, in the order the vocabulary declares them.
2666///
2667/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
2668/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
2669/// added to the shared vocabulary fails to compile until it is named there, and this
2670/// crate's suite reconciles this list against that enum's own derived schema, which is
2671/// generated from the variants rather than written beside them. The schema is what
2672/// catches a list left one short — a list checking only the positions it already holds
2673/// would pass while every mapping indexed by the new position panicked.
2674pub const CATEGORIES: [StatusCategory; 8] = [
2675    StatusCategory::Draft,
2676    StatusCategory::Backlog,
2677    StatusCategory::Todo,
2678    StatusCategory::Queued,
2679    StatusCategory::InProgress,
2680    StatusCategory::Done,
2681    StatusCategory::Cancelled,
2682    StatusCategory::Unknown,
2683];
2684
2685/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
2686#[must_use]
2687pub const fn category_position(category: StatusCategory) -> usize {
2688    match category {
2689        StatusCategory::Draft => 0,
2690        StatusCategory::Backlog => 1,
2691        StatusCategory::Todo => 2,
2692        StatusCategory::Queued => 3,
2693        StatusCategory::InProgress => 4,
2694        StatusCategory::Done => 5,
2695        StatusCategory::Cancelled => 6,
2696        StatusCategory::Unknown => 7,
2697    }
2698}
2699
2700/// The spelling a status category is configured and reported under.
2701fn category_name(category: StatusCategory) -> &'static str {
2702    match category {
2703        StatusCategory::Draft => "draft",
2704        StatusCategory::Backlog => "backlog",
2705        StatusCategory::Todo => "todo",
2706        StatusCategory::Queued => "queued",
2707        StatusCategory::InProgress => "in-progress",
2708        StatusCategory::Done => "done",
2709        StatusCategory::Cancelled => "cancelled",
2710        StatusCategory::Unknown => "unknown",
2711    }
2712}
2713
2714/// A shipped default's option name.
2715///
2716/// The literals below are this file's own and non-blank, and they are validated by the
2717/// one constructor a configured name goes through rather than beside it.
2718fn shipped_column(name: &'static str) -> ColumnName {
2719    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
2720}
2721
2722/// The shipped default for one category this instance's `status_mapping` does not mention,
2723/// for either kind.
2724fn shipped_default(category: StatusCategory) -> StatusTarget {
2725    match category {
2726        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
2727        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
2728        StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
2729        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
2730        StatusCategory::Done => {
2731            StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
2732        }
2733        StatusCategory::Cancelled => {
2734            StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
2735        }
2736        StatusCategory::Draft | StatusCategory::Unknown => {
2737            StatusTarget::Disabled(UnmappedStatus::Unconfigured)
2738        }
2739    }
2740}
2741
2742/// The two kinds a status is written and read for, each with its own half of the mapping.
2743const STATUS_KINDS: [ItemKind; 2] = [ItemKind::Task, ItemKind::Project];
2744
2745/// This instance's complete category-to-target mapping for each kind, read in both
2746/// directions.
2747///
2748/// One target per category per kind, held at that category's own [`category_position`], so
2749/// a category missing from the mapping, named twice in it, or filed out of order is a state
2750/// this type cannot hold rather than one [`Self::target`] has to defend against. Both kinds'
2751/// targets are options of the board's one `Status` field.
2752#[derive(Debug, Clone)]
2753struct BoardStatuses {
2754    tasks: [StatusTarget; CATEGORIES.len()],
2755    projects: [StatusTarget; CATEGORIES.len()],
2756}
2757
2758impl BoardStatuses {
2759    /// Resolve `configured` against the shipped defaults, refusing two categories one kind
2760    /// would read back from one option.
2761    ///
2762    /// A category the mapping does not mention keeps its shipped default for both kinds; one
2763    /// it does mention is exactly what it configures, so a per-kind object leaves the kind it
2764    /// omits unmapped rather than defaulted.
2765    fn resolve(configured: &StatusMapping, instance: &SourceName) -> Result<Self, SourceError> {
2766        let resolve_kind =
2767            |kind: ItemKind| -> Result<[StatusTarget; CATEGORIES.len()], SourceError> {
2768                // `CATEGORIES[position] == category` for every category — the crate's suite
2769                // asserts it — so mapping the list in order fills each category's own slot.
2770                let mut targets = CATEGORIES.map(shipped_default);
2771                for (slot, category) in targets.iter_mut().zip(CATEGORIES) {
2772                    if !configured.mentions(category) {
2773                        continue;
2774                    }
2775                    *slot = match configured.name_for(category, kind) {
2776                        Err(why) => StatusTarget::Disabled(why),
2777                        Ok(name) => {
2778                            let option = ColumnName::try_from(name.as_str().to_owned())
2779                                .map_err(|message| SourceError::Config { message })?;
2780                            match category {
2781                                StatusCategory::Done => {
2782                                    StatusTarget::Terminal(option, ClosedState::Completed)
2783                                }
2784                                StatusCategory::Cancelled => {
2785                                    StatusTarget::Terminal(option, ClosedState::NotPlanned)
2786                                }
2787                                _ => StatusTarget::Column(option),
2788                            }
2789                        }
2790                    };
2791                }
2792                StatusMapping::distinct(
2793                    instance,
2794                    kind,
2795                    CATEGORIES
2796                        .iter()
2797                        .zip(&targets)
2798                        .filter_map(|(category, target)| target.option().map(|o| (*category, o))),
2799                )?;
2800                Ok(targets)
2801            };
2802        Ok(Self {
2803            tasks: resolve_kind(ItemKind::Task)?,
2804            projects: resolve_kind(ItemKind::Project)?,
2805        })
2806    }
2807
2808    /// Every category's target for `kind`, in category order.
2809    const fn targets(&self, kind: ItemKind) -> &[StatusTarget; CATEGORIES.len()] {
2810        match kind {
2811            ItemKind::Task => &self.tasks,
2812            ItemKind::Project => &self.projects,
2813        }
2814    }
2815
2816    fn target(&self, kind: ItemKind, category: StatusCategory) -> &StatusTarget {
2817        &self.targets(kind)[category_position(category)]
2818    }
2819
2820    /// The category a board option name reports for `kind`, or `None` when nothing of that
2821    /// kind maps to it.
2822    fn category_of(&self, kind: ItemKind, option: &str) -> Option<StatusCategory> {
2823        CATEGORIES.into_iter().find(|category| {
2824            self.target(kind, *category)
2825                .option()
2826                .is_some_and(|name| name.eq_ignore_ascii_case(option))
2827        })
2828    }
2829
2830    /// Every option name either kind maps a category to, each once ignoring case, in
2831    /// category order with a task's name before a project's — what the guarded setup asks
2832    /// the `Status` field to hold.
2833    fn wanted(&self) -> Vec<String> {
2834        let mut wanted: Vec<String> = Vec::new();
2835        for category in CATEGORIES {
2836            for kind in STATUS_KINDS {
2837                if let Some(name) = self.target(kind, category).option()
2838                    && !wanted.iter().any(|held| held.eq_ignore_ascii_case(name))
2839                {
2840                    wanted.push(name.to_owned());
2841                }
2842            }
2843        }
2844        wanted
2845    }
2846
2847    /// The status an item of `kind` reports, from the three things a read of it says: its
2848    /// board `Status` option, whether its issue is closed, and the reason it was closed with.
2849    ///
2850    /// The closed state decides the category and the `Status` option decides the name, so
2851    /// a closed issue sitting in a "Shipped" column reports `done` named `Shipped`, whatever
2852    /// its kind. A closed issue whose reason is `DUPLICATE` or `REOPENED` reports `Unknown`:
2853    /// a duplicate is not finished work, and calling it done is a lie the next copy would
2854    /// write back. `REOPENED`-while-closed is a state this source can never produce, so
2855    /// it is read permissively rather than refused — reads are faithful, and refusals
2856    /// belong on writes. An open item's option reads through its own kind's mapping, and an
2857    /// option that mapping does not name reads as `Unknown` under its own name.
2858    ///
2859    /// One function of those three rather than of a response, so a narrow status write can
2860    /// answer what a re-read would report by applying it to the state it has just written.
2861    fn status(
2862        &self,
2863        kind: ItemKind,
2864        option: Option<&str>,
2865        closed: bool,
2866        reason: Option<&str>,
2867    ) -> Status {
2868        if closed {
2869            let category = match reason {
2870                None | Some("COMPLETED") => StatusCategory::Done,
2871                Some("NOT_PLANNED") => StatusCategory::Cancelled,
2872                Some(_) => StatusCategory::Unknown,
2873            };
2874            let fallback = match category {
2875                StatusCategory::Done => "Done",
2876                StatusCategory::Cancelled => "Cancelled",
2877                _ => "Closed",
2878            };
2879            return Status {
2880                category,
2881                name: option.unwrap_or(fallback).to_owned(),
2882            };
2883        }
2884        let name = option.unwrap_or("Open").to_owned();
2885        Status {
2886            category: self
2887                .category_of(kind, &name)
2888                .unwrap_or(StatusCategory::Unknown),
2889            name,
2890        }
2891    }
2892}
2893
2894impl BoardStatuses {
2895    /// For each kind, the option names it maps a category to that `existing` lacks, ignoring
2896    /// case; a kind lacking none is left out.
2897    fn missing_by_kind(&self, existing: &[StatusOption]) -> Vec<KindMissing> {
2898        STATUS_KINDS
2899            .into_iter()
2900            .filter_map(|kind| {
2901                let missing: Vec<String> = self
2902                    .targets(kind)
2903                    .iter()
2904                    .filter_map(StatusTarget::option)
2905                    .filter(|wanted| {
2906                        !existing
2907                            .iter()
2908                            .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2909                    })
2910                    .map(str::to_owned)
2911                    .collect();
2912                (!missing.is_empty()).then_some(KindMissing { kind, missing })
2913            })
2914            .collect()
2915    }
2916}
2917
2918impl StatusTarget {
2919    /// The board option this target selects, or `None` for an unmapped one.
2920    fn option(&self) -> Option<&str> {
2921        match self {
2922            Self::Column(name) | Self::Terminal(name, _) => Some(name.as_str()),
2923            Self::Disabled(_) => None,
2924        }
2925    }
2926}
2927
2928// 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.
2929/// One repository this source can create an issue in, as `owner/name`.
2930///
2931/// Every `createIssue` this source sends names one of these: the item's own single
2932/// `repositories` entry, else its parent project issue's repository, else the configured
2933/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
2934/// that choice and says what it refuses before `createIssue`.
2935// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
2936#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
2937struct RepositoryTarget {
2938    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
2939    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
2940}
2941
2942impl RepositoryTarget {
2943    fn parse(value: &str) -> Result<Self, SourceError> {
2944        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
2945            message: format!(
2946                "repository must be spelled owner/name; {value:?} names no repository"
2947            ),
2948        })?;
2949        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
2950            return Err(SourceError::Config {
2951                message: format!(
2952                    "repository must be spelled owner/name with a GitHub login and one \
2953                     repository name; {value:?} is not"
2954                ),
2955            });
2956        }
2957        Ok(Self {
2958            owner: owner.to_owned(),
2959            name: name.to_owned(),
2960        })
2961    }
2962
2963    /// The one host whose repositories this source creates issues in, spelled once: it is
2964    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
2965    const HOST: &str = "github.com";
2966
2967    fn origin(&self) -> String {
2968        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
2969    }
2970
2971    /// The repository a normalized origin names, or why it is none this source can create
2972    /// an issue in: another host, or more or fewer than `owner/name` under this one.
2973    fn from_origin(origin: &Repository) -> Result<Self, String> {
2974        let not_here = || {
2975            format!(
2976                "{} is not a {}/owner/name repository",
2977                origin.as_str(),
2978                Self::HOST
2979            )
2980        };
2981        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
2982        if host != Self::HOST {
2983            return Err(not_here());
2984        }
2985        Self::parse(rest).map_err(|_| not_here())
2986    }
2987
2988    fn slug(&self) -> String {
2989        format!("{}/{}", self.owner, self.name)
2990    }
2991}
2992
2993/// A source which reads GitHub afresh for every operation.
2994pub struct GitHubProjectsSource {
2995    /// This source's configured name, used both to tell a far end naming this source
2996    /// from one naming a system it knows nothing about, and to name the instance a
2997    /// status refusal is about.
2998    name: SourceName,
2999    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
3000    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
3001    repository: Option<RepositoryTarget>,
3002    endpoint: Url,
3003    token: SecretString,
3004    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
3005    statuses: BoardStatuses,
3006    /// Where each priority lands on this board, or `None` when this instance holds none.
3007    priorities: Option<PriorityMapping>,
3008    /// The metadata values this instance projects onto board text fields, in configured order.
3009    metadata_fields: Vec<MetadataField>,
3010    client: Client,
3011    asset_client: Client,
3012    /// Every item this source has created in this command, in the order it created them —
3013    /// dropped by [`TaskSource::end_command`].
3014    ///
3015    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
3016    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
3017    /// a copy resolving a dependency on an item it had just created refused it as not
3018    /// found. A board read is completed from this — an item remembered here and absent from
3019    /// the read is added back, because the board really does hold it and only the read is
3020    /// behind.
3021    ///
3022    /// It is not a cache of a user's work: nothing is remembered that this process did not
3023    /// itself just write, it lives and dies with the process, and it is never consulted for
3024    /// an item this source did not create.
3025    created: Mutex<Vec<Resolved>>,
3026    /// Every item that already existed and that this source has written in this command, as
3027    /// it wrote it — dropped by [`TaskSource::end_command`].
3028    ///
3029    /// The other half of [`Self::created`], held on the same terms and for the reason a
3030    /// narrowed read needs it: an answer from GitHub's search or from the board's own field
3031    /// filter is an index behind a write this process made moments ago, so a query matching
3032    /// what this process just wrote onto an existing item would otherwise miss it. Nothing
3033    /// is remembered that this process did not itself just write.
3034    updated: Mutex<Vec<Resolved>>,
3035    /// Every issue this source has added a comment to or edited a comment of in this command
3036    /// — dropped by [`TaskSource::end_command`].
3037    ///
3038    /// A comment-activity read is narrowed by GitHub's issue search, whose `updated:` index
3039    /// lags the write that moved an issue's `updatedAt`, and neither [`Self::created`] nor
3040    /// [`Self::updated`] is moved by a comment, so an issue this process had just commented
3041    /// on was missing from such a read — or ruled out by the `updatedAt` its own record held
3042    /// from before — until the index caught up. Each id here is a candidate of every such
3043    /// search-narrowed read, and wherever it is a candidate its comments are read rather than
3044    /// it being ruled out by a stale `updatedAt`; that read is of the issue's own node, so it
3045    /// is current. It holds ids alone: nothing of a comment is remembered. A comment another
3046    /// process wrote is still found only once the index has it.
3047    commented: Mutex<Vec<NativeId>>,
3048    /// How fast this source writes, and how long it waits out a refusal.
3049    pacing: Pacing,
3050    /// When the last content-creating mutation finished, or the moment the furthest-out
3051    /// reserved slot releases the next one, whichever is later — so the one after it can be
3052    /// spaced from that. See [`MIN_MUTATION_INTERVAL_MS`] for the interval and
3053    /// [`GitHubProjectsSource::finish_mutation`] for why completion rather than release is
3054    /// what it is measured from.
3055    last_mutation: Mutex<Option<Duration>>,
3056    clock: SharedClock,
3057    numeric_repositories: tokio::sync::Mutex<BTreeMap<RepositoryTarget, std::num::NonZeroU64>>,
3058    /// The board as this process last read it, for the length of one command — dropped by
3059    /// [`TaskSource::end_command`].
3060    ///
3061    /// A copy of a project used to re-read the whole board, paged, before writing each of
3062    /// its items, which is by far the largest part of a copy's request count and none of
3063    /// its work. Nothing else changes this board while a command runs — this source's own
3064    /// writes are the only writer — so one read answers them all.
3065    ///
3066    /// It is not a store of a user's work and it is not the cache the no-persistence
3067    /// invariant forbids: it lives and dies with the process exactly as `created` does,
3068    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
3069    /// an item this command created and then depends on resolves whether or not GitHub's
3070    /// own eventually-consistent read has caught up. A write to an item already on the
3071    /// board updates the entry here too, so what this holds is the last read plus this
3072    /// process's own writes rather than a snapshot taken before them.
3073    board_cache: Mutex<Option<Board>>,
3074    /// Every issue this board's own search reported, for the length of one command — dropped
3075    /// by [`TaskSource::end_command`].
3076    ///
3077    /// The second half of a board read, and cached for the same reason and on the same
3078    /// terms as the first: it lives and dies with the process, nothing is written down, and
3079    /// a write this process makes updates the entry here exactly as it updates the one in
3080    /// [`Self::board_cache`]. One read answers every question a command asks, so a command
3081    /// that lists this board's projects and its tasks pays for one search rather than two.
3082    search_cache: Mutex<Option<Vec<Resolved>>>,
3083    /// What each narrowed question GitHub was asked answered, keyed by that question, for
3084    /// the length of one command — dropped by [`TaskSource::end_command`].
3085    ///
3086    /// The narrowed counterpart of [`Self::search_cache`], held on the same terms: it lives
3087    /// and dies with the process, nothing is written down, a write this process makes
3088    /// updates the entry here as it updates the other two, and every answer is completed
3089    /// with this process's own writes each time it is given. A command that asks the same
3090    /// narrowed question twice — a wait polling for its own items, a listing repeated after a
3091    /// write — pays for it once, which is what the whole-board read it replaced gave it.
3092    narrowed_cache: Mutex<BTreeMap<String, Vec<Resolved>>>,
3093    search_next: Mutex<BTreeMap<String, Option<String>>>,
3094    /// Records already resolved in this command, reused by writes and for comment identity.
3095    /// Explicit item reads still reach GitHub. Nothing is persisted, and
3096    /// [`TaskSource::end_command`] drops every record, so a write in the next command reads
3097    /// its item as a person has since left it.
3098    resolved_cache: Mutex<BTreeMap<NativeId, Resolved>>,
3099    /// Each project's sub-issues as GitHub answered them, keyed by the selector they were
3100    /// asked for under, with the project that selector named — for the length of one command,
3101    /// dropped by [`TaskSource::end_command`].
3102    ///
3103    /// A caller pages through a project's tasks one engine page at a time, and every page
3104    /// is cut from the whole list of them, so without this each page walked every
3105    /// [`graphql::SUB_ISSUES`] page again and a project of `n` listing pages cost `n²`
3106    /// requests to read once. Held on the terms [`Self::narrowed_cache`] is: it lives and
3107    /// dies with the process, nothing is written down, a write this process makes updates or
3108    /// removes the entry here as it does there, and every answer is completed with this
3109    /// process's own writes each time it is given.
3110    children_cache: Mutex<ProjectChildren>,
3111    /// The board's own id and field definitions as this process last read them on their
3112    /// own, for the length of one command — dropped by [`TaskSource::end_command`].
3113    ///
3114    /// What a write needs of the board and its item does not say, read once per command
3115    /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
3116    /// lives and dies with the process and nothing is written down. It holds no item and so
3117    /// can answer no question about one — see [`Self::board_fields`].
3118    fields_cache: Mutex<Option<BoardFields>>,
3119    /// Each destination repository's node id, resolved once per repository
3120    /// rather than per issue created.
3121    ///
3122    /// A repository's node id does not change, and re-reading it for every issue of a copy
3123    /// spent one request per item on an answer this source already had. It is a map rather
3124    /// than one entry because a copy files each item in the repository its own
3125    /// `repositories` field names, so a plan across five repositories asks GitHub five
3126    /// times and not once per item.
3127    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
3128    /// What every request this source sends is recorded into.
3129    ///
3130    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
3131    /// a request leaves this crate, so nothing has to be switched on for a session to be
3132    /// counted. It is shared rather than owned so a caller accounting for a whole session —
3133    /// its own schema verification, board lookups, residue sweep and cleanup beside this
3134    /// source's reads and writes — adds up one accounting instead of two. See
3135    /// [`accounting`] for what a record carries and what a session's spend is and is not.
3136    ledger: Arc<Accounting>,
3137    /// What [`expected`] holds for a write the store's cache expects: the journal while one is
3138    /// open, the issues primed from the cache, and the comments known on each.
3139    expected: expected::Held,
3140}
3141
3142/// GitHub's closed single-select color vocabulary.
3143#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
3144#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
3145pub enum StatusOptionColor {
3146    /// Gray.
3147    Gray,
3148    /// Blue.
3149    Blue,
3150    /// Green.
3151    Green,
3152    /// Yellow.
3153    Yellow,
3154    /// Purple.
3155    Purple,
3156    /// Red.
3157    Red,
3158    /// Orange.
3159    Orange,
3160    /// Pink.
3161    Pink,
3162}
3163
3164/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
3165/// applies its additions.
3166#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3167pub enum SetupMode {
3168    /// Read without mutation.
3169    Plan,
3170    /// Apply and verify.
3171    Apply,
3172}
3173
3174/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
3175/// against it goes on compiling.
3176pub type StatusOptionsMode = SetupMode;
3177
3178/// The explicit result of the requested operation.
3179#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3180#[serde(rename_all = "kebab-case")]
3181pub enum StatusOptionsOutcome {
3182    /// A read-only plan.
3183    Planned,
3184    /// Apply found nothing missing.
3185    Unchanged,
3186    /// Additions were applied and verified.
3187    Applied,
3188}
3189
3190/// A GitHub single-select option's opaque GraphQL node identifier.
3191#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3192#[serde(transparent)]
3193pub struct StatusOptionId(#[schemars(length(min = 1))] String);
3194
3195impl TryFrom<String> for StatusOptionId {
3196    type Error = String;
3197
3198    fn try_from(id: String) -> Result<Self, Self::Error> {
3199        if id.trim().is_empty() {
3200            return Err("a GitHub Status option id cannot be blank".to_owned());
3201        }
3202        Ok(Self(id))
3203    }
3204}
3205
3206/// One existing or proposed option in a guarded Status-field update.
3207#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3208pub struct StatusOption {
3209    /// GitHub's stable id.
3210    pub id: StatusOptionId,
3211    /// The visible option name.
3212    pub name: ColumnName,
3213    /// GitHub's single-select color token.
3214    pub color: StatusOptionColor,
3215    /// The option description, including an empty one.
3216    pub description: String,
3217}
3218
3219/// One board item's Status assignment, retained as recovery data.
3220#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3221pub struct StatusAssignment {
3222    /// The project item id whose assignment this is.
3223    // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
3224    // carried verbatim as operator recovery data; introducing a semantic type would claim
3225    // validation rules GitHub does not publish and no operation here interprets.
3226    pub item_id: String,
3227    /// The selected option, absent when the item has no status.
3228    #[serde(skip_serializing_if = "Option::is_none")]
3229    pub option: Option<AssignedStatusOption>,
3230}
3231
3232/// The inseparable id and name of an assigned option.
3233#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3234pub struct AssignedStatusOption {
3235    /// GitHub's stable id.
3236    pub id: StatusOptionId,
3237    /// The visible name.
3238    pub name: ColumnName,
3239}
3240
3241/// The plan and verified outcome of reconciling configured Status options.
3242#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3243pub struct StatusOptionsReport {
3244    /// The configured source name.
3245    pub source: SourceName,
3246    /// Configured option names absent before the operation.
3247    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
3248    // `ColumnName` and has therefore already passed its nonblank validation; retaining the
3249    // serialized string here preserves the report's intentionally simple public contract.
3250    pub missing: Vec<String>,
3251    /// What the requested operation did.
3252    pub outcome: StatusOptionsOutcome,
3253    /// The complete option list observed before any mutation.
3254    pub existing: Vec<StatusOption>,
3255}
3256
3257#[derive(Debug, Clone, PartialEq, Eq)]
3258struct StatusSnapshot {
3259    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3260    // passed back as the mutation's project identity; a newtype could enforce no stronger
3261    // invariant because GitHub publishes no grammar for it.
3262    board_id: String,
3263    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3264    // passed back as the mutation's field identity; a newtype could enforce no stronger
3265    // invariant because GitHub publishes no grammar for it.
3266    field_id: String,
3267    options: Vec<StatusOption>,
3268    assignments: Vec<StatusAssignment>,
3269}
3270
3271/// The name of the board field a status is held in.
3272const STATUS_FIELD: &str = "Status";
3273
3274/// Every item's value of each field `report` names, as it stood before the setup wrote
3275/// anything — what a person puts back when the setup is refused part way.
3276fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
3277    let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
3278        .fields
3279        .iter()
3280        .map(|field| (field.field.name(), before.assignments(field.field)))
3281        .collect();
3282    serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
3283        message: format!("cannot render the pre-write field recovery snapshot: {error}"),
3284    })
3285}
3286
3287/// One board field the guarded setup reads and writes — every one it reads, and the only
3288/// ones it writes.
3289#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
3290pub enum BoardField {
3291    /// The single-select `Status` field every instance's `status_mapping` resolves into.
3292    Status,
3293    /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
3294    Priority,
3295}
3296
3297impl BoardField {
3298    /// The field's name on the board.
3299    #[must_use]
3300    pub const fn name(self) -> &'static str {
3301        match self {
3302            Self::Status => STATUS_FIELD,
3303            Self::Priority => PRIORITY_FIELD,
3304        }
3305    }
3306
3307    /// The field a board calls `name`, or `None` for one this setup does not own.
3308    fn named(name: &str) -> Option<Self> {
3309        [Self::Status, Self::Priority]
3310            .into_iter()
3311            .find(|field| field.name() == name)
3312    }
3313}
3314
3315/// What the guarded setup did to one field.
3316#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3317#[serde(rename_all = "kebab-case")]
3318pub enum FieldOutcome {
3319    /// A read-only plan.
3320    Planned,
3321    /// Apply found the field there with every configured option.
3322    Unchanged,
3323    /// Missing options were added to the field that was there, and verified.
3324    Applied,
3325    /// The field was not there; it was created holding the configured options, and verified.
3326    Created,
3327}
3328
3329/// One field's plan, or its verified outcome.
3330#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3331pub struct FieldReport {
3332    /// Which field.
3333    pub field: BoardField,
3334    /// Whether the board had the field before the operation.
3335    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
3336    // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
3337    // "outcome", "existing"}` — so folding one into the other would change a published JSON
3338    // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
3339    // one constructor, and it derives `outcome` from `exists` in one match.
3340    pub exists: bool,
3341    /// Configured option names the field lacked before the operation — every one of them,
3342    /// in the order a new field lists them, when the field was not there at all.
3343    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3344    // mapping name and has therefore already passed its nonblank validation; the serialized
3345    // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
3346    pub missing: Vec<String>,
3347    /// For the `Status` field, which item kind each missing name is configured for: one
3348    /// entry per kind `status_mapping` names a missing option for, task before project, each
3349    /// listing that kind's missing names in category order. A name both kinds use is in
3350    /// both. Empty — and left out of the JSON — when nothing is missing, and always for
3351    /// `Priority`, which only a task holds.
3352    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3353    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3354    // both SDKs model an absent `kinds` as an empty list rather than as `null`.
3355    #[schemars(!skip_serializing_if)]
3356    pub kinds: Vec<KindMissing>,
3357    /// What the requested operation did.
3358    pub outcome: FieldOutcome,
3359    /// The field's complete option list observed before any mutation; empty when the field
3360    /// was not there.
3361    pub existing: Vec<StatusOption>,
3362}
3363
3364/// The `Status` option names one item kind's `status_mapping` names that the field lacked.
3365#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3366pub struct KindMissing {
3367    /// The kind these names are configured for.
3368    pub kind: ItemKind,
3369    /// The names that kind maps a category to and the field lacked, in category order.
3370    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
3371    // mapping name, as `FieldReport::missing`'s do, and the serialized string is the report's
3372    // intentionally simple public contract.
3373    pub missing: Vec<String>,
3374}
3375
3376/// The plan and verified outcome of setting up every field a source's configuration names.
3377#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3378pub struct FieldsReport {
3379    /// The configured source name.
3380    pub source: SourceName,
3381    /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
3382    // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
3383    // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
3384    // per field would change a published JSON shape. The states the list could hold and the
3385    // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
3386    // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
3387    pub fields: Vec<FieldReport>,
3388    /// Each board text field the source's `metadata_fields` projects a value onto, in
3389    /// configured order. Empty — and left out of the JSON — when it projects none.
3390    #[serde(default, skip_serializing_if = "Vec::is_empty")]
3391    // Kept in the schema as `"default": []` although the JSON leaves an empty list out, so
3392    // both SDKs model an absent list as an empty one rather than as `null`.
3393    #[schemars(!skip_serializing_if)]
3394    pub metadata_fields: Vec<MetadataFieldReport>,
3395}
3396
3397/// The type of a board field that is not a text field, as GitHub names it — its `dataType`, or
3398/// its GraphQL type when it has none — which is what a projected field's conflict reports.
3399///
3400/// Never empty, never beginning with whitespace, and never `TEXT`: a text field of that name is
3401/// no conflict.
3402// The schema states that rule as `NON_TEXT_FIELD_TYPE_PATTERN`, so a generated SDK model
3403// refuses exactly what `try_from` below does.
3404#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3405#[serde(transparent)]
3406pub struct NonTextFieldType(#[schemars(regex(pattern = NON_TEXT_FIELD_TYPE_PATTERN))] String);
3407
3408/// What a [`NonTextFieldType`] may be, as the emitted schema states it: a first character that
3409/// is not whitespace, and anything but exactly `TEXT` — spelled without a lookahead, which
3410/// neither SDK's validator can be relied on to support.
3411const NON_TEXT_FIELD_TYPE_PATTERN: &str =
3412    r"^(?:[^T\s]|T(?:[^E]|$)|TE(?:[^X]|$)|TEX(?:[^T]|$)|TEXT[\s\S])";
3413
3414impl TryFrom<String> for NonTextFieldType {
3415    type Error = String;
3416
3417    fn try_from(kind: String) -> Result<Self, Self::Error> {
3418        if kind.is_empty() || kind.starts_with(char::is_whitespace) {
3419            return Err("a board field's type cannot be blank or begin with whitespace".to_owned());
3420        }
3421        if kind == "TEXT" {
3422            return Err("a TEXT field is no conflict for a projected text field".to_owned());
3423        }
3424        Ok(Self(kind))
3425    }
3426}
3427
3428impl std::fmt::Display for NonTextFieldType {
3429    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3430        formatter.write_str(&self.0)
3431    }
3432}
3433
3434/// One projected metadata text field's plan, or its verified outcome.
3435///
3436/// The setup creates a missing one as a text field and never alters a field that is there:
3437/// a same-named field of another type is reported in `conflict` and left as it is.
3438#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
3439pub struct MetadataFieldReport {
3440    /// The board field's name.
3441    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.
3442    /// The metadata key the value is read from.
3443    pub key: String, // llmlint: ignore[invalid_states_unrepresentable] As `field`: a validated configuration value reported back as written.
3444    /// The object keys walked inside that key's value; empty for the key's own value.
3445    pub path: Vec<String>,
3446    /// Whether the board had a text field of that name before the operation.
3447    // 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.
3448    pub exists: bool,
3449    /// The type of a field of that name that is not a text field, which the setup leaves as
3450    /// it is; absent — and left out of the JSON — when there is none.
3451    #[serde(default, skip_serializing_if = "Option::is_none")]
3452    pub conflict: Option<NonTextFieldType>,
3453    /// What the requested operation did: `planned`; `created` for a field that was not
3454    /// there; `unchanged` for one that was, and for a conflict, which is never changed.
3455    pub outcome: FieldOutcome,
3456}
3457
3458/// Which options one field is configured with, in the order a new field would list them.
3459struct FieldPlan {
3460    field: BoardField,
3461    wanted: Vec<String>,
3462}
3463
3464/// One single-select field as the guarded setup snapshots it.
3465#[derive(Debug, Clone, PartialEq, Eq)]
3466struct SnapshotField {
3467    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3468    // passed back as the mutation's field identity; a newtype could enforce no stronger
3469    // invariant because GitHub publishes no grammar for it.
3470    field_id: String,
3471    options: Vec<StatusOption>,
3472}
3473
3474/// Every single-select field of a board and every item's value of each.
3475#[derive(Debug, Clone, PartialEq, Eq)]
3476struct BoardSnapshot {
3477    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
3478    // passed back as the mutation's project identity; a newtype could enforce no stronger
3479    // invariant because GitHub publishes no grammar for it.
3480    board_id: String,
3481    fields: BTreeMap<BoardField, SnapshotField>,
3482    /// Each board item's id, and its value of each field this setup owns that it holds one of.
3483    items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
3484}
3485
3486impl BoardSnapshot {
3487    /// Every item's value of `field`, in board order — the recovery data a drift refusal
3488    /// carries.
3489    fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
3490        self.items
3491            .iter()
3492            .map(|(item_id, values)| StatusAssignment {
3493                item_id: item_id.clone(),
3494                option: values.get(&field).cloned(),
3495            })
3496            .collect()
3497    }
3498}
3499
3500impl GitHubProjectsSource {
3501    /// Report missing configured Status options and, when `apply` is true, add them with
3502    /// a whole-list mutation that preserves every existing id and verifies the result.
3503    ///
3504    /// # Errors
3505    ///
3506    /// Refuses a board without a single-select `Status` field. A post-write difference in
3507    /// any pre-existing option id or item assignment is refused with the complete pre-write
3508    /// assignment snapshot in the diagnostic for recovery.
3509    // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
3510    // successful mutation, both drift refusals, source selection, missing Status, casing,
3511    // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
3512    // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
3513    // responses from entering the defensive malformed-response branches below.
3514    pub async fn status_options(
3515        &self,
3516        mode: StatusOptionsMode,
3517    ) -> Result<StatusOptionsReport, SourceError> {
3518        let before = self.status_snapshot().await?;
3519        // A terminal category's option is as configured as an open one's: a terminal
3520        // write validates it before closing and refuses when the board lacks it. Both
3521        // kinds' names are options of the one field, so both are asked for.
3522        let missing = self
3523            .statuses
3524            .wanted()
3525            .into_iter()
3526            .filter(|wanted| {
3527                !before
3528                    .options
3529                    .iter()
3530                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3531            })
3532            .collect::<Vec<_>>();
3533        let report = StatusOptionsReport {
3534            source: self.name.clone(),
3535            missing: missing.clone(),
3536            outcome: match (mode, missing.is_empty()) {
3537                (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
3538                (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
3539                (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
3540            },
3541            existing: before.options.clone(),
3542        };
3543        if mode == StatusOptionsMode::Plan || missing.is_empty() {
3544            return Ok(report);
3545        }
3546        let mut options = before
3547            .options
3548            .iter()
3549            .map(|option| {
3550                json!({
3551                    "id": option.id, "name": option.name, "color": option.color,
3552                    "description": option.description,
3553                })
3554            })
3555            .collect::<Vec<_>>();
3556        options.extend(missing.iter().map(|name| {
3557            json!({
3558                "name": name, "color": "GRAY", "description": ""
3559            })
3560        }));
3561        self.graphql(
3562            graphql::STATUS_OPTIONS_UPDATE,
3563            json!({"input": {
3564                "projectId": before.board_id, "fieldId": before.field_id,
3565                "singleSelectOptions": options,
3566            }}),
3567        )
3568        .await?;
3569        let after = self.status_snapshot().await?;
3570        let options_preserved = before
3571            .options
3572            .iter()
3573            .all(|old| after.options.iter().any(|new| new == old));
3574        let additions_present = missing.iter().all(|wanted| {
3575            after
3576                .options
3577                .iter()
3578                .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3579        });
3580        if !options_preserved || !additions_present || after.assignments != before.assignments {
3581            let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
3582                SourceError::Malformed {
3583                    message: format!("cannot render pre-write Status recovery snapshot: {error}"),
3584                }
3585            })?;
3586            return Err(SourceError::Refused {
3587                message: format!(
3588                    "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}"
3589                ),
3590            });
3591        }
3592        Ok(report)
3593    }
3594
3595    /// A fresh snapshot of the Status field and every board item's assignment of it.
3596    ///
3597    /// # Errors
3598    ///
3599    /// Refuses a board without a single-select `Status` field, and one the token cannot see.
3600    async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
3601        // Status alone, as this operation has always read it: a `Priority` field is another
3602        // operation's, so nothing about it can refuse this one.
3603        let mut board = self.board_snapshot(&[BoardField::Status]).await?;
3604        let field = board
3605            .fields
3606            .remove(&BoardField::Status)
3607            .ok_or_else(|| self.no_status_field())?;
3608        Ok(StatusSnapshot {
3609            assignments: board.assignments(BoardField::Status),
3610            board_id: board.board_id,
3611            field_id: field.field_id,
3612            options: field.options,
3613        })
3614    }
3615
3616    /// The refusal a board with no `Status` field is answered with by the guarded setup.
3617    fn no_status_field(&self) -> SourceError {
3618        SourceError::Refused {
3619            message: format!("source {} board has no Status field", self.name),
3620        }
3621    }
3622
3623    // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
3624    // the real CLI loopback journey, including pagination. The individual malformed guards
3625    // are defensive validation of a schema-pinned third-party response, not separate user
3626    // journeys; drift and missing-field failures cover the operation's recovery behavior.
3627    /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
3628    /// every board item's value of each, walked to the end of the board's items. A field not
3629    /// in `owned` is read past whatever it holds.
3630    async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
3631        let mut after: Option<String> = None;
3632        let mut snapshot: Option<BoardSnapshot> = None;
3633        loop {
3634            let data = self
3635                .graphql(
3636                    graphql::STATUS_OPTIONS_SNAPSHOT,
3637                    json!({
3638                        "owner": self.owner, "number": self.project_number,
3639                        "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
3640                    }),
3641                )
3642                .await?;
3643            let board = data
3644                .pointer("/owner/projectV2")
3645                .filter(|board| board.is_object())
3646                .ok_or_else(|| SourceError::Refused {
3647                    message: format!(
3648                        "source {} has no accessible GitHub Projects board",
3649                        self.name
3650                    ),
3651                })?;
3652            if board
3653                .pointer("/fields/pageInfo/hasNextPage")
3654                .and_then(Value::as_bool)
3655                != Some(false)
3656            {
3657                return Err(SourceError::Malformed {
3658                    message:
3659                        "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
3660                            .into(),
3661                });
3662            }
3663            let mut fields = BTreeMap::new();
3664            // Only the fields this setup owns, by name: a node the single-select fragment did not
3665            // match carries no name, and a person's own single-select field — a `Size`, a
3666            // `Team` — is none of this setup's business, so nothing about it can refuse one. A
3667            // `Status` or `Priority` field without its options is malformed, not absent.
3668            // 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.
3669            for (owned, field) in board
3670                .pointer("/fields/nodes")
3671                .and_then(Value::as_array)
3672                .ok_or_else(|| SourceError::Malformed {
3673                    message: "GitHub project fields.nodes is not an array".into(),
3674                })?
3675                .iter()
3676                .filter_map(|field| {
3677                    let named = BoardField::named(field.get("name")?.as_str()?)?;
3678                    owned.contains(&named).then_some((named, field))
3679                })
3680            {
3681                let options = field
3682                    .get("options")
3683                    .and_then(Value::as_array)
3684                    .ok_or_else(|| SourceError::Malformed {
3685                        message: "GitHub single-select field options is not an array".into(),
3686                    })?
3687                    .iter()
3688                    .map(|option| {
3689                        Ok(StatusOption {
3690                            id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
3691                                .map_err(|message| SourceError::Malformed { message })?,
3692                            name: ColumnName::try_from(required_str(option, "name")?.to_owned())
3693                                .map_err(|message| SourceError::Malformed {
3694                                    message: format!(
3695                                        "GitHub single-select option name is invalid: {message}"
3696                                    ),
3697                                })?,
3698                            color: serde_json::from_value(
3699                                option.get("color").cloned().unwrap_or(Value::Null),
3700                            )
3701                            .map_err(|error| {
3702                                SourceError::Malformed {
3703                                    message: format!(
3704                                        "GitHub single-select option color is invalid: {error}"
3705                                    ),
3706                                }
3707                            })?,
3708                            description: optional_str(option, "description")?
3709                                .unwrap_or_default()
3710                                .to_owned(),
3711                        })
3712                    })
3713                    .collect::<Result<Vec<_>, SourceError>>()?;
3714                let snapshot = SnapshotField {
3715                    field_id: required_nonblank_str(field, "id")?.to_owned(),
3716                    options,
3717                };
3718                // A board's field names are unique, so a second one is an answer that cannot
3719                // say which field the setup would act on — refused rather than one chosen.
3720                if fields.insert(owned, snapshot).is_some() {
3721                    return Err(SourceError::Malformed {
3722                        message: format!(
3723                            "GitHub answered two {} fields for this board",
3724                            owned.name()
3725                        ),
3726                    });
3727                }
3728            }
3729            let board_id = required_nonblank_str(board, "id")?.to_owned();
3730            let current = snapshot.get_or_insert_with(|| BoardSnapshot {
3731                board_id,
3732                fields,
3733                items: Vec::new(),
3734            });
3735            let items = board
3736                .pointer("/items/nodes")
3737                .and_then(Value::as_array)
3738                .ok_or_else(|| SourceError::Malformed {
3739                    message: "GitHub project items.nodes is not an array".into(),
3740                })?;
3741            for item in items {
3742                let field_values =
3743                    item.get("fieldValues")
3744                        .ok_or_else(|| SourceError::Malformed {
3745                            message: "GitHub project item is missing fieldValues".into(),
3746                        })?;
3747                if field_values
3748                    .pointer("/pageInfo/hasNextPage")
3749                    .and_then(Value::as_bool)
3750                    != Some(false)
3751                {
3752                    return Err(SourceError::Malformed {
3753                        message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
3754                    });
3755                }
3756                let values = item
3757                    .pointer("/fieldValues/nodes")
3758                    .and_then(Value::as_array)
3759                    .ok_or_else(|| SourceError::Malformed {
3760                        message: "GitHub project item fieldValues.nodes is not an array".into(),
3761                    })?;
3762                let item_id = required_nonblank_str(item, "id")?;
3763                let mut assigned = BTreeMap::new();
3764                for value in values {
3765                    let Some(field) = value
3766                        .pointer("/field/name")
3767                        .and_then(Value::as_str)
3768                        .and_then(BoardField::named)
3769                        .filter(|field| owned.contains(field))
3770                    else {
3771                        continue;
3772                    };
3773                    let held = assigned.insert(
3774                        field,
3775                        AssignedStatusOption {
3776                            id: StatusOptionId::try_from(
3777                                required_str(value, "optionId")?.to_owned(),
3778                            )
3779                            .map_err(|message| SourceError::Malformed { message })?,
3780                            name: ColumnName::try_from(required_str(value, "name")?.to_owned())
3781                                .map_err(|message| SourceError::Malformed {
3782                                    message: format!(
3783                                        "GitHub assigned {} name is invalid: {message}",
3784                                        field.name()
3785                                    ),
3786                                })?,
3787                        },
3788                    );
3789                    // An item holds one value of a field, so a second one leaves no way to
3790                    // tell which it holds — and a verification or recovery built on either
3791                    // could restore the wrong one.
3792                    if held.is_some() {
3793                        return Err(SourceError::Malformed {
3794                            message: format!(
3795                                "GitHub answered two {} values for board item {item_id}",
3796                                field.name()
3797                            ),
3798                        });
3799                    }
3800                }
3801                current.items.push((item_id.to_owned(), assigned));
3802            }
3803            let page = board.get("items").ok_or_else(|| SourceError::Malformed {
3804                message: "GitHub project is missing items".into(),
3805            })?;
3806            let has_next = page
3807                .pointer("/pageInfo/hasNextPage")
3808                .and_then(Value::as_bool)
3809                .ok_or_else(|| SourceError::Malformed {
3810                    message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
3811                })?;
3812            if !has_next {
3813                break;
3814            }
3815            let next =
3816                required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
3817            validate_cursor_progress(after.as_deref(), next)?;
3818            after = Some(next.to_owned());
3819        }
3820        snapshot.ok_or_else(|| SourceError::Malformed {
3821            message: "GitHub returned no board field snapshot".into(),
3822        })
3823    }
3824    // llmlint: ignore-end[changed_behavior_has_e2e]
3825
3826    /// Report every board field this source's configuration names and, with
3827    /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
3828    /// the `Priority` field when the board has none.
3829    ///
3830    /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
3831    /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
3832    /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
3833    /// color and description: the whole option list goes back with every existing id, because
3834    /// a re-minted id clears every item's value.
3835    ///
3836    /// # Errors
3837    ///
3838    /// Refuses a board without a single-select `Status` field. After an apply the board is
3839    /// read again, and a pre-existing option or any item's value of either field that moved is
3840    /// refused with the complete pre-write assignments in the diagnostic, for recovery.
3841    // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
3842    // unchanged apply, a created field, an added option to each field, drift refusal, a board
3843    // with no Status field and a non-github-projects source through the compiled CLI against
3844    // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
3845    pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
3846        let owned: Vec<BoardField> = if self.priorities.is_some() {
3847            vec![BoardField::Status, BoardField::Priority]
3848        } else {
3849            vec![BoardField::Status]
3850        };
3851        let before = self.board_snapshot(&owned).await?;
3852        let mut plans = vec![FieldPlan {
3853            field: BoardField::Status,
3854            wanted: self.statuses.wanted(),
3855        }];
3856        if !before.fields.contains_key(&BoardField::Status) {
3857            return Err(self.no_status_field());
3858        }
3859        if let Some(mapping) = &self.priorities {
3860            plans.push(FieldPlan {
3861                field: BoardField::Priority,
3862                wanted: mapping.names().map(str::to_owned).collect(),
3863            });
3864        }
3865        // The snapshot reads single-select fields alone, so a field it did not find may still
3866        // be on the board under the name, of another type: creating one beside it would fail
3867        // part way, or leave two fields of one name. Asked of the board's own field list, and
3868        // only when a field is missing.
3869        if plans
3870            .iter()
3871            .any(|plan| !before.fields.contains_key(&plan.field))
3872        {
3873            let board = self.board_fields().await?;
3874            for plan in plans
3875                .iter()
3876                .filter(|plan| !before.fields.contains_key(&plan.field))
3877            {
3878                if let Some(field) = Board::field(&board.fields, plan.field.name())? {
3879                    return Err(SourceError::Refused {
3880                        message: format!(
3881                            "source {}'s board has a {} field that is not a single-select field \
3882                             (it is a {}), so it cannot hold this source's options; next: rename \
3883                             or remove that field, then run this again",
3884                            self.name,
3885                            plan.field.name(),
3886                            optional_str(field, "__typename")?.unwrap_or("field of another type")
3887                        ),
3888                    });
3889                }
3890            }
3891        }
3892        let mut reports = Vec::new();
3893        for plan in &plans {
3894            let held = before.fields.get(&plan.field);
3895            let existing = held.map(|field| field.options.clone()).unwrap_or_default();
3896            let mut missing: Vec<String> = Vec::new();
3897            for wanted in &plan.wanted {
3898                let present = existing
3899                    .iter()
3900                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
3901                    || missing
3902                        .iter()
3903                        .any(|named| named.eq_ignore_ascii_case(wanted));
3904                if !present {
3905                    missing.push(wanted.clone());
3906                }
3907            }
3908            let kinds = match plan.field {
3909                BoardField::Status => self.statuses.missing_by_kind(&existing),
3910                BoardField::Priority => Vec::new(),
3911            };
3912            reports.push(FieldReport {
3913                field: plan.field,
3914                exists: held.is_some(),
3915                kinds,
3916                outcome: match (mode, held.is_some(), missing.is_empty()) {
3917                    (SetupMode::Plan, _, _) => FieldOutcome::Planned,
3918                    (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
3919                    (SetupMode::Apply, true, false) => FieldOutcome::Applied,
3920                    (SetupMode::Apply, false, _) => FieldOutcome::Created,
3921                },
3922                missing,
3923                existing,
3924            });
3925        }
3926        // A projected field is a text field, which the single-select snapshot cannot see, so
3927        // it is asked of the board's own field list — and only when one is configured.
3928        let mut metadata_fields = Vec::new();
3929        if !self.metadata_fields.is_empty() {
3930            let board = self.board_fields().await?;
3931            for projection in &self.metadata_fields {
3932                let conflict = match Board::field(&board.fields, &projection.field)? {
3933                    None => None,
3934                    Some(field) => {
3935                        let typename = optional_str(field, "__typename")?;
3936                        let data_type = optional_str(field, "dataType")?;
3937                        if typename == Some("ProjectV2Field") && data_type == Some("TEXT") {
3938                            None
3939                        } else {
3940                            let named = data_type
3941                                .filter(|kind| *kind != "TEXT")
3942                                .or(typename)
3943                                .unwrap_or("field of another type");
3944                            Some(NonTextFieldType::try_from(named.to_owned()).map_err(
3945                                |message| SourceError::Malformed {
3946                                    message: format!(
3947                                        "GitHub answered the board's {} field with a type this \
3948                                         setup cannot read: {message}",
3949                                        &*projection.field
3950                                    ),
3951                                },
3952                            )?)
3953                        }
3954                    }
3955                };
3956                let exists =
3957                    conflict.is_none() && Board::field(&board.fields, &projection.field)?.is_some();
3958                metadata_fields.push(MetadataFieldReport {
3959                    field: projection.field.to_string(),
3960                    key: projection.key.to_string(),
3961                    path: projection.path.clone(),
3962                    exists,
3963                    outcome: match (mode, exists || conflict.is_some()) {
3964                        (SetupMode::Plan, _) => FieldOutcome::Planned,
3965                        (SetupMode::Apply, true) => FieldOutcome::Unchanged,
3966                        (SetupMode::Apply, false) => FieldOutcome::Created,
3967                    },
3968                    conflict,
3969                });
3970            }
3971        }
3972        let report = FieldsReport {
3973            source: self.name.clone(),
3974            fields: reports,
3975            metadata_fields,
3976        };
3977        let writes: Vec<&FieldReport> = report
3978            .fields
3979            .iter()
3980            .filter(|field| !field.missing.is_empty() || !field.exists)
3981            .collect();
3982        let creates: Vec<&MetadataFieldReport> = report
3983            .metadata_fields
3984            .iter()
3985            .filter(|field| field.outcome == FieldOutcome::Created)
3986            .collect();
3987        if mode == SetupMode::Plan || (writes.is_empty() && creates.is_empty()) {
3988            return Ok(report);
3989        }
3990        let mut landed: Vec<&str> = Vec::new();
3991        for field in &writes {
3992            let added = field
3993                .missing
3994                .iter()
3995                .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
3996            let sent = match before.fields.get(&field.field) {
3997                Some(held) => {
3998                    let mut options = held
3999                        .options
4000                        .iter()
4001                        .map(|option| {
4002                            json!({
4003                                "id": option.id, "name": option.name, "color": option.color,
4004                                "description": option.description,
4005                            })
4006                        })
4007                        .collect::<Vec<_>>();
4008                    options.extend(added);
4009                    self.graphql(
4010                        graphql::STATUS_OPTIONS_UPDATE,
4011                        json!({"input": {
4012                            "projectId": before.board_id, "fieldId": held.field_id,
4013                            "singleSelectOptions": options,
4014                        }}),
4015                    )
4016                    .await
4017                }
4018                None => {
4019                    self.graphql(
4020                        graphql::CREATE_FIELD,
4021                        json!({"input": {
4022                            "projectId": before.board_id, "dataType": "SINGLE_SELECT",
4023                            "name": field.field.name(),
4024                            "singleSelectOptions": added.collect::<Vec<_>>(),
4025                        }}),
4026                    )
4027                    .await
4028                }
4029            };
4030            // A mutation that failed does not establish that GitHub left its field as it was,
4031            // so every failure from here on carries the recovery data a drift refusal does.
4032            match sent {
4033                Ok(_) => landed.push(field.field.name()),
4034                Err(error) => {
4035                    let changed = if landed.is_empty() {
4036                        String::new()
4037                    } else {
4038                        format!("changed the {} field and then ", landed.join(" and "))
4039                    };
4040                    return Err(SourceError::Refused {
4041                        message: format!(
4042                            "the guarded field setup {changed}failed on the {} field, which it may \
4043                             have changed part way: {error}; the pre-write item assignments \
4044                             are:\n{}",
4045                            field.field.name(),
4046                            recovery(&report, &before)?
4047                        ),
4048                    });
4049                }
4050            }
4051        }
4052        for field in &creates {
4053            let sent = self
4054                .graphql(
4055                    graphql::CREATE_FIELD,
4056                    json!({"input": {
4057                        "projectId": before.board_id, "dataType": "TEXT", "name": field.field,
4058                    }}),
4059                )
4060                .await;
4061            if let Err(error) = sent {
4062                let changed = if landed.is_empty() {
4063                    String::new()
4064                } else {
4065                    format!("changed the {} field and then ", landed.join(" and "))
4066                };
4067                return Err(SourceError::Refused {
4068                    message: format!(
4069                        "the guarded field setup {changed}failed creating the {} text field: \
4070                         {error}; run it again to create what is still missing",
4071                        field.field
4072                    ),
4073                });
4074            }
4075            landed.push(&field.field);
4076        }
4077        // The board has been written, so a verification read that fails leaves it unverified
4078        // rather than unchanged, and says what to put back.
4079        let after = match self.board_snapshot(&owned).await {
4080            Ok(after) => after,
4081            Err(error) => {
4082                return Err(SourceError::Refused {
4083                    message: format!(
4084                        "the guarded field setup changed the {} field and then could not read the \
4085                         board back to verify it: {error}; the pre-write item assignments are:\n{}",
4086                        landed.join(" and "),
4087                        recovery(&report, &before)?
4088                    ),
4089                });
4090            }
4091        };
4092        let mut moved = Vec::new();
4093        for field in &report.fields {
4094            let name = field.field.name();
4095            let now = after
4096                .fields
4097                .get(&field.field)
4098                .map(|held| held.options.as_slice())
4099                .unwrap_or_default();
4100            if !field.existing.iter().all(|old| now.contains(old)) {
4101                moved.push(format!(
4102                    "a pre-existing {name} option id, name, color or description"
4103                ));
4104            }
4105            if !field.missing.iter().all(|wanted| {
4106                now.iter()
4107                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
4108            }) {
4109                moved.push(format!("an added {name} option"));
4110            }
4111            if after.assignments(field.field) != before.assignments(field.field) {
4112                moved.push(format!("an item's {name} value"));
4113            }
4114        }
4115        if !creates.is_empty() {
4116            // Read afresh: the view held for this command is the one from before the creates.
4117            *self.fields_cache()? = None;
4118            let board = match self.board_fields().await {
4119                Ok(board) => board,
4120                Err(error) => {
4121                    return Err(SourceError::Refused {
4122                        message: format!(
4123                            "the guarded field setup changed the {} field and then could not \
4124                             read the board back to verify it: {error}; run it again to verify \
4125                             it, which creates nothing a second time",
4126                            landed.join(" and ")
4127                        ),
4128                    });
4129                }
4130            };
4131            for field in &creates {
4132                let text = Board::field(&board.fields, &field.field)?.is_some_and(|held| {
4133                    held.get("__typename").and_then(Value::as_str) == Some("ProjectV2Field")
4134                        && held.get("dataType").and_then(Value::as_str) == Some("TEXT")
4135                });
4136                if !text {
4137                    moved.push(format!("the created {} text field", field.field));
4138                }
4139            }
4140        }
4141        if !moved.is_empty() {
4142            return Err(SourceError::Refused {
4143                message: format!(
4144                    "GitHub changed {} after the guarded field setup; the pre-write item \
4145                     assignments are:\n{}",
4146                    moved.join(", "),
4147                    recovery(&report, &before)?
4148                ),
4149            });
4150        }
4151        Ok(report)
4152    }
4153
4154    /// Validate configuration and capture the named credential without exposing it.
4155    ///
4156    /// # Errors
4157    ///
4158    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
4159    /// [`SourceError::Auth`] when the named credential is missing or empty.
4160    pub fn new(
4161        name: &SourceName,
4162        config: GitHubProjectsConfig,
4163        secrets: &dyn SecretResolver,
4164    ) -> Result<Self, SourceError> {
4165        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
4166    }
4167
4168    /// The same, recording every request it sends into an accounting the caller holds too.
4169    ///
4170    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
4171    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
4172    /// up — passes the one it records those into, so the session total accounts for the
4173    /// whole session rather than for this source's share of it.
4174    ///
4175    /// # Errors
4176    ///
4177    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
4178    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
4179    pub fn recording_into(
4180        name: &SourceName,
4181        config: GitHubProjectsConfig,
4182        secrets: &dyn SecretResolver,
4183        ledger: Arc<Accounting>,
4184    ) -> Result<Self, SourceError> {
4185        if !valid_github_owner(&config.owner) {
4186            return Err(SourceError::Config {
4187                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
4188            });
4189        }
4190        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
4191            return Err(SourceError::Config {
4192                message: format!("project_number must be between 1 and {}", i32::MAX),
4193            });
4194        }
4195        if !valid_environment_name(&config.token_env) {
4196            return Err(SourceError::Config {
4197                message: "token_env must be a valid environment-variable name".into(),
4198            });
4199        }
4200        let repository = config
4201            .repository
4202            .as_deref()
4203            .map(RepositoryTarget::parse)
4204            .transpose()?;
4205        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
4206            message: format!("endpoint is not a valid URL: {e}"),
4207        })?;
4208        if endpoint.scheme() != "https"
4209            && !(endpoint.scheme() == "http"
4210                && endpoint
4211                    .host_str()
4212                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
4213        {
4214            return Err(SourceError::Config {
4215                message:
4216                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
4217                        .into(),
4218            });
4219        }
4220        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
4221            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),
4222        })?;
4223        Ok(Self {
4224            name: name.clone(),
4225            owner: config.owner,
4226            project_number: config.project_number,
4227            repository,
4228            asset_client: assets::client(&endpoint)?,
4229            endpoint,
4230            token,
4231            credential_name: config.token_env,
4232            statuses: BoardStatuses::resolve(&config.status_mapping, name)?,
4233            priorities: config
4234                .priority_mapping
4235                .map(|mapping| PriorityMapping::resolve(mapping, name))
4236                .transpose()?,
4237            metadata_fields: MetadataField::resolve(config.metadata_fields, name)?,
4238            client: Client::builder()
4239                .user_agent("onetaskgraph")
4240                .build()
4241                .map_err(|e| SourceError::Config {
4242                    message: format!("cannot build HTTP client: {e}"),
4243                })?,
4244            created: Mutex::new(Vec::new()),
4245            updated: Mutex::new(Vec::new()),
4246            commented: Mutex::new(Vec::new()),
4247            pacing: Pacing::resolve(config.pacing, name)?,
4248            last_mutation: Mutex::new(None),
4249            clock: system_clock(),
4250            numeric_repositories: tokio::sync::Mutex::new(BTreeMap::new()),
4251            board_cache: Mutex::new(None),
4252            search_cache: Mutex::new(None),
4253            narrowed_cache: Mutex::new(BTreeMap::new()),
4254            resolved_cache: Mutex::new(BTreeMap::new()),
4255            children_cache: Mutex::new(BTreeMap::new()),
4256            search_next: Mutex::new(BTreeMap::new()),
4257            fields_cache: Mutex::new(None),
4258            repository_cache: Mutex::new(BTreeMap::new()),
4259            ledger,
4260            expected: expected::Held::default(),
4261        })
4262    }
4263
4264    /// A snapshot of every request this source has sent, and what each cost.
4265    ///
4266    /// A value to hold and compare rather than a borrow of the accounting itself, so two
4267    /// of them can sit side by side. When this source was built with
4268    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
4269    /// point of building it that way.
4270    #[must_use]
4271    pub fn accounting(&self) -> accounting::Session {
4272        self.ledger.snapshot()
4273    }
4274
4275    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
4276    /// rate limit rather than handing it straight back as an error.
4277    ///
4278    /// Retrying is safe for every document here, including the mutations, and the reason
4279    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
4280    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
4281    /// this replays has already taken effect. An outcome this source cannot know — the
4282    /// send failed, or the body could not be read, so the mutation may well have landed —
4283    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
4284    /// attempt. A duplicate write would come from replaying one of those, and none is
4285    /// replayed.
4286    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4287        if !self.expected.journaling() {
4288            return self.graphql_sent(query, variables).await;
4289        }
4290        // While a write the store's cache expects runs, its mutations select the whole issue
4291        // they leave behind, so the response vouches for what the cache keeps — and every
4292        // request is recorded, so the write's parts are reported from their own responses.
4293        let (query, variables) = match graphql::whole_twin(query) {
4294            Some(twin) => (twin, expected::whole_variables(variables)),
4295            None => (query, variables),
4296        };
4297        let sent = self.graphql_sent(query, variables.clone()).await;
4298        self.journal(query, &variables, &sent).await;
4299        sent
4300    }
4301
4302    /// One document sent, retried while GitHub asks for a wait, exactly as asked.
4303    async fn graphql_sent(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
4304        if is_mutation(query)
4305            && ![
4306                graphql::ADD_COMMENT,
4307                graphql::UPDATE_COMMENT,
4308                graphql::DELETE_COMMENT,
4309                graphql::ADD_COMMENT_WHOLE,
4310                graphql::UPDATE_COMMENT_WHOLE,
4311            ]
4312            .contains(&query)
4313        {
4314            let mut cache = self.resolved_cache()?;
4315            for argument in ["input", "second", "third", "clear"] {
4316                if let Some(input) = variables.get(argument) {
4317                    cache.retain(|id, item| {
4318                        !["id", "issueId", "subjectId", "itemId"].iter().any(|key| {
4319                            input
4320                                .get(key)
4321                                .and_then(Value::as_str)
4322                                .is_some_and(|value| value == id.0 || value == item.item_id)
4323                        })
4324                    });
4325                }
4326            }
4327        }
4328        let doing = operation_description(query);
4329        let mut waited = Duration::ZERO;
4330        let mut waits = 0_u32;
4331        let mut backoff = self.pacing.retry_backoff;
4332        loop {
4333            if is_mutation(query) {
4334                let spacing = self.reserve_mutation_slot();
4335                if !spacing.is_zero() {
4336                    self.clock.sleep(spacing).await;
4337                }
4338            }
4339            let attempt = self.send_once(query, &variables, waits + 1).await;
4340            if is_mutation(query) {
4341                self.finish_mutation();
4342            }
4343            let limited = match attempt {
4344                Ok(data) => return Ok(data),
4345                Err(Attempt::Failed(error)) => return Err(error),
4346                Err(Attempt::Limited(limited)) => limited,
4347            };
4348            // GitHub really does send `retry-after: 0`, and retrying at once is the one
4349            // move that extends a secondary limit, so a hint below the schedule's own next
4350            // wait is raised to it.
4351            let wait = match limited.hint {
4352                Some(hint) => Duration::from_secs(hint).max(backoff),
4353                None => backoff,
4354            };
4355            let remaining = self.pacing.retry_budget.saturating_sub(waited);
4356            // A wait of nothing spends none of the budget, so it is exhaustion rather
4357            // than a retry. `Pacing::resolve` rules out every way of configuring one
4358            // except a budget of zero, where reporting the first refusal is the ask.
4359            if wait.is_zero() || wait > remaining {
4360                return Err(limited.exhausted(
4361                    doing,
4362                    waits,
4363                    waited,
4364                    wait,
4365                    self.pacing.retry_budget,
4366                ));
4367            }
4368            self.clock.sleep(wait).await;
4369            waited += wait;
4370            waits += 1;
4371            backoff = backoff.saturating_mul(2);
4372        }
4373    }
4374
4375    /// The next moment a content-creating mutation may leave this source, as a wait from
4376    /// now.
4377    ///
4378    /// The slot is reserved under the lock and the waiting happens outside it, so two
4379    /// callers take two slots rather than the same one — and no lock is held across an
4380    /// await.
4381    ///
4382    /// The moment it is spaced from is the previous mutation's *completion*, which
4383    /// [`Self::finish_mutation`] records. See that method for why the release moment on its
4384    /// own is the wrong thing to measure from.
4385    fn reserve_mutation_slot(&self) -> Duration {
4386        if self.pacing.min_mutation_interval.is_zero() {
4387            return Duration::ZERO;
4388        }
4389        // A poisoned lock here costs pacing, not correctness, and refusing the write over
4390        // it would turn an earlier failure into a second one for no gain.
4391        let mut last = self
4392            .last_mutation
4393            .lock()
4394            .unwrap_or_else(std::sync::PoisonError::into_inner);
4395        let now = self.clock.now();
4396        // `checked_add` rather than `+`: adding durations can panic on overflow, and
4397        // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
4398        let at = last.map_or(now, |previous| {
4399            previous
4400                .checked_add(self.pacing.min_mutation_interval)
4401                .map_or(now, |earliest| earliest.max(now))
4402        });
4403        *last = Some(at);
4404        at.saturating_sub(now)
4405    }
4406
4407    /// Record that a content-creating mutation has finished, so the next one is spaced
4408    /// from here rather than from the moment this one was released.
4409    ///
4410    /// This source can only choose when a request *departs*; the limiter counts when it
4411    /// *arrives*, and the two differ by whatever the request spent in transit. Spacing one
4412    /// departure from the last therefore hands the limiter a gap of the interval less that
4413    /// transit, so a source pacing at 750 ms can still be seen arriving faster — which is
4414    /// exactly how a copy paced well inside a board's threshold was refused by it on a
4415    /// slower machine while passing on a quick one.
4416    ///
4417    /// Spacing from completion removes the subtraction rather than budgeting for it. The
4418    /// previous request had already arrived before its response came back, so its arrival
4419    /// is no later than this moment, and the next mutation is released at least the
4420    /// interval after this moment and arrives no earlier than it is released: the gap the
4421    /// limiter measures is therefore at least the interval, whatever transit costs and on
4422    /// whatever platform. The price is that a mutation's own round trip no longer counts
4423    /// towards its spacing, which makes this source slightly slower than the configured
4424    /// rate rather than slightly faster — the safe side of a limit that punishes being
4425    /// wrong by refusing reads for the next fifty minutes.
4426    ///
4427    /// A failed attempt is recorded too: a request refused by the limiter still arrived,
4428    /// and one that never left costs only a wait nobody needed.
4429    fn finish_mutation(&self) {
4430        if self.pacing.min_mutation_interval.is_zero() {
4431            return;
4432        }
4433        // A poisoned lock here costs pacing, not correctness, exactly as in the reservation.
4434        let mut last = self
4435            .last_mutation
4436            .lock()
4437            .unwrap_or_else(std::sync::PoisonError::into_inner);
4438        let now = self.clock.now();
4439        // `max` rather than an assignment: a concurrent caller may already have reserved a
4440        // slot further out, and completing this request must never pull that slot back in.
4441        *last = Some(last.map_or(now, |reserved| reserved.max(now)));
4442    }
4443
4444    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
4445    /// failure that waiting cannot help — and recorded, whichever of the three it was.
4446    ///
4447    /// This is the one place a request leaves this crate, which is why the accounting is
4448    /// here rather than at each of the callers: a read path added later is counted without
4449    /// anybody remembering to count it, and
4450    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
4451    /// when one is not.
4452    async fn send_once(
4453        &self,
4454        query: &str,
4455        variables: &Value,
4456        number: u32,
4457    ) -> Result<Value, Attempt> {
4458        let executed = self.ledger.priced_document(query);
4459        let Attempted {
4460            result,
4461            limits,
4462            reported_cost,
4463            response_outcome,
4464        } = self.attempt(executed.as_ref(), variables).await;
4465        // No `otherwise` name: every document this source sends is one of its own, and the
4466        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
4467        let sending = accounting::Request::graphql(query, variables, None, reported_cost);
4468        let outcome = match &result {
4469            Ok(_) => accounting::Outcome::Answered,
4470            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
4471            Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
4472        };
4473        self.ledger
4474            .record(sending.finished(outcome, limits).with_execution(
4475                Some(executed.into_owned()),
4476                response_outcome,
4477                number,
4478            ));
4479        result
4480    }
4481
4482    /// The attempt itself, with what its response said about the rate limit alongside.
4483    ///
4484    /// The two are returned together rather than recorded here because every one of the
4485    /// early exits below is a different outcome, and a record written at each of them is a
4486    /// record one of them can be added without.
4487    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
4488        let mut limits = accounting::RateLimit::default();
4489        let mut reported_cost = None;
4490        let mut response_outcome = accounting::ResponseOutcome::TransportError;
4491        let result = self
4492            .attempted(
4493                query,
4494                variables,
4495                &mut limits,
4496                &mut reported_cost,
4497                &mut response_outcome,
4498            )
4499            .await;
4500        Attempted {
4501            result,
4502            limits,
4503            reported_cost,
4504            response_outcome,
4505        }
4506    }
4507
4508    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
4509    async fn attempted(
4510        &self,
4511        query: &str,
4512        variables: &Value,
4513        limits: &mut accounting::RateLimit,
4514        reported_cost: &mut Option<u64>,
4515        response_outcome: &mut accounting::ResponseOutcome,
4516    ) -> Result<Value, Attempt> {
4517        let response = self
4518            .client
4519            .post(self.endpoint.clone())
4520            .bearer_auth(self.token.expose_secret())
4521            .json(&json!({"query": query, "variables": variables}))
4522            .send()
4523            .await
4524            .map_err(|e| {
4525                Attempt::Failed(SourceError::Unavailable {
4526                    message: format!("GitHub GraphQL request failed: {e}"),
4527                })
4528            })?;
4529        let status = response.status();
4530        if !status.is_success() {
4531            *response_outcome = accounting::ResponseOutcome::HttpError;
4532        }
4533        let header = |name: &str| whole_seconds(response.headers().get(name));
4534        let served = response
4535            .headers()
4536            .get(reqwest::header::DATE)
4537            .and_then(|value| value.to_str().ok())
4538            .and_then(|value| DateTime::parse_from_rfc2822(value).ok())
4539            .map(|at| at.with_timezone(&Utc));
4540        *limits = accounting::RateLimit::read(|name| {
4541            response
4542                .headers()
4543                .get(name)
4544                .and_then(|value| value.to_str().ok())
4545                .map(str::to_owned)
4546        });
4547        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
4548        // that are not text at all — is "not known to be exhausted". This never makes a
4549        // response a refusal on its own: it says which limiter a refusal is attributed to
4550        // and where its hint comes from, so a value this cannot read costs a hint rather
4551        // than an answer.
4552        let exhausted = response
4553            .headers()
4554            .get("x-ratelimit-remaining")
4555            .and_then(|value| value.to_str().ok())
4556            == Some("0");
4557        // `retry-after` is what GitHub asks for when it asks; when it does not and the
4558        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
4559        // which is the same question answered as an absolute time. Nothing else here is a
4560        // hint, and a schedule is what answers a refusal that carries none.
4561        let hint = header("retry-after").or_else(|| {
4562            exhausted
4563                .then(|| header("x-ratelimit-reset"))
4564                .flatten()
4565                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
4566        });
4567        // Read before it is parsed, because the evidence which tells a secondary rate
4568        // limit from a rejected credential is in the body of a response whose status says
4569        // only "forbidden" — and a non-success response was never parsed at all.
4570        let body = response.text().await.map_err(|e| {
4571            Attempt::Failed(SourceError::Unavailable {
4572                message: format!("GitHub GraphQL response could not be read: {e}"),
4573            })
4574        })?;
4575        *reported_cost = serde_json::from_str::<Value>(&body)
4576            .ok()
4577            .as_ref()
4578            .and_then(|body| body.pointer("/data/rateLimit/cost"))
4579            .and_then(Value::as_u64);
4580        let parsed = serde_json::from_str::<Value>(&body).ok();
4581        *response_outcome = accounting::ResponseOutcome::of_graphql_reply(status, parsed.as_ref());
4582        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
4583            return Err(Attempt::Limited(Limited { limiter, hint }));
4584        }
4585        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
4586            return Err(Attempt::Failed(SourceError::Auth {
4587                message: format!(
4588                    "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"
4589                ),
4590            }));
4591        }
4592        if !status.is_success() {
4593            return Err(Attempt::Failed(SourceError::Unavailable {
4594                message: format!("GitHub GraphQL returned HTTP {status}"),
4595            }));
4596        }
4597        let mut data = self.answer(&body).map_err(Attempt::Failed)?;
4598        if let Some(served) = served {
4599            stamp_served(&mut data, &served.to_rfc3339());
4600        }
4601        Ok(data)
4602    }
4603
4604    /// What one successful HTTP response says, once its GraphQL errors are read.
4605    fn answer(&self, body: &str) -> Result<Value, SourceError> {
4606        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
4607            message: format!("GitHub returned invalid JSON: {e}"),
4608        })?;
4609        let errors = body
4610            .get("errors")
4611            .map(|value| {
4612                value.as_array().ok_or_else(|| SourceError::Malformed {
4613                    message: "GitHub response errors is not an array".into(),
4614                })
4615            })
4616            .transpose()?;
4617        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
4618            let messages = errors
4619                .iter()
4620                .filter_map(|e| e.get("message").and_then(Value::as_str))
4621                .collect::<Vec<_>>()
4622                .join("; ");
4623            let message = if messages.is_empty() {
4624                "GitHub returned GraphQL errors".into()
4625            } else {
4626                messages
4627            };
4628            let normalized = message.to_ascii_lowercase();
4629            if normalized.contains("resource not accessible") || normalized.contains("scope") {
4630                return Err(SourceError::Auth {
4631                    message: format!(
4632                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
4633                        self.credential_name
4634                    ),
4635                });
4636            }
4637            return Err(SourceError::Refused { message });
4638        }
4639        body.get("data")
4640            .filter(|data| data.is_object())
4641            .cloned()
4642            .ok_or_else(|| SourceError::Malformed {
4643                message: "GitHub response has no data object".into(),
4644            })
4645    }
4646
4647    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
4648    // GraphQL cannot independently page them inside the outer item page. This source page is
4649    // deliberately bounded at that published maximum; the live drift journey exercises it.
4650    async fn board_page(
4651        &self,
4652        items_after: Option<&str>,
4653        items_first: u32,
4654    ) -> Result<Value, SourceError> {
4655        let data = self
4656            .graphql(
4657                graphql::BOARD,
4658                json!({"owner":self.owner,"number":self.project_number,
4659                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
4660                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
4661            )
4662            .await?;
4663        data.pointer("/owner/projectV2")
4664            .filter(|v| !v.is_null())
4665            .cloned()
4666            .ok_or_else(|| SourceError::Refused {
4667                message: format!(
4668                    "GitHub project {}/{} was not found or is not visible to the token",
4669                    self.owner, self.project_number
4670                ),
4671            })
4672    }
4673
4674    /// The search that finds the issues of this board, narrowed by `also` when it is
4675    /// given.
4676    ///
4677    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
4678    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
4679    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
4680    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
4681    /// from a task by the `parent` field each issue carries rather than by the search.
4682    fn board_search(&self, also: Option<&str>) -> String {
4683        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
4684        match also {
4685            Some(also) => format!("{scope} {also}"),
4686            None => scope,
4687        }
4688    }
4689
4690    /// One issue this source reached directly, as the board item a read of the board would
4691    /// have produced — or `None` when this board does not hold it.
4692    ///
4693    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
4694    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
4695    /// item's own id, that item's field values, and the issue as its content. One resolver
4696    /// for both routes is what makes an issue read through a search, through its own node
4697    /// id, or through its project's sub-issues report the same title, the same status, the
4698    /// same labels and the same qualified id.
4699    ///
4700    /// An issue with no entry for *this* board is not this source's to report, which is
4701    /// what keeps an id naming some other repository's issue from being answered as an item
4702    /// of this board. That answer is given about an **exhausted** connection and never
4703    /// about an unread page: the entry is looked for on the page in hand, and only if that
4704    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
4705    /// rest of it.
4706    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
4707        if optional_str(issue, "__typename")? != Some("Issue") {
4708            return Ok(None);
4709        }
4710        let memberships = issue
4711            .get("projectItems")
4712            .ok_or_else(|| SourceError::Malformed {
4713                message: "GitHub issue is missing projectItems".into(),
4714            })?;
4715        let nodes = memberships
4716            .get("nodes")
4717            .and_then(Value::as_array)
4718            .ok_or_else(|| SourceError::Malformed {
4719                message: "GitHub issue projectItems.nodes is not an array".into(),
4720            })?;
4721        let held = match self.board_entry(nodes) {
4722            Some(held) => held.clone(),
4723            None => {
4724                let info = memberships
4725                    .get("pageInfo")
4726                    .ok_or_else(|| SourceError::Malformed {
4727                        message: "GitHub issue projectItems has no pageInfo".into(),
4728                    })?;
4729                // The page held no entry for this board. Whether that means the issue is
4730                // not on it is a question about the rest of the connection, and only a
4731                // connection with no rest answers it here.
4732                if !required_bool(info, "hasNextPage")? {
4733                    return Ok(None);
4734                }
4735                let cursor = required_str(info, "endCursor")?;
4736                validate_cursor_progress(None, cursor)?;
4737                let issue_id = required_str(issue, "id")?;
4738                match self.board_membership(issue_id, cursor).await? {
4739                    Some(held) => held,
4740                    None => return Ok(None),
4741                }
4742            }
4743        };
4744        // The membership's own `updatedAt` rides along: it is the board half of the item's change
4745        // token, which a field change moves where the issue's own time does not.
4746        let item = json!({
4747            "id": required_str(&held, "id")?,
4748            "updatedAt": held.get("updatedAt"),
4749            "project": held.get("project"),
4750            "fieldValues": held.get("fieldValues"),
4751            "content": issue,
4752        });
4753        self.resolve(&item)
4754    }
4755
4756    /// This board's own entry among one page of an issue's `Issue.projectItems`.
4757    ///
4758    /// One spelling of *which membership is this board's*, so the page a read carries and
4759    /// the pages [`Self::board_membership`] walks are searched by the same rule.
4760    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
4761        nodes.iter().find(|node| {
4762            node.pointer("/project/number").and_then(Value::as_u64)
4763                == Some(u64::from(self.project_number))
4764        })
4765    }
4766
4767    /// The rest of one issue's board memberships, from `after`, for this board's entry.
4768    ///
4769    /// The recovery read: a page of memberships that holds no entry for this board says
4770    /// nothing about the memberships past it, so the connection is walked to exhaustion
4771    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
4772    /// positive answer — the whole connection was read and no entry named this board —
4773    /// rather than a failure, and the walk is held to
4774    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
4775    /// with a cursor that does not advance is refused instead of spun on.
4776    async fn board_membership(
4777        &self,
4778        issue: &str,
4779        after: &str,
4780    ) -> Result<Option<Value>, SourceError> {
4781        let mut after = after.to_owned();
4782        loop {
4783            let data = self
4784                .graphql(
4785                    graphql::ISSUE_BOARD_ITEMS,
4786                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
4787                           "nestedFirst":NESTED_PAGE_SIZE}),
4788                )
4789                .await?;
4790            let Some(connection) = data
4791                .pointer("/node/projectItems")
4792                .filter(|value| !value.is_null())
4793            else {
4794                // The id resolved to nothing, or to something with no memberships to walk —
4795                // which is the same answer as a connection holding no entry for this board.
4796                return Ok(None);
4797            };
4798            let nodes = connection
4799                .get("nodes")
4800                .and_then(Value::as_array)
4801                .ok_or_else(|| SourceError::Malformed {
4802                    message: "GitHub issue projectItems.nodes is not an array".into(),
4803                })?;
4804            if let Some(held) = self.board_entry(nodes) {
4805                return Ok(Some(held.clone()));
4806            }
4807            let info = connection
4808                .get("pageInfo")
4809                .ok_or_else(|| SourceError::Malformed {
4810                    message: "GitHub issue projectItems has no pageInfo".into(),
4811                })?;
4812            let next = required_bool(info, "hasNextPage")?
4813                .then(|| required_str(info, "endCursor"))
4814                .transpose()?;
4815            match next {
4816                Some(next) => {
4817                    validate_cursor_progress(Some(&after), next)?;
4818                    after = next.to_owned();
4819                }
4820                None => return Ok(None),
4821            }
4822        }
4823    }
4824
4825    /// One page of a board-scoped issue search, and where the next page resumes.
4826    async fn search_page(
4827        &self,
4828        search: &str,
4829        first: u32,
4830        after: Option<&str>,
4831    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
4832        let data = self
4833            .graphql(
4834                graphql::SEARCH_ISSUES,
4835                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
4836                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
4837                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4838            )
4839            .await?;
4840        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
4841            message: "GitHub search response has no search connection".into(),
4842        })?;
4843        let mut found = Vec::new();
4844        for node in connection
4845            .get("nodes")
4846            .and_then(Value::as_array)
4847            .ok_or_else(|| SourceError::Malformed {
4848                message: "GitHub search nodes is not an array".into(),
4849            })?
4850        {
4851            if let Some(resolved) = self.resolve_issue(node).await? {
4852                found.push(resolved);
4853            }
4854        }
4855        let info = connection
4856            .get("pageInfo")
4857            .ok_or_else(|| SourceError::Malformed {
4858                message: "GitHub search connection has no pageInfo".into(),
4859            })?;
4860        let next = required_bool(info, "hasNextPage")?
4861            .then(|| required_str(info, "endCursor"))
4862            .transpose()?
4863            .map(str::to_owned);
4864        if let Some(next) = &next {
4865            validate_cursor_progress(after, next)?;
4866        }
4867        Ok((found, next))
4868    }
4869
4870    /// Every issue this board holds, completed with what this run wrote.
4871    ///
4872    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
4873    /// is an index and is eventually consistent, so an issue this run created seconds ago
4874    /// can be absent from it, and a project listed straight after being written would
4875    /// otherwise be missing from its own board. What is added back is only what this
4876    /// process itself wrote, out of [`Self::created`], which lives and dies with the
4877    /// process.
4878    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4879        let found = self.searched_issues().await?;
4880        self.completed_with_written(found, |_| true)
4881    }
4882
4883    /// Every issue this board's own search reports, walked to exhaustion, read once per
4884    /// source.
4885    ///
4886    /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
4887    /// needs it too and the two would otherwise walk the same search twice in one command.
4888    /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
4889    /// is.
4890    async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
4891        let cached = self.search_cache()?.clone();
4892        if let Some(held) = cached {
4893            return Ok(held);
4894        }
4895        let mut after: Option<String> = None;
4896        let mut found = Vec::new();
4897        let search = self.board_search(None);
4898        loop {
4899            let (page, next) = self
4900                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
4901                .await?;
4902            found.extend(page);
4903            match next {
4904                Some(next) => after = Some(next),
4905                None => break,
4906            }
4907        }
4908        *self.search_cache()? = Some(found.clone());
4909        Ok(found)
4910    }
4911
4912    /// This process's own view of the board's issues, or the refusal a poisoned lock is.
4913    fn search_cache(
4914        &self,
4915    ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
4916        self.search_cache
4917            .lock()
4918            .map_err(|_| SourceError::Unavailable {
4919                message: "this source's view of the board's issues was left inconsistent by an \
4920                      earlier failure; next: run the command again"
4921                    .into(),
4922            })
4923    }
4924
4925    /// `found`, with everything this run wrote that `keep` accepts and the read did not
4926    /// report.
4927    ///
4928    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
4929    /// at all: the search index is behind, and a node read of an item filed moments ago can
4930    /// be too.
4931    fn completed_with_written(
4932        &self,
4933        mut found: Vec<Resolved>,
4934        keep: impl Fn(&Resolved) -> bool,
4935    ) -> Result<Vec<Resolved>, SourceError> {
4936        for own in self.created()?.iter().filter(|own| keep(own)) {
4937            if !found.iter().any(|item| item.id == own.id) {
4938                found.push(own.clone());
4939            }
4940        }
4941        Ok(found)
4942    }
4943
4944    /// What resolving one node id reached.
4945    ///
4946    /// Three answers rather than an `Option`, because a board *draft* is none of the other
4947    /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
4948    /// is completed by a read of the draft itself rather than reported as nothing.
4949    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
4950        let asked = self
4951            .graphql(
4952                graphql::ISSUE,
4953                json!({"id":id.0,"first":MAX_PAGE_SIZE,"nestedFirst":NESTED_PAGE_SIZE,
4954                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
4955            )
4956            .await;
4957        let data = match asked {
4958            Ok(data) => data,
4959            // A string that is not a node id at all is not a failure to report: it is an id
4960            // this board does not hold, which is what every read of one already answers.
4961            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
4962            Err(error) => return Err(error),
4963        };
4964        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
4965            return Ok(Reached::Nothing);
4966        };
4967        if optional_str(node, "__typename")? == Some("DraftIssue") {
4968            return Ok(Reached::Draft);
4969        }
4970        Ok(match self.resolve_issue(node).await? {
4971            Some(item) => Reached::Held(Box::new(item)),
4972            None => Reached::Nothing,
4973        })
4974    }
4975
4976    /// One item of this board by its own id, whatever kind it is.
4977    ///
4978    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
4979    /// run wrote is read first, because a node read of an item created moments ago can
4980    /// still be behind the board field values written onto it — see [`Self::created`].
4981    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
4982        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
4983            return Ok(Some(own.clone()));
4984        }
4985        match self.reach(id).await? {
4986            Reached::Held(item) => Ok(Some(*item)),
4987            Reached::Nothing => Ok(None),
4988            Reached::Draft => self.draft_by_id(id).await,
4989        }
4990    }
4991
4992    /// Several items of this board, each by its own id, in order — what [`Self::item_by_id`]
4993    /// answers for each, read [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] rather
4994    /// than one request per id.
4995    ///
4996    /// What this run wrote answers first, as it does there, and only the rest is read. One id
4997    /// left to read is read by [`Self::item_by_id`] itself, which costs what a batch does. A
4998    /// batch GitHub refuses because one of its ids resolves to no node at all is read again one
4999    /// id at a time, so that id is answered as not held and the others as themselves; a draft
5000    /// is completed by a read of the draft, exactly as there.
5001    async fn items_by_ids(&self, ids: &[NativeId]) -> Result<Vec<Option<Resolved>>, SourceError> {
5002        let mut found: Vec<Option<Option<Resolved>>> = {
5003            let created = self.created()?;
5004            ids.iter()
5005                .map(|id| {
5006                    created
5007                        .iter()
5008                        .find(|own| own.id == *id)
5009                        .map(|own| Some(own.clone()))
5010                })
5011                .collect()
5012        };
5013        let unread: Vec<NativeId> = ids
5014            .iter()
5015            .zip(&found)
5016            .filter(|(_, found)| found.is_none())
5017            .map(|(id, _)| id.clone())
5018            .collect();
5019        let mut read = Vec::with_capacity(unread.len());
5020        if let [one] = unread.as_slice() {
5021            read.push(self.item_by_id(one).await?);
5022        } else {
5023            for batch in unread.chunks(DETAIL_BATCH) {
5024                let data = match self
5025                    .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, None))
5026                    .await
5027                {
5028                    Ok(data) => data,
5029                    Err(error) if unresolvable_node(&error) => {
5030                        for id in batch {
5031                            read.push(self.item_by_id(id).await?);
5032                        }
5033                        continue;
5034                    }
5035                    Err(error) => return Err(error),
5036                };
5037                for (slot, id) in batch.iter().enumerate() {
5038                    let node =
5039                        data.get(format!("i{slot}"))
5040                            .ok_or_else(|| SourceError::Malformed {
5041                                message: format!(
5042                                    "GitHub answered a batch read with no item for {}",
5043                                    id.0
5044                                ),
5045                            })?;
5046                    read.push(if node.is_null() {
5047                        None
5048                    } else if optional_str(node, "__typename")? == Some("DraftIssue") {
5049                        self.draft_by_id(id).await?
5050                    } else {
5051                        if optional_str(node, "__typename")? == Some("Issue")
5052                            && required_str(node, "id")? != id.0
5053                        {
5054                            return Err(SourceError::Malformed {
5055                                message: format!(
5056                                    "GitHub answered the read of {} with issue {}",
5057                                    id.0,
5058                                    required_str(node, "id")?
5059                                ),
5060                            });
5061                        }
5062                        self.resolve_issue(node).await?
5063                    });
5064                }
5065            }
5066        }
5067        let mut read = read.into_iter();
5068        Ok(found
5069            .iter_mut()
5070            .map(|slot| slot.take().unwrap_or_else(|| read.next().flatten()))
5071            .collect())
5072    }
5073
5074    fn resolved_cache(
5075        &self,
5076    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<NativeId, Resolved>>, SourceError> {
5077        self.resolved_cache
5078            .lock()
5079            .map_err(|_| SourceError::Unavailable {
5080                message: "resolved item records were left inconsistent; run the command again"
5081                    .into(),
5082            })
5083    }
5084
5085    /// Reuse a record this invocation already resolved. The mutation sender invalidates
5086    /// it before writing, so a partial failure cannot leave a pre-write binding behind.
5087    async fn bound_item(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5088        let cached = self.resolved_cache()?.get(id).cloned();
5089        match cached {
5090            Some(item) => Ok(Some(item)),
5091            None => self.item_by_id(id).await,
5092        }
5093    }
5094
5095    /// One board draft by its own id, with the board item it sits in — or `None` when no
5096    /// item of this board is that draft's.
5097    ///
5098    /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
5099    /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
5100    /// links a draft to one board item, so the page this read carries is the whole of that
5101    /// connection, and a page that reports more than it holds is refused rather than read
5102    /// as an answer about memberships nobody read.
5103    async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
5104        let data = self
5105            .graphql(
5106                graphql::DRAFT,
5107                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
5108                       "boardItems":BOARD_ITEMS_PAGE_SIZE}),
5109            )
5110            .await?;
5111        // Gone between the two reads is an answer — the draft is no longer there. Anything
5112        // else than the draft [`Self::reach`] was just told this id is, is not one.
5113        let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
5114            return Ok(None);
5115        };
5116        if optional_str(draft, "__typename")? != Some("DraftIssue") {
5117            return Err(SourceError::Malformed {
5118                message: format!(
5119                    "GitHub answered {} as a draft and then as something else",
5120                    id.0
5121                ),
5122            });
5123        }
5124        if required_str(draft, "id")? != id.0 {
5125            return Err(SourceError::Malformed {
5126                message: format!("GitHub answered a different draft for {}", id.0),
5127            });
5128        }
5129        let memberships = draft
5130            .get("projectV2Items")
5131            .ok_or_else(|| SourceError::Malformed {
5132                message: format!("GitHub draft {} is missing projectV2Items", id.0),
5133            })?;
5134        let nodes = memberships
5135            .get("nodes")
5136            .and_then(Value::as_array)
5137            .ok_or_else(|| SourceError::Malformed {
5138                message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
5139            })?;
5140        let info = memberships
5141            .get("pageInfo")
5142            .ok_or_else(|| SourceError::Malformed {
5143                message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
5144            })?;
5145        // Read whether or not this board's entry is on the page: a page claiming more than
5146        // the one item GitHub links a draft to is a malformed answer either way.
5147        if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
5148            return Err(SourceError::Malformed {
5149                message: format!(
5150                    "GitHub draft {} reports more board items than the one GitHub links a draft \
5151                     to",
5152                    id.0
5153                ),
5154            });
5155        }
5156        if let Some(node) = nodes.first()
5157            && node
5158                .pointer("/project/number")
5159                .and_then(Value::as_u64)
5160                .is_none()
5161        {
5162            return Err(SourceError::Malformed {
5163                message: format!(
5164                    "GitHub draft {} board item has no numeric project number",
5165                    id.0
5166                ),
5167            });
5168        }
5169        let Some(held) = self.board_entry(nodes) else {
5170            return Ok(None);
5171        };
5172        if required_str(
5173            held.get("project").ok_or_else(|| SourceError::Malformed {
5174                message: format!("GitHub draft {} board item has no project", id.0),
5175            })?,
5176            "id",
5177        )? != self.board_fields().await?.id.as_str()
5178        {
5179            return Ok(None);
5180        }
5181        let item = json!({
5182            "id": required_str(held, "id")?,
5183            "project": held.get("project"),
5184            "fieldValues": held.get("fieldValues"),
5185            "content": draft,
5186        });
5187        self.resolve(&item)
5188    }
5189
5190    /// The board's own id and field definitions, for a write whose item does not carry
5191    /// them — never its items.
5192    ///
5193    /// A board this command has already listed supplies them, since it read them beside its
5194    /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
5195    /// is consulted about which items the board holds: see the module documentation for
5196    /// why a question about one known item is answered by reading that item.
5197    async fn board_fields(&self) -> Result<BoardFields, SourceError> {
5198        if let Some(board) = self.board_cache()?.as_ref() {
5199            return Ok(BoardFields {
5200                id: BoardId::parse(&board.id)?,
5201                fields: board.fields.clone(),
5202            });
5203        }
5204        if let Some(held) = self.fields_cache()?.clone() {
5205            return Ok(held);
5206        }
5207        let data = self
5208            .graphql(
5209                graphql::BOARD_FIELDS,
5210                json!({"owner":self.owner,"number":self.project_number,
5211                       "nestedFirst":NESTED_PAGE_SIZE}),
5212            )
5213            .await?;
5214        self.fields_read(&data)
5215    }
5216
5217    /// The board's id and fields out of an answer carrying the `boardFields` root, held for
5218    /// the rest of this command.
5219    fn fields_read(&self, data: &Value) -> Result<BoardFields, SourceError> {
5220        let board = data
5221            .pointer("/boardFields/projectV2")
5222            .filter(|value| !value.is_null())
5223            .ok_or_else(|| SourceError::Refused {
5224                message: format!(
5225                    "GitHub project {}/{} was not found or is not visible to the token",
5226                    self.owner, self.project_number
5227                ),
5228            })?;
5229        let read = BoardFields {
5230            id: BoardId::parse(required_str(board, "id")?)?,
5231            fields: board.get("fields").cloned().unwrap_or(Value::Null),
5232        };
5233        *self.fields_cache()? = Some(read.clone());
5234        Ok(read)
5235    }
5236
5237    /// Read what creating an issue in `repository` needs and this command has not read yet —
5238    /// the board's fields and the repository's node id — in one request when it needs both.
5239    ///
5240    /// When either is already known this sends nothing, and the other is read by its own
5241    /// document where it is asked for, so no create reads anything twice.
5242    async fn creation_context(
5243        &self,
5244        repository: &RepositoryTarget,
5245        incoming: &Incoming<'_>,
5246    ) -> Result<(), SourceError> {
5247        let fields_known = self.board_cache()?.is_some() || self.fields_cache()?.is_some();
5248        if fields_known || self.repository_cache()?.contains_key(repository) {
5249            return Ok(());
5250        }
5251        let data = self
5252            .graphql(
5253                graphql::CREATION_CONTEXT,
5254                json!({"owner":self.owner,"number":self.project_number,
5255                       "nestedFirst":NESTED_PAGE_SIZE,"repositoryOwner":repository.owner,
5256                       "repositoryName":repository.name}),
5257            )
5258            .await?;
5259        self.fields_read(&data)?;
5260        self.repository_read(&data, repository, incoming)?;
5261        Ok(())
5262    }
5263
5264    /// This process's own view of the board's fields, or the refusal a poisoned lock is.
5265    fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
5266        self.fields_cache
5267            .lock()
5268            .map_err(|_| SourceError::Unavailable {
5269                message: "this source's view of the board's fields was left inconsistent by an \
5270                      earlier failure; next: run the command again"
5271                    .into(),
5272            })
5273    }
5274
5275    /// What a write to `item` needs of the board, read off that item when it says enough and
5276    /// off [`Self::board_fields`] when it does not.
5277    ///
5278    /// A node read of an item names its board and carries the definition of every field it
5279    /// holds a value of — so an item naming its board, holding a value of the origin field,
5280    /// and, when the write carries a status, holding a `Status` value, needs no read of the
5281    /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
5282    /// of may still be on the board, and a view reading it as absent would refuse a write the
5283    /// board can take or skip a field write the board needs, so such an item — and a create,
5284    /// which has no item yet — takes the board's fields from their own read instead.
5285    async fn fields_for(
5286        &self,
5287        item: Option<&Resolved>,
5288        writes_status: bool,
5289        selects_priority: bool,
5290    ) -> Result<BoardFields, SourceError> {
5291        if let Some(board) = item.and_then(Resolved::carried_board) {
5292            return Ok(board);
5293        }
5294        if let Some(item) = item
5295            && let Some(board_id) = item.named_board()
5296            && item.defines(ORIGIN_FIELD)
5297            && (!writes_status || item.defines("Status"))
5298            && (!selects_priority || item.defines(PRIORITY_FIELD))
5299            && self
5300                .metadata_fields
5301                .iter()
5302                .all(|projection| item.defines(&projection.field))
5303        {
5304            return Ok(BoardFields {
5305                id: board_id,
5306                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
5307            });
5308        }
5309        self.board_fields().await
5310    }
5311
5312    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
5313    /// when that id names nothing here with a sub-issue relationship to walk.
5314    ///
5315    /// `None` and an empty answer are different: `None` is *this is not an issue of this
5316    /// GitHub*, which is what sends a project selector on to be read as a name, and an
5317    /// empty vector is a project that holds nothing.
5318    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
5319        let mut after: Option<String> = None;
5320        let mut children = Vec::new();
5321        loop {
5322            let asked = self
5323                .graphql(
5324                    graphql::SUB_ISSUES,
5325                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
5326                           "nestedFirst":NESTED_PAGE_SIZE,
5327                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
5328                )
5329                .await;
5330            let data = match asked {
5331                Ok(data) => data,
5332                // A string that is not a node id at all is not a failure to report: it is
5333                // the ordinary answer to a selector naming a project by its name.
5334                Err(error) if unresolvable_node(&error) => return Ok(None),
5335                Err(error) => return Err(error),
5336            };
5337            let Some(connection) = data
5338                .pointer("/node/subIssues")
5339                .filter(|value| !value.is_null())
5340            else {
5341                // No such node, or one with no sub-issue relationship — a board draft is
5342                // the one this board can really hold.
5343                return Ok(None);
5344            };
5345            for node in connection
5346                .get("nodes")
5347                .and_then(Value::as_array)
5348                .ok_or_else(|| SourceError::Malformed {
5349                    message: "GitHub subIssues.nodes is not an array".into(),
5350                })?
5351            {
5352                if let Some(resolved) = self.resolve_issue(node).await? {
5353                    children.push(resolved);
5354                }
5355            }
5356            let info = connection
5357                .get("pageInfo")
5358                .ok_or_else(|| SourceError::Malformed {
5359                    message: "GitHub subIssues connection has no pageInfo".into(),
5360                })?;
5361            let next = required_bool(info, "hasNextPage")?
5362                .then(|| required_str(info, "endCursor"))
5363                .transpose()?;
5364            match next {
5365                Some(next) => {
5366                    validate_cursor_progress(after.as_deref(), next)?;
5367                    after = Some(next.to_owned());
5368                }
5369                None => return Ok(Some(children)),
5370            }
5371        }
5372    }
5373
5374    /// Which issue of this board a project *name* is, or `None` when none is.
5375    ///
5376    /// One bounded query which filters on that name at the server, rather than a walk of
5377    /// every issue the board holds. The name is compared again here: the qualifier narrows
5378    /// what GitHub sends, and this source decides what it names.
5379    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
5380        let search = self.board_search(Some(&title_qualifier(name)));
5381        let mut after = None;
5382        loop {
5383            let (candidates, next) = self
5384                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
5385                .await?;
5386            if let Some(item) = candidates.into_iter().find(|item| {
5387                item.kind == BoardKind::Work(ItemKind::Project)
5388                    && item.title.eq_ignore_ascii_case(name)
5389            }) {
5390                return Ok(Some(item.id));
5391            }
5392            match next {
5393                Some(next) => after = Some(next),
5394                None => return Ok(None),
5395            }
5396        }
5397    }
5398
5399    /// Everything filed under one project of this board: the sub-issues of the issue that
5400    /// project is.
5401    ///
5402    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
5403    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
5404    /// gains projects, or as another project gains tasks.
5405    ///
5406    /// A qualified id names the issue and is asked for its sub-issues directly: one
5407    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
5408    /// read as a project *name*, which costs the one bounded search
5409    /// [`Self::project_by_name`] makes.
5410    ///
5411    /// What GitHub answered is held for the rest of the command — see [`Self::children_cache`]
5412    /// — and each answer is still cut to the items whose parent is this project, so one this
5413    /// process has since filed elsewhere is not reported here, and completed with what this
5414    /// process filed under it.
5415    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
5416        let held = self.children_cache()?.get(selector).cloned();
5417        let (project, children) = match held {
5418            Some(held) => held,
5419            None => {
5420                let answered = match self.sub_issues(selector).await? {
5421                    Some(children) => (selector.clone(), children),
5422                    None => match self.project_by_name(&selector.0).await? {
5423                        Some(project) => {
5424                            let children = self.sub_issues(&project).await?.unwrap_or_default();
5425                            (project, children)
5426                        }
5427                        None => return Ok(Vec::new()),
5428                    },
5429                };
5430                self.children_cache()?
5431                    .insert(selector.clone(), answered.clone());
5432                answered
5433            }
5434        };
5435        let mut children: Vec<Resolved> = children
5436            .into_iter()
5437            .filter(|child| child.parent.as_ref() == Some(&project))
5438            .collect();
5439        for own in self.updated()?.iter() {
5440            if own.parent.as_ref() == Some(&project) && !children.iter().any(|c| c.id == own.id) {
5441                children.push(own.clone());
5442            }
5443        }
5444        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
5445    }
5446
5447    /// Every issue of this board GitHub's issue search reports updated at or after `since`,
5448    /// completed with what this run wrote — the candidates a comment-activity read confirms.
5449    ///
5450    /// Scoped by the board and by nothing else: `project:<owner>/<number>` reaches every issue
5451    /// on the board whatever repository, and whatever owner, it lives in, so no repository or
5452    /// owner qualifier is added and none is needed. What makes the `updated:` qualifier
5453    /// sufficient is a fact about GitHub rather than about this source: a comment written on an
5454    /// issue **and a comment edited on it** both move that issue's `updatedAt`. The credentialed
5455    /// journey `an_edited_comment_moves_its_issue_and_is_selected_since` in `tests/journey`
5456    /// re-takes that fact on every run of the lane, so a change on GitHub's side fails there
5457    /// rather than silently narrowing a caller's answer.
5458    ///
5459    /// The instant is written to the second, rounded down, which can only widen what the
5460    /// search returns; confirmation against each candidate's own comments is what makes the
5461    /// answer exact. The search is an index that lags a write by a second or two — the module
5462    /// documentation records it — so a caller that asks again from its last instant should
5463    /// overlap the two by more than that.
5464    async fn updated_since(&self, since: DateTime<Utc>) -> Result<Vec<Resolved>, SourceError> {
5465        let found = self.searched(&updated_qualifier(since)).await?;
5466        self.completed_with_written(found, |_| true)
5467    }
5468
5469    /// Every issue of this board GitHub's issue search reports for the board-scoped search
5470    /// narrowed by `also`, in pages of [`SEARCH_PAGE_SIZE`].
5471    ///
5472    /// Uncompleted: what this process wrote is added by the caller, which knows whether its
5473    /// own record is the fresher of the two.
5474    async fn searched(&self, also: &str) -> Result<Vec<Resolved>, SourceError> {
5475        let search = self.board_search(Some(also));
5476        let mut after: Option<String> = None;
5477        let mut found = Vec::new();
5478        loop {
5479            let (page, next) = self
5480                .search_page(&search, SEARCH_PAGE_SIZE, after.as_deref())
5481                .await?;
5482            found.extend(page);
5483            match next {
5484                Some(next) => after = Some(next),
5485                None => return Ok(found),
5486            }
5487        }
5488    }
5489
5490    /// A bounded task answer; the versioned cursor carries the connection position, how
5491    /// many rows of the page starting there were already handed out, and the own-write ids
5492    /// already observed, including across a new source instance.
5493    ///
5494    /// Every page is sent at [`SEARCH_PAGE_SIZE`] whatever the caller's limit, and a limit is
5495    /// sliced from the pages it needs; why is the module documentation's paging contract.
5496    async fn search_tasks(
5497        &self,
5498        query: &TaskQuery,
5499        page: &PageRequest,
5500        also: &str,
5501    ) -> Result<Page<Task>, SourceError> {
5502        let mut position = match &page.cursor {
5503            None => SearchPosition::default(),
5504            Some(cursor) => serde_json::from_str::<SearchPosition>(&cursor.0)
5505                .ok()
5506                .filter(|position| {
5507                    position.version == SEARCH_CURSOR_VERSION
5508                        && position.connection.valid_resume(position.offset)
5509                })
5510                .ok_or_else(|| SourceError::Config {
5511                    message: "page cursor is invalid".into(),
5512                })?,
5513        };
5514        let search = self.board_search(Some(also));
5515        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
5516        let own = self.with_own_writes(Vec::new())?;
5517        // An issue this process commented on is a candidate of a comment-activity read
5518        // whether or not the search has caught up with the comment; see `Self::commented`.
5519        let commented = match query.commented_since {
5520            Some(_) => self.commented()?.clone(),
5521            None => Vec::new(),
5522        };
5523        for id in own.iter().map(|item| &item.id).chain(&commented) {
5524            if !position.own.contains(id) {
5525                position.own.push(id.clone());
5526            }
5527        }
5528        let mut tasks = Vec::new();
5529        while !position.connection.exhausted() && tasks.len() < limit {
5530            let first = SEARCH_PAGE_SIZE;
5531            // Page size is part of the key: a short cached answer cannot answer a wider ask.
5532            let key =
5533                serde_json::to_string(&("page", &search, &position.connection.after(), first))
5534                    .expect("search page key is serializable");
5535            let cached = if query.commented_since.is_none() {
5536                self.narrowed_cache()?.get(&key).cloned()
5537            } else {
5538                None
5539            };
5540            let (found, next) = match cached {
5541                Some(found) => {
5542                    let next = self
5543                        .search_next
5544                        .lock()
5545                        .map_err(|_| SourceError::Unavailable {
5546                            message:
5547                                "search pagination was left inconsistent; run the command again"
5548                                    .into(),
5549                        })?
5550                        .get(&key)
5551                        .cloned()
5552                        .flatten();
5553                    (found, next)
5554                }
5555                None => {
5556                    let (found, next) = self
5557                        .search_page(&search, first, position.connection.after())
5558                        .await?;
5559                    if query.commented_since.is_none() {
5560                        self.search_next
5561                            .lock()
5562                            .map_err(|_| SourceError::Unavailable {
5563                                message:
5564                                    "search pagination was left inconsistent; run the command again"
5565                                        .into(),
5566                            })?
5567                            .insert(key.clone(), next.clone());
5568                        self.narrowed_cache()?.insert(key, found.clone());
5569                    }
5570                    (found, next)
5571                }
5572            };
5573            let rows = found.len();
5574            for mut item in found.into_iter().skip(position.offset) {
5575                if tasks.len() == limit {
5576                    break;
5577                }
5578                position.offset += 1;
5579                if position.own.contains(&item.id) {
5580                    if position.seen.contains(&item.id) {
5581                        continue;
5582                    }
5583                    position.seen.push(item.id.clone());
5584                    // The search's own copy of an issue this process only commented on is as
5585                    // good as a node read of it, since its comments are read either way.
5586                    let only_commented = commented.contains(&item.id)
5587                        && !own.iter().any(|written| written.id == item.id);
5588                    if !only_commented {
5589                        let updated_at = item.updated_at;
5590                        let Some(written) = self.search_written(&own, &item.id).await? else {
5591                            continue;
5592                        };
5593                        item = written;
5594                        item.updated_at = item.updated_at.max(updated_at);
5595                        self.resolved_cache()?.insert(item.id.clone(), item.clone());
5596                    }
5597                }
5598                if item.kind == BoardKind::Work(ItemKind::Task) {
5599                    let task = item.task()?;
5600                    if task_matches(&task, query, &query.project)
5601                        && self.commented_since(&item, query.commented_since).await?
5602                    {
5603                        tasks.push(task);
5604                    }
5605                }
5606            }
5607            if position.offset < rows {
5608                continue;
5609            }
5610            position.offset = 0;
5611            position.connection = match next {
5612                Some(after) => SearchConnection::Continuing {
5613                    after: Cursor(after),
5614                },
5615                None => SearchConnection::Exhausted {},
5616            };
5617        }
5618        if position.connection.exhausted() {
5619            for id in position.own.clone() {
5620                if position.seen.contains(&id) {
5621                    continue;
5622                }
5623                if tasks.len() == limit {
5624                    break;
5625                }
5626                position.seen.push(id.clone());
5627                let Some(item) = self.search_written(&own, &id).await? else {
5628                    continue;
5629                };
5630                if item.kind == BoardKind::Work(ItemKind::Task) {
5631                    let task = item.task()?;
5632                    if task_matches(&task, query, &query.project)
5633                        && self.commented_since(&item, query.commented_since).await?
5634                    {
5635                        tasks.push(task);
5636                    }
5637                }
5638            }
5639        }
5640        let more = !position.connection.exhausted()
5641            || position.own.iter().any(|id| !position.seen.contains(id));
5642        Ok(Page {
5643            items: tasks,
5644            next: more.then(|| {
5645                Cursor(serde_json::to_string(&position).expect("search position is serializable"))
5646            }),
5647        })
5648    }
5649
5650    /// A resumed process has the ids but no write records; resolve only a record the
5651    /// current page needs, by its uncached node read rather than the lagging search index.
5652    async fn search_written(
5653        &self,
5654        own: &[Resolved],
5655        id: &NativeId,
5656    ) -> Result<Option<Resolved>, SourceError> {
5657        match own.iter().find(|item| item.id == *id) {
5658            Some(item) => Ok(Some(item.clone())),
5659            None => self.item_by_id(id).await,
5660        }
5661    }
5662
5663    /// The candidates for a task query carrying a text, metadata or origin predicate, read
5664    /// without enumerating the board — or `None` for a query carrying none of the three, which
5665    /// keeps the reads it always had.
5666    ///
5667    /// An origin is answered by [`Self::origin_candidates`], whatever else the query carries,
5668    /// because it names at most a handful of items. Text and metadata are answered by one
5669    /// board-scoped issue search carrying every term — see [`narrowing_qualifiers`] — narrowed
5670    /// further by `updated:>=` when the query also asks for comment activity, since both
5671    /// qualifiers must hold of an issue the answer keeps. Every candidate is confirmed in
5672    /// process afterwards by the same predicates [`task_matches`] applies to every read.
5673    ///
5674    /// Completed with what this process wrote, its own record winning over the index's copy
5675    /// of the same item: see [`Self::with_own_writes`].
5676    async fn narrowed(&self, query: &TaskQuery) -> Result<Option<Vec<Resolved>>, SourceError> {
5677        let asked = match (&query.origin, narrowing_qualifiers(query)) {
5678            (Some(origin), _) => Narrowing::Origin(origin.clone()),
5679            (None, Some(qualifiers)) => Narrowing::Search(match query.commented_since {
5680                Some(since) => format!("{} {qualifiers}", updated_qualifier(since)),
5681                None => qualifiers,
5682            }),
5683            (None, None) => return Ok(None),
5684        };
5685        // A question about comment activity is asked afresh every time, as it always was: it
5686        // is the one a caller polls from one source while waiting for the index, and an
5687        // answer held from the first poll would be the answer to every later one.
5688        let key = query.commented_since.is_none().then(|| asked.key());
5689        let cached = match &key {
5690            Some(key) => self.narrowed_cache()?.get(key).cloned(),
5691            None => None,
5692        };
5693        let found = match cached {
5694            Some(found) => found,
5695            None => {
5696                let found = match &asked {
5697                    Narrowing::Origin(origin) => self.origin_candidates(origin).await?,
5698                    Narrowing::Search(also) => self.searched(also).await?,
5699                };
5700                if let Some(key) = key {
5701                    self.narrowed_cache()?.insert(key, found.clone());
5702                }
5703                found
5704            }
5705        };
5706        self.with_own_writes(found).map(Some)
5707    }
5708
5709    /// The candidates for a project or unscoped document query carrying a searchable text,
5710    /// read without enumerating the board — or `None` for a query with no text or a blank one,
5711    /// which keeps the read it always had.
5712    ///
5713    /// The text is sent as the very phrase a task query's text is — see [`text_qualifiers`] —
5714    /// in one board-scoped issue search walked to its end at [`SEARCH_PAGE_SIZE`], so what it
5715    /// costs is the issues that match and never the board. Its answer is held for the command
5716    /// under the same key [`Self::narrowed`] holds that search under, so a walk of the caller's
5717    /// pages asks GitHub once. Every candidate is confirmed afterwards by its kind and by the
5718    /// substring rule, exactly as an item of the wider read was, and is completed with what this
5719    /// process wrote: see [`Self::with_own_writes`].
5720    async fn text_searched(
5721        &self,
5722        text: Option<&TextQuery>,
5723    ) -> Result<Option<Vec<Resolved>>, SourceError> {
5724        let Some(also) = text_qualifiers(text) else {
5725            return Ok(None);
5726        };
5727        let key = Narrowing::Search(also.clone()).key();
5728        let cached = self.narrowed_cache()?.get(&key).cloned();
5729        let found = match cached {
5730            Some(found) => found,
5731            None => {
5732                let found = self.searched(&also).await?;
5733                self.narrowed_cache()?.insert(key, found.clone());
5734                found
5735            }
5736        };
5737        self.with_own_writes(found).map(Some)
5738    }
5739
5740    /// Every item of this board that may carry `origin` — a superset of those that do — found
5741    /// by [`graphql::ORIGIN_LOOKUP`] and never by enumerating the board.
5742    ///
5743    /// The union of the board's own field filter over the `onetaskgraph.origin` text field —
5744    /// which reads the field every carrier holds, whichever release wrote it — and the
5745    /// board-scoped issue search for the same id as a phrase in the body, where this source
5746    /// mirrors it. The caller adds the third read, this process's own writes. Candidates are
5747    /// returned unconfirmed; [`task_matches`] compares each one's own origin field with the
5748    /// query's, exactly.
5749    ///
5750    /// Both connections are walked to exhaustion, each from its own cursor. One that has
5751    /// already ended is sent its last cursor again, which answers an empty page, so the one
5752    /// document serves every page of either. What the two leave is stated in the module
5753    /// documentation: a carrier another process added within the last second or two, before
5754    /// either index has it.
5755    async fn origin_candidates(&self, origin: &str) -> Result<Vec<Resolved>, SourceError> {
5756        let filter = format!("{ORIGIN_FIELD}:{}", quoted(origin));
5757        let search = self.board_search(Some(&format!("in:body {}", quoted(&as_stored(origin)))));
5758        let mut items_after: Option<String> = None;
5759        let mut search_after: Option<String> = None;
5760        let mut found: Vec<Resolved> = Vec::new();
5761        let keep = |resolved: Resolved, found: &mut Vec<Resolved>| {
5762            if !found.iter().any(|held| held.id == resolved.id) {
5763                found.push(resolved);
5764            }
5765        };
5766        loop {
5767            let data = self
5768                .graphql(
5769                    graphql::ORIGIN_LOOKUP,
5770                    json!({"owner":self.owner,"number":self.project_number,"filter":filter,
5771                           "search":search,"type":"ISSUE","originFirst":ORIGIN_PAGE_SIZE,
5772                           "itemsAfter":items_after,"searchAfter":search_after,
5773                           "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
5774                           "duplicates":true}),
5775                )
5776                .await?;
5777            let items = data
5778                .pointer("/originItems/projectV2/items")
5779                .filter(|value| !value.is_null())
5780                .ok_or_else(|| SourceError::Refused {
5781                    message: format!(
5782                        "GitHub project {}/{} was not found or is not visible to the token",
5783                        self.owner, self.project_number
5784                    ),
5785                })?;
5786            for item in optional_nodes(Some(items), "project items")?
5787                .into_iter()
5788                .flatten()
5789            {
5790                // The board's own items list its drafts too, and a draft is not an issue: no
5791                // narrowed read answers with one, whatever its origin field holds.
5792                if let Some(resolved) = self.resolve(item)?
5793                    && resolved.content_kind == ContentKind::Issue
5794                {
5795                    keep(resolved, &mut found);
5796                }
5797            }
5798            let searched = data.get("search").ok_or_else(|| SourceError::Malformed {
5799                message: "GitHub search response has no search connection".into(),
5800            })?;
5801            for node in optional_nodes(Some(searched), "search")?
5802                .into_iter()
5803                .flatten()
5804            {
5805                if let Some(resolved) = self.resolve_issue(node).await? {
5806                    keep(resolved, &mut found);
5807                }
5808            }
5809            let items_next = resumed(items, items_after.as_deref())?;
5810            let search_next = resumed(searched, search_after.as_deref())?;
5811            if !items_next.has_more() && !search_next.has_more() {
5812                return Ok(found);
5813            }
5814            items_after = items_next.cursor();
5815            search_after = search_next.cursor();
5816        }
5817    }
5818
5819    /// `found`, with every item this process created or wrote in its place, and every one of
5820    /// them the read did not report added.
5821    ///
5822    /// This process's own record wins over the read's copy of the same item, because a read
5823    /// of an item written moments ago can still be behind what was written onto it — the
5824    /// origin field included, which is the one a narrowed read is confirmed against — and a
5825    /// read that still names an item under a predicate this process's write moved it out of
5826    /// must not return it. The one thing the read knows that the record cannot is when GitHub
5827    /// last saw the item change, which is what a comment-activity read rules a candidate out
5828    /// by, so the read's `updatedAt` is kept when the record has none of its own. See
5829    /// [`Self::created`] and [`Self::updated`](GitHubProjectsSource::updated).
5830    fn with_own_writes(&self, mut found: Vec<Resolved>) -> Result<Vec<Resolved>, SourceError> {
5831        // A board draft is not an issue, so no narrowed read returns one, and this process
5832        // having written one does not make it an answer either.
5833        let own: Vec<Resolved> = self
5834            .created()?
5835            .iter()
5836            .chain(self.updated()?.iter())
5837            .filter(|own| own.content_kind == ContentKind::Issue)
5838            .cloned()
5839            .collect();
5840        for mut own in own {
5841            self.resolved_cache()?.insert(own.id.clone(), own.clone());
5842            match found.iter_mut().find(|read| read.id == own.id) {
5843                Some(read) => {
5844                    own.updated_at = own.updated_at.max(read.updated_at);
5845                    *read = own;
5846                }
5847                None => found.push(own),
5848            }
5849        }
5850        Ok(found)
5851    }
5852
5853    /// Whether `item` has a comment created or last edited at or after `since` — always, when
5854    /// there is no instant to hold it to.
5855    ///
5856    /// The candidate's own `updatedAt` is read first, because a comment written or edited at
5857    /// or after the instant moved it there: an issue not updated since holds no such comment,
5858    /// and its comments are never asked for — unless this process commented on it in this
5859    /// command, when the `updatedAt` held may predate that comment; see [`Self::commented`]. Otherwise its comments are walked, oldest first,
5860    /// only as far as the first that matches. A board draft is not an issue and has no
5861    /// comments, so it never matches.
5862    async fn commented_since(
5863        &self,
5864        item: &Resolved,
5865        since: Option<DateTime<Utc>>,
5866    ) -> Result<bool, SourceError> {
5867        let Some(since) = since else {
5868            return Ok(true);
5869        };
5870        if item.content_kind == ContentKind::DraftIssue {
5871            return Ok(false);
5872        }
5873        // An `updatedAt` this process's own record or a lagging index holds can predate a
5874        // comment this process wrote since, so only an issue it did not comment on is ruled
5875        // out by one.
5876        if item.updated_at.is_some_and(|updated| updated < since)
5877            && !self.commented()?.contains(&item.id)
5878        {
5879            return Ok(false);
5880        }
5881        let query = TaskQuery {
5882            commented_since: Some(since),
5883            ..TaskQuery::default()
5884        };
5885        let mut after: Option<String> = None;
5886        loop {
5887            let data = self
5888                .graphql(
5889                    graphql::ISSUE_COMMENTS,
5890                    json!({"id":item.id.0,"first":MAX_PAGE_SIZE,"after":after}),
5891                )
5892                .await?;
5893            let Some(connection) = data
5894                .get("node")
5895                .filter(|value| !value.is_null())
5896                .and_then(|node| node.get("comments"))
5897                .filter(|value| !value.is_null())
5898            else {
5899                // Removed since the search reported it: no longer an issue with comments.
5900                return Ok(false);
5901            };
5902            let comments = optional_nodes(Some(connection), "issue comments")?
5903                .into_iter()
5904                .flatten()
5905                .map(comment_from)
5906                .collect::<Result<Vec<_>, _>>()?;
5907            if query.comments_match(&comments) {
5908                return Ok(true);
5909            }
5910            match next_cursor(connection)? {
5911                Some(next) => {
5912                    validate_cursor_progress(after.as_deref(), &next.0)?;
5913                    after = Some(next.0);
5914                }
5915                None => return Ok(false),
5916            }
5917        }
5918    }
5919
5920    /// Every item on the board: the union of both enumerations GitHub offers of one.
5921    ///
5922    /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
5923    /// board **draft** and reads the board's own fields beside its items, and only the search
5924    /// reports an item that connection is behind on. The module documentation is where the lag and the
5925    /// measurements behind it are written down.
5926    ///
5927    /// A search result is admitted on the same terms as any other issue this source reaches
5928    /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
5929    /// names *this* board — so an issue the index still believes is here after it was taken
5930    /// off is refused rather than reported.
5931    ///
5932    /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
5933    /// which is what the cache could otherwise have broken.
5934    async fn board(&self) -> Result<Board, SourceError> {
5935        let cached = self.board_cache()?.clone();
5936        let mut board = match cached {
5937            Some(board) => board,
5938            None => {
5939                let read = self.read_board().await?;
5940                *self.board_cache()? = Some(read.clone());
5941                read
5942            }
5943        };
5944        for held in self.searched_issues().await? {
5945            if !board.items.iter().any(|item| item.id == held.id) {
5946                board.items.push(held);
5947            }
5948        }
5949        for own in self.created()?.iter() {
5950            if !board.items.iter().any(|item| item.id == own.id) {
5951                board.items.push(own.clone());
5952            }
5953        }
5954        Ok(board)
5955    }
5956
5957    /// This process's own view of the board, or the refusal a poisoned lock is.
5958    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
5959        self.board_cache
5960            .lock()
5961            .map_err(|_| SourceError::Unavailable {
5962                message: "this source's view of the board was left inconsistent by an earlier \
5963                      failure; next: run the command again"
5964                    .into(),
5965            })
5966    }
5967
5968    /// Bring this process's own view of the board up to an item it has just written.
5969    ///
5970    /// A created item goes to `created`, which is what completes a board read GitHub's own
5971    /// eventual consistency has left behind. An item that was already there is replaced
5972    /// where it sits, so a second write of it in the same command reads its real parent
5973    /// rather than the one it had before the first write.
5974    ///
5975    /// "Where it sits" is three places, and missing an earlier one leaves a stale record
5976    /// that wins: an item this same run created is held in `created` and not in the cached
5977    /// board, and `board` completes the cached board *from* `created`, so replacing only
5978    /// the cached copy of such an item replaces nothing and the read still reports the
5979    /// title it was created with. The search is the third, and it is the one an item the
5980    /// board's own projection is behind on sits in *alone* — which is exactly the item this
5981    /// source is least able to re-read, so leaving it out would put the stale title back on
5982    /// the only items the completion in [`Self::board`] exists for.
5983    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
5984        self.resolved_cache()?.insert(item.id.clone(), item.clone());
5985        if created {
5986            self.created()?.push(item);
5987            return Ok(());
5988        }
5989        {
5990            let mut own = self.created()?;
5991            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
5992                *held = item;
5993                return Ok(());
5994            }
5995        }
5996        {
5997            let mut own = self.updated()?;
5998            match own.iter_mut().find(|held| held.id == item.id) {
5999                Some(held) => *held = item.clone(),
6000                None => own.push(item.clone()),
6001            }
6002        }
6003        if let Some(board) = self.board_cache()?.as_mut()
6004            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
6005        {
6006            *held = item.clone();
6007        }
6008        if let Some(found) = self.search_cache()?.as_mut()
6009            && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
6010        {
6011            *held = item.clone();
6012        }
6013        for found in self.narrowed_cache()?.values_mut() {
6014            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
6015                *held = item.clone();
6016            }
6017        }
6018        for (_, found) in self.children_cache()?.values_mut() {
6019            if let Some(held) = found.iter_mut().find(|held| held.id == item.id) {
6020                *held = item.clone();
6021            }
6022        }
6023        Ok(())
6024    }
6025
6026    /// Forget one item this process has just deleted, from every half of its own view.
6027    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
6028        self.resolved_cache()?.remove(id);
6029        self.created()?.retain(|own| own.id != *id);
6030        self.updated()?.retain(|own| own.id != *id);
6031        self.commented()?.retain(|own| own != id);
6032        if let Some(board) = self.board_cache()?.as_mut() {
6033            board.items.retain(|item| item.id != *id);
6034        }
6035        if let Some(found) = self.search_cache()?.as_mut() {
6036            found.retain(|item| item.id != *id);
6037        }
6038        for found in self.narrowed_cache()?.values_mut() {
6039            found.retain(|item| item.id != *id);
6040        }
6041        for (_, found) in self.children_cache()?.values_mut() {
6042            found.retain(|item| item.id != *id);
6043        }
6044        Ok(())
6045    }
6046
6047    /// This process's own record of each narrowed answer, or the refusal a poisoned lock is.
6048    fn narrowed_cache(
6049        &self,
6050    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<String, Vec<Resolved>>>, SourceError> {
6051        self.narrowed_cache
6052            .lock()
6053            .map_err(|_| SourceError::Unavailable {
6054                message: "this source's view of a narrowed read was left inconsistent by an \
6055                      earlier failure; next: run the command again"
6056                    .into(),
6057            })
6058    }
6059
6060    /// This process's own record of each project's sub-issues, or the refusal a poisoned lock
6061    /// is.
6062    fn children_cache(&self) -> Result<std::sync::MutexGuard<'_, ProjectChildren>, SourceError> {
6063        self.children_cache
6064            .lock()
6065            .map_err(|_| SourceError::Unavailable {
6066                message: "this source's view of a project's tasks was left inconsistent by an \
6067                      earlier failure; next: run the command again"
6068                    .into(),
6069            })
6070    }
6071
6072    /// Every page of the board, read from GitHub.
6073    async fn read_board(&self) -> Result<Board, SourceError> {
6074        let mut after: Option<String> = None;
6075        let mut items = Vec::new();
6076        let mut board;
6077        loop {
6078            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
6079            for item in page
6080                .pointer("/items/nodes")
6081                .and_then(Value::as_array)
6082                .ok_or_else(|| SourceError::Malformed {
6083                    message: "GitHub project items.nodes is not an array".into(),
6084                })?
6085            {
6086                if let Some(resolved) = self.resolve(item)? {
6087                    items.push(resolved);
6088                }
6089            }
6090            let info = page
6091                .pointer("/items/pageInfo")
6092                .ok_or_else(|| SourceError::Malformed {
6093                    message: "GitHub project items have no pageInfo".into(),
6094                })?;
6095            let has_next = required_bool(info, "hasNextPage")?;
6096            let next = has_next
6097                .then(|| required_str(info, "endCursor"))
6098                .transpose()?;
6099            board = page.clone();
6100            match next {
6101                Some(next) => {
6102                    validate_cursor_progress(after.as_deref(), next)?;
6103                    after = Some(next.to_owned());
6104                }
6105                None => break,
6106            }
6107        }
6108        Ok(Board {
6109            id: required_str(&board, "id")?.to_owned(),
6110            fields: board.get("fields").cloned().unwrap_or(Value::Null),
6111            items,
6112        })
6113    }
6114
6115    /// The existing items this source has written, for completing a narrowed read that is
6116    /// behind; see [`Self::updated`](GitHubProjectsSource::updated).
6117    fn updated(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
6118        self.updated.lock().map_err(|_| SourceError::Unavailable {
6119            message: "this source's record of what it wrote in this run was left inconsistent \
6120                      by an earlier failure; next: run the command again"
6121                .into(),
6122        })
6123    }
6124
6125    /// The issues this source has commented on in this command; see
6126    /// [`Self::commented`](GitHubProjectsSource::commented).
6127    fn commented(&self) -> Result<std::sync::MutexGuard<'_, Vec<NativeId>>, SourceError> {
6128        self.commented.lock().map_err(|_| SourceError::Unavailable {
6129            message: "this source's record of what it commented on in this run was left \
6130                      inconsistent by an earlier failure; next: run the command again"
6131                .into(),
6132        })
6133    }
6134
6135    /// Called only once GitHub has answered the comment write, so an issue whose comment
6136    /// failed is never made a candidate a later read would pay a node read for.
6137    fn remember_commented(&self, issue: &NativeId) -> Result<(), SourceError> {
6138        let mut commented = self.commented()?;
6139        if !commented.contains(issue) {
6140            commented.push(issue.clone());
6141        }
6142        Ok(())
6143    }
6144
6145    /// The items this source has created, for completing a board read that is behind.
6146    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
6147        self.created.lock().map_err(|_| SourceError::Unavailable {
6148            message: "this source's record of what it created in this run was left \
6149                      inconsistent by an earlier failure; next: run the command again"
6150                .into(),
6151        })
6152    }
6153
6154    /// One board item as this source reports it, or `None` for content it ignores.
6155    ///
6156    /// A pull request is neither a project nor a task — it is somebody's change, not a
6157    /// unit of plan — and an item whose content the token cannot see has nothing to
6158    /// report at all.
6159    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
6160        let resolved = self.resolve_unremembered(item)?;
6161        if let Some(resolved) = &resolved {
6162            self.resolved_cache()?
6163                .insert(resolved.id.clone(), resolved.clone());
6164        }
6165        Ok(resolved)
6166    }
6167
6168    /// [`Self::resolve`], without remembering the item it resolves: what reading a token off a
6169    /// write's own response needs, which says nothing this process should answer a later read
6170    /// with.
6171    fn resolve_unremembered(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
6172        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
6173            message: "GitHub project item is missing content".into(),
6174        })?;
6175        if content.is_null() {
6176            return Ok(None);
6177        }
6178        let content_kind = match required_str(content, "__typename")? {
6179            "Issue" => ContentKind::Issue,
6180            "DraftIssue" => ContentKind::DraftIssue,
6181            _ => return Ok(None),
6182        };
6183        let field_values = item
6184            .get("fieldValues")
6185            .ok_or_else(|| SourceError::Malformed {
6186                message: "GitHub project item is missing fieldValues".into(),
6187            })?;
6188        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
6189        let nodes = field_values
6190            .get("nodes")
6191            .and_then(Value::as_array)
6192            .ok_or_else(|| SourceError::Malformed {
6193                message: "GitHub project item fieldValues.nodes is not an array".into(),
6194            })?;
6195        if let Some(labels) = content.get("labels") {
6196            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
6197        }
6198        let raw_body = optional_str(content, "body")?.map(str::to_owned);
6199        let (body, slot) = metadata_body(raw_body.clone())?;
6200        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
6201            .map(|id| NativeId(id.to_owned()));
6202        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
6203        // to read one from; it is a task, and never a project.
6204        let sub_issues = match content_kind {
6205            ContentKind::Issue => sub_issue_total(content)?,
6206            ContentKind::DraftIssue => 0,
6207        };
6208        let content_id = required_str(content, "id")?;
6209        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
6210            message: format!("GitHub issue {content_id}: {message}"),
6211        })?;
6212        let raw_title = required_str(content, "title")?;
6213        let kind = BoardKind::of(raw_title, parent.is_some(), sub_issues, marked);
6214        // The title a person wrote, which for a document is the one without the prefix —
6215        // the same way `content` above is the body without this source's metadata slot.
6216        let title = match kind {
6217            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
6218            BoardKind::Work(_) => raw_title.to_owned(),
6219        };
6220        let own_repository = content
6221            .pointer("/repository/nameWithOwner")
6222            .and_then(Value::as_str)
6223            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
6224            .transpose()
6225            .map_err(|message| SourceError::Malformed { message })?;
6226        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
6227            Repository::from_metadata(&slot)
6228                .map_err(|message| SourceError::Malformed { message })?
6229        } else {
6230            own_repository.clone().into_iter().collect()
6231        };
6232        let classification = Classification::from_metadata(&slot)
6233            .map_err(|message| SourceError::Malformed { message })?;
6234        let id = NativeId(content_id.to_owned());
6235        // Read only for a task, because only a task has either list: a project or a
6236        // document holding one of these keys holds nothing this source reports, and the
6237        // keys are left out of its caller-visible metadata all the same.
6238        let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
6239            let listed = |key: &str| {
6240                TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
6241                    .map_err(|message| SourceError::Malformed { message })
6242            };
6243            (
6244                listed(TaskRef::DELIVERS_KEY)?,
6245                listed(TaskRef::DELIVERED_BY_KEY)?,
6246            )
6247        } else {
6248            (Vec::new(), Vec::new())
6249        };
6250        let (option, closed, reason) = Self::status_parts(nodes, content)?;
6251        let priority = self.held_priority(nodes)?;
6252        let item_labels = labels(content)?;
6253        // Present when the item was reached through its own issue, whose board entry
6254        // names the board; a read of the board's own items has the board already. An
6255        // empty id names nothing a field write could address, so it is read as absent and
6256        // the write goes back to reading the board.
6257        let board_id = item
6258            .pointer("/project/id")
6259            .and_then(Value::as_str)
6260            .filter(|id| !id.is_empty());
6261        let resolved = Resolved {
6262            item_id: required_str(item, "id")?.to_owned(),
6263            id,
6264            content_kind,
6265            kind,
6266            title,
6267            body: body.filter(|value| !value.is_empty()),
6268            raw_body,
6269            status: self
6270                .statuses
6271                .status(kind.status_kind(), option, closed, reason),
6272            option: option.map(str::to_owned),
6273            priority,
6274            closed,
6275            delivers,
6276            delivered_by,
6277            labels: item_labels.clone(),
6278            parent,
6279            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
6280            projected: self.held_projections(nodes)?,
6281            number: match content_kind {
6282                ContentKind::Issue => Some(issue_number(content)?),
6283                // A draft is filed in no repository, so nothing ever numbered it:
6284                // `DraftIssue` declares no `number` at all, exactly as it declares no
6285                // `subIssuesSummary` the branch above reads.
6286                ContentKind::DraftIssue => None,
6287            },
6288            url: optional_str(content, "url")?.map(str::to_owned),
6289            created_at: optional_time(content, "createdAt")?,
6290            updated_at: optional_time(content, "updatedAt")?,
6291            item_updated_at: optional_time(item, "updatedAt")?,
6292            comment_count: content
6293                .pointer("/commentCount/totalCount")
6294                .and_then(Value::as_u64),
6295            relationship_events: content
6296                .pointer("/relationshipEvents/totalCount")
6297                .and_then(Value::as_u64),
6298            // The issue node is stamped wherever it was read; a board item a read of the issue
6299            // put together from its memberships is not.
6300            served_at: match optional_time(content, SERVED_AT)? {
6301                Some(served) => Some(served),
6302                None => optional_time(item, SERVED_AT)?,
6303            },
6304            // Both connections were read to their end above — one reporting a further page is
6305            // refused — so these are every field value and every label the item holds. A
6306            // response that did not select the labels at all says nothing of them, and gives
6307            // no facets.
6308            facets: content.get("labels").is_some().then(|| {
6309                change_token::Facets::new(kind.facet(), facet_fields(nodes), &item_labels).digest()
6310            }),
6311            own_repository,
6312            repositories,
6313            classification,
6314            slot,
6315            board_id: board_id.map(str::to_owned),
6316            fields: field_definitions(nodes),
6317            board_fields: Self::carried_board_fields(content, board_id)?,
6318            blocked_by: carried_blocked_by(content)?,
6319        };
6320        Ok(Some(resolved))
6321    }
6322
6323    /// The field definitions of the board `board_id` names — the project this issue's own
6324    /// board item is on — off the `boards` page a read of an issue by its own id carries, or
6325    /// `None` when the read carried none, carried no entry for that board, or the board item
6326    /// named no board, which a write then answers by reading the board's fields itself.
6327    ///
6328    /// Matched by the board's node id and never by its number alone: a project number is
6329    /// unique only within its owner, so another owner's board numbered alike can sit on the
6330    /// same page, and its field and option ids address nothing on this one.
6331    fn carried_board_fields(
6332        content: &Value,
6333        board_id: Option<&str>,
6334    ) -> Result<Option<Value>, SourceError> {
6335        let (Some(nodes), Some(board_id)) = (
6336            content.pointer("/boards/nodes").and_then(Value::as_array),
6337            board_id,
6338        ) else {
6339            return Ok(None);
6340        };
6341        let Some(board) = nodes.iter().find_map(|node| {
6342            let project = node.get("project")?;
6343            (project.get("id").and_then(Value::as_str) == Some(board_id)).then_some(project)
6344        }) else {
6345            return Ok(None);
6346        };
6347        let Some(fields) = board.get("fields").filter(|fields| !fields.is_null()) else {
6348            return Ok(None);
6349        };
6350        complete_connection(fields, "board fields", NESTED_PAGE_SIZE)?;
6351        Ok(Some(fields.clone()))
6352    }
6353
6354    /// What one board item's `Priority` field says, through this instance's mapping.
6355    ///
6356    /// An instance with no mapping holds no priority, so every item reads as `none` whatever
6357    /// its board holds. With one, no value is `none`, a mapped option is its level, and an
6358    /// option the mapping does not name is kept as itself — never read as a level or as
6359    /// `none` — for a read of the task to report by name.
6360    fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
6361        let Some(mapping) = &self.priorities else {
6362            return Ok(HeldPriority::Read(Priority::None));
6363        };
6364        // A value of the field that names no option — a text field someone called `Priority` —
6365        // is malformed rather than `none`: reading it as no priority would let the next copy
6366        // clear one a person set.
6367        let Some(option) = field_values
6368            .iter()
6369            .find(|value| {
6370                value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
6371            })
6372            .map(|value| required_str(value, "name"))
6373            .transpose()?
6374        else {
6375            return Ok(HeldPriority::Read(Priority::None));
6376        };
6377        Ok(mapping.priority_of(option).map_or_else(
6378            || HeldPriority::Unmapped(option.to_owned()),
6379            HeldPriority::Read,
6380        ))
6381    }
6382
6383    /// What each projected metadata field holds for one board item, by field name — every text
6384    /// value GitHub answers, an empty one included.
6385    fn held_projections(
6386        &self,
6387        field_values: &[Value],
6388    ) -> Result<BTreeMap<String, String>, SourceError> {
6389        let mut held = BTreeMap::new();
6390        for projection in &self.metadata_fields {
6391            if let Some(text) = text_field(field_values, &projection.field)? {
6392                held.insert(projection.field.to_string(), text);
6393            }
6394        }
6395        Ok(held)
6396    }
6397
6398    /// What one board item's status is read from: its `Status` option, whether its issue
6399    /// is closed, and the reason it was closed with. [`BoardStatuses::status`] turns the
6400    /// three into the status it reports.
6401    fn status_parts<'a>(
6402        field_values: &'a [Value],
6403        content: &'a Value,
6404    ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
6405        let option = field_values
6406            .iter()
6407            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
6408            .map(|value| required_str(value, "name"))
6409            .transpose()?;
6410        let closed = optional_str(content, "state")? == Some("CLOSED");
6411        Ok((option, closed, optional_str(content, "stateReason")?))
6412    }
6413
6414    /// The board Status option this write selects, or the refusal that says why not.
6415    ///
6416    /// The mapped option is required for both open and terminal targets. A terminal write
6417    /// validates it before changing either representation, so it can never fall back to
6418    /// closing an issue whose board cannot display the matching status.
6419    ///
6420    /// Answers the field's id, the option's id, and the option's name as the board spells
6421    /// it — which is the name a read of the item reports once it sits there.
6422    fn column_for(
6423        &self,
6424        fields: &Value,
6425        kind: ItemKind,
6426        category: StatusCategory,
6427        target: &StatusTarget,
6428    ) -> Result<Option<(String, String, String)>, SourceError> {
6429        let Some(wanted) = target.option() else {
6430            return Ok(None);
6431        };
6432        let missing = |detail: &str| SourceError::Refused {
6433            message: format!(
6434                "{} status {} of source {} needs the board Status option {wanted:?}, and \
6435                 {detail}; next: add that option to the board, which `onetaskgraph sources \
6436                 fields {} --apply` does, or point status_mapping.{}.{} of this source at one \
6437                 it has",
6438                kind.marker(),
6439                category_name(category),
6440                self.name,
6441                self.name,
6442                category_name(category),
6443                kind.marker()
6444            ),
6445        };
6446        let Some(field) = Board::field(fields, "Status")? else {
6447            return Err(missing("this board has no Status field"));
6448        };
6449        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
6450            return Err(missing(
6451                "this board's Status field is not a single-select field",
6452            ));
6453        }
6454        let option = field
6455            .get("options")
6456            .and_then(Value::as_array)
6457            .and_then(|options| {
6458                options.iter().find(|option| {
6459                    option
6460                        .get("name")
6461                        .and_then(Value::as_str)
6462                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
6463                })
6464            });
6465        match option {
6466            None => Err(missing("this board does not have it")),
6467            Some(option) => Ok(Some((
6468                required_str(field, "id")?.to_owned(),
6469                required_str(option, "id")?.to_owned(),
6470                required_str(option, "name")?.to_owned(),
6471            ))),
6472        }
6473    }
6474
6475    /// The refusal a status that closes an issue is answered with over a board draft.
6476    fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
6477        SourceError::Refused {
6478            message: format!(
6479                "status {} of source {} closes the item's issue, and GitHub draft items have \
6480                 no open or closed state",
6481                category_name(category),
6482                self.name
6483            ),
6484        }
6485    }
6486
6487    /// What a status write to one item needs of the board: the board's id and the
6488    /// definition of its `Status` field, read off the item when the item says both.
6489    ///
6490    /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
6491    /// and its `Status` value carries that field's definition, options and all. An item that
6492    /// does not say — no board id, or no `Status` value to read the field off — takes them
6493    /// from [`Self::board_fields`], which reads no item.
6494    async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
6495        if let Some(board) = item.carried_board() {
6496            return Ok(board);
6497        }
6498        if item.defines("Status")
6499            && let Some(board_id) = item.named_board()
6500        {
6501            return Ok(BoardFields {
6502                id: board_id,
6503                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6504            });
6505        }
6506        self.board_fields().await
6507    }
6508
6509    /// Set one task's status and nothing else; see [`TaskSource::set_task_status`].
6510    async fn set_status(
6511        &self,
6512        id: &NativeId,
6513        category: StatusCategory,
6514    ) -> Result<Option<Status>, SourceError> {
6515        // Refused before anything is read, in the words a write of the same status is.
6516        let target = self.resolved_target(ItemKind::Task, category)?;
6517        let Some(mut item) = self
6518            .bound_item(id)
6519            .await?
6520            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6521        else {
6522            return Ok(None);
6523        };
6524        let board = self.status_board(&item).await?;
6525        let (field, option, name) = self
6526            .column_for(&board.fields, ItemKind::Task, category, &target)?
6527            .ok_or_else(|| SourceError::Malformed {
6528                message: format!(
6529                    "status {} of source {} names no board Status option",
6530                    category_name(category),
6531                    self.name
6532                ),
6533            })?;
6534        if item.status.category == category && item.option.as_deref() == Some(&name) {
6535            return Ok(Some(item.status));
6536        }
6537        match &target {
6538            StatusTarget::Terminal(_, reason) => {
6539                if item.content_kind == ContentKind::DraftIssue {
6540                    return Err(self.closes_a_draft(category));
6541                }
6542                self.set_item_field(
6543                    board.id.as_str(),
6544                    &item.item_id,
6545                    &field,
6546                    json!({"singleSelectOptionId": option}),
6547                )
6548                .await?;
6549                self.update_content(
6550                    ContentKind::Issue,
6551                    &item.id,
6552                    json!({"stateInput": state_input(Some(&target))}),
6553                )
6554                .await?;
6555                item.closed = true;
6556                item.status =
6557                    self.statuses
6558                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()));
6559                item.option = Some(name);
6560            }
6561            StatusTarget::Column(_) => {
6562                // An option is what an open item's status is, so a closed issue is reopened
6563                // first — sitting closed in the column, it would read back as closed. A draft has
6564                // no state to reopen.
6565                if item.content_kind == ContentKind::Issue && item.closed {
6566                    self.update_content(
6567                        ContentKind::Issue,
6568                        &item.id,
6569                        json!({"stateInput": state_input(Some(&target))}),
6570                    )
6571                    .await?;
6572                    item.closed = false;
6573                }
6574                self.set_item_field(
6575                    board.id.as_str(),
6576                    &item.item_id,
6577                    &field,
6578                    json!({"singleSelectOptionId": option}),
6579                )
6580                .await?;
6581                item.status = self
6582                    .statuses
6583                    .status(ItemKind::Task, Some(&name), false, None);
6584                item.option = Some(name);
6585            }
6586            StatusTarget::Disabled(_) => {
6587                unreachable!("resolved_target refused a disabled status")
6588            }
6589        }
6590        let status = item.status.clone();
6591        self.remember_written(item, false)?;
6592        Ok(Some(status))
6593    }
6594
6595    /// Replace one task's `delivered_by` and nothing else; see
6596    /// [`TaskSource::set_delivered_by`].
6597    ///
6598    /// One update of the body, which differs from the body GitHub holds only inside the
6599    /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
6600    async fn replace_delivered_by(
6601        &self,
6602        id: &NativeId,
6603        delivered_by: &[TaskRef],
6604    ) -> Result<Option<()>, SourceError> {
6605        let entries = TaskRef::listed(
6606            TaskRef::DELIVERED_BY_KEY,
6607            id,
6608            Some(&self.name),
6609            delivered_by.to_vec(),
6610        )
6611        .map_err(|message| SourceError::Refused { message })?;
6612        let Some(mut item) = self
6613            .bound_item(id)
6614            .await?
6615            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6616        else {
6617            return Ok(None);
6618        };
6619        let mut slot = item.slot.clone();
6620        set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
6621        self.write_slot(&mut item, &slot).await?;
6622        item.delivered_by = entries;
6623        self.remember_written(item, false)?;
6624        Ok(Some(()))
6625    }
6626
6627    /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
6628    /// see [`TaskSource::set_task_metadata`].
6629    ///
6630    /// `None` when this board holds no item by that id, or holds one of another kind. The
6631    /// answer is the item as this source now reads it, so what a caller is told the key
6632    /// holds is what the slot holds.
6633    ///
6634    /// A key already holding the value is answered without a write, compared as JSON rather
6635    /// than as the body's bytes: a slot a person spelled with other whitespace would
6636    /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
6637    async fn set_slot_key(
6638        &self,
6639        id: &NativeId,
6640        kind: BoardKind,
6641        key: &MetadataKey,
6642        value: &Value,
6643    ) -> Result<Option<Resolved>, SourceError> {
6644        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
6645            return Ok(None);
6646        };
6647        if item.slot.get(key.as_str()) == Some(value) {
6648            return Ok(Some(item));
6649        }
6650        let mut slot = item.slot.clone();
6651        slot.insert(key.as_str().to_owned(), value.clone());
6652        // A key a board text field projects moves that field first, so the body — the value's
6653        // one authoritative home — is written last, as every write of an existing item is.
6654        if self
6655            .metadata_fields
6656            .iter()
6657            .any(|projection| &*projection.key == key.as_str())
6658        {
6659            let board = self.projection_board(&item).await?;
6660            let (writes, projected) = self.projection_writes(
6661                &board.fields,
6662                Some(&item.projected),
6663                &slot,
6664                Some(&[key.as_str()]),
6665            )?;
6666            let (mut values, mut clears) = (Vec::new(), Vec::new());
6667            for write in &writes {
6668                write.join(&mut values, &mut clears);
6669            }
6670            self.set_item_fields(board.id.as_str(), &item.item_id, &values, &clears)
6671                .await?;
6672            item.projected = projected;
6673        }
6674        self.write_slot(&mut item, &slot).await?;
6675        self.remember_written(item.clone(), false)?;
6676        Ok(Some(item))
6677    }
6678
6679    /// What a projection write to `item` needs of the board, read off the item when it says
6680    /// enough, as [`Self::fields_for`] reads it.
6681    async fn projection_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
6682        if let Some(board) = item.carried_board() {
6683            return Ok(board);
6684        }
6685        if let Some(board_id) = item.named_board()
6686            && self
6687                .metadata_fields
6688                .iter()
6689                .all(|projection| item.defines(&projection.field))
6690        {
6691            return Ok(BoardFields {
6692                id: board_id,
6693                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
6694            });
6695        }
6696        self.board_fields().await
6697    }
6698
6699    /// Put `slot` in one item's metadata slot with a single update of its body, and bring
6700    /// `item` up to what that write left.
6701    ///
6702    /// The body sent differs from the body GitHub holds only inside the slot — see
6703    /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
6704    /// the mutation the item's content takes, so a board draft's body is written with
6705    /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
6706    async fn write_slot(
6707        &self,
6708        item: &mut Resolved,
6709        slot: &BTreeMap<String, Value>,
6710    ) -> Result<(), SourceError> {
6711        let held = item.raw_body.clone().unwrap_or_default();
6712        let body = with_slot(&held, slot)?;
6713        if body != held {
6714            self.update_content(item.content_kind, &item.id, json!({"body": body}))
6715                .await?;
6716        }
6717        let (visible, slot) = metadata_body(Some(body.clone()))?;
6718        item.body = visible.filter(|value| !value.is_empty());
6719        item.raw_body = Some(body);
6720        item.slot = slot;
6721        Ok(())
6722    }
6723
6724    /// This instance's target for a category written to an item of `kind`, refusing one
6725    /// that kind has no option for — before anything is read or written.
6726    ///
6727    /// Nothing here mutates the board's option set to make room for a status. GitHub
6728    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
6729    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
6730    /// field and every item's status.
6731    fn resolved_target(
6732        &self,
6733        kind: ItemKind,
6734        category: StatusCategory,
6735    ) -> Result<StatusTarget, SourceError> {
6736        let target = self.statuses.target(kind, category).clone();
6737        let StatusTarget::Disabled(why) = target else {
6738            return Ok(target);
6739        };
6740        let refusal = why.refusal(&self.name, category, kind);
6741        // Why there is no shipped default, which is the question a person meeting this
6742        // refusal on a source that never mentioned the category asks.
6743        let shipped_none = match category {
6744            StatusCategory::Draft => Some(
6745                "draft has no shipped default because GitHub draft issues cannot have \
6746                 sub-issues, and this source stores a project's tasks as its issue's sub-issues",
6747            ),
6748            StatusCategory::Unknown => Some(
6749                "unknown has no shipped default because this board keeps no open-ended status \
6750                 word: every word classified unknown is written to the one board Status option \
6751                 status_mapping.unknown names",
6752            ),
6753            _ => None,
6754        };
6755        Err(match (refusal, shipped_none, why) {
6756            (SourceError::Refused { message }, Some(note), UnmappedStatus::Unconfigured) => {
6757                SourceError::Refused {
6758                    message: format!("{message}; {note}"),
6759                }
6760            }
6761            (refusal, _, _) => refusal,
6762        })
6763    }
6764
6765    /// What writing `priority` does to one item's `Priority` field on this board, or the
6766    /// refusal naming what the board lacks.
6767    ///
6768    /// `none` is no value, so it clears the field — and asks nothing of an item that holds
6769    /// none already, or of an item not created yet. Every other priority selects the option
6770    /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
6771    /// without that option, is refused rather than given one: reads and writes never create
6772    /// a field or an option.
6773    fn priority_write(
6774        &self,
6775        fields: &Value,
6776        existing: Option<&Resolved>,
6777        priority: Priority,
6778    ) -> Result<Option<PriorityWrite>, SourceError> {
6779        let Some(mapping) = &self.priorities else {
6780            return Err(self.holds_no_priority());
6781        };
6782        let Some(wanted) = mapping.option(priority) else {
6783            if !existing.is_some_and(Resolved::holds_priority) {
6784                return Ok(None);
6785            }
6786            let field =
6787                Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
6788                    message: format!(
6789                        "an item holding a {PRIORITY_FIELD} value was read without that field"
6790                    ),
6791                })?;
6792            return Ok(Some(PriorityWrite::Clear {
6793                field: required_str(field, "id")?.to_owned(),
6794            }));
6795        };
6796        let missing = |detail: &str| SourceError::Refused {
6797            message: format!(
6798                "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
6799                 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
6800                 it, or point priority_mapping.{priority} of this source at an option the board \
6801                 has",
6802                self.name, self.name
6803            ),
6804        };
6805        let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
6806            return Err(missing(&format!(
6807                "this board has no {PRIORITY_FIELD} field"
6808            )));
6809        };
6810        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
6811            return Err(missing(&format!(
6812                "this board's {PRIORITY_FIELD} field is not a single-select field"
6813            )));
6814        }
6815        // An options list that is absent or not a list is an answer this source cannot read,
6816        // not a board lacking the option: `sources fields --apply` is no remedy for it.
6817        let option = field
6818            .get("options")
6819            .and_then(Value::as_array)
6820            .ok_or_else(|| SourceError::Malformed {
6821                message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
6822            })?
6823            .iter()
6824            .find(|option| {
6825                option
6826                    .get("name")
6827                    .and_then(Value::as_str)
6828                    .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
6829            })
6830            .ok_or_else(|| missing("this board does not have it"))?;
6831        Ok(Some(PriorityWrite::Select {
6832            field: required_str(field, "id")?.to_owned(),
6833            option: required_str(option, "id")?.to_owned(),
6834        }))
6835    }
6836
6837    /// What writing `metadata` does to each projected text field of one item, and what those
6838    /// fields hold after it — or the refusal, before anything is sent.
6839    ///
6840    /// `held` is what the item's fields hold now, `None` for an item not created yet. `keys`
6841    /// narrows the work to the entries projecting one of those metadata keys, for a write that
6842    /// touches only them; `None` is every entry. A string the field already holds, and nothing
6843    /// for a field holding nothing, sends nothing. A value no text field can hold is refused,
6844    /// and so is a board without the field, or with a field of that name that is not a text
6845    /// field: reads and writes never create a field.
6846    fn projection_writes(
6847        &self,
6848        fields: &Value,
6849        held: Option<&BTreeMap<String, String>>,
6850        metadata: &BTreeMap<String, Value>,
6851        keys: Option<&[&str]>,
6852    ) -> Result<(Vec<ProjectionWrite>, BTreeMap<String, String>), SourceError> {
6853        let mut projected = held.cloned().unwrap_or_default();
6854        let mut writes = Vec::new();
6855        for projection in self
6856            .metadata_fields
6857            .iter()
6858            .filter(|projection| keys.is_none_or(|keys| keys.contains(&&*projection.key)))
6859        {
6860            let wanted = projection.projected(metadata, &self.name)?;
6861            let field = self.projection_field(fields, projection)?;
6862            let holding = projected.get(&*projection.field);
6863            match wanted {
6864                Projected::Text(text) if holding != Some(&text) => {
6865                    writes.push(ProjectionWrite::Set {
6866                        field,
6867                        text: text.clone(),
6868                    });
6869                    projected.insert(projection.field.to_string(), text);
6870                }
6871                Projected::Nothing if holding.is_some() => {
6872                    writes.push(ProjectionWrite::Clear { field });
6873                    projected.remove(&*projection.field);
6874                }
6875                Projected::Text(_) | Projected::Nothing => {}
6876            }
6877        }
6878        Ok((writes, projected))
6879    }
6880
6881    /// The id of the board text field one projection writes, or the refusal naming what the
6882    /// board has instead.
6883    fn projection_field(
6884        &self,
6885        fields: &Value,
6886        projection: &MetadataField,
6887    ) -> Result<String, SourceError> {
6888        let refuse = |detail: String| SourceError::Refused {
6889            message: format!(
6890                "source {} projects metadata key {:?} onto the board text field {:?}, and \
6891                 {detail}; next: run `onetaskgraph sources fields {} --apply` to create it as a \
6892                 text field, or name another field in metadata_fields",
6893                self.name, &*projection.key, &*projection.field, self.name
6894            ),
6895        };
6896        let Some(field) = Board::field(fields, &projection.field)? else {
6897            return Err(refuse("this board has no field of that name".to_owned()));
6898        };
6899        let typename = optional_str(field, "__typename")?.unwrap_or("field of another type");
6900        let data_type = optional_str(field, "dataType")?;
6901        if typename != "ProjectV2Field" || data_type != Some("TEXT") {
6902            return Err(refuse(format!(
6903                "this board's field of that name is not a text field (it is a {}), so it is \
6904                 left as it is; rename or remove that field first",
6905                data_type.unwrap_or(typename)
6906            )));
6907        }
6908        Ok(required_str(field, "id")?.to_owned())
6909    }
6910
6911    /// Apply one priority write to one board item.
6912    async fn write_priority(
6913        &self,
6914        board_id: &str,
6915        item_id: &str,
6916        write: &PriorityWrite,
6917    ) -> Result<(), SourceError> {
6918        match write {
6919            PriorityWrite::Select { field, option } => {
6920                self.set_item_field(
6921                    board_id,
6922                    item_id,
6923                    field,
6924                    json!({"singleSelectOptionId": option}),
6925                )
6926                .await
6927            }
6928            PriorityWrite::Clear { field } => {
6929                let data = self
6930                    .graphql(
6931                        graphql::CLEAR_FIELD,
6932                        json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field},
6933                            "readPriority":false,"priorityName":PRIORITY_FIELD}),
6934                    )
6935                    .await?;
6936                let returned = data
6937                    .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
6938                    .ok_or_else(|| SourceError::Malformed {
6939                        message: "GitHub field clear returned no project item".into(),
6940                    })?;
6941                if required_str(returned, "id")? != item_id {
6942                    return Err(SourceError::Malformed {
6943                        message: "GitHub field clear returned the wrong project item".into(),
6944                    });
6945                }
6946                Ok(())
6947            }
6948        }
6949    }
6950
6951    /// The refusal a priority is answered with by an instance configured with no
6952    /// `priority_mapping`, which holds none.
6953    fn holds_no_priority(&self) -> SourceError {
6954        SourceError::Refused {
6955            message: format!(
6956                "source {} holds no task priority: its configuration sets no priority_mapping; \
6957                 next: set priority_mapping on this source, then run `onetaskgraph sources \
6958                 fields {} --apply` to set its board up",
6959                self.name, self.name
6960            ),
6961        }
6962    }
6963
6964    /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
6965    ///
6966    /// One field write — a select, or a clear for `none` — and no title, body, label, state
6967    /// or `Status` request. Clearing a priority an item does not hold sends nothing.
6968    async fn set_priority(
6969        &self,
6970        id: &NativeId,
6971        priority: Priority,
6972    ) -> Result<Option<Priority>, SourceError> {
6973        if self.priorities.is_none() {
6974            return Err(self.holds_no_priority());
6975        }
6976        let Some(mut item) = self
6977            .bound_item(id)
6978            .await?
6979            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6980        else {
6981            return Ok(None);
6982        };
6983        if priority == Priority::None && !item.holds_priority() {
6984            return Ok(Some(priority));
6985        }
6986        // The item's own read carries the field's definition whenever it holds a value of
6987        // it, which a clear always does; a select onto an item holding none reads the board.
6988        let board = match (item.carried_board(), item.named_board()) {
6989            (Some(board), _) => board,
6990            (None, Some(id)) if item.defines(PRIORITY_FIELD) => BoardFields {
6991                id,
6992                fields: json!({"nodes": item.fields.clone(), "pageInfo": {"hasNextPage": false}}),
6993            },
6994            _ => self.board_fields().await?,
6995        };
6996        let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
6997            return Ok(Some(priority));
6998        };
6999        let (document, root, input) = match write {
7000            PriorityWrite::Select { field, option } => (
7001                graphql::UPDATE_FIELD,
7002                "updateProjectV2ItemFieldValue",
7003                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field,"value":{"singleSelectOptionId":option}}),
7004            ),
7005            PriorityWrite::Clear { field } => (
7006                graphql::CLEAR_FIELD,
7007                "clearProjectV2ItemFieldValue",
7008                json!({"projectId":board.id.as_str(),"itemId":item.item_id,"fieldId":field}),
7009            ),
7010        };
7011        let data = self
7012            .graphql(
7013                document,
7014                json!({"input":input,"readPriority":true,"priorityName":PRIORITY_FIELD}),
7015            )
7016            .await?;
7017        let returned = data
7018            .get(root)
7019            .and_then(|value| value.get("projectV2Item"))
7020            .ok_or_else(|| SourceError::Malformed {
7021                message: "GitHub priority write returned no project item".into(),
7022            })?;
7023        if required_str(returned, "id")? != item.item_id {
7024            return Err(SourceError::Malformed {
7025                message: "GitHub priority write returned the wrong project item".into(),
7026            });
7027        }
7028        let value = returned
7029            .get("fieldValueByName")
7030            .ok_or_else(|| SourceError::Malformed {
7031                message: "GitHub priority write returned no priority read-back".into(),
7032            })?;
7033        if !value.is_null()
7034            && value.pointer("/field/name").and_then(Value::as_str) != Some(PRIORITY_FIELD)
7035        {
7036            return Err(SourceError::Malformed {
7037                message: "GitHub priority read-back is not a Priority field value".into(),
7038            });
7039        }
7040        let values = if value.is_null() {
7041            Vec::new()
7042        } else {
7043            vec![value.clone()]
7044        };
7045        item.priority = self.held_priority(&values)?;
7046        let answer = item.task()?.priority;
7047        self.remember_written(item, false)?;
7048        Ok(Some(answer))
7049    }
7050
7051    /// Replace one task's visible body and nothing else; see
7052    /// [`TaskSource::set_task_content`].
7053    ///
7054    /// One update of the body, which differs from the body GitHub holds only outside the
7055    /// metadata slot — the slot is kept byte for byte, so every caller key and every list
7056    /// this source keeps there reads back as it was. A body that would not change is not
7057    /// sent at all.
7058    async fn replace_content(
7059        &self,
7060        id: &NativeId,
7061        content: &str,
7062    ) -> Result<Option<()>, SourceError> {
7063        let Some(mut item) = self
7064            .bound_item(id)
7065            .await?
7066            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7067        else {
7068            return Ok(None);
7069        };
7070        let held = item.raw_body.clone().unwrap_or_default();
7071        let body = with_content(&held, content)?;
7072        // Checked before anything is sent: content ending in what this source reads as its own
7073        // metadata slot would read back as metadata rather than as the content it was.
7074        let (visible, slot) = metadata_body(Some(body.clone()))?;
7075        if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
7076            return Err(SourceError::Refused {
7077                message: format!(
7078                    "this content ends in what source {} reads as its own metadata slot \
7079                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7080                     as content; next: remove that trailing block from the content",
7081                    self.name
7082                ),
7083            });
7084        }
7085        if body != held {
7086            self.update_content(item.content_kind, &item.id, json!({"body": body}))
7087                .await?;
7088        }
7089        item.body = visible.filter(|value| !value.is_empty());
7090        item.raw_body = Some(body);
7091        item.slot = slot;
7092        self.remember_written(item, false)?;
7093        Ok(Some(()))
7094    }
7095
7096    /// Apply one targeted update to one task; see [`TaskSource::update_task`].
7097    ///
7098    /// One read of the item — which carries the board's field definitions and the issue's
7099    /// `blockedBy`, so neither is read again — and then only what differs from it: the
7100    /// `Status` option and the `Priority` field together in one request, the `blockedBy`
7101    /// additions and removals the named edges differ by, and last one `updateIssue` carrying
7102    /// the title, the body — visible content and metadata slot together — and a state change.
7103    /// So an update naming any of title, body, metadata, status and priority is one read and
7104    /// at most two writes. The body goes last so that a write refused part-way leaves it, and
7105    /// the metadata in it, as it stood. A terminal status selects its option and then closes,
7106    /// as a whole write does; an open one selects its option and then reopens. The origin
7107    /// field is never written: an update is of an item that already exists, whose origin is
7108    /// what it is.
7109    ///
7110    /// The task answered is the item as those writes left it, built from the read and what was
7111    /// sent rather than read again — the same record a later read in this run answers from.
7112    async fn targeted_update(
7113        &self,
7114        id: &NativeId,
7115        update: &TaskUpdate,
7116    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
7117        // Everything this source can refuse without reading the item is refused first, in the
7118        // words a whole write of the same fields is refused with.
7119        update.consistent()?;
7120        if update
7121            .title
7122            .as_deref()
7123            .is_some_and(|title| title.starts_with(DESIGN_TITLE_PREFIX))
7124        {
7125            return Err(SourceError::Refused {
7126                message: format!(
7127                    "the title of this task begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
7128                     spells a document, so it would read back as one rather than as a task; \
7129                     retitle it",
7130                    self.name
7131                ),
7132            });
7133        }
7134        if let Some(delivers) = &update.delivers {
7135            TaskRef::listed(
7136                TaskRef::DELIVERS_KEY,
7137                id,
7138                Some(&self.name),
7139                delivers.clone(),
7140            )
7141            .map_err(|message| SourceError::Refused { message })?;
7142        }
7143        if self.priorities.is_none()
7144            && update
7145                .priority
7146                .is_some_and(|priority| priority != Priority::None)
7147        {
7148            return Err(self.holds_no_priority());
7149        }
7150        let target = update
7151            .status
7152            .as_ref()
7153            .map(|status| self.resolved_target(ItemKind::Task, status.category))
7154            .transpose()?;
7155        let Some(mut item) = self
7156            .bound_item(id)
7157            .await?
7158            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
7159        else {
7160            return Ok(None);
7161        };
7162        let before = item.task()?;
7163
7164        let mut status_move = None;
7165        if let (Some(status), Some(target)) = (&update.status, target) {
7166            let board = self.status_board(&item).await?;
7167            let (field, option, name) = self
7168                .column_for(&board.fields, ItemKind::Task, status.category, &target)?
7169                .ok_or_else(|| SourceError::Malformed {
7170                    message: format!(
7171                        "status {} of source {} names no board Status option",
7172                        category_name(status.category),
7173                        self.name
7174                    ),
7175                })?;
7176            let terminal = matches!(target, StatusTarget::Terminal(_, _));
7177            if terminal && item.content_kind == ContentKind::DraftIssue {
7178                return Err(self.closes_a_draft(status.category));
7179            }
7180            let landed = match &target {
7181                StatusTarget::Terminal(_, reason) => {
7182                    self.statuses
7183                        .status(ItemKind::Task, Some(&name), true, Some(reason.reason()))
7184                }
7185                _ => self
7186                    .statuses
7187                    .status(ItemKind::Task, Some(&name), false, None),
7188            };
7189            let option_moves = item
7190                .option
7191                .as_deref()
7192                .is_none_or(|held| !held.eq_ignore_ascii_case(&name));
7193            let state_moves = item.content_kind == ContentKind::Issue
7194                && (item.closed != terminal || (terminal && item.status != landed));
7195            if let Some(moves) = Moves::of(option_moves, state_moves) {
7196                status_move = Some(StatusMove {
7197                    board: board.id,
7198                    field,
7199                    option,
7200                    name,
7201                    target,
7202                    landed,
7203                    moves,
7204                });
7205            }
7206        }
7207
7208        let mut priority_move = None;
7209        if let Some(priority) = update.priority
7210            && self.priorities.is_some()
7211            && item.priority != HeldPriority::Read(priority)
7212        {
7213            let board = match (item.carried_board(), item.named_board()) {
7214                (Some(board), _) => board,
7215                (None, Some(board)) if item.defines(PRIORITY_FIELD) => BoardFields {
7216                    id: board,
7217                    fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
7218                },
7219                _ => self.board_fields().await?,
7220            };
7221            if let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? {
7222                priority_move = Some((board.id, write, priority));
7223            }
7224        }
7225
7226        // The metadata keys this update names that a board text field projects, and what
7227        // that does to those fields — refused, as a whole write refuses it, before anything
7228        // is sent.
7229        let touched: Vec<&str> = update
7230            .metadata_set
7231            .keys()
7232            .chain(update.metadata_remove.iter())
7233            .map(MetadataKey::as_str)
7234            .collect();
7235        let mut projection_move = None;
7236        if self
7237            .metadata_fields
7238            .iter()
7239            .any(|projection| touched.contains(&&*projection.key))
7240        {
7241            let mut metadata = item.slot.clone();
7242            for (key, value) in &update.metadata_set {
7243                metadata.insert(key.as_str().to_owned(), value.clone());
7244            }
7245            for key in &update.metadata_remove {
7246                metadata.remove(key.as_str());
7247            }
7248            let board = self.projection_board(&item).await?;
7249            let (writes, projected) = self.projection_writes(
7250                &board.fields,
7251                Some(&item.projected),
7252                &metadata,
7253                Some(&touched),
7254            )?;
7255            projection_move = Some((board.id, writes, projected));
7256        }
7257
7258        // Resolved before the body is composed, because a far end `blockedBy` cannot name is
7259        // recorded in the slot, and the slot travels in the one body update below.
7260        let edges = match &update.depends_on {
7261            Some(edges) => Some(
7262                self.partition_edges(
7263                    BoardKind::Work(ItemKind::Task),
7264                    item.content_kind,
7265                    item.blocked_by.as_deref(),
7266                    edges,
7267                )
7268                .await?,
7269            ),
7270            None => None,
7271        };
7272
7273        let mut slot = item.slot.clone();
7274        for (key, value) in &update.metadata_set {
7275            slot.insert(key.as_str().to_owned(), value.clone());
7276        }
7277        for key in &update.metadata_remove {
7278            slot.remove(key.as_str());
7279        }
7280        if let Some(delivers) = &update.delivers {
7281            set_task_list(&mut slot, TaskRef::DELIVERS_KEY, delivers);
7282        }
7283        if let Some((_, recorded)) = &edges {
7284            record_edges(&mut slot, recorded);
7285        }
7286        let held = item.raw_body.clone().unwrap_or_default();
7287        let content = match &update.content {
7288            Some(content) => with_content(&held, content)?,
7289            None => held.clone(),
7290        };
7291        // A slot holding what it held is kept byte for byte, compared as JSON rather than as
7292        // the body's bytes, as a metadata write compares it: a slot a person spelled with
7293        // other whitespace would otherwise be re-encoded, which is a write nobody asked for.
7294        let body = if slot == item.slot {
7295            content
7296        } else {
7297            with_slot(&content, &slot)?
7298        };
7299        // Checked before anything is sent, as a content write checks it: content ending in
7300        // what this source reads as its own slot would read back as metadata.
7301        let (visible, read) = metadata_body(Some(body.clone()))?;
7302        let wanted = update.content.as_deref().or(item.body.as_deref());
7303        if visible.as_deref().unwrap_or_default() != wanted.unwrap_or_default() || read != slot {
7304            return Err(SourceError::Refused {
7305                message: format!(
7306                    "this content ends in what source {} reads as its own metadata slot \
7307                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7308                     as content; next: remove that trailing block from the content",
7309                    self.name
7310                ),
7311            });
7312        }
7313        let recorded_moves =
7314            slot.get(DependencyEdge::RECORDED_KEY) != item.slot.get(DependencyEdge::RECORDED_KEY);
7315
7316        // One `updateIssue` carries all three, because every mutation spends the secondary
7317        // limiter and the title, body and state are one mutation's inputs.
7318        let mut fields = serde_json::Map::new();
7319        if let Some(title) = update.title.as_ref().filter(|title| **title != item.title) {
7320            fields.insert("title".to_owned(), json!(title));
7321        }
7322        if body != held {
7323            fields.insert("body".to_owned(), json!(body));
7324        }
7325        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.state()) {
7326            fields.insert("stateInput".to_owned(), state_input(Some(&moving.target)));
7327        }
7328        // **The body is written last, and that is the guarantee a refusal part-way keeps.**
7329        // GitHub runs no two requests as one, and runs one document's mutation fields in order
7330        // without undoing an earlier field when a later one fails — so a body written before a
7331        // board field the board then refused would be left changed. Written after every other
7332        // write has landed, a refusal anywhere leaves the item's body, and every metadata key
7333        // it carries, exactly as they stood. So the `Status` option and the `Priority` field go
7334        // first, together in one request — a terminal option selected before the issue
7335        // closes, as a whole write does — then the `blockedBy` difference, then the body.
7336        let mut board_writes: Vec<(&BoardId, (String, Value))> = Vec::new();
7337        let mut clears: Vec<(&BoardId, &str)> = Vec::new();
7338        if let Some(moving) = status_move.as_ref().filter(|moving| moving.moves.option()) {
7339            board_writes.push((
7340                &moving.board,
7341                (
7342                    moving.field.clone(),
7343                    json!({"singleSelectOptionId": moving.option}),
7344                ),
7345            ));
7346        }
7347        match &priority_move {
7348            Some((board, PriorityWrite::Select { field, option }, _)) => board_writes.push((
7349                board,
7350                (field.clone(), json!({"singleSelectOptionId": option})),
7351            )),
7352            Some((board, PriorityWrite::Clear { field }, _)) => clears.push((board, field)),
7353            None => {}
7354        }
7355        if let Some((board, writes, _)) = &projection_move {
7356            for write in writes {
7357                let (mut values, mut cleared) = (Vec::new(), Vec::new());
7358                write.join(&mut values, &mut cleared);
7359                board_writes.extend(values.into_iter().map(|value| (board, value)));
7360                clears.extend(cleared.into_iter().map(|field| (board, field)));
7361            }
7362        }
7363        let mut boards: Vec<&BoardId> = board_writes.iter().map(|(board, _)| *board).collect();
7364        boards.extend(clears.iter().map(|(board, _)| *board));
7365        boards.dedup_by(|one, other| one.as_str() == other.as_str());
7366        for board in boards {
7367            let writes = board_writes
7368                .iter()
7369                .filter(|(on, _)| on.as_str() == board.as_str())
7370                .map(|(_, write)| write.clone())
7371                .collect::<Vec<_>>();
7372            let cleared = clears
7373                .iter()
7374                .filter(|(on, _)| on.as_str() == board.as_str())
7375                .map(|(_, field)| *field)
7376                .collect::<Vec<_>>();
7377            self.set_item_fields(board.as_str(), &item.item_id, &writes, &cleared)
7378                .await?;
7379        }
7380        let mut blocked_by_moved = false;
7381        if let Some((native, _)) = &edges
7382            && item.content_kind == ContentKind::Issue
7383        {
7384            blocked_by_moved = self
7385                .reconcile_blocked_by(
7386                    &item.id,
7387                    native,
7388                    Issue::Existing(item.blocked_by.as_deref()),
7389                )
7390                .await?;
7391        }
7392        if !fields.is_empty() {
7393            self.update_content(item.content_kind, &item.id, Value::Object(fields))
7394                .await?;
7395        }
7396
7397        // The `blockedBy` this item was read with is what the write just changed, so it is no
7398        // answer any more: a later write in this command would reconcile against it — re-adding
7399        // what it kept and never removing what it dropped — and a forward-edge read would
7400        // answer with it. Left unknown, each asks GitHub instead.
7401        if blocked_by_moved {
7402            item.blocked_by = None;
7403        }
7404        if let Some(title) = &update.title {
7405            item.title.clone_from(title);
7406        }
7407        item.body = visible.filter(|value| !value.is_empty());
7408        item.raw_body = (!body.is_empty() || item.raw_body.is_some()).then_some(body);
7409        item.slot = slot;
7410        if let Some(delivers) = &update.delivers {
7411            item.delivers.clone_from(delivers);
7412        }
7413        if let Some(moving) = status_move {
7414            item.closed = matches!(moving.target, StatusTarget::Terminal(_, _))
7415                && item.content_kind == ContentKind::Issue;
7416            item.status = moving.landed;
7417            item.option = Some(moving.name);
7418        }
7419        if let Some((_, _, priority)) = priority_move {
7420            item.priority = HeldPriority::Read(priority);
7421        }
7422        if let Some((_, _, projected)) = projection_move {
7423            item.projected = projected;
7424        }
7425        let task = item.task()?;
7426        let mut written = update.changed(&before, &task);
7427        if blocked_by_moved || recorded_moves {
7428            written.insert(UpdatedField::DependsOn);
7429        }
7430        self.remember_written(item, false)?;
7431        Ok(Some(TaskUpdateOutcome {
7432            task,
7433            written,
7434            delivers_before: before.delivers,
7435        }))
7436    }
7437
7438    /// Replace one issue's visible body and its [`MetadataKey::TEMPLATE_KEY`] slot entry
7439    /// together, and nothing else; see [`TaskSource::set_task_rendering`].
7440    ///
7441    /// One update of the body: the content outside the slot, and inside it that one entry,
7442    /// every other entry kept as it was. This source keeps no template answers — an issue has
7443    /// no room beside itself that is not its body, and answers written there would duplicate
7444    /// what the content already says and count against GitHub's body limit — so `answers`
7445    /// reaches nothing here. A body that would not change is not sent at all.
7446    async fn replace_rendering(
7447        &self,
7448        id: &NativeId,
7449        kind: BoardKind,
7450        content: &str,
7451        provenance: &Value,
7452        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
7453    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
7454        let Some(mut item) = self.bound_item(id).await?.filter(|item| item.kind == kind) else {
7455            return Ok(None);
7456        };
7457        let held = item.raw_body.clone().unwrap_or_default();
7458        let mut slot = item.slot.clone();
7459        slot.insert(MetadataKey::TEMPLATE_KEY.to_owned(), provenance.clone());
7460        let rewritten;
7461        let content = if let Some(assets) = assets {
7462            let uploads = self
7463                .upload_assets(item.own_repository.as_ref(), assets)
7464                .await?;
7465            rewritten =
7466                onetaskgraph_plugin_api::serve_asset_references(content, &mut slot, &uploads);
7467            rewritten.as_str()
7468        } else {
7469            content
7470        };
7471        let body = with_slot(&with_content(&held, content)?, &slot)?;
7472        // Checked before anything is sent, as a content write checks it.
7473        let (visible, read) = metadata_body(Some(body.clone()))?;
7474        if visible.as_deref().unwrap_or_default() != content || read != slot {
7475            return Err(SourceError::Refused {
7476                message: format!(
7477                    "this content ends in what source {} reads as its own metadata slot \
7478                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
7479                     as content; next: remove that trailing block from the template",
7480                    self.name
7481                ),
7482            });
7483        }
7484        if body != held {
7485            self.update_content(item.content_kind, &item.id, json!({"body": body}))
7486                .await?;
7487        }
7488        item.body = visible.filter(|value| !value.is_empty());
7489        item.raw_body = Some(body);
7490        item.slot = read;
7491        self.remember_written(item, false)?;
7492        Ok(Some(onetaskgraph_plugin_api::AssetsWritten {
7493            id: id.clone(),
7494            content: Some(content.to_owned()),
7495        }))
7496    }
7497
7498    async fn set_item_field(
7499        &self,
7500        board_id: &str,
7501        item_id: &str,
7502        field_id: &str,
7503        value: Value,
7504    ) -> Result<(), SourceError> {
7505        let data = self
7506            .graphql(
7507                graphql::UPDATE_FIELD,
7508                json!({"input":{
7509                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
7510                },"readPriority":false,"priorityName":PRIORITY_FIELD}),
7511            )
7512            .await?;
7513        let returned = data
7514            .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
7515            .ok_or_else(|| SourceError::Malformed {
7516                message: "GitHub field update returned no project item".into(),
7517            })?;
7518        if required_str(returned, "id")? != item_id {
7519            return Err(SourceError::Malformed {
7520                message: "GitHub field update returned the wrong project item".into(),
7521            });
7522        }
7523        Ok(())
7524    }
7525
7526    /// GitHub accepts one value per field mutation; aliases combine those mutations in
7527    /// one request — writes in the order given, then clears. A lone write is
7528    /// [`graphql::UPDATE_FIELD`] and a lone clear [`graphql::CLEAR_FIELD`]; more than one
7529    /// request's slots hold go in further requests of the same document, in order. Every
7530    /// returned item id is checked, including optional aliases.
7531    async fn set_item_fields(
7532        &self,
7533        board: &str,
7534        item: &str,
7535        fields: &[(String, Value)],
7536        clears: &[&str],
7537    ) -> Result<(), SourceError> {
7538        match (fields, clears) {
7539            ([], []) => return Ok(()),
7540            ([(field, value)], []) => {
7541                return self.set_item_field(board, item, field, value.clone()).await;
7542            }
7543            ([], [field]) => {
7544                return self
7545                    .write_priority(
7546                        board,
7547                        item,
7548                        &PriorityWrite::Clear {
7549                            field: (*field).to_owned(),
7550                        },
7551                    )
7552                    .await;
7553            }
7554            _ => {}
7555        }
7556        // Any field of the request stands in for an input a slot leaves out, which GitHub
7557        // still requires to be well formed although it never runs it.
7558        let filler = fields
7559            .first()
7560            .map_or_else(|| clears[0], |(field, _)| field.as_str());
7561        let mut writes = fields.iter();
7562        let mut cleared = clears.iter();
7563        loop {
7564            let batch: Vec<&(String, Value)> =
7565                writes.by_ref().take(FIELD_WRITE_SLOTS.len()).collect();
7566            let batch_clears: Vec<&&str> = cleared.by_ref().take(FIELD_CLEAR_SLOTS.len()).collect();
7567            if batch.is_empty() && batch_clears.is_empty() {
7568                return Ok(());
7569            }
7570            let mut variables = serde_json::Map::new();
7571            for (index, slot) in FIELD_WRITE_SLOTS.iter().enumerate() {
7572                let input = match batch.get(index) {
7573                    Some((field, value)) => {
7574                        json!({"projectId":board,"itemId":item,"fieldId":field,"value":value})
7575                    }
7576                    None => json!({"projectId":board,"itemId":item,"fieldId":filler,
7577                                   "value":{"text":""}}),
7578                };
7579                variables.insert(slot.variable.to_owned(), input);
7580                variables.insert(slot.include.to_owned(), json!(index < batch.len()));
7581            }
7582            for (index, slot) in FIELD_CLEAR_SLOTS.iter().enumerate() {
7583                let field = batch_clears.get(index).map_or(filler, |field| **field);
7584                variables.insert(
7585                    slot.variable.to_owned(),
7586                    json!({"projectId":board,"itemId":item,"fieldId":field}),
7587                );
7588                variables.insert(slot.include.to_owned(), json!(index < batch_clears.len()));
7589            }
7590            let data = self
7591                .graphql(graphql::UPDATE_FIELDS, Value::Object(variables))
7592                .await?;
7593            let sent = FIELD_WRITE_SLOTS[..batch.len()]
7594                .iter()
7595                .chain(&FIELD_CLEAR_SLOTS[..batch_clears.len()]);
7596            for slot in sent {
7597                let alias = slot.alias;
7598                let returned = data
7599                    .get(alias)
7600                    .and_then(|value| value.get("projectV2Item"))
7601                    .ok_or_else(|| SourceError::Malformed {
7602                        message: format!("GitHub field update {alias} returned no project item"),
7603                    })?;
7604                if required_str(returned, "id")? != item {
7605                    return Err(SourceError::Malformed {
7606                        message: format!(
7607                            "GitHub field update {alias} returned the wrong project item"
7608                        ),
7609                    });
7610                }
7611            }
7612        }
7613    }
7614
7615    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
7616        let mut after: Option<String> = None;
7617        let mut ids = Vec::new();
7618        loop {
7619            let data = self
7620                .graphql(
7621                    graphql::ISSUE_DEPENDENCIES,
7622                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
7623                )
7624                .await?;
7625            let connection =
7626                data.pointer("/node/blockedBy")
7627                    .ok_or_else(|| SourceError::Malformed {
7628                        message: "GitHub dependency response has no blockedBy connection".into(),
7629                    })?;
7630            ids.extend(
7631                connection
7632                    .get("nodes")
7633                    .and_then(Value::as_array)
7634                    .ok_or_else(|| SourceError::Malformed {
7635                        message: "GitHub dependency response nodes is not an array".into(),
7636                    })?
7637                    .iter()
7638                    .map(|value| required_str(value, "id").map(str::to_owned))
7639                    .collect::<Result<Vec<_>, _>>()?,
7640            );
7641            let next = next_cursor(connection)?;
7642            if let Some(next) = &next {
7643                validate_cursor_progress(after.as_deref(), &next.0)?;
7644            }
7645            after = next.map(|cursor| cursor.0);
7646            if after.is_none() {
7647                return Ok(ids);
7648            }
7649        }
7650    }
7651
7652    async fn dependencies(
7653        &self,
7654        id: &NativeId,
7655        near_kind: ItemKind,
7656        direction: Direction,
7657        page: &PageRequest,
7658    ) -> Result<Page<DependencyEdge>, SourceError> {
7659        validate_page(page)?;
7660        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
7661        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
7662        let recorded = recorded_offset(cursor, direction)?;
7663        // What this issue is blocked by, when a read of it by its own id in this command
7664        // already carried the whole connection — a copy reads the item it writes before it
7665        // reads its edges — and the page asked for is the whole of it, or the recorded tail
7666        // after it. Answered from that read, in the shape the dependency read answers in;
7667        // anything else is asked of GitHub.
7668        let carried = match direction {
7669            Direction::DependsOn => self
7670                .resolved_cache()?
7671                .get(id)
7672                .filter(|item| item.content_kind == ContentKind::Issue)
7673                .and_then(|item| Some((item.blocked_by.clone()?, item.raw_body.clone()))),
7674            Direction::DependedOnBy => None,
7675        }
7676        .filter(|(nodes, _)| recorded.is_some() || (cursor.is_none() && nodes.len() <= limit));
7677        // Asked for even in the recorded phase, whose page reads nothing from the
7678        // connection: `__typename` is what says whether this item has a native
7679        // relationship at all, and that is what decides which far ends the reserved key is
7680        // allowed to hold.
7681        let data = match carried {
7682            Some((nodes, body)) => json!({"node":{"__typename":"Issue","body":body,
7683                "blockedBy":{"nodes":nodes,"pageInfo":{"hasNextPage":false,"endCursor":null}}}}),
7684            None => {
7685                self.graphql(
7686                    graphql::ISSUE_DEPENDENCIES,
7687                    json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
7688                           "after":if recorded.is_some() {None} else {cursor}}),
7689                )
7690                .await?
7691            }
7692        };
7693        let node =
7694            data.get("node")
7695                .filter(|v| !v.is_null())
7696                .ok_or_else(|| SourceError::Refused {
7697                    message: format!(
7698                        "GitHub item {} was not found or does not support dependencies",
7699                        id.0
7700                    ),
7701                })?;
7702        let connection_name = match direction {
7703            Direction::DependsOn => "blockedBy",
7704            Direction::DependedOnBy => "blocking",
7705        };
7706        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
7707        // named natively and the reserved key may hold any far end. An issue's connections
7708        // hold issues, and this source reads them at the near item's own level.
7709        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
7710        if let Some(offset) = recorded {
7711            return Ok(recorded_page(
7712                self.recorded_edges(id, near_kind, direction, natively_names, node)
7713                    .await?,
7714                offset,
7715                limit,
7716            ));
7717        }
7718        if natively_names.is_none() {
7719            return Ok(recorded_page(
7720                self.recorded_edges(id, near_kind, direction, natively_names, node)
7721                    .await?,
7722                0,
7723                limit,
7724            ));
7725        }
7726        let connection = node
7727            .get(connection_name)
7728            .ok_or_else(|| SourceError::Malformed {
7729                message: "GitHub dependency response is missing its connection".into(),
7730            })?;
7731        let nodes = connection
7732            .get("nodes")
7733            .and_then(Value::as_array)
7734            .ok_or_else(|| SourceError::Malformed {
7735                message: "GitHub dependency response nodes is not an array".into(),
7736            })?;
7737        // `from` depends on `to`, always. GitHub spells the same relationship from either
7738        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
7739        // it — so the near item is `from` in one direction and `to` in the other.
7740        let items = nodes
7741            .iter()
7742            .map(|value| {
7743                let related = NativeId(required_str(value, "id")?.into());
7744                let related_kind = related_kind(value)?;
7745                let (from, to) = match direction {
7746                    Direction::DependsOn => (
7747                        DependencyEndpoint::from_native(id.clone(), near_kind),
7748                        DependencyEndpoint::from_native(related, related_kind),
7749                    ),
7750                    Direction::DependedOnBy => (
7751                        DependencyEndpoint::from_native(related, related_kind),
7752                        DependencyEndpoint::from_native(id.clone(), near_kind),
7753                    ),
7754                };
7755                Ok(DependencyEdge {
7756                    from,
7757                    to,
7758                    kind: DependencyKind::Blocks,
7759                })
7760            })
7761            .collect::<Result<Vec<_>, SourceError>>()?;
7762        let mut next = next_cursor(connection)?;
7763        if let Some(next) = &next {
7764            validate_cursor_progress(cursor, &next.0)?;
7765        }
7766        if next.is_none()
7767            && !self
7768                .recorded_edges(id, near_kind, direction, natively_names, node)
7769                .await?
7770                .is_empty()
7771        {
7772            next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
7773        }
7774        Ok(Page { items, next })
7775    }
7776
7777    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
7778    /// a far end in another source has to live: no GitHub issue relationship can name one.
7779    ///
7780    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
7781    /// source never writes one down.
7782    ///
7783    /// The metadata lives in the item's own body slot, and `node` is the dependency read's
7784    /// own answer, which carries an issue's body — so an issue's recorded edges cost no
7785    /// request beyond the read already made, and reading the board for them would be a
7786    /// walk of every item for one field of one. A draft has no body in that answer, because
7787    /// a draft is not an issue, so a draft's are read off its own read by id — never off a
7788    /// listing of the board, which can be behind on the very item asked about.
7789    async fn recorded_edges(
7790        &self,
7791        id: &NativeId,
7792        near_kind: ItemKind,
7793        direction: Direction,
7794        natively_names: Option<ItemKind>,
7795        node: &Value,
7796    ) -> Result<Vec<DependencyEdge>, SourceError> {
7797        if direction != Direction::DependsOn {
7798            return Ok(Vec::new());
7799        }
7800        let slot = match node.get("body") {
7801            Some(body) if natively_names.is_some() => {
7802                metadata_body(body.as_str().map(str::to_owned))?.1
7803            }
7804            _ => {
7805                let Some(item) = self.bound_item(id).await? else {
7806                    return Ok(Vec::new());
7807                };
7808                item.slot
7809            }
7810        };
7811        DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
7812            .map_err(|message| SourceError::Malformed { message })
7813    }
7814
7815    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
7816        self.repository
7817            .as_ref()
7818            .ok_or_else(|| SourceError::Refused {
7819                message: format!(
7820                    "source {} has no repository configured, and a GitHub Projects board has no \
7821                 repository of its own to create an issue in; set repository: owner/name on \
7822                 this source",
7823                    self.name
7824                ),
7825            })
7826    }
7827
7828    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
7829    /// states.
7830    ///
7831    /// The fallback is demanded first, whichever arm answers: a write without a configured
7832    /// repository is refused naming the field exactly as it was before the rule existed,
7833    /// so a source that could not write before cannot write now, rather than writing for
7834    /// the one item whose own field happens to decide it.
7835    ///
7836    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
7837    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
7838    /// entry owned by someone other than the owner of the parent issue's repository —
7839    /// GitHub accepts a sub-issue from another repository of the same owner and from no
7840    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
7841    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
7842    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
7843    /// and is visible to the token is checked where its node id is resolved, still before
7844    /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
7845    /// looked up in a listing of the board, which can be minutes behind an issue its own
7846    /// `projectItems` already places on it — and that read answers first from this process's
7847    /// own record, so a project created moments ago in this command answers though GitHub
7848    /// has not caught up.
7849    async fn creation_target(
7850        &self,
7851        incoming: &Incoming<'_>,
7852    ) -> Result<RepositoryTarget, SourceError> {
7853        let fallback = self.configured_repository()?;
7854        let what = |incoming: &Incoming<'_>| {
7855            format!(
7856                "{} {:?}",
7857                incoming.written.kind().describes(),
7858                incoming.title
7859            )
7860        };
7861        let parent = match incoming.parent {
7862            Some(parent) => Some(self.bound_item(parent).await?.ok_or_else(|| {
7863                SourceError::Refused {
7864                    message: format!(
7865                        "GitHub project issue {} was not found on the board of source {}, so {} \
7866                         cannot be filed under it",
7867                        parent.0,
7868                        self.name,
7869                        what(incoming)
7870                    ),
7871                }
7872            })?),
7873            None => None,
7874        };
7875        let parents_repository = parent
7876            .as_ref()
7877            .map(|parent| {
7878                // A draft is on the board and so is found, but it has no repository to
7879                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
7880                // would refuse the task only once `createIssue` had made it.
7881                if parent.content_kind == ContentKind::DraftIssue {
7882                    return Err(SourceError::Refused {
7883                        message: format!(
7884                            "GitHub project item {} on the board of source {} is a draft, \
7885                             which cannot have sub-issues, so {} cannot be filed under it",
7886                            parent.id.0,
7887                            self.name,
7888                            what(incoming)
7889                        ),
7890                    });
7891                }
7892                // An issue's repository is where a sub-issue is placed and whose owner it
7893                // is compared against, so a parent whose repository this source cannot
7894                // spell as `owner/name` — GitHub's login grammar is wider than this
7895                // source's floor — is one nothing can be filed under.
7896                parent
7897                    .own_repository
7898                    .as_ref()
7899                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
7900                    .ok_or_else(|| SourceError::Malformed {
7901                        message: format!(
7902                            "GitHub project issue {} on the board of source {} is in {}, which \
7903                             is not a {}/owner/name repository this source can place {} in",
7904                            parent.id.0,
7905                            self.name,
7906                            parent
7907                                .own_repository
7908                                .as_ref()
7909                                .map_or("no repository", Repository::as_str),
7910                            RepositoryTarget::HOST,
7911                            what(incoming)
7912                        ),
7913                    })
7914            })
7915            .transpose()?;
7916        match incoming.repositories {
7917            [named] => {
7918                let target =
7919                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
7920                        message: format!(
7921                            "{} names repository {}, which is not a {}/owner/name repository \
7922                             source {} can create an issue in; name one that is, or name none",
7923                            what(incoming),
7924                            named.as_str(),
7925                            RepositoryTarget::HOST,
7926                            self.name
7927                        ),
7928                    })?;
7929                if let Some(parents) = &parents_repository
7930                    && parents.owner != target.owner
7931                {
7932                    return Err(SourceError::Refused {
7933                        message: format!(
7934                            "{} names repository {}, owned by {}, but its project's issue is in \
7935                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
7936                             of the same owner as its parent issue; name a repository of {}, or \
7937                             name none",
7938                            what(incoming),
7939                            target.slug(),
7940                            target.owner,
7941                            parents.slug(),
7942                            parents.owner,
7943                            parents.owner
7944                        ),
7945                    });
7946                }
7947                Ok(target)
7948            }
7949            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
7950        }
7951    }
7952
7953    /// The node id of the repository `incoming` is being created in, or the refusal naming
7954    /// the item and the repository the token cannot see.
7955    ///
7956    /// Resolved once per command per repository; see [`Self::repository_cache`].
7957    async fn repository_id(
7958        &self,
7959        repository: &RepositoryTarget,
7960        incoming: &Incoming<'_>,
7961    ) -> Result<String, SourceError> {
7962        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
7963            return Ok(id);
7964        }
7965        let data = self
7966            .graphql(
7967                graphql::REPOSITORY,
7968                json!({"owner":repository.owner,"name":repository.name}),
7969            )
7970            .await?;
7971        self.repository_read(&data, repository, incoming)
7972    }
7973
7974    /// The repository's node id out of an answer carrying the `repository` root, held for
7975    /// the rest of this command, or the refusal naming the item that cannot be created in it.
7976    fn repository_read(
7977        &self,
7978        data: &Value,
7979        repository: &RepositoryTarget,
7980        incoming: &Incoming<'_>,
7981    ) -> Result<String, SourceError> {
7982        let node = data
7983            .get("repository")
7984            .filter(|value| !value.is_null())
7985            .ok_or_else(|| SourceError::Refused {
7986                message: format!(
7987                    "GitHub repository {} was not found or is not visible to the token, so {} \
7988                     {:?} cannot be created in it",
7989                    repository.slug(),
7990                    incoming.written.kind().describes(),
7991                    incoming.title
7992                ),
7993            })?;
7994        let id = required_str(node, "id")?.to_owned();
7995        self.repository_cache()?
7996            .insert(repository.clone(), id.clone());
7997        Ok(id)
7998    }
7999
8000    fn repository_cache(
8001        &self,
8002    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
8003        self.repository_cache
8004            .lock()
8005            .map_err(|_| SourceError::Unavailable {
8006                message: "this source's record of the destination repository was left \
8007                          inconsistent by an earlier failure; next: run the command again"
8008                    .into(),
8009            })
8010    }
8011
8012    /// Create or update one board item, whichever kind it is.
8013    async fn write_item(
8014        &self,
8015        incoming: &Incoming<'_>,
8016        target: Option<&NativeId>,
8017        depends_on: &[DependencyEdge],
8018    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
8019        // Refused before anything is read or written: a task or a project titled the way
8020        // this board spells a document would land as an issue this same source reads back
8021        // as a document, so the field this destination cannot carry is named rather than
8022        // written and silently reclassified.
8023        if let Written::Work(kind, _) = incoming.written
8024            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
8025        {
8026            return Err(SourceError::Refused {
8027                message: format!(
8028                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
8029                     spells a document, so it would read back as one rather than as a {}; \
8030                     retitle it, or copy it as a document",
8031                    kind.marker(),
8032                    self.name,
8033                    kind.marker()
8034                ),
8035            });
8036        }
8037        // The destination is read by its own id, and whether this board holds it is decided
8038        // by that read — its own `projectItems` — rather than by whether a listing of the
8039        // board happens to include it yet. See the module documentation.
8040        let existing = match target {
8041            Some(target) => {
8042                Some(
8043                    self.bound_item(target)
8044                        .await?
8045                        .ok_or_else(|| SourceError::Refused {
8046                            message: format!("GitHub destination item {} was not found", target.0),
8047                        })?,
8048                )
8049            }
8050            None => None,
8051        };
8052        let existing = existing.as_ref();
8053        // An existing issue is never moved; a new one is created where the rule says — and
8054        // knowing where is what lets the board's fields and that repository's id be read
8055        // together, before anything below needs either.
8056        let creation_target = match existing {
8057            Some(_) => None,
8058            None => {
8059                let target = self.creation_target(incoming).await?;
8060                self.creation_context(&target, incoming).await?;
8061                Some(target)
8062            }
8063        };
8064        let board = self
8065            .fields_for(
8066                existing,
8067                incoming.written.status().is_some(),
8068                incoming
8069                    .priority
8070                    .is_some_and(|priority| priority != Priority::None),
8071            )
8072            .await?;
8073        let status_target = incoming
8074            .written
8075            .work_status()
8076            .map(|(kind, status)| self.resolved_target(kind, status.category))
8077            .transpose()?;
8078        let column = match (incoming.written.work_status(), status_target.as_ref()) {
8079            (Some((kind, status)), Some(target)) => {
8080                self.column_for(&board.fields, kind, status.category, target)?
8081            }
8082            _ => None,
8083        };
8084        // Resolved before anything is created, for the reason the column above is: a
8085        // priority this board has no option for is refused while nothing has been written.
8086        let priority_write = match incoming.priority {
8087            Some(priority) => self.priority_write(&board.fields, existing, priority)?,
8088            None => None,
8089        };
8090        // And for the reason the priority is: a value no projected field can hold, or a board
8091        // that cannot hold the field, is refused while nothing has been written.
8092        let (projections, projected) = self.projection_writes(
8093            &board.fields,
8094            existing.map(|item| &item.projected),
8095            incoming.metadata,
8096            None,
8097        )?;
8098        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
8099        if content_kind == ContentKind::DraftIssue {
8100            if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
8101                (status_target.as_ref(), incoming.written.status())
8102            {
8103                return Err(self.closes_a_draft(status.category));
8104            }
8105            if incoming.parent.is_some() {
8106                return Err(SourceError::Refused {
8107                    message: "GitHub draft items cannot be a project's sub-issue".into(),
8108                });
8109            }
8110        }
8111        match existing {
8112            Some(item) if content_kind == ContentKind::Issue => {
8113                if item.labels != incoming.labels {
8114                    return Err(SourceError::Refused {
8115                        message: "GitHub issue labels differ from the labels being written".into(),
8116                    });
8117                }
8118            }
8119            _ => {
8120                if !incoming.labels.is_empty() {
8121                    return Err(SourceError::Refused {
8122                        message: "GitHub items created by this destination carry no labels".into(),
8123                    });
8124                }
8125            }
8126        }
8127
8128        // The repository the issue really lives in is what the slot below is written against,
8129        // so a single entry that is where the issue is created travels as no key at all, and
8130        // the read side derives it back from the issue.
8131        let own_repository = match (existing, &creation_target) {
8132            (Some(item), _) => item.own_repository.clone(),
8133            (None, Some(target)) => Some(
8134                Repository::try_from(target.origin())
8135                    .map_err(|message| SourceError::Config { message })?,
8136            ),
8137            (None, None) => None,
8138        };
8139        let (native, fallback) = self
8140            .partition_edges(
8141                incoming.written.kind(),
8142                content_kind,
8143                existing.and_then(|item| item.blocked_by.as_deref()),
8144                depends_on,
8145            )
8146            .await?;
8147        let mut slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
8148        let content = match incoming.assets {
8149            Some(assets) => {
8150                let uploads = self.upload_assets(own_repository.as_ref(), assets).await?;
8151                let rewritten = onetaskgraph_plugin_api::serve_asset_references(
8152                    incoming.content.unwrap_or_default(),
8153                    &mut slot,
8154                    &uploads,
8155                );
8156                incoming.content.map(|_| rewritten)
8157            }
8158            None => incoming.content.map(str::to_owned),
8159        };
8160        let body = compose_body(content.as_deref(), &slot)?;
8161        // Read before anything is created, for the reason the field below is: a value
8162        // this destination cannot store has to refuse, and refusing after `createIssue`
8163        // would leave an issue behind that nothing asked for. The engine writes a
8164        // qualified id here; a caller handing this key anything else is told so rather
8165        // than having it silently stored as no origin at all.
8166        // 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.
8167        let origin = match incoming.metadata.get(ORIGIN_KEY) {
8168            None => "",
8169            Some(Value::String(origin)) => origin.as_str(),
8170            Some(other) => {
8171                return Err(SourceError::Refused {
8172                    message: format!(
8173                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
8174                         is {other}"
8175                    ),
8176                });
8177            }
8178        };
8179        // Resolved before anything is created: a board that cannot carry the copy origin
8180        // has to refuse the write, and refusing it after `createIssue` would leave an
8181        // issue behind that nothing asked for.
8182        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
8183            Some(field) => {
8184                if required_str(field, "__typename")? != "ProjectV2Field" {
8185                    return Err(SourceError::Refused {
8186                        message: format!(
8187                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
8188                        ),
8189                    });
8190                }
8191                Some(required_str(field, "id")?.to_owned())
8192            }
8193            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
8194                return Err(SourceError::Refused {
8195                    message: format!(
8196                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
8197                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
8198                         the board"
8199                    ),
8200                });
8201            }
8202            None => None,
8203        };
8204
8205        let Landed {
8206            content_id,
8207            item_id,
8208            url,
8209            number,
8210        } = match existing {
8211            // Its content is written last, below, once everything else has landed.
8212            Some(item) => Landed {
8213                content_id: item.id.clone(),
8214                item_id: item.item_id.clone(),
8215                url: item.url.clone(),
8216                number: item.number,
8217            },
8218            None => {
8219                let target = creation_target
8220                    .as_ref()
8221                    .ok_or_else(|| SourceError::Malformed {
8222                        message: "a new item was decided without a repository to create it in"
8223                            .into(),
8224                    })?;
8225                self.create_and_file_issue(board.id.as_str(), target, incoming, &body)
8226                    .await?
8227            }
8228        };
8229
8230        // A write the store's cache expects sends only what differs from the version it expects:
8231        // a priority the item already holds there is not sent, so a person's edit of it since is
8232        // left alone. Every other write sends it, as its pinned cost says.
8233        let priority_write = priority_write.filter(|_| {
8234            !existing.is_some_and(|item| {
8235                self.expected.is_primed(&item.id)
8236                    && incoming.priority.map(HeldPriority::Read) == Some(item.priority.clone())
8237            })
8238        });
8239        let written_option = column.as_ref().map(|(_, _, name)| name.clone());
8240        let column = column
8241            .filter(|(_, _, name)| existing.is_none_or(|item| item.option.as_ref() != Some(name)))
8242            .map(|(field, option, _)| (field, option));
8243        // Creating an item here is several calls — `createIssue`, which files it on the
8244        // board, then its board fields, the parent and the dependencies — and GitHub can fail
8245        // at any of them. Everything this source can refuse *before* the first of those is
8246        // already checked above, so what is left is GitHub itself failing part way. When it
8247        // does over an item this call created, the issue is taken back: a write that
8248        // refused must not leave an item behind that nobody asked for, and one that does
8249        // makes the retry create a second.
8250        // Whether the board-field write carrying a moved origin was answered as landing whole.
8251        // When it was refused, GitHub does not say which of its fields ran before the one that
8252        // failed, so the origin may or may not have moved.
8253        let mut origin_landed = false;
8254        let landed = self
8255            .finish_write(
8256                board.id.as_str(),
8257                incoming,
8258                &content_id,
8259                &item_id,
8260                content_kind,
8261                existing,
8262                origin_field.as_deref(),
8263                origin,
8264                column,
8265                status_target.as_ref(),
8266                priority_write.as_ref(),
8267                &projections,
8268                &native,
8269                &mut origin_landed,
8270            )
8271            .await;
8272        // An existing item's title, body and state go last, in one `updateIssue`, once its board
8273        // fields and its relationships have landed: a refusal of any of those then leaves its
8274        // body — and the metadata slot inside it — exactly as it stood.
8275        let landed = match (landed, existing) {
8276            (Ok(()), Some(item)) => {
8277                self.update_existing(item, incoming, &body, status_target.as_ref())
8278                    .await
8279            }
8280            (landed, _) => landed,
8281        };
8282        if let Err(error) = landed {
8283            match existing {
8284                // Best effort, and the write's own failure is what the caller is told: a
8285                // refusal naming the tidy-up would hide why the write failed at all.
8286                None => {
8287                    let _ = self.delete_issue(&content_id).await;
8288                }
8289                // The origin field is the one piece of an existing item's metadata written
8290                // before its body, so a write refused after it puts it back as it was. When
8291                // that is refused too, the write's own failure is still what the caller is
8292                // told — with what it left behind added, because the item's metadata is then
8293                // not as it stood and a caller retrying has to know which key moved.
8294                Some(item) => {
8295                    let before = item.origin.as_deref().unwrap_or("");
8296                    if let Some(field) = origin_field.as_deref()
8297                        && before != origin
8298                        && let Err(restore) = self
8299                            .set_item_field(
8300                                board.id.as_str(),
8301                                &item.item_id,
8302                                field,
8303                                json!({"text": before}),
8304                            )
8305                            .await
8306                    {
8307                        let left = if origin_landed {
8308                            format!(
8309                                "its {ORIGIN_KEY} was moved to {origin:?} before that and could \
8310                                 not be put back to {before:?} ({restore}), so item {} still \
8311                                 holds {origin:?} there",
8312                                item.id.0
8313                            )
8314                        } else {
8315                            format!(
8316                                "the refused write carried its {ORIGIN_KEY} from {before:?} to \
8317                                 {origin:?}, GitHub does not say whether that part of it ran, \
8318                                 and putting it back to {before:?} was refused ({restore}), so \
8319                                 item {} holds {origin:?} or {before:?} there",
8320                                item.id.0
8321                            )
8322                        };
8323                        return Err(noting(
8324                            error,
8325                            &format!(
8326                                "; {left}; next: set {ORIGIN_KEY} on it back to {before:?}, or \
8327                                 run the write again"
8328                            ),
8329                        ));
8330                    }
8331                }
8332            }
8333            return Err(error);
8334        }
8335
8336        let written_status = match (incoming.written.work_status(), status_target.as_ref()) {
8337            (Some((kind, _)), Some(StatusTarget::Terminal(_, reason))) => {
8338                self.statuses
8339                    .status(kind, written_option.as_deref(), true, Some(reason.reason()))
8340            }
8341            (Some((kind, _)), Some(StatusTarget::Column(_))) => {
8342                self.statuses
8343                    .status(kind, written_option.as_deref(), false, None)
8344            }
8345            (Some((_, status)), _) => status.clone(),
8346            (None, _) => Status {
8347                category: StatusCategory::Unknown,
8348                name: "Open".to_owned(),
8349            },
8350        };
8351
8352        // So the rest of this command reads what it just did rather than what the board
8353        // said before it. See `remember_written` for which half takes it.
8354        let remembered = Resolved {
8355            item_id,
8356            id: content_id.clone(),
8357            content_kind,
8358            kind: incoming.written.kind(),
8359            title: incoming.title.to_owned(),
8360            // The visible half of the body this write composed, split back off it the
8361            // way a read splits it — so what this record reports is what a read of the
8362            // same issue reports, rather than the person's text with the metadata slot
8363            // still on the end of it.
8364            body: metadata_body(body.clone())?.0,
8365            raw_body: body.clone(),
8366            // A document has no status of its own; what it reads back as is whatever
8367            // the issue's own state says, which is what a re-read reports.
8368            status: written_status,
8369            option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
8370            priority: match incoming.priority {
8371                Some(priority) => HeldPriority::Read(priority),
8372                None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
8373                    item.priority.clone()
8374                }),
8375            },
8376            // What `state_input` asked for: closed for a terminal target, open for any other
8377            // status, and the issue's own state left as it was by a document write.
8378            closed: content_kind == ContentKind::Issue
8379                && match status_target.as_ref() {
8380                    Some(StatusTarget::Terminal(_, _)) => true,
8381                    Some(_) => false,
8382                    None => existing.is_some_and(|item| item.closed),
8383                },
8384            delivers: incoming.delivers.to_vec(),
8385            delivered_by: incoming.delivered_by.to_vec(),
8386            labels: incoming.labels.to_vec(),
8387            parent: incoming.parent.cloned(),
8388            origin: (!origin.is_empty()).then(|| origin.to_owned()),
8389            projected,
8390            number,
8391            // In the update path this is the item's own url, read off `existing` where the
8392            // record above was bound, so one expression serves both halves.
8393            url,
8394            created_at: existing.and_then(|item| item.created_at),
8395            updated_at: existing.and_then(|item| item.updated_at),
8396            // What this write moved is GitHub's to say, so a record of it gives no token until
8397            // it is read again.
8398            item_updated_at: None,
8399            comment_count: None,
8400            relationship_events: None,
8401            served_at: None,
8402            facets: None,
8403            own_repository,
8404            repositories: incoming.repositories.to_vec(),
8405            classification: incoming.classification,
8406            slot,
8407            board_id: Some(board.id.as_str().to_owned()),
8408            fields: board
8409                .fields
8410                .get("nodes")
8411                .and_then(Value::as_array)
8412                .cloned()
8413                .unwrap_or_default(),
8414            board_fields: Some(board.fields.clone()),
8415            // What this write left the relationship holding is known by id alone, and a
8416            // later read of its edges needs each far end's kind, so it reads them again.
8417            blocked_by: None,
8418        };
8419        self.remember_written(remembered, existing.is_none())?;
8420        Ok(onetaskgraph_plugin_api::AssetsWritten {
8421            id: content_id,
8422            content,
8423        })
8424    }
8425
8426    /// Everything a write does after the item exists: its board fields, its parent, and
8427    /// its dependencies.
8428    ///
8429    /// Split out of `write_item` so there is one place a failure past the point of no
8430    /// return is caught, rather than a tidy-up repeated at each `?` above.
8431    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
8432    // so there is one place a failure past the point of no return is caught, and its
8433    // arguments are exactly the values that tail already had in scope. Bundling them into a
8434    // struct would describe no concept — it would be "the arguments of this function" — and
8435    // would put the whole of `write_item`'s locals behind one more indirection.
8436    #[allow(clippy::too_many_arguments)]
8437    async fn finish_write(
8438        &self,
8439        board_id: &str,
8440        incoming: &Incoming<'_>,
8441        content_id: &NativeId,
8442        item_id: &str,
8443        content_kind: ContentKind,
8444        existing: Option<&Resolved>,
8445        origin_field: Option<&str>,
8446        origin: &str,
8447        column: Option<(String, String)>,
8448        status_target: Option<&StatusTarget>,
8449        priority: Option<&PriorityWrite>,
8450        projections: &[ProjectionWrite],
8451        native: &[String],
8452        origin_landed: &mut bool,
8453    ) -> Result<(), SourceError> {
8454        let mut fields = Vec::new();
8455        if let Some(field_id) = origin_field
8456            && existing.map_or(!origin.is_empty(), |item| {
8457                item.origin.as_deref().unwrap_or("") != origin
8458            })
8459        {
8460            fields.push((field_id.to_owned(), json!({"text":origin})));
8461        }
8462        if let Some((field_id, option_id)) = column {
8463            fields.push((field_id, json!({"singleSelectOptionId":option_id})));
8464        }
8465        let mut clears = Vec::new();
8466        match priority {
8467            Some(PriorityWrite::Select { field, option }) => {
8468                fields.push((field.clone(), json!({"singleSelectOptionId":option})));
8469            }
8470            Some(PriorityWrite::Clear { field }) => clears.push(field.as_str()),
8471            None => {}
8472        }
8473        for projection in projections {
8474            projection.join(&mut fields, &mut clears);
8475        }
8476        self.set_item_fields(board_id, item_id, &fields, &clears)
8477            .await?;
8478        *origin_landed = true;
8479
8480        // An existing issue closes in the `updateIssue` its write ends with; one created just
8481        // now closes here, once its option is selected.
8482        if existing.is_none()
8483            && content_kind == ContentKind::Issue
8484            && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
8485        {
8486            self.update_content(
8487                ContentKind::Issue,
8488                content_id,
8489                json!({"stateInput":state_input(status_target)}),
8490            )
8491            .await?;
8492        }
8493
8494        if content_kind == ContentKind::Issue {
8495            self.reparent(
8496                existing.and_then(|item| item.parent.clone()),
8497                content_id,
8498                incoming.parent,
8499            )
8500            .await?;
8501            // A document takes part in no dependency graph, so writing one neither reads
8502            // nor changes the issue's own `blockedBy` relationships. Reconciling them
8503            // against the empty list a document write carries would *delete* whatever
8504            // relationships a person had made on that issue, which is a write nobody
8505            // asked for.
8506            if incoming.written.kind() != BoardKind::Document {
8507                let issue = match existing {
8508                    Some(item) => Issue::Existing(item.blocked_by.as_deref()),
8509                    None => Issue::Created,
8510                };
8511                self.reconcile_blocked_by(content_id, native, issue).await?;
8512            }
8513        }
8514        Ok(())
8515    }
8516
8517    /// Delete one issue, which takes its board item with it.
8518    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
8519        let data = self
8520            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
8521            .await?;
8522        data.pointer("/deleteIssue/repository")
8523            .filter(|value| !value.is_null())
8524            .ok_or_else(|| SourceError::Malformed {
8525                message: "GitHub issue deletion returned no repository".into(),
8526            })?;
8527        self.forget(id)?;
8528        Ok(())
8529    }
8530
8531    /// Remove one item this copy created, so a copy that could not finish leaves the board
8532    /// as it found it.
8533    ///
8534    /// Deleting the issue takes its board item with it, so there is no second mutation to
8535    /// keep in step. An id the board does not hold is not an error: the item is already
8536    /// gone, which is the state this asks for. Which that is, is decided by reading the item
8537    /// by its own id — a listing of the board can still be missing an item it holds, and
8538    /// reading that as *already gone* would leave behind the very item this was asked to
8539    /// take back.
8540    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
8541        let Some(item) = self.bound_item(id).await? else {
8542            return Ok(());
8543        };
8544        if item.content_kind == ContentKind::DraftIssue {
8545            return Err(SourceError::Refused {
8546                message: format!(
8547                    "GitHub item {} is a draft, and this source removes an item by deleting \
8548                     its issue; next: remove it from the board by hand",
8549                    id.0
8550                ),
8551            });
8552        }
8553        let data = self
8554            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
8555            .await?;
8556        data.pointer("/deleteIssue/repository")
8557            .filter(|value| !value.is_null())
8558            .ok_or_else(|| SourceError::Malformed {
8559                message: "GitHub issue deletion returned no repository".into(),
8560            })?;
8561        self.forget(id)?;
8562        Ok(())
8563    }
8564
8565    /// The issue a comment call on `task` is about, or `None` when this board holds no such
8566    /// task.
8567    ///
8568    /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
8569    /// read of the task cannot disagree about which ids name one: a project or a document of
8570    /// this board is not a task here either.
8571    ///
8572    /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
8573    /// issues and a draft is not one. It is refused rather than answered with an empty page,
8574    /// which would read as a task nobody has commented on yet.
8575    async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
8576        let cached = self.resolved_cache()?.get(task).cloned();
8577        let Some(item) = (match cached {
8578            Some(item) => Some(item),
8579            None => self.item_by_id(task).await?,
8580        })
8581        .filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
8582            return Ok(None);
8583        };
8584        if item.content_kind == ContentKind::DraftIssue {
8585            return Err(self.draft_has_no_comments(task));
8586        }
8587        Ok(Some(item.id))
8588    }
8589
8590    /// The refusal a comment call on a board draft is answered with: GitHub keeps comments on
8591    /// issues, and a draft is not one.
8592    fn draft_has_no_comments(&self, task: &NativeId) -> SourceError {
8593        SourceError::Refused {
8594            message: format!(
8595                "task {} of source {} is a draft item on the board, and GitHub keeps \
8596                 comments on issues alone, so a draft has none to read or write; next: \
8597                 convert the draft to an issue on the board, then comment on the issue it \
8598                 becomes",
8599                task.0, self.name
8600            ),
8601        }
8602    }
8603
8604    /// One task and a page of its comments, read with [`graphql::ISSUE_DETAIL`] in one
8605    /// request — or `None` when this board holds no task by that id.
8606    ///
8607    /// What `task show` and a comment listing read. A draft is a task with no comments, so it
8608    /// is answered with the draft and the refusal, at the price of the draft's own read.
8609    async fn issue_detail(
8610        &self,
8611        id: &NativeId,
8612        page: &PageRequest,
8613    ) -> Result<Option<TaskDetailRead>, SourceError> {
8614        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
8615        let asked = self
8616            .graphql(
8617                graphql::ISSUE_DETAIL,
8618                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after,
8619                       "nestedFirst":NESTED_PAGE_SIZE,"boardItems":BOARD_ITEMS_PAGE_SIZE,
8620                       "duplicates":true}),
8621            )
8622            .await;
8623        let data = match asked {
8624            Ok(data) => data,
8625            Err(error) if unresolvable_node(&error) => return Ok(None),
8626            Err(error) => return Err(error),
8627        };
8628        // `node` is null for an id that names nothing, and absent only from an answer this
8629        // source cannot read — never the same thing.
8630        let node = data.get("node").ok_or_else(|| SourceError::Malformed {
8631            message: format!("GitHub answered the read of {} with no node", id.0),
8632        })?;
8633        self.detail_of(id, node, true, after).await
8634    }
8635
8636    /// Several tasks, each with the first page of its comments when `comments` is set, read
8637    /// [`DETAIL_BATCH`] at a time with [`graphql::ISSUE_DETAILS`] — one answer per id, in
8638    /// order.
8639    ///
8640    /// A batch GitHub refuses because one of its ids resolves to no node at all is read again
8641    /// one item at a time, so that id is answered as missing and the others as themselves; any
8642    /// other refusal is every id of that batch's answer.
8643    async fn issue_details(
8644        &self,
8645        ids: &[NativeId],
8646        comments: Option<&PageRequest>,
8647    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
8648        let mut read = Vec::with_capacity(ids.len());
8649        for batch in ids.chunks(DETAIL_BATCH) {
8650            match self
8651                .graphql(graphql::ISSUE_DETAILS, detail_batch(batch, comments))
8652                .await
8653            {
8654                Ok(data) => {
8655                    for (slot, id) in batch.iter().enumerate() {
8656                        // Every alias asked for is answered, null for an id naming nothing;
8657                        // one missing is an answer this source cannot read.
8658                        let read_one = match data.get(format!("i{slot}")) {
8659                            Some(node) => self.detail_of(id, node, comments.is_some(), None).await,
8660                            None => Err(SourceError::Malformed {
8661                                message: format!(
8662                                    "GitHub answered a batch read with no item for {}",
8663                                    id.0
8664                                ),
8665                            }),
8666                        };
8667                        read.push(read_one);
8668                    }
8669                }
8670                Err(error) if unresolvable_node(&error) => {
8671                    for id in batch {
8672                        read.push(match comments {
8673                            Some(page) => self.issue_detail(id, page).await,
8674                            None => self.task_read(id).await,
8675                        });
8676                    }
8677                }
8678                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
8679            }
8680        }
8681        read
8682    }
8683
8684    /// One task and nothing of its comments, as [`TaskSource::get_task`] reads it.
8685    async fn task_read(&self, id: &NativeId) -> Result<Option<TaskDetailRead>, SourceError> {
8686        Ok(self.get_task(id).await?.map(|task| TaskDetailRead {
8687            task,
8688            comments: None,
8689        }))
8690    }
8691
8692    /// What one node a detail read reached says: the task this board holds by `id`, with the
8693    /// page of comments the node carries when `commented` — or `None` for a node that is no
8694    /// task of this board.
8695    ///
8696    /// Resolved as [`Self::item_by_id`] resolves an item: a draft is read again as a draft,
8697    /// and an item this process created answers from this process's own record, which a node
8698    /// read taken moments after the write can still be behind.
8699    async fn detail_of(
8700        &self,
8701        id: &NativeId,
8702        node: &Value,
8703        commented: bool,
8704        after: Option<&str>,
8705    ) -> Result<Option<TaskDetailRead>, SourceError> {
8706        if node.is_null() {
8707            return Ok(None);
8708        }
8709        let draft = optional_str(node, "__typename")? == Some("DraftIssue");
8710        // An issue answered under one id is that id's, or the answer is not one this source
8711        // can report: reporting another issue's task and comments under the qualified id asked
8712        // for would be the one wrong answer here. A draft's own read checks the same.
8713        if !draft
8714            && optional_str(node, "__typename")? == Some("Issue")
8715            && required_str(node, "id")? != id.0
8716        {
8717            return Err(SourceError::Malformed {
8718                message: format!(
8719                    "GitHub answered the read of {} with issue {}",
8720                    id.0,
8721                    required_str(node, "id")?
8722                ),
8723            });
8724        }
8725        let item = if draft {
8726            self.draft_by_id(id).await?
8727        } else {
8728            self.resolve_issue(node).await?
8729        };
8730        // A read that carried the whole of what the issue is blocked by — a copy's — is kept
8731        // for this command, so the dependency read the copy makes next is answered from it.
8732        if let Some(read) = item.as_ref().filter(|item| item.blocked_by.is_some()) {
8733            self.resolved_cache()?.insert(read.id.clone(), read.clone());
8734        }
8735        let Some(item) = item.filter(|item| item.kind == BoardKind::Work(ItemKind::Task)) else {
8736            return Ok(None);
8737        };
8738        let own = self.created()?.iter().find(|own| own.id == *id).cloned();
8739        let task = own.unwrap_or(item).task()?;
8740        let comments = match (commented, draft) {
8741            (false, _) => None,
8742            (true, true) => Some(Err(self.draft_has_no_comments(id))),
8743            (true, false) => Some(comment_page(node, &id.0, after).map(Some)),
8744        };
8745        if commented && !draft && after.is_none() {
8746            self.expected.learn_comments(id, node);
8747        }
8748        Ok(Some(TaskDetailRead { task, comments }))
8749    }
8750
8751    /// Whether the comment `comment` is one of `issue`'s own.
8752    ///
8753    /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
8754    /// comment's id and nothing else: a comment id given against the wrong task would
8755    /// otherwise change a comment on some other issue entirely. An id that names nothing, or
8756    /// names something that is not an issue comment, is a comment this task does not have —
8757    /// which is what GitHub refusing to resolve it means too.
8758    async fn comment_is_on(
8759        &self,
8760        issue: &NativeId,
8761        comment: &NativeId,
8762    ) -> Result<bool, SourceError> {
8763        Ok(self.comment_on(issue, comment).await?.is_some())
8764    }
8765
8766    /// The body the comment `comment` holds, exactly as GitHub holds it, when it is one of
8767    /// `issue`'s own — the read [`Self::comment_is_on`] makes, answering with what it selected.
8768    async fn comment_on(
8769        &self,
8770        issue: &NativeId,
8771        comment: &NativeId,
8772    ) -> Result<Option<String>, SourceError> {
8773        // A write the store's cache expects knows which comments its issue held, and their
8774        // bodies, at the version it expects: what this read would answer.
8775        if let Some(primed) = self.expected.primed_comment(issue, comment) {
8776            return Ok(primed);
8777        }
8778        let asked = self
8779            .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
8780            .await;
8781        let data = match asked {
8782            Ok(data) => data,
8783            Err(error) if unresolvable_node(&error) => return Ok(None),
8784            Err(error) => return Err(error),
8785        };
8786        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
8787            return Ok(None);
8788        };
8789        if optional_str(node, "__typename")? != Some("IssueComment") {
8790            return Ok(None);
8791        }
8792        let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
8793            message: format!("GitHub issue comment {} names no issue", comment.0),
8794        })?;
8795        if required_str(on, "id")? != issue.0 {
8796            return Ok(None);
8797        }
8798        Ok(Some(required_str(node, "body")?.to_owned()))
8799    }
8800
8801    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
8802    async fn partition_edges(
8803        &self,
8804        near_kind: BoardKind,
8805        near_content: ContentKind,
8806        carried: Option<&[Value]>,
8807        depends_on: &[DependencyEdge],
8808    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
8809        let mut native = Vec::new();
8810        let mut fallback = Vec::new();
8811        let far_ends: Vec<(&DependencyEdge, &str, bool, Option<&Value>)> = depends_on
8812            .iter()
8813            .map(|edge| {
8814                let same_source = edge
8815                    .to
8816                    .source()
8817                    .is_none_or(|source| source == self.name.as_str());
8818                // A qualified id's source segment runs to its *first* colon — `GlobalId` and
8819                // `DependencyEndpoint::source` both read it that way — and a native id may hold
8820                // colons of its own, so the far end is everything after that one separator.
8821                // Splitting at the last would truncate `work:urn:task:7` to `7`.
8822                let far_id = if edge.to.is_qualified() {
8823                    edge.to
8824                        .id()
8825                        .split_once(':')
8826                        .map_or(edge.to.id(), |(_, native)| native)
8827                } else {
8828                    edge.to.id()
8829                };
8830                // One that already blocks the near issue was answered by that issue's own
8831                // read, which carried each of its blockers' kinds — an issue every one — so it
8832                // is not read again.
8833                let blocking = carried.and_then(|nodes| {
8834                    nodes
8835                        .iter()
8836                        .find(|node| node.get("id").and_then(Value::as_str) == Some(far_id))
8837                });
8838                (edge, far_id, same_source, blocking)
8839            })
8840            .collect();
8841        // Every other same-source far end is read by its own id, exactly as the item it is a
8842        // far end of is: whether this board holds it is that read's answer, never a listing's.
8843        // They are read together, [`DETAIL_BATCH`] to a request, rather than one each.
8844        let mut unread: Vec<NativeId> = Vec::new();
8845        for (_, far_id, same_source, blocking) in &far_ends {
8846            let id = NativeId((*far_id).to_owned());
8847            if *same_source && blocking.is_none() && !unread.contains(&id) {
8848                unread.push(id);
8849            }
8850        }
8851        let read: BTreeMap<NativeId, Option<Resolved>> = unread
8852            .iter()
8853            .cloned()
8854            .zip(self.items_by_ids(&unread).await?)
8855            .collect();
8856        for (edge, far_id, same_source, blocking) in far_ends {
8857            let far = match (same_source, blocking) {
8858                (false, _) => None,
8859                (true, Some(node)) => Some(FarEnd {
8860                    kind: if required_str(node, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
8861                        BoardKind::Document
8862                    } else {
8863                        BoardKind::Work(related_kind(node)?)
8864                    },
8865                    content_kind: ContentKind::Issue,
8866                }),
8867                (true, None) => {
8868                    let read = read
8869                        .get(&NativeId(far_id.to_owned()))
8870                        .cloned()
8871                        .flatten()
8872                        .ok_or_else(|| SourceError::Refused {
8873                            message: format!("GitHub dependency item {far_id} was not found"),
8874                        })?;
8875                    Some(FarEnd {
8876                        kind: read.kind,
8877                        content_kind: read.content_kind,
8878                    })
8879                }
8880            };
8881            let far = far.as_ref();
8882            // The caller says which kind the far end is, and this board holds the far end
8883            // itself, so a disagreement is settled here rather than stored: recorded, the
8884            // wrong kind would read back as a cross-level edge that never existed; written
8885            // natively, it would name a relationship of a different level than the caller
8886            // asked for.
8887            //
8888            // A far end this board holds as a *document* fails the same comparison and is
8889            // refused by the same sentence: `ItemKind` has no document variant because
8890            // nothing may point at one, so no caller can name it correctly and the refusal
8891            // is the only honest answer.
8892            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
8893                return Err(SourceError::Refused {
8894                    message: format!(
8895                        "GitHub dependency item {far_id} is a {} of this board, and this item \
8896                         names it as a {}; record the kind it is",
8897                        disagreeing.kind.describes(),
8898                        edge.to.kind.marker()
8899                    ),
8900                });
8901            }
8902            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
8903            // however the far end is spelled — and one classified native here would be
8904            // written nowhere at all, because a draft's native reconciliation never runs.
8905            let native_here = near_content == ContentKind::Issue
8906                && far.is_some_and(|far| {
8907                    far.content_kind == ContentKind::Issue
8908                        && BoardKind::Work(edge.to.kind) == near_kind
8909                });
8910            if native_here {
8911                native.push(far_id.to_owned());
8912            } else {
8913                fallback.push(edge.clone());
8914            }
8915        }
8916        Ok((native, fallback))
8917    }
8918
8919    async fn update_existing(
8920        &self,
8921        item: &Resolved,
8922        incoming: &Incoming<'_>,
8923        body: &Option<String>,
8924        status_target: Option<&StatusTarget>,
8925    ) -> Result<(), SourceError> {
8926        let title = incoming.written_title();
8927        // A terminal status closes the issue here, in the same mutation as its body: its board
8928        // option was selected before this, so a close never lands on an item whose board cannot
8929        // show it.
8930        let mut fields = match item.content_kind {
8931            ContentKind::DraftIssue => json!({"title":title,"body":body}),
8932            ContentKind::Issue => json!({"title":title,"body":body,
8933                                         "stateInput":state_input(status_target)}),
8934        };
8935        // A write the store's cache expects sends only the members whose value differs from the
8936        // version it expects, and nothing at all when none does.
8937        if self.expected.is_primed(&item.id)
8938            && let Some(members) = fields.as_object_mut()
8939        {
8940            if members.get("title").and_then(Value::as_str) == Some(item.title.as_str()) {
8941                members.remove("title");
8942            }
8943            if members.get("body").and_then(Value::as_str)
8944                == Some(item.raw_body.as_deref().unwrap_or_default())
8945            {
8946                members.remove("body");
8947            }
8948            let terminal = matches!(status_target, Some(StatusTarget::Terminal(_, _)));
8949            if members.contains_key("stateInput") && terminal == item.closed {
8950                members.remove("stateInput");
8951            }
8952            if members.is_empty() {
8953                return Ok(());
8954            }
8955        }
8956        self.update_content(item.content_kind, &item.id, fields)
8957            .await
8958    }
8959
8960    /// Update one board item's content with exactly `fields` beside its id, through the
8961    /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
8962    /// a draft.
8963    ///
8964    /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
8965    /// is what lets a narrow write carry the one thing it changes and nothing else.
8966    async fn update_content(
8967        &self,
8968        kind: ContentKind,
8969        id: &NativeId,
8970        fields: Value,
8971    ) -> Result<(), SourceError> {
8972        let (operation, id_key, pointer) = match kind {
8973            ContentKind::DraftIssue => (
8974                graphql::UPDATE_DRAFT,
8975                "draftIssueId",
8976                "/updateProjectV2DraftIssue/draftIssue",
8977            ),
8978            ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
8979        };
8980        let mut input = fields;
8981        input[id_key] = json!(id.0);
8982        let data = self.graphql(operation, json!({"input":input})).await?;
8983        let returned = data
8984            .pointer(pointer)
8985            .ok_or_else(|| SourceError::Malformed {
8986                message: "GitHub item update returned no item".into(),
8987            })?;
8988        if required_str(returned, "id")? != id.0 {
8989            return Err(SourceError::Malformed {
8990                message: "GitHub item update returned the wrong item".into(),
8991            });
8992        }
8993        Ok(())
8994    }
8995
8996    /// Creates one issue, files it on the board, and reports what a read of it would say:
8997    /// its content id, its board item id, and the web address GitHub gave it.
8998    ///
8999    /// Two calls rather than one: `createIssue` answers with an issue that is on no board,
9000    /// and `addProjectV2ItemById` is what puts it there. Filing it at creation through
9001    /// `CreateIssueInput.projectV2Ids` was tried and is not done: GitHub answered with no
9002    /// board item, and the `addProjectV2ItemById` that then had to follow was refused
9003    /// "Content already exists in this project". A terminal status is not written here:
9004    /// `finish_write` selects its option first and closes the issue after, so a close never
9005    /// lands on an item whose board cannot show it.
9006    ///
9007    /// The address and the number come back here because this is the only place either is
9008    /// known before GitHub's own board read catches up — an item this run created answers
9009    /// the reads that follow it out of the record below, and one remembered without them
9010    /// would report no location and no key for the rest of the run.
9011    async fn create_and_file_issue(
9012        &self,
9013        board_id: &str,
9014        repository: &RepositoryTarget,
9015        incoming: &Incoming<'_>,
9016        body: &Option<String>,
9017    ) -> Result<Landed, SourceError> {
9018        let repository_id = self.repository_id(repository, incoming).await?;
9019        let data = self
9020            .graphql(
9021                graphql::CREATE_ISSUE,
9022                json!({"input":{
9023                    "repositoryId":repository_id,"title":incoming.written_title(),"body":body
9024                }}),
9025            )
9026            .await?;
9027        let created = data
9028            .pointer("/createIssue/issue")
9029            .filter(|value| !value.is_null())
9030            .ok_or_else(|| SourceError::Malformed {
9031                message: "GitHub issue creation returned no issue".into(),
9032            })?;
9033        let content_id = NativeId(required_str(created, "id")?.to_owned());
9034        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
9035        // a response without it is not worth failing a landed write over — the item simply
9036        // reports no location until the board read catches up, which is what it did before.
9037        let url = optional_str(created, "url")?.map(str::to_owned);
9038        // The issue exists from here on, so an unreadable number and a refused board
9039        // filing below each try, best effort, to take it back: an issue in the repository
9040        // that is on no board is an item nobody asked for and nothing here would find again.
9041        //
9042        // Its number is optional on the same terms its address is — a landed write is not
9043        // worth failing over a member that came back missing, and such an item reports no
9044        // handle until a board read catches up. A number that is *present* and is not an
9045        // unsigned integer is still a response this source cannot read.
9046        let number = match created_issue_number(created) {
9047            Ok(number) => number,
9048            Err(error) => {
9049                let _ = self.delete_issue(&content_id).await;
9050                return Err(error);
9051            }
9052        };
9053        let added = match self
9054            .graphql(
9055                graphql::ADD_TO_BOARD,
9056                json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
9057            )
9058            .await
9059        {
9060            Ok(added) => added,
9061            Err(error) => {
9062                let _ = self.delete_issue(&content_id).await;
9063                return Err(error);
9064            }
9065        };
9066        let item = added
9067            .pointer("/addProjectV2ItemById/item")
9068            .filter(|value| !value.is_null())
9069            .ok_or_else(|| SourceError::Malformed {
9070                message: "GitHub board addition returned no project item".into(),
9071            })?;
9072        Ok(Landed {
9073            content_id,
9074            item_id: required_str(item, "id")?.to_owned(),
9075            url,
9076            number,
9077        })
9078    }
9079
9080    /// Move one issue under the project it now belongs to, or out of the one it left.
9081    async fn reparent(
9082        &self,
9083        held: Option<NativeId>,
9084        child: &NativeId,
9085        wanted: Option<&NativeId>,
9086    ) -> Result<(), SourceError> {
9087        if held.as_ref() == wanted {
9088            return Ok(());
9089        }
9090        if let Some(held) = &held {
9091            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
9092                .await?;
9093        }
9094        if let Some(wanted) = wanted {
9095            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
9096                .await?;
9097        }
9098        Ok(())
9099    }
9100
9101    async fn sub_issue(
9102        &self,
9103        operation: &str,
9104        parent: &NativeId,
9105        child: &NativeId,
9106        root: &str,
9107    ) -> Result<(), SourceError> {
9108        let data = self
9109            .graphql(
9110                operation,
9111                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
9112            )
9113            .await?;
9114        let issue =
9115            data.pointer(&format!("/{root}/issue"))
9116                .ok_or_else(|| SourceError::Malformed {
9117                    message: "GitHub sub-issue update returned no issue".into(),
9118                })?;
9119        let sub =
9120            data.pointer(&format!("/{root}/subIssue"))
9121                .ok_or_else(|| SourceError::Malformed {
9122                    message: "GitHub sub-issue update returned no sub-issue".into(),
9123                })?;
9124        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
9125            return Err(SourceError::Malformed {
9126                message: "GitHub sub-issue update returned the wrong issues".into(),
9127            });
9128        }
9129        Ok(())
9130    }
9131
9132    /// Bring one issue's `blockedBy` to exactly `native`, sending only the difference, and say
9133    /// whether there was one.
9134    ///
9135    /// An issue [`Issue::Created`] by this very write is blocked by nothing yet, so its
9136    /// relationships are not read: there is nothing a read of them could find.
9137    async fn reconcile_blocked_by(
9138        &self,
9139        content_id: &NativeId,
9140        native: &[String],
9141        issue: Issue<'_>,
9142    ) -> Result<bool, SourceError> {
9143        let current = match issue {
9144            Issue::Created => Vec::new(),
9145            Issue::Existing(Some(held)) => held
9146                .iter()
9147                .map(|far| required_str(far, "id").map(str::to_owned))
9148                .collect::<Result<Vec<_>, _>>()?,
9149            Issue::Existing(None) => self.native_dependency_ids(content_id).await?,
9150        };
9151        let mut changed = false;
9152        for (operation, far_id) in current
9153            .iter()
9154            .filter(|id| !native.contains(id))
9155            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
9156            .chain(
9157                native
9158                    .iter()
9159                    .filter(|id| !current.contains(id))
9160                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
9161            )
9162        {
9163            let data = self
9164                .graphql(
9165                    operation,
9166                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
9167                )
9168                .await?;
9169            let root = if operation == graphql::ADD_BLOCKED_BY {
9170                "addBlockedBy"
9171            } else {
9172                "removeBlockedBy"
9173            };
9174            let issue =
9175                data.pointer(&format!("/{root}/issue"))
9176                    .ok_or_else(|| SourceError::Malformed {
9177                        message: "GitHub dependency update returned no issue".into(),
9178                    })?;
9179            let blocker = data
9180                .pointer(&format!("/{root}/blockingIssue"))
9181                .ok_or_else(|| SourceError::Malformed {
9182                    message: "GitHub dependency update returned no blocking issue".into(),
9183                })?;
9184            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
9185            {
9186                return Err(SourceError::Malformed {
9187                    message: "GitHub dependency update returned the wrong issues".into(),
9188                });
9189            }
9190            changed = true;
9191        }
9192        Ok(changed)
9193    }
9194}
9195
9196/// What a write needs to know of one far end it names: which kind of item it is, and whether
9197/// it is an issue a native relationship can name.
9198struct FarEnd {
9199    kind: BoardKind,
9200    content_kind: ContentKind,
9201}
9202
9203/// Whether the issue one write reconciles was created by that write or was already there.
9204#[derive(Clone, Copy, PartialEq, Eq)]
9205enum Issue<'a> {
9206    /// Created by this write, so it holds no relationships yet.
9207    Created,
9208    /// On the board before this write, holding whatever relationships it holds — the far
9209    /// ends of its whole `blockedBy`, when the read that reached it carried them.
9210    Existing(Option<&'a [Value]>),
9211}
9212
9213/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
9214enum Reached {
9215    /// An issue this board holds, resolved into everything this source reports about it.
9216    Held(Box<Resolved>),
9217    /// Nothing this board holds: no such node, or a node on some other board.
9218    Nothing,
9219    /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
9220    /// again by [`GitHubProjectsSource::draft_by_id`].
9221    Draft,
9222}
9223
9224/// What GitHub says when a string is not a node id it can resolve.
9225///
9226/// Matched because it is the ordinary answer to a project selector naming a project by its
9227/// *name*, and reporting that as a failure would make naming one impossible. It is read
9228/// off the refusal GitHub sent, never guessed from the shape of the string: this source
9229/// does not define the syntax of a GitHub node id and would be wrong about it.
9230const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
9231
9232/// `error` with `note` added to the end of what it says, its kind and every other member
9233/// unchanged — so a caller still branches on the failure that happened, and reads beside it
9234/// what that failure left behind.
9235fn noting(error: SourceError, note: &str) -> SourceError {
9236    match error {
9237        SourceError::Config { message } => SourceError::Config {
9238            message: message + note,
9239        },
9240        SourceError::Auth { message } => SourceError::Auth {
9241            message: message + note,
9242        },
9243        SourceError::Refused { message } => SourceError::Refused {
9244            message: message + note,
9245        },
9246        SourceError::RateLimited {
9247            retry_after_seconds,
9248            message,
9249        } => SourceError::RateLimited {
9250            retry_after_seconds,
9251            message: Some(message.unwrap_or_default() + note),
9252        },
9253        SourceError::Unavailable { message } => SourceError::Unavailable {
9254            message: message + note,
9255        },
9256        SourceError::Malformed { message } => SourceError::Malformed {
9257            message: message + note,
9258        },
9259    }
9260}
9261
9262/// The variables of one [`graphql::ISSUE_COPY_DETAILS`] request, as [`detail_batch`] binds its
9263/// own: a slot `batch` has no id for is bound to its last id, read again at no added price.
9264fn copy_detail_batch(batch: &[NativeId], comments: &PageRequest) -> Value {
9265    let mut variables = serde_json::Map::new();
9266    for slot in 0..COPY_DETAIL_BATCH {
9267        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
9268        variables.insert(format!("id{slot}"), json!(id));
9269    }
9270    variables.insert("first".to_owned(), json!(comments.limit.min(MAX_PAGE_SIZE)));
9271    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
9272    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
9273    variables.insert("duplicates".to_owned(), json!(true));
9274    Value::Object(variables)
9275}
9276
9277/// The variables of one [`graphql::ISSUE_DETAILS`] request over `batch` — at most
9278/// [`DETAIL_BATCH`] ids — each item with the first page of its comments when `comments` asks
9279/// for them.
9280///
9281/// The document is fixed-size, so a slot `batch` has no id for is bound to its last id, which
9282/// is read again at no added price.
9283fn detail_batch(batch: &[NativeId], comments: Option<&PageRequest>) -> Value {
9284    let mut variables = serde_json::Map::new();
9285    for slot in 0..DETAIL_BATCH {
9286        let id = batch.get(slot).or(batch.last()).map(|id| id.0.clone());
9287        variables.insert(format!("id{slot}"), json!(id));
9288    }
9289    variables.insert(
9290        "first".to_owned(),
9291        json!(comments.map_or(MAX_PAGE_SIZE, |page| page.limit.min(MAX_PAGE_SIZE))),
9292    );
9293    variables.insert("comments".to_owned(), json!(comments.is_some()));
9294    variables.insert("nestedFirst".to_owned(), json!(NESTED_PAGE_SIZE));
9295    variables.insert("boardItems".to_owned(), json!(BOARD_ITEMS_PAGE_SIZE));
9296    variables.insert("duplicates".to_owned(), json!(true));
9297    Value::Object(variables)
9298}
9299
9300/// Whether this refusal is GitHub saying the id names no node at all.
9301fn unresolvable_node(error: &SourceError) -> bool {
9302    matches!(error, SourceError::Refused { message }
9303        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
9304}
9305
9306/// One project name, as a search qualifier which filters on it at the server.
9307///
9308/// Quoted so the whole title is one phrase rather than a bag of words, with the two
9309/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
9310/// the way it documents. A title matched here is still compared for equality afterwards:
9311/// the qualifier narrows what the server sends, and this source decides what it names.
9312fn title_qualifier(name: &str) -> String {
9313    format!("in:title {}", quoted(name))
9314}
9315
9316/// `value` as one quoted phrase of a GitHub search or a board filter, with the two
9317/// characters GitHub's quoting grammar gives a meaning inside a quoted phrase escaped the way
9318/// it documents — so a value holding a qualifier's spelling is searched for rather than
9319/// obeyed.
9320fn quoted(value: &str) -> String {
9321    let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
9322    format!("\"{escaped}\"")
9323}
9324
9325/// The search qualifier for the issues updated at or after `since`.
9326///
9327/// Written to the second, rounded down, which can only widen what the search returns.
9328fn updated_qualifier(since: DateTime<Utc>) -> String {
9329    format!("updated:>={}", since.format("%Y-%m-%dT%H:%M:%S+00:00"))
9330}
9331
9332/// The search terms that narrow a board-scoped issue search to a task query's text and
9333/// metadata predicates, or `None` when it carries neither.
9334///
9335/// The text is one quoted phrase, searched `in:title`, `in:body` or both as its fields say,
9336/// and each metadata value is one more quoted phrase, which GitHub finds in the body because
9337/// its index covers the metadata comment the value is stored in. GitHub ANDs the phrases and
9338/// matches each in any field the `in:` qualifier names, so a query naming a title search and
9339/// a metadata value searches both fields for both — wider than asked, never narrower, and
9340/// every candidate is confirmed in process afterwards.
9341///
9342/// **This narrows a text search, and that is this source's declared semantics.** GitHub
9343/// matches whole tokens where a substring rule would match inside a word, so an item holding
9344/// the text only inside a longer word is not returned. A text of nothing but whitespace
9345/// matches every item, so it narrows nothing and is not sent.
9346fn narrowing_qualifiers(query: &TaskQuery) -> Option<String> {
9347    let text = query
9348        .text
9349        .as_ref()
9350        .filter(|text| !text.terms.trim().is_empty());
9351    if text.is_none() && query.metadata.is_empty() {
9352        return None;
9353    }
9354    let (title, body) = match text.map(|text| text.fields) {
9355        None => (false, true),
9356        Some(TextFields::Title) => (true, !query.metadata.is_empty()),
9357        Some(TextFields::Content) => (false, true),
9358        Some(TextFields::TitleOrContent) => (true, true),
9359    };
9360    let fields = match (title, body) {
9361        (true, true) => "in:title,body",
9362        (true, false) => "in:title",
9363        _ => "in:body",
9364    };
9365    let phrases = text
9366        .map(|text| text.terms.clone())
9367        .into_iter()
9368        .chain(
9369            query
9370                .metadata
9371                .iter()
9372                .map(|wanted| as_stored(wanted.value())),
9373        )
9374        .map(|phrase| quoted(&phrase))
9375        .collect::<Vec<_>>();
9376    Some(format!("{fields} {}", phrases.join(" ")))
9377}
9378
9379/// The search terms that narrow a board-scoped issue search to a project or document query's
9380/// text, or `None` when it has none or a blank one: the phrase, in the fields, a task query
9381/// carrying that text alone is sent as by [`narrowing_qualifiers`].
9382fn text_qualifiers(text: Option<&TextQuery>) -> Option<String> {
9383    narrowing_qualifiers(&TaskQuery {
9384        text: text.cloned(),
9385        ..TaskQuery::default()
9386    })
9387}
9388
9389/// Refuses a project or document query's text GitHub's issue search cannot find, before
9390/// anything is asked of GitHub, on exactly the terms [`refuse_unsearchable`] refuses a task
9391/// query's.
9392fn refuse_unsearchable_text(text: Option<&TextQuery>) -> Result<(), SourceError> {
9393    refuse_unsearchable(&TaskQuery {
9394        text: text.cloned(),
9395        ..TaskQuery::default()
9396    })
9397}
9398
9399/// Refuses a task query naming a text or a metadata value GitHub's issue search cannot find,
9400/// before anything is asked of GitHub.
9401///
9402/// GitHub's index holds words, so a phrase with no letter or digit names none to find, and no
9403/// bounded query answers it: sent, GitHub's answer to it is nothing this source may rely on;
9404/// left out, the search is every issue of the board. So this source says it cannot answer
9405/// rather than reading the board or answering nothing. A blank text is not refused: it narrows
9406/// nothing GitHub could search for, and keeps the board read it always had.
9407fn refuse_unsearchable(query: &TaskQuery) -> Result<(), SourceError> {
9408    const WHY: &str = "GitHub's issue search indexes words, so it cannot answer a value with no \
9409                       letter or digit with a bounded query";
9410    if let Some(text) = &query.text
9411        && !text.terms.trim().is_empty()
9412        && !has_words(&text.terms)
9413    {
9414        return Err(SourceError::Refused {
9415            message: format!(
9416                "cannot search for the text {:?}: {WHY}; search for a text holding a letter or a digit",
9417                text.terms
9418            ),
9419        });
9420    }
9421    if let Some(wanted) = query
9422        .metadata
9423        .iter()
9424        .find(|wanted| !has_words(wanted.value()))
9425    {
9426        return Err(SourceError::Refused {
9427            message: format!(
9428                "cannot filter by the metadata value {:?} at {:?}: {WHY}; filter by a value holding a letter or a digit",
9429                wanted.value(),
9430                std::iter::once(wanted.key())
9431                    .chain(wanted.path().iter().map(String::as_str))
9432                    .collect::<Vec<_>>()
9433                    .join("/"),
9434            ),
9435        });
9436    }
9437    Ok(())
9438}
9439
9440/// Whether GitHub's index could hold a word of `phrase`: whether it has a letter or a digit.
9441fn has_words(phrase: &str) -> bool {
9442    phrase.chars().any(char::is_alphanumeric)
9443}
9444
9445/// `value` spelled the way the metadata slot stores it: as the inside of its JSON string.
9446///
9447/// What GitHub indexes is the slot's JSON text, so a value holding a character JSON escapes —
9448/// a newline, a tab, a quote — is found by the escape the body holds and not by the character,
9449/// which GitHub's word match would read as different words.
9450fn as_stored(value: &str) -> String {
9451    let encoded = Value::String(value.to_owned()).to_string();
9452    encoded[1..encoded.len() - 1].to_owned()
9453}
9454
9455/// The one narrower question a task query carrying a text, metadata or origin predicate is
9456/// sent as.
9457enum Narrowing {
9458    /// Every carrier of this origin: [`graphql::ORIGIN_LOOKUP`].
9459    Origin(String),
9460    /// The board-scoped issue search narrowed by these qualifiers.
9461    Search(String),
9462}
9463
9464impl Narrowing {
9465    /// What this question is remembered under for the length of one command.
9466    fn key(&self) -> String {
9467        match self {
9468            Self::Origin(origin) => format!("origin {origin}"),
9469            Self::Search(also) => format!("search {also}"),
9470        }
9471    }
9472}
9473
9474/// Where one connection of [`graphql::ORIGIN_LOOKUP`] resumes.
9475enum Resumed {
9476    /// It reported another page, which starts after this cursor.
9477    More(String),
9478    /// It has ended. Sending this cursor again — the page's own end when it had one, and
9479    /// otherwise the cursor it was reached from — answers an empty page, so the one document
9480    /// can go on walking the other connection.
9481    Ended(Option<String>),
9482}
9483
9484impl Resumed {
9485    /// Whether the connection has another page.
9486    const fn has_more(&self) -> bool {
9487        matches!(self, Self::More(_))
9488    }
9489
9490    /// The cursor to send this connection next.
9491    fn cursor(self) -> Option<String> {
9492        match self {
9493            Self::More(next) => Some(next),
9494            Self::Ended(last) => last,
9495        }
9496    }
9497}
9498
9499/// Where `connection`, reached from `after`, resumes — refused when it reports another page
9500/// with no cursor to it, or from a cursor that does not advance.
9501fn resumed(connection: &Value, after: Option<&str>) -> Result<Resumed, SourceError> {
9502    let info = connection
9503        .get("pageInfo")
9504        .ok_or_else(|| SourceError::Malformed {
9505            message: "GitHub connection has no pageInfo".into(),
9506        })?;
9507    let end = optional_str(info, "endCursor")?;
9508    if required_bool(info, "hasNextPage")? {
9509        let next = end.ok_or_else(|| SourceError::Malformed {
9510            message: "GitHub connection reports another page and no endCursor".into(),
9511        })?;
9512        validate_cursor_progress(after, next)?;
9513        return Ok(Resumed::More(next.to_owned()));
9514    }
9515    Ok(Resumed::Ended(
9516        end.map(str::to_owned).or_else(|| after.map(str::to_owned)),
9517    ))
9518}
9519
9520/// The board, and every item on it this source reports.
9521#[derive(Clone)]
9522struct Board {
9523    id: String,
9524    fields: Value,
9525    items: Vec<Resolved>,
9526}
9527
9528/// What a write needs of the board and nothing more: its node id and its field
9529/// definitions, in the shape a read of the board's own `fields` gives them.
9530///
9531/// Deliberately no items. A write decides which item it writes, which parent it files
9532/// under and which far ends it names by reading each of them by its own id; this is the
9533/// half of the board those reads cannot carry, and holding no item is what keeps it from
9534/// ever being asked whether an item is there.
9535#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
9536struct BoardFields {
9537    id: BoardId,
9538    fields: Value,
9539}
9540
9541/// A board's node id: what a field write and `addProjectV2ItemById` address.
9542///
9543/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
9544/// refused where it is read, and one an item names blank is read as not named at all.
9545#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
9546#[serde(try_from = "String")]
9547struct BoardId(String);
9548
9549impl TryFrom<String> for BoardId {
9550    type Error = String;
9551
9552    /// The same refusal [`BoardId::parse`] makes, for an id read back from a kept context.
9553    fn try_from(id: String) -> Result<Self, Self::Error> {
9554        Self::parse(&id).map_err(|error| error.to_string())
9555    }
9556}
9557
9558/// Where one write left its item, for the record the rest of the command reads it out of.
9559///
9560/// A named record rather than a tuple because the update arm and the create arm each fill
9561/// all four, and two `Option`s of different meaning side by side in a tuple are two
9562/// positions a reader has to count.
9563struct Landed {
9564    /// The issue's own node id, which is the [`NativeId`] this source reports.
9565    content_id: NativeId,
9566    /// The board item's id, which is what a field write addresses.
9567    // 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.
9568    item_id: String,
9569    /// The web address GitHub gave the issue, when it gave one.
9570    // 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.
9571    url: Option<String>,
9572    /// The issue's number on its repository, when GitHub reported one.
9573    number: Option<u64>,
9574}
9575
9576impl BoardId {
9577    fn parse(id: &str) -> Result<Self, SourceError> {
9578        if id.trim().is_empty() {
9579            return Err(SourceError::Malformed {
9580                message: "GitHub named a board with a blank node id".into(),
9581            });
9582        }
9583        Ok(Self(id.to_owned()))
9584    }
9585
9586    fn as_str(&self) -> &str {
9587        &self.0
9588    }
9589}
9590
9591impl Board {
9592    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
9593        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
9594        let nodes = fields
9595            .get("nodes")
9596            .and_then(Value::as_array)
9597            .ok_or_else(|| SourceError::Malformed {
9598                message: "GitHub project fields.nodes is not an array".into(),
9599            })?;
9600        Ok(nodes
9601            .iter()
9602            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
9603    }
9604}
9605
9606/// One board item, resolved into everything this source reports about it.
9607///
9608/// Serializable because it is what [`TaskSource::write_context`] hands the store's cache and
9609/// [`TaskSource::prime_write`] takes back — see [`expected`].
9610#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
9611struct Resolved {
9612    item_id: String,
9613    id: NativeId,
9614    content_kind: ContentKind,
9615    kind: BoardKind,
9616    title: String,
9617    body: Option<String>,
9618    /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
9619    /// that changes the slot alone has to keep byte for byte outside it.
9620    raw_body: Option<String>,
9621    status: Status,
9622    /// The name of the board `Status` option this item sits in, as the board spells it.
9623    option: Option<String>,
9624    /// What its `Priority` field says, read through this instance's mapping.
9625    priority: HeldPriority,
9626    /// Whether this item's issue is closed. A draft has no such state and is never closed.
9627    closed: bool,
9628    /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
9629    delivers: Vec<TaskRef>,
9630    /// Every task that delivers this one, read out of its slot. Empty for anything not a
9631    /// task.
9632    delivered_by: Vec<TaskRef>,
9633    labels: Vec<Label>,
9634    parent: Option<NativeId>,
9635    // 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.
9636    origin: Option<String>,
9637    /// What each board text field this instance projects metadata onto holds for this item,
9638    /// by field name — only the fields holding a value, an empty text included. What a write compares the
9639    /// item's metadata against, so a field already holding its value is not written again;
9640    /// never read back as metadata, which the slot alone reports.
9641    projected: BTreeMap<String, String>,
9642    /// The issue's own number on its repository, as GitHub reports it.
9643    ///
9644    /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
9645    /// declares none, and a draft is not filed in a repository to be numbered by one — and
9646    /// an issue this run created whose creating mutation answered without one, which is a
9647    /// response GitHub's own schema says cannot happen and which a landed write is not
9648    /// worth failing over. An `Issue` read off the board always has one.
9649    number: Option<u64>,
9650    url: Option<String>,
9651    created_at: Option<DateTime<Utc>>,
9652    updated_at: Option<DateTime<Utc>>,
9653    /// When the board item itself last changed — what a board field write moves — when the
9654    /// read that reached it said.
9655    item_updated_at: Option<DateTime<Utc>>,
9656    /// How many comments the issue holds, when the read that reached it said.
9657    comment_count: Option<u64>,
9658    /// How many relationship events the issue's timeline holds — every dependency, as
9659    /// `blockedBy` or `blocking`, and every project membership, as its parent, added or
9660    /// removed — when the read that reached it said.
9661    relationship_events: Option<u64>,
9662    /// The server's clock on the response that read it — its `Date` header — when the
9663    /// response carried one.
9664    served_at: Option<DateTime<Utc>>,
9665    /// The digest of its [`change_token::Facets`] — its own kind, its board field values and
9666    /// its labels — when the read that reached it carried all three whole.
9667    #[serde(default)]
9668    facets: Option<change_token::FacetsDigest>,
9669    own_repository: Option<Repository>,
9670    repositories: Vec<Repository>,
9671    classification: Classification,
9672    slot: BTreeMap<String, Value>,
9673    /// The node id of the board this item sits on, when the read that reached it said.
9674    board_id: Option<String>,
9675    /// The definition of every board field this item holds a value of, in the shape a read
9676    /// of the board's own `fields` gives one.
9677    ///
9678    /// Only the fields this item has a value in: a field it holds nothing of is not here,
9679    /// which says nothing about whether the board has it.
9680    fields: Vec<Value>,
9681    /// Every field the board this item sits on defines, as its own read of the board's
9682    /// `fields` gives them — when the read that reached the item carried them, which a read
9683    /// of it by its own id does. What a write of it needs of the board, then, needs no read
9684    /// of the board.
9685    board_fields: Option<Value>,
9686    /// The far ends of this issue's whole `blockedBy` connection, each as a dependency read
9687    /// selects one — when the read that reached it carried the connection to its end, which a
9688    /// read of it by its own id does for any issue blocked by no more than a page. What a
9689    /// write reconciles that relationship against, and what a read of its forward edges in
9690    /// the same command answers with.
9691    blocked_by: Option<Vec<Value>>,
9692}
9693
9694impl Resolved {
9695    /// The board this item's own read names it on, when that read named one this source can
9696    /// address.
9697    fn named_board(&self) -> Option<BoardId> {
9698        self.board_id
9699            .as_deref()
9700            .and_then(|id| BoardId::parse(id).ok())
9701    }
9702
9703    /// The board's id and every field it defines, when the read that reached this item
9704    /// carried both — which a read of it by its own id does.
9705    fn carried_board(&self) -> Option<BoardFields> {
9706        Some(BoardFields {
9707            id: self.named_board()?,
9708            fields: self.board_fields.clone()?,
9709        })
9710    }
9711
9712    /// Whether this item holds a value of the board field called `name`, and so carries
9713    /// that field's definition. `false` says nothing about whether the board has the field.
9714    fn defines(&self, name: &str) -> bool {
9715        self.fields
9716            .iter()
9717            .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
9718    }
9719
9720    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
9721    /// in a field of its own, and none of the five keys that are only an encoding.
9722    ///
9723    /// The two delivery keys are left out for every kind, not only for a task: they are
9724    /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
9725    /// document carrying one holds nothing a caller's own metadata could mean by it.
9726    fn metadata(&self) -> BTreeMap<String, Value> {
9727        let mut metadata = self.slot.clone();
9728        metadata.remove(Repository::METADATA_KEY);
9729        metadata.remove(DependencyEdge::RECORDED_KEY);
9730        metadata.remove(ItemKind::METADATA_KEY);
9731        metadata.remove(TaskRef::DELIVERS_KEY);
9732        metadata.remove(TaskRef::DELIVERED_BY_KEY);
9733        metadata.remove(Classification::METADATA_KEY);
9734        // The board field is the origin, and the body's copy of it is only a mirror for the
9735        // issue search to find: an item whose field holds none has none, whatever its body
9736        // says, so no reader ever sees two answers.
9737        metadata.remove(ORIGIN_KEY);
9738        if let Some(origin) = &self.origin {
9739            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
9740        }
9741        metadata
9742    }
9743
9744    /// Where this item is, as a link a reader can open.
9745    ///
9746    /// A board is a hosted place and every issue on it has a web address, so that address
9747    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
9748    /// of place it is, so a reader knows to open it rather than to read a file out. It
9749    /// does not replace or derive from `url`: the field goes on reporting exactly what it
9750    /// reported before, and this says what that address *is*.
9751    ///
9752    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
9753    /// rather than a third variant, which is the contract's "the source did not say". An
9754    /// issue this run created is not one of those: its address comes back from the
9755    /// creating mutation, so it is somewhere a reader can open from the moment it exists
9756    /// rather than from whenever the board read catches up.
9757    fn location(&self) -> Option<Location> {
9758        self.url.clone().map(Location::Url)
9759    }
9760
9761    /// The short handle this board's backend shows people for a task: the issue's number
9762    /// alone, as a decimal string.
9763    ///
9764    /// The number alone rather than `owner/repo#1043`, because that is the contract's
9765    /// value for this backend. A draft has no number and so no handle, which is the
9766    /// contract's *absent* rather than a handle of some other shape — and the native
9767    /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
9768    /// derives from.
9769    fn key(&self) -> Option<String> {
9770        self.number.map(|number| number.to_string())
9771    }
9772
9773    /// This item's change token — see [`change_token`] — or `None` when the read that
9774    /// reached it did not carry all six of its parts, which a draft never does, did not say
9775    /// when GitHub served it, or found it changed too recently to have settled.
9776    fn version(&self) -> Option<String> {
9777        match self.content_kind {
9778            ContentKind::Issue => {
9779                let (issue, item) = (self.updated_at?, self.item_updated_at?);
9780                let parts = change_token::Parts {
9781                    issue,
9782                    item,
9783                    comments: self.comment_count?,
9784                    relationships: self.relationship_events?,
9785                    facets: self.facets.clone()?,
9786                    repository: self.own_repository.clone()?,
9787                };
9788                change_token::settled(issue, item, self.served_at?)
9789                    .then(|| change_token::spell(&parts))
9790            }
9791            ContentKind::DraftIssue => None,
9792        }
9793    }
9794
9795    /// Whether its `Priority` field holds a value at all, mapped or not.
9796    fn holds_priority(&self) -> bool {
9797        self.priority != HeldPriority::Read(Priority::None)
9798    }
9799
9800    /// The task this item is.
9801    ///
9802    /// Fails for an item whose `Priority` field holds an option the mapping does not name:
9803    /// reading that as a level would be a guess, and reading it as `none` would let the next
9804    /// copy clear a priority a person set.
9805    fn task(&self) -> Result<Task, SourceError> {
9806        let priority = match &self.priority {
9807            HeldPriority::Read(priority) => *priority,
9808            HeldPriority::Unmapped(option) => {
9809                return Err(SourceError::Malformed {
9810                    message: format!(
9811                        "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
9812                         this source's priority_mapping does not name, so its priority cannot be \
9813                         read; next: name {option:?} under priority_mapping, or move the item to \
9814                         a mapped option",
9815                        self.id,
9816                        self.number
9817                            .map(|number| format!(" (#{number})"))
9818                            .unwrap_or_default()
9819                    ),
9820                });
9821            }
9822        };
9823        Ok(Task {
9824            version: self.version(),
9825            id: self.id.clone(),
9826            key: self.key(),
9827            title: self.title.clone(),
9828            content: self.body.clone(),
9829            status: self.status.clone(),
9830            priority,
9831            labels: self.labels.clone(),
9832            project: self.parent.clone(),
9833            url: self.url.clone(),
9834            location: self.location(),
9835            created_at: self.created_at,
9836            updated_at: self.updated_at,
9837            metadata: self.metadata(),
9838            repositories: self.repositories.clone(),
9839            delivers: self.delivers.clone(),
9840            delivered_by: self.delivered_by.clone(),
9841            classification: self.classification,
9842        })
9843    }
9844
9845    fn project(&self) -> Project {
9846        Project {
9847            version: self.version(),
9848            id: self.id.clone(),
9849            title: self.title.clone(),
9850            content: self.body.clone(),
9851            status: self.status.clone(),
9852            labels: self.labels.clone(),
9853            url: self.url.clone(),
9854            location: self.location(),
9855            created_at: self.created_at,
9856            updated_at: self.updated_at,
9857            metadata: self.metadata(),
9858            repositories: self.repositories.clone(),
9859            classification: self.classification,
9860        }
9861    }
9862
9863    /// The same issue as a document: the project it is filed under, and no status and no
9864    /// dependencies, because a document is not work.
9865    fn document(&self) -> Document {
9866        Document {
9867            version: self.version(),
9868            id: self.id.clone(),
9869            title: self.title.clone(),
9870            content: self.body.clone(),
9871            project: self.parent.clone(),
9872            labels: self.labels.clone(),
9873            url: self.url.clone(),
9874            location: self.location(),
9875            created_at: self.created_at,
9876            updated_at: self.updated_at,
9877            metadata: self.metadata(),
9878            repositories: self.repositories.clone(),
9879            classification: self.classification,
9880        }
9881    }
9882}
9883
9884/// Where one targeted update moves an item's status, and which of its two halves move.
9885struct StatusMove {
9886    /// The board the item's `Status` field is on.
9887    board: BoardId,
9888    /// The `Status` field's id.
9889    field: String,
9890    /// The option's id.
9891    option: String,
9892    /// The option's name, as the board spells it.
9893    name: String,
9894    /// What the status asks of the issue's state.
9895    target: StatusTarget,
9896    /// The status the item reads as once it is there.
9897    landed: Status,
9898    /// Which of the status's two halves differ from what the item holds.
9899    moves: Moves,
9900}
9901
9902/// Which halves of an item's status one targeted update moves: its `Status` option, the open or
9903/// closed state of its issue, or both. A status neither half of which differs is no move at all,
9904/// and is not a value of this type.
9905#[derive(Clone, Copy, PartialEq, Eq)]
9906enum Moves {
9907    /// The option alone.
9908    Option,
9909    /// The issue's state alone: open, closed, or closed with another reason.
9910    State,
9911    /// Both.
9912    Both,
9913}
9914
9915impl Moves {
9916    /// What differs, or `None` when nothing does.
9917    const fn of(option: bool, state: bool) -> Option<Self> {
9918        match (option, state) {
9919            (true, true) => Some(Self::Both),
9920            (true, false) => Some(Self::Option),
9921            (false, true) => Some(Self::State),
9922            (false, false) => None,
9923        }
9924    }
9925
9926    /// Whether the option moves.
9927    const fn option(self) -> bool {
9928        matches!(self, Self::Option | Self::Both)
9929    }
9930
9931    /// Whether the issue's state moves.
9932    const fn state(self) -> bool {
9933        matches!(self, Self::State | Self::Both)
9934    }
9935}
9936
9937/// What one write is, and the status that comes with being it.
9938///
9939/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
9940/// status and a task or a project always has one, so "a document carrying a status" and
9941/// "a task carrying none" are states a write cannot be in rather than states every use
9942/// site below has to defend against.
9943enum Written<'a> {
9944    /// A document, which is not work and so has no status at all.
9945    Document,
9946    /// A task or a project, and the status it is being written with.
9947    Work(ItemKind, &'a Status),
9948}
9949
9950impl Written<'_> {
9951    /// Which of the board's three kinds this write is.
9952    const fn kind(&self) -> BoardKind {
9953        match self {
9954            Self::Document => BoardKind::Document,
9955            Self::Work(kind, _) => BoardKind::Work(*kind),
9956        }
9957    }
9958
9959    /// The status this write carries. A document carries none, so a write of one says
9960    /// nothing about the issue's open or closed state and selects no board `Status`
9961    /// option.
9962    const fn status(&self) -> Option<&Status> {
9963        match self {
9964            Self::Document => None,
9965            Self::Work(_, status) => Some(status),
9966        }
9967    }
9968
9969    /// The status this write carries with the kind whose half of `status_mapping` it is
9970    /// written through.
9971    const fn work_status(&self) -> Option<(ItemKind, &Status)> {
9972        match self {
9973            Self::Document => None,
9974            Self::Work(kind, status) => Some((*kind, status)),
9975        }
9976    }
9977}
9978
9979/// The item being written, in the one shape all three write methods reach.
9980struct Incoming<'a> {
9981    written: Written<'a>,
9982    /// The title a person wrote. A document's goes onto the issue with
9983    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
9984    title: &'a str,
9985    content: Option<&'a str>,
9986    assets: Option<&'a onetaskgraph_plugin_api::AssetWrite>,
9987    labels: &'a [Label],
9988    metadata: &'a BTreeMap<String, Value>,
9989    repositories: &'a [Repository],
9990    /// Recorded in the slot while private, and nowhere while public.
9991    classification: Classification,
9992    parent: Option<&'a NativeId>,
9993    /// [`Task::delivers`], already checked. Empty for a project or a document, which is
9994    /// what keeps either key out of their slot.
9995    delivers: &'a [TaskRef],
9996    /// [`Task::delivered_by`], already checked. Empty for a project or a document.
9997    delivered_by: &'a [TaskRef],
9998    /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
9999    /// project, a document, and every write to an instance with no `priority_mapping` —
10000    /// which is what keeps such a write's requests exactly what they were before.
10001    priority: Option<Priority>,
10002}
10003
10004/// What one write does to one projected metadata text field, by the field's id.
10005#[derive(Debug, Clone)]
10006enum ProjectionWrite {
10007    /// Write this text.
10008    Set {
10009        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.
10010        text: String,
10011    },
10012    /// Clear the field's value.
10013    Clear { field: String }, // llmlint: ignore[invalid_states_unrepresentable] As above.
10014}
10015
10016impl ProjectionWrite {
10017    /// Add this write to a field write's list of values and list of clears.
10018    fn join<'a>(&'a self, values: &mut Vec<(String, Value)>, clears: &mut Vec<&'a str>) {
10019        match self {
10020            Self::Set { field, text } => values.push((field.clone(), json!({"text": text}))),
10021            Self::Clear { field } => clears.push(field),
10022        }
10023    }
10024}
10025
10026/// What one write does to an item's `Priority` field.
10027enum PriorityWrite {
10028    /// Select this option of this field.
10029    Select {
10030        /// The `Priority` field's id.
10031        field: String,
10032        /// The mapped option's id.
10033        option: String,
10034    },
10035    /// Clear the field's value, which is what `none` is.
10036    Clear {
10037        /// The `Priority` field's id.
10038        field: String,
10039    },
10040}
10041
10042impl Incoming<'_> {
10043    /// The title this write puts on the issue.
10044    fn written_title(&self) -> String {
10045        match self.written {
10046            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
10047            Written::Work(..) => self.title.to_owned(),
10048        }
10049    }
10050}
10051
10052#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
10053enum ContentKind {
10054    DraftIssue,
10055    Issue,
10056}
10057
10058/// What one board issue is: a document, or the work an [`ItemKind`] names.
10059///
10060/// A type of this source's own rather than an `ItemKind` with a third variant, because
10061/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
10062/// document — the contract keeps a document out of that enum deliberately. Holding the
10063/// board's three answers in one value is what makes every place that asks "which is this?"
10064/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
10065/// two thirds of the board.
10066#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
10067enum BoardKind {
10068    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
10069    Document,
10070    /// Every other issue, and every draft.
10071    Work(ItemKind),
10072}
10073
10074impl BoardKind {
10075    /// The kind of an item titled `title`, filed under a project or not, holding `sub_issues`
10076    /// sub-issues and carrying the kind marker `marked` in its slot.
10077    ///
10078    /// The design prefix is read *first*, before either of the two rules that separate a
10079    /// project from a task. A document is not work whatever sub-issues it has and whatever
10080    /// marker it carries, and reading the prefix later would make a design issue with none of
10081    /// either an empty project. Being a sub-issue wins outright over the rest, and no marker
10082    /// overrides it: an issue filed under a project is that project's task even when it has
10083    /// sub-issues of its own.
10084    fn of(title: &str, filed: bool, sub_issues: u64, marked: Option<ItemKind>) -> Self {
10085        if title.starts_with(DESIGN_TITLE_PREFIX) {
10086            Self::Document
10087        } else if filed {
10088            Self::Work(ItemKind::Task)
10089        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
10090            Self::Work(ItemKind::Project)
10091        } else {
10092            Self::Work(ItemKind::Task)
10093        }
10094    }
10095
10096    /// What the change token's facets call this kind.
10097    const fn facet(self) -> change_token::FacetKind {
10098        match self {
10099            Self::Document => change_token::FacetKind::Document,
10100            Self::Work(ItemKind::Project) => change_token::FacetKind::Project,
10101            Self::Work(ItemKind::Task) => change_token::FacetKind::Task,
10102        }
10103    }
10104
10105    /// Whose half of `status_mapping` an item of this kind reads its status through. A
10106    /// document has no status of its own, so the task half stands in for whatever the issue
10107    /// holds; nothing reports it.
10108    const fn status_kind(self) -> ItemKind {
10109        match self {
10110            Self::Document => ItemKind::Task,
10111            Self::Work(kind) => kind,
10112        }
10113    }
10114
10115    /// How a refusal names this kind to the person reading it.
10116    const fn describes(self) -> &'static str {
10117        match self {
10118            Self::Document => "document",
10119            Self::Work(kind) => kind.marker(),
10120        }
10121    }
10122}
10123
10124/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
10125///
10126/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
10127/// the shared cross-source journeys assert one answer to one question, so two sources
10128/// that disagree about what "carries the label bug" means fail them.
10129fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
10130    let holds = |name: &String| {
10131        labels
10132            .iter()
10133            .any(|label| label.name.eq_ignore_ascii_case(name))
10134    };
10135    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
10136        && filter.all_of.iter().all(holds)
10137        && !filter.none_of.iter().any(holds)
10138}
10139
10140/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
10141/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
10142fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
10143    statuses.is_empty() || statuses.contains(&category)
10144}
10145
10146/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
10147///
10148/// `content` is the item's own prose — the body with this source's trailing metadata
10149/// comment already taken off — so a search never matches an encoding the author of the
10150/// issue never wrote.
10151fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
10152    let terms = query.terms.to_lowercase();
10153    let in_title = title.to_lowercase().contains(&terms);
10154    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
10155    match query.fields {
10156        TextFields::Title => in_title,
10157        TextFields::Content => in_content,
10158        TextFields::TitleOrContent => in_title || in_content,
10159    }
10160}
10161
10162/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
10163///
10164/// The project predicate is passed separately because a read narrowed to one project has
10165/// already answered it by asking *that project* for its own items — and re-applying it
10166/// there would compare the caller's selector, which may be a project's **name**, against
10167/// the id of the project that name resolved to, and keep nothing. Every other read passes
10168/// `query.project` and applies it here, which is what keeps `projects` a predicate this
10169/// source really does apply.
10170fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
10171    labels_match(&task.labels, &query.labels)
10172        && status_matches(task.status.category, &query.statuses)
10173        && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
10174        && match project {
10175            ProjectFilter::Any => true,
10176            ProjectFilter::Orphans => task.project.is_none(),
10177            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
10178        }
10179        && query
10180            .text
10181            .as_ref()
10182            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
10183        // Against the parsed metadata slot, and against the origin field, which is where
10184        // `Resolved::metadata` reads each of them from.
10185        && query.metadata_matches(&task.metadata)
10186        && query.origin_matches(&task.metadata)
10187}
10188
10189fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
10190    labels_match(&project.labels, &query.labels)
10191        && status_matches(project.status.category, &query.statuses)
10192        && query
10193            .text
10194            .as_ref()
10195            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
10196}
10197
10198/// The same three predicates a task query carries, minus the status filter.
10199///
10200/// A document is not work, so it has no status for one to compare against and the query
10201/// type carries none. The project predicate is the same one — a design issue filed under a
10202/// project issue is in that project, and one filed under nothing is in none — so it is
10203/// spelled the same way here rather than answered differently.
10204fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
10205    labels_match(&document.labels, &query.labels)
10206        && match project {
10207            ProjectFilter::Any => true,
10208            ProjectFilter::Orphans => document.project.is_none(),
10209            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
10210        }
10211        && query
10212            .text
10213            .as_ref()
10214            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
10215}
10216
10217/// Why a GitHub item's change token does not vouch for its forward edges.
10218///
10219/// A forward edge names its far end and that far end's kind, and the near item's token moves
10220/// with the edges it holds but says nothing of the far ends themselves: a far end turning from a
10221/// task into a project when it gains a sub-issue, retitled into a document, or deleted moves
10222/// nothing the near token reads, so an edge kept at the near version could name a far end that
10223/// is no longer what it said.
10224const FAR_ENDS_UNCOVERED: &str = "a github item's change token does not attest a far end's kind, \
10225     whether it became a document, or its deletion, so its forward edges are always read from \
10226     github";
10227
10228#[async_trait::async_trait]
10229impl TaskSource for GitHubProjectsSource {
10230    fn kind(&self) -> &'static str {
10231        KIND
10232    }
10233    fn order_versions(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
10234        change_token::order(a, b)
10235    }
10236    /// A task's fields and comments: §4.13d of `docs/plugin-protocol.md` states, member by
10237    /// member, the change each part of the token moves for. Not its forward edges — see
10238    /// [`FAR_ENDS_UNCOVERED`].
10239    fn version_coverage(&self) -> VersionCoverage {
10240        VersionCoverage {
10241            forward_edges: Coverage::Uncovered {
10242                reason: FAR_ENDS_UNCOVERED.to_owned(),
10243            },
10244            ..VersionCoverage::whole()
10245        }
10246    }
10247    /// Every part, for a copy's skip: the relationship count moves for an edge added or removed
10248    /// and for a far end deleted, so the token sees every change to a task's edges but one — a
10249    /// far end's own kind flipping, by gaining a sub-issue or being retitled into a document,
10250    /// which `spike-coverage` measured stale 1 of 1. A skipping recopy does not carry that, and
10251    /// the next copy that reads the task does (`docs/plugin-protocol.md` §4.13d).
10252    // llmlint: ignore[names_match_behavior] The manager's ruling (c-d7838f2c) keeps 0.6.0's GitHub skip and has this plugin state that bounded claim through the coverage it reports, with the bound documented here, in §4.13d and by the copy_skip journey; a separate bounded representation would be a contract change beyond a defaulted addition, which the ruling reserves for the contract's owner, and a follow-up is drafted for a far-end-aware skip.
10253    fn copy_version_coverage(&self) -> VersionCoverage {
10254        VersionCoverage::whole()
10255    }
10256    /// A project's fields, and not its forward edges, for the same reason a task's are not.
10257    fn project_version_coverage(&self) -> ProjectVersionCoverage {
10258        ProjectVersionCoverage {
10259            fields: Coverage::Covered,
10260            forward_edges: Coverage::Uncovered {
10261                reason: FAR_ENDS_UNCOVERED.to_owned(),
10262            },
10263        }
10264    }
10265    /// A document's fields and the manifest of its image assets, which its metadata slot
10266    /// records: both are the issue's body, whose every edit moves the token's issue time.
10267    fn document_version_coverage(&self) -> DocumentVersionCoverage {
10268        DocumentVersionCoverage::whole()
10269    }
10270    fn write_context(&self, id: &NativeId) -> Option<Value> {
10271        self.context_of(id)
10272    }
10273    fn board_context(&self) -> Option<Value> {
10274        self.board_context_held()
10275    }
10276    fn prime_write(&self, id: &NativeId, context: &Value, board: Option<&Value>) -> bool {
10277        self.prime(id, context, board)
10278    }
10279    fn unprime(&self, id: &NativeId) {
10280        self.unprime_held(id);
10281    }
10282    fn begin_journal(&self) {
10283        self.expected.begin();
10284    }
10285    fn end_journal(&self) -> Option<WriteJournal> {
10286        Some(self.expected.end())
10287    }
10288    fn capabilities(&self) -> Capabilities {
10289        Capabilities {
10290            projects: Support::Native,
10291            documents: Support::Native,
10292            comments: Support::Native,
10293            assets: Support::Native,
10294            priority: if self.priorities.is_some() {
10295                Support::Native
10296            } else {
10297                Support::Unsupported
10298            },
10299            filter_by_priority: Support::Native,
10300            filter_by_comment_activity: Support::Native,
10301            filter_by_metadata: Support::Native,
10302            filter_by_origin: Support::Native,
10303            orphan_tasks: Support::Native,
10304            filter_by_label: Support::Native,
10305            filter_by_status: Support::Native,
10306            search_title: Support::Native,
10307            search_content: Support::Native,
10308            task_dependencies: DependencySupport::BothDirections,
10309            project_dependencies: DependencySupport::BothDirections,
10310            max_page_size: MAX_PAGE_SIZE,
10311        }
10312    }
10313    async fn visibility(
10314        &self,
10315        target: &onetaskgraph_plugin_api::WriteTarget<'_>,
10316    ) -> Result<onetaskgraph_plugin_api::Visibility, SourceError> {
10317        self.write_visibility(target).await
10318    }
10319    async fn health(&self) -> Result<Health, SourceError> {
10320        let board = self.board_page(None, 1).await?;
10321        Ok(Health {
10322            reachable: true,
10323            detail: Some(format!(
10324                "reading GitHub project {}/{} ({})",
10325                self.owner,
10326                self.project_number,
10327                required_str(&board, "title")?
10328            )),
10329        })
10330    }
10331    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
10332        self.item_by_id(id)
10333            .await?
10334            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
10335            .map(|item| item.task())
10336            .transpose()
10337    }
10338    async fn task_assets(
10339        &self,
10340        id: &NativeId,
10341    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
10342        self.held_assets(id, BoardKind::Work(ItemKind::Task)).await
10343    }
10344    async fn task_asset(
10345        &self,
10346        id: &NativeId,
10347        name: &onetaskgraph_plugin_api::AssetName,
10348    ) -> Result<Option<Vec<u8>>, SourceError> {
10349        self.held_asset(id, BoardKind::Work(ItemKind::Task), name)
10350            .await
10351    }
10352    async fn set_task_rendering_with_assets(
10353        &self,
10354        id: &NativeId,
10355        content: &str,
10356        provenance: &Value,
10357        _answers: &BTreeMap<String, Value>,
10358        assets: &onetaskgraph_plugin_api::AssetWrite,
10359    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
10360        self.replace_rendering(
10361            id,
10362            BoardKind::Work(ItemKind::Task),
10363            content,
10364            provenance,
10365            Some(assets),
10366        )
10367        .await
10368    }
10369    async fn document_assets(
10370        &self,
10371        id: &NativeId,
10372    ) -> Result<Vec<onetaskgraph_plugin_api::Asset>, SourceError> {
10373        self.held_assets(id, BoardKind::Document).await
10374    }
10375    async fn document_asset(
10376        &self,
10377        id: &NativeId,
10378        name: &onetaskgraph_plugin_api::AssetName,
10379    ) -> Result<Option<Vec<u8>>, SourceError> {
10380        self.held_asset(id, BoardKind::Document, name).await
10381    }
10382    async fn set_document_rendering_with_assets(
10383        &self,
10384        id: &NativeId,
10385        content: &str,
10386        provenance: &Value,
10387        _answers: &BTreeMap<String, Value>,
10388        assets: &onetaskgraph_plugin_api::AssetWrite,
10389    ) -> Result<Option<onetaskgraph_plugin_api::AssetsWritten>, SourceError> {
10390        self.replace_rendering(id, BoardKind::Document, content, provenance, Some(assets))
10391            .await
10392    }
10393    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
10394        Ok(self
10395            .item_by_id(id)
10396            .await?
10397            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
10398            .map(|item| item.project()))
10399    }
10400    async fn query_tasks(
10401        &self,
10402        query: &TaskQuery,
10403        page: &PageRequest,
10404    ) -> Result<Page<Task>, SourceError> {
10405        validate_page(page)?;
10406        refuse_unsearchable(query)?;
10407        if query.origin.is_none() && !matches!(query.project, ProjectFilter::Is(_)) {
10408            let qualifiers = match (narrowing_qualifiers(query), query.commented_since) {
10409                (Some(also), Some(since)) => Some(format!("{} {also}", updated_qualifier(since))),
10410                (Some(also), None) => Some(also),
10411                (None, Some(since)) => Some(updated_qualifier(since)),
10412                (None, None) => None,
10413            };
10414            if let Some(also) = qualifiers {
10415                return self.search_tasks(query, page, &also).await;
10416            }
10417        }
10418
10419        // A read narrowed to one project asks that project for its own tasks, so nothing
10420        // about it costs what the rest of the board holds. A read carrying a text, metadata
10421        // or origin predicate asks GitHub the narrower question those predicates are, and a
10422        // read narrowed to comment activity alone asks the board's own issue search for the
10423        // issues updated since, which is every issue a comment could have been written or
10424        // edited on since. Every other task read is a question about the whole board and is
10425        // answered by reading it.
10426        let (held, membership) = match (&query.project, query.commented_since) {
10427            (ProjectFilter::Is(project), _) => (
10428                self.project_children(project).await?,
10429                // Answered by where these items came from; see `task_matches`.
10430                &ProjectFilter::Any,
10431            ),
10432            (ProjectFilter::Any | ProjectFilter::Orphans, since) => {
10433                match (self.narrowed(query).await?, since) {
10434                    (Some(narrowed), _) => (narrowed, &query.project),
10435                    (None, Some(since)) => (self.updated_since(since).await?, &query.project),
10436                    (None, None) => (self.board().await?.items, &query.project),
10437                }
10438            }
10439        };
10440        // Filtered before paged: a page of a filtered result is a page of the survivors,
10441        // never the survivors of a page.
10442        let mut tasks = Vec::new();
10443        for item in held
10444            .iter()
10445            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
10446        {
10447            let task = item.task()?;
10448            if task_matches(&task, query, membership)
10449                && self.commented_since(item, query.commented_since).await?
10450            {
10451                tasks.push(task);
10452            }
10453        }
10454        Ok(offset_page(
10455            tasks,
10456            numeric_cursor(page.cursor.as_ref())?,
10457            page.limit.min(MAX_PAGE_SIZE) as usize,
10458        ))
10459    }
10460    async fn query_projects(
10461        &self,
10462        query: &ProjectQuery,
10463        page: &PageRequest,
10464    ) -> Result<Page<Project>, SourceError> {
10465        validate_page(page)?;
10466        refuse_unsearchable_text(query.text.as_ref())?;
10467        // The projects a board holds are found by an issue search scoped to that board,
10468        // never by walking the board's own item connection: what tells a project from a
10469        // task is the `parent` each issue carries, which costs nothing to read. A query
10470        // carrying a text asks that search for the text too, so it reads the issues that
10471        // hold it rather than every issue of the board.
10472        let held = match self.text_searched(query.text.as_ref()).await? {
10473            Some(searched) => searched,
10474            None => self.board_issues().await?,
10475        };
10476        let projects = held
10477            .iter()
10478            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
10479            .map(Resolved::project)
10480            .filter(|project| project_matches(project, query))
10481            .collect();
10482        Ok(offset_page(
10483            projects,
10484            numeric_cursor(page.cursor.as_ref())?,
10485            page.limit.min(MAX_PAGE_SIZE) as usize,
10486        ))
10487    }
10488    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
10489        Ok(self
10490            .item_by_id(id)
10491            .await?
10492            .filter(|item| item.kind == BoardKind::Document)
10493            .map(|item| item.document()))
10494    }
10495    async fn query_documents(
10496        &self,
10497        query: &DocumentQuery,
10498        page: &PageRequest,
10499    ) -> Result<Page<Document>, SourceError> {
10500        validate_page(page)?;
10501        // Narrowed to one project, this is the same sub-issue read a task list scoped to
10502        // that project makes — a document filed under a project is a sub-issue of it too,
10503        // and which of them come back is the kind this caller asked for. Unscoped, a query
10504        // carrying a text asks the board-scoped issue search for it, as a task query does,
10505        // and only one carrying none reads the board.
10506        let (held, membership) = match &query.project {
10507            ProjectFilter::Is(project) => (
10508                self.project_children(project).await?,
10509                // Answered by where these items came from; see `task_matches`.
10510                &ProjectFilter::Any,
10511            ),
10512            ProjectFilter::Any | ProjectFilter::Orphans => {
10513                refuse_unsearchable_text(query.text.as_ref())?;
10514                match self.text_searched(query.text.as_ref()).await? {
10515                    Some(searched) => (searched, &query.project),
10516                    None => (self.board().await?.items, &query.project),
10517                }
10518            }
10519        };
10520        // Filtered before paged, exactly as a task read is: a page of a filtered result is
10521        // a page of the survivors, never the survivors of a page.
10522        let documents = held
10523            .iter()
10524            .filter(|item| item.kind == BoardKind::Document)
10525            .map(Resolved::document)
10526            .filter(|document| document_matches(document, query, membership))
10527            .collect();
10528        Ok(offset_page(
10529            documents,
10530            numeric_cursor(page.cursor.as_ref())?,
10531            page.limit.min(MAX_PAGE_SIZE) as usize,
10532        ))
10533    }
10534    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
10535        validate_page(page)?;
10536        let offset = numeric_cursor(page.cursor.as_ref())?;
10537        let mut labels = self
10538            .board()
10539            .await?
10540            .items
10541            .into_iter()
10542            .flat_map(|item| item.labels)
10543            .fold(Vec::new(), |mut all, label| {
10544                if !all.iter().any(|x: &Label| x.id == label.id) {
10545                    all.push(label);
10546                }
10547                all
10548            });
10549        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
10550        Ok(offset_page(
10551            labels,
10552            offset,
10553            page.limit.min(MAX_PAGE_SIZE) as usize,
10554        ))
10555    }
10556    async fn task_dependencies(
10557        &self,
10558        id: &NativeId,
10559        direction: Direction,
10560        page: &PageRequest,
10561    ) -> Result<Page<DependencyEdge>, SourceError> {
10562        self.dependencies(id, ItemKind::Task, direction, page).await
10563    }
10564    async fn project_dependencies(
10565        &self,
10566        id: &NativeId,
10567        direction: Direction,
10568        page: &PageRequest,
10569    ) -> Result<Page<DependencyEdge>, SourceError> {
10570        self.dependencies(id, ItemKind::Project, direction, page)
10571            .await
10572    }
10573
10574    fn writes(&self) -> WriteSupport {
10575        WriteSupport::Supported
10576    }
10577
10578    /// Create or update one task.
10579    ///
10580    /// Its `delivers` and `delivered_by` are checked before anything is read or written —
10581    /// neither may name the task itself or name one task twice — and land in the body's
10582    /// metadata slot under their reserved keys, in place of any caller metadata of those
10583    /// names.
10584    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
10585        self.write_task_assets(write, None)
10586            .await
10587            .map(|written| written.id)
10588    }
10589
10590    async fn write_task_with_assets(
10591        &self,
10592        write: &ItemWrite<Task>,
10593        _answers: Option<&BTreeMap<String, Value>>,
10594        assets: &onetaskgraph_plugin_api::AssetWrite,
10595    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
10596        self.write_task_assets(write, Some(assets)).await
10597    }
10598
10599    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
10600        self.write_item(
10601            &Incoming {
10602                written: Written::Work(ItemKind::Project, &write.item.status),
10603                title: &write.item.title,
10604                content: write.item.content.as_deref(),
10605                assets: None,
10606                labels: &write.item.labels,
10607                metadata: &write.item.metadata,
10608                repositories: &write.item.repositories,
10609                classification: write.item.classification,
10610                parent: None,
10611                delivers: &[],
10612                delivered_by: &[],
10613                priority: None,
10614            },
10615            write.target.as_ref(),
10616            &write.depends_on,
10617        )
10618        .await
10619        .map(|written| written.id)
10620    }
10621
10622    /// Create or update one document, which is one issue titled the way this board spells
10623    /// a document.
10624    ///
10625    /// Everything else is exactly a task write: caller metadata goes to the same canonical
10626    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
10627    /// or a field this board cannot carry is refused by name rather than dropped, a target
10628    /// naming an issue this board does not hold is refused rather than created, and an
10629    /// issue this call created is taken back when the rest of the write fails.
10630    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
10631        self.write_document_assets(write, None)
10632            .await
10633            .map(|written| written.id)
10634    }
10635
10636    async fn write_document_with_assets(
10637        &self,
10638        write: &ItemWrite<Document>,
10639        _answers: Option<&BTreeMap<String, Value>>,
10640        assets: &onetaskgraph_plugin_api::AssetWrite,
10641    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
10642        self.write_document_assets(write, Some(assets)).await
10643    }
10644
10645    /// Refused exactly as the write refuses it, from what the write reads: the mapping first,
10646    /// which reads nothing; then the board's `Status` option. Over an existing item that is
10647    /// read off the item, as the write reads it, and the item is held among this command's
10648    /// resolved records so the write that follows reuses that read rather than repeating it;
10649    /// an item that does not carry the field takes the board's fields, which are held once
10650    /// read. A create is checked against the board's fields only when this command already
10651    /// holds them, because a create reads them together with its repository, in one request,
10652    /// and refuses a missing option before it writes anything.
10653    async fn check_status_write(
10654        &self,
10655        kind: ItemKind,
10656        category: StatusCategory,
10657        target: Option<&NativeId>,
10658    ) -> Result<(), SourceError> {
10659        let status = self.resolved_target(kind, category)?;
10660        if status.option().is_none() {
10661            return Ok(());
10662        }
10663        let fields = match target {
10664            Some(target) => {
10665                // A target this board does not hold is the write's own refusal to make.
10666                let Some(item) = self.bound_item(target).await? else {
10667                    return Ok(());
10668                };
10669                self.resolved_cache()?.insert(target.clone(), item.clone());
10670                self.fields_for(Some(&item), true, false).await?.fields
10671            }
10672            None => {
10673                let held = self
10674                    .board_cache()?
10675                    .as_ref()
10676                    .map(|board| board.fields.clone());
10677                match held.or_else(|| {
10678                    self.fields_cache()
10679                        .ok()
10680                        .and_then(|cache| cache.as_ref().map(|board| board.fields.clone()))
10681                }) {
10682                    Some(fields) => fields,
10683                    None => return Ok(()),
10684                }
10685            }
10686        };
10687        self.column_for(&fields, kind, category, &status)
10688            .map(|_| ())
10689    }
10690
10691    /// Set one task's status alone.
10692    ///
10693    /// An open target reopens a closed issue with an `updateIssue` carrying only its
10694    /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
10695    /// terminal target selects its mapped option, then closes with its fixed reason. No
10696    /// request carries a title, a body or a label. The status
10697    /// answered is what [`BoardStatuses::status`] reads off the state just written, which is
10698    /// what a re-read reports.
10699    async fn set_task_status(
10700        &self,
10701        id: &NativeId,
10702        category: StatusCategory,
10703    ) -> Result<Option<Status>, SourceError> {
10704        self.set_status(id, category).await
10705    }
10706
10707    /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
10708    /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
10709    /// for `none`. Refused by an instance with no `priority_mapping`.
10710    async fn set_task_priority(
10711        &self,
10712        id: &NativeId,
10713        priority: Priority,
10714    ) -> Result<Option<Priority>, SourceError> {
10715        self.set_priority(id, priority).await
10716    }
10717
10718    /// Replace one task's content with a single body update that keeps the metadata slot
10719    /// byte for byte.
10720    async fn set_task_content(
10721        &self,
10722        id: &NativeId,
10723        content: &str,
10724    ) -> Result<Option<()>, SourceError> {
10725        self.replace_content(id, content).await
10726    }
10727
10728    /// Replace one task issue's content and its provenance slot entry with a single body
10729    /// update. The answers are not kept: see `replace_rendering`.
10730    async fn set_task_rendering(
10731        &self,
10732        id: &NativeId,
10733        content: &str,
10734        provenance: &Value,
10735        _answers: &BTreeMap<String, Value>,
10736    ) -> Result<Option<()>, SourceError> {
10737        self.replace_rendering(
10738            id,
10739            BoardKind::Work(ItemKind::Task),
10740            content,
10741            provenance,
10742            None,
10743        )
10744        .await
10745        .map(|written| written.map(|_| ()))
10746    }
10747
10748    /// Replace one design-document issue's content and its provenance slot entry, on exactly
10749    /// the terms of [`set_task_rendering`](TaskSource::set_task_rendering).
10750    async fn set_document_rendering(
10751        &self,
10752        id: &NativeId,
10753        content: &str,
10754        provenance: &Value,
10755        _answers: &BTreeMap<String, Value>,
10756    ) -> Result<Option<()>, SourceError> {
10757        self.replace_rendering(id, BoardKind::Document, content, provenance, None)
10758            .await
10759            .map(|written| written.map(|_| ()))
10760    }
10761
10762    /// Replace one project issue's content and its provenance slot entry, on exactly the
10763    /// terms of [`set_task_rendering`](TaskSource::set_task_rendering).
10764    async fn set_project_rendering(
10765        &self,
10766        id: &NativeId,
10767        content: &str,
10768        provenance: &Value,
10769        _answers: &BTreeMap<String, Value>,
10770    ) -> Result<Option<()>, SourceError> {
10771        self.replace_rendering(
10772            id,
10773            BoardKind::Work(ItemKind::Project),
10774            content,
10775            provenance,
10776            None,
10777        )
10778        .await
10779        .map(|written| written.map(|_| ()))
10780    }
10781
10782    /// Apply a targeted update with one read of the item and a write only for what differs:
10783    /// the `Status` and `Priority` field writes in one request, the `blockedBy` difference,
10784    /// and last one `updateIssue` for title, body and state. See `targeted_update`.
10785    async fn update_task(
10786        &self,
10787        id: &NativeId,
10788        update: &TaskUpdate,
10789    ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
10790        self.targeted_update(id, update).await
10791    }
10792
10793    /// Replace one task's `delivered_by` with a single body update that changes the
10794    /// metadata slot and nothing outside it.
10795    async fn set_delivered_by(
10796        &self,
10797        id: &NativeId,
10798        delivered_by: &[TaskRef],
10799    ) -> Result<Option<()>, SourceError> {
10800        self.replace_delivered_by(id, delivered_by).await
10801    }
10802
10803    /// Set one key of one task issue's metadata with a single body update that changes the
10804    /// metadata slot and nothing outside it — no title, label, state or board field request —
10805    /// and sends nothing when the task already holds that value under the key.
10806    async fn set_task_metadata(
10807        &self,
10808        id: &NativeId,
10809        key: &MetadataKey,
10810        value: &Value,
10811    ) -> Result<Option<Task>, SourceError> {
10812        Ok(self
10813            .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
10814            .await?
10815            .map(|item| item.task())
10816            .transpose()?)
10817    }
10818
10819    /// Set one key of one project issue's metadata, on exactly the terms of
10820    /// [`set_task_metadata`](TaskSource::set_task_metadata).
10821    async fn set_project_metadata(
10822        &self,
10823        id: &NativeId,
10824        key: &MetadataKey,
10825        value: &Value,
10826    ) -> Result<Option<Project>, SourceError> {
10827        Ok(self
10828            .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
10829            .await?
10830            .map(|item| item.project()))
10831    }
10832
10833    /// Set one key of one design-document issue's metadata, on exactly the terms of
10834    /// [`set_task_metadata`](TaskSource::set_task_metadata).
10835    async fn set_document_metadata(
10836        &self,
10837        id: &NativeId,
10838        key: &MetadataKey,
10839        value: &Value,
10840    ) -> Result<Option<Document>, SourceError> {
10841        Ok(self
10842            .set_slot_key(id, BoardKind::Document, key, value)
10843            .await?
10844            .map(|item| item.document()))
10845    }
10846
10847    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
10848        self.delete_item(id).await
10849    }
10850
10851    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
10852        self.delete_item(id).await
10853    }
10854
10855    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
10856        self.delete_item(id).await
10857    }
10858
10859    /// One page of the task issue's own comments, walked by GitHub's own cursor.
10860    ///
10861    /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
10862    /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
10863    ///
10864    /// One request, [`graphql::ISSUE_DETAIL`]: the read that says the id names a task of this
10865    /// board is the read of its comments. A draft this process already resolved is refused
10866    /// without one.
10867    async fn task_comments(
10868        &self,
10869        task: &NativeId,
10870        page: &PageRequest,
10871    ) -> Result<Option<Page<Comment>>, SourceError> {
10872        validate_page(page)?;
10873        let cached = self.resolved_cache()?.get(task).cloned();
10874        if let Some(item) = cached {
10875            if item.kind != BoardKind::Work(ItemKind::Task) {
10876                return Ok(None);
10877            }
10878            if item.content_kind == ContentKind::DraftIssue {
10879                return Err(self.draft_has_no_comments(task));
10880            }
10881        }
10882        match self.issue_detail(task, page).await? {
10883            Some(TaskDetailRead {
10884                comments: Some(comments),
10885                ..
10886            }) => comments,
10887            _ => Ok(None),
10888        }
10889    }
10890
10891    /// Every id's task, with the first page of its comments when `comments` names it:
10892    /// [`DETAIL_BATCH`] items per [`graphql::ISSUE_DETAILS`] request, and one item with its
10893    /// comments in one [`graphql::ISSUE_DETAIL`] request.
10894    async fn get_task_details(
10895        &self,
10896        ids: &[NativeId],
10897        comments: Option<&PageRequest>,
10898    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10899        if let Some(page) = comments
10900            && let Err(error) = validate_page(page)
10901        {
10902            return ids.iter().map(|_| Err(error.clone())).collect();
10903        }
10904        match (ids, comments) {
10905            ([id], Some(page)) => vec![self.issue_detail(id, page).await],
10906            ([id], None) => vec![self.task_read(id).await],
10907            _ => self.issue_details(ids, comments).await,
10908        }
10909    }
10910
10911    /// Every id's task with the first page of its comments and its `blockedBy`, as a copy reads
10912    /// the tasks it carries: [`COPY_DETAIL_BATCH`] items per [`graphql::ISSUE_COPY_DETAILS`]
10913    /// request, so the dependency read a copy makes of each next is answered from this one.
10914    async fn get_task_details_for_copy(
10915        &self,
10916        ids: &[NativeId],
10917        comments: &PageRequest,
10918    ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
10919        // A page past the first is a page of one task's comments, which the detail read of it
10920        // alone answers.
10921        if comments.cursor.is_some() {
10922            return self.get_task_details(ids, Some(comments)).await;
10923        }
10924        if let Err(error) = validate_page(comments) {
10925            return ids.iter().map(|_| Err(error.clone())).collect();
10926        }
10927        let mut read = Vec::with_capacity(ids.len());
10928        for batch in ids.chunks(COPY_DETAIL_BATCH) {
10929            match self
10930                .graphql(
10931                    graphql::ISSUE_COPY_DETAILS,
10932                    copy_detail_batch(batch, comments),
10933                )
10934                .await
10935            {
10936                Ok(data) => {
10937                    for (slot, id) in batch.iter().enumerate() {
10938                        let read_one = match data.get(format!("i{slot}")) {
10939                            Some(node) => self.detail_of(id, node, true, None).await,
10940                            None => Err(SourceError::Malformed {
10941                                message: format!(
10942                                    "GitHub answered a batch read with no item for {}",
10943                                    id.0
10944                                ),
10945                            }),
10946                        };
10947                        read.push(read_one);
10948                    }
10949                }
10950                Err(error) if unresolvable_node(&error) => {
10951                    for id in batch {
10952                        read.push(self.issue_detail(id, comments).await);
10953                    }
10954                }
10955                Err(error) => read.extend(batch.iter().map(|_| Err(error.clone()))),
10956            }
10957        }
10958        read
10959    }
10960
10961    /// Add one comment to the task's issue, as the account the token belongs to.
10962    ///
10963    /// The author is refused before anything is sent — not even the task is read — because
10964    /// no answer GitHub could give would make posting under another name than the one asked
10965    /// for the right outcome.
10966    async fn add_comment(
10967        &self,
10968        task: &NativeId,
10969        comment: &NewComment,
10970    ) -> Result<Option<Comment>, SourceError> {
10971        if let Some(author) = &comment.author {
10972            return Err(SourceError::Refused {
10973                message: format!(
10974                    "source {} cannot post a comment as {author:?}: GitHub records the account \
10975                     the token signs in as the author of every comment; next: leave --author \
10976                     out, and the comment is posted as that account",
10977                    self.name
10978                ),
10979            });
10980        }
10981        let Some(issue) = self.commented_issue(task).await? else {
10982            return Ok(None);
10983        };
10984        let data = self
10985            .graphql(
10986                graphql::ADD_COMMENT,
10987                json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
10988            )
10989            .await?;
10990        let subject = data
10991            .pointer("/addComment/subject")
10992            .filter(|value| !value.is_null())
10993            .ok_or_else(|| SourceError::Malformed {
10994                message: "GitHub comment addition returned no subject".into(),
10995            })?;
10996        if required_str(subject, "id")? != issue.0 {
10997            return Err(SourceError::Malformed {
10998                message: "GitHub comment addition answered about another issue".into(),
10999            });
11000        }
11001        let added = data
11002            .pointer("/addComment/commentEdge/node")
11003            .filter(|value| !value.is_null())
11004            .ok_or_else(|| SourceError::Malformed {
11005                message: "GitHub comment addition returned no comment".into(),
11006            })?;
11007        let added = comment_from(added)?;
11008        self.remember_commented(&issue)?;
11009        Ok(Some(added))
11010    }
11011
11012    async fn edit_comment(
11013        &self,
11014        task: &NativeId,
11015        comment: &NativeId,
11016        body: &CommentBody,
11017    ) -> Result<Option<Comment>, SourceError> {
11018        let Some(issue) = self.commented_issue(task).await? else {
11019            return Ok(None);
11020        };
11021        let Some(held) = self.comment_on(&issue, comment).await? else {
11022            return Ok(None);
11023        };
11024        // A comment a copy wrote keeps its origin through an edit: its marker is carried over.
11025        let body = CommentOrigin::kept(&held, body)?;
11026        let data = self
11027            .graphql(
11028                graphql::UPDATE_COMMENT,
11029                json!({"input":{"id":comment.0,"body":body.as_str()}}),
11030            )
11031            .await?;
11032        let edited = data
11033            .pointer("/updateIssueComment/issueComment")
11034            .filter(|value| !value.is_null())
11035            .ok_or_else(|| SourceError::Malformed {
11036                message: "GitHub comment update returned no comment".into(),
11037            })?;
11038        let edited = comment_from(edited)?;
11039        if edited.id != *comment {
11040            return Err(SourceError::Malformed {
11041                message: "GitHub comment update returned the wrong comment".into(),
11042            });
11043        }
11044        self.remember_commented(&issue)?;
11045        Ok(Some(edited))
11046    }
11047
11048    async fn delete_comment(
11049        &self,
11050        task: &NativeId,
11051        comment: &NativeId,
11052    ) -> Result<Option<NativeId>, SourceError> {
11053        let Some(issue) = self.commented_issue(task).await? else {
11054            return Ok(None);
11055        };
11056        if !self.comment_is_on(&issue, comment).await? {
11057            return Ok(None);
11058        }
11059        let data = self
11060            .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
11061            .await?;
11062        // The payload says nothing about the comment it removed, so what is checked is that
11063        // GitHub answered the mutation at all rather than leaving it unanswered.
11064        data.get("deleteIssueComment")
11065            .filter(|value| !value.is_null())
11066            .ok_or_else(|| SourceError::Malformed {
11067                message: "GitHub comment deletion returned no payload".into(),
11068            })?;
11069        Ok(Some(comment.clone()))
11070    }
11071
11072    /// Every request this source has recorded, and what each of GitHub's two budgets was
11073    /// attributed — read off the same accounting the session report is rendered from, so
11074    /// the two cannot count one request two ways.
11075    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
11076        Ok(Some(self.ledger.snapshot().metering()))
11077    }
11078
11079    /// Drop every item, search answer and board read this source holds, so the next command
11080    /// reads the board as a person has since left it.
11081    ///
11082    /// Every one of those is held on the assumption that nothing but this source writes the
11083    /// board while a command runs, which stops being true the moment the command is over: a
11084    /// body a person edited would be overwritten from the record held here, and a card they
11085    /// moved would be read as still where this source left it. The board's own field
11086    /// definitions go too, because a person can add or delete a `Status` option and a write
11087    /// resolved against the held list would not re-read on a miss. What stays is what stays
11088    /// valid in normal use: each repository's node id, which a miss re-reads, the pacing of
11089    /// mutations, which is about GitHub's limiter rather than anybody's work, and the running
11090    /// accounting [`metering`](TaskSource::metering) answers from.
11091    ///
11092    /// Infallible in practice: a lock an earlier failure poisoned is cleared rather than
11093    /// refused, because clearing it is what puts it right.
11094    async fn end_command(&self) -> Result<(), SourceError> {
11095        fn clear<T: Default>(held: &Mutex<T>) {
11096            *held
11097                .lock()
11098                .unwrap_or_else(std::sync::PoisonError::into_inner) = T::default();
11099            held.clear_poison();
11100        }
11101        clear(&self.created);
11102        clear(&self.updated);
11103        clear(&self.commented);
11104        clear(&self.board_cache);
11105        clear(&self.search_cache);
11106        clear(&self.narrowed_cache);
11107        clear(&self.search_next);
11108        clear(&self.resolved_cache);
11109        clear(&self.children_cache);
11110        clear(&self.fields_cache);
11111        self.expected.end_command();
11112        Ok(())
11113    }
11114}
11115
11116/// Each project's sub-issues as one command read them, keyed by the selector they were asked
11117/// for under, beside the project that selector named; see `children_cache`.
11118type ProjectChildren = BTreeMap<NativeId, (NativeId, Vec<Resolved>)>;
11119
11120/// One issue comment as the contract carries it.
11121///
11122/// `author` is absent both when GitHub answers `null` for an account that no longer exists
11123/// and when it answers an actor with no login, because either way the source did not say who
11124/// wrote it — which is what an absent author means, rather than an author called nothing.
11125///
11126/// A comment a copy wrote here carries its origin as the hidden marker at the end of its body
11127/// — GitHub sets every comment's author and times itself, so there is nowhere else to keep it —
11128/// and is read back with the marker taken out and reported as its origin.
11129fn comment_from(value: &Value) -> Result<Comment, SourceError> {
11130    Ok(Comment {
11131        origin: None,
11132        id: NativeId(required_str(value, "id")?.to_owned()),
11133        author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
11134            .map(str::to_owned),
11135        created_at: optional_time(value, "createdAt")?,
11136        updated_at: optional_time(value, "updatedAt")?,
11137        body: required_str(value, "body")?.to_owned(),
11138        url: optional_str(value, "url")?.map(str::to_owned),
11139    }
11140    .unmarked())
11141}
11142
11143/// The page of comments one issue node carries, resumed from `after`.
11144fn comment_page(
11145    node: &Value,
11146    issue: &str,
11147    after: Option<&str>,
11148) -> Result<Page<Comment>, SourceError> {
11149    let connection = node
11150        .get("comments")
11151        .filter(|value| !value.is_null())
11152        .ok_or_else(|| SourceError::Malformed {
11153            message: format!("GitHub issue {issue} answered with no comments connection"),
11154        })?;
11155    let items = optional_nodes(Some(connection), "issue comments")?
11156        .into_iter()
11157        .flatten()
11158        .map(comment_from)
11159        .collect::<Result<Vec<_>, _>>()?;
11160    let next = next_cursor(connection)?;
11161    if let Some(next) = &next {
11162        validate_cursor_progress(after, &next.0)?;
11163    }
11164    Ok(Page { items, next })
11165}
11166
11167/// The far ends of an issue's whole `blockedBy` connection, when the read carried it to its
11168/// end — `None` when it carried none, or a page with more past it.
11169fn carried_blocked_by(content: &Value) -> Result<Option<Vec<Value>>, SourceError> {
11170    let Some(connection) = content.get("blockedBy").filter(|value| !value.is_null()) else {
11171        return Ok(None);
11172    };
11173    if next_cursor(connection)?.is_some() {
11174        return Ok(None);
11175    }
11176    Ok(Some(
11177        optional_nodes(Some(connection), "blocked-by issues")?
11178            .into_iter()
11179            .flatten()
11180            .cloned()
11181            .collect(),
11182    ))
11183}
11184
11185/// Where the recorded tail of a dependency walk resumes; see
11186/// [`GitHubProjectsSource::recorded_edges`].
11187const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
11188
11189/// The board text field this source keeps a copy's origin in.
11190///
11191/// Named after the key it holds, and held to that name by the guard below rather than by
11192/// a reader noticing.
11193const ORIGIN_FIELD: &str = "onetaskgraph.origin";
11194
11195/// The metadata key that field holds.
11196///
11197/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
11198/// constructs or interprets the qualified id it carries. This source names it only to
11199/// route it — a short, typed value belongs in a typed field rather than in the body slot
11200/// a caller's own prose shares.
11201///
11202/// Restated rather than imported, because no plugin crate may depend on the engine. What
11203/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
11204/// target in `check`: it reads the engine's own literal and fails naming the file and the
11205/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
11206/// that creates a second item every run instead of finding the one it wrote — and that is
11207/// too late to learn it.
11208const ORIGIN_KEY: &str = "onetaskgraph.origin";
11209
11210/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
11211///
11212/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
11213/// is derived from the far end, never written down on the near item — so only a forward
11214/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
11215/// it did not come from, and it is told so rather than answered with an empty page that
11216/// reads as a walk which ended.
11217fn recorded_offset(
11218    cursor: Option<&str>,
11219    direction: Direction,
11220) -> Result<Option<usize>, SourceError> {
11221    cursor
11222        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
11223        .map(|offset| {
11224            if direction != Direction::DependsOn {
11225                return Err(SourceError::Config {
11226                    message: format!(
11227                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
11228                         reverse dependency read never issues; resume it in the direction \
11229                         that reported it"
11230                    ),
11231                });
11232            }
11233            offset.parse().map_err(|_| SourceError::Config {
11234                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
11235            })
11236        })
11237        .transpose()
11238}
11239
11240fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
11241    let mut page = offset_page(edges, offset, limit.max(1));
11242    page.next = page
11243        .next
11244        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
11245    page
11246}
11247
11248/// The kind of one issue reached through a dependency connection.
11249///
11250/// The same questions the board scan asks, over the fields the dependency document
11251/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
11252/// then anything with sub-issues or the marker is a project.
11253///
11254/// # Errors
11255///
11256/// A far end this board holds as a document is refused rather than reported. The two
11257/// answers that are not refusals would both be wrong: reporting it as a task names an id
11258/// no task read of this source can find, and reporting it as a project names one no
11259/// project read can. There is no third value to return — `ItemKind` has no document
11260/// variant, because nothing may point at a document — so the relationship itself is what
11261/// the person is told about.
11262fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
11263    let id = required_str(value, "id")?;
11264    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
11265        return Err(SourceError::Refused {
11266            message: format!(
11267                "GitHub issue {id} is a document of this board — its title begins \
11268                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
11269                 on by one; next: remove that issue's blocking relationship on this board"
11270            ),
11271        });
11272    }
11273    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
11274    if parent.is_some() {
11275        return Ok(ItemKind::Task);
11276    }
11277    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
11278    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
11279        message: format!("GitHub issue {id}: {message}"),
11280    })?;
11281    let sub_issues = sub_issue_total(value)?;
11282    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
11283        ItemKind::Project
11284    } else {
11285        ItemKind::Task
11286    })
11287}
11288
11289/// The `IssueStateUpdateInput` one status target asks for.
11290///
11291/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
11292/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
11293/// a currently-closed issue: without that the item would read back `Unknown` and a copy
11294/// would report a change forever. A document has no status at all, and asks for neither.
11295fn state_input(target: Option<&StatusTarget>) -> Value {
11296    match target {
11297        Some(StatusTarget::Terminal(_, reason)) => {
11298            json!({"value":"CLOSED","stateReason":reason.reason()})
11299        }
11300        Some(StatusTarget::Column(_) | StatusTarget::Disabled(_)) => json!({"value":"OPEN"}),
11301        // A document has no status, so a write of one says nothing about the issue's open
11302        // or closed state rather than forcing it open: `stateInput` is what carries that
11303        // instruction, and an explicit null asks for no change to it.
11304        None => Value::Null,
11305    }
11306}
11307
11308/// The metadata one write stores in the item's body slot.
11309///
11310/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
11311/// rather than carried: the kind marker so an empty project stays readable, the
11312/// repository list only when it is not exactly the issue's own repository, and the far
11313/// ends no relationship here can name.
11314///
11315/// The copy origin is the one typed field that is also mirrored here, and only as a
11316/// mirror: it lands in the board's origin field as well, which stays the one every reader
11317/// takes it from, and it is here so that GitHub's issue search — which indexes this comment
11318/// and catches up with a write in seconds rather than minutes — can find the item by it.
11319/// A reader of the release before this one drops the slot's copy and reads the field, so an
11320/// item written here still reads with exactly one origin there.
11321fn slot_metadata(
11322    incoming: &Incoming<'_>,
11323    own_repository: Option<&Repository>,
11324    fallback: &[DependencyEdge],
11325) -> BTreeMap<String, Value> {
11326    let mut metadata = incoming.metadata.clone();
11327    match metadata.remove(ORIGIN_KEY) {
11328        Some(Value::String(origin)) if !origin.is_empty() => {
11329            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin));
11330        }
11331        _ => {}
11332    }
11333    match incoming.written.kind() {
11334        BoardKind::Work(kind) => metadata.insert(
11335            ItemKind::METADATA_KEY.to_owned(),
11336            Value::String(kind.marker().to_owned()),
11337        ),
11338        // A document is told by its title, so it carries no kind marker: that key names
11339        // what a dependency endpoint points at, and nothing may point at a document.
11340        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
11341    };
11342    incoming.classification.record(&mut metadata);
11343    let derivable = own_repository
11344        .map(|own| incoming.repositories == [own.clone()])
11345        .unwrap_or(incoming.repositories.is_empty());
11346    if derivable {
11347        metadata.remove(Repository::METADATA_KEY);
11348    } else {
11349        metadata.insert(
11350            Repository::METADATA_KEY.to_owned(),
11351            Value::Array(
11352                incoming
11353                    .repositories
11354                    .iter()
11355                    .map(|repository| Value::String(repository.as_str().to_owned()))
11356                    .collect(),
11357            ),
11358        );
11359    }
11360    // The typed lists are what land, whatever the caller's own metadata held under their
11361    // keys: a key of either name travelling beside the field would otherwise be a second
11362    // answer to the same question, and the field is the one the contract names.
11363    for (key, entries) in [
11364        (TaskRef::DELIVERS_KEY, incoming.delivers),
11365        (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
11366    ] {
11367        set_task_list(&mut metadata, key, entries);
11368    }
11369    record_edges(&mut metadata, fallback);
11370    metadata
11371}
11372
11373/// Hold the far ends no relationship here can name under [`DependencyEdge::RECORDED_KEY`] in
11374/// one slot's metadata, or no such key when there are none.
11375fn record_edges(metadata: &mut BTreeMap<String, Value>, fallback: &[DependencyEdge]) {
11376    if fallback.is_empty() {
11377        metadata.remove(DependencyEdge::RECORDED_KEY);
11378    } else {
11379        metadata.insert(
11380            DependencyEdge::RECORDED_KEY.to_owned(),
11381            Value::Array(
11382                fallback
11383                    .iter()
11384                    .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
11385                    .collect(),
11386            ),
11387        );
11388    }
11389}
11390
11391/// Every label one item carries, from its content's own connection and nowhere else.
11392///
11393/// There is no second place to read one from: no document this source sends selects the
11394/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
11395/// cannot carry one at all. The module documentation records the three schema facts that
11396/// settle it.
11397fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
11398    optional_nodes(content.get("labels"), "content labels")?
11399        .into_iter()
11400        .flatten()
11401        .map(|v| {
11402            Ok(Label {
11403                id: NativeId(required_str(v, "id")?.to_owned()),
11404                name: required_str(v, "name")?.to_owned(),
11405                color: optional_str(v, "color")?.map(str::to_owned),
11406            })
11407        })
11408        .collect()
11409}
11410
11411/// Each board field value one item holds, as the change token's facets name it: the field's
11412/// name, with the single-select option's name or the text. A value of a type no read here
11413/// selects, and one naming no field, says nothing a person can rename and is left out.
11414fn facet_fields(field_values: &[Value]) -> Vec<(String, String)> {
11415    field_values
11416        .iter()
11417        .filter_map(|value| {
11418            let field = value.pointer("/field/name")?.as_str()?;
11419            let held = value.get("name").or_else(|| value.get("text"))?.as_str()?;
11420            Some((field.to_owned(), held.to_owned()))
11421        })
11422        .collect()
11423}
11424
11425/// The digest of the facets of the issue `content`, whose board item's values are
11426/// `field_values` — or `None` when the response did not carry, whole, every member they are read
11427/// from: the title, the parent, the sub-issue summary and the body its kind is read from, every
11428/// label, and every field value.
11429///
11430/// What a write's own response spells its token from, by exactly the rules a read takes them by.
11431fn facets_of(content: &Value, field_values: &Value) -> Option<change_token::FacetsDigest> {
11432    let ended = |connection: &Value| {
11433        connection.pointer("/pageInfo/hasNextPage") == Some(&Value::Bool(false))
11434    };
11435    let labels = content.get("labels").filter(|labels| ended(labels))?;
11436    if !ended(field_values) {
11437        return None;
11438    }
11439    let parent = content.get("parent")?;
11440    let (_, slot) = metadata_body(content.get("body")?.as_str().map(str::to_owned)).ok()?;
11441    let kind = BoardKind::of(
11442        content.get("title")?.as_str()?,
11443        !parent.is_null(),
11444        sub_issue_total(content).ok()?,
11445        ItemKind::from_metadata(&slot).ok()?,
11446    );
11447    let labels = self::labels(&json!({"labels": labels})).ok()?;
11448    let nodes = field_values.get("nodes")?.as_array()?;
11449    Some(change_token::Facets::new(kind.facet(), facet_fields(nodes), &labels).digest())
11450}
11451
11452/// The definition of each board field one item's values are values of, in the shape a read
11453/// of the board's own `fields` gives one.
11454///
11455/// A value names its field through a fragment on that field's own type, so the type is
11456/// known from which kind of value it is: a single-select value's field is a
11457/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
11458/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
11459fn field_definitions(field_values: &[Value]) -> Vec<Value> {
11460    field_values
11461        .iter()
11462        .filter_map(|value| {
11463            let field = value.get("field")?.as_object()?;
11464            field.get("id")?.as_str().filter(|id| !id.is_empty())?;
11465            let typename = if value.get("text").is_some() {
11466                "ProjectV2Field"
11467            } else if value.get("name").is_some() {
11468                "ProjectV2SingleSelectField"
11469            } else {
11470                return None;
11471            };
11472            let mut defined = field.clone();
11473            defined.insert("__typename".to_owned(), json!(typename));
11474            // Only a text field holds a text value, so the value says the field's type too.
11475            if typename == "ProjectV2Field" {
11476                defined.insert("dataType".to_owned(), json!("TEXT"));
11477            }
11478            Some(Value::Object(defined))
11479        })
11480        .collect()
11481}
11482
11483fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
11484    let Some(node) = field_values
11485        .iter()
11486        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
11487    else {
11488        return Ok(None);
11489    };
11490    Ok(optional_str(node, "text")?.map(str::to_owned))
11491}
11492
11493fn valid_github_owner(owner: &str) -> bool {
11494    !owner.is_empty()
11495        && owner.len() <= 39
11496        && !owner.starts_with('-')
11497        && !owner.ends_with('-')
11498        && !owner.contains("--")
11499        && owner
11500            .bytes()
11501            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
11502}
11503
11504/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
11505/// neither of the two names a path segment already means.
11506fn valid_github_repository_name(name: &str) -> bool {
11507    !name.is_empty()
11508        && name.len() <= 100
11509        && name != "."
11510        && name != ".."
11511        && name
11512            .bytes()
11513            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
11514}
11515
11516fn valid_environment_name(name: &str) -> bool {
11517    let mut bytes = name.bytes();
11518    bytes
11519        .next()
11520        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
11521        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
11522}
11523
11524/// How many sub-issues one issue has.
11525///
11526/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
11527/// absent or non-integer one is a response this source cannot read — and reading it as
11528/// zero would classify a project as a task, which is exactly the mistake the marker
11529/// exists to keep from happening quietly.
11530fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
11531    let summary = issue
11532        .get("subIssuesSummary")
11533        .ok_or_else(|| SourceError::Malformed {
11534            message: "GitHub issue is missing subIssuesSummary".into(),
11535        })?;
11536    summary
11537        .get("total")
11538        .and_then(Value::as_u64)
11539        .ok_or_else(|| SourceError::Malformed {
11540            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
11541        })
11542}
11543
11544/// One issue's own `number`.
11545///
11546/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
11547/// an issue in this module asks for it. So a read of one that comes back without it, or
11548/// with something that is not an unsigned integer, is a response this source cannot read —
11549/// absence here is **not** "this issue has no number". A draft is the content that has
11550/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
11551/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
11552fn issue_number(issue: &Value) -> Result<u64, SourceError> {
11553    issue
11554        .get("number")
11555        .and_then(Value::as_u64)
11556        .ok_or_else(|| SourceError::Malformed {
11557            message: "GitHub issue number is missing or is not an unsigned integer".into(),
11558        })
11559}
11560
11561/// The `number` a creating mutation answered with, and `None` when it answered without one;
11562/// why a missing one is tolerated is at the call in `create_and_file_issue`.
11563fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
11564    match created.get("number") {
11565        None | Some(Value::Null) => Ok(None),
11566        Some(value) => value
11567            .as_u64()
11568            .map(Some)
11569            .ok_or_else(|| SourceError::Malformed {
11570                message: "GitHub created issue number is not an unsigned integer".into(),
11571            }),
11572    }
11573}
11574
11575fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
11576    value
11577        .get(field)
11578        .and_then(Value::as_str)
11579        .ok_or_else(|| SourceError::Malformed {
11580            message: format!("GitHub response is missing string field {field}"),
11581        })
11582}
11583
11584fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
11585    let found = required_str(value, field)?;
11586    if found.trim().is_empty() {
11587        return Err(SourceError::Malformed {
11588            message: format!("GitHub response has blank string field {field}"),
11589        });
11590    }
11591    Ok(found)
11592}
11593
11594/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
11595/// needs one — Linear spells them too, in its own description field.
11596///
11597/// Restated rather than shared, because a plugin crate depends on the contract crate and
11598/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
11599/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
11600/// source round-trips its own writes perfectly well under its own spelling.
11601const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
11602const METADATA_CLOSE: &str = "\n-->";
11603
11604/// What the composer puts between a non-empty visible body and the slot, and the one thing
11605/// the parser takes off the visible body when it takes the slot off — exactly once, so every
11606/// other trailing byte of the body comes back as it was written.
11607// 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.
11608const METADATA_SEPARATOR: &str = "\n\n";
11609
11610/// The visible body and the metadata slot at the end of it.
11611///
11612/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
11613/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
11614/// own content and is left alone. The visible body is everything before the slot less the
11615/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
11616fn metadata_body(
11617    body: Option<String>,
11618) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
11619    let Some(body) = body else {
11620        return Ok((None, BTreeMap::new()));
11621    };
11622    let Some(slot) = slot_span(&body)? else {
11623        return Ok((Some(body), BTreeMap::new()));
11624    };
11625    let metadata =
11626        serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
11627            SourceError::Malformed {
11628                message: format!(
11629                    "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
11630                ),
11631            }
11632        })?;
11633    let before = &body[..slot.start];
11634    let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
11635    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
11636}
11637
11638/// Where the metadata slot sits in one body, as byte offsets into it.
11639struct SlotSpan {
11640    /// Where [`METADATA_OPEN`] begins.
11641    start: usize,
11642    /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
11643    encoded_start: usize,
11644    /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
11645    encoded_end: usize,
11646    /// Just past [`METADATA_CLOSE`].
11647    end: usize,
11648}
11649
11650/// The slot at the very end of `body`, or `None` when it has none.
11651///
11652/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
11653/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
11654/// slot.
11655fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
11656    let Some(start) = body.rfind(METADATA_OPEN) else {
11657        return Ok(None);
11658    };
11659    let encoded_start = start + METADATA_OPEN.len();
11660    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
11661        return Err(SourceError::Malformed {
11662            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
11663        });
11664    };
11665    let encoded_end = encoded_start + relative_end;
11666    let end = encoded_end + METADATA_CLOSE.len();
11667    if !body[end..].trim().is_empty() {
11668        return Ok(None);
11669    }
11670    Ok(Some(SlotSpan {
11671        start,
11672        encoded_start,
11673        encoded_end,
11674        end,
11675    }))
11676}
11677
11678/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
11679/// slot as it was.
11680///
11681/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
11682/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
11683/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
11684/// or alone in an empty body — and a body with no slot that is given no metadata is
11685/// returned as it is.
11686fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
11687    let encoded = if metadata.is_empty() {
11688        None
11689    } else {
11690        Some(
11691            serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
11692                message: error.to_string(),
11693            })?,
11694        )
11695    };
11696    Ok(match (slot_span(body)?, encoded) {
11697        (Some(slot), Some(encoded)) => format!(
11698            "{}{encoded}{}",
11699            &body[..slot.encoded_start],
11700            &body[slot.encoded_end..]
11701        ),
11702        (Some(slot), None) => {
11703            let before = &body[..slot.start];
11704            format!(
11705                "{}{}",
11706                before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
11707                &body[slot.end..]
11708            )
11709        }
11710        (None, None) => body.to_owned(),
11711        (None, Some(encoded)) if body.is_empty() => {
11712            format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11713        }
11714        (None, Some(encoded)) => {
11715            format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11716        }
11717    })
11718}
11719
11720/// `body` with everything before its metadata slot replaced by `content`, and the slot
11721/// itself kept byte for byte.
11722///
11723/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
11724/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
11725/// `content` is empty — so a read of the result reports `content` as the visible body and
11726/// the slot's metadata exactly as it was.
11727fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
11728    let Some(slot) = slot_span(body)? else {
11729        return Ok(content.to_owned());
11730    };
11731    let kept = &body[slot.start..];
11732    Ok(if content.is_empty() {
11733        kept.to_owned()
11734    } else {
11735        format!("{content}{METADATA_SEPARATOR}{kept}")
11736    })
11737}
11738
11739/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
11740fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
11741    if entries.is_empty() {
11742        metadata.remove(key);
11743    } else {
11744        metadata.insert(
11745            key.to_owned(),
11746            Value::Array(
11747                entries
11748                    .iter()
11749                    .map(|entry| Value::String(entry.as_str().to_owned()))
11750                    .collect(),
11751            ),
11752        );
11753    }
11754}
11755
11756fn compose_body(
11757    content: Option<&str>,
11758    metadata: &BTreeMap<String, Value>,
11759) -> Result<Option<String>, SourceError> {
11760    let visible = content.unwrap_or_default();
11761    if metadata.is_empty() {
11762        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
11763    }
11764    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
11765        message: error.to_string(),
11766    })?;
11767    Ok(Some(if visible.is_empty() {
11768        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11769    } else {
11770        format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
11771    }))
11772}
11773
11774fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
11775    value
11776        .get(field)
11777        .and_then(Value::as_bool)
11778        .ok_or_else(|| SourceError::Malformed {
11779            message: format!("GitHub response is missing boolean field {field}"),
11780        })
11781}
11782fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
11783    match value.get(field) {
11784        None | Some(Value::Null) => Ok(None),
11785        Some(value) => value
11786            .as_str()
11787            .map(Some)
11788            .ok_or_else(|| SourceError::Malformed {
11789                message: format!("GitHub response field {field} is not a string or null"),
11790            }),
11791    }
11792}
11793fn optional_nodes<'a>(
11794    connection: Option<&'a Value>,
11795    name: &str,
11796) -> Result<Option<&'a Vec<Value>>, SourceError> {
11797    match connection {
11798        None | Some(Value::Null) => Ok(None),
11799        Some(value) => value
11800            .get("nodes")
11801            .and_then(Value::as_array)
11802            .map(Some)
11803            .ok_or_else(|| SourceError::Malformed {
11804                message: format!("GitHub {name}.nodes is not an array"),
11805            }),
11806    }
11807}
11808fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
11809    let page_info = connection
11810        .get("pageInfo")
11811        .ok_or_else(|| SourceError::Malformed {
11812            message: format!("GitHub {name} has no pageInfo"),
11813        })?;
11814    if required_bool(page_info, "hasNextPage")? {
11815        return Err(SourceError::Malformed {
11816            message: format!(
11817                "GitHub {name} exceeds the supported nested connection size of {size}"
11818            ),
11819        });
11820    }
11821    Ok(())
11822}
11823fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
11824    optional_str(value, field)?
11825        .map(|timestamp| {
11826            timestamp.parse().map_err(|error| SourceError::Malformed {
11827                message: format!("GitHub response field {field} is not a timestamp: {error}"),
11828            })
11829        })
11830        .transpose()
11831}
11832fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
11833    if page.limit == 0 {
11834        Err(SourceError::Config {
11835            message: "page limit must be at least 1".into(),
11836        })
11837    } else {
11838        Ok(())
11839    }
11840}
11841fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
11842    let page = connection
11843        .get("pageInfo")
11844        .filter(|value| value.is_object())
11845        .ok_or_else(|| SourceError::Malformed {
11846            message: "GitHub connection is missing pageInfo".into(),
11847        })?;
11848    if required_bool(page, "hasNextPage")? {
11849        let cursor = required_str(page, "endCursor")?;
11850        validate_cursor_progress(None, cursor)?;
11851        Ok(Some(Cursor(cursor.into())))
11852    } else {
11853        Ok(None)
11854    }
11855}
11856fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
11857    if next.is_empty() || previous == Some(next) {
11858        Err(SourceError::Malformed {
11859            message: "GitHub pagination cursor is empty or did not advance".into(),
11860        })
11861    } else {
11862        Ok(())
11863    }
11864}
11865/// The version of this plugin's opaque narrowing-search cursor.
11866pub const SEARCH_CURSOR_VERSION: u32 = 4;
11867
11868#[derive(Serialize, Deserialize)]
11869#[serde(tag = "state", rename_all = "snake_case", deny_unknown_fields)]
11870enum SearchConnection {
11871    Initial {},
11872    Continuing { after: Cursor },
11873    Exhausted {},
11874}
11875impl SearchConnection {
11876    fn after(&self) -> Option<&str> {
11877        match self {
11878            Self::Continuing { after } => Some(&after.0),
11879            _ => None,
11880        }
11881    }
11882    fn exhausted(&self) -> bool {
11883        matches!(self, Self::Exhausted { .. })
11884    }
11885    /// Whether a cursor naming this position, `offset` rows into its page, is one this
11886    /// plugin could have handed out: a page is resumed only part of the way through it — an
11887    /// offset of a whole page or more would skip rows nobody was given — an initial page
11888    /// only once some of it was handed out, and an exhausted connection has no page to be
11889    /// part of the way through.
11890    fn valid_resume(&self, offset: usize) -> bool {
11891        let within = offset < SEARCH_PAGE_SIZE as usize;
11892        match self {
11893            Self::Initial { .. } => offset > 0 && within,
11894            Self::Continuing { after } => !after.0.is_empty() && within,
11895            Self::Exhausted { .. } => offset == 0,
11896        }
11897    }
11898}
11899
11900/// Versioned source cursor. A zero offset and empty own-write ids are omitted.
11901#[derive(Serialize, Deserialize)]
11902#[serde(deny_unknown_fields)]
11903struct SearchPosition {
11904    version: u32,
11905    connection: SearchConnection,
11906    /// How many rows of the page `connection` starts were already handed out.
11907    #[serde(default, skip_serializing_if = "is_zero")]
11908    offset: usize,
11909    #[serde(default, skip_serializing_if = "Vec::is_empty")]
11910    seen: Vec<NativeId>,
11911    #[serde(default, skip_serializing_if = "Vec::is_empty")]
11912    own: Vec<NativeId>,
11913}
11914impl Default for SearchPosition {
11915    fn default() -> Self {
11916        Self {
11917            version: SEARCH_CURSOR_VERSION,
11918            connection: SearchConnection::Initial {},
11919            offset: 0,
11920            seen: Vec::new(),
11921            own: Vec::new(),
11922        }
11923    }
11924}
11925
11926fn is_zero(offset: &usize) -> bool {
11927    *offset == 0
11928}
11929
11930fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
11931    cursor.map_or(Ok(0), |c| {
11932        c.0.parse().map_err(|_| SourceError::Config {
11933            message: "page cursor is invalid".into(),
11934        })
11935    })
11936}
11937fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
11938    if offset > items.len() {
11939        return Page::last(vec![]);
11940    }
11941    let tail = items.split_off(offset);
11942    let mut selected = tail;
11943    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
11944    selected.truncate(limit);
11945    Page {
11946        items: selected,
11947        next,
11948    }
11949}
11950
11951impl GitHubProjectsSource {
11952    async fn write_task_assets(
11953        &self,
11954        write: &ItemWrite<Task>,
11955        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
11956    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
11957        let near = write.target.as_ref().unwrap_or(&write.item.id);
11958        for (key, entries) in [
11959            (TaskRef::DELIVERS_KEY, &write.item.delivers),
11960            (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
11961        ] {
11962            TaskRef::listed(key, near, Some(&self.name), entries.clone())
11963                .map_err(|message| SourceError::Refused { message })?;
11964        }
11965        if self.priorities.is_none() && write.item.priority != Priority::None {
11966            return Err(self.holds_no_priority());
11967        }
11968        self.write_item(
11969            &Incoming {
11970                written: Written::Work(ItemKind::Task, &write.item.status),
11971                title: &write.item.title,
11972                content: write.item.content.as_deref(),
11973                assets,
11974                labels: &write.item.labels,
11975                metadata: &write.item.metadata,
11976                repositories: &write.item.repositories,
11977                classification: write.item.classification,
11978                parent: write.item.project.as_ref(),
11979                delivers: &write.item.delivers,
11980                delivered_by: &write.item.delivered_by,
11981                priority: self.priorities.as_ref().map(|_| write.item.priority),
11982            },
11983            write.target.as_ref(),
11984            &write.depends_on,
11985        )
11986        .await
11987    }
11988    async fn write_document_assets(
11989        &self,
11990        write: &ItemWrite<Document>,
11991        assets: Option<&onetaskgraph_plugin_api::AssetWrite>,
11992    ) -> Result<onetaskgraph_plugin_api::AssetsWritten, SourceError> {
11993        // A document takes part in no dependency graph, so there is no far end to write
11994        // natively and none to record: a caller naming one is told so rather than having it
11995        // stored under the reserved key, where a later read would report an edge the
11996        // contract says cannot exist.
11997        if !write.depends_on.is_empty() {
11998            return Err(SourceError::Refused {
11999                message: format!(
12000                    "this write names {} dependencies for a document, and a document takes \
12001                     part in no dependency graph; next: put the dependency on the task or \
12002                     project the document is about",
12003                    write.depends_on.len()
12004                ),
12005            });
12006        }
12007        self.write_item(
12008            &Incoming {
12009                written: Written::Document,
12010                title: &write.item.title,
12011                content: write.item.content.as_deref(),
12012                assets,
12013                labels: &write.item.labels,
12014                metadata: &write.item.metadata,
12015                repositories: &write.item.repositories,
12016                classification: write.item.classification,
12017                parent: write.item.project.as_ref(),
12018                delivers: &[],
12019                delivered_by: &[],
12020                priority: None,
12021            },
12022            write.target.as_ref(),
12023            &[],
12024        )
12025        .await
12026    }
12027}
12028
12029/// What [`TaskSource::end_command`] leaves of this source's held state, asserted on the state
12030/// itself, for the two things no journey can observe.
12031///
12032/// The journeys in `crates/onetaskgraph-e2e/tests/e2e/end_command.rs` prove through the engine,
12033/// with and without the call, that a settlement, a board listing and a metadata search each
12034/// read afresh after it — the resolved records, the written-item overlay, the board and its
12035/// search, and the narrowed searches. What they cannot reach is the held field definitions,
12036/// because a status write naming an option a person deleted is refused the same whether or
12037/// not the list is held, and a poisoned lock, because nothing outside the source can panic
12038/// while one of its locks is held. So these assert those directly, and every other holder
12039/// beside them so a holder added later without a clear in the call fails here.
12040#[cfg(test)]
12041mod end_command_tests {
12042    use super::*;
12043
12044    struct Token;
12045
12046    impl SecretResolver for Token {
12047        fn get(&self, var: &str) -> Option<SecretString> {
12048            (var == "GH_PROJECTS_TOKEN").then(|| "test-token".into())
12049        }
12050    }
12051
12052    fn source() -> GitHubProjectsSource {
12053        let config = serde_json::from_value(json!({
12054            "owner": "octo-org", "project_number": 7, "repository": "acme/work",
12055            // Nothing here is sent: the source is only built and its state inspected.
12056            "endpoint": "http://127.0.0.1:9/graphql",
12057        }))
12058        .expect("a usable configuration");
12059        GitHubProjectsSource::new(&SourceName::new("work").unwrap(), config, &Token)
12060            .expect("the source builds")
12061    }
12062
12063    /// One issue as a board read answers it.
12064    fn resolved(source: &GitHubProjectsSource) -> Resolved {
12065        source
12066            .resolve(&json!({
12067                "id": "ITEM-1",
12068                "content": {"__typename": "Issue", "id": "I_1", "title": "Held",
12069                            "body": "what a person may since have edited", "state": "OPEN",
12070                            "stateReason": null, "url": null, "number": 1,
12071                            "subIssuesSummary": {"total": 0},
12072                            "labels": {"nodes": [], "pageInfo": {"hasNextPage": false}}},
12073                "fieldValues": {"nodes": [], "pageInfo": {"hasNextPage": false}},
12074            }))
12075            .expect("the item reads")
12076            .expect("an issue")
12077    }
12078
12079    /// Hold something in every holder the call clears, and the repository id it keeps.
12080    fn fill(source: &GitHubProjectsSource) {
12081        let item = resolved(source);
12082        source.created.lock().unwrap().push(item.clone());
12083        source.updated.lock().unwrap().push(item.clone());
12084        *source.board_cache.lock().unwrap() = Some(Board {
12085            id: "PVT-board".into(),
12086            fields: json!({"nodes": []}),
12087            items: vec![item.clone()],
12088        });
12089        *source.search_cache.lock().unwrap() = Some(vec![item.clone()]);
12090        source
12091            .narrowed_cache
12092            .lock()
12093            .unwrap()
12094            .insert("status:todo".into(), vec![item.clone()]);
12095        source.children_cache.lock().unwrap().insert(
12096            NativeId("P-1".into()),
12097            (NativeId("P-1".into()), vec![item.clone()]),
12098        );
12099        source
12100            .search_next
12101            .lock()
12102            .unwrap()
12103            .insert("status:todo".into(), Some("cursor".into()));
12104        source
12105            .resolved_cache
12106            .lock()
12107            .unwrap()
12108            .insert(item.id.clone(), item);
12109        *source.fields_cache.lock().unwrap() = Some(BoardFields {
12110            id: BoardId::parse("PVT-board").unwrap(),
12111            fields: json!({"nodes": []}),
12112        });
12113        source
12114            .repository_cache
12115            .lock()
12116            .unwrap()
12117            .insert(RepositoryTarget::parse("acme/work").unwrap(), "R_1".into());
12118    }
12119
12120    fn assert_dropped(source: &GitHubProjectsSource) {
12121        assert!(source.created().unwrap().is_empty(), "created");
12122        assert!(source.updated().unwrap().is_empty(), "updated");
12123        assert!(source.board_cache().unwrap().is_none(), "board");
12124        assert!(source.search_cache.lock().unwrap().is_none(), "search");
12125        assert!(source.narrowed_cache.lock().unwrap().is_empty(), "narrowed");
12126        assert!(
12127            source.children_cache.lock().unwrap().is_empty(),
12128            "project children"
12129        );
12130        assert!(
12131            source.search_next.lock().unwrap().is_empty(),
12132            "search paging"
12133        );
12134        assert!(
12135            source.resolved_cache().unwrap().is_empty(),
12136            "resolved records"
12137        );
12138        assert!(source.fields_cache().unwrap().is_none(), "board fields");
12139        assert_eq!(
12140            source.repository_cache().unwrap().len(),
12141            1,
12142            "a repository's node id stays valid and is kept"
12143        );
12144    }
12145
12146    fn end(source: &GitHubProjectsSource) {
12147        tokio::runtime::Builder::new_current_thread()
12148            .build()
12149            .unwrap()
12150            .block_on(source.end_command())
12151            .expect("the command ends");
12152    }
12153
12154    #[test]
12155    fn the_call_drops_every_item_search_and_board_read_and_keeps_repository_ids() {
12156        let source = source();
12157        fill(&source);
12158        end(&source);
12159        assert_dropped(&source);
12160    }
12161
12162    #[test]
12163    fn the_call_clears_a_lock_an_earlier_failure_poisoned() {
12164        fn poison<T: Send>(held: &Mutex<T>) {
12165            std::thread::scope(|scope| {
12166                let _ = scope
12167                    .spawn(|| {
12168                        let _guard = held.lock().unwrap();
12169                        panic!("a failure while the lock is held");
12170                    })
12171                    .join();
12172            });
12173            assert!(held.is_poisoned());
12174        }
12175        let source = source();
12176        fill(&source);
12177        poison(&source.created);
12178        poison(&source.updated);
12179        poison(&source.board_cache);
12180        poison(&source.search_cache);
12181        poison(&source.narrowed_cache);
12182        poison(&source.search_next);
12183        poison(&source.resolved_cache);
12184        poison(&source.fields_cache);
12185        assert!(
12186            source.resolved_cache().is_err(),
12187            "a poisoned lock is refused before the call"
12188        );
12189        end(&source);
12190        assert_dropped(&source);
12191    }
12192}
12193
12194#[cfg(test)]
12195mod unprojectable_fields_tests {
12196    use super::*;
12197
12198    /// The field names `docs/metadata.md` says a `metadata_fields` entry may not name.
12199    fn documented() -> std::collections::BTreeSet<String> {
12200        let document = std::fs::read_to_string(
12201            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/metadata.md"),
12202        )
12203        .expect("docs/metadata.md is readable");
12204        let document = document.split_whitespace().collect::<Vec<_>>().join(" ");
12205        let lead = "a `field` equal, ignoring case, to ";
12206        let start = document.find(lead).expect("the refused-field sentence") + lead.len();
12207        let sentence = &document[start..];
12208        let sentence = &sentence[..sentence
12209            .find("; and two entries naming one field")
12210            .expect("the refused-field sentence ends at the duplicate rule")];
12211        let (own, github) = sentence
12212            .split_once(" or a field GitHub owns on every board (")
12213            .expect("the sentence names GitHub's own fields");
12214        own.split('`')
12215            .skip(1)
12216            .step_by(2)
12217            .chain(
12218                github
12219                    .strip_suffix(')')
12220                    .expect("GitHub's own fields close the sentence")
12221                    .split(", "),
12222            )
12223            .map(str::to_owned)
12224            .collect()
12225    }
12226
12227    #[test]
12228    fn the_documented_refused_fields_are_exactly_the_ones_the_configuration_refuses() {
12229        let refused: std::collections::BTreeSet<String> = UNPROJECTABLE_FIELDS
12230            .iter()
12231            .map(|&name| name.to_owned())
12232            .collect();
12233        assert_eq!(documented(), refused, "the fields docs/metadata.md lists");
12234        let instance = SourceName::new("work").unwrap();
12235        for name in UNPROJECTABLE_FIELDS {
12236            for spelled in [name.to_owned(), name.to_uppercase()] {
12237                let entry = MetadataFieldConfig {
12238                    field: spelled.clone(),
12239                    key: "team.machine".to_owned(),
12240                    path: Vec::new(),
12241                };
12242                let refused = MetadataField::resolve(vec![entry], &instance)
12243                    .expect_err("an unprojectable field is refused");
12244                assert!(
12245                    refused.to_string().contains(&format!("{name:?}")),
12246                    "{spelled}: {refused}"
12247                );
12248            }
12249        }
12250    }
12251}