loopflow 0.9.12

Run steps and flows with coding agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use std::collections::HashMap;
use std::path::Path;

use anyhow::{anyhow, Result};
use bollard::container::LogOutput;
use bollard::models::{ContainerCreateBody, Mount, MountTypeEnum};
use bollard::query_parameters::{
    CreateContainerOptions, InspectContainerOptions, StartContainerOptions, WaitContainerOptions,
};
use bollard::Docker;
use futures_util::StreamExt;
use tracing::warn;

use crate::engine::stream::StreamParser;

use super::{
    container_host_config, handle_output_line, logs_options, remove_container_options,
    sanitize_token, stop_container_options, DockerCredentialMount, DockerExecutor, DockerWorkspace,
    OutputContext, AGENT_USER, CONTAINER_PREFIX_AGENT, CONTAINER_PREFIX_PREP, CONTAINER_WORKSPACE,
    LABEL_AGENT_ID, LABEL_MANAGED, LABEL_WAVE_ID, LABEL_WAVE_RUN_ID,
};

impl DockerExecutor {
    pub(super) fn build_container_name(agent_id: &str) -> String {
        format!("{CONTAINER_PREFIX_AGENT}{}", agent_id.replace('_', "-"))
    }

    pub(super) fn build_helper_container_name(label: &str) -> String {
        format!(
            "{CONTAINER_PREFIX_PREP}{}-{}",
            sanitize_token(label),
            uuid::Uuid::new_v4().simple()
        )
    }

    pub(super) fn build_agent_labels(
        agent_id: &str,
        wave_id: &str,
        wave_run_id: &str,
    ) -> HashMap<String, String> {
        HashMap::from([
            (LABEL_MANAGED.to_string(), "true".to_string()),
            (LABEL_AGENT_ID.to_string(), agent_id.to_string()),
            (LABEL_WAVE_ID.to_string(), wave_id.to_string()),
            (LABEL_WAVE_RUN_ID.to_string(), wave_run_id.to_string()),
        ])
    }

    pub(super) fn rewrite_command_paths(
        cmd: Vec<String>,
        host_root: &Path,
        container_root: &str,
    ) -> Vec<String> {
        let host_root = host_root.to_string_lossy();
        let host_root = if host_root == "/" {
            "/".to_string()
        } else {
            host_root.trim_end_matches('/').to_string()
        };
        cmd.into_iter()
            .map(|arg| Self::rewrite_command_arg_path(&arg, &host_root, container_root))
            .collect()
    }

    pub(super) fn rewrite_command_arg_path(
        arg: &str,
        host_root: &str,
        container_root: &str,
    ) -> String {
        if arg == host_root {
            return container_root.to_string();
        }

        match arg.strip_prefix(host_root) {
            Some(suffix) if suffix.starts_with('/') => format!("{container_root}{suffix}"),
            _ => arg.to_string(),
        }
    }

    pub(super) async fn collect_env(&self, program: Option<&str>) -> Vec<String> {
        let mut env: Vec<String> = Vec::new();

        // Inject provider tokens from the DB first so OAuth credentials win.
        for (env_var, value) in crate::lfd::provider_auth::provider_env_vars(&self.store).await {
            if let Some(program) = program {
                if !crate::lfd::provider_auth::provider_env_allowed_for_program(program, &env_var) {
                    continue;
                }
            }
            if env
                .iter()
                .any(|entry| entry.starts_with(&format!("{env_var}=")))
            {
                continue;
            }
            env.push(format!("{env_var}={value}"));
        }

        for name in &self.credential_env {
            if crate::lfd::provider_auth::is_api_key_env_name(name) {
                match program {
                    Some(program) => {
                        if !crate::lfd::provider_auth::api_key_env_allowed_for_program(
                            program, name,
                        ) {
                            continue;
                        }
                    }
                    None => continue,
                }
            }
            if env
                .iter()
                .any(|entry| entry.starts_with(&format!("{name}=")))
            {
                continue;
            }
            if let Ok(value) = std::env::var(name) {
                env.push(format!("{name}={value}"));
            }
        }

        // Inject provider tokens from the DB. One query, then use the
        // canonical env_var_for_token() mapping for each.
        if let Ok(tokens) = self.store.list_provider_tokens().await {
            for token in tokens {
                if let Some((env_var, value)) = crate::lfd::provider_auth::env_var_for_token(&token)
                {
                    if !env.iter().any(|e| e.starts_with(&format!("{env_var}="))) {
                        env.push(format!("{env_var}={value}"));
                    }
                }
            }
        }

        // When SSH credentials are mounted, build a GIT_SSH_COMMAND that:
        // - Bypasses the host SSH config (may have macOS-only options like UseKeychain)
        // - Accepts new host keys so first-contact clones succeed
        // - Explicitly lists available private keys from the mounted .ssh dir
        if let Some(ssh_mount) = self
            .credential_mounts
            .iter()
            .find(|m| m.container_path.ends_with(".ssh"))
        {
            let key_args = Self::discover_ssh_keys(&ssh_mount.host_path, &ssh_mount.container_path);
            env.push(format!(
                "GIT_SSH_COMMAND=ssh -F /dev/null \
                     -o StrictHostKeyChecking=accept-new \
                     -o UserKnownHostsFile=/tmp/.ssh_known_hosts{}",
                key_args,
            ));
        }

        env
    }

