Skip to content

Commit b5649e9

Browse files
authored
Merge pull request DeusData#2468 from DeusData/fix/issue-1167-1180-client-detect
fix(cli): client detection for OpenCode, Hermes, Claude (DeusData#1167, DeusData#1180)
2 parents 8c12de9 + d13c0ec commit b5649e9

4 files changed

Lines changed: 451 additions & 38 deletions

File tree

‎scripts/smoke-test.sh‎

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ copy_smoke_binary() {
116116
cp "$BINARY" "$destination"
117117
}
118118

119+
# A fixture home that belongs to a Claude Code user. An empty ~/.claude is not
120+
# evidence of one (#1180: other tools create that directory, and install then
121+
# configured a client the user never had); the user config Claude Code itself
122+
# writes is. Same seed as test_mark_claude_code_installed in tests/test_cli.c.
123+
mark_claude_code_installed() {
124+
local home="$1"
125+
mkdir -p "$home/.claude"
126+
echo '{}' > "$home/.claude.json"
127+
}
128+
119129
# Retire this run's private account daemon (if one is running) and wait until
120130
# it reports not-running. Install/uninstall flows leave an ephemeral daemon
121131
# draining asynchronously whose mapped generation backing and open logs
@@ -2126,27 +2136,33 @@ sys.exit(0 if ok else 1)
21262136
fi
21272137
echo "OK 8w-ii: Kiro MCP + steering + isolated exact-tool graph agent"
21282138

2129-
# 8x: Hermes Agent YAML MCP mapping
2139+
# 8x: Hermes Agent YAML MCP mapping. Native Windows Hermes keeps its home in
2140+
# %LOCALAPPDATA%\hermes (#1180); the stub-only FAKE_HOME has no legacy ~/.hermes.
2141+
if [[ "$BINARY" == *.exe ]]; then
2142+
HERMES_HOME_DIR="$FAKE_HOME/AppData/Local/hermes"
2143+
else
2144+
HERMES_HOME_DIR="$FAKE_HOME/.hermes"
2145+
fi
21302146
HERMES_CMD=$(sed -n '/^ codebase-memory-mcp:/{n;s/^ *command: *//p;}' \
2131-
"$FAKE_HOME/.hermes/config.yaml" 2>/dev/null | head -1)
2132-
if ! grep -q '^mcp_servers:' "$FAKE_HOME/.hermes/config.yaml" 2>/dev/null ||
2133-
! grep -q '^ codebase-memory-mcp:' "$FAKE_HOME/.hermes/config.yaml" 2>/dev/null ||
2147+
"$HERMES_HOME_DIR/config.yaml" 2>/dev/null | head -1)
2148+
if ! grep -q '^mcp_servers:' "$HERMES_HOME_DIR/config.yaml" 2>/dev/null ||
2149+
! grep -q '^ codebase-memory-mcp:' "$HERMES_HOME_DIR/config.yaml" 2>/dev/null ||
21342150
! quoted_path_value_matches "$HERMES_CMD" "$SELF_PATH"; then
21352151
echo "FAIL 8x: Hermes MCP mapping missing or malformed"
21362152
exit 1
21372153
fi
21382154
echo "OK 8x: Hermes Agent MCP"
2139-
if ! grep -q '^name: codebase-memory$' "$FAKE_HOME/.hermes/skills/codebase-memory/SKILL.md" 2>/dev/null ||
2140-
! grep -q 'delegate_task' "$FAKE_HOME/.hermes/skills/codebase-memory/SKILL.md" 2>/dev/null ||
2141-
! grep -q '`context`' "$FAKE_HOME/.hermes/skills/codebase-memory/SKILL.md" 2>/dev/null; then
2155+
if ! grep -q '^name: codebase-memory$' "$HERMES_HOME_DIR/skills/codebase-memory/SKILL.md" 2>/dev/null ||
2156+
! grep -q 'delegate_task' "$HERMES_HOME_DIR/skills/codebase-memory/SKILL.md" 2>/dev/null ||
2157+
! grep -q '`context`' "$HERMES_HOME_DIR/skills/codebase-memory/SKILL.md" 2>/dev/null; then
21422158
echo "FAIL 8x-i: Hermes delegation skill missing"
21432159
exit 1
21442160
fi
21452161
echo "OK 8x-i: Hermes durable delegation skill"
2146-
if ! grep -q '^hooks:' "$FAKE_HOME/.hermes/config.yaml" 2>/dev/null ||
2147-
! grep -q '^ pre_llm_call:' "$FAKE_HOME/.hermes/config.yaml" 2>/dev/null ||
2148-
! grep -q 'hook-augment' "$FAKE_HOME/.hermes/config.yaml" 2>/dev/null ||
2149-
! grep -q -- '--dialect hermes' "$FAKE_HOME/.hermes/config.yaml" 2>/dev/null; then
2162+
if ! grep -q '^hooks:' "$HERMES_HOME_DIR/config.yaml" 2>/dev/null ||
2163+
! grep -q '^ pre_llm_call:' "$HERMES_HOME_DIR/config.yaml" 2>/dev/null ||
2164+
! grep -q 'hook-augment' "$HERMES_HOME_DIR/config.yaml" 2>/dev/null ||
2165+
! grep -q -- '--dialect hermes' "$HERMES_HOME_DIR/config.yaml" 2>/dev/null; then
21502166
echo "FAIL 8x-ii: Hermes pre_llm_call context hook missing"
21512167
exit 1
21522168
fi
@@ -3148,8 +3164,8 @@ fi
31483164
echo "OK 9l: JSON agents, lifecycle hooks, and Kilo cleaned; foreign settings preserved"
31493165

