Skip to main content

cpa_exec/
devin.rs

1//! Devin executor (internal/runtime/executor/devin_executor.go).
2//!
3//! Every client format is translated to Gemini Interactions, parsed into Devin's chat
4//! model (system prompt, history prompts, tools), and sent as one Connect-RPC
5//! `GetChatMessage` protobuf to the Codeium server. The streamed Connect frames
6//! (text, thinking, signatures, tool-call deltas, usage, an EOS trailer) become
7//! Interactions events, which the registered Interactions translator turns into the
8//! client's format; non-streaming clients get one Interactions response built from
9//! all frames.
10//!
11//! Credentials are permanent session tokens; "refresh" only re-reads seat, plan and
12//! quota from `GetUserStatus` (and only when `refresh_interval` asks for it, as Go's
13//! nil refresh lead implies).
14//!
15//! Request logging records Go's readable log bodies instead of the protobuf
16//! (`devin_wire::request_log_body`, `response_log_body` and the stream summary).
17
18use std::collections::{BTreeMap, HashMap, VecDeque};
19use std::sync::{Arc, Mutex};
20use std::time::Duration;
21
22use bytes::Bytes;
23use cpa_common::json::{self as gj};
24use cpa_common::thinking::parse_suffix;
25use cpa_core::config::Config;
26use cpa_core::credential::{Credential, MetadataPatch};
27use cpa_core::exec::{ExecError, ExecRequest, ExecResponse, ExecStream, FailureScope, Operation, ResponseBody};
28use cpa_core::format::Format;
29use cpa_translate::{ResponseCtx, StreamTranslator};
30use futures_util::StreamExt;
31use serde_json::Value;
32
33use crate::codex_quota::Snapshot;
34use crate::devin_auth::DevinAuth;
35use crate::devin_models::resolve_chat_model_uid;
36use crate::devin_request::parse_interactions;
37use crate::devin_wire::{
38    CHAT_PATH, ChatRequest, DEFAULT_BASE_URL, FLAG_END_STREAM, FrameError, FrameReader, ResponseLog, SensitiveWords,
39    ToolCall, ToolCallDelta, Usage, Utf8Split, build_chat_request, parse_dimension_groups, parse_frame,
40    parse_trailer_error, response_log_body, sentry_trace, wrap_envelope,
41};
42use crate::gemini_stream::ClaudeInputTokens;
43use crate::kimi_http::{capture_chunk, capture_error, capture_metadata, capture_request};
44use crate::openai_compat_payload::ensure_responses_usage_details;
45use crate::proxy::{GoClients, GoHeaders, Proxy, default_client, read_all, send};
46
47/// Provider string served by this executor.
48pub const PROVIDER: &str = "devin";
49/// `maxDevinToolCalls`.
50const MAX_TOOL_CALLS: usize = 128;
51/// Upstream error bodies are read up to 1 MiB.
52const ERROR_BODY_LIMIT: usize = 1 << 20;
53
54pub struct DevinExecutor {
55    clients: GoClients,
56    matcher: Mutex<Option<(String, Option<Arc<SensitiveWords>>)>>,
57    /// Go's runtime-only `Quota.Signals` and `LastRefreshedAt`, per credential.
58    runtime: Mutex<HashMap<String, Runtime>>,
59}
60
61#[derive(Debug, Clone, Default)]
62struct Runtime {
63    signals: BTreeMap<String, String>,
64    observed_at: Option<std::time::SystemTime>,
65    refreshed_at: Option<chrono::DateTime<chrono::Utc>>,
66}
67
68impl Default for DevinExecutor {
69    fn default() -> Self {
70        Self::with_client(default_client())
71    }
72}
73
74/// `devinAuthCredentials`: session token, base URL and device seed. Attributes win
75/// over metadata; a metadata base URL applies only while the base is still the default.
76fn creds(c: &Credential) -> (String, String, String) {
77    let attr = |k: &str| c.attributes.get(k).map(|v| v.trim().to_owned()).unwrap_or_default();
78    let meta = |k: &str| c.str(k).map(|v| v.trim().to_owned()).unwrap_or_default();
79    let mut key = String::new();
80    for k in ["api_key", "session_token", "token"] {
81        if key.is_empty() {
82            key = attr(k);
83        }
84    }
85    let mut base = DEFAULT_BASE_URL.to_owned();
86    if !attr("base_url").is_empty() {
87        base = attr("base_url");
88    }
89    let mut seed = attr("device_seed");
90    for k in ["api_key", "session_token"] {
91        if key.is_empty() {
92            key = meta(k);
93        }
94    }
95    if base == DEFAULT_BASE_URL && !meta("base_url").is_empty() {
96        base = meta("base_url");
97    }
98    if seed.is_empty() {
99        seed = meta("device_seed");
100    }
101    (key, base, seed)
102}
103
104/// `normalizeDevinUUID`: a UUID passes through, anything else becomes its UUIDv5 in
105/// the OID namespace, and nothing becomes a random UUID.
106pub(crate) fn normalize_uuid(raw: &str) -> String {
107    let raw = raw.trim();
108    if raw.is_empty() {
109        return uuid::Uuid::new_v4().to_string();
110    }
111    if go_uuid_parses(raw) {
112        return raw.to_owned();
113    }
114    uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_OID, raw.as_bytes()).to_string()
115}
116
117/// Go `uuid.Parse` acceptance: 36-character hyphenated, `{...}`, `urn:uuid:...`
118/// (case-insensitive prefix) or 32 hex digits.
119fn go_uuid_parses(s: &str) -> bool {
120    let b = s.as_bytes();
121    let hyphenated = |b: &[u8]| {
122        b.len() == 36
123            && b.iter().enumerate().all(|(i, c)| {
124                if matches!(i, 8 | 13 | 18 | 23) {
125                    *c == b'-'
126                } else {
127                    c.is_ascii_hexdigit()
128                }
129            })
130    };
131    match b.len() {
132        36 => hyphenated(b),
133        38 => b[0] == b'{' && b[37] == b'}' && hyphenated(&b[1..37]),
134        45 => b[..9].eq_ignore_ascii_case(b"urn:uuid:") && hyphenated(&b[9..]),
135        32 => b.iter().all(u8::is_ascii_hexdigit),
136        _ => false,
137    }
138}
139
140/// Go's statusErr scope conventions: a 429 cools the model, auth and server faults the
141/// credential, anything else is the request's.
142fn scope(status: u16) -> FailureScope {
143    match status {
144        429 => FailureScope::Model,
145        401 | 402 | 403 | 408 | 500.. => FailureScope::Credential,
146        _ => FailureScope::Request,
147    }
148}
149
150fn status_error(status: u16, message: impl Into<String>) -> ExecError {
151    ExecError::local(status, scope(status), message)
152}
153
154/// A plain Go error (no status): the handler answers 500.
155fn plain_error(message: impl Into<String>) -> ExecError {
156    ExecError::local(500, FailureScope::Transport, message)
157}
158
159fn apply_patch_error() -> ExecError {
160    ExecError::local(502, FailureScope::Request, cpa_translate::APPLY_PATCH_UPSTREAM_ERROR)
161}
162
163/// `newDevinStatusError`: the upstream body, with a 429's `Retry-After` (seconds or an
164/// HTTP date) as the cooldown hint.
165fn upstream_error(status: u16, headers: http::HeaderMap, body: Bytes) -> ExecError {
166    let mut retry_after = None;
167    if status == 429
168        && let Some(raw) = headers
169            .get(http::header::RETRY_AFTER)
170            .and_then(|v| v.to_str().ok())
171            .map(str::trim)
172            .filter(|v| !v.is_empty())
173    {
174        if let Ok(seconds) = raw.parse::<i64>() {
175            if seconds >= 0 {
176                retry_after = Some(Duration::from_secs(seconds as u64));
177            }
178        } else if let Ok(at) = httpdate::parse_http_date(raw) {
179            retry_after = at
180                .duration_since(std::time::SystemTime::now())
181                .ok()
182                .filter(|d| !d.is_zero());
183        }
184    }
185    ExecError {
186        status,
187        scope: scope(status),
188        body,
189        headers: Box::new(headers),
190        retry_after,
191        direct: false,
192        addon: None,
193    }
194}
195
196/// `opts.OriginalRequest`, falling back to the payload (`ApplyPatchOriginalRequest`).
197fn original(req: &ExecRequest) -> &Bytes {
198    if req.original_body.is_empty() {
199        &req.body
200    } else {
201        &req.original_body
202    }
203}
204
205/// ponytail: adapter for `helps.ApplyPatchRequested` / `IsApplyPatchUpstreamTool`
206/// (owner: translators; cpa-translate's `reverse_identity_map` is crate-private).
207/// Minimal: a top-level custom `apply_patch` tool in the original Responses request (or
208/// its `request` wrapper); Go's full resolver also maps namespaced and sanitized names.
209fn apply_patch_tool_names(original: &[u8]) -> Vec<Vec<u8>> {
210    if original.is_empty() || !gj::valid(original) {
211        return Vec::new();
212    }
213    let mut root = gj::parse(original);
214    let req = root.get("request");
215    if req.exists() && (req.get("model").exists() || req.get("input").exists() || req.get("tools").exists()) {
216        root = req;
217    }
218    let tools = root.get("tools");
219    if !tools.is_array() {
220        return Vec::new();
221    }
222    tools
223        .array()
224        .iter()
225        .filter(|t| {
226            *t.get("type").bytes() == *b"custom"
227                && cpa_common::gostr::trim_space(&t.get("name").bytes()) == b"apply_patch"
228        })
229        .map(|_| b"apply_patch".to_vec())
230        .collect()
231}
232
233fn apply_patch_requested(original: &[u8]) -> bool {
234    !apply_patch_tool_names(original).is_empty()
235}
236
237fn is_apply_patch_upstream_tool(original: &[u8], name: &[u8]) -> bool {
238    apply_patch_tool_names(original).iter().any(|n| n == name)
239}
240
241struct Prepared {
242    url: String,
243    headers: GoHeaders,
244    body: Bytes,
245    /// The chat model UID (Go `SetUpstreamModel` when non-empty).
246    model_uid: String,
247    /// The payload rules' JSON view of the protobuf, which request logging records
248    /// instead of it (`FinalizeDevinPayload`).
249    log_body: Vec<u8>,
250    /// `devinAuthLogFields`' value: the key's first and last four bytes.
251    auth_value: String,
252}
253
254/// `devinAuthLogFields`' value for a session key: its first and last four bytes, `***`
255/// for a short key, empty without one.
256fn auth_log_value(key: &str) -> String {
257    match key.len() {
258        0 => String::new(),
259        1..=8 => "***".into(),
260        n => {
261            let b = key.as_bytes();
262            format!(
263                "{}...{}",
264                String::from_utf8_lossy(&b[..4]),
265                String::from_utf8_lossy(&b[n - 4..])
266            )
267        }
268    }
269}
270
271impl DevinExecutor {
272    /// Uses a caller-built client (tests point it at local mocks).
273    pub fn with_client(client: wreq::Client) -> Self {
274        Self {
275            clients: GoClients::with_default(client),
276            matcher: Mutex::new(None),
277            runtime: Mutex::new(HashMap::new()),
278        }
279    }
280
281    /// `getSensitiveWordMatcher`: `devin.sensitive-words` (v8
282    /// `oauth.providers.devin.sensitive-words`), compiled once per word list.
283    fn matcher(&self, cfg: &Config) -> Option<Arc<SensitiveWords>> {
284        let words: Vec<String> = cfg
285            .document
286            .get("oauth")
287            .and_then(|o| o.get("providers"))
288            .and_then(|p| p.get("devin"))
289            .and_then(|d| d.get("sensitive-words"))
290            .and_then(|w| w.as_sequence())
291            .map(|seq| seq.iter().filter_map(|v| v.as_str().map(str::to_owned)).collect())
292            .unwrap_or_default();
293        if words.is_empty() {
294            return None;
295        }
296        let key = words.join("\0");
297        let mut cached = self.matcher.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
298        if let Some((k, m)) = cached.as_ref()
299            && *k == key
300        {
301            return m.clone();
302        }
303        let built = SensitiveWords::new(&words).map(Arc::new);
304        *cached = Some((key, built.clone()));
305        built
306    }
307
308    /// Go's refresh scheduling for Devin: no SDK lead, so only a `refresh_interval`
309    /// (or an expiry inside it) makes the background loop re-read the user status.
310    pub fn needs_prepare(&self, credential: &Credential, cfg: &Config) -> bool {
311        self.needs_prepare_at(credential, cfg, chrono::Utc::now())
312    }
313
314    pub fn needs_prepare_at(&self, credential: &Credential, _cfg: &Config, now: chrono::DateTime<chrono::Utc>) -> bool {
315        // Go's refresh loop never schedules API-key-kind credentials.
316        if creds(credential).0.is_empty() || cpa_core::registry::dynamic::is_api_key(credential) {
317            return false;
318        }
319        let Some(interval) = crate::kimi_http::preferred_interval(credential) else {
320            return false;
321        };
322        if let Some(expiry) = crate::kimi_http::expiration(credential)
323            && (expiry <= now || expiry - now <= interval)
324        {
325            return true;
326        }
327        let refreshed = self
328            .runtime
329            .lock()
330            .unwrap_or_else(std::sync::PoisonError::into_inner)
331            .get(&credential.id)
332            .and_then(|r| r.refreshed_at);
333        let last = match (crate::kimi_http::last_refresh(credential), refreshed) {
334            (Some(a), Some(b)) => Some(a.max(b)),
335            (a, b) => a.or(b),
336        };
337        last.is_none_or(|last| now - last >= interval)
338    }
339
340    /// `Refresh`: user status, plan and quota from `GetUserStatus`. Returns the account
341    /// fields to persist; quota signals stay in memory ([`Self::quota`]).
342    pub async fn prepare(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
343        let (token, base, seed) = creds(credential);
344        if token.is_empty() {
345            return Ok(MetadataPatch::default());
346        }
347        let client = self.clients.get(&Proxy::effective(credential, cfg));
348        let auth = DevinAuth::new(client).with_devin_transport();
349        let auth = if base.is_empty() {
350            auth
351        } else {
352            auth.with_server_base(&base)
353        };
354        let status = auth.fetch_user_status(&token, &seed).await.map_err(|e| {
355            tracing::warn!("devin executor: failed to refresh user status for {}", credential.id);
356            ExecError::local(500, FailureScope::Credential, e)
357        })?;
358        let mut patch = MetadataPatch::default();
359        for (key, value) in [
360            ("email", &status.email),
361            ("user_name", &status.user_name),
362            ("user_id", &status.user_id),
363            ("team_id", &status.team_id),
364            ("plan", &status.plan),
365            ("org_id", &status.org_id),
366            ("org_name", &status.org_name),
367        ] {
368            if !value.is_empty() {
369                patch.set.insert(key.into(), Value::String(value.clone()));
370            }
371        }
372        let mut runtime = self.runtime.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
373        let entry = runtime.entry(credential.id.clone()).or_default();
374        for (key, value) in status.quota_signals() {
375            entry.signals.insert(key.into(), value);
376        }
377        entry.observed_at = Some(std::time::SystemTime::now());
378        entry.refreshed_at = Some(chrono::Utc::now());
379        Ok(patch)
380    }
381
382    /// The credential's quota observation (plan, daily and weekly remaining, resets)
383    /// from its last refresh.
384    pub fn quota(&self, credential_id: &str) -> Option<Snapshot> {
385        let runtime = self.runtime.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
386        let r = runtime.get(credential_id)?;
387        Some(Snapshot {
388            observed_at: r.observed_at?,
389            signals: r.signals.clone(),
390        })
391    }
392
393    /// Seeds a credential's quota observation (Go's login record carries one).
394    pub fn observe_quota(&self, credential_id: &str, signals: BTreeMap<String, String>) {
395        let mut runtime = self.runtime.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
396        let entry = runtime.entry(credential_id.to_owned()).or_default();
397        entry.signals = signals;
398        entry.observed_at = Some(std::time::SystemTime::now());
399    }
400
401    /// `prepareDevinHTTPRequest`.
402    fn prepare_request(&self, credential: &Credential, req: &ExecRequest, cfg: &Config) -> Result<Prepared, ExecError> {
403        let (key, base, seed) = creds(credential);
404        if key.is_empty() {
405            const MISSING: &str = "devin credentials missing: api_key or session_token required";
406            // Go's error is plain: the usage record carries no status.
407            req.usage.publish_failure(0, MISSING);
408            return Err(ExecError::local(500, FailureScope::Credential, MISSING));
409        }
410        let payload = if req.source_format == Format::Interactions {
411            req.body.to_vec()
412        } else {
413            cpa_translate::translate_request(
414                req.source_format,
415                Format::Interactions,
416                &cpa_translate::RequestCtx {
417                    model: &req.model,
418                    stream: req.stream,
419                    tool_search: false,
420                },
421                &req.body,
422            )
423            .map_err(|e| ExecError::local(400, FailureScope::Request, e.0))?
424        };
425        let mut parsed = parse_interactions(&payload, &req.original_body);
426        // Devin cannot fetch a remote uri or take audio, video or documents: a user turn
427        // left with nothing else to send is refused before any upstream call.
428        parsed.prompts = crate::devin_request::check_user_turns(std::mem::take(&mut parsed.prompts))
429            .map_err(|e| ExecError::local(400, FailureScope::Request, e.0))?;
430        let session = String::from_utf8_lossy(&parsed.session_id).into_owned();
431        let session = if session.is_empty() {
432            cpa_common::session::cpa_session_id(req.session.as_deref()).unwrap_or_default()
433        } else {
434            session
435        };
436        let session_id = normalize_uuid(&session);
437        let cascade = String::from_utf8_lossy(&parsed.cascade_id).into_owned();
438        let cascade_id = if cascade.is_empty() {
439            session_id.clone()
440        } else {
441            normalize_uuid(&cascade)
442        };
443        let mut max_tokens = parsed.max_tokens;
444        let base_model = parse_suffix(&req.model).model_name;
445        if let Some(info) = cpa_core::registry::lookup_model(&base_model, Some(PROVIDER)) {
446            let limit = info
447                .raw
448                .get("max_completion_tokens")
449                .and_then(Value::as_i64)
450                .unwrap_or(0);
451            if limit > 0 && (max_tokens > limit || max_tokens <= 0) {
452                max_tokens = limit;
453            }
454        }
455        let model_uid = resolve_chat_model_uid(&req.model, &parsed.thinking_level, parsed.budget);
456        let matcher = self.matcher(cfg);
457        let proto = build_chat_request(&ChatRequest {
458            session_token: &key,
459            device_seed: &seed,
460            model_uid: &model_uid,
461            system_prompt: &parsed.system,
462            prompts: &parsed.prompts,
463            tools: &parsed.tools,
464            temperature: parsed.temperature,
465            max_tokens,
466            session_id: &session_id,
467            cascade_id: &cascade_id,
468            matcher: matcher.as_deref(),
469        });
470        // 868e137: the payload rules edit the protobuf's business fields last, and Go logs
471        // that JSON view in place of the protobuf.
472        let (proto, log_body) = crate::devin_payload::finalize(&proto, |view| {
473            let original = crate::devin_payload::defaults_source(view, &payload);
474            crate::payload_barrier::Barrier {
475                executor: PROVIDER,
476                model: &base_model,
477                protocol: "devin",
478                root: "",
479                original: &original,
480            }
481            .apply(cfg, req, view.to_vec())
482        })
483        .map_err(|message| {
484            req.usage.publish_failure(0, &message);
485            plain_error(message)
486        })?;
487        Ok(Prepared {
488            url: format!("{}{CHAT_PATH}", base.trim_end_matches('/')),
489            headers: headers(credential, req, &key),
490            body: Bytes::from(wrap_envelope(&proto)),
491            model_uid,
492            log_body,
493            auth_value: auth_log_value(&key),
494        })
495    }
496
497    pub async fn execute(
498        &self,
499        credential: &Credential,
500        req: ExecRequest,
501        cfg: &Config,
502    ) -> Result<ExecResponse, ExecError> {
503        if req.operation == Operation::CountTokens {
504            // Devin has no token counting endpoint: a length estimate.
505            let n = req.body.len() / 4;
506            return Ok(ExecResponse {
507                status: 200,
508                headers: http::HeaderMap::new(),
509                body: ResponseBody::Buffered(Bytes::from(format!(r#"{{"total_tokens":{n},"input_tokens":{n}}}"#))),
510            });
511        }
512        let prepared = self.prepare_request(credential, &req, cfg)?;
513        if !prepared.model_uid.is_empty() {
514            req.usage.upstream_model(&prepared.model_uid);
515        }
516        let client = self.clients.get(&Proxy::effective(credential, cfg));
517        let capture = req.capture().clone();
518        capture_request(
519            &capture,
520            credential,
521            &prepared.url,
522            &prepared.headers,
523            &prepared.log_body,
524            PROVIDER,
525            ("devin", &prepared.auth_value),
526        );
527        req.usage.round_trip_started();
528        let mut upstream = send(&client, &prepared.url, prepared.headers, prepared.body, None)
529            .await
530            .inspect_err(|e| capture_error(&capture, e))?;
531        capture_metadata(&capture, upstream.status, &upstream.headers);
532        upstream.body = crate::kimi_http::track_first_byte(upstream.body, &req.usage, false);
533        if !(200..300).contains(&upstream.status) {
534            let headers = upstream.headers.clone();
535            let body = read_all(upstream.body, ERROR_BODY_LIMIT, true)
536                .await
537                .unwrap_or_default();
538            capture_chunk(&capture, &body);
539            return Err(upstream_error(upstream.status, headers, body));
540        }
541        let reader = FrameReader::new(upstream.body);
542        let ctx_original = original(&req).clone();
543        let response_format = req.response_format;
544        let pair = cpa_translate::pair(response_format, Format::Interactions);
545        if pair.is_none() && response_format != Format::Interactions {
546            return Err(ExecError::local(
547                501,
548                FailureScope::Request,
549                "Devin response translation pair is not registered",
550            ));
551        }
552        let ctx = ResponseCtx {
553            model: &req.model,
554            original_request: &ctx_original,
555            translated_request: &req.body,
556            tool_search: false,
557        };
558        if req.stream {
559            let translator = pair.map(|pair| crate::translate::stream(pair, &ctx, &req));
560            let claude = ClaudeInputTokens::new(
561                req.source_format,
562                Format::Interactions,
563                response_format,
564                req.original_body.clone(),
565            );
566            let stream = DevinStream::new(
567                req.model.clone(),
568                response_format,
569                translator,
570                claude,
571                reader,
572                req.usage.clone(),
573                capture,
574            );
575            return Ok(ExecResponse {
576                status: upstream.status,
577                headers: upstream.headers,
578                body: ResponseBody::Stream(stream.run()),
579            });
580        }
581        let (consumed, log) = consume_frames(reader, &req.model, &ctx_original).await;
582        let built = consumed.as_ref().map(|(json, _)| json.as_slice()).unwrap_or_default();
583        if capture.logs_responses() && (log.is_some() || !built.is_empty()) {
584            capture_chunk(&capture, &response_log_body(log.as_ref(), built));
585        }
586        let interactions = match consumed {
587            Ok((json, usage_model)) => {
588                if !usage_model.is_empty() {
589                    req.usage.response_model(&String::from_utf8_lossy(&usage_model));
590                }
591                json
592            }
593            Err(_) if apply_patch_requested(&ctx_original) => return Err(apply_patch_error()),
594            Err(e) => {
595                capture_error(&capture, &e);
596                return Err(e);
597            }
598        };
599        let out = match pair {
600            Some(pair) => (pair.non_stream)(&ctx, &interactions)
601                .ok()
602                .filter(|out| !out.is_empty())
603                .ok_or_else(apply_patch_error)?,
604            None => interactions.clone(),
605        };
606        if req.usage.enabled() {
607            req.usage
608                .response_body(Format::Interactions, &usage_payload(&interactions, "model"));
609        }
610        let out = if response_format == Format::OpenAIResponse {
611            ensure_responses_usage_details(&out)
612        } else {
613            out
614        };
615        Ok(ExecResponse {
616            status: upstream.status,
617            headers: upstream.headers,
618            body: ResponseBody::Buffered(Bytes::from(out)),
619        })
620    }
621}
622
623/// `PrepareRequest`: Connect headers, `Basic <token>-<token>`, a Sentry trace on chat
624/// calls, no User-Agent and no automatic gzip; credential custom headers last.
625fn headers(credential: &Credential, req: &ExecRequest, key: &str) -> GoHeaders {
626    let mut h = GoHeaders::new();
627    h.disable_compression();
628    if !key.is_empty() {
629        h.set("Authorization", format!("Basic {key}-{key}"));
630    }
631    h.set("Content-Type", "application/connect+proto");
632    h.set("Connect-Protocol-Version", "1");
633    h.set("Accept", "*/*");
634    h.set("Sentry-Trace", sentry_trace());
635    h.set("User-Agent", "");
636    for (name, value) in crate::kimi_http::credential_headers(credential, req, original(req)) {
637        h.set(&name, value);
638    }
639    h
640}
641
642/// The Interactions payload Go parses usage from, without the client-facing model (Go
643/// sets the response model only from upstream usage).
644fn usage_payload(payload: &[u8], model_path: &str) -> Vec<u8> {
645    let mut out = payload.to_vec();
646    gj::delete(&mut out, model_path);
647    out
648}
649
650fn interaction_id() -> String {
651    let id = uuid::Uuid::new_v4().to_string();
652    format!("interaction_{}", &id[..12])
653}
654
655/// Merges one frame's usage and dimension-group counts into the running total.
656fn merge_usage(total: &mut Option<Usage>, frame_usage: Option<&Usage>, groups: &[Vec<u8>]) {
657    if let Some(u) = frame_usage {
658        match total {
659            None => *total = Some(u.clone()),
660            Some(t) => {
661                if u.prompt_tokens > 0 {
662                    t.prompt_tokens = u.prompt_tokens;
663                }
664                if u.completion_tokens > 0 {
665                    t.completion_tokens = u.completion_tokens;
666                }
667                if u.cached_tokens > 0 {
668                    t.cached_tokens = u.cached_tokens;
669                }
670                if u.cache_write_tokens > 0 {
671                    t.cache_write_tokens = u.cache_write_tokens;
672                }
673                if !u.request_id.is_empty() {
674                    t.request_id.clone_from(&u.request_id);
675                }
676                if !u.model_name.is_empty() {
677                    t.model_name.clone_from(&u.model_name);
678                }
679                for (k, v) in &u.headers {
680                    t.headers.insert(k.clone(), v.clone());
681                }
682            }
683        }
684    }
685    let incomplete = total
686        .as_ref()
687        .is_none_or(|t| t.prompt_tokens == 0 || t.completion_tokens == 0 || t.cached_tokens == 0);
688    if !groups.is_empty()
689        && incomplete
690        && let Some((input, output, cached)) = parse_dimension_groups(groups)
691    {
692        let t = total.get_or_insert_with(Usage::default);
693        if t.prompt_tokens == 0 {
694            t.prompt_tokens = input;
695        }
696        if t.completion_tokens == 0 {
697            t.completion_tokens = output;
698        }
699        if t.cached_tokens == 0 {
700            t.cached_tokens = cached;
701        }
702    }
703}
704
705/// Status and finish reason for the last stop reason (1 and 3 are length, 11 is the
706/// content filter).
707fn completion_status(stop_reason: u64) -> (&'static str, &'static str) {
708    match stop_reason {
709        1 | 3 => ("incomplete", "length"),
710        11 => ("incomplete", "content_filter"),
711        _ => ("completed", ""),
712    }
713}
714
715/// Writes the usage fields the completed interaction carries under `prefix`.
716fn set_usage(out: &mut Vec<u8>, prefix: &str, usage: &Usage) {
717    let input = usage.prompt_tokens.wrapping_add(usage.cached_tokens);
718    gj::set_int(out, &format!("{prefix}.total_input_tokens"), input);
719    gj::set_int(out, &format!("{prefix}.total_output_tokens"), usage.completion_tokens);
720    gj::set_int(out, &format!("{prefix}.total_cached_tokens"), usage.cached_tokens);
721    if usage.cache_write_tokens > 0 {
722        gj::set_int(out, &format!("{prefix}.cache_write_tokens"), usage.cache_write_tokens);
723    }
724    gj::set_int(
725        out,
726        &format!("{prefix}.total_tokens"),
727        input.wrapping_add(usage.completion_tokens),
728    );
729}
730
731#[derive(Default)]
732struct ToolBuilder {
733    legacy: bool,
734    id: Vec<u8>,
735    name: Vec<u8>,
736    args: Vec<u8>,
737}
738
739/// `consumeDevinFramesToInteractions`: one Interactions response from every frame.
740/// Also returns the upstream usage model name (empty when none was reported).
741async fn consume_frames(
742    mut reader: FrameReader,
743    model: &str,
744    original: &[u8],
745) -> (Result<(Vec<u8>, Vec<u8>), ExecError>, Option<ResponseLog>) {
746    let (mut pre_tool, mut post_tool, mut thinking) = (Vec::new(), Vec::new(), Vec::new());
747    let (mut has_pre, mut has_post, mut has_thinking) = (false, false, false);
748    let mut builders: Vec<ToolBuilder> = Vec::new();
749    let mut by_id: HashMap<Vec<u8>, usize> = HashMap::new();
750    let mut last: Option<usize> = None;
751    let mut usage: Option<Usage> = None;
752    let mut signature: Vec<u8> = Vec::new();
753    let mut signature_type: Vec<u8> = Vec::new();
754    let mut unknown_fields: Vec<i64> = Vec::new();
755    let mut frames = 0;
756    let mut stop_reason = 0;
757    let mut saw_eos = false;
758    // Go's respLog for the frames read so far.
759    let log = |status: String,
760               builders: &[ToolBuilder],
761               usage: &Option<Usage>,
762               signature: &[u8],
763               signature_type: &[u8],
764               unknown_fields: &[i64],
765               frames: usize,
766               content: Vec<u8>,
767               thinking: &[u8]| {
768        Some(ResponseLog {
769            status,
770            frames,
771            content,
772            thinking: thinking.to_vec(),
773            signature: signature.to_vec(),
774            signature_type: signature_type.to_vec(),
775            tool_calls: builders
776                .iter()
777                .filter(|b| !(b.id.is_empty() && b.name.is_empty() && b.args.is_empty()))
778                .map(|b| ToolCall {
779                    id: b.id.clone(),
780                    name: b.name.clone(),
781                    arguments: b.args.clone(),
782                })
783                .collect(),
784            usage: usage.clone(),
785            unknown_fields: unknown_fields.to_vec(),
786        })
787    };
788    macro_rules! log_now {
789        ($status:expr) => {
790            log(
791                $status,
792                &builders,
793                &usage,
794                &signature,
795                &signature_type,
796                &unknown_fields,
797                frames,
798                [pre_tool.as_slice(), post_tool.as_slice()].concat(),
799                &thinking,
800            )
801        };
802    }
803    loop {
804        let (flag, payload) = match reader.next().await {
805            Ok(frame) => frame,
806            Err(FrameError::Eof) => break,
807            Err(FrameError::Failed(message)) => {
808                return (
809                    Err(plain_error(message.clone())),
810                    log_now!(format!("read_error: {message}")),
811                );
812            }
813        };
814        frames += 1;
815        if flag & FLAG_END_STREAM != 0 {
816            if let Some((code, message)) = parse_trailer_error(&payload) {
817                let status = format!("trailer_error({code}): {message}");
818                return (Err(status_error(code, message)), log_now!(status));
819            }
820            saw_eos = true;
821            break;
822        }
823        let (frame, error) = parse_frame(&payload);
824        if error.is_some() {
825            continue;
826        }
827        if frame.stop_reason != 0 {
828            stop_reason = frame.stop_reason;
829        }
830        for field in &frame.unknown_fields {
831            if !unknown_fields.contains(field) {
832                unknown_fields.push(*field);
833            }
834        }
835        merge_usage(&mut usage, frame.usage.as_ref(), &frame.dimension_groups);
836        signature.extend_from_slice(&frame.signature);
837        if !frame.signature_type.is_empty() {
838            signature_type.clone_from(&frame.signature_type);
839        }
840        if !frame.thinking.is_empty() {
841            thinking.extend_from_slice(&frame.thinking);
842            has_thinking = true;
843        }
844        for tc in &frame.tool_calls {
845            let chunk = if tc.arguments.is_empty() {
846                &tc.invalid_json
847            } else {
848                &tc.arguments
849            };
850            let existing = if tc.id.is_empty() {
851                last
852            } else {
853                by_id.get(&tc.id).copied()
854            };
855            let index = match existing {
856                None => {
857                    if builders.len() >= MAX_TOOL_CALLS {
858                        tracing::warn!("devin executor: total tool calls exceeded max {MAX_TOOL_CALLS}, dropping");
859                        continue;
860                    }
861                    builders.push(ToolBuilder {
862                        id: tc.id.clone(),
863                        name: tc.name.clone(),
864                        ..ToolBuilder::default()
865                    });
866                    let index = builders.len() - 1;
867                    if !tc.id.is_empty() {
868                        by_id.insert(tc.id.clone(), index);
869                    }
870                    last = Some(index);
871                    index
872                }
873                Some(index) => {
874                    last = Some(index);
875                    let b = &mut builders[index];
876                    if b.id.is_empty() && !tc.id.is_empty() {
877                        b.id.clone_from(&tc.id);
878                        by_id.insert(tc.id.clone(), index);
879                    }
880                    if !tc.name.is_empty() {
881                        b.name.clone_from(&tc.name);
882                    }
883                    index
884                }
885            };
886            let b = &mut builders[index];
887            if tc.arguments.is_empty() && !tc.invalid_json.is_empty() {
888                b.legacy = true;
889            }
890            b.args.extend_from_slice(chunk);
891        }
892        if !frame.content.is_empty() {
893            if builders.is_empty() {
894                pre_tool.extend_from_slice(&frame.content);
895                has_pre = true;
896            } else {
897                post_tool.extend_from_slice(&frame.content);
898                has_post = true;
899            }
900        }
901    }
902    if builders
903        .iter()
904        .any(|b| b.legacy && is_apply_patch_upstream_tool(original, &b.name))
905    {
906        return (Err(apply_patch_error()), None);
907    }
908    if !saw_eos {
909        return (
910            Err(plain_error(
911                "devin upstream stream terminated prematurely before EOS trailer",
912            )),
913            log_now!("premature_eof_before_eos".into()),
914        );
915    }
916    let response_log = log_now!("completed".into());
917    let (status, finish_reason) = completion_status(stop_reason);
918    let mut out =
919        br#"{"id":"","model":"","status":"completed","steps":[],"usage":{"total_input_tokens":0,"total_output_tokens":0,"total_cached_tokens":0}}"#.to_vec();
920    gj::set_str(&mut out, "id", interaction_id());
921    gj::set_str(&mut out, "model", model);
922    gj::set_str(&mut out, "status", status);
923    if !finish_reason.is_empty() {
924        gj::set_str(&mut out, "finish_reason", finish_reason);
925    }
926    let mut steps: Vec<Vec<u8>> = Vec::new();
927    if has_thinking || !signature.is_empty() {
928        let mut step = br#"{"type":"thought","content":[{"type":"text","text":""}]}"#.to_vec();
929        if has_thinking {
930            gj::set_str(&mut step, "content.0.text", &thinking);
931        } else {
932            gj::delete(&mut step, "content");
933        }
934        if !signature.is_empty() {
935            gj::set_str(&mut step, "signature", &signature);
936            gj::set_str(&mut step, "thought_signature", &signature);
937        }
938        steps.push(step);
939    }
940    let text_step = |text: &[u8]| {
941        let mut step = br#"{"type":"model_output","content":[{"type":"text","text":""}]}"#.to_vec();
942        gj::set_str(&mut step, "content.0.text", text);
943        step
944    };
945    if has_pre {
946        steps.push(text_step(&pre_tool));
947    }
948    for b in builders
949        .iter()
950        .filter(|b| !(b.id.is_empty() && b.name.is_empty() && b.args.is_empty()))
951    {
952        let mut step = br#"{"type":"function_call","name":"","id":"","call_id":"","arguments":{}}"#.to_vec();
953        gj::set_str(&mut step, "name", &b.name);
954        gj::set_str(&mut step, "id", &b.id);
955        gj::set_str(&mut step, "call_id", &b.id);
956        if !b.args.is_empty() {
957            if gj::std_valid(&b.args) {
958                gj::set_raw(&mut step, "arguments", &b.args);
959            } else {
960                gj::set_str_no_html(&mut step, "arguments", &b.args);
961            }
962        }
963        steps.push(step);
964    }
965    if has_post {
966        steps.push(text_step(&post_tool));
967    }
968    if !steps.is_empty() {
969        gj::set_raw(&mut out, "steps", gj::join(&steps));
970    }
971    if let Some(u) = &usage {
972        set_usage(&mut out, "usage", u);
973    }
974    (Ok((out, usage.map(|u| u.model_name).unwrap_or_default())), response_log)
975}
976
977/// A tool call slot opened by the stream (`devinActiveToolSlot`).
978#[derive(Clone)]
979struct Slot {
980    step: i64,
981    id: Vec<u8>,
982    name: Vec<u8>,
983}
984
985/// Work deferred while a thought step is open (`pendingActions`).
986enum Pending {
987    Content(Vec<u8>),
988    Tool(ToolCallDelta),
989}
990
991/// `streamDevinFrames`: the Connect frame stream as Interactions events, translated for
992/// the client.
993struct DevinStream {
994    reader: Option<FrameReader>,
995    model: String,
996    response_format: Format,
997    translator: Option<Box<dyn StreamTranslator>>,
998    claude: ClaudeInputTokens,
999    out: VecDeque<Result<Bytes, ExecError>>,
1000    interaction_id: String,
1001    step_index: i64,
1002    thought_started: bool,
1003    content_started: bool,
1004    slots: BTreeMap<i64, Slot>,
1005    by_id: HashMap<Vec<u8>, i64>,
1006    active: Option<i64>,
1007    tool_calls: usize,
1008    thinking_buf: Utf8Split,
1009    content_buf: Utf8Split,
1010    usage: Option<Usage>,
1011    thought_step: i64,
1012    stop_reason: u64,
1013    pending: Vec<Pending>,
1014    post_tool: Vec<Vec<u8>>,
1015    created_sent: bool,
1016    translation_failed: bool,
1017    ended: bool,
1018    usage_sink: cpa_core::exec::UsageSink,
1019    /// Request logging (Go's `AppendAPIResponseChunk` and `RecordAPIResponseError`).
1020    capture: cpa_core::exec::CaptureSink,
1021    /// What Go's end-of-stream summary reports: frames read, thinking, content and the
1022    /// signature as received.
1023    summary: ResponseLog,
1024    /// Response logging was on when the stream started: only then are thinking, content
1025    /// and the signature copied into `summary`, which would otherwise hold the whole
1026    /// response for nothing.
1027    // ponytail: decided once per stream, so turning request-log on mid-stream logs no
1028    // summary (Go always buffers it); upgrade by re-checking per frame and marking a
1029    // partial summary.
1030    log_summary: bool,
1031    /// No interactions event has been logged yet.
1032    first_logged_event: bool,
1033}
1034
1035/// `{"event_type":"step.stop","index":N}`.
1036fn stop_event(index: i64) -> Vec<u8> {
1037    let mut e = br#"{"event_type":"step.stop","index":0}"#.to_vec();
1038    gj::set_int(&mut e, "index", index);
1039    e
1040}
1041
1042fn start_event(index: i64, step: &[u8]) -> Vec<u8> {
1043    let mut e = br#"{"event_type":"step.start","index":0,"step":{}}"#.to_vec();
1044    gj::set_int(&mut e, "index", index);
1045    gj::set_raw(&mut e, "step", step);
1046    e
1047}
1048
1049fn tool_start_event(index: i64, name: &[u8], id: &[u8]) -> Vec<u8> {
1050    let mut e = br#"{"event_type":"step.start","index":0,"step":{"type":"function_call","name":"","id":"","call_id":"","arguments":{}}}"#.to_vec();
1051    gj::set_int(&mut e, "index", index);
1052    gj::set_str(&mut e, "step.name", name);
1053    gj::set_str(&mut e, "step.id", id);
1054    gj::set_str(&mut e, "step.call_id", id);
1055    e
1056}
1057
1058fn text_delta_event(index: i64, text: &[u8]) -> Vec<u8> {
1059    let mut e = br#"{"event_type":"step.delta","index":0,"delta":{"type":"text","text":""}}"#.to_vec();
1060    gj::set_int(&mut e, "index", index);
1061    gj::set_str(&mut e, "delta.text", text);
1062    e
1063}
1064
1065impl DevinStream {
1066    fn new(
1067        model: String,
1068        response_format: Format,
1069        translator: Option<Box<dyn StreamTranslator>>,
1070        claude: ClaudeInputTokens,
1071        reader: FrameReader,
1072        usage_sink: cpa_core::exec::UsageSink,
1073        capture: cpa_core::exec::CaptureSink,
1074    ) -> Self {
1075        // Go's Devin reporter takes the response model only from upstream usage frames
1076        // (`response_model`); reporting this neutral line marks the record as
1077        // executor-fed, so the server never falls back to the client-format frames,
1078        // which carry the client's model.
1079        usage_sink.response_line(Format::Interactions, b"{}");
1080        Self {
1081            reader: Some(reader),
1082            model,
1083            response_format,
1084            translator,
1085            claude,
1086            out: VecDeque::new(),
1087            interaction_id: interaction_id(),
1088            step_index: 0,
1089            thought_started: false,
1090            content_started: false,
1091            slots: BTreeMap::new(),
1092            by_id: HashMap::new(),
1093            active: None,
1094            tool_calls: 0,
1095            thinking_buf: Utf8Split::default(),
1096            content_buf: Utf8Split::default(),
1097            usage: None,
1098            thought_step: -1,
1099            stop_reason: 0,
1100            pending: Vec::new(),
1101            post_tool: Vec::new(),
1102            created_sent: false,
1103            translation_failed: false,
1104            ended: false,
1105            usage_sink,
1106            log_summary: capture.logs_responses(),
1107            capture,
1108            summary: ResponseLog {
1109                status: "completed".into(),
1110                ..ResponseLog::default()
1111            },
1112            first_logged_event: true,
1113        }
1114    }
1115
1116    fn run(self) -> ExecStream {
1117        futures_util::stream::unfold(self, |mut st| async move {
1118            loop {
1119                if let Some(item) = st.out.pop_front() {
1120                    return Some((item, st));
1121                }
1122                if st.ended {
1123                    return None;
1124                }
1125                st.step().await;
1126            }
1127        })
1128        .boxed()
1129    }
1130
1131    /// Ends the stream with `error` after any client frames still pending.
1132    fn fail(&mut self, error: ExecError) {
1133        self.ended = true;
1134        if let Some(t) = self.translator.as_mut() {
1135            self.out.extend(t.flush_frames().into_iter().map(Ok));
1136        }
1137        self.out.push_back(Err(error));
1138    }
1139
1140    /// Client chunks for one translated batch: Responses usage details and the Claude
1141    /// input-token estimate, unless the translator rejected apply_patch input
1142    /// (`TranslateStreamWithClaudeInputTokens`).
1143    fn deliver(&mut self, mut chunks: Vec<Bytes>) {
1144        let failed = self.translator.as_ref().is_some_and(|t| t.tool_input_failed());
1145        if !failed {
1146            if self.response_format == Format::OpenAIResponse {
1147                for c in &mut chunks {
1148                    *c = Bytes::from(ensure_responses_usage_details(c));
1149                }
1150            }
1151            self.claude.apply(&mut chunks);
1152        }
1153        self.out.extend(chunks.into_iter().map(Ok));
1154    }
1155
1156    /// `emitInteractionsEvent`: false once the stream must stop.
1157    fn emit(&mut self, raw: Vec<u8>) -> bool {
1158        if self.translation_failed || self.ended {
1159            return false;
1160        }
1161        if raw.is_empty() {
1162            return true;
1163        }
1164        let trimmed = cpa_common::gostr::trim_space(&raw).to_vec();
1165        let kind = gj::get(&trimmed, "event_type").bytes().into_owned();
1166        let failed = kind == b"response.failed" || kind == b"interaction.failed";
1167        // A failure before anything started is answered as an HTTP error instead.
1168        if failed && !self.created_sent {
1169            return true;
1170        }
1171        if !self.created_sent && kind != b"interaction.created" {
1172            self.created_sent = true;
1173            let mut created = br#"{"event_type":"interaction.created","interaction":{"id":"","model":""}}"#.to_vec();
1174            gj::set_str(&mut created, "interaction.id", &self.interaction_id);
1175            gj::set_str(&mut created, "interaction.model", &self.model);
1176            if !self.emit(created) {
1177                return false;
1178            }
1179        }
1180        if kind == b"interaction.created" {
1181            self.created_sent = true;
1182        }
1183        if self.capture.enabled() {
1184            if std::mem::take(&mut self.first_logged_event) {
1185                capture_chunk(&self.capture, b"=== INTERMEDIATE INTERACTIONS STREAM ===\n");
1186            }
1187            // Go logs `trimmed + "\n"`.
1188            capture_chunk(&self.capture, &[trimmed.as_slice(), b"\n"].concat());
1189        }
1190        if self.response_format == Format::Interactions {
1191            let mut frame = b"data: ".to_vec();
1192            frame.extend_from_slice(&trimmed);
1193            frame.extend_from_slice(b"\n\n");
1194            self.out.push_back(Ok(Bytes::from(frame)));
1195            return true;
1196        }
1197        let Some(t) = self.translator.as_mut() else {
1198            return true;
1199        };
1200        // The shared Interactions-upstream adapter reads SSE: one event per blank line.
1201        match t.event(&[trimmed.as_slice(), b"\n\n"].concat()) {
1202            Ok(chunks) => self.deliver(chunks),
1203            Err(e) => {
1204                self.translation_failed = true;
1205                self.fail(ExecError::local(502, FailureScope::Request, e.to_string()));
1206                return false;
1207            }
1208        }
1209        if self.translator.as_ref().is_some_and(|t| t.tool_input_failed()) {
1210            self.translation_failed = true;
1211            self.fail(apply_patch_error());
1212            return false;
1213        }
1214        true
1215    }
1216
1217    /// `EndApplyPatchStream`: finalize tool input at transport end; true when that
1218    /// failed the stream.
1219    fn end_apply_patch(&mut self) -> bool {
1220        let Some(t) = self.translator.as_mut() else {
1221            return false;
1222        };
1223        let chunks = t.finalize_tool_input();
1224        let failed = t.tool_input_failed();
1225        self.out.extend(chunks.into_iter().map(Ok));
1226        if failed {
1227            self.translation_failed = true;
1228            self.fail(apply_patch_error());
1229        }
1230        failed
1231    }
1232
1233    fn flush_pending(&mut self) -> bool {
1234        if self.thought_started {
1235            if !self.emit(stop_event(self.thought_step)) {
1236                return false;
1237            }
1238            self.thought_started = false;
1239            self.step_index += 1;
1240        }
1241        for action in std::mem::take(&mut self.pending) {
1242            let ok = match action {
1243                Pending::Content(chunk) => self.emit_content(chunk),
1244                Pending::Tool(tc) => self.emit_tool_call(&tc),
1245            };
1246            if !ok {
1247                return false;
1248            }
1249        }
1250        true
1251    }
1252
1253    fn emit_content(&mut self, chunk: Vec<u8>) -> bool {
1254        if self.thought_started {
1255            let index = if self.thought_step < 0 {
1256                self.step_index
1257            } else {
1258                self.thought_step
1259            };
1260            if !self.emit(stop_event(index)) {
1261                return false;
1262            }
1263            self.thought_started = false;
1264            self.step_index += 1;
1265        }
1266        if self.tool_calls > 0 {
1267            self.post_tool.push(chunk);
1268            return true;
1269        }
1270        if !self.content_started {
1271            if !self.emit(start_event(self.step_index, br#"{"type":"model_output"}"#)) {
1272                return false;
1273            }
1274            self.content_started = true;
1275        }
1276        self.emit(text_delta_event(self.step_index, &chunk))
1277    }
1278
1279    fn emit_tool_call(&mut self, tc: &ToolCallDelta) -> bool {
1280        if self.thought_started {
1281            if !self.emit(stop_event(self.step_index)) {
1282                return false;
1283            }
1284            self.thought_started = false;
1285            self.step_index += 1;
1286        }
1287        if self.content_started {
1288            if !self.emit(stop_event(self.step_index)) {
1289                return false;
1290            }
1291            self.content_started = false;
1292            self.step_index += 1;
1293        }
1294        let (args, invalid) = if tc.arguments.is_empty() {
1295            (&tc.invalid_json, !tc.invalid_json.is_empty())
1296        } else {
1297            (&tc.arguments, false)
1298        };
1299        let slot = if tc.id.is_empty() {
1300            self.active
1301        } else {
1302            self.by_id.get(&tc.id).copied()
1303        };
1304        let step = match slot {
1305            None => {
1306                if self.tool_calls >= MAX_TOOL_CALLS {
1307                    tracing::warn!("devin executor: total tool calls exceeded max {MAX_TOOL_CALLS}, dropping");
1308                    return true;
1309                }
1310                self.tool_calls += 1;
1311                let step = self.step_index;
1312                self.step_index += 1;
1313                self.slots.insert(
1314                    step,
1315                    Slot {
1316                        step,
1317                        id: tc.id.clone(),
1318                        name: tc.name.clone(),
1319                    },
1320                );
1321                if !tc.id.is_empty() {
1322                    self.by_id.insert(tc.id.clone(), step);
1323                }
1324                self.active = Some(step);
1325                if !self.emit(tool_start_event(step, &tc.name, &tc.id)) {
1326                    return false;
1327                }
1328                step
1329            }
1330            Some(step) => {
1331                self.active = Some(step);
1332                let mut update = None;
1333                if let Some(slot) = self.slots.get_mut(&step) {
1334                    let mut updated = false;
1335                    if slot.id.is_empty() && !tc.id.is_empty() {
1336                        slot.id.clone_from(&tc.id);
1337                        updated = true;
1338                    }
1339                    if slot.name.is_empty() && !tc.name.is_empty() {
1340                        slot.name.clone_from(&tc.name);
1341                        updated = true;
1342                    }
1343                    if updated {
1344                        update = Some(slot.clone());
1345                    }
1346                }
1347                if let Some(slot) = update {
1348                    if !tc.id.is_empty() {
1349                        self.by_id.insert(tc.id.clone(), step);
1350                    }
1351                    if !self.emit(tool_start_event(slot.step, &slot.name, &slot.id)) {
1352                        return false;
1353                    }
1354                }
1355                step
1356            }
1357        };
1358        if !args.is_empty() {
1359            let mut delta =
1360                br#"{"event_type":"step.delta","index":0,"delta":{"type":"arguments_delta","arguments":""}}"#.to_vec();
1361            gj::set_int(&mut delta, "index", step);
1362            gj::set_str_no_html(&mut delta, "delta.arguments", args);
1363            if invalid {
1364                gj::set_bool(&mut delta, "delta.invalid_json_str", true);
1365            }
1366            if !self.emit(delta) {
1367                return false;
1368            }
1369        }
1370        true
1371    }
1372
1373    fn close_open_steps(&mut self) {
1374        if !self.pending.is_empty() || self.thought_started {
1375            let _ = self.flush_pending();
1376        }
1377        if !self.slots.is_empty() {
1378            let steps: Vec<i64> = self.slots.keys().copied().collect();
1379            for step in steps {
1380                let _ = self.emit(stop_event(step));
1381            }
1382            self.slots.clear();
1383            self.by_id.clear();
1384            self.active = None;
1385        }
1386        if !self.post_tool.is_empty() {
1387            let _ = self.emit(start_event(self.step_index, br#"{"type":"model_output"}"#));
1388            self.content_started = true;
1389            for chunk in std::mem::take(&mut self.post_tool) {
1390                let _ = self.emit(text_delta_event(self.step_index, &chunk));
1391            }
1392        }
1393        if self.content_started {
1394            let _ = self.emit(stop_event(self.step_index));
1395            self.content_started = false;
1396        }
1397    }
1398
1399    /// Reads and handles one frame, or ends the stream.
1400    async fn step(&mut self) {
1401        let Some(reader) = self.reader.as_mut() else {
1402            self.ended = true;
1403            return;
1404        };
1405        let (flag, payload) = match reader.next().await {
1406            Ok(frame) => frame,
1407            Err(FrameError::Eof) => return self.finish(None, false),
1408            Err(FrameError::Failed(message)) => {
1409                tracing::warn!("devin executor: stream read error: {message}");
1410                return self.finish(Some(message), false);
1411            }
1412        };
1413        self.summary.frames += 1;
1414        if flag & FLAG_END_STREAM != 0 {
1415            match parse_trailer_error(&payload) {
1416                Some((code, message)) => self.trailer_error(code, message),
1417                None => self.finish(None, true),
1418            }
1419            return;
1420        }
1421        let (frame, error) = parse_frame(&payload);
1422        if error.is_some() {
1423            return;
1424        }
1425        if frame.stop_reason != 0 {
1426            self.stop_reason = frame.stop_reason;
1427        }
1428        merge_usage(&mut self.usage, frame.usage.as_ref(), &frame.dimension_groups);
1429        if let Some(u) = &frame.usage
1430            && !u.model_name.is_empty()
1431        {
1432            self.usage_sink.response_model(&String::from_utf8_lossy(&u.model_name));
1433        }
1434        if self.log_summary {
1435            self.summary.signature.extend_from_slice(&frame.signature);
1436            if !frame.signature_type.is_empty() {
1437                self.summary.signature_type.clone_from(&frame.signature_type);
1438            }
1439        }
1440        if !frame.thinking.is_empty() {
1441            if !self.pending.is_empty() && !self.flush_pending() {
1442                return;
1443            }
1444            if self.log_summary {
1445                self.summary.thinking.extend_from_slice(&frame.thinking);
1446            }
1447            let chunk = self.thinking_buf.feed(&frame.thinking);
1448            if !chunk.is_empty() {
1449                if self.content_started {
1450                    if !self.emit(stop_event(self.step_index)) {
1451                        return;
1452                    }
1453                    self.content_started = false;
1454                    self.step_index += 1;
1455                }
1456                if !self.thought_started {
1457                    self.thought_step = self.step_index;
1458                    if !self.emit(start_event(self.step_index, br#"{"type":"thought"}"#)) {
1459                        return;
1460                    }
1461                    self.thought_started = true;
1462                }
1463                let mut delta = br#"{"event_type":"step.delta","index":0,"delta":{"type":"thought_summary","text":"","content":{"type":"text","text":""}}}"#.to_vec();
1464                gj::set_int(&mut delta, "index", self.thought_step);
1465                gj::set_str(&mut delta, "delta.text", &chunk);
1466                gj::set_str(&mut delta, "delta.content.text", &chunk);
1467                if !self.emit(delta) {
1468                    return;
1469                }
1470            }
1471        }
1472        if !frame.signature.is_empty() {
1473            if self.thought_step == -1 && !self.content_started {
1474                self.thought_step = self.step_index;
1475                if !self.emit(start_event(self.step_index, br#"{"type":"thought"}"#)) {
1476                    return;
1477                }
1478                self.thought_started = true;
1479            }
1480            let mut delta =
1481                br#"{"event_type":"step.delta","index":0,"delta":{"type":"thought_signature","signature":""}}"#
1482                    .to_vec();
1483            gj::set_int(&mut delta, "index", self.thought_step.max(0));
1484            gj::set_str(&mut delta, "delta.signature", &frame.signature);
1485            if !frame.signature_type.is_empty() {
1486                gj::set_str(&mut delta, "delta.signature_type", &frame.signature_type);
1487            }
1488            if !self.emit(delta) {
1489                return;
1490            }
1491        }
1492        for tc in frame.tool_calls {
1493            if self.thought_started {
1494                self.pending.push(Pending::Tool(tc));
1495            } else if !self.emit_tool_call(&tc) {
1496                return;
1497            }
1498        }
1499        if !frame.content.is_empty() {
1500            if self.log_summary {
1501                self.summary.content.extend_from_slice(&frame.content);
1502            }
1503            let chunk = self.content_buf.feed(&frame.content);
1504            if !chunk.is_empty() {
1505                if self.thought_started {
1506                    self.pending.push(Pending::Content(chunk));
1507                } else {
1508                    let _ = self.emit_content(chunk);
1509                }
1510            }
1511        }
1512    }
1513
1514    /// An EOS trailer carrying an error: open steps close, `response.failed`, then the
1515    /// mapped HTTP error.
1516    fn trailer_error(&mut self, code: u16, message: String) {
1517        self.reader = None;
1518        if self.end_apply_patch() {
1519            return;
1520        }
1521        self.close_open_steps();
1522        if self.translation_failed || self.ended {
1523            self.ended = true;
1524            return;
1525        }
1526        // Go: reporter.PublishFailure(errTrailer), a plain error: no status.
1527        self.usage_sink.publish_failure(0, &message);
1528        tracing::warn!("devin executor: trailer error ({code}): {message}");
1529        self.capture
1530            .record(cpa_core::exec::CaptureEvent::ResponseError(&message));
1531        let mut failed = br#"{"event_type":"response.failed","error":{"message":"","code":""}}"#.to_vec();
1532        gj::set_str(&mut failed, "error.message", &message);
1533        gj::set_str(&mut failed, "error.code", code.to_string());
1534        let _ = self.emit(failed);
1535        if !self.ended {
1536            self.fail(status_error(code, message));
1537        }
1538    }
1539
1540    /// The end of the frame stream: EOS (`saw_eos`), EOF, or a read error.
1541    fn finish(&mut self, read_error: Option<String>, saw_eos: bool) {
1542        self.reader = None;
1543        if (!saw_eos || read_error.is_some()) && self.end_apply_patch() {
1544            return;
1545        }
1546        self.close_open_steps();
1547        if self.translation_failed || self.ended {
1548            self.ended = true;
1549            return;
1550        }
1551        // A read error or a missing EOS returns without publishing: Go's deferred
1552        // EnsurePublished records a success without usage, not the stream error.
1553        if read_error.is_some() || !saw_eos {
1554            self.usage_sink.publish();
1555        }
1556        if let Some(message) = &read_error {
1557            self.capture
1558                .record(cpa_core::exec::CaptureEvent::ResponseError(message));
1559        } else if !saw_eos {
1560            self.capture.record(cpa_core::exec::CaptureEvent::ResponseError(
1561                "devin stream terminated prematurely before EOS trailer",
1562            ));
1563        }
1564        if let Some(message) = read_error {
1565            let mut failed =
1566                br#"{"event_type":"response.failed","error":{"message":"","code":"stream_read_error"}}"#.to_vec();
1567            gj::set_str(&mut failed, "error.message", &message);
1568            let _ = self.emit(failed);
1569            if !self.ended {
1570                self.fail(plain_error(message));
1571            }
1572            return;
1573        }
1574        if !saw_eos {
1575            const TRUNCATED: &str = "devin stream terminated prematurely before EOS trailer";
1576            let failed = br#"{"event_type":"response.failed","error":{"message":"devin stream terminated prematurely before EOS trailer","code":"stream_truncated"}}"#.to_vec();
1577            let _ = self.emit(failed);
1578            if !self.ended {
1579                self.fail(plain_error(TRUNCATED));
1580            }
1581            return;
1582        }
1583        let (status, finish_reason) = completion_status(self.stop_reason);
1584        let mut completed = br#"{"event_type":"interaction.completed","interaction":{"id":"","model":"","status":"completed","usage":{"total_input_tokens":0,"total_output_tokens":0,"total_cached_tokens":0}}}"#.to_vec();
1585        gj::set_str(&mut completed, "interaction.id", &self.interaction_id);
1586        gj::set_str(&mut completed, "interaction.model", &self.model);
1587        gj::set_str(&mut completed, "interaction.status", status);
1588        if !finish_reason.is_empty() {
1589            gj::set_str(&mut completed, "interaction.finish_reason", finish_reason);
1590        }
1591        if let Some(u) = self.usage.clone() {
1592            set_usage(&mut completed, "interaction.usage", &u);
1593        }
1594        let reported = self
1595            .usage_sink
1596            .enabled()
1597            .then(|| usage_payload(&completed, "interaction.model"));
1598        if !self.emit(completed) {
1599            self.ended = true;
1600            return;
1601        }
1602        // Go publishes ParseInteractionsStreamUsage(completed event) once it is sent.
1603        if let Some(payload) = reported {
1604            self.usage_sink.response_line(Format::Interactions, &payload);
1605        }
1606        // Go's response summary: the signature as received (not reformatted), no tool
1607        // calls.
1608        if self.log_summary && (self.usage.is_some() || !self.summary.signature.is_empty()) {
1609            let summary = ResponseLog {
1610                usage: self.usage.clone(),
1611                ..std::mem::take(&mut self.summary)
1612            };
1613            let mut chunk = b"\n=== DEVIN UPSTREAM RESPONSE SUMMARY ===\n".to_vec();
1614            chunk.extend_from_slice(&summary.marshal_indent());
1615            chunk.push(b'\n');
1616            capture_chunk(&self.capture, &chunk);
1617        }
1618        self.ended = true;
1619        if self.response_format == Format::Interactions {
1620            self.out.push_back(Ok(Bytes::from_static(b"data: [DONE]\n\n")));
1621            return;
1622        }
1623        // The synthetic [DONE] goes through the translator like an event.
1624        let Some(t) = self.translator.as_mut() else {
1625            return;
1626        };
1627        if let Ok(chunks) = t.event(b"[DONE]\n\n") {
1628            self.deliver(chunks);
1629        }
1630        if let Some(t) = self.translator.as_mut()
1631            && let Ok(chunks) = t.finish()
1632        {
1633            self.out.extend(chunks.into_iter().map(Ok));
1634        }
1635    }
1636}
1637
1638#[cfg(test)]
1639#[path = "devin_tests.rs"]
1640mod tests;