basis-tasks 0.7.1

The durable task layer over basis: a filesystem-coordinated lifecycle for spawned agents, reachable from Rust and not only from the CLI.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Durable per-agent state: metadata, terminal records, and their bounds.
//!
//! Three files carry an agent's task state, each atomic-replace JSON:
//! `meta.json` (bookkeeping and the recorded spawn request — never
//! conversation content, which is mentra's store's alone), `inbox.json`
//! (messages, see [`super::inbox`](mod@super::inbox)), and `terminal.json` — written as the
//! executor's **last** act, whose existence is the completion signal. An agent
//! is resumable iff its terminal record does not exist; every crash before
//! that write resolves toward resumable.

use std::{
    io,
    time::{SystemTime, UNIX_EPOCH},
};

use basis::{Bound, Effort, RunUsage, SystemPrompt};

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::approve::Approve;
use crate::data_dir::{AgentPaths, write_private_atomic};

pub(crate) const MAX_MESSAGES: usize = 16;
pub(crate) const MAX_MESSAGE: usize = 256 * 1024;
pub(crate) const MAX_PROMPT: usize = 256 * 1024;
pub(crate) const MAX_EVENT_BYTES: usize = 32 * 1024;
pub(crate) const MAX_RESULT_BYTES: usize = 1024 * 1024;
/// How many tasks one workspace may hold at once. `spawn` refuses past it
/// (archiving an old agent directory is the way out); the ownership-policy
/// walks in `policy` and `attach::cancel_tree` are bounded by it too, so
/// corrupt or cyclic metadata cannot loop forever.
pub const MAX_TASKS: usize = 1024;
/// Byte cap on one agent's `events.jsonl`, standing in for the daemon-era
/// journal cap: on overflow a final notice line is appended and recording
/// stops — a run is never failed for observability.
pub(crate) const MAX_EVENTS_BYTES: u64 = 32 * 1024 * 1024;

/// The recorded spawn request, so a later attach can execute what spawn
/// accepted. Credentials are intentionally absent: the attaching process owns
/// its own environment, exactly as any other host does.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub(crate) struct RunOptions {
    pub provider: Option<String>,
    pub base_url: Option<String>,
    pub model: Option<String>,
    pub no_shell: bool,
    /// The host's say over the system prompt, held as the type it is:
    /// `Replace` displaces the workspace's context, `Append` follows it.
    ///
    /// Written typed (`{"replace": …}` / `{"append": …}`). The pre-0.6
    /// record spelled it as two flat strings, and a task directory outlives
    /// the binary that minted it, so the compat reader below still accepts
    /// the flat `Replace` half and [`folded`](Self::folded) picks up the
    /// legacy append field on load.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "system_prompt_compat"
    )]
    pub system_prompt: Option<SystemPrompt>,
    /// The pre-0.6 append half, read-only: folded into
    /// [`system_prompt`](Self::system_prompt) on load and never written back.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub append_system_prompt: Option<String>,
    /// Typed, and byte-for-byte the strings the old writer spelled
    /// (`"low"` … `"max"`), so an old record needs no shim.
    pub effort: Option<Effort>,
    /// Typed, same story: `"always"` / `"never"` / `"prompt"` on the wire,
    /// unchanged — and an invalid mode is now unrepresentable, so corruption
    /// fails at decode, named, instead of somewhere downstream.
    pub approve: Approve,
    pub deadline_ms: Option<u64>,
    pub tool_budget: Option<usize>,
    pub token_budget: Option<u64>,
}

impl RunOptions {
    /// Folds the pre-0.6 two-string system-prompt spelling into the typed
    /// field. Called by [`load_meta`], so everything downstream sees one
    /// spelling however old the record is.
    fn folded(self) -> Self {
        let (system_prompt, append) = (
            self.system_prompt.clone(),
            self.append_system_prompt.clone(),
        );
        Self {
            system_prompt: system_prompt.or_else(|| append.map(SystemPrompt::Append)),
            append_system_prompt: None,
            ..self
        }
    }
}

/// Accepts the typed spelling and the pre-0.6 flat string, which was always
/// the `Replace` half — clap refused both flags at once before either was
/// recorded.
fn system_prompt_compat<'de, D>(deserializer: D) -> Result<Option<SystemPrompt>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum Compat {
        Typed(SystemPrompt),
        Legacy(String),
    }

    Ok(
        Option::<Compat>::deserialize(deserializer)?.map(|value| match value {
            Compat::Typed(system_prompt) => system_prompt,
            Compat::Legacy(replace) => SystemPrompt::Replace(replace),
        }),
    )
}

