start-command 0.34.0

Gamification of coding, execute any command with ability to auto-report issues on GitHub
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! Apply a durable resume plan without holding the database lock over Docker.
use super::*;
use crate::docker_snapshot_resume::{
    commit_with_progress, progress_interval_seconds, remove_original_after, run_snapshot_prepare,
    SnapshotReporter,
};
use crate::docker_snapshot_safety::{
    acquire_snapshot_lock_with, df_free_bytes, preflight_snapshot_guarded, PreflightOptions,
    SnapshotFailure, SnapshotLockRequest,
};
use crate::snapshot_resume_options::SnapshotSettings;

/// Resume a tracked execution by UUID or session name.
pub fn resume_execution(
    store: Option<&ExecutionStore>,
    identifier: &str,
    command: Option<&str>,
    output_format: Option<&str>,
) -> ExecutionResumeResult {
    resume_execution_with(
        store,
        identifier,
        command,
        output_format,
        &SystemCommandRunner,
        &SystemResumeHooks,
    )
}

/// Resume with an injectable command runner and hooks, so tests never touch
/// docker.
pub fn resume_execution_with<R: CommandRunner, H: ResumeHooks>(
    store: Option<&ExecutionStore>,
    identifier: &str,
    command: Option<&str>,
    output_format: Option<&str>,
    runner: &R,
    hooks: &H,
) -> ExecutionResumeResult {
    resume_execution_with_resources(
        store,
        identifier,
        command,
        output_format,
        runner,
        hooks,
        &json!({}),
    )
}

pub fn resume_execution_with_options(
    store: Option<&ExecutionStore>,
    identifier: &str,
    command: Option<&str>,
    output_format: Option<&str>,
    options: &crate::args_parser::WrapperOptions,
) -> ExecutionResumeResult {
    let mut overrides = crate::docker_resource_options::specs(options);
    overrides["removeOriginal"] = json!(options.remove_original);
    overrides["labels"] = json!(options.labels);
    crate::snapshot_resume_options::insert_overrides(&options.snapshot, &mut overrides);
    resume_execution_with_resources(
        store,
        identifier,
        command,
        output_format,
        &SystemCommandRunner,
        &SystemResumeHooks,
        &overrides,
    )
}

