Skip to main content

WorkflowState

Trait WorkflowState 

Source
pub trait WorkflowState:
    Clone
    + Send
    + Sync {
    type Checkpoint: Serialize + DeserializeOwned + Clone + Send;
    type Mutation: StateMutation<Self>;

    // Required methods
    fn snapshot(&self) -> Self::Checkpoint;
    fn restore(checkpoint: Self::Checkpoint) -> Self;

    // Provided methods
    fn apply_batch(
        &mut self,
        mutations: impl IntoIterator<Item = Self::Mutation>,
    ) { ... }
    fn initial() -> Self
       where Self: Default { ... }
}
Expand description

工作流状态 — 编译期类型安全的状态容器。

替代 HashMap<String, Value> 动态模型。 每个工作流定义自己的 State struct 和 Mutation enum, 实现此 trait 以声明关联类型。

State 只是数据。 状态变更逻辑在 StateMutation trait 中。 Merge 职责已从 WorkflowState 剥离到 MergeStrategy Checkpoint 采用 Projection 模式 — Runtime State 可包含不可序列化字段 (如 Arc<dyn ...>, Sender, Cache),Checkpoint 只序列化必要字段。

§示例

// State 只是数据
pub struct AgentState {
    pub messages: Vec<Message>,
    pub iterations: usize,
    pub output_tokens: usize,
    pub cache: Arc<dyn ToolCatalog>,  // 不可序列化
}

// 可序列化的 Checkpoint 投影
#[derive(Serialize, Deserialize)]
pub struct AgentCheckpoint {
    pub messages: Vec<Message>,
    pub iterations: usize,
    pub output_tokens: usize,
    // 不包含 cache
}

// Mutation 自己知道怎么改 State
pub enum AgentMutation {
    AppendMessage(Message),
    IncrementIteration,
    RecordOutputTokens(usize),
}

impl StateMutation<AgentState> for AgentMutation {
    fn apply(self, state: &mut AgentState) {
        match self {
            AgentMutation::AppendMessage(msg) => state.messages.push(msg),
            AgentMutation::IncrementIteration => state.iterations += 1,
            AgentMutation::RecordOutputTokens(n) => state.output_tokens += n,
        }
    }
}

// WorkflowState 声明 Checkpoint 和 Mutation 关联类型
impl WorkflowState for AgentState {
    type Checkpoint = AgentCheckpoint;
    type Mutation = AgentMutation;

    fn snapshot(&self) -> AgentCheckpoint {
        AgentCheckpoint {
            messages: self.messages.clone(),
            iterations: self.iterations,
            output_tokens: self.output_tokens,
        }
    }

    fn restore(checkpoint: AgentCheckpoint) -> Self {
        AgentState {
            messages: checkpoint.messages,
            iterations: checkpoint.iterations,
            output_tokens: checkpoint.output_tokens,
            cache: Arc::new(ToolCatalog::default()),  // 重建
        }
    }
}

Required Associated Types§

Source

type Checkpoint: Serialize + DeserializeOwned + Clone + Send

可序列化的 Checkpoint 快照(projection,不是 raw state)。

Runtime State 可以包含不可序列化字段(Arc<dyn ...>, Sender, Cache), Checkpoint 只序列化必要字段。这是强制的 Projection 模式。

Source

type Mutation: StateMutation<Self>

与此状态关联的 Mutation 类型。

Required Methods§

Source

fn snapshot(&self) -> Self::Checkpoint

创建 checkpoint 快照 — 只序列化必要字段。

这是 Projection 的核心:开发者必须决定哪些字段需要持久化。 编译期保证 Checkpoint 可序列化。

Source

fn restore(checkpoint: Self::Checkpoint) -> Self

从 checkpoint 恢复运行时状态。

恢复时明确哪些字段从 checkpoint 加载,哪些需要重建。

Provided Methods§

Source

fn apply_batch(&mut self, mutations: impl IntoIterator<Item = Self::Mutation>)

批量应用 Mutation — 唯一公开入口。

默认实现:逐个调用 StateMutation::apply。 未来可覆盖为 Transaction 语义(begin → validate → apply → commit/rollback)。

Source

fn initial() -> Self
where Self: Default,

创建默认/初始状态。

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§