thirdshift 0.12.3

Turn a GitHub issue into a ready-for-review pull request, unattended
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
//! Claude and Grok share stream framing, with private dialect rules.
//!
//! A result ends a turn, not necessarily the session: background sub-agents
//! can resume it. Only the first init sets shared session fields, and only
//! completion supplies the last result and any work killed since that result.

use std::collections::{BTreeMap, HashMap};

use serde_json::Value;

use super::{
    Decoder, Ended, Evidence, Facts, ModelScope, Report, SafeguardRefusal, StreamUpdate,
    TurnOutcome,
};
use crate::progress::{bash, shorten};

pub struct Stream {
    init: Option<Init>,
    /// Cumulative totals survive later results without a complete pair.
    turns_and_cost: Option<(u64, f64)>,
    final_message: Option<String>,
    dialect: Dialect,
}

struct Init {
    cwd: Option<String>,
    session_id: Option<String>,
    subscription: bool,
}

enum Dialect {
    Claude(Claude),
    Grok(Grok),
}

#[derive(Default)]
struct Claude {
    failed: bool,
    diagnostic: Option<String>,
    /// The latest root API error, only until this turn's result.
    api_error: Option<ClaudeApiError>,
    tasks: HashMap<String, String>,
    /// Task id and description, in order of the first kill since the result.
    killed: Vec<(String, String)>,
}

#[derive(Default)]
struct ClaudeApiError {
    message: Option<String>,
    /// Only diagnostic fields, never unrelated raw event content.
    facts: BTreeMap<&'static str, String>,
}

#[derive(Default)]
struct Grok {
    failed: bool,
    diagnostic: Option<String>,
    fallback_id: Option<String>,
    usage: Option<String>,
    cost_known: bool,
}

impl Dialect {
    /// Extract live facts before root-result and initialization guards. Grok
    /// shares assistant framing without Claude's usage or safeguard rules.
    fn evidence(&self, event: &Value) -> Vec<Evidence> {
        let mut evidence = Vec::new();
        if event["type"] == "assistant"
            && event["is_api_error_message"] != true
            && let Some(name) = event["message"]["model"].as_str()
        {
            let scope = if event["parent_tool_use_id"].is_null() {
                ModelScope::MainLoop
            } else {
                ModelScope::AttributionOnly
            };
            evidence.push(Evidence::ObservedModel {
                name: name.into(),
                scope,
            });
        }
        if matches!(self, Self::Claude(_)) {
            if event["type"] == "result"
                && let Some(usage) = event["modelUsage"].as_object()
            {
                evidence.extend(usage.keys().map(|name| Evidence::ObservedModel {
                    name: name.clone(),
                    scope: ModelScope::AttributionOnly,
                }));
            }
            let refused = match event["type"].as_str() {
                Some("system") => {
                    event["subtype"] == "model_refusal_no_fallback"
                        && event["api_refusal_category"] == "cyber"
                }
                Some("result") => event["result"].as_str().is_some_and(|text| {
                    let text = text.trim_start();
                    text.starts_with("[cyber]")
                        || (text.starts_with("API Error:") && text.contains("[cyber]"))
                }),
                _ => false,
            };
            if refused {
                evidence.push(Evidence::SafeguardRefusal(SafeguardRefusal::ClaudeCyber));
            }
        }
        evidence
    }
}

impl Stream {
    pub fn claude() -> Self {
        Self::new(Dialect::Claude(Claude::default()))
    }

    pub fn grok() -> Self {
        Self::new(Dialect::Grok(Grok::default()))
    }

    fn new(dialect: Dialect) -> Self {
        Self {
            init: None,
            turns_and_cost: None,
            final_message: None,
            dialect,
        }
    }

    fn summary(&self) -> Option<String> {
        let totals = || {
            self.turns_and_cost.map(|(turns, cost)| {
                let basis = if self.init.as_ref().is_some_and(|init| init.subscription) {
                    " at API prices"
                } else {
                    ""
                };
                format!("{turns} turns, ${cost:.2}{basis}")
            })
        };
        match &self.dialect {
            Dialect::Claude(_) => totals(),
            Dialect::Grok(grok) if grok.cost_known => totals().or_else(|| grok.usage.clone()),
            Dialect::Grok(grok) => grok.usage.clone(),
        }
    }

    fn tool_use(&self, name: &str, input: &Value) -> String {
        let grok = matches!(self.dialect, Dialect::Grok(_));
        let name = if grok && matches!(name, "bash" | "run_terminal_command") {
            "Bash"
        } else {
            name
        };
        match name {
            "Skill" => format!("skill {}", input["skill"].as_str().unwrap_or("?")),
            "Bash" => bash(input["command"].as_str().unwrap_or("")),
            _ => {
                let path = if grok { input["path"].as_str() } else { None };
                let detail = path.or_else(|| {
                    ["file_path", "pattern", "description", "url", "query"]
                        .iter()
                        .find_map(|key| input[key].as_str())
                });
                match detail {
                    Some(detail) => format!("{name} {}", shorten(self.relative(detail))),
                    None => name.to_string(),
                }
            }
        }
    }

