add marketplace import diagnostic

This commit is contained in:
stefanstokic-oai
2026-06-11 15:54:45 -04:00
parent 814446e16b
commit bfc9913683
9 changed files with 2268 additions and 157 deletions
+13
View File
@@ -50,6 +50,7 @@ mod doctor;
mod marketplace_cmd;
mod mcp_cmd;
mod plugin_cmd;
mod plugin_import_diagnostic;
mod remote_control_cmd;
#[cfg(target_os = "windows")]
mod sandbox_setup;
@@ -235,6 +236,10 @@ enum DebugSubcommand {
/// Internal: reset local memory state for a fresh start.
#[clap(hide = true)]
ClearMemories,
/// Internal: produce a redacted, per-plugin marketplace import report.
#[clap(hide = true)]
MarketplaceImportDiagnostic(plugin_import_diagnostic::MarketplaceImportDiagnosticCommand),
}
#[derive(Debug, Parser)]
@@ -1470,6 +1475,14 @@ async fn cli_main(arg0_paths: Arg0DispatchPaths) -> anyhow::Result<()> {
)?;
run_debug_clear_memories_command(&root_config_overrides).await?;
}
DebugSubcommand::MarketplaceImportDiagnostic(cmd) => {
reject_remote_mode_for_subcommand(
root_remote.as_deref(),
root_remote_auth_token_env.as_deref(),
"debug marketplace-import-diagnostic",
)?;
plugin_import_diagnostic::run(cmd).await?;
}
},
Some(Subcommand::Execpolicy(ExecpolicyCommand { sub })) => match sub {
ExecpolicySubcommand::Check(cmd) => {
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,77 @@
use super::Redactor;
use super::truncate_chars;
use std::path::Path;
#[test]
fn redactor_removes_roots_credentials_query_and_deep_url_paths() {
let redactor = Redactor::new(
Path::new("/private/customer/marketplace"),
Path::new("/tmp/diagnostic-home"),
);
let detail = redactor.detail(
"failed in /private/customer/marketplace/plugins/demo via \
https://user:secret@example.com/private/repo.git?token=secret and \
git@github.example.com:private/repo.git and \
/outside/customer/path and \
/tmp/diagnostic-home/plugins/cache; \
api_key=sk-123456789012345678901234567890",
);
assert!(detail.contains("<marketplace-root>/plugins/demo"));
assert!(detail.contains("<diagnostic-home>/plugins/cache"));
assert!(detail.contains("<redacted-url>"));
assert!(detail.contains("<git-remote>:<redacted>"));
assert!(!detail.contains("/outside/customer/path"));
assert!(!detail.contains("user:secret"));
assert!(!detail.contains("token=secret"));
assert!(!detail.contains("sk-123456789012345678901234567890"));
assert!(detail.contains("api_key=[REDACTED_SECRET]"));
}
#[test]
fn detail_truncation_is_bounded() {
let truncated = truncate_chars("x".repeat(20), 8);
assert_eq!(truncated, "xxxxxxxx...[truncated]");
}
#[test]
fn redactor_removes_git_command_arguments_and_remote_output() {
let redactor = Redactor::new(
Path::new("/private/customer/marketplace"),
Path::new("/tmp/diagnostic-home"),
);
let detail = redactor.detail(
"git checkout private-ref\nsecond-private-ref \
/tmp/diagnostic-home/staging failed with status exit status: 128\n\
stdout:\n\
private account output\n\
stderr:\n\
authentication failed",
);
assert!(detail.contains("git <redacted arguments> failed with status exit status: 128"));
assert!(detail.contains("failure class: authentication_or_authorization"));
assert!(detail.contains("git output omitted"));
assert!(!detail.contains("private-ref"));
assert!(!detail.contains("private account output"));
assert!(!detail.contains("authentication failed"));
}
#[test]
fn redactor_removes_parser_values_and_private_hosts() {
let redactor = Redactor::new(
Path::new("/private/customer/marketplace"),
Path::new("/tmp/diagnostic-home"),
);
let detail = redactor.detail(
"invalid type: integer `8675309`; unknown variant `private-transport`; fetch from \
https://internal.example/private/config failed",
);
assert!(detail.contains("integer <redacted-value>"));
assert!(detail.contains("variant <redacted-value>"));
assert!(detail.contains("<redacted-url>"));
assert!(!detail.contains("8675309"));
assert!(!detail.contains("private-transport"));
assert!(!detail.contains("internal.example"));
}
@@ -0,0 +1,210 @@
use anyhow::Result;
use serde_json::Value;
use std::fs;
use std::path::Path;
use tempfile::TempDir;
fn write_file(path: &Path, contents: &str) -> Result<()> {
let Some(parent) = path.parent() else {
anyhow::bail!("fixture file should have a parent: {}", path.display());
};
fs::create_dir_all(parent)?;
fs::write(path, contents)?;
Ok(())
}
#[test]
fn diagnostic_reports_every_entry_and_continues_after_failures() -> Result<()> {
let fixture = TempDir::new()?;
let marketplace = fixture.path();
write_file(
&marketplace.join(".claude-plugin/marketplace.json"),
r#"{
"name": "diagnostic-fixture",
"plugins": [
{ "name": "good", "source": "./plugins/good" },
{ "name": "missing-manifest", "source": "./plugins/missing-manifest" },
{ "name": "missing-source", "source": "./plugins/missing-source" },
{ "name": "unsupported", "source": { "source": "npm", "package": "private-package" } },
{ "name": "bad-path", "source": "plugins/bad-path" }
]
}"#,
)?;
write_file(
&marketplace.join("plugins/good/.claude-plugin/plugin.json"),
r#"{
"name": "good",
"version": "1.0.0",
"commands": "./commands",
"hooks": "./hooks/missing.json"
}"#,
)?;
write_file(
&marketplace.join("plugins/good/skills/demo/SKILL.md"),
r#"---
name: demo
description: Diagnostic fixture skill.
---
Use this skill only in the diagnostic fixture.
"#,
)?;
write_file(
&marketplace.join("plugins/good/skills/broken/SKILL.md"),
"This skill is missing YAML frontmatter.",
)?;
write_file(
&marketplace.join("plugins/good/.mcp.json"),
r#"{ "mcpServers": { "broken": { "command": 42 } } }"#,
)?;
write_file(
&marketplace.join("plugins/good/.app.json"),
r#"{ "apps": { "broken": { "id": "" } } }"#,
)?;
fs::create_dir_all(marketplace.join("plugins/missing-manifest"))?;
let output_dir = TempDir::new()?;
let output = output_dir.path().join("plugin-diagnostic.jsonl");
let mut command = assert_cmd::Command::new(codex_utils_cargo_bin::cargo_bin("codex")?);
command
.arg("debug")
.arg("marketplace-import-diagnostic")
.arg(marketplace)
.arg("--output")
.arg(&output)
.arg("--attempt-install")
.assert()
.success();
let report = fs::read_to_string(&output)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
assert_eq!(fs::metadata(&output)?.permissions().mode() & 0o077, 0);
}
assert!(!report.contains(&marketplace.display().to_string()));
assert!(!report.contains("private-package"));
let records = report
.lines()
.map(serde_json::from_str::<Value>)
.collect::<Result<Vec<_>, _>>()?;
assert_eq!(records.len(), 19);
assert_eq!(records[0]["attemptInstall"], true);
let started = records
.iter()
.filter(|record| record["event"] == "plugin_started")
.collect::<Vec<_>>();
assert_eq!(started.len(), 5);
assert_eq!(started[0]["pluginName"], "good");
let stages = records
.iter()
.filter(|record| record["event"] == "plugin_stage_started")
.collect::<Vec<_>>();
assert_eq!(stages.len(), 6);
assert_eq!(stages[0]["stage"], "install");
assert_eq!(stages[1]["stage"], "capabilities");
let plugins = records
.iter()
.filter(|record| record["event"] == "plugin_diagnostic")
.collect::<Vec<_>>();
assert_eq!(plugins.len(), 5);
assert_eq!(plugins[0]["pluginName"], "good");
assert_eq!(plugins[0]["outcome"], "installed");
assert_eq!(
plugins[0]["capabilities"]["manifest"]["unsupportedCapabilityFields"],
serde_json::json!(["commands"])
);
assert_eq!(
plugins[0]["capabilities"]["skills"]["detected"][0]["name"],
"good:demo"
);
assert!(
plugins[0]["capabilities"]["skills"]["errors"]
.as_array()
.is_some_and(|errors| !errors.is_empty())
);
assert_eq!(
plugins[0]["capabilities"]["mcp"]["serverNames"],
serde_json::json!([])
);
assert!(
plugins[0]["capabilities"]["mcp"]["issues"]
.as_array()
.is_some_and(|issues| !issues.is_empty())
);
assert!(
plugins[0]["capabilities"]["hooks"]["issues"]
.as_array()
.is_some_and(|issues| !issues.is_empty())
);
assert!(
plugins[0]["capabilities"]["apps"]["issues"]
.as_array()
.is_some_and(|issues| !issues.is_empty())
);
assert_eq!(plugins[1]["pluginName"], "missing-manifest");
assert_eq!(plugins[1]["outcome"], "install_failed");
assert_eq!(plugins[1]["install"]["code"], "install_failed");
assert_eq!(plugins[2]["pluginName"], "missing-source");
assert_eq!(plugins[2]["outcome"], "install_failed");
assert_eq!(plugins[2]["install"]["code"], "source_path_not_found");
assert_eq!(plugins[3]["detection"]["code"], "unsupported_source");
assert_eq!(plugins[4]["detection"]["code"], "invalid_source");
let completed = records.last().expect("completion record");
assert_eq!(completed["event"], "run_completed");
assert_eq!(completed["summary"]["rawEntries"], 5);
assert_eq!(completed["summary"]["detectedEntries"], 3);
assert_eq!(completed["summary"]["detectionFailures"], 2);
assert_eq!(completed["summary"]["installedEntries"], 1);
assert_eq!(completed["summary"]["installFailures"], 2);
Ok(())
}
#[test]
fn diagnostic_writes_preflight_failures_to_the_report() -> Result<()> {
let fixture = TempDir::new()?;
let missing_marketplace = fixture.path().join("missing-marketplace");
let output = fixture.path().join("preflight-diagnostic.jsonl");
let mut command = assert_cmd::Command::new(codex_utils_cargo_bin::cargo_bin("codex")?);
command
.arg("debug")
.arg("marketplace-import-diagnostic")
.arg(&missing_marketplace)
.arg("--output")
.arg(&output)
.assert()
.success();
let records = fs::read_to_string(&output)?
.lines()
.map(serde_json::from_str::<Value>)
.collect::<Result<Vec<_>, _>>()?;
assert_eq!(records.len(), 3);
assert_eq!(records[0]["event"], "run_started");
assert_eq!(records[1]["event"], "marketplace_diagnostic");
assert_eq!(records[1]["code"], "marketplace_root_unavailable");
assert_eq!(records[2]["status"], "report_complete_preflight_failed");
Ok(())
}
#[test]
fn diagnostic_does_not_overwrite_an_existing_report() -> Result<()> {
let fixture = TempDir::new()?;
let output = fixture.path().join("existing.jsonl");
fs::write(&output, "keep-me")?;
let mut command = assert_cmd::Command::new(codex_utils_cargo_bin::cargo_bin("codex")?);
command
.arg("debug")
.arg("marketplace-import-diagnostic")
.arg(fixture.path())
.arg("--output")
.arg(&output)
.assert()
.failure();
assert_eq!(fs::read_to_string(output)?, "keep-me");
Ok(())
}