#[allow(clippy::too_many_arguments)]
pub fn resume_execution_with_resources<R: CommandRunner, H: ResumeHooks>(
    store: Option<&ExecutionStore>,
    identifier: &str,
    command: Option<&str>,
    output_format: Option<&str>,
    runner: &R,
    hooks: &H,
    overrides: &Value,
) -> ExecutionResumeResult {
    let Some(store) = store else {
        return ExecutionResumeResult {
            success: false,
            output: None,
            error: Some("Execution tracking is disabled.".to_string()),
            exit_code: None,
        };
    };

    let Some(mut record) = store.get(identifier) else {
        return ExecutionResumeResult {
            success: false,
            output: None,
            error: Some(format!(
                "No execution found with UUID or session name: {}",
                identifier
            )),
            exit_code: None,
        };
    };

    if crate::launch_owner::has_active_launch(&record) {
        return ExecutionResumeResult {
            success: false,
            output: None,
            error: Some("An execution launch is already reserved.".into()),
            exit_code: None,
        };
    }
    let previous = record.clone();
    let probe = probe_session(&record, runner);
    let resources = crate::resume_resources::prepare(&record, &probe, overrides, runner);
    let (limits, resolved, old_memory) = match resources {
        Ok(result) => result,
        Err(error) => {
            return ExecutionResumeResult {
                success: false,
                output: None,
                error: Some(error),
                exit_code: None,
            }
        }
    };
    let mut planning_record = record.clone();
    if let Some(labels) = overrides
        .get("labels")
        .and_then(Value::as_array)
        .filter(|labels| !labels.is_empty())
    {
        let mut merged = record_strings(&record, "labels");
        for label in labels.iter().filter_map(Value::as_str) {
            let key = label.split('=').next().unwrap_or(label);
            if let Some(index) = merged
                .iter()
                .position(|existing| existing.split('=').next() == Some(key))
            {
                merged[index] = label.to_string();
            } else {
                merged.push(label.to_string());
            }
        }
        planning_record
            .options
            .insert("labels".into(), json!(merged));
    }
    if planning_record.options.get("labels") != record.options.get("labels") {
        if record_option(&record, "isolated") != Some("docker") {
            return ExecutionResumeResult {
                success: false,
                output: None,
                error: Some("Docker labels require a Docker execution when resuming.".into()),
                exit_code: None,
            };
        }
        planning_record
            .options
            .insert("forceSnapshot".into(), json!(true));
    }
    if let Some(limits) = &limits {
        planning_record
            .options
            .insert("resourceLimits".into(), json!(limits));
    }
    let mut plan =
        match build_resume_plan_with_limits(&planning_record, command, &probe, limits.clone()) {
            Ok(plan) => plan,
            Err(error) => {
                return ExecutionResumeResult {
                    success: false,
                    output: None,
                    error: Some(error),
                    exit_code: None,
                }
            }
        };
    if let Some(limits) = limits {
        plan.resource_limits = limits.clone();
        let changed = ["memory", "memorySwap", "memoryReservation", "cpus"]
            .iter()
            .any(|k| overrides.get(k).and_then(Value::as_str).is_some())
            || record.options.contains_key("cpuPenaltyConfig");
        if plan.mode == ResumeMode::DockerStart && changed {
            let mut args = vec!["update".into()];
            args.extend(limits.into_iter().filter(|s| {
                [
                    "--memory=",
                    "--memory-swap=",
                    "--memory-reservation=",
                    "--cpus=",
                    "--cpu-quota=",
                    "--cpu-period=",
                ]
                .iter()
                .any(|f| s.starts_with(f))
            }));
            args.push(plan.session_name.clone());
            plan.steps.insert(
                0,
                ResumeStep {
                    command: docker_command().to_string_lossy().into(),
                    args,
                    description: "Apply resume limits before start".into(),
                },
            );
        }
    }
    let mut container_id: Option<String> = None;
    let mut attempt = crate::execution_attempt::create_attempt(
        &record,
        plan.mode.as_str(),
        active_session_name(&plan),
    );
    let mut lifecycle_record = record.clone();
    lifecycle_record.attempt = Some(attempt.clone());
    crate::execution_attempt::append_lifecycle(&lifecycle_record, "resume-started", json!({}));
    apply_resume_to_record(&mut record, &plan, None);
    if let Some(labels) = planning_record.options.get("labels") {
        record.options.insert("labels".into(), labels.clone());
    }
    record.attempt = Some(attempt.clone());
    if let Some(resolved) = resolved {
        record.options.insert("resolvedLimits".into(), resolved);
    }
    let mut requested = record
        .options
        .get("resourceLimitSpecs")
        .filter(|value| value.is_object())
        .cloned()
        .unwrap_or_else(|| json!({}));
    for k in ["memory", "memorySwap", "memoryReservation", "cpus"] {
        if let Some(value) = overrides.get(k).filter(|v| v.is_string()) {
            requested[k] = value.clone();
        }
    }
    record
        .options
        .insert("resourceLimitSpecs".into(), requested);
    if record.options.contains_key("cpuPenaltyConfig")
        && overrides.get("cpus").and_then(Value::as_str).is_some()
    {
        record
            .options
            .insert("baseResourceLimits".into(), json!(&plan.resource_limits));
    }
    crate::launch_owner::mark_launch(&mut record);
    if let Err(error) = store.reserve_launch(&record, &previous) {
        return ExecutionResumeResult {
            success: false,
            output: None,
            error: Some(format!("Launch reservation failed: {}", error)),
            exit_code: None,
        };
    }

    if let Some(spec) = overrides.get("memory").and_then(Value::as_str) {
        let new = record
            .options
            .get("resolvedLimits")
            .and_then(|v| v.get("memory"))
            .cloned()
            .unwrap_or(Value::Null);
        if !record.log_path.is_empty() {
            append_log_file(
                &PathBuf::from(&record.log_path),
                &format!(
                    "Resume memory limit: {} -> {} ({})\n",
                    old_memory, new, spec
                ),
            );
        }
    }

    if plan.mode == ResumeMode::Relaunch {
        let mut launch_options = plan.launch_options.clone().unwrap_or_default();
        launch_options.attempt_number = attempt.number;
        let launch_result = hooks.relaunch(&plan.backend, &plan.command, &launch_options);
        if !launch_result.success {
            let _ = store.save(&previous);
            crate::execution_attempt::append_lifecycle(
                &lifecycle_record,
                "launch-failed",
                json!({ "error": launch_result.message }),
            );
            return ExecutionResumeResult {
                success: false,
                output: None,
                error: Some(format!(
                    "Failed to relaunch {} session \"{}\": {}",
                    plan.backend, plan.session_name, launch_result.message
                )),
                exit_code: None,
            };
        }
        container_id = launch_result.container_id;
    } else {
        let mut snapshot_created = false;
        let mut derived_created = false;
        let handoff = plan.mode == ResumeMode::DockerStart
            && command.is_some()
            && record_flag(&previous, "commandHandoff");
        let settings = SnapshotSettings::from_overrides(overrides);
        let notify = |line: &str| eprintln!("{}", line);
        let reporter = SnapshotReporter {
            log_path: &record.log_path,
            notify: &notify,
        };
        let mut failure_exit_code = None;
        let launch_result = (|| -> Result<(), String> {
            let mut exit_code_of = |failure: SnapshotFailure| {
                failure_exit_code = failure.exit_code;
                failure.message
            };
            let _snapshot_lock = if plan.mode == ResumeMode::DockerSnapshot {
                Some(
                    acquire_snapshot_lock_with(&SnapshotLockRequest {
                        lock_path: settings.lock_path.clone(),
                        holder: Some(&plan.session_name),
                        wait_seconds: settings.wait_seconds,
                        notify: &notify,
                    })
                    .map_err(&mut exit_code_of)?,
                )
            } else {
                None
            };
            if plan.mode == ResumeMode::DockerSnapshot {
                if let Some(prepare) = &settings.prepare {
                    run_snapshot_prepare(&plan.session_name, prepare, runner, &reporter);
                }
                let options = PreflightOptions {
                    size_hint: settings.size_hint,
                    disk_wait_seconds: settings.disk_wait_seconds,
                    disk_poll_seconds: settings.disk_poll_seconds,
                    notify: &notify,
                    ..Default::default()
                };
                let message =
                    preflight_snapshot_guarded(&plan.session_name, runner, &options, &|root| {
                        df_free_bytes(runner, root)
                    })
                    .map_err(&mut exit_code_of)?;
                plan.message.push_str(&format!("\n{}", message));
                if !record.log_path.is_empty() {
                    append_log_file(&PathBuf::from(&record.log_path), &format!("{}\n", message));
                }
            }
            if handoff {
                let copied = crate::docker_command_handoff::write_command_handoff(
                    &plan.session_name,
                    &plan.command,
                    record_option(&previous, "shell").unwrap_or("auto"),
                    record_flag(&previous, "keepAlive"),
                    runner,
                )?;
                if !copied.success {
                    return Err(if copied.stderr.trim().is_empty() {
                        copied.error.unwrap_or_else(|| {
                            "Could not copy replacement command into the stopped container".into()
                        })
                    } else {
                        copied.stderr.trim().into()
                    });
                }
            }
            if plan.mode == ResumeMode::DockerStart && record_flag(&previous, "commandHandoff") {
                // `docker start` keeps the creation env; refresh START_ATTEMPT (#230).
                let env = crate::execution_env::build_execution_env(
                    Some(&record.uuid),
                    Some(&plan.session_name),
                    attempt.number,
                );
                let _ = crate::docker_command_handoff::write_execution_env_handoff(
                    &plan.session_name,
                    &env,
                    runner,
                );
            }
            for step in &plan.steps {
                let result = if step.args[0] == "commit" {
                    commit_with_progress(
                        runner,
                        &step.command,
                        &step.args,
                        &plan.session_name,
                        &reporter,
                        settings
                            .progress_interval_seconds
                            .unwrap_or_else(progress_interval_seconds),
                    )
                } else {
                    runner.run(&step.command, &step.args)
                };
                if !result.success {
                    let detail = if !result.stderr.trim().is_empty() {
                        result.stderr.trim().to_string()
                    } else {
                        result.error.clone().unwrap_or_else(|| {
                            format!(
                                "exit code {}",
                                result
                                    .status
                                    .map(|code| code.to_string())
                                    .unwrap_or_else(|| "unknown".to_string())
                            )
                        })
                    };
                    return Err(detail);
                }
                if step.args[0] == "commit" {
                    snapshot_created = true;
                }
                if step.args[0] == "create" {
                    derived_created = true;
                }
                let stdout = result.stdout.trim().to_string();
                if !stdout.is_empty() && matches!(step.args[0].as_str(), "run" | "create") {
                    container_id = Some(stdout);
                }
            }
            if plan.mode == ResumeMode::DockerSnapshot
                && overrides.get("removeOriginal").and_then(Value::as_bool) == Some(true)
                && settings.remove_original_after <= 0.0
            {
                let running = runner.run(
                    &docker_command().to_string_lossy(),
                    &[
                        "inspect".into(),
                        "-f".into(),
                        "{{.State.Running}}".into(),
                        active_session_name(&plan).into(),
                    ],
                );
                let note = if running.success && running.stdout.trim() == "true" {
                    let removed = runner.run(
                        &docker_command().to_string_lossy(),
                        &["rm".into(), plan.session_name.clone()],
                    );
                    if removed.success {
                        format!("Stopped original container removed: {}", plan.session_name)
                    } else {
                        format!("Original container retained: {}", removed.stderr.trim())
                    }
                } else {
                    "Original container retained: the snapshot container is not running.".into()
                };
                plan.message.push_str(&format!("\n{}", note));
                if !record.log_path.is_empty() {
                    append_log_file(&PathBuf::from(&record.log_path), &format!("{}\n", note));
                }
            }
            Ok(())
        })();
        if let Err(detail) = launch_result {
            if derived_created {
                runner.run(
                    &docker_command().to_string_lossy(),
                    &["rm".into(), active_session_name(&plan).into()],
                );
            }
            if snapshot_created {
                runner.run(
                    &docker_command().to_string_lossy(),
                    &["rmi".into(), plan.snapshot_image.clone().unwrap()],
                );
            }
            if handoff {
                let _ = crate::docker_command_handoff::write_command_handoff(
                    &plan.session_name,
                    &previous.command,
                    record_option(&previous, "shell").unwrap_or("auto"),
                    record_flag(&previous, "keepAlive"),
                    runner,
                );
            }
            let _ = store.save(&previous);
            crate::execution_attempt::append_lifecycle(
                &lifecycle_record,
                "launch-failed",
                json!({ "error": detail }),
            );
            return ExecutionResumeResult {
                success: false,
                output: None,
                error: Some(format!(
                    "Failed to resume {} session \"{}\": {}",
                    plan.backend, plan.session_name, detail
                )),
                exit_code: failure_exit_code,
            };
        }

        if let Some(line) = build_resource_limits_status_line(&plan.resource_limits) {
            if !record.log_path.is_empty() {
                append_log_file(&PathBuf::from(&record.log_path), &format!("{}\n", line));
            }
        }
    }

    let previous_session_name = plan
        .new_session_name
        .as_ref()
        .map(|_| plan.session_name.clone());
    if let Some(id) = container_id.as_deref() {
        record.options.insert("containerId".into(), json!(id));
    }
    record.options.insert("launchPending".into(), json!(false));
    attempt.launch_accepted_at = Some(Utc::now().to_rfc3339());
    record
        .options
        .insert("resumedAt".into(), json!(attempt.started_at));
    record.attempt = Some(attempt.clone());
    if let Err(error) = store.save(&record) {
        if plan.backend == "docker" {
            let _ = hooks.attach_watcher(active_session_name(&plan), &record);
        }
        let stopped = plan.backend == "docker"
            && runner
                .run(
                    &docker_command().to_string_lossy(),
                    &["stop".into(), active_session_name(&plan).into()],
                )
                .success;
        if stopped {
            record.status = ExecutionStatus::Executed;
            record.exit_code = Some(-1);
            record.end_time = Some(Utc::now().to_rfc3339());
            record.options.insert("launchPending".into(), json!(false));
            let _ = store.save(&record);
        }
        return ExecutionResumeResult {
            success: false,
            output: None,
            error: Some(
                json!({"code": "LAUNCH_PERSISTENCE_FAILED", "uuid": record.uuid,
                "containerName": active_session_name(&plan), "running": !stopped,
                "error": error})
                .to_string(),
            ),
            exit_code: None,
        };
    }
    crate::execution_attempt::append_lifecycle(&record, "launch-accepted", json!({}));
    if plan.backend == "docker" {
        let attachment = hooks.attach_watcher(active_session_name(&plan), &record);
        let fields = match &attachment {
            Ok(()) => json!({"watcherAttachedAt": Utc::now().to_rfc3339()}),
            Err(error) => json!({"watcherError": error}),
        };
        match store.patch_attempt(&record.uuid, attempt.number, fields) {
            Ok(Some(current)) => crate::execution_attempt::append_lifecycle(
                &current,
                if attachment.is_ok() {
                    "watcher-attached"
                } else {
                    "watcher-attachment-failed"
                },
                json!({"error": attachment.as_ref().err()}),
            ),
            Ok(None) => {}
            Err(error) => {
                return ExecutionResumeResult {
                    success: false,
                    output: None,
                    error: Some(error),
                    exit_code: None,
                }
            }
        }
        if let Err(error) = attachment {
            return ExecutionResumeResult {
                success: false,
                output: None,
                error: Some(format!(
                    "Launch accepted, but completion watcher attachment failed: {}",
                    error
                )),
                exit_code: None,
            };
        }
    }

    let remove_after = SnapshotSettings::from_overrides(overrides).remove_original_after;
    if plan.mode == ResumeMode::DockerSnapshot
        && overrides.get("removeOriginal").and_then(Value::as_bool) == Some(true)
        && remove_after > 0.0
    {
        let notify = |line: &str| eprintln!("{}", line);
        let note = remove_original_after(
            &plan.session_name,
            active_session_name(&plan),
            remove_after,
            runner,
            &SnapshotReporter {
                log_path: &record.log_path,
                notify: &notify,
            },
            &std::thread::sleep,
        );
        plan.message.push_str(&format!("\n{}", note));
    }

    let session_name = record_option(&record, "sessionName")
        .unwrap_or(&plan.session_name)
        .to_string();

    ExecutionResumeResult {
        success: true,
        output: Some(format_resume_result(
            &ResumeResultFields {
                identifier,
                uuid: &record.uuid,
                mode: plan.mode,
                backend: &plan.backend,
                session_name: &session_name,
                previous_session_name: previous_session_name.as_deref(),
                snapshot_image: plan.snapshot_image.as_deref(),
                resource_limits: &plan.resource_limits,
                command: &plan.command,
                message: &plan.message,
            },
            output_format,
        )),
        error: None,
        exit_code: None,
    }
}

#[cfg(test)]
#[path = "execution_resume_cases.rs"]
mod tests;