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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
Complete child-local environments and ObjectScript parse deadlines
Martin Vogelafeaca0217f8
diff --git a/src/foundation/subprocess.c b/src/foundation/subprocess.cindex 196165cea..c574080da 100644--- a/src/foundation/subprocess.c+++ b/src/foundation/subprocess.c@@ -26,9 +26,10 @@#include <fcntl.h>#include <signal.h>#ifdef __APPLE__+#include <paths.h>#include <spawn.h>-extern char **environ;#endif+extern char **environ;#include <sys/stat.h>#include <sys/wait.h>#include <unistd.h>@@ -426,11 +427,16 @@ struct cbm_subprocess {cbm_proc_result_t result;#ifdef _WIN32+ wchar_t *environment;HANDLE process;HANDLE job;DWORD process_id;bool root_forced;#else+ char **environment;+#ifdef __APPLE__+ char *search_path;+#endifpid_t pid;pid_t pgid;#endif@@ -446,6 +452,133 @@ static void cbm_subprocess_result_init(cbm_proc_result_t *result) {result->supervision_failed = false;}+#ifdef _WIN32+/* Preserve the native Unicode environment, including Windows' hidden =C:+ * drive entries. Names compare ordinally without case, independent of locale. */+static size_t cbm_win_environment_name_length(const wchar_t *entry) {+ const wchar_t *equals = wcschr(entry + (entry[0] == L'=' ? 1 : 0), L'=');+ return equals ? (size_t)(equals - entry) : wcslen(entry);+}++static int cbm_win_environment_compare(const void *left, const void *right) {+ const wchar_t *a = *(const wchar_t *const *)left;+ const wchar_t *b = *(const wchar_t *const *)right;+ return CompareStringOrdinal(a, (int)cbm_win_environment_name_length(a), b,+ (int)cbm_win_environment_name_length(b), TRUE) -+ CSTR_EQUAL;+}++static bool cbm_subprocess_copy_environment(cbm_subprocess_t *process, const char *const *overrides,+ size_t override_count) {+ wchar_t *inherited = GetEnvironmentStringsW();+ if (!inherited) {+ return false;+ }+ size_t count = 0;+ for (const wchar_t *entry = inherited; *entry; entry += wcslen(entry) + 1) {+ count++;+ }+ wchar_t **entries = calloc(count + override_count, sizeof(*entries));+ wchar_t **converted = calloc(override_count, sizeof(*converted));+ bool ok = false;+ if (!entries || !converted) {+ goto done;+ }+ size_t index = 0;+ for (wchar_t *entry = inherited; *entry; entry += wcslen(entry) + 1) {+ entries[index++] = entry;+ }+ for (size_t i = 0; i < override_count; i++) {+ converted[i] = cbm_utf8_to_wide(overrides[i]);+ if (!converted[i]) {+ goto done;+ }+ size_t slot = 0;+ while (slot < count && cbm_win_environment_compare(&entries[slot], &converted[i]) != 0) {+ slot++;+ }+ entries[slot] = converted[i];+ if (slot == count) {+ count++;+ }+ }+ qsort(entries, count, sizeof(*entries), cbm_win_environment_compare);+ size_t length = 1; /* Final NUL, in addition to each entry's terminator. */+ for (size_t i = 0; i < count; i++) {+ size_t entry_length = wcslen(entries[i]) + 1;+ if (entry_length > SIZE_MAX / sizeof(wchar_t) - length) {+ goto done;+ }+ length += entry_length;+ }+ process->environment = calloc(length, sizeof(*process->environment));+ if (!process->environment) {+ goto done;+ }+ wchar_t *next = process->environment;+ for (size_t i = 0; i < count; i++) {+ size_t entry_length = wcslen(entries[i]) + 1;+ wmemcpy(next, entries[i], entry_length);+ next += entry_length;+ }+ ok = true;+done:+ if (converted) {+ for (size_t i = 0; i < override_count; i++) {+ free(converted[i]);+ }+ }+ free(converted);+ free(entries);+ FreeEnvironmentStringsW(inherited);+ return ok;+}+#else+static bool cbm_subprocess_copy_environment(cbm_subprocess_t *process, const char *const *overrides,+ size_t override_count) {+#ifdef __APPLE__+ const char *search_path = getenv("PATH");+ process->search_path = cbm_strdup(search_path ? search_path : _PATH_DEFPATH);+ if (!process->search_path) {+ return false;+ }+#endif+ size_t count = 0;+ while (environ && environ[count]) {+ count++;+ }+ process->environment = calloc(count + override_count + 1, sizeof(*process->environment));+ if (!process->environment) {+ return false;+ }+ for (size_t i = 0; i < count; i++) {+ process->environment[i] = cbm_strdup(environ[i]);+ if (!process->environment[i]) {+ return false;+ }+ }+ for (size_t i = 0; i < override_count; i++) {+ size_t name_length = (size_t)(strchr(overrides[i], '=') - overrides[i]);+ size_t slot = 0;+ while (slot < count &&+ !(strncmp(process->environment[slot], overrides[i], name_length) == 0 &&+ process->environment[slot][name_length] == '=')) {+ slot++;+ }+ char *entry = cbm_strdup(overrides[i]);+ if (!entry) {+ return false;+ }+ free(process->environment[slot]);+ process->environment[slot] = entry;+ if (slot == count) {+ count++;+ }+ }+ return true;+}+#endif+static void cbm_subprocess_free_config(cbm_subprocess_t *process) {if (!process) {return;@@ -456,6 +589,17 @@ static void cbm_subprocess_free_config(cbm_subprocess_t *process) {}}free(process->argv);+#ifndef _WIN32+ if (process->environment) {+ for (size_t i = 0; process->environment[i]; i++) {+ free(process->environment[i]);+ }+ }+#endif+ free(process->environment);+#ifdef __APPLE__+ free(process->search_path);+#endiffree(process->bin);free(process->windows_cmd_payload);free(process->log_file);@@ -477,6 +621,21 @@ static cbm_subprocess_t *cbm_subprocess_copy_opts(const cbm_proc_opts_t *opts) {}#endif+ size_t override_count = 0;+ if (opts->env_overrides) {+ while (override_count < CBM_SUBPROCESS_ARGV_LIMIT && opts->env_overrides[override_count]) {+ const char *entry = opts->env_overrides[override_count];+ const char *equals = strchr(entry, '=');+ if (!equals || equals == entry) {+ return NULL;+ }+ override_count++;+ }+ if (override_count == CBM_SUBPROCESS_ARGV_LIMIT) {+ return NULL;+ }+ }+size_t argc = 1;if (opts->argv) {argc = 0;@@ -492,6 +651,11 @@ static cbm_subprocess_t *cbm_subprocess_copy_opts(const cbm_proc_opts_t *opts) {if (!process) {return NULL;}+ if (override_count > 0 &&+ !cbm_subprocess_copy_environment(process, opts->env_overrides, override_count)) {+ cbm_subprocess_free_config(process);+ return NULL;+ }process->bin = cbm_strdup(opts->bin);process->windows_cmd_payload =opts->windows_cmd_payload ? cbm_strdup(opts->windows_cmd_payload) : NULL;@@ -736,9 +900,9 @@ static int cbm_subprocess_spawn_win(cbm_subprocess_t *process) {PROCESS_INFORMATION child;ZeroMemory(&child, sizeof(child));DWORD flags = EXTENDED_STARTUPINFO_PRESENT | CREATE_SUSPENDED | CREATE_NEW_PROCESS_GROUP |- CREATE_NO_WINDOW;- BOOL created = CreateProcessW(wbin, wcmdline, NULL, NULL, TRUE, flags, NULL, NULL,- &startup.StartupInfo, &child);+ CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT;+ BOOL created = CreateProcessW(wbin, wcmdline, NULL, NULL, TRUE, flags, process->environment,+ NULL, &startup.StartupInfo, &child);cbm_win_close_spawn_handles(nul, log, attrs, attrs_init);free(wbin);free(wcmdline);@@ -1047,7 +1211,20 @@ static void cbm_posix_child_exec(cbm_subprocess_t *process, int input, int outpu/* A fixed literal tool name (for example "git" or "curl") uses the* caller's normal PATH without introducing a shell. An explicit path* still has execvp's exact-path semantics because it contains '/'. */- execvp(process->bin, process->argv);+ if (process->environment) {+#ifdef __APPLE__+ /* Only this child changes environ. execvP keeps the caller's PATH for+ * executable lookup, including execvp's ENOEXEC shell fallback. */+ environ = process->environment;+ execvP(process->bin, process->search_path, process->argv);+#else+ /* execvpe searches the inherited caller PATH while passing the owned+ * environment to the executed image; the parent's environ is untouched. */+ execvpe(process->bin, process->argv, process->environment);+#endif+ } else {+ execvp(process->bin, process->argv);+ }_exit(127);}@@ -1121,8 +1298,10 @@ static int cbm_posix_spawn_apple(cbm_subprocess_t *process, int input, int outpuposix_spawn_file_actions_adddup2(&actions, output, STDOUT_FILENO) == 0 &&posix_spawn_file_actions_adddup2(&actions, output, STDERR_FILENO) == 0;pid_t pid = -1;- int rc =- configured ? posix_spawnp(&pid, process->bin, &actions, &attr, process->argv, environ) : -1;+ char **environment = process->environment ? process->environment : environ;+ int rc = configured+ ? posix_spawnp(&pid, process->bin, &actions, &attr, process->argv, environment)+ : -1;(void)posix_spawn_file_actions_destroy(&actions);(void)posix_spawnattr_destroy(&attr);if (configured && rc == 0 && pid > 0) {diff --git a/src/foundation/subprocess.h b/src/foundation/subprocess.hindex 62592c592..010b20665 100644--- a/src/foundation/subprocess.h+++ b/src/foundation/subprocess.h@@ -53,22 +53,28 @@ typedef struct {typedef void (*cbm_proc_log_cb)(const char *line, void *ud);typedef struct {- const char *bin; /* executable path or literal PATH name;- * also argv[0] when argv is NULL */- const char *const *argv; /* NULL-terminated argv; NULL => { bin, NULL } */- const char *windows_cmd_payload; /* Windows-only cmd.exe command text. When set, bin must- * be an absolute path ending in cmd.exe and argv must be- * NULL. The fixed /D /S /V:OFF /C prefix is added while- * this payload is copied verbatim for cmd.exe to parse. */- const char *log_file; /* child stdout+stderr are redirected here and tailed;- * NULL => discard child output, no tailing */- cbm_proc_log_cb on_log_line; /* optional per-line callback */- void *log_ud; /* user data for on_log_line */- int quiet_timeout_ms; /* <= 0 => no timeout; else kill+HANG after this many- * ms with no new completed log line */- int cancel_grace_ms; /* graceful tree-termination window; <= 0 uses the finite- * CBM_SUBPROCESS_DEFAULT_CANCEL_GRACE_MS */- bool delete_log_on_exit; /* unlink log_file after reaping */+ const char *bin; /* executable path or literal PATH name;+ * also argv[0] when argv is NULL */+ const char *const *argv; /* NULL-terminated argv; NULL => { bin, NULL } */+ const char *const *env_overrides; /* NULL-terminated NAME=value entries, merged with the+ * inherited environment for this child only. NAME= keeps+ * an empty value; the last duplicate wins. Names must be+ * nonempty (case-insensitive on Windows). NULL or an empty+ * list preserves normal inheritance. Executable lookup+ * continues to use the caller's PATH. */+ const char *windows_cmd_payload; /* Windows-only cmd.exe command text. When set, bin must+ * be an absolute path ending in cmd.exe and argv must be+ * NULL. The fixed /D /S /V:OFF /C prefix is added while+ * this payload is copied verbatim for cmd.exe to parse. */+ const char *log_file; /* child stdout+stderr are redirected here and tailed;+ * NULL => discard child output, no tailing */+ cbm_proc_log_cb on_log_line; /* optional per-line callback */+ void *log_ud; /* user data for on_log_line */+ int quiet_timeout_ms; /* <= 0 => no timeout; else kill+HANG after this many+ * ms with no new completed log line */+ int cancel_grace_ms; /* graceful tree-termination window; <= 0 uses the finite+ * CBM_SUBPROCESS_DEFAULT_CANCEL_GRACE_MS */+ bool delete_log_on_exit; /* unlink log_file after reaping */} cbm_proc_opts_t;#define CBM_SUBPROCESS_DEFAULT_CANCEL_GRACE_MS 1000diff --git a/src/pipeline/pass_definitions.c b/src/pipeline/pass_definitions.cindex 7ac98e9cd..0411e843f 100644--- a/src/pipeline/pass_definitions.c+++ b/src/pipeline/pass_definitions.c@@ -570,9 +570,17 @@ static bool objectscript_export_append_error_ranges(CBMFileResult *aggregate,* class independently (preserving the upstream parser behavior), then compose* every extracted carrier into one result for the normal registry/call/usage/* semantic passes. */-CBMFileResult *cbm_pipeline_extract_objectscript_export(+static CBM_TLS cbm_objectscript_export_extractor_t objectscript_export_test_extractor;++void cbm_pipeline_set_objectscript_export_extractor_for_tests(+ cbm_objectscript_export_extractor_t extractor) {+ objectscript_export_test_extractor = extractor;+}++CBMFileResult *cbm_pipeline_extract_objectscript_export_with_timeout(const char *source, int source_len, const char *project_name, const char *rel_path,- const CBMMacroTable *macro_table, const CBMReturnTypeTable *return_type_table) {+ const CBMMacroTable *macro_table, const CBMReturnTypeTable *return_type_table,+ int64_t timeout_micros) {CBMArena export_arena;cbm_arena_init(&export_arena);int class_count = 0;@@ -602,10 +610,13 @@ CBMFileResult *cbm_pipeline_extract_objectscript_export(}aggregate->cached_lang = CBM_LANG_OBJECTSCRIPT_UDL;+ cbm_objectscript_export_extractor_t extract = objectscript_export_test_extractor+ ? objectscript_export_test_extractor+ : cbm_extract_file_ex;for (int ci = 0; ci < class_count; ci++) {- CBMFileResult *part = cbm_extract_file_ex(- udl_strings[ci], (int)strlen(udl_strings[ci]), CBM_LANG_OBJECTSCRIPT_UDL, project_name,- rel_path, CBM_EXTRACT_BUDGET, NULL, NULL, macro_table, return_type_table);+ CBMFileResult *part = extract(udl_strings[ci], (int)strlen(udl_strings[ci]),+ CBM_LANG_OBJECTSCRIPT_UDL, project_name, rel_path,+ timeout_micros, NULL, NULL, macro_table, return_type_table);if (!part) {continue;}@@ -655,6 +666,14 @@ CBMFileResult *cbm_pipeline_extract_objectscript_export(return aggregate;}+CBMFileResult *cbm_pipeline_extract_objectscript_export(+ const char *source, int source_len, const char *project_name, const char *rel_path,+ const CBMMacroTable *macro_table, const CBMReturnTypeTable *return_type_table) {+ return cbm_pipeline_extract_objectscript_export_with_timeout(+ source, source_len, project_name, rel_path, macro_table, return_type_table,+ CBM_EXTRACT_BUDGET);+}+#undef OBJECTSCRIPT_EXPORT_APPEND_ARRAYint cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.hindex 1d65f57ae..394fd8111 100644--- a/src/pipeline/pipeline_internal.h+++ b/src/pipeline/pipeline_internal.h@@ -23,7 +23,7 @@/* ── Shared pipeline constants ─────────────────────────────────── */-/* Maximum byte budget for tree-sitter extraction per file */+/* Maximum tree-sitter parse time per file, in microseconds. */#define CBM_EXTRACT_BUDGET 5000000/* Route node QN buffer size (must fit __route__METHOD__/full/url/path) */@@ -145,6 +145,23 @@ CBMFileResult *cbm_pipeline_extract_objectscript_export(const char *source, int source_len, const char *project_name, const char *rel_path,const CBMMacroTable *macro_table, const CBMReturnTypeTable *return_type_table);+/* Explicit per-class parse deadline for bounded extraction and ownership tests.+ * Zero disables the parse deadline; production callers use the wrapper above. */+CBMFileResult *cbm_pipeline_extract_objectscript_export_with_timeout(+ const char *source, int source_len, const char *project_name, const char *rel_path,+ const CBMMacroTable *macro_table, const CBMReturnTypeTable *return_type_table,+ int64_t timeout_micros);++/* Synchronous thread-local extraction seam for verifying timeout forwarding.+ * NULL restores cbm_extract_file_ex; tests must restore it before asserting. */+typedef CBMFileResult *(*cbm_objectscript_export_extractor_t)(const char *, int, CBMLanguage,+ const char *, const char *, int64_t,+ const char **, const char **,+ const CBMMacroTable *,+ const CBMReturnTypeTable *);+void cbm_pipeline_set_objectscript_export_extractor_for_tests(+ cbm_objectscript_export_extractor_t extractor);+/* Materialize CONFIGURES edges from one extracted file's env-access carriers.* Shared by sequential definition processing and the parallel cache registry. */int cbm_pipeline_create_env_configures_for_file(cbm_pipeline_ctx_t *ctx,diff --git a/tests/test_main.c b/tests/test_main.cindex 83c263374..47b23a53d 100644--- a/tests/test_main.c+++ b/tests/test_main.c@@ -791,6 +791,7 @@ extern void suite_discover(void);extern void suite_graph_buffer(void);extern void suite_registry(void);extern void suite_pipeline(void);+extern void suite_pipeline_objectscript_timeout(void);extern void suite_importance(void);extern void suite_pipeline_semantic_manifest_repro(void);extern void suite_cross_repo(void);@@ -895,7 +896,19 @@ extern void suite_dump_verify_io(void);* caches at thread teardown (pass_parallel.c). */extern void cbm_kind_in_set_free_cache(void);+const char *cbm_test_program_path;+int main(int argc, char **argv) {+ cbm_test_program_path = argv[0];+ if (argc >= 4 && argc % 2 == 0 && strcmp(argv[1], "__cbm_env_values") == 0) {+ for (int i = 2; i < argc; i += 2) {+ const char *value = getenv(argv[i]);+ if (!value || strcmp(value, argv[i + 1]) != 0) {+ return 61;+ }+ }+ return 0;+ }/* Skip the multi-hundred-MB executable-image hash that computes the exact* build fingerprint: it is tens of seconds per spawned worker/daemon under* ASan on constrained CI runners and the sole cause of the daemon-family@@ -1067,6 +1080,7 @@ int main(int argc, char **argv) {/* Pipeline (M8) */RUN_SELECTED_SUITE(registry);RUN_SELECTED_SUITE(pipeline);+ RUN_SELECTED_SUITE(pipeline_objectscript_timeout);RUN_SELECTED_SUITE(importance);RUN_SELECTED_SUITE(index_format);RUN_SELECTED_SUITE(pipeline_semantic_manifest_repro);diff --git a/tests/test_pipeline.c b/tests/test_pipeline.cindex cf40dd57a..45c2bf7af 100644--- a/tests/test_pipeline.c+++ b/tests/test_pipeline.c@@ -1498,6 +1498,67 @@ static bool test_buffer_appendf(char *buffer, size_t capacity, size_t *used, conreturn true;}+static CBM_TLS int objectscript_timeout_calls;+static CBM_TLS int64_t objectscript_expected_timeout;+static CBM_TLS bool objectscript_timeout_mismatch;++static CBMFileResult *test_objectscript_timeout_extractor(+ const char *source, int source_len, CBMLanguage language, const char *project,+ const char *rel_path, int64_t timeout_micros, const char **extra_defines,+ const char **include_paths, const CBMMacroTable *macro_table,+ const CBMReturnTypeTable *return_type_table) {+ (void)source;+ (void)source_len;+ (void)language;+ (void)project;+ (void)rel_path;+ (void)extra_defines;+ (void)include_paths;+ (void)macro_table;+ (void)return_type_table;+ objectscript_timeout_calls++;+ objectscript_timeout_mismatch |= timeout_micros != objectscript_expected_timeout;+ CBMFileResult *result = calloc(1, sizeof(*result));+ if (result) {+ cbm_arena_init(&result->arena);+ }+ return result;+}++TEST(pipeline_objectscript_export_forwards_parse_timeout) {+ const char *xml =+ "<Export generator=\"Cache\"><Class name=\"Timeout.First\">"+ "<Method name=\"Run\"><ClassMethod>1</ClassMethod>"+ "<Implementation><![CDATA[ Quit ]]></Implementation></Method></Class>"+ "<Class name=\"Timeout.Second\">"+ "<Method name=\"Run\"><ClassMethod>1</ClassMethod>"+ "<Implementation><![CDATA[ Quit ]]></Implementation></Method></Class></Export>";+ const int64_t timeouts[] = {0, 17, 5000000};+ for (size_t i = 0; i < sizeof(timeouts) / sizeof(timeouts[0]); i++) {+ objectscript_timeout_calls = 0;+ objectscript_expected_timeout = timeouts[i];+ objectscript_timeout_mismatch = false;+ cbm_pipeline_set_objectscript_export_extractor_for_tests(+ test_objectscript_timeout_extractor);+ CBMFileResult *result =+ i == 2 ? cbm_pipeline_extract_objectscript_export(xml, (int)strlen(xml), "timeout",+ "export.xml", NULL, NULL)+ : cbm_pipeline_extract_objectscript_export_with_timeout(+ xml, (int)strlen(xml), "timeout", "export.xml", NULL, NULL, timeouts[i]);+ cbm_pipeline_set_objectscript_export_extractor_for_tests(NULL);+ bool complete = result && result->owned_result_count == 2;+ cbm_free_result(result);+ ASSERT_TRUE(complete);+ ASSERT_EQ(objectscript_timeout_calls, 2);+ ASSERT_FALSE(objectscript_timeout_mismatch);+ }+ PASS();+}++SUITE(pipeline_objectscript_timeout) {+ RUN_TEST(pipeline_objectscript_export_forwards_parse_timeout);+}+/* Each extracted class owns a separate arena. A CBMCall includes eight argument* slots, so growing its call array through the 513th item requires at least four* arena blocks per class. Sixty-four classes therefore exceed the aggregate'sdiff --git a/tests/test_subprocess.c b/tests/test_subprocess.cindex 1554821c6..0fb5ba3d7 100644--- a/tests/test_subprocess.c+++ b/tests/test_subprocess.c@@ -21,6 +21,7 @@#ifndef _WIN32#include <fcntl.h>#include <signal.h>+#include <sys/stat.h>#include <sys/time.h>#include <unistd.h>#else@@ -87,6 +88,78 @@ TEST(subprocess_outcome_str) {/* ── Layer 2: real spawn/reap (POSIX) ─────────────────────────────────────── */+extern const char *cbm_test_program_path;++TEST(subprocess_environment_is_child_local) {+ const char *names[] = {"CBM_ENV_TEST_VALUE", "CBM_ENV_TEST_KEEP", "CBM_ENV_TEST_EMPTY",+ "CBM_ENV_TEST_NEW"};+ char *saved[4] = {0};+ for (int i = 0; i < 4; i++) {+ const char *value = getenv(names[i]);+ saved[i] = value ? cbm_strdup(value) : NULL;+ if (i == 3) {+ (void)cbm_unsetenv(names[i]);+ } else {+ (void)cbm_setenv(names[i], i == 1 ? "kept" : "parent", 1);+ }+ }+ const char *args[] = {cbm_test_program_path,+ "__cbm_env_values",+ names[0],+ "child",+ names[1],+ "kept",+ names[2],+ "",+ names[3],+ "last=tail",+ NULL};+ const char *overrides[] = {+#ifdef _WIN32+ "cbm_env_test_value=child",+#else+ "CBM_ENV_TEST_VALUE=child",+#endif+ "CBM_ENV_TEST_EMPTY=", "CBM_ENV_TEST_NEW=first", "CBM_ENV_TEST_NEW=last=tail", NULL};+ cbm_proc_opts_t opts = {.bin = cbm_test_program_path,+ .argv = args,+ .env_overrides = overrides,+ .quiet_timeout_ms = 5000};+ cbm_proc_result_t result;+ int rc = cbm_subprocess_run(&opts, &result);+ bool parent_unchanged = strcmp(getenv(names[0]), "parent") == 0 &&+ strcmp(getenv(names[1]), "kept") == 0 &&+ strcmp(getenv(names[2]), "parent") == 0 && getenv(names[3]) == NULL;+ for (int i = 0; i < 4; i++) {+ if (saved[i]) {+ (void)cbm_setenv(names[i], saved[i], 1);+ } else {+ (void)cbm_unsetenv(names[i]);+ }+ free(saved[i]);+ }+ ASSERT_EQ(rc, 0);+ ASSERT_EQ(result.outcome, CBM_PROC_CLEAN);+ ASSERT_TRUE(parent_unchanged);+ PASS();+}++TEST(subprocess_environment_rejects_invalid_override) {+ const char *args[] = {cbm_test_program_path, "--version", NULL};+ const char *invalid[] = {"MISSING_EQUALS", "", "=value"};+ for (size_t i = 0; i < sizeof(invalid) / sizeof(invalid[0]); i++) {+ const char *overrides[] = {invalid[i], NULL};+ cbm_proc_opts_t opts = {.bin = cbm_test_program_path,+ .argv = args,+ .env_overrides = overrides,+ .quiet_timeout_ms = 5000};+ cbm_proc_result_t result;+ ASSERT_EQ(cbm_subprocess_run(&opts, &result), -1);+ ASSERT_EQ(result.outcome, CBM_PROC_SPAWN_FAILED);+ }+ PASS();+}+#ifndef _WIN32static cbm_proc_result_t run_sh(const char *script, int quiet_timeout_ms) {const char *argv[] = {"/bin/sh", "-c", script, NULL};@@ -145,6 +218,99 @@ TEST(subprocess_run_resolves_literal_binary_name_from_path) {#endif}+#ifndef _WIN32+/* Both the native spawn and ENOEXEC shell fallback must select the caller's+ * tool, even when the child receives a different PATH with the same tool name. */+TEST(subprocess_environment_path_preserves_caller_lookup) {+ char parent_dir[] = "/tmp/cbm-env-parent-XXXXXX";+ char child_dir[] = "/tmp/cbm-env-child-XXXXXX";+ ASSERT_NOT_NULL(mkdtemp(parent_dir));+ if (!mkdtemp(child_dir)) {+ (void)rmdir(parent_dir);+ FAIL("could not create child PATH fixture");+ }+ char parent_tool[PATH_MAX];+ char child_tool[PATH_MAX];+ char path_override[PATH_MAX];+ snprintf(parent_tool, sizeof(parent_tool), "%s/cbm-env-probe", parent_dir);+ snprintf(child_tool, sizeof(child_tool), "%s/cbm-env-probe", child_dir);+ snprintf(path_override, sizeof(path_override), "PATH=%s", child_dir);+ const char *old_path = getenv("PATH");+ char *saved_path = old_path ? cbm_strdup(old_path) : NULL;+ bool had_path = old_path != NULL;+ bool setup = !had_path || saved_path != NULL;+ bool parent_unchanged = true;+ cbm_proc_result_t results[3] = {0};+ int spawn_rc[3] = {-1, -1, -1};+ const char *args[] = {"cbm-env-probe", child_dir, NULL};+ const char *overrides[] = {path_override, NULL};+ cbm_proc_opts_t opts = {+ .bin = args[0], .argv = args, .env_overrides = overrides, .quiet_timeout_ms = 5000};+ for (int i = 0; setup && i < 2; i++) {+ FILE *parent_file = fopen(parent_tool, "w");+ FILE *child_file = fopen(child_tool, "w");+ if (!parent_file || !child_file) {+ if (parent_file) {+ fclose(parent_file);+ }+ if (child_file) {+ fclose(child_file);+ }+ setup = false;+ break;+ }+ const char *prefix = i == 0 ? "#!/bin/sh\n" : "";+ setup = fprintf(parent_file, "%stest \"$PATH\" = \"$1\"\n", prefix) >= 0 &&+ fprintf(child_file, "%sexit 42\n", prefix) >= 0 &&+ fchmod(fileno(parent_file), 0700) == 0 && fchmod(fileno(child_file), 0700) == 0;+ if (fclose(parent_file) != 0) {+ setup = false;+ }+ if (fclose(child_file) != 0) {+ setup = false;+ }+ if (!setup || cbm_setenv("PATH", parent_dir, 1) != 0) {+ setup = false;+ break;+ }+ spawn_rc[i] = cbm_subprocess_run(&opts, &results[i]);+ parent_unchanged =+ parent_unchanged && getenv("PATH") && strcmp(getenv("PATH"), parent_dir) == 0;+ }+ if (setup) {+ /* ENOENT must remain a missing caller-side tool, even though an+ * executable with the same name exists only in the child's PATH. */+ setup = unlink(parent_tool) == 0;+ if (setup) {+ spawn_rc[2] = cbm_subprocess_run(&opts, &results[2]);+ parent_unchanged =+ parent_unchanged && getenv("PATH") && strcmp(getenv("PATH"), parent_dir) == 0;+ }+ }+ if (saved_path) {+ (void)cbm_setenv("PATH", saved_path, 1);+ } else if (!had_path) {+ (void)cbm_unsetenv("PATH");+ }+ free(saved_path);+ (void)unlink(parent_tool);+ (void)unlink(child_tool);+ (void)rmdir(parent_dir);+ (void)rmdir(child_dir);+ ASSERT_TRUE(setup);+ ASSERT_TRUE(parent_unchanged);+ for (int i = 0; i < 2; i++) {+ ASSERT_EQ(spawn_rc[i], 0);+ ASSERT_EQ(results[i].exit_code, 0);+ ASSERT_EQ(results[i].outcome, CBM_PROC_CLEAN);+ }+ ASSERT_EQ(spawn_rc[2], 0);+ ASSERT_EQ(results[2].exit_code, 127);+ ASSERT_EQ(results[2].outcome, CBM_PROC_EXIT_NONZERO);+ PASS();+}+#endif+/* A child that dies of SIGSEGV must classify as CRASH — NOT exit_nonzero and NOT* killed. This is the whole point of the primitive: distinguish a crash from a* clean failure so the supervisor can quarantine the culprit. */@@ -1213,6 +1379,11 @@ SUITE(subprocess) {RUN_TEST(subprocess_classify_non_fault_signal_is_killed);RUN_TEST(subprocess_classify_timeout_dominates);RUN_TEST(subprocess_outcome_str);+ RUN_TEST(subprocess_environment_is_child_local);+ RUN_TEST(subprocess_environment_rejects_invalid_override);+#ifndef _WIN32+ RUN_TEST(subprocess_environment_path_preserves_caller_lookup);+#endifRUN_TEST(subprocess_run_clean);RUN_TEST(subprocess_run_exit_nonzero);RUN_TEST(subprocess_run_resolves_literal_binary_name_from_path);