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.
81//!
82// 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.
83//! **Status.** `status_mapping` is per-instance configuration from a status category to
84//! `null` or a board `Status` option name. `done` selects its mapped option and closes the
85//! issue as `COMPLETED`; `cancelled` selects its mapped option and closes it as
86//! `NOT_PLANNED`. Every open category reopens a closed issue before selecting its option.
87//! A missing mapped option refuses the write before either representation changes. Reads
88//! give a closed issue's reason precedence over its option, while an open issue's option
89//! decides its category. The guarded [`GitHubProjectsSource::status_options`] operation is
90//! the one path here that calls `updateProjectV2Field`: GitHub replaces the whole option
91//! list, so it preserves every existing option id and verifies the field and item
92//! assignments immediately afterwards. It counts a terminal category's mapped option as
93//! configured, because a terminal write refuses without it. No ordinary source read or
94//! write calls that mutation, whose
95//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
96//! and a mistake destroys every item's status. A status this board cannot represent is a
97//! refusal naming the status and the instance instead.
98//!
99//! `unknown` is disabled by default because this source cannot preserve an open-ended
100//! status word: it writes an existing board option and never
101//! creates an option. An operator may map `unknown` to one existing option, in which case
102//! every unknown word lands on that option and reads back as `unknown` under the option's
103//! name. This differs from `local-md`, which writes and reads the original word itself.
104//!
105//! The shipped terminal options are exactly `done: Done` and `cancelled: Cancelled`.
106//! `done` also closes the issue because GitHub derives `subIssuesSummary.completed`
107//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
108//! finished tasks were only moved to a "Done" column would read 0% complete forever.
109// llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
110//!
111//! # What this source declares, field by field
112//!
113//! One verdict per field of [`Capabilities`], and what `Native` means when this source
114//! says it. *Proven* means a shared journey drives it against the real
115//! binary over this source's own row in `crates/onetaskgraph/tests/e2e/fixtures.rs`, and
116//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
117//! [`capabilities`](TaskSource::capabilities) from parting.
118//!
119//! | Field | Verdict |
120//! | --- | --- |
121//! | `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. |
122//! | `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. |
123//! | `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. |
124//! | `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. |
125//! | `filter_by_priority` | **Supported and proven,** over the priority each task reads as — `none` for every task of an instance without `priority_mapping`. |
126//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
127//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
128//! | `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`. |
129//! | `search_title` | **Supported and proven,** over `Issue.title`. |
130//! | `search_content` | **Supported and proven,** over the visible body — the trailing metadata comment is not part of it. |
131//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
132//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
133//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
134//!
135//! Nothing here is unsupported. `documents` and `comments` are not predicates — they say this
136//! source has documents and that its tasks have comments, both of which hold — and the three
137//! facts behind the uniform `Native` on the
138//! predicates beside it are recorded below rather than re-derived, because a reader who
139//! takes `Native` to mean *the remote service filters* will read that uniformity as a
140//! lie.
141//!
142//! First, the plugin contract defines `Support::Native` as *the source applies this
143//! predicate itself*, and says nothing about where it applies it. What the declaration
144//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
145//! — so that the engine may push it down and apply nothing of its own.
146//!
147//! Second, this source can keep that promise for every predicate at no additional API
148//! cost, because whichever of the three reads below answers a query has already read every
149//! item that query could keep before it filters anything. Filtering those items is
150//! in-process work over data already in hand.
151//!
152//! Third, no predicate but `projects` could be pushed into the API even if that were
153//! wanted, and `projects` is pushed down: `ProjectV2.items` takes `first` and `after` and
154//! offers no filter argument of any kind, GitHub's issue search offers no qualifier for a
155//! label set, a status column or a substring of a body, and its title qualifier matches
156//! tokens where this source — and the local Markdown source beside it — match substrings,
157//! so pushing a search down would silently *narrow* the answer. What a project filter has
158//! instead is a relationship: a project's tasks are that issue's sub-issues, and asking
159//! the issue for them is both cheaper and exact. So there is one predicate this source
160//! applies by asking a narrower question, six it applies in process, and none it is unable
161//! to apply. Declaring one `Unsupported` would make the engine compensate for work this
162//! source has already done, and declaring `projects` native while ignoring the filter
163//! (which this source once did) silently returns another project's tasks, because the
164//! engine trusts the declaration and applies nothing locally.
165//!
166//! # The three ways this source reaches an item, and what each costs
167//!
168//! A board read is charged for what its *nested* connections could return rather than for
169//! what was asked, so one whole-board read costs the same whether the question was about
170//! one project or about all of them. That is why a question about one project is never
171//! answered by reading the board:
172//!
173//! | The question | What is sent | What it costs |
174//! | --- | --- | --- |
175//! | one item, by its own id | [`graphql::ISSUE`] — `node(id:)` — and, when that node is a board draft, [`graphql::DRAFT`] — the draft and the one board item it is | the item |
176//! | 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` | the board's fields |
177//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
178//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
179//! | every task, every document, every label | [`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 |
180//! | 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 |
181//!
182//! The board half of an issue — its board item's id, its `Status` option and this
183//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
184//! an item reached any of those ways resolves through the same
185//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
186//! same status, the same labels and the same qualified id. That connection comes back a
187//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
188//! on the page in hand and — only if that page reports more of the connection — in the
189//! last row's read of that one issue's memberships, resumed from the page's own cursor and
190//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
191//! report, which is what keeps an id naming another repository's issue from being answered
192//! as an item of this board; and because the page is where the search starts rather than
193//! where it ends, that answer is one about a connection read to exhaustion and never about
194//! an unread page. Nothing costs the extra read but an issue on more boards than a page
195//! holds: an issue this board really does not hold reports no next page, so its
196//! memberships are already exhausted where they arrived.
197//!
198//! **No document here selects the board's own `Labels` field, and nothing is lost by
199//! that.** An item's labels are read from its content alone, wherever that content is
200//! reached: the three documents above select `Issue.labels` on the fragment, and
201//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
202//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
203//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
204//! create one, and `ProjectV2FieldValue` — the whole of what
205//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
206//! it from the content, for every content type it exists on, and there is nothing it can
207//! hold that the content does not already say: for an `Issue` it *is* that issue's own
208//! labels, so selecting it beside them unions a set with itself.
209//!
210//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
211//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
212//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
213//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
214//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
215//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
216//! label set, and that set is the fixture issue's own, by
217//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
218//! item whose content is a draft reports an empty set, by
219//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
220//! selection is held over [`graphql::DOCUMENTS`] by
221//! `no_document_selects_the_boards_own_labels_field`.
222//!
223//! The whole-board row is still the board's own item connection, and deliberately: a
224//! **draft** board item is not an issue, so no search can list one, and the reads that have
225//! to answer for the whole board are the ones whose cost is the board's size anyway.
226//!
227//! **A question about one item this source already names by id never lists the board.**
228//! Whether that item is on this board, and what its board fields are, is answered by reading
229//! that item — its own `Issue.projectItems`, walked to exhaustion by
230//! [`GitHubProjectsSource::resolve_issue`], or a draft's own board item — and never by
231//! looking for it in [`graphql::BOARD`]'s `items` or in a listing this command already
232//! holds. That covers a write's destination, the project a new item is filed under, a
233//! same-source far end a dependency names, a status write, the dependency slot a draft keeps,
234//! and the delete that takes back an item a copy made. What such a write needs of the board
235//! and the item does not carry — the board's id, the `Status` and origin field definitions —
236//! comes from [`graphql::BOARD_FIELDS`], which reads no item at all. The reason is evidence,
237//! not economy alone: `ProjectV2.items` is a projection that lags the membership GitHub
238//! itself reports — an issue added with `addProjectV2ItemById` can be missing from it for
239//! minutes. Scanning this host's 842-item board has refused a document copy and an update
240//! even though the items' own reads named that board. A scan there gives the wrong answer
241//! as well as paying for every page. So a `board.items` lookup does not belong on any of
242//! those paths.
243//!
244//! **What a read may return is capped too, and that cap is on the document rather than on
245//! the board.** GitHub limits the number of nodes **one query may return** to
246//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
247//! an error naming the connection the count crossed at, not a slow or a partial result.
248//! Every board this source reads is refused the same way, so no board is too big for these
249//! documents and none is small enough to save one that is over.
250//!
251//! The count is arithmetic over the document's own text: each connection contributes the
252//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
253//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
254//! restate them — `github-graphql-node-count` implements them, and
255//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
256//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
257//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
258//! same text on every run and fails naming any that reaches the limit — so a connection
259//! added to a shared fragment is caught there rather than by GitHub.
260//!
261//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
262//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
263//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
264//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
265//! effectively squared there, which is why it is the one the limit is most sensitive to.
266//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
267//! page of memberships misses is recovered by one further read rather than refused, so it
268//! buys a bound every read pays for at the price of a request only a multi-board issue
269//! pays.
270//!
271//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
272//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
273//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
274//! `cost` is rate-limit points, metered per hour across everything one credential does; it
275//! is what the two limiters [`Limiter`] tells apart meter, and a document under
276//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
277//! that second number, and `tests/point_cost.rs` pins every document in
278//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
279//! one under, the pin itself is the check. The credentialed lane reconciles both figures
280//! against GitHub's own, off a probe it already sends.
281//!
282//! **What is pinned that way is a per-document price and never a session's.** The record in
283//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
284//! **requests** and **worst-case nodes** — and neither is points. What one whole session
285//! consumes of the hourly point allowance is observable only from a credentialed run's own
286//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
287//! and what `tests/live.rs` prints at the end of every run.
288//!
289//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
290//!
291//! **Where a read-after-write guarantee comes from, since neither of GitHub's two
292//! enumerations of a board can supply one alone.** Resolving a node id is strongly
293//! consistent, so a read by id and a project's own sub-issues are already current. The
294//! other two are not, and they are behind by different amounts and in different directions:
295//!
296//! - GitHub's **issue search** is an index and answers a write made moments ago with the
297//!   value from before it — usually for a second or two.
298//! - **`ProjectV2.items`** is a projection GitHub rebuilds behind the write, and an item put
299//!   on a board with `addProjectV2ItemById` can be **absent** from it — not present with its
300//!   content withheld, absent, with the connection walked to its own `hasNextPage: false` —
301//!   for *minutes*, while `Issue.projectItems` names the same membership at once.
302//!
303//! That second one is a measurement rather than a caution. This repository's own
304//! credentialed journey writes a project and waits for the board to report it, then writes a
305//! task and waits for the same thing seconds later on the same board: the project wait is
306//! answered through the search and converged in two or three attempts in each of three runs,
307//! and the task wait is answered through `ProjectV2.items` and converged in none of them
308//! inside thirty. Separately, an item added to a second and larger board was read back by
309//! `Issue.projectItems` on that board's own id while every one of that connection's nine
310//! pages, walked to exhaustion nine minutes after the add, did not name it. Reading a board
311//! through the lagging one alone is what had a board read deny an issue that had certainly
312//! landed on it.
313//!
314//! So [`GitHubProjectsSource::board`] is the **union** of both — each search result still
315//! admitted only on this board's own strongly-consistent `Issue.projectItems`, and neither
316//! enumeration dropped, because only `ProjectV2.items` lists a board draft and only the
317//! search reports what the projection is behind on. What closes the last
318//! gap, the one where both are behind, is [`GitHubProjectsSource::created`]: every read this
319//! source answers is completed with what this process itself wrote, so an item created
320//! seconds ago is reported whether or not GitHub has caught up. Nothing else is remembered,
321//! nothing is written down, and the record dies with the process. **A wait that has to
322//! observe GitHub's own data cannot be answered from that record** — which is why the
323//! credentialed journey asks through a source built afresh, and why the union above rather
324//! than a longer wait is what makes such a wait converge.
325//!
326//! Filtering happens before paging, so a page of a filtered result is a page of the
327//! survivors rather than the survivors of a page. Label and text matching answer the same
328//! question the same way the local Markdown source's do, so one cross-source expectation
329//! holds for both.
330//!
331//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
332//! has one source, `capabilities`, and the note above is the reasoning behind it rather
333//! than a second copy of it: without the three facts recorded here a reader takes the
334//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
335//! crate's own capabilities test, which pins every field of it against a fully spelled-out
336//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
337//! fails to compile there rather than going unasserted. -->
338//! The fixture-server tests above run wherever this crate is selected; the credentialed
339//! lane runs in the same required check, beside them, and can fail it — it verifies the
340//! 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,
341//! one filed under neither, a label on one of the three and a closed status on another —
342//! because that shape is what tells an honoured predicate from an ignored one: a board
343//! holding a single project answers a project filter the same way whether or not this
344//! source applies it, which is exactly how the defect above went unseen.
345//!
346//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
347//! and only into the repository `GH_PROJECTS_REPOSITORY` names, and skips — as it does
348//! without `GH_PROJECTS_TOKEN` — when any of them is absent. Requiring both to be
349//! nominated is what keeps a credentialed write lane off a board and a repository nobody
350//! nominated; it never asks GitHub which project was updated most recently. Before it
351//! starts, the lane also clears any item titled — and any repository label named — the way
352//! it titles and names its own artifacts, which is self-healing after an interrupted run:
353//! a process killed between its writes and its cleanup leaves artifacts the next run
354//! removes.
355//!
356//! # What a session of requests costs, and where the report is
357//!
358//! This source records **every** request it sends into [`accounting::Accounting`], at
359//! `send_once` — the one place a request leaves this crate, which is why a read path added
360//! later is counted without anybody remembering to count it. That is the whole of what this
361//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
362//! session's spend is arrived at, and what it deliberately does not know are set out.
363//!
364//! What one whole session of the live journey costs, counted that way against this crate's
365//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
366//! reduction it came out of, and with what it does and does not say about rate-limit points.
367//!
368//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
369//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
370//! code path — no environment variable, no feature, no build configuration — because an
371//! instrument nobody switches on measures nothing, and
372//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
373//! source's counts the whole session rather than this source's share. The credentialed lane
374//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
375//! passed or failed.
376//!
377//! **A live session refuses to start unless the account can afford it.** Before it does any
378//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
379//! GitHub documents as not counting against the REST rate limit and which answers both of
380//! its budgets at once — and starts only if, for each of them, what remains minus this
381//! session's estimated cost is still at least
382//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
383//! allowance. A session that cannot **declines**: it did not run, so it is
384//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
385//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
386//! waiting for the budget to come back. The estimate is derived offline from
387//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
388//! which is also where the published rule that model rests on is cited; the accounting
389//! above records the gate's own read like any other request, and
390//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
391//!
392//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
393//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
394//! text, which is what lets it run on every platform and on a pull request from a fork with
395//! no credential — and that is what actually stops a regression merging. But an offline
396//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
397//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
398//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
399//! number of nodes this query may return"* and whose `cost` is what that document would
400//! spend, both for a document **without executing it**, and the lane asks it for every query
401//! document this source sends, under the largest bindings this source sends, and fails when
402//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
403//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
404//! one; the offline pins still cover it. It records what those calls reported about the
405//! account's own allowance, because whether asking is free is a thing to observe rather than
406//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
407//! and `cost` is metered against an hourly allowance the accounting above reads off a
408//! credentialed run's own response headers.
409//!
410//! **GitHub has two rate limiters and this source is refused by both, so nothing here
411//! treats them as one thing.** The primary budget is the hourly allowance `gh api
412//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
413//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
414//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
415//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
416//! one part of.
417#![deny(missing_docs)]
418
419use std::collections::BTreeMap;
420use std::sync::{Arc, Mutex};
421use std::time::{Duration, Instant};
422
423use chrono::{DateTime, Utc};
424use onetaskgraph_plugin_api::{
425    Capabilities, Comment, CommentBody, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind,
426    DependencySupport, Direction, Document, DocumentQuery, Health, ItemKind, ItemWrite, Label,
427    LabelFilter, Location, MetadataKey, Metering, NativeId, NewComment, Page, PageRequest,
428    Priority, Project, ProjectFilter, ProjectQuery, Repository, SecretResolver, SourceError,
429    SourceName, SourcePlugin, Status, StatusCategory, Support, Task, TaskQuery, TaskRef,
430    TaskSource, TextFields, TextQuery, WriteSupport,
431};
432use reqwest::{Client, StatusCode, Url};
433use schemars::{Schema, schema_for};
434use secrecy::{ExposeSecret, SecretString};
435use serde::{Deserialize, Serialize};
436use serde_json::{Value, json};
437
438pub mod accounting;
439
440use accounting::Accounting;
441
442/// The registry name for this plugin.
443pub const KIND: &str = "github-projects";
444/// GitHub's maximum connection page size.
445pub const MAX_PAGE_SIZE: u32 = 100;
446
447/// The most nodes any one document this source sends may be asked to return.
448///
449/// GitHub's own published per-query ceiling, taken from
450/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
451/// workspace cannot hold a stale copy of somebody else's number. A query above it is
452/// **refused before it is executed**, whoever is asking and whatever board they are
453/// asking about — so this is a bound on the documents rather than a budget that runs out.
454///
455/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
456/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
457/// everything the credential does — two numbers against two limits, and this constant
458/// bounds only the first. The second is computed offline too, per document:
459/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
460/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
461/// lane. There is no constant like this one to hold a price under, because points are an
462/// hourly allowance rather than a per-call bound.
463///
464/// Neither is a session's price. What `session-cost.md` records of a whole session is its
465/// **requests** and its **worst-case nodes**; what a whole session spends in points is
466/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
467/// [`accounting`]. The module section on the three ways this source reaches an item says how
468/// the count is arrived at, and which of the page sizes below decide it.
469pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
470
471/// Nested connection size for the connections that hang off one item.
472///
473/// It multiplies through every document that reaches an item under a page — the count
474/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
475/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
476/// every document under these constants and fails naming any that reaches the limit, so
477/// raising this is caught there rather than by GitHub.
478const NESTED_PAGE_SIZE: u32 = 50;
479/// How many of one issue's board memberships are read when an issue is reached directly.
480///
481/// An issue reached through a search or through its own node id carries its board half in
482/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
483/// under a page of issues, so every point of it multiplies through the whole document and
484/// is paid for whether or not any issue is on a second board — which is why it is
485/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
486///
487/// **Three, because what a page misses is now recovered rather than refused**, and the
488/// recovery is what the value is chosen against. An issue whose entry for this board sits
489/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
490/// that page's own cursor — so the value trades a bound every read pays for a request only
491/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
492/// boards would pay that request *per issue*, which is order N against the one page per
493/// hundred issues a read costs today. At three it is only reached by an issue on four or
494/// more boards at once, which keeps the recovery path exceptional rather than routine for
495/// a plausible deployment.
496const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
497
498pub use github_graphql_node_count::{NodeCountError, Variables};
499
500/// The largest value this source can bind to each page-size variable its documents name.
501///
502/// Every `first:` in [`graphql`] reads one of these three, and each is capped at the
503/// constant above it wherever a caller's own limit could reach it — `$first` at
504/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
505/// `BOARD_ITEMS_PAGE_SIZE`. So this is the worst case a caller can drive this source to,
506/// not one configuration of it, which is what makes a bound computed under it a bound on
507/// every read.
508pub fn largest_page_sizes() -> Variables {
509    Variables::from([
510        ("first".to_owned(), MAX_PAGE_SIZE),
511        ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
512        ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
513    ])
514}
515
516/// The most nodes `document` could be asked to return, by GitHub's published rules.
517///
518/// Computed offline from the document's own text under [`largest_page_sizes`] — no
519/// network, no credential and no schema — by
520/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
521/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
522/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
523///
524/// # Errors
525///
526/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
527/// no single operation, or binds a page size this source does not name — each of which is
528/// a defect in the document rather than a number.
529pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
530    node_count(document, &largest_page_sizes())
531}
532
533/// The most rate-limit points one call of `document` could spend, by GitHub's published
534/// rules.
535///
536/// Computed offline from the document's own text under [`largest_page_sizes`] — no
537/// network, no credential and no schema — by
538/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
539/// This is `cost`, metered **per hour** against the allowance one credential shares across
540/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
541/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
542/// under, so what `tests/point_cost.rs` does with it is pin every document in
543/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
544/// figures against GitHub's own reported `cost`.
545///
546/// # Errors
547///
548/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
549/// no single operation, or binds a page size this source does not name — each of which is
550/// a defect in the document rather than a number.
551pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
552    github_graphql_node_count::point_cost(document, &largest_page_sizes())
553}
554
555/// The most nodes `document` could be asked to return under `variables`.
556///
557/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
558/// [`accounting`] is this under the bindings one request really sent — one spelling of the
559/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
560/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
561///
562/// # Errors
563///
564/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
565/// no single operation, or binds a page size `variables` does not name.
566pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
567    github_graphql_node_count::node_count(document, variables)
568}
569
570/// The issue-title prefix that makes a board issue a document.
571///
572/// A GitHub Projects board has no document type — it holds issues — so the discriminator
573/// is the title, and this is the whole of it: an issue whose title begins with these bytes
574/// is a document and every other issue is the task or project the sub-issue rule makes it.
575///
576/// It is spelled **once**, here, and read rather than restated everywhere else — including
577/// by the shared journeys, which take it from this constant so a board fixture cannot
578/// drift from what this source reads. `docs/metadata.md` records the two consequences that
579/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
580/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
581/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
582pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
583
584/// Exact GraphQL query documents issued by this plugin.
585///
586/// Keeping the production documents here lets the pinned-schema test validate the same
587/// bytes that are sent to GitHub, rather than a test-only copy which could drift
588/// independently. [`STATUS_OPTIONS_UPDATE`] is the sole document that may rewrite a board
589/// field, and its guarded caller always supplies the complete existing option set with ids.
590pub mod graphql {
591    /// The board half of one item: the field values every document here reads it from.
592    ///
593    /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
594    /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
595    /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
596    /// *the same value*, because
597    /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
598    /// one path. Three spellings of it is what would drift, so there is one.
599    ///
600    /// The `Status` option and this source's own origin text field are the whole of it. It
601    /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
602    /// content, so it holds nothing the content's own `labels` do not already say, and it
603    /// would sit a label connection two page sizes deep.
604    macro_rules! board_item_values {
605        () => {
606            r#"fieldValues(first:$nestedFirst){nodes{
607          ... on ProjectV2ItemFieldSingleSelectValue{name field{
608            ... on ProjectV2SingleSelectField{id name options{id name}}
609          }}
610          ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
611        }pageInfo{hasNextPage}}"#
612        };
613    }
614
615    /// Everything this source reads about one issue, wherever it reaches that issue.
616    ///
617    /// A macro rather than a constant so the three documents below can `concat!` it: one
618    /// spelling of these fields is what makes an issue read through the board-scoped
619    /// search, through its own node id, and through its project's sub-issue relationship
620    /// resolve to *the same* item, which is the whole of what
621    /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
622    ///
623    /// `projectItems` is what carries the board half of an issue: the board item's own id
624    /// and the [`board_item_values!`] above — the `Status` option and this source's origin
625    /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
626    /// issue rather than on the board, which is what makes the cost of a read proportional
627    /// to what was asked for instead of to the board's size.
628    ///
629    /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
630    /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
631    /// not on that page: a page here is where the search for the entry starts rather than
632    /// where it ends.
633    ///
634    /// It does **not** select the board's `Labels` field value, and that is the whole of
635    /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
636    /// a label connection there sits under `fieldValues` under `projectItems` under a page
637    /// of issues, spending `$nestedFirst` twice down one path, and took
638    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
639    /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
640    /// above, and that connection is where every label this source reports comes from. No
641    /// document in this module selects the board field any longer, [`BOARD`] included; the
642    /// module documentation records why nothing it could have held is lost.
643    macro_rules! board_issue {
644        () => {
645            concat!(
646                r#" fragment BoardIssue on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total}
647      labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
648      projectItems(first:$boardItems){nodes{id project{id number}
649        "#,
650                board_item_values!(),
651                r#"}pageInfo{hasNextPage endCursor}}}"#
652            )
653        };
654    }
655
656    /// Every issue of one board, found by a search scoped to that board.
657    ///
658    /// This is how the projects a board holds are listed, and it selects no `items`
659    /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
660    /// container walked page by page, so nothing nested inside a board item is paid for.
661    /// Which of the issues it returns is a project is then read off `parent` — GitHub
662    /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
663    /// discriminator has to be applied to the field, which is a scalar on the issue and
664    /// costs nothing.
665    pub const SEARCH_ISSUES: &str = concat!(
666        r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
667      search(query:$search,type:$type,first:$first,after:$after){
668        pageInfo{hasNextPage endCursor}
669        nodes{__typename ...BoardIssue}
670      }
671    }"#,
672        board_issue!()
673    );
674
675    /// One issue by its own node id, which is what a qualified id names here.
676    ///
677    /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
678    /// answers a write made moments ago with the value from before it, and resolving a node
679    /// id does not.
680    pub const ISSUE: &str = concat!(
681        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
682      node(id:$id){__typename ...BoardIssue}
683    }"#,
684        board_issue!()
685    );
686
687    /// One project's tasks: the sub-issues of the issue that project is.
688    ///
689    /// The work this costs is the project's own size. Nothing about it grows as the board
690    /// gains projects, or as those projects gain tasks.
691    pub const SUB_ISSUES: &str = concat!(
692        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
693      node(id:$id){__typename
694        ... on Issue{subIssues(first:$first,after:$after){
695          pageInfo{hasNextPage endCursor}
696          nodes{__typename ...BoardIssue}
697        }}}
698    }"#,
699        board_issue!()
700    );
701
702    /// Reads the board's fields and one page of its items.
703    pub const BOARD: &str = concat!(
704        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!){
705      owner:repositoryOwner(login:$owner){
706        ... on ProjectV2Owner{projectV2(number:$number){...Board}}
707      }
708    } fragment Board on ProjectV2 { id title
709      fields(first:$nestedFirst){nodes{
710        ... on ProjectV2SingleSelectField{__typename id name options{id name}}
711        ... on ProjectV2Field{__typename id name}
712      }pageInfo{hasNextPage}}
713      items(first:$first,after:$after){nodes{id "#,
714        board_item_values!(),
715        r#" content{
716        ... on Issue{__typename id number title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}}
717        ... on PullRequest{__typename id}
718        ... on DraftIssue{__typename id title body createdAt updatedAt}
719      }} pageInfo{hasNextPage endCursor}}
720    }"#
721    );
722
723    /// The board's own id and field definitions, and not one of its items.
724    ///
725    /// What a write needs of the board when the item it writes does not say: the id a field
726    /// write and `addProjectV2ItemById` address, and the definitions of the `Status` and
727    /// origin fields. It selects no `items`, so what it costs is the board's field list
728    /// however many items the board holds — and it decides nothing about which items those
729    /// are, which is the question a read of one item by its own id answers instead.
730    ///
731    /// The root is aliased `boardFields` rather than `owner`, so nothing counting the
732    /// board's item reads by their root counts this one among them.
733    pub const BOARD_FIELDS: &str = r#"query($owner:String!,$number:Int!,$nestedFirst:Int!){
734      boardFields:repositoryOwner(login:$owner){
735        ... on ProjectV2Owner{projectV2(number:$number){id
736          fields(first:$nestedFirst){nodes{
737            ... on ProjectV2SingleSelectField{__typename id name options{id name}}
738            ... on ProjectV2Field{__typename id name}
739          }pageInfo{hasNextPage}}
740        }}
741      }
742    }"#;
743
744    /// One board draft by its own node id, with the board item it sits in.
745    ///
746    /// A draft is not an issue, so [`ISSUE`] reaches it and reads nothing of it; this is the
747    /// second read that answers it. `DraftIssue.projectV2Items` names the board item a draft
748    /// is — GitHub links a draft to one item — with the same [`board_item_values!`] the
749    /// issue fragment reads, so a draft reached by id resolves through the same resolver a
750    /// board listing hands it to, and nothing has to list the board to find one.
751    pub const DRAFT: &str = concat!(
752        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!){
753      node(id:$id){__typename ... on DraftIssue{id title body createdAt updatedAt
754        projectV2Items(first:$boardItems){nodes{id project{id number}
755        "#,
756        board_item_values!(),
757        r#"}pageInfo{hasNextPage endCursor}}}}
758    }"#
759    );
760
761    /// One issue's board memberships alone, walked past the page a read of it carried.
762    ///
763    /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
764    /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
765    /// boards than that page holds may have this board's entry past its end. This asks that
766    /// one issue for its memberships and nothing else — the caller already holds the issue —
767    /// so an answer of "this board does not hold it" is only ever given about a connection
768    /// read to exhaustion.
769    ///
770    /// It selects the board item's id, its project number and the same
771    /// [`board_item_values!`] the fragment does, because what it produces is handed to the
772    /// very same resolver: an issue recovered this way reports the same title, the same
773    /// status, the same labels and the same qualified id as one whose entry was on the
774    /// page.
775    ///
776    /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
777    /// multiplies through it and the membership connection can be walked at
778    /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
779    /// further request for any issue a person really keeps.
780    pub const ISSUE_BOARD_ITEMS: &str = concat!(
781        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
782      node(id:$id){
783        ... on Issue{projectItems(first:$first,after:$after){
784          nodes{id project{id number}
785        "#,
786        board_item_values!(),
787        r#"}
788          pageInfo{hasNextPage endCursor}}}
789      }
790    }"#
791    );
792    /// Resolves the configured repository's node id, which creating an issue requires.
793    pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
794    /// Reads both dependency directions for one issue, with each far end's own kind — and
795    /// the issue's own body, which is where an edge to another source is recorded, so that
796    /// half of a dependency read needs no second read of the issue or of the board.
797    pub const ISSUE_DEPENDENCIES: &str = r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
798      ... on Issue{body
799        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
800        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
801      }}} fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"#;
802    /// Creates one issue in the configured repository.
803    pub const CREATE_ISSUE: &str =
804        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id number url}}}"#;
805    /// Puts an existing issue on the configured board.
806    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
807    /// Updates an issue's visible fields and its open or closed state in one call.
808    pub const UPDATE_ISSUE: &str =
809        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
810    /// Updates an existing draft's user-visible fields.
811    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
812    /// Updates a text or single-select value on one project item.
813    pub const UPDATE_FIELD: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id}}}"#;
814    /// Clears one project item's value of one field, which is what a `none` priority is.
815    pub const CLEAR_FIELD: &str = r#"mutation($input:ClearProjectV2ItemFieldValueInput!){clearProjectV2ItemFieldValue(input:$input){projectV2Item{id}}}"#;
816    /// Creates one single-select field with its options. Only the guarded field setup may use
817    /// this document, and only for a field the board lacks.
818    pub const CREATE_FIELD: &str = r#"mutation($input:CreateProjectV2FieldInput!){createProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id name options{id name color description}}}}}"#;
819    /// Replaces a single-select field's options. Only the guarded field setup — the
820    /// `status-options` and `fields` operations — may use this document, because GitHub
821    /// treats the input as the complete option list.
822    pub const STATUS_OPTIONS_UPDATE: &str = r#"mutation($input:UpdateProjectV2FieldInput!){updateProjectV2Field(input:$input){projectV2Field{... on ProjectV2SingleSelectField{id options{id name color description}}}}}"#;
823    /// A fresh snapshot of the Status field and every board item's assignment.
824    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}}}}}}"#;
825    /// Files one issue under another as a sub-issue, which is what project membership is.
826    pub const ADD_SUB_ISSUE: &str =
827        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
828    /// Takes one issue back out of its parent.
829    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
830    /// Adds GitHub's native issue blocked-by relationship.
831    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
832    /// Removes one native issue blocked-by relationship.
833    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
834    /// Deletes one issue, which takes its board item with it.
835    ///
836    /// The engine sends this in one situation only: undoing a copy that could not finish,
837    /// over the items that same copy created. Deleting the issue removes the board item
838    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
839    pub const DELETE_ISSUE: &str =
840        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
841
842    /// Everything this source reads about one issue comment, wherever it reaches one.
843    ///
844    /// A macro for the reason [`board_issue!`] is one: a comment listed, a comment just added
845    /// and a comment just edited are handed to one mapper, so they are selected by one
846    /// spelling. `author` is `Actor`, which GitHub answers `null` for an account that no
847    /// longer exists, and `login` is the one member every kind of actor carries.
848    macro_rules! issue_comment {
849        () => {
850            "id author{login} createdAt updatedAt body url"
851        };
852    }
853
854    /// One task's comments: a page of its issue's own `comments` connection.
855    ///
856    /// **No `orderBy`, and that is what makes the page oldest first.** GitHub's only
857    /// `IssueCommentOrder` field is `UPDATED_AT`, which would move a comment to the end of the
858    /// list every time somebody edited it; left unordered the connection answers in the order
859    /// the comments were written, which is the order GitHub documents for the same collection
860    /// over REST — ascending id. Nothing multiplies through it, so `$first` is the whole of its
861    /// node count and the caller's own page size is pushed straight down.
862    pub const ISSUE_COMMENTS: &str = concat!(
863        r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename ... on Issue{comments(first:$first,after:$after){nodes{"#,
864        issue_comment!(),
865        r#"}pageInfo{hasNextPage endCursor}}}}}"#
866    );
867    /// Which issue one comment is on, read before that comment is edited or removed.
868    ///
869    /// GitHub's comment mutations take the comment's id and nothing else, so without this a
870    /// comment id given against the wrong task would change a comment on another issue.
871    pub const COMMENT_ISSUE: &str =
872        r#"query($id:ID!){node(id:$id){__typename ... on IssueComment{id issue{id}}}}"#;
873    /// Adds one comment to an issue, signed as the account the token belongs to.
874    pub const ADD_COMMENT: &str = concat!(
875        r#"mutation($input:AddCommentInput!){addComment(input:$input){subject{id} commentEdge{node{"#,
876        issue_comment!(),
877        r#"}}}}"#
878    );
879    /// Replaces the body of one issue comment.
880    pub const UPDATE_COMMENT: &str = concat!(
881        r#"mutation($input:UpdateIssueCommentInput!){updateIssueComment(input:$input){issueComment{"#,
882        issue_comment!(),
883        r#"}}}"#
884    );
885    /// Removes one issue comment. Its payload carries nothing about the comment it removed.
886    pub const DELETE_COMMENT: &str = r#"mutation($input:DeleteIssueCommentInput!){deleteIssueComment(input:$input){clientMutationId}}"#;
887
888    /// Every document above, with what this source is doing when it sends one.
889    ///
890    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
891    /// name the call that was refused, and a `match` with a catch-all arm would answer a
892    /// document added later with "talking to GitHub" and never say so.
893    ///
894    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
895    /// const` here that this list omits, so the two cannot part — which is the same guard
896    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
897    pub const DOCUMENTS: [(&str, &str); 28] = [
898        (SEARCH_ISSUES, "searching this board's issues"),
899        (ISSUE, "reading one issue"),
900        (
901            ISSUE_BOARD_ITEMS,
902            "reading one issue's board memberships past the page it came with",
903        ),
904        (SUB_ISSUES, "reading a project's tasks"),
905        (BOARD, "reading the board"),
906        (BOARD_FIELDS, "reading the board's fields"),
907        (DRAFT, "reading one draft"),
908        (REPOSITORY, "reading the destination repository"),
909        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
910        (CREATE_ISSUE, "creating an issue"),
911        (ADD_TO_BOARD, "adding an issue to the board"),
912        (UPDATE_ISSUE, "updating an issue"),
913        (UPDATE_DRAFT, "updating a draft item"),
914        (UPDATE_FIELD, "writing a board field"),
915        (CLEAR_FIELD, "clearing a board field"),
916        (
917            CREATE_FIELD,
918            "creating a board single-select field with its options",
919        ),
920        (
921            STATUS_OPTIONS_SNAPSHOT,
922            "snapshotting board Status options and assignments",
923        ),
924        (
925            STATUS_OPTIONS_UPDATE,
926            "safely replacing the board Status option list",
927        ),
928        (ADD_SUB_ISSUE, "filing an issue under its project"),
929        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
930        (ADD_BLOCKED_BY, "recording a dependency"),
931        (REMOVE_BLOCKED_BY, "removing a dependency"),
932        (DELETE_ISSUE, "deleting an issue"),
933        (ISSUE_COMMENTS, "reading a task's comments"),
934        (COMMENT_ISSUE, "reading which issue a comment is on"),
935        (ADD_COMMENT, "adding a comment"),
936        (UPDATE_COMMENT, "editing a comment"),
937        (DELETE_COMMENT, "deleting a comment"),
938    ];
939}
940
941/// Which of GitHub's two rate limiters refused a request.
942///
943/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
944/// secondary one — so an operator told the wrong one takes the wrong next step, which is
945/// the whole reason this is carried rather than collapsed into "rate limited".
946#[derive(Debug, Clone, Copy, PartialEq, Eq)]
947enum Limiter {
948    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
949    Primary,
950    /// The burst limiter over content-generating requests, which nothing reports.
951    Secondary,
952}
953
954/// The wordings GitHub answers a secondary rate limit with.
955///
956/// It sends them under a forbidden status, under a too-many-requests status, and inside
957/// the `errors` of a *successful* response, which is why the text is what this matches on
958/// rather than the status. `abuse detection` is the wording GitHub used before the
959/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
960/// what a burst of content creation is refused with.
961///
962/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
963/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
964/// when, and the drift gate reconciles the two lists both ways. Public for that gate
965/// alone — a caller has no use for it, and matching on a refusal is this source's job.
966pub const SECONDARY_WORDINGS: [&str; 5] = [
967    "secondary rate limit",
968    "temporarily blocked from content creation",
969    "abuse detection",
970    "submitted too quickly",
971    "exceeded a secondary",
972];
973
974/// The wordings GitHub answers an exhausted primary budget with.
975///
976/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
977/// than looked for in the response text. Pinned and gated exactly as
978/// [`SECONDARY_WORDINGS`] is, and public for the same one reason.
979pub const PRIMARY_WORDINGS: [&str; 3] = [
980    "api rate limit exceeded",
981    "rate limit exceeded",
982    "rate_limited",
983];
984
985/// What a response *says about itself*, which is the only place a refusal can be read.
986///
987/// Deliberately not the whole response body. A board is a place people write about their
988/// own work, and a task on it titled "the secondary rate limit" would, matched across the
989/// raw text, turn a perfectly good answer into a refusal this source then waited out and
990/// reported. So the item data is never read: what is read is GitHub's own REST-style
991/// `message` envelope, which is what a forbidden status carries, and the `message` and
992/// `type` of each GraphQL error, which is where a *successful* response says it.
993///
994/// A body that is not JSON at all has nothing structured to read, so only a failing
995/// response's own text is taken — a successful response that is not JSON is malformed
996/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
997fn refusal_wording(status: StatusCode, body: &str) -> String {
998    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
999        return if status.is_success() {
1000            String::new()
1001        } else {
1002            body.to_owned()
1003        };
1004    };
1005    let mut said: Vec<&str> = parsed
1006        .get("message")
1007        .and_then(Value::as_str)
1008        .into_iter()
1009        .collect();
1010    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
1011        for error in errors {
1012            said.extend(
1013                ["message", "type"]
1014                    .into_iter()
1015                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
1016            );
1017        }
1018    }
1019    said.join("; ")
1020}
1021
1022impl Limiter {
1023    /// Which limiter refused this response, or `None` when none of them did.
1024    ///
1025    /// The wording is read first and the status only decides what carries none of it,
1026    /// because GitHub answers a secondary limit with a forbidden status far more often
1027    /// than with too-many-requests — while a forbidden status saying nothing about a limit
1028    /// really is a credential this token lacks.
1029    ///
1030    /// A response is a refusal because of its status or its own wording. A spent budget
1031    /// only ever explains one; it never turns an answer into a refusal.
1032    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
1033        let normalized = refusal_wording(status, body).to_ascii_lowercase();
1034        if SECONDARY_WORDINGS
1035            .iter()
1036            .any(|wording| normalized.contains(wording))
1037        {
1038            return Some(Self::Secondary);
1039        }
1040        if status == StatusCode::TOO_MANY_REQUESTS {
1041            return Some(Self::Primary);
1042        }
1043        // An exhausted budget *explains* a response that failed; it does not make one that
1044        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
1045        // request the budget allowed as well as on the ones it then refuses, so reading
1046        // the header alone threw away a good answer — and, once refusals were retried,
1047        // replayed a request that had already taken effect.
1048        if !status.is_success() && budget_exhausted {
1049            return Some(Self::Primary);
1050        }
1051        // A successful response saying it: GitHub reports a GraphQL rate limit in the
1052        // `errors` of an HTTP 200, where nothing about the status says so at all.
1053        if status.is_success()
1054            && PRIMARY_WORDINGS
1055                .iter()
1056                .any(|wording| normalized.contains(wording))
1057        {
1058            return Some(Self::Primary);
1059        }
1060        None
1061    }
1062
1063    /// What this limiter is called where an operator can look it up.
1064    const fn name(self) -> &'static str {
1065        match self {
1066            Self::Primary => "GitHub's primary API rate limit",
1067            Self::Secondary => "GitHub's secondary rate limit",
1068        }
1069    }
1070
1071    /// What the endpoint an operator would go and check says about this limiter.
1072    const fn where_to_look(self) -> &'static str {
1073        match self {
1074            Self::Primary => {
1075                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
1076                 comes back."
1077            }
1078            Self::Secondary => {
1079                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
1080                 primary budget and does not report this one, so budget showing there says \
1081                 nothing about this refusal, and every further attempt extends it."
1082            }
1083        }
1084    }
1085
1086    /// The next step this limiter actually calls for.
1087    const fn what_to_do(self) -> &'static str {
1088        match self {
1089            Self::Primary => {
1090                "wait for the reset `gh api rate_limit` reports, then run the command again."
1091            }
1092            Self::Secondary => {
1093                "leave this board alone for a few minutes, then run the command again — or \
1094                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
1095            }
1096        }
1097    }
1098}
1099
1100/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
1101#[derive(Debug, Clone, Copy)]
1102struct Limited {
1103    limiter: Limiter,
1104    hint: Option<u64>,
1105}
1106
1107impl Limited {
1108    /// What the caller is told once this source has waited as long as it may.
1109    ///
1110    /// Both limiters report as [`SourceError::RateLimited`], because that is what
1111    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
1112    /// about *which* limiter it was makes it a different kind of failure. What differs is
1113    /// the operator's next step, and that is what the message carries — a secondary
1114    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
1115    /// budget looks fine, and then back to retry the very burst that was refused.
1116    fn exhausted(
1117        self,
1118        doing: &str,
1119        waits: u32,
1120        waited: Duration,
1121        needed: Duration,
1122        budget: Duration,
1123    ) -> SourceError {
1124        SourceError::RateLimited {
1125            retry_after_seconds: self.hint,
1126            message: Some(format!(
1127                "{} refused this source while {doing}; it waited {} out over {} and was refused \
1128                 again, and the next wait of {} would take it past the {} one call may spend \
1129                 waiting. {} next: {}",
1130                self.limiter.name(),
1131                plural(waits, "refusal"),
1132                seconds(waited),
1133                seconds(needed),
1134                seconds(budget),
1135                self.limiter.where_to_look(),
1136                self.limiter.what_to_do(),
1137            )),
1138        }
1139    }
1140}
1141
1142/// One HTTP attempt's result, with what its response said about the rate limit.
1143///
1144/// The two travel together so the record and the outcome are written from the same place:
1145/// what a response said about the budget is only readable while that response is in hand,
1146/// and what the attempt *meant* is only decidable once its body has been read.
1147struct Attempted {
1148    result: Result<Value, Attempt>,
1149    limits: accounting::RateLimit,
1150    /// GitHub's own reported cost for this call, for a document that asked for it.
1151    reported_cost: Option<u64>,
1152}
1153
1154/// One attempt's outcome: an error to report, or a rate limit to wait out.
1155enum Attempt {
1156    Failed(SourceError),
1157    Limited(Limited),
1158}
1159
1160fn plural(count: u32, thing: &str) -> String {
1161    if count == 1 {
1162        format!("{count} {thing}")
1163    } else {
1164        format!("{count} {thing}s")
1165    }
1166}
1167
1168fn seconds(duration: Duration) -> String {
1169    format!("{:.1}s", duration.as_secs_f64())
1170}
1171
1172/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
1173///
1174/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
1175/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
1176/// header, and neither is what makes a response a refusal — so the whole cost of one this
1177/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
1178/// instead. Refusing the response over the header would turn a readable refusal into an
1179/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
1180fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
1181    value
1182        .and_then(|value| value.to_str().ok())
1183        .and_then(|value| value.trim().parse::<u64>().ok())
1184}
1185
1186/// Every mutation this source sends creates content — an issue, a board item, a field of
1187/// one, a sub-issue link, a dependency, a comment — or edits or removes content of that
1188/// kind, and no query in [`graphql::DOCUMENTS`] does, so what the secondary limiter counts
1189/// and what the keyword says are the same set. That is what makes the keyword a sound test
1190/// rather than a convenient one: pacing an edit or a removal the limiter might not have
1191/// counted costs a wait, and not pacing one it did count costs the next fifty minutes.
1192fn is_mutation(query: &str) -> bool {
1193    query.trim_start().starts_with("mutation")
1194}
1195
1196/// What this source was doing, for a diagnostic that has to say so.
1197///
1198/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
1199/// a document added without a description is caught by that list's own gate instead of
1200/// falling through to the vague arm below.
1201fn operation_description(query: &str) -> &'static str {
1202    graphql::DOCUMENTS
1203        .iter()
1204        .find(|(document, _)| *document == query)
1205        .map_or("talking to GitHub", |(_, doing)| *doing)
1206}
1207
1208/// GitHub's published ceiling on content-generating requests, per minute.
1209///
1210/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
1211/// GitHub's number rather than this source's: [`MIN_MUTATION_INTERVAL_MS`] is *derived*
1212/// from it, so a pacing value checked only against itself cannot go stale here.
1213pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
1214/// The same ceiling as GitHub publishes it per hour, which this source does **not** pace
1215/// at. See [`MIN_MUTATION_INTERVAL_MS`] for why the per-minute bound is the one that
1216/// governs; it is pinned beside its sibling so the gate would notice either one moving.
1217pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
1218/// Shortest interval between two content-creating mutations, in milliseconds.
1219///
1220/// GitHub documents two secondary limits on content-generating requests:
1221/// [`CONTENT_CREATION_PER_MINUTE`] and [`CONTENT_CREATION_PER_HOUR`]. 60000/80 is 750, so
1222/// a mutation every 750 ms is the fastest rate that cannot exceed the per-minute bound,
1223/// and that is the bound a copy actually trips: a copy of one plan-sized project is a
1224/// burst of a few dozen mutations inside a few seconds. The hourly bound works out at one
1225/// every 7.2 seconds sustained, which no single copy reaches and which, used as the
1226/// spacing here, would turn an ordinary copy into an hour of waiting — so it is
1227/// deliberately *not* what this paces at. An installation that wants the hourly bound
1228/// honoured for a long sequence of copies says so through
1229/// `pacing.min_mutation_interval_ms`.
1230pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
1231/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
1232///
1233/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
1234/// own advice for a secondary limit — wait, and wait longer each time — without spending
1235/// the first minute of a transient refusal doing nothing.
1236pub const RETRY_BACKOFF_MS: u64 = 1_000;
1237/// Total time one call may spend waiting out rate limits before it reports a failure.
1238///
1239/// Two minutes is long enough to ride out the refusals a paced copy still collects and
1240/// short enough that a command an operator is watching returns. The bound is what makes
1241/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
1242/// the limiter, not in a process nobody can tell from a wedged one.
1243pub const RETRY_BUDGET_MS: u64 = 120_000;
1244
1245fn default_token_env() -> String {
1246    "GH_PROJECTS_TOKEN".to_owned()
1247}
1248fn default_endpoint() -> String {
1249    "https://api.github.com/graphql".to_owned()
1250}
1251
1252/// Where one status category lands on this board.
1253///
1254/// `null` — an absent value — disables the category for this instance, and using a
1255/// disabled status is a refusal naming the status and the instance.
1256#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
1257#[serde(untagged)]
1258pub enum StatusTargetConfig {
1259    /// The name of a `Status` single-select option already on the board.
1260    Column(ColumnName),
1261}
1262
1263/// The name of a `Status` single-select option on the board.
1264///
1265/// Validated on the way in rather than checked later, so a blank option name — which
1266/// nothing on a board can be — is a state this type cannot hold.
1267#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1268#[serde(try_from = "String")]
1269#[schemars(extend("minLength" = 1))]
1270pub struct ColumnName(String);
1271
1272impl ColumnName {
1273    /// The option name, as the board spells it.
1274    fn as_str(&self) -> &str {
1275        &self.0
1276    }
1277}
1278
1279impl TryFrom<String> for ColumnName {
1280    type Error = String;
1281
1282    fn try_from(name: String) -> Result<Self, Self::Error> {
1283        if name.trim().is_empty() {
1284            return Err("a status_mapping option name cannot be blank".to_owned());
1285        }
1286        Ok(Self(name))
1287    }
1288}
1289
1290/// The two closed states this product can mean.
1291///
1292/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which is neither finished
1293/// work nor abandoned work, so nothing here ever writes it.
1294#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
1295#[serde(rename_all = "kebab-case")]
1296pub enum ClosedState {
1297    /// `COMPLETED` — precisely done.
1298    Completed,
1299    /// `NOT_PLANNED` — precisely cancelled.
1300    NotPlanned,
1301}
1302
1303impl ClosedState {
1304    const fn reason(self) -> &'static str {
1305        match self {
1306            Self::Completed => "COMPLETED",
1307            Self::NotPlanned => "NOT_PLANNED",
1308        }
1309    }
1310}
1311
1312/// Configuration for one GitHub Projects v2 board.
1313#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1314#[serde(default, deny_unknown_fields)]
1315pub struct GitHubProjectsConfig {
1316    /// Login of the user or organization which owns the board.
1317    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
1318    /// The project number shown in the board's GitHub URL.
1319    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
1320    // 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.
1321    /// `owner/name` of the repository this source creates an issue in when the item's own
1322    /// `repositories` field does not decide it.
1323    ///
1324    /// An item naming exactly one repository is created there; a task or a document naming
1325    /// none or several is created in its parent project's repository; and a project, or a
1326    /// task or document with no parent, naming none or several is created here. A board
1327    /// has no repository of its own and `createIssue` requires one, so a write without
1328    /// this is refused naming the field. Reads never need it.
1329    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
1330    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
1331    /// Environment variable containing a fine-grained token with Projects and Issues
1332    /// read/write plus Pull requests read-only access for every repository represented on
1333    /// the board.
1334    #[serde(default = "default_token_env")]
1335    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
1336    /// GraphQL endpoint. GitHub Enterprise installations may override it.
1337    #[serde(default = "default_endpoint")]
1338    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
1339    /// Per-instance mapping from a status category to where it lands on this board.
1340    ///
1341    /// A category this does not mention keeps its shipped default: `backlog` to
1342    /// "Backlog", `todo` to "Todo", `queued` to "Queued", `in-progress` to "In Progress",
1343    /// `done` to "Done" plus closed as completed, `cancelled` to "Cancelled" plus closed
1344    /// as not planned, and `draft` and `unknown` disabled. `unknown` may name one existing
1345    /// board option; every unknown word then lands on that option and reads back as
1346    /// `unknown` under its name. Unlike `local-md`, this source cannot keep each unknown
1347    /// word because it never creates board options.
1348    #[serde(default)]
1349    pub status_mapping: BTreeMap<String, Option<StatusTargetConfig>>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` parses each key into a `StatusCategory` and reports an unknown one against this instance.
1350    /// Per-instance mapping from a task's priority to an option of this board's
1351    /// single-select field named `Priority`.
1352    ///
1353    /// Absent, this source holds no priority: every task reads as `none`, and a write of any
1354    /// other priority is refused before it reaches this board. Present, each of `urgent`,
1355    /// `high`, `medium` and `low` it does not mention keeps its shipped default — `Urgent`,
1356    /// `High`, `Medium` and `Low` — and an item with no value in the `Priority` field reads
1357    /// as `none`, so writing `none` clears the value. Option names match case-insensitively;
1358    /// no two levels may name one option. Reads and writes never create the field or an
1359    /// option: `onetaskgraph sources fields <source> --apply` does, and a write naming one
1360    /// the board lacks is refused pointing there.
1361    #[serde(default)]
1362    pub priority_mapping: Option<PriorityMappingConfig>,
1363    /// How fast this source writes, and how long it waits out a rate-limit refusal.
1364    ///
1365    /// Every field keeps its shipped default when it is absent, and the defaults are
1366    /// GitHub's own published limits rather than taste. See [`Pacing`].
1367    #[serde(default)]
1368    pub pacing: PacingConfig,
1369}
1370
1371/// Which option of the board's `Priority` field each priority lands on.
1372///
1373/// One member per level rather than a map, so a key that is not a level is refused where
1374/// the configuration is read, naming the levels there are. `none` is not a member: it is no
1375/// value in the field, not an option of it.
1376#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1377#[serde(default, deny_unknown_fields)]
1378pub struct PriorityMappingConfig {
1379    /// The option `urgent` lands on; `Urgent` when absent.
1380    pub urgent: Option<PriorityOptionName>,
1381    /// The option `high` lands on; `High` when absent.
1382    pub high: Option<PriorityOptionName>,
1383    /// The option `medium` lands on; `Medium` when absent.
1384    pub medium: Option<PriorityOptionName>,
1385    /// The option `low` lands on; `Low` when absent.
1386    pub low: Option<PriorityOptionName>,
1387}
1388
1389/// The name of an option of the board's `Priority` single-select field.
1390///
1391/// Validated on the way in, for the reason [`ColumnName`] is: nothing on a board can have a
1392/// blank name.
1393#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
1394#[serde(try_from = "String")]
1395#[schemars(extend("minLength" = 1))]
1396pub struct PriorityOptionName(String);
1397
1398impl PriorityOptionName {
1399    /// The option name, as the board spells it.
1400    fn as_str(&self) -> &str {
1401        &self.0
1402    }
1403}
1404
1405impl TryFrom<String> for PriorityOptionName {
1406    type Error = String;
1407
1408    fn try_from(name: String) -> Result<Self, Self::Error> {
1409        if name.trim().is_empty() {
1410            return Err("a priority_mapping option name cannot be blank".to_owned());
1411        }
1412        Ok(Self(name))
1413    }
1414}
1415
1416/// The name of the board field a priority is held in.
1417pub const PRIORITY_FIELD: &str = "Priority";
1418
1419/// The four priorities a board option can hold, in the order a new `Priority` field lists
1420/// them. `none` is not among them: it is the field holding no value.
1421///
1422/// This list mirrors `Priority`, so it carries its own drift gate, in the shape [`CATEGORIES`]
1423/// does: [`level_position`] is a wildcard-free match, so a priority added to the shared
1424/// vocabulary fails to compile until it is placed there, and this crate's suite reconciles
1425/// this list and [`PriorityMappingConfig`]'s members against that enum's own derived schema.
1426pub const PRIORITY_LEVELS: [Priority; 4] = [
1427    Priority::Urgent,
1428    Priority::High,
1429    Priority::Medium,
1430    Priority::Low,
1431];
1432
1433/// Where one priority sits in [`PRIORITY_LEVELS`], or `None` for `none`, which is no option;
1434/// see that list for what this pins.
1435#[must_use]
1436pub const fn level_position(priority: Priority) -> Option<usize> {
1437    match priority {
1438        Priority::None => None,
1439        Priority::Urgent => Some(0),
1440        Priority::High => Some(1),
1441        Priority::Medium => Some(2),
1442        Priority::Low => Some(3),
1443    }
1444}
1445
1446/// This instance's complete priority-to-option mapping, read in both directions.
1447///
1448/// One option per level, held in [`PRIORITY_LEVELS`] order, once it is established that no
1449/// two levels name one option.
1450#[derive(Debug, Clone)]
1451struct PriorityMapping {
1452    options: [PriorityOptionName; 4],
1453}
1454
1455impl PriorityMapping {
1456    fn resolve(config: PriorityMappingConfig, instance: &SourceName) -> Result<Self, SourceError> {
1457        let shipped = |name: &str| PriorityOptionName(name.to_owned());
1458        let mapping = Self {
1459            options: [
1460                config.urgent.unwrap_or_else(|| shipped("Urgent")),
1461                config.high.unwrap_or_else(|| shipped("High")),
1462                config.medium.unwrap_or_else(|| shipped("Medium")),
1463                config.low.unwrap_or_else(|| shipped("Low")),
1464            ],
1465        };
1466        for (index, option) in mapping.options.iter().enumerate() {
1467            if let Some(earlier) = mapping.options[..index]
1468                .iter()
1469                .position(|other| other.as_str().eq_ignore_ascii_case(option.as_str()))
1470            {
1471                return Err(SourceError::Config {
1472                    message: format!(
1473                        "priority_mapping of source {instance} sends both {} and {} to the board \
1474                         option {:?}; one option cannot read back as two priorities",
1475                        PRIORITY_LEVELS[earlier],
1476                        PRIORITY_LEVELS[index],
1477                        option.as_str()
1478                    ),
1479                });
1480            }
1481        }
1482        Ok(mapping)
1483    }
1484
1485    /// The option `priority` lands on, or `None` for `none`, which is no option at all.
1486    fn option(&self, priority: Priority) -> Option<&str> {
1487        level_position(priority).map(|index| self.options[index].as_str())
1488    }
1489
1490    /// The priority a board option name reports, or `None` when nothing maps to it.
1491    fn priority_of(&self, option: &str) -> Option<Priority> {
1492        self.options
1493            .iter()
1494            .position(|name| name.as_str().eq_ignore_ascii_case(option))
1495            .map(|index| PRIORITY_LEVELS[index])
1496    }
1497
1498    /// Every mapped option name, in the order a new `Priority` field lists them.
1499    fn names(&self) -> impl Iterator<Item = &str> {
1500        self.options.iter().map(PriorityOptionName::as_str)
1501    }
1502}
1503
1504/// What one item's `Priority` field says, read through this instance's mapping.
1505#[derive(Debug, Clone, PartialEq, Eq)]
1506enum HeldPriority {
1507    /// A priority this source reports: an option the mapping names, or no value (`none`).
1508    Read(Priority),
1509    /// An option the mapping does not name, which is never read as a level or as `none`.
1510    Unmapped(String),
1511}
1512
1513/// How fast this source writes, and how long it waits out a rate-limit refusal.
1514///
1515/// Configurable because a GitHub Enterprise installation sets its own limits and an
1516/// operator who has already been refused may want to go slower still — not because the
1517/// defaults are guesses.
1518#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1519#[serde(default, deny_unknown_fields)]
1520pub struct PacingConfig {
1521    /// Shortest interval between two content-creating mutations, in milliseconds.
1522    ///
1523    /// Zero sends them as fast as they are asked for, which is what a fixture server on
1524    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
1525    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.
1526    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
1527    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
1528    /// zero while there is a budget to spend, because a schedule of zero-length waits
1529    /// consumes none of it and so never ends.
1530    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.
1531    /// Total time one call may spend waiting out rate limits, in milliseconds.
1532    ///
1533    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
1534    /// the bound is what makes this a wait rather than a hang.
1535    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.
1536}
1537
1538/// The largest any pacing setting may be, in milliseconds.
1539///
1540/// One hour. GitHub's own harshest published bound on content-generating requests works
1541/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
1542/// anything a real limit asks for, and past it the settings stop describing pacing at all:
1543/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
1544/// and an interval beyond it is a command that never sends its second mutation. It also
1545/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
1546/// what an `Instant` can hold on every platform.
1547pub const MAX_PACING_MS: u64 = 3_600_000;
1548
1549/// [`PacingConfig`] with every default resolved and every value checked, which is what the
1550/// source holds.
1551#[derive(Debug, Clone, Copy)]
1552struct Pacing {
1553    min_mutation_interval: Duration,
1554    retry_backoff: Duration,
1555    retry_budget: Duration,
1556}
1557
1558impl Pacing {
1559    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
1560    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
1561        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
1562            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
1563                message: format!(
1564                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
1565                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
1566                     GitHub's own harshest published limit"
1567                ),
1568            }),
1569            Some(value) => Ok(Duration::from_millis(value)),
1570            None => Ok(Duration::from_millis(default)),
1571        };
1572        let retry_backoff = bounded(
1573            config.retry_backoff_ms,
1574            RETRY_BACKOFF_MS,
1575            "retry_backoff_ms",
1576        )?;
1577        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
1578        if retry_backoff.is_zero() && !retry_budget.is_zero() {
1579            return Err(SourceError::Config {
1580                message: format!(
1581                    "pacing.retry_backoff_ms of source {instance} is 0 while \
1582                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
1583                     none of that budget, so it would retry a refusal forever. Set a backoff of \
1584                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
1585                     waiting at all",
1586                    retry_budget.as_millis()
1587                ),
1588            });
1589        }
1590        Ok(Self {
1591            min_mutation_interval: bounded(
1592                config.min_mutation_interval_ms,
1593                MIN_MUTATION_INTERVAL_MS,
1594                "min_mutation_interval_ms",
1595            )?,
1596            retry_backoff,
1597            retry_budget,
1598        })
1599    }
1600}
1601
1602/// Factory for [`GitHubProjectsSource`].
1603#[derive(Debug, Clone, Copy, Default)]
1604pub struct Plugin;
1605
1606impl SourcePlugin for Plugin {
1607    fn kind(&self) -> &'static str {
1608        KIND
1609    }
1610    fn config_schema(&self) -> Schema {
1611        schema_for!(GitHubProjectsConfig)
1612    }
1613    fn build(
1614        &self,
1615        name: &SourceName,
1616        config: &Value,
1617        secrets: &dyn SecretResolver,
1618    ) -> Result<Box<dyn TaskSource>, SourceError> {
1619        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
1620    }
1621}
1622
1623impl Plugin {
1624    /// Build a source recording every request it sends into an accounting the caller holds.
1625    ///
1626    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
1627    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
1628    /// session total rather than two — see [`accounting`] and
1629    /// [`GitHubProjectsSource::recording_into`].
1630    ///
1631    /// # Errors
1632    ///
1633    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
1634    /// [`SourceError::Config`] for configuration this plugin cannot use and
1635    /// [`SourceError::Auth`] for a credential it cannot find.
1636    pub fn build_recording_into(
1637        &self,
1638        name: &SourceName,
1639        config: &Value,
1640        secrets: &dyn SecretResolver,
1641        ledger: Arc<Accounting>,
1642    ) -> Result<Box<dyn TaskSource>, SourceError> {
1643        let config: GitHubProjectsConfig =
1644            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
1645                message: format!("source {name}: {e}"),
1646            })?;
1647        let source = GitHubProjectsSource::recording_into(name, config, secrets, ledger).map_err(
1648            |error| match error {
1649                SourceError::Config { message } => SourceError::Config {
1650                    message: format!("source {name}: {message}"),
1651                },
1652                SourceError::Auth { message } => SourceError::Auth {
1653                    message: format!("source {name}: {message}"),
1654                },
1655                other => other,
1656            },
1657        )?;
1658        Ok(Box::new(source))
1659    }
1660}
1661
1662/// Where a status category lands on this board, once configuration is resolved.
1663#[derive(Debug, Clone, PartialEq, Eq)]
1664enum StatusTarget {
1665    /// Not usable against this instance.
1666    Disabled,
1667    /// The board's `Status` option of this name.
1668    Column(ColumnName),
1669    /// A closed issue, with both its board option and the reason that says which closed it means.
1670    // 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, `StatusMapping::new`, 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.
1671    Terminal(ColumnName, ClosedState),
1672}
1673
1674/// Every status category, in the order the vocabulary declares them.
1675///
1676/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
1677/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
1678/// added to the shared vocabulary fails to compile until it is named there, and this
1679/// crate's suite reconciles this list against that enum's own derived schema, which is
1680/// generated from the variants rather than written beside them. The schema is what
1681/// catches a list left one short — a list checking only the positions it already holds
1682/// would pass while every mapping indexed by the new position panicked.
1683pub const CATEGORIES: [StatusCategory; 8] = [
1684    StatusCategory::Draft,
1685    StatusCategory::Backlog,
1686    StatusCategory::Todo,
1687    StatusCategory::Queued,
1688    StatusCategory::InProgress,
1689    StatusCategory::Done,
1690    StatusCategory::Cancelled,
1691    StatusCategory::Unknown,
1692];
1693
1694/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
1695#[must_use]
1696pub const fn category_position(category: StatusCategory) -> usize {
1697    match category {
1698        StatusCategory::Draft => 0,
1699        StatusCategory::Backlog => 1,
1700        StatusCategory::Todo => 2,
1701        StatusCategory::Queued => 3,
1702        StatusCategory::InProgress => 4,
1703        StatusCategory::Done => 5,
1704        StatusCategory::Cancelled => 6,
1705        StatusCategory::Unknown => 7,
1706    }
1707}
1708
1709/// The spelling a status category is configured and reported under.
1710fn category_name(category: StatusCategory) -> &'static str {
1711    match category {
1712        StatusCategory::Draft => "draft",
1713        StatusCategory::Backlog => "backlog",
1714        StatusCategory::Todo => "todo",
1715        StatusCategory::Queued => "queued",
1716        StatusCategory::InProgress => "in-progress",
1717        StatusCategory::Done => "done",
1718        StatusCategory::Cancelled => "cancelled",
1719        StatusCategory::Unknown => "unknown",
1720    }
1721}
1722
1723/// A shipped default's option name.
1724///
1725/// The literals below are this file's own and non-blank, and they are validated by the
1726/// one constructor a configured name goes through rather than beside it.
1727fn shipped_column(name: &'static str) -> ColumnName {
1728    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
1729}
1730
1731/// The shipped default for one category, before this instance's configuration.
1732fn shipped_default(category: StatusCategory) -> StatusTarget {
1733    match category {
1734        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
1735        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
1736        StatusCategory::Queued => StatusTarget::Column(shipped_column("Queued")),
1737        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
1738        StatusCategory::Done => {
1739            StatusTarget::Terminal(shipped_column("Done"), ClosedState::Completed)
1740        }
1741        StatusCategory::Cancelled => {
1742            StatusTarget::Terminal(shipped_column("Cancelled"), ClosedState::NotPlanned)
1743        }
1744        StatusCategory::Draft | StatusCategory::Unknown => StatusTarget::Disabled,
1745    }
1746}
1747
1748/// This instance's complete category-to-target mapping, read in both directions.
1749///
1750/// One target per category, held at that category's own [`category_position`], so a
1751/// category missing from the mapping, named twice in it, or filed out of order is a
1752/// state this type cannot hold rather than one [`Self::target`] has to defend against.
1753#[derive(Debug, Clone)]
1754struct StatusMapping {
1755    targets: [StatusTarget; CATEGORIES.len()],
1756}
1757
1758impl StatusMapping {
1759    fn resolve(
1760        configured: BTreeMap<String, Option<StatusTargetConfig>>,
1761        instance: &SourceName,
1762    ) -> Result<Self, SourceError> {
1763        let mut overrides: BTreeMap<&'static str, Option<StatusTargetConfig>> = BTreeMap::new();
1764        for (key, value) in configured {
1765            let category = CATEGORIES
1766                .iter()
1767                .find(|category| category_name(**category) == key)
1768                .ok_or_else(|| SourceError::Config {
1769                    message: format!(
1770                        "status_mapping names {key:?}, which is not a status category of source \
1771                         {instance}; the categories are {}",
1772                        CATEGORIES
1773                            .iter()
1774                            .map(|category| category_name(*category))
1775                            .collect::<Vec<_>>()
1776                            .join(", ")
1777                    ),
1778                })?;
1779            overrides.insert(category_name(*category), value);
1780        }
1781        // `CATEGORIES[position] == category` for every category — the crate's suite
1782        // asserts it — so mapping the list in order fills each category's own slot.
1783        let targets = CATEGORIES.map(|category| match overrides.remove(category_name(category)) {
1784            None => shipped_default(category),
1785            Some(None) => StatusTarget::Disabled,
1786            Some(Some(StatusTargetConfig::Column(option))) => match category {
1787                StatusCategory::Done => StatusTarget::Terminal(option, ClosedState::Completed),
1788                StatusCategory::Cancelled => {
1789                    StatusTarget::Terminal(option, ClosedState::NotPlanned)
1790                }
1791                _ => StatusTarget::Column(option),
1792            },
1793        });
1794        let mapping = Self { targets };
1795        for (index, category) in CATEGORIES.into_iter().enumerate() {
1796            let option = match mapping.target(category) {
1797                StatusTarget::Column(option) | StatusTarget::Terminal(option, _) => option,
1798                StatusTarget::Disabled => continue,
1799            };
1800            if let Some(other) = CATEGORIES[..index].iter().find(|earlier| {
1801                matches!(mapping.target(**earlier), StatusTarget::Column(name) | StatusTarget::Terminal(name, _)
1802                    if name.as_str().eq_ignore_ascii_case(option.as_str()))
1803            }) {
1804                return Err(SourceError::Config {
1805                    message: format!(
1806                        "status_mapping of source {instance} sends both {} and {} to the board \
1807                         option {:?}; one option cannot read back as two categories",
1808                        category_name(*other),
1809                        category_name(category),
1810                        option.as_str()
1811                    ),
1812                });
1813            }
1814        }
1815        Ok(mapping)
1816    }
1817
1818    fn target(&self, category: StatusCategory) -> &StatusTarget {
1819        &self.targets[category_position(category)]
1820    }
1821
1822    /// The category a board option name reports, or `None` when nothing maps to it.
1823    fn category_of(&self, option: &str) -> Option<StatusCategory> {
1824        CATEGORIES.into_iter().find(|category| {
1825            matches!(self.target(*category), StatusTarget::Column(name) | StatusTarget::Terminal(name, _)
1826                if name.as_str().eq_ignore_ascii_case(option))
1827        })
1828    }
1829
1830    /// The status an item reports, from the three things a read of it says: its board
1831    /// `Status` option, whether its issue is closed, and the reason it was closed with.
1832    ///
1833    /// The closed state decides the category and the `Status` option decides the name, so
1834    /// a closed issue sitting in a "Shipped" column reports `done` named `Shipped`. A
1835    /// closed issue whose reason is `DUPLICATE` or `REOPENED` reports `Unknown`: a
1836    /// duplicate is not finished work, and calling it done is a lie the next copy would
1837    /// write back. `REOPENED`-while-closed is a state this source can never produce, so
1838    /// it is read permissively rather than refused — reads are faithful, and refusals
1839    /// belong on writes.
1840    ///
1841    /// One function of those three rather than of a response, so a narrow status write can
1842    /// answer what a re-read would report by applying it to the state it has just written.
1843    fn status(&self, option: Option<&str>, closed: bool, reason: Option<&str>) -> Status {
1844        if closed {
1845            let category = match reason {
1846                None | Some("COMPLETED") => StatusCategory::Done,
1847                Some("NOT_PLANNED") => StatusCategory::Cancelled,
1848                Some(_) => StatusCategory::Unknown,
1849            };
1850            let fallback = match category {
1851                StatusCategory::Done => "Done",
1852                StatusCategory::Cancelled => "Cancelled",
1853                _ => "Closed",
1854            };
1855            return Status {
1856                category,
1857                name: option.unwrap_or(fallback).to_owned(),
1858            };
1859        }
1860        let name = option.unwrap_or("Open").to_owned();
1861        Status {
1862            category: self.category_of(&name).unwrap_or(StatusCategory::Unknown),
1863            name,
1864        }
1865    }
1866}
1867
1868// 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.
1869/// One repository this source can create an issue in, as `owner/name`.
1870///
1871/// Every `createIssue` this source sends names one of these: the item's own single
1872/// `repositories` entry, else its parent project issue's repository, else the configured
1873/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
1874/// that choice and says what it refuses before `createIssue`.
1875// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
1876#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
1877struct RepositoryTarget {
1878    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
1879    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
1880}
1881
1882impl RepositoryTarget {
1883    fn parse(value: &str) -> Result<Self, SourceError> {
1884        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
1885            message: format!(
1886                "repository must be spelled owner/name; {value:?} names no repository"
1887            ),
1888        })?;
1889        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
1890            return Err(SourceError::Config {
1891                message: format!(
1892                    "repository must be spelled owner/name with a GitHub login and one \
1893                     repository name; {value:?} is not"
1894                ),
1895            });
1896        }
1897        Ok(Self {
1898            owner: owner.to_owned(),
1899            name: name.to_owned(),
1900        })
1901    }
1902
1903    /// The one host whose repositories this source creates issues in, spelled once: it is
1904    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
1905    const HOST: &str = "github.com";
1906
1907    fn origin(&self) -> String {
1908        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
1909    }
1910
1911    /// The repository a normalized origin names, or why it is none this source can create
1912    /// an issue in: another host, or more or fewer than `owner/name` under this one.
1913    fn from_origin(origin: &Repository) -> Result<Self, String> {
1914        let not_here = || {
1915            format!(
1916                "{} is not a {}/owner/name repository",
1917                origin.as_str(),
1918                Self::HOST
1919            )
1920        };
1921        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
1922        if host != Self::HOST {
1923            return Err(not_here());
1924        }
1925        Self::parse(rest).map_err(|_| not_here())
1926    }
1927
1928    fn slug(&self) -> String {
1929        format!("{}/{}", self.owner, self.name)
1930    }
1931}
1932
1933/// A source which reads GitHub afresh for every operation.
1934pub struct GitHubProjectsSource {
1935    /// This source's configured name, used both to tell a far end naming this source
1936    /// from one naming a system it knows nothing about, and to name the instance a
1937    /// status refusal is about.
1938    name: SourceName,
1939    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
1940    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
1941    repository: Option<RepositoryTarget>,
1942    endpoint: Url,
1943    token: SecretString,
1944    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
1945    statuses: StatusMapping,
1946    /// Where each priority lands on this board, or `None` when this instance holds none.
1947    priorities: Option<PriorityMapping>,
1948    client: Client,
1949    /// Every item this source has created since it was built, in the order it created
1950    /// them.
1951    ///
1952    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
1953    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
1954    /// a copy resolving a dependency on an item it had just created refused it as not
1955    /// found. A board read is completed from this — an item remembered here and absent from
1956    /// the read is added back, because the board really does hold it and only the read is
1957    /// behind.
1958    ///
1959    /// It is not a cache of a user's work: nothing is remembered that this process did not
1960    /// itself just write, it lives and dies with the process, and it is never consulted for
1961    /// an item this source did not create.
1962    created: Mutex<Vec<Resolved>>,
1963    /// How fast this source writes, and how long it waits out a refusal.
1964    pacing: Pacing,
1965    /// When the last content-creating mutation finished, or the moment the furthest-out
1966    /// reserved slot releases the next one, whichever is later — so the one after it can be
1967    /// spaced from that. See [`MIN_MUTATION_INTERVAL_MS`] for the interval and
1968    /// [`GitHubProjectsSource::finish_mutation`] for why completion rather than release is
1969    /// what it is measured from.
1970    last_mutation: Mutex<Option<Instant>>,
1971    /// The board as this process last read it, for the length of one command.
1972    ///
1973    /// A copy of a project used to re-read the whole board, paged, before writing each of
1974    /// its items, which is by far the largest part of a copy's request count and none of
1975    /// its work. Nothing else changes this board while a command runs — this source's own
1976    /// writes are the only writer — so one read answers them all.
1977    ///
1978    /// It is not a store of a user's work and it is not the cache the no-persistence
1979    /// invariant forbids: it lives and dies with the process exactly as `created` does,
1980    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
1981    /// an item this command created and then depends on resolves whether or not GitHub's
1982    /// own eventually-consistent read has caught up. A write to an item already on the
1983    /// board updates the entry here too, so what this holds is the last read plus this
1984    /// process's own writes rather than a snapshot taken before them.
1985    board_cache: Mutex<Option<Board>>,
1986    /// Every issue this board's own search reported, for the length of one command.
1987    ///
1988    /// The second half of a board read, and cached for the same reason and on the same
1989    /// terms as the first: it lives and dies with the process, nothing is written down, and
1990    /// a write this process makes updates the entry here exactly as it updates the one in
1991    /// [`Self::board_cache`]. One read answers every question a command asks, so a command
1992    /// that lists this board's projects and its tasks pays for one search rather than two.
1993    search_cache: Mutex<Option<Vec<Resolved>>>,
1994    /// The board's own id and field definitions as this process last read them on their
1995    /// own, for the length of one command.
1996    ///
1997    /// What a write needs of the board and its item does not say, read once per command
1998    /// rather than once per item written, on the terms [`Self::board_cache`] is held on: it
1999    /// lives and dies with the process and nothing is written down. It holds no item and so
2000    /// can answer no question about one — see [`Self::board_fields`].
2001    fields_cache: Mutex<Option<BoardFields>>,
2002    /// Each destination repository's node id, resolved once per repository
2003    /// rather than per issue created.
2004    ///
2005    /// A repository's node id does not change, and re-reading it for every issue of a copy
2006    /// spent one request per item on an answer this source already had. It is a map rather
2007    /// than one entry because a copy files each item in the repository its own
2008    /// `repositories` field names, so a plan across five repositories asks GitHub five
2009    /// times and not once per item.
2010    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
2011    /// What every request this source sends is recorded into.
2012    ///
2013    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
2014    /// a request leaves this crate, so nothing has to be switched on for a session to be
2015    /// counted. It is shared rather than owned so a caller accounting for a whole session —
2016    /// its own schema verification, board lookups, residue sweep and cleanup beside this
2017    /// source's reads and writes — adds up one accounting instead of two. See
2018    /// [`accounting`] for what a record carries and what a session's spend is and is not.
2019    ledger: Arc<Accounting>,
2020}
2021
2022/// GitHub's closed single-select color vocabulary.
2023#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, schemars::JsonSchema)]
2024#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
2025pub enum StatusOptionColor {
2026    /// Gray.
2027    Gray,
2028    /// Blue.
2029    Blue,
2030    /// Green.
2031    Green,
2032    /// Yellow.
2033    Yellow,
2034    /// Purple.
2035    Purple,
2036    /// Red.
2037    Red,
2038    /// Orange.
2039    Orange,
2040    /// Pink.
2041    Pink,
2042}
2043
2044/// Whether a guarded board setup — of the fields, or of the Status options alone — plans or
2045/// applies its additions.
2046#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2047pub enum SetupMode {
2048    /// Read without mutation.
2049    Plan,
2050    /// Apply and verify.
2051    Apply,
2052}
2053
2054/// The name [`SetupMode`] had when Status was the one field set up, kept so a caller written
2055/// against it goes on compiling.
2056pub type StatusOptionsMode = SetupMode;
2057
2058/// The explicit result of the requested operation.
2059#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2060#[serde(rename_all = "kebab-case")]
2061pub enum StatusOptionsOutcome {
2062    /// A read-only plan.
2063    Planned,
2064    /// Apply found nothing missing.
2065    Unchanged,
2066    /// Additions were applied and verified.
2067    Applied,
2068}
2069
2070/// A GitHub single-select option's opaque GraphQL node identifier.
2071#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2072#[serde(transparent)]
2073pub struct StatusOptionId(#[schemars(length(min = 1))] String);
2074
2075impl TryFrom<String> for StatusOptionId {
2076    type Error = String;
2077
2078    fn try_from(id: String) -> Result<Self, Self::Error> {
2079        if id.trim().is_empty() {
2080            return Err("a GitHub Status option id cannot be blank".to_owned());
2081        }
2082        Ok(Self(id))
2083    }
2084}
2085
2086/// One existing or proposed option in a guarded Status-field update.
2087#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2088pub struct StatusOption {
2089    /// GitHub's stable id.
2090    pub id: StatusOptionId,
2091    /// The visible option name.
2092    pub name: ColumnName,
2093    /// GitHub's single-select color token.
2094    pub color: StatusOptionColor,
2095    /// The option description, including an empty one.
2096    pub description: String,
2097}
2098
2099/// One board item's Status assignment, retained as recovery data.
2100#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2101pub struct StatusAssignment {
2102    /// The project item id whose assignment this is.
2103    // llmlint: ignore[invalid_states_unrepresentable] This opaque GraphQL node ID is
2104    // carried verbatim as operator recovery data; introducing a semantic type would claim
2105    // validation rules GitHub does not publish and no operation here interprets.
2106    pub item_id: String,
2107    /// The selected option, absent when the item has no status.
2108    #[serde(skip_serializing_if = "Option::is_none")]
2109    pub option: Option<AssignedStatusOption>,
2110}
2111
2112/// The inseparable id and name of an assigned option.
2113#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2114pub struct AssignedStatusOption {
2115    /// GitHub's stable id.
2116    pub id: StatusOptionId,
2117    /// The visible name.
2118    pub name: ColumnName,
2119}
2120
2121/// The plan and verified outcome of reconciling configured Status options.
2122#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2123pub struct StatusOptionsReport {
2124    /// The configured source name.
2125    pub source: SourceName,
2126    /// Configured option names absent before the operation.
2127    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a
2128    // `ColumnName` and has therefore already passed its nonblank validation; retaining the
2129    // serialized string here preserves the report's intentionally simple public contract.
2130    pub missing: Vec<String>,
2131    /// What the requested operation did.
2132    pub outcome: StatusOptionsOutcome,
2133    /// The complete option list observed before any mutation.
2134    pub existing: Vec<StatusOption>,
2135}
2136
2137#[derive(Debug, Clone, PartialEq, Eq)]
2138struct StatusSnapshot {
2139    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2140    // passed back as the mutation's project identity; a newtype could enforce no stronger
2141    // invariant because GitHub publishes no grammar for it.
2142    board_id: String,
2143    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2144    // passed back as the mutation's field identity; a newtype could enforce no stronger
2145    // invariant because GitHub publishes no grammar for it.
2146    field_id: String,
2147    options: Vec<StatusOption>,
2148    assignments: Vec<StatusAssignment>,
2149}
2150
2151/// The name of the board field a status is held in.
2152const STATUS_FIELD: &str = "Status";
2153
2154/// Every item's value of each field `report` names, as it stood before the setup wrote
2155/// anything — what a person puts back when the setup is refused part way.
2156fn recovery(report: &FieldsReport, before: &BoardSnapshot) -> Result<String, SourceError> {
2157    let assignments: BTreeMap<&str, Vec<StatusAssignment>> = report
2158        .fields
2159        .iter()
2160        .map(|field| (field.field.name(), before.assignments(field.field)))
2161        .collect();
2162    serde_json::to_string_pretty(&assignments).map_err(|error| SourceError::Malformed {
2163        message: format!("cannot render the pre-write field recovery snapshot: {error}"),
2164    })
2165}
2166
2167/// One board field the guarded setup reads and writes — every one it reads, and the only
2168/// ones it writes.
2169#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, schemars::JsonSchema)]
2170pub enum BoardField {
2171    /// The single-select `Status` field every instance's `status_mapping` resolves into.
2172    Status,
2173    /// The single-select `Priority` field an instance's `priority_mapping` resolves into.
2174    Priority,
2175}
2176
2177impl BoardField {
2178    /// The field's name on the board.
2179    #[must_use]
2180    pub const fn name(self) -> &'static str {
2181        match self {
2182            Self::Status => STATUS_FIELD,
2183            Self::Priority => PRIORITY_FIELD,
2184        }
2185    }
2186
2187    /// The field a board calls `name`, or `None` for one this setup does not own.
2188    fn named(name: &str) -> Option<Self> {
2189        [Self::Status, Self::Priority]
2190            .into_iter()
2191            .find(|field| field.name() == name)
2192    }
2193}
2194
2195/// What the guarded setup did to one field.
2196#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2197#[serde(rename_all = "kebab-case")]
2198pub enum FieldOutcome {
2199    /// A read-only plan.
2200    Planned,
2201    /// Apply found the field there with every configured option.
2202    Unchanged,
2203    /// Missing options were added to the field that was there, and verified.
2204    Applied,
2205    /// The field was not there; it was created holding the configured options, and verified.
2206    Created,
2207}
2208
2209/// One field's plan, or its verified outcome.
2210#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2211pub struct FieldReport {
2212    /// Which field.
2213    pub field: BoardField,
2214    /// Whether the board had the field before the operation.
2215    // llmlint: ignore[invalid_states_unrepresentable] `exists` beside `outcome` is the report's
2216    // wire shape as its consumer's contract fixes it — `{"field", "exists", "missing",
2217    // "outcome", "existing"}` — so folding one into the other would change a published JSON
2218    // shape. The contradictory pairings cannot be built: `GitHubProjectsSource::fields` is the
2219    // one constructor, and it derives `outcome` from `exists` in one match.
2220    pub exists: bool,
2221    /// Configured option names the field lacked before the operation — every one of them,
2222    /// in the order a new field lists them, when the field was not there at all.
2223    // llmlint: ignore[invalid_states_unrepresentable] Each value originates from a validated
2224    // mapping name and has therefore already passed its nonblank validation; the serialized
2225    // string is the report's intentionally simple public contract, as `StatusOptionsReport`'s is.
2226    pub missing: Vec<String>,
2227    /// What the requested operation did.
2228    pub outcome: FieldOutcome,
2229    /// The field's complete option list observed before any mutation; empty when the field
2230    /// was not there.
2231    pub existing: Vec<StatusOption>,
2232}
2233
2234/// The plan and verified outcome of setting up every field a source's configuration names.
2235#[derive(Debug, Clone, PartialEq, Eq, Serialize, schemars::JsonSchema)]
2236pub struct FieldsReport {
2237    /// The configured source name.
2238    pub source: SourceName,
2239    /// `Status`, always, and `Priority` when the source sets `priority_mapping`.
2240    // llmlint: ignore[invalid_states_unrepresentable] A list is the report's wire shape as its
2241    // consumer's contract fixes it — `{"source", "fields": [...]}` — so a struct with one member
2242    // per field would change a published JSON shape. The states the list could hold and the
2243    // contract forbids cannot be built: `GitHubProjectsSource::fields` is the one constructor,
2244    // and it pushes `Status` first and exactly once, then `Priority` exactly when configured.
2245    pub fields: Vec<FieldReport>,
2246}
2247
2248/// Which options one field is configured with, in the order a new field would list them.
2249struct FieldPlan {
2250    field: BoardField,
2251    wanted: Vec<String>,
2252}
2253
2254/// One single-select field as the guarded setup snapshots it.
2255#[derive(Debug, Clone, PartialEq, Eq)]
2256struct SnapshotField {
2257    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2258    // passed back as the mutation's field identity; a newtype could enforce no stronger
2259    // invariant because GitHub publishes no grammar for it.
2260    field_id: String,
2261    options: Vec<StatusOption>,
2262}
2263
2264/// Every single-select field of a board and every item's value of each.
2265#[derive(Debug, Clone, PartialEq, Eq)]
2266struct BoardSnapshot {
2267    // llmlint: ignore[invalid_states_unrepresentable] This private opaque GraphQL ID is
2268    // passed back as the mutation's project identity; a newtype could enforce no stronger
2269    // invariant because GitHub publishes no grammar for it.
2270    board_id: String,
2271    fields: BTreeMap<BoardField, SnapshotField>,
2272    /// Each board item's id, and its value of each field this setup owns that it holds one of.
2273    items: Vec<(String, BTreeMap<BoardField, AssignedStatusOption>)>,
2274}
2275
2276impl BoardSnapshot {
2277    /// Every item's value of `field`, in board order — the recovery data a drift refusal
2278    /// carries.
2279    fn assignments(&self, field: BoardField) -> Vec<StatusAssignment> {
2280        self.items
2281            .iter()
2282            .map(|(item_id, values)| StatusAssignment {
2283                item_id: item_id.clone(),
2284                option: values.get(&field).cloned(),
2285            })
2286            .collect()
2287    }
2288}
2289
2290impl GitHubProjectsSource {
2291    /// Report missing configured Status options and, when `apply` is true, add them with
2292    /// a whole-list mutation that preserves every existing id and verifies the result.
2293    ///
2294    /// # Errors
2295    ///
2296    /// Refuses a board without a single-select `Status` field. A post-write difference in
2297    /// any pre-existing option id or item assignment is refused with the complete pre-write
2298    /// assignment snapshot in the diagnostic for recovery.
2299    // llmlint: ignore[changed_behavior_has_e2e] The CLI journeys cover plan, no-op apply,
2300    // successful mutation, both drift refusals, source selection, missing Status, casing,
2301    // and paging. Transport errors remain the shared `graphql` boundary's behavior rather
2302    // than a new status-options behavior, and the pinned-schema test prevents valid GitHub
2303    // responses from entering the defensive malformed-response branches below.
2304    pub async fn status_options(
2305        &self,
2306        mode: StatusOptionsMode,
2307    ) -> Result<StatusOptionsReport, SourceError> {
2308        let before = self.status_snapshot().await?;
2309        let configured = self
2310            .statuses
2311            .targets
2312            .iter()
2313            // A terminal category's option is as configured as an open one's: a terminal
2314            // write validates it before closing and refuses when the board lacks it.
2315            .filter_map(|target| match target {
2316                StatusTarget::Column(name) | StatusTarget::Terminal(name, _) => {
2317                    Some(name.as_str().to_owned())
2318                }
2319                StatusTarget::Disabled => None,
2320            });
2321        let missing = configured
2322            .filter(|wanted| {
2323                !before
2324                    .options
2325                    .iter()
2326                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2327            })
2328            .collect::<Vec<_>>();
2329        let report = StatusOptionsReport {
2330            source: self.name.clone(),
2331            missing: missing.clone(),
2332            outcome: match (mode, missing.is_empty()) {
2333                (StatusOptionsMode::Plan, _) => StatusOptionsOutcome::Planned,
2334                (StatusOptionsMode::Apply, true) => StatusOptionsOutcome::Unchanged,
2335                (StatusOptionsMode::Apply, false) => StatusOptionsOutcome::Applied,
2336            },
2337            existing: before.options.clone(),
2338        };
2339        if mode == StatusOptionsMode::Plan || missing.is_empty() {
2340            return Ok(report);
2341        }
2342        let mut options = before
2343            .options
2344            .iter()
2345            .map(|option| {
2346                json!({
2347                    "id": option.id, "name": option.name, "color": option.color,
2348                    "description": option.description,
2349                })
2350            })
2351            .collect::<Vec<_>>();
2352        options.extend(missing.iter().map(|name| {
2353            json!({
2354                "name": name, "color": "GRAY", "description": ""
2355            })
2356        }));
2357        self.graphql(
2358            graphql::STATUS_OPTIONS_UPDATE,
2359            json!({"input": {
2360                "projectId": before.board_id, "fieldId": before.field_id,
2361                "singleSelectOptions": options,
2362            }}),
2363        )
2364        .await?;
2365        let after = self.status_snapshot().await?;
2366        let options_preserved = before
2367            .options
2368            .iter()
2369            .all(|old| after.options.iter().any(|new| new == old));
2370        let additions_present = missing.iter().all(|wanted| {
2371            after
2372                .options
2373                .iter()
2374                .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2375        });
2376        if !options_preserved || !additions_present || after.assignments != before.assignments {
2377            let recovery = serde_json::to_string_pretty(&before.assignments).map_err(|error| {
2378                SourceError::Malformed {
2379                    message: format!("cannot render pre-write Status recovery snapshot: {error}"),
2380                }
2381            })?;
2382            return Err(SourceError::Refused {
2383                message: format!(
2384                    "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}"
2385                ),
2386            });
2387        }
2388        Ok(report)
2389    }
2390
2391    /// A fresh snapshot of the Status field and every board item's assignment of it.
2392    ///
2393    /// # Errors
2394    ///
2395    /// Refuses a board without a single-select `Status` field, and one the token cannot see.
2396    async fn status_snapshot(&self) -> Result<StatusSnapshot, SourceError> {
2397        // Status alone, as this operation has always read it: a `Priority` field is another
2398        // operation's, so nothing about it can refuse this one.
2399        let mut board = self.board_snapshot(&[BoardField::Status]).await?;
2400        let field = board
2401            .fields
2402            .remove(&BoardField::Status)
2403            .ok_or_else(|| self.no_status_field())?;
2404        Ok(StatusSnapshot {
2405            assignments: board.assignments(BoardField::Status),
2406            board_id: board.board_id,
2407            field_id: field.field_id,
2408            options: field.options,
2409        })
2410    }
2411
2412    /// The refusal a board with no `Status` field is answered with by the guarded setup.
2413    fn no_status_field(&self) -> SourceError {
2414        SourceError::Refused {
2415            message: format!("source {} board has no Status field", self.name),
2416        }
2417    }
2418
2419    // llmlint: ignore-block[changed_behavior_has_e2e] Valid snapshot shapes are exercised through
2420    // the real CLI loopback journey, including pagination. The individual malformed guards
2421    // are defensive validation of a schema-pinned third-party response, not separate user
2422    // journeys; drift and missing-field failures cover the operation's recovery behavior.
2423    /// A fresh snapshot of each of the `owned` fields on the board, with its options, and of
2424    /// every board item's value of each, walked to the end of the board's items. A field not
2425    /// in `owned` is read past whatever it holds.
2426    async fn board_snapshot(&self, owned: &[BoardField]) -> Result<BoardSnapshot, SourceError> {
2427        let mut after: Option<String> = None;
2428        let mut snapshot: Option<BoardSnapshot> = None;
2429        loop {
2430            let data = self
2431                .graphql(
2432                    graphql::STATUS_OPTIONS_SNAPSHOT,
2433                    json!({
2434                        "owner": self.owner, "number": self.project_number,
2435                        "first": MAX_PAGE_SIZE, "after": after, "nestedFirst": MAX_PAGE_SIZE,
2436                    }),
2437                )
2438                .await?;
2439            let board = data
2440                .pointer("/owner/projectV2")
2441                .filter(|board| board.is_object())
2442                .ok_or_else(|| SourceError::Refused {
2443                    message: format!(
2444                        "source {} has no accessible GitHub Projects board",
2445                        self.name
2446                    ),
2447                })?;
2448            if board
2449                .pointer("/fields/pageInfo/hasNextPage")
2450                .and_then(Value::as_bool)
2451                != Some(false)
2452            {
2453                return Err(SourceError::Malformed {
2454                    message:
2455                        "GitHub project fields is incomplete or has malformed pageInfo.hasNextPage"
2456                            .into(),
2457                });
2458            }
2459            let mut fields = BTreeMap::new();
2460            // Only the fields this setup owns, by name: a node the single-select fragment did not
2461            // match carries no name, and a person's own single-select field — a `Size`, a
2462            // `Team` — is none of this setup's business, so nothing about it can refuse one. A
2463            // `Status` or `Priority` field without its options is malformed, not absent.
2464            // 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.
2465            for (owned, field) in board
2466                .pointer("/fields/nodes")
2467                .and_then(Value::as_array)
2468                .ok_or_else(|| SourceError::Malformed {
2469                    message: "GitHub project fields.nodes is not an array".into(),
2470                })?
2471                .iter()
2472                .filter_map(|field| {
2473                    let named = BoardField::named(field.get("name")?.as_str()?)?;
2474                    owned.contains(&named).then_some((named, field))
2475                })
2476            {
2477                let options = field
2478                    .get("options")
2479                    .and_then(Value::as_array)
2480                    .ok_or_else(|| SourceError::Malformed {
2481                        message: "GitHub single-select field options is not an array".into(),
2482                    })?
2483                    .iter()
2484                    .map(|option| {
2485                        Ok(StatusOption {
2486                            id: StatusOptionId::try_from(required_str(option, "id")?.to_owned())
2487                                .map_err(|message| SourceError::Malformed { message })?,
2488                            name: ColumnName::try_from(required_str(option, "name")?.to_owned())
2489                                .map_err(|message| SourceError::Malformed {
2490                                    message: format!(
2491                                        "GitHub single-select option name is invalid: {message}"
2492                                    ),
2493                                })?,
2494                            color: serde_json::from_value(
2495                                option.get("color").cloned().unwrap_or(Value::Null),
2496                            )
2497                            .map_err(|error| {
2498                                SourceError::Malformed {
2499                                    message: format!(
2500                                        "GitHub single-select option color is invalid: {error}"
2501                                    ),
2502                                }
2503                            })?,
2504                            description: optional_str(option, "description")?
2505                                .unwrap_or_default()
2506                                .to_owned(),
2507                        })
2508                    })
2509                    .collect::<Result<Vec<_>, SourceError>>()?;
2510                let snapshot = SnapshotField {
2511                    field_id: required_nonblank_str(field, "id")?.to_owned(),
2512                    options,
2513                };
2514                // A board's field names are unique, so a second one is an answer that cannot
2515                // say which field the setup would act on — refused rather than one chosen.
2516                if fields.insert(owned, snapshot).is_some() {
2517                    return Err(SourceError::Malformed {
2518                        message: format!(
2519                            "GitHub answered two {} fields for this board",
2520                            owned.name()
2521                        ),
2522                    });
2523                }
2524            }
2525            let board_id = required_nonblank_str(board, "id")?.to_owned();
2526            let current = snapshot.get_or_insert_with(|| BoardSnapshot {
2527                board_id,
2528                fields,
2529                items: Vec::new(),
2530            });
2531            let items = board
2532                .pointer("/items/nodes")
2533                .and_then(Value::as_array)
2534                .ok_or_else(|| SourceError::Malformed {
2535                    message: "GitHub project items.nodes is not an array".into(),
2536                })?;
2537            for item in items {
2538                let field_values =
2539                    item.get("fieldValues")
2540                        .ok_or_else(|| SourceError::Malformed {
2541                            message: "GitHub project item is missing fieldValues".into(),
2542                        })?;
2543                if field_values
2544                    .pointer("/pageInfo/hasNextPage")
2545                    .and_then(Value::as_bool)
2546                    != Some(false)
2547                {
2548                    return Err(SourceError::Malformed {
2549                        message: "GitHub project item fieldValues is incomplete or has malformed pageInfo.hasNextPage".into(),
2550                    });
2551                }
2552                let values = item
2553                    .pointer("/fieldValues/nodes")
2554                    .and_then(Value::as_array)
2555                    .ok_or_else(|| SourceError::Malformed {
2556                        message: "GitHub project item fieldValues.nodes is not an array".into(),
2557                    })?;
2558                let item_id = required_nonblank_str(item, "id")?;
2559                let mut assigned = BTreeMap::new();
2560                for value in values {
2561                    let Some(field) = value
2562                        .pointer("/field/name")
2563                        .and_then(Value::as_str)
2564                        .and_then(BoardField::named)
2565                        .filter(|field| owned.contains(field))
2566                    else {
2567                        continue;
2568                    };
2569                    let held = assigned.insert(
2570                        field,
2571                        AssignedStatusOption {
2572                            id: StatusOptionId::try_from(
2573                                required_str(value, "optionId")?.to_owned(),
2574                            )
2575                            .map_err(|message| SourceError::Malformed { message })?,
2576                            name: ColumnName::try_from(required_str(value, "name")?.to_owned())
2577                                .map_err(|message| SourceError::Malformed {
2578                                    message: format!(
2579                                        "GitHub assigned {} name is invalid: {message}",
2580                                        field.name()
2581                                    ),
2582                                })?,
2583                        },
2584                    );
2585                    // An item holds one value of a field, so a second one leaves no way to
2586                    // tell which it holds — and a verification or recovery built on either
2587                    // could restore the wrong one.
2588                    if held.is_some() {
2589                        return Err(SourceError::Malformed {
2590                            message: format!(
2591                                "GitHub answered two {} values for board item {item_id}",
2592                                field.name()
2593                            ),
2594                        });
2595                    }
2596                }
2597                current.items.push((item_id.to_owned(), assigned));
2598            }
2599            let page = board.get("items").ok_or_else(|| SourceError::Malformed {
2600                message: "GitHub project is missing items".into(),
2601            })?;
2602            let has_next = page
2603                .pointer("/pageInfo/hasNextPage")
2604                .and_then(Value::as_bool)
2605                .ok_or_else(|| SourceError::Malformed {
2606                    message: "GitHub project items.pageInfo.hasNextPage is not a boolean".into(),
2607                })?;
2608            if !has_next {
2609                break;
2610            }
2611            let next =
2612                required_nonblank_str(page.get("pageInfo").unwrap_or(&Value::Null), "endCursor")?;
2613            validate_cursor_progress(after.as_deref(), next)?;
2614            after = Some(next.to_owned());
2615        }
2616        snapshot.ok_or_else(|| SourceError::Malformed {
2617            message: "GitHub returned no board field snapshot".into(),
2618        })
2619    }
2620    // llmlint: ignore-end[changed_behavior_has_e2e]
2621
2622    /// Report every board field this source's configuration names and, with
2623    /// [`SetupMode::Apply`], set each up: add the options a field lacks, and create
2624    /// the `Priority` field when the board has none.
2625    ///
2626    /// The fields are `Status`, always, with the options `status_mapping` resolves to; and
2627    /// `Priority`, when `priority_mapping` is set, with its four mapped options — created in
2628    /// the order urgent, high, medium, low. An option a field already has keeps its id, name,
2629    /// color and description: the whole option list goes back with every existing id, because
2630    /// a re-minted id clears every item's value.
2631    ///
2632    /// # Errors
2633    ///
2634    /// Refuses a board without a single-select `Status` field. After an apply the board is
2635    /// read again, and a pre-existing option or any item's value of either field that moved is
2636    /// refused with the complete pre-write assignments in the diagnostic, for recovery.
2637    // llmlint: ignore[changed_behavior_has_e2e] The `sources fields` journeys drive plan,
2638    // unchanged apply, a created field, an added option to each field, drift refusal, a board
2639    // with no Status field and a non-github-projects source through the compiled CLI against
2640    // the loopback board. Transport errors are the shared `graphql` boundary's behavior.
2641    pub async fn fields(&self, mode: SetupMode) -> Result<FieldsReport, SourceError> {
2642        let owned: Vec<BoardField> = if self.priorities.is_some() {
2643            vec![BoardField::Status, BoardField::Priority]
2644        } else {
2645            vec![BoardField::Status]
2646        };
2647        let before = self.board_snapshot(&owned).await?;
2648        let mut plans = vec![FieldPlan {
2649            field: BoardField::Status,
2650            wanted: self
2651                .statuses
2652                .targets
2653                .iter()
2654                .filter_map(|target| match target {
2655                    StatusTarget::Column(name) | StatusTarget::Terminal(name, _) => {
2656                        Some(name.as_str().to_owned())
2657                    }
2658                    StatusTarget::Disabled => None,
2659                })
2660                .collect(),
2661        }];
2662        if !before.fields.contains_key(&BoardField::Status) {
2663            return Err(self.no_status_field());
2664        }
2665        if let Some(mapping) = &self.priorities {
2666            plans.push(FieldPlan {
2667                field: BoardField::Priority,
2668                wanted: mapping.names().map(str::to_owned).collect(),
2669            });
2670        }
2671        // The snapshot reads single-select fields alone, so a field it did not find may still
2672        // be on the board under the name, of another type: creating one beside it would fail
2673        // part way, or leave two fields of one name. Asked of the board's own field list, and
2674        // only when a field is missing.
2675        if plans
2676            .iter()
2677            .any(|plan| !before.fields.contains_key(&plan.field))
2678        {
2679            let board = self.board_fields().await?;
2680            for plan in plans
2681                .iter()
2682                .filter(|plan| !before.fields.contains_key(&plan.field))
2683            {
2684                if let Some(field) = Board::field(&board.fields, plan.field.name())? {
2685                    return Err(SourceError::Refused {
2686                        message: format!(
2687                            "source {}'s board has a {} field that is not a single-select field \
2688                             (it is a {}), so it cannot hold this source's options; next: rename \
2689                             or remove that field, then run this again",
2690                            self.name,
2691                            plan.field.name(),
2692                            optional_str(field, "__typename")?.unwrap_or("field of another type")
2693                        ),
2694                    });
2695                }
2696            }
2697        }
2698        let mut reports = Vec::new();
2699        for plan in &plans {
2700            let held = before.fields.get(&plan.field);
2701            let existing = held.map(|field| field.options.clone()).unwrap_or_default();
2702            let mut missing: Vec<String> = Vec::new();
2703            for wanted in &plan.wanted {
2704                let present = existing
2705                    .iter()
2706                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2707                    || missing
2708                        .iter()
2709                        .any(|named| named.eq_ignore_ascii_case(wanted));
2710                if !present {
2711                    missing.push(wanted.clone());
2712                }
2713            }
2714            reports.push(FieldReport {
2715                field: plan.field,
2716                exists: held.is_some(),
2717                outcome: match (mode, held.is_some(), missing.is_empty()) {
2718                    (SetupMode::Plan, _, _) => FieldOutcome::Planned,
2719                    (SetupMode::Apply, true, true) => FieldOutcome::Unchanged,
2720                    (SetupMode::Apply, true, false) => FieldOutcome::Applied,
2721                    (SetupMode::Apply, false, _) => FieldOutcome::Created,
2722                },
2723                missing,
2724                existing,
2725            });
2726        }
2727        let report = FieldsReport {
2728            source: self.name.clone(),
2729            fields: reports,
2730        };
2731        let writes: Vec<&FieldReport> = report
2732            .fields
2733            .iter()
2734            .filter(|field| !field.missing.is_empty() || !field.exists)
2735            .collect();
2736        if mode == SetupMode::Plan || writes.is_empty() {
2737            return Ok(report);
2738        }
2739        let mut landed: Vec<&str> = Vec::new();
2740        for field in &writes {
2741            let added = field
2742                .missing
2743                .iter()
2744                .map(|name| json!({"name": name, "color": "GRAY", "description": ""}));
2745            let sent = match before.fields.get(&field.field) {
2746                Some(held) => {
2747                    let mut options = held
2748                        .options
2749                        .iter()
2750                        .map(|option| {
2751                            json!({
2752                                "id": option.id, "name": option.name, "color": option.color,
2753                                "description": option.description,
2754                            })
2755                        })
2756                        .collect::<Vec<_>>();
2757                    options.extend(added);
2758                    self.graphql(
2759                        graphql::STATUS_OPTIONS_UPDATE,
2760                        json!({"input": {
2761                            "projectId": before.board_id, "fieldId": held.field_id,
2762                            "singleSelectOptions": options,
2763                        }}),
2764                    )
2765                    .await
2766                }
2767                None => {
2768                    self.graphql(
2769                        graphql::CREATE_FIELD,
2770                        json!({"input": {
2771                            "projectId": before.board_id, "dataType": "SINGLE_SELECT",
2772                            "name": field.field.name(),
2773                            "singleSelectOptions": added.collect::<Vec<_>>(),
2774                        }}),
2775                    )
2776                    .await
2777                }
2778            };
2779            // A mutation that failed does not establish that GitHub left its field as it was,
2780            // so every failure from here on carries the recovery data a drift refusal does.
2781            match sent {
2782                Ok(_) => landed.push(field.field.name()),
2783                Err(error) => {
2784                    let changed = if landed.is_empty() {
2785                        String::new()
2786                    } else {
2787                        format!("changed the {} field and then ", landed.join(" and "))
2788                    };
2789                    return Err(SourceError::Refused {
2790                        message: format!(
2791                            "the guarded field setup {changed}failed on the {} field, which it may \
2792                             have changed part way: {error}; the pre-write item assignments \
2793                             are:\n{}",
2794                            field.field.name(),
2795                            recovery(&report, &before)?
2796                        ),
2797                    });
2798                }
2799            }
2800        }
2801        // The board has been written, so a verification read that fails leaves it unverified
2802        // rather than unchanged, and says what to put back.
2803        let after = match self.board_snapshot(&owned).await {
2804            Ok(after) => after,
2805            Err(error) => {
2806                return Err(SourceError::Refused {
2807                    message: format!(
2808                        "the guarded field setup changed the {} field and then could not read the \
2809                         board back to verify it: {error}; the pre-write item assignments are:\n{}",
2810                        landed.join(" and "),
2811                        recovery(&report, &before)?
2812                    ),
2813                });
2814            }
2815        };
2816        let mut moved = Vec::new();
2817        for field in &report.fields {
2818            let name = field.field.name();
2819            let now = after
2820                .fields
2821                .get(&field.field)
2822                .map(|held| held.options.as_slice())
2823                .unwrap_or_default();
2824            if !field.existing.iter().all(|old| now.contains(old)) {
2825                moved.push(format!(
2826                    "a pre-existing {name} option id, name, color or description"
2827                ));
2828            }
2829            if !field.missing.iter().all(|wanted| {
2830                now.iter()
2831                    .any(|option| option.name.as_str().eq_ignore_ascii_case(wanted))
2832            }) {
2833                moved.push(format!("an added {name} option"));
2834            }
2835            if after.assignments(field.field) != before.assignments(field.field) {
2836                moved.push(format!("an item's {name} value"));
2837            }
2838        }
2839        if !moved.is_empty() {
2840            return Err(SourceError::Refused {
2841                message: format!(
2842                    "GitHub changed {} after the guarded field setup; the pre-write item \
2843                     assignments are:\n{}",
2844                    moved.join(", "),
2845                    recovery(&report, &before)?
2846                ),
2847            });
2848        }
2849        Ok(report)
2850    }
2851
2852    /// Validate configuration and capture the named credential without exposing it.
2853    ///
2854    /// # Errors
2855    ///
2856    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
2857    /// [`SourceError::Auth`] when the named credential is missing or empty.
2858    pub fn new(
2859        name: &SourceName,
2860        config: GitHubProjectsConfig,
2861        secrets: &dyn SecretResolver,
2862    ) -> Result<Self, SourceError> {
2863        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
2864    }
2865
2866    /// The same, recording every request it sends into an accounting the caller holds too.
2867    ///
2868    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
2869    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
2870    /// up — passes the one it records those into, so the session total accounts for the
2871    /// whole session rather than for this source's share of it.
2872    ///
2873    /// # Errors
2874    ///
2875    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
2876    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
2877    pub fn recording_into(
2878        name: &SourceName,
2879        config: GitHubProjectsConfig,
2880        secrets: &dyn SecretResolver,
2881        ledger: Arc<Accounting>,
2882    ) -> Result<Self, SourceError> {
2883        if !valid_github_owner(&config.owner) {
2884            return Err(SourceError::Config {
2885                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
2886            });
2887        }
2888        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
2889            return Err(SourceError::Config {
2890                message: format!("project_number must be between 1 and {}", i32::MAX),
2891            });
2892        }
2893        if !valid_environment_name(&config.token_env) {
2894            return Err(SourceError::Config {
2895                message: "token_env must be a valid environment-variable name".into(),
2896            });
2897        }
2898        let repository = config
2899            .repository
2900            .as_deref()
2901            .map(RepositoryTarget::parse)
2902            .transpose()?;
2903        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
2904            message: format!("endpoint is not a valid URL: {e}"),
2905        })?;
2906        if endpoint.scheme() != "https"
2907            && !(endpoint.scheme() == "http"
2908                && endpoint
2909                    .host_str()
2910                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
2911        {
2912            return Err(SourceError::Config {
2913                message:
2914                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
2915                        .into(),
2916            });
2917        }
2918        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
2919            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),
2920        })?;
2921        Ok(Self {
2922            name: name.clone(),
2923            owner: config.owner,
2924            project_number: config.project_number,
2925            repository,
2926            endpoint,
2927            token,
2928            credential_name: config.token_env,
2929            statuses: StatusMapping::resolve(config.status_mapping, name)?,
2930            priorities: config
2931                .priority_mapping
2932                .map(|mapping| PriorityMapping::resolve(mapping, name))
2933                .transpose()?,
2934            client: Client::builder()
2935                .user_agent("onetaskgraph")
2936                .build()
2937                .map_err(|e| SourceError::Config {
2938                    message: format!("cannot build HTTP client: {e}"),
2939                })?,
2940            created: Mutex::new(Vec::new()),
2941            pacing: Pacing::resolve(config.pacing, name)?,
2942            last_mutation: Mutex::new(None),
2943            board_cache: Mutex::new(None),
2944            search_cache: Mutex::new(None),
2945            fields_cache: Mutex::new(None),
2946            repository_cache: Mutex::new(BTreeMap::new()),
2947            ledger,
2948        })
2949    }
2950
2951    /// A snapshot of every request this source has sent, and what each cost.
2952    ///
2953    /// A value to hold and compare rather than a borrow of the accounting itself, so two
2954    /// of them can sit side by side. When this source was built with
2955    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
2956    /// point of building it that way.
2957    #[must_use]
2958    pub fn accounting(&self) -> accounting::Session {
2959        self.ledger.snapshot()
2960    }
2961
2962    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
2963    /// rate limit rather than handing it straight back as an error.
2964    ///
2965    /// Retrying is safe for every document here, including the mutations, and the reason
2966    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
2967    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
2968    /// this replays has already taken effect. An outcome this source cannot know — the
2969    /// send failed, or the body could not be read, so the mutation may well have landed —
2970    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
2971    /// attempt. A duplicate write would come from replaying one of those, and none is
2972    /// replayed.
2973    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
2974        let doing = operation_description(query);
2975        let mut waited = Duration::ZERO;
2976        let mut waits = 0_u32;
2977        let mut backoff = self.pacing.retry_backoff;
2978        loop {
2979            if is_mutation(query) {
2980                let spacing = self.reserve_mutation_slot();
2981                if !spacing.is_zero() {
2982                    tokio::time::sleep(spacing).await;
2983                }
2984            }
2985            let attempt = self.send_once(query, &variables).await;
2986            if is_mutation(query) {
2987                self.finish_mutation();
2988            }
2989            let limited = match attempt {
2990                Ok(data) => return Ok(data),
2991                Err(Attempt::Failed(error)) => return Err(error),
2992                Err(Attempt::Limited(limited)) => limited,
2993            };
2994            // GitHub really does send `retry-after: 0`, and retrying at once is the one
2995            // move that extends a secondary limit, so a hint below the schedule's own next
2996            // wait is raised to it.
2997            let wait = match limited.hint {
2998                Some(hint) => Duration::from_secs(hint).max(backoff),
2999                None => backoff,
3000            };
3001            let remaining = self.pacing.retry_budget.saturating_sub(waited);
3002            // A wait of nothing spends none of the budget, so it is exhaustion rather
3003            // than a retry. `Pacing::resolve` rules out every way of configuring one
3004            // except a budget of zero, where reporting the first refusal is the ask.
3005            if wait.is_zero() || wait > remaining {
3006                return Err(limited.exhausted(
3007                    doing,
3008                    waits,
3009                    waited,
3010                    wait,
3011                    self.pacing.retry_budget,
3012                ));
3013            }
3014            tokio::time::sleep(wait).await;
3015            waited += wait;
3016            waits += 1;
3017            backoff = backoff.saturating_mul(2);
3018        }
3019    }
3020
3021    /// The next moment a content-creating mutation may leave this source, as a wait from
3022    /// now.
3023    ///
3024    /// The slot is reserved under the lock and the waiting happens outside it, so two
3025    /// callers take two slots rather than the same one — and no lock is held across an
3026    /// await.
3027    ///
3028    /// The moment it is spaced from is the previous mutation's *completion*, which
3029    /// [`Self::finish_mutation`] records. See that method for why the release moment on its
3030    /// own is the wrong thing to measure from.
3031    fn reserve_mutation_slot(&self) -> Duration {
3032        if self.pacing.min_mutation_interval.is_zero() {
3033            return Duration::ZERO;
3034        }
3035        // A poisoned lock here costs pacing, not correctness, and refusing the write over
3036        // it would turn an earlier failure into a second one for no gain.
3037        let mut last = self
3038            .last_mutation
3039            .lock()
3040            .unwrap_or_else(std::sync::PoisonError::into_inner);
3041        let now = Instant::now();
3042        // `checked_add` rather than `+`: `Instant + Duration` panics on overflow, and
3043        // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
3044        let at = last.map_or(now, |previous| {
3045            previous
3046                .checked_add(self.pacing.min_mutation_interval)
3047                .map_or(now, |earliest| earliest.max(now))
3048        });
3049        *last = Some(at);
3050        at.saturating_duration_since(now)
3051    }
3052
3053    /// Record that a content-creating mutation has finished, so the next one is spaced
3054    /// from here rather than from the moment this one was released.
3055    ///
3056    /// This source can only choose when a request *departs*; the limiter counts when it
3057    /// *arrives*, and the two differ by whatever the request spent in transit. Spacing one
3058    /// departure from the last therefore hands the limiter a gap of the interval less that
3059    /// transit, so a source pacing at 750 ms can still be seen arriving faster — which is
3060    /// exactly how a copy paced well inside a board's threshold was refused by it on a
3061    /// slower machine while passing on a quick one.
3062    ///
3063    /// Spacing from completion removes the subtraction rather than budgeting for it. The
3064    /// previous request had already arrived before its response came back, so its arrival
3065    /// is no later than this moment, and the next mutation is released at least the
3066    /// interval after this moment and arrives no earlier than it is released: the gap the
3067    /// limiter measures is therefore at least the interval, whatever transit costs and on
3068    /// whatever platform. The price is that a mutation's own round trip no longer counts
3069    /// towards its spacing, which makes this source slightly slower than the configured
3070    /// rate rather than slightly faster — the safe side of a limit that punishes being
3071    /// wrong by refusing reads for the next fifty minutes.
3072    ///
3073    /// A failed attempt is recorded too: a request refused by the limiter still arrived,
3074    /// and one that never left costs only a wait nobody needed.
3075    fn finish_mutation(&self) {
3076        if self.pacing.min_mutation_interval.is_zero() {
3077            return;
3078        }
3079        // A poisoned lock here costs pacing, not correctness, exactly as in the reservation.
3080        let mut last = self
3081            .last_mutation
3082            .lock()
3083            .unwrap_or_else(std::sync::PoisonError::into_inner);
3084        let now = Instant::now();
3085        // `max` rather than an assignment: a concurrent caller may already have reserved a
3086        // slot further out, and completing this request must never pull that slot back in.
3087        *last = Some(last.map_or(now, |reserved| reserved.max(now)));
3088    }
3089
3090    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
3091    /// failure that waiting cannot help — and recorded, whichever of the three it was.
3092    ///
3093    /// This is the one place a request leaves this crate, which is why the accounting is
3094    /// here rather than at each of the callers: a read path added later is counted without
3095    /// anybody remembering to count it, and
3096    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
3097    /// when one is not.
3098    async fn send_once(&self, query: &str, variables: &Value) -> Result<Value, Attempt> {
3099        let Attempted {
3100            result,
3101            limits,
3102            reported_cost,
3103        } = self.attempt(query, variables).await;
3104        // No `otherwise` name: every document this source sends is one of its own, and the
3105        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
3106        let sending = accounting::Request::graphql(query, variables, None, reported_cost);
3107        let outcome = match &result {
3108            Ok(_) => accounting::Outcome::Answered,
3109            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
3110            Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
3111        };
3112        self.ledger.record(sending.finished(outcome, limits));
3113        result
3114    }
3115
3116    /// The attempt itself, with what its response said about the rate limit alongside.
3117    ///
3118    /// The two are returned together rather than recorded here because every one of the
3119    /// early exits below is a different outcome, and a record written at each of them is a
3120    /// record one of them can be added without.
3121    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
3122        let mut limits = accounting::RateLimit::default();
3123        let mut reported_cost = None;
3124        let result = self
3125            .attempted(query, variables, &mut limits, &mut reported_cost)
3126            .await;
3127        Attempted {
3128            result,
3129            limits,
3130            reported_cost,
3131        }
3132    }
3133
3134    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
3135    async fn attempted(
3136        &self,
3137        query: &str,
3138        variables: &Value,
3139        limits: &mut accounting::RateLimit,
3140        reported_cost: &mut Option<u64>,
3141    ) -> Result<Value, Attempt> {
3142        let response = self
3143            .client
3144            .post(self.endpoint.clone())
3145            .bearer_auth(self.token.expose_secret())
3146            .json(&json!({"query": query, "variables": variables}))
3147            .send()
3148            .await
3149            .map_err(|e| {
3150                Attempt::Failed(SourceError::Unavailable {
3151                    message: format!("GitHub GraphQL request failed: {e}"),
3152                })
3153            })?;
3154        let status = response.status();
3155        let header = |name: &str| whole_seconds(response.headers().get(name));
3156        *limits = accounting::RateLimit::read(|name| {
3157            response
3158                .headers()
3159                .get(name)
3160                .and_then(|value| value.to_str().ok())
3161                .map(str::to_owned)
3162        });
3163        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
3164        // that are not text at all — is "not known to be exhausted". This never makes a
3165        // response a refusal on its own: it says which limiter a refusal is attributed to
3166        // and where its hint comes from, so a value this cannot read costs a hint rather
3167        // than an answer.
3168        let exhausted = response
3169            .headers()
3170            .get("x-ratelimit-remaining")
3171            .and_then(|value| value.to_str().ok())
3172            == Some("0");
3173        // `retry-after` is what GitHub asks for when it asks; when it does not and the
3174        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
3175        // which is the same question answered as an absolute time. Nothing else here is a
3176        // hint, and a schedule is what answers a refusal that carries none.
3177        let hint = header("retry-after").or_else(|| {
3178            exhausted
3179                .then(|| header("x-ratelimit-reset"))
3180                .flatten()
3181                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
3182        });
3183        // Read before it is parsed, because the evidence which tells a secondary rate
3184        // limit from a rejected credential is in the body of a response whose status says
3185        // only "forbidden" — and a non-success response was never parsed at all.
3186        let body = response.text().await.map_err(|e| {
3187            Attempt::Failed(SourceError::Unavailable {
3188                message: format!("GitHub GraphQL response could not be read: {e}"),
3189            })
3190        })?;
3191        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
3192            return Err(Attempt::Limited(Limited { limiter, hint }));
3193        }
3194        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
3195            return Err(Attempt::Failed(SourceError::Auth {
3196                message: format!(
3197                    "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"
3198                ),
3199            }));
3200        }
3201        if !status.is_success() {
3202            return Err(Attempt::Failed(SourceError::Unavailable {
3203                message: format!("GitHub GraphQL returned HTTP {status}"),
3204            }));
3205        }
3206        // GitHub reports what a call cost only when the document asked it to, and no
3207        // document this source sends does — so this is `None` here and carries the figure
3208        // for a caller whose own document selects `rateLimit { cost }`. What it must never
3209        // pick up is a `dryRun` probe's cost, which is some other document's.
3210        *reported_cost = serde_json::from_str::<Value>(&body)
3211            .ok()
3212            .as_ref()
3213            .and_then(|body| body.pointer("/data/rateLimit/cost"))
3214            .and_then(Value::as_u64);
3215        self.answer(&body).map_err(Attempt::Failed)
3216    }
3217
3218    /// What one successful HTTP response says, once its GraphQL errors are read.
3219    fn answer(&self, body: &str) -> Result<Value, SourceError> {
3220        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
3221            message: format!("GitHub returned invalid JSON: {e}"),
3222        })?;
3223        let errors = body
3224            .get("errors")
3225            .map(|value| {
3226                value.as_array().ok_or_else(|| SourceError::Malformed {
3227                    message: "GitHub response errors is not an array".into(),
3228                })
3229            })
3230            .transpose()?;
3231        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
3232            let messages = errors
3233                .iter()
3234                .filter_map(|e| e.get("message").and_then(Value::as_str))
3235                .collect::<Vec<_>>()
3236                .join("; ");
3237            let message = if messages.is_empty() {
3238                "GitHub returned GraphQL errors".into()
3239            } else {
3240                messages
3241            };
3242            let normalized = message.to_ascii_lowercase();
3243            if normalized.contains("resource not accessible") || normalized.contains("scope") {
3244                return Err(SourceError::Auth {
3245                    message: format!(
3246                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
3247                        self.credential_name
3248                    ),
3249                });
3250            }
3251            return Err(SourceError::Refused { message });
3252        }
3253        body.get("data")
3254            .filter(|data| data.is_object())
3255            .cloned()
3256            .ok_or_else(|| SourceError::Malformed {
3257                message: "GitHub response has no data object".into(),
3258            })
3259    }
3260
3261    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
3262    // GraphQL cannot independently page them inside the outer item page. This source page is
3263    // deliberately bounded at that published maximum; the live drift journey exercises it.
3264    async fn board_page(
3265        &self,
3266        items_after: Option<&str>,
3267        items_first: u32,
3268    ) -> Result<Value, SourceError> {
3269        let data = self
3270            .graphql(
3271                graphql::BOARD,
3272                json!({"owner":self.owner,"number":self.project_number,
3273                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
3274                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
3275            )
3276            .await?;
3277        data.pointer("/owner/projectV2")
3278            .filter(|v| !v.is_null())
3279            .cloned()
3280            .ok_or_else(|| SourceError::Refused {
3281                message: format!(
3282                    "GitHub project {}/{} was not found or is not visible to the token",
3283                    self.owner, self.project_number
3284                ),
3285            })
3286    }
3287
3288    /// The search that finds the issues of this board, narrowed by `also` when it is
3289    /// given.
3290    ///
3291    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
3292    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
3293    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
3294    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
3295    /// from a task by the `parent` field each issue carries rather than by the search.
3296    fn board_search(&self, also: Option<&str>) -> String {
3297        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
3298        match also {
3299            Some(also) => format!("{scope} {also}"),
3300            None => scope,
3301        }
3302    }
3303
3304    /// One issue this source reached directly, as the board item a read of the board would
3305    /// have produced — or `None` when this board does not hold it.
3306    ///
3307    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
3308    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
3309    /// item's own id, that item's field values, and the issue as its content. One resolver
3310    /// for both routes is what makes an issue read through a search, through its own node
3311    /// id, or through its project's sub-issues report the same title, the same status, the
3312    /// same labels and the same qualified id.
3313    ///
3314    /// An issue with no entry for *this* board is not this source's to report, which is
3315    /// what keeps an id naming some other repository's issue from being answered as an item
3316    /// of this board. That answer is given about an **exhausted** connection and never
3317    /// about an unread page: the entry is looked for on the page in hand, and only if that
3318    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
3319    /// rest of it.
3320    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
3321        if optional_str(issue, "__typename")? != Some("Issue") {
3322            return Ok(None);
3323        }
3324        let memberships = issue
3325            .get("projectItems")
3326            .ok_or_else(|| SourceError::Malformed {
3327                message: "GitHub issue is missing projectItems".into(),
3328            })?;
3329        let nodes = memberships
3330            .get("nodes")
3331            .and_then(Value::as_array)
3332            .ok_or_else(|| SourceError::Malformed {
3333                message: "GitHub issue projectItems.nodes is not an array".into(),
3334            })?;
3335        let held = match self.board_entry(nodes) {
3336            Some(held) => held.clone(),
3337            None => {
3338                let info = memberships
3339                    .get("pageInfo")
3340                    .ok_or_else(|| SourceError::Malformed {
3341                        message: "GitHub issue projectItems has no pageInfo".into(),
3342                    })?;
3343                // The page held no entry for this board. Whether that means the issue is
3344                // not on it is a question about the rest of the connection, and only a
3345                // connection with no rest answers it here.
3346                if !required_bool(info, "hasNextPage")? {
3347                    return Ok(None);
3348                }
3349                let cursor = required_str(info, "endCursor")?;
3350                validate_cursor_progress(None, cursor)?;
3351                let issue_id = required_str(issue, "id")?;
3352                match self.board_membership(issue_id, cursor).await? {
3353                    Some(held) => held,
3354                    None => return Ok(None),
3355                }
3356            }
3357        };
3358        let item = json!({
3359            "id": required_str(&held, "id")?,
3360            "project": held.get("project"),
3361            "fieldValues": held.get("fieldValues"),
3362            "content": issue,
3363        });
3364        self.resolve(&item)
3365    }
3366
3367    /// This board's own entry among one page of an issue's `Issue.projectItems`.
3368    ///
3369    /// One spelling of *which membership is this board's*, so the page a read carries and
3370    /// the pages [`Self::board_membership`] walks are searched by the same rule.
3371    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
3372        nodes.iter().find(|node| {
3373            node.pointer("/project/number").and_then(Value::as_u64)
3374                == Some(u64::from(self.project_number))
3375        })
3376    }
3377
3378    /// The rest of one issue's board memberships, from `after`, for this board's entry.
3379    ///
3380    /// The recovery read: a page of memberships that holds no entry for this board says
3381    /// nothing about the memberships past it, so the connection is walked to exhaustion
3382    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
3383    /// positive answer — the whole connection was read and no entry named this board —
3384    /// rather than a failure, and the walk is held to
3385    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
3386    /// with a cursor that does not advance is refused instead of spun on.
3387    async fn board_membership(
3388        &self,
3389        issue: &str,
3390        after: &str,
3391    ) -> Result<Option<Value>, SourceError> {
3392        let mut after = after.to_owned();
3393        loop {
3394            let data = self
3395                .graphql(
3396                    graphql::ISSUE_BOARD_ITEMS,
3397                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
3398                           "nestedFirst":NESTED_PAGE_SIZE}),
3399                )
3400                .await?;
3401            let Some(connection) = data
3402                .pointer("/node/projectItems")
3403                .filter(|value| !value.is_null())
3404            else {
3405                // The id resolved to nothing, or to something with no memberships to walk —
3406                // which is the same answer as a connection holding no entry for this board.
3407                return Ok(None);
3408            };
3409            let nodes = connection
3410                .get("nodes")
3411                .and_then(Value::as_array)
3412                .ok_or_else(|| SourceError::Malformed {
3413                    message: "GitHub issue projectItems.nodes is not an array".into(),
3414                })?;
3415            if let Some(held) = self.board_entry(nodes) {
3416                return Ok(Some(held.clone()));
3417            }
3418            let info = connection
3419                .get("pageInfo")
3420                .ok_or_else(|| SourceError::Malformed {
3421                    message: "GitHub issue projectItems has no pageInfo".into(),
3422                })?;
3423            let next = required_bool(info, "hasNextPage")?
3424                .then(|| required_str(info, "endCursor"))
3425                .transpose()?;
3426            match next {
3427                Some(next) => {
3428                    validate_cursor_progress(Some(&after), next)?;
3429                    after = next.to_owned();
3430                }
3431                None => return Ok(None),
3432            }
3433        }
3434    }
3435
3436    /// One page of a board-scoped issue search, and where the next page resumes.
3437    async fn search_page(
3438        &self,
3439        search: &str,
3440        first: u32,
3441        after: Option<&str>,
3442    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
3443        let data = self
3444            .graphql(
3445                graphql::SEARCH_ISSUES,
3446                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
3447                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
3448                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
3449            )
3450            .await?;
3451        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
3452            message: "GitHub search response has no search connection".into(),
3453        })?;
3454        let mut found = Vec::new();
3455        for node in connection
3456            .get("nodes")
3457            .and_then(Value::as_array)
3458            .ok_or_else(|| SourceError::Malformed {
3459                message: "GitHub search nodes is not an array".into(),
3460            })?
3461        {
3462            if let Some(resolved) = self.resolve_issue(node).await? {
3463                found.push(resolved);
3464            }
3465        }
3466        let info = connection
3467            .get("pageInfo")
3468            .ok_or_else(|| SourceError::Malformed {
3469                message: "GitHub search connection has no pageInfo".into(),
3470            })?;
3471        let next = required_bool(info, "hasNextPage")?
3472            .then(|| required_str(info, "endCursor"))
3473            .transpose()?
3474            .map(str::to_owned);
3475        if let Some(next) = &next {
3476            validate_cursor_progress(after, next)?;
3477        }
3478        Ok((found, next))
3479    }
3480
3481    /// Every issue this board holds, completed with what this run wrote.
3482    ///
3483    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
3484    /// is an index and is eventually consistent, so an issue this run created seconds ago
3485    /// can be absent from it, and a project listed straight after being written would
3486    /// otherwise be missing from its own board. What is added back is only what this
3487    /// process itself wrote, out of [`Self::created`], which lives and dies with the
3488    /// process.
3489    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
3490        let found = self.searched_issues().await?;
3491        self.completed_with_written(found, |_| true)
3492    }
3493
3494    /// Every issue this board's own search reports, walked to exhaustion, read once per
3495    /// source.
3496    ///
3497    /// The uncompleted half of [`Self::board_issues`], separated because [`Self::board`]
3498    /// needs it too and the two would otherwise walk the same search twice in one command.
3499    /// See [`Self::search_cache`] for why holding it is the same bargain holding the board
3500    /// is.
3501    async fn searched_issues(&self) -> Result<Vec<Resolved>, SourceError> {
3502        let cached = self.search_cache()?.clone();
3503        if let Some(held) = cached {
3504            return Ok(held);
3505        }
3506        let mut after: Option<String> = None;
3507        let mut found = Vec::new();
3508        let search = self.board_search(None);
3509        loop {
3510            let (page, next) = self
3511                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
3512                .await?;
3513            found.extend(page);
3514            match next {
3515                Some(next) => after = Some(next),
3516                None => break,
3517            }
3518        }
3519        *self.search_cache()? = Some(found.clone());
3520        Ok(found)
3521    }
3522
3523    /// This process's own view of the board's issues, or the refusal a poisoned lock is.
3524    fn search_cache(
3525        &self,
3526    ) -> Result<std::sync::MutexGuard<'_, Option<Vec<Resolved>>>, SourceError> {
3527        self.search_cache
3528            .lock()
3529            .map_err(|_| SourceError::Unavailable {
3530                message: "this source's view of the board's issues was left inconsistent by an \
3531                      earlier failure; next: run the command again"
3532                    .into(),
3533            })
3534    }
3535
3536    /// `found`, with everything this run wrote that `keep` accepts and the read did not
3537    /// report.
3538    ///
3539    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
3540    /// at all: the search index is behind, and a node read of an item filed moments ago can
3541    /// be too.
3542    fn completed_with_written(
3543        &self,
3544        mut found: Vec<Resolved>,
3545        keep: impl Fn(&Resolved) -> bool,
3546    ) -> Result<Vec<Resolved>, SourceError> {
3547        for own in self.created()?.iter().filter(|own| keep(own)) {
3548            if !found.iter().any(|item| item.id == own.id) {
3549                found.push(own.clone());
3550            }
3551        }
3552        Ok(found)
3553    }
3554
3555    /// What resolving one node id reached.
3556    ///
3557    /// Three answers rather than an `Option`, because a board *draft* is none of the other
3558    /// two: it is not an issue, so the issue fragment reads nothing of it, and a read of one
3559    /// is completed by a read of the draft itself rather than reported as nothing.
3560    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
3561        let asked = self
3562            .graphql(
3563                graphql::ISSUE,
3564                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
3565                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
3566            )
3567            .await;
3568        let data = match asked {
3569            Ok(data) => data,
3570            // A string that is not a node id at all is not a failure to report: it is an id
3571            // this board does not hold, which is what every read of one already answers.
3572            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
3573            Err(error) => return Err(error),
3574        };
3575        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
3576            return Ok(Reached::Nothing);
3577        };
3578        if optional_str(node, "__typename")? == Some("DraftIssue") {
3579            return Ok(Reached::Draft);
3580        }
3581        Ok(match self.resolve_issue(node).await? {
3582            Some(item) => Reached::Held(Box::new(item)),
3583            None => Reached::Nothing,
3584        })
3585    }
3586
3587    /// One item of this board by its own id, whatever kind it is.
3588    ///
3589    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
3590    /// run wrote is read first, because a node read of an item created moments ago can
3591    /// still be behind the board field values written onto it — see [`Self::created`].
3592    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
3593        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
3594            return Ok(Some(own.clone()));
3595        }
3596        match self.reach(id).await? {
3597            Reached::Held(item) => Ok(Some(*item)),
3598            Reached::Nothing => Ok(None),
3599            Reached::Draft => self.draft_by_id(id).await,
3600        }
3601    }
3602
3603    /// One board draft by its own id, with the board item it sits in — or `None` when no
3604    /// item of this board is that draft's.
3605    ///
3606    /// The same decision [`Self::resolve_issue`] makes for an issue, over the draft's own
3607    /// `projectV2Items`: an entry naming this board is what makes it this board's. GitHub
3608    /// links a draft to one board item, so the page this read carries is the whole of that
3609    /// connection, and a page that reports more than it holds is refused rather than read
3610    /// as an answer about memberships nobody read.
3611    async fn draft_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
3612        let data = self
3613            .graphql(
3614                graphql::DRAFT,
3615                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
3616                       "boardItems":BOARD_ITEMS_PAGE_SIZE}),
3617            )
3618            .await?;
3619        // Gone between the two reads is an answer — the draft is no longer there. Anything
3620        // else than the draft [`Self::reach`] was just told this id is, is not one.
3621        let Some(draft) = data.get("node").filter(|node| !node.is_null()) else {
3622            return Ok(None);
3623        };
3624        if optional_str(draft, "__typename")? != Some("DraftIssue") {
3625            return Err(SourceError::Malformed {
3626                message: format!(
3627                    "GitHub answered {} as a draft and then as something else",
3628                    id.0
3629                ),
3630            });
3631        }
3632        if required_str(draft, "id")? != id.0 {
3633            return Err(SourceError::Malformed {
3634                message: format!("GitHub answered a different draft for {}", id.0),
3635            });
3636        }
3637        let memberships = draft
3638            .get("projectV2Items")
3639            .ok_or_else(|| SourceError::Malformed {
3640                message: format!("GitHub draft {} is missing projectV2Items", id.0),
3641            })?;
3642        let nodes = memberships
3643            .get("nodes")
3644            .and_then(Value::as_array)
3645            .ok_or_else(|| SourceError::Malformed {
3646                message: format!("GitHub draft {} projectV2Items.nodes is not an array", id.0),
3647            })?;
3648        let info = memberships
3649            .get("pageInfo")
3650            .ok_or_else(|| SourceError::Malformed {
3651                message: format!("GitHub draft {} projectV2Items has no pageInfo", id.0),
3652            })?;
3653        // Read whether or not this board's entry is on the page: a page claiming more than
3654        // the one item GitHub links a draft to is a malformed answer either way.
3655        if required_bool(info, "hasNextPage")? || nodes.len() > 1 {
3656            return Err(SourceError::Malformed {
3657                message: format!(
3658                    "GitHub draft {} reports more board items than the one GitHub links a draft \
3659                     to",
3660                    id.0
3661                ),
3662            });
3663        }
3664        if let Some(node) = nodes.first()
3665            && node
3666                .pointer("/project/number")
3667                .and_then(Value::as_u64)
3668                .is_none()
3669        {
3670            return Err(SourceError::Malformed {
3671                message: format!(
3672                    "GitHub draft {} board item has no numeric project number",
3673                    id.0
3674                ),
3675            });
3676        }
3677        let Some(held) = self.board_entry(nodes) else {
3678            return Ok(None);
3679        };
3680        if required_str(
3681            held.get("project").ok_or_else(|| SourceError::Malformed {
3682                message: format!("GitHub draft {} board item has no project", id.0),
3683            })?,
3684            "id",
3685        )? != self.board_fields().await?.id.as_str()
3686        {
3687            return Ok(None);
3688        }
3689        let item = json!({
3690            "id": required_str(held, "id")?,
3691            "project": held.get("project"),
3692            "fieldValues": held.get("fieldValues"),
3693            "content": draft,
3694        });
3695        self.resolve(&item)
3696    }
3697
3698    /// The board's own id and field definitions, for a write whose item does not carry
3699    /// them — never its items.
3700    ///
3701    /// A board this command has already listed supplies them, since it read them beside its
3702    /// items; otherwise they come from [`graphql::BOARD_FIELDS`], once per command. Neither
3703    /// is consulted about which items the board holds: see the module documentation for
3704    /// why a question about one known item is answered by reading that item.
3705    async fn board_fields(&self) -> Result<BoardFields, SourceError> {
3706        if let Some(board) = self.board_cache()?.as_ref() {
3707            return Ok(BoardFields {
3708                id: BoardId::parse(&board.id)?,
3709                fields: board.fields.clone(),
3710            });
3711        }
3712        if let Some(held) = self.fields_cache()?.clone() {
3713            return Ok(held);
3714        }
3715        let data = self
3716            .graphql(
3717                graphql::BOARD_FIELDS,
3718                json!({"owner":self.owner,"number":self.project_number,
3719                       "nestedFirst":NESTED_PAGE_SIZE}),
3720            )
3721            .await?;
3722        let board = data
3723            .pointer("/boardFields/projectV2")
3724            .filter(|value| !value.is_null())
3725            .ok_or_else(|| SourceError::Refused {
3726                message: format!(
3727                    "GitHub project {}/{} was not found or is not visible to the token",
3728                    self.owner, self.project_number
3729                ),
3730            })?;
3731        let read = BoardFields {
3732            id: BoardId::parse(required_str(board, "id")?)?,
3733            fields: board.get("fields").cloned().unwrap_or(Value::Null),
3734        };
3735        *self.fields_cache()? = Some(read.clone());
3736        Ok(read)
3737    }
3738
3739    /// This process's own view of the board's fields, or the refusal a poisoned lock is.
3740    fn fields_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<BoardFields>>, SourceError> {
3741        self.fields_cache
3742            .lock()
3743            .map_err(|_| SourceError::Unavailable {
3744                message: "this source's view of the board's fields was left inconsistent by an \
3745                      earlier failure; next: run the command again"
3746                    .into(),
3747            })
3748    }
3749
3750    /// What a write to `item` needs of the board, read off that item when it says enough and
3751    /// off [`Self::board_fields`] when it does not.
3752    ///
3753    /// A node read of an item names its board and carries the definition of every field it
3754    /// holds a value of — so an item naming its board, holding a value of the origin field,
3755    /// and, when the write carries a status, holding a `Status` value, needs no read of the
3756    /// board at all. **Nothing the item does not say is guessed:** a field it holds no value
3757    /// of may still be on the board, and a view reading it as absent would refuse a write the
3758    /// board can take or skip a field write the board needs, so such an item — and a create,
3759    /// which has no item yet — takes the board's fields from their own read instead.
3760    async fn fields_for(
3761        &self,
3762        item: Option<&Resolved>,
3763        writes_status: bool,
3764        selects_priority: bool,
3765    ) -> Result<BoardFields, SourceError> {
3766        if let Some(item) = item
3767            && let Some(board_id) = item.named_board()
3768            && item.defines(ORIGIN_FIELD)
3769            && (!writes_status || item.defines("Status"))
3770            && (!selects_priority || item.defines(PRIORITY_FIELD))
3771        {
3772            return Ok(BoardFields {
3773                id: board_id,
3774                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
3775            });
3776        }
3777        self.board_fields().await
3778    }
3779
3780    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
3781    /// when that id names nothing here with a sub-issue relationship to walk.
3782    ///
3783    /// `None` and an empty answer are different: `None` is *this is not an issue of this
3784    /// GitHub*, which is what sends a project selector on to be read as a name, and an
3785    /// empty vector is a project that holds nothing.
3786    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
3787        let mut after: Option<String> = None;
3788        let mut children = Vec::new();
3789        loop {
3790            let asked = self
3791                .graphql(
3792                    graphql::SUB_ISSUES,
3793                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
3794                           "nestedFirst":NESTED_PAGE_SIZE,
3795                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
3796                )
3797                .await;
3798            let data = match asked {
3799                Ok(data) => data,
3800                // A string that is not a node id at all is not a failure to report: it is
3801                // the ordinary answer to a selector naming a project by its name.
3802                Err(error) if unresolvable_node(&error) => return Ok(None),
3803                Err(error) => return Err(error),
3804            };
3805            let Some(connection) = data
3806                .pointer("/node/subIssues")
3807                .filter(|value| !value.is_null())
3808            else {
3809                // No such node, or one with no sub-issue relationship — a board draft is
3810                // the one this board can really hold.
3811                return Ok(None);
3812            };
3813            for node in connection
3814                .get("nodes")
3815                .and_then(Value::as_array)
3816                .ok_or_else(|| SourceError::Malformed {
3817                    message: "GitHub subIssues.nodes is not an array".into(),
3818                })?
3819            {
3820                if let Some(resolved) = self.resolve_issue(node).await? {
3821                    children.push(resolved);
3822                }
3823            }
3824            let info = connection
3825                .get("pageInfo")
3826                .ok_or_else(|| SourceError::Malformed {
3827                    message: "GitHub subIssues connection has no pageInfo".into(),
3828                })?;
3829            let next = required_bool(info, "hasNextPage")?
3830                .then(|| required_str(info, "endCursor"))
3831                .transpose()?;
3832            match next {
3833                Some(next) => {
3834                    validate_cursor_progress(after.as_deref(), next)?;
3835                    after = Some(next.to_owned());
3836                }
3837                None => return Ok(Some(children)),
3838            }
3839        }
3840    }
3841
3842    /// Which issue of this board a project *name* is, or `None` when none is.
3843    ///
3844    /// One bounded query which filters on that name at the server, rather than a walk of
3845    /// every issue the board holds. The name is compared again here: the qualifier narrows
3846    /// what GitHub sends, and this source decides what it names.
3847    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
3848        let search = self.board_search(Some(&title_qualifier(name)));
3849        let (candidates, _) = self.search_page(&search, MAX_PAGE_SIZE, None).await?;
3850        Ok(candidates
3851            .into_iter()
3852            .find(|item| {
3853                item.kind == BoardKind::Work(ItemKind::Project)
3854                    && item.title.eq_ignore_ascii_case(name)
3855            })
3856            .map(|item| item.id))
3857    }
3858
3859    /// Everything filed under one project of this board: the sub-issues of the issue that
3860    /// project is.
3861    ///
3862    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
3863    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
3864    /// gains projects, or as another project gains tasks.
3865    ///
3866    /// A qualified id names the issue and is asked for its sub-issues directly: one
3867    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
3868    /// read as a project *name*, which costs the one bounded search
3869    /// [`Self::project_by_name`] makes.
3870    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
3871        let (project, children) = match self.sub_issues(selector).await? {
3872            Some(children) => (selector.clone(), children),
3873            None => match self.project_by_name(&selector.0).await? {
3874                Some(project) => {
3875                    let children = self.sub_issues(&project).await?.unwrap_or_default();
3876                    (project, children)
3877                }
3878                None => return Ok(Vec::new()),
3879            },
3880        };
3881        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
3882    }
3883
3884    /// Every item on the board: the union of both enumerations GitHub offers of one.
3885    ///
3886    /// Neither contains the other, so neither is dropped — only `ProjectV2.items` lists a
3887    /// board **draft** and reads the board's own fields beside its items, and only the search
3888    /// reports an item that connection is behind on. The module documentation is where the lag and the
3889    /// measurements behind it are written down.
3890    ///
3891    /// A search result is admitted on the same terms as any other issue this source reaches
3892    /// directly — [`Self::resolve_issue`] keeps it only if that issue's own `projectItems`
3893    /// names *this* board — so an issue the index still believes is here after it was taken
3894    /// off is refused rather than reported.
3895    ///
3896    /// See [`Self::board_cache`]. Both completions happen on every call rather than once,
3897    /// which is what the cache could otherwise have broken.
3898    async fn board(&self) -> Result<Board, SourceError> {
3899        let cached = self.board_cache()?.clone();
3900        let mut board = match cached {
3901            Some(board) => board,
3902            None => {
3903                let read = self.read_board().await?;
3904                *self.board_cache()? = Some(read.clone());
3905                read
3906            }
3907        };
3908        for held in self.searched_issues().await? {
3909            if !board.items.iter().any(|item| item.id == held.id) {
3910                board.items.push(held);
3911            }
3912        }
3913        for own in self.created()?.iter() {
3914            if !board.items.iter().any(|item| item.id == own.id) {
3915                board.items.push(own.clone());
3916            }
3917        }
3918        Ok(board)
3919    }
3920
3921    /// This process's own view of the board, or the refusal a poisoned lock is.
3922    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
3923        self.board_cache
3924            .lock()
3925            .map_err(|_| SourceError::Unavailable {
3926                message: "this source's view of the board was left inconsistent by an earlier \
3927                      failure; next: run the command again"
3928                    .into(),
3929            })
3930    }
3931
3932    /// Bring this process's own view of the board up to an item it has just written.
3933    ///
3934    /// A created item goes to `created`, which is what completes a board read GitHub's own
3935    /// eventual consistency has left behind. An item that was already there is replaced
3936    /// where it sits, so a second write of it in the same command reads its real parent
3937    /// rather than the one it had before the first write.
3938    ///
3939    /// "Where it sits" is three places, and missing an earlier one leaves a stale record
3940    /// that wins: an item this same run created is held in `created` and not in the cached
3941    /// board, and `board` completes the cached board *from* `created`, so replacing only
3942    /// the cached copy of such an item replaces nothing and the read still reports the
3943    /// title it was created with. The search is the third, and it is the one an item the
3944    /// board's own projection is behind on sits in *alone* — which is exactly the item this
3945    /// source is least able to re-read, so leaving it out would put the stale title back on
3946    /// the only items the completion in [`Self::board`] exists for.
3947    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
3948        if created {
3949            self.created()?.push(item);
3950            return Ok(());
3951        }
3952        {
3953            let mut own = self.created()?;
3954            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
3955                *held = item;
3956                return Ok(());
3957            }
3958        }
3959        if let Some(board) = self.board_cache()?.as_mut()
3960            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
3961        {
3962            *held = item.clone();
3963        }
3964        if let Some(found) = self.search_cache()?.as_mut()
3965            && let Some(held) = found.iter_mut().find(|held| held.id == item.id)
3966        {
3967            *held = item;
3968        }
3969        Ok(())
3970    }
3971
3972    /// Forget one item this process has just deleted, from every half of its own view.
3973    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
3974        self.created()?.retain(|own| own.id != *id);
3975        if let Some(board) = self.board_cache()?.as_mut() {
3976            board.items.retain(|item| item.id != *id);
3977        }
3978        if let Some(found) = self.search_cache()?.as_mut() {
3979            found.retain(|item| item.id != *id);
3980        }
3981        Ok(())
3982    }
3983
3984    /// Every page of the board, read from GitHub.
3985    async fn read_board(&self) -> Result<Board, SourceError> {
3986        let mut after: Option<String> = None;
3987        let mut items = Vec::new();
3988        let mut board;
3989        loop {
3990            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
3991            for item in page
3992                .pointer("/items/nodes")
3993                .and_then(Value::as_array)
3994                .ok_or_else(|| SourceError::Malformed {
3995                    message: "GitHub project items.nodes is not an array".into(),
3996                })?
3997            {
3998                if let Some(resolved) = self.resolve(item)? {
3999                    items.push(resolved);
4000                }
4001            }
4002            let info = page
4003                .pointer("/items/pageInfo")
4004                .ok_or_else(|| SourceError::Malformed {
4005                    message: "GitHub project items have no pageInfo".into(),
4006                })?;
4007            let has_next = required_bool(info, "hasNextPage")?;
4008            let next = has_next
4009                .then(|| required_str(info, "endCursor"))
4010                .transpose()?;
4011            board = page.clone();
4012            match next {
4013                Some(next) => {
4014                    validate_cursor_progress(after.as_deref(), next)?;
4015                    after = Some(next.to_owned());
4016                }
4017                None => break,
4018            }
4019        }
4020        Ok(Board {
4021            id: required_str(&board, "id")?.to_owned(),
4022            fields: board.get("fields").cloned().unwrap_or(Value::Null),
4023            items,
4024        })
4025    }
4026
4027    /// The items this source has created, for completing a board read that is behind.
4028    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
4029        self.created.lock().map_err(|_| SourceError::Unavailable {
4030            message: "this source's record of what it created in this run was left \
4031                      inconsistent by an earlier failure; next: run the command again"
4032                .into(),
4033        })
4034    }
4035
4036    /// One board item as this source reports it, or `None` for content it ignores.
4037    ///
4038    /// A pull request is neither a project nor a task — it is somebody's change, not a
4039    /// unit of plan — and an item whose content the token cannot see has nothing to
4040    /// report at all.
4041    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
4042        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
4043            message: "GitHub project item is missing content".into(),
4044        })?;
4045        if content.is_null() {
4046            return Ok(None);
4047        }
4048        let content_kind = match required_str(content, "__typename")? {
4049            "Issue" => ContentKind::Issue,
4050            "DraftIssue" => ContentKind::DraftIssue,
4051            _ => return Ok(None),
4052        };
4053        let field_values = item
4054            .get("fieldValues")
4055            .ok_or_else(|| SourceError::Malformed {
4056                message: "GitHub project item is missing fieldValues".into(),
4057            })?;
4058        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
4059        let nodes = field_values
4060            .get("nodes")
4061            .and_then(Value::as_array)
4062            .ok_or_else(|| SourceError::Malformed {
4063                message: "GitHub project item fieldValues.nodes is not an array".into(),
4064            })?;
4065        if let Some(labels) = content.get("labels") {
4066            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
4067        }
4068        let raw_body = optional_str(content, "body")?.map(str::to_owned);
4069        let (body, slot) = metadata_body(raw_body.clone())?;
4070        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
4071            .map(|id| NativeId(id.to_owned()));
4072        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
4073        // to read one from; it is a task, and never a project.
4074        let sub_issues = match content_kind {
4075            ContentKind::Issue => sub_issue_total(content)?,
4076            ContentKind::DraftIssue => 0,
4077        };
4078        let content_id = required_str(content, "id")?;
4079        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
4080            message: format!("GitHub issue {content_id}: {message}"),
4081        })?;
4082        let raw_title = required_str(content, "title")?;
4083        // The design prefix is read *first*, before either of the two rules that separate
4084        // a project from a task. A document is not work whatever sub-issues it has and
4085        // whatever marker it carries, and reading the prefix later would make a design
4086        // issue with none of either an empty project.
4087        let kind = if raw_title.starts_with(DESIGN_TITLE_PREFIX) {
4088            BoardKind::Document
4089        } else if parent.is_some() {
4090            // Being a sub-issue wins outright, and no marker overrides it: an issue filed
4091            // under a project is that project's task even when it has sub-issues of its
4092            // own.
4093            BoardKind::Work(ItemKind::Task)
4094        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
4095            BoardKind::Work(ItemKind::Project)
4096        } else {
4097            BoardKind::Work(ItemKind::Task)
4098        };
4099        // The title a person wrote, which for a document is the one without the prefix —
4100        // the same way `content` above is the body without this source's metadata slot.
4101        let title = match kind {
4102            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
4103            BoardKind::Work(_) => raw_title.to_owned(),
4104        };
4105        let own_repository = content
4106            .pointer("/repository/nameWithOwner")
4107            .and_then(Value::as_str)
4108            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
4109            .transpose()
4110            .map_err(|message| SourceError::Malformed { message })?;
4111        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
4112            Repository::from_metadata(&slot)
4113                .map_err(|message| SourceError::Malformed { message })?
4114        } else {
4115            own_repository.clone().into_iter().collect()
4116        };
4117        let id = NativeId(content_id.to_owned());
4118        // Read only for a task, because only a task has either list: a project or a
4119        // document holding one of these keys holds nothing this source reports, and the
4120        // keys are left out of its caller-visible metadata all the same.
4121        let (delivers, delivered_by) = if kind == BoardKind::Work(ItemKind::Task) {
4122            let listed = |key: &str| {
4123                TaskRef::from_value(key, &id, Some(&self.name), slot.get(key))
4124                    .map_err(|message| SourceError::Malformed { message })
4125            };
4126            (
4127                listed(TaskRef::DELIVERS_KEY)?,
4128                listed(TaskRef::DELIVERED_BY_KEY)?,
4129            )
4130        } else {
4131            (Vec::new(), Vec::new())
4132        };
4133        let (option, closed, reason) = Self::status_parts(nodes, content)?;
4134        let priority = self.held_priority(nodes)?;
4135        Ok(Some(Resolved {
4136            item_id: required_str(item, "id")?.to_owned(),
4137            id,
4138            content_kind,
4139            kind,
4140            title,
4141            body: body.filter(|value| !value.is_empty()),
4142            raw_body,
4143            status: self.statuses.status(option, closed, reason),
4144            option: option.map(str::to_owned),
4145            priority,
4146            closed,
4147            delivers,
4148            delivered_by,
4149            labels: labels(content)?,
4150            parent,
4151            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
4152            number: match content_kind {
4153                ContentKind::Issue => Some(issue_number(content)?),
4154                // A draft is filed in no repository, so nothing ever numbered it:
4155                // `DraftIssue` declares no `number` at all, exactly as it declares no
4156                // `subIssuesSummary` the branch above reads.
4157                ContentKind::DraftIssue => None,
4158            },
4159            url: optional_str(content, "url")?.map(str::to_owned),
4160            created_at: optional_time(content, "createdAt")?,
4161            updated_at: optional_time(content, "updatedAt")?,
4162            own_repository,
4163            repositories,
4164            slot,
4165            // Present when the item was reached through its own issue, whose board entry
4166            // names the board; a read of the board's own items has the board already. An
4167            // empty id names nothing a field write could address, so it is read as absent and
4168            // the write goes back to reading the board.
4169            board_id: item
4170                .pointer("/project/id")
4171                .and_then(Value::as_str)
4172                .filter(|id| !id.is_empty())
4173                .map(str::to_owned),
4174            fields: field_definitions(nodes),
4175        }))
4176    }
4177
4178    /// What one board item's `Priority` field says, through this instance's mapping.
4179    ///
4180    /// An instance with no mapping holds no priority, so every item reads as `none` whatever
4181    /// its board holds. With one, no value is `none`, a mapped option is its level, and an
4182    /// option the mapping does not name is kept as itself — never read as a level or as
4183    /// `none` — for a read of the task to report by name.
4184    fn held_priority(&self, field_values: &[Value]) -> Result<HeldPriority, SourceError> {
4185        let Some(mapping) = &self.priorities else {
4186            return Ok(HeldPriority::Read(Priority::None));
4187        };
4188        // A value of the field that names no option — a text field someone called `Priority` —
4189        // is malformed rather than `none`: reading it as no priority would let the next copy
4190        // clear one a person set.
4191        let Some(option) = field_values
4192            .iter()
4193            .find(|value| {
4194                value.pointer("/field/name").and_then(Value::as_str) == Some(PRIORITY_FIELD)
4195            })
4196            .map(|value| required_str(value, "name"))
4197            .transpose()?
4198        else {
4199            return Ok(HeldPriority::Read(Priority::None));
4200        };
4201        Ok(mapping.priority_of(option).map_or_else(
4202            || HeldPriority::Unmapped(option.to_owned()),
4203            HeldPriority::Read,
4204        ))
4205    }
4206
4207    /// What one board item's status is read from: its `Status` option, whether its issue
4208    /// is closed, and the reason it was closed with. [`StatusMapping::status`] turns the
4209    /// three into the status it reports.
4210    fn status_parts<'a>(
4211        field_values: &'a [Value],
4212        content: &'a Value,
4213    ) -> Result<(Option<&'a str>, bool, Option<&'a str>), SourceError> {
4214        let option = field_values
4215            .iter()
4216            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
4217            .map(|value| required_str(value, "name"))
4218            .transpose()?;
4219        let closed = optional_str(content, "state")? == Some("CLOSED");
4220        Ok((option, closed, optional_str(content, "stateReason")?))
4221    }
4222
4223    /// The board Status option this write selects, or the refusal that says why not.
4224    ///
4225    /// The mapped option is required for both open and terminal targets. A terminal write
4226    /// validates it before changing either representation, so it can never fall back to
4227    /// closing an issue whose board cannot display the matching status.
4228    ///
4229    /// Answers the field's id, the option's id, and the option's name as the board spells
4230    /// it — which is the name a read of the item reports once it sits there.
4231    fn column_for(
4232        &self,
4233        fields: &Value,
4234        status: &Status,
4235        target: &StatusTarget,
4236    ) -> Result<Option<(String, String, String)>, SourceError> {
4237        let wanted = match target {
4238            StatusTarget::Column(wanted) | StatusTarget::Terminal(wanted, _) => wanted.as_str(),
4239            StatusTarget::Disabled => return Ok(None),
4240        };
4241        let missing = |detail: &str| SourceError::Refused {
4242            message: format!(
4243                "status {} of source {} needs the board Status option {wanted:?}, and {detail};                  add that option to the board, or point status_mapping.{} of this source at one                  it has",
4244                category_name(status.category),
4245                self.name,
4246                category_name(status.category)
4247            ),
4248        };
4249        let Some(field) = Board::field(fields, "Status")? else {
4250            return Err(missing("this board has no Status field"));
4251        };
4252        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
4253            return Err(missing(
4254                "this board's Status field is not a single-select field",
4255            ));
4256        }
4257        let option = field
4258            .get("options")
4259            .and_then(Value::as_array)
4260            .and_then(|options| {
4261                options.iter().find(|option| {
4262                    option
4263                        .get("name")
4264                        .and_then(Value::as_str)
4265                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
4266                })
4267            });
4268        match option {
4269            None => Err(missing("this board does not have it")),
4270            Some(option) => Ok(Some((
4271                required_str(field, "id")?.to_owned(),
4272                required_str(option, "id")?.to_owned(),
4273                required_str(option, "name")?.to_owned(),
4274            ))),
4275        }
4276    }
4277
4278    /// The refusal a status that closes an issue is answered with over a board draft.
4279    fn closes_a_draft(&self, category: StatusCategory) -> SourceError {
4280        SourceError::Refused {
4281            message: format!(
4282                "status {} of source {} closes the item's issue, and GitHub draft items have \
4283                 no open or closed state",
4284                category_name(category),
4285                self.name
4286            ),
4287        }
4288    }
4289
4290    /// What a status write to one item needs of the board: the board's id and the
4291    /// definition of its `Status` field, read off the item when the item says both.
4292    ///
4293    /// The same reasoning as [`Self::fields_for`]: a node read of the item names its board,
4294    /// and its `Status` value carries that field's definition, options and all. An item that
4295    /// does not say — no board id, or no `Status` value to read the field off — takes them
4296    /// from [`Self::board_fields`], which reads no item.
4297    async fn status_board(&self, item: &Resolved) -> Result<BoardFields, SourceError> {
4298        if item.defines("Status")
4299            && let Some(board_id) = item.named_board()
4300        {
4301            return Ok(BoardFields {
4302                id: board_id,
4303                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
4304            });
4305        }
4306        self.board_fields().await
4307    }
4308
4309    /// Set one task's status and nothing else; see [`TaskSource::set_task_status`].
4310    async fn set_status(
4311        &self,
4312        id: &NativeId,
4313        category: StatusCategory,
4314    ) -> Result<Option<Status>, SourceError> {
4315        // Refused before anything is read, in the words a write of the same status is.
4316        let target = self.resolved_target(category)?;
4317        let Some(mut item) = self
4318            .item_by_id(id)
4319            .await?
4320            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
4321        else {
4322            return Ok(None);
4323        };
4324        let board = self.status_board(&item).await?;
4325        let wanted = Status {
4326            category,
4327            name: category_name(category).to_owned(),
4328        };
4329        let (field, option, name) = self
4330            .column_for(&board.fields, &wanted, &target)?
4331            .ok_or_else(|| SourceError::Malformed {
4332                message: format!(
4333                    "status {} of source {} names no board Status option",
4334                    category_name(category),
4335                    self.name
4336                ),
4337            })?;
4338        match &target {
4339            StatusTarget::Terminal(_, reason) => {
4340                if item.content_kind == ContentKind::DraftIssue {
4341                    return Err(self.closes_a_draft(category));
4342                }
4343                self.set_item_field(
4344                    board.id.as_str(),
4345                    &item.item_id,
4346                    &field,
4347                    json!({"singleSelectOptionId": option}),
4348                )
4349                .await?;
4350                self.update_content(
4351                    ContentKind::Issue,
4352                    &item.id,
4353                    json!({"stateInput": state_input(Some(&target))}),
4354                )
4355                .await?;
4356                item.closed = true;
4357                item.status = self
4358                    .statuses
4359                    .status(Some(&name), true, Some(reason.reason()));
4360                item.option = Some(name);
4361            }
4362            StatusTarget::Column(_) => {
4363                // An option is what an open item's status is, so a closed issue is reopened
4364                // first — sitting closed in the column, it would read back as closed. A draft has
4365                // no state to reopen.
4366                if item.content_kind == ContentKind::Issue && item.closed {
4367                    self.update_content(
4368                        ContentKind::Issue,
4369                        &item.id,
4370                        json!({"stateInput": state_input(Some(&target))}),
4371                    )
4372                    .await?;
4373                    item.closed = false;
4374                }
4375                self.set_item_field(
4376                    board.id.as_str(),
4377                    &item.item_id,
4378                    &field,
4379                    json!({"singleSelectOptionId": option}),
4380                )
4381                .await?;
4382                item.status = self.statuses.status(Some(&name), false, None);
4383                item.option = Some(name);
4384            }
4385            StatusTarget::Disabled => unreachable!("resolved_target refused a disabled status"),
4386        }
4387        let status = item.status.clone();
4388        self.remember_written(item, false)?;
4389        Ok(Some(status))
4390    }
4391
4392    /// Replace one task's `delivered_by` and nothing else; see
4393    /// [`TaskSource::set_delivered_by`].
4394    ///
4395    /// One update of the body, which differs from the body GitHub holds only inside the
4396    /// metadata slot — see [`with_slot`]. A body that would not change is not sent at all.
4397    async fn replace_delivered_by(
4398        &self,
4399        id: &NativeId,
4400        delivered_by: &[TaskRef],
4401    ) -> Result<Option<()>, SourceError> {
4402        let entries = TaskRef::listed(
4403            TaskRef::DELIVERED_BY_KEY,
4404            id,
4405            Some(&self.name),
4406            delivered_by.to_vec(),
4407        )
4408        .map_err(|message| SourceError::Refused { message })?;
4409        let Some(mut item) = self
4410            .item_by_id(id)
4411            .await?
4412            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
4413        else {
4414            return Ok(None);
4415        };
4416        let mut slot = item.slot.clone();
4417        set_task_list(&mut slot, TaskRef::DELIVERED_BY_KEY, &entries);
4418        self.write_slot(&mut item, &slot).await?;
4419        item.delivered_by = entries;
4420        self.remember_written(item, false)?;
4421        Ok(Some(()))
4422    }
4423
4424    /// Set one caller key of the metadata slot of one issue of `kind`, and nothing else;
4425    /// see [`TaskSource::set_task_metadata`].
4426    ///
4427    /// `None` when this board holds no item by that id, or holds one of another kind. The
4428    /// answer is the item as this source now reads it, so what a caller is told the key
4429    /// holds is what the slot holds.
4430    ///
4431    /// A key already holding the value is answered without a write, compared as JSON rather
4432    /// than as the body's bytes: a slot a person spelled with other whitespace would
4433    /// otherwise be re-encoded, which is a write that changes nothing the caller asked for.
4434    async fn set_slot_key(
4435        &self,
4436        id: &NativeId,
4437        kind: BoardKind,
4438        key: &MetadataKey,
4439        value: &Value,
4440    ) -> Result<Option<Resolved>, SourceError> {
4441        let Some(mut item) = self.item_by_id(id).await?.filter(|item| item.kind == kind) else {
4442            return Ok(None);
4443        };
4444        if item.slot.get(key.as_str()) == Some(value) {
4445            return Ok(Some(item));
4446        }
4447        let mut slot = item.slot.clone();
4448        slot.insert(key.as_str().to_owned(), value.clone());
4449        self.write_slot(&mut item, &slot).await?;
4450        self.remember_written(item.clone(), false)?;
4451        Ok(Some(item))
4452    }
4453
4454    /// Put `slot` in one item's metadata slot with a single update of its body, and bring
4455    /// `item` up to what that write left.
4456    ///
4457    /// The body sent differs from the body GitHub holds only inside the slot — see
4458    /// [`with_slot`] — and a body that would not change is not sent at all. It goes through
4459    /// the mutation the item's content takes, so a board draft's body is written with
4460    /// `updateProjectV2DraftIssue` exactly as an issue's is with `updateIssue`.
4461    async fn write_slot(
4462        &self,
4463        item: &mut Resolved,
4464        slot: &BTreeMap<String, Value>,
4465    ) -> Result<(), SourceError> {
4466        let held = item.raw_body.clone().unwrap_or_default();
4467        let body = with_slot(&held, slot)?;
4468        if body != held {
4469            self.update_content(item.content_kind, &item.id, json!({"body": body}))
4470                .await?;
4471        }
4472        let (visible, slot) = metadata_body(Some(body.clone()))?;
4473        item.body = visible.filter(|value| !value.is_empty());
4474        item.raw_body = Some(body);
4475        item.slot = slot;
4476        Ok(())
4477    }
4478
4479    /// This instance's target for a category, refusing one it has disabled.
4480    ///
4481    /// Nothing here mutates the board's option set to make room for a status. GitHub
4482    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
4483    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
4484    /// field and every item's status.
4485    fn resolved_target(&self, category: StatusCategory) -> Result<StatusTarget, SourceError> {
4486        let target = self.statuses.target(category).clone();
4487        if target != StatusTarget::Disabled {
4488            return Ok(target);
4489        }
4490        Err(SourceError::Refused {
4491            message: if category == StatusCategory::Draft {
4492                format!(
4493                    "status draft is disabled for source {}: draft is incompatible with this \
4494                     integration because GitHub draft issues cannot have sub-issues, and this \
4495                     source stores a project's tasks as its issue's sub-issues",
4496                    self.name
4497                )
4498            } else if category == StatusCategory::Unknown {
4499                format!(
4500                    "status {} is disabled for source {}; set status_mapping.{} of this source \
4501                     to one board Status option name; every word classified unknown is written \
4502                     to that one option",
4503                    category_name(category),
4504                    self.name,
4505                    category_name(category)
4506                )
4507            } else {
4508                format!(
4509                    "status {} is disabled for source {}; set status_mapping.{} of this source \
4510                     to a board Status option name",
4511                    category_name(category),
4512                    self.name,
4513                    category_name(category)
4514                )
4515            },
4516        })
4517    }
4518
4519    /// What writing `priority` does to one item's `Priority` field on this board, or the
4520    /// refusal naming what the board lacks.
4521    ///
4522    /// `none` is no value, so it clears the field — and asks nothing of an item that holds
4523    /// none already, or of an item not created yet. Every other priority selects the option
4524    /// the mapping names, matched case-insensitively; a board with no `Priority` field, or
4525    /// without that option, is refused rather than given one: reads and writes never create
4526    /// a field or an option.
4527    fn priority_write(
4528        &self,
4529        fields: &Value,
4530        existing: Option<&Resolved>,
4531        priority: Priority,
4532    ) -> Result<Option<PriorityWrite>, SourceError> {
4533        let Some(mapping) = &self.priorities else {
4534            return Err(self.holds_no_priority());
4535        };
4536        let Some(wanted) = mapping.option(priority) else {
4537            if !existing.is_some_and(Resolved::holds_priority) {
4538                return Ok(None);
4539            }
4540            let field =
4541                Board::field(fields, PRIORITY_FIELD)?.ok_or_else(|| SourceError::Malformed {
4542                    message: format!(
4543                        "an item holding a {PRIORITY_FIELD} value was read without that field"
4544                    ),
4545                })?;
4546            return Ok(Some(PriorityWrite::Clear {
4547                field: required_str(field, "id")?.to_owned(),
4548            }));
4549        };
4550        let missing = |detail: &str| SourceError::Refused {
4551            message: format!(
4552                "priority {priority} of source {} needs the board {PRIORITY_FIELD} option \
4553                 {wanted:?}, and {detail}; run `onetaskgraph sources fields {} --apply` to add \
4554                 it, or point priority_mapping.{priority} of this source at an option the board \
4555                 has",
4556                self.name, self.name
4557            ),
4558        };
4559        let Some(field) = Board::field(fields, PRIORITY_FIELD)? else {
4560            return Err(missing(&format!(
4561                "this board has no {PRIORITY_FIELD} field"
4562            )));
4563        };
4564        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
4565            return Err(missing(&format!(
4566                "this board's {PRIORITY_FIELD} field is not a single-select field"
4567            )));
4568        }
4569        // An options list that is absent or not a list is an answer this source cannot read,
4570        // not a board lacking the option: `sources fields --apply` is no remedy for it.
4571        let option = field
4572            .get("options")
4573            .and_then(Value::as_array)
4574            .ok_or_else(|| SourceError::Malformed {
4575                message: format!("GitHub {PRIORITY_FIELD} field options is not an array"),
4576            })?
4577            .iter()
4578            .find(|option| {
4579                option
4580                    .get("name")
4581                    .and_then(Value::as_str)
4582                    .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
4583            })
4584            .ok_or_else(|| missing("this board does not have it"))?;
4585        Ok(Some(PriorityWrite::Select {
4586            field: required_str(field, "id")?.to_owned(),
4587            option: required_str(option, "id")?.to_owned(),
4588        }))
4589    }
4590
4591    /// Apply one priority write to one board item.
4592    async fn write_priority(
4593        &self,
4594        board_id: &str,
4595        item_id: &str,
4596        write: &PriorityWrite,
4597    ) -> Result<(), SourceError> {
4598        match write {
4599            PriorityWrite::Select { field, option } => {
4600                self.set_item_field(
4601                    board_id,
4602                    item_id,
4603                    field,
4604                    json!({"singleSelectOptionId": option}),
4605                )
4606                .await
4607            }
4608            PriorityWrite::Clear { field } => {
4609                let data = self
4610                    .graphql(
4611                        graphql::CLEAR_FIELD,
4612                        json!({"input":{"projectId":board_id,"itemId":item_id,"fieldId":field}}),
4613                    )
4614                    .await?;
4615                let returned = data
4616                    .pointer("/clearProjectV2ItemFieldValue/projectV2Item")
4617                    .ok_or_else(|| SourceError::Malformed {
4618                        message: "GitHub field clear returned no project item".into(),
4619                    })?;
4620                if required_str(returned, "id")? != item_id {
4621                    return Err(SourceError::Malformed {
4622                        message: "GitHub field clear returned the wrong project item".into(),
4623                    });
4624                }
4625                Ok(())
4626            }
4627        }
4628    }
4629
4630    /// The refusal a priority is answered with by an instance configured with no
4631    /// `priority_mapping`, which holds none.
4632    fn holds_no_priority(&self) -> SourceError {
4633        SourceError::Refused {
4634            message: format!(
4635                "source {} holds no task priority: its configuration sets no priority_mapping; \
4636                 next: set priority_mapping on this source, then run `onetaskgraph sources \
4637                 fields {} --apply` to set its board up",
4638                self.name, self.name
4639            ),
4640        }
4641    }
4642
4643    /// Set one task's priority and nothing else; see [`TaskSource::set_task_priority`].
4644    ///
4645    /// One field write — a select, or a clear for `none` — and no title, body, label, state
4646    /// or `Status` request. Clearing a priority an item does not hold sends nothing.
4647    async fn set_priority(
4648        &self,
4649        id: &NativeId,
4650        priority: Priority,
4651    ) -> Result<Option<Priority>, SourceError> {
4652        if self.priorities.is_none() {
4653            return Err(self.holds_no_priority());
4654        }
4655        let Some(item) = self
4656            .item_by_id(id)
4657            .await?
4658            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
4659        else {
4660            return Ok(None);
4661        };
4662        if priority == Priority::None && !item.holds_priority() {
4663            return Ok(Some(priority));
4664        }
4665        // The item's own read carries the field's definition whenever it holds a value of
4666        // it, which a clear always does; a select onto an item holding none reads the board.
4667        let board = match item.named_board() {
4668            Some(id) if item.defines(PRIORITY_FIELD) => BoardFields {
4669                id,
4670                fields: json!({"nodes": item.fields, "pageInfo": {"hasNextPage": false}}),
4671            },
4672            _ => self.board_fields().await?,
4673        };
4674        let Some(write) = self.priority_write(&board.fields, Some(&item), priority)? else {
4675            return Ok(Some(priority));
4676        };
4677        self.write_priority(board.id.as_str(), &item.item_id, &write)
4678            .await?;
4679        // Read back rather than echoed: the answer is what the board now holds, read by the
4680        // item's own id — strongly consistent, unlike a search — and past what this run
4681        // remembers writing, so a write the board did not keep is reported as it stands.
4682        let read = match self.reach(id).await? {
4683            Reached::Held(item) => Some(*item),
4684            Reached::Draft => self.draft_by_id(id).await?,
4685            Reached::Nothing => None,
4686        }
4687        .ok_or_else(|| SourceError::Malformed {
4688            message: format!("task {id} was written and then could not be read back"),
4689        })?;
4690        let answer = read.task()?.priority;
4691        self.remember_written(read, false)?;
4692        Ok(Some(answer))
4693    }
4694
4695    /// Replace one task's visible body and nothing else; see
4696    /// [`TaskSource::set_task_content`].
4697    ///
4698    /// One update of the body, which differs from the body GitHub holds only outside the
4699    /// metadata slot — the slot is kept byte for byte, so every caller key and every list
4700    /// this source keeps there reads back as it was. A body that would not change is not
4701    /// sent at all.
4702    async fn replace_content(
4703        &self,
4704        id: &NativeId,
4705        content: &str,
4706    ) -> Result<Option<()>, SourceError> {
4707        let Some(mut item) = self
4708            .item_by_id(id)
4709            .await?
4710            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
4711        else {
4712            return Ok(None);
4713        };
4714        let held = item.raw_body.clone().unwrap_or_default();
4715        let body = with_content(&held, content)?;
4716        // Checked before anything is sent: content ending in what this source reads as its own
4717        // metadata slot would read back as metadata rather than as the content it was.
4718        let (visible, slot) = metadata_body(Some(body.clone()))?;
4719        if visible.as_deref().unwrap_or_default() != content || slot != item.slot {
4720            return Err(SourceError::Refused {
4721                message: format!(
4722                    "this content ends in what source {} reads as its own metadata slot \
4723                     ({METADATA_OPEN:?}), so part of it would read back as metadata rather than \
4724                     as content; next: remove that trailing block from the content",
4725                    self.name
4726                ),
4727            });
4728        }
4729        if body != held {
4730            self.update_content(item.content_kind, &item.id, json!({"body": body}))
4731                .await?;
4732        }
4733        item.body = visible.filter(|value| !value.is_empty());
4734        item.raw_body = Some(body);
4735        item.slot = slot;
4736        self.remember_written(item, false)?;
4737        Ok(Some(()))
4738    }
4739
4740    async fn set_item_field(
4741        &self,
4742        board_id: &str,
4743        item_id: &str,
4744        field_id: &str,
4745        value: Value,
4746    ) -> Result<(), SourceError> {
4747        let data = self
4748            .graphql(
4749                graphql::UPDATE_FIELD,
4750                json!({"input":{
4751                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
4752                }}),
4753            )
4754            .await?;
4755        let returned = data
4756            .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
4757            .ok_or_else(|| SourceError::Malformed {
4758                message: "GitHub field update returned no project item".into(),
4759            })?;
4760        if required_str(returned, "id")? != item_id {
4761            return Err(SourceError::Malformed {
4762                message: "GitHub field update returned the wrong project item".into(),
4763            });
4764        }
4765        Ok(())
4766    }
4767
4768    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
4769        let mut after: Option<String> = None;
4770        let mut ids = Vec::new();
4771        loop {
4772            let data = self
4773                .graphql(
4774                    graphql::ISSUE_DEPENDENCIES,
4775                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
4776                )
4777                .await?;
4778            let connection =
4779                data.pointer("/node/blockedBy")
4780                    .ok_or_else(|| SourceError::Malformed {
4781                        message: "GitHub dependency response has no blockedBy connection".into(),
4782                    })?;
4783            ids.extend(
4784                connection
4785                    .get("nodes")
4786                    .and_then(Value::as_array)
4787                    .ok_or_else(|| SourceError::Malformed {
4788                        message: "GitHub dependency response nodes is not an array".into(),
4789                    })?
4790                    .iter()
4791                    .map(|value| required_str(value, "id").map(str::to_owned))
4792                    .collect::<Result<Vec<_>, _>>()?,
4793            );
4794            let next = next_cursor(connection)?;
4795            if let Some(next) = &next {
4796                validate_cursor_progress(after.as_deref(), &next.0)?;
4797            }
4798            after = next.map(|cursor| cursor.0);
4799            if after.is_none() {
4800                return Ok(ids);
4801            }
4802        }
4803    }
4804
4805    async fn dependencies(
4806        &self,
4807        id: &NativeId,
4808        near_kind: ItemKind,
4809        direction: Direction,
4810        page: &PageRequest,
4811    ) -> Result<Page<DependencyEdge>, SourceError> {
4812        validate_page(page)?;
4813        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
4814        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
4815        let recorded = recorded_offset(cursor, direction)?;
4816        // Asked for even in the recorded phase, whose page reads nothing from the
4817        // connection: `__typename` is what says whether this item has a native
4818        // relationship at all, and that is what decides which far ends the reserved key is
4819        // allowed to hold.
4820        let data = self
4821            .graphql(
4822                graphql::ISSUE_DEPENDENCIES,
4823                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
4824                       "after":if recorded.is_some() {None} else {cursor}}),
4825            )
4826            .await?;
4827        let node =
4828            data.get("node")
4829                .filter(|v| !v.is_null())
4830                .ok_or_else(|| SourceError::Refused {
4831                    message: format!(
4832                        "GitHub item {} was not found or does not support dependencies",
4833                        id.0
4834                    ),
4835                })?;
4836        let connection_name = match direction {
4837            Direction::DependsOn => "blockedBy",
4838            Direction::DependedOnBy => "blocking",
4839        };
4840        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
4841        // named natively and the reserved key may hold any far end. An issue's connections
4842        // hold issues, and this source reads them at the near item's own level.
4843        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
4844        if let Some(offset) = recorded {
4845            return Ok(recorded_page(
4846                self.recorded_edges(id, near_kind, direction, natively_names, node)
4847                    .await?,
4848                offset,
4849                limit,
4850            ));
4851        }
4852        if natively_names.is_none() {
4853            return Ok(recorded_page(
4854                self.recorded_edges(id, near_kind, direction, natively_names, node)
4855                    .await?,
4856                0,
4857                limit,
4858            ));
4859        }
4860        let connection = node
4861            .get(connection_name)
4862            .ok_or_else(|| SourceError::Malformed {
4863                message: "GitHub dependency response is missing its connection".into(),
4864            })?;
4865        let nodes = connection
4866            .get("nodes")
4867            .and_then(Value::as_array)
4868            .ok_or_else(|| SourceError::Malformed {
4869                message: "GitHub dependency response nodes is not an array".into(),
4870            })?;
4871        // `from` depends on `to`, always. GitHub spells the same relationship from either
4872        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
4873        // it — so the near item is `from` in one direction and `to` in the other.
4874        let items = nodes
4875            .iter()
4876            .map(|value| {
4877                let related = NativeId(required_str(value, "id")?.into());
4878                let related_kind = related_kind(value)?;
4879                let (from, to) = match direction {
4880                    Direction::DependsOn => (
4881                        DependencyEndpoint::from_native(id.clone(), near_kind),
4882                        DependencyEndpoint::from_native(related, related_kind),
4883                    ),
4884                    Direction::DependedOnBy => (
4885                        DependencyEndpoint::from_native(related, related_kind),
4886                        DependencyEndpoint::from_native(id.clone(), near_kind),
4887                    ),
4888                };
4889                Ok(DependencyEdge {
4890                    from,
4891                    to,
4892                    kind: DependencyKind::Blocks,
4893                })
4894            })
4895            .collect::<Result<Vec<_>, SourceError>>()?;
4896        let mut next = next_cursor(connection)?;
4897        if let Some(next) = &next {
4898            validate_cursor_progress(cursor, &next.0)?;
4899        }
4900        if next.is_none()
4901            && !self
4902                .recorded_edges(id, near_kind, direction, natively_names, node)
4903                .await?
4904                .is_empty()
4905        {
4906            next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
4907        }
4908        Ok(Page { items, next })
4909    }
4910
4911    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
4912    /// a far end in another source has to live: no GitHub issue relationship can name one.
4913    ///
4914    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
4915    /// source never writes one down.
4916    ///
4917    /// The metadata lives in the item's own body slot, and `node` is the dependency read's
4918    /// own answer, which carries an issue's body — so an issue's recorded edges cost no
4919    /// request beyond the read already made, and reading the board for them would be a
4920    /// walk of every item for one field of one. A draft has no body in that answer, because
4921    /// a draft is not an issue, so a draft's are read off its own read by id — never off a
4922    /// listing of the board, which can be behind on the very item asked about.
4923    async fn recorded_edges(
4924        &self,
4925        id: &NativeId,
4926        near_kind: ItemKind,
4927        direction: Direction,
4928        natively_names: Option<ItemKind>,
4929        node: &Value,
4930    ) -> Result<Vec<DependencyEdge>, SourceError> {
4931        if direction != Direction::DependsOn {
4932            return Ok(Vec::new());
4933        }
4934        let slot = match node.get("body") {
4935            Some(body) if natively_names.is_some() => {
4936                metadata_body(body.as_str().map(str::to_owned))?.1
4937            }
4938            _ => {
4939                let Some(item) = self.item_by_id(id).await? else {
4940                    return Ok(Vec::new());
4941                };
4942                item.slot
4943            }
4944        };
4945        DependencyEdge::recorded(&slot, id, near_kind, &self.name, natively_names)
4946            .map_err(|message| SourceError::Malformed { message })
4947    }
4948
4949    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
4950        self.repository
4951            .as_ref()
4952            .ok_or_else(|| SourceError::Refused {
4953                message: format!(
4954                    "source {} has no repository configured, and a GitHub Projects board has no \
4955                 repository of its own to create an issue in; set repository: owner/name on \
4956                 this source",
4957                    self.name
4958                ),
4959            })
4960    }
4961
4962    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
4963    /// states.
4964    ///
4965    /// The fallback is demanded first, whichever arm answers: a write without a configured
4966    /// repository is refused naming the field exactly as it was before the rule existed,
4967    /// so a source that could not write before cannot write now, rather than writing for
4968    /// the one item whose own field happens to decide it.
4969    ///
4970    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
4971    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
4972    /// entry owned by someone other than the owner of the parent issue's repository —
4973    /// GitHub accepts a sub-issue from another repository of the same owner and from no
4974    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
4975    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
4976    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
4977    /// and is visible to the token is checked where its node id is resolved, still before
4978    /// `createIssue`. The parent is read by its own id through [`Self::item_by_id`] — never
4979    /// looked up in a listing of the board, which can be minutes behind an issue its own
4980    /// `projectItems` already places on it — and that read answers first from this process's
4981    /// own record, so a project created moments ago in this command answers though GitHub
4982    /// has not caught up.
4983    async fn creation_target(
4984        &self,
4985        incoming: &Incoming<'_>,
4986    ) -> Result<RepositoryTarget, SourceError> {
4987        let fallback = self.configured_repository()?;
4988        let what = |incoming: &Incoming<'_>| {
4989            format!(
4990                "{} {:?}",
4991                incoming.written.kind().describes(),
4992                incoming.title
4993            )
4994        };
4995        let parent = match incoming.parent {
4996            Some(parent) => Some(self.item_by_id(parent).await?.ok_or_else(|| {
4997                SourceError::Refused {
4998                    message: format!(
4999                        "GitHub project issue {} was not found on the board of source {}, so {} \
5000                         cannot be filed under it",
5001                        parent.0,
5002                        self.name,
5003                        what(incoming)
5004                    ),
5005                }
5006            })?),
5007            None => None,
5008        };
5009        let parents_repository = parent
5010            .as_ref()
5011            .map(|parent| {
5012                // A draft is on the board and so is found, but it has no repository to
5013                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
5014                // would refuse the task only once `createIssue` had made it.
5015                if parent.content_kind == ContentKind::DraftIssue {
5016                    return Err(SourceError::Refused {
5017                        message: format!(
5018                            "GitHub project item {} on the board of source {} is a draft, \
5019                             which cannot have sub-issues, so {} cannot be filed under it",
5020                            parent.id.0,
5021                            self.name,
5022                            what(incoming)
5023                        ),
5024                    });
5025                }
5026                // An issue's repository is where a sub-issue is placed and whose owner it
5027                // is compared against, so a parent whose repository this source cannot
5028                // spell as `owner/name` — GitHub's login grammar is wider than this
5029                // source's floor — is one nothing can be filed under.
5030                parent
5031                    .own_repository
5032                    .as_ref()
5033                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
5034                    .ok_or_else(|| SourceError::Malformed {
5035                        message: format!(
5036                            "GitHub project issue {} on the board of source {} is in {}, which \
5037                             is not a {}/owner/name repository this source can place {} in",
5038                            parent.id.0,
5039                            self.name,
5040                            parent
5041                                .own_repository
5042                                .as_ref()
5043                                .map_or("no repository", Repository::as_str),
5044                            RepositoryTarget::HOST,
5045                            what(incoming)
5046                        ),
5047                    })
5048            })
5049            .transpose()?;
5050        match incoming.repositories {
5051            [named] => {
5052                let target =
5053                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
5054                        message: format!(
5055                            "{} names repository {}, which is not a {}/owner/name repository \
5056                             source {} can create an issue in; name one that is, or name none",
5057                            what(incoming),
5058                            named.as_str(),
5059                            RepositoryTarget::HOST,
5060                            self.name
5061                        ),
5062                    })?;
5063                if let Some(parents) = &parents_repository
5064                    && parents.owner != target.owner
5065                {
5066                    return Err(SourceError::Refused {
5067                        message: format!(
5068                            "{} names repository {}, owned by {}, but its project's issue is in \
5069                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
5070                             of the same owner as its parent issue; name a repository of {}, or \
5071                             name none",
5072                            what(incoming),
5073                            target.slug(),
5074                            target.owner,
5075                            parents.slug(),
5076                            parents.owner,
5077                            parents.owner
5078                        ),
5079                    });
5080                }
5081                Ok(target)
5082            }
5083            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
5084        }
5085    }
5086
5087    /// The node id of the repository `incoming` is being created in, or the refusal naming
5088    /// the item and the repository the token cannot see.
5089    ///
5090    /// Resolved once per command per repository; see [`Self::repository_cache`].
5091    async fn repository_id(
5092        &self,
5093        repository: &RepositoryTarget,
5094        incoming: &Incoming<'_>,
5095    ) -> Result<String, SourceError> {
5096        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
5097            return Ok(id);
5098        }
5099        let data = self
5100            .graphql(
5101                graphql::REPOSITORY,
5102                json!({"owner":repository.owner,"name":repository.name}),
5103            )
5104            .await?;
5105        let node = data
5106            .get("repository")
5107            .filter(|value| !value.is_null())
5108            .ok_or_else(|| SourceError::Refused {
5109                message: format!(
5110                    "GitHub repository {} was not found or is not visible to the token, so {} \
5111                     {:?} cannot be created in it",
5112                    repository.slug(),
5113                    incoming.written.kind().describes(),
5114                    incoming.title
5115                ),
5116            })?;
5117        let id = required_str(node, "id")?.to_owned();
5118        self.repository_cache()?
5119            .insert(repository.clone(), id.clone());
5120        Ok(id)
5121    }
5122
5123    fn repository_cache(
5124        &self,
5125    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
5126        self.repository_cache
5127            .lock()
5128            .map_err(|_| SourceError::Unavailable {
5129                message: "this source's record of the destination repository was left \
5130                          inconsistent by an earlier failure; next: run the command again"
5131                    .into(),
5132            })
5133    }
5134
5135    /// Create or update one board item, whichever kind it is.
5136    async fn write_item(
5137        &self,
5138        incoming: &Incoming<'_>,
5139        target: Option<&NativeId>,
5140        depends_on: &[DependencyEdge],
5141    ) -> Result<NativeId, SourceError> {
5142        // Refused before anything is read or written: a task or a project titled the way
5143        // this board spells a document would land as an issue this same source reads back
5144        // as a document, so the field this destination cannot carry is named rather than
5145        // written and silently reclassified.
5146        if let Written::Work(kind, _) = incoming.written
5147            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
5148        {
5149            return Err(SourceError::Refused {
5150                message: format!(
5151                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
5152                     spells a document, so it would read back as one rather than as a {}; \
5153                     retitle it, or copy it as a document",
5154                    kind.marker(),
5155                    self.name,
5156                    kind.marker()
5157                ),
5158            });
5159        }
5160        // The destination is read by its own id, and whether this board holds it is decided
5161        // by that read — its own `projectItems` — rather than by whether a listing of the
5162        // board happens to include it yet. See the module documentation.
5163        let existing = match target {
5164            Some(target) => {
5165                Some(
5166                    self.item_by_id(target)
5167                        .await?
5168                        .ok_or_else(|| SourceError::Refused {
5169                            message: format!("GitHub destination item {} was not found", target.0),
5170                        })?,
5171                )
5172            }
5173            None => None,
5174        };
5175        let existing = existing.as_ref();
5176        let board = self
5177            .fields_for(
5178                existing,
5179                incoming.written.status().is_some(),
5180                incoming
5181                    .priority
5182                    .is_some_and(|priority| priority != Priority::None),
5183            )
5184            .await?;
5185        let status_target = incoming
5186            .written
5187            .status()
5188            .map(|status| self.resolved_target(status.category))
5189            .transpose()?;
5190        let column = match (incoming.written.status(), status_target.as_ref()) {
5191            (Some(status), Some(target)) => self.column_for(&board.fields, status, target)?,
5192            _ => None,
5193        };
5194        // Resolved before anything is created, for the reason the column above is: a
5195        // priority this board has no option for is refused while nothing has been written.
5196        let priority_write = match incoming.priority {
5197            Some(priority) => self.priority_write(&board.fields, existing, priority)?,
5198            None => None,
5199        };
5200        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
5201        if content_kind == ContentKind::DraftIssue {
5202            if let (Some(StatusTarget::Terminal(_, _)), Some(status)) =
5203                (status_target.as_ref(), incoming.written.status())
5204            {
5205                return Err(self.closes_a_draft(status.category));
5206            }
5207            if incoming.parent.is_some() {
5208                return Err(SourceError::Refused {
5209                    message: "GitHub draft items cannot be a project's sub-issue".into(),
5210                });
5211            }
5212        }
5213        match existing {
5214            Some(item) if content_kind == ContentKind::Issue => {
5215                if item.labels != incoming.labels {
5216                    return Err(SourceError::Refused {
5217                        message: "GitHub issue labels differ from the labels being written".into(),
5218                    });
5219                }
5220            }
5221            _ => {
5222                if !incoming.labels.is_empty() {
5223                    return Err(SourceError::Refused {
5224                        message: "GitHub items created by this destination carry no labels".into(),
5225                    });
5226                }
5227            }
5228        }
5229
5230        // An existing issue is never moved; a new one is created where the rule says. The
5231        // repository the issue really lives in is what the slot below is written against,
5232        // so a single entry that is where the issue is created travels as no key at all,
5233        // and the read side derives it back from the issue.
5234        let (own_repository, creation_target) = match existing {
5235            Some(item) => (item.own_repository.clone(), None),
5236            None => {
5237                let target = self.creation_target(incoming).await?;
5238                let origin = Repository::try_from(target.origin())
5239                    .map_err(|message| SourceError::Config { message })?;
5240                (Some(origin), Some(target))
5241            }
5242        };
5243        let (native, fallback) = self
5244            .partition_edges(incoming.written.kind(), content_kind, depends_on)
5245            .await?;
5246        let slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
5247        let body = compose_body(incoming.content, &slot)?;
5248        // Read before anything is created, for the reason the field below is: a value
5249        // this destination cannot store has to refuse, and refusing after `createIssue`
5250        // would leave an issue behind that nothing asked for. The engine writes a
5251        // qualified id here; a caller handing this key anything else is told so rather
5252        // than having it silently stored as no origin at all.
5253        // 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.
5254        let origin = match incoming.metadata.get(ORIGIN_KEY) {
5255            None => "",
5256            Some(Value::String(origin)) => origin.as_str(),
5257            Some(other) => {
5258                return Err(SourceError::Refused {
5259                    message: format!(
5260                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
5261                         is {other}"
5262                    ),
5263                });
5264            }
5265        };
5266        // Resolved before anything is created: a board that cannot carry the copy origin
5267        // has to refuse the write, and refusing it after `createIssue` would leave an
5268        // issue behind that nothing asked for.
5269        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
5270            Some(field) => {
5271                if required_str(field, "__typename")? != "ProjectV2Field" {
5272                    return Err(SourceError::Refused {
5273                        message: format!(
5274                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
5275                        ),
5276                    });
5277                }
5278                Some(required_str(field, "id")?.to_owned())
5279            }
5280            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
5281                return Err(SourceError::Refused {
5282                    message: format!(
5283                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
5284                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
5285                         the board"
5286                    ),
5287                });
5288            }
5289            None => None,
5290        };
5291
5292        let Landed {
5293            content_id,
5294            item_id,
5295            url,
5296            number,
5297        } = match existing {
5298            Some(item) => {
5299                self.update_existing(item, incoming, &body, status_target.as_ref())
5300                    .await?;
5301                Landed {
5302                    content_id: item.id.clone(),
5303                    item_id: item.item_id.clone(),
5304                    url: item.url.clone(),
5305                    number: item.number,
5306                }
5307            }
5308            None => {
5309                let target = creation_target
5310                    .as_ref()
5311                    .ok_or_else(|| SourceError::Malformed {
5312                        message: "a new item was decided without a repository to create it in"
5313                            .into(),
5314                    })?;
5315                self.create_and_file_issue(board.id.as_str(), target, incoming, &body)
5316                    .await?
5317            }
5318        };
5319
5320        let written_option = column.as_ref().map(|(_, _, name)| name.clone());
5321        let column = column.map(|(field, option, _)| (field, option));
5322        // Creating an item here is several calls — `createIssue`, `addProjectV2ItemById`,
5323        // then each board field, the parent and the dependencies — and GitHub can fail at
5324        // any of them. Everything this source can refuse *before* the first of those is
5325        // already checked above, so what is left is GitHub itself failing part way. When it
5326        // does over an item this call created, the issue is taken back: a write that
5327        // refused must not leave an item behind that nobody asked for, and one that does
5328        // makes the retry create a second.
5329        let landed = self
5330            .finish_write(
5331                board.id.as_str(),
5332                incoming,
5333                &content_id,
5334                &item_id,
5335                content_kind,
5336                existing,
5337                origin_field.as_deref(),
5338                origin,
5339                column,
5340                status_target.as_ref(),
5341                priority_write.as_ref(),
5342                &native,
5343            )
5344            .await;
5345        if let Err(error) = landed {
5346            if existing.is_none() {
5347                // Best effort, and the write's own failure is what the caller is told: a
5348                // refusal naming the tidy-up would hide why the write failed at all.
5349                let _ = self.delete_issue(&content_id).await;
5350            }
5351            return Err(error);
5352        }
5353
5354        let written_status = match (incoming.written.status(), status_target.as_ref()) {
5355            (Some(_), Some(StatusTarget::Terminal(_, reason))) => {
5356                self.statuses
5357                    .status(written_option.as_deref(), true, Some(reason.reason()))
5358            }
5359            (Some(_), Some(StatusTarget::Column(_))) => {
5360                self.statuses.status(written_option.as_deref(), false, None)
5361            }
5362            (Some(status), _) => status.clone(),
5363            (None, _) => Status {
5364                category: StatusCategory::Unknown,
5365                name: "Open".to_owned(),
5366            },
5367        };
5368
5369        // So the rest of this command reads what it just did rather than what the board
5370        // said before it. See `remember_written` for which half takes it.
5371        let remembered = Resolved {
5372            item_id,
5373            id: content_id.clone(),
5374            content_kind,
5375            kind: incoming.written.kind(),
5376            title: incoming.title.to_owned(),
5377            // The visible half of the body this write composed, split back off it the
5378            // way a read splits it — so what this record reports is what a read of the
5379            // same issue reports, rather than the person's text with the metadata slot
5380            // still on the end of it.
5381            body: metadata_body(body.clone())?.0,
5382            raw_body: body.clone(),
5383            // A document has no status of its own; what it reads back as is whatever
5384            // the issue's own state says, which is what a re-read reports.
5385            status: written_status,
5386            option: written_option.or_else(|| existing.and_then(|item| item.option.clone())),
5387            priority: match incoming.priority {
5388                Some(priority) => HeldPriority::Read(priority),
5389                None => existing.map_or(HeldPriority::Read(Priority::None), |item| {
5390                    item.priority.clone()
5391                }),
5392            },
5393            // What `state_input` asked for: closed for a terminal target, open for any other
5394            // status, and the issue's own state left as it was by a document write.
5395            closed: content_kind == ContentKind::Issue
5396                && match status_target.as_ref() {
5397                    Some(StatusTarget::Terminal(_, _)) => true,
5398                    Some(_) => false,
5399                    None => existing.is_some_and(|item| item.closed),
5400                },
5401            delivers: incoming.delivers.to_vec(),
5402            delivered_by: incoming.delivered_by.to_vec(),
5403            labels: incoming.labels.to_vec(),
5404            parent: incoming.parent.cloned(),
5405            origin: (!origin.is_empty()).then(|| origin.to_owned()),
5406            number,
5407            // In the update path this is the item's own url, read off `existing` where the
5408            // record above was bound, so one expression serves both halves.
5409            url,
5410            created_at: existing.and_then(|item| item.created_at),
5411            updated_at: existing.and_then(|item| item.updated_at),
5412            own_repository,
5413            repositories: incoming.repositories.to_vec(),
5414            slot,
5415            board_id: Some(board.id.as_str().to_owned()),
5416            fields: board
5417                .fields
5418                .get("nodes")
5419                .and_then(Value::as_array)
5420                .cloned()
5421                .unwrap_or_default(),
5422        };
5423        self.remember_written(remembered, existing.is_none())?;
5424        Ok(content_id)
5425    }
5426
5427    /// Everything a write does after the item exists: its board fields, its parent, and
5428    /// its dependencies.
5429    ///
5430    /// Split out of `write_item` so there is one place a failure past the point of no
5431    /// return is caught, rather than a tidy-up repeated at each `?` above.
5432    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
5433    // so there is one place a failure past the point of no return is caught, and its
5434    // arguments are exactly the values that tail already had in scope. Bundling them into a
5435    // struct would describe no concept — it would be "the arguments of this function" — and
5436    // would put the whole of `write_item`'s locals behind one more indirection.
5437    #[allow(clippy::too_many_arguments)]
5438    async fn finish_write(
5439        &self,
5440        board_id: &str,
5441        incoming: &Incoming<'_>,
5442        content_id: &NativeId,
5443        item_id: &str,
5444        content_kind: ContentKind,
5445        existing: Option<&Resolved>,
5446        origin_field: Option<&str>,
5447        origin: &str,
5448        column: Option<(String, String)>,
5449        status_target: Option<&StatusTarget>,
5450        priority: Option<&PriorityWrite>,
5451        native: &[String],
5452    ) -> Result<(), SourceError> {
5453        if let Some(field_id) = origin_field {
5454            self.set_item_field(board_id, item_id, field_id, json!({"text":origin}))
5455                .await?;
5456        }
5457
5458        if let Some((field_id, option_id)) = column {
5459            self.set_item_field(
5460                board_id,
5461                item_id,
5462                &field_id,
5463                json!({"singleSelectOptionId":option_id}),
5464            )
5465            .await?;
5466        }
5467
5468        if let Some(priority) = priority {
5469            self.write_priority(board_id, item_id, priority).await?;
5470        }
5471
5472        if content_kind == ContentKind::Issue
5473            && matches!(status_target, Some(StatusTarget::Terminal(_, _)))
5474        {
5475            self.update_content(
5476                ContentKind::Issue,
5477                content_id,
5478                json!({"stateInput":state_input(status_target)}),
5479            )
5480            .await?;
5481        }
5482
5483        if content_kind == ContentKind::Issue {
5484            self.reparent(
5485                existing.and_then(|item| item.parent.clone()),
5486                content_id,
5487                incoming.parent,
5488            )
5489            .await?;
5490            // A document takes part in no dependency graph, so writing one neither reads
5491            // nor changes the issue's own `blockedBy` relationships. Reconciling them
5492            // against the empty list a document write carries would *delete* whatever
5493            // relationships a person had made on that issue, which is a write nobody
5494            // asked for.
5495            if incoming.written.kind() != BoardKind::Document {
5496                self.reconcile_blocked_by(content_id, native).await?;
5497            }
5498        }
5499        Ok(())
5500    }
5501
5502    /// Delete one issue, which takes its board item with it.
5503    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
5504        let data = self
5505            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
5506            .await?;
5507        data.pointer("/deleteIssue/repository")
5508            .filter(|value| !value.is_null())
5509            .ok_or_else(|| SourceError::Malformed {
5510                message: "GitHub issue deletion returned no repository".into(),
5511            })?;
5512        self.forget(id)?;
5513        Ok(())
5514    }
5515
5516    /// Remove one item this copy created, so a copy that could not finish leaves the board
5517    /// as it found it.
5518    ///
5519    /// Deleting the issue takes its board item with it, so there is no second mutation to
5520    /// keep in step. An id the board does not hold is not an error: the item is already
5521    /// gone, which is the state this asks for. Which that is, is decided by reading the item
5522    /// by its own id — a listing of the board can still be missing an item it holds, and
5523    /// reading that as *already gone* would leave behind the very item this was asked to
5524    /// take back.
5525    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
5526        let Some(item) = self.item_by_id(id).await? else {
5527            return Ok(());
5528        };
5529        if item.content_kind == ContentKind::DraftIssue {
5530            return Err(SourceError::Refused {
5531                message: format!(
5532                    "GitHub item {} is a draft, and this source removes an item by deleting \
5533                     its issue; next: remove it from the board by hand",
5534                    id.0
5535                ),
5536            });
5537        }
5538        let data = self
5539            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
5540            .await?;
5541        data.pointer("/deleteIssue/repository")
5542            .filter(|value| !value.is_null())
5543            .ok_or_else(|| SourceError::Malformed {
5544                message: "GitHub issue deletion returned no repository".into(),
5545            })?;
5546        self.forget(id)?;
5547        Ok(())
5548    }
5549
5550    /// The issue a comment call on `task` is about, or `None` when this board holds no such
5551    /// task.
5552    ///
5553    /// Resolved exactly as [`TaskSource::get_task`] resolves it, so the comment verbs and a
5554    /// read of the task cannot disagree about which ids name one: a project or a document of
5555    /// this board is not a task here either.
5556    ///
5557    /// A **draft** is a task with nowhere to keep a comment, because GitHub keeps comments on
5558    /// issues and a draft is not one. It is refused rather than answered with an empty page,
5559    /// which would read as a task nobody has commented on yet.
5560    async fn commented_issue(&self, task: &NativeId) -> Result<Option<NativeId>, SourceError> {
5561        let Some(item) = self
5562            .item_by_id(task)
5563            .await?
5564            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
5565        else {
5566            return Ok(None);
5567        };
5568        if item.content_kind == ContentKind::DraftIssue {
5569            return Err(SourceError::Refused {
5570                message: format!(
5571                    "task {} of source {} is a draft item on the board, and GitHub keeps \
5572                     comments on issues alone, so a draft has none to read or write; next: \
5573                     convert the draft to an issue on the board, then comment on the issue it \
5574                     becomes",
5575                    task.0, self.name
5576                ),
5577            });
5578        }
5579        Ok(Some(item.id))
5580    }
5581
5582    /// Whether the comment `comment` is one of `issue`'s own.
5583    ///
5584    /// Read before an edit or a removal is sent, because GitHub's comment mutations take the
5585    /// comment's id and nothing else: a comment id given against the wrong task would
5586    /// otherwise change a comment on some other issue entirely. An id that names nothing, or
5587    /// names something that is not an issue comment, is a comment this task does not have —
5588    /// which is what GitHub refusing to resolve it means too.
5589    async fn comment_is_on(
5590        &self,
5591        issue: &NativeId,
5592        comment: &NativeId,
5593    ) -> Result<bool, SourceError> {
5594        let asked = self
5595            .graphql(graphql::COMMENT_ISSUE, json!({"id":comment.0}))
5596            .await;
5597        let data = match asked {
5598            Ok(data) => data,
5599            Err(error) if unresolvable_node(&error) => return Ok(false),
5600            Err(error) => return Err(error),
5601        };
5602        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
5603            return Ok(false);
5604        };
5605        if optional_str(node, "__typename")? != Some("IssueComment") {
5606            return Ok(false);
5607        }
5608        let on = node.get("issue").ok_or_else(|| SourceError::Malformed {
5609            message: format!("GitHub issue comment {} names no issue", comment.0),
5610        })?;
5611        Ok(required_str(on, "id")? == issue.0)
5612    }
5613
5614    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
5615    async fn partition_edges(
5616        &self,
5617        near_kind: BoardKind,
5618        near_content: ContentKind,
5619        depends_on: &[DependencyEdge],
5620    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
5621        let mut native = Vec::new();
5622        let mut fallback = Vec::new();
5623        for edge in depends_on {
5624            let same_source = edge
5625                .to
5626                .source()
5627                .is_none_or(|source| source == self.name.as_str());
5628            // A qualified id's source segment runs to its *first* colon — `GlobalId` and
5629            // `DependencyEndpoint::source` both read it that way — and a native id may hold
5630            // colons of its own, so the far end is everything after that one separator.
5631            // Splitting at the last would truncate `work:urn:task:7` to `7`.
5632            let far_id = if edge.to.is_qualified() {
5633                edge.to
5634                    .id()
5635                    .split_once(':')
5636                    .map_or(edge.to.id(), |(_, native)| native)
5637            } else {
5638                edge.to.id()
5639            };
5640            // A same-source far end is read by its own id, exactly as the item it is a far end
5641            // of is: whether this board holds it is that read's answer, never a listing's.
5642            let far = if same_source {
5643                Some(
5644                    self.item_by_id(&NativeId(far_id.to_owned()))
5645                        .await?
5646                        .ok_or_else(|| SourceError::Refused {
5647                            message: format!("GitHub dependency item {far_id} was not found"),
5648                        })?,
5649                )
5650            } else {
5651                None
5652            };
5653            let far = far.as_ref();
5654            // The caller says which kind the far end is, and this board holds the far end
5655            // itself, so a disagreement is settled here rather than stored: recorded, the
5656            // wrong kind would read back as a cross-level edge that never existed; written
5657            // natively, it would name a relationship of a different level than the caller
5658            // asked for.
5659            //
5660            // A far end this board holds as a *document* fails the same comparison and is
5661            // refused by the same sentence: `ItemKind` has no document variant because
5662            // nothing may point at one, so no caller can name it correctly and the refusal
5663            // is the only honest answer.
5664            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
5665                return Err(SourceError::Refused {
5666                    message: format!(
5667                        "GitHub dependency item {far_id} is a {} of this board, and this item \
5668                         names it as a {}; record the kind it is",
5669                        disagreeing.kind.describes(),
5670                        edge.to.kind.marker()
5671                    ),
5672                });
5673            }
5674            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
5675            // however the far end is spelled — and one classified native here would be
5676            // written nowhere at all, because a draft's native reconciliation never runs.
5677            let native_here = near_content == ContentKind::Issue
5678                && far.is_some_and(|far| {
5679                    far.content_kind == ContentKind::Issue
5680                        && BoardKind::Work(edge.to.kind) == near_kind
5681                });
5682            if native_here {
5683                native.push(far_id.to_owned());
5684            } else {
5685                fallback.push(edge.clone());
5686            }
5687        }
5688        Ok((native, fallback))
5689    }
5690
5691    async fn update_existing(
5692        &self,
5693        item: &Resolved,
5694        incoming: &Incoming<'_>,
5695        body: &Option<String>,
5696        status_target: Option<&StatusTarget>,
5697    ) -> Result<(), SourceError> {
5698        let title = incoming.written_title();
5699        let mut fields = match item.content_kind {
5700            ContentKind::DraftIssue => json!({"title":title,"body":body}),
5701            ContentKind::Issue => json!({"title":title,"body":body,
5702                                         "stateInput":state_input(status_target)}),
5703        };
5704        if matches!(status_target, Some(StatusTarget::Terminal(_, _))) {
5705            fields
5706                .as_object_mut()
5707                .expect("update fields are an object")
5708                .remove("stateInput");
5709        }
5710        self.update_content(item.content_kind, &item.id, fields)
5711            .await
5712    }
5713
5714    /// Update one board item's content with exactly `fields` beside its id, through the
5715    /// mutation its kind takes: `updateIssue` for an issue, `updateProjectV2DraftIssue` for
5716    /// a draft.
5717    ///
5718    /// Every input field either mutation leaves out is a field GitHub leaves as it is, which
5719    /// is what lets a narrow write carry the one thing it changes and nothing else.
5720    async fn update_content(
5721        &self,
5722        kind: ContentKind,
5723        id: &NativeId,
5724        fields: Value,
5725    ) -> Result<(), SourceError> {
5726        let (operation, id_key, pointer) = match kind {
5727            ContentKind::DraftIssue => (
5728                graphql::UPDATE_DRAFT,
5729                "draftIssueId",
5730                "/updateProjectV2DraftIssue/draftIssue",
5731            ),
5732            ContentKind::Issue => (graphql::UPDATE_ISSUE, "id", "/updateIssue/issue"),
5733        };
5734        let mut input = fields;
5735        input[id_key] = json!(id.0);
5736        let data = self.graphql(operation, json!({"input":input})).await?;
5737        let returned = data
5738            .pointer(pointer)
5739            .ok_or_else(|| SourceError::Malformed {
5740                message: "GitHub item update returned no item".into(),
5741            })?;
5742        if required_str(returned, "id")? != id.0 {
5743            return Err(SourceError::Malformed {
5744                message: "GitHub item update returned the wrong item".into(),
5745            });
5746        }
5747        Ok(())
5748    }
5749
5750    /// Creates one issue, files it on the board, and reports what a read of it would say:
5751    /// its content id, its board item id, and the web address GitHub gave it.
5752    ///
5753    /// Two calls rather than one: `createIssue` needs a repository and answers with an
5754    /// issue that is on no board, and `addProjectV2ItemById` is what puts it there. A
5755    /// terminal status is not written here: `finish_write` selects its option first and
5756    /// closes the issue after, so a close never lands on an item whose board cannot show it.
5757    ///
5758    /// The address and the number come back here because this is the only place either is
5759    /// known before GitHub's own board read catches up — an item this run created answers
5760    /// the reads that follow it out of the record below, and one remembered without them
5761    /// would report no location and no key for the rest of the run.
5762    async fn create_and_file_issue(
5763        &self,
5764        board_id: &str,
5765        repository: &RepositoryTarget,
5766        incoming: &Incoming<'_>,
5767        body: &Option<String>,
5768    ) -> Result<Landed, SourceError> {
5769        let repository_id = self.repository_id(repository, incoming).await?;
5770        let data = self
5771            .graphql(
5772                graphql::CREATE_ISSUE,
5773                json!({"input":{
5774                    "repositoryId":repository_id,"title":incoming.written_title(),"body":body
5775                }}),
5776            )
5777            .await?;
5778        let created = data
5779            .pointer("/createIssue/issue")
5780            .filter(|value| !value.is_null())
5781            .ok_or_else(|| SourceError::Malformed {
5782                message: "GitHub issue creation returned no issue".into(),
5783            })?;
5784        let content_id = NativeId(required_str(created, "id")?.to_owned());
5785        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
5786        // a response without it is not worth failing a landed write over — the item simply
5787        // reports no location until the board read catches up, which is what it did before.
5788        let url = optional_str(created, "url")?.map(str::to_owned);
5789        // The issue exists from here on, so an unreadable number and a refused board
5790        // filing below each try, best effort, to take it back: an issue in the repository
5791        // that is on no board is an item nobody asked for and nothing here would find again.
5792        //
5793        // Its number is optional on the same terms its address is — a landed write is not
5794        // worth failing over a member that came back missing, and such an item reports no
5795        // handle until a board read catches up. A number that is *present* and is not an
5796        // unsigned integer is still a response this source cannot read.
5797        let number = match created_issue_number(created) {
5798            Ok(number) => number,
5799            Err(error) => {
5800                let _ = self.delete_issue(&content_id).await;
5801                return Err(error);
5802            }
5803        };
5804        let added = match self
5805            .graphql(
5806                graphql::ADD_TO_BOARD,
5807                json!({"input":{"projectId":board_id,"contentId":content_id.0}}),
5808            )
5809            .await
5810        {
5811            Ok(added) => added,
5812            Err(error) => {
5813                let _ = self.delete_issue(&content_id).await;
5814                return Err(error);
5815            }
5816        };
5817        let item = added
5818            .pointer("/addProjectV2ItemById/item")
5819            .filter(|value| !value.is_null())
5820            .ok_or_else(|| SourceError::Malformed {
5821                message: "GitHub board addition returned no project item".into(),
5822            })?;
5823        Ok(Landed {
5824            content_id,
5825            item_id: required_str(item, "id")?.to_owned(),
5826            url,
5827            number,
5828        })
5829    }
5830
5831    /// Move one issue under the project it now belongs to, or out of the one it left.
5832    async fn reparent(
5833        &self,
5834        held: Option<NativeId>,
5835        child: &NativeId,
5836        wanted: Option<&NativeId>,
5837    ) -> Result<(), SourceError> {
5838        if held.as_ref() == wanted {
5839            return Ok(());
5840        }
5841        if let Some(held) = &held {
5842            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
5843                .await?;
5844        }
5845        if let Some(wanted) = wanted {
5846            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
5847                .await?;
5848        }
5849        Ok(())
5850    }
5851
5852    async fn sub_issue(
5853        &self,
5854        operation: &str,
5855        parent: &NativeId,
5856        child: &NativeId,
5857        root: &str,
5858    ) -> Result<(), SourceError> {
5859        let data = self
5860            .graphql(
5861                operation,
5862                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
5863            )
5864            .await?;
5865        let issue =
5866            data.pointer(&format!("/{root}/issue"))
5867                .ok_or_else(|| SourceError::Malformed {
5868                    message: "GitHub sub-issue update returned no issue".into(),
5869                })?;
5870        let sub =
5871            data.pointer(&format!("/{root}/subIssue"))
5872                .ok_or_else(|| SourceError::Malformed {
5873                    message: "GitHub sub-issue update returned no sub-issue".into(),
5874                })?;
5875        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
5876            return Err(SourceError::Malformed {
5877                message: "GitHub sub-issue update returned the wrong issues".into(),
5878            });
5879        }
5880        Ok(())
5881    }
5882
5883    async fn reconcile_blocked_by(
5884        &self,
5885        content_id: &NativeId,
5886        native: &[String],
5887    ) -> Result<(), SourceError> {
5888        let current = self.native_dependency_ids(content_id).await?;
5889        for (operation, far_id) in current
5890            .iter()
5891            .filter(|id| !native.contains(id))
5892            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
5893            .chain(
5894                native
5895                    .iter()
5896                    .filter(|id| !current.contains(id))
5897                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
5898            )
5899        {
5900            let data = self
5901                .graphql(
5902                    operation,
5903                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
5904                )
5905                .await?;
5906            let root = if operation == graphql::ADD_BLOCKED_BY {
5907                "addBlockedBy"
5908            } else {
5909                "removeBlockedBy"
5910            };
5911            let issue =
5912                data.pointer(&format!("/{root}/issue"))
5913                    .ok_or_else(|| SourceError::Malformed {
5914                        message: "GitHub dependency update returned no issue".into(),
5915                    })?;
5916            let blocker = data
5917                .pointer(&format!("/{root}/blockingIssue"))
5918                .ok_or_else(|| SourceError::Malformed {
5919                    message: "GitHub dependency update returned no blocking issue".into(),
5920                })?;
5921            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
5922            {
5923                return Err(SourceError::Malformed {
5924                    message: "GitHub dependency update returned the wrong issues".into(),
5925                });
5926            }
5927        }
5928        Ok(())
5929    }
5930}
5931
5932/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
5933enum Reached {
5934    /// An issue this board holds, resolved into everything this source reports about it.
5935    Held(Box<Resolved>),
5936    /// Nothing this board holds: no such node, or a node on some other board.
5937    Nothing,
5938    /// A board draft, which [`graphql::ISSUE`] reaches and reads nothing of, so it is read
5939    /// again by [`GitHubProjectsSource::draft_by_id`].
5940    Draft,
5941}
5942
5943/// What GitHub says when a string is not a node id it can resolve.
5944///
5945/// Matched because it is the ordinary answer to a project selector naming a project by its
5946/// *name*, and reporting that as a failure would make naming one impossible. It is read
5947/// off the refusal GitHub sent, never guessed from the shape of the string: this source
5948/// does not define the syntax of a GitHub node id and would be wrong about it.
5949const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
5950
5951/// Whether this refusal is GitHub saying the id names no node at all.
5952fn unresolvable_node(error: &SourceError) -> bool {
5953    matches!(error, SourceError::Refused { message }
5954        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
5955}
5956
5957/// One project name, as a search qualifier which filters on it at the server.
5958///
5959/// Quoted so the whole title is one phrase rather than a bag of words, with the two
5960/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
5961/// the way it documents. A title matched here is still compared for equality afterwards:
5962/// the qualifier narrows what the server sends, and this source decides what it names.
5963fn title_qualifier(name: &str) -> String {
5964    let escaped = name.replace('\\', "\\\\").replace('"', "\\\"");
5965    format!("in:title \"{escaped}\"")
5966}
5967
5968/// The board, and every item on it this source reports.
5969#[derive(Clone)]
5970struct Board {
5971    id: String,
5972    fields: Value,
5973    items: Vec<Resolved>,
5974}
5975
5976/// What a write needs of the board and nothing more: its node id and its field
5977/// definitions, in the shape a read of the board's own `fields` gives them.
5978///
5979/// Deliberately no items. A write decides which item it writes, which parent it files
5980/// under and which far ends it names by reading each of them by its own id; this is the
5981/// half of the board those reads cannot carry, and holding no item is what keeps it from
5982/// ever being asked whether an item is there.
5983#[derive(Clone)]
5984struct BoardFields {
5985    id: BoardId,
5986    fields: Value,
5987}
5988
5989/// A board's node id: what a field write and `addProjectV2ItemById` address.
5990///
5991/// Never blank, because a blank one addresses no board — so an id GitHub answers blank is
5992/// refused where it is read, and one an item names blank is read as not named at all.
5993#[derive(Clone)]
5994struct BoardId(String);
5995
5996/// Where one write left its item, for the record the rest of the command reads it out of.
5997///
5998/// A named record rather than a tuple because the update arm and the create arm each fill
5999/// all four, and two `Option`s of different meaning side by side in a tuple are two
6000/// positions a reader has to count.
6001struct Landed {
6002    /// The issue's own node id, which is the [`NativeId`] this source reports.
6003    content_id: NativeId,
6004    /// The board item's id, which is what a field write addresses.
6005    // 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.
6006    item_id: String,
6007    /// The web address GitHub gave the issue, when it gave one.
6008    // 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.
6009    url: Option<String>,
6010    /// The issue's number on its repository, when GitHub reported one.
6011    number: Option<u64>,
6012}
6013
6014impl BoardId {
6015    fn parse(id: &str) -> Result<Self, SourceError> {
6016        if id.trim().is_empty() {
6017            return Err(SourceError::Malformed {
6018                message: "GitHub named a board with a blank node id".into(),
6019            });
6020        }
6021        Ok(Self(id.to_owned()))
6022    }
6023
6024    fn as_str(&self) -> &str {
6025        &self.0
6026    }
6027}
6028
6029impl Board {
6030    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
6031        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
6032        let nodes = fields
6033            .get("nodes")
6034            .and_then(Value::as_array)
6035            .ok_or_else(|| SourceError::Malformed {
6036                message: "GitHub project fields.nodes is not an array".into(),
6037            })?;
6038        Ok(nodes
6039            .iter()
6040            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
6041    }
6042}
6043
6044/// One board item, resolved into everything this source reports about it.
6045#[derive(Clone)]
6046struct Resolved {
6047    item_id: String,
6048    id: NativeId,
6049    content_kind: ContentKind,
6050    kind: BoardKind,
6051    title: String,
6052    body: Option<String>,
6053    /// The body exactly as GitHub holds it, metadata slot and all, which is what a write
6054    /// that changes the slot alone has to keep byte for byte outside it.
6055    raw_body: Option<String>,
6056    status: Status,
6057    /// The name of the board `Status` option this item sits in, as the board spells it.
6058    option: Option<String>,
6059    /// What its `Priority` field says, read through this instance's mapping.
6060    priority: HeldPriority,
6061    /// Whether this item's issue is closed. A draft has no such state and is never closed.
6062    closed: bool,
6063    /// The tasks this one delivers, read out of its slot. Empty for anything not a task.
6064    delivers: Vec<TaskRef>,
6065    /// Every task that delivers this one, read out of its slot. Empty for anything not a
6066    /// task.
6067    delivered_by: Vec<TaskRef>,
6068    labels: Vec<Label>,
6069    parent: Option<NativeId>,
6070    // 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.
6071    origin: Option<String>,
6072    /// The issue's own number on its repository, as GitHub reports it.
6073    ///
6074    /// `None` in exactly two cases: a draft, which has no number at all — `DraftIssue`
6075    /// declares none, and a draft is not filed in a repository to be numbered by one — and
6076    /// an issue this run created whose creating mutation answered without one, which is a
6077    /// response GitHub's own schema says cannot happen and which a landed write is not
6078    /// worth failing over. An `Issue` read off the board always has one.
6079    number: Option<u64>,
6080    url: Option<String>,
6081    created_at: Option<DateTime<Utc>>,
6082    updated_at: Option<DateTime<Utc>>,
6083    own_repository: Option<Repository>,
6084    repositories: Vec<Repository>,
6085    slot: BTreeMap<String, Value>,
6086    /// The node id of the board this item sits on, when the read that reached it said.
6087    board_id: Option<String>,
6088    /// The definition of every board field this item holds a value of, in the shape a read
6089    /// of the board's own `fields` gives one.
6090    ///
6091    /// Only the fields this item has a value in: a field it holds nothing of is not here,
6092    /// which says nothing about whether the board has it.
6093    fields: Vec<Value>,
6094}
6095
6096impl Resolved {
6097    /// The board this item's own read names it on, when that read named one this source can
6098    /// address.
6099    fn named_board(&self) -> Option<BoardId> {
6100        self.board_id
6101            .as_deref()
6102            .and_then(|id| BoardId::parse(id).ok())
6103    }
6104
6105    /// Whether this item holds a value of the board field called `name`, and so carries
6106    /// that field's definition. `false` says nothing about whether the board has the field.
6107    fn defines(&self, name: &str) -> bool {
6108        self.fields
6109            .iter()
6110            .any(|field| field.get("name").and_then(Value::as_str) == Some(name))
6111    }
6112
6113    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
6114    /// in a field of its own, and none of the five keys that are only an encoding.
6115    ///
6116    /// The two delivery keys are left out for every kind, not only for a task: they are
6117    /// the encoding of [`Task::delivers`] and [`Task::delivered_by`], and a project or a
6118    /// document carrying one holds nothing a caller's own metadata could mean by it.
6119    fn metadata(&self) -> BTreeMap<String, Value> {
6120        let mut metadata = self.slot.clone();
6121        metadata.remove(Repository::METADATA_KEY);
6122        metadata.remove(DependencyEdge::RECORDED_KEY);
6123        metadata.remove(ItemKind::METADATA_KEY);
6124        metadata.remove(TaskRef::DELIVERS_KEY);
6125        metadata.remove(TaskRef::DELIVERED_BY_KEY);
6126        if let Some(origin) = &self.origin {
6127            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
6128        }
6129        metadata
6130    }
6131
6132    /// Where this item is, as a link a reader can open.
6133    ///
6134    /// A board is a hosted place and every issue on it has a web address, so that address
6135    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
6136    /// of place it is, so a reader knows to open it rather than to read a file out. It
6137    /// does not replace or derive from `url`: the field goes on reporting exactly what it
6138    /// reported before, and this says what that address *is*.
6139    ///
6140    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
6141    /// rather than a third variant, which is the contract's "the source did not say". An
6142    /// issue this run created is not one of those: its address comes back from the
6143    /// creating mutation, so it is somewhere a reader can open from the moment it exists
6144    /// rather than from whenever the board read catches up.
6145    fn location(&self) -> Option<Location> {
6146        self.url.clone().map(Location::Url)
6147    }
6148
6149    /// The short handle this board's backend shows people for a task: the issue's number
6150    /// alone, as a decimal string.
6151    ///
6152    /// The number alone rather than `owner/repo#1043`, because that is the contract's
6153    /// value for this backend. A draft has no number and so no handle, which is the
6154    /// contract's *absent* rather than a handle of some other shape — and the native
6155    /// [`Task::id`] here is the issue's GraphQL node id, which this neither replaces nor
6156    /// derives from.
6157    fn key(&self) -> Option<String> {
6158        self.number.map(|number| number.to_string())
6159    }
6160
6161    /// Whether its `Priority` field holds a value at all, mapped or not.
6162    fn holds_priority(&self) -> bool {
6163        self.priority != HeldPriority::Read(Priority::None)
6164    }
6165
6166    /// The task this item is.
6167    ///
6168    /// Fails for an item whose `Priority` field holds an option the mapping does not name:
6169    /// reading that as a level would be a guess, and reading it as `none` would let the next
6170    /// copy clear a priority a person set.
6171    fn task(&self) -> Result<Task, SourceError> {
6172        let priority = match &self.priority {
6173            HeldPriority::Read(priority) => *priority,
6174            HeldPriority::Unmapped(option) => {
6175                return Err(SourceError::Malformed {
6176                    message: format!(
6177                        "task {}{} sits in the board {PRIORITY_FIELD} option {option:?}, which \
6178                         this source's priority_mapping does not name, so its priority cannot be \
6179                         read; next: name {option:?} under priority_mapping, or move the item to \
6180                         a mapped option",
6181                        self.id,
6182                        self.number
6183                            .map(|number| format!(" (#{number})"))
6184                            .unwrap_or_default()
6185                    ),
6186                });
6187            }
6188        };
6189        Ok(Task {
6190            id: self.id.clone(),
6191            key: self.key(),
6192            title: self.title.clone(),
6193            content: self.body.clone(),
6194            status: self.status.clone(),
6195            priority,
6196            labels: self.labels.clone(),
6197            project: self.parent.clone(),
6198            url: self.url.clone(),
6199            location: self.location(),
6200            created_at: self.created_at,
6201            updated_at: self.updated_at,
6202            metadata: self.metadata(),
6203            repositories: self.repositories.clone(),
6204            delivers: self.delivers.clone(),
6205            delivered_by: self.delivered_by.clone(),
6206        })
6207    }
6208
6209    fn project(&self) -> Project {
6210        Project {
6211            id: self.id.clone(),
6212            title: self.title.clone(),
6213            content: self.body.clone(),
6214            status: self.status.clone(),
6215            labels: self.labels.clone(),
6216            url: self.url.clone(),
6217            location: self.location(),
6218            created_at: self.created_at,
6219            updated_at: self.updated_at,
6220            metadata: self.metadata(),
6221            repositories: self.repositories.clone(),
6222        }
6223    }
6224
6225    /// The same issue as a document: the project it is filed under, and no status and no
6226    /// dependencies, because a document is not work.
6227    fn document(&self) -> Document {
6228        Document {
6229            id: self.id.clone(),
6230            title: self.title.clone(),
6231            content: self.body.clone(),
6232            project: self.parent.clone(),
6233            labels: self.labels.clone(),
6234            url: self.url.clone(),
6235            location: self.location(),
6236            created_at: self.created_at,
6237            updated_at: self.updated_at,
6238            metadata: self.metadata(),
6239            repositories: self.repositories.clone(),
6240        }
6241    }
6242}
6243
6244/// What one write is, and the status that comes with being it.
6245///
6246/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
6247/// status and a task or a project always has one, so "a document carrying a status" and
6248/// "a task carrying none" are states a write cannot be in rather than states every use
6249/// site below has to defend against.
6250enum Written<'a> {
6251    /// A document, which is not work and so has no status at all.
6252    Document,
6253    /// A task or a project, and the status it is being written with.
6254    Work(ItemKind, &'a Status),
6255}
6256
6257impl Written<'_> {
6258    /// Which of the board's three kinds this write is.
6259    const fn kind(&self) -> BoardKind {
6260        match self {
6261            Self::Document => BoardKind::Document,
6262            Self::Work(kind, _) => BoardKind::Work(*kind),
6263        }
6264    }
6265
6266    /// The status this write carries. A document carries none, so a write of one says
6267    /// nothing about the issue's open or closed state and selects no board `Status`
6268    /// option.
6269    const fn status(&self) -> Option<&Status> {
6270        match self {
6271            Self::Document => None,
6272            Self::Work(_, status) => Some(status),
6273        }
6274    }
6275}
6276
6277/// The item being written, in the one shape all three write methods reach.
6278struct Incoming<'a> {
6279    written: Written<'a>,
6280    /// The title a person wrote. A document's goes onto the issue with
6281    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
6282    title: &'a str,
6283    content: Option<&'a str>,
6284    labels: &'a [Label],
6285    metadata: &'a BTreeMap<String, Value>,
6286    repositories: &'a [Repository],
6287    parent: Option<&'a NativeId>,
6288    /// [`Task::delivers`], already checked. Empty for a project or a document, which is
6289    /// what keeps either key out of their slot.
6290    delivers: &'a [TaskRef],
6291    /// [`Task::delivered_by`], already checked. Empty for a project or a document.
6292    delivered_by: &'a [TaskRef],
6293    /// [`Task::priority`], for a task written to an instance that holds one; `None` for a
6294    /// project, a document, and every write to an instance with no `priority_mapping` —
6295    /// which is what keeps such a write's requests exactly what they were before.
6296    priority: Option<Priority>,
6297}
6298
6299/// What one write does to an item's `Priority` field.
6300enum PriorityWrite {
6301    /// Select this option of this field.
6302    Select {
6303        /// The `Priority` field's id.
6304        field: String,
6305        /// The mapped option's id.
6306        option: String,
6307    },
6308    /// Clear the field's value, which is what `none` is.
6309    Clear {
6310        /// The `Priority` field's id.
6311        field: String,
6312    },
6313}
6314
6315impl Incoming<'_> {
6316    /// The title this write puts on the issue.
6317    fn written_title(&self) -> String {
6318        match self.written {
6319            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
6320            Written::Work(..) => self.title.to_owned(),
6321        }
6322    }
6323}
6324
6325#[derive(Clone, Copy, PartialEq, Eq)]
6326enum ContentKind {
6327    DraftIssue,
6328    Issue,
6329}
6330
6331/// What one board issue is: a document, or the work an [`ItemKind`] names.
6332///
6333/// A type of this source's own rather than an `ItemKind` with a third variant, because
6334/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
6335/// document — the contract keeps a document out of that enum deliberately. Holding the
6336/// board's three answers in one value is what makes every place that asks "which is this?"
6337/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
6338/// two thirds of the board.
6339#[derive(Clone, Copy, PartialEq, Eq)]
6340enum BoardKind {
6341    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
6342    Document,
6343    /// Every other issue, and every draft.
6344    Work(ItemKind),
6345}
6346
6347impl BoardKind {
6348    /// How a refusal names this kind to the person reading it.
6349    const fn describes(self) -> &'static str {
6350        match self {
6351            Self::Document => "document",
6352            Self::Work(kind) => kind.marker(),
6353        }
6354    }
6355}
6356
6357/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
6358///
6359/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
6360/// the shared cross-source journeys assert one answer to one question, so two sources
6361/// that disagree about what "carries the label bug" means fail them.
6362fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
6363    let holds = |name: &String| {
6364        labels
6365            .iter()
6366            .any(|label| label.name.eq_ignore_ascii_case(name))
6367    };
6368    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
6369        && filter.all_of.iter().all(holds)
6370        && !filter.none_of.iter().any(holds)
6371}
6372
6373/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
6374/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
6375fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
6376    statuses.is_empty() || statuses.contains(&category)
6377}
6378
6379/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
6380///
6381/// `content` is the item's own prose — the body with this source's trailing metadata
6382/// comment already taken off — so a search never matches an encoding the author of the
6383/// issue never wrote.
6384fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
6385    let terms = query.terms.to_lowercase();
6386    let in_title = title.to_lowercase().contains(&terms);
6387    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
6388    match query.fields {
6389        TextFields::Title => in_title,
6390        TextFields::Content => in_content,
6391        TextFields::TitleOrContent => in_title || in_content,
6392    }
6393}
6394
6395/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
6396///
6397/// The project predicate is passed separately because a read narrowed to one project has
6398/// already answered it by asking *that project* for its own items — and re-applying it
6399/// there would compare the caller's selector, which may be a project's **name**, against
6400/// the id of the project that name resolved to, and keep nothing. Every other read passes
6401/// `query.project` and applies it here, which is what keeps `projects` a predicate this
6402/// source really does apply.
6403fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
6404    labels_match(&task.labels, &query.labels)
6405        && status_matches(task.status.category, &query.statuses)
6406        && (query.priorities.is_empty() || query.priorities.contains(&task.priority))
6407        && match project {
6408            ProjectFilter::Any => true,
6409            ProjectFilter::Orphans => task.project.is_none(),
6410            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
6411        }
6412        && query
6413            .text
6414            .as_ref()
6415            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
6416}
6417
6418fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
6419    labels_match(&project.labels, &query.labels)
6420        && status_matches(project.status.category, &query.statuses)
6421        && query
6422            .text
6423            .as_ref()
6424            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
6425}
6426
6427/// The same three predicates a task query carries, minus the status filter.
6428///
6429/// A document is not work, so it has no status for one to compare against and the query
6430/// type carries none. The project predicate is the same one — a design issue filed under a
6431/// project issue is in that project, and one filed under nothing is in none — so it is
6432/// spelled the same way here rather than answered differently.
6433fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
6434    labels_match(&document.labels, &query.labels)
6435        && match project {
6436            ProjectFilter::Any => true,
6437            ProjectFilter::Orphans => document.project.is_none(),
6438            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
6439        }
6440        && query
6441            .text
6442            .as_ref()
6443            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
6444}
6445
6446#[async_trait::async_trait]
6447impl TaskSource for GitHubProjectsSource {
6448    fn kind(&self) -> &'static str {
6449        KIND
6450    }
6451    fn capabilities(&self) -> Capabilities {
6452        Capabilities {
6453            projects: Support::Native,
6454            documents: Support::Native,
6455            comments: Support::Native,
6456            priority: if self.priorities.is_some() {
6457                Support::Native
6458            } else {
6459                Support::Unsupported
6460            },
6461            filter_by_priority: Support::Native,
6462            orphan_tasks: Support::Native,
6463            filter_by_label: Support::Native,
6464            filter_by_status: Support::Native,
6465            search_title: Support::Native,
6466            search_content: Support::Native,
6467            task_dependencies: DependencySupport::BothDirections,
6468            project_dependencies: DependencySupport::BothDirections,
6469            max_page_size: MAX_PAGE_SIZE,
6470        }
6471    }
6472    async fn health(&self) -> Result<Health, SourceError> {
6473        let board = self.board_page(None, 1).await?;
6474        Ok(Health {
6475            reachable: true,
6476            detail: Some(format!(
6477                "reading GitHub project {}/{} ({})",
6478                self.owner,
6479                self.project_number,
6480                required_str(&board, "title")?
6481            )),
6482        })
6483    }
6484    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
6485        self.item_by_id(id)
6486            .await?
6487            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6488            .map(|item| item.task())
6489            .transpose()
6490    }
6491    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
6492        Ok(self
6493            .item_by_id(id)
6494            .await?
6495            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
6496            .map(|item| item.project()))
6497    }
6498    async fn query_tasks(
6499        &self,
6500        query: &TaskQuery,
6501        page: &PageRequest,
6502    ) -> Result<Page<Task>, SourceError> {
6503        validate_page(page)?;
6504        // A read narrowed to one project asks that project for its own tasks, so nothing
6505        // about it costs what the rest of the board holds. Every other task read is a
6506        // question about the whole board and is answered by reading it.
6507        let (held, membership) = match &query.project {
6508            ProjectFilter::Is(project) => (
6509                self.project_children(project).await?,
6510                // Answered by where these items came from; see `task_matches`.
6511                &ProjectFilter::Any,
6512            ),
6513            ProjectFilter::Any | ProjectFilter::Orphans => {
6514                (self.board().await?.items, &query.project)
6515            }
6516        };
6517        // Filtered before paged: a page of a filtered result is a page of the survivors,
6518        // never the survivors of a page.
6519        let mut tasks = Vec::new();
6520        for item in held
6521            .iter()
6522            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
6523        {
6524            let task = item.task()?;
6525            if task_matches(&task, query, membership) {
6526                tasks.push(task);
6527            }
6528        }
6529        Ok(offset_page(
6530            tasks,
6531            numeric_cursor(page.cursor.as_ref())?,
6532            page.limit.min(MAX_PAGE_SIZE) as usize,
6533        ))
6534    }
6535    async fn query_projects(
6536        &self,
6537        query: &ProjectQuery,
6538        page: &PageRequest,
6539    ) -> Result<Page<Project>, SourceError> {
6540        validate_page(page)?;
6541        // The projects a board holds are found by an issue search scoped to that board,
6542        // never by walking the board's own item connection: what tells a project from a
6543        // task is the `parent` each issue carries, which costs nothing to read.
6544        let projects = self
6545            .board_issues()
6546            .await?
6547            .iter()
6548            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
6549            .map(Resolved::project)
6550            .filter(|project| project_matches(project, query))
6551            .collect();
6552        Ok(offset_page(
6553            projects,
6554            numeric_cursor(page.cursor.as_ref())?,
6555            page.limit.min(MAX_PAGE_SIZE) as usize,
6556        ))
6557    }
6558    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
6559        Ok(self
6560            .item_by_id(id)
6561            .await?
6562            .filter(|item| item.kind == BoardKind::Document)
6563            .map(|item| item.document()))
6564    }
6565    async fn query_documents(
6566        &self,
6567        query: &DocumentQuery,
6568        page: &PageRequest,
6569    ) -> Result<Page<Document>, SourceError> {
6570        validate_page(page)?;
6571        // Narrowed to one project, this is the same sub-issue read a task list scoped to
6572        // that project makes — a document filed under a project is a sub-issue of it too,
6573        // and which of them come back is the kind this caller asked for.
6574        let (held, membership) = match &query.project {
6575            ProjectFilter::Is(project) => (
6576                self.project_children(project).await?,
6577                // Answered by where these items came from; see `task_matches`.
6578                &ProjectFilter::Any,
6579            ),
6580            ProjectFilter::Any | ProjectFilter::Orphans => {
6581                (self.board().await?.items, &query.project)
6582            }
6583        };
6584        // Filtered before paged, exactly as a task read is: a page of a filtered result is
6585        // a page of the survivors, never the survivors of a page.
6586        let documents = held
6587            .iter()
6588            .filter(|item| item.kind == BoardKind::Document)
6589            .map(Resolved::document)
6590            .filter(|document| document_matches(document, query, membership))
6591            .collect();
6592        Ok(offset_page(
6593            documents,
6594            numeric_cursor(page.cursor.as_ref())?,
6595            page.limit.min(MAX_PAGE_SIZE) as usize,
6596        ))
6597    }
6598    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
6599        validate_page(page)?;
6600        let offset = numeric_cursor(page.cursor.as_ref())?;
6601        let mut labels = self
6602            .board()
6603            .await?
6604            .items
6605            .into_iter()
6606            .flat_map(|item| item.labels)
6607            .fold(Vec::new(), |mut all, label| {
6608                if !all.iter().any(|x: &Label| x.id == label.id) {
6609                    all.push(label);
6610                }
6611                all
6612            });
6613        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
6614        Ok(offset_page(
6615            labels,
6616            offset,
6617            page.limit.min(MAX_PAGE_SIZE) as usize,
6618        ))
6619    }
6620    async fn task_dependencies(
6621        &self,
6622        id: &NativeId,
6623        direction: Direction,
6624        page: &PageRequest,
6625    ) -> Result<Page<DependencyEdge>, SourceError> {
6626        self.dependencies(id, ItemKind::Task, direction, page).await
6627    }
6628    async fn project_dependencies(
6629        &self,
6630        id: &NativeId,
6631        direction: Direction,
6632        page: &PageRequest,
6633    ) -> Result<Page<DependencyEdge>, SourceError> {
6634        self.dependencies(id, ItemKind::Project, direction, page)
6635            .await
6636    }
6637
6638    fn writes(&self) -> WriteSupport {
6639        WriteSupport::Supported
6640    }
6641
6642    /// Create or update one task.
6643    ///
6644    /// Its `delivers` and `delivered_by` are checked before anything is read or written —
6645    /// neither may name the task itself or name one task twice — and land in the body's
6646    /// metadata slot under their reserved keys, in place of any caller metadata of those
6647    /// names.
6648    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
6649        let near = write.target.as_ref().unwrap_or(&write.item.id);
6650        for (key, entries) in [
6651            (TaskRef::DELIVERS_KEY, &write.item.delivers),
6652            (TaskRef::DELIVERED_BY_KEY, &write.item.delivered_by),
6653        ] {
6654            TaskRef::listed(key, near, Some(&self.name), entries.clone())
6655                .map_err(|message| SourceError::Refused { message })?;
6656        }
6657        if self.priorities.is_none() && write.item.priority != Priority::None {
6658            return Err(self.holds_no_priority());
6659        }
6660        self.write_item(
6661            &Incoming {
6662                written: Written::Work(ItemKind::Task, &write.item.status),
6663                title: &write.item.title,
6664                content: write.item.content.as_deref(),
6665                labels: &write.item.labels,
6666                metadata: &write.item.metadata,
6667                repositories: &write.item.repositories,
6668                parent: write.item.project.as_ref(),
6669                delivers: &write.item.delivers,
6670                delivered_by: &write.item.delivered_by,
6671                priority: self.priorities.as_ref().map(|_| write.item.priority),
6672            },
6673            write.target.as_ref(),
6674            &write.depends_on,
6675        )
6676        .await
6677    }
6678
6679    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
6680        self.write_item(
6681            &Incoming {
6682                written: Written::Work(ItemKind::Project, &write.item.status),
6683                title: &write.item.title,
6684                content: write.item.content.as_deref(),
6685                labels: &write.item.labels,
6686                metadata: &write.item.metadata,
6687                repositories: &write.item.repositories,
6688                parent: None,
6689                delivers: &[],
6690                delivered_by: &[],
6691                priority: None,
6692            },
6693            write.target.as_ref(),
6694            &write.depends_on,
6695        )
6696        .await
6697    }
6698
6699    /// Create or update one document, which is one issue titled the way this board spells
6700    /// a document.
6701    ///
6702    /// Everything else is exactly a task write: caller metadata goes to the same canonical
6703    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
6704    /// or a field this board cannot carry is refused by name rather than dropped, a target
6705    /// naming an issue this board does not hold is refused rather than created, and an
6706    /// issue this call created is taken back when the rest of the write fails.
6707    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
6708        // A document takes part in no dependency graph, so there is no far end to write
6709        // natively and none to record: a caller naming one is told so rather than having it
6710        // stored under the reserved key, where a later read would report an edge the
6711        // contract says cannot exist.
6712        if !write.depends_on.is_empty() {
6713            return Err(SourceError::Refused {
6714                message: format!(
6715                    "this write names {} dependencies for a document, and a document takes \
6716                     part in no dependency graph; next: put the dependency on the task or \
6717                     project the document is about",
6718                    write.depends_on.len()
6719                ),
6720            });
6721        }
6722        self.write_item(
6723            &Incoming {
6724                written: Written::Document,
6725                title: &write.item.title,
6726                content: write.item.content.as_deref(),
6727                labels: &write.item.labels,
6728                metadata: &write.item.metadata,
6729                repositories: &write.item.repositories,
6730                parent: write.item.project.as_ref(),
6731                delivers: &[],
6732                delivered_by: &[],
6733                priority: None,
6734            },
6735            write.target.as_ref(),
6736            &[],
6737        )
6738        .await
6739    }
6740
6741    /// Set one task's status alone.
6742    ///
6743    /// An open target reopens a closed issue with an `updateIssue` carrying only its
6744    /// `stateInput`, then selects the board option with `updateProjectV2ItemFieldValue`; a
6745    /// terminal target selects its mapped option, then closes with its fixed reason. No
6746    /// request carries a title, a body or a label. The status
6747    /// answered is what [`StatusMapping::status`] reads off the state just written, which is
6748    /// what a re-read reports.
6749    async fn set_task_status(
6750        &self,
6751        id: &NativeId,
6752        category: StatusCategory,
6753    ) -> Result<Option<Status>, SourceError> {
6754        self.set_status(id, category).await
6755    }
6756
6757    /// Set one task's priority alone: one `updateProjectV2ItemFieldValue` selecting the
6758    /// mapped option of the board's `Priority` field, or one `clearProjectV2ItemFieldValue`
6759    /// for `none`. Refused by an instance with no `priority_mapping`.
6760    async fn set_task_priority(
6761        &self,
6762        id: &NativeId,
6763        priority: Priority,
6764    ) -> Result<Option<Priority>, SourceError> {
6765        self.set_priority(id, priority).await
6766    }
6767
6768    /// Replace one task's content with a single body update that keeps the metadata slot
6769    /// byte for byte.
6770    async fn set_task_content(
6771        &self,
6772        id: &NativeId,
6773        content: &str,
6774    ) -> Result<Option<()>, SourceError> {
6775        self.replace_content(id, content).await
6776    }
6777
6778    /// Replace one task's `delivered_by` with a single body update that changes the
6779    /// metadata slot and nothing outside it.
6780    async fn set_delivered_by(
6781        &self,
6782        id: &NativeId,
6783        delivered_by: &[TaskRef],
6784    ) -> Result<Option<()>, SourceError> {
6785        self.replace_delivered_by(id, delivered_by).await
6786    }
6787
6788    /// Set one key of one task issue's metadata with a single body update that changes the
6789    /// metadata slot and nothing outside it — no title, label, state or board field request —
6790    /// and sends nothing when the task already holds that value under the key.
6791    async fn set_task_metadata(
6792        &self,
6793        id: &NativeId,
6794        key: &MetadataKey,
6795        value: &Value,
6796    ) -> Result<Option<Task>, SourceError> {
6797        Ok(self
6798            .set_slot_key(id, BoardKind::Work(ItemKind::Task), key, value)
6799            .await?
6800            .map(|item| item.task())
6801            .transpose()?)
6802    }
6803
6804    /// Set one key of one project issue's metadata, on exactly the terms of
6805    /// [`set_task_metadata`](TaskSource::set_task_metadata).
6806    async fn set_project_metadata(
6807        &self,
6808        id: &NativeId,
6809        key: &MetadataKey,
6810        value: &Value,
6811    ) -> Result<Option<Project>, SourceError> {
6812        Ok(self
6813            .set_slot_key(id, BoardKind::Work(ItemKind::Project), key, value)
6814            .await?
6815            .map(|item| item.project()))
6816    }
6817
6818    /// Set one key of one design-document issue's metadata, on exactly the terms of
6819    /// [`set_task_metadata`](TaskSource::set_task_metadata).
6820    async fn set_document_metadata(
6821        &self,
6822        id: &NativeId,
6823        key: &MetadataKey,
6824        value: &Value,
6825    ) -> Result<Option<Document>, SourceError> {
6826        Ok(self
6827            .set_slot_key(id, BoardKind::Document, key, value)
6828            .await?
6829            .map(|item| item.document()))
6830    }
6831
6832    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
6833        self.delete_item(id).await
6834    }
6835
6836    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
6837        self.delete_item(id).await
6838    }
6839
6840    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
6841        self.delete_item(id).await
6842    }
6843
6844    /// One page of the task issue's own comments, walked by GitHub's own cursor.
6845    ///
6846    /// Nothing here filters, so nothing has to be read ahead of the page: the caller's limit is
6847    /// the page GitHub is asked for and GitHub's `endCursor` is the cursor handed back.
6848    async fn task_comments(
6849        &self,
6850        task: &NativeId,
6851        page: &PageRequest,
6852    ) -> Result<Option<Page<Comment>>, SourceError> {
6853        validate_page(page)?;
6854        let Some(issue) = self.commented_issue(task).await? else {
6855            return Ok(None);
6856        };
6857        let after = page.cursor.as_ref().map(|cursor| cursor.0.as_str());
6858        let data = self
6859            .graphql(
6860                graphql::ISSUE_COMMENTS,
6861                json!({"id":issue.0,"first":page.limit.min(MAX_PAGE_SIZE),"after":after}),
6862            )
6863            .await?;
6864        // The issue was there a moment ago; one removed since is no longer a task here.
6865        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
6866            return Ok(None);
6867        };
6868        let connection = node
6869            .get("comments")
6870            .filter(|value| !value.is_null())
6871            .ok_or_else(|| SourceError::Malformed {
6872                message: format!(
6873                    "GitHub issue {} answered with no comments connection",
6874                    issue.0
6875                ),
6876            })?;
6877        let items = optional_nodes(Some(connection), "issue comments")?
6878            .into_iter()
6879            .flatten()
6880            .map(comment_from)
6881            .collect::<Result<Vec<_>, _>>()?;
6882        let next = next_cursor(connection)?;
6883        if let Some(next) = &next {
6884            validate_cursor_progress(after, &next.0)?;
6885        }
6886        Ok(Some(Page { items, next }))
6887    }
6888
6889    /// Add one comment to the task's issue, as the account the token belongs to.
6890    ///
6891    /// The author is refused before anything is sent — not even the task is read — because
6892    /// no answer GitHub could give would make posting under another name than the one asked
6893    /// for the right outcome.
6894    async fn add_comment(
6895        &self,
6896        task: &NativeId,
6897        comment: &NewComment,
6898    ) -> Result<Option<Comment>, SourceError> {
6899        if let Some(author) = &comment.author {
6900            return Err(SourceError::Refused {
6901                message: format!(
6902                    "source {} cannot post a comment as {author:?}: GitHub records the account \
6903                     the token signs in as the author of every comment; next: leave --author \
6904                     out, and the comment is posted as that account",
6905                    self.name
6906                ),
6907            });
6908        }
6909        let Some(issue) = self.commented_issue(task).await? else {
6910            return Ok(None);
6911        };
6912        let data = self
6913            .graphql(
6914                graphql::ADD_COMMENT,
6915                json!({"input":{"subjectId":issue.0,"body":comment.body.as_str()}}),
6916            )
6917            .await?;
6918        let subject = data
6919            .pointer("/addComment/subject")
6920            .filter(|value| !value.is_null())
6921            .ok_or_else(|| SourceError::Malformed {
6922                message: "GitHub comment addition returned no subject".into(),
6923            })?;
6924        if required_str(subject, "id")? != issue.0 {
6925            return Err(SourceError::Malformed {
6926                message: "GitHub comment addition answered about another issue".into(),
6927            });
6928        }
6929        let added = data
6930            .pointer("/addComment/commentEdge/node")
6931            .filter(|value| !value.is_null())
6932            .ok_or_else(|| SourceError::Malformed {
6933                message: "GitHub comment addition returned no comment".into(),
6934            })?;
6935        comment_from(added).map(Some)
6936    }
6937
6938    async fn edit_comment(
6939        &self,
6940        task: &NativeId,
6941        comment: &NativeId,
6942        body: &CommentBody,
6943    ) -> Result<Option<Comment>, SourceError> {
6944        let Some(issue) = self.commented_issue(task).await? else {
6945            return Ok(None);
6946        };
6947        if !self.comment_is_on(&issue, comment).await? {
6948            return Ok(None);
6949        }
6950        let data = self
6951            .graphql(
6952                graphql::UPDATE_COMMENT,
6953                json!({"input":{"id":comment.0,"body":body.as_str()}}),
6954            )
6955            .await?;
6956        let edited = data
6957            .pointer("/updateIssueComment/issueComment")
6958            .filter(|value| !value.is_null())
6959            .ok_or_else(|| SourceError::Malformed {
6960                message: "GitHub comment update returned no comment".into(),
6961            })?;
6962        let edited = comment_from(edited)?;
6963        if edited.id != *comment {
6964            return Err(SourceError::Malformed {
6965                message: "GitHub comment update returned the wrong comment".into(),
6966            });
6967        }
6968        Ok(Some(edited))
6969    }
6970
6971    async fn delete_comment(
6972        &self,
6973        task: &NativeId,
6974        comment: &NativeId,
6975    ) -> Result<Option<NativeId>, SourceError> {
6976        let Some(issue) = self.commented_issue(task).await? else {
6977            return Ok(None);
6978        };
6979        if !self.comment_is_on(&issue, comment).await? {
6980            return Ok(None);
6981        }
6982        let data = self
6983            .graphql(graphql::DELETE_COMMENT, json!({"input":{"id":comment.0}}))
6984            .await?;
6985        // The payload says nothing about the comment it removed, so what is checked is that
6986        // GitHub answered the mutation at all rather than leaving it unanswered.
6987        data.get("deleteIssueComment")
6988            .filter(|value| !value.is_null())
6989            .ok_or_else(|| SourceError::Malformed {
6990                message: "GitHub comment deletion returned no payload".into(),
6991            })?;
6992        Ok(Some(comment.clone()))
6993    }
6994
6995    /// Every request this source has recorded, and what each of GitHub's two budgets was
6996    /// attributed — read off the same accounting the session report is rendered from, so
6997    /// the two cannot count one request two ways.
6998    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
6999        Ok(Some(self.ledger.snapshot().metering()))
7000    }
7001}
7002
7003/// One issue comment as the contract carries it.
7004///
7005/// `author` is absent both when GitHub answers `null` for an account that no longer exists
7006/// and when it answers an actor with no login, because either way the source did not say who
7007/// wrote it — which is what an absent author means, rather than an author called nothing.
7008fn comment_from(value: &Value) -> Result<Comment, SourceError> {
7009    Ok(Comment {
7010        id: NativeId(required_str(value, "id")?.to_owned()),
7011        author: optional_str(value.get("author").unwrap_or(&Value::Null), "login")?
7012            .map(str::to_owned),
7013        created_at: optional_time(value, "createdAt")?,
7014        updated_at: optional_time(value, "updatedAt")?,
7015        body: required_str(value, "body")?.to_owned(),
7016        url: optional_str(value, "url")?.map(str::to_owned),
7017    })
7018}
7019
7020/// Where the recorded tail of a dependency walk resumes; see
7021/// [`GitHubProjectsSource::recorded_edges`].
7022const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
7023
7024/// The board text field this source keeps a copy's origin in.
7025///
7026/// Named after the key it holds, and held to that name by the guard below rather than by
7027/// a reader noticing.
7028const ORIGIN_FIELD: &str = "onetaskgraph.origin";
7029
7030/// The metadata key that field holds.
7031///
7032/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
7033/// constructs or interprets the qualified id it carries. This source names it only to
7034/// route it — a short, typed value belongs in a typed field rather than in the body slot
7035/// a caller's own prose shares.
7036///
7037/// Restated rather than imported, because no plugin crate may depend on the engine. What
7038/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
7039/// target in `check`: it reads the engine's own literal and fails naming the file and the
7040/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
7041/// that creates a second item every run instead of finding the one it wrote — and that is
7042/// too late to learn it.
7043const ORIGIN_KEY: &str = "onetaskgraph.origin";
7044
7045/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
7046///
7047/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
7048/// is derived from the far end, never written down on the near item — so only a forward
7049/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
7050/// it did not come from, and it is told so rather than answered with an empty page that
7051/// reads as a walk which ended.
7052fn recorded_offset(
7053    cursor: Option<&str>,
7054    direction: Direction,
7055) -> Result<Option<usize>, SourceError> {
7056    cursor
7057        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
7058        .map(|offset| {
7059            if direction != Direction::DependsOn {
7060                return Err(SourceError::Config {
7061                    message: format!(
7062                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
7063                         reverse dependency read never issues; resume it in the direction \
7064                         that reported it"
7065                    ),
7066                });
7067            }
7068            offset.parse().map_err(|_| SourceError::Config {
7069                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
7070            })
7071        })
7072        .transpose()
7073}
7074
7075fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
7076    let mut page = offset_page(edges, offset, limit.max(1));
7077    page.next = page
7078        .next
7079        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
7080    page
7081}
7082
7083/// The kind of one issue reached through a dependency connection.
7084///
7085/// The same questions the board scan asks, over the fields the dependency document
7086/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
7087/// then anything with sub-issues or the marker is a project.
7088///
7089/// # Errors
7090///
7091/// A far end this board holds as a document is refused rather than reported. The two
7092/// answers that are not refusals would both be wrong: reporting it as a task names an id
7093/// no task read of this source can find, and reporting it as a project names one no
7094/// project read can. There is no third value to return — `ItemKind` has no document
7095/// variant, because nothing may point at a document — so the relationship itself is what
7096/// the person is told about.
7097fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
7098    let id = required_str(value, "id")?;
7099    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
7100        return Err(SourceError::Refused {
7101            message: format!(
7102                "GitHub issue {id} is a document of this board — its title begins \
7103                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
7104                 on by one; next: remove that issue's blocking relationship on this board"
7105            ),
7106        });
7107    }
7108    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
7109    if parent.is_some() {
7110        return Ok(ItemKind::Task);
7111    }
7112    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
7113    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
7114        message: format!("GitHub issue {id}: {message}"),
7115    })?;
7116    let sub_issues = sub_issue_total(value)?;
7117    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
7118        ItemKind::Project
7119    } else {
7120        ItemKind::Task
7121    })
7122}
7123
7124/// The `IssueStateUpdateInput` one status target asks for.
7125///
7126/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
7127/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
7128/// a currently-closed issue: without that the item would read back `Unknown` and a copy
7129/// would report a change forever. A document has no status at all, and asks for neither.
7130fn state_input(target: Option<&StatusTarget>) -> Value {
7131    match target {
7132        Some(StatusTarget::Terminal(_, reason)) => {
7133            json!({"value":"CLOSED","stateReason":reason.reason()})
7134        }
7135        Some(StatusTarget::Column(_) | StatusTarget::Disabled) => json!({"value":"OPEN"}),
7136        // A document has no status, so a write of one says nothing about the issue's open
7137        // or closed state rather than forcing it open: `stateInput` is what carries that
7138        // instruction, and an explicit null asks for no change to it.
7139        None => Value::Null,
7140    }
7141}
7142
7143/// The metadata one write stores in the item's body slot.
7144///
7145/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
7146/// rather than carried: the kind marker so an empty project stays readable, the
7147/// repository list only when it is not exactly the issue's own repository, and the far
7148/// ends no relationship here can name.
7149fn slot_metadata(
7150    incoming: &Incoming<'_>,
7151    own_repository: Option<&Repository>,
7152    fallback: &[DependencyEdge],
7153) -> BTreeMap<String, Value> {
7154    let mut metadata = incoming.metadata.clone();
7155    metadata.remove(ORIGIN_KEY);
7156    match incoming.written.kind() {
7157        BoardKind::Work(kind) => metadata.insert(
7158            ItemKind::METADATA_KEY.to_owned(),
7159            Value::String(kind.marker().to_owned()),
7160        ),
7161        // A document is told by its title, so it carries no kind marker: that key names
7162        // what a dependency endpoint points at, and nothing may point at a document.
7163        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
7164    };
7165    let derivable = own_repository
7166        .map(|own| incoming.repositories == [own.clone()])
7167        .unwrap_or(incoming.repositories.is_empty());
7168    if derivable {
7169        metadata.remove(Repository::METADATA_KEY);
7170    } else {
7171        metadata.insert(
7172            Repository::METADATA_KEY.to_owned(),
7173            Value::Array(
7174                incoming
7175                    .repositories
7176                    .iter()
7177                    .map(|repository| Value::String(repository.as_str().to_owned()))
7178                    .collect(),
7179            ),
7180        );
7181    }
7182    // The typed lists are what land, whatever the caller's own metadata held under their
7183    // keys: a key of either name travelling beside the field would otherwise be a second
7184    // answer to the same question, and the field is the one the contract names.
7185    for (key, entries) in [
7186        (TaskRef::DELIVERS_KEY, incoming.delivers),
7187        (TaskRef::DELIVERED_BY_KEY, incoming.delivered_by),
7188    ] {
7189        set_task_list(&mut metadata, key, entries);
7190    }
7191    if fallback.is_empty() {
7192        metadata.remove(DependencyEdge::RECORDED_KEY);
7193    } else {
7194        metadata.insert(
7195            DependencyEdge::RECORDED_KEY.to_owned(),
7196            Value::Array(
7197                fallback
7198                    .iter()
7199                    .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
7200                    .collect(),
7201            ),
7202        );
7203    }
7204    metadata
7205}
7206
7207/// Every label one item carries, from its content's own connection and nowhere else.
7208///
7209/// There is no second place to read one from: no document this source sends selects the
7210/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
7211/// cannot carry one at all. The module documentation records the three schema facts that
7212/// settle it.
7213fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
7214    optional_nodes(content.get("labels"), "content labels")?
7215        .into_iter()
7216        .flatten()
7217        .map(|v| {
7218            Ok(Label {
7219                id: NativeId(required_str(v, "id")?.to_owned()),
7220                name: required_str(v, "name")?.to_owned(),
7221                color: optional_str(v, "color")?.map(str::to_owned),
7222            })
7223        })
7224        .collect()
7225}
7226
7227/// The definition of each board field one item's values are values of, in the shape a read
7228/// of the board's own `fields` gives one.
7229///
7230/// A value names its field through a fragment on that field's own type, so the type is
7231/// known from which kind of value it is: a single-select value's field is a
7232/// `ProjectV2SingleSelectField`, options and all, and a text value's is a `ProjectV2Field`.
7233/// A value whose field carried no id, or an empty one, says nothing usable and is left out.
7234fn field_definitions(field_values: &[Value]) -> Vec<Value> {
7235    field_values
7236        .iter()
7237        .filter_map(|value| {
7238            let field = value.get("field")?.as_object()?;
7239            field.get("id")?.as_str().filter(|id| !id.is_empty())?;
7240            let typename = if value.get("text").is_some() {
7241                "ProjectV2Field"
7242            } else if value.get("name").is_some() {
7243                "ProjectV2SingleSelectField"
7244            } else {
7245                return None;
7246            };
7247            let mut defined = field.clone();
7248            defined.insert("__typename".to_owned(), json!(typename));
7249            Some(Value::Object(defined))
7250        })
7251        .collect()
7252}
7253
7254fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
7255    let Some(node) = field_values
7256        .iter()
7257        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
7258    else {
7259        return Ok(None);
7260    };
7261    Ok(optional_str(node, "text")?.map(str::to_owned))
7262}
7263
7264fn valid_github_owner(owner: &str) -> bool {
7265    !owner.is_empty()
7266        && owner.len() <= 39
7267        && !owner.starts_with('-')
7268        && !owner.ends_with('-')
7269        && !owner.contains("--")
7270        && owner
7271            .bytes()
7272            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
7273}
7274
7275/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
7276/// neither of the two names a path segment already means.
7277fn valid_github_repository_name(name: &str) -> bool {
7278    !name.is_empty()
7279        && name.len() <= 100
7280        && name != "."
7281        && name != ".."
7282        && name
7283            .bytes()
7284            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
7285}
7286
7287fn valid_environment_name(name: &str) -> bool {
7288    let mut bytes = name.bytes();
7289    bytes
7290        .next()
7291        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
7292        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
7293}
7294
7295/// How many sub-issues one issue has.
7296///
7297/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
7298/// absent or non-integer one is a response this source cannot read — and reading it as
7299/// zero would classify a project as a task, which is exactly the mistake the marker
7300/// exists to keep from happening quietly.
7301fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
7302    let summary = issue
7303        .get("subIssuesSummary")
7304        .ok_or_else(|| SourceError::Malformed {
7305            message: "GitHub issue is missing subIssuesSummary".into(),
7306        })?;
7307    summary
7308        .get("total")
7309        .and_then(Value::as_u64)
7310        .ok_or_else(|| SourceError::Malformed {
7311            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
7312        })
7313}
7314
7315/// One issue's own `number`.
7316///
7317/// An issue always has one: GitHub declares `Issue.number` as `Int!` and every selection of
7318/// an issue in this module asks for it. So a read of one that comes back without it, or
7319/// with something that is not an unsigned integer, is a response this source cannot read —
7320/// absence here is **not** "this issue has no number". A draft is the content that has
7321/// none, and a draft never reaches this: the caller decides on `__typename` first, the way
7322/// it does for `subIssuesSummary`, which `DraftIssue` equally declares nothing for.
7323fn issue_number(issue: &Value) -> Result<u64, SourceError> {
7324    issue
7325        .get("number")
7326        .and_then(Value::as_u64)
7327        .ok_or_else(|| SourceError::Malformed {
7328            message: "GitHub issue number is missing or is not an unsigned integer".into(),
7329        })
7330}
7331
7332/// The `number` a creating mutation answered with, and `None` when it answered without one;
7333/// why a missing one is tolerated is at the call in `create_and_file_issue`.
7334fn created_issue_number(created: &Value) -> Result<Option<u64>, SourceError> {
7335    match created.get("number") {
7336        None | Some(Value::Null) => Ok(None),
7337        Some(value) => value
7338            .as_u64()
7339            .map(Some)
7340            .ok_or_else(|| SourceError::Malformed {
7341                message: "GitHub created issue number is not an unsigned integer".into(),
7342            }),
7343    }
7344}
7345
7346fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
7347    value
7348        .get(field)
7349        .and_then(Value::as_str)
7350        .ok_or_else(|| SourceError::Malformed {
7351            message: format!("GitHub response is missing string field {field}"),
7352        })
7353}
7354
7355fn required_nonblank_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
7356    let found = required_str(value, field)?;
7357    if found.trim().is_empty() {
7358        return Err(SourceError::Malformed {
7359            message: format!("GitHub response has blank string field {field}"),
7360        });
7361    }
7362    Ok(found)
7363}
7364
7365/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
7366/// needs one — Linear spells them too, in its own description field.
7367///
7368/// Restated rather than shared, because a plugin crate depends on the contract crate and
7369/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
7370/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
7371/// source round-trips its own writes perfectly well under its own spelling.
7372const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
7373const METADATA_CLOSE: &str = "\n-->";
7374
7375/// What the composer puts between a non-empty visible body and the slot, and the one thing
7376/// the parser takes off the visible body when it takes the slot off — exactly once, so every
7377/// other trailing byte of the body comes back as it was written.
7378// 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.
7379const METADATA_SEPARATOR: &str = "\n\n";
7380
7381/// The visible body and the metadata slot at the end of it.
7382///
7383/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
7384/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
7385/// own content and is left alone. The visible body is everything before the slot less the
7386/// one [`METADATA_SEPARATOR`] the composer put there, byte for byte.
7387fn metadata_body(
7388    body: Option<String>,
7389) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
7390    let Some(body) = body else {
7391        return Ok((None, BTreeMap::new()));
7392    };
7393    let Some(slot) = slot_span(&body)? else {
7394        return Ok((Some(body), BTreeMap::new()));
7395    };
7396    let metadata =
7397        serde_json::from_str(&body[slot.encoded_start..slot.encoded_end]).map_err(|error| {
7398            SourceError::Malformed {
7399                message: format!(
7400                    "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
7401                ),
7402            }
7403        })?;
7404    let before = &body[..slot.start];
7405    let visible = before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before);
7406    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
7407}
7408
7409/// Where the metadata slot sits in one body, as byte offsets into it.
7410struct SlotSpan {
7411    /// Where [`METADATA_OPEN`] begins.
7412    start: usize,
7413    /// Where the encoded JSON begins, just past [`METADATA_OPEN`].
7414    encoded_start: usize,
7415    /// Where the encoded JSON ends, at the start of [`METADATA_CLOSE`].
7416    encoded_end: usize,
7417    /// Just past [`METADATA_CLOSE`].
7418    end: usize,
7419}
7420
7421/// The slot at the very end of `body`, or `None` when it has none.
7422///
7423/// The one reading of *where the slot is*, shared by [`metadata_body`], which reads it, and
7424/// [`with_slot`], which rewrites it — so the two cannot disagree about which comment is the
7425/// slot.
7426fn slot_span(body: &str) -> Result<Option<SlotSpan>, SourceError> {
7427    let Some(start) = body.rfind(METADATA_OPEN) else {
7428        return Ok(None);
7429    };
7430    let encoded_start = start + METADATA_OPEN.len();
7431    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
7432        return Err(SourceError::Malformed {
7433            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
7434        });
7435    };
7436    let encoded_end = encoded_start + relative_end;
7437    let end = encoded_end + METADATA_CLOSE.len();
7438    if !body[end..].trim().is_empty() {
7439        return Ok(None);
7440    }
7441    Ok(Some(SlotSpan {
7442        start,
7443        encoded_start,
7444        encoded_end,
7445        end,
7446    }))
7447}
7448
7449/// `body` with its metadata slot holding exactly `metadata`, and every byte outside the
7450/// slot as it was.
7451///
7452/// A slot that is there has its JSON replaced in place; one that becomes empty is removed
7453/// together with the one [`METADATA_SEPARATOR`] separating it from the prose before it. A
7454/// body with no slot gains one the way [`compose_body`] writes it — after that separator,
7455/// or alone in an empty body — and a body with no slot that is given no metadata is
7456/// returned as it is.
7457fn with_slot(body: &str, metadata: &BTreeMap<String, Value>) -> Result<String, SourceError> {
7458    let encoded = if metadata.is_empty() {
7459        None
7460    } else {
7461        Some(
7462            serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
7463                message: error.to_string(),
7464            })?,
7465        )
7466    };
7467    Ok(match (slot_span(body)?, encoded) {
7468        (Some(slot), Some(encoded)) => format!(
7469            "{}{encoded}{}",
7470            &body[..slot.encoded_start],
7471            &body[slot.encoded_end..]
7472        ),
7473        (Some(slot), None) => {
7474            let before = &body[..slot.start];
7475            format!(
7476                "{}{}",
7477                before.strip_suffix(METADATA_SEPARATOR).unwrap_or(before),
7478                &body[slot.end..]
7479            )
7480        }
7481        (None, None) => body.to_owned(),
7482        (None, Some(encoded)) if body.is_empty() => {
7483            format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
7484        }
7485        (None, Some(encoded)) => {
7486            format!("{body}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
7487        }
7488    })
7489}
7490
7491/// `body` with everything before its metadata slot replaced by `content`, and the slot
7492/// itself kept byte for byte.
7493///
7494/// The inverse of how [`metadata_body`] splits a body: the slot, when there is one, follows
7495/// `content` after the one [`METADATA_SEPARATOR`] the composer puts there — or alone, when
7496/// `content` is empty — so a read of the result reports `content` as the visible body and
7497/// the slot's metadata exactly as it was.
7498fn with_content(body: &str, content: &str) -> Result<String, SourceError> {
7499    let Some(slot) = slot_span(body)? else {
7500        return Ok(content.to_owned());
7501    };
7502    let kept = &body[slot.start..];
7503    Ok(if content.is_empty() {
7504        kept.to_owned()
7505    } else {
7506        format!("{content}{METADATA_SEPARATOR}{kept}")
7507    })
7508}
7509
7510/// Hold `entries` under `key` in one slot's metadata, or no such key when there are none.
7511fn set_task_list(metadata: &mut BTreeMap<String, Value>, key: &str, entries: &[TaskRef]) {
7512    if entries.is_empty() {
7513        metadata.remove(key);
7514    } else {
7515        metadata.insert(
7516            key.to_owned(),
7517            Value::Array(
7518                entries
7519                    .iter()
7520                    .map(|entry| Value::String(entry.as_str().to_owned()))
7521                    .collect(),
7522            ),
7523        );
7524    }
7525}
7526
7527fn compose_body(
7528    content: Option<&str>,
7529    metadata: &BTreeMap<String, Value>,
7530) -> Result<Option<String>, SourceError> {
7531    let visible = content.unwrap_or_default();
7532    if metadata.is_empty() {
7533        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
7534    }
7535    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
7536        message: error.to_string(),
7537    })?;
7538    Ok(Some(if visible.is_empty() {
7539        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
7540    } else {
7541        format!("{visible}{METADATA_SEPARATOR}{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
7542    }))
7543}
7544
7545fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
7546    value
7547        .get(field)
7548        .and_then(Value::as_bool)
7549        .ok_or_else(|| SourceError::Malformed {
7550            message: format!("GitHub response is missing boolean field {field}"),
7551        })
7552}
7553fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
7554    match value.get(field) {
7555        None | Some(Value::Null) => Ok(None),
7556        Some(value) => value
7557            .as_str()
7558            .map(Some)
7559            .ok_or_else(|| SourceError::Malformed {
7560                message: format!("GitHub response field {field} is not a string or null"),
7561            }),
7562    }
7563}
7564fn optional_nodes<'a>(
7565    connection: Option<&'a Value>,
7566    name: &str,
7567) -> Result<Option<&'a Vec<Value>>, SourceError> {
7568    match connection {
7569        None | Some(Value::Null) => Ok(None),
7570        Some(value) => value
7571            .get("nodes")
7572            .and_then(Value::as_array)
7573            .map(Some)
7574            .ok_or_else(|| SourceError::Malformed {
7575                message: format!("GitHub {name}.nodes is not an array"),
7576            }),
7577    }
7578}
7579fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
7580    let page_info = connection
7581        .get("pageInfo")
7582        .ok_or_else(|| SourceError::Malformed {
7583            message: format!("GitHub {name} has no pageInfo"),
7584        })?;
7585    if required_bool(page_info, "hasNextPage")? {
7586        return Err(SourceError::Malformed {
7587            message: format!(
7588                "GitHub {name} exceeds the supported nested connection size of {size}"
7589            ),
7590        });
7591    }
7592    Ok(())
7593}
7594fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
7595    optional_str(value, field)?
7596        .map(|timestamp| {
7597            timestamp.parse().map_err(|error| SourceError::Malformed {
7598                message: format!("GitHub response field {field} is not a timestamp: {error}"),
7599            })
7600        })
7601        .transpose()
7602}
7603fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
7604    if page.limit == 0 {
7605        Err(SourceError::Config {
7606            message: "page limit must be at least 1".into(),
7607        })
7608    } else {
7609        Ok(())
7610    }
7611}
7612fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
7613    let page = connection
7614        .get("pageInfo")
7615        .filter(|value| value.is_object())
7616        .ok_or_else(|| SourceError::Malformed {
7617            message: "GitHub connection is missing pageInfo".into(),
7618        })?;
7619    if required_bool(page, "hasNextPage")? {
7620        let cursor = required_str(page, "endCursor")?;
7621        validate_cursor_progress(None, cursor)?;
7622        Ok(Some(Cursor(cursor.into())))
7623    } else {
7624        Ok(None)
7625    }
7626}
7627fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
7628    if next.is_empty() || previous == Some(next) {
7629        Err(SourceError::Malformed {
7630            message: "GitHub pagination cursor is empty or did not advance".into(),
7631        })
7632    } else {
7633        Ok(())
7634    }
7635}
7636fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
7637    cursor.map_or(Ok(0), |c| {
7638        c.0.parse().map_err(|_| SourceError::Config {
7639            message: "page cursor is invalid".into(),
7640        })
7641    })
7642}
7643fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
7644    if offset > items.len() {
7645        return Page::last(vec![]);
7646    }
7647    let tail = items.split_off(offset);
7648    let mut selected = tail;
7649    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
7650    selected.truncate(limit);
7651    Page {
7652        items: selected,
7653        next,
7654    }
7655}