quantumclaw 0.1.0

Single-crate public API for the QuantumClaw agent runtime built on ZeroClaw.
Documentation
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DecisionProblem {
    pub id: String,
    pub goals: Vec<Goal>,
    pub subtasks: Vec<Subtask>,
    pub candidate_actions: Vec<CandidateAction>,
    pub dependencies: Vec<Dependency>,
    pub constraints: Vec<Constraint>,
    pub cost_model: CostModel,
    pub risk_model: RiskModel,
    pub deadline: Option<Deadline>,
    pub resource_budget: ResourceBudget,
    pub confidence: ConfidenceEstimate,
    pub rollback: RollbackStrategy,
    pub metadata: ExecutionMetadata,
}

impl DecisionProblem {
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            goals: Vec::new(),
            subtasks: Vec::new(),
            candidate_actions: Vec::new(),
            dependencies: Vec::new(),
            constraints: Vec::new(),
            cost_model: CostModel::default(),
            risk_model: RiskModel::default(),
            deadline: None,
            resource_budget: ResourceBudget::default(),
            confidence: ConfidenceEstimate::default(),
            rollback: RollbackStrategy::default(),
            metadata: ExecutionMetadata::default(),
        }
    }

    pub fn for_task(task: impl Into<String>) -> Self {
        let task = task.into();
        let mut problem = Self::new("decision-problem");
        problem
            .goals
            .push(Goal::new("goal", task.clone()).with_priority(1.0));
        problem.subtasks = vec![
            Subtask::new(
                "understand-context",
                "Understand the requested work and current code shape",
            ),
            Subtask::new(
                "design-safe-plan",
                "Design a small-step execution plan with validation gates",
            ),
            Subtask::new(
                "simulate-execution",
                "Resolve tools and simulate the execution path",
            ),
            Subtask::new(
                "capture-learning",
                "Capture reusable procedure after a successful execution",
            ),
        ];
        problem.candidate_actions = vec![
            CandidateAction::new(
                "inspect",
                "Inspect target module",
                "Read relevant files and interfaces",
            )
            .with_tool_hint("filesystem")
            .with_utility(0.78),
            CandidateAction::new(
                "test-first",
                "Add characterization tests",
                "Prove intended behavior before edits",
            )
            .with_tool_hint("shell")
            .with_utility(0.9),
            CandidateAction::new(
                "small-edit",
                "Apply focused code edit",
                "Change the smallest safe surface area",
            )
            .with_tool_hint("code_edit")
            .with_utility(0.86),
            CandidateAction::new(
                "validate",
                "Run validation suite",
                "Run formatter and tests before completion",
            )
            .with_tool_hint("shell")
            .with_utility(0.93),
        ];
        problem.dependencies = vec![
            Dependency::new(
                "inspect",
                "test-first",
                "Tests depend on knowing existing behavior",
            ),
            Dependency::new(
                "test-first",
                "small-edit",
                "Edits follow failing or characterization tests",
            ),
            Dependency::new(
                "small-edit",
                "validate",
                "Validation follows implementation",
            ),
        ];
        problem.constraints = vec![
            Constraint::hard(
                "validation-required",
                "No execution plan is complete without deterministic validation",
            ),
            Constraint::hard(
                "reversible-edits",
                "Prefer edits that can be rolled back cleanly",
            ),
            Constraint::soft(
                "low-blast-radius",
                "Minimize touched files and external side effects",
                0.8,
            ),
        ];
        problem.resource_budget = ResourceBudget {
            max_tool_calls: Some(12),
            max_latency_ms: Some(30_000),
            max_cost_micros: Some(50_000),
        };
        problem.metadata.tags = vec!["general-agent-runtime".into(), "safe-refactor".into()];
        problem.metadata.data.insert("task".into(), task);
        problem
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Goal {
    pub id: String,
    pub description: String,
    pub priority: UtilityScore,
    pub success_criteria: Vec<String>,
}

impl Goal {
    pub fn new(id: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            description: description.into(),
            priority: UtilityScore(1.0),
            success_criteria: Vec::new(),
        }
    }

    pub fn with_priority(mut self, value: f64) -> Self {
        self.priority = UtilityScore(value);
        self
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Subtask {
    pub id: String,
    pub description: String,
    pub required: bool,
    pub estimated_cost: CostModel,
    pub risk: RiskModel,
}

impl Subtask {
    pub fn new(id: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            description: description.into(),
            required: true,
            estimated_cost: CostModel::default(),
            risk: RiskModel::default(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CandidateAction {
    pub id: String,
    pub name: String,
    pub description: String,
    pub tool_hint: Option<String>,
    pub preconditions: Preconditions,
    pub postconditions: Postconditions,
    pub cost: CostModel,
    pub risk: RiskModel,
    pub utility: UtilityScore,
    pub rollback: RollbackStrategy,
}

impl CandidateAction {
    pub fn new(
        id: impl Into<String>,
        name: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            description: description.into(),
            tool_hint: None,
            preconditions: Preconditions::default(),
            postconditions: Postconditions::default(),
            cost: CostModel::default(),
            risk: RiskModel::default(),
            utility: UtilityScore(0.5),
            rollback: RollbackStrategy::default(),
        }
    }

    pub fn with_tool_hint(mut self, tool: impl Into<String>) -> Self {
        self.tool_hint = Some(tool.into());
        self
    }

    pub fn with_utility(mut self, value: f64) -> Self {
        self.utility = UtilityScore(value);
        self
    }

    pub fn with_risk(mut self, score: f64) -> Self {
        self.risk.score = score;
        self
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Dependency {
    pub before: String,
    pub after: String,
    pub reason: String,
}

impl Dependency {
    pub fn new(
        before: impl Into<String>,
        after: impl Into<String>,
        reason: impl Into<String>,
    ) -> Self {
        Self {
            before: before.into(),
            after: after.into(),
            reason: reason.into(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct Preconditions {
    pub checks: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct Postconditions {
    pub checks: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Constraint {
    pub id: String,
    pub description: String,
    pub kind: ConstraintKind,
    pub weight: f64,
}

impl Constraint {
    pub fn hard(id: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            description: description.into(),
            kind: ConstraintKind::Hard,
            weight: 1.0,
        }
    }

    pub fn soft(id: impl Into<String>, description: impl Into<String>, weight: f64) -> Self {
        Self {
            id: id.into(),
            description: description.into(),
            kind: ConstraintKind::Soft,
            weight,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConstraintKind {
    Hard,
    Soft,
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct CostModel {
    pub estimated_latency_ms: u64,
    pub estimated_tokens: u64,
    pub estimated_money_micros: u64,
}

impl Default for CostModel {
    fn default() -> Self {
        Self {
            estimated_latency_ms: 100,
            estimated_tokens: 0,
            estimated_money_micros: 0,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RiskModel {
    pub score: f64,
    pub labels: Vec<String>,
}

impl Default for RiskModel {
    fn default() -> Self {
        Self {
            score: 0.1,
            labels: Vec::new(),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct UtilityScore(pub f64);

impl Default for UtilityScore {
    fn default() -> Self {
        Self(0.0)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Deadline {
    pub unix_epoch_ms: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ResourceBudget {
    pub max_tool_calls: Option<u32>,
    pub max_latency_ms: Option<u64>,
    pub max_cost_micros: Option<u64>,
}

impl Default for ResourceBudget {
    fn default() -> Self {
        Self {
            max_tool_calls: Some(16),
            max_latency_ms: Some(60_000),
            max_cost_micros: Some(100_000),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct ConfidenceEstimate(pub f64);

impl Default for ConfidenceEstimate {
    fn default() -> Self {
        Self(0.5)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RollbackStrategy {
    pub kind: RollbackKind,
    pub description: String,
}

impl Default for RollbackStrategy {
    fn default() -> Self {
        Self {
            kind: RollbackKind::Checkpoint,
            description: "Preserve state before execution and revert on validation failure".into(),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RollbackKind {
    None,
    Checkpoint,
    CompensatingAction,
    ManualReview,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExecutionMetadata {
    pub trace_id: String,
    pub tags: Vec<String>,
    pub data: BTreeMap<String, String>,
}

impl Default for ExecutionMetadata {
    fn default() -> Self {
        Self {
            trace_id: "trace-local".into(),
            tags: Vec::new(),
            data: BTreeMap::new(),
        }
    }
}