Skip to content

Commit b053dae

Browse files
committed
mcp: a session next to another build is refused within 5 s, not 30 s
A new MCP session whose build differs from the running daemon's waited the whole 30 s startup budget for the other build to leave before it answered. Clients give up on initialize sooner (Codex after 10 s by default), so the agent saw a dead server and never the reason. The MCP client now waits at most 5 s (MAIN_MCP_CONFLICT_WAIT_MS, never more than the startup budget) for another build's cohort to leave. A session that closes inside that window still lets the new build in. After it, the session answers initialize, tools/list and every tools/call with the reason at once. A lock that is only slow (BUSY, not a conflict) keeps the rest of the startup budget, as before. CLI and hook clients are unchanged. tests/test_mcp_conflict_refusal.sh drives a real daemon and a client with a forced build fingerprint (new CBM_TEST_MCP_CLIENT_BUILD seam, TEST_SEAMS=1 builds only). It asserts the refusal content and that it comes within 12 s; with the old 30 s wait it fails. Local-only, like the hook conflict test. Signed-off-by: chidev <[email protected]>
1 parent 850846a commit b053dae

2 files changed

Lines changed: 137 additions & 3 deletions

File tree

‎src/main.c‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ enum {
6060
* that never finishes, so a command cannot hang indefinitely. */
6161
MAIN_STARTUP_CONTENTION_CEILING_MS = 120000,
6262
MAIN_MCP_STARTUP_TIMEOUT_MS = 30000,
63+
/* How long a new MCP session waits for a daemon of another build to leave
64+
* before it answers with the refusal. Clients time out on initialize
65+
* (Codex after 10 s by default), so the refusal must come well inside that;
66+
* a session that closes within this window still lets the new build in. */
67+
MAIN_MCP_CONFLICT_WAIT_MS = 5000,
6368
MAIN_STARTUP_TIMEOUT_MIN_MS = 1000,
6469
MAIN_STARTUP_TIMEOUT_MAX_MS = 600000,
6570
MAIN_REQUEST_TIMEOUT_MS = 24 * 60 * 60 * 1000,
@@ -80,6 +85,15 @@ enum {
8085
PARENT_WATCHDOG_STACK_SIZE = 256 * CBM_SZ_1K,
8186
};
8287

88+
static uint32_t main_startup_timeout_ms(void);
89+
90+
/* The MCP client's wait for another build's cohort to leave: the short refusal
91+
* window, never longer than the startup budget. */
92+
static uint32_t main_mcp_conflict_wait_ms(void) {
93+
uint32_t budget = main_startup_timeout_ms();
94+
return budget < MAIN_MCP_CONFLICT_WAIT_MS ? budget : MAIN_MCP_CONFLICT_WAIT_MS;
95+
}
96+
8397
/* Daemon startup budget, overridable for loaded hosts: a fixed 30 s races
8498
* whatever else the machine is doing (parallel test waves, CI legs), and a
8599
* cold daemon on a starved host loses through no fault of its own.
@@ -3321,19 +3335,41 @@ int main(int argc, char **argv) {
33213335
if (role == CBM_DAEMON_PROCESS_HOOK_CLIENT && seam_forced_build && seam_forced_build[0]) {
33223336
identity.build_fingerprint = seam_hook_build;
33233337
}
3338+
/* The same seam for an MCP client, so the refusal session is testable
3339+
* against a daemon of this binary (tests/test_mcp_conflict_refusal.sh). */
3340+
static char seam_mcp_build[CBM_DAEMON_BUILD_FINGERPRINT_SIZE];
3341+
const char *seam_mcp_forced =
3342+
cbm_safe_getenv("CBM_TEST_MCP_CLIENT_BUILD", seam_mcp_build, sizeof(seam_mcp_build), NULL);
3343+
if (role == CBM_DAEMON_PROCESS_MCP_CLIENT && seam_mcp_forced && seam_mcp_forced[0]) {
3344+
identity.build_fingerprint = seam_mcp_build;
3345+
}
33243346
#endif
33253347

33263348
cbm_version_cohort_manager_t *client_cohort_manager = cbm_version_cohort_manager_new(endpoint);
33273349
cbm_version_cohort_lease_t *client_cohort_lease = NULL;
33283350
cbm_daemon_conflict_t client_cohort_conflict;
3351+
/* An MCP client waits only the short refusal window for another build to
3352+
* leave; see MAIN_MCP_CONFLICT_WAIT_MS. */
3353+
uint32_t client_cohort_wait_ms =
3354+
role == CBM_DAEMON_PROCESS_HOOK_CLIENT ? MAIN_HOOK_REQUEST_TIMEOUT_MS
3355+
: role == CBM_DAEMON_PROCESS_MCP_CLIENT ? main_mcp_conflict_wait_ms()
3356+
: main_startup_timeout_ms();
33293357
cbm_version_cohort_status_t client_cohort_status =
33303358
client_cohort_manager
33313359
? cbm_version_cohort_acquire(client_cohort_manager, &identity,
3332-
main_deadline_after(role == CBM_DAEMON_PROCESS_HOOK_CLIENT
3333-
? MAIN_HOOK_REQUEST_TIMEOUT_MS
3334-
: main_startup_timeout_ms()),
3360+
main_deadline_after(client_cohort_wait_ms),
33353361
&client_cohort_lease, &client_cohort_conflict)
33363362
: CBM_VERSION_COHORT_IO;
3363+
/* The short window is for a conflict only. A lock that is merely slow on a
3364+
* loaded host (BUSY) keeps the rest of the startup budget, as before. */
3365+
if (client_cohort_manager && role == CBM_DAEMON_PROCESS_MCP_CLIENT &&
3366+
client_cohort_status == CBM_VERSION_COHORT_BUSY &&
3367+
client_cohort_wait_ms < main_startup_timeout_ms()) {
3368+
client_cohort_status = cbm_version_cohort_acquire(
3369+
client_cohort_manager, &identity,
3370+
main_deadline_after(main_startup_timeout_ms() - client_cohort_wait_ms),
3371+
&client_cohort_lease, &client_cohort_conflict);
3372+
}
33373373
if (client_cohort_status != CBM_VERSION_COHORT_OK) {
33383374
char message[CBM_DAEMON_CONFLICT_MESSAGE_SIZE];
33393375
bool formatted =

‎tests/test_mcp_conflict_refusal.sh‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env bash
2+
# A new MCP session next to a daemon of a DIFFERENT build must answer MCP with
3+
# the reason, and soon: clients give up on `initialize` after a few seconds
4+
# (Codex after 10 s by default). The session waits at most
5+
# MAIN_MCP_CONFLICT_WAIT_MS (5 s) for the other build to leave, then answers
6+
# initialize with the reason as instructions and every tools/call as a tool
7+
# error carrying it. Requires a TEST_SEAMS=1 binary: CBM_TEST_MCP_CLIENT_BUILD
8+
# forces the client fingerprint.
9+
#
10+
# Local-only, like tests/test_hook_conflict_notice.sh: on upstream CI legs a
11+
# forced fingerprint has been seen to join without a cohort conflict. Run it by
12+
# hand: make -f Makefile.cbm cbm TEST_SEAMS=1 && bash tests/test_mcp_conflict_refusal.sh
13+
set -euo pipefail
14+
15+
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
16+
BINARY="${CBM_TEST_BINARY:-${ROOT}/build/c/codebase-memory-mcp}"
17+
if [[ ! -x "${BINARY}" && -x "${BINARY}.exe" ]]; then
18+
BINARY="${BINARY}.exe"
19+
fi
20+
if [[ ! -x "${BINARY}" ]]; then
21+
echo "missing binary: ${BINARY}" >&2
22+
exit 2
23+
fi
24+
if ! LC_ALL=C grep -a -q -F 'CBM_TEST_MCP_CLIENT_BUILD' "${BINARY}"; then
25+
echo "binary lacks the MCP-client build seam: ${BINARY}" >&2
26+
echo " rebuild it with: make -f Makefile.cbm cbm TEST_SEAMS=1" >&2
27+
exit 2
28+
fi
29+
30+
# shellcheck source=../scripts/test-runtime.sh
31+
source "${ROOT}/scripts/test-runtime.sh"
32+
cbm_test_runtime_init
33+
tmpdir="${CBM_TEST_RUNTIME_ROOT}"
34+
cleanup() {
35+
CBM_CACHE_DIR="${tmpdir}/cache" "${BINARY}" daemon stop >/dev/null 2>&1 || true
36+
cbm_test_runtime_cleanup "${BINARY}"
37+
}
38+
trap cleanup EXIT
39+
40+
if ! CBM_CACHE_DIR="${tmpdir}/cache" "${BINARY}" daemon start >"${tmpdir}/daemon-start.log" 2>&1; then
41+
echo "daemon start failed on this host - cannot exercise the conflict path" >&2
42+
cat "${tmpdir}/daemon-start.log" >&2
43+
exit 2
44+
fi
45+
46+
forced_build="eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
47+
requests='{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"conflict-test","version":"1"}}}
48+
{"jsonrpc":"2.0","method":"notifications/initialized"}
49+
{"jsonrpc":"2.0","id":2,"method":"tools/list"}
50+
{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"list_projects","arguments":{}}}'
51+
52+
# The daemon's cohort join completes asynchronously after `daemon start`
53+
# returns: retry until a session observes the conflict, with a bounded backstop.
54+
out=""
55+
elapsed=0
56+
for _attempt in $(seq 1 10); do
57+
start=${SECONDS}
58+
set +e
59+
out="$(printf '%s\n' "${requests}" | CBM_CACHE_DIR="${tmpdir}/cache" \
60+
CBM_TEST_MCP_CLIENT_BUILD="${forced_build}" \
61+
"${BINARY}" 2>"${tmpdir}/session.err")"
62+
rc=$?
63+
set -e
64+
elapsed=$((SECONDS - start))
65+
if grep -q "conflicting CBM process" "${tmpdir}/session.err"; then
66+
break
67+
fi
68+
sleep 0.5
69+
done
70+
71+
if ! grep -q "conflicting CBM process" "${tmpdir}/session.err"; then
72+
echo "no session observed the build conflict - the daemon cohort never became active" >&2
73+
cat "${tmpdir}/session.err" >&2
74+
CBM_CACHE_DIR="${tmpdir}/cache" "${BINARY}" daemon status >&2 2>&1 || true
75+
exit 2
76+
fi
77+
fail() {
78+
echo "$1" >&2
79+
echo "--- stdout ---" >&2
80+
printf '%s\n' "${out}" >&2
81+
echo "--- stderr ---" >&2
82+
cat "${tmpdir}/session.err" >&2
83+
exit 1
84+
}
85+
[[ ${rc} -ne 0 ]] || fail "a refused session must exit non-zero; got 0"
86+
# 5 s wait, plus process start and the remedy probe; 30 s was the old wait.
87+
[[ ${elapsed} -le 12 ]] || fail "the refusal took ${elapsed} s; it must come within the 5 s window"
88+
init="$(printf '%s\n' "${out}" | grep '"id":1' || true)"
89+
list="$(printf '%s\n' "${out}" | grep '"id":2' || true)"
90+
call="$(printf '%s\n' "${out}" | grep '"id":3' || true)"
91+
printf '%s' "${init}" | grep -q 'is unavailable in this session' ||
92+
fail "initialize must carry the reason as instructions"
93+
printf '%s' "${list}" | grep -q 'cbm_unavailable' ||
94+
fail "tools/list must offer only cbm_unavailable"
95+
printf '%s' "${call}" | grep -q '"isError":true' || fail "tools/call must be a tool error"
96+
printf '%s' "${call}" | grep -q 'conflicting CBM process' || fail "tools/call must carry the reason"
97+
printf '%s' "${call}" | grep -q 'daemon stop' || fail "the reason must name the way out"
98+
echo "mcp conflict refusal: answered in ${elapsed} s with the reason"

0 commit comments

Comments
 (0)