onetaskgraph_plugin_api/source.rs
1//! The two traits a plugin implements, and the secret lookup it is handed.
2
3use std::cmp::Ordering;
4use std::collections::BTreeMap;
5
6use schemars::{JsonSchema, Schema};
7use secrecy::SecretString;
8use serde::{Deserialize, Serialize};
9use serde_json::Value;
10
11use crate::{
12 Asset, AssetName, AssetWrite, AssetsWritten, Capabilities, Comment, CommentBody, CommentCopy,
13 DependencyEdge, Direction, Document, DocumentQuery, DocumentVersionCoverage, IssueHolding,
14 IssueRef, ItemKind, ItemWrite, Label, MetadataKey, MetadataRecord, Metering, NativeId,
15 NewComment, Page, PageRequest, Priority, Project, ProjectQuery, ProjectVersionCoverage,
16 SharedClock, SourceError, SourceName, Status, StatusCategory, StatusContradiction, Task,
17 TaskDetailRead, TaskQuery, TaskRef, TaskUpdate, TaskUpdateOutcome, VersionCoverage, Visibility,
18 WriteJournal, WriteSupport, WriteTarget, assetless, commentless, documentless, unwritable,
19 unwritable_field, unwritable_metadata,
20};
21
22/// Whether a source is answering right now.
23///
24/// # Placement is an open contract question
25///
26/// This type lives here because [`TaskSource::health`] returns it and the trait
27/// lives here: placing it in `onetaskgraph-core` would make this crate depend on
28/// the engine and invert the one direction the crate split exists to establish.
29/// The approved contract enumerates this crate's contents exhaustively and does
30/// not name `Health`, so the enumeration and the trait as written cannot both
31/// stand. Compiling forces the placement below; the resolution — add it to the
32/// enumeration, or redesign `health` so no such type crosses the boundary —
33/// belongs to the contract's owner, not to this crate. See `AGENTS.md`.
34#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
35// llmlint: ignore[invalid_states_unrepresentable] SECOND PERMITTED REASON — this restates at a new site the justification already recorded at `Capabilities.max_page_size` (capability.rs) and `PageRequest.limit` (query.rs), and a third time in this type's own doc comment above and in AGENTS.md's "Open contract question — `Health`": `Health`'s shape is approved contract text that `TaskSource::health` returns, so an enum here would change the serialized form and the trait six undispatched nodes implement. That is the contract owner's call, not this crate's.
36pub struct Health {
37 /// Whether the source answered.
38 ///
39 /// A bare `bool` beside an untyped `detail` cannot say that an unreachable source
40 /// must explain itself, or keep "reachable with a warning" apart from "reachable";
41 /// an enum carrying the detail in its unreachable variant would.
42 // llmlint: ignore[invalid_states_unrepresentable] SECOND PERMITTED REASON — this
43 // restates at this field the justification already recorded at
44 // `Capabilities.max_page_size` (capability.rs), `PageRequest.limit` (query.rs), this
45 // type's own doc comment above, and AGENTS.md's "Open contract question — `Health`":
46 // `Health`'s shape is approved contract text that `TaskSource::health` returns, so an
47 // enum here would change the serialized form and the trait six undispatched nodes
48 // implement. That is the contract owner's call, not this crate's.
49 // llmlint: ignore[boundary_inputs_validated] making "unreachable with no reason given" unrepresentable means an enum here, which changes the serialized form and the trait six undispatched nodes implement. Deferred to the contract's owner — AGENTS.md, "Open contract question — `Health`".
50 pub reachable: bool,
51 /// What the source said, when it said anything useful.
52 pub detail: Option<String>,
53}
54
55/// One configured source, as the engine drives it.
56///
57/// Dyn-compatible through `async_trait` because the engine holds
58/// `Vec<Box<dyn TaskSource>>` over heterogeneous plugins.
59///
60/// Three rules bind every implementation, and the engine's compensation is only
61/// correct while all three hold:
62///
63/// 1. **Apply** every predicate you declare [`Support::Native`](crate::Support::Native).
64/// 2. **Ignore** every [`Support`](crate::Support)-typed predicate you declare
65/// `Unsupported` — return the *wider* result set, never a narrower one.
66/// Silently dropping rows for a predicate you did not declare is the one
67/// failure no test above the plugin can catch.
68/// 3. Never return a silently empty dependency read. Rule 2 reaches the
69/// `Support`-typed *predicates* alone; a dependency read is always real, and so is a
70/// document read — [`Capabilities::documents`] says whether this source has documents
71/// at all, and a source that says it has none is never asked for one rather than
72/// answering an empty page.
73#[async_trait::async_trait]
74pub trait TaskSource: Send + Sync {
75 /// The plugin kind that built this source, for display and for plan output.
76 fn kind(&self) -> &'static str;
77
78 /// What this source applies itself. Read once per query by the engine.
79 fn capabilities(&self) -> Capabilities;
80
81 /// How `a`, a [`Task::version`] this source gave one of its tasks, stands to `b`, another
82 /// it gave the same task: `Greater` when `a` is the later, `Less` when it is the earlier,
83 /// `Equal` when nothing the token covers changed between them, and `None` when the two
84 /// cannot be put in order — a value this source cannot read, or two versions its token
85 /// says differ without saying which came first.
86 ///
87 /// A token's format and its order are the one source's that spells it, so the engine asks
88 /// the source that gave both rather than reading inside either.
89 ///
90 /// Defaulted to equality alone: two identical tokens are one version, and any two others
91 /// are unordered, which a reader takes as *changed, read it again* and never as
92 /// unchanged. A source whose token says which of two versions is the later overrides it.
93 fn order_versions(&self, a: &str, b: &str) -> Option<Ordering> {
94 (a == b).then_some(Ordering::Equal)
95 }
96
97 /// Which parts of a task this source's [`Task::version`] vouches for, which is what the
98 /// store's local cache may serve a task's parts from.
99 ///
100 /// Defaulted to no part, which keeps the cache from ever serving what a token was never
101 /// promised to cover: a source overrides it only for the parts its token covers, as
102 /// `docs/plugin-protocol.md` §4.13d states them.
103 fn version_coverage(&self) -> VersionCoverage {
104 VersionCoverage::none(format!(
105 "the {} plugin does not declare that its change token covers a task",
106 self.kind()
107 ))
108 }
109
110 /// Which parts of a task a copy may take this source's unchanged [`Task::version`] as
111 /// vouching for, when it decides to skip a task its destination already holds as written:
112 /// the task's fields, its comments when the copy carries them, and its forward edges. A copy
113 /// skips only a task whose source covers every one of those it writes.
114 ///
115 /// Defaulted to [`version_coverage`](Self::version_coverage). A source overrides it only to
116 /// state, with its measured bound, a claim about a copy's skip that is not one the cache may
117 /// serve from — as `docs/plugin-protocol.md` §4.13d does for the one plugin that does.
118 fn copy_version_coverage(&self) -> VersionCoverage {
119 self.version_coverage()
120 }
121
122 /// Which parts of a project this source's [`Project::version`] vouches for, on the terms
123 /// of [`version_coverage`](Self::version_coverage).
124 ///
125 /// Defaulted to no part, for the same reason.
126 fn project_version_coverage(&self) -> ProjectVersionCoverage {
127 ProjectVersionCoverage::none(format!(
128 "the {} plugin does not declare that its change token covers a project",
129 self.kind()
130 ))
131 }
132
133 /// Which parts of a document this source's [`Document::version`] vouches for, on the terms
134 /// of [`version_coverage`](Self::version_coverage).
135 ///
136 /// Defaulted to no part, for the same reason.
137 fn document_version_coverage(&self) -> DocumentVersionCoverage {
138 DocumentVersionCoverage::none(format!(
139 "the {} plugin does not declare that its change token covers a document",
140 self.kind()
141 ))
142 }
143
144 /// Tell this source the store's local cache now stands in front of it.
145 ///
146 /// Called exactly once, by the cache as it wraps the source, before any read and before
147 /// any coverage is read — so a source whose token needs a part only a wider selection
148 /// carries can widen its reads here, and declare the coverage that widening buys. Never
149 /// carried over the plugin protocol, as [`version_coverage`](Self::version_coverage) is
150 /// not.
151 ///
152 /// Defaulted to doing nothing: a source not overriding it reads exactly as it did.
153 fn attach_cache(&self) {}
154
155 /// The change token the response that answered this source's last
156 /// [`Direction::DependsOn`] read of task `id` in this process spelled, or `None` when it
157 /// spelled none.
158 ///
159 /// What lets the store's cache keep a task's forward edges by the version they were read
160 /// at when nothing else in the command read the task: those edges are served only to a pin
161 /// that version is at or after, and never vouch for the task's own fields. Synchronous, and
162 /// never carried over the plugin protocol, as [`version_coverage`](Self::version_coverage)
163 /// is not.
164 ///
165 /// Defaulted to `None`: a source whose dependency read spells no token keeps its edges
166 /// remote unless a read of the task itself vouched for them.
167 fn forward_edges_version(&self, id: &NativeId) -> Option<String> {
168 let _ = id;
169 None
170 }
171
172 /// The change token the response that answered this source's last dependency read of the
173 /// `kind` item `id` in `direction`, from its first page, spelled — or `None` when it spelled
174 /// none — on the terms of [`forward_edges_version`](Self::forward_edges_version), which is
175 /// what it answers for a task's [`Direction::DependsOn`].
176 ///
177 /// What lets the store's cache keep a project's forward edges, and either kind's reverse
178 /// ones, by the version they were read at: a project's forward edges are served only where
179 /// [`project_version_coverage`](Self::project_version_coverage) covers them, reverse ones only
180 /// where [`reverse_edges_coverage`](Self::reverse_edges_coverage) covers that kind's, and each
181 /// only to a pin that version is at or after. Synchronous, and never
182 /// carried over the plugin protocol.
183 ///
184 /// Defaulted to [`forward_edges_version`](Self::forward_edges_version) for a task's forward
185 /// edges and to `None` for every other read, which keeps those remote.
186 fn edges_version(&self, kind: ItemKind, id: &NativeId, direction: Direction) -> Option<String> {
187 match (kind, direction) {
188 (ItemKind::Task, Direction::DependsOn) => self.forward_edges_version(id),
189 _ => None,
190 }
191 }
192
193 /// Whether this source's token for a `kind` item — [`Task::version`] or
194 /// [`Project::version`] — vouches for its reverse edges, what a [`Direction::DependedOnBy`]
195 /// read answers, so the store's cache may serve them.
196 ///
197 /// A reverse edge is recorded on another item, so a token vouches for one only where it
198 /// carries a part that moves whenever one is added to or removed from that other item —
199 /// which no part of an item's own fields does. Never carried over the plugin protocol.
200 ///
201 /// Defaulted to not covering them, with that reason.
202 fn reverse_edges_coverage(&self, kind: ItemKind) -> crate::Coverage {
203 crate::Coverage::Uncovered {
204 reason: format!(
205 "the {} plugin does not declare that its change token covers a {}'s reverse \
206 edges, which are recorded on the items that depend on it",
207 self.kind(),
208 match kind {
209 ItemKind::Task => "task",
210 ItemKind::Project => "project",
211 }
212 ),
213 }
214 }
215
216 /// What this source holds about task `id` — the item it read last in this process — that
217 /// a later write expecting that version of it could take instead of reading it again, or
218 /// `None` when it holds nothing of the kind.
219 ///
220 /// Opaque to the engine, which keeps it beside the cached task and hands it back through
221 /// [`prime_write`](Self::prime_write). Defaulted to `None`: a source whose writes read
222 /// nothing first has nothing to keep.
223 fn write_context(&self, id: &NativeId) -> Option<serde_json::Value> {
224 let _ = id;
225 None
226 }
227
228 /// What this source holds about the container every task of it shares — a board's field
229 /// and option ids — that a write could take instead of reading it, or `None`.
230 ///
231 /// Defaulted to `None`, on the terms of [`write_context`](Self::write_context).
232 fn board_context(&self) -> Option<serde_json::Value> {
233 None
234 }
235
236 /// Hold `context`, which [`write_context`](Self::write_context) gave for task `id` at a
237 /// version the caller expects it still at, and `board`, which
238 /// [`board_context`](Self::board_context) gave, so the next write of `id` takes them
239 /// instead of reading — answering whether it took them.
240 ///
241 /// A context this source cannot read is not taken, and the write then reads as it always
242 /// does. Defaulted to taking nothing.
243 fn prime_write(
244 &self,
245 id: &NativeId,
246 context: &serde_json::Value,
247 board: Option<&serde_json::Value>,
248 ) -> bool {
249 let _ = (id, context, board);
250 false
251 }
252
253 /// Drop whatever [`prime_write`](Self::prime_write) took for `id`, and the board context
254 /// with it, so the next write of it reads afresh.
255 fn unprime(&self, id: &NativeId) {
256 let _ = id;
257 }
258
259 /// Start recording every request this source sends, for [`end_journal`](Self::end_journal).
260 ///
261 /// The engine opens one journal around one write at a time. Defaulted to recording
262 /// nothing.
263 fn begin_journal(&self) {}
264
265 /// Stop recording, answering with every request sent since
266 /// [`begin_journal`](Self::begin_journal) — or `None` for a source that records none.
267 fn end_journal(&self) -> Option<WriteJournal> {
268 None
269 }
270
271 /// Whether the source is answering right now.
272 ///
273 /// # Errors
274 ///
275 /// Returns a [`SourceError`] when the check itself could not be made.
276 async fn health(&self) -> Result<Health, SourceError>;
277
278 /// Fetch one task by its native id, or `None` when there is no such task.
279 ///
280 /// # Errors
281 ///
282 /// Returns a [`SourceError`] when the source could not answer.
283 async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError>;
284
285 /// Fetch several tasks by their native ids, each with the first page of its comments
286 /// when `comments` names the page to read — one answer per id, in the order they were
287 /// given.
288 ///
289 /// Each answer is what [`get_task`](Self::get_task) answers for that id, so a task that
290 /// is not there is `Ok(None)` and a read that failed fails that id alone. Its
291 /// [`TaskDetailRead::comments`] is what [`task_comments`](Self::task_comments) answers
292 /// for `comments`. The engine passes `comments` only to a source declaring
293 /// [`Capabilities::comments`](crate::Capabilities::comments) native.
294 ///
295 /// Defaulted to exactly those calls, item by item, so a source that implements nothing
296 /// here answers as it always did. A source that can read many items — or an item and its
297 /// comments — in fewer requests than that overrides it, which is the whole reason it
298 /// exists: a caller holding twenty ids pays for one batch rather than forty reads.
299 async fn get_task_details(
300 &self,
301 ids: &[NativeId],
302 comments: Option<&PageRequest>,
303 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
304 let mut read = Vec::with_capacity(ids.len());
305 for id in ids {
306 read.push(match self.get_task(id).await {
307 Ok(Some(task)) => {
308 let comments = match comments {
309 Some(page) => Some(self.task_comments(id, page).await),
310 None => None,
311 };
312 Ok(Some(TaskDetailRead { task, comments }))
313 }
314 Ok(None) => Ok(None),
315 Err(error) => Err(error),
316 });
317 }
318 read
319 }
320
321 /// Fetch one project by its native id, or `None` when there is no such project.
322 ///
323 /// # Errors
324 ///
325 /// Returns a [`SourceError`] when the source could not answer.
326 async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError>;
327
328 /// One page of the tasks matching `query`.
329 ///
330 /// # Errors
331 ///
332 /// Returns a [`SourceError`] when the source could not answer.
333 async fn query_tasks(
334 &self,
335 query: &TaskQuery,
336 page: &PageRequest,
337 ) -> Result<Page<Task>, SourceError>;
338
339 /// One page of the projects matching `query`.
340 ///
341 /// # Errors
342 ///
343 /// Returns a [`SourceError`] when the source could not answer.
344 async fn query_projects(
345 &self,
346 query: &ProjectQuery,
347 page: &PageRequest,
348 ) -> Result<Page<Project>, SourceError>;
349
350 /// One page of every label this source knows.
351 ///
352 /// # Errors
353 ///
354 /// Returns a [`SourceError`] when the source could not answer.
355 async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError>;
356
357 /// One page of the task dependency edges at `id`, in `direction`.
358 ///
359 /// # Errors
360 ///
361 /// Returns a [`SourceError`] when the source could not answer.
362 async fn task_dependencies(
363 &self,
364 id: &NativeId,
365 direction: Direction,
366 page: &PageRequest,
367 ) -> Result<Page<DependencyEdge>, SourceError>;
368
369 /// One page of the project dependency edges at `id`, in `direction`.
370 ///
371 /// # Errors
372 ///
373 /// Returns a [`SourceError`] when the source could not answer.
374 async fn project_dependencies(
375 &self,
376 id: &NativeId,
377 direction: Direction,
378 page: &PageRequest,
379 ) -> Result<Page<DependencyEdge>, SourceError>;
380
381 /// Whether this source can be written through at all.
382 ///
383 /// Defaulted to [`WriteSupport::Unsupported`], which is what keeps this a read
384 /// interface for every source that has nothing to write into: one that cannot be
385 /// written needs no edit and keeps working. Read before a write is attempted, so a
386 /// copy naming such a source as its destination is refused before anything is read.
387 fn writes(&self) -> WriteSupport {
388 WriteSupport::Unsupported
389 }
390
391 /// Create or update one task, answering with the native id the destination holds it
392 /// under.
393 ///
394 /// A source declaring [`WriteSupport::Supported`] owes three things here. It refuses,
395 /// naming the field, anything it cannot represent rather than dropping it — including
396 /// a metadata key it cannot carry, which it names. It writes every other field it was
397 /// given. And it never creates when [`ItemWrite::target`] names an item it does not
398 /// hold.
399 ///
400 /// # Errors
401 ///
402 /// Returns [`SourceError::Refused`] when this source has no write side, when a field
403 /// or a metadata key cannot be represented, or when `target` names nothing here; and
404 /// whatever else the source could not do the write for.
405 async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
406 let _ = write;
407 Err(unwritable(self.kind()))
408 }
409
410 /// Create or update one project, on exactly the terms of
411 /// [`write_task`](Self::write_task).
412 ///
413 /// # Errors
414 ///
415 /// As [`write_task`](Self::write_task).
416 async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
417 let _ = write;
418 Err(unwritable(self.kind()))
419 }
420
421 /// Whether this source can represent every field of `write` — its status, its content, the
422 /// tasks it delivers and every other field a [`write_task`](Self::write_task) of it would
423 /// refuse as "cannot represent the field …" — asked before anything is written and changing
424 /// nothing.
425 ///
426 /// A task copy asks it of every task it plans before its first write, and leaves out each
427 /// task this refuses — answered `failed`, with this refusal as its reason — writing the rest
428 /// as one copy. It answers what the write itself would refuse in the same words; and it is a
429 /// promise only about representability, so a write may still fail for any other reason.
430 ///
431 /// Defaulted to `Ok(())`: a source that does not answer it in advance still refuses such a
432 /// write in its write, which a copy then takes back whole, exactly as before.
433 ///
434 /// # Errors
435 ///
436 /// Returns [`SourceError::Refused`] naming the field this source cannot represent; and
437 /// whatever else kept it from finding out.
438 async fn check_task_write(&self, write: &ItemWrite<Task>) -> Result<(), SourceError> {
439 let _ = write;
440 Ok(())
441 }
442
443 /// Whether a write of an item of `kind` at `category` — over the item `target` names, or
444 /// creating one when it is `None` — would have a status to write, asked before the write
445 /// and changing nothing.
446 ///
447 /// A caller that has to put an item back when a later write of it fails — a copy, whose
448 /// journal records what an item held before overwriting it — asks this first, so a status
449 /// this source has no name for is refused while nothing has been recorded or written, and
450 /// nothing has to be put back. It answers what the write itself would refuse a status
451 /// with, in the same words, and sends no request a write of that status would not have
452 /// sent anyway: what it reads to answer, it holds for the write that follows.
453 ///
454 /// Defaulted to `Ok(())`: a source that does not answer it in advance still refuses the
455 /// status in its write, exactly as before.
456 ///
457 /// # Errors
458 ///
459 /// Returns [`SourceError::Refused`] naming the kind, the category and what is missing
460 /// when this source has no name for that status; and whatever else kept it from finding
461 /// out.
462 async fn check_status_write(
463 &self,
464 kind: ItemKind,
465 category: StatusCategory,
466 target: Option<&NativeId>,
467 ) -> Result<(), SourceError> {
468 let _ = (kind, category, target);
469 Ok(())
470 }
471
472 /// Set the status of one task this source holds, and change nothing else about it,
473 /// answering with the status as this source now reads it — or `None` when this source
474 /// holds no such task.
475 ///
476 /// The category lands where this source's own mapping sends it, exactly as a
477 /// [`write_task`](Self::write_task) of a task in that category would: a category this
478 /// source has disabled is refused in the words a write of it is refused with. Title,
479 /// content, labels, metadata, dependencies, [`Task::delivers`], [`Task::delivered_by`],
480 /// project and comments are left exactly as they are.
481 ///
482 /// Defaulted to [`unwritable_field`], which is what keeps this an addition rather than a
483 /// break: a source that cannot write a status on its own needs no edit and refuses by
484 /// saying so. A source declaring [`WriteSupport::Unsupported`] is never asked.
485 ///
486 /// [`Task::delivers`]: crate::Task::delivers
487 /// [`Task::delivered_by`]: crate::Task::delivered_by
488 ///
489 /// # Errors
490 ///
491 /// Returns [`SourceError::Refused`] when this source cannot write a status, or cannot
492 /// write this one; and whatever else the source could not do the write for.
493 async fn set_task_status(
494 &self,
495 id: &NativeId,
496 category: StatusCategory,
497 ) -> Result<Option<Status>, SourceError> {
498 let _ = (id, category);
499 Err(unwritable_field(self.kind(), "status"))
500 }
501
502 /// Set the status of one task this source holds, as [`set_task_status`](Self::set_task_status)
503 /// does, and answer with the whole task as this source now reads it — its
504 /// [`Task::delivers`] among what a caller keeping delivered tasks in step needs — or `None`
505 /// when this source holds no such task.
506 ///
507 /// Defaulted to exactly the two calls a caller would otherwise make: [`get_task`] first,
508 /// answering `None` with nothing written when it does, then `set_task_status`, the status
509 /// it answers put on the task read. A source whose status write answers the whole task in
510 /// the same round trip overrides it to save the read.
511 ///
512 /// [`Task::delivers`]: crate::Task::delivers
513 /// [`get_task`]: Self::get_task
514 ///
515 /// # Errors
516 ///
517 /// As [`get_task`](Self::get_task) and [`set_task_status`](Self::set_task_status).
518 async fn set_task_status_reading(
519 &self,
520 id: &NativeId,
521 category: StatusCategory,
522 ) -> Result<Option<Task>, SourceError> {
523 let Some(mut task) = self.get_task(id).await? else {
524 return Ok(None);
525 };
526 let Some(status) = self.set_task_status(id, category).await? else {
527 return Ok(None);
528 };
529 task.status = status;
530 Ok(Some(task))
531 }
532
533 /// Every task this source holds whose card contradicts its issue's closure, each with the
534 /// status that brings it in line — what `task status reconcile` writes, through
535 /// [`set_task_status`](Self::set_task_status), one task at a time.
536 ///
537 /// A task needs it when its issue is closed with a reason that reads `done` or
538 /// `cancelled` and its card does not sit at the option this source's mapping gives that
539 /// category. An open task, a closure that reads `unknown` and a card already in line are
540 /// never answered. Changes nothing.
541 ///
542 /// Asked only of a source declaring
543 /// [`Capabilities::reconcile_status`](crate::Capabilities::reconcile_status); defaulted to a
544 /// refusal saying so, which no caller holding to that declaration ever receives.
545 ///
546 /// # Errors
547 ///
548 /// Returns [`SourceError::Refused`] when this source answers no such question; and whatever
549 /// kept it from reading its items.
550 async fn status_contradictions(&self) -> Result<Vec<StatusContradiction>, SourceError> {
551 Err(SourceError::Refused {
552 message: format!(
553 "a {} source's status is its closure, so nothing needs reconciling",
554 self.kind()
555 ),
556 })
557 }
558
559 /// For each issue in `issues`, in order and once each, the task this source holds for it
560 /// and what moving it to `done` comes to — what `task status complete` writes, through
561 /// [`set_task_status`](Self::set_task_status), for each one answered
562 /// [`Completion::Due`](crate::Completion::Due).
563 ///
564 /// The read is proportional to `issues` and never to the size of what this source holds,
565 /// and an empty `issues` reads nothing. Changes nothing.
566 ///
567 /// Asked only of a source declaring
568 /// [`Capabilities::complete_by_issue`](crate::Capabilities::complete_by_issue); defaulted
569 /// to a refusal saying so, which no caller holding to that declaration ever receives.
570 ///
571 /// # Errors
572 ///
573 /// Returns [`SourceError::Refused`] when this source answers no such question; and whatever
574 /// kept it from reading the issues.
575 async fn issue_holdings(&self, issues: &[IssueRef]) -> Result<Vec<IssueHolding>, SourceError> {
576 let _ = issues;
577 Err(SourceError::Refused {
578 message: format!("a {} source holds no items by issue reference", self.kind()),
579 })
580 }
581
582 /// Set the priority of one task this source holds, and change nothing else about it,
583 /// answering with the priority as this source now reads it — or `None` when this source
584 /// holds no such task.
585 ///
586 /// [`Priority::None`] clears the priority. Title, content, status, labels, metadata,
587 /// repositories, dependencies and comments are left exactly as they are. Nothing about
588 /// the task's status moves, so the engine re-evaluates no delivered task after it.
589 ///
590 /// Defaulted to [`unwritable_field`], which is what keeps this an addition rather than a
591 /// break. A source declaring [`WriteSupport::Unsupported`], or declaring
592 /// [`Capabilities::priority`] unsupported, is never asked.
593 ///
594 /// # Errors
595 ///
596 /// Returns [`SourceError::Refused`] when this source cannot write a priority, or cannot
597 /// write this one — a board with no option for it, naming the option; and whatever else
598 /// the source could not do the write for.
599 async fn set_task_priority(
600 &self,
601 id: &NativeId,
602 priority: Priority,
603 ) -> Result<Option<Priority>, SourceError> {
604 let _ = (id, priority);
605 Err(unwritable_field(self.kind(), "priority"))
606 }
607
608 /// Replace the content of one task this source holds with `content`, byte for byte, and
609 /// change nothing else about it — or answer `None` when this source holds no such task.
610 ///
611 /// The content is [`Task::content`] exactly as this source reports it: what a later read
612 /// answers there is `content`. Where the source keeps something else inside the same
613 /// backend field — a metadata block in an issue body — that is kept as it was, and so is
614 /// every other member: title, status, priority, labels, metadata, repositories,
615 /// dependencies, project and comments. Nothing about the task's status moves, so the
616 /// engine re-evaluates no delivered task after it.
617 ///
618 /// Defaulted to [`unwritable_field`] on exactly the terms of
619 /// [`set_task_status`](Self::set_task_status). A source declaring
620 /// [`WriteSupport::Unsupported`] is never asked.
621 ///
622 /// # Errors
623 ///
624 /// Returns [`SourceError::Refused`] when this source cannot write a task's content on its
625 /// own, or cannot represent this one; and whatever else the source could not do the write
626 /// for.
627 async fn set_task_content(
628 &self,
629 id: &NativeId,
630 content: &str,
631 ) -> Result<Option<()>, SourceError> {
632 let _ = (id, content);
633 Err(unwritable_field(self.kind(), "content"))
634 }
635
636 /// Replace the [`Task::delivered_by`] of one task this source holds, and change nothing
637 /// else about it — or answer `None` when this source holds no such task.
638 ///
639 /// Every entry is a qualified id, and the list is the whole of it: what the task held
640 /// there before is replaced, not merged. It is the store's to keep in step — the engine
641 /// calls this whenever it writes a task's [`Task::delivers`] — and nothing a person types
642 /// reaches it directly.
643 ///
644 /// Defaulted to [`unwritable_field`] on exactly the terms of
645 /// [`set_task_status`](Self::set_task_status).
646 ///
647 /// [`Task::delivers`]: crate::Task::delivers
648 /// [`Task::delivered_by`]: crate::Task::delivered_by
649 ///
650 /// # Errors
651 ///
652 /// Returns [`SourceError::Refused`] when this source cannot hold the list, and whatever
653 /// else it could not do the write for.
654 async fn set_delivered_by(
655 &self,
656 id: &NativeId,
657 delivered_by: &[TaskRef],
658 ) -> Result<Option<()>, SourceError> {
659 let _ = (id, delivered_by);
660 Err(unwritable_field(self.kind(), "delivered_by"))
661 }
662
663 /// Set one key of the metadata of one task this source holds, and change nothing else
664 /// about it, answering with the task as this source reads it back after the write — or
665 /// `None` when this source holds no such task.
666 ///
667 /// The key is added when the task does not hold it and replaced when it does; every other
668 /// metadata key, and every other field of the task, is left exactly as it was. A `value`
669 /// the task already holds under `key` is a write that changes nothing, and a source owes
670 /// it no write at all. The answer is a read, not an echo: what the engine reports as the
671 /// value is what the returned task holds under `key`.
672 ///
673 /// Nothing about the task's status or its [`Task::delivers`] moves, so the engine
674 /// re-evaluates no delivered task after this write.
675 ///
676 /// Defaulted to [`unwritable_metadata`] on exactly the terms of
677 /// [`set_task_status`](Self::set_task_status): a source that cannot write one key on its
678 /// own needs no edit and refuses by saying so. A source declaring
679 /// [`WriteSupport::Unsupported`] is never asked.
680 ///
681 /// [`Task::delivers`]: crate::Task::delivers
682 ///
683 /// # Errors
684 ///
685 /// Returns [`SourceError::Refused`] when this source cannot write one key of a task's
686 /// metadata on its own, or cannot write this one without changing something else; and
687 /// whatever else the source could not do the write for.
688 async fn set_task_metadata(
689 &self,
690 id: &NativeId,
691 key: &MetadataKey,
692 value: &Value,
693 ) -> Result<Option<Task>, SourceError> {
694 let _ = (id, key, value);
695 Err(unwritable_metadata(self.kind(), MetadataRecord::Task))
696 }
697
698 /// Apply a targeted update to one task this source holds — every field `update` names, and
699 /// nothing else — answering with a [`TaskUpdateOutcome`], or `None` when this source holds
700 /// no such task.
701 ///
702 /// The outcome is everything the engine reports about the call, so it reads nothing of its
703 /// own: the task as this source reads it once the update landed — from the one read it made
704 /// and what it then wrote, where its backend answers a write with what it holds — the
705 /// fields it actually wrote, and the `delivers` the task held before.
706 ///
707 /// A field already holding the requested value is sent no write, and an update in which
708 /// nothing differs sends no write at all. Every field `update` leaves unnamed — labels,
709 /// repositories, project, [`Task::delivered_by`] and comments always among them — is left
710 /// exactly as it is. An update naming one metadata key both to set and to remove is refused
711 /// before anything is written, in the words of [`TaskUpdate::consistent`].
712 ///
713 /// Defaulted to [`TaskUpdate::rewrite`]: read the task, apply the update, and — only when
714 /// anything differs — [`write_task`](Self::write_task) it with its `target` set. That keeps
715 /// this an addition rather than a break, and a source that predates it behaves correctly and
716 /// is merely not minimal; one whose backend can take a field on its own owes an override
717 /// that sends only what differs. A source declaring [`WriteSupport::Unsupported`] is never
718 /// asked.
719 ///
720 /// [`Task::delivered_by`]: crate::Task::delivered_by
721 ///
722 /// # Errors
723 ///
724 /// Returns [`SourceError::Refused`] when the update contradicts itself, when a field it
725 /// names cannot be represented here — a category this source has disabled included — and
726 /// whatever else the source could not do the write for.
727 async fn update_task(
728 &self,
729 id: &NativeId,
730 update: &TaskUpdate,
731 ) -> Result<Option<TaskUpdateOutcome>, SourceError> {
732 update.rewrite(self, id).await
733 }
734
735 /// Set one key of the metadata of one project this source holds, on exactly the terms of
736 /// [`set_task_metadata`](Self::set_task_metadata).
737 ///
738 /// # Errors
739 ///
740 /// As [`set_task_metadata`](Self::set_task_metadata).
741 async fn set_project_metadata(
742 &self,
743 id: &NativeId,
744 key: &MetadataKey,
745 value: &Value,
746 ) -> Result<Option<Project>, SourceError> {
747 let _ = (id, key, value);
748 Err(unwritable_metadata(self.kind(), MetadataRecord::Project))
749 }
750
751 /// Set one key of the metadata of one document this source holds, on exactly the terms of
752 /// [`set_task_metadata`](Self::set_task_metadata).
753 ///
754 /// A source declaring [`Capabilities::documents`] unsupported is never asked, exactly as
755 /// it is never asked for a document read.
756 ///
757 /// # Errors
758 ///
759 /// As [`set_task_metadata`](Self::set_task_metadata).
760 async fn set_document_metadata(
761 &self,
762 id: &NativeId,
763 key: &MetadataKey,
764 value: &Value,
765 ) -> Result<Option<Document>, SourceError> {
766 let _ = (id, key, value);
767 Err(unwritable_metadata(self.kind(), MetadataRecord::Document))
768 }
769
770 /// Remove one task this destination holds, so a copy that could not finish can put
771 /// the destination back the way it found it.
772 ///
773 /// This is not a verb of the product: nothing a user types deletes anything, and a
774 /// copy never deletes an item it did not itself create in the run that is failing.
775 /// It exists because a copy is either complete or it never happened — a half-written
776 /// project has to be run again, and the re-run is the mutation burst that trips a
777 /// hosted destination's rate limiter. Undoing this run's own creates is what removes
778 /// that retry at source.
779 ///
780 /// A source declaring [`WriteSupport::Supported`] owes a real implementation, for the
781 /// reason it owes [`write_task`](Self::write_task) one: the engine will create items
782 /// there, so it has to be able to remove the ones it created. An `id` naming nothing
783 /// is **not** an error — the item is already gone, which is the state this asks for.
784 ///
785 /// # Errors
786 ///
787 /// Returns [`SourceError::Refused`] when this source has no write side, and whatever
788 /// else the source could not remove the item for.
789 async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
790 let _ = id;
791 Err(unwritable(self.kind()))
792 }
793
794 /// Remove one project this destination holds, on exactly the terms of
795 /// [`delete_task`](Self::delete_task).
796 ///
797 /// # Errors
798 ///
799 /// As [`delete_task`](Self::delete_task).
800 async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
801 let _ = id;
802 Err(unwritable(self.kind()))
803 }
804
805 /// Fetch one document by its native id, or `None` when there is no such document.
806 ///
807 /// Defaulted to [`documentless`], which is what keeps documents an addition rather
808 /// than a break: a source with none needs no edit, keeps working, and says so in the
809 /// same words every other document-free source does. A source that has documents
810 /// declares [`Support::Native`](crate::Support::Native) for
811 /// [`Capabilities::documents`] and owes a real implementation here, because that
812 /// declaration is what makes the engine ask.
813 ///
814 /// # Errors
815 ///
816 /// Returns [`SourceError::Refused`] when this source has no documents, and whatever
817 /// else the source could not answer for.
818 async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
819 let _ = id;
820 Err(documentless(self.kind()))
821 }
822
823 /// One page of the documents matching `query`.
824 ///
825 /// Defaulted on exactly the terms of [`get_document`](Self::get_document). A source
826 /// with no documents refuses rather than answering an empty page: an empty page reads
827 /// as a source that has documents and holds none matching, which is the one wrong
828 /// answer this method can give.
829 ///
830 /// # Errors
831 ///
832 /// As [`get_document`](Self::get_document).
833 async fn query_documents(
834 &self,
835 query: &DocumentQuery,
836 page: &PageRequest,
837 ) -> Result<Page<Document>, SourceError> {
838 let _ = (query, page);
839 Err(documentless(self.kind()))
840 }
841
842 /// Create or update one document, on exactly the terms of
843 /// [`write_task`](Self::write_task).
844 ///
845 /// # Errors
846 ///
847 /// As [`write_task`](Self::write_task).
848 async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
849 let _ = write;
850 Err(unwritable(self.kind()))
851 }
852
853 /// Remove one document this destination holds, on exactly the terms of
854 /// [`delete_task`](Self::delete_task).
855 ///
856 /// # Errors
857 ///
858 /// As [`delete_task`](Self::delete_task).
859 async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
860 let _ = id;
861 Err(unwritable(self.kind()))
862 }
863
864 /// One page of the comments on `task`, oldest first, or `None` when this source holds
865 /// no such task.
866 ///
867 /// Defaulted to [`commentless`], which is what keeps comments an addition rather than a
868 /// break: a source with none needs no edit and keeps working. A source whose tasks have
869 /// comments declares [`Support::Native`](crate::Support::Native) for
870 /// [`Capabilities::comments`] and owes a real implementation of all four comment methods,
871 /// because that declaration is what makes the engine ask.
872 ///
873 /// "No such task" is `None` rather than an error, exactly as it is for
874 /// [`get_task`](Self::get_task); a task that exists and has no comments is an empty page.
875 ///
876 /// # Errors
877 ///
878 /// Returns [`SourceError::Refused`] when this source has no comments, and whatever else
879 /// the source could not answer for.
880 async fn task_comments(
881 &self,
882 task: &NativeId,
883 page: &PageRequest,
884 ) -> Result<Option<Page<Comment>>, SourceError> {
885 let _ = (task, page);
886 Err(commentless(self.kind()))
887 }
888
889 /// [`get_task_details`](Self::get_task_details) as a copy reads the tasks it carries: each
890 /// with the first page of its comments `comments` names — and, where the same request can
891 /// carry them, what this source needs to answer [`task_dependencies`](Self::task_dependencies)
892 /// for it without another, since a copy reads every task's edges next.
893 ///
894 /// Defaulted to `get_task_details(ids, Some(comments))`, which is exactly what it answers.
895 /// A source whose detail read is priced by what it selects overrides it with a read that
896 /// also carries the edges, so a copy of a changed task costs its detail read and nothing
897 /// more; one whose reads are free has no reason to.
898 async fn get_task_details_for_copy(
899 &self,
900 ids: &[NativeId],
901 comments: &PageRequest,
902 ) -> Vec<Result<Option<TaskDetailRead>, SourceError>> {
903 self.get_task_details(ids, Some(comments)).await
904 }
905
906 /// Add one comment to `task`, answering with the comment as the source now holds it, or
907 /// `None` when this source holds no such task.
908 ///
909 /// The body is stored byte for byte. A source that records the author itself refuses a
910 /// [`NewComment::author`] rather than dropping it, naming why; a source that cannot
911 /// represent the body refuses it, naming why, rather than escaping it into something
912 /// else.
913 ///
914 /// # Errors
915 ///
916 /// Returns [`SourceError::Refused`] when this source has no comments or cannot be
917 /// written, when it cannot record what it was given, and whatever else it could not do
918 /// the write for.
919 async fn add_comment(
920 &self,
921 task: &NativeId,
922 comment: &NewComment,
923 ) -> Result<Option<Comment>, SourceError> {
924 let _ = (task, comment);
925 Err(commentless(self.kind()))
926 }
927
928 /// Replace the body of the comment `comment` on `task`, answering with the comment as the
929 /// source now holds it, or `None` when this source holds no such task or that task has no
930 /// such comment.
931 ///
932 /// Only the body and the time it last changed move: the id, the author and the time it
933 /// was written are the comment's own.
934 ///
935 /// # Errors
936 ///
937 /// As [`add_comment`](Self::add_comment).
938 async fn edit_comment(
939 &self,
940 task: &NativeId,
941 comment: &NativeId,
942 body: &CommentBody,
943 ) -> Result<Option<Comment>, SourceError> {
944 let _ = (task, comment, body);
945 Err(commentless(self.kind()))
946 }
947
948 /// Remove the comment `comment` from `task`, answering with the id it removed, or `None`
949 /// when this source holds no such task or that task has no such comment.
950 ///
951 /// Unlike [`delete_task`](Self::delete_task), this *is* a verb of the product — a person
952 /// removes a comment they posted — so a comment that is not there is reported as `None`
953 /// for the engine to refuse by name, rather than treated as already gone.
954 ///
955 /// # Errors
956 ///
957 /// As [`add_comment`](Self::add_comment).
958 async fn delete_comment(
959 &self,
960 task: &NativeId,
961 comment: &NativeId,
962 ) -> Result<Option<NativeId>, SourceError> {
963 let _ = (task, comment);
964 Err(commentless(self.kind()))
965 }
966
967 /// Write one comment a copy carries onto `task`: a new one when `existing` is `None`, and
968 /// otherwise the comment `existing`, which a copy wrote before, made to say `copy` — or
969 /// `None` when this source holds no such task or no such comment.
970 ///
971 /// The answer reports [`Comment::origin`] as `copy.origin`, however the source keeps it.
972 ///
973 /// **Defaulted to the hidden marker**, which is right for every source that records a
974 /// comment's author and times itself: the body is written through
975 /// [`add_comment`](Self::add_comment) or [`edit_comment`](Self::edit_comment) with
976 /// [`CommentOrigin::mark`](crate::CommentOrigin::mark) appended, and read back
977 /// [`unmarked`](Comment::unmarked). A source that can store the origin natively — and so
978 /// takes the origin's own id, author and times for the copy — overrides this.
979 ///
980 /// # Errors
981 ///
982 /// As [`add_comment`](Self::add_comment).
983 async fn write_comment_copy(
984 &self,
985 task: &NativeId,
986 existing: Option<&NativeId>,
987 copy: &CommentCopy,
988 ) -> Result<Option<Comment>, SourceError> {
989 let body = CommentBody::new(copy.origin.qualified()?.mark(copy.body.as_str()))?;
990 let written = match existing {
991 Some(comment) => self.edit_comment(task, comment, &body).await?,
992 None => {
993 self.add_comment(task, &NewComment { body, author: None })
994 .await?
995 }
996 };
997 Ok(written.map(Comment::unmarked))
998 }
999
1000 /// Whether this source keeps template answers beside its items at all.
1001 ///
1002 /// What lets a regenerate tell an item whose answers went missing — a block deleted by
1003 /// hand, which the regenerate writes back — from an item of a source that never keeps any,
1004 /// where a missing answer is no difference. Defaulted to `false`, as
1005 /// [`task_template_answers`](Self::task_template_answers) is defaulted to `None`; a source
1006 /// that keeps answers answers `true` here.
1007 fn keeps_template_answers(&self) -> bool {
1008 false
1009 }
1010
1011 /// The template answers the task `id` was last rendered from, as this source keeps them
1012 /// beside the task — or `None` when it keeps none for it, or holds no such task.
1013 ///
1014 /// Keeping answers is a source's own choice, never an obligation: a source whose items
1015 /// are one record in a hosted system has no room beside the item that is not the item, and
1016 /// answers written into its content or its metadata would duplicate what the content
1017 /// already says. Defaulted to `None`, which is what keeps this an addition rather than a
1018 /// break. A source that keeps them owes three things: they are in neither
1019 /// [`Task::content`] nor [`Task::metadata`], they are written only by
1020 /// [`write_task_rendered`](Self::write_task_rendered) and
1021 /// [`set_task_rendering`](Self::set_task_rendering), and they come back here exactly as
1022 /// they were written, JSON types intact.
1023 ///
1024 /// # Errors
1025 ///
1026 /// Returns a [`SourceError`] when the source could not answer, a record whose answers it
1027 /// cannot read included.
1028 async fn task_template_answers(
1029 &self,
1030 id: &NativeId,
1031 ) -> Result<Option<BTreeMap<String, Value>>, SourceError> {
1032 let _ = id;
1033 Ok(None)
1034 }
1035
1036 /// The template answers the document `id` was last rendered from, on exactly the terms of
1037 /// [`task_template_answers`](Self::task_template_answers).
1038 ///
1039 /// # Errors
1040 ///
1041 /// As [`task_template_answers`](Self::task_template_answers).
1042 async fn document_template_answers(
1043 &self,
1044 id: &NativeId,
1045 ) -> Result<Option<BTreeMap<String, Value>>, SourceError> {
1046 let _ = id;
1047 Ok(None)
1048 }
1049
1050 /// The template answers the project `id` was last rendered from, on exactly the terms of
1051 /// [`task_template_answers`](Self::task_template_answers).
1052 ///
1053 /// # Errors
1054 ///
1055 /// As [`task_template_answers`](Self::task_template_answers).
1056 async fn project_template_answers(
1057 &self,
1058 id: &NativeId,
1059 ) -> Result<Option<BTreeMap<String, Value>>, SourceError> {
1060 let _ = id;
1061 Ok(None)
1062 }
1063
1064 /// Create or update one task exactly as [`write_task`](Self::write_task) does, keeping
1065 /// `answers` — the answers its content was rendered from — beside it in the same write
1066 /// where this source keeps answers at all.
1067 ///
1068 /// The task arrives carrying its provenance under [`MetadataKey::TEMPLATE_KEY`] like any
1069 /// other metadata entry. Defaulted to [`write_task`](Self::write_task) alone: a source that
1070 /// keeps no answers writes the task and nothing beside it, which is the whole of what it
1071 /// owes. A source that keeps them writes the task and the answers together, so a reader
1072 /// never finds one without the other.
1073 ///
1074 /// # Errors
1075 ///
1076 /// As [`write_task`](Self::write_task).
1077 async fn write_task_rendered(
1078 &self,
1079 write: &ItemWrite<Task>,
1080 answers: &BTreeMap<String, Value>,
1081 ) -> Result<NativeId, SourceError> {
1082 let _ = answers;
1083 self.write_task(write).await
1084 }
1085
1086 /// Create or update one document, on exactly the terms of
1087 /// [`write_task_rendered`](Self::write_task_rendered).
1088 ///
1089 /// # Errors
1090 ///
1091 /// As [`write_document`](Self::write_document).
1092 async fn write_document_rendered(
1093 &self,
1094 write: &ItemWrite<Document>,
1095 answers: &BTreeMap<String, Value>,
1096 ) -> Result<NativeId, SourceError> {
1097 let _ = answers;
1098 self.write_document(write).await
1099 }
1100
1101 /// Create or update one project, on exactly the terms of
1102 /// [`write_task_rendered`](Self::write_task_rendered).
1103 ///
1104 /// # Errors
1105 ///
1106 /// As [`write_project`](Self::write_project).
1107 async fn write_project_rendered(
1108 &self,
1109 write: &ItemWrite<Project>,
1110 answers: &BTreeMap<String, Value>,
1111 ) -> Result<NativeId, SourceError> {
1112 let _ = answers;
1113 self.write_project(write).await
1114 }
1115
1116 /// Replace one task's rendering — its content, byte for byte, its
1117 /// [`MetadataKey::TEMPLATE_KEY`] entry, set to `provenance`, and the answers it keeps
1118 /// beside the task where it keeps any — in one write, and change nothing else about it;
1119 /// or answer `None` when this source holds no such task.
1120 ///
1121 /// The content is [`Task::content`] exactly as a later read reports it, on the terms of
1122 /// [`set_task_content`](Self::set_task_content). Title, status, priority, labels, every
1123 /// other metadata entry, repositories, dependencies, [`Task::delivers`],
1124 /// [`Task::delivered_by`], project and comments are left exactly as they are. The write is
1125 /// one write: a reader sees the task as it was or as it is now, never a new content beside
1126 /// the old provenance or the old answers.
1127 ///
1128 /// Defaulted to [`unwritable_field`] on exactly the terms of
1129 /// [`set_task_status`](Self::set_task_status). A source declaring
1130 /// [`WriteSupport::Unsupported`] is never asked.
1131 ///
1132 /// [`Task::delivers`]: crate::Task::delivers
1133 /// [`Task::delivered_by`]: crate::Task::delivered_by
1134 ///
1135 /// # Errors
1136 ///
1137 /// Returns [`SourceError::Refused`] when this source cannot replace a task's rendering on
1138 /// its own, or cannot represent this one; and whatever else the source could not do the
1139 /// write for.
1140 async fn set_task_rendering(
1141 &self,
1142 id: &NativeId,
1143 content: &str,
1144 provenance: &Value,
1145 answers: &BTreeMap<String, Value>,
1146 ) -> Result<Option<()>, SourceError> {
1147 let _ = (id, content, provenance, answers);
1148 Err(unwritable_field(self.kind(), "rendering"))
1149 }
1150
1151 /// Replace one document's rendering, on exactly the terms of
1152 /// [`set_task_rendering`](Self::set_task_rendering). A source declaring
1153 /// [`Capabilities::documents`] unsupported is never asked.
1154 ///
1155 /// # Errors
1156 ///
1157 /// As [`set_task_rendering`](Self::set_task_rendering).
1158 async fn set_document_rendering(
1159 &self,
1160 id: &NativeId,
1161 content: &str,
1162 provenance: &Value,
1163 answers: &BTreeMap<String, Value>,
1164 ) -> Result<Option<()>, SourceError> {
1165 let _ = (id, content, provenance, answers);
1166 Err(SourceError::Refused {
1167 message: format!(
1168 "the {} plugin cannot write a document's rendering on its own",
1169 self.kind()
1170 ),
1171 })
1172 }
1173
1174 /// Replace one project's rendering, on exactly the terms of
1175 /// [`set_task_rendering`](Self::set_task_rendering): its content, its
1176 /// [`MetadataKey::TEMPLATE_KEY`] entry and the answers kept beside it change, and its
1177 /// title, status, labels, every other metadata entry, repositories and dependencies are
1178 /// left exactly as they are.
1179 ///
1180 /// # Errors
1181 ///
1182 /// As [`set_task_rendering`](Self::set_task_rendering).
1183 async fn set_project_rendering(
1184 &self,
1185 id: &NativeId,
1186 content: &str,
1187 provenance: &Value,
1188 answers: &BTreeMap<String, Value>,
1189 ) -> Result<Option<()>, SourceError> {
1190 let _ = (id, content, provenance, answers);
1191 Err(SourceError::Refused {
1192 message: format!(
1193 "the {} plugin cannot write a project's rendering on its own",
1194 self.kind()
1195 ),
1196 })
1197 }
1198
1199 /// The image assets the task `id` holds, or none when it holds none or this source holds
1200 /// no such task.
1201 ///
1202 /// Defaulted to none, which is what keeps assets an addition rather than a break: a source
1203 /// that keeps no assets needs no edit and reports every record as holding none. A source
1204 /// declaring [`Capabilities::assets`] native answers what it holds for the record, in any
1205 /// order — the engine orders them by the content's first reference.
1206 ///
1207 /// # Errors
1208 ///
1209 /// Returns a [`SourceError`] when the source could not answer.
1210 async fn task_assets(&self, id: &NativeId) -> Result<Vec<Asset>, SourceError> {
1211 let _ = id;
1212 Ok(Vec::new())
1213 }
1214
1215 /// The image assets the document `id` holds, on exactly the terms of
1216 /// [`task_assets`](Self::task_assets).
1217 ///
1218 /// # Errors
1219 ///
1220 /// As [`task_assets`](Self::task_assets).
1221 async fn document_assets(&self, id: &NativeId) -> Result<Vec<Asset>, SourceError> {
1222 let _ = id;
1223 Ok(Vec::new())
1224 }
1225
1226 /// The bytes of the asset `name` the task `id` holds, or `None` when it holds no such asset
1227 /// or this source holds no such task.
1228 ///
1229 /// Defaulted to `None`, on the terms of [`task_assets`](Self::task_assets).
1230 ///
1231 /// # Errors
1232 ///
1233 /// Returns a [`SourceError`] when the source could not answer.
1234 async fn task_asset(
1235 &self,
1236 id: &NativeId,
1237 name: &AssetName,
1238 ) -> Result<Option<Vec<u8>>, SourceError> {
1239 let _ = (id, name);
1240 Ok(None)
1241 }
1242
1243 /// The bytes of the asset `name` the document `id` holds, on exactly the terms of
1244 /// [`task_asset`](Self::task_asset).
1245 ///
1246 /// # Errors
1247 ///
1248 /// As [`task_asset`](Self::task_asset).
1249 async fn document_asset(
1250 &self,
1251 id: &NativeId,
1252 name: &AssetName,
1253 ) -> Result<Option<Vec<u8>>, SourceError> {
1254 let _ = (id, name);
1255 Ok(None)
1256 }
1257
1258 /// Create or update one task carrying image assets, in one write: the task as
1259 /// [`write_task`](Self::write_task) writes it — or, with `answers`, as
1260 /// [`write_task_rendered`](Self::write_task_rendered) does — and `assets` as the record's
1261 /// whole asset set, answering with where it landed and the content as stored.
1262 ///
1263 /// Only a source declaring [`Capabilities::assets`] native is asked, and the engine has
1264 /// already checked that every reference in the content names one of `assets` and every one
1265 /// of `assets` is referenced. A source that keeps the bytes beside the record stores each
1266 /// payload under its name, removes every asset the record held that `assets` does not
1267 /// name, and answers the content unchanged. A source that serves them at a URL uploads each
1268 /// payload that carries bytes, reuses the URL [`AssetWrite::recorded_assets`] records for
1269 /// each that does not, and writes the record through
1270 /// [`serve_asset_references`](crate::serve_asset_references) — so its references point at
1271 /// those URLs, its [`MetadataKey::ASSETS_KEY`] records each `{sha256, url}`, and a
1272 /// rendering's `body_digest` stays true of what landed — answering the content it wrote.
1273 ///
1274 /// Defaulted to [`assetless`], which is what a source with no assets answers.
1275 ///
1276 /// # Errors
1277 ///
1278 /// As [`write_task`](Self::write_task), and [`SourceError::Refused`] from a source that
1279 /// keeps no assets.
1280 async fn write_task_with_assets(
1281 &self,
1282 write: &ItemWrite<Task>,
1283 answers: Option<&BTreeMap<String, Value>>,
1284 assets: &AssetWrite,
1285 ) -> Result<AssetsWritten, SourceError> {
1286 let _ = (write, answers, assets);
1287 Err(assetless(self.kind()))
1288 }
1289
1290 /// Create or update one document carrying image assets, on exactly the terms of
1291 /// [`write_task_with_assets`](Self::write_task_with_assets).
1292 ///
1293 /// # Errors
1294 ///
1295 /// As [`write_task_with_assets`](Self::write_task_with_assets).
1296 async fn write_document_with_assets(
1297 &self,
1298 write: &ItemWrite<Document>,
1299 answers: Option<&BTreeMap<String, Value>>,
1300 assets: &AssetWrite,
1301 ) -> Result<AssetsWritten, SourceError> {
1302 let _ = (write, answers, assets);
1303 Err(assetless(self.kind()))
1304 }
1305
1306 /// Replace one task's rendering as [`set_task_rendering`](Self::set_task_rendering) does,
1307 /// and its asset set as [`write_task_with_assets`](Self::write_task_with_assets) does, in
1308 /// one write; or answer `None` when this source holds no such task.
1309 ///
1310 /// Defaulted to [`assetless`].
1311 ///
1312 /// # Errors
1313 ///
1314 /// As [`set_task_rendering`](Self::set_task_rendering), and [`SourceError::Refused`] from a
1315 /// source that keeps no assets.
1316 async fn set_task_rendering_with_assets(
1317 &self,
1318 id: &NativeId,
1319 content: &str,
1320 provenance: &Value,
1321 answers: &BTreeMap<String, Value>,
1322 assets: &AssetWrite,
1323 ) -> Result<Option<AssetsWritten>, SourceError> {
1324 let _ = (id, content, provenance, answers, assets);
1325 Err(assetless(self.kind()))
1326 }
1327
1328 /// Replace one document's rendering and its asset set, on exactly the terms of
1329 /// [`set_task_rendering_with_assets`](Self::set_task_rendering_with_assets).
1330 ///
1331 /// # Errors
1332 ///
1333 /// As [`set_task_rendering_with_assets`](Self::set_task_rendering_with_assets).
1334 async fn set_document_rendering_with_assets(
1335 &self,
1336 id: &NativeId,
1337 content: &str,
1338 provenance: &Value,
1339 answers: &BTreeMap<String, Value>,
1340 assets: &AssetWrite,
1341 ) -> Result<Option<AssetsWritten>, SourceError> {
1342 let _ = (id, content, provenance, answers, assets);
1343 Err(assetless(self.kind()))
1344 }
1345
1346 /// What this source has sent to its backend since it was built and what that spent, or
1347 /// `None` when it does not meter its own requests.
1348 ///
1349 /// Defaulted to `None`, which is what keeps metering an addition rather than a break: a
1350 /// source that does not count its requests needs no edit, and is reported as not
1351 /// metering rather than as having spent nothing. A source that answers owes a running
1352 /// total — see [`Metering`] — because what one command spent is read as the difference
1353 /// between two readings.
1354 ///
1355 /// # Errors
1356 ///
1357 /// Returns a [`SourceError`] when the reading itself could not be taken. A caller
1358 /// reports such a source as not metering; what a command cost is never a reason for the
1359 /// command to fail.
1360 async fn metering(&self) -> Result<Option<Metering>, SourceError> {
1361 Ok(None)
1362 }
1363
1364 /// End one command: drop everything held that a person can change.
1365 ///
1366 /// A command is one unit of work — one invocation of the binary, or whatever a caller
1367 /// holding one engine for many units of work calls between them. Within a command a
1368 /// source may hold what it read and reuse it; across this call it may not. After it
1369 /// returns, a source answers no read and bases no write on item content, statuses, board
1370 /// contents or search results it held before the call. It may keep only identifiers and
1371 /// vocabulary that stay valid in normal use — a repository's node id, a team's workflow
1372 /// states — and only where a lookup that misses one re-reads rather than refusing.
1373 ///
1374 /// Defaulted to doing nothing, which is right for a source that holds nothing between
1375 /// calls or holds only what the rule above lets it keep. A source that holds more owes
1376 /// an override. Never called by the engine on its own: the binary's one command is its
1377 /// whole process, so only a caller that outlives a command calls it.
1378 ///
1379 /// # Errors
1380 ///
1381 /// Returns a [`SourceError`] when the source could not be sure it dropped what it held —
1382 /// ordinarily a hosted plugin that did not answer. A caller should not go on to the next
1383 /// command on that source's earlier reads.
1384 async fn end_command(&self) -> Result<(), SourceError> {
1385 Ok(())
1386 }
1387
1388 /// Who can read where `target` lands, read live from the backend.
1389 ///
1390 /// This is the destination's reality, which the engine holds a declaration against
1391 /// before it lets a write through: a source a configuration declares private receives an
1392 /// item that may only be written somewhere private only while this answers
1393 /// [`Visibility::Private`]. The engine asks immediately before the write it is about, so
1394 /// a source must not answer from anything it read for an earlier write unless its
1395 /// backend's visibility cannot change in between.
1396 ///
1397 /// Defaulted to [`Visibility::Unknown`], which is the answer of a source that cannot say
1398 /// and is never treated as private. A source on this machine answers `Private` — what it
1399 /// writes stays on the host — and a hosted source reads its backend.
1400 ///
1401 /// # Errors
1402 ///
1403 /// Returns a [`SourceError`] when the backend could not be asked or would not answer —
1404 /// a credential lacking the scope the read needs among them. An unreadable visibility is
1405 /// never guessed: the engine refuses the write.
1406 async fn visibility(&self, target: &WriteTarget<'_>) -> Result<Visibility, SourceError> {
1407 let _ = target;
1408 Ok(Visibility::Unknown)
1409 }
1410}
1411
1412/// The factory that turns one configuration block into a live [`TaskSource`].
1413///
1414/// Having the compile-time registry and the subprocess seam be the same shape is
1415/// the whole reason this is a trait rather than a free function.
1416pub trait SourcePlugin: Send + Sync + 'static {
1417 /// The name a configuration document's `plugin:` field names.
1418 fn kind(&self) -> &'static str;
1419
1420 /// The JSON Schema for this plugin's own `config:` block.
1421 fn config_schema(&self) -> Schema;
1422
1423 /// Build a live source from one configuration block.
1424 ///
1425 /// `name` is the configured source's name, for error messages only — a
1426 /// plugin never learns it for any other purpose.
1427 ///
1428 /// # Errors
1429 ///
1430 /// Returns [`SourceError::Config`] when `config` is not valid for this
1431 /// plugin, or [`SourceError::Auth`] when a named credential is absent.
1432 fn build(
1433 &self,
1434 name: &SourceName,
1435 config: &serde_json::Value,
1436 secrets: &dyn SecretResolver,
1437 ) -> Result<Box<dyn TaskSource>, SourceError>;
1438
1439 /// Build a live source from one configuration block, handing it the clock it paces and
1440 /// backs off on.
1441 ///
1442 /// The binary builds every in-process source through this, with the process's one
1443 /// [`SharedClock`] — [`system_clock`](crate::system_clock) unless a test asked for
1444 /// simulated time. Defaulted to [`build`](Self::build), which ignores the clock: a plugin
1445 /// that never waits needs no edit, and one that does overrides this and routes its waits
1446 /// through `clock`.
1447 ///
1448 /// # Errors
1449 ///
1450 /// As [`build`](Self::build).
1451 fn build_with_clock(
1452 &self,
1453 name: &SourceName,
1454 config: &serde_json::Value,
1455 secrets: &dyn SecretResolver,
1456 clock: SharedClock,
1457 ) -> Result<Box<dyn TaskSource>, SourceError> {
1458 let _ = clock;
1459 self.build(name, config, secrets)
1460 }
1461
1462 /// The fields of this plugin's `config:` block that name a filesystem path, as dotted
1463 /// paths into that block.
1464 ///
1465 /// A relative value at one of these, **supplied by a configuration document**, is
1466 /// resolved against the directory holding that document before [`Self::build`] sees it;
1467 /// supplied through the environment or a flag it keeps resolving against the process
1468 /// working directory, because there is no document to rebase it on. A plugin is handed
1469 /// values and no origins, so this declaration is the only way it can say which of its
1470 /// own fields that rule reaches.
1471 ///
1472 /// Defaulted to none, which is what keeps this an addition rather than a break: a
1473 /// plugin whose block holds no path needs no edit, and a caller asks every plugin
1474 /// rather than keeping a table of which ones answer.
1475 // llmlint: ignore[invalid_states_unrepresentable] The identity of a configuration field
1476 // is a name, and no type can make a wrong one unrepresentable here: every string is a
1477 // syntactically valid dotted path, so a newtype would validate nothing and would only
1478 // move where a name that is not a field of *this* plugin is accepted. What decides that
1479 // is whether the name is a property of the schema `config_schema` publishes — a
1480 // per-plugin fact no shared type can hold — so the gate is per plugin and executable:
1481 // `document_relative_fields_are_fields_this_plugin_declares` in
1482 // `onetaskgraph-local-md/tests/plugin.rs`, which a plugin adding a declaration owes its
1483 // own copy of.
1484 fn document_relative_paths(&self) -> &'static [&'static str] {
1485 &[]
1486 }
1487
1488 /// What names the remote store a source built from `config` reads — a board's owner and
1489 /// number, a Linear endpoint and team — or `None` for a source with no remote.
1490 ///
1491 /// The store's local cache of a remote source lives under a digest of this, so every
1492 /// process configuring the same remote shares one cache and no two remotes share one.
1493 /// Defaulted to `None`, which is what a source keeping its work on this machine — a
1494 /// folder of Markdown — answers: it has nothing to cache, and its cache setting is
1495 /// ignored. A `config` this plugin would refuse answers `None` too.
1496 fn remote_identity(&self, config: &serde_json::Value) -> Option<String> {
1497 let _ = config;
1498 None
1499 }
1500}
1501
1502/// How a plugin reads the credential its configuration names.
1503///
1504/// A configuration document never carries a credential value, only the name of
1505/// the environment variable holding it.
1506pub trait SecretResolver: Send + Sync {
1507 /// The value of `var`, or `None` when nothing defines it.
1508 ///
1509 /// The returned value is never logged and never appears in `Debug` output.
1510 fn get(&self, var: &str) -> Option<SecretString>;
1511}