31503166
# 9m: YAML/TOML new agents are cleaned.
3151-
if grep -q '^ codebase-memory-mcp:' "$FAKE_HOME/.hermes/config.yaml" 2>/dev/null ||
3152-
grep -q '^ pre_llm_call:' "$FAKE_HOME/.hermes/config.yaml" 2>/dev/null ||
3167+
if grep -q '^ codebase-memory-mcp:' "$HERMES_HOME_DIR/config.yaml" 2>/dev/null ||
3168+
grep -q '^ pre_llm_call:' "$HERMES_HOME_DIR/config.yaml" 2>/dev/null ||
31533169
grep -q '^ codebase-memory-mcp:' "$GOOSE_CFG" 2>/dev/null ||
31543170
grep -q 'codebase-memory-mcp' "$FAKE_HOME/.grok/config.toml" 2>/dev/null ||
31553171
grep -q '^name = "codebase-memory-mcp"' "$FAKE_HOME/.vibe/config.toml" 2>/dev/null; then
@@ -3203,7 +3219,7 @@ echo "OK 9m-i: durable instruction blocks removed"
32033219

32043220
# 9n: Skills removed (consolidated skill dir)
32053221
if [ -d "$FAKE_HOME/.claude/skills/codebase-memory" ] ||
3206-
[ -d "$FAKE_HOME/.hermes/skills/codebase-memory" ] ||
3222+
[ -d "$HERMES_HOME_DIR/skills/codebase-memory" ] ||
32073223
[ -d "$FAKE_HOME/.agents/skills/codebase-memory" ] ||
32083224
[ -d "$FAKE_HOME/.qoder/skills/codebase-memory" ] ||
32093225
[ -d "$CUSTOM_KIMI_HOME/skills/codebase-memory" ] ||
@@ -3281,9 +3297,40 @@ echo "OK 9b-1: install with minimal agents exits cleanly"
32813297
retire_account_daemon "9b-1-cleanup"
32823298
smoke_rmtree "$EMPTY_HOME"
32833299

