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")
}