codex-cli-captain 0.0.3

Codex-Cli-Captain runtime, installer, and MCP server for Codex CLI.
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
use crate::PUBLIC_ENTRY_LABEL;
use serde_json::Value;

fn config_surface_statuses_line(payload: &Value) -> Option<String> {
    let surfaces = payload
        .pointer("/configSurfaceReadiness/surfaces")
        .and_then(Value::as_array)?;
    let status_for = |surface_id: &str| {
        surfaces
            .iter()
            .find(|surface| surface.get("surface").and_then(Value::as_str) == Some(surface_id))
            .and_then(|surface| surface.get("status").and_then(Value::as_str))
            .unwrap_or("unknown")
    };
    Some(format!(
        "{} config surfaces: registry={} category_routing={} fallback_policy={} concurrency={} prompt_sections={} directory_rule_injection={} hook_settings={} lsp_runtime={} custom_agent_sync={} generated_defaults={}",
        env!("CARGO_PKG_VERSION"),
        status_for("registry"),
        status_for("category_routing"),
        status_for("fallback_policy"),
        status_for("concurrency"),
        status_for("prompt_sections"),
        status_for("directory_rule_injection"),
        status_for("hook_settings"),
        status_for("lsp_runtime"),
        status_for("custom_agent_sync"),
        status_for("generated_defaults_version"),
    ))
}

fn config_setup_guidance_line(payload: &Value) -> Option<String> {
    let guidance = payload.pointer("/configSurfaceReadiness/setup_guidance")?;
    let dry_run = guidance
        .get("dry_run")
        .and_then(Value::as_str)
        .unwrap_or("ccc setup --dry-run");
    let rollback = guidance
        .get("rollback")
        .and_then(Value::as_str)
        .unwrap_or("ccc setup --rollback-config <backup_path>");
    let backup = guidance
        .get("backup")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    let restart = guidance
        .get("restart")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    Some(format!(
        "Config setup safeguards: dry_run=\"{dry_run}\" backup={backup} rollback=\"{rollback}\" restart={restart}"
    ))
}

fn graph_context_readiness_line(payload: &Value) -> Option<String> {
    let readiness = payload
        .pointer("/graphContextReadiness/readiness")?
        .as_str()?;
    let graphify_state = payload
        .pointer("/graphContextReadiness/graphify_state")
        .and_then(Value::as_str)
        .unwrap_or("fallback");
    let setup_state = payload
        .pointer("/graphContextReadiness/setup_policy/state")
        .and_then(Value::as_str)
        .unwrap_or("fallback");
    let opt_in = payload
        .pointer("/graphContextReadiness/opt_in")
        .and_then(Value::as_bool)
        .unwrap_or(false);
    let check_install_status = payload
        .pointer("/graphContextReadiness/check_install_status")
        .and_then(Value::as_str)
        .unwrap_or("warning");
    let reason = payload
        .pointer("/graphContextReadiness/reason")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    let fallback = payload
        .pointer("/graphContextReadiness/fallback")
        .and_then(Value::as_str)
        .or_else(|| {
            payload
                .pointer("/graphContextReadiness/fallback_when_unavailable")
                .and_then(Value::as_str)
        })
        .unwrap_or("none");
    Some(format!(
        "Graph context: state={graphify_state} setup={setup_state} opt_in={opt_in} readiness={readiness} check_install={check_install_status} reason={reason} fallback={fallback} completion={}",
        payload
            .pointer("/graphContextReadiness/inventory_evidence/status")
            .and_then(Value::as_str)
            .unwrap_or("unknown")
    ))
}

fn lsp_server_availability_line(payload: &Value) -> Option<String> {
    let lsp = payload
        .get("lspRuntimeReadiness")
        .filter(|value| value.is_object())?;
    let server_summary = |server: &str| {
        let available = lsp
            .pointer(&format!("/language_servers/{server}/available"))
            .and_then(Value::as_bool)
            .unwrap_or(false);
        let package_hint = lsp
            .pointer(&format!("/language_servers/{server}/package_hint"))
            .and_then(Value::as_str)
            .or_else(|| {
                lsp.pointer(&format!("/server_availability/{server}/package_hint"))
                    .and_then(Value::as_str)
            })
            .unwrap_or("unknown");
        format!("{server}_available={available} {server}_package_hint=\"{package_hint}\"")
    };
    Some(format!(
        "LSP servers: {} {}",
        server_summary("rust"),
        server_summary("typescript_javascript")
    ))
}