3300+
# 9b-1b (#1180): an empty ~/.claude directory is not a Claude Code install.
3301+
# The empty home above is this venue's control: where it showed no Claude Code,
3302+
# the same home plus a bare ~/.claude must not be configured for it either.
3303+
#
3304+
# SKIP_WHITELIST: a venue with a claude executable (a developer machine) detects
3305+
# Claude Code in every home, so there is nothing for the bare directory to
3306+
# change. Narrowing PATH for this one install does not make such a venue
3307+
# claude-free: with HOME as the scanned home the product also probes fixed
3308+
# locations (/usr/local/bin, /opt/homebrew/bin). A venue without one, such as
3309+
# the Linux test image, runs the check.
3310+
if grep -q 'Detected agents:.* Claude-Code' <<<"$INSTALL_OUT"; then
3311+
echo "SKIP 9b-1b: a claude executable is installed in this venue (whitelisted above)"
3312+
else
3313+
BARE_HOME=$(smoke_mktemp_dir)
3314+
mkdir -p "$BARE_HOME/.claude" "$BARE_HOME/.local/bin"
3315+
BARE_RC=0
3316+
BARE_OUT=$(HOME="$BARE_HOME" LOCALAPPDATA="$BARE_HOME/AppData/Local" "$BINARY" install -y 2>&1) || BARE_RC=$?
3317+
if [ "$BARE_RC" -ge 128 ] ||
3318+
! grep -q 'Detected agents:' <<<"$BARE_OUT" ||
3319+
grep -q 'Detected agents:.* Claude-Code' <<<"$BARE_OUT" ||
3320+
[ -e "$BARE_HOME/.claude.json" ] ||
3321+
[ -n "$(ls -A "$BARE_HOME/.claude" 2>/dev/null)" ]; then
3322+
echo "FAIL 9b-1b: a bare ~/.claude directory was configured as a Claude Code install"
3323+
exit 1
3324+
fi
3325+
echo "OK 9b-1b: a bare ~/.claude directory is not configured as Claude Code"
3326+
retire_account_daemon "9b-1b-cleanup"
3327+
smoke_rmtree "$BARE_HOME"
3328+
fi
3329+
32843330
# 9b-2: Install twice (idempotent)
32853331
IDEM_HOME=$(smoke_mktemp_dir)
3286-
mkdir -p "$IDEM_HOME/.claude" "$IDEM_HOME/.local/bin"
3332+
mkdir -p "$IDEM_HOME/.local/bin"
3333+
mark_claude_code_installed "$IDEM_HOME"
32873334
copy_smoke_binary "$IDEM_HOME/.local/bin/codebase-memory-mcp"
32883335
run_no_crash 9b-2 env HOME="$IDEM_HOME" LOCALAPPDATA="$IDEM_HOME/AppData/Local" "$BINARY" install -y
32893336
IDEM_INSTALLER="$BINARY"
@@ -3307,7 +3354,8 @@ smoke_rmtree "$IDEM_HOME"
33073354

33083355
# 9b-3: Uninstall without prior install
33093356
CLEAN_HOME=$(smoke_mktemp_dir)
3310-
mkdir -p "$CLEAN_HOME/.claude" "$CLEAN_HOME/.local/bin"
3357+
mkdir -p "$CLEAN_HOME/.local/bin"
3358+
mark_claude_code_installed "$CLEAN_HOME"
33113359
UNINSTALL_RC=0
33123360
UNINSTALL_OUT=$(HOME="$CLEAN_HOME" "$BINARY" uninstall -y -n 2>&1) || UNINSTALL_RC=$?
33133361
if [ "$UNINSTALL_RC" -ge 128 ]; then
@@ -3331,7 +3379,8 @@ smoke_rmtree "$CORRUPT_HOME"
33313379

33323380
# 9b-8: Double uninstall
33333381
DBL_HOME=$(smoke_mktemp_dir)
3334-
mkdir -p "$DBL_HOME/.claude" "$DBL_HOME/.local/bin"
3382+
mkdir -p "$DBL_HOME/.local/bin"
3383+
mark_claude_code_installed "$DBL_HOME"
33353384
copy_smoke_binary "$DBL_HOME/.local/bin/codebase-memory-mcp"
33363385
run_no_crash 9b-8-install env HOME="$DBL_HOME" "$BINARY" install -y
33373386
DBL_UNINSTALLER="$BINARY"
@@ -3828,7 +3877,7 @@ if [ "$DL_OS" != "windows" ] && [ -f "$REPO_ROOT/install.sh" ]; then
38283877
echo "--- Phase 13: install.sh E2E ---"
38293878
INSTALL_TEST_HOME=$(smoke_mktemp_dir)
38303879
INSTALL_TEST_DIR=$(smoke_mktemp_dir)
3831-
mkdir -p "$INSTALL_TEST_HOME/.claude"
3880+
mark_claude_code_installed "$INSTALL_TEST_HOME"
38323881
mkdir -p "$INSTALL_TEST_HOME/.local/bin"
38333882

