| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| use super::*; | |
| pub use maka_scheduler::{plan::Misfire as TaskMisfire, schedule::Schedule as TaskSchedule}; | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] | |
| #[serde( | |
| tag = "kind", | |
| rename_all = "snake_case", | |
| rename_all_fields = "camelCase", | |
| deny_unknown_fields | |
| )] | |
| #[schemars(rename = "AgentScheduledEffect")] | |
| pub enum ScheduledEffect { | |
| Session { | |
| #[serde(default)] | |
| delivery: AutomationDelivery, | |
| target: ScheduledTarget, | |
| model: ModelSelection, | |
| thinking_level: Option<maka_agent_facts::execution::ThinkingLevel>, | |
| }, | |
| ExecutorSession { | |
| target: ScheduledTarget, | |
| selection: maka_agent_facts::executor::Selection, | |
| }, | |
| AgentRun { | |
| target: ScheduledTarget, | |
| model: ModelSelection, | |
| thinking_level: Option<maka_agent_facts::execution::ThinkingLevel>, | |
| }, | |
| /// Investigation in a fresh worker. The target is the current Bot. | |
| Discovery { | |
| target: ScheduledTarget, | |
| model: ModelSelection, | |
| thinking_level: Option<maka_agent_facts::execution::ThinkingLevel>, | |
| /// Tool and permission profile requested for this plan. | |
| worker_tools: DiscoveryWorkerTools, | |
| /// Optional exact tool choices and the categories shown to the user. | |
| /// The profile and the current Session ceiling still apply. | |
| #[serde(default, skip_serializing_if = "Option::is_none")] | |
| selected_tools: Option<DiscoveryToolSelection>, | |
| }, | |
| } | |
| #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] | |
| #[serde(rename_all = "snake_case")] | |
| pub enum DiscoveryWorkerTools { | |
| #[default] | |
| ReadOnly, | |
| Session, | |
| } | |
| /// The category is derived from the bound service, even for renamed tools. | |
| #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] | |
| #[serde(rename_all = "snake_case")] | |
| pub enum DiscoveryToolGroup { | |
| ReadFiles, | |
| ModifyFiles, | |
| History, | |
| Commands, | |
| Web, | |
| Sessions, | |
| Desktop, | |
| PluginManagement, | |
| Other, | |
| } | |
| pub type DiscoveryToolSelection = BTreeMap<String, DiscoveryToolGroup>; | |
| impl ScheduledEffect { | |
| pub fn selected_discovery_tools(&self) -> Option<&DiscoveryToolSelection> { | |
| match self { | |
| Self::Discovery { selected_tools, .. } => selected_tools.as_ref(), | |
| _ => None, | |
| } | |
| } | |
| pub fn target(&self) -> &ScheduledTarget { | |
| match self { | |
| Self::Session { target, .. } | |
| | Self::ExecutorSession { target, .. } | |
| | Self::AgentRun { target, .. } | |
| | Self::Discovery { target, .. } => target, | |
| } | |
| } | |
| } | |
| impl maka_scheduler::task::TaskEffect for ScheduledEffect { | |
| fn validate(&self) -> Result<(), maka_scheduler::Error> { | |
| let target = self.target(); | |
| if target.session_id.is_empty() | |
| || target.boundary_revision > maka_agent_facts::interaction::MAX_SAFE_INTEGER | |
| || target.binding.validate().is_err() | |
| { | |
| return Err(maka_scheduler::Error::Invalid( | |
| "invalid scheduled execution target".into(), | |
| )); | |
| } | |
| if let Self::ExecutorSession { selection, .. } = self { | |
| selection | |
| .settings | |
| .validate() | |
| .map_err(|error| maka_scheduler::Error::Invalid(error.into()))?; | |
| } | |
| Ok(()) | |
| } | |
| fn allows_empty_intent(&self) -> bool { | |
| matches!(self, Self::Discovery { .. }) | |
| } | |
| } | |
| #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] | |
| #[serde(rename_all = "snake_case")] | |
| pub enum AutomationDelivery { | |
| #[default] | |
| Steering, | |
| Followup, | |
| } | |
| /// Exact observed source boundary. This data is checked against the original | |
| /// Session; passing it never grants execution by itself. | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] | |
| #[serde(rename_all = "camelCase", deny_unknown_fields)] | |
| #[schemars(rename = "AgentScheduledTarget")] | |
| pub struct ScheduledTarget { | |
| pub session_id: String, | |
| pub workspace: Workspace, | |
| pub permissions: WorkspacePermissions, | |
| pub boundary_revision: u64, | |
| pub binding: maka_sandbox::workspace::BindingStamp, | |
| } | |
| #[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)] | |
| #[serde(rename_all = "camelCase", deny_unknown_fields)] | |
| #[schemars(rename = "AgentScheduledInput")] | |
| pub struct ScheduledInput { | |
| pub task: maka_scheduler::task::Create<ScheduledEffect>, | |
| pub timezone: String, | |
| pub misfire: TaskMisfire, | |
| /// Tool names presented to the user when approving a discovery plan. | |
| #[serde(skip_serializing_if = "Option::is_none")] | |
| pub approved_tools: Option<Vec<String>>, | |
| } | |
| #[derive(Clone, Deserialize, JsonSchema)] | |
| #[serde( | |
| tag = "kind", | |
| rename_all = "snake_case", | |
| rename_all_fields = "camelCase", | |
| deny_unknown_fields | |
| )] | |
| #[schemars(rename = "AgentScheduleRead")] | |
| pub enum ScheduleRead { | |
| PreviewDiscovery { | |
| effect: ScheduledEffect, | |
| task_id: Option<String>, | |
| }, | |
| PreviewSchedule { | |
| schedule: TaskSchedule, | |
| timezone: String, | |
| #[serde(default)] | |
| count: Option<u8>, | |
| }, | |
| SessionCount { | |
| session_id: String, | |
| }, | |
| Discovery { | |
| session_id: String, | |
| cursor: Option<String>, | |
| expected_revision: Option<u64>, | |
| }, | |
| List { | |
| cursor: Option<String>, | |
| expected_revision: Option<u64>, | |
| #[serde(default)] | |
| session_id: Option<String>, | |
| }, | |
| Get { | |
| task_id: String, | |
| }, | |
| } | |
| /// Compact schedule projection for list summaries (omits script source bodies). | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] | |
| #[serde( | |
| tag = "kind", | |
| rename_all = "snake_case", | |
| rename_all_fields = "camelCase", | |
| deny_unknown_fields | |
| )] | |
| #[schemars(rename = "AgentScheduleSummary")] | |
| pub enum ScheduleSummary { | |
| Once { | |
| run_at: i64, | |
| }, | |
| Interval { | |
| every_seconds: u32, | |
| cadence: maka_scheduler::schedule::IntervalCadence, | |
| }, | |
| Calendar { | |
| recurrence: maka_scheduler::schedule::Recurrence, | |
| anchor_at: i64, | |
| }, | |
| Cron { | |
| expression: String, | |
| }, | |
| Script { | |
| every_seconds: u32, | |
| }, | |
| } | |
| impl From<&TaskSchedule> for ScheduleSummary { | |
| fn from(value: &TaskSchedule) -> Self { | |
| match value { | |
| TaskSchedule::Once { run_at } => Self::Once { run_at: *run_at }, | |
| TaskSchedule::Interval { | |
| every_seconds, | |
| cadence, | |
| .. | |
| } => Self::Interval { | |
| every_seconds: *every_seconds, | |
| cadence: *cadence, | |
| }, | |
| TaskSchedule::Calendar { | |
| recurrence, | |
| anchor_at, | |
| } => Self::Calendar { | |
| recurrence: *recurrence, | |
| anchor_at: *anchor_at, | |
| }, | |
| TaskSchedule::Cron { expression, .. } => Self::Cron { | |
| expression: expression.clone(), | |
| }, | |
| TaskSchedule::Script { every_seconds, .. } => Self::Script { | |
| every_seconds: *every_seconds, | |
| }, | |
| } | |
| } | |
| } | |
| #[derive(Clone, Serialize, Deserialize, JsonSchema)] | |
| #[serde(rename_all = "camelCase", deny_unknown_fields)] | |
| #[schemars(rename = "AgentScheduledSummary")] | |
| pub struct ScheduledSummary { | |
| pub id: String, | |
| pub revision: u64, | |
| pub title: String, | |
| pub session_id: String, | |
| #[serde(default, skip_serializing_if = "Option::is_none")] | |
| pub session_title: Option<String>, | |
| pub workspace_path: String, | |
| pub schedule: ScheduleSummary, | |
| pub timezone: String, | |
| pub starts_new_session: bool, | |
| pub script: bool, | |
| pub approved: bool, | |
| pub status: maka_scheduler::task::Status, | |
| /// A Bot-created one-time discovery that no longer needs approval. | |
| #[serde(default, skip_serializing_if = "std::ops::Not::not")] | |
| pub background_followup: bool, | |
| pub next_fire_at: Option<i64>, | |
| pub last_fire_at: Option<i64>, | |
| #[serde(default, skip_serializing_if = "Option::is_none")] | |
| pub last_outcome: Option<maka_scheduler::task::Outcome>, | |
| pub fire_count: u32, | |
| pub created_at: i64, | |
| pub updated_at: i64, | |
| pub pending_fire: Option<String>, | |
| pub last_error: Option<String>, | |
| } | |
| #[derive(Serialize, JsonSchema)] | |
| #[serde(rename_all = "camelCase", deny_unknown_fields)] | |
| #[schemars(rename = "AgentScheduledDetail")] | |
| pub struct ScheduledDetail { | |
| #[serde(skip_serializing_if = "Option::is_none")] | |
| pub investigation: Option<ScheduledInvestigation>, | |
| pub revision: u64, | |
| pub task: maka_scheduler::task::Task<ScheduledEffect>, | |
| pub timezone: String, | |
| pub misfire: TaskMisfire, | |
| pub approved: bool, | |
| pub pending_fire: Option<String>, | |
| } | |
| #[derive(Clone, Serialize, Deserialize, JsonSchema)] | |
| #[serde(rename_all = "camelCase", deny_unknown_fields)] | |
| #[schemars(rename = "AgentScheduledInvestigation")] | |
| pub struct ScheduledInvestigation { | |
| pub session_id: String, | |
| pub turn_id: String, | |
| pub phase: InvestigationPhase, | |
| } | |
| #[derive(Clone, Serialize, Deserialize, JsonSchema)] | |
| #[serde(rename_all = "snake_case")] | |
| #[schemars(rename = "AgentInvestigationPhase")] | |
| pub enum InvestigationPhase { | |
| Investigating, | |
| Delivering, | |
| Stopping, | |
| } | |
| #[derive(Serialize, JsonSchema)] | |
| #[serde( | |
| tag = "kind", | |
| rename_all = "snake_case", | |
| rename_all_fields = "camelCase", | |
| deny_unknown_fields | |
| )] | |
| #[schemars(rename = "AgentSchedules")] | |
| pub enum Schedules { | |
| DiscoveryTools { | |
| /// All names available in this profile, including the required return tool. | |
| names: Vec<String>, | |
| /// Selectable names classified by their current binding. | |
| groups: DiscoveryToolSelection, | |
| /// Previously approved names for this plan and profile, if any. | |
| #[serde(skip_serializing_if = "Option::is_none")] | |
| approved_names: Option<Vec<String>>, | |
| }, | |
| SchedulePreview { | |
| next_fires: Vec<i64>, | |
| }, | |
| SessionCount { | |
| revision: u64, | |
| active: u32, | |
| paused: u32, | |
| has_error: bool, | |
| }, | |
| Page { | |
| revision: u64, | |
| tasks: Vec<ScheduledSummary>, | |
| next_cursor: Option<String>, | |
| #[serde(default, skip_serializing_if = "Option::is_none")] | |
| counts: Option<ScheduleCounts>, | |
| }, | |
| Task { | |
| detail: Option<Box<ScheduledDetail>>, | |
| }, | |
| Changed { | |
| revision: u64, | |
| }, | |
| } | |
| #[derive(Deserialize, JsonSchema)] | |
| #[serde( | |
| tag = "kind", | |
| rename_all = "snake_case", | |
| rename_all_fields = "camelCase", | |
| deny_unknown_fields | |
| )] | |
| #[schemars(rename = "AgentScheduleChange")] | |
| pub enum ScheduleChange { | |
| Create { | |
| input: ScheduledInput, | |
| }, | |
| Update { | |
| task_id: String, | |
| revision: u64, | |
| input: ScheduledInput, | |
| }, | |
| Approve { | |
| task_id: String, | |
| revision: u64, | |
| /// Refresh the discovery target and approve it in one operation. | |
| input: Option<ScheduledInput>, | |
| }, | |
| Pause { | |
| task_id: String, | |
| revision: u64, | |
| }, | |
| Resume { | |
| task_id: String, | |
| revision: u64, | |
| }, | |
| TriggerNow { | |
| task_id: String, | |
| revision: u64, | |
| }, | |
| ClearHistory { | |
| task_id: String, | |
| revision: u64, | |
| }, | |
| Delete { | |
| task_id: String, | |
| revision: u64, | |
| }, | |
| /// Atomically remove all completed or expired non-discovery automations with no pending fire. | |
| ClearFinished { | |
| #[serde(default)] | |
| session_id: Option<String>, | |
| }, | |
| } | |
| #[derive(Clone, Serialize, Deserialize, JsonSchema)] | |
| #[serde( | |
| tag = "kind", | |
| rename_all = "snake_case", | |
| rename_all_fields = "camelCase", | |
| deny_unknown_fields | |
| )] | |
| #[schemars(rename = "AgentScheduleResult")] | |
| pub enum ScheduleResult { | |
| Committed { task_id: String, revision: u64 }, | |
| Cleared { count: u32, revision: u64 }, | |
| Conflict { actual: Option<u64> }, | |
| Rejected { reason: String }, | |
| } | |
| #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] | |
| #[serde(rename_all = "camelCase", deny_unknown_fields)] | |
| #[schemars(rename = "AgentScheduleCounts")] | |
| pub struct ScheduleCounts { | |
| pub active: u32, | |
| pub paused: u32, | |
| pub finished: u32, | |
| pub clearable_finished: u32, | |
| pub total: u32, | |
| } | |
| #[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)] | |
| #[serde(rename_all = "camelCase", deny_unknown_fields)] | |
| #[schemars(rename = "AgentScheduleScriptTest")] | |
| pub struct ScheduleScriptTest { | |
| pub session_id: String, | |
| pub source: String, | |
| #[serde(default)] | |
| pub task_id: Option<String>, | |
| } | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] | |
| #[serde(rename_all = "camelCase", deny_unknown_fields)] | |
| #[schemars(rename = "AgentScheduleScriptTestResult")] | |
| pub struct ScheduleScriptTestResult { | |
| pub ok: bool, | |
| #[serde(default, skip_serializing_if = "Option::is_none")] | |
| pub triggered: Option<bool>, | |
| pub stdout: String, | |
| pub stderr: String, | |
| #[serde(default, skip_serializing_if = "Option::is_none")] | |
| pub error: Option<String>, | |
| pub duration_ms: u64, | |
| } |