imp-core 0.2.0

Agent engine for imp: loop, tools, sessions, hooks, context, and SDK
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use serde::{Deserialize, Serialize};

use crate::workflow::{VerificationCloseoutEffect, VerificationGate, VerificationGateStatus};

/// Coarse-grained phase of a single agent turn.
///
/// This is intentionally small and mechanical: it describes where the runtime
/// is, not why the runtime should continue or stop. Policy decisions live in
/// [`LoopDecision`] / [`RunFinalStatus`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TurnPhase {
    ReceiveCommands,
    PreTurn,
    BuildContext,
    SampleModel,
    FinalizeAssistantMessage,
    PlanTools,
    ExecuteTools,
    RecordObservations,
    AssessTurn,
    DecideNext,
    Finish,
}

impl TurnPhase {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::ReceiveCommands => "receive_commands",
            Self::PreTurn => "pre_turn",
            Self::BuildContext => "build_context",
            Self::SampleModel => "sample_model",
            Self::FinalizeAssistantMessage => "finalize_assistant_message",
            Self::PlanTools => "plan_tools",
            Self::ExecuteTools => "execute_tools",
            Self::RecordObservations => "record_observations",
            Self::AssessTurn => "assess_turn",
            Self::DecideNext => "decide_next",
            Self::Finish => "finish",
        }
    }
}

/// Minimal visible state for a turn. This is the first slice of making turn
/// state explicit; later slices can add durable IDs and recovery cursors.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TurnState {
    pub index: u32,
    pub phase: TurnPhase,
    pub continue_reason: Option<ContinueReason>,
    pub planned_tools: usize,
    pub completed_tools: usize,
}

impl TurnState {
    pub fn new(index: u32) -> Self {
        Self {
            index,
            phase: TurnPhase::ReceiveCommands,
            continue_reason: None,
            planned_tools: 0,
            completed_tools: 0,
        }
    }

    pub fn enter(&mut self, phase: TurnPhase) {
        self.phase = phase;
    }

    pub fn record_continue(&mut self, reason: ContinueReason) {
        self.continue_reason = Some(reason);
    }

    pub fn record_tool_plan(&mut self, planned_tools: usize) {
        self.planned_tools = planned_tools;
    }

    pub fn record_tool_results(&mut self, completed_tools: usize) {
        self.completed_tools = completed_tools;
    }
}

/// Why the agent is allowed to spend another turn.
///
/// There should be no anonymous `continue`; long-running autonomy is safe only
/// when each continuation has a concrete, inspectable reason.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContinueReason {
    ExternalizationNeeded,
    HighConfidenceVisibleNextStep,
    ExecutionDebt,
    ToolResultsNeedInterpretation,
    QueuedUserFollowUp,
    RecoveryContinuation,
}

impl ContinueReason {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::ExternalizationNeeded => "externalization_needed",
            Self::HighConfidenceVisibleNextStep => "high_confidence_visible_next_step",
            Self::ExecutionDebt => "execution_debt",
            Self::ToolResultsNeedInterpretation => "tool_results_need_interpretation",
            Self::QueuedUserFollowUp => "queued_user_follow_up",
            Self::RecoveryContinuation => "recovery_continuation",
        }
    }
}

/// Semantic reason the runtime stopped intentionally.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StopReason {
    NoAutomaticFollowUp,
    NoProgress,
    RepeatedAction,
    UserBlocker,
    ExecutionBlocked,
    DecompositionCompleted,
    WorkCompleted,
}

impl StopReason {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::NoAutomaticFollowUp => "no_automatic_follow_up",
            Self::NoProgress => "no_progress",
            Self::RepeatedAction => "repeated_action",
            Self::UserBlocker => "user_blocker",
            Self::ExecutionBlocked => "execution_blocked",
            Self::DecompositionCompleted => "decomposition_completed",
            Self::WorkCompleted => "work_completed",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum LoopDecision {
    Continue {
        reason: ContinueReason,
        prompt: String,
    },
    Finish {
        status: RunFinalStatus,
    },
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum RunFinalStatus {
    Done {
        reason: StopReason,
    },
    DoneWithConcerns {
        reason: StopReason,
        concerns: Vec<String>,
    },
    Blocked {
        reason: StopReason,
        message: String,
    },
    NeedsUserInput {
        question: String,
    },
    Cancelled,
    Failed {
        message: String,
    },
}

impl RunFinalStatus {
    pub fn from_stop_reason(reason: StopReason) -> Self {
        match reason {
            StopReason::UserBlocker | StopReason::ExecutionBlocked | StopReason::RepeatedAction => {
                Self::Blocked {
                    reason,
                    message: reason.as_str().to_string(),
                }
            }
            StopReason::NoProgress => Self::DoneWithConcerns {
                reason,
                concerns: vec!["stopped because no justified continuation was available".into()],
            },
            _ => Self::Done { reason },
        }
    }
    pub fn with_concern(self, concern: impl Into<String>) -> Self {
        match self {
            Self::Done { reason } => Self::DoneWithConcerns {
                reason,
                concerns: vec![concern.into()],
            },
            Self::DoneWithConcerns {
                reason,
                mut concerns,
            } => {
                concerns.push(concern.into());
                Self::DoneWithConcerns { reason, concerns }
            }
            other => other,
        }
    }
}

pub fn enforce_verification_closeout(
    proposed: RunFinalStatus,
    gates: &[VerificationGate],
) -> RunFinalStatus {
    if gates.is_empty() {
        return proposed;
    }

    let mut concerns = Vec::new();
    let mut blocked = Vec::new();
    for gate in gates.iter().filter(|gate| gate.is_required()) {
        match gate.closeout_effect() {
            VerificationCloseoutEffect::AllowsDone => {}
            VerificationCloseoutEffect::BlocksDone => blocked.push(verification_gate_message(gate)),
            VerificationCloseoutEffect::BlocksDoneWithConcerns => {
                concerns.push(verification_gate_message(gate));
            }
        }
    }

    if blocked.is_empty() && concerns.is_empty() {
        return proposed;
    }

    if !blocked.is_empty() {
        let message = blocked.join("; ");
        return match proposed {
            RunFinalStatus::Cancelled | RunFinalStatus::Failed { .. } => proposed,
            _ => RunFinalStatus::Blocked {
                reason: StopReason::ExecutionBlocked,
                message,
            },
        };
    }

    match proposed {
        RunFinalStatus::Done { reason } => RunFinalStatus::DoneWithConcerns { reason, concerns },
        RunFinalStatus::DoneWithConcerns {
            reason,
            concerns: mut existing,
        } => {
            existing.extend(concerns);
            RunFinalStatus::DoneWithConcerns {
                reason,
                concerns: existing,
            }
        }
        RunFinalStatus::Blocked { .. }
        | RunFinalStatus::NeedsUserInput { .. }
        | RunFinalStatus::Cancelled
        | RunFinalStatus::Failed { .. } => proposed,
    }
}

#[allow(dead_code)]
pub fn enforce_verification_decision(
    decision: LoopDecision,
    gates: &[VerificationGate],
) -> LoopDecision {
    match decision {
        LoopDecision::Finish { status } => LoopDecision::Finish {
            status: enforce_verification_closeout(status, gates),
        },
        LoopDecision::Continue { .. } => decision,
    }
}

fn verification_gate_message(gate: &VerificationGate) -> String {
    let name = if gate.name.is_empty() {
        &gate.id
    } else {
        &gate.name
    };
    let status = match gate.status {
        VerificationGateStatus::Pending => "pending",
        VerificationGateStatus::Running => "still running",
        VerificationGateStatus::Passed => "passed",
        VerificationGateStatus::Failed => "failed",
        VerificationGateStatus::Skipped => "skipped",
        VerificationGateStatus::Blocked => "blocked",
    };
    let detail = gate.reason.as_deref().or_else(|| {
        gate.result
            .as_ref()
            .and_then(|result| result.summary.as_deref())
    });
    match detail {
        Some(detail) if !detail.is_empty() => {
            format!("required verification {status}: {name} ({detail})")
        }
        _ => format!("required verification {status}: {name}"),
    }
}

/// Tool risk visible before execution. This is deliberately conservative and
/// can be refined as tool metadata grows.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolRisk {
    ReadOnly,
    Mutable,
    ExternalSideEffect,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolExecutionMode {
    ParallelReadonlyThenSequentialMutable,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PlannedToolCall {
    pub index: usize,
    pub id: String,
    pub name: String,
    pub args: serde_json::Value,
    pub risk: ToolRisk,
    pub retry_safe: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ToolPlan {
    pub mode: ToolExecutionMode,
    pub calls: Vec<PlannedToolCall>,
}

impl ToolPlan {
    pub fn empty() -> Self {
        Self {
            mode: ToolExecutionMode::ParallelReadonlyThenSequentialMutable,
            calls: Vec::new(),
        }
    }

    pub fn len(&self) -> usize {
        self.calls.len()
    }

    pub fn is_empty(&self) -> bool {
        self.calls.is_empty()
    }
}

#[cfg(test)]
mod workflow_closeout_tests {
    use super::*;
    use crate::workflow::{
        VerificationGate, VerificationGateRequirement, VerificationGateResult,
        VerificationGateStatus,
    };

    fn done() -> RunFinalStatus {
        RunFinalStatus::Done {
            reason: StopReason::WorkCompleted,
        }
    }

    #[test]
    fn workflow_closeout_preserves_done_when_no_gates_or_required_passed() {
        assert_eq!(enforce_verification_closeout(done(), &[]), done());

        let mut gate = VerificationGate::command("unit", "cargo test");
        gate.mark_passed(VerificationGateResult::passed(0));
        assert_eq!(enforce_verification_closeout(done(), &[gate]), done());
    }

    #[test]
    fn workflow_closeout_downgrades_done_for_required_failed_or_skipped_gates() {
        let mut failed = VerificationGate::command("unit", "cargo test");
        failed.mark_failed(VerificationGateResult {
            summary: Some("tests failed".into()),
            ..VerificationGateResult::failed(101)
        });
        let status = enforce_verification_closeout(done(), &[failed]);
        match status {
            RunFinalStatus::DoneWithConcerns { reason, concerns } => {
                assert_eq!(reason, StopReason::WorkCompleted);
                assert!(concerns
                    .iter()
                    .any(|concern| concern.contains("required verification failed: unit")));
                assert!(concerns
                    .iter()
                    .any(|concern| concern.contains("tests failed")));
            }
            other => panic!("expected DoneWithConcerns, got {other:?}"),
        }

        let mut skipped = VerificationGate::command("fmt", "cargo fmt --check");
        skipped.mark_skipped("formatter unavailable");
        let status = enforce_verification_closeout(done(), &[skipped]);
        match status {
            RunFinalStatus::DoneWithConcerns { concerns, .. } => {
                assert!(concerns
                    .iter()
                    .any(|concern| concern.contains("required verification skipped: fmt")));
                assert!(concerns
                    .iter()
                    .any(|concern| concern.contains("formatter unavailable")));
            }
            other => panic!("expected DoneWithConcerns, got {other:?}"),
        }
    }

    #[test]
    fn workflow_closeout_blocks_done_for_required_blocked_gates() {
        let mut gate = VerificationGate::command("unit", "cargo test");
        gate.mark_blocked("cargo missing");
        let status = enforce_verification_closeout(done(), &[gate]);
        match status {
            RunFinalStatus::Blocked { reason, message } => {
                assert_eq!(reason, StopReason::ExecutionBlocked);
                assert!(message.contains("required verification blocked: unit"));
                assert!(message.contains("cargo missing"));
            }
            other => panic!("expected Blocked, got {other:?}"),
        }
    }

    #[test]
    fn workflow_closeout_pending_and_running_required_gates_cannot_report_done() {
        let pending = VerificationGate::command("unit", "cargo test");
        let status = enforce_verification_closeout(done(), &[pending]);
        assert!(matches!(status, RunFinalStatus::DoneWithConcerns { .. }));

        let mut running = VerificationGate::command("fmt", "cargo fmt --check");
        running.mark_running();
        let status = enforce_verification_closeout(done(), &[running]);
        match status {
            RunFinalStatus::DoneWithConcerns { concerns, .. } => {
                assert!(concerns
                    .iter()
                    .any(|concern| concern.contains("required verification still running: fmt")));
            }
            other => panic!("expected DoneWithConcerns, got {other:?}"),
        }
    }

    #[test]
    fn workflow_closeout_optional_failed_gate_does_not_block_done() {
        let mut gate = VerificationGate::command("smoke", "cargo test smoke");
        gate.requirement = VerificationGateRequirement::Optional;
        gate.mark_failed(VerificationGateResult::failed(1));
        assert_eq!(enforce_verification_closeout(done(), &[gate]), done());
    }

    #[test]
    fn workflow_closeout_merges_existing_concerns_and_wraps_loop_decision() {
        let mut gate = VerificationGate::command("fmt", "cargo fmt --check");
        gate.status = VerificationGateStatus::Failed;
        let proposed = RunFinalStatus::DoneWithConcerns {
            reason: StopReason::NoProgress,
            concerns: vec!["pre-existing".into()],
        };
        let status = enforce_verification_closeout(proposed, &[gate]);
        match status {
            RunFinalStatus::DoneWithConcerns { reason, concerns } => {
                assert_eq!(reason, StopReason::NoProgress);
                assert!(concerns.iter().any(|concern| concern == "pre-existing"));
                assert!(concerns
                    .iter()
                    .any(|concern| concern.contains("required verification failed: fmt")));
            }
            other => panic!("expected DoneWithConcerns, got {other:?}"),
        }

        let mut blocked = VerificationGate::command("unit", "cargo test");
        blocked.mark_blocked("timeout");
        let decision = LoopDecision::Finish { status: done() };
        assert!(matches!(
            enforce_verification_decision(decision, &[blocked]),
            LoopDecision::Finish {
                status: RunFinalStatus::Blocked { .. }
            }
        ));
    }
}