38343883
# 13a: run install.sh with local URL + isolated HOME
@@ -3865,7 +3914,8 @@ if [ "$DL_OS" != "windows" ] && [ -f "$REPO_ROOT/install.sh" ]; then
38653914
echo "OK 13d: no signing needed ($DL_OS)"
38663915
fi
38673916

3868-
# 13e: agent configs created (at least Claude Code since we made ~/.claude)
3917+
# 13e: agent configs created (at least Claude Code: the fixture home is a
3918+
# Claude Code user's)
38693919
if [ -f "$INSTALL_TEST_HOME/.claude.json" ] && grep -q 'codebase-memory-mcp' "$INSTALL_TEST_HOME/.claude.json" 2>/dev/null; then
38703920
echo "OK 13e: agent configs created by install.sh"
38713921
else
@@ -3892,7 +3942,7 @@ elif [ -f "$REPO_ROOT/install.ps1" ] && command -v powershell.exe &>/dev/null; t
38923942
echo "--- Phase 13: install.ps1 E2E (Windows) ---"
38933943
PS1_TEST_HOME=$(smoke_mktemp_dir)
38943944
PS1_TEST_DIR=$(smoke_mktemp_dir)
3895-
mkdir -p "$PS1_TEST_HOME/.claude"
3945+
mark_claude_code_installed "$PS1_TEST_HOME"
38963946

38973947
# Convert MSYS paths to Windows paths for PowerShell
38983948
if command -v cygpath &>/dev/null; then

‎src/cli/cli.c‎

Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,10 +2778,41 @@ static void cbm_kiro_home_dir(const char *home_dir, char *out, size_t out_sz) {
27782778
cbm_env_home_dir("KIRO_HOME", home_dir, ".kiro", out, out_sz);
27792779
}
27802780

2781+
/* #1180: native Windows Hermes -- the install.ps1 CLI, the MSIX package and
2782+
* the Desktop app -- defaults HERMES_HOME to %LOCALAPPDATA%\hermes and keeps a
2783+
* legacy ~/.hermes only while the new directory is absent (hermes-agent
2784+
* website/docs/user-guide/windows-native.md "Data layout";
2785+
* apps/desktop/electron/data-paths.mjs resolveDesktopHermesHome). Probing
2786+
* only ~/.hermes missed those installs and aimed config.yaml at a home Hermes
2787+
* never reads. Other platforms use ~/.hermes. */
2788+
static void cbm_hermes_home_dir_for(const char *home_dir, bool windows, char *out, size_t out_sz) {
2789+
const char *fallback = ".hermes";
2790+
if (windows) {
2791+
char native[CLI_BUF_1K];
2792+
char legacy[CLI_BUF_1K];
2793+
snprintf(native, sizeof(native), "%s/AppData/Local/hermes", home_dir);
2794+
snprintf(legacy, sizeof(legacy), "%s/.hermes", home_dir);
2795+
if (dir_exists(native) || !dir_exists(legacy)) {
2796+
fallback = "AppData/Local/hermes";
2797+
}
2798+
}
2799+
cbm_env_home_dir("HERMES_HOME", home_dir, fallback, out, out_sz);
2800+
}
2801+
27812802
static void cbm_hermes_home_dir(const char *home_dir, char *out, size_t out_sz) {
2782-
cbm_env_home_dir("HERMES_HOME", home_dir, ".hermes", out, out_sz);
2803+
#ifdef _WIN32
2804+
cbm_hermes_home_dir_for(home_dir, true, out, out_sz);
2805+
#else
2806+
cbm_hermes_home_dir_for(home_dir, false, out, out_sz);
2807+
#endif
27832808
}
27842809