/// Accepts what the typed writer spells — the same `deadline` /
/// `tool_budget` / `token_budget` strings the old one used — and reads
/// anything else, like the `unknown` the old writer could emit for a bound it
/// had no name for, as no bound rather than an unloadable record.
fn bound_compat<'de, D>(deserializer: D) -> Result<Option<Bound>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let name = Option::<String>::deserialize(deserializer)?;
    Ok(name.and_then(|name| serde_json::from_value(Value::String(name)).ok()))
}

/// A task whose own work has finished, but whose attached children may still
/// lack terminal records. Recorded in `meta.json` before the settle pass, so a
/// kill between a child's terminal and the parent's leaves the parent
/// resumable with the model work already done.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "state", rename_all = "snake_case")]
pub(crate) enum PendingTerminal {
    Succeeded { result: String },
    Failed { error: String },
    Cancelled,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum MessageState {
    Pending,
    InFlight,
    Delivered,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct MessageReply {
    pub result: String,
    #[serde(default, skip_serializing_if = "is_false")]
    pub result_truncated: bool,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "bound_compat"
    )]
    pub stopped_by: Option<Bound>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct MessageRecord {
    pub id: String,
    pub body: String,
    pub state: MessageState,
    pub created_ms: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reply: Option<MessageReply>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) struct TaskMeta {
    pub id: String,
    pub parent: Option<String>,
    pub detached: bool,
    pub workspace: String,
    /// The mentra resume key; empty until the first attach prepares the run.
    pub agent_id: String,
    /// The conversation this task was told to pick up, when `spawn` was given
    /// `--continue` or `--session`. The first attach resumes it instead of
    /// minting one, which is what lets a new handle carry an old dialogue —
    /// and why continuing a settled task is a new task rather than a message
    /// to a closed inbox (ADR-0019).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub continues: Option<String>,
    /// How many assistant turns the conversation already held when this task
    /// first attached to it.
    ///
    /// Zero for a task that opened its own conversation. Nonzero only for a
    /// continued one, where the previous answers are on the transcript from
    /// the very first turn — and where the resume recovery in `attach` would
    /// otherwise read one of them as this task's own answer and settle
    /// `succeeded` without ever asking its prompt.
    #[serde(default)]
    pub answered_before: usize,
    pub prompt: String,
    pub options: RunOptions,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pending_terminal: Option<PendingTerminal>,
    #[serde(default, skip_serializing_if = "is_false")]
    pub result_truncated: bool,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "bound_compat"
    )]
    pub stopped_by: Option<Bound>,
    /// What every turn this task has driven reported spending, summed.
    ///
    /// Kept in `meta.json` rather than recomputed from the event journal
    /// because a task outlives the process that drove it: two attaches, each
    /// running turns, both add to one tally, and the journal may have been
    /// capped ([`MAX_EVENTS_BYTES`]) long before anyone asks.
    #[serde(default)]
    pub usage: RunUsage,
    pub deadline_at_ms: Option<u64>,
    pub created_ms: u64,
    /// When this task last advanced, as its executor records it: the attach
    /// that opened its conversation, every turn it banked, and the completion
    /// it settled on. Each write to this record is a step the task took, which
    /// is what lets one field carry both meanings — a bookkeeping rewrite that
    /// is *not* a step would have to say so rather than borrow this one.
    ///
    /// Read by [`super::tasks`](mod@super::tasks) to answer "the conversation
    /// I was just having", the question `--continue` asks. Defaulted for that
    /// reason: a record that omits it must still load, because a task that
    /// vanished from `basis list` after an upgrade would be a worse answer
    /// than a task ordered by the wrong clock. The reader falls back to
    /// `created_ms`.
    #[serde(default)]
    pub updated_ms: u64,
}

impl TaskMeta {
    pub(crate) fn new(
        id: String,
        parent: Option<String>,
        detached: bool,
        workspace: String,
        prompt: String,
        options: RunOptions,
        deadline_at_ms: Option<u64>,
    ) -> Self {
        let now = now_ms();
        Self {
            id,
            parent,
            detached,
            workspace,
            agent_id: String::new(),
            continues: None,
            answered_before: 0,
            prompt,
            options,
            pending_terminal: None,
            result_truncated: false,
            stopped_by: None,
            usage: RunUsage::default(),
            deadline_at_ms,
            created_ms: now,
            updated_ms: now,
        }
    }

    /// Records the conversation this task continues, when it continues one.
    ///
    /// A field rather than a pre-set `agent_id`, because the two mean
    /// different things to the executor: `agent_id` says *this task has
    /// attached before*, and taking a continued conversation for a resumed
    /// one is how a task settles on someone else's answer.
    #[must_use]
    pub(crate) fn continuing(self, agent_id: Option<String>) -> Self {
        Self {
            continues: agent_id,
            ..self
        }
    }

