carryctx 0.8.0

Local-first memory for coding agents — resume tasks, checkpoints, and context across windows, sessions, and worktrees.
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
use crate::*;
use carryctx::adapter::git::GitCli;
use carryctx::adapter::sqlite_repos::{
    SqliteSessionRepository, SqliteTaskRepository, SqliteWorktreeRepository,
};
use carryctx::adapter::xdg::XdgPaths;
use carryctx::application::runtime::{InvocationContext, ProjectRuntime};
use carryctx::domain::session::SessionState;
use carryctx::domain::task::TaskStatus;
use carryctx::error::{CarryCtxError, ExitCode};
use carryctx::repository::{SessionRepository, WorktreeRepository};
use clap::Parser;

// ── Doctor ───────────────────────────────────────────────────────────────

/// Diagnose and automatically fix potential issues with the project's SQLite state database.
///
/// Checks Git repository health, database connectivity, schema version, orphaned
/// tasks (tasks with non-existent owners), stale active sessions, and git hook
/// installation status.
///
/// Exit codes reflect finding severity (CTX-0083): exit 0 when findings are
/// info/warning only, exit 1 when any check reports an error/critical
/// finding or diagnostics could not run. The rendered report is identical
/// either way — warnings still carry their status and fix hints.
#[derive(Parser, Debug)]
pub struct DoctorArgs {
    /// Automatically attempt to fix detected anomalies in the database and configuration.
    #[arg(long)]
    pub fix: bool,

    /// Remove registered worktrees whose directories are missing. This never deletes files.
    #[arg(long)]
    pub prune_stale_worktrees: bool,

    /// Output the diagnostic results in JSON format.
    #[arg(long)]
    pub json: bool,
}