    fn relative<'a>(&self, path: &'a str) -> &'a str {
        self.init
            .as_ref()
            .and_then(|init| init.cwd.as_deref())
            .and_then(|cwd| path.strip_prefix(cwd)?.strip_prefix('/'))
            .unwrap_or(path)
    }
}

impl Stream {
    fn progress(&mut self, event: &Value) -> Vec<String> {
        if let Dialect::Grok(grok) = &mut self.dialect
            && let Some(id) = event["session_id"].as_str()
        {
            grok.fallback_id.get_or_insert_with(|| id.to_string());
        }
        let mut lines = match event["type"].as_str() {
            Some("system") if event["subtype"] == "init" && self.init.is_none() => {
                self.init = Some(Init {
                    cwd: event["cwd"].as_str().map(String::from),
                    session_id: event["session_id"].as_str().map(String::from),
                    subscription: event["apiKeySource"] == "none",
                });
                vec!["session started".to_string()]
            }
            Some("assistant") => {
                if let Dialect::Claude(claude) = &mut self.dialect {
                    claude.assistant(event);
                }
                event["message"]["content"]
                    .as_array()
                    .into_iter()
                    .flatten()
                    .filter(|block| block["type"] == "tool_use")
                    .filter_map(|block| {
                        Some(self.tool_use(block["name"].as_str()?, &block["input"]))
                    })
                    .collect()
            }
            Some("system") => {
                if let Dialect::Claude(claude) = &mut self.dialect {
                    claude.track_task(event);
                }
                Vec::new()
            }
            Some("result") => {
                if matches!(self.dialect, Dialect::Claude(_))
                    && !event["parent_tool_use_id"].is_null()
                {
                    return Vec::new();
                }
                self.final_message = event["result"].as_str().map(String::from);
                if let (Some(turns), Some(cost)) = (
                    event["num_turns"].as_u64(),
                    event["total_cost_usd"].as_f64(),
                ) {
                    self.turns_and_cost = Some((turns, cost));
                }
                let failed = event["subtype"] != "success" || event["is_error"] == true;
                match &mut self.dialect {
                    Dialect::Claude(claude) => {
                        claude.result(event, failed);
                    }
                    Dialect::Grok(grok) => grok.result(event, failed),
                }
                Vec::new()
            }
            Some("error") => {
                if let Dialect::Grok(grok) = &mut self.dialect {
                    grok.failed = true;
                    grok.diagnostic = event["message"].as_str().map(String::from);
                }
                Vec::new()
            }
            _ => Vec::new(),
        };
        if let Dialect::Grok(grok) = &self.dialect
            && grok.failed
            && matches!(event["type"].as_str(), Some("error" | "result"))
        {
            lines.push(format!(
                "error: {}",
                shorten(grok.diagnostic.as_deref().unwrap_or("Grok's turn failed"))
            ));
        }
        lines
    }
}

impl Decoder for Stream {
    fn condense(&mut self, raw: &str) -> StreamUpdate {
        let Ok(event) = serde_json::from_str::<Value>(raw) else {
            return StreamUpdate::default();
        };
        let evidence = self.dialect.evidence(&event);
        StreamUpdate {
            progress: self.progress(&event),
            evidence,
        }
    }

    fn complete(self: Box<Self>) -> Facts {
        let summary = self.summary();
        let mut ended = Ended {
            session_id: self.init.and_then(|init| init.session_id),
            killed: Vec::new(),
            final_message: self.final_message,
        };
        let (failed, diagnostic) = match self.dialect {
            Dialect::Claude(claude) => {
                ended.killed = claude.killed.into_iter().map(|(_, text)| text).collect();
                let diagnostic = claude
                    .api_error
                    .and_then(|error| error.diagnostic(None))
                    .or(claude.diagnostic);
                (claude.failed, diagnostic)
            }
            Dialect::Grok(grok) => {
                ended.session_id = ended.session_id.or(grok.fallback_id);
                (grok.failed, grok.diagnostic)
            }
        };
        Facts {
            report: Report {
                warnings: Vec::new(),
                summary,
                ..Report::default()
            },
            ended,
            outcome: TurnOutcome::from_failed(failed),
            diagnostic,
        }
    }
}

impl Claude {
    fn assistant(&mut self, event: &Value) {
        if !event["parent_tool_use_id"].is_null() {
            return;
        }
        self.api_error = (event["is_api_error_message"] == true
            || nonempty_text(&event["error"]).is_some()
            || nonempty_text(&event["api_error"]).is_some())
        .then(|| ClaudeApiError::read(event));
    }