    pub(super) fn discover_ssh_keys(host_ssh_dir: &Path, container_ssh_dir: &str) -> String {
        let mut key_args = String::new();
        if let Ok(entries) = std::fs::read_dir(host_ssh_dir) {
            for entry in entries.flatten() {
                let name = entry.file_name();
                let name_str = name.to_string_lossy();
                // Skip public keys, config, known_hosts, and other non-key files
                if name_str.ends_with(".pub")
                    || name_str.starts_with("known_hosts")
                    || name_str == "config"
                    || name_str == "authorized_keys"
                    || name_str.starts_with('.')
                {
                    continue;
                }
                // Only include regular files
                if entry.file_type().map(|t| t.is_file()).unwrap_or(false) {
                    key_args.push_str(&format!(" -i {container_ssh_dir}/{name_str}"));
                }
            }
        }
        key_args
    }

    pub(super) fn build_mounts_for(
        volume_name: &str,
        credential_mounts: &[DockerCredentialMount],
    ) -> Vec<Mount> {
        let mut mounts = vec![Mount {
            target: Some(CONTAINER_WORKSPACE.to_string()),
            source: Some(volume_name.to_string()),
            typ: Some(MountTypeEnum::VOLUME),
            read_only: Some(false),
            ..Default::default()
        }];

        for credential_mount in credential_mounts {
            mounts.push(Mount {
                target: Some(credential_mount.container_path.clone()),
                source: Some(credential_mount.host_path.to_string_lossy().to_string()),
                typ: Some(MountTypeEnum::BIND),
                read_only: Some(credential_mount.read_only),
                ..Default::default()
            });
        }

        mounts
    }

    pub(super) fn build_mounts(&self, volume_name: &str) -> Vec<Mount> {
        Self::build_mounts_for(volume_name, &self.credential_mounts)
    }

    pub(super) fn bind_mount(path: &Path, target: &str, read_only: bool) -> Mount {
        Mount {
            target: Some(target.to_string()),
            source: Some(path.to_string_lossy().to_string()),
            typ: Some(MountTypeEnum::BIND),
            read_only: Some(read_only),
            ..Default::default()
        }
    }

    pub(super) fn helper_mounts(
        &self,
        workspace: &DockerWorkspace,
        extra: Vec<Mount>,
    ) -> Vec<Mount> {
        let mut mounts = self.build_mounts(&workspace.volume.volume_name);
        mounts.extend(extra);
        mounts
    }

    pub(super) async fn remove_container(&self, container_id: &str) {
        if let Err(err) = self
            .docker
            .remove_container(container_id, Some(remove_container_options()))
            .await
        {
            warn!(container_id, error = %err, "failed to remove container");
        }
    }

    pub(super) async fn wait_for_container_with_logs(
        &self,
        container_id: &str,
        context: OutputContext,
    ) -> Result<i32> {
        let logs_task = tokio::spawn(Self::stream_logs(
            self.docker.clone(),
            container_id.to_string(),
            context,
        ));

        let mut wait_stream = self
            .docker
            .wait_container(container_id, None::<WaitContainerOptions>);
        let wait_result = tokio::time::timeout(self.agent_timeout, wait_stream.next()).await;

        match wait_result {
            Ok(Some(result)) => {
                let tail_lines = logs_task.await.unwrap_or_default();
                match result {
                    Ok(status) => {
                        let code = status.status_code as i32;
                        if code != 0 && !tail_lines.is_empty() {
                            let tail = tail_lines.join("\n");
                            warn!(
                                container_id,
                                exit_code = code,
                                tail = %tail,
                                "agent container exited with non-zero code"
                            );
                        }
                        Ok(code)
                    }
                    Err(err) => {
                        // Log tail output for diagnostics
                        if !tail_lines.is_empty() {
                            let tail = tail_lines.join("\n");
                            warn!(
                                container_id,
                                tail = %tail,
                                "agent container output before failure"
                            );
                        }
                        // Inspect the container for more context on failure
                        let inspect = self
                            .docker
                            .inspect_container(container_id, None::<InspectContainerOptions>)
                            .await;
                        let detail = match inspect {
                            Ok(info) => {
                                let state = info.state.as_ref();
                                format!(
                                    "status={} exit_code={} error={} oom={}",
                                    state
                                        .and_then(|s| s.status.as_ref().map(|v| format!("{v:?}")))
                                        .unwrap_or_default(),
                                    state.and_then(|s| s.exit_code).unwrap_or(-1),
                                    state.and_then(|s| s.error.as_deref()).unwrap_or(""),
                                    state.and_then(|s| s.oom_killed).unwrap_or(false),
                                )
                            }
                            Err(inspect_err) => format!("(inspect failed: {inspect_err})"),
                        };
                        Err(anyhow!("Docker container wait error: {err} [{detail}]"))
                    }
                }
            }
            Ok(None) => {
                let _tail = logs_task.await;
                Err(anyhow!("docker wait stream ended without status"))
            }
            Err(_) => {
                let _ = self
                    .docker
                    .stop_container(container_id, Some(stop_container_options()))
                    .await;
                let _tail = logs_task.await;
                Err(anyhow!(
                    "agent execution timed out after {}",
                    humantime::format_duration(self.agent_timeout)
                ))
            }
        }
    }

