Skip to content

Commit 3007dbf

Browse files
committed
fix(cli): YAML removers answer OK for an absent config, so uninstall no longer refuses
`uninstall` failed with rc 1 on any macOS where Hermes is installed: Hermes is detected by its binary on PATH, not by its home, so a HOME without `.hermes/` (every test HOME, and a fresh user's) reached the Hermes uninstall ops with `~/.hermes/config.yaml` absent. The JSON removers already treat a missing config as "nothing to remove"; the YAML removers acquired the lock first, and the lock directory is created beside the target — with the parent directory absent that failed, the adapter recorded error: agent_config agent=Hermes op=pre_llm_hook_uninstall ... (target: does not exist or cannot be inspected) error: agent_config agent=Hermes op=mcp_uninstall ... and uninstall refused to remove the executable and the indexes. This is what made cli_uninstall_quiesces_active_cohort_before_removing_binary_and_index fail on such a machine (7837 passed, 1 failed on a pristine main). All four YAML removers short-circuit on a genuinely absent path and create nothing: lstat + ENOENT on POSIX, and CreateFileW with FILE_FLAG_OPEN_REPARSE_POINT on Windows, mirroring toml_read_file. Opening the reparse point rather than following it keeps a dangling symlink out of the short-circuit on both platforms, so it still reaches the reader and its byte-identical rejection. The regression test covers the four removers and runs on every platform: it asserts "nothing was created" through cbm_path_info_utf8 rather than lstat, so the Windows branch is exercised rather than compiled out. Signed-off-by: David Gil <dgilperez@gmail.com>
1 parent fe85a6b commit 3007dbf

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎src/cli/config_yaml_edit.c‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3601,8 +3601,49 @@ int cbm_yaml_upsert_mapping_entry(const char *file_path, const char *section_key
36013601
return result == 0 && release_result == 0 ? 0 : YAML_ERROR;
36023602
}
36033603

3604+
/* Removal is idempotent: a config that does not exist has nothing to remove.
3605+
* The JSON removers already answer OK here (cbm_json_like_read_document's
3606+
* "missing" result); the YAML removers instead took the lock first, and the
3607+
* lock directory is created beside the target — so with the parent directory
3608+
* absent (`~/.hermes/` never created because Hermes was detected by its
3609+
* binary on PATH, not by its home) every uninstall reported
3610+
* "does not exist or cannot be inspected" and refused to remove the
3611+
* executable. Only a true ENOENT short-circuits: a dangling symlink still
3612+
* reaches the reader and its byte-identical rejection. */
3613+
static bool yaml_remove_target_absent(const char *path) {
3614+
if (!path) {
3615+
return false;
3616+
}
3617+
#ifdef _WIN32
3618+
/* FILE_FLAG_OPEN_REPARSE_POINT, as in toml_read_file: opens the link
3619+
* itself rather than following it, so a dangling symlink does NOT count as
3620+
* absent and still reaches the reader — matching the POSIX branch, where
3621+
* lstat does not follow either. */
3622+
wchar_t *wide = cbm_utf8_to_wide(path);
3623+
if (!wide) {
3624+
return false;
3625+
}
3626+
HANDLE handle = CreateFileW(
3627+
wide, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
3628+
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
3629+
DWORD error = handle == INVALID_HANDLE_VALUE ? GetLastError() : 0;
3630+
free(wide);
3631+
if (handle != INVALID_HANDLE_VALUE) {
3632+
CloseHandle(handle);
3633+
return false;
3634+
}
3635+
return error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND;
3636+
#else
3637+
struct stat path_state;
3638+
return lstat(path, &path_state) != 0 && errno == ENOENT;
3639+
#endif
3640+
}
3641+
36043642
int cbm_yaml_remove_mapping_entry(const char *file_path, const char *section_key,
36053643
const char *entry_key) {
3644+
if (yaml_remove_target_absent(file_path)) {
3645+
return 0;
3646+
}
36063647
yaml_config_lock_t lock;
36073648
if (yaml_lock_acquire(file_path, &lock) != 0) {
36083649
return YAML_ERROR;
@@ -3626,6 +3667,9 @@ int cbm_yaml_upsert_owned_mapping_entry(const char *file_path, const char *secti
36263667

36273668
int cbm_yaml_remove_owned_mapping_entry(const char *file_path, const char *section_key,
36283669
const char *entry_key, const char *canonical_entry_block) {
3670+
if (yaml_remove_target_absent(file_path)) {
3671+
return CBM_YAML_IDENTITY_EDIT_OK;
3672+
}
36293673
yaml_config_lock_t lock;
36303674
if (yaml_lock_acquire(file_path, &lock) != 0) {
36313675
return CBM_YAML_IDENTITY_EDIT_ERROR;
@@ -3653,6 +3697,9 @@ int cbm_yaml_upsert_mapping_sequence_item(const char *file_path, const char *con
36533697
int cbm_yaml_remove_mapping_sequence_item(const char *file_path, const char *const *sequence_path,
36543698
size_t sequence_path_len, const char *identity_key,
36553699
const char *identity_scalar, const char *canonical_item) {
3700+
if (yaml_remove_target_absent(file_path)) {
3701+
return CBM_YAML_IDENTITY_EDIT_OK;
3702+
}
36563703
yaml_config_lock_t lock;
36573704
if (yaml_lock_acquire(file_path, &lock) != 0) {
36583705
return CBM_YAML_IDENTITY_EDIT_ERROR;
@@ -3675,6 +3722,9 @@ int cbm_yaml_upsert_string_list_item(const char *file_path, const char *key, con
36753722
}
36763723

36773724
int cbm_yaml_remove_string_list_item(const char *file_path, const char *key, const char *item) {
3725+
if (yaml_remove_target_absent(file_path)) {
3726+
return 0;
3727+
}
36783728
yaml_config_lock_t lock;
36793729
if (yaml_lock_acquire(file_path, &lock) != 0) {
36803730
return YAML_ERROR;

‎tests/test_config_yaml_edit.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* test_config_yaml_edit.c — Conservative YAML config editor tests.
33
*/
44
#include "../src/foundation/compat.h"
5+
#include "../src/foundation/compat_fs.h"
56
#include "test_framework.h"
67
#include "test_helpers.h"
78

@@ -1826,6 +1827,37 @@ TEST(config_yaml_edit_nested_sequence_removes_only_exact_canonical_item) {
18261827
PASS();
18271828
}
18281829

1830+
/* Uninstall must be idempotent against a config that was never written. Hermes
1831+
* is detected by its binary on PATH, so a HOME without `.hermes/` reached the
1832+
* YAML removers with an absent target — and they failed acquiring the lock
1833+
* beside a file whose directory does not exist, which `uninstall` then
1834+
* counted as an agent cleanup error and refused to remove the executable.
1835+
* Every remover answers OK for a genuinely absent path and creates nothing.
1836+
* Runs on every platform: the fix has a separate Windows branch. */
1837+
TEST(config_yaml_edit_remove_on_absent_config_is_a_no_op) {
1838+
yaml_fixture_t fixture;
1839+
ASSERT_EQ(yaml_fixture_init(&fixture, NULL), 0);
1840+
char absent[sizeof(fixture.path) + 32U];
1841+
ASSERT(snprintf(absent, sizeof(absent), "%s/no-such-dir/config.yaml", fixture.dir) > 0);
1842+
1843+
ASSERT_EQ(cbm_yaml_remove_mapping_sequence_item(absent, yaml_hook_sequence_path, 2U, "id",
1844+
yaml_hook_identity, yaml_hook_canonical_item),
1845+
CBM_YAML_IDENTITY_EDIT_OK);
1846+
ASSERT_EQ(cbm_yaml_remove_owned_mapping_entry(absent, "mcp_servers", "codebase-memory-mcp",
1847+
" command: \"/opt/cbm\"\n"),
1848+
CBM_YAML_IDENTITY_EDIT_OK);
1849+
ASSERT_EQ(cbm_yaml_remove_mapping_entry(absent, "hooks", "pre_llm_call"), 0);
1850+
ASSERT_EQ(cbm_yaml_remove_string_list_item(absent, "read", "AGENTS.md"), 0);
1851+
1852+
/* Nothing was created. cbm_path_info_utf8 rather than lstat: same meaning
1853+
* — it reports the link instead of following it — and it compiles on
1854+
* Windows, which is exactly where the fix has its own branch. */
1855+
cbm_path_info_t absent_info;
1856+
ASSERT(cbm_path_info_utf8(absent, &absent_info) != 0);
1857+
th_cleanup(fixture.dir);
1858+
PASS();
1859+
}
1860+
18291861
TEST(config_yaml_edit_nested_sequence_ambiguity_fails_byte_identically) {
18301862
const char *cases[] = {
18311863
"hooks:\n pre_llm_call:\n - id: \"bad-indent\"\n",
@@ -1980,6 +2012,7 @@ SUITE(config_yaml_edit) {
19802012
RUN_TEST(config_yaml_edit_nested_sequence_foreign_identity_is_preserved);
19812013
RUN_TEST(config_yaml_edit_nested_sequence_removes_only_exact_canonical_item);
19822014
RUN_TEST(config_yaml_edit_nested_sequence_ambiguity_fails_byte_identically);
2015+
RUN_TEST(config_yaml_edit_remove_on_absent_config_is_a_no_op);
19832016
#ifndef _WIN32
19842017
RUN_TEST(config_yaml_edit_accepts_interior_asterisk_in_plain_scalar_issue1631);
19852018
RUN_TEST(config_yaml_edit_still_refuses_leading_alias_issue1631);

0 commit comments

Comments
 (0)