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