    pub(super) async fn stream_logs(
        docker: Docker,
        container_id: String,
        context: OutputContext,
    ) -> Vec<String> {
        let mut logs = docker.logs(&container_id, Some(logs_options(true)));
        const TAIL_SIZE: usize = 20;
        let mut tail: std::collections::VecDeque<String> = std::collections::VecDeque::new();

        let mut parser = StreamParser::new();
        let mut pending = String::new();
        while let Some(entry) = logs.next().await {
            match entry {
                Ok(LogOutput::StdOut { message })
                | Ok(LogOutput::StdErr { message })
                | Ok(LogOutput::Console { message }) => {
                    pending.push_str(&String::from_utf8_lossy(&message));
                    while let Some(newline) = pending.find('\n') {
                        let mut line = pending.drain(..=newline).collect::<String>();
                        if line.ends_with('\n') {
                            line.pop();
                        }
                        if line.ends_with('\r') {
                            line.pop();
                        }
                        if tail.len() >= TAIL_SIZE {
                            tail.pop_front();
                        }
                        tail.push_back(line.clone());
                        handle_output_line(&line, &mut parser, &context);
                    }
                }
                Err(err) => {
                    warn!(container_id, error = %err, "failed streaming container logs");
                    break;
                }
                _ => {}
            }
        }

        if !pending.is_empty() {
            if tail.len() >= TAIL_SIZE {
                tail.pop_front();
            }
            tail.push_back(pending.clone());
            handle_output_line(&pending, &mut parser, &context);
        }

        tail.into()
    }

    pub(super) async fn run_helper_command(
        &self,
        label: &str,
        cmd: Vec<String>,
        mounts: Vec<Mount>,
        working_dir: Option<String>,
    ) -> Result<String> {
        let container = self
            .docker
            .create_container(
                Some(CreateContainerOptions {
                    name: Some(Self::build_helper_container_name(label)),
                    ..Default::default()
                }),
                ContainerCreateBody {
                    image: Some(self.image.clone()),
                    cmd: Some(cmd),
                    working_dir,
                    env: Some(self.collect_env(None).await),
                    user: Some(AGENT_USER.to_string()),
                    host_config: Some(container_host_config(mounts, &self.limits)),
                    labels: Some(HashMap::from([(
                        LABEL_MANAGED.to_string(),
                        "true".to_string(),
                    )])),
                    attach_stdout: Some(true),
                    attach_stderr: Some(true),
                    ..Default::default()
                },
            )
            .await?;
        let container_id = container.id;

        if let Err(err) = self
            .docker
            .start_container(&container_id, None::<StartContainerOptions>)
            .await
        {
            self.remove_container(&container_id).await;
            return Err(err.into());
        }

        let mut wait_stream = self
            .docker
            .wait_container(&container_id, None::<WaitContainerOptions>);
        let wait_result = wait_stream.next().await;

        let mut logs = self.docker.logs(&container_id, Some(logs_options(false)));
        let mut output = String::new();
        while let Some(entry) = logs.next().await {
            match entry {
                Ok(LogOutput::StdOut { message })
                | Ok(LogOutput::StdErr { message })
                | Ok(LogOutput::Console { message }) => {
                    output.push_str(&String::from_utf8_lossy(&message));
                }
                Err(err) => {
                    warn!(container_id, error = %err, "failed reading helper logs");
                }
                _ => {}
            }
        }

        self.remove_container(&container_id).await;

        let status = wait_result
            .ok_or_else(|| anyhow!("docker wait stream ended without status"))?
            .map_err(|err| {
                anyhow!(
                    "docker helper '{}' wait failed: {:?} output={}",
                    label,
                    err,
                    output.trim()
                )
            })?;
        if status.status_code != 0 {
            return Err(anyhow!(
                "docker helper '{}' failed (exit {}): {}",
                label,
                status.status_code,
                output.trim()
            ));
        }
        Ok(output)
    }
}