    fn result(&mut self, event: &Value, failed: bool) {
        self.failed = failed;
        self.killed.clear();
        let mut api_error = self.api_error.take().unwrap_or_default();
        self.diagnostic = if failed {
            // Terminal facts replace only the fields they actually supply.
            api_error.facts.extend(ClaudeApiError::facts(event));
            let errors: Vec<_> = event["errors"]
                .as_array()
                .into_iter()
                .flatten()
                .filter_map(nonempty_text)
                .collect();
            let message = if errors.is_empty() {
                nonempty_text(&event["result"]).map(String::from)
            } else {
                Some(errors.join("\n"))
            };
            api_error.diagnostic(message)
        } else {
            None
        };
    }

    fn track_task(&mut self, event: &Value) {
        let Some(id) = event["task_id"].as_str() else {
            return;
        };
        let killed = match event["subtype"].as_str() {
            Some("task_started") => {
                if let Some(description) = event["description"].as_str() {
                    self.tasks.insert(id.to_string(), description.to_string());
                }
                false
            }
            Some("task_updated") => event["patch"]["status"] == "killed",
            Some("task_notification") => event["status"] == "stopped",
            _ => false,
        };
        if killed && !self.killed.iter().any(|(killed_id, _)| killed_id == id) {
            let description = self
                .tasks
                .get(id)
                .map(String::as_str)
                .or(event["summary"].as_str())
                .unwrap_or(id)
                .to_string();
            self.killed.push((id.to_string(), description));
        }
    }
}

impl ClaudeApiError {
    fn read(event: &Value) -> Self {
        let text: Vec<_> = event["message"]["content"]
            .as_array()
            .into_iter()
            .flatten()
            .filter(|block| block["type"] == "text")
            .filter_map(|block| nonempty_text(&block["text"]))
            .collect();
        Self {
            message: (!text.is_empty()).then(|| text.join("\n")),
            facts: Self::facts(event),
        }
    }

    fn facts(event: &Value) -> BTreeMap<&'static str, String> {
        let mut facts = BTreeMap::new();
        if let Some(kind) = nonempty_text(&event["api_error"]) {
            facts.insert("api_error", kind.to_string());
        }
        if let Some(status) = event["api_error_status"].as_u64() {
            facts.insert("api_error_status", status.to_string());
        }
        let limit = &event["api_error_params"]["rate_limit_info"];
        for key in [
            "status",
            "rateLimitType",
            "overageStatus",
            "overageDisabledReason",
        ] {
            if let Some(text) = nonempty_text(&limit[key]) {
                facts.insert(key, text.to_string());
            }
        }
        for key in ["resetsAt", "overageResetsAt"] {
            if let Some(timestamp) = limit[key].as_u64() {
                facts.insert(key, timestamp.to_string());
            }
        }
        facts
    }

    fn diagnostic(self, message: Option<String>) -> Option<String> {
        let message = message.or(self.message);
        if self.facts.is_empty() {
            return message;
        }
        let facts = self
            .facts
            .into_iter()
            .map(|(key, value)| format!("{key}: {value}"))
            .collect::<Vec<_>>()
            .join("; ");
        Some(format!(
            "{}\n{facts}",
            message.as_deref().unwrap_or("Claude's turn failed")
        ))
    }
}

fn nonempty_text(value: &Value) -> Option<&str> {
    value.as_str().filter(|text| !text.trim().is_empty())
}

impl Grok {
    fn result(&mut self, event: &Value, failed: bool) {
        self.failed = failed;
        // Zero is a placeholder for unknown cost in Messages streams.
        self.cost_known = event["total_cost_usd"]
            .as_f64()
            .is_some_and(|cost| cost > 0.0);
        let errors: Vec<_> = event["errors"]
            .as_array()
            .into_iter()
            .flatten()
            .filter_map(Value::as_str)
            .collect();
        if !errors.is_empty() {
            self.diagnostic = Some(errors.join("\n"));
        } else if failed
            && let Some(text) = event["result"].as_str().filter(|text| !text.is_empty())
        {
            self.diagnostic = Some(text.to_string());
        }
        self.usage = match (
            event["usage"]["input_tokens"].as_u64(),
            event["usage"]["output_tokens"].as_u64(),
        ) {
            (Some(input), Some(output)) => {
                let cached = event["usage"]["cache_read_input_tokens"]
                    .as_u64()
                    .unwrap_or(0);
                let created = event["usage"]["cache_creation_input_tokens"]
                    .as_u64()
                    .unwrap_or(0);
                Some(format!(
                    "{input} input tokens, {cached} cache read tokens, {created} cache creation tokens, {output} output tokens"
                ))
            }
            _ => None,
        };
    }
}

#[cfg(test)]
mod tests;