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//! **Where an entity is, is a link.** Every project, task and document this source reports
53//! carries a [`Location::Url`] naming the issue's own web address — the same address the
54//! `url` field already reports, in the shape that says a reader can open it. That is the
55//! contrast the location contract exists for: a reader holding an entity from this source
56//! is handed something to link to and one holding an entity from a folder of Markdown is
57//! handed a path, and neither has to know which plugin answered. It does not replace or
58//! derive from `url`; that field goes on reporting what it always reported.
59//!
60//! **Where metadata lives.** Short typed things go to typed fields and native relations:
61//! status to the board's `Status` single-select and the issue's own state, the copy
62//! origin to a source-owned `onetaskgraph.origin` text field, and dependencies to
63//! `blockedBy` and to sub-issue links. Unbounded caller JSON goes in a trailing
64//! `<!-- onetaskgraph.metadata ... -->` comment at the end of the issue body — the same
65//! encoding `docs/metadata.md` settles for Linear, not a second one. A ProjectV2 text
66//! field is length-bounded and `shortDescription` is capped at 300 characters, which is
67//! why neither can hold a caller's own prose.
68//!
69//! **Status.** `status_mapping` is per-instance configuration from a status category to
70//! `null`, a board `Status` option name, or a closed state of `completed` or
71//! `not-planned`. Nothing here ever calls `updateProjectV2Field`: that mutation's
72//! `singleSelectOptions` *overwrites* a field's option set, so no addition is additive
73//! and a mistake destroys every item's status. A status this board cannot represent is a
74//! refusal naming the status and the instance instead.
75//!
76//! `unknown` is disabled by default because this source cannot preserve an open-ended
77//! status word: it writes an existing board option or the issue's closed state and never
78//! creates an option. An operator may map `unknown` to one existing option, in which case
79//! every unknown word lands on that option and reads back as `unknown` under the option's
80//! name. Mapping it to a closed state is refused because GitHub reads that state back as
81//! `done` or `cancelled`, changing the category on every copy. This differs from
82//! `local-md`, which writes and reads the original word itself.
83//!
84//! `done` closes the issue by default because GitHub derives `subIssuesSummary.completed`
85//! and the board's own `Sub-issues progress` field from closed sub-issues: a plan whose
86//! finished tasks were only moved to a "Done" column would read 0% complete forever.
87//!
88//! # What this source declares, field by field
89//!
90//! One verdict per field of [`Capabilities`], and what `Native` means when this source
91//! says it. *Proven* means a shared journey drives it against the real
92//! binary over this source's own row in `crates/onetaskgraph/tests/e2e/fixtures.rs`, and
93//! `every_row_declares_exactly_what_its_plugin_reports` is what keeps this list and
94//! [`capabilities`](TaskSource::capabilities) from parting.
95//!
96//! | Field | Verdict |
97//! | --- | --- |
98//! | `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. |
99//! | `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. |
100//! | `orphan_tasks` | **Supported and proven.** A task issue with no `parent` is in no project. |
101//! | `filter_by_label` | **Supported and proven,** over the issue's own labels. |
102//! | `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`. |
103//! | `search_title` | **Supported and proven,** over `Issue.title`. |
104//! | `search_content` | **Supported and proven,** over the visible body — the trailing metadata comment is not part of it. |
105//! | `task_dependencies` | **Supported and proven,** in both directions: `blockedBy` and `blocking`. |
106//! | `project_dependencies` | **Supported and proven,** in both directions, over the same two connections, because a project here is an issue. |
107//! | `max_page_size` | **Supported and proven.** [`MAX_PAGE_SIZE`], GitHub's own connection maximum. |
108//!
109//! Nothing here is unsupported. `documents` is not a predicate — it says this source has
110//! documents, which it does — and the three facts behind the uniform `Native` on the
111//! predicates beside it are recorded below rather than re-derived, because a reader who
112//! takes `Native` to mean *the remote service filters* will read that uniformity as a
113//! lie.
114//!
115//! First, the plugin contract defines `Support::Native` as *the source applies this
116//! predicate itself*, and says nothing about where it applies it. What the declaration
117//! promises the engine is capability rule 1 — a predicate declared `Native` **is** applied
118//! — so that the engine may push it down and apply nothing of its own.
119//!
120//! Second, this source can keep that promise for every predicate at no additional API
121//! cost, because whichever of the three reads below answers a query has already read every
122//! item that query could keep before it filters anything. Filtering those items is
123//! in-process work over data already in hand.
124//!
125//! Third, no predicate but `projects` could be pushed into the API even if that were
126//! wanted, and `projects` is pushed down: `ProjectV2.items` takes `first` and `after` and
127//! offers no filter argument of any kind, GitHub's issue search offers no qualifier for a
128//! label set, a status column or a substring of a body, and its title qualifier matches
129//! tokens where this source — and the local Markdown source beside it — match substrings,
130//! so pushing a search down would silently *narrow* the answer. What a project filter has
131//! instead is a relationship: a project's tasks are that issue's sub-issues, and asking
132//! the issue for them is both cheaper and exact. So there is one predicate this source
133//! applies by asking a narrower question, six it applies in process, and none it is unable
134//! to apply. Declaring one `Unsupported` would make the engine compensate for work this
135//! source has already done, and declaring `projects` native while ignoring the filter
136//! (which this source once did) silently returns another project's tasks, because the
137//! engine trusts the declaration and applies nothing locally.
138//!
139//! # The three ways this source reaches an item, and what each costs
140//!
141//! A board read is charged for what its *nested* connections could return rather than for
142//! what was asked, so one whole-board read costs the same whether the question was about
143//! one project or about all of them. That is why a question about one project is never
144//! answered by reading the board:
145//!
146//! | The question | What is sent | What it costs |
147//! | --- | --- | --- |
148//! | one item, by its own id | [`graphql::ISSUE`] — `node(id:)` | the item |
149//! | one project's tasks or documents | [`graphql::SUB_ISSUES`] — that issue's own `subIssues` | that project |
150//! | which projects this board holds | [`graphql::SEARCH_ISSUES`] — an issue search scoped to the board | the board's issues, without their board items |
151//! | every task, every document, every label | [`graphql::BOARD`] — the board's own `items` | the board |
152//! | 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 |
153//!
154//! The board half of an issue — its board item's id, its `Status` option and this
155//! source's origin text field — rides along on `Issue.projectItems` in the first three, so
156//! an item reached any of those ways resolves through the same
157//! [`GitHubProjectsSource::resolve`] the board walk uses and reports the same title, the
158//! same status, the same labels and the same qualified id. That connection comes back a
159//! *page* at a time, at `BOARD_ITEMS_PAGE_SIZE`, so the entry for this board is looked for
160//! on the page in hand and — only if that page reports more of the connection — in the
161//! last row's read of that one issue's memberships, resumed from the page's own cursor and
162//! walked to exhaustion. An issue with no entry for *this* board is not this source's to
163//! report, which is what keeps an id naming another repository's issue from being answered
164//! as an item of this board; and because the page is where the search starts rather than
165//! where it ends, that answer is one about a connection read to exhaustion and never about
166//! an unread page. Nothing costs the extra read but an issue on more boards than a page
167//! holds: an issue this board really does not hold reports no next page, so its
168//! memberships are already exhausted where they arrived.
169//!
170//! **No document here selects the board's own `Labels` field, and nothing is lost by
171//! that.** An item's labels are read from its content alone, wherever that content is
172//! reached: the three documents above select `Issue.labels` on the fragment, and
173//! [`graphql::BOARD`] selects the same connection on the `... on Issue` arm of its
174//! `content`. A board's `Labels` field is not one anybody fills in: it is a built-in
175//! `ProjectV2FieldType`, it is absent from `ProjectV2CustomFieldType` so no project can
176//! create one, and `ProjectV2FieldValue` — the whole of what
177//! `updateProjectV2ItemFieldValue` accepts — offers no way to write one. So GitHub derives
178//! it from the content, for every content type it exists on, and there is nothing it can
179//! hold that the content does not already say: for an `Issue` it *is* that issue's own
180//! labels, so selecting it beside them unions a set with itself.
181//!
182//! **A draft loses nothing by that either**, which is the reasoning this paragraph once had
183//! backwards. `DraftIssue` exposes no `labels` field, and by the three schema facts above
184//! it cannot carry a board `Labels` value to be derived from one — so a draft has nothing
185//! to select *and nothing to lose*, and reports no labels at all. A `PullRequest` item is
186//! discarded by [`GitHubProjectsSource::resolve`] before labels are read. Both halves are
187//! held to that by tests in `tests/plugin.rs`: the four ways an item is reached report one
188//! label set, and that set is the fixture issue's own, by
189//! `an_item_reports_the_same_labels_title_status_and_id_however_it_is_reached`; and a board
190//! item whose content is a draft reports an empty set, by
191//! `a_board_item_whose_content_is_a_draft_reports_no_labels_at_all`. The absence of the
192//! selection is held over [`graphql::DOCUMENTS`] by
193//! `no_document_selects_the_boards_own_labels_field`.
194//!
195//! The last row is still the board's own item connection, and deliberately: a **draft**
196//! board item is not an issue, so no search and no node read can reach one, and the reads
197//! that have to answer for the whole board are the ones whose cost is the board's size
198//! anyway.
199//!
200//! **What a read may return is capped too, and that cap is on the document rather than on
201//! the board.** GitHub limits the number of nodes **one query may return** to
202//! [`NODE_COUNT_LIMIT`] and refuses a query above that before executing it: the answer is
203//! an error naming the connection the count crossed at, not a slow or a partial result.
204//! Every board this source reads is refused the same way, so no board is too big for these
205//! documents and none is small enough to save one that is over.
206//!
207//! The count is arithmetic over the document's own text: each connection contributes the
208//! `first:` it asks for, counts **multiply** down a nested path and **sum** across sibling
209//! paths. Those are [GitHub's published rules][node-limits] and this workspace does not
210//! restate them — `github-graphql-node-count` implements them, and
211//! [`worst_case_node_count`] under [`largest_page_sizes`] is where every node count here
212//! comes from. `every_document_this_source_sends_stays_under_githubs_node_limit`, in
213//! `tests/node_count.rs`, recomputes every document in [`graphql::DOCUMENTS`] from that
214//! same text on every run and fails naming any that reaches the limit — so a connection
215//! added to a shared fragment is caught there rather than by GitHub.
216//!
217//! What decides those counts is the page sizes: [`MAX_PAGE_SIZE`] on the outer page,
218//! `NESTED_PAGE_SIZE` on the connections hanging off one item, and
219//! `BOARD_ITEMS_PAGE_SIZE` on the page of an issue's board memberships a read carries.
220//! `$nestedFirst` is spent twice down one path of a board read, so that constant is
221//! effectively squared there, which is why it is the one the limit is most sensitive to.
222//! `BOARD_ITEMS_PAGE_SIZE` is small for a reason of its own, recorded beside it: what a
223//! page of memberships misses is recovered by one further read rather than refused, so it
224//! buys a bound every read pays for at the price of a request only a multi-board issue
225//! pays.
226//!
227//! **`nodeCount` and `cost` are two numbers against two limits, and both are computed
228//! offline here — per document, one document at a time.** `nodeCount` is the one above: the
229//! most nodes one query may return, checked per query and bounded by [`NODE_COUNT_LIMIT`].
230//! `cost` is rate-limit points, metered per hour across everything one credential does; it
231//! is what the two limiters [`Limiter`] tells apart meter, and a document under
232//! [`NODE_COUNT_LIMIT`] still says nothing about its price. [`worst_case_point_cost`] is
233//! that second number, and `tests/point_cost.rs` pins every document in
234//! [`graphql::DOCUMENTS`] at what it costs — there being no per-call point ceiling to hold
235//! one under, the pin itself is the check. The credentialed lane reconciles both figures
236//! against GitHub's own, off a probe it already sends.
237//!
238//! **What is pinned that way is a per-document price and never a session's.** The record in
239//! `session-cost.md` measures the two quantities a whole session can be counted in offline —
240//! **requests** and **worst-case nodes** — and neither is points. What one whole session
241//! consumes of the hourly point allowance is observable only from a credentialed run's own
242//! `x-ratelimit-*` headers, which is what [`accounting`] fills its per-budget figures from
243//! and what `tests/live.rs` prints at the end of every run.
244//!
245//! [node-limits]: https://docs.github.com/en/graphql/overview/rate-limits-and-node-limits-for-the-graphql-api
246//!
247//! **Where a read-after-write guarantee comes from, since a search index cannot supply
248//! one.** GitHub's issue search is eventually consistent and answers a write made moments
249//! ago with the value from before it. Resolving a node id is not, so a read by id and a
250//! project's own sub-issues are already current. What closes the gap for the search is
251//! [`GitHubProjectsSource::created`]: every read this source answers is completed with
252//! what this process itself wrote, so an item created seconds ago is reported whether or
253//! not GitHub's index has caught up. Nothing else is remembered, nothing is written down,
254//! and the record dies with the process.
255//!
256//! Filtering happens before paging, so a page of a filtered result is a page of the
257//! survivors rather than the survivors of a page. Label and text matching answer the same
258//! question the same way the local Markdown source's do, so one cross-source expectation
259//! holds for both.
260//!
261//! <!-- llmlint: ignore[contracts_have_one_source_or_a_drift_gate] The declaration itself
262//! has one source, `capabilities`, and the note above is the reasoning behind it rather
263//! than a second copy of it: without the three facts recorded here a reader takes the
264//! uniform `Native` for a lie and reverts it. The drift gate on the declaration is this
265//! crate's own capabilities test, which pins every field of it against a fully spelled-out
266//! `Capabilities` literal — a struct with no `Default`, so a field added to the contract
267//! fails to compile there rather than going unasserted. -->
268//! The fixture-server tests above run wherever this crate is selected; the credentialed
269//! lane runs in the same required check, beside them, and can fail it — it verifies the
270//! 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,
271//! one filed under neither, a label on one of the three and a closed status on another —
272//! because that shape is what tells an honoured predicate from an ignored one: a board
273//! holding a single project answers a project filter the same way whether or not this
274//! source applies it, which is exactly how the defect above went unseen.
275//!
276//! That lane writes only to the board `GH_PROJECTS_OWNER` and `GH_PROJECTS_NUMBER` name,
277//! and only into the repository `GH_PROJECTS_REPOSITORY` names, and skips — as it does
278//! without `GH_PROJECTS_TOKEN` — when any of them is absent. Requiring both to be
279//! nominated is what keeps a credentialed write lane off a board and a repository nobody
280//! nominated; it never asks GitHub which project was updated most recently. Before it
281//! starts, the lane also clears any item titled — and any repository label named — the way
282//! it titles and names its own artifacts, which is self-healing after an interrupted run:
283//! a process killed between its writes and its cleanup leaves artifacts the next run
284//! removes.
285//!
286//! # What a session of requests costs, and where the report is
287//!
288//! This source records **every** request it sends into [`accounting::Accounting`], at
289//! `send_once` — the one place a request leaves this crate, which is why a read path added
290//! later is counted without anybody remembering to count it. That is the whole of what this
291//! crate adds to the arrangement; [`accounting`] is where what a record carries, how a
292//! session's spend is arrived at, and what it deliberately does not know are set out.
293//!
294//! What one whole session of the live journey costs, counted that way against this crate's
295//! loopback fixture board, is written down in `session-cost.md` beside this crate — with the
296//! reduction it came out of, and with what it does and does not say about rate-limit points.
297//!
298//! [`GitHubProjectsSource::accounting`] is the read: a snapshot to hold and compare, which
299//! [`accounting::Session::report`] renders the session report from. It is on the ordinary
300//! code path — no environment variable, no feature, no build configuration — because an
301//! instrument nobody switches on measures nothing, and
302//! [`Plugin::build_recording_into`] is how a caller making its own calls beside this
303//! source's counts the whole session rather than this source's share. The credentialed lane
304//! in `tests/live.rs` does exactly that, and prints the report at the end of every run,
305//! passed or failed.
306//!
307//! **A live session refuses to start unless the account can afford it.** Before it does any
308//! of the work it exists to do, the journey makes one request — `GET /rate_limit`, which
309//! GitHub documents as not counting against the REST rate limit and which answers both of
310//! its budgets at once — and starts only if, for each of them, what remains minus this
311//! session's estimated cost is still at least
312//! `onetaskgraph_live::RETAINED_BUFFER` — twenty per cent — of that budget's whole
313//! allowance. A session that cannot **declines**: it did not run, so it is
314//! neither a pass nor a failing assertion, and it says which budget was short, that budget's
315//! limit, what remained, the estimate, the buffer and when it resets — then stops, without
316//! waiting for the budget to come back. The estimate is derived offline from
317//! `tests/fixtures/session-cost.txt` and a cost model stated in `tests/journey/budget.rs`,
318//! which is also where the published rule that model rests on is cited; the accounting
319//! above records the gate's own read like any other request, and
320//! [`accounting::Session::report`] prints the estimate beside what the session really spent.
321//!
322//! **GitHub is the authority on both of its own numbers, and the credentialed lane goes and
323//! asks it.** Everything above computes `nodeCount` and `cost` offline from a document's own
324//! text, which is what lets it run on every platform and on a pull request from a fork with
325//! no credential — and that is what actually stops a regression merging. But an offline
326//! arithmetic can only ever agree with itself: if GitHub changes its rules, this workspace
327//! goes on computing the old answer and nothing notices. So `tests/live.rs` reconciles them.
328//! GitHub's schema exposes `rateLimit(dryRun: true)`, whose `nodeCount` is *"the maximum
329//! number of nodes this query may return"* and whose `cost` is what that document would
330//! spend, both for a document **without executing it**, and the lane asks it for every query
331//! document this source sends, under the largest bindings this source sends, and fails when
332//! GitHub's figure and [`worst_case_node_count`] or [`worst_case_point_cost`] disagree. A
333//! mutation is skipped, because `rateLimit` is a field of `Query` and cannot be asked about
334//! one; the offline pins still cover it. It records what those calls reported about the
335//! account's own allowance, because whether asking is free is a thing to observe rather than
336//! to assume. Two quantities, not one: [`NODE_COUNT_LIMIT`] bounds `nodeCount` per query,
337//! and `cost` is metered against an hourly allowance the accounting above reads off a
338//! credentialed run's own response headers.
339//!
340//! **GitHub has two rate limiters and this source is refused by both, so nothing here
341//! treats them as one thing.** The primary budget is the hourly allowance `gh api
342//! rate_limit` reports; the secondary limiter is a burst limiter over content-generating
343//! requests, and *nothing* reports it. Which one refused decides the operator's next step,
344//! so [`Limiter`] is a type rather than a detail, and it is what [`MIN_MUTATION_INTERVAL_MS`],
345//! [`GitHubProjectsSource::board_cache`] and [`GitHubProjectsSource::graphql`] each answer
346//! one part of.
347#![deny(missing_docs)]
348
349use std::collections::BTreeMap;
350use std::sync::{Arc, Mutex};
351use std::time::{Duration, Instant};
352
353use chrono::{DateTime, Utc};
354use onetaskgraph_plugin_api::{
355    Capabilities, Cursor, DependencyEdge, DependencyEndpoint, DependencyKind, DependencySupport,
356    Direction, Document, DocumentQuery, Health, ItemKind, ItemWrite, Label, LabelFilter, Location,
357    NativeId, Page, PageRequest, Project, ProjectFilter, ProjectQuery, Repository, SecretResolver,
358    SourceError, SourceName, SourcePlugin, Status, StatusCategory, Support, Task, TaskQuery,
359    TaskSource, TextFields, TextQuery, WriteSupport,
360};
361use reqwest::{Client, StatusCode, Url};
362use schemars::{Schema, schema_for};
363use secrecy::{ExposeSecret, SecretString};
364use serde::Deserialize;
365use serde_json::{Value, json};
366
367pub mod accounting;
368
369use accounting::Accounting;
370
371/// The registry name for this plugin.
372pub const KIND: &str = "github-projects";
373/// GitHub's maximum connection page size.
374pub const MAX_PAGE_SIZE: u32 = 100;
375
376/// The most nodes any one document this source sends may be asked to return.
377///
378/// GitHub's own published per-query ceiling, taken from
379/// [`github_graphql_node_count::NODE_LIMIT`] rather than written out again here, so this
380/// workspace cannot hold a stale copy of somebody else's number. A query above it is
381/// **refused before it is executed**, whoever is asking and whatever board they are
382/// asking about — so this is a bound on the documents rather than a budget that runs out.
383///
384/// This is `nodeCount`, the maximum number of nodes *one query may return*. It is not
385/// `cost`, the rate-limit points a call spends against an hourly allowance shared by
386/// everything the credential does — two numbers against two limits, and this constant
387/// bounds only the first. The second is computed offline too, per document:
388/// [`worst_case_point_cost`], pinned for every document in [`graphql::DOCUMENTS`] by
389/// `tests/point_cost.rs`, and reconciled against GitHub's own `cost` by the credentialed
390/// lane. There is no constant like this one to hold a price under, because points are an
391/// hourly allowance rather than a per-call bound.
392///
393/// Neither is a session's price. What `session-cost.md` records of a whole session is its
394/// **requests** and its **worst-case nodes**; what a whole session spends in points is
395/// reported only by a credentialed run's own `x-ratelimit-*` headers, through
396/// [`accounting`]. The module section on the three ways this source reaches an item says how
397/// the count is arrived at, and which of the page sizes below decide it.
398pub const NODE_COUNT_LIMIT: u64 = github_graphql_node_count::NODE_LIMIT;
399
400/// Nested connection size for the connections that hang off one item.
401///
402/// It multiplies through every document that reaches an item under a page — the count
403/// rules multiply down a nested path — so it is the constant [`NODE_COUNT_LIMIT`] is most
404/// sensitive to. `tests/node_count.rs` is what holds the pair together: it recomputes
405/// every document under these constants and fails naming any that reaches the limit, so
406/// raising this is caught there rather than by GitHub.
407const NESTED_PAGE_SIZE: u32 = 50;
408/// How many of one issue's board memberships are read when an issue is reached directly.
409///
410/// An issue reached through a search or through its own node id carries its board half in
411/// `Issue.projectItems`, and only the entry for *this* board is read. This connection sits
412/// under a page of issues, so every point of it multiplies through the whole document and
413/// is paid for whether or not any issue is on a second board — which is why it is
414/// deliberately far smaller than [`NESTED_PAGE_SIZE`].
415///
416/// **Three, because what a page misses is now recovered rather than refused**, and the
417/// recovery is what the value is chosen against. An issue whose entry for this board sits
418/// past this page costs one further request — [`graphql::ISSUE_BOARD_ITEMS`], resumed from
419/// that page's own cursor — so the value trades a bound every read pays for a request only
420/// a multi-board issue pays. At one, a deployment whose issues commonly sit on two or more
421/// boards would pay that request *per issue*, which is order N against the one page per
422/// hundred issues a read costs today. At three it is only reached by an issue on four or
423/// more boards at once, which keeps the recovery path exceptional rather than routine for
424/// a plausible deployment.
425const BOARD_ITEMS_PAGE_SIZE: u32 = 3;
426
427pub use github_graphql_node_count::{NodeCountError, Variables};
428
429/// The largest value this source can bind to each page-size variable its documents name.
430///
431/// Every `first:` in [`graphql`] reads one of these three, and each is capped at the
432/// constant above it wherever a caller's own limit could reach it — `$first` at
433/// [`MAX_PAGE_SIZE`], `$nestedFirst` at `NESTED_PAGE_SIZE`, `$boardItems` at
434/// `BOARD_ITEMS_PAGE_SIZE`. So this is the worst case a caller can drive this source to,
435/// not one configuration of it, which is what makes a bound computed under it a bound on
436/// every read.
437pub fn largest_page_sizes() -> Variables {
438    Variables::from([
439        ("first".to_owned(), MAX_PAGE_SIZE),
440        ("nestedFirst".to_owned(), NESTED_PAGE_SIZE),
441        ("boardItems".to_owned(), BOARD_ITEMS_PAGE_SIZE),
442    ])
443}
444
445/// The most nodes `document` could be asked to return, by GitHub's published rules.
446///
447/// Computed offline from the document's own text under [`largest_page_sizes`] — no
448/// network, no credential and no schema — by
449/// [`github_graphql_node_count::node_count`], which is where the rules themselves live.
450/// A document at or above [`NODE_COUNT_LIMIT`] is one GitHub refuses before executing, so
451/// this is what a check holds every document in [`graphql::DOCUMENTS`] below.
452///
453/// # Errors
454///
455/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
456/// no single operation, or binds a page size this source does not name — each of which is
457/// a defect in the document rather than a number.
458pub fn worst_case_node_count(document: &str) -> Result<u64, NodeCountError> {
459    node_count(document, &largest_page_sizes())
460}
461
462/// The most rate-limit points one call of `document` could spend, by GitHub's published
463/// rules.
464///
465/// Computed offline from the document's own text under [`largest_page_sizes`] — no
466/// network, no credential and no schema — by
467/// [`github_graphql_node_count::point_cost`], which is where the rules themselves live.
468/// This is `cost`, metered **per hour** against the allowance one credential shares across
469/// everything it does; it is not `nodeCount`, which is [`worst_case_node_count`] and is
470/// bounded per query by [`NODE_COUNT_LIMIT`]. There is no per-call ceiling to hold this
471/// under, so what `tests/point_cost.rs` does with it is pin every document in
472/// [`graphql::DOCUMENTS`] at what it costs, and the credentialed lane reconciles those
473/// figures against GitHub's own reported `cost`.
474///
475/// # Errors
476///
477/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
478/// no single operation, or binds a page size this source does not name — each of which is
479/// a defect in the document rather than a number.
480pub fn worst_case_point_cost(document: &str) -> Result<u64, NodeCountError> {
481    github_graphql_node_count::point_cost(document, &largest_page_sizes())
482}
483
484/// The most nodes `document` could be asked to return under `variables`.
485///
486/// [`worst_case_node_count`] is this under [`largest_page_sizes`], and the accounting in
487/// [`accounting`] is this under the bindings one request really sent — one spelling of the
488/// calculation, so a bound checked offline and a cost recorded at run time cannot come to
489/// disagree. The rules themselves live in [`github_graphql_node_count::node_count`].
490///
491/// # Errors
492///
493/// Returns the calculation's own [`NodeCountError`] when `document` does not parse, holds
494/// no single operation, or binds a page size `variables` does not name.
495pub fn node_count(document: &str, variables: &Variables) -> Result<u64, NodeCountError> {
496    github_graphql_node_count::node_count(document, variables)
497}
498
499/// The issue-title prefix that makes a board issue a document.
500///
501/// A GitHub Projects board has no document type — it holds issues — so the discriminator
502/// is the title, and this is the whole of it: an issue whose title begins with these bytes
503/// is a document and every other issue is the task or project the sub-issue rule makes it.
504///
505/// It is spelled **once**, here, and read rather than restated everywhere else — including
506/// by the shared journeys, which take it from this constant so a board fixture cannot
507/// drift from what this source reads. `docs/metadata.md` records the two consequences that
508/// are not obvious from the bytes: the reported title has this prefix taken off, exactly
509/// as the body's metadata slot is taken off `content`, and this prefix is read *before*
510/// the sub-issue rule, so a design issue with no sub-issues is never an empty project.
511pub const DESIGN_TITLE_PREFIX: &str = "DESIGN: ";
512
513/// Exact GraphQL query documents issued by this plugin.
514///
515/// Keeping the production documents here lets the pinned-schema test validate the same
516/// bytes that are sent to GitHub, rather than a test-only copy which could drift
517/// independently. No document in this module writes the board itself, and none of them
518/// names `updateProjectV2Field`.
519pub mod graphql {
520    /// The board half of one item: the field values every document here reads it from.
521    ///
522    /// A macro for the same reason [`board_issue!`] below is one, a level further in. This
523    /// selection is needed by that fragment, by [`BOARD`] under the board's own `items`,
524    /// and by [`ISSUE_BOARD_ITEMS`] under a membership walk — and all three have to produce
525    /// *the same value*, because
526    /// [`GitHubProjectsSource::resolve`](super::GitHubProjectsSource) reads them through
527    /// one path. Three spellings of it is what would drift, so there is one.
528    ///
529    /// The `Status` option and this source's own origin text field are the whole of it. It
530    /// selects no `ProjectV2ItemFieldLabelValue`: GitHub derives that field from the item's
531    /// content, so it holds nothing the content's own `labels` do not already say, and it
532    /// would sit a label connection two page sizes deep.
533    macro_rules! board_item_values {
534        () => {
535            r#"fieldValues(first:$nestedFirst){nodes{
536          ... on ProjectV2ItemFieldSingleSelectValue{name field{
537            ... on ProjectV2SingleSelectField{id name options{id name}}
538          }}
539          ... on ProjectV2ItemFieldTextValue{text field{... on ProjectV2Field{id name}}}
540        }pageInfo{hasNextPage}}"#
541        };
542    }
543
544    /// Everything this source reads about one issue, wherever it reaches that issue.
545    ///
546    /// A macro rather than a constant so the three documents below can `concat!` it: one
547    /// spelling of these fields is what makes an issue read through the board-scoped
548    /// search, through its own node id, and through its project's sub-issue relationship
549    /// resolve to *the same* item, which is the whole of what
550    /// [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource) relies on.
551    ///
552    /// `projectItems` is what carries the board half of an issue: the board item's own id
553    /// and the [`board_item_values!`] above — the `Status` option and this source's origin
554    /// text field — that a `ProjectV2.items` read used to carry. It is asked for on the
555    /// issue rather than on the board, which is what makes the cost of a read proportional
556    /// to what was asked for instead of to the board's size.
557    ///
558    /// It carries a *page* of that connection, at `BOARD_ITEMS_PAGE_SIZE`, and its
559    /// `endCursor` is what [`ISSUE_BOARD_ITEMS`] resumes from when this board's entry is
560    /// not on that page: a page here is where the search for the entry starts rather than
561    /// where it ends.
562    ///
563    /// It does **not** select the board's `Labels` field value, and that is the whole of
564    /// what keeps the three documents below under [`NODE_COUNT_LIMIT`](super::NODE_COUNT_LIMIT):
565    /// a label connection there sits under `fieldValues` under `projectItems` under a page
566    /// of issues, spending `$nestedFirst` twice down one path, and took
567    /// [`SEARCH_ISSUES`] and [`SUB_ISSUES`] to 2,556,100 nodes against a limit of 500,000.
568    /// No label is lost — this is a fragment `on Issue`, whose own `labels` are selected
569    /// above, and that connection is where every label this source reports comes from. No
570    /// document in this module selects the board field any longer, [`BOARD`] included; the
571    /// module documentation records why nothing it could have held is lost.
572    macro_rules! board_issue {
573        () => {
574            concat!(
575                r#" fragment BoardIssue on Issue{__typename id title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total}
576      labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}
577      projectItems(first:$boardItems){nodes{id project{number}
578        "#,
579                board_item_values!(),
580                r#"}pageInfo{hasNextPage endCursor}}}"#
581            )
582        };
583    }
584
585    /// Every issue of one board, found by a search scoped to that board.
586    ///
587    /// This is how the projects a board holds are listed, and it selects no `items`
588    /// connection on `ProjectV2`: the board is a *qualifier of the search* rather than a
589    /// container walked page by page, so nothing nested inside a board item is paid for.
590    /// Which of the issues it returns is a project is then read off `parent` — GitHub
591    /// accepts `-has:parent` as a search qualifier and silently ignores it, so the
592    /// discriminator has to be applied to the field, which is a scalar on the issue and
593    /// costs nothing.
594    pub const SEARCH_ISSUES: &str = concat!(
595        r#"query($search:String!,$type:SearchType!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
596      search(query:$search,type:$type,first:$first,after:$after){
597        pageInfo{hasNextPage endCursor}
598        nodes{__typename ...BoardIssue}
599      }
600    }"#,
601        board_issue!()
602    );
603
604    /// One issue by its own node id, which is what a qualified id names here.
605    ///
606    /// Strongly consistent, unlike the search above: GitHub's issue search is an index and
607    /// answers a write made moments ago with the value from before it, and resolving a node
608    /// id does not.
609    pub const ISSUE: &str = concat!(
610        r#"query($id:ID!,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
611      node(id:$id){__typename ...BoardIssue}
612    }"#,
613        board_issue!()
614    );
615
616    /// One project's tasks: the sub-issues of the issue that project is.
617    ///
618    /// The work this costs is the project's own size. Nothing about it grows as the board
619    /// gains projects, or as those projects gain tasks.
620    pub const SUB_ISSUES: &str = concat!(
621        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!,$boardItems:Int!,$duplicates:Boolean!){
622      node(id:$id){__typename
623        ... on Issue{subIssues(first:$first,after:$after){
624          pageInfo{hasNextPage endCursor}
625          nodes{__typename ...BoardIssue}
626        }}}
627    }"#,
628        board_issue!()
629    );
630
631    /// Reads the board's fields and one page of its items.
632    pub const BOARD: &str = concat!(
633        r#"query($owner:String!,$number:Int!,$first:Int!,$after:String,$nestedFirst:Int!,$duplicates:Boolean!){
634      owner:repositoryOwner(login:$owner){
635        ... on ProjectV2Owner{projectV2(number:$number){...Board}}
636      }
637    } fragment Board on ProjectV2 { id title
638      fields(first:$nestedFirst){nodes{
639        ... on ProjectV2SingleSelectField{__typename id name options{id name}}
640        ... on ProjectV2Field{__typename id name}
641      }pageInfo{hasNextPage}}
642      items(first:$first,after:$after){nodes{id "#,
643        board_item_values!(),
644        r#" content{
645        ... on Issue{__typename id title body url createdAt updatedAt state stateReason(enableDuplicate:$duplicates) repository{nameWithOwner} parent{id} subIssuesSummary{total} labels(first:$nestedFirst){nodes{id name color}pageInfo{hasNextPage}}}
646        ... on PullRequest{__typename id}
647        ... on DraftIssue{__typename id title body createdAt updatedAt}
648      }} pageInfo{hasNextPage endCursor}}
649    }"#
650    );
651
652    /// One issue's board memberships alone, walked past the page a read of it carried.
653    ///
654    /// The recovery read behind [`GitHubProjectsSource::resolve_issue`](super::GitHubProjectsSource):
655    /// every document above carries a *page* of `Issue.projectItems`, and an issue on more
656    /// boards than that page holds may have this board's entry past its end. This asks that
657    /// one issue for its memberships and nothing else — the caller already holds the issue —
658    /// so an answer of "this board does not hold it" is only ever given about a connection
659    /// read to exhaustion.
660    ///
661    /// It selects the board item's id, its project number and the same
662    /// [`board_item_values!`] the fragment does, because what it produces is handed to the
663    /// very same resolver: an issue recovered this way reports the same title, the same
664    /// status, the same labels and the same qualified id as one whose entry was on the
665    /// page.
666    ///
667    /// `$first` rather than `$boardItems`: this document reads one issue, so nothing
668    /// multiplies through it and the membership connection can be walked at
669    /// [`MAX_PAGE_SIZE`](super::MAX_PAGE_SIZE) — which is what keeps the recovery to one
670    /// further request for any issue a person really keeps.
671    pub const ISSUE_BOARD_ITEMS: &str = concat!(
672        r#"query($id:ID!,$first:Int!,$after:String,$nestedFirst:Int!){
673      node(id:$id){
674        ... on Issue{projectItems(first:$first,after:$after){
675          nodes{id project{number}
676        "#,
677        board_item_values!(),
678        r#"}
679          pageInfo{hasNextPage endCursor}}}
680      }
681    }"#
682    );
683    /// Resolves the configured repository's node id, which creating an issue requires.
684    pub const REPOSITORY: &str = r#"query($owner:String!,$name:String!){repository(owner:$owner,name:$name){id nameWithOwner}}"#;
685    /// Reads both dependency directions for one issue, with each far end's own kind.
686    pub const ISSUE_DEPENDENCIES: &str = r#"query($id:ID!,$first:Int!,$after:String){node(id:$id){__typename
687      ... on Issue{
688        blockedBy(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
689        blocking(first:$first,after:$after){nodes{...Related}pageInfo{hasNextPage endCursor}}
690      }}} fragment Related on Issue{id title body parent{id} subIssuesSummary{total}}"#;
691    /// Creates one issue in the configured repository.
692    pub const CREATE_ISSUE: &str =
693        r#"mutation($input:CreateIssueInput!){createIssue(input:$input){issue{id url}}}"#;
694    /// Puts an existing issue on the configured board.
695    pub const ADD_TO_BOARD: &str = r#"mutation($input:AddProjectV2ItemByIdInput!){addProjectV2ItemById(input:$input){item{id}}}"#;
696    /// Updates an issue's visible fields and its open or closed state in one call.
697    pub const UPDATE_ISSUE: &str =
698        r#"mutation($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}"#;
699    /// Updates an existing draft's user-visible fields.
700    pub const UPDATE_DRAFT: &str = r#"mutation($input:UpdateProjectV2DraftIssueInput!){updateProjectV2DraftIssue(input:$input){draftIssue{id}}}"#;
701    /// Updates a text or single-select value on one project item.
702    pub const UPDATE_FIELD: &str = r#"mutation($input:UpdateProjectV2ItemFieldValueInput!){updateProjectV2ItemFieldValue(input:$input){projectV2Item{id}}}"#;
703    /// Files one issue under another as a sub-issue, which is what project membership is.
704    pub const ADD_SUB_ISSUE: &str =
705        r#"mutation($input:AddSubIssueInput!){addSubIssue(input:$input){issue{id} subIssue{id}}}"#;
706    /// Takes one issue back out of its parent.
707    pub const REMOVE_SUB_ISSUE: &str = r#"mutation($input:RemoveSubIssueInput!){removeSubIssue(input:$input){issue{id} subIssue{id}}}"#;
708    /// Adds GitHub's native issue blocked-by relationship.
709    pub const ADD_BLOCKED_BY: &str = r#"mutation($input:AddBlockedByInput!){addBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
710    /// Removes one native issue blocked-by relationship.
711    pub const REMOVE_BLOCKED_BY: &str = r#"mutation($input:RemoveBlockedByInput!){removeBlockedBy(input:$input){issue{id} blockingIssue{id}}}"#;
712    /// Deletes one issue, which takes its board item with it.
713    ///
714    /// The engine sends this in one situation only: undoing a copy that could not finish,
715    /// over the items that same copy created. Deleting the issue removes the board item
716    /// too, so there is no second `deleteProjectV2Item` to keep in step with it.
717    pub const DELETE_ISSUE: &str =
718        r#"mutation($input:DeleteIssueInput!){deleteIssue(input:$input){repository{id}}}"#;
719
720    /// Every document above, with what this source is doing when it sends one.
721    ///
722    /// One list rather than a `match` beside the constants: a rate-limit diagnostic has to
723    /// name the call that was refused, and a `match` with a catch-all arm would answer a
724    /// document added later with "talking to GitHub" and never say so.
725    ///
726    /// `documents_are_all_inventoried` reads this file back and fails naming any `pub
727    /// const` here that this list omits, so the two cannot part — which is the same guard
728    /// `CATEGORIES` carries, in the one shape available to a set of `&str` constants.
729    pub const DOCUMENTS: [(&str, &str); 17] = [
730        (SEARCH_ISSUES, "searching this board's issues"),
731        (ISSUE, "reading one issue"),
732        (
733            ISSUE_BOARD_ITEMS,
734            "reading one issue's board memberships past the page it came with",
735        ),
736        (SUB_ISSUES, "reading a project's tasks"),
737        (BOARD, "reading the board"),
738        (REPOSITORY, "reading the destination repository"),
739        (ISSUE_DEPENDENCIES, "reading an issue's dependencies"),
740        (CREATE_ISSUE, "creating an issue"),
741        (ADD_TO_BOARD, "adding an issue to the board"),
742        (UPDATE_ISSUE, "updating an issue"),
743        (UPDATE_DRAFT, "updating a draft item"),
744        (UPDATE_FIELD, "writing a board field"),
745        (ADD_SUB_ISSUE, "filing an issue under its project"),
746        (REMOVE_SUB_ISSUE, "taking an issue out of its project"),
747        (ADD_BLOCKED_BY, "recording a dependency"),
748        (REMOVE_BLOCKED_BY, "removing a dependency"),
749        (DELETE_ISSUE, "deleting an issue"),
750    ];
751}
752
753/// Which of GitHub's two rate limiters refused a request.
754///
755/// Waiting is the whole answer to the primary budget, and polling is what *extends* the
756/// secondary one — so an operator told the wrong one takes the wrong next step, which is
757/// the whole reason this is carried rather than collapsed into "rate limited".
758#[derive(Debug, Clone, Copy, PartialEq, Eq)]
759enum Limiter {
760    /// The hourly API budget, which `gh api rate_limit` reports and a wait answers.
761    Primary,
762    /// The burst limiter over content-generating requests, which nothing reports.
763    Secondary,
764}
765
766/// The wordings GitHub answers a secondary rate limit with.
767///
768/// It sends them under a forbidden status, under a too-many-requests status, and inside
769/// the `errors` of a *successful* response, which is why the text is what this matches on
770/// rather than the status. `abuse detection` is the wording GitHub used before the
771/// limiter was renamed and still returns from some endpoints; `submitted too quickly` is
772/// what a burst of content creation is refused with.
773///
774/// This is GitHub's vocabulary rather than this source's, so it is pinned rather than
775/// remembered: `tests/fixtures/rate-limits.json` records where each wording was read and
776/// when, and the drift gate reconciles the two lists both ways. Public for that gate
777/// alone — a caller has no use for it, and matching on a refusal is this source's job.
778pub const SECONDARY_WORDINGS: [&str; 5] = [
779    "secondary rate limit",
780    "temporarily blocked from content creation",
781    "abuse detection",
782    "submitted too quickly",
783    "exceeded a secondary",
784];
785
786/// The wordings GitHub answers an exhausted primary budget with.
787///
788/// `rate_limited` is the `type` its GraphQL error carries, which is read as a field rather
789/// than looked for in the response text. Pinned and gated exactly as
790/// [`SECONDARY_WORDINGS`] is, and public for the same one reason.
791pub const PRIMARY_WORDINGS: [&str; 3] = [
792    "api rate limit exceeded",
793    "rate limit exceeded",
794    "rate_limited",
795];
796
797/// What a response *says about itself*, which is the only place a refusal can be read.
798///
799/// Deliberately not the whole response body. A board is a place people write about their
800/// own work, and a task on it titled "the secondary rate limit" would, matched across the
801/// raw text, turn a perfectly good answer into a refusal this source then waited out and
802/// reported. So the item data is never read: what is read is GitHub's own REST-style
803/// `message` envelope, which is what a forbidden status carries, and the `message` and
804/// `type` of each GraphQL error, which is where a *successful* response says it.
805///
806/// A body that is not JSON at all has nothing structured to read, so only a failing
807/// response's own text is taken — a successful response that is not JSON is malformed
808/// rather than refused, and [`GitHubProjectsSource::answer`] says so.
809fn refusal_wording(status: StatusCode, body: &str) -> String {
810    let Ok(parsed) = serde_json::from_str::<Value>(body) else {
811        return if status.is_success() {
812            String::new()
813        } else {
814            body.to_owned()
815        };
816    };
817    let mut said: Vec<&str> = parsed
818        .get("message")
819        .and_then(Value::as_str)
820        .into_iter()
821        .collect();
822    if let Some(errors) = parsed.get("errors").and_then(Value::as_array) {
823        for error in errors {
824            said.extend(
825                ["message", "type"]
826                    .into_iter()
827                    .filter_map(|key| error.get(key).and_then(Value::as_str)),
828            );
829        }
830    }
831    said.join("; ")
832}
833
834impl Limiter {
835    /// Which limiter refused this response, or `None` when none of them did.
836    ///
837    /// The wording is read first and the status only decides what carries none of it,
838    /// because GitHub answers a secondary limit with a forbidden status far more often
839    /// than with too-many-requests — while a forbidden status saying nothing about a limit
840    /// really is a credential this token lacks.
841    ///
842    /// A response is a refusal because of its status or its own wording. A spent budget
843    /// only ever explains one; it never turns an answer into a refusal.
844    fn classify(status: StatusCode, budget_exhausted: bool, body: &str) -> Option<Self> {
845        let normalized = refusal_wording(status, body).to_ascii_lowercase();
846        if SECONDARY_WORDINGS
847            .iter()
848            .any(|wording| normalized.contains(wording))
849        {
850            return Some(Self::Secondary);
851        }
852        if status == StatusCode::TOO_MANY_REQUESTS {
853            return Some(Self::Primary);
854        }
855        // An exhausted budget *explains* a response that failed; it does not make one that
856        // succeeded into a failure. GitHub sets `x-ratelimit-remaining: 0` on the last
857        // request the budget allowed as well as on the ones it then refuses, so reading
858        // the header alone threw away a good answer — and, once refusals were retried,
859        // replayed a request that had already taken effect.
860        if !status.is_success() && budget_exhausted {
861            return Some(Self::Primary);
862        }
863        // A successful response saying it: GitHub reports a GraphQL rate limit in the
864        // `errors` of an HTTP 200, where nothing about the status says so at all.
865        if status.is_success()
866            && PRIMARY_WORDINGS
867                .iter()
868                .any(|wording| normalized.contains(wording))
869        {
870            return Some(Self::Primary);
871        }
872        None
873    }
874
875    /// What this limiter is called where an operator can look it up.
876    const fn name(self) -> &'static str {
877        match self {
878            Self::Primary => "GitHub's primary API rate limit",
879            Self::Secondary => "GitHub's secondary rate limit",
880        }
881    }
882
883    /// What the endpoint an operator would go and check says about this limiter.
884    const fn where_to_look(self) -> &'static str {
885        match self {
886            Self::Primary => {
887                "That is the budget `gh api rate_limit` reports, so that endpoint says when it \
888                 comes back."
889            }
890            Self::Secondary => {
891                "That limiter is not the primary API budget: `gh api rate_limit` reports the \
892                 primary budget and does not report this one, so budget showing there says \
893                 nothing about this refusal, and every further attempt extends it."
894            }
895        }
896    }
897
898    /// The next step this limiter actually calls for.
899    const fn what_to_do(self) -> &'static str {
900        match self {
901            Self::Primary => {
902                "wait for the reset `gh api rate_limit` reports, then run the command again."
903            }
904            Self::Secondary => {
905                "leave this board alone for a few minutes, then run the command again — or \
906                 raise pacing.min_mutation_interval_ms on this source so it writes more slowly."
907            }
908        }
909    }
910}
911
912/// One rate-limit refusal, and the wait GitHub asked for if it asked for one.
913#[derive(Debug, Clone, Copy)]
914struct Limited {
915    limiter: Limiter,
916    hint: Option<u64>,
917}
918
919impl Limited {
920    /// What the caller is told once this source has waited as long as it may.
921    ///
922    /// Both limiters report as [`SourceError::RateLimited`], because that is what
923    /// happened: the kind a caller matches on says a rate limit refused this, and nothing
924    /// about *which* limiter it was makes it a different kind of failure. What differs is
925    /// the operator's next step, and that is what the message carries — a secondary
926    /// refusal read as a primary one sends an operator to `gh api rate_limit`, where the
927    /// budget looks fine, and then back to retry the very burst that was refused.
928    fn exhausted(
929        self,
930        doing: &str,
931        waits: u32,
932        waited: Duration,
933        needed: Duration,
934        budget: Duration,
935    ) -> SourceError {
936        SourceError::RateLimited {
937            retry_after_seconds: self.hint,
938            message: Some(format!(
939                "{} refused this source while {doing}; it waited {} out over {} and was refused \
940                 again, and the next wait of {} would take it past the {} one call may spend \
941                 waiting. {} next: {}",
942                self.limiter.name(),
943                plural(waits, "refusal"),
944                seconds(waited),
945                seconds(needed),
946                seconds(budget),
947                self.limiter.where_to_look(),
948                self.limiter.what_to_do(),
949            )),
950        }
951    }
952}
953
954/// One HTTP attempt's result, with what its response said about the rate limit.
955///
956/// The two travel together so the record and the outcome are written from the same place:
957/// what a response said about the budget is only readable while that response is in hand,
958/// and what the attempt *meant* is only decidable once its body has been read.
959struct Attempted {
960    result: Result<Value, Attempt>,
961    limits: accounting::RateLimit,
962    /// GitHub's own reported cost for this call, for a document that asked for it.
963    reported_cost: Option<u64>,
964}
965
966/// One attempt's outcome: an error to report, or a rate limit to wait out.
967enum Attempt {
968    Failed(SourceError),
969    Limited(Limited),
970}
971
972fn plural(count: u32, thing: &str) -> String {
973    if count == 1 {
974        format!("{count} {thing}")
975    } else {
976        format!("{count} {thing}s")
977    }
978}
979
980fn seconds(duration: Duration) -> String {
981    format!("{:.1}s", duration.as_secs_f64())
982}
983
984/// A header GitHub spells as a whole number of seconds, or `None` when this one is not.
985///
986/// A value that is present and unreadable is deliberately *not* an error. `retry-after` is
987/// allowed by HTTP to be a date rather than a count, an intermediary can rewrite either
988/// header, and neither is what makes a response a refusal — so the whole cost of one this
989/// cannot read is that the refusal carries no hint and the backing-off schedule answers it
990/// instead. Refusing the response over the header would turn a readable refusal into an
991/// unreadable one, and refusing to *wait* would be the one wrong direction to fail in.
992fn whole_seconds(value: Option<&reqwest::header::HeaderValue>) -> Option<u64> {
993    value
994        .and_then(|value| value.to_str().ok())
995        .and_then(|value| value.trim().parse::<u64>().ok())
996}
997
998/// Every mutation this source sends creates content — an issue, a board item, a field of
999/// one, a sub-issue link, a dependency — and no query in [`graphql::DOCUMENTS`] does, so
1000/// what the secondary limiter counts and what the keyword says are the same set. That is
1001/// what makes the keyword a sound test rather than a convenient one.
1002fn is_mutation(query: &str) -> bool {
1003    query.trim_start().starts_with("mutation")
1004}
1005
1006/// What this source was doing, for a diagnostic that has to say so.
1007///
1008/// Read out of [`graphql::DOCUMENTS`], which is the inventory rather than a copy of it, so
1009/// a document added without a description is caught by that list's own gate instead of
1010/// falling through to the vague arm below.
1011fn operation_description(query: &str) -> &'static str {
1012    graphql::DOCUMENTS
1013        .iter()
1014        .find(|(document, _)| *document == query)
1015        .map_or("talking to GitHub", |(_, doing)| *doing)
1016}
1017
1018/// GitHub's published ceiling on content-generating requests, per minute.
1019///
1020/// Pinned in `tests/fixtures/rate-limits.json` and gated against it, because it is
1021/// GitHub's number rather than this source's: [`MIN_MUTATION_INTERVAL_MS`] is *derived*
1022/// from it, so a pacing value checked only against itself cannot go stale here.
1023pub const CONTENT_CREATION_PER_MINUTE: u64 = 80;
1024/// The same ceiling as GitHub publishes it per hour, which this source does **not** pace
1025/// at. See [`MIN_MUTATION_INTERVAL_MS`] for why the per-minute bound is the one that
1026/// governs; it is pinned beside its sibling so the gate would notice either one moving.
1027pub const CONTENT_CREATION_PER_HOUR: u64 = 500;
1028/// Shortest interval between two content-creating mutations, in milliseconds.
1029///
1030/// GitHub documents two secondary limits on content-generating requests:
1031/// [`CONTENT_CREATION_PER_MINUTE`] and [`CONTENT_CREATION_PER_HOUR`]. 60000/80 is 750, so
1032/// a mutation every 750 ms is the fastest rate that cannot exceed the per-minute bound,
1033/// and that is the bound a copy actually trips: a copy of one plan-sized project is a
1034/// burst of a few dozen mutations inside a few seconds. The hourly bound works out at one
1035/// every 7.2 seconds sustained, which no single copy reaches and which, used as the
1036/// spacing here, would turn an ordinary copy into an hour of waiting — so it is
1037/// deliberately *not* what this paces at. An installation that wants the hourly bound
1038/// honoured for a long sequence of copies says so through
1039/// `pacing.min_mutation_interval_ms`.
1040pub const MIN_MUTATION_INTERVAL_MS: u64 = 60_000 / CONTENT_CREATION_PER_MINUTE;
1041/// First wait when a rate-limit refusal carries no hint; each further wait doubles it.
1042///
1043/// A doubling schedule from one second reaches a minute in six waits, which is GitHub's
1044/// own advice for a secondary limit — wait, and wait longer each time — without spending
1045/// the first minute of a transient refusal doing nothing.
1046pub const RETRY_BACKOFF_MS: u64 = 1_000;
1047/// Total time one call may spend waiting out rate limits before it reports a failure.
1048///
1049/// Two minutes is long enough to ride out the refusals a paced copy still collects and
1050/// short enough that a command an operator is watching returns. The bound is what makes
1051/// the wait a wait rather than a hang: a call refused past it ends in a diagnostic naming
1052/// the limiter, not in a process nobody can tell from a wedged one.
1053pub const RETRY_BUDGET_MS: u64 = 120_000;
1054
1055fn default_token_env() -> String {
1056    "GH_PROJECTS_TOKEN".to_owned()
1057}
1058fn default_endpoint() -> String {
1059    "https://api.github.com/graphql".to_owned()
1060}
1061
1062/// Where one status category lands on this board.
1063///
1064/// `null` — an absent value — disables the category for this instance, and using a
1065/// disabled status is a refusal naming the status and the instance.
1066#[derive(Debug, Clone, Deserialize, schemars::JsonSchema)]
1067#[serde(untagged)]
1068pub enum StatusTargetConfig {
1069    /// The name of a `Status` single-select option already on the board.
1070    Column(ColumnName),
1071    /// A closed issue state, whose reason is what tells done from cancelled.
1072    Closed {
1073        /// The `IssueClosedStateReason` to close with.
1074        closed: ClosedState,
1075    },
1076}
1077
1078/// The name of a `Status` single-select option on the board.
1079///
1080/// Validated on the way in rather than checked later, so a blank option name — which
1081/// nothing on a board can be — is a state this type cannot hold.
1082#[derive(Debug, Clone, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
1083#[serde(try_from = "String")]
1084pub struct ColumnName(String);
1085
1086impl ColumnName {
1087    /// The option name, as the board spells it.
1088    fn as_str(&self) -> &str {
1089        &self.0
1090    }
1091}
1092
1093impl TryFrom<String> for ColumnName {
1094    type Error = String;
1095
1096    fn try_from(name: String) -> Result<Self, Self::Error> {
1097        if name.trim().is_empty() {
1098            return Err("a status_mapping option name cannot be blank".to_owned());
1099        }
1100        Ok(Self(name))
1101    }
1102}
1103
1104/// The two closed states this product can mean.
1105///
1106/// GitHub's `IssueClosedStateReason` also spells `DUPLICATE`, which is neither finished
1107/// work nor abandoned work, so nothing here ever writes it.
1108#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
1109#[serde(rename_all = "kebab-case")]
1110pub enum ClosedState {
1111    /// `COMPLETED` — precisely done.
1112    Completed,
1113    /// `NOT_PLANNED` — precisely cancelled.
1114    NotPlanned,
1115}
1116
1117impl ClosedState {
1118    const fn reason(self) -> &'static str {
1119        match self {
1120            Self::Completed => "COMPLETED",
1121            Self::NotPlanned => "NOT_PLANNED",
1122        }
1123    }
1124}
1125
1126/// Configuration for one GitHub Projects v2 board.
1127#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1128#[serde(default, deny_unknown_fields)]
1129pub struct GitHubProjectsConfig {
1130    /// Login of the user or organization which owns the board.
1131    pub owner: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates GitHub's owner grammar before private construction.
1132    /// The project number shown in the board's GitHub URL.
1133    pub project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` bounds this to a positive GraphQL Int.
1134    // 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.
1135    /// `owner/name` of the repository this source creates an issue in when the item's own
1136    /// `repositories` field does not decide it.
1137    ///
1138    /// An item naming exactly one repository is created there; a task or a document naming
1139    /// none or several is created in its parent project's repository; and a project, or a
1140    /// task or document with no parent, naming none or several is created here. A board
1141    /// has no repository of its own and `createIssue` requires one, so a write without
1142    /// this is refused naming the field. Reads never need it.
1143    pub repository: Option<String>, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the `owner/name` grammar before private construction.
1144    // llmlint: ignore-end[contracts_have_one_source_or_a_drift_gate]
1145    /// Environment variable containing a fine-grained token with Projects and Issues
1146    /// read/write plus Pull requests read-only access for every repository represented on
1147    /// the board.
1148    #[serde(default = "default_token_env")]
1149    pub token_env: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` validates the environment-variable grammar.
1150    /// GraphQL endpoint. GitHub Enterprise installations may override it.
1151    #[serde(default = "default_endpoint")]
1152    pub endpoint: String, // llmlint: ignore[invalid_states_unrepresentable] Schema DTO; `new` converts it to the private validated `Url`.
1153    /// Per-instance mapping from a status category to where it lands on this board.
1154    ///
1155    /// A category this does not mention keeps its shipped default: `backlog` to
1156    /// "Backlog", `todo` to "Todo", `in-progress` to "In Progress", `done` to closed as
1157    /// completed, `cancelled` to closed as not planned, and `draft` and `unknown`
1158    /// disabled. `unknown` may name one existing board option; every unknown word then
1159    /// lands on that option and reads back as `unknown` under its name. It cannot name a
1160    /// closed state because that reads back as `done` or `cancelled`. Unlike `local-md`,
1161    /// this source cannot keep each unknown word because it never creates board options.
1162    #[serde(default)]
1163    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.
1164    /// How fast this source writes, and how long it waits out a rate-limit refusal.
1165    ///
1166    /// Every field keeps its shipped default when it is absent, and the defaults are
1167    /// GitHub's own published limits rather than taste. See [`Pacing`].
1168    #[serde(default)]
1169    pub pacing: PacingConfig,
1170}
1171
1172/// How fast this source writes, and how long it waits out a rate-limit refusal.
1173///
1174/// Configurable because a GitHub Enterprise installation sets its own limits and an
1175/// operator who has already been refused may want to go slower still — not because the
1176/// defaults are guesses.
1177#[derive(Debug, Clone, Default, Deserialize, schemars::JsonSchema)]
1178#[serde(default, deny_unknown_fields)]
1179pub struct PacingConfig {
1180    /// Shortest interval between two content-creating mutations, in milliseconds.
1181    ///
1182    /// Zero sends them as fast as they are asked for, which is what a fixture server on
1183    /// loopback wants and what no board on github.com does. At most [`MAX_PACING_MS`].
1184    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.
1185    /// First wait when a rate-limit refusal carries no hint, in milliseconds. Each
1186    /// further wait of the same call doubles it. At most [`MAX_PACING_MS`], and never
1187    /// zero while there is a budget to spend, because a schedule of zero-length waits
1188    /// consumes none of it and so never ends.
1189    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.
1190    /// Total time one call may spend waiting out rate limits, in milliseconds.
1191    ///
1192    /// Zero reports the refusal rather than waiting at all. At most [`MAX_PACING_MS`]:
1193    /// the bound is what makes this a wait rather than a hang.
1194    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.
1195}
1196
1197/// The largest any pacing setting may be, in milliseconds.
1198///
1199/// One hour. GitHub's own harshest published bound on content-generating requests works
1200/// out at one every 7.2 seconds, so an hour is already three orders of magnitude past
1201/// anything a real limit asks for, and past it the settings stop describing pacing at all:
1202/// a wait budget beyond it is the unbounded wait this whole mechanism exists to replace,
1203/// and an interval beyond it is a command that never sends its second mutation. It also
1204/// keeps the clock arithmetic in [`GitHubProjectsSource::reserve_mutation_slot`] inside
1205/// what an `Instant` can hold on every platform.
1206pub const MAX_PACING_MS: u64 = 3_600_000;
1207
1208/// [`PacingConfig`] with every default resolved and every value checked, which is what the
1209/// source holds.
1210#[derive(Debug, Clone, Copy)]
1211struct Pacing {
1212    min_mutation_interval: Duration,
1213    retry_backoff: Duration,
1214    retry_budget: Duration,
1215}
1216
1217impl Pacing {
1218    /// Resolve one instance's pacing, refusing a configuration that would not pace at all.
1219    fn resolve(config: PacingConfig, instance: &SourceName) -> Result<Self, SourceError> {
1220        let bounded = |value: Option<u64>, default: u64, field: &str| match value {
1221            Some(value) if value > MAX_PACING_MS => Err(SourceError::Config {
1222                message: format!(
1223                    "pacing.{field} of source {instance} is {value} ms, and the most any pacing \
1224                     setting may be is {MAX_PACING_MS} ms — an hour, which is already far past \
1225                     GitHub's own harshest published limit"
1226                ),
1227            }),
1228            Some(value) => Ok(Duration::from_millis(value)),
1229            None => Ok(Duration::from_millis(default)),
1230        };
1231        let retry_backoff = bounded(
1232            config.retry_backoff_ms,
1233            RETRY_BACKOFF_MS,
1234            "retry_backoff_ms",
1235        )?;
1236        let retry_budget = bounded(config.retry_budget_ms, RETRY_BUDGET_MS, "retry_budget_ms")?;
1237        if retry_backoff.is_zero() && !retry_budget.is_zero() {
1238            return Err(SourceError::Config {
1239                message: format!(
1240                    "pacing.retry_backoff_ms of source {instance} is 0 while \
1241                     pacing.retry_budget_ms is {} ms; a schedule of zero-length waits spends \
1242                     none of that budget, so it would retry a refusal forever. Set a backoff of \
1243                     at least 1 ms, or set retry_budget_ms to 0 to report a refusal without \
1244                     waiting at all",
1245                    retry_budget.as_millis()
1246                ),
1247            });
1248        }
1249        Ok(Self {
1250            min_mutation_interval: bounded(
1251                config.min_mutation_interval_ms,
1252                MIN_MUTATION_INTERVAL_MS,
1253                "min_mutation_interval_ms",
1254            )?,
1255            retry_backoff,
1256            retry_budget,
1257        })
1258    }
1259}
1260
1261/// Factory for [`GitHubProjectsSource`].
1262#[derive(Debug, Clone, Copy, Default)]
1263pub struct Plugin;
1264
1265impl SourcePlugin for Plugin {
1266    fn kind(&self) -> &'static str {
1267        KIND
1268    }
1269    fn config_schema(&self) -> Schema {
1270        schema_for!(GitHubProjectsConfig)
1271    }
1272    fn build(
1273        &self,
1274        name: &SourceName,
1275        config: &Value,
1276        secrets: &dyn SecretResolver,
1277    ) -> Result<Box<dyn TaskSource>, SourceError> {
1278        self.build_recording_into(name, config, secrets, Arc::new(Accounting::new()))
1279    }
1280}
1281
1282impl Plugin {
1283    /// Build a source recording every request it sends into an accounting the caller holds.
1284    ///
1285    /// [`SourcePlugin::build`] is this with an accounting of its own, which is what the
1286    /// registry gets. This is for a caller that is also calling GitHub itself and wants one
1287    /// session total rather than two — see [`accounting`] and
1288    /// [`GitHubProjectsSource::recording_into`].
1289    ///
1290    /// # Errors
1291    ///
1292    /// Exactly [`SourcePlugin::build`]'s, with the same source name in front of each:
1293    /// [`SourceError::Config`] for configuration this plugin cannot use and
1294    /// [`SourceError::Auth`] for a credential it cannot find.
1295    pub fn build_recording_into(
1296        &self,
1297        name: &SourceName,
1298        config: &Value,
1299        secrets: &dyn SecretResolver,
1300        ledger: Arc<Accounting>,
1301    ) -> Result<Box<dyn TaskSource>, SourceError> {
1302        let config: GitHubProjectsConfig =
1303            serde_json::from_value(config.clone()).map_err(|e| SourceError::Config {
1304                message: format!("source {name}: {e}"),
1305            })?;
1306        let source = GitHubProjectsSource::recording_into(name, config, secrets, ledger).map_err(
1307            |error| match error {
1308                SourceError::Config { message } => SourceError::Config {
1309                    message: format!("source {name}: {message}"),
1310                },
1311                SourceError::Auth { message } => SourceError::Auth {
1312                    message: format!("source {name}: {message}"),
1313                },
1314                other => other,
1315            },
1316        )?;
1317        Ok(Box::new(source))
1318    }
1319}
1320
1321/// Where a status category lands on this board, once configuration is resolved.
1322#[derive(Debug, Clone, PartialEq, Eq)]
1323enum StatusTarget {
1324    /// Not usable against this instance.
1325    Disabled,
1326    /// The board's `Status` option of this name.
1327    Column(ColumnName),
1328    /// A closed issue, with the reason that says which closed it means.
1329    Closed(ClosedState),
1330}
1331
1332/// Every status category, in the order the vocabulary declares them.
1333///
1334/// This list mirrors `StatusCategory`, so it carries its own drift gate rather than a
1335/// reviewer's attention: [`category_position`] is a wildcard-free match, so a variant
1336/// added to the shared vocabulary fails to compile until it is named there, and this
1337/// crate's suite reconciles this list against that enum's own derived schema, which is
1338/// generated from the variants rather than written beside them. The schema is what
1339/// catches a list left one short — a list checking only the positions it already holds
1340/// would pass while every mapping indexed by the new position panicked.
1341pub const CATEGORIES: [StatusCategory; 7] = [
1342    StatusCategory::Draft,
1343    StatusCategory::Backlog,
1344    StatusCategory::Todo,
1345    StatusCategory::InProgress,
1346    StatusCategory::Done,
1347    StatusCategory::Cancelled,
1348    StatusCategory::Unknown,
1349];
1350
1351/// Where one category sits in [`CATEGORIES`]; see that list for what this pins.
1352#[must_use]
1353pub const fn category_position(category: StatusCategory) -> usize {
1354    match category {
1355        StatusCategory::Draft => 0,
1356        StatusCategory::Backlog => 1,
1357        StatusCategory::Todo => 2,
1358        StatusCategory::InProgress => 3,
1359        StatusCategory::Done => 4,
1360        StatusCategory::Cancelled => 5,
1361        StatusCategory::Unknown => 6,
1362    }
1363}
1364
1365/// The spelling a status category is configured and reported under.
1366fn category_name(category: StatusCategory) -> &'static str {
1367    match category {
1368        StatusCategory::Draft => "draft",
1369        StatusCategory::Backlog => "backlog",
1370        StatusCategory::Todo => "todo",
1371        StatusCategory::InProgress => "in-progress",
1372        StatusCategory::Done => "done",
1373        StatusCategory::Cancelled => "cancelled",
1374        StatusCategory::Unknown => "unknown",
1375    }
1376}
1377
1378/// A shipped default's option name.
1379///
1380/// The literals below are this file's own and non-blank, and they are validated by the
1381/// one constructor a configured name goes through rather than beside it.
1382fn shipped_column(name: &'static str) -> ColumnName {
1383    ColumnName::try_from(name.to_owned()).expect("a shipped default names a board option")
1384}
1385
1386/// The shipped default for one category, before this instance's configuration.
1387fn shipped_default(category: StatusCategory) -> StatusTarget {
1388    match category {
1389        StatusCategory::Backlog => StatusTarget::Column(shipped_column("Backlog")),
1390        StatusCategory::Todo => StatusTarget::Column(shipped_column("Todo")),
1391        StatusCategory::InProgress => StatusTarget::Column(shipped_column("In Progress")),
1392        StatusCategory::Done => StatusTarget::Closed(ClosedState::Completed),
1393        StatusCategory::Cancelled => StatusTarget::Closed(ClosedState::NotPlanned),
1394        StatusCategory::Draft | StatusCategory::Unknown => StatusTarget::Disabled,
1395    }
1396}
1397
1398/// This instance's complete category-to-target mapping, read in both directions.
1399///
1400/// One target per category, held at that category's own [`category_position`], so a
1401/// category missing from the mapping, named twice in it, or filed out of order is a
1402/// state this type cannot hold rather than one [`Self::target`] has to defend against.
1403#[derive(Debug, Clone)]
1404struct StatusMapping {
1405    targets: [StatusTarget; CATEGORIES.len()],
1406}
1407
1408impl StatusMapping {
1409    fn resolve(
1410        configured: BTreeMap<String, Option<StatusTargetConfig>>,
1411        instance: &SourceName,
1412    ) -> Result<Self, SourceError> {
1413        let mut overrides: BTreeMap<&'static str, Option<StatusTargetConfig>> = BTreeMap::new();
1414        for (key, value) in configured {
1415            let category = CATEGORIES
1416                .iter()
1417                .find(|category| category_name(**category) == key)
1418                .ok_or_else(|| SourceError::Config {
1419                    message: format!(
1420                        "status_mapping names {key:?}, which is not a status category of source \
1421                         {instance}; the categories are {}",
1422                        CATEGORIES
1423                            .iter()
1424                            .map(|category| category_name(*category))
1425                            .collect::<Vec<_>>()
1426                            .join(", ")
1427                    ),
1428                })?;
1429            overrides.insert(category_name(*category), value);
1430        }
1431        if let Some(Some(StatusTargetConfig::Closed { closed })) =
1432            overrides.get(category_name(StatusCategory::Unknown))
1433        {
1434            let read_back = match closed {
1435                ClosedState::Completed => StatusCategory::Done,
1436                ClosedState::NotPlanned => StatusCategory::Cancelled,
1437            };
1438            return Err(SourceError::Config {
1439                message: format!(
1440                    "status_mapping.unknown of source {instance} cannot target the closed state \
1441                     {} because a copy reads that item back as {}; map unknown to one existing \
1442                     board Status option instead",
1443                    closed.reason().to_ascii_lowercase().replace('_', "-"),
1444                    category_name(read_back)
1445                ),
1446            });
1447        }
1448        // `CATEGORIES[position] == category` for every category — the crate's suite
1449        // asserts it — so mapping the list in order fills each category's own slot.
1450        let targets = CATEGORIES.map(|category| match overrides.remove(category_name(category)) {
1451            None => shipped_default(category),
1452            Some(None) => StatusTarget::Disabled,
1453            Some(Some(StatusTargetConfig::Column(option))) => StatusTarget::Column(option),
1454            Some(Some(StatusTargetConfig::Closed { closed })) => StatusTarget::Closed(closed),
1455        });
1456        let mapping = Self { targets };
1457        for (index, category) in CATEGORIES.into_iter().enumerate() {
1458            let StatusTarget::Column(option) = mapping.target(category) else {
1459                continue;
1460            };
1461            if let Some(other) = CATEGORIES[..index].iter().find(|earlier| {
1462                matches!(mapping.target(**earlier), StatusTarget::Column(name)
1463                    if name.as_str().eq_ignore_ascii_case(option.as_str()))
1464            }) {
1465                return Err(SourceError::Config {
1466                    message: format!(
1467                        "status_mapping of source {instance} sends both {} and {} to the board \
1468                         option {:?}; one option cannot read back as two categories",
1469                        category_name(*other),
1470                        category_name(category),
1471                        option.as_str()
1472                    ),
1473                });
1474            }
1475        }
1476        Ok(mapping)
1477    }
1478
1479    fn target(&self, category: StatusCategory) -> &StatusTarget {
1480        &self.targets[category_position(category)]
1481    }
1482
1483    /// The category a board option name reports, or `None` when nothing maps to it.
1484    fn category_of(&self, option: &str) -> Option<StatusCategory> {
1485        CATEGORIES.into_iter().find(|category| {
1486            matches!(self.target(*category), StatusTarget::Column(name)
1487                if name.as_str().eq_ignore_ascii_case(option))
1488        })
1489    }
1490}
1491
1492// 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.
1493/// One repository this source can create an issue in, as `owner/name`.
1494///
1495/// Every `createIssue` this source sends names one of these: the item's own single
1496/// `repositories` entry, else its parent project issue's repository, else the configured
1497/// [`GitHubProjectsConfig::repository`]. [`GitHubProjectsSource::creation_target`] makes
1498/// that choice and says what it refuses before `createIssue`.
1499// llmlint: ignore-end[comments_earn_their_place, contracts_have_one_source_or_a_drift_gate]
1500#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
1501struct RepositoryTarget {
1502    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
1503    name: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only after `owner/name` validation in `new`.
1504}
1505
1506impl RepositoryTarget {
1507    fn parse(value: &str) -> Result<Self, SourceError> {
1508        let (owner, name) = value.split_once('/').ok_or_else(|| SourceError::Config {
1509            message: format!(
1510                "repository must be spelled owner/name; {value:?} names no repository"
1511            ),
1512        })?;
1513        if !valid_github_owner(owner) || !valid_github_repository_name(name) {
1514            return Err(SourceError::Config {
1515                message: format!(
1516                    "repository must be spelled owner/name with a GitHub login and one \
1517                     repository name; {value:?} is not"
1518                ),
1519            });
1520        }
1521        Ok(Self {
1522            owner: owner.to_owned(),
1523            name: name.to_owned(),
1524        })
1525    }
1526
1527    /// The one host whose repositories this source creates issues in, spelled once: it is
1528    /// what [`Self::origin`] renders and what [`Self::from_origin`] accepts.
1529    const HOST: &str = "github.com";
1530
1531    fn origin(&self) -> String {
1532        format!("{}/{}/{}", Self::HOST, self.owner, self.name)
1533    }
1534
1535    /// The repository a normalized origin names, or why it is none this source can create
1536    /// an issue in: another host, or more or fewer than `owner/name` under this one.
1537    fn from_origin(origin: &Repository) -> Result<Self, String> {
1538        let not_here = || {
1539            format!(
1540                "{} is not a {}/owner/name repository",
1541                origin.as_str(),
1542                Self::HOST
1543            )
1544        };
1545        let (host, rest) = origin.as_str().split_once('/').ok_or_else(not_here)?;
1546        if host != Self::HOST {
1547            return Err(not_here());
1548        }
1549        Self::parse(rest).map_err(|_| not_here())
1550    }
1551
1552    fn slug(&self) -> String {
1553        format!("{}/{}", self.owner, self.name)
1554    }
1555}
1556
1557/// A source which reads GitHub afresh for every operation.
1558pub struct GitHubProjectsSource {
1559    /// This source's configured name, used both to tell a far end naming this source
1560    /// from one naming a system it knows nothing about, and to name the instance a
1561    /// status refusal is about.
1562    name: SourceName,
1563    owner: String, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after full GitHub-owner validation.
1564    project_number: u32, // llmlint: ignore[invalid_states_unrepresentable] Private, constructed only by `new` after GraphQL-Int validation.
1565    repository: Option<RepositoryTarget>,
1566    endpoint: Url,
1567    token: SecretString,
1568    credential_name: String, // llmlint: ignore[invalid_states_unrepresentable] Private diagnostic value constructed only after environment-name validation.
1569    statuses: StatusMapping,
1570    client: Client,
1571    /// Every item this source has created since it was built, in the order it created
1572    /// them.
1573    ///
1574    /// GitHub's `projectV2.items` is eventually consistent: an issue added to a board with
1575    /// `addProjectV2ItemById` is routinely absent from the very next read of that board, so
1576    /// a copy resolving a dependency on an item it had just created refused it as not
1577    /// found. A board read is completed from this — an item remembered here and absent from
1578    /// the read is added back, because the board really does hold it and only the read is
1579    /// behind.
1580    ///
1581    /// It is not a cache of a user's work: nothing is remembered that this process did not
1582    /// itself just write, it lives and dies with the process, and it is never consulted for
1583    /// an item this source did not create.
1584    created: Mutex<Vec<Resolved>>,
1585    /// How fast this source writes, and how long it waits out a refusal.
1586    pacing: Pacing,
1587    /// When the last content-creating mutation finished, or the moment the furthest-out
1588    /// reserved slot releases the next one, whichever is later — so the one after it can be
1589    /// spaced from that. See [`MIN_MUTATION_INTERVAL_MS`] for the interval and
1590    /// [`GitHubProjectsSource::finish_mutation`] for why completion rather than release is
1591    /// what it is measured from.
1592    last_mutation: Mutex<Option<Instant>>,
1593    /// The board as this process last read it, for the length of one command.
1594    ///
1595    /// A copy of a project used to re-read the whole board, paged, before writing each of
1596    /// its items, which is by far the largest part of a copy's request count and none of
1597    /// its work. Nothing else changes this board while a command runs — this source's own
1598    /// writes are the only writer — so one read answers them all.
1599    ///
1600    /// It is not a store of a user's work and it is not the cache the no-persistence
1601    /// invariant forbids: it lives and dies with the process exactly as `created` does,
1602    /// nothing is written down, and [`Self::board`] still completes it from `created`, so
1603    /// an item this command created and then depends on resolves whether or not GitHub's
1604    /// own eventually-consistent read has caught up. A write to an item already on the
1605    /// board updates the entry here too, so what this holds is the last read plus this
1606    /// process's own writes rather than a snapshot taken before them.
1607    board_cache: Mutex<Option<Board>>,
1608    /// Each destination repository's node id, resolved once per repository
1609    /// rather than per issue created.
1610    ///
1611    /// A repository's node id does not change, and re-reading it for every issue of a copy
1612    /// spent one request per item on an answer this source already had. It is a map rather
1613    /// than one entry because a copy files each item in the repository its own
1614    /// `repositories` field names, so a plan across five repositories asks GitHub five
1615    /// times and not once per item.
1616    repository_cache: Mutex<BTreeMap<RepositoryTarget, String>>,
1617    /// What every request this source sends is recorded into.
1618    ///
1619    /// Ordinary code path, not a mode: [`Self::send_once`] records into it at the one place
1620    /// a request leaves this crate, so nothing has to be switched on for a session to be
1621    /// counted. It is shared rather than owned so a caller accounting for a whole session —
1622    /// its own schema verification, board lookups, residue sweep and cleanup beside this
1623    /// source's reads and writes — adds up one accounting instead of two. See
1624    /// [`accounting`] for what a record carries and what a session's spend is and is not.
1625    ledger: Arc<Accounting>,
1626}
1627
1628impl GitHubProjectsSource {
1629    /// Validate configuration and capture the named credential without exposing it.
1630    ///
1631    /// # Errors
1632    ///
1633    /// Returns [`SourceError::Config`] for a configuration this instance cannot use and
1634    /// [`SourceError::Auth`] when the named credential is missing or empty.
1635    pub fn new(
1636        name: &SourceName,
1637        config: GitHubProjectsConfig,
1638        secrets: &dyn SecretResolver,
1639    ) -> Result<Self, SourceError> {
1640        Self::recording_into(name, config, secrets, Arc::new(Accounting::new()))
1641    }
1642
1643    /// The same, recording every request it sends into an accounting the caller holds too.
1644    ///
1645    /// [`Self::new`] is this with an accounting of its own. A caller that is also making
1646    /// its own calls to GitHub — a lane verifying a schema, sweeping residue or cleaning
1647    /// up — passes the one it records those into, so the session total accounts for the
1648    /// whole session rather than for this source's share of it.
1649    ///
1650    /// # Errors
1651    ///
1652    /// Exactly [`Self::new`]'s: [`SourceError::Config`] for a configuration this instance
1653    /// cannot use and [`SourceError::Auth`] when the named credential is missing or empty.
1654    pub fn recording_into(
1655        name: &SourceName,
1656        config: GitHubProjectsConfig,
1657        secrets: &dyn SecretResolver,
1658        ledger: Arc<Accounting>,
1659    ) -> Result<Self, SourceError> {
1660        if !valid_github_owner(&config.owner) {
1661            return Err(SourceError::Config {
1662                message: "owner must be 1-39 ASCII letters, digits, or single hyphens, and cannot start or end with a hyphen".into(),
1663            });
1664        }
1665        if config.project_number == 0 || config.project_number > i32::MAX as u32 {
1666            return Err(SourceError::Config {
1667                message: format!("project_number must be between 1 and {}", i32::MAX),
1668            });
1669        }
1670        if !valid_environment_name(&config.token_env) {
1671            return Err(SourceError::Config {
1672                message: "token_env must be a valid environment-variable name".into(),
1673            });
1674        }
1675        let repository = config
1676            .repository
1677            .as_deref()
1678            .map(RepositoryTarget::parse)
1679            .transpose()?;
1680        let endpoint = Url::parse(&config.endpoint).map_err(|e| SourceError::Config {
1681            message: format!("endpoint is not a valid URL: {e}"),
1682        })?;
1683        if endpoint.scheme() != "https"
1684            && !(endpoint.scheme() == "http"
1685                && endpoint
1686                    .host_str()
1687                    .is_some_and(|h| h == "127.0.0.1" || h == "localhost" || h == "::1"))
1688        {
1689            return Err(SourceError::Config {
1690                message:
1691                    "endpoint must use HTTPS (HTTP is accepted only for a loopback test server)"
1692                        .into(),
1693            });
1694        }
1695        let token = secrets.get(&config.token_env).filter(|token| !token.expose_secret().trim().is_empty()).ok_or_else(|| SourceError::Auth {
1696            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),
1697        })?;
1698        Ok(Self {
1699            name: name.clone(),
1700            owner: config.owner,
1701            project_number: config.project_number,
1702            repository,
1703            endpoint,
1704            token,
1705            credential_name: config.token_env,
1706            statuses: StatusMapping::resolve(config.status_mapping, name)?,
1707            client: Client::builder()
1708                .user_agent("onetaskgraph")
1709                .build()
1710                .map_err(|e| SourceError::Config {
1711                    message: format!("cannot build HTTP client: {e}"),
1712                })?,
1713            created: Mutex::new(Vec::new()),
1714            pacing: Pacing::resolve(config.pacing, name)?,
1715            last_mutation: Mutex::new(None),
1716            board_cache: Mutex::new(None),
1717            repository_cache: Mutex::new(BTreeMap::new()),
1718            ledger,
1719        })
1720    }
1721
1722    /// A snapshot of every request this source has sent, and what each cost.
1723    ///
1724    /// A value to hold and compare rather than a borrow of the accounting itself, so two
1725    /// of them can sit side by side. When this source was built with
1726    /// [`Self::recording_into`] the snapshot is the whole shared session, which is the
1727    /// point of building it that way.
1728    #[must_use]
1729    pub fn accounting(&self) -> accounting::Session {
1730        self.ledger.snapshot()
1731    }
1732
1733    /// Send one GraphQL document, pacing this source's own mutations and waiting out a
1734    /// rate limit rather than handing it straight back as an error.
1735    ///
1736    /// Retrying is safe for every document here, including the mutations, and the reason
1737    /// is that only a *refusal* is retried: [`Limiter::classify`] rules on a response
1738    /// GitHub sent, and a request GitHub refused for a rate limit did not run, so nothing
1739    /// this replays has already taken effect. An outcome this source cannot know — the
1740    /// send failed, or the body could not be read, so the mutation may well have landed —
1741    /// is [`Attempt::Failed`] in [`send_once`] and leaves this loop without a second
1742    /// attempt. A duplicate write would come from replaying one of those, and none is
1743    /// replayed.
1744    async fn graphql(&self, query: &str, variables: Value) -> Result<Value, SourceError> {
1745        let doing = operation_description(query);
1746        let mut waited = Duration::ZERO;
1747        let mut waits = 0_u32;
1748        let mut backoff = self.pacing.retry_backoff;
1749        loop {
1750            if is_mutation(query) {
1751                let spacing = self.reserve_mutation_slot();
1752                if !spacing.is_zero() {
1753                    tokio::time::sleep(spacing).await;
1754                }
1755            }
1756            let attempt = self.send_once(query, &variables).await;
1757            if is_mutation(query) {
1758                self.finish_mutation();
1759            }
1760            let limited = match attempt {
1761                Ok(data) => return Ok(data),
1762                Err(Attempt::Failed(error)) => return Err(error),
1763                Err(Attempt::Limited(limited)) => limited,
1764            };
1765            // GitHub really does send `retry-after: 0`, and retrying at once is the one
1766            // move that extends a secondary limit, so a hint below the schedule's own next
1767            // wait is raised to it.
1768            let wait = match limited.hint {
1769                Some(hint) => Duration::from_secs(hint).max(backoff),
1770                None => backoff,
1771            };
1772            let remaining = self.pacing.retry_budget.saturating_sub(waited);
1773            // A wait of nothing spends none of the budget, so it is exhaustion rather
1774            // than a retry. `Pacing::resolve` rules out every way of configuring one
1775            // except a budget of zero, where reporting the first refusal is the ask.
1776            if wait.is_zero() || wait > remaining {
1777                return Err(limited.exhausted(
1778                    doing,
1779                    waits,
1780                    waited,
1781                    wait,
1782                    self.pacing.retry_budget,
1783                ));
1784            }
1785            tokio::time::sleep(wait).await;
1786            waited += wait;
1787            waits += 1;
1788            backoff = backoff.saturating_mul(2);
1789        }
1790    }
1791
1792    /// The next moment a content-creating mutation may leave this source, as a wait from
1793    /// now.
1794    ///
1795    /// The slot is reserved under the lock and the waiting happens outside it, so two
1796    /// callers take two slots rather than the same one — and no lock is held across an
1797    /// await.
1798    ///
1799    /// The moment it is spaced from is the previous mutation's *completion*, which
1800    /// [`Self::finish_mutation`] records. See that method for why the release moment on its
1801    /// own is the wrong thing to measure from.
1802    fn reserve_mutation_slot(&self) -> Duration {
1803        if self.pacing.min_mutation_interval.is_zero() {
1804            return Duration::ZERO;
1805        }
1806        // A poisoned lock here costs pacing, not correctness, and refusing the write over
1807        // it would turn an earlier failure into a second one for no gain.
1808        let mut last = self
1809            .last_mutation
1810            .lock()
1811            .unwrap_or_else(std::sync::PoisonError::into_inner);
1812        let now = Instant::now();
1813        // `checked_add` rather than `+`: `Instant + Duration` panics on overflow, and
1814        // pacing is not worth a panic even at a bound `MAX_PACING_MS` already rules out.
1815        let at = last.map_or(now, |previous| {
1816            previous
1817                .checked_add(self.pacing.min_mutation_interval)
1818                .map_or(now, |earliest| earliest.max(now))
1819        });
1820        *last = Some(at);
1821        at.saturating_duration_since(now)
1822    }
1823
1824    /// Record that a content-creating mutation has finished, so the next one is spaced
1825    /// from here rather than from the moment this one was released.
1826    ///
1827    /// This source can only choose when a request *departs*; the limiter counts when it
1828    /// *arrives*, and the two differ by whatever the request spent in transit. Spacing one
1829    /// departure from the last therefore hands the limiter a gap of the interval less that
1830    /// transit, so a source pacing at 750 ms can still be seen arriving faster — which is
1831    /// exactly how a copy paced well inside a board's threshold was refused by it on a
1832    /// slower machine while passing on a quick one.
1833    ///
1834    /// Spacing from completion removes the subtraction rather than budgeting for it. The
1835    /// previous request had already arrived before its response came back, so its arrival
1836    /// is no later than this moment, and the next mutation is released at least the
1837    /// interval after this moment and arrives no earlier than it is released: the gap the
1838    /// limiter measures is therefore at least the interval, whatever transit costs and on
1839    /// whatever platform. The price is that a mutation's own round trip no longer counts
1840    /// towards its spacing, which makes this source slightly slower than the configured
1841    /// rate rather than slightly faster — the safe side of a limit that punishes being
1842    /// wrong by refusing reads for the next fifty minutes.
1843    ///
1844    /// A failed attempt is recorded too: a request refused by the limiter still arrived,
1845    /// and one that never left costs only a wait nobody needed.
1846    fn finish_mutation(&self) {
1847        if self.pacing.min_mutation_interval.is_zero() {
1848            return;
1849        }
1850        // A poisoned lock here costs pacing, not correctness, exactly as in the reservation.
1851        let mut last = self
1852            .last_mutation
1853            .lock()
1854            .unwrap_or_else(std::sync::PoisonError::into_inner);
1855        let now = Instant::now();
1856        // `max` rather than an assignment: a concurrent caller may already have reserved a
1857        // slot further out, and completing this request must never pull that slot back in.
1858        *last = Some(last.map_or(now, |reserved| reserved.max(now)));
1859    }
1860
1861    /// One HTTP attempt, classified into an answer, a rate limit to wait out, or a
1862    /// failure that waiting cannot help — and recorded, whichever of the three it was.
1863    ///
1864    /// This is the one place a request leaves this crate, which is why the accounting is
1865    /// here rather than at each of the callers: a read path added later is counted without
1866    /// anybody remembering to count it, and
1867    /// `the_session_report_counts_every_request_the_board_served_and_what_each_cost` fails
1868    /// when one is not.
1869    async fn send_once(&self, query: &str, variables: &Value) -> Result<Value, Attempt> {
1870        let Attempted {
1871            result,
1872            limits,
1873            reported_cost,
1874        } = self.attempt(query, variables).await;
1875        // No `otherwise` name: every document this source sends is one of its own, and the
1876        // inventory gate on `graphql::DOCUMENTS` is what keeps that true.
1877        let sending = accounting::Request::graphql(query, variables, None, reported_cost);
1878        let outcome = match &result {
1879            Ok(_) => accounting::Outcome::Answered,
1880            Err(Attempt::Limited(_)) => accounting::Outcome::RateLimited,
1881            Err(Attempt::Failed(_)) => accounting::Outcome::Refused,
1882        };
1883        self.ledger.record(sending.finished(outcome, limits));
1884        result
1885    }
1886
1887    /// The attempt itself, with what its response said about the rate limit alongside.
1888    ///
1889    /// The two are returned together rather than recorded here because every one of the
1890    /// early exits below is a different outcome, and a record written at each of them is a
1891    /// record one of them can be added without.
1892    async fn attempt(&self, query: &str, variables: &Value) -> Attempted {
1893        let mut limits = accounting::RateLimit::default();
1894        let mut reported_cost = None;
1895        let result = self
1896            .attempted(query, variables, &mut limits, &mut reported_cost)
1897            .await;
1898        Attempted {
1899            result,
1900            limits,
1901            reported_cost,
1902        }
1903    }
1904
1905    /// One HTTP attempt, filling in what its response said about the rate limit as it goes.
1906    async fn attempted(
1907        &self,
1908        query: &str,
1909        variables: &Value,
1910        limits: &mut accounting::RateLimit,
1911        reported_cost: &mut Option<u64>,
1912    ) -> Result<Value, Attempt> {
1913        let response = self
1914            .client
1915            .post(self.endpoint.clone())
1916            .bearer_auth(self.token.expose_secret())
1917            .json(&json!({"query": query, "variables": variables}))
1918            .send()
1919            .await
1920            .map_err(|e| {
1921                Attempt::Failed(SourceError::Unavailable {
1922                    message: format!("GitHub GraphQL request failed: {e}"),
1923                })
1924            })?;
1925        let status = response.status();
1926        let header = |name: &str| whole_seconds(response.headers().get(name));
1927        *limits = accounting::RateLimit::read(|name| {
1928            response
1929                .headers()
1930                .get(name)
1931                .and_then(|value| value.to_str().ok())
1932                .map(str::to_owned)
1933        });
1934        // Exactly `0` is exhaustion and everything else — a count, an empty value, bytes
1935        // that are not text at all — is "not known to be exhausted". This never makes a
1936        // response a refusal on its own: it says which limiter a refusal is attributed to
1937        // and where its hint comes from, so a value this cannot read costs a hint rather
1938        // than an answer.
1939        let exhausted = response
1940            .headers()
1941            .get("x-ratelimit-remaining")
1942            .and_then(|value| value.to_str().ok())
1943            == Some("0");
1944        // `retry-after` is what GitHub asks for when it asks; when it does not and the
1945        // primary budget is spent, `x-ratelimit-reset` says when that budget comes back,
1946        // which is the same question answered as an absolute time. Nothing else here is a
1947        // hint, and a schedule is what answers a refusal that carries none.
1948        let hint = header("retry-after").or_else(|| {
1949            exhausted
1950                .then(|| header("x-ratelimit-reset"))
1951                .flatten()
1952                .map(|reset| reset.saturating_sub(Utc::now().timestamp().max(0).unsigned_abs()))
1953        });
1954        // Read before it is parsed, because the evidence which tells a secondary rate
1955        // limit from a rejected credential is in the body of a response whose status says
1956        // only "forbidden" — and a non-success response was never parsed at all.
1957        let body = response.text().await.map_err(|e| {
1958            Attempt::Failed(SourceError::Unavailable {
1959                message: format!("GitHub GraphQL response could not be read: {e}"),
1960            })
1961        })?;
1962        if let Some(limiter) = Limiter::classify(status, exhausted, &body) {
1963            return Err(Attempt::Limited(Limited { limiter, hint }));
1964        }
1965        if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
1966            return Err(Attempt::Failed(SourceError::Auth {
1967                message: format!(
1968                    "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"
1969                ),
1970            }));
1971        }
1972        if !status.is_success() {
1973            return Err(Attempt::Failed(SourceError::Unavailable {
1974                message: format!("GitHub GraphQL returned HTTP {status}"),
1975            }));
1976        }
1977        // GitHub reports what a call cost only when the document asked it to, and no
1978        // document this source sends does — so this is `None` here and carries the figure
1979        // for a caller whose own document selects `rateLimit { cost }`. What it must never
1980        // pick up is a `dryRun` probe's cost, which is some other document's.
1981        *reported_cost = serde_json::from_str::<Value>(&body)
1982            .ok()
1983            .as_ref()
1984            .and_then(|body| body.pointer("/data/rateLimit/cost"))
1985            .and_then(Value::as_u64);
1986        self.answer(&body).map_err(Attempt::Failed)
1987    }
1988
1989    /// What one successful HTTP response says, once its GraphQL errors are read.
1990    fn answer(&self, body: &str) -> Result<Value, SourceError> {
1991        let body: Value = serde_json::from_str(body).map_err(|e| SourceError::Malformed {
1992            message: format!("GitHub returned invalid JSON: {e}"),
1993        })?;
1994        let errors = body
1995            .get("errors")
1996            .map(|value| {
1997                value.as_array().ok_or_else(|| SourceError::Malformed {
1998                    message: "GitHub response errors is not an array".into(),
1999                })
2000            })
2001            .transpose()?;
2002        if let Some(errors) = errors.filter(|errors| !errors.is_empty()) {
2003            let messages = errors
2004                .iter()
2005                .filter_map(|e| e.get("message").and_then(Value::as_str))
2006                .collect::<Vec<_>>()
2007                .join("; ");
2008            let message = if messages.is_empty() {
2009                "GitHub returned GraphQL errors".into()
2010            } else {
2011                messages
2012            };
2013            let normalized = message.to_ascii_lowercase();
2014            if normalized.contains("resource not accessible") || normalized.contains("scope") {
2015                return Err(SourceError::Auth {
2016                    message: format!(
2017                        "{message}; grant {} Projects and Issues read/write plus Pull requests read-only access for every repository represented on the board",
2018                        self.credential_name
2019                    ),
2020                });
2021            }
2022            return Err(SourceError::Refused { message });
2023        }
2024        body.get("data")
2025            .filter(|data| data.is_object())
2026            .cloned()
2027            .ok_or_else(|| SourceError::Malformed {
2028                message: "GitHub response has no data object".into(),
2029            })
2030    }
2031
2032    // llmlint: ignore[boundary_inputs_validated] GitHub caps nested connections at 100 and
2033    // GraphQL cannot independently page them inside the outer item page. This source page is
2034    // deliberately bounded at that published maximum; the live drift journey exercises it.
2035    async fn board_page(
2036        &self,
2037        items_after: Option<&str>,
2038        items_first: u32,
2039    ) -> Result<Value, SourceError> {
2040        let data = self
2041            .graphql(
2042                graphql::BOARD,
2043                json!({"owner":self.owner,"number":self.project_number,
2044                       "first":items_first.min(MAX_PAGE_SIZE),"after":items_after,
2045                       "nestedFirst":NESTED_PAGE_SIZE,"duplicates":true}),
2046            )
2047            .await?;
2048        data.pointer("/owner/projectV2")
2049            .filter(|v| !v.is_null())
2050            .cloned()
2051            .ok_or_else(|| SourceError::Refused {
2052                message: format!(
2053                    "GitHub project {}/{} was not found or is not visible to the token",
2054                    self.owner, self.project_number
2055                ),
2056            })
2057    }
2058
2059    /// The search that finds the issues of this board, narrowed by `also` when it is
2060    /// given.
2061    ///
2062    /// `project:owner/number` is what scopes a search to one board, and `is:issue` is what
2063    /// keeps pull requests out of it: GitHub's `ISSUE` search type covers both, and a pull
2064    /// request is somebody's change rather than a unit of plan. `-has:parent` is *not*
2065    /// here on purpose — GitHub accepts it and silently ignores it, so a project is told
2066    /// from a task by the `parent` field each issue carries rather than by the search.
2067    fn board_search(&self, also: Option<&str>) -> String {
2068        let scope = format!("project:{}/{} is:issue", self.owner, self.project_number);
2069        match also {
2070            Some(also) => format!("{scope} {also}"),
2071            None => scope,
2072        }
2073    }
2074
2075    /// One issue this source reached directly, as the board item a read of the board would
2076    /// have produced — or `None` when this board does not hold it.
2077    ///
2078    /// The board half of an issue rides along on `Issue.projectItems`, so the value handed
2079    /// to [`Self::resolve`] is the very shape a `ProjectV2.items` read gives it: the board
2080    /// item's own id, that item's field values, and the issue as its content. One resolver
2081    /// for both routes is what makes an issue read through a search, through its own node
2082    /// id, or through its project's sub-issues report the same title, the same status, the
2083    /// same labels and the same qualified id.
2084    ///
2085    /// An issue with no entry for *this* board is not this source's to report, which is
2086    /// what keeps an id naming some other repository's issue from being answered as an item
2087    /// of this board. That answer is given about an **exhausted** connection and never
2088    /// about an unread page: the entry is looked for on the page in hand, and only if that
2089    /// page reports more of the connection, in [`Self::board_membership`]'s walk of the
2090    /// rest of it.
2091    async fn resolve_issue(&self, issue: &Value) -> Result<Option<Resolved>, SourceError> {
2092        if optional_str(issue, "__typename")? != Some("Issue") {
2093            return Ok(None);
2094        }
2095        let memberships = issue
2096            .get("projectItems")
2097            .ok_or_else(|| SourceError::Malformed {
2098                message: "GitHub issue is missing projectItems".into(),
2099            })?;
2100        let nodes = memberships
2101            .get("nodes")
2102            .and_then(Value::as_array)
2103            .ok_or_else(|| SourceError::Malformed {
2104                message: "GitHub issue projectItems.nodes is not an array".into(),
2105            })?;
2106        let held = match self.board_entry(nodes) {
2107            Some(held) => held.clone(),
2108            None => {
2109                let info = memberships
2110                    .get("pageInfo")
2111                    .ok_or_else(|| SourceError::Malformed {
2112                        message: "GitHub issue projectItems has no pageInfo".into(),
2113                    })?;
2114                // The page held no entry for this board. Whether that means the issue is
2115                // not on it is a question about the rest of the connection, and only a
2116                // connection with no rest answers it here.
2117                if !required_bool(info, "hasNextPage")? {
2118                    return Ok(None);
2119                }
2120                let cursor = required_str(info, "endCursor")?;
2121                validate_cursor_progress(None, cursor)?;
2122                let issue_id = required_str(issue, "id")?;
2123                match self.board_membership(issue_id, cursor).await? {
2124                    Some(held) => held,
2125                    None => return Ok(None),
2126                }
2127            }
2128        };
2129        let item = json!({
2130            "id": required_str(&held, "id")?,
2131            "fieldValues": held.get("fieldValues"),
2132            "content": issue,
2133        });
2134        self.resolve(&item)
2135    }
2136
2137    /// This board's own entry among one page of an issue's `Issue.projectItems`.
2138    ///
2139    /// One spelling of *which membership is this board's*, so the page a read carries and
2140    /// the pages [`Self::board_membership`] walks are searched by the same rule.
2141    fn board_entry<'a>(&self, nodes: &'a [Value]) -> Option<&'a Value> {
2142        nodes.iter().find(|node| {
2143            node.pointer("/project/number").and_then(Value::as_u64)
2144                == Some(u64::from(self.project_number))
2145        })
2146    }
2147
2148    /// The rest of one issue's board memberships, from `after`, for this board's entry.
2149    ///
2150    /// The recovery read: a page of memberships that holds no entry for this board says
2151    /// nothing about the memberships past it, so the connection is walked to exhaustion
2152    /// before an issue is reported as one this board does not hold. `Ok(None)` is that
2153    /// positive answer — the whole connection was read and no entry named this board —
2154    /// rather than a failure, and the walk is held to
2155    /// [`validate_cursor_progress`] like every other page walk here, so a source answering
2156    /// with a cursor that does not advance is refused instead of spun on.
2157    async fn board_membership(
2158        &self,
2159        issue: &str,
2160        after: &str,
2161    ) -> Result<Option<Value>, SourceError> {
2162        let mut after = after.to_owned();
2163        loop {
2164            let data = self
2165                .graphql(
2166                    graphql::ISSUE_BOARD_ITEMS,
2167                    json!({"id":issue,"first":MAX_PAGE_SIZE,"after":after,
2168                           "nestedFirst":NESTED_PAGE_SIZE}),
2169                )
2170                .await?;
2171            let Some(connection) = data
2172                .pointer("/node/projectItems")
2173                .filter(|value| !value.is_null())
2174            else {
2175                // The id resolved to nothing, or to something with no memberships to walk —
2176                // which is the same answer as a connection holding no entry for this board.
2177                return Ok(None);
2178            };
2179            let nodes = connection
2180                .get("nodes")
2181                .and_then(Value::as_array)
2182                .ok_or_else(|| SourceError::Malformed {
2183                    message: "GitHub issue projectItems.nodes is not an array".into(),
2184                })?;
2185            if let Some(held) = self.board_entry(nodes) {
2186                return Ok(Some(held.clone()));
2187            }
2188            let info = connection
2189                .get("pageInfo")
2190                .ok_or_else(|| SourceError::Malformed {
2191                    message: "GitHub issue projectItems has no pageInfo".into(),
2192                })?;
2193            let next = required_bool(info, "hasNextPage")?
2194                .then(|| required_str(info, "endCursor"))
2195                .transpose()?;
2196            match next {
2197                Some(next) => {
2198                    validate_cursor_progress(Some(&after), next)?;
2199                    after = next.to_owned();
2200                }
2201                None => return Ok(None),
2202            }
2203        }
2204    }
2205
2206    /// One page of a board-scoped issue search, and where the next page resumes.
2207    async fn search_page(
2208        &self,
2209        search: &str,
2210        first: u32,
2211        after: Option<&str>,
2212    ) -> Result<(Vec<Resolved>, Option<String>), SourceError> {
2213        let data = self
2214            .graphql(
2215                graphql::SEARCH_ISSUES,
2216                json!({"search":search,"type":"ISSUE","first":first.min(MAX_PAGE_SIZE),
2217                       "after":after,"nestedFirst":NESTED_PAGE_SIZE,
2218                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
2219            )
2220            .await?;
2221        let connection = data.get("search").ok_or_else(|| SourceError::Malformed {
2222            message: "GitHub search response has no search connection".into(),
2223        })?;
2224        let mut found = Vec::new();
2225        for node in connection
2226            .get("nodes")
2227            .and_then(Value::as_array)
2228            .ok_or_else(|| SourceError::Malformed {
2229                message: "GitHub search nodes is not an array".into(),
2230            })?
2231        {
2232            if let Some(resolved) = self.resolve_issue(node).await? {
2233                found.push(resolved);
2234            }
2235        }
2236        let info = connection
2237            .get("pageInfo")
2238            .ok_or_else(|| SourceError::Malformed {
2239                message: "GitHub search connection has no pageInfo".into(),
2240            })?;
2241        let next = required_bool(info, "hasNextPage")?
2242            .then(|| required_str(info, "endCursor"))
2243            .transpose()?
2244            .map(str::to_owned);
2245        if let Some(next) = &next {
2246            validate_cursor_progress(after, next)?;
2247        }
2248        Ok((found, next))
2249    }
2250
2251    /// Every issue this board holds, walked to exhaustion, completed with what this run
2252    /// wrote.
2253    ///
2254    /// The completion is not an optimisation and it is not a cache: GitHub's issue search
2255    /// is an index and is eventually consistent, so an issue this run created seconds ago
2256    /// is routinely absent from it, and a project listed straight after being written would
2257    /// otherwise be missing from its own board. What is added back is only what this
2258    /// process itself wrote, out of [`Self::created`], which lives and dies with the
2259    /// process.
2260    async fn board_issues(&self) -> Result<Vec<Resolved>, SourceError> {
2261        let mut after: Option<String> = None;
2262        let mut found = Vec::new();
2263        let search = self.board_search(None);
2264        loop {
2265            let (page, next) = self
2266                .search_page(&search, MAX_PAGE_SIZE, after.as_deref())
2267                .await?;
2268            found.extend(page);
2269            match next {
2270                Some(next) => after = Some(next),
2271                None => break,
2272            }
2273        }
2274        self.completed_with_written(found, |_| true)
2275    }
2276
2277    /// `found`, with everything this run wrote that `keep` accepts and the read did not
2278    /// report.
2279    ///
2280    /// See [`Self::created`] and [`Self::board_issues`] for why a read has to be completed
2281    /// at all: the search index is behind, and a node read of an item filed moments ago can
2282    /// be too.
2283    fn completed_with_written(
2284        &self,
2285        mut found: Vec<Resolved>,
2286        keep: impl Fn(&Resolved) -> bool,
2287    ) -> Result<Vec<Resolved>, SourceError> {
2288        for own in self.created()?.iter().filter(|own| keep(own)) {
2289            if !found.iter().any(|item| item.id == own.id) {
2290                found.push(own.clone());
2291            }
2292        }
2293        Ok(found)
2294    }
2295
2296    /// What resolving one node id reached.
2297    ///
2298    /// Three answers rather than an `Option`, because a board *draft* is none of the other
2299    /// two: it is not an issue, it has no node of its own this source can read the board
2300    /// half off, and its only home is the board's own item connection — so a read of one
2301    /// is completed from there rather than reported as nothing.
2302    async fn reach(&self, id: &NativeId) -> Result<Reached, SourceError> {
2303        let asked = self
2304            .graphql(
2305                graphql::ISSUE,
2306                json!({"id":id.0,"nestedFirst":NESTED_PAGE_SIZE,
2307                       "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
2308            )
2309            .await;
2310        let data = match asked {
2311            Ok(data) => data,
2312            // A string that is not a node id at all is not a failure to report: it is an id
2313            // this board does not hold, which is what every read of one already answers.
2314            Err(error) if unresolvable_node(&error) => return Ok(Reached::Nothing),
2315            Err(error) => return Err(error),
2316        };
2317        let Some(node) = data.get("node").filter(|value| !value.is_null()) else {
2318            return Ok(Reached::Nothing);
2319        };
2320        if optional_str(node, "__typename")? == Some("DraftIssue") {
2321            return Ok(Reached::Draft);
2322        }
2323        Ok(match self.resolve_issue(node).await? {
2324            Some(item) => Reached::Held(Box::new(item)),
2325            None => Reached::Nothing,
2326        })
2327    }
2328
2329    /// One item of this board by its own id, whatever kind it is.
2330    ///
2331    /// Resolved from the identifier alone: no search, board-wide or otherwise. What this
2332    /// run wrote is read first, because a node read of an item created moments ago can
2333    /// still be behind the board field values written onto it — see [`Self::created`].
2334    async fn item_by_id(&self, id: &NativeId) -> Result<Option<Resolved>, SourceError> {
2335        if let Some(own) = self.created()?.iter().find(|own| own.id == *id) {
2336            return Ok(Some(own.clone()));
2337        }
2338        match self.reach(id).await? {
2339            Reached::Held(item) => Ok(Some(*item)),
2340            Reached::Nothing => Ok(None),
2341            // The one read that still costs the board: a draft lives nowhere else.
2342            Reached::Draft => Ok(self
2343                .board()
2344                .await?
2345                .items
2346                .into_iter()
2347                .find(|item| item.id == *id)),
2348        }
2349    }
2350
2351    /// Everything filed under one issue of this board, walked to exhaustion — or `None`
2352    /// when that id names nothing here with a sub-issue relationship to walk.
2353    ///
2354    /// `None` and an empty answer are different: `None` is *this is not an issue of this
2355    /// GitHub*, which is what sends a project selector on to be read as a name, and an
2356    /// empty vector is a project that holds nothing.
2357    async fn sub_issues(&self, id: &NativeId) -> Result<Option<Vec<Resolved>>, SourceError> {
2358        let mut after: Option<String> = None;
2359        let mut children = Vec::new();
2360        loop {
2361            let asked = self
2362                .graphql(
2363                    graphql::SUB_ISSUES,
2364                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after,
2365                           "nestedFirst":NESTED_PAGE_SIZE,
2366                           "boardItems":BOARD_ITEMS_PAGE_SIZE,"duplicates":true}),
2367                )
2368                .await;
2369            let data = match asked {
2370                Ok(data) => data,
2371                // A string that is not a node id at all is not a failure to report: it is
2372                // the ordinary answer to a selector naming a project by its name.
2373                Err(error) if unresolvable_node(&error) => return Ok(None),
2374                Err(error) => return Err(error),
2375            };
2376            let Some(connection) = data
2377                .pointer("/node/subIssues")
2378                .filter(|value| !value.is_null())
2379            else {
2380                // No such node, or one with no sub-issue relationship — a board draft is
2381                // the one this board can really hold.
2382                return Ok(None);
2383            };
2384            for node in connection
2385                .get("nodes")
2386                .and_then(Value::as_array)
2387                .ok_or_else(|| SourceError::Malformed {
2388                    message: "GitHub subIssues.nodes is not an array".into(),
2389                })?
2390            {
2391                if let Some(resolved) = self.resolve_issue(node).await? {
2392                    children.push(resolved);
2393                }
2394            }
2395            let info = connection
2396                .get("pageInfo")
2397                .ok_or_else(|| SourceError::Malformed {
2398                    message: "GitHub subIssues connection has no pageInfo".into(),
2399                })?;
2400            let next = required_bool(info, "hasNextPage")?
2401                .then(|| required_str(info, "endCursor"))
2402                .transpose()?;
2403            match next {
2404                Some(next) => {
2405                    validate_cursor_progress(after.as_deref(), next)?;
2406                    after = Some(next.to_owned());
2407                }
2408                None => return Ok(Some(children)),
2409            }
2410        }
2411    }
2412
2413    /// Which issue of this board a project *name* is, or `None` when none is.
2414    ///
2415    /// One bounded query which filters on that name at the server, rather than a walk of
2416    /// every issue the board holds. The name is compared again here: the qualifier narrows
2417    /// what GitHub sends, and this source decides what it names.
2418    async fn project_by_name(&self, name: &str) -> Result<Option<NativeId>, SourceError> {
2419        let search = self.board_search(Some(&title_qualifier(name)));
2420        let (candidates, _) = self.search_page(&search, MAX_PAGE_SIZE, None).await?;
2421        Ok(candidates
2422            .into_iter()
2423            .find(|item| {
2424                item.kind == BoardKind::Work(ItemKind::Project)
2425                    && item.title.eq_ignore_ascii_case(name)
2426            })
2427            .map(|item| item.id))
2428    }
2429
2430    /// Everything filed under one project of this board: the sub-issues of the issue that
2431    /// project is.
2432    ///
2433    /// Tasks *and* documents, because a document filed under a project is a sub-issue of it
2434    /// too — the caller keeps the kind it asked for. Nothing about this grows as the board
2435    /// gains projects, or as another project gains tasks.
2436    ///
2437    /// A qualified id names the issue and is asked for its sub-issues directly: one
2438    /// request, no search of any kind. Only a selector GitHub cannot resolve that way is
2439    /// read as a project *name*, which costs the one bounded search
2440    /// [`Self::project_by_name`] makes.
2441    async fn project_children(&self, selector: &NativeId) -> Result<Vec<Resolved>, SourceError> {
2442        let (project, children) = match self.sub_issues(selector).await? {
2443            Some(children) => (selector.clone(), children),
2444            None => match self.project_by_name(&selector.0).await? {
2445                Some(project) => {
2446                    let children = self.sub_issues(&project).await?.unwrap_or_default();
2447                    (project, children)
2448                }
2449                None => return Ok(Vec::new()),
2450            },
2451        };
2452        self.completed_with_written(children, |own| own.parent.as_ref() == Some(&project))
2453    }
2454
2455    /// Every item on the board, with the one board identity they all share.
2456    ///
2457    /// See [`Self::board_cache`]. The completion from `created` happens on every call
2458    /// rather than once, which is what the cache could otherwise have broken.
2459    async fn board(&self) -> Result<Board, SourceError> {
2460        let cached = self.board_cache()?.clone();
2461        let mut board = match cached {
2462            Some(board) => board,
2463            None => {
2464                let read = self.read_board().await?;
2465                *self.board_cache()? = Some(read.clone());
2466                read
2467            }
2468        };
2469        for own in self.created()?.iter() {
2470            if !board.items.iter().any(|item| item.id == own.id) {
2471                board.items.push(own.clone());
2472            }
2473        }
2474        Ok(board)
2475    }
2476
2477    /// This process's own view of the board, or the refusal a poisoned lock is.
2478    fn board_cache(&self) -> Result<std::sync::MutexGuard<'_, Option<Board>>, SourceError> {
2479        self.board_cache
2480            .lock()
2481            .map_err(|_| SourceError::Unavailable {
2482                message: "this source's view of the board was left inconsistent by an earlier \
2483                      failure; next: run the command again"
2484                    .into(),
2485            })
2486    }
2487
2488    /// Bring this process's own view of the board up to an item it has just written.
2489    ///
2490    /// A created item goes to `created`, which is what completes a board read GitHub's own
2491    /// eventual consistency has left behind. An item that was already there is replaced
2492    /// where it sits, so a second write of it in the same command reads its real parent
2493    /// rather than the one it had before the first write.
2494    ///
2495    /// "Where it sits" is two places, and missing the first leaves a stale record that
2496    /// wins: an item this same run created is held in `created` and not in the cached
2497    /// board, and `board` completes the cached board *from* `created`, so replacing only
2498    /// the cached copy of such an item replaces nothing and the read still reports the
2499    /// title it was created with.
2500    fn remember_written(&self, item: Resolved, created: bool) -> Result<(), SourceError> {
2501        if created {
2502            self.created()?.push(item);
2503            return Ok(());
2504        }
2505        {
2506            let mut own = self.created()?;
2507            if let Some(held) = own.iter_mut().find(|held| held.id == item.id) {
2508                *held = item;
2509                return Ok(());
2510            }
2511        }
2512        if let Some(board) = self.board_cache()?.as_mut()
2513            && let Some(held) = board.items.iter_mut().find(|held| held.id == item.id)
2514        {
2515            *held = item;
2516        }
2517        Ok(())
2518    }
2519
2520    /// Forget one item this process has just deleted, from both halves of its own view.
2521    fn forget(&self, id: &NativeId) -> Result<(), SourceError> {
2522        self.created()?.retain(|own| own.id != *id);
2523        if let Some(board) = self.board_cache()?.as_mut() {
2524            board.items.retain(|item| item.id != *id);
2525        }
2526        Ok(())
2527    }
2528
2529    /// Every page of the board, read from GitHub.
2530    async fn read_board(&self) -> Result<Board, SourceError> {
2531        let mut after: Option<String> = None;
2532        let mut items = Vec::new();
2533        let mut board;
2534        loop {
2535            let page = self.board_page(after.as_deref(), MAX_PAGE_SIZE).await?;
2536            for item in page
2537                .pointer("/items/nodes")
2538                .and_then(Value::as_array)
2539                .ok_or_else(|| SourceError::Malformed {
2540                    message: "GitHub project items.nodes is not an array".into(),
2541                })?
2542            {
2543                if let Some(resolved) = self.resolve(item)? {
2544                    items.push(resolved);
2545                }
2546            }
2547            let info = page
2548                .pointer("/items/pageInfo")
2549                .ok_or_else(|| SourceError::Malformed {
2550                    message: "GitHub project items have no pageInfo".into(),
2551                })?;
2552            let has_next = required_bool(info, "hasNextPage")?;
2553            let next = has_next
2554                .then(|| required_str(info, "endCursor"))
2555                .transpose()?;
2556            board = page.clone();
2557            match next {
2558                Some(next) => {
2559                    validate_cursor_progress(after.as_deref(), next)?;
2560                    after = Some(next.to_owned());
2561                }
2562                None => break,
2563            }
2564        }
2565        Ok(Board {
2566            id: required_str(&board, "id")?.to_owned(),
2567            fields: board.get("fields").cloned().unwrap_or(Value::Null),
2568            items,
2569        })
2570    }
2571
2572    /// The items this source has created, for completing a board read that is behind.
2573    fn created(&self) -> Result<std::sync::MutexGuard<'_, Vec<Resolved>>, SourceError> {
2574        self.created.lock().map_err(|_| SourceError::Unavailable {
2575            message: "this source's record of what it created in this run was left \
2576                      inconsistent by an earlier failure; next: run the command again"
2577                .into(),
2578        })
2579    }
2580
2581    /// One board item as this source reports it, or `None` for content it ignores.
2582    ///
2583    /// A pull request is neither a project nor a task — it is somebody's change, not a
2584    /// unit of plan — and an item whose content the token cannot see has nothing to
2585    /// report at all.
2586    fn resolve(&self, item: &Value) -> Result<Option<Resolved>, SourceError> {
2587        let content = item.get("content").ok_or_else(|| SourceError::Malformed {
2588            message: "GitHub project item is missing content".into(),
2589        })?;
2590        if content.is_null() {
2591            return Ok(None);
2592        }
2593        let content_kind = match required_str(content, "__typename")? {
2594            "Issue" => ContentKind::Issue,
2595            "DraftIssue" => ContentKind::DraftIssue,
2596            _ => return Ok(None),
2597        };
2598        let field_values = item
2599            .get("fieldValues")
2600            .ok_or_else(|| SourceError::Malformed {
2601                message: "GitHub project item is missing fieldValues".into(),
2602            })?;
2603        complete_connection(field_values, "project item field values", NESTED_PAGE_SIZE)?;
2604        let nodes = field_values
2605            .get("nodes")
2606            .and_then(Value::as_array)
2607            .ok_or_else(|| SourceError::Malformed {
2608                message: "GitHub project item fieldValues.nodes is not an array".into(),
2609            })?;
2610        if let Some(labels) = content.get("labels") {
2611            complete_connection(labels, "content labels", NESTED_PAGE_SIZE)?;
2612        }
2613        let (body, slot) = metadata_body(optional_str(content, "body")?.map(str::to_owned))?;
2614        let parent = optional_str(content.get("parent").unwrap_or(&Value::Null), "id")?
2615            .map(|id| NativeId(id.to_owned()));
2616        // A draft has no sub-issues to summarise, and GitHub's schema gives it no field
2617        // to read one from; it is a task, and never a project.
2618        let sub_issues = match content_kind {
2619            ContentKind::Issue => sub_issue_total(content)?,
2620            ContentKind::DraftIssue => 0,
2621        };
2622        let content_id = required_str(content, "id")?;
2623        let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
2624            message: format!("GitHub issue {content_id}: {message}"),
2625        })?;
2626        let raw_title = required_str(content, "title")?;
2627        // The design prefix is read *first*, before either of the two rules that separate
2628        // a project from a task. A document is not work whatever sub-issues it has and
2629        // whatever marker it carries, and reading the prefix later would make a design
2630        // issue with none of either an empty project.
2631        let kind = if raw_title.starts_with(DESIGN_TITLE_PREFIX) {
2632            BoardKind::Document
2633        } else if parent.is_some() {
2634            // Being a sub-issue wins outright, and no marker overrides it: an issue filed
2635            // under a project is that project's task even when it has sub-issues of its
2636            // own.
2637            BoardKind::Work(ItemKind::Task)
2638        } else if sub_issues > 0 || marked == Some(ItemKind::Project) {
2639            BoardKind::Work(ItemKind::Project)
2640        } else {
2641            BoardKind::Work(ItemKind::Task)
2642        };
2643        // The title a person wrote, which for a document is the one without the prefix —
2644        // the same way `content` above is the body without this source's metadata slot.
2645        let title = match kind {
2646            BoardKind::Document => raw_title[DESIGN_TITLE_PREFIX.len()..].to_owned(),
2647            BoardKind::Work(_) => raw_title.to_owned(),
2648        };
2649        let own_repository = content
2650            .pointer("/repository/nameWithOwner")
2651            .and_then(Value::as_str)
2652            .map(|origin| Repository::try_from(format!("{}/{origin}", RepositoryTarget::HOST)))
2653            .transpose()
2654            .map_err(|message| SourceError::Malformed { message })?;
2655        let repositories = if slot.contains_key(Repository::METADATA_KEY) {
2656            Repository::from_metadata(&slot)
2657                .map_err(|message| SourceError::Malformed { message })?
2658        } else {
2659            own_repository.clone().into_iter().collect()
2660        };
2661        Ok(Some(Resolved {
2662            item_id: required_str(item, "id")?.to_owned(),
2663            id: NativeId(content_id.to_owned()),
2664            content_kind,
2665            kind,
2666            title,
2667            body: body.filter(|value| !value.is_empty()),
2668            status: self.status(item, content)?,
2669            labels: labels(content)?,
2670            parent,
2671            origin: text_field(nodes, ORIGIN_FIELD)?.filter(|value| !value.is_empty()),
2672            url: optional_str(content, "url")?.map(str::to_owned),
2673            created_at: optional_time(content, "createdAt")?,
2674            updated_at: optional_time(content, "updatedAt")?,
2675            own_repository,
2676            repositories,
2677            slot,
2678        }))
2679    }
2680
2681    /// The status one board item reports.
2682    ///
2683    /// The closed state decides the category and the `Status` option decides the name, so
2684    /// a closed issue sitting in a "Shipped" column reports `done` named `Shipped`. A
2685    /// closed issue whose reason is `DUPLICATE` or `REOPENED` reports `Unknown`: a
2686    /// duplicate is not finished work, and calling it done is a lie the next copy would
2687    /// write back. `REOPENED`-while-closed is a state this source can never produce, so
2688    /// it is read permissively rather than refused — reads are faithful, and refusals
2689    /// belong on writes.
2690    fn status(&self, item: &Value, content: &Value) -> Result<Status, SourceError> {
2691        let nodes = item
2692            .pointer("/fieldValues/nodes")
2693            .and_then(Value::as_array)
2694            .expect("resolve validates fieldValues.nodes before mapping status");
2695        let option = nodes
2696            .iter()
2697            .find(|value| value.pointer("/field/name").and_then(Value::as_str) == Some("Status"))
2698            .map(|value| required_str(value, "name"))
2699            .transpose()?;
2700        let state = optional_str(content, "state")?;
2701        if state == Some("CLOSED") {
2702            let category = match optional_str(content, "stateReason")? {
2703                None | Some("COMPLETED") => StatusCategory::Done,
2704                Some("NOT_PLANNED") => StatusCategory::Cancelled,
2705                Some(_) => StatusCategory::Unknown,
2706            };
2707            let fallback = match category {
2708                StatusCategory::Done => "Done",
2709                StatusCategory::Cancelled => "Cancelled",
2710                _ => "Closed",
2711            };
2712            return Ok(Status {
2713                category,
2714                name: option.unwrap_or(fallback).to_owned(),
2715            });
2716        }
2717        let name = option.unwrap_or("Open").to_owned();
2718        Ok(Status {
2719            category: self
2720                .statuses
2721                .category_of(&name)
2722                .unwrap_or(StatusCategory::Unknown),
2723            name,
2724        })
2725    }
2726
2727    /// The board Status option this write selects, or the refusal that says why not.
2728    ///
2729    /// For a column target the option is what the status *is*, so a board that has no such
2730    /// option is a refusal naming the status and the instance. For a closed target the
2731    /// issue's own state carries the category, and the option carries only the name a
2732    /// reader reports — so an option spelled the way this status is spelled is selected
2733    /// when the board has one, and nothing is refused when it does not.
2734    fn column_for(
2735        &self,
2736        board: &Board,
2737        status: &Status,
2738        target: &StatusTarget,
2739    ) -> Result<Option<(String, String)>, SourceError> {
2740        let (wanted, required) = match target {
2741            StatusTarget::Column(wanted) => (wanted.as_str(), true),
2742            StatusTarget::Closed(_) => (status.name.as_str(), false),
2743            StatusTarget::Disabled => return Ok(None),
2744        };
2745        let missing = |detail: &str| SourceError::Refused {
2746            message: format!(
2747                "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",
2748                category_name(status.category),
2749                self.name,
2750                category_name(status.category)
2751            ),
2752        };
2753        let Some(field) = Board::field(&board.fields, "Status")? else {
2754            return if required {
2755                Err(missing("this board has no Status field"))
2756            } else {
2757                Ok(None)
2758            };
2759        };
2760        if required_str(field, "__typename")? != "ProjectV2SingleSelectField" {
2761            return if required {
2762                Err(missing(
2763                    "this board's Status field is not a single-select field",
2764                ))
2765            } else {
2766                Ok(None)
2767            };
2768        }
2769        let option = field
2770            .get("options")
2771            .and_then(Value::as_array)
2772            .and_then(|options| {
2773                options.iter().find(|option| {
2774                    option
2775                        .get("name")
2776                        .and_then(Value::as_str)
2777                        .is_some_and(|name| name.eq_ignore_ascii_case(wanted))
2778                })
2779            });
2780        match option {
2781            None if required => Err(missing("this board does not have it")),
2782            None => Ok(None),
2783            Some(option) => Ok(Some((
2784                required_str(field, "id")?.to_owned(),
2785                required_str(option, "id")?.to_owned(),
2786            ))),
2787        }
2788    }
2789
2790    /// This instance's target for a category, refusing one it has disabled.
2791    ///
2792    /// Nothing here mutates the board's option set to make room for a status. GitHub
2793    /// documents `UpdateProjectV2FieldInput.singleSelectOptions` as *"provided values
2794    /// overwrite existing options"*, so no addition is additive and a mistake destroys the
2795    /// field and every item's status.
2796    fn resolved_target(&self, category: StatusCategory) -> Result<StatusTarget, SourceError> {
2797        let target = self.statuses.target(category).clone();
2798        if target != StatusTarget::Disabled {
2799            return Ok(target);
2800        }
2801        Err(SourceError::Refused {
2802            message: if category == StatusCategory::Draft {
2803                format!(
2804                    "status draft is disabled for source {}: draft is incompatible with this \
2805                     integration because GitHub draft issues cannot have sub-issues, and this \
2806                     source stores a project's tasks as its issue's sub-issues",
2807                    self.name
2808                )
2809            } else if category == StatusCategory::Unknown {
2810                format!(
2811                    "status {} is disabled for source {}; set status_mapping.{} of this source \
2812                     to one board Status option name; every word classified unknown is written \
2813                     to that one option",
2814                    category_name(category),
2815                    self.name,
2816                    category_name(category)
2817                )
2818            } else {
2819                format!(
2820                    "status {} is disabled for source {}; set status_mapping.{} of this source \
2821                     to a board Status option name or to a closed state",
2822                    category_name(category),
2823                    self.name,
2824                    category_name(category)
2825                )
2826            },
2827        })
2828    }
2829
2830    async fn set_item_field(
2831        &self,
2832        board_id: &str,
2833        item_id: &str,
2834        field_id: &str,
2835        value: Value,
2836    ) -> Result<(), SourceError> {
2837        let data = self
2838            .graphql(
2839                graphql::UPDATE_FIELD,
2840                json!({"input":{
2841                    "projectId":board_id,"itemId":item_id,"fieldId":field_id,"value":value
2842                }}),
2843            )
2844            .await?;
2845        let returned = data
2846            .pointer("/updateProjectV2ItemFieldValue/projectV2Item")
2847            .ok_or_else(|| SourceError::Malformed {
2848                message: "GitHub field update returned no project item".into(),
2849            })?;
2850        if required_str(returned, "id")? != item_id {
2851            return Err(SourceError::Malformed {
2852                message: "GitHub field update returned the wrong project item".into(),
2853            });
2854        }
2855        Ok(())
2856    }
2857
2858    async fn native_dependency_ids(&self, id: &NativeId) -> Result<Vec<String>, SourceError> {
2859        let mut after: Option<String> = None;
2860        let mut ids = Vec::new();
2861        loop {
2862            let data = self
2863                .graphql(
2864                    graphql::ISSUE_DEPENDENCIES,
2865                    json!({"id":id.0,"first":MAX_PAGE_SIZE,"after":after}),
2866                )
2867                .await?;
2868            let connection =
2869                data.pointer("/node/blockedBy")
2870                    .ok_or_else(|| SourceError::Malformed {
2871                        message: "GitHub dependency response has no blockedBy connection".into(),
2872                    })?;
2873            ids.extend(
2874                connection
2875                    .get("nodes")
2876                    .and_then(Value::as_array)
2877                    .ok_or_else(|| SourceError::Malformed {
2878                        message: "GitHub dependency response nodes is not an array".into(),
2879                    })?
2880                    .iter()
2881                    .map(|value| required_str(value, "id").map(str::to_owned))
2882                    .collect::<Result<Vec<_>, _>>()?,
2883            );
2884            let next = next_cursor(connection)?;
2885            if let Some(next) = &next {
2886                validate_cursor_progress(after.as_deref(), &next.0)?;
2887            }
2888            after = next.map(|cursor| cursor.0);
2889            if after.is_none() {
2890                return Ok(ids);
2891            }
2892        }
2893    }
2894
2895    async fn dependencies(
2896        &self,
2897        id: &NativeId,
2898        near_kind: ItemKind,
2899        direction: Direction,
2900        page: &PageRequest,
2901    ) -> Result<Page<DependencyEdge>, SourceError> {
2902        validate_page(page)?;
2903        let limit = page.limit.min(MAX_PAGE_SIZE) as usize;
2904        let cursor = page.cursor.as_ref().map(|c| c.0.as_str());
2905        let recorded = recorded_offset(cursor, direction)?;
2906        // Asked for even in the recorded phase, whose page reads nothing from the
2907        // connection: `__typename` is what says whether this item has a native
2908        // relationship at all, and that is what decides which far ends the reserved key is
2909        // allowed to hold.
2910        let data = self
2911            .graphql(
2912                graphql::ISSUE_DEPENDENCIES,
2913                json!({"id":id.0,"first":page.limit.min(MAX_PAGE_SIZE),
2914                       "after":if recorded.is_some() {None} else {cursor}}),
2915            )
2916            .await?;
2917        let node =
2918            data.get("node")
2919                .filter(|v| !v.is_null())
2920                .ok_or_else(|| SourceError::Refused {
2921                    message: format!(
2922                        "GitHub item {} was not found or does not support dependencies",
2923                        id.0
2924                    ),
2925                })?;
2926        let connection_name = match direction {
2927            Direction::DependsOn => "blockedBy",
2928            Direction::DependedOnBy => "blocking",
2929        };
2930        // A draft has neither `blockedBy` nor `blocking`, so nothing it depends on can be
2931        // named natively and the reserved key may hold any far end. An issue's connections
2932        // hold issues, and this source reads them at the near item's own level.
2933        let natively_names = (required_str(node, "__typename")? == "Issue").then_some(near_kind);
2934        if let Some(offset) = recorded {
2935            return Ok(recorded_page(
2936                self.recorded_edges(id, near_kind, direction, natively_names)
2937                    .await?,
2938                offset,
2939                limit,
2940            ));
2941        }
2942        if natively_names.is_none() {
2943            return Ok(recorded_page(
2944                self.recorded_edges(id, near_kind, direction, natively_names)
2945                    .await?,
2946                0,
2947                limit,
2948            ));
2949        }
2950        let connection = node
2951            .get(connection_name)
2952            .ok_or_else(|| SourceError::Malformed {
2953                message: "GitHub dependency response is missing its connection".into(),
2954            })?;
2955        let nodes = connection
2956            .get("nodes")
2957            .and_then(Value::as_array)
2958            .ok_or_else(|| SourceError::Malformed {
2959                message: "GitHub dependency response nodes is not an array".into(),
2960            })?;
2961        // `from` depends on `to`, always. GitHub spells the same relationship from either
2962        // end — `blockedBy` lists what this item waits on, `blocking` lists what waits on
2963        // it — so the near item is `from` in one direction and `to` in the other.
2964        let items = nodes
2965            .iter()
2966            .map(|value| {
2967                let related = NativeId(required_str(value, "id")?.into());
2968                let related_kind = related_kind(value)?;
2969                let (from, to) = match direction {
2970                    Direction::DependsOn => (
2971                        DependencyEndpoint::from_native(id.clone(), near_kind),
2972                        DependencyEndpoint::from_native(related, related_kind),
2973                    ),
2974                    Direction::DependedOnBy => (
2975                        DependencyEndpoint::from_native(related, related_kind),
2976                        DependencyEndpoint::from_native(id.clone(), near_kind),
2977                    ),
2978                };
2979                Ok(DependencyEdge {
2980                    from,
2981                    to,
2982                    kind: DependencyKind::Blocks,
2983                })
2984            })
2985            .collect::<Result<Vec<_>, SourceError>>()?;
2986        let mut next = next_cursor(connection)?;
2987        if let Some(next) = &next {
2988            validate_cursor_progress(cursor, &next.0)?;
2989        }
2990        if next.is_none()
2991            && !self
2992                .recorded_edges(id, near_kind, direction, natively_names)
2993                .await?
2994                .is_empty()
2995        {
2996            next = Some(Cursor(format!("{RECORDED_CURSOR}0")));
2997        }
2998        Ok(Page { items, next })
2999    }
3000
3001    /// The edges this item records under [`DependencyEdge::RECORDED_KEY`], which is where
3002    /// a far end in another source has to live: no GitHub issue relationship can name one.
3003    ///
3004    /// Only forwards. The reverse of a recorded edge is derived from the far end, and this
3005    /// source never writes one down.
3006    ///
3007    /// The metadata lives in the item's own body slot, so reading it costs one board scan.
3008    /// That is why it happens once the native connection is spent rather than on every
3009    /// page.
3010    async fn recorded_edges(
3011        &self,
3012        id: &NativeId,
3013        near_kind: ItemKind,
3014        direction: Direction,
3015        natively_names: Option<ItemKind>,
3016    ) -> Result<Vec<DependencyEdge>, SourceError> {
3017        if direction != Direction::DependsOn {
3018            return Ok(Vec::new());
3019        }
3020        let Some(item) = self
3021            .board()
3022            .await?
3023            .items
3024            .into_iter()
3025            .find(|item| item.id == *id)
3026        else {
3027            return Ok(Vec::new());
3028        };
3029        DependencyEdge::recorded(&item.slot, id, near_kind, &self.name, natively_names)
3030            .map_err(|message| SourceError::Malformed { message })
3031    }
3032
3033    fn configured_repository(&self) -> Result<&RepositoryTarget, SourceError> {
3034        self.repository
3035            .as_ref()
3036            .ok_or_else(|| SourceError::Refused {
3037                message: format!(
3038                    "source {} has no repository configured, and a GitHub Projects board has no \
3039                 repository of its own to create an issue in; set repository: owner/name on \
3040                 this source",
3041                    self.name
3042                ),
3043            })
3044    }
3045
3046    /// The repository one new issue is created in, under the rule [`RepositoryTarget`]
3047    /// states.
3048    ///
3049    /// The fallback is demanded first, whichever arm answers: a write without a configured
3050    /// repository is refused naming the field exactly as it was before the rule existed,
3051    /// so a source that could not write before cannot write now, rather than writing for
3052    /// the one item whose own field happens to decide it.
3053    ///
3054    /// Everything this refuses is refused before `createIssue`, so a refusal leaves no
3055    /// issue behind: an entry that is not a repository on [`RepositoryTarget::HOST`], an
3056    /// entry owned by someone other than the owner of the parent issue's repository —
3057    /// GitHub accepts a sub-issue from another repository of the same owner and from no
3058    /// other, so `addSubIssue` would refuse it after the issue existed — a parent the
3059    /// board does not hold, and a parent that is a draft, which GitHub gives no sub-issues,
3060    /// both of which `addSubIssue` would likewise refuse too late. Whether the entry exists
3061    /// and is visible to the token is checked where its node id is resolved, still before
3062    /// `createIssue`. The parent is read off `board`, which is completed from
3063    /// this process's own record, so a project created moments ago in this command answers
3064    /// though GitHub's board read has not caught up.
3065    fn creation_target(
3066        &self,
3067        board: &Board,
3068        incoming: &Incoming<'_>,
3069    ) -> Result<RepositoryTarget, SourceError> {
3070        let fallback = self.configured_repository()?;
3071        let what = |incoming: &Incoming<'_>| {
3072            format!(
3073                "{} {:?}",
3074                incoming.written.kind().describes(),
3075                incoming.title
3076            )
3077        };
3078        let parent = incoming
3079            .parent
3080            .map(|parent| {
3081                board
3082                    .items
3083                    .iter()
3084                    .find(|item| item.id == *parent)
3085                    .ok_or_else(|| SourceError::Refused {
3086                        message: format!(
3087                            "GitHub project issue {} was not found on the board of source {}, \
3088                             so {} cannot be filed under it",
3089                            parent.0,
3090                            self.name,
3091                            what(incoming)
3092                        ),
3093                    })
3094            })
3095            .transpose()?;
3096        let parents_repository = parent
3097            .map(|parent| {
3098                // A draft is on the board and so is found, but it has no repository to
3099                // place a task in and GitHub gives it no sub-issues, so `addSubIssue`
3100                // would refuse the task only once `createIssue` had made it.
3101                if parent.content_kind == ContentKind::DraftIssue {
3102                    return Err(SourceError::Refused {
3103                        message: format!(
3104                            "GitHub project item {} on the board of source {} is a draft, \
3105                             which cannot have sub-issues, so {} cannot be filed under it",
3106                            parent.id.0,
3107                            self.name,
3108                            what(incoming)
3109                        ),
3110                    });
3111                }
3112                // An issue's repository is where a sub-issue is placed and whose owner it
3113                // is compared against, so a parent whose repository this source cannot
3114                // spell as `owner/name` — GitHub's login grammar is wider than this
3115                // source's floor — is one nothing can be filed under.
3116                parent
3117                    .own_repository
3118                    .as_ref()
3119                    .and_then(|origin| RepositoryTarget::from_origin(origin).ok())
3120                    .ok_or_else(|| SourceError::Malformed {
3121                        message: format!(
3122                            "GitHub project issue {} on the board of source {} is in {}, which \
3123                             is not a {}/owner/name repository this source can place {} in",
3124                            parent.id.0,
3125                            self.name,
3126                            parent
3127                                .own_repository
3128                                .as_ref()
3129                                .map_or("no repository", Repository::as_str),
3130                            RepositoryTarget::HOST,
3131                            what(incoming)
3132                        ),
3133                    })
3134            })
3135            .transpose()?;
3136        match incoming.repositories {
3137            [named] => {
3138                let target =
3139                    RepositoryTarget::from_origin(named).map_err(|_| SourceError::Refused {
3140                        message: format!(
3141                            "{} names repository {}, which is not a {}/owner/name repository \
3142                             source {} can create an issue in; name one that is, or name none",
3143                            what(incoming),
3144                            named.as_str(),
3145                            RepositoryTarget::HOST,
3146                            self.name
3147                        ),
3148                    })?;
3149                if let Some(parents) = &parents_repository
3150                    && parents.owner != target.owner
3151                {
3152                    return Err(SourceError::Refused {
3153                        message: format!(
3154                            "{} names repository {}, owned by {}, but its project's issue is in \
3155                             {}, owned by {}, and GitHub files a sub-issue only in a repository \
3156                             of the same owner as its parent issue; name a repository of {}, or \
3157                             name none",
3158                            what(incoming),
3159                            target.slug(),
3160                            target.owner,
3161                            parents.slug(),
3162                            parents.owner,
3163                            parents.owner
3164                        ),
3165                    });
3166                }
3167                Ok(target)
3168            }
3169            _ => Ok(parents_repository.unwrap_or_else(|| fallback.clone())),
3170        }
3171    }
3172
3173    /// The node id of the repository `incoming` is being created in, or the refusal naming
3174    /// the item and the repository the token cannot see.
3175    ///
3176    /// Resolved once per command per repository; see [`Self::repository_cache`].
3177    async fn repository_id(
3178        &self,
3179        repository: &RepositoryTarget,
3180        incoming: &Incoming<'_>,
3181    ) -> Result<String, SourceError> {
3182        if let Some(id) = self.repository_cache()?.get(repository).cloned() {
3183            return Ok(id);
3184        }
3185        let data = self
3186            .graphql(
3187                graphql::REPOSITORY,
3188                json!({"owner":repository.owner,"name":repository.name}),
3189            )
3190            .await?;
3191        let node = data
3192            .get("repository")
3193            .filter(|value| !value.is_null())
3194            .ok_or_else(|| SourceError::Refused {
3195                message: format!(
3196                    "GitHub repository {} was not found or is not visible to the token, so {} \
3197                     {:?} cannot be created in it",
3198                    repository.slug(),
3199                    incoming.written.kind().describes(),
3200                    incoming.title
3201                ),
3202            })?;
3203        let id = required_str(node, "id")?.to_owned();
3204        self.repository_cache()?
3205            .insert(repository.clone(), id.clone());
3206        Ok(id)
3207    }
3208
3209    fn repository_cache(
3210        &self,
3211    ) -> Result<std::sync::MutexGuard<'_, BTreeMap<RepositoryTarget, String>>, SourceError> {
3212        self.repository_cache
3213            .lock()
3214            .map_err(|_| SourceError::Unavailable {
3215                message: "this source's record of the destination repository was left \
3216                          inconsistent by an earlier failure; next: run the command again"
3217                    .into(),
3218            })
3219    }
3220
3221    /// Create or update one board item, whichever kind it is.
3222    async fn write_item(
3223        &self,
3224        incoming: &Incoming<'_>,
3225        target: Option<&NativeId>,
3226        depends_on: &[DependencyEdge],
3227    ) -> Result<NativeId, SourceError> {
3228        // Refused before anything is read or written: a task or a project titled the way
3229        // this board spells a document would land as an issue this same source reads back
3230        // as a document, so the field this destination cannot carry is named rather than
3231        // written and silently reclassified.
3232        if let Written::Work(kind, _) = incoming.written
3233            && incoming.title.starts_with(DESIGN_TITLE_PREFIX)
3234        {
3235            return Err(SourceError::Refused {
3236                message: format!(
3237                    "the title of this {} begins {DESIGN_TITLE_PREFIX:?}, which is how source {} \
3238                     spells a document, so it would read back as one rather than as a {}; \
3239                     retitle it, or copy it as a document",
3240                    kind.marker(),
3241                    self.name,
3242                    kind.marker()
3243                ),
3244            });
3245        }
3246        let board = self.board().await?;
3247        let status_target = incoming
3248            .written
3249            .status()
3250            .map(|status| self.resolved_target(status.category))
3251            .transpose()?;
3252        let column = match (incoming.written.status(), status_target.as_ref()) {
3253            (Some(status), Some(target)) => self.column_for(&board, status, target)?,
3254            _ => None,
3255        };
3256        let existing = target
3257            .map(|target| {
3258                board
3259                    .items
3260                    .iter()
3261                    .find(|item| item.id == *target)
3262                    .ok_or_else(|| SourceError::Refused {
3263                        message: format!("GitHub destination item {} was not found", target.0),
3264                    })
3265            })
3266            .transpose()?;
3267        let content_kind = existing.map_or(ContentKind::Issue, |item| item.content_kind);
3268        if content_kind == ContentKind::DraftIssue {
3269            if let (Some(StatusTarget::Closed(_)), Some(status)) =
3270                (status_target.as_ref(), incoming.written.status())
3271            {
3272                return Err(SourceError::Refused {
3273                    message: format!(
3274                        "status {} of source {} closes the item's issue, and GitHub draft items \
3275                         have no open or closed state",
3276                        category_name(status.category),
3277                        self.name
3278                    ),
3279                });
3280            }
3281            if incoming.parent.is_some() {
3282                return Err(SourceError::Refused {
3283                    message: "GitHub draft items cannot be a project's sub-issue".into(),
3284                });
3285            }
3286        }
3287        match existing {
3288            Some(item) if content_kind == ContentKind::Issue => {
3289                if item.labels != incoming.labels {
3290                    return Err(SourceError::Refused {
3291                        message: "GitHub issue labels differ from the labels being written".into(),
3292                    });
3293                }
3294            }
3295            _ => {
3296                if !incoming.labels.is_empty() {
3297                    return Err(SourceError::Refused {
3298                        message: "GitHub items created by this destination carry no labels".into(),
3299                    });
3300                }
3301            }
3302        }
3303
3304        // An existing issue is never moved; a new one is created where the rule says. The
3305        // repository the issue really lives in is what the slot below is written against,
3306        // so a single entry that is where the issue is created travels as no key at all,
3307        // and the read side derives it back from the issue.
3308        let (own_repository, creation_target) = match existing {
3309            Some(item) => (item.own_repository.clone(), None),
3310            None => {
3311                let target = self.creation_target(&board, incoming)?;
3312                let origin = Repository::try_from(target.origin())
3313                    .map_err(|message| SourceError::Config { message })?;
3314                (Some(origin), Some(target))
3315            }
3316        };
3317        let (native, fallback) = self
3318            .partition_edges(&board, incoming.written.kind(), content_kind, depends_on)
3319            .await?;
3320        let slot = slot_metadata(incoming, own_repository.as_ref(), &fallback);
3321        let body = compose_body(incoming.content, &slot)?;
3322        // Read before anything is created, for the reason the field below is: a value
3323        // this destination cannot store has to refuse, and refusing after `createIssue`
3324        // would leave an issue behind that nothing asked for. The engine writes a
3325        // qualified id here; a caller handing this key anything else is told so rather
3326        // than having it silently stored as no origin at all.
3327        // 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.
3328        let origin = match incoming.metadata.get(ORIGIN_KEY) {
3329            None => "",
3330            Some(Value::String(origin)) => origin.as_str(),
3331            Some(other) => {
3332                return Err(SourceError::Refused {
3333                    message: format!(
3334                        "{ORIGIN_KEY} holds a qualified id spelled as a string, and this item's \
3335                         is {other}"
3336                    ),
3337                });
3338            }
3339        };
3340        // Resolved before anything is created: a board that cannot carry the copy origin
3341        // has to refuse the write, and refusing it after `createIssue` would leave an
3342        // issue behind that nothing asked for.
3343        let origin_field = match Board::field(&board.fields, ORIGIN_FIELD)? {
3344            Some(field) => {
3345                if required_str(field, "__typename")? != "ProjectV2Field" {
3346                    return Err(SourceError::Refused {
3347                        message: format!(
3348                            "GitHub board source-owned {ORIGIN_FIELD} field is not a text field"
3349                        ),
3350                    });
3351                }
3352                Some(required_str(field, "id")?.to_owned())
3353            }
3354            None if incoming.metadata.contains_key(ORIGIN_KEY) => {
3355                return Err(SourceError::Refused {
3356                    message: format!(
3357                        "GitHub board has no source-owned {ORIGIN_FIELD} text field, and the \
3358                         item carries {ORIGIN_KEY}; add a text field named {ORIGIN_FIELD} to \
3359                         the board"
3360                    ),
3361                });
3362            }
3363            None => None,
3364        };
3365
3366        let (content_id, item_id, url) = match existing {
3367            Some(item) => {
3368                self.update_existing(item, incoming, &body, status_target.as_ref())
3369                    .await?;
3370                (item.id.clone(), item.item_id.clone(), item.url.clone())
3371            }
3372            None => {
3373                let target = creation_target
3374                    .as_ref()
3375                    .ok_or_else(|| SourceError::Malformed {
3376                        message: "a new item was decided without a repository to create it in"
3377                            .into(),
3378                    })?;
3379                self.create_and_file_issue(&board, target, incoming, &body, status_target.as_ref())
3380                    .await?
3381            }
3382        };
3383
3384        // Creating an item here is several calls — `createIssue`, `addProjectV2ItemById`,
3385        // then each board field, the parent and the dependencies — and GitHub can fail at
3386        // any of them. Everything this source can refuse *before* the first of those is
3387        // already checked above, so what is left is GitHub itself failing part way. When it
3388        // does over an item this call created, the issue is taken back: a write that
3389        // refused must not leave an item behind that nobody asked for, and one that does
3390        // makes the retry create a second.
3391        let landed = self
3392            .finish_write(
3393                &board,
3394                incoming,
3395                &content_id,
3396                &item_id,
3397                content_kind,
3398                existing,
3399                origin_field.as_deref(),
3400                origin,
3401                column,
3402                &native,
3403            )
3404            .await;
3405        if let Err(error) = landed {
3406            if existing.is_none() {
3407                // Best effort, and the write's own failure is what the caller is told: a
3408                // refusal naming the tidy-up would hide why the write failed at all.
3409                let _ = self.delete_issue(&content_id).await;
3410            }
3411            return Err(error);
3412        }
3413
3414        // So the rest of this command reads what it just did rather than what the board
3415        // said before it. See `remember_written` for which half takes it.
3416        let remembered = Resolved {
3417            item_id,
3418            id: content_id.clone(),
3419            content_kind,
3420            kind: incoming.written.kind(),
3421            title: incoming.title.to_owned(),
3422            // The visible half of the body this write composed, split back off it the
3423            // way a read splits it — so what this record reports is what a read of the
3424            // same issue reports, rather than the person's text with the metadata slot
3425            // still on the end of it.
3426            body: metadata_body(body.clone())?.0,
3427            // A document has no status of its own; what it reads back as is whatever
3428            // the issue's own state says, which is what a re-read reports.
3429            status: incoming
3430                .written
3431                .status()
3432                .cloned()
3433                .unwrap_or_else(|| Status {
3434                    category: StatusCategory::Unknown,
3435                    name: "Open".to_owned(),
3436                }),
3437            labels: incoming.labels.to_vec(),
3438            parent: incoming.parent.cloned(),
3439            origin: (!origin.is_empty()).then(|| origin.to_owned()),
3440            // In the update path this is the item's own url, read off `existing` where the
3441            // tuple above was bound, so one expression serves both halves.
3442            url,
3443            created_at: existing.and_then(|item| item.created_at),
3444            updated_at: existing.and_then(|item| item.updated_at),
3445            own_repository,
3446            repositories: incoming.repositories.to_vec(),
3447            slot,
3448        };
3449        self.remember_written(remembered, existing.is_none())?;
3450        Ok(content_id)
3451    }
3452
3453    /// Everything a write does after the item exists: its board fields, its parent, and
3454    /// its dependencies.
3455    ///
3456    /// Split out of `write_item` so there is one place a failure past the point of no
3457    /// return is caught, rather than a tidy-up repeated at each `?` above.
3458    // llmlint: ignore[suppressions_justified] This is the tail of `write_item` lifted out
3459    // so there is one place a failure past the point of no return is caught, and its
3460    // arguments are exactly the values that tail already had in scope. Bundling them into a
3461    // struct would describe no concept — it would be "the arguments of this function" — and
3462    // would put the whole of `write_item`'s locals behind one more indirection.
3463    #[allow(clippy::too_many_arguments)]
3464    async fn finish_write(
3465        &self,
3466        board: &Board,
3467        incoming: &Incoming<'_>,
3468        content_id: &NativeId,
3469        item_id: &str,
3470        content_kind: ContentKind,
3471        existing: Option<&Resolved>,
3472        origin_field: Option<&str>,
3473        origin: &str,
3474        column: Option<(String, String)>,
3475        native: &[String],
3476    ) -> Result<(), SourceError> {
3477        if let Some(field_id) = origin_field {
3478            self.set_item_field(&board.id, item_id, field_id, json!({"text":origin}))
3479                .await?;
3480        }
3481
3482        if let Some((field_id, option_id)) = column {
3483            self.set_item_field(
3484                &board.id,
3485                item_id,
3486                &field_id,
3487                json!({"singleSelectOptionId":option_id}),
3488            )
3489            .await?;
3490        }
3491
3492        if content_kind == ContentKind::Issue {
3493            self.reparent(
3494                existing.and_then(|item| item.parent.clone()),
3495                content_id,
3496                incoming.parent,
3497            )
3498            .await?;
3499            // A document takes part in no dependency graph, so writing one neither reads
3500            // nor changes the issue's own `blockedBy` relationships. Reconciling them
3501            // against the empty list a document write carries would *delete* whatever
3502            // relationships a person had made on that issue, which is a write nobody
3503            // asked for.
3504            if incoming.written.kind() != BoardKind::Document {
3505                self.reconcile_blocked_by(content_id, native).await?;
3506            }
3507        }
3508        Ok(())
3509    }
3510
3511    /// Delete one issue, which takes its board item with it.
3512    async fn delete_issue(&self, id: &NativeId) -> Result<(), SourceError> {
3513        let data = self
3514            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
3515            .await?;
3516        data.pointer("/deleteIssue/repository")
3517            .filter(|value| !value.is_null())
3518            .ok_or_else(|| SourceError::Malformed {
3519                message: "GitHub issue deletion returned no repository".into(),
3520            })?;
3521        self.forget(id)?;
3522        Ok(())
3523    }
3524
3525    /// Remove one item this copy created, so a copy that could not finish leaves the board
3526    /// as it found it.
3527    ///
3528    /// Deleting the issue takes its board item with it, so there is no second mutation to
3529    /// keep in step. An id the board does not hold is not an error: the item is already
3530    /// gone, which is the state this asks for.
3531    async fn delete_item(&self, id: &NativeId) -> Result<(), SourceError> {
3532        let board = self.board().await?;
3533        let Some(item) = board.items.iter().find(|item| item.id == *id) else {
3534            return Ok(());
3535        };
3536        if item.content_kind == ContentKind::DraftIssue {
3537            return Err(SourceError::Refused {
3538                message: format!(
3539                    "GitHub item {} is a draft, and this source removes an item by deleting \
3540                     its issue; next: remove it from the board by hand",
3541                    id.0
3542                ),
3543            });
3544        }
3545        let data = self
3546            .graphql(graphql::DELETE_ISSUE, json!({"input":{"issueId":id.0}}))
3547            .await?;
3548        data.pointer("/deleteIssue/repository")
3549            .filter(|value| !value.is_null())
3550            .ok_or_else(|| SourceError::Malformed {
3551                message: "GitHub issue deletion returned no repository".into(),
3552            })?;
3553        self.forget(id)?;
3554        Ok(())
3555    }
3556
3557    /// Which far ends this item's own `blockedBy` relationship holds, and which it cannot.
3558    async fn partition_edges(
3559        &self,
3560        board: &Board,
3561        near_kind: BoardKind,
3562        near_content: ContentKind,
3563        depends_on: &[DependencyEdge],
3564    ) -> Result<(Vec<String>, Vec<DependencyEdge>), SourceError> {
3565        let mut native = Vec::new();
3566        let mut fallback = Vec::new();
3567        for edge in depends_on {
3568            let same_source = edge
3569                .to
3570                .source()
3571                .is_none_or(|source| source == self.name.as_str());
3572            // A qualified id's source segment runs to its *first* colon — `GlobalId` and
3573            // `DependencyEndpoint::source` both read it that way — and a native id may hold
3574            // colons of its own, so the far end is everything after that one separator.
3575            // Splitting at the last would truncate `work:urn:task:7` to `7`.
3576            let far_id = if edge.to.is_qualified() {
3577                edge.to
3578                    .id()
3579                    .split_once(':')
3580                    .map_or(edge.to.id(), |(_, native)| native)
3581            } else {
3582                edge.to.id()
3583            };
3584            let far = if same_source {
3585                Some(
3586                    board
3587                        .items
3588                        .iter()
3589                        .find(|item| item.id.0 == far_id)
3590                        .ok_or_else(|| SourceError::Refused {
3591                            message: format!("GitHub dependency item {far_id} was not found"),
3592                        })?,
3593                )
3594            } else {
3595                None
3596            };
3597            // The caller says which kind the far end is, and this board holds the far end
3598            // itself, so a disagreement is settled here rather than stored: recorded, the
3599            // wrong kind would read back as a cross-level edge that never existed; written
3600            // natively, it would name a relationship of a different level than the caller
3601            // asked for.
3602            //
3603            // A far end this board holds as a *document* fails the same comparison and is
3604            // refused by the same sentence: `ItemKind` has no document variant because
3605            // nothing may point at one, so no caller can name it correctly and the refusal
3606            // is the only honest answer.
3607            if let Some(disagreeing) = far.filter(|far| far.kind != BoardKind::Work(edge.to.kind)) {
3608                return Err(SourceError::Refused {
3609                    message: format!(
3610                        "GitHub dependency item {far_id} is a {} of this board, and this item \
3611                         names it as a {}; record the kind it is",
3612                        disagreeing.kind.describes(),
3613                        edge.to.kind.marker()
3614                    ),
3615                });
3616            }
3617            // A draft has neither `blockedBy` nor `blocking`, so no edge of one is native
3618            // however the far end is spelled — and one classified native here would be
3619            // written nowhere at all, because a draft's native reconciliation never runs.
3620            let native_here = near_content == ContentKind::Issue
3621                && far.is_some_and(|far| {
3622                    far.content_kind == ContentKind::Issue
3623                        && BoardKind::Work(edge.to.kind) == near_kind
3624                });
3625            if native_here {
3626                native.push(far_id.to_owned());
3627            } else {
3628                fallback.push(edge.clone());
3629            }
3630        }
3631        Ok((native, fallback))
3632    }
3633
3634    async fn update_existing(
3635        &self,
3636        item: &Resolved,
3637        incoming: &Incoming<'_>,
3638        body: &Option<String>,
3639        status_target: Option<&StatusTarget>,
3640    ) -> Result<(), SourceError> {
3641        let title = incoming.written_title();
3642        let (operation, input, pointer) = match item.content_kind {
3643            ContentKind::DraftIssue => (
3644                graphql::UPDATE_DRAFT,
3645                json!({"draftIssueId":item.id.0,"title":title,"body":body}),
3646                "/updateProjectV2DraftIssue/draftIssue",
3647            ),
3648            ContentKind::Issue => (
3649                graphql::UPDATE_ISSUE,
3650                json!({"id":item.id.0,"title":title,"body":body,
3651                       "stateInput":state_input(status_target)}),
3652                "/updateIssue/issue",
3653            ),
3654        };
3655        let data = self.graphql(operation, json!({"input":input})).await?;
3656        let returned = data
3657            .pointer(pointer)
3658            .ok_or_else(|| SourceError::Malformed {
3659                message: "GitHub item update returned no item".into(),
3660            })?;
3661        if required_str(returned, "id")? != item.id.0 {
3662            return Err(SourceError::Malformed {
3663                message: "GitHub item update returned the wrong item".into(),
3664            });
3665        }
3666        Ok(())
3667    }
3668
3669    /// Creates one issue, files it on the board, and closes it when the status says so.
3670    ///
3671    /// Three calls rather than one: `createIssue` needs a repository and answers with an
3672    /// issue that is on no board, `addProjectV2ItemById` is what puts it there, and a
3673    /// closed status is a state of the issue rather than a field of the board item.
3674    /// Creates the issue, files it on the board, and reports what a read of it would say:
3675    /// its content id, its board item id, and the web address GitHub gave it.
3676    ///
3677    /// The address comes back here because this is the only place it is known before
3678    /// GitHub's own board read catches up — an item this run created answers the reads
3679    /// that follow it out of the record below, and one remembered without its address
3680    /// would report no location for the rest of the run.
3681    async fn create_and_file_issue(
3682        &self,
3683        board: &Board,
3684        repository: &RepositoryTarget,
3685        incoming: &Incoming<'_>,
3686        body: &Option<String>,
3687        status_target: Option<&StatusTarget>,
3688    ) -> Result<(NativeId, String, Option<String>), SourceError> {
3689        let repository_id = self.repository_id(repository, incoming).await?;
3690        let data = self
3691            .graphql(
3692                graphql::CREATE_ISSUE,
3693                json!({"input":{
3694                    "repositoryId":repository_id,"title":incoming.written_title(),"body":body
3695                }}),
3696            )
3697            .await?;
3698        let created = data
3699            .pointer("/createIssue/issue")
3700            .filter(|value| !value.is_null())
3701            .ok_or_else(|| SourceError::Malformed {
3702                message: "GitHub issue creation returned no issue".into(),
3703            })?;
3704        let content_id = NativeId(required_str(created, "id")?.to_owned());
3705        // Optional although GitHub's schema makes it non-null: the issue exists by now, so
3706        // a response without it is not worth failing a landed write over — the item simply
3707        // reports no location until the board read catches up, which is what it did before.
3708        let url = optional_str(created, "url")?.map(str::to_owned);
3709        // The issue exists from here on, so a failure filing it on the board takes it
3710        // back: an issue in the repository that is on no board is an item nobody asked for
3711        // and nothing here would find again.
3712        let added = match self
3713            .graphql(
3714                graphql::ADD_TO_BOARD,
3715                json!({"input":{"projectId":board.id,"contentId":content_id.0}}),
3716            )
3717            .await
3718        {
3719            Ok(added) => added,
3720            Err(error) => {
3721                let _ = self.delete_issue(&content_id).await;
3722                return Err(error);
3723            }
3724        };
3725        let item = added
3726            .pointer("/addProjectV2ItemById/item")
3727            .filter(|value| !value.is_null())
3728            .ok_or_else(|| SourceError::Malformed {
3729                message: "GitHub board addition returned no project item".into(),
3730            })?;
3731        if let Some(StatusTarget::Closed(_)) = status_target {
3732            let closed = self
3733                .graphql(
3734                    graphql::UPDATE_ISSUE,
3735                    json!({"input":{"id":content_id.0,"stateInput":state_input(status_target)}}),
3736                )
3737                .await?;
3738            let returned =
3739                closed
3740                    .pointer("/updateIssue/issue")
3741                    .ok_or_else(|| SourceError::Malformed {
3742                        message: "GitHub item update returned no item".into(),
3743                    })?;
3744            if required_str(returned, "id")? != content_id.0 {
3745                return Err(SourceError::Malformed {
3746                    message: "GitHub item update returned the wrong item".into(),
3747                });
3748            }
3749        }
3750        Ok((content_id, required_str(item, "id")?.to_owned(), url))
3751    }
3752
3753    /// Move one issue under the project it now belongs to, or out of the one it left.
3754    async fn reparent(
3755        &self,
3756        held: Option<NativeId>,
3757        child: &NativeId,
3758        wanted: Option<&NativeId>,
3759    ) -> Result<(), SourceError> {
3760        if held.as_ref() == wanted {
3761            return Ok(());
3762        }
3763        if let Some(held) = &held {
3764            self.sub_issue(graphql::REMOVE_SUB_ISSUE, held, child, "removeSubIssue")
3765                .await?;
3766        }
3767        if let Some(wanted) = wanted {
3768            self.sub_issue(graphql::ADD_SUB_ISSUE, wanted, child, "addSubIssue")
3769                .await?;
3770        }
3771        Ok(())
3772    }
3773
3774    async fn sub_issue(
3775        &self,
3776        operation: &str,
3777        parent: &NativeId,
3778        child: &NativeId,
3779        root: &str,
3780    ) -> Result<(), SourceError> {
3781        let data = self
3782            .graphql(
3783                operation,
3784                json!({"input":{"issueId":parent.0,"subIssueId":child.0}}),
3785            )
3786            .await?;
3787        let issue =
3788            data.pointer(&format!("/{root}/issue"))
3789                .ok_or_else(|| SourceError::Malformed {
3790                    message: "GitHub sub-issue update returned no issue".into(),
3791                })?;
3792        let sub =
3793            data.pointer(&format!("/{root}/subIssue"))
3794                .ok_or_else(|| SourceError::Malformed {
3795                    message: "GitHub sub-issue update returned no sub-issue".into(),
3796                })?;
3797        if required_str(issue, "id")? != parent.0 || required_str(sub, "id")? != child.0 {
3798            return Err(SourceError::Malformed {
3799                message: "GitHub sub-issue update returned the wrong issues".into(),
3800            });
3801        }
3802        Ok(())
3803    }
3804
3805    async fn reconcile_blocked_by(
3806        &self,
3807        content_id: &NativeId,
3808        native: &[String],
3809    ) -> Result<(), SourceError> {
3810        let current = self.native_dependency_ids(content_id).await?;
3811        for (operation, far_id) in current
3812            .iter()
3813            .filter(|id| !native.contains(id))
3814            .map(|id| (graphql::REMOVE_BLOCKED_BY, id))
3815            .chain(
3816                native
3817                    .iter()
3818                    .filter(|id| !current.contains(id))
3819                    .map(|id| (graphql::ADD_BLOCKED_BY, id)),
3820            )
3821        {
3822            let data = self
3823                .graphql(
3824                    operation,
3825                    json!({"input":{"issueId":content_id.0,"blockingIssueId":far_id}}),
3826                )
3827                .await?;
3828            let root = if operation == graphql::ADD_BLOCKED_BY {
3829                "addBlockedBy"
3830            } else {
3831                "removeBlockedBy"
3832            };
3833            let issue =
3834                data.pointer(&format!("/{root}/issue"))
3835                    .ok_or_else(|| SourceError::Malformed {
3836                        message: "GitHub dependency update returned no issue".into(),
3837                    })?;
3838            let blocker = data
3839                .pointer(&format!("/{root}/blockingIssue"))
3840                .ok_or_else(|| SourceError::Malformed {
3841                    message: "GitHub dependency update returned no blocking issue".into(),
3842                })?;
3843            if required_str(issue, "id")? != content_id.0 || required_str(blocker, "id")? != far_id
3844            {
3845                return Err(SourceError::Malformed {
3846                    message: "GitHub dependency update returned the wrong issues".into(),
3847                });
3848            }
3849        }
3850        Ok(())
3851    }
3852}
3853
3854/// What resolving one node id reached; see [`GitHubProjectsSource::reach`].
3855enum Reached {
3856    /// An issue this board holds, resolved into everything this source reports about it.
3857    Held(Box<Resolved>),
3858    /// Nothing this board holds: no such node, or a node on some other board.
3859    Nothing,
3860    /// A board draft, which exists only inside the board's own item connection.
3861    Draft,
3862}
3863
3864/// What GitHub says when a string is not a node id it can resolve.
3865///
3866/// Matched because it is the ordinary answer to a project selector naming a project by its
3867/// *name*, and reporting that as a failure would make naming one impossible. It is read
3868/// off the refusal GitHub sent, never guessed from the shape of the string: this source
3869/// does not define the syntax of a GitHub node id and would be wrong about it.
3870const UNRESOLVABLE_NODE: &str = "could not resolve to a node";
3871
3872/// Whether this refusal is GitHub saying the id names no node at all.
3873fn unresolvable_node(error: &SourceError) -> bool {
3874    matches!(error, SourceError::Refused { message }
3875        if message.to_ascii_lowercase().contains(UNRESOLVABLE_NODE))
3876}
3877
3878/// One project name, as a search qualifier which filters on it at the server.
3879///
3880/// Quoted so the whole title is one phrase rather than a bag of words, with the two
3881/// characters GitHub's own quoting grammar gives a meaning inside a quoted phrase escaped
3882/// the way it documents. A title matched here is still compared for equality afterwards:
3883/// the qualifier narrows what the server sends, and this source decides what it names.
3884fn title_qualifier(name: &str) -> String {
3885    let escaped = name.replace('\\', "\\\\").replace('"', "\\\"");
3886    format!("in:title \"{escaped}\"")
3887}
3888
3889/// The board, and every item on it this source reports.
3890#[derive(Clone)]
3891struct Board {
3892    id: String,
3893    fields: Value,
3894    items: Vec<Resolved>,
3895}
3896
3897impl Board {
3898    fn field<'a>(fields: &'a Value, name: &str) -> Result<Option<&'a Value>, SourceError> {
3899        complete_connection(fields, "project fields", NESTED_PAGE_SIZE)?;
3900        let nodes = fields
3901            .get("nodes")
3902            .and_then(Value::as_array)
3903            .ok_or_else(|| SourceError::Malformed {
3904                message: "GitHub project fields.nodes is not an array".into(),
3905            })?;
3906        Ok(nodes
3907            .iter()
3908            .find(|field| field.get("name").and_then(Value::as_str) == Some(name)))
3909    }
3910}
3911
3912/// One board item, resolved into everything this source reports about it.
3913#[derive(Clone)]
3914struct Resolved {
3915    item_id: String,
3916    id: NativeId,
3917    content_kind: ContentKind,
3918    kind: BoardKind,
3919    title: String,
3920    body: Option<String>,
3921    status: Status,
3922    labels: Vec<Label>,
3923    parent: Option<NativeId>,
3924    // 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.
3925    origin: Option<String>,
3926    url: Option<String>,
3927    created_at: Option<DateTime<Utc>>,
3928    updated_at: Option<DateTime<Utc>>,
3929    own_repository: Option<Repository>,
3930    repositories: Vec<Repository>,
3931    slot: BTreeMap<String, Value>,
3932}
3933
3934impl Resolved {
3935    /// The metadata a caller sees: their own keys, plus the copy origin this source keeps
3936    /// in a field of its own, and none of the three keys that are only an encoding.
3937    fn metadata(&self) -> BTreeMap<String, Value> {
3938        let mut metadata = self.slot.clone();
3939        metadata.remove(Repository::METADATA_KEY);
3940        metadata.remove(DependencyEdge::RECORDED_KEY);
3941        metadata.remove(ItemKind::METADATA_KEY);
3942        if let Some(origin) = &self.origin {
3943            metadata.insert(ORIGIN_KEY.to_owned(), Value::String(origin.clone()));
3944        }
3945        metadata
3946    }
3947
3948    /// Where this item is, as a link a reader can open.
3949    ///
3950    /// A board is a hosted place and every issue on it has a web address, so that address
3951    /// is what "where is this?" means here — and [`Location::Url`] is what says which kind
3952    /// of place it is, so a reader knows to open it rather than to read a file out. It
3953    /// does not replace or derive from `url`: the field goes on reporting exactly what it
3954    /// reported before, and this says what that address *is*.
3955    ///
3956    /// An item GitHub gave no `url` for — a draft has none — reports no location at all
3957    /// rather than a third variant, which is the contract's "the source did not say". An
3958    /// issue this run created is not one of those: its address comes back from the
3959    /// creating mutation, so it is somewhere a reader can open from the moment it exists
3960    /// rather than from whenever the board read catches up.
3961    fn location(&self) -> Option<Location> {
3962        self.url.clone().map(Location::Url)
3963    }
3964
3965    fn task(&self) -> Task {
3966        Task {
3967            id: self.id.clone(),
3968            title: self.title.clone(),
3969            content: self.body.clone(),
3970            status: self.status.clone(),
3971            labels: self.labels.clone(),
3972            project: self.parent.clone(),
3973            url: self.url.clone(),
3974            location: self.location(),
3975            created_at: self.created_at,
3976            updated_at: self.updated_at,
3977            metadata: self.metadata(),
3978            repositories: self.repositories.clone(),
3979        }
3980    }
3981
3982    fn project(&self) -> Project {
3983        Project {
3984            id: self.id.clone(),
3985            title: self.title.clone(),
3986            content: self.body.clone(),
3987            status: self.status.clone(),
3988            labels: self.labels.clone(),
3989            url: self.url.clone(),
3990            location: self.location(),
3991            created_at: self.created_at,
3992            updated_at: self.updated_at,
3993            metadata: self.metadata(),
3994            repositories: self.repositories.clone(),
3995        }
3996    }
3997
3998    /// The same issue as a document: the project it is filed under, and no status and no
3999    /// dependencies, because a document is not work.
4000    fn document(&self) -> Document {
4001        Document {
4002            id: self.id.clone(),
4003            title: self.title.clone(),
4004            content: self.body.clone(),
4005            project: self.parent.clone(),
4006            labels: self.labels.clone(),
4007            url: self.url.clone(),
4008            location: self.location(),
4009            created_at: self.created_at,
4010            updated_at: self.updated_at,
4011            metadata: self.metadata(),
4012            repositories: self.repositories.clone(),
4013        }
4014    }
4015}
4016
4017/// What one write is, and the status that comes with being it.
4018///
4019/// One value rather than a [`BoardKind`] beside an `Option<Status>`: a document has no
4020/// status and a task or a project always has one, so "a document carrying a status" and
4021/// "a task carrying none" are states a write cannot be in rather than states every use
4022/// site below has to defend against.
4023enum Written<'a> {
4024    /// A document, which is not work and so has no status at all.
4025    Document,
4026    /// A task or a project, and the status it is being written with.
4027    Work(ItemKind, &'a Status),
4028}
4029
4030impl Written<'_> {
4031    /// Which of the board's three kinds this write is.
4032    const fn kind(&self) -> BoardKind {
4033        match self {
4034            Self::Document => BoardKind::Document,
4035            Self::Work(kind, _) => BoardKind::Work(*kind),
4036        }
4037    }
4038
4039    /// The status this write carries. A document carries none, so a write of one says
4040    /// nothing about the issue's open or closed state and selects no board `Status`
4041    /// option.
4042    const fn status(&self) -> Option<&Status> {
4043        match self {
4044            Self::Document => None,
4045            Self::Work(_, status) => Some(status),
4046        }
4047    }
4048}
4049
4050/// The item being written, in the one shape all three write methods reach.
4051struct Incoming<'a> {
4052    written: Written<'a>,
4053    /// The title a person wrote. A document's goes onto the issue with
4054    /// [`DESIGN_TITLE_PREFIX`] put back, so a round trip returns the title that went in.
4055    title: &'a str,
4056    content: Option<&'a str>,
4057    labels: &'a [Label],
4058    metadata: &'a BTreeMap<String, Value>,
4059    repositories: &'a [Repository],
4060    parent: Option<&'a NativeId>,
4061}
4062
4063impl Incoming<'_> {
4064    /// The title this write puts on the issue.
4065    fn written_title(&self) -> String {
4066        match self.written {
4067            Written::Document => format!("{DESIGN_TITLE_PREFIX}{}", self.title),
4068            Written::Work(..) => self.title.to_owned(),
4069        }
4070    }
4071}
4072
4073#[derive(Clone, Copy, PartialEq, Eq)]
4074enum ContentKind {
4075    DraftIssue,
4076    Issue,
4077}
4078
4079/// What one board issue is: a document, or the work an [`ItemKind`] names.
4080///
4081/// A type of this source's own rather than an `ItemKind` with a third variant, because
4082/// `ItemKind` names what a dependency endpoint points at and nothing may point at a
4083/// document — the contract keeps a document out of that enum deliberately. Holding the
4084/// board's three answers in one value is what makes every place that asks "which is this?"
4085/// answer all three, rather than a `document: bool` beside a `kind` that means nothing for
4086/// two thirds of the board.
4087#[derive(Clone, Copy, PartialEq, Eq)]
4088enum BoardKind {
4089    /// An issue whose title begins [`DESIGN_TITLE_PREFIX`].
4090    Document,
4091    /// Every other issue, and every draft.
4092    Work(ItemKind),
4093}
4094
4095impl BoardKind {
4096    /// How a refusal names this kind to the person reading it.
4097    const fn describes(self) -> &'static str {
4098        match self {
4099            Self::Document => "document",
4100            Self::Work(kind) => kind.marker(),
4101        }
4102    }
4103}
4104
4105/// Whether `labels` satisfies `filter`, matching by name, case-insensitively.
4106///
4107/// This is the local Markdown source's `labels_match`, spelled the same way on purpose:
4108/// the shared cross-source journeys assert one answer to one question, so two sources
4109/// that disagree about what "carries the label bug" means fail them.
4110fn labels_match(labels: &[Label], filter: &LabelFilter) -> bool {
4111    let holds = |name: &String| {
4112        labels
4113            .iter()
4114            .any(|label| label.name.eq_ignore_ascii_case(name))
4115    };
4116    (filter.any_of.is_empty() || filter.any_of.iter().any(holds))
4117        && filter.all_of.iter().all(holds)
4118        && !filter.none_of.iter().any(holds)
4119}
4120
4121/// Whether `category` is one of `statuses`. An empty list is unfiltered rather than
4122/// "keeps nothing", which is what lets a `Vec<StatusCategory>` spell no filter at all.
4123fn status_matches(category: StatusCategory, statuses: &[StatusCategory]) -> bool {
4124    statuses.is_empty() || statuses.contains(&category)
4125}
4126
4127/// Whether `title`/`content` satisfies `query`, matching case-insensitively.
4128///
4129/// `content` is the item's own prose — the body with this source's trailing metadata
4130/// comment already taken off — so a search never matches an encoding the author of the
4131/// issue never wrote.
4132fn text_matches(title: &str, content: Option<&str>, query: &TextQuery) -> bool {
4133    let terms = query.terms.to_lowercase();
4134    let in_title = title.to_lowercase().contains(&terms);
4135    let in_content = content.is_some_and(|body| body.to_lowercase().contains(&terms));
4136    match query.fields {
4137        TextFields::Title => in_title,
4138        TextFields::Content => in_content,
4139        TextFields::TitleOrContent => in_title || in_content,
4140    }
4141}
4142
4143/// Whether `task` satisfies `query`, with `project` deciding the project predicate.
4144///
4145/// The project predicate is passed separately because a read narrowed to one project has
4146/// already answered it by asking *that project* for its own items — and re-applying it
4147/// there would compare the caller's selector, which may be a project's **name**, against
4148/// the id of the project that name resolved to, and keep nothing. Every other read passes
4149/// `query.project` and applies it here, which is what keeps `projects` a predicate this
4150/// source really does apply.
4151fn task_matches(task: &Task, query: &TaskQuery, project: &ProjectFilter) -> bool {
4152    labels_match(&task.labels, &query.labels)
4153        && status_matches(task.status.category, &query.statuses)
4154        && match project {
4155            ProjectFilter::Any => true,
4156            ProjectFilter::Orphans => task.project.is_none(),
4157            ProjectFilter::Is(id) => task.project.as_ref() == Some(id),
4158        }
4159        && query
4160            .text
4161            .as_ref()
4162            .is_none_or(|text| text_matches(&task.title, task.content.as_deref(), text))
4163}
4164
4165fn project_matches(project: &Project, query: &ProjectQuery) -> bool {
4166    labels_match(&project.labels, &query.labels)
4167        && status_matches(project.status.category, &query.statuses)
4168        && query
4169            .text
4170            .as_ref()
4171            .is_none_or(|text| text_matches(&project.title, project.content.as_deref(), text))
4172}
4173
4174/// The same three predicates a task query carries, minus the status filter.
4175///
4176/// A document is not work, so it has no status for one to compare against and the query
4177/// type carries none. The project predicate is the same one — a design issue filed under a
4178/// project issue is in that project, and one filed under nothing is in none — so it is
4179/// spelled the same way here rather than answered differently.
4180fn document_matches(document: &Document, query: &DocumentQuery, project: &ProjectFilter) -> bool {
4181    labels_match(&document.labels, &query.labels)
4182        && match project {
4183            ProjectFilter::Any => true,
4184            ProjectFilter::Orphans => document.project.is_none(),
4185            ProjectFilter::Is(id) => document.project.as_ref() == Some(id),
4186        }
4187        && query
4188            .text
4189            .as_ref()
4190            .is_none_or(|text| text_matches(&document.title, document.content.as_deref(), text))
4191}
4192
4193#[async_trait::async_trait]
4194impl TaskSource for GitHubProjectsSource {
4195    fn kind(&self) -> &'static str {
4196        KIND
4197    }
4198    fn capabilities(&self) -> Capabilities {
4199        Capabilities {
4200            projects: Support::Native,
4201            documents: Support::Native,
4202            orphan_tasks: Support::Native,
4203            filter_by_label: Support::Native,
4204            filter_by_status: Support::Native,
4205            search_title: Support::Native,
4206            search_content: Support::Native,
4207            task_dependencies: DependencySupport::BothDirections,
4208            project_dependencies: DependencySupport::BothDirections,
4209            max_page_size: MAX_PAGE_SIZE,
4210        }
4211    }
4212    async fn health(&self) -> Result<Health, SourceError> {
4213        let board = self.board_page(None, 1).await?;
4214        Ok(Health {
4215            reachable: true,
4216            detail: Some(format!(
4217                "reading GitHub project {}/{} ({})",
4218                self.owner,
4219                self.project_number,
4220                required_str(&board, "title")?
4221            )),
4222        })
4223    }
4224    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError> {
4225        Ok(self
4226            .item_by_id(id)
4227            .await?
4228            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
4229            .map(|item| item.task()))
4230    }
4231    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError> {
4232        Ok(self
4233            .item_by_id(id)
4234            .await?
4235            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
4236            .map(|item| item.project()))
4237    }
4238    async fn query_tasks(
4239        &self,
4240        query: &TaskQuery,
4241        page: &PageRequest,
4242    ) -> Result<Page<Task>, SourceError> {
4243        validate_page(page)?;
4244        // A read narrowed to one project asks that project for its own tasks, so nothing
4245        // about it costs what the rest of the board holds. Every other task read is a
4246        // question about the whole board and is answered by reading it.
4247        let (held, membership) = match &query.project {
4248            ProjectFilter::Is(project) => (
4249                self.project_children(project).await?,
4250                // Answered by where these items came from; see `task_matches`.
4251                &ProjectFilter::Any,
4252            ),
4253            ProjectFilter::Any | ProjectFilter::Orphans => {
4254                (self.board().await?.items, &query.project)
4255            }
4256        };
4257        // Filtered before paged: a page of a filtered result is a page of the survivors,
4258        // never the survivors of a page.
4259        let tasks = held
4260            .iter()
4261            .filter(|item| item.kind == BoardKind::Work(ItemKind::Task))
4262            .map(Resolved::task)
4263            .filter(|task| task_matches(task, query, membership))
4264            .collect();
4265        Ok(offset_page(
4266            tasks,
4267            numeric_cursor(page.cursor.as_ref())?,
4268            page.limit.min(MAX_PAGE_SIZE) as usize,
4269        ))
4270    }
4271    async fn query_projects(
4272        &self,
4273        query: &ProjectQuery,
4274        page: &PageRequest,
4275    ) -> Result<Page<Project>, SourceError> {
4276        validate_page(page)?;
4277        // The projects a board holds are found by an issue search scoped to that board,
4278        // never by walking the board's own item connection: what tells a project from a
4279        // task is the `parent` each issue carries, which costs nothing to read.
4280        let projects = self
4281            .board_issues()
4282            .await?
4283            .iter()
4284            .filter(|item| item.kind == BoardKind::Work(ItemKind::Project))
4285            .map(Resolved::project)
4286            .filter(|project| project_matches(project, query))
4287            .collect();
4288        Ok(offset_page(
4289            projects,
4290            numeric_cursor(page.cursor.as_ref())?,
4291            page.limit.min(MAX_PAGE_SIZE) as usize,
4292        ))
4293    }
4294    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
4295        Ok(self
4296            .item_by_id(id)
4297            .await?
4298            .filter(|item| item.kind == BoardKind::Document)
4299            .map(|item| item.document()))
4300    }
4301    async fn query_documents(
4302        &self,
4303        query: &DocumentQuery,
4304        page: &PageRequest,
4305    ) -> Result<Page<Document>, SourceError> {
4306        validate_page(page)?;
4307        // Narrowed to one project, this is the same sub-issue read a task list scoped to
4308        // that project makes — a document filed under a project is a sub-issue of it too,
4309        // and which of them come back is the kind this caller asked for.
4310        let (held, membership) = match &query.project {
4311            ProjectFilter::Is(project) => (
4312                self.project_children(project).await?,
4313                // Answered by where these items came from; see `task_matches`.
4314                &ProjectFilter::Any,
4315            ),
4316            ProjectFilter::Any | ProjectFilter::Orphans => {
4317                (self.board().await?.items, &query.project)
4318            }
4319        };
4320        // Filtered before paged, exactly as a task read is: a page of a filtered result is
4321        // a page of the survivors, never the survivors of a page.
4322        let documents = held
4323            .iter()
4324            .filter(|item| item.kind == BoardKind::Document)
4325            .map(Resolved::document)
4326            .filter(|document| document_matches(document, query, membership))
4327            .collect();
4328        Ok(offset_page(
4329            documents,
4330            numeric_cursor(page.cursor.as_ref())?,
4331            page.limit.min(MAX_PAGE_SIZE) as usize,
4332        ))
4333    }
4334    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError> {
4335        validate_page(page)?;
4336        let offset = numeric_cursor(page.cursor.as_ref())?;
4337        let mut labels = self
4338            .board()
4339            .await?
4340            .items
4341            .into_iter()
4342            .flat_map(|item| item.labels)
4343            .fold(Vec::new(), |mut all, label| {
4344                if !all.iter().any(|x: &Label| x.id == label.id) {
4345                    all.push(label);
4346                }
4347                all
4348            });
4349        labels.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.0.cmp(&b.id.0)));
4350        Ok(offset_page(
4351            labels,
4352            offset,
4353            page.limit.min(MAX_PAGE_SIZE) as usize,
4354        ))
4355    }
4356    async fn task_dependencies(
4357        &self,
4358        id: &NativeId,
4359        direction: Direction,
4360        page: &PageRequest,
4361    ) -> Result<Page<DependencyEdge>, SourceError> {
4362        self.dependencies(id, ItemKind::Task, direction, page).await
4363    }
4364    async fn project_dependencies(
4365        &self,
4366        id: &NativeId,
4367        direction: Direction,
4368        page: &PageRequest,
4369    ) -> Result<Page<DependencyEdge>, SourceError> {
4370        self.dependencies(id, ItemKind::Project, direction, page)
4371            .await
4372    }
4373
4374    fn writes(&self) -> WriteSupport {
4375        WriteSupport::Supported
4376    }
4377
4378    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
4379        self.write_item(
4380            &Incoming {
4381                written: Written::Work(ItemKind::Task, &write.item.status),
4382                title: &write.item.title,
4383                content: write.item.content.as_deref(),
4384                labels: &write.item.labels,
4385                metadata: &write.item.metadata,
4386                repositories: &write.item.repositories,
4387                parent: write.item.project.as_ref(),
4388            },
4389            write.target.as_ref(),
4390            &write.depends_on,
4391        )
4392        .await
4393    }
4394
4395    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
4396        self.write_item(
4397            &Incoming {
4398                written: Written::Work(ItemKind::Project, &write.item.status),
4399                title: &write.item.title,
4400                content: write.item.content.as_deref(),
4401                labels: &write.item.labels,
4402                metadata: &write.item.metadata,
4403                repositories: &write.item.repositories,
4404                parent: None,
4405            },
4406            write.target.as_ref(),
4407            &write.depends_on,
4408        )
4409        .await
4410    }
4411
4412    /// Create or update one document, which is one issue titled the way this board spells
4413    /// a document.
4414    ///
4415    /// Everything else is exactly a task write: caller metadata goes to the same canonical
4416    /// JSON slot at the end of the body and comes back with its JSON types intact, a key
4417    /// or a field this board cannot carry is refused by name rather than dropped, a target
4418    /// naming an issue this board does not hold is refused rather than created, and an
4419    /// issue this call created is taken back when the rest of the write fails.
4420    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
4421        // A document takes part in no dependency graph, so there is no far end to write
4422        // natively and none to record: a caller naming one is told so rather than having it
4423        // stored under the reserved key, where a later read would report an edge the
4424        // contract says cannot exist.
4425        if !write.depends_on.is_empty() {
4426            return Err(SourceError::Refused {
4427                message: format!(
4428                    "this write names {} dependencies for a document, and a document takes \
4429                     part in no dependency graph; next: put the dependency on the task or \
4430                     project the document is about",
4431                    write.depends_on.len()
4432                ),
4433            });
4434        }
4435        self.write_item(
4436            &Incoming {
4437                written: Written::Document,
4438                title: &write.item.title,
4439                content: write.item.content.as_deref(),
4440                labels: &write.item.labels,
4441                metadata: &write.item.metadata,
4442                repositories: &write.item.repositories,
4443                parent: write.item.project.as_ref(),
4444            },
4445            write.target.as_ref(),
4446            &[],
4447        )
4448        .await
4449    }
4450
4451    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
4452        self.delete_item(id).await
4453    }
4454
4455    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
4456        self.delete_item(id).await
4457    }
4458
4459    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
4460        self.delete_item(id).await
4461    }
4462}
4463
4464/// Where the recorded tail of a dependency walk resumes; see
4465/// [`GitHubProjectsSource::recorded_edges`].
4466const RECORDED_CURSOR: &str = "onetaskgraph.depends_on:";
4467
4468/// The board text field this source keeps a copy's origin in.
4469///
4470/// Named after the key it holds, and held to that name by the guard below rather than by
4471/// a reader noticing.
4472const ORIGIN_FIELD: &str = "onetaskgraph.origin";
4473
4474/// The metadata key that field holds.
4475///
4476/// The engine owns this key and spells it once as `GlobalId::ORIGIN_KEY`; a plugin never
4477/// constructs or interprets the qualified id it carries. This source names it only to
4478/// route it — a short, typed value belongs in a typed field rather than in the body slot
4479/// a caller's own prose shares.
4480///
4481/// Restated rather than imported, because no plugin crate may depend on the engine. What
4482/// keeps the two spellings one contract is `scripts/check-origin-key-spelling.sh`, a
4483/// target in `check`: it reads the engine's own literal and fails naming the file and the
4484/// line when a plugin's parts from it either way. Drift here has one symptom — a copy
4485/// that creates a second item every run instead of finding the one it wrote — and that is
4486/// too late to learn it.
4487const ORIGIN_KEY: &str = "onetaskgraph.origin";
4488
4489/// Where a recorded tail resumes, refusing a cursor no walk in `direction` reported.
4490///
4491/// The reserved key holds forward edges and nothing else — the reverse of a recorded edge
4492/// is derived from the far end, never written down on the near item — so only a forward
4493/// walk ever reports one of these cursors. A reverse read carrying one is resuming a walk
4494/// it did not come from, and it is told so rather than answered with an empty page that
4495/// reads as a walk which ended.
4496fn recorded_offset(
4497    cursor: Option<&str>,
4498    direction: Direction,
4499) -> Result<Option<usize>, SourceError> {
4500    cursor
4501        .and_then(|cursor| cursor.strip_prefix(RECORDED_CURSOR))
4502        .map(|offset| {
4503            if direction != Direction::DependsOn {
4504                return Err(SourceError::Config {
4505                    message: format!(
4506                        "{RECORDED_CURSOR}{offset} resumes recorded forward edges, which a \
4507                         reverse dependency read never issues; resume it in the direction \
4508                         that reported it"
4509                    ),
4510                });
4511            }
4512            offset.parse().map_err(|_| SourceError::Config {
4513                message: format!("{RECORDED_CURSOR}{offset} is not a recorded-edge cursor"),
4514            })
4515        })
4516        .transpose()
4517}
4518
4519fn recorded_page(edges: Vec<DependencyEdge>, offset: usize, limit: usize) -> Page<DependencyEdge> {
4520    let mut page = offset_page(edges, offset, limit.max(1));
4521    page.next = page
4522        .next
4523        .map(|cursor| Cursor(format!("{RECORDED_CURSOR}{}", cursor.0)));
4524    page
4525}
4526
4527/// The kind of one issue reached through a dependency connection.
4528///
4529/// The same questions the board scan asks, over the fields the dependency document
4530/// selects, and in the same order: the design prefix first, then a sub-issue is a task,
4531/// then anything with sub-issues or the marker is a project.
4532///
4533/// # Errors
4534///
4535/// A far end this board holds as a document is refused rather than reported. The two
4536/// answers that are not refusals would both be wrong: reporting it as a task names an id
4537/// no task read of this source can find, and reporting it as a project names one no
4538/// project read can. There is no third value to return — `ItemKind` has no document
4539/// variant, because nothing may point at a document — so the relationship itself is what
4540/// the person is told about.
4541fn related_kind(value: &Value) -> Result<ItemKind, SourceError> {
4542    let id = required_str(value, "id")?;
4543    if required_str(value, "title")?.starts_with(DESIGN_TITLE_PREFIX) {
4544        return Err(SourceError::Refused {
4545            message: format!(
4546                "GitHub issue {id} is a document of this board — its title begins \
4547                 {DESIGN_TITLE_PREFIX:?} — and nothing may depend on a document or be depended \
4548                 on by one; next: remove that issue's blocking relationship on this board"
4549            ),
4550        });
4551    }
4552    let parent = optional_str(value.get("parent").unwrap_or(&Value::Null), "id")?;
4553    if parent.is_some() {
4554        return Ok(ItemKind::Task);
4555    }
4556    let (_, slot) = metadata_body(optional_str(value, "body")?.map(str::to_owned))?;
4557    let marked = ItemKind::from_metadata(&slot).map_err(|message| SourceError::Malformed {
4558        message: format!("GitHub issue {id}: {message}"),
4559    })?;
4560    let sub_issues = sub_issue_total(value)?;
4561    Ok(if sub_issues > 0 || marked == Some(ItemKind::Project) {
4562        ItemKind::Project
4563    } else {
4564        ItemKind::Task
4565    })
4566}
4567
4568/// The `IssueStateUpdateInput` one status target asks for.
4569///
4570/// `stateInput` and `state` are mutually exclusive on `UpdateIssueInput`, and only this
4571/// one is ever sent. A non-terminal status always asks for `OPEN`, which is what reopens
4572/// a currently-closed issue: without that the item would read back `Unknown` and a copy
4573/// would report a change forever. A document has no status at all, and asks for neither.
4574fn state_input(target: Option<&StatusTarget>) -> Value {
4575    match target {
4576        Some(StatusTarget::Closed(reason)) => {
4577            json!({"value":"CLOSED","stateReason":reason.reason()})
4578        }
4579        Some(StatusTarget::Column(_) | StatusTarget::Disabled) => json!({"value":"OPEN"}),
4580        // A document has no status, so a write of one says nothing about the issue's open
4581        // or closed state rather than forcing it open: `stateInput` is what carries that
4582        // instruction, and an explicit null asks for no change to it.
4583        None => Value::Null,
4584    }
4585}
4586
4587/// The metadata one write stores in the item's body slot.
4588///
4589/// The typed fields travel as themselves, so the three reserved keys are rebuilt here
4590/// rather than carried: the kind marker so an empty project stays readable, the
4591/// repository list only when it is not exactly the issue's own repository, and the far
4592/// ends no relationship here can name.
4593fn slot_metadata(
4594    incoming: &Incoming<'_>,
4595    own_repository: Option<&Repository>,
4596    fallback: &[DependencyEdge],
4597) -> BTreeMap<String, Value> {
4598    let mut metadata = incoming.metadata.clone();
4599    metadata.remove(ORIGIN_KEY);
4600    match incoming.written.kind() {
4601        BoardKind::Work(kind) => metadata.insert(
4602            ItemKind::METADATA_KEY.to_owned(),
4603            Value::String(kind.marker().to_owned()),
4604        ),
4605        // A document is told by its title, so it carries no kind marker: that key names
4606        // what a dependency endpoint points at, and nothing may point at a document.
4607        BoardKind::Document => metadata.remove(ItemKind::METADATA_KEY),
4608    };
4609    let derivable = own_repository
4610        .map(|own| incoming.repositories == [own.clone()])
4611        .unwrap_or(incoming.repositories.is_empty());
4612    if derivable {
4613        metadata.remove(Repository::METADATA_KEY);
4614    } else {
4615        metadata.insert(
4616            Repository::METADATA_KEY.to_owned(),
4617            Value::Array(
4618                incoming
4619                    .repositories
4620                    .iter()
4621                    .map(|repository| Value::String(repository.as_str().to_owned()))
4622                    .collect(),
4623            ),
4624        );
4625    }
4626    if fallback.is_empty() {
4627        metadata.remove(DependencyEdge::RECORDED_KEY);
4628    } else {
4629        metadata.insert(
4630            DependencyEdge::RECORDED_KEY.to_owned(),
4631            Value::Array(
4632                fallback
4633                    .iter()
4634                    .map(|edge| json!({"id":edge.to.id(),"kind":edge.to.kind}))
4635                    .collect(),
4636            ),
4637        );
4638    }
4639    metadata
4640}
4641
4642/// Every label one item carries, from its content's own connection and nowhere else.
4643///
4644/// There is no second place to read one from: no document this source sends selects the
4645/// board's built-in `Labels` field, because GitHub derives it from the content and a draft
4646/// cannot carry one at all. The module documentation records the three schema facts that
4647/// settle it.
4648fn labels(content: &Value) -> Result<Vec<Label>, SourceError> {
4649    optional_nodes(content.get("labels"), "content labels")?
4650        .into_iter()
4651        .flatten()
4652        .map(|v| {
4653            Ok(Label {
4654                id: NativeId(required_str(v, "id")?.to_owned()),
4655                name: required_str(v, "name")?.to_owned(),
4656                color: optional_str(v, "color")?.map(str::to_owned),
4657            })
4658        })
4659        .collect()
4660}
4661
4662fn text_field(field_values: &[Value], name: &str) -> Result<Option<String>, SourceError> {
4663    let Some(node) = field_values
4664        .iter()
4665        .find(|node| node.pointer("/field/name").and_then(Value::as_str) == Some(name))
4666    else {
4667        return Ok(None);
4668    };
4669    Ok(optional_str(node, "text")?.map(str::to_owned))
4670}
4671
4672fn valid_github_owner(owner: &str) -> bool {
4673    !owner.is_empty()
4674        && owner.len() <= 39
4675        && !owner.starts_with('-')
4676        && !owner.ends_with('-')
4677        && !owner.contains("--")
4678        && owner
4679            .bytes()
4680            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
4681}
4682
4683/// GitHub's repository-name grammar: 1-100 ASCII letters, digits, `-`, `_` or `.`, and
4684/// neither of the two names a path segment already means.
4685fn valid_github_repository_name(name: &str) -> bool {
4686    !name.is_empty()
4687        && name.len() <= 100
4688        && name != "."
4689        && name != ".."
4690        && name
4691            .bytes()
4692            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
4693}
4694
4695fn valid_environment_name(name: &str) -> bool {
4696    let mut bytes = name.bytes();
4697    bytes
4698        .next()
4699        .is_some_and(|byte| byte.is_ascii_alphabetic() || byte == b'_')
4700        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
4701}
4702
4703/// How many sub-issues one issue has.
4704///
4705/// `Issue.subIssuesSummary` is `SubIssuesSummary!` and its `total` is `Int!`, so an
4706/// absent or non-integer one is a response this source cannot read — and reading it as
4707/// zero would classify a project as a task, which is exactly the mistake the marker
4708/// exists to keep from happening quietly.
4709fn sub_issue_total(issue: &Value) -> Result<u64, SourceError> {
4710    let summary = issue
4711        .get("subIssuesSummary")
4712        .ok_or_else(|| SourceError::Malformed {
4713            message: "GitHub issue is missing subIssuesSummary".into(),
4714        })?;
4715    summary
4716        .get("total")
4717        .and_then(Value::as_u64)
4718        .ok_or_else(|| SourceError::Malformed {
4719            message: "GitHub issue subIssuesSummary.total is not an unsigned integer".into(),
4720        })
4721}
4722
4723fn required_str<'a>(value: &'a Value, field: &str) -> Result<&'a str, SourceError> {
4724    value
4725        .get(field)
4726        .and_then(Value::as_str)
4727        .ok_or_else(|| SourceError::Malformed {
4728            message: format!("GitHub response is missing string field {field}"),
4729        })
4730}
4731
4732/// The slot's delimiters, which `docs/metadata.md` settles once for every source that
4733/// needs one — Linear spells them too, in its own description field.
4734///
4735/// Restated rather than shared, because a plugin crate depends on the contract crate and
4736/// nothing else of this workspace. `scripts/check-metadata-slot-encoding.sh`, a target in
4737/// `check`, is what keeps the two one encoding: drift is otherwise quiet, since each
4738/// source round-trips its own writes perfectly well under its own spelling.
4739const METADATA_OPEN: &str = "<!-- onetaskgraph.metadata\n";
4740const METADATA_CLOSE: &str = "\n-->";
4741
4742/// The visible body and the metadata slot at the end of it.
4743///
4744/// The encoding is the one `docs/metadata.md` settles for Linear, which is where its
4745/// reasons are. Only a comment at the very end is a slot; one in the middle is a person's
4746/// own content and is left alone.
4747fn metadata_body(
4748    body: Option<String>,
4749) -> Result<(Option<String>, BTreeMap<String, Value>), SourceError> {
4750    let Some(body) = body else {
4751        return Ok((None, BTreeMap::new()));
4752    };
4753    let Some(start) = body.rfind(METADATA_OPEN) else {
4754        return Ok((Some(body), BTreeMap::new()));
4755    };
4756    let encoded_start = start + METADATA_OPEN.len();
4757    let Some(relative_end) = body[encoded_start..].find(METADATA_CLOSE) else {
4758        return Err(SourceError::Malformed {
4759            message: "unterminated onetaskgraph metadata slot in GitHub issue body".into(),
4760        });
4761    };
4762    let encoded_end = encoded_start + relative_end;
4763    if !body[encoded_end + METADATA_CLOSE.len()..].trim().is_empty() {
4764        return Ok((Some(body), BTreeMap::new()));
4765    }
4766    let metadata = serde_json::from_str(&body[encoded_start..encoded_end]).map_err(|error| {
4767        SourceError::Malformed {
4768            message: format!(
4769                "invalid canonical JSON in GitHub issue onetaskgraph metadata slot: {error}"
4770            ),
4771        }
4772    })?;
4773    let visible = body[..start].trim_end();
4774    Ok(((!visible.is_empty()).then(|| visible.to_owned()), metadata))
4775}
4776
4777fn compose_body(
4778    content: Option<&str>,
4779    metadata: &BTreeMap<String, Value>,
4780) -> Result<Option<String>, SourceError> {
4781    let visible = content.unwrap_or_default();
4782    if metadata.is_empty() {
4783        return Ok((!visible.is_empty()).then(|| visible.to_owned()));
4784    }
4785    let encoded = serde_json::to_string(metadata).map_err(|error| SourceError::Malformed {
4786        message: error.to_string(),
4787    })?;
4788    Ok(Some(if visible.is_empty() {
4789        format!("{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
4790    } else {
4791        format!("{visible}\n\n{METADATA_OPEN}{encoded}{METADATA_CLOSE}")
4792    }))
4793}
4794
4795fn required_bool(value: &Value, field: &str) -> Result<bool, SourceError> {
4796    value
4797        .get(field)
4798        .and_then(Value::as_bool)
4799        .ok_or_else(|| SourceError::Malformed {
4800            message: format!("GitHub response is missing boolean field {field}"),
4801        })
4802}
4803fn optional_str<'a>(value: &'a Value, field: &str) -> Result<Option<&'a str>, SourceError> {
4804    match value.get(field) {
4805        None | Some(Value::Null) => Ok(None),
4806        Some(value) => value
4807            .as_str()
4808            .map(Some)
4809            .ok_or_else(|| SourceError::Malformed {
4810                message: format!("GitHub response field {field} is not a string or null"),
4811            }),
4812    }
4813}
4814fn optional_nodes<'a>(
4815    connection: Option<&'a Value>,
4816    name: &str,
4817) -> Result<Option<&'a Vec<Value>>, SourceError> {
4818    match connection {
4819        None | Some(Value::Null) => Ok(None),
4820        Some(value) => value
4821            .get("nodes")
4822            .and_then(Value::as_array)
4823            .map(Some)
4824            .ok_or_else(|| SourceError::Malformed {
4825                message: format!("GitHub {name}.nodes is not an array"),
4826            }),
4827    }
4828}
4829fn complete_connection(connection: &Value, name: &str, size: u32) -> Result<(), SourceError> {
4830    let page_info = connection
4831        .get("pageInfo")
4832        .ok_or_else(|| SourceError::Malformed {
4833            message: format!("GitHub {name} has no pageInfo"),
4834        })?;
4835    if required_bool(page_info, "hasNextPage")? {
4836        return Err(SourceError::Malformed {
4837            message: format!(
4838                "GitHub {name} exceeds the supported nested connection size of {size}"
4839            ),
4840        });
4841    }
4842    Ok(())
4843}
4844fn optional_time(value: &Value, field: &str) -> Result<Option<DateTime<Utc>>, SourceError> {
4845    optional_str(value, field)?
4846        .map(|timestamp| {
4847            timestamp.parse().map_err(|error| SourceError::Malformed {
4848                message: format!("GitHub response field {field} is not a timestamp: {error}"),
4849            })
4850        })
4851        .transpose()
4852}
4853fn validate_page(page: &PageRequest) -> Result<(), SourceError> {
4854    if page.limit == 0 {
4855        Err(SourceError::Config {
4856            message: "page limit must be at least 1".into(),
4857        })
4858    } else {
4859        Ok(())
4860    }
4861}
4862fn next_cursor(connection: &Value) -> Result<Option<Cursor>, SourceError> {
4863    let page = connection
4864        .get("pageInfo")
4865        .filter(|value| value.is_object())
4866        .ok_or_else(|| SourceError::Malformed {
4867            message: "GitHub connection is missing pageInfo".into(),
4868        })?;
4869    if required_bool(page, "hasNextPage")? {
4870        let cursor = required_str(page, "endCursor")?;
4871        validate_cursor_progress(None, cursor)?;
4872        Ok(Some(Cursor(cursor.into())))
4873    } else {
4874        Ok(None)
4875    }
4876}
4877fn validate_cursor_progress(previous: Option<&str>, next: &str) -> Result<(), SourceError> {
4878    if next.is_empty() || previous == Some(next) {
4879        Err(SourceError::Malformed {
4880            message: "GitHub pagination cursor is empty or did not advance".into(),
4881        })
4882    } else {
4883        Ok(())
4884    }
4885}
4886fn numeric_cursor(cursor: Option<&Cursor>) -> Result<usize, SourceError> {
4887    cursor.map_or(Ok(0), |c| {
4888        c.0.parse().map_err(|_| SourceError::Config {
4889            message: "page cursor is invalid".into(),
4890        })
4891    })
4892}
4893fn offset_page<T>(mut items: Vec<T>, offset: usize, limit: usize) -> Page<T> {
4894    if offset > items.len() {
4895        return Page::last(vec![]);
4896    }
4897    let tail = items.split_off(offset);
4898    let mut selected = tail;
4899    let next = (selected.len() > limit).then(|| Cursor((offset + limit).to_string()));
4900    selected.truncate(limit);
4901    Page {
4902        items: selected,
4903        next,
4904    }
4905}