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
use anyhow::Result;
use serde_json::json;
use crate::cli::PlanCommands;
use crate::config::RuntimeConfig;
use crate::order_status::{wait_for_order, wait_result_json, WaitCondition};
use crate::output::ResponseEnvelope;
use crate::plan::{json_schema, llm_prompt, load_plan};
use crate::safety::{execute_trading_order, require_trading_approval};
pub async fn run(runtime: &RuntimeConfig, command: PlanCommands) -> Result<()> {
match command {
PlanCommands::Schema => {
runtime.emit(ResponseEnvelope::ok("plan schema", json_schema()));
}
PlanCommands::Prompt => {
runtime.emit(ResponseEnvelope::ok("plan prompt", llm_prompt()));
}
PlanCommands::Validate { file } => {
let plan = load_plan(&file)?;
let api = runtime.build_api()?;
let report = plan.validate_with_api(&runtime.safety, &api).await?;
let mut envelope = if report.all_ok {
ResponseEnvelope::ok("plan validate", json!(report))
} else {
let mut e = ResponseEnvelope::err(
"plan validate",
"One or more steps failed safety validation",
);
e.data = json!(report);
e
};
envelope = envelope
.with_inputs(json!({
"file": file.display().to_string(),
"plan_id": plan.plan_id,
}))
.with_warnings(if report.all_ok {
vec![]
} else {
vec!["Fix failing steps or adjust safety.json before running".into()]
})
.with_next_actions(vec![
format!("schwab plan run {} --dry-run --json", file.display()),
]);
runtime.emit(envelope);
}
PlanCommands::Show { file } => {
let plan = load_plan(&file)?;
runtime.emit(
ResponseEnvelope::ok("plan show", json!(plan))
.with_inputs(json!({ "file": file.display().to_string() })),
);
}
PlanCommands::Run {
file,
step,
from_step,
} => {
let plan = load_plan(&file)?;
let steps = plan.steps_filtered(step.as_deref(), from_step.as_deref())?;
let summary = format!(
"Execute trade plan `{}` ({} step(s): {}).",
plan.plan_id,
steps.len(),
steps
.iter()
.map(|s| s.id.as_str())
.collect::<Vec<_>>()
.join(", ")
);
require_trading_approval(runtime, "plan run", &summary)?;
let api = runtime.build_api()?;
let equity = crate::portfolio::account_equity(&api, &plan.account_hash)
.await
.ok()
.flatten();
let mut results = Vec::new();
for (idx, step) in steps.iter().enumerate() {
let order = plan.step_order_json(step)?;
let wait_opts = plan.wait_options_for_step(step);
if runtime.dry_run {
runtime.safety.validate_order(&order, None, equity)?;
results.push(json!({
"step_id": step.id,
"status": "dry_run_ok",
"order": order,
"wait_until": wait_opts.condition.as_str(),
}));
continue;
}
match execute_trading_order(runtime, &api, &plan.account_hash, &order).await {
Ok(data) => {
let mut step_result = json!({
"step_id": step.id,
"status": "submitted",
"submit": data,
});
if wait_opts.condition != WaitCondition::Accepted {
if let Some(order_id) = data
.get("order_id")
.and_then(|v| v.as_str())
.map(str::to_string)
{
match wait_for_order(&api, &plan.account_hash, &order_id, wait_opts)
.await
{
Ok(wait) => {
step_result["wait"] = wait_result_json(&wait);
if wait.met {
step_result["status"] = json!("filled");
} else {
step_result["status"] = json!("wait_timeout");
results.push(step_result);
if plan.execution.stop_on_error {
runtime.emit(
ResponseEnvelope::err(
"plan run",
wait.error.unwrap_or_else(|| {
"Order wait timed out".into()
}),
)
.with_inputs(json!({
"file": file.display().to_string(),
"plan_id": plan.plan_id,
"completed_steps": results,
})),
);
return Ok(());
}
continue;
}
}
Err(e) => {
step_result["status"] = json!("wait_failed");
step_result["error"] = json!(e.to_string());
results.push(step_result);
if plan.execution.stop_on_error {
runtime.emit(
ResponseEnvelope::err("plan run", e.to_string())
.with_inputs(json!({
"file": file.display().to_string(),
"plan_id": plan.plan_id,
"completed_steps": results,
})),
);
return Ok(());
}
continue;
}
}
} else {
step_result["status"] = json!("submitted_no_order_id");
step_result["warning"] = json!(
"Could not parse order_id from Location; cannot wait for fill"
);
}
} else {
step_result["status"] = json!("executed");
}
results.push(step_result);
}
Err(e) => {
results.push(json!({
"step_id": step.id,
"status": "failed",
"error": e.to_string(),
}));
if plan.execution.stop_on_error {
runtime.emit(
ResponseEnvelope::err("plan run", e.to_string()).with_inputs(json!({
"file": file.display().to_string(),
"plan_id": plan.plan_id,
"completed_steps": results,
})),
);
return Ok(());
}
}
}
if !runtime.dry_run
&& plan.execution.pause_seconds_between_steps > 0
&& idx + 1 < steps.len()
{
tokio::time::sleep(std::time::Duration::from_secs(
plan.execution.pause_seconds_between_steps,
))
.await;
}
}
runtime.emit(
ResponseEnvelope::ok(
"plan run",
json!({
"plan_id": plan.plan_id,
"dry_run": runtime.dry_run,
"steps": results,
}),
)
.with_inputs(json!({
"file": file.display().to_string(),
"step_filter": step,
"from_step": from_step,
})),
);
}
}
Ok(())
}