Skip to main content

ag_session/
task.rs

1//! Desired outcomes and explicit, single-turn attempts to advance them.
2
3use crate::{DurableMessageVisibility, EnvId, SessionId};
4
5/// A desired outcome to persist without admitting an attempt or starting work.
6///
7/// Repeating the same identity, session, and objective is idempotent, even
8/// after completion. The owning session and objective cannot be changed.
9#[derive(Clone, Debug, Eq, PartialEq)]
10pub struct CreateTaskRequest {
11    /// Nonempty desired outcome, immutable across attempts.
12    pub objective: String,
13    /// Globally unique, nonempty identity of the desired outcome.
14    pub task_id: String,
15}
16
17/// An explicit attempt to advance a task in an existing idle session.
18///
19/// Re-deliver the identical request with the same `attempt_id` to acknowledge
20/// an existing attempt, even after failure. Use a new ID to retry or continue
21/// after clarification. Provider retries stay inside the same attempt.
22#[derive(Clone, Debug, Eq, PartialEq)]
23pub struct SubmitTaskRequest {
24    /// Globally unique delivery identity for this attempt.
25    pub attempt_id: String,
26    /// Prompt for this attempt, including any clarification or retry context.
27    pub message: String,
28    /// Desired outcome, immutable across attempts of the same task.
29    pub objective: String,
30    /// Stable identity of the desired outcome.
31    pub task_id: String,
32    /// Whether the attempt prompt appears in the human transcript.
33    pub visibility: DurableMessageVisibility,
34}
35
36impl SubmitTaskRequest {
37    /// Operation identity reserved for this attempt's ordered workflow.
38    pub fn operation_id(&self) -> String {
39        format!("task-attempt:{}", self.attempt_id)
40    }
41}
42
43/// A desired outcome with retained attempt history, scoped to one session.
44///
45/// Successful execution does not establish that the objective is satisfied.
46/// Only explicit completion sets `completed_at`. Clarification, cancellation,
47/// and failed or interrupted attempts leave the task open.
48#[derive(Clone, Debug, Eq, PartialEq)]
49pub struct Task {
50    /// Attempts in admission order; execution status comes from operations.
51    pub attempts: Vec<TaskAttempt>,
52    /// Time the caller explicitly accepted the outcome, if complete.
53    pub completed_at: Option<i64>,
54    /// Time the task was created, possibly before its first attempt.
55    pub created_at: i64,
56    /// Stable task identity.
57    pub id: String,
58    /// Desired outcome, separate from an individual attempt's prompt.
59    pub objective: String,
60    /// Conversation and workspace used by every attempt.
61    pub session_id: SessionId,
62}
63
64impl Task {
65    /// Returns the shared environment used by this task and all its attempts.
66    ///
67    /// The immutable, persisted session ownership is the environment binding;
68    /// no second record or independently mutable workspace assignment exists.
69    pub fn env_id(&self) -> EnvId {
70        EnvId::SessionWorktree(self.session_id.clone())
71    }
72}
73
74/// One turn and its ordered post-processing, backed by a unique operation.
75///
76/// A clarification response finishes the attempt normally and leaves the
77/// session waiting for input. It does not keep a worker slot occupied.
78#[derive(Clone, Debug, Eq, PartialEq)]
79pub struct TaskAttempt {
80    /// Time execution and post-processing settled, if terminal.
81    pub finished_at: Option<i64>,
82    /// Stable attempt delivery identity.
83    pub id: String,
84    /// Execution or recovery failure, if any.
85    pub last_error: Option<String>,
86    /// Prompt admitted for this attempt.
87    pub message: String,
88    /// Unique operation; never reused for another attempt.
89    pub operation_id: String,
90    /// Time the attempt was durably admitted.
91    pub queued_at: i64,
92    /// Time the worker started execution, if started.
93    pub started_at: Option<i64>,
94    /// Execution state projected from the operation, not task completion.
95    pub status: TaskAttemptStatus,
96    /// Transcript presentation captured at admission.
97    pub visibility: DurableMessageVisibility,
98}
99
100/// Operation lifecycle exposed by the task read model.
101#[derive(Clone, Copy, Debug, Eq, PartialEq)]
102pub enum TaskAttemptStatus {
103    /// Durably admitted, awaiting worker execution.
104    Queued,
105    /// Executing a turn or its ordered post-processing.
106    Running,
107    /// Workflow finished successfully, possibly requesting clarification.
108    Succeeded,
109    /// Workflow failed, including interruption by application restart.
110    Failed,
111    /// Workflow was canceled before or during execution.
112    Canceled,
113}
114
115#[cfg(test)]
116#[path = "task_test.rs"]
117mod tests;