fn hooks_readiness_line(payload: &Value) -> Option<String> {
    let hooks = payload
        .get("hooksReadiness")
        .filter(|value| value.is_object())?;
    Some(format!(
        "Codex hooks: state={} readiness={} runtime={} fallback={} reason={}",
        hooks
            .get("state")
            .and_then(Value::as_str)
            .unwrap_or("unknown"),
        hooks
            .get("readiness")
            .and_then(Value::as_str)
            .unwrap_or("unknown"),
        hooks
            .get("runtime_path")
            .and_then(Value::as_str)
            .unwrap_or("ccc_fallback"),
        hooks
            .get("fallback")
            .and_then(Value::as_str)
            .unwrap_or("ccc_cli_mcp_status_fan_in"),
        hooks
            .get("reason")
            .and_then(Value::as_str)
            .unwrap_or("unknown")
    ))
}

fn optional_string<'a>(value: &'a Value, pointer: &str) -> &'a str {
    value
        .pointer(pointer)
        .and_then(Value::as_str)
        .unwrap_or("unknown")
}

pub(crate) fn create_check_install_text(payload: &Value) -> String {
    let status = payload
        .get("status")
        .and_then(Value::as_str)
        .unwrap_or("warning");
    let version = payload
        .get("packageVersion")
        .and_then(Value::as_str)
        .unwrap_or(env!("CARGO_PKG_VERSION"));
    let entry = payload
        .get("publicEntryLabel")
        .and_then(Value::as_str)
        .unwrap_or(PUBLIC_ENTRY_LABEL);
    let registration = payload
        .get("registrationStatus")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    let config = payload
        .get("configStatus")
        .and_then(Value::as_str)
        .unwrap_or_else(|| {
            if payload
                .get("configExists")
                .and_then(Value::as_bool)
                .unwrap_or(false)
            {
                "canonical-current"
            } else {
                "missing"
            }
        });
    let config_action = payload
        .get("configActionStatus")
        .and_then(Value::as_str)
        .unwrap_or("skipped");
    let config_restart = payload
        .get("configRestartStatus")
        .and_then(Value::as_str)
        .unwrap_or("not-required");
    let config_backup = payload
        .get("configBackupStatus")
        .and_then(Value::as_str)
        .unwrap_or("not-required");
    let skill = payload
        .get("capSkillStatus")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    let surface = payload
        .pointer("/installSurfaceVisibility/status")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    let surface_restart = payload
        .pointer("/installSurfaceVisibility/restart_required")
        .and_then(Value::as_bool)
        .map(|required| if required { "required" } else { "not-required" })
        .unwrap_or("unknown");
    let custom_agents = payload
        .get("customAgentStatus")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    let registry_health = payload
        .pointer("/skillRegistryHealth/status")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    let registry_available = payload
        .pointer("/skillRegistryHealth/available_count")
        .and_then(Value::as_u64)
        .unwrap_or(0);
    let registry_total = payload
        .pointer("/skillRegistryHealth/agent_count")
        .and_then(Value::as_u64)
        .unwrap_or(0);
    let contract_status = payload
        .pointer("/executionContractRegistry/status")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    let contract_count = payload
        .pointer("/executionContractRegistry/role_count")
        .and_then(Value::as_u64)
        .unwrap_or(0);
    let plugin_status = payload
        .pointer("/pluginDiscovery/status")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    let plugin_severity = payload
        .pointer("/pluginDiscovery/diagnostic_severity")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    let plugin_missing = payload
        .pointer("/pluginDiscovery/missing")
        .and_then(Value::as_array)
        .map(|values| {
            values
                .iter()
                .filter_map(Value::as_str)
                .collect::<Vec<_>>()
                .join(",")
        })
        .unwrap_or_default();
    let plugin_stale = payload
        .pointer("/pluginDiscovery/stale")
        .and_then(Value::as_array)
        .map(|values| {
            values
                .iter()
                .filter_map(Value::as_str)
                .collect::<Vec<_>>()
                .join(",")
        })
        .unwrap_or_default();
    let discovery_surface_summary = payload
        .pointer("/pluginDiscoverySurfaces/summary")
        .and_then(Value::as_str)
        .map(str::to_string)
        .or_else(|| {
            let marketplace = payload
                .pointer("/pluginMarketplaceDiscovery/status")
                .and_then(Value::as_str)
                .unwrap_or("unknown");
            let stale_paths = payload
                .pointer("/legacyBundleCleanup/stalePathCount")
                .and_then(Value::as_u64)
                .unwrap_or(0);
            let restart = if payload
                .pointer("/installSurfaceVisibility/restart_required")
                .and_then(Value::as_bool)
                .unwrap_or(false)
                || payload
                    .pointer("/pluginDiscovery/restart_required")
                    .and_then(Value::as_bool)
                    .unwrap_or(false)
            {
                "required"
            } else {
                "not-required"
            };
            Some(format!(
                "Discovery surfaces: status={plugin_status} marketplace={marketplace} plugin_cache={plugin_status} cap_skill={skill} stale_paths={stale_paths} restart={restart}"
            ))
        })
        .unwrap_or_else(|| {
            "Discovery surfaces: status=unknown marketplace=unknown plugin_cache=unknown cap_skill=unknown stale_paths=0 restart=unknown".to_string()
        });
    let cap_continuity_status = payload
        .pointer("/capContinuity/status")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    let cap_continuity_resume = payload
        .pointer("/capContinuity/resume_command_template")
        .and_then(Value::as_str)
        .unwrap_or("$cap continue <run_id>");
    let cap_continuity_path = payload
        .pointer("/capContinuity/cap_skill_path")
        .and_then(Value::as_str)
        .unwrap_or("unknown");
    let cap_auto_restart = payload
        .pointer("/capContinuity/automatic_restart")
        .and_then(Value::as_bool)
        .unwrap_or(false);
    let cap_slash_execution = payload
        .pointer("/capContinuity/slash_command_execution")
        .and_then(Value::as_bool)
        .unwrap_or(false);

    let mut lines = vec![
        format!(
            "CCC install check: status={status} version={version} entry={entry} registration={registration} config={config} config_action={config_action} config_restart={config_restart} skill={skill}"
        ),
        format!(
            "Install surface: status={surface} restart={surface_restart} mcp={registration} skill={skill} custom_agents={custom_agents}"
        ),
        format!(
            "Skill registry: status={registry_health} available={registry_available}/{registry_total}"
        ),
        format!(
            "Plugin discovery: status={plugin_status} severity={plugin_severity} missing=[{plugin_missing}] stale=[{plugin_stale}]"
        ),
        discovery_surface_summary,
        format!(
            "$cap continuity: status={cap_continuity_status} path={cap_continuity_path} resume=\"{cap_continuity_resume}\" automatic_restart={cap_auto_restart} slash_execution={cap_slash_execution}"
        ),
        format!("Execution contracts: status={contract_status} roles={contract_count}"),
    ];
    if let Some(line) = config_surface_statuses_line(payload) {
        lines.push(line);
    }
    if let Some(line) = graph_context_readiness_line(payload) {
        lines.push(line);
    }
    if let Some(lsp) = payload
        .get("lspRuntimeReadiness")
        .filter(|value| value.is_object())
    {
        lines.push(format!(
            "LSP runtime: readiness={} execution={} usable={} completion={} blocking=false",
            lsp.get("readiness")
                .and_then(Value::as_str)
                .unwrap_or("unknown"),
            lsp.get("runtime_execution")
                .and_then(Value::as_str)
                .unwrap_or("unknown"),
            lsp.get("surface_usable")
                .and_then(Value::as_bool)
                .unwrap_or(false),
            lsp.pointer("/inventory_evidence/status")
                .and_then(Value::as_str)
                .unwrap_or("unknown")
        ));
    }
    if let Some(line) = lsp_server_availability_line(payload) {
        lines.push(line);
    }
    if let Some(line) = hooks_readiness_line(payload) {
        lines.push(line);
    }
    if let Some(diagnostics) = payload
        .get("pathShadowDiagnostics")
        .filter(|value| value.is_object())
    {
        lines.push(format!(
            "PATH shadowing: shell_ccc={} cargo_candidate={} cargo_version={} current_exe={} legacy_binary={} legacy_shadows_cargo={}",
            optional_string(diagnostics, "/shellResolvedCcc"),
            optional_string(diagnostics, "/cargoBinaryCandidate"),
            optional_string(diagnostics, "/cargoBinaryCandidateVersion"),
            optional_string(diagnostics, "/currentExe"),
            optional_string(diagnostics, "/legacyBinaryCandidate"),
            diagnostics
                .get("legacyShadowsCargo")
                .and_then(Value::as_bool)
                .unwrap_or(false)
        ));
        lines.push(
            diagnostics
                .get("summary")
                .and_then(Value::as_str)
                .unwrap_or("No PATH shadowing summary available.")
                .to_string(),
        );
        if diagnostics.get("status").and_then(Value::as_str) == Some("warning") {
            lines.push(
                diagnostics
                    .get("recommendation")
                    .and_then(Value::as_str)
                    .unwrap_or(
                        "Run ~/.cargo/bin/ccc setup, put ~/.cargo/bin earlier in PATH, or demote the legacy ccc binary.",
                    )
                    .to_string(),
            );
        }
    }
    if let Some(cleanup) = payload
        .get("legacyDirectRegistrationCleanup")
        .filter(|value| value.is_object())
    {
        lines.push(format!(
            "Legacy cleanup: status={} direct_mcp={} standalone_cap={}",
            cleanup
                .get("status")
                .and_then(Value::as_str)
                .unwrap_or("unknown"),
            cleanup
                .get("legacy_direct_mcp_servers_ccc_present")
                .and_then(Value::as_bool)
                .unwrap_or(false),
            cleanup
                .get("standalone_skills_cap_present")
                .and_then(Value::as_bool)
                .unwrap_or(false)
        ));
    }
    if let Some(cleanup) = payload
        .get("legacyBundleCleanup")
        .filter(|value| value.is_object())
    {
        let stale_paths = cleanup
            .get("stalePaths")
            .and_then(Value::as_array)
            .map(|values| {
                values
                    .iter()
                    .filter_map(Value::as_str)
                    .collect::<Vec<_>>()
                    .join(", ")
            })
            .unwrap_or_default();
        lines.push(format!(
            "Legacy bundle cleanup: status={} stale_paths={}",
            cleanup
                .get("status")
                .and_then(Value::as_str)
                .unwrap_or("unknown"),
            cleanup
                .get("stalePathCount")
                .and_then(Value::as_u64)
                .unwrap_or(0)
        ));
        if !stale_paths.is_empty() {
            lines.push(format!("Legacy bundle cleanup paths: {stale_paths}"));
        }
    }
    lines.extend([
        format!(
            "Expected MCP launch: {} {}",
            payload
                .get("expectedLaunchCommand")
                .and_then(Value::as_str)
                .unwrap_or("unknown"),
            payload
                .get("expectedLaunchArgs")
                .and_then(Value::as_array)
                .map(|values| values
                    .iter()
                    .filter_map(Value::as_str)
                    .collect::<Vec<_>>()
                    .join(" "))
                .unwrap_or_default()
        ),
        payload
            .get("registrationSummary")
            .and_then(Value::as_str)
            .unwrap_or("No registration summary available.")
            .to_string(),
        payload
            .get("configSummary")
            .and_then(Value::as_str)
            .unwrap_or("No config summary available.")
            .to_string(),
        format!("Config backup: status={config_backup}"),
    ]);
    if let Some(line) = config_setup_guidance_line(payload) {
        lines.push(line);
    }
    lines.extend([
        payload
            .get("capSkillSummary")
            .and_then(Value::as_str)
            .unwrap_or("No $cap summary available.")
            .to_string(),
        payload
            .get("customAgentSummary")
            .and_then(Value::as_str)
            .unwrap_or("No custom-agent summary available.")
            .to_string(),
    ]);
    lines.join("\n")
}