2810+
#ifdef CBM_CLI_ENABLE_TEST_API
2811+
void cbm_hermes_home_dir_for_testing(const char *home_dir, bool windows, char *out, size_t out_sz) {
2812+
cbm_hermes_home_dir_for(home_dir, windows, out, out_sz);
2813+
}
2814+
#endif
2815+
27852816
static void cbm_qwen_home_dir(const char *home_dir, char *out, size_t out_sz) {
27862817
cbm_env_home_dir("QWEN_HOME", home_dir, ".qwen", out, out_sz);
27872818
}
@@ -3083,6 +3114,53 @@ static int cbm_resolve_released_hook_command(const char *script_name, char *out,
30833114
return written > 0 && (size_t)written < out_sz ? CLI_OK : CLI_ERR;
30843115
}
30853116

3117+
/* #1180: a bare ~/.claude directory is not a Claude Code install -- other
3118+
* tools create it -- and treating it as one wrote hooks, skills and MCP
3119+
* entries for a client the user never installed. Claude Code itself leaves
3120+
* its user config .claude.json (under CLAUDE_CONFIG_DIR when set), its
3121+
* settings.json, or the claude CLI. */
3122+
static bool cbm_claude_code_detected(const char *home_dir) {
3123+
char dir[CLI_BUF_1K];
3124+
char path[CLI_BUF_1K];
3125+
cbm_claude_config_dir(home_dir, dir, sizeof(dir));
3126+
int written = snprintf(path, sizeof(path), "%s/settings.json", dir);
3127+
if (dir[0] && written > 0 && (size_t)written < sizeof(path) && cbm_file_exists(path)) {
3128+
return true;
3129+
}
3130+
cbm_claude_user_root(home_dir, dir, sizeof(dir));
3131+
written = snprintf(path, sizeof(path), "%s/.claude.json", dir);
3132+
if (dir[0] && written > 0 && (size_t)written < sizeof(path) && cbm_file_exists(path)) {
3133+
return true;
3134+
}
3135+
return cbm_agent_cli_exists("claude", home_dir);
3136+
}
3137+
3138+
/* OpenCode keeps its data (auth, logs, sessions; the global config itself on
3139+
* older installs) in ~/.local/share/opencode on every OS --
3140+
* %USERPROFILE%\.local\share\opencode on Windows (opencode.ai/docs/
3141+
* troubleshooting, "Storage"). The CLI and the Desktop app's bundled
3142+
* opencode-cli server both create it, so it identifies an install that has
3143+
* no config file, no ~/.config/opencode and no `opencode` on PATH
3144+
* (#1167, #1180). */
3145+
static bool cbm_opencode_detected(const char *home_dir) {
3146+
char path[CLI_BUF_1K];
3147+
cbm_opencode_config_path(home_dir, path, sizeof(path));
3148+
if (cbm_file_exists(path) || cbm_agent_cli_exists("opencode", home_dir)) {
3149+
return true;
3150+
}
3151+
snprintf(path, sizeof(path), "%s/.config/opencode", home_dir);
3152+
if (dir_exists(path)) {
3153+
return true;
3154+
}
3155+
char env_buf[CLI_BUF_1K];
3156+
const char *config_dir = cbm_safe_getenv("OPENCODE_CONFIG_DIR", env_buf, sizeof(env_buf), NULL);
3157+
if (config_dir && config_dir[0] && dir_exists(config_dir)) {
3158+
return true;
3159+
}
3160+
snprintf(path, sizeof(path), "%s/.local/share/opencode", home_dir);
3161+
return dir_exists(path);
3162+
}
3163+
30863164
cbm_detected_agents_t cbm_detect_agents(const char *home_dir) {
30873165
cbm_detected_agents_t agents;
30883166
memset(&agents, 0, sizeof(agents));
@@ -3092,8 +3170,7 @@ cbm_detected_agents_t cbm_detect_agents(const char *home_dir) {
30923170

30933171
char path[CLI_BUF_1K];
30943172

3095-
cbm_claude_config_dir(home_dir, path, sizeof(path));
3096-
agents.claude_code = path[0] != '\0' && dir_exists(path);
3173+
agents.claude_code = cbm_claude_code_detected(home_dir);
30973174

30983175
cbm_codex_config_dir(home_dir, path, sizeof(path));
30993176
agents.codex = path[0] != '\0' && dir_exists(path);
@@ -3107,18 +3184,7 @@ cbm_detected_agents_t cbm_detect_agents(const char *home_dir) {
31073184
cbm_zed_config_dir(home_dir, path, sizeof(path));
31083185
agents.zed = dir_exists(path);
31093186

3110-
cbm_opencode_config_path(home_dir, path, sizeof(path));
3111-
agents.opencode = cbm_file_exists(path) || cbm_agent_cli_exists("opencode", home_dir);
3112-
if (!agents.opencode) {
3113-
snprintf(path, sizeof(path), "%s/.config/opencode", home_dir);
3114-
agents.opencode = dir_exists(path);
3115-
}
3116-
if (!agents.opencode) {
3117-
char env_buf[CLI_BUF_1K];
3118-
const char *config_dir =
3119-
cbm_safe_getenv("OPENCODE_CONFIG_DIR", env_buf, sizeof(env_buf), NULL);
3120-
agents.opencode = config_dir && config_dir[0] && dir_exists(config_dir);
3121-
}
3187+
agents.opencode = cbm_opencode_detected(home_dir);
31223188

31233189
agents.aider = cbm_agent_cli_exists("aider", home_dir);
31243190

‎src/cli/cli.h‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,11 @@ int cbm_remove_zed_mcp_owned(const char *binary_path, const char *config_path);
182182

183183
/* Detected coding agents on the system. */
184184
typedef struct {
185-
bool claude_code; /* ~/.claude/ exists */
185+
bool claude_code; /* settings.json, .claude.json, or claude CLI exists */
186186
bool codex; /* $CODEX_HOME or ~/.codex exists */
187187
bool gemini; /* Gemini settings or executable exists */
188188
bool zed; /* platform-specific Zed config dir exists */
189-
bool opencode; /* opencode on PATH or config exists */
189+
bool opencode; /* opencode on PATH, config, or data dir exists */
190190
bool antigravity; /* Antigravity CLI config or executable exists */
191191
bool aider; /* aider on PATH */
192192
bool kilocode; /* KiloCode globalStorage dir exists */
@@ -197,7 +197,7 @@ typedef struct {
197197
bool openclaw; /* ~/.openclaw/ exists */
198198
bool kiro; /* ~/.kiro/ exists */
199199
bool junie; /* ~/.junie/ exists */
200-
bool hermes; /* ~/.hermes/ or hermes CLI exists */
200+
bool hermes; /* Hermes home (Windows: %LOCALAPPDATA%\hermes) or CLI */
201201
bool openhands; /* ~/.openhands/ or openhands CLI exists */
202202
bool cline; /* ~/.cline/ or cline CLI exists */
203203
bool warp; /* Warp footprint or oz/oz-preview/warp-cli exists */
@@ -217,6 +217,13 @@ cbm_detected_agents_t cbm_detect_agents(const char *home_dir);
217217
/* Install or refresh every detected agent integration below home. */
218218
int cbm_install_agent_configs(const char *home, const char *binary_path, bool force, bool dry_run);
219219

220+
#ifdef CBM_CLI_ENABLE_TEST_API
221+
/* #1180: the Hermes home as resolved on Windows (`windows`) or elsewhere, so
222+
* the %LOCALAPPDATA%\hermes default and its legacy ~/.hermes fallback are
223+
* exercised on every host. */
224+
void cbm_hermes_home_dir_for_testing(const char *home_dir, bool windows, char *out, size_t out_sz);
225+
#endif
226+
220227
#ifdef CBM_CLI_ENABLE_TEST_API
221228
/* #1558: client-selector vocabulary, exposed so a test can prove every token
222229
* resolves and an unknown one is rejected. */

0 commit comments

Comments
 (0)