fix: expose CLI runtime env diagnostics

This commit is contained in:
Shakker
2026-06-16 16:57:39 +01:00
committed by Shakker
parent 2bde35d29c
commit c38c4e9212
2 changed files with 22 additions and 0 deletions

View File

@@ -3623,11 +3623,13 @@ ${JSON.stringify({
it("formats CLI auth env diagnostics as key names without secret values", () => {
vi.stubEnv("ANTHROPIC_API_KEY", "sk-ant-host");
vi.stubEnv("ANTHROPIC_API_TOKEN", "token-host");
vi.stubEnv("GEMINI_CLI_SYSTEM_SETTINGS_PATH", "/tmp/host-gemini-settings.json");
vi.stubEnv("OPENAI_API_KEY", "sk-openai-host");
const log = buildCliEnvAuthLog({
ANTHROPIC_API_TOKEN: "token-child",
CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST: "1",
GEMINI_CLI_HOME: "/tmp/child-gemini-home",
OPENAI_API_KEY: "sk-openai-child",
});
@@ -3638,8 +3640,12 @@ ${JSON.stringify({
expect(log).toMatch(/child=.*CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST/);
expect(log).toMatch(/child=.*OPENAI_API_KEY/);
expect(log).toMatch(/cleared=.*ANTHROPIC_API_KEY/);
expect(log).toMatch(/runtimeHost=.*GEMINI_CLI_SYSTEM_SETTINGS_PATH/);
expect(log).toMatch(/runtimeChild=.*GEMINI_CLI_HOME/);
expect(log).toMatch(/runtimeCleared=.*GEMINI_CLI_SYSTEM_SETTINGS_PATH/);
expect(log).not.toContain("sk-ant-host");
expect(log).not.toContain("token-child");
expect(log).not.toContain("/tmp/child-gemini-home");
expect(log).not.toContain("sk-openai-child");
});

View File

@@ -312,6 +312,8 @@ const CLI_ENV_AUTH_LOG_KEYS = [
"OPENROUTER_API_KEY",
] as const;
const CLI_ENV_RUNTIME_LOG_KEYS = ["GEMINI_CLI_HOME", "GEMINI_CLI_SYSTEM_SETTINGS_PATH"] as const;
const CLI_BACKEND_PRESERVE_ENV = "OPENCLAW_LIVE_CLI_BACKEND_PRESERVE_ENV";
function parseCliBackendPreserveEnv(raw: string | undefined): Set<string> {
@@ -346,6 +348,13 @@ function listPresentCliAuthEnvKeys(env: Record<string, string | undefined>): str
});
}
function listPresentCliRuntimeEnvKeys(env: Record<string, string | undefined>): string[] {
return CLI_ENV_RUNTIME_LOG_KEYS.filter((key) => {
const value = env[key];
return typeof value === "string" && value.length > 0;
});
}
function formatCliEnvKeyList(keys: readonly string[]): string {
return keys.length > 0 ? keys.join(",") : "none";
}
@@ -405,10 +414,17 @@ export function buildCliEnvAuthLog(childEnv: Record<string, string>): string {
const childKeys = listPresentCliAuthEnvKeys(childEnv);
const childKeySet = new Set(childKeys);
const clearedKeys = hostKeys.filter((key) => !childKeySet.has(key));
const runtimeHostKeys = listPresentCliRuntimeEnvKeys(process.env);
const runtimeChildKeys = listPresentCliRuntimeEnvKeys(childEnv);
const runtimeChildKeySet = new Set(runtimeChildKeys);
const runtimeClearedKeys = runtimeHostKeys.filter((key) => !runtimeChildKeySet.has(key));
return [
`host=${formatCliEnvKeyList(hostKeys)}`,
`child=${formatCliEnvKeyList(childKeys)}`,
`cleared=${formatCliEnvKeyList(clearedKeys)}`,
`runtimeHost=${formatCliEnvKeyList(runtimeHostKeys)}`,
`runtimeChild=${formatCliEnvKeyList(runtimeChildKeys)}`,
`runtimeCleared=${formatCliEnvKeyList(runtimeClearedKeys)}`,
].join(" ");
}