ag_session/task_chain.rs
1//! A static Planner, explicit acceptance, and Implementer workflow.
2
3use ag_protocol::TurnPromptAttachment;
4
5use crate::{
6 AcceptedTaskPlan, AgentSelection, BuiltinAgentDefinition, DurableMessageVisibility, SessionId,
7 SubmitAgentTaskRequest, TaskAgentAssignment, TaskAgentInput,
8};
9
10/// Idempotent launch input captured before the first Planner attempt.
11#[derive(Clone, Debug, Eq, PartialEq)]
12pub struct StartPlanThenImplementRequest {
13 /// Provider/model captured for both roles, independent of later settings.
14 pub agent: AgentSelection,
15 /// Images accompanying the original request.
16 pub attachments: Vec<TurnPromptAttachment>,
17 /// Globally unique workflow identity, retained for launch redelivery.
18 pub chain_id: String,
19 /// Globally unique initial Planner delivery identity.
20 pub planner_attempt_id: String,
21 /// Immutable original objective shared by both isolated task contexts.
22 pub user_request: String,
23}
24
25impl StartPlanThenImplementRequest {
26 /// Builds the exact first role request without consulting session state.
27 pub fn planner_request(&self) -> SubmitAgentTaskRequest {
28 SubmitAgentTaskRequest {
29 assignment: TaskAgentAssignment {
30 agent: self.agent,
31 definition: BuiltinAgentDefinition::Planner,
32 },
33 attempt_id: self.planner_attempt_id.clone(),
34 input: TaskAgentInput {
35 accepted_plan: None,
36 attachments: self.attachments.clone(),
37 clarification: None,
38 user_request: self.user_request.clone(),
39 },
40 objective: self.user_request.clone(),
41 task_id: format!("chain:{}:planner", self.chain_id),
42 visibility: DurableMessageVisibility::Hidden,
43 }
44 }
45
46 /// Identifies the separate Implementer task reserved by this workflow.
47 pub fn implementer_task_id(&self) -> String {
48 format!("chain:{}:implementer", self.chain_id)
49 }
50}
51
52/// Explicit acceptance and initial implementation admission in one action.
53#[derive(Clone, Debug, Eq, PartialEq)]
54pub struct AcceptTaskChainPlanRequest {
55 /// New delivery identity for the first Implementer attempt.
56 pub attempt_id: String,
57 /// Exact latest Planner version selected by the user.
58 pub plan_attempt_id: String,
59}
60
61/// Explicit revision, clarification, or retry of the current workflow stage.
62#[derive(Clone, Debug, Eq, PartialEq)]
63pub struct ContinueTaskChainRequest {
64 /// Images accompanying this clarification, retained by later attempts.
65 pub attachments: Vec<TurnPromptAttachment>,
66 /// New delivery identity; old deliveries never restart or uncancel work.
67 pub attempt_id: String,
68 /// User guidance addressed only to the current role's own context.
69 pub clarification: Option<String>,
70 /// Current attempt observed when this guidance was authored. Admission
71 /// rejects a new delivery if the workflow has advanced since that snapshot.
72 /// Identical redelivery retains its original anchor after later progress.
73 pub expected_attempt_id: String,
74}
75
76/// Natural-language user reply addressed to a session's latest workflow.
77///
78/// The host decides what the reply means: plan approval, plan revision, or
79/// guidance for the current role. No words are reserved.
80#[derive(Clone, Debug, Eq, PartialEq)]
81pub struct ReplyToTaskChainRequest {
82 /// Images accompanying the visible reply.
83 pub attachments: Vec<TurnPromptAttachment>,
84 /// User-authored reply, recorded once as a visible session prompt.
85 pub message: String,
86 /// Globally unique delivery identity. Redelivery returns the original
87 /// outcome without recording, deciding, or admitting work again.
88 pub reply_id: String,
89}
90
91/// Workflow action admitted for one natural-language reply.
92#[derive(Clone, Copy, Debug, Eq, PartialEq)]
93pub enum TaskChainReplyOutcome {
94 /// The reply clearly approved the plan; the Implementer was admitted.
95 PlanAccepted,
96 /// The reply was delivered to the Planner as plan revision guidance.
97 PlanRevised,
98 /// The reply was delivered to the current role as an answer or retry.
99 Continued,
100}
101
102impl TaskChainReplyOutcome {
103 /// Every outcome, in the order hosts look up delivery receipts.
104 pub const ALL: [Self; 3] = [Self::PlanAccepted, Self::PlanRevised, Self::Continued];
105
106 /// Derives the attempt identity admitted when one reply has this outcome.
107 pub fn attempt_id(self, reply_id: &str) -> String {
108 let action = match self {
109 Self::PlanAccepted => "implement",
110 Self::PlanRevised => "revise",
111 Self::Continued => "continue",
112 };
113
114 format!("reply:{reply_id}:{action}")
115 }
116}
117
118/// Consistent durable workflow snapshot; reading never dispatches work.
119#[derive(Clone, Debug, Eq, PartialEq)]
120pub struct TaskChain {
121 /// Exact immutable accepted version, present after explicit handoff.
122 pub accepted_plan: Option<AcceptedTaskPlan>,
123 /// Shared captured provider/model selection.
124 pub agent: AgentSelection,
125 /// Exact latest admitted request for the current stage, including IDs.
126 pub current_request: SubmitAgentTaskRequest,
127 /// Stable workflow identity.
128 pub id: String,
129 /// Separate Implementer task, reserved at launch.
130 pub implementer_task_id: String,
131 /// Projection from durable stage selection and operation-backed attempts.
132 pub phase: TaskChainPhase,
133 /// Separate Planner task, retained after implementation begins.
134 pub planner_task_id: String,
135 /// Session owning the shared workspace and serialized worker.
136 pub session_id: SessionId,
137 /// Immutable objective captured at launch.
138 pub user_request: String,
139}
140
141impl TaskChain {
142 /// Builds a new delivery to the current role while retaining its inputs.
143 pub fn continuation_request(
144 &self,
145 request: &ContinueTaskChainRequest,
146 ) -> SubmitAgentTaskRequest {
147 SubmitAgentTaskRequest {
148 attempt_id: request.attempt_id.clone(),
149 input: TaskAgentInput {
150 attachments: self
151 .current_request
152 .input
153 .attachments
154 .iter()
155 .chain(&request.attachments)
156 .cloned()
157 .collect(),
158 clarification: request.clarification.clone(),
159 ..self.current_request.input.clone()
160 },
161 ..self.current_request.clone()
162 }
163 }
164}
165
166/// Workflow presentation derived from the current attempt and saved response.
167/// Success records execution, never implicit acceptance of a plan or outcome.
168#[derive(Clone, Copy, Debug, Eq, PartialEq)]
169pub enum TaskChainPhase {
170 /// Planner attempt is queued or executing, including post-processing.
171 Planning,
172 /// Latest successful Planner response needs explicit user acceptance.
173 AwaitingPlanAcceptance,
174 /// Planner has questions that need an explicit continuation.
175 PlannerQuestion,
176 /// Accepted plan is being implemented, including post-processing.
177 Implementing,
178 /// Implementer has questions that need an explicit continuation.
179 ImplementerQuestion,
180 /// Current stage failed or was interrupted; retry is always explicit.
181 Failed,
182 /// Cancellation requested; an explicit new attempt may retry the stage.
183 Canceled,
184 /// Implementer finished successfully; the session remains reviewable.
185 Succeeded,
186}
187
188#[cfg(test)]
189#[path = "task_chain_test.rs"]
190mod tests;