    pub(crate) fn deadline_passed(&self) -> bool {
        self.deadline_at_ms
            .is_some_and(|deadline| deadline <= now_ms())
    }

    /// The terminal payload the recorded completion earns: exactly the shape
    /// the daemon's journal produced, minted once into `terminal.json`.
    pub(crate) fn terminal_payload(&self) -> Option<Value> {
        let payload = match self.pending_terminal.as_ref()? {
            PendingTerminal::Succeeded { result } => {
                let mut terminal = serde_json::json!({"state": "succeeded", "result": result});
                if self.result_truncated {
                    terminal["result_truncated"] = Value::Bool(true);
                }
                with_stopped_by(terminal, self.stopped_by)
            }
            PendingTerminal::Failed { error } => with_stopped_by(
                serde_json::json!({"state": "failed", "error": error}),
                self.stopped_by,
            ),
            PendingTerminal::Cancelled => serde_json::json!({"state": "cancelled"}),
        };
        Some(with_usage(payload, self.usage))
    }
}

fn with_stopped_by(mut payload: Value, stopped_by: Option<Bound>) -> Value {
    if let Some(stopped_by) = stopped_by {
        payload["stopped_by"] = serde_json::json!(stopped_by);
    }
    payload
}

/// Adds what the task spent, when it spent anything.
///
/// A record that names no usage is a task whose turns reported none — a
/// cancellation that never reached the model is the ordinary case — and a
/// `usage` object full of zeros would claim a measurement nobody made. The
/// same rule the finish line follows (`Event::RunFinished::usage`), so the
/// stream and the record cannot disagree.
fn with_usage(mut payload: Value, usage: RunUsage) -> Value {
    if usage != RunUsage::default() {
        payload["usage"] = serde_json::json!(usage);
    }
    payload
}

fn is_false(value: &bool) -> bool {
    !*value
}

pub(crate) fn load_meta(paths: &AgentPaths) -> Result<TaskMeta, String> {
    let bytes = std::fs::read(paths.meta())
        .map_err(|error| format!("read task metadata for {}: {error}", paths.dir().display()))?;
    let meta: TaskMeta = serde_json::from_slice(&bytes).map_err(|error| {
        format!(
            "decode task metadata for {}: {error}",
            paths.dir().display()
        )
    })?;

    // One spelling downstream, however old the record: the pre-0.6 system
    // prompt strings fold into the typed field on the way in.
    Ok(TaskMeta {
        options: meta.options.folded(),
        ..meta
    })
}

pub(crate) fn save_meta(paths: &AgentPaths, meta: &TaskMeta) -> Result<(), String> {
    let bytes =
        serde_json::to_vec(meta).map_err(|error| format!("encode task metadata: {error}"))?;
    write_private_atomic(&paths.meta(), &bytes)
        .map_err(|error| format!("persist task metadata: {error}"))
}

/// Reads the terminal record. `None` is the resumable state.
pub(crate) fn read_terminal(paths: &AgentPaths) -> Result<Option<Value>, String> {
    match std::fs::read(paths.terminal()) {
        Ok(bytes) => serde_json::from_slice(&bytes)
            .map(Some)
            .map_err(|error| format!("decode terminal record: {error}")),
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(None),
        Err(error) => Err(format!("read terminal record: {error}")),
    }
}

pub(crate) fn write_terminal(paths: &AgentPaths, payload: &Value) -> Result<(), String> {
    let bytes =
        serde_json::to_vec(payload).map_err(|error| format!("encode terminal record: {error}"))?;
    write_private_atomic(&paths.terminal(), &bytes)
        .map_err(|error| format!("persist terminal record: {error}"))
}

pub(crate) fn cancel_requested(paths: &AgentPaths) -> bool {
    paths.cancel_marker().exists()
}

/// Writes the cancel marker. Existence is the signal; the content is
/// diagnostic only.
pub(crate) fn request_cancel(paths: &AgentPaths, by: Option<&str>) -> Result<(), String> {
    let content = serde_json::json!({"requested_ms": now_ms(), "by": by});
    write_private_atomic(&paths.cancel_marker(), content.to_string().as_bytes())
        .map_err(|error| format!("record cancel request: {error}"))
}

pub(crate) fn bounded_text(mut value: String, limit: usize) -> (String, bool) {
    if value.len() <= limit {
        return (value, false);
    }
    let mut end = limit;
    while !value.is_char_boundary(end) {
        end -= 1;
    }
    value.truncate(end);
    (value, true)
}

