Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix: make Lens settings and impact history portable

Martin Vogel5d10cb7af7fc
diff --git a/src/cli/cli.c b/src/cli/cli.c
index 172657edd..14fb053c8 100644
--- a/src/cli/cli.c
+++ b/src/cli/cli.c
@@ -1157,7 +1157,7 @@ static bool find_in_path(const char *name, char *out, size_t out_sz) {
}
const char *cbm_find_cli(const char *name, const char *home_dir) {
- static char buf[CLI_BUF_512];
+ static CBM_TLS char buf[CLI_BUF_512];
if (!name || !name[0]) {
return "";
}
diff --git a/src/cli/cli.h b/src/cli/cli.h
index e5e10417c..ed7524604 100644
--- a/src/cli/cli.h
+++ b/src/cli/cli.h
@@ -89,7 +89,8 @@ const char *cbm_detect_shell_rc(const char *home_dir);
/* Find a CLI binary by name.
* Checks PATH first, then common install locations.
- * Returns static buffer — do NOT free. Returns "" if not found. */
+ * Returns a per-thread buffer, overwritten by the next call on that thread.
+ * Do NOT free. Returns "" if not found. */
const char *cbm_find_cli(const char *name, const char *home_dir);
/* ── File utilities ───────────────────────────────────────────── */
diff --git a/src/cli/runtime_settings.c b/src/cli/runtime_settings.c
index 183656606..d8ce46c2a 100644
--- a/src/cli/runtime_settings.c
+++ b/src/cli/runtime_settings.c
@@ -520,7 +520,7 @@ static char *snapshot(sqlite3 *db, const char *cache) {
yyjson_mut_obj_add_val(doc, root, "settings", items);
char canonical[SETTINGS_PATH_CAP];
bool current_process = initialized && cbm_canonical_path(cache, canonical, sizeof(canonical)) &&
- strcmp(canonical, initial_cache) == 0;
+ strcmp(cbm_normalize_path_sep(canonical), initial_cache) == 0;
for (size_t i = 0; i < SETTINGS_COUNT; i++) {
const setting_t *setting = &settings[i];
char buffer[SETTINGS_VALUE_CAP];
diff --git a/src/daemon/host.c b/src/daemon/host.c
index a44fcdaf1..bd5e55573 100644
--- a/src/daemon/host.c
+++ b/src/daemon/host.c
@@ -161,6 +161,7 @@ static void host_start_failure_record(const cbm_daemon_ipc_endpoint_t *endpoint,
static void host_log_close(void) {
cbm_log_set_sink(NULL);
+ cbm_ui_log_close();
if (g_host_log_mutex_initialized) {
cbm_mutex_lock(&g_host_log_mutex);
}
diff --git a/src/ui/atlas_impact.c b/src/ui/atlas_impact.c
index a6b12a4fe..ebb4fd9c4 100644
--- a/src/ui/atlas_impact.c
+++ b/src/ui/atlas_impact.c
@@ -7,6 +7,7 @@
#include "foundation/constants.h"
#include "foundation/json_writer.h"
#include "ui/atlas.h"
+#include "cli/cli.h"
#include "foundation/compat_fs.h"
#include "foundation/subprocess.h"
#include "foundation/platform.h"
@@ -230,26 +231,40 @@ static char *ia_git(const char *root, const char *const *args, size_t *size, tim
*bounded = true;
return NULL;
}
+ const char *git = "git";
+#ifdef _WIN32
+ /* CreateProcess needs an executable path; it does not resolve a bare
+ * application name through PATH. Keep the resolver's result locally. */
+ char git_path[1024];
+ const char *resolved = cbm_find_cli("git", NULL);
+ if (!resolved || !*resolved || strlen(resolved) >= sizeof(git_path))
+ return NULL;
+ snprintf(git_path, sizeof(git_path), "%s", resolved);
+ git = git_path;
+#endif
char dir[256] = "/tmp/cbm-impact-XXXXXX";
if (!cbm_mkdtemp(dir))
return NULL;
char log[512];
snprintf(log, sizeof(log), "%s/output", dir);
- const char *argv[40] = {"git",
- "--no-lazy-fetch",
- "--no-optional-locks",
- "-c",
- "core.fsmonitor=false",
- "-c",
- "core.untrackedCache=false",
- "-C",
- root};
- int n = 9;
+ /* Git before 2.44 rejects --no-lazy-fetch. The environment form is safe
+ * to pass to older Git; an empty protocol allowlist also denies every
+ * fetch transport, even when repository configuration enables one. */
+ const char *const git_env[] = {"GIT_NO_LAZY_FETCH=1",
+ "GIT_ALLOW_PROTOCOL=", "GIT_TERMINAL_PROMPT=0", NULL};
+ const char *argv[40] = {git, "--no-optional-locks",
+ "-c", "protocol.allow=never",
+ "-c", "core.fsmonitor=false",
+ "-c", "core.untrackedCache=false",
+ "-C", root};
+ int n = 10;
while (*args && n < 39)
argv[n++] = *args++;
argv[n] = NULL;
- cbm_proc_opts_t opts = {.bin = "git",
+ cbm_proc_opts_t opts = {.bin = git,
.argv = argv,
+ .env_overrides = git_env,
+ .strip_git_repo_env = true,
.log_file = log,
.quiet_timeout_ms = 2000,
.cancel_grace_ms = 100};
diff --git a/src/ui/http_server.c b/src/ui/http_server.c
index 1523f71fa..ffc834f6d 100644
--- a/src/ui/http_server.c
+++ b/src/ui/http_server.c
@@ -502,6 +502,16 @@ void cbm_ui_log_append(const char *line) {
ui_log_append_record(line, line);
}
+void cbm_ui_log_close(void) {
+ cbm_ui_log_init();
+ cbm_mutex_lock(&g_log_mutex);
+ if (sqlite3_close(g_activity_db) == SQLITE_OK) {
+ g_activity_db = NULL;
+ g_activity_path[0] = '\0';
+ }
+ cbm_mutex_unlock(&g_log_mutex);
+}
+
/* Append a printf-formatted fragment at *pos within a bufsz buffer, never
* advancing *pos past bufsz. snprintf returns the length it WOULD have written,
* so `pos += snprintf(...)` runs pos past the end on truncation and the next
@@ -3512,11 +3522,7 @@ bool cbm_http_server_free(cbm_http_server_t *srv) {
return false;
cbm_atlas_impact_analysis_shutdown();
cbm_mcp_server_free(srv->mcp);
- cbm_ui_log_init();
- cbm_mutex_lock(&g_log_mutex);
- sqlite3_close(g_activity_db);
- g_activity_db = NULL;
- cbm_mutex_unlock(&g_log_mutex);
+ cbm_ui_log_close();
cbm_secure_zero(srv->readiness_secret, sizeof(srv->readiness_secret));
free(srv);
return true;
diff --git a/src/ui/http_server.h b/src/ui/http_server.h
index b42326157..5c322b04a 100644
--- a/src/ui/http_server.h
+++ b/src/ui/http_server.h
@@ -104,6 +104,10 @@ void cbm_ui_log_init(void);
/* Persist a daemon log line in SQLite and the emergency memory ring. */
void cbm_ui_log_append(const char *line);
+/* Release the current activity database. A later append can reopen it;
+ * final shutdown must first quiesce log producers. The mutex remains valid. */
+void cbm_ui_log_close(void);
+
/* Frontend compatibility export: <cache_dir>/logs/ui.log, one JSON line
* per accepted entry. Both HTTP log readers use the SQLite journal.
* Returns false when the path does not fit in out. */
diff --git a/tests/test_cli.c b/tests/test_cli.c
index 8fa842b21..c5b9b2e22 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -4129,6 +4129,56 @@ TEST(cli_find_cli_not_found) {
PASS();
}
+typedef struct {
+ const char *name;
+ bool found;
+} cli_find_thread_t;
+
+static void *cli_find_on_other_thread(void *opaque) {
+ cli_find_thread_t *context = opaque;
+ const char *result = cbm_find_cli(context->name, NULL);
+ context->found = result && strstr(result, context->name) != NULL;
+ return NULL;
+}
+
+TEST(cli_find_cli_result_is_thread_local) {
+ char temp[256] = "/tmp/cli-find-thread-XXXXXX";
+ ASSERT_NOT_NULL(cbm_mkdtemp(temp));
+ char first[512], second[512];
+ snprintf(first, sizeof(first), "%s/cbm-first.exe", temp);
+ snprintf(second, sizeof(second), "%s/cbm-second.exe", temp);
+ write_test_file(first, "fixture\n");
+ write_test_file(second, "fixture\n");
+ th_make_executable(first);
+ th_make_executable(second);
+ const char *raw = getenv("PATH");
+ char *old_path = raw ? strdup(raw) : NULL;
+ ASSERT_TRUE(!raw || old_path);
+ bool environment_ready = cbm_setenv("PATH", temp, 1) == 0;
+ const char *retained = cbm_find_cli("cbm-first.exe", NULL);
+ cli_find_thread_t context = {.name = "cbm-second.exe"};
+ cbm_thread_t thread;
+ bool started =
+ environment_ready && cbm_thread_create(&thread, 0, cli_find_on_other_thread, &context) == 0;
+ bool joined = started && cbm_thread_join(&thread) == 0;
+ /* The other call has completed: this is an ordered overwrite check,
+ * not a timing-dependent attempt to observe a concurrent data race. */
+ bool retained_first = retained && strcmp(retained, first) == 0;
+ if (old_path)
+ cbm_setenv("PATH", old_path, 1);
+ else
+ cbm_unsetenv("PATH");
+ free(old_path);
+ bool cleaned = th_rmtree(temp) == 0;
+ ASSERT_TRUE(environment_ready);
+ ASSERT_TRUE(started);
+ ASSERT_TRUE(joined);
+ ASSERT_TRUE(context.found);
+ ASSERT_TRUE(retained_first);
+ ASSERT_TRUE(cleaned);
+ PASS();
+}
+
TEST(cli_find_cli_on_path) {
/* Port of TestFindCLI_FoundOnPATH */
char tmpdir[256];
@@ -18118,6 +18168,7 @@ SUITE(cli) {
/* CLI binary detection (3 tests — install_test.go) */
RUN_TEST(cli_find_cli_not_found);
+ RUN_TEST(cli_find_cli_result_is_thread_local);
RUN_TEST(cli_find_cli_on_path);
RUN_TEST(cli_find_cli_fallback_paths);
diff --git a/tests/test_ui.c b/tests/test_ui.c
index 09829f1a9..a53744594 100644
--- a/tests/test_ui.c
+++ b/tests/test_ui.c
@@ -11,6 +11,7 @@
#include "ui/embedded_assets.h"
#include "ui/layout3d.h"
#include "ui/atlas.h"
+#include "cli/cli.h"
#include "store/store.h"
#include "foundation/subprocess.h"
#include "foundation/mem_core.h"
@@ -2316,7 +2317,16 @@ TEST(atlas_impact_completed_worker_memory_returns_to_caller_baseline) {
}
static bool impact_test_git(const char *root, const char *const *args) {
- const char *argv[30] = {"git",
+ const char *git = "git";
+#ifdef _WIN32
+ char git_path[1024];
+ const char *resolved = cbm_find_cli("git", NULL);
+ if (!resolved || !*resolved || strlen(resolved) >= sizeof(git_path))
+ return false;
+ snprintf(git_path, sizeof(git_path), "%s", resolved);
+ git = git_path;
+#endif
+ const char *argv[30] = {git,
"-C",
root,
"-c",
@@ -2328,7 +2338,8 @@ static bool impact_test_git(const char *root, const char *const *args) {
int n = 9;
while (*args && n < 29)
argv[n++] = *args++;
- cbm_proc_opts_t opts = {.bin = "git", .argv = argv, .quiet_timeout_ms = 3000};
+ cbm_proc_opts_t opts = {
+ .bin = git, .argv = argv, .quiet_timeout_ms = 3000, .strip_git_repo_env = true};
cbm_proc_result_t result;
return cbm_subprocess_run(&opts, &result) == 0 && result.outcome == CBM_PROC_CLEAN;
}