fn append_schema_check(
    checks: &mut Vec<serde_json::Value>,
    all_ok: &mut bool,
    pending_result: Result<Vec<carryctx::adapter::sqlite::MigrationSource>, CarryCtxError>,
) {
    match pending_result {
        Ok(pending) if pending.is_empty() => checks.push(serde_json::json!({
            "check": "database.schema",
            "status": "ok",
            "message": "Schema version up to date"
        })),
        Ok(pending) => {
            *all_ok = false;
            checks.push(serde_json::json!({
                "check": "database.schema",
                "status": "error",
                "message": format!(
                    "{} pending migration(s) not applied: {}",
                    pending.len(),
                    pending.iter().map(|m| m.name.as_str()).collect::<Vec<_>>().join(", ")
                ),
                "repairable": true,
                "fix_command": "carryctx project migrate"
            }));
        }
        Err(error) => {
            *all_ok = false;
            checks.push(serde_json::json!({
                "check": "database.schema",
                "status": "error",
                "message": format!("Could not inspect database schema: {error}"),
                "repairable": false
            }));
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════
//  Handler: doctor
// ═══════════════════════════════════════════════════════════════════════════

pub fn handle_doctor(
    args: &DoctorArgs,
    pre_opened: Option<ProjectRuntime>,
    ctx: &InvocationContext,
    is_json: bool,
) -> Result<ExitCode, ExitCode> {
    let mut checks: Vec<serde_json::Value> = Vec::new();
    let mut all_ok = true;

    // ── 1. Global config ─────────────────────────────────────────────────
    let xdg = XdgPaths::new();
    let global_config = xdg.global_config();
    if global_config.exists() {
        match std::fs::read_to_string(&global_config) {
            Ok(content) => {
                match toml::from_str::<carryctx::domain::config::CarryCtxConfig>(&content) {
                    Ok(_) => checks.push(serde_json::json!({
                        "check": "config.global",
                        "status": "ok",
                        "message": "Global config is valid"
                    })),
                    Err(e) => {
                        all_ok = false;
                        checks.push(serde_json::json!({
                            "check": "config.global",
                            "status": "error",
                            "message": format!("Invalid global config: {e}"),
                            "repairable": false
                        }));
                    }
                }
            }
            Err(e) => {
                checks.push(serde_json::json!({
                    "check": "config.global",
                    "status": "warning",
                    "message": format!("Cannot read global config: {e}"),
                    "repairable": false
                }));
            }
        }
    } else {
        checks.push(serde_json::json!({
            "check": "config.global",
            "status": "info",
            "message": "No global config found (using defaults)"
        }));
    }

    // ── 2. Git repository ─────────────────────────────────────────────────
    let work_dir = resolve_work_dir(ctx);
    let git = GitCli::new();
    let git_project = match git.discover(work_dir) {
        Ok(gp) => {
            checks.push(serde_json::json!({
                "check": "git.repository",
                "status": "ok",
                "message": format!("Git repository at {}", gp.repository_root.display())
            }));
            Some(gp)
        }
        Err(e) => {
            all_ok = false;
            checks.push(serde_json::json!({
                "check": "git.repository",
                "status": "error",
                "message": format!("{e}"),
                "repairable": false
            }));
            None
        }
    };

    // ── 3. Git hooks ──────────────────────────────────────────────────────
    if let Some(ref gp) = git_project {
        let hooks_dir = gp.git_common_dir.join("hooks");
        let managed_hooks: Vec<&str> = ["post-commit", "prepare-commit-msg"]
            .iter()
            .filter(|&&name| {
                let p = hooks_dir.join(name);
                if !p.exists() {
                    return false;
                }
                std::fs::read_to_string(p)
                    .unwrap_or_default()
                    .contains("CarryCtx")
            })
            .copied()
            .collect();

        if managed_hooks.is_empty() {
            checks.push(serde_json::json!({
                "check": "git.hooks",
                "status": "info",
                "message": "No CarryCtx git hooks installed. Run `carryctx hooks install` to enable auto-checkpoint on commit.",
                "fix_command": "carryctx hooks install"
            }));
        } else {
            checks.push(serde_json::json!({
                "check": "git.hooks",
                "status": "ok",
                "message": format!("CarryCtx hooks installed: {}", managed_hooks.join(", "))
            }));
        }
    }

    // ── 3b. Jujutsu (jj) colocation ─────────────────────────────────────────
    if let Some(gp) = &git_project {
        if carryctx::adapter::git::detect_jj_colocation(&gp.git_common_dir) {
            checks.push(serde_json::json!({
                "check": "vcs.jj_colocation",
                "status": "info",
                "message": "jj colocation detected (.jj/ alongside .git/). CarryCtx reads Git state directly; some data (e.g. checkpoint staged/unstaged split) may be less precise under jj. See carryctx-docs/plans/2026-07-25-jujutsu-compatibility.md."
            }));
        }
    }

    // ── 4. Database connection + schema ───────────────────────────────────
    // Reuse the dispatcher's pre-opened runtime when available; a fresh open
    // only happens when that failed. Doctor keeps its own diagnostic
    // envelope: an open failure is reported as a failed `database.connection`
    // check inside the report, not as a global error.
    let opened = match pre_opened {
        Some(runtime) => Ok(runtime),
        None => try_open_runtime(ctx),
    };
    let runtime = match opened {
        Ok(rt) => {
            checks.push(serde_json::json!({
                "check": "database.connection",
                "status": "ok",
                "message": format!("Database at {}", rt.db_path.display())
            }));
            append_schema_check(&mut checks, &mut all_ok, rt.database.pending_migrations());
            Some(rt)
        }
        Err(exit_code) => {
            all_ok = false;
            let msg = match exit_code {
                ExitCode::Database => {
                    "Database connection failed — try `carryctx init` to reinitialise"
                }
                ExitCode::Git => "Not in a Git repository",
                _ => "Cannot open project (not initialised? Run `carryctx init`)",
            };
            checks.push(serde_json::json!({
                "check": "database.connection",
                "status": "error",
                "message": msg,
                "repairable": true,
                "fix_command": "carryctx init"
            }));
            None
        }
    };

    // ── 5. Orphaned tasks + in-progress state ──────────────────────────────
    // Diagnostics must see every task, so they read through exact
    // COUNT(*)-style queries instead of the capped listing (CTX-0080).
    if let Some(ref rt) = runtime {
        let conn = rt.database.connection();
        let project_id = &rt.config.project.id;
        let repository_root = &rt.git_project.repository_root;
        let task_repo = SqliteTaskRepository::new(conn);
        let worktree_repo = SqliteWorktreeRepository::new(conn);
        let cleanup_repo = carryctx::adapter::sqlite_repos::SqliteCleanupRepository::new(conn);

        match task_repo.list_orphaned_owner_refs(project_id) {
            Ok(orphaned) => {
                if orphaned.is_empty() {
                    checks.push(serde_json::json!({
                        "check": "tasks.orphaned",
                        "status": "ok",
                        "message": "No orphaned tasks (all owners exist)"
                    }));
                } else {
                    all_ok = false;
                    checks.push(serde_json::json!({
                        "check": "tasks.orphaned",
                        "status": "warning",
                        "message": format!(
                            "{} task(s) have deleted owners: {}",
                            orphaned.len(),
                            orphaned
                                .iter()
                                .map(|(display_id, title)| format!("{display_id} ({title})"))
                                .collect::<Vec<_>>()
                                .join(", ")
                        ),
                        "note": "Use `carryctx task unclaim <id>` to release ownership"
                    }));
                }
            }
            Err(e) => {
                checks.push(serde_json::json!({
                    "check": "tasks.orphaned",
                    "status": "warning",
                    "message": format!("Could not check tasks: {e}")
                }));
            }
        }

        match task_repo.count_by_status(project_id, &TaskStatus::InProgress) {
            Ok(in_progress_count) if in_progress_count > 0 => {
                let display_ids = task_repo
                    .list_display_ids_by_status(project_id, &TaskStatus::InProgress)
                    .unwrap_or_default();
                checks.push(serde_json::json!({
                    "check": "tasks.in_progress",
                    "status": "info",
                    "message": format!("{} task(s) currently in progress", in_progress_count),
                    "tasks": display_ids
                }));
            }
            Ok(_) => {}
            Err(e) => {
                checks.push(serde_json::json!({
                    "check": "tasks.in_progress",
                    "status": "warning",
                    "message": format!("Could not check in-progress tasks: {e}")
                }));
            }
        }

        // ── 6. Active sessions ──────────────────────────────────────────────
        let session_repo = SqliteSessionRepository::new(conn);
        let audit_session_id = ctx.session.clone().or_else(|| {
            session_repo
                .list(project_id)
                .ok()?
                .into_iter()
                .find(|session| matches!(session.state, SessionState::Active))
                .map(|session| session.id)
        });
        match session_repo.list(project_id) {
            Ok(sessions) => {
                let active: Vec<_> = sessions
                    .iter()
                    .filter(|s| matches!(s.state, SessionState::Active))
                    .collect();
                if !active.is_empty() {
                    checks.push(serde_json::json!({
                        "check": "sessions.active",
                        "status": "ok",
                        "message": format!("{} active session(s)", active.len())
                    }));
                } else {
                    checks.push(serde_json::json!({
                        "check": "sessions.active",
                        "status": "info",
                        "message": "No active session. Run `carryctx session start` to begin."
                    }));
                }
            }
            Err(e) => {
                checks.push(serde_json::json!({
                    "check": "sessions.active",
                    "status": "warning",
                    "message": format!("Could not check sessions: {e}")
                }));
            }
        }

        match cleanup_repo.list(project_id, None) {
            Ok(requests) => {
                let outstanding: Vec<_> = requests
                    .iter()
                    .filter(|request| {
                        matches!(
                            request.state,
                            carryctx::domain::cleanup::CleanupState::Pending
                                | carryctx::domain::cleanup::CleanupState::Running
                                | carryctx::domain::cleanup::CleanupState::Blocked
                                | carryctx::domain::cleanup::CleanupState::Failed
                        )
                    })
                    .collect();
                if outstanding.is_empty() {
                    checks.push(serde_json::json!({
                        "check": "worktrees.cleanup",
                        "status": "ok",
                        "message": "No pending, running, blocked, or failed worktree cleanups"
                    }));
                } else {
                    checks.push(serde_json::json!({
                        "check": "worktrees.cleanup",
                        "status": "warning",
                         "message": format!("{} worktree cleanup request(s) are pending, running, blocked, or failed and require attention", outstanding.len()),
                        "requests": outstanding.iter().map(|request| serde_json::json!({
                            "id": request.id,
                            "status": request.state,
                            "reason": request.reason,
                            "attempt_count": request.attempt_count,
                            "blocked_reason": request.blocked_reason,
                        })).collect::<Vec<_>>(),
                        "fix_command": "carryctx worktree cleanup run"
                    }));
                }
            }
            Err(e) => checks.push(serde_json::json!({
                "check": "worktrees.cleanup",
                "status": "warning",
                "message": format!("Could not check worktree cleanups: {e}")
            })),
        }

        if args.prune_stale_worktrees && ctx.dry_run {
            // Detection remains read-only; dry-run reports the same plan without writing.
        } else if args.prune_stale_worktrees && !ctx.yes {
            return render_and_print::<serde_json::Value>(
                "doctor",
                Err(CarryCtxError::permission_scope(
                    "Pruning stale worktrees requires explicit confirmation with --yes.",
                )),
                is_json || args.json,
                ctx.quiet,
            );
        }
        let stale_result = if args.prune_stale_worktrees && !ctx.dry_run {
            // An unresolvable actor must render the standard error envelope,
            // not collapse to a bare exit code with no output.
            let actor = match ctx
                .agent
                .as_deref()
                .map(|agent| resolve_agent_id(project_id, agent, conn))
                .transpose()
            {
                Ok(actor) => actor,
                Err(error) => {
                    return render_and_print::<serde_json::Value>(
                        "doctor",
                        Err(error),
                        is_json || args.json,
                        ctx.quiet,
                    );
                }
            };
            worktree_repo.prune_stale(
                project_id,
                repository_root,
                actor.as_deref(),
                audit_session_id.as_deref(),
                &chrono::Utc::now().to_rfc3339(),
            )
        } else {
            carryctx::application::worktree::stale_worktrees(
                &worktree_repo,
                project_id,
                repository_root,
            )
        };
        match stale_result {
            Ok(stale) if stale.is_empty() => checks.push(serde_json::json!({
                "check": "worktrees.stale",
                "status": "ok",
                "message": "No registered worktrees have missing directories"
            })),
            Ok(stale) => {
                if !args.prune_stale_worktrees {
                    all_ok = false;
                }
                checks.push(serde_json::json!({
                    "check": "worktrees.stale",
                    "status": if args.prune_stale_worktrees && !ctx.dry_run { "ok" } else { "warning" },
                    "message": if args.prune_stale_worktrees && !ctx.dry_run {
                        format!("Pruned {} stale worktree registration(s)", stale.len())
                    } else if args.prune_stale_worktrees {
                        format!("Would prune {} stale worktree registration(s)", stale.len())
                    } else {
                        format!("{} registered worktree(s) point to missing directories", stale.len())
                    },
                    "count": stale.len(),
                    "worktrees": stale.iter().map(|w| &w.path).collect::<Vec<_>>(),
                    "fix_command": "carryctx doctor --prune-stale-worktrees"
                }));
            }
            Err(e) => {
                return render_and_print::<serde_json::Value>(
                    "doctor",
                    Err(e),
                    is_json || args.json,
                    ctx.quiet,
                );
            }
        }
    }

    // ── Output ────────────────────────────────────────────────────────────
    let summary = if all_ok { "healthy" } else { "issues_found" };
    let result = serde_json::json!({
        "summary": summary,
        "checks": checks,
        "fix_requested": args.fix,
        "all_ok": all_ok,
    });

    // CTX-0083: exit codes reflect severity, not the mere presence of
    // findings. Info/warning-only reports (e.g. a stale worktree
    // registration) exit 0 so scripts can distinguish "nothing broken"
    // from real trouble; error/critical findings keep exit 1. The rendered
    // report is unchanged — `all_ok` still summarizes every non-ok check.
    let has_blocking_findings = checks
        .iter()
        .any(|check| matches!(check["status"].as_str(), Some("error") | Some("critical")));
    let exit_code = if has_blocking_findings {
        ExitCode::General
    } else {
        ExitCode::Success
    };

    if !is_json && !args.json && !ctx.quiet {
        println!("CarryCtx Doctor\n");
        for check in result["checks"].as_array().unwrap() {
            let status = check["status"].as_str().unwrap_or("?");
            let message = check["message"].as_str().unwrap_or("");
            let icon = match status {
                "ok" => "",
                "error" => "",
                "warning" => "",
                _ => "·",
            };
            println!("  {icon} {message}");
            if let Some(fix_cmd) = check["fix_command"].as_str() {
                println!("      → Fix: {fix_cmd}");
            }
        }
        println!();
        if all_ok {
            println!("Everything looks good!");
        } else {
            println!("Issues detected. Some may be fixed with `carryctx doctor --fix`.");
        }
        return Ok(exit_code);
    }

    let err_result: Result<serde_json::Value, CarryCtxError> = Ok(result);
    let _ = render_and_print("doctor", err_result, is_json || args.json, ctx.quiet);
    Ok(exit_code)
}

#[cfg(test)]
mod tests {
    use super::append_schema_check;
    use carryctx::error::CarryCtxError;

    #[test]
    fn migration_inspection_failure_is_a_failed_diagnostic() {
        let mut checks = Vec::new();
        let mut all_ok = true;

        append_schema_check(
            &mut checks,
            &mut all_ok,
            Err(CarryCtxError::database_error("injected inspection failure")),
        );

        assert!(!all_ok);
        assert_eq!(checks.len(), 1);
        assert_eq!(checks[0]["check"], "database.schema");
        assert_eq!(checks[0]["status"], "error");
        assert!(
            checks[0]["message"]
                .as_str()
                .unwrap()
                .contains("Could not inspect database schema")
        );
    }
}