/// Milliseconds since the epoch, clamped rather than panicking on a clock
/// before it. The one clock every durable record here is stamped with —
/// `created_ms`, `updated_ms`, a cancel marker's `requested_ms`, a message's
/// `created_ms` — and what a host rendering one of those alongside its own
/// notion of "now" (`basis list`'s own age column, for one) should read
/// `now` from, so the two clocks cannot disagree about the epoch.
pub fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis()
        .min(u128::from(u64::MAX)) as u64
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data_dir::DataDir;

    fn agent(dir: &tempfile::TempDir) -> AgentPaths {
        let data = DataDir::from_path(dir.path()).unwrap();
        let paths = data
            .agent_dir("0123456789abcdef/0123456789abcdef0123456789abcdef")
            .unwrap();
        std::fs::create_dir_all(paths.dir()).unwrap();
        paths
    }

    fn meta(paths: &AgentPaths) -> TaskMeta {
        TaskMeta::new(
            "0123456789abcdef/0123456789abcdef0123456789abcdef".to_string(),
            None,
            true,
            "/repo".to_string(),
            "prompt".to_string(),
            RunOptions::default(),
            None,
        )
        .tap(paths)
    }

    trait Tap {
        fn tap(self, paths: &AgentPaths) -> Self;
    }
    impl Tap for TaskMeta {
        fn tap(self, paths: &AgentPaths) -> Self {
            save_meta(paths, &self).unwrap();
            self
        }
    }

    #[test]
    fn metadata_round_trips_through_its_file() {
        let dir = tempfile::tempdir().unwrap();
        let paths = agent(&dir);
        let saved = meta(&paths);
        assert_eq!(load_meta(&paths).unwrap(), saved);
    }

    #[test]
    fn terminal_records_are_absent_until_written_then_repeatably_observable() {
        let dir = tempfile::tempdir().unwrap();
        let paths = agent(&dir);
        assert!(read_terminal(&paths).unwrap().is_none());

        let mut record = meta(&paths);
        record.pending_terminal = Some(PendingTerminal::Succeeded {
            result: "done".to_string(),
        });
        let payload = record.terminal_payload().unwrap();
        write_terminal(&paths, &payload).unwrap();

        assert_eq!(read_terminal(&paths).unwrap().unwrap(), payload);
        assert_eq!(read_terminal(&paths).unwrap().unwrap(), payload);
        assert_eq!(
            payload,
            serde_json::json!({"state": "succeeded", "result": "done"})
        );
    }

    #[test]
    fn terminal_payloads_carry_truncation_and_bound_metadata() {
        let dir = tempfile::tempdir().unwrap();
        let paths = agent(&dir);
        let mut record = meta(&paths);
        record.pending_terminal = Some(PendingTerminal::Failed {
            error: "took too long".to_string(),
        });
        record.stopped_by = Some(Bound::Deadline);
        assert_eq!(
            record.terminal_payload().unwrap(),
            serde_json::json!({"state": "failed", "error": "took too long", "stopped_by": "deadline"})
        );

        record.pending_terminal = Some(PendingTerminal::Cancelled);
        assert_eq!(
            record.terminal_payload().unwrap(),
            serde_json::json!({"state": "cancelled"}),
            "a cancelled terminal never reports a bound"
        );
    }

    /// The terminal record is what `basis wait --json` and `basis list --json`
    /// read, and it is the only place a settled task's cost survives: the
    /// event journal can be capped, and the process that spent the tokens is
    /// gone.
    #[test]
    fn a_settled_task_records_what_it_spent_only_when_it_spent_something() {
        let dir = tempfile::tempdir().unwrap();
        let paths = agent(&dir);
        let mut record = meta(&paths);
        record.pending_terminal = Some(PendingTerminal::Succeeded {
            result: "done".to_string(),
        });

        assert_eq!(
            record.terminal_payload().unwrap(),
            serde_json::json!({"state": "succeeded", "result": "done"}),
            "a task whose turns reported nothing claims no measurement"
        );

        record.usage = RunUsage {
            input_tokens: 900,
            output_tokens: 100,
            cache_read_tokens: 7,
            cache_creation_tokens: 3,
            reasoning_tokens: 0,
            thoughts_tokens: 0,
        };
        let payload = record.terminal_payload().unwrap();
        assert_eq!(payload["usage"]["input_tokens"], 900);
        assert_eq!(payload["usage"]["output_tokens"], 100);
        assert_eq!(payload["usage"]["cache_read_tokens"], 7);
        assert_eq!(payload["usage"]["cache_creation_tokens"], 3);
    }

    #[test]
    fn cancel_markers_signal_by_existence() {
        let dir = tempfile::tempdir().unwrap();
        let paths = agent(&dir);
        assert!(!cancel_requested(&paths));
        request_cancel(&paths, Some("caller/id")).unwrap();
        assert!(cancel_requested(&paths));
    }

    #[test]
    fn bounded_text_never_splits_utf8() {
        let (value, truncated) = bounded_text("a界b".to_string(), 2);
        assert_eq!(value, "a");
        assert!(truncated);
    }
}