Skip to main content

onetaskgraph_plugin_api/
source.rs

1//! The two traits a plugin implements, and the secret lookup it is handed.
2
3use std::collections::BTreeMap;
4
5use schemars::{JsonSchema, Schema};
6use secrecy::SecretString;
7use serde::{Deserialize, Serialize};
8use serde_json::Value;
9
10use crate::{
11    Capabilities, Comment, CommentBody, DependencyEdge, Direction, Document, DocumentQuery,
12    ItemWrite, Label, MetadataKey, MetadataRecord, Metering, NativeId, NewComment, Page,
13    PageRequest, Priority, Project, ProjectQuery, SourceError, SourceName, Status, StatusCategory,
14    Task, TaskQuery, TaskRef, WriteSupport, commentless, documentless, unwritable,
15    unwritable_field, unwritable_metadata,
16};
17
18/// Whether a source is answering right now.
19///
20/// # Placement is an open contract question
21///
22/// This type lives here because [`TaskSource::health`] returns it and the trait
23/// lives here: placing it in `onetaskgraph-core` would make this crate depend on
24/// the engine and invert the one direction the crate split exists to establish.
25/// The approved contract enumerates this crate's contents exhaustively and does
26/// not name `Health`, so the enumeration and the trait as written cannot both
27/// stand. Compiling forces the placement below; the resolution — add it to the
28/// enumeration, or redesign `health` so no such type crosses the boundary —
29/// belongs to the contract's owner, not to this crate. See `AGENTS.md`.
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
31// 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.
32pub struct Health {
33    /// Whether the source answered.
34    ///
35    /// A bare `bool` beside an untyped `detail` cannot say that an unreachable source
36    /// must explain itself, or keep "reachable with a warning" apart from "reachable";
37    /// an enum carrying the detail in its unreachable variant would.
38    // llmlint: ignore[invalid_states_unrepresentable] SECOND PERMITTED REASON — this
39    // restates at this field the justification already recorded at
40    // `Capabilities.max_page_size` (capability.rs), `PageRequest.limit` (query.rs), this
41    // type's own doc comment above, and AGENTS.md's "Open contract question — `Health`":
42    // `Health`'s shape is approved contract text that `TaskSource::health` returns, so an
43    // enum here would change the serialized form and the trait six undispatched nodes
44    // implement. That is the contract owner's call, not this crate's.
45    // 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`".
46    pub reachable: bool,
47    /// What the source said, when it said anything useful.
48    pub detail: Option<String>,
49}
50
51/// One configured source, as the engine drives it.
52///
53/// Dyn-compatible through `async_trait` because the engine holds
54/// `Vec<Box<dyn TaskSource>>` over heterogeneous plugins.
55///
56/// Three rules bind every implementation, and the engine's compensation is only
57/// correct while all three hold:
58///
59/// 1. **Apply** every predicate you declare [`Support::Native`](crate::Support::Native).
60/// 2. **Ignore** every [`Support`](crate::Support)-typed predicate you declare
61///    `Unsupported` — return the *wider* result set, never a narrower one.
62///    Silently dropping rows for a predicate you did not declare is the one
63///    failure no test above the plugin can catch.
64/// 3. Never return a silently empty dependency read. Rule 2 reaches the
65///    `Support`-typed *predicates* alone; a dependency read is always real, and so is a
66///    document read — [`Capabilities::documents`] says whether this source has documents
67///    at all, and a source that says it has none is never asked for one rather than
68///    answering an empty page.
69#[async_trait::async_trait]
70pub trait TaskSource: Send + Sync {
71    /// The plugin kind that built this source, for display and for plan output.
72    fn kind(&self) -> &'static str;
73
74    /// What this source applies itself. Read once per query by the engine.
75    fn capabilities(&self) -> Capabilities;
76
77    /// Whether the source is answering right now.
78    ///
79    /// # Errors
80    ///
81    /// Returns a [`SourceError`] when the check itself could not be made.
82    async fn health(&self) -> Result<Health, SourceError>;
83
84    /// Fetch one task by its native id, or `None` when there is no such task.
85    ///
86    /// # Errors
87    ///
88    /// Returns a [`SourceError`] when the source could not answer.
89    async fn get_task(&self, id: &NativeId) -> Result<Option<Task>, SourceError>;
90
91    /// Fetch one project by its native id, or `None` when there is no such project.
92    ///
93    /// # Errors
94    ///
95    /// Returns a [`SourceError`] when the source could not answer.
96    async fn get_project(&self, id: &NativeId) -> Result<Option<Project>, SourceError>;
97
98    /// One page of the tasks matching `query`.
99    ///
100    /// # Errors
101    ///
102    /// Returns a [`SourceError`] when the source could not answer.
103    async fn query_tasks(
104        &self,
105        query: &TaskQuery,
106        page: &PageRequest,
107    ) -> Result<Page<Task>, SourceError>;
108
109    /// One page of the projects matching `query`.
110    ///
111    /// # Errors
112    ///
113    /// Returns a [`SourceError`] when the source could not answer.
114    async fn query_projects(
115        &self,
116        query: &ProjectQuery,
117        page: &PageRequest,
118    ) -> Result<Page<Project>, SourceError>;
119
120    /// One page of every label this source knows.
121    ///
122    /// # Errors
123    ///
124    /// Returns a [`SourceError`] when the source could not answer.
125    async fn labels(&self, page: &PageRequest) -> Result<Page<Label>, SourceError>;
126
127    /// One page of the task dependency edges at `id`, in `direction`.
128    ///
129    /// # Errors
130    ///
131    /// Returns a [`SourceError`] when the source could not answer.
132    async fn task_dependencies(
133        &self,
134        id: &NativeId,
135        direction: Direction,
136        page: &PageRequest,
137    ) -> Result<Page<DependencyEdge>, SourceError>;
138
139    /// One page of the project dependency edges at `id`, in `direction`.
140    ///
141    /// # Errors
142    ///
143    /// Returns a [`SourceError`] when the source could not answer.
144    async fn project_dependencies(
145        &self,
146        id: &NativeId,
147        direction: Direction,
148        page: &PageRequest,
149    ) -> Result<Page<DependencyEdge>, SourceError>;
150
151    /// Whether this source can be written through at all.
152    ///
153    /// Defaulted to [`WriteSupport::Unsupported`], which is what keeps this a read
154    /// interface for every source that has nothing to write into: one that cannot be
155    /// written needs no edit and keeps working. Read before a write is attempted, so a
156    /// copy naming such a source as its destination is refused before anything is read.
157    fn writes(&self) -> WriteSupport {
158        WriteSupport::Unsupported
159    }
160
161    /// Create or update one task, answering with the native id the destination holds it
162    /// under.
163    ///
164    /// A source declaring [`WriteSupport::Supported`] owes three things here. It refuses,
165    /// naming the field, anything it cannot represent rather than dropping it — including
166    /// a metadata key it cannot carry, which it names. It writes every other field it was
167    /// given. And it never creates when [`ItemWrite::target`] names an item it does not
168    /// hold.
169    ///
170    /// # Errors
171    ///
172    /// Returns [`SourceError::Refused`] when this source has no write side, when a field
173    /// or a metadata key cannot be represented, or when `target` names nothing here; and
174    /// whatever else the source could not do the write for.
175    async fn write_task(&self, write: &ItemWrite<Task>) -> Result<NativeId, SourceError> {
176        let _ = write;
177        Err(unwritable(self.kind()))
178    }
179
180    /// Create or update one project, on exactly the terms of
181    /// [`write_task`](Self::write_task).
182    ///
183    /// # Errors
184    ///
185    /// As [`write_task`](Self::write_task).
186    async fn write_project(&self, write: &ItemWrite<Project>) -> Result<NativeId, SourceError> {
187        let _ = write;
188        Err(unwritable(self.kind()))
189    }
190
191    /// Set the status of one task this source holds, and change nothing else about it,
192    /// answering with the status as this source now reads it — or `None` when this source
193    /// holds no such task.
194    ///
195    /// The category lands where this source's own mapping sends it, exactly as a
196    /// [`write_task`](Self::write_task) of a task in that category would: a category this
197    /// source has disabled is refused in the words a write of it is refused with. Title,
198    /// content, labels, metadata, dependencies, [`Task::delivers`], [`Task::delivered_by`],
199    /// project and comments are left exactly as they are.
200    ///
201    /// Defaulted to [`unwritable_field`], which is what keeps this an addition rather than a
202    /// break: a source that cannot write a status on its own needs no edit and refuses by
203    /// saying so. A source declaring [`WriteSupport::Unsupported`] is never asked.
204    ///
205    /// [`Task::delivers`]: crate::Task::delivers
206    /// [`Task::delivered_by`]: crate::Task::delivered_by
207    ///
208    /// # Errors
209    ///
210    /// Returns [`SourceError::Refused`] when this source cannot write a status, or cannot
211    /// write this one; and whatever else the source could not do the write for.
212    async fn set_task_status(
213        &self,
214        id: &NativeId,
215        category: StatusCategory,
216    ) -> Result<Option<Status>, SourceError> {
217        let _ = (id, category);
218        Err(unwritable_field(self.kind(), "status"))
219    }
220
221    /// Set the priority of one task this source holds, and change nothing else about it,
222    /// answering with the priority as this source now reads it — or `None` when this source
223    /// holds no such task.
224    ///
225    /// [`Priority::None`] clears the priority. Title, content, status, labels, metadata,
226    /// repositories, dependencies and comments are left exactly as they are. Nothing about
227    /// the task's status moves, so the engine re-evaluates no delivered task after it.
228    ///
229    /// Defaulted to [`unwritable_field`], which is what keeps this an addition rather than a
230    /// break. A source declaring [`WriteSupport::Unsupported`], or declaring
231    /// [`Capabilities::priority`] unsupported, is never asked.
232    ///
233    /// # Errors
234    ///
235    /// Returns [`SourceError::Refused`] when this source cannot write a priority, or cannot
236    /// write this one — a board with no option for it, naming the option; and whatever else
237    /// the source could not do the write for.
238    async fn set_task_priority(
239        &self,
240        id: &NativeId,
241        priority: Priority,
242    ) -> Result<Option<Priority>, SourceError> {
243        let _ = (id, priority);
244        Err(unwritable_field(self.kind(), "priority"))
245    }
246
247    /// Replace the content of one task this source holds with `content`, byte for byte, and
248    /// change nothing else about it — or answer `None` when this source holds no such task.
249    ///
250    /// The content is [`Task::content`] exactly as this source reports it: what a later read
251    /// answers there is `content`. Where the source keeps something else inside the same
252    /// backend field — a metadata block in an issue body — that is kept as it was, and so is
253    /// every other member: title, status, priority, labels, metadata, repositories,
254    /// dependencies, project and comments. Nothing about the task's status moves, so the
255    /// engine re-evaluates no delivered task after it.
256    ///
257    /// Defaulted to [`unwritable_field`] on exactly the terms of
258    /// [`set_task_status`](Self::set_task_status). A source declaring
259    /// [`WriteSupport::Unsupported`] is never asked.
260    ///
261    /// # Errors
262    ///
263    /// Returns [`SourceError::Refused`] when this source cannot write a task's content on its
264    /// own, or cannot represent this one; and whatever else the source could not do the write
265    /// for.
266    async fn set_task_content(
267        &self,
268        id: &NativeId,
269        content: &str,
270    ) -> Result<Option<()>, SourceError> {
271        let _ = (id, content);
272        Err(unwritable_field(self.kind(), "content"))
273    }
274
275    /// Replace the [`Task::delivered_by`] of one task this source holds, and change nothing
276    /// else about it — or answer `None` when this source holds no such task.
277    ///
278    /// Every entry is a qualified id, and the list is the whole of it: what the task held
279    /// there before is replaced, not merged. It is the store's to keep in step — the engine
280    /// calls this whenever it writes a task's [`Task::delivers`] — and nothing a person types
281    /// reaches it directly.
282    ///
283    /// Defaulted to [`unwritable_field`] on exactly the terms of
284    /// [`set_task_status`](Self::set_task_status).
285    ///
286    /// [`Task::delivers`]: crate::Task::delivers
287    /// [`Task::delivered_by`]: crate::Task::delivered_by
288    ///
289    /// # Errors
290    ///
291    /// Returns [`SourceError::Refused`] when this source cannot hold the list, and whatever
292    /// else it could not do the write for.
293    async fn set_delivered_by(
294        &self,
295        id: &NativeId,
296        delivered_by: &[TaskRef],
297    ) -> Result<Option<()>, SourceError> {
298        let _ = (id, delivered_by);
299        Err(unwritable_field(self.kind(), "delivered_by"))
300    }
301
302    /// Set one key of the metadata of one task this source holds, and change nothing else
303    /// about it, answering with the task as this source reads it back after the write — or
304    /// `None` when this source holds no such task.
305    ///
306    /// The key is added when the task does not hold it and replaced when it does; every other
307    /// metadata key, and every other field of the task, is left exactly as it was. A `value`
308    /// the task already holds under `key` is a write that changes nothing, and a source owes
309    /// it no write at all. The answer is a read, not an echo: what the engine reports as the
310    /// value is what the returned task holds under `key`.
311    ///
312    /// Nothing about the task's status or its [`Task::delivers`] moves, so the engine
313    /// re-evaluates no delivered task after this write.
314    ///
315    /// Defaulted to [`unwritable_metadata`] on exactly the terms of
316    /// [`set_task_status`](Self::set_task_status): a source that cannot write one key on its
317    /// own needs no edit and refuses by saying so. A source declaring
318    /// [`WriteSupport::Unsupported`] is never asked.
319    ///
320    /// [`Task::delivers`]: crate::Task::delivers
321    ///
322    /// # Errors
323    ///
324    /// Returns [`SourceError::Refused`] when this source cannot write one key of a task's
325    /// metadata on its own, or cannot write this one without changing something else; and
326    /// whatever else the source could not do the write for.
327    async fn set_task_metadata(
328        &self,
329        id: &NativeId,
330        key: &MetadataKey,
331        value: &Value,
332    ) -> Result<Option<Task>, SourceError> {
333        let _ = (id, key, value);
334        Err(unwritable_metadata(self.kind(), MetadataRecord::Task))
335    }
336
337    /// Set one key of the metadata of one project this source holds, on exactly the terms of
338    /// [`set_task_metadata`](Self::set_task_metadata).
339    ///
340    /// # Errors
341    ///
342    /// As [`set_task_metadata`](Self::set_task_metadata).
343    async fn set_project_metadata(
344        &self,
345        id: &NativeId,
346        key: &MetadataKey,
347        value: &Value,
348    ) -> Result<Option<Project>, SourceError> {
349        let _ = (id, key, value);
350        Err(unwritable_metadata(self.kind(), MetadataRecord::Project))
351    }
352
353    /// Set one key of the metadata of one document this source holds, on exactly the terms of
354    /// [`set_task_metadata`](Self::set_task_metadata).
355    ///
356    /// A source declaring [`Capabilities::documents`] unsupported is never asked, exactly as
357    /// it is never asked for a document read.
358    ///
359    /// # Errors
360    ///
361    /// As [`set_task_metadata`](Self::set_task_metadata).
362    async fn set_document_metadata(
363        &self,
364        id: &NativeId,
365        key: &MetadataKey,
366        value: &Value,
367    ) -> Result<Option<Document>, SourceError> {
368        let _ = (id, key, value);
369        Err(unwritable_metadata(self.kind(), MetadataRecord::Document))
370    }
371
372    /// Remove one task this destination holds, so a copy that could not finish can put
373    /// the destination back the way it found it.
374    ///
375    /// This is not a verb of the product: nothing a user types deletes anything, and a
376    /// copy never deletes an item it did not itself create in the run that is failing.
377    /// It exists because a copy is either complete or it never happened — a half-written
378    /// project has to be run again, and the re-run is the mutation burst that trips a
379    /// hosted destination's rate limiter. Undoing this run's own creates is what removes
380    /// that retry at source.
381    ///
382    /// A source declaring [`WriteSupport::Supported`] owes a real implementation, for the
383    /// reason it owes [`write_task`](Self::write_task) one: the engine will create items
384    /// there, so it has to be able to remove the ones it created. An `id` naming nothing
385    /// is **not** an error — the item is already gone, which is the state this asks for.
386    ///
387    /// # Errors
388    ///
389    /// Returns [`SourceError::Refused`] when this source has no write side, and whatever
390    /// else the source could not remove the item for.
391    async fn delete_task(&self, id: &NativeId) -> Result<(), SourceError> {
392        let _ = id;
393        Err(unwritable(self.kind()))
394    }
395
396    /// Remove one project this destination holds, on exactly the terms of
397    /// [`delete_task`](Self::delete_task).
398    ///
399    /// # Errors
400    ///
401    /// As [`delete_task`](Self::delete_task).
402    async fn delete_project(&self, id: &NativeId) -> Result<(), SourceError> {
403        let _ = id;
404        Err(unwritable(self.kind()))
405    }
406
407    /// Fetch one document by its native id, or `None` when there is no such document.
408    ///
409    /// Defaulted to [`documentless`], which is what keeps documents an addition rather
410    /// than a break: a source with none needs no edit, keeps working, and says so in the
411    /// same words every other document-free source does. A source that has documents
412    /// declares [`Support::Native`](crate::Support::Native) for
413    /// [`Capabilities::documents`] and owes a real implementation here, because that
414    /// declaration is what makes the engine ask.
415    ///
416    /// # Errors
417    ///
418    /// Returns [`SourceError::Refused`] when this source has no documents, and whatever
419    /// else the source could not answer for.
420    async fn get_document(&self, id: &NativeId) -> Result<Option<Document>, SourceError> {
421        let _ = id;
422        Err(documentless(self.kind()))
423    }
424
425    /// One page of the documents matching `query`.
426    ///
427    /// Defaulted on exactly the terms of [`get_document`](Self::get_document). A source
428    /// with no documents refuses rather than answering an empty page: an empty page reads
429    /// as a source that has documents and holds none matching, which is the one wrong
430    /// answer this method can give.
431    ///
432    /// # Errors
433    ///
434    /// As [`get_document`](Self::get_document).
435    async fn query_documents(
436        &self,
437        query: &DocumentQuery,
438        page: &PageRequest,
439    ) -> Result<Page<Document>, SourceError> {
440        let _ = (query, page);
441        Err(documentless(self.kind()))
442    }
443
444    /// Create or update one document, on exactly the terms of
445    /// [`write_task`](Self::write_task).
446    ///
447    /// # Errors
448    ///
449    /// As [`write_task`](Self::write_task).
450    async fn write_document(&self, write: &ItemWrite<Document>) -> Result<NativeId, SourceError> {
451        let _ = write;
452        Err(unwritable(self.kind()))
453    }
454
455    /// Remove one document this destination holds, on exactly the terms of
456    /// [`delete_task`](Self::delete_task).
457    ///
458    /// # Errors
459    ///
460    /// As [`delete_task`](Self::delete_task).
461    async fn delete_document(&self, id: &NativeId) -> Result<(), SourceError> {
462        let _ = id;
463        Err(unwritable(self.kind()))
464    }
465
466    /// One page of the comments on `task`, oldest first, or `None` when this source holds
467    /// no such task.
468    ///
469    /// Defaulted to [`commentless`], which is what keeps comments an addition rather than a
470    /// break: a source with none needs no edit and keeps working. A source whose tasks have
471    /// comments declares [`Support::Native`](crate::Support::Native) for
472    /// [`Capabilities::comments`] and owes a real implementation of all four comment methods,
473    /// because that declaration is what makes the engine ask.
474    ///
475    /// "No such task" is `None` rather than an error, exactly as it is for
476    /// [`get_task`](Self::get_task); a task that exists and has no comments is an empty page.
477    ///
478    /// # Errors
479    ///
480    /// Returns [`SourceError::Refused`] when this source has no comments, and whatever else
481    /// the source could not answer for.
482    async fn task_comments(
483        &self,
484        task: &NativeId,
485        page: &PageRequest,
486    ) -> Result<Option<Page<Comment>>, SourceError> {
487        let _ = (task, page);
488        Err(commentless(self.kind()))
489    }
490
491    /// Add one comment to `task`, answering with the comment as the source now holds it, or
492    /// `None` when this source holds no such task.
493    ///
494    /// The body is stored byte for byte. A source that records the author itself refuses a
495    /// [`NewComment::author`] rather than dropping it, naming why; a source that cannot
496    /// represent the body refuses it, naming why, rather than escaping it into something
497    /// else.
498    ///
499    /// # Errors
500    ///
501    /// Returns [`SourceError::Refused`] when this source has no comments or cannot be
502    /// written, when it cannot record what it was given, and whatever else it could not do
503    /// the write for.
504    async fn add_comment(
505        &self,
506        task: &NativeId,
507        comment: &NewComment,
508    ) -> Result<Option<Comment>, SourceError> {
509        let _ = (task, comment);
510        Err(commentless(self.kind()))
511    }
512
513    /// Replace the body of the comment `comment` on `task`, answering with the comment as the
514    /// source now holds it, or `None` when this source holds no such task or that task has no
515    /// such comment.
516    ///
517    /// Only the body and the time it last changed move: the id, the author and the time it
518    /// was written are the comment's own.
519    ///
520    /// # Errors
521    ///
522    /// As [`add_comment`](Self::add_comment).
523    async fn edit_comment(
524        &self,
525        task: &NativeId,
526        comment: &NativeId,
527        body: &CommentBody,
528    ) -> Result<Option<Comment>, SourceError> {
529        let _ = (task, comment, body);
530        Err(commentless(self.kind()))
531    }
532
533    /// Remove the comment `comment` from `task`, answering with the id it removed, or `None`
534    /// when this source holds no such task or that task has no such comment.
535    ///
536    /// Unlike [`delete_task`](Self::delete_task), this *is* a verb of the product — a person
537    /// removes a comment they posted — so a comment that is not there is reported as `None`
538    /// for the engine to refuse by name, rather than treated as already gone.
539    ///
540    /// # Errors
541    ///
542    /// As [`add_comment`](Self::add_comment).
543    async fn delete_comment(
544        &self,
545        task: &NativeId,
546        comment: &NativeId,
547    ) -> Result<Option<NativeId>, SourceError> {
548        let _ = (task, comment);
549        Err(commentless(self.kind()))
550    }
551
552    /// Whether this source keeps template answers beside its items at all.
553    ///
554    /// What lets a regenerate tell an item whose answers went missing — a block deleted by
555    /// hand, which the regenerate writes back — from an item of a source that never keeps any,
556    /// where a missing answer is no difference. Defaulted to `false`, as
557    /// [`task_template_answers`](Self::task_template_answers) is defaulted to `None`; a source
558    /// that keeps answers answers `true` here.
559    fn keeps_template_answers(&self) -> bool {
560        false
561    }
562
563    /// The template answers the task `id` was last rendered from, as this source keeps them
564    /// beside the task — or `None` when it keeps none for it, or holds no such task.
565    ///
566    /// Keeping answers is a source's own choice, never an obligation: a source whose items
567    /// are one record in a hosted system has no room beside the item that is not the item, and
568    /// answers written into its content or its metadata would duplicate what the content
569    /// already says. Defaulted to `None`, which is what keeps this an addition rather than a
570    /// break. A source that keeps them owes three things: they are in neither
571    /// [`Task::content`] nor [`Task::metadata`], they are written only by
572    /// [`write_task_rendered`](Self::write_task_rendered) and
573    /// [`set_task_rendering`](Self::set_task_rendering), and they come back here exactly as
574    /// they were written, JSON types intact.
575    ///
576    /// # Errors
577    ///
578    /// Returns a [`SourceError`] when the source could not answer, a record whose answers it
579    /// cannot read included.
580    async fn task_template_answers(
581        &self,
582        id: &NativeId,
583    ) -> Result<Option<BTreeMap<String, Value>>, SourceError> {
584        let _ = id;
585        Ok(None)
586    }
587
588    /// The template answers the document `id` was last rendered from, on exactly the terms of
589    /// [`task_template_answers`](Self::task_template_answers).
590    ///
591    /// # Errors
592    ///
593    /// As [`task_template_answers`](Self::task_template_answers).
594    async fn document_template_answers(
595        &self,
596        id: &NativeId,
597    ) -> Result<Option<BTreeMap<String, Value>>, SourceError> {
598        let _ = id;
599        Ok(None)
600    }
601
602    /// Create or update one task exactly as [`write_task`](Self::write_task) does, keeping
603    /// `answers` — the answers its content was rendered from — beside it in the same write
604    /// where this source keeps answers at all.
605    ///
606    /// The task arrives carrying its provenance under [`MetadataKey::TEMPLATE_KEY`] like any
607    /// other metadata entry. Defaulted to [`write_task`](Self::write_task) alone: a source that
608    /// keeps no answers writes the task and nothing beside it, which is the whole of what it
609    /// owes. A source that keeps them writes the task and the answers together, so a reader
610    /// never finds one without the other.
611    ///
612    /// # Errors
613    ///
614    /// As [`write_task`](Self::write_task).
615    async fn write_task_rendered(
616        &self,
617        write: &ItemWrite<Task>,
618        answers: &BTreeMap<String, Value>,
619    ) -> Result<NativeId, SourceError> {
620        let _ = answers;
621        self.write_task(write).await
622    }
623
624    /// Create or update one document, on exactly the terms of
625    /// [`write_task_rendered`](Self::write_task_rendered).
626    ///
627    /// # Errors
628    ///
629    /// As [`write_document`](Self::write_document).
630    async fn write_document_rendered(
631        &self,
632        write: &ItemWrite<Document>,
633        answers: &BTreeMap<String, Value>,
634    ) -> Result<NativeId, SourceError> {
635        let _ = answers;
636        self.write_document(write).await
637    }
638
639    /// Replace one task's rendering — its content, byte for byte, its
640    /// [`MetadataKey::TEMPLATE_KEY`] entry, set to `provenance`, and the answers it keeps
641    /// beside the task where it keeps any — in one write, and change nothing else about it;
642    /// or answer `None` when this source holds no such task.
643    ///
644    /// The content is [`Task::content`] exactly as a later read reports it, on the terms of
645    /// [`set_task_content`](Self::set_task_content). Title, status, priority, labels, every
646    /// other metadata entry, repositories, dependencies, [`Task::delivers`],
647    /// [`Task::delivered_by`], project and comments are left exactly as they are. The write is
648    /// one write: a reader sees the task as it was or as it is now, never a new content beside
649    /// the old provenance or the old answers.
650    ///
651    /// Defaulted to [`unwritable_field`] on exactly the terms of
652    /// [`set_task_status`](Self::set_task_status). A source declaring
653    /// [`WriteSupport::Unsupported`] is never asked.
654    ///
655    /// [`Task::delivers`]: crate::Task::delivers
656    /// [`Task::delivered_by`]: crate::Task::delivered_by
657    ///
658    /// # Errors
659    ///
660    /// Returns [`SourceError::Refused`] when this source cannot replace a task's rendering on
661    /// its own, or cannot represent this one; and whatever else the source could not do the
662    /// write for.
663    async fn set_task_rendering(
664        &self,
665        id: &NativeId,
666        content: &str,
667        provenance: &Value,
668        answers: &BTreeMap<String, Value>,
669    ) -> Result<Option<()>, SourceError> {
670        let _ = (id, content, provenance, answers);
671        Err(unwritable_field(self.kind(), "rendering"))
672    }
673
674    /// Replace one document's rendering, on exactly the terms of
675    /// [`set_task_rendering`](Self::set_task_rendering). A source declaring
676    /// [`Capabilities::documents`] unsupported is never asked.
677    ///
678    /// # Errors
679    ///
680    /// As [`set_task_rendering`](Self::set_task_rendering).
681    async fn set_document_rendering(
682        &self,
683        id: &NativeId,
684        content: &str,
685        provenance: &Value,
686        answers: &BTreeMap<String, Value>,
687    ) -> Result<Option<()>, SourceError> {
688        let _ = (id, content, provenance, answers);
689        Err(SourceError::Refused {
690            message: format!(
691                "the {} plugin cannot write a document's rendering on its own",
692                self.kind()
693            ),
694        })
695    }
696
697    /// What this source has sent to its backend since it was built and what that spent, or
698    /// `None` when it does not meter its own requests.
699    ///
700    /// Defaulted to `None`, which is what keeps metering an addition rather than a break: a
701    /// source that does not count its requests needs no edit, and is reported as not
702    /// metering rather than as having spent nothing. A source that answers owes a running
703    /// total — see [`Metering`] — because what one command spent is read as the difference
704    /// between two readings.
705    ///
706    /// # Errors
707    ///
708    /// Returns a [`SourceError`] when the reading itself could not be taken. A caller
709    /// reports such a source as not metering; what a command cost is never a reason for the
710    /// command to fail.
711    async fn metering(&self) -> Result<Option<Metering>, SourceError> {
712        Ok(None)
713    }
714}
715
716/// The factory that turns one configuration block into a live [`TaskSource`].
717///
718/// Having the compile-time registry and the subprocess seam be the same shape is
719/// the whole reason this is a trait rather than a free function.
720pub trait SourcePlugin: Send + Sync + 'static {
721    /// The name a configuration document's `plugin:` field names.
722    fn kind(&self) -> &'static str;
723
724    /// The JSON Schema for this plugin's own `config:` block.
725    fn config_schema(&self) -> Schema;
726
727    /// Build a live source from one configuration block.
728    ///
729    /// `name` is the configured source's name, for error messages only — a
730    /// plugin never learns it for any other purpose.
731    ///
732    /// # Errors
733    ///
734    /// Returns [`SourceError::Config`] when `config` is not valid for this
735    /// plugin, or [`SourceError::Auth`] when a named credential is absent.
736    fn build(
737        &self,
738        name: &SourceName,
739        config: &serde_json::Value,
740        secrets: &dyn SecretResolver,
741    ) -> Result<Box<dyn TaskSource>, SourceError>;
742
743    /// The fields of this plugin's `config:` block that name a filesystem path, as dotted
744    /// paths into that block.
745    ///
746    /// A relative value at one of these, **supplied by a configuration document**, is
747    /// resolved against the directory holding that document before [`Self::build`] sees it;
748    /// supplied through the environment or a flag it keeps resolving against the process
749    /// working directory, because there is no document to rebase it on. A plugin is handed
750    /// values and no origins, so this declaration is the only way it can say which of its
751    /// own fields that rule reaches.
752    ///
753    /// Defaulted to none, which is what keeps this an addition rather than a break: a
754    /// plugin whose block holds no path needs no edit, and a caller asks every plugin
755    /// rather than keeping a table of which ones answer.
756    // llmlint: ignore[invalid_states_unrepresentable] The identity of a configuration field
757    // is a name, and no type can make a wrong one unrepresentable here: every string is a
758    // syntactically valid dotted path, so a newtype would validate nothing and would only
759    // move where a name that is not a field of *this* plugin is accepted. What decides that
760    // is whether the name is a property of the schema `config_schema` publishes — a
761    // per-plugin fact no shared type can hold — so the gate is per plugin and executable:
762    // `document_relative_fields_are_fields_this_plugin_declares` in
763    // `onetaskgraph-local-md/tests/plugin.rs`, which a plugin adding a declaration owes its
764    // own copy of.
765    fn document_relative_paths(&self) -> &'static [&'static str] {
766        &[]
767    }
768}
769
770/// How a plugin reads the credential its configuration names.
771///
772/// A configuration document never carries a credential value, only the name of
773/// the environment variable holding it.
774pub trait SecretResolver: Send + Sync {
775    /// The value of `var`, or `None` when nothing defines it.
776    ///
777    /// The returned value is never logged and never appears in `Debug` output.
778    fn get(&self, var: &str) -> Option<SecretString>;
779}