albatross-cli 2.0.3

Open source, terminal-first AI coding agent with fully transparent multi-model routing. Local (Ollama, LM Studio, MLX, llama.cpp) or cloud, your keys, one TUI.
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_json::{json, Value};

use super::registry::HookDispatchResult;
use super::{
    HookDecision, HookDispatch, HookEffect, HookEventName, HookInvocationContext, HookRegistry,
};
use crate::turn_trace::{SharedTurnTrace, TracePayload};

const MAX_HOOK_CONTEXT_ITEM_CHARS: usize = 2_000;
const MAX_HOOK_CONTEXT_TOTAL_CHARS: usize = 8_000;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HookNoticeLevel {
    Warning,
    Blocked,
    Denied,
    Stopped,
    Feedback,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HookNotice {
    pub event: HookEventName,
    pub hook_key: Option<String>,
    pub level: HookNoticeLevel,
    pub message: String,
}

#[derive(Debug, Clone, Default)]
pub struct HookOutcome {
    pub blocking_reason: Option<String>,
    pub stop_reason: Option<String>,
    pub allowed: bool,
    pub additional_context: Vec<String>,
    pub updated_input: Option<Value>,
    pub notices: Vec<HookNotice>,
}

pub async fn dispatch_hook_payload(
    registry: &HookRegistry,
    event: HookEventName,
    payload: &Value,
    matcher_value: Option<&str>,
    trace: Option<SharedTurnTrace>,
) -> HookOutcome {
    if let Some(trace) = &trace {
        for hook in registry.matching_hooks(event, matcher_value) {
            append_trace(
                trace,
                TracePayload::HookStart {
                    event: event.as_str().into(),
                    key: hook.key,
                    source: hook.source.label,
                    command: crate::turn_trace::redact_string(&hook.handler.command),
                    matcher: hook.matcher,
                },
            );
        }
    }

    let dispatch = registry.dispatch(event, payload, matcher_value).await;
    if let Some(trace) = &trace {
        for result in &dispatch.results {
            append_trace(
                trace,
                TracePayload::HookEnd {
                    event: event.as_str().into(),
                    key: result.hook.key.clone(),
                    duration_ms: result.run.duration_ms,
                    exit_code: result.run.exit_code,
                    timed_out: result.run.timed_out,
                    stdout: trace_preview(&result.run.stdout),
                    stderr: trace_preview(&result.run.stderr),
                },
            );
            let effect = &result.run.effect;
            let decision = effective_decision(event, result);
            if decision.is_some()
                || effect.feedback.is_some()
                || effect.warning.is_some()
                || effect.reason.is_some()
            {
                append_trace(
                    trace,
                    TracePayload::HookDecision {
                        event: event.as_str().into(),
                        key: result.hook.key.clone(),
                        decision: decision.map(hook_decision_name).map(str::to_string),
                        reason: decision_reason(event, result, decision)
                            .map(|reason| bounded_hook_context_text(&reason)),
                        feedback: effect.feedback.as_deref().map(bounded_hook_context_text),
                        warning: effect.warning.as_deref().map(bounded_hook_context_text),
                    },
                );
            }
        }
    }
    summarize_hook_dispatch(event, &dispatch)
}

pub fn summarize_hook_dispatch(event: HookEventName, dispatch: &HookDispatch) -> HookOutcome {
    let mut outcome = HookOutcome::default();
    for result in &dispatch.results {
        let effect = &result.run.effect;
        if let Some(warning) = &effect.warning {
            outcome.notices.push(HookNotice {
                event,
                hook_key: Some(result.hook.key.clone()),
                level: HookNoticeLevel::Warning,
                message: bounded_hook_context_text(warning),
            });
        }
        let decision = effective_decision(event, result);
        match decision {
            Some(HookDecision::Block) => {
                let raw_reason = decision_reason(event, result, decision)
                    .unwrap_or_else(|| format!("{} hook blocked execution", event.as_str()));
                let reason = bounded_hook_context_text(&raw_reason);
                if outcome.blocking_reason.is_none() {
                    outcome.blocking_reason = Some(reason.clone());
                }
                outcome.notices.push(HookNotice {
                    event,
                    hook_key: Some(result.hook.key.clone()),
                    level: HookNoticeLevel::Blocked,
                    message: reason,
                });
            }
            Some(HookDecision::Deny) => {
                let raw_reason = effect
                    .reason
                    .clone()
                    .unwrap_or_else(|| format!("{} hook denied execution", event.as_str()));
                let reason = bounded_hook_context_text(&raw_reason);
                if outcome.blocking_reason.is_none() {
                    outcome.blocking_reason = Some(reason.clone());
                }
                outcome.notices.push(HookNotice {
                    event,
                    hook_key: Some(result.hook.key.clone()),
                    level: HookNoticeLevel::Denied,
                    message: reason,
                });
            }
            Some(HookDecision::Stop) => {
                let raw_reason = effect
                    .reason
                    .clone()
                    .unwrap_or_else(|| format!("{} hook stopped the turn", event.as_str()));
                let reason = bounded_hook_context_text(&raw_reason);
                if outcome.stop_reason.is_none() {
                    outcome.stop_reason = Some(reason.clone());
                }
                outcome.notices.push(HookNotice {
                    event,
                    hook_key: Some(result.hook.key.clone()),
                    level: HookNoticeLevel::Stopped,
                    message: reason,
                });
            }
            Some(HookDecision::Allow) => {
                outcome.allowed = true;
            }
            None => {}
        }
        if let Some(feedback) = &effect.feedback {
            outcome.notices.push(HookNotice {
                event,
                hook_key: Some(result.hook.key.clone()),
                level: HookNoticeLevel::Feedback,
                message: bounded_hook_context_text(feedback),
            });
        }
        if let Some(context) = &effect.additional_context {
            outcome.additional_context.push(context.clone());
        }
        if let Some(updated_input) = &effect.updated_input {
            if accepts_updated_input(event, effect) {
                outcome.updated_input = Some(updated_input.clone());
            }
        }
    }
    if outcome.blocking_reason.is_some() || outcome.stop_reason.is_some() {
        outcome.allowed = false;
        outcome.updated_input = None;
    }
    outcome
}

pub fn hook_context_messages(event: HookEventName, outcome: &HookOutcome) -> Vec<String> {
    let mut messages = Vec::new();
    let mut total = 0usize;
    for context in &outcome.additional_context {
        push_bounded_context(
            &mut messages,
            &mut total,
            format!(
                "{} hook additional context: {}",
                event.as_str(),
                bounded_hook_context_text(context)
            ),
        );
    }
    for notice in &outcome.notices {
        if notice.level == HookNoticeLevel::Feedback {
            push_bounded_context(
                &mut messages,
                &mut total,
                format!(
                    "{} hook feedback: {}",
                    notice.event.as_str(),
                    bounded_hook_context_text(&notice.message)
                ),
            );
        }
    }
    messages
}

pub fn render_hook_context_block(contexts: Vec<String>) -> Option<String> {
    let mut unique = Vec::<String>::new();
    let mut total = 0usize;
    for context in contexts {
        let context = context.trim();
        if context.is_empty() || unique.iter().any(|existing| existing == context) {
            continue;
        }
        if total >= MAX_HOOK_CONTEXT_TOTAL_CHARS {
            break;
        }
        let remaining = MAX_HOOK_CONTEXT_TOTAL_CHARS - total;
        let bounded = truncate_chars(context, remaining);
        total += bounded.chars().count();
        unique.push(bounded);
    }
    if unique.is_empty() {
        return None;
    }

    let mut content = String::from("Additional context from hooks:\n");
    for context in unique {
        content.push_str("- ");
        content.push_str(&context);
        content.push('\n');
    }
    Some(content)
}

pub fn bounded_hook_context_text(text: &str) -> String {
    let redacted = crate::turn_trace::redact_string(text.trim());
    let framed = single_line_hook_text(&redacted);
    truncate_chars(&framed, MAX_HOOK_CONTEXT_ITEM_CHARS)
}

fn single_line_hook_text(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut chars = text.chars().peekable();
    while let Some(ch) = chars.next() {
        match ch {
            '\r' => {
                if chars.peek() == Some(&'\n') {
                    chars.next();
                }
                out.push_str("\\n");
            }
            '\n' => out.push_str("\\n"),
            '\t' => out.push(' '),
            ch if ch.is_control() => out.push(' '),
            ch => out.push(ch),
        }
    }
    out
}

fn push_bounded_context(messages: &mut Vec<String>, total: &mut usize, message: String) {
    if *total >= MAX_HOOK_CONTEXT_TOTAL_CHARS {
        return;
    }
    let remaining = MAX_HOOK_CONTEXT_TOTAL_CHARS - *total;
    let bounded = truncate_chars(&message, remaining);
    *total += bounded.chars().count();
    messages.push(bounded);
}

fn truncate_chars(text: &str, max_chars: usize) -> String {
    if text.chars().count() <= max_chars {
        return text.to_string();
    }
    let keep = max_chars.saturating_sub("...[truncated]".len());
    let mut out: String = text.chars().take(keep).collect();
    out.push_str("...[truncated]");
    out
}

fn effective_decision(event: HookEventName, result: &HookDispatchResult) -> Option<HookDecision> {
    match result.run.effect.decision {
        Some(HookDecision::Block | HookDecision::Deny | HookDecision::Stop) => {
            result.run.effect.decision
        }
        _ if fail_closed_reason(event, result).is_some() => Some(HookDecision::Block),
        other => other,
    }
}

fn decision_reason(
    event: HookEventName,
    result: &HookDispatchResult,
    decision: Option<HookDecision>,
) -> Option<String> {
    if decision == Some(HookDecision::Block)
        && result.run.effect.decision != Some(HookDecision::Block)
    {
        if let Some(reason) = fail_closed_reason(event, result) {
            return Some(reason);
        }
    }
    result.run.effect.reason.clone()
}

fn fail_closed_reason(event: HookEventName, result: &HookDispatchResult) -> Option<String> {
    if !matches!(
        event,
        HookEventName::PreToolUse | HookEventName::PermissionRequest
    ) {
        return None;
    }
    result.run.fail_closed_reason()
}

fn accepts_updated_input(event: HookEventName, effect: &HookEffect) -> bool {
    match event {
        HookEventName::PreToolUse => effect.decision == Some(HookDecision::Allow),
        HookEventName::UserPromptSubmit => !matches!(
            effect.decision,
            Some(HookDecision::Block | HookDecision::Deny | HookDecision::Stop)
        ),
        _ => false,
    }
}

pub fn plan_updated_payload_from_tool_result(
    ctx: &HookInvocationContext,
    tool_use_id: &str,
    output: &str,
) -> Option<Value> {
    let value: Value = serde_json::from_str(output).ok()?;
    if value.get("plan_updated").and_then(Value::as_bool) != Some(true) {
        return None;
    }
    let steps = value.get("steps").and_then(Value::as_array)?.clone();
    let done = value
        .get("done")
        .and_then(Value::as_u64)
        .unwrap_or_else(|| count_steps_with_status(&steps, "done") as u64);
    let total = value
        .get("total")
        .and_then(Value::as_u64)
        .unwrap_or(steps.len() as u64);
    let active_step = steps
        .iter()
        .find(|step| step.get("status").and_then(Value::as_str) == Some("in_progress"))
        .cloned();

    let mut payload = ctx
        .payload(HookEventName::PlanUpdated)
        .insert("tool_use_id", json!(tool_use_id))
        .insert("progress", json!({ "done": done, "total": total }))
        .insert("plan", Value::Array(steps));
    if let Some(active_step) = active_step {
        payload = payload.insert("active_step", active_step);
    }
    Some(payload.into_value())
}

fn count_steps_with_status(steps: &[Value], status: &str) -> usize {
    steps
        .iter()
        .filter(|step| step.get("status").and_then(Value::as_str) == Some(status))
        .count()
}

fn hook_decision_name(decision: HookDecision) -> &'static str {
    match decision {
        HookDecision::Block => "block",
        HookDecision::Allow => "allow",
        HookDecision::Deny => "deny",
        HookDecision::Stop => "stop",
    }
}

fn append_trace(trace: &SharedTurnTrace, payload: TracePayload) {
    if let Ok(guard) = trace.lock() {
        let _ = guard.append(payload);
    }
}

fn trace_preview(text: &str) -> Option<String> {
    let trimmed = text.trim();
    if trimmed.is_empty() {
        return None;
    }
    let mut preview: String = trimmed.chars().take(400).collect();
    if trimmed.chars().count() > 400 {
        preview.push_str("...[truncated]");
    }
    Some(crate::turn_trace::redact_string(&preview))
}