Skip to content

Commit 60887e0

Browse files
committed
fix(index): use the per-worker budget and log the host RSS ceiling
Strict is defined against the slice main budgets for one worker. An explicit charged-memory limit above 80% of host memory is lowered, and that tightening is logged. Signed-off-by: 刘冲 <mail@liuchong.dev>
1 parent bfa7681 commit 60887e0

6 files changed

Lines changed: 54 additions & 8 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,9 @@ codebase-memory-mcp config reset auto_index # reset to default
939939
`index_resource_profile` defaults to `off` and accepts `off`, `balanced`, or
940940
`strict`. The six individual resource settings also default to `off`; an
941941
explicit value, including `off`, replaces that dimension of a selected
942-
profile. Exceeding an effective limit fails the complete index attempt rather
942+
profile. `index_max_rss_mb` cannot exceed 80% of detected host memory: a higher
943+
explicit value is lowered to that ceiling and logged. Exceeding an effective
944+
limit fails the complete index attempt rather
943945
than publishing a partial graph, and an existing serving index is preserved.
944946
Storage probes also fail closed when an enabled measurement cannot be
945947
completed. See

‎docs/CONFIGURATION.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Current keys:
9191
| `doc_candidates` | `auto` | Score documentation sections against code to store doc↔code candidates (the possible targets of a section, and the sections that may describe a function or file, each with a judged probability). Unset (`auto`), the index mode decides: on for `full` indexes (the default mode), off for `moderate` and `fast`. Set `true` or `false` to choose for every mode; `config reset doc_candidates` returns to `auto`. The scoring grows with sections × functions: under a second up to ~100k functions, ~15 s at ~400k, ~3 minutes on the Linux kernel. Off stores no candidates and leaves the rest of the graph unchanged; on in `fast` mode stores candidates without adding `fast`'s skipped `SIMILAR_TO` / `SEMANTICALLY_RELATED` edges. `index_repository` also takes `doc_candidates` for a single index, which wins over this setting. Applies from the next index. |
9292
| `watcher_enabled` | `true` | Master switch for the background watcher subsystem. Set `false` to stop the watcher from starting at all — no poll thread and no project registration. Reindex manually with `index_repository` when disabled. Only an open MCP session's own project is ever watched; `index_status` reports the current state in its `watch` object. |
9393
| `watch_non_git` | `false` | Also poll project roots that are **not git repositories**. By default the watcher only follows git projects, so a project indexed from a plain directory is never refreshed after its first index — reindex it manually with `index_repository`. Set `true` to poll such roots on the same adaptive cadence with a file-tree scan: the indexer's own discovery walk (same skip lists, `.gitignore` and `.cbmignore` rules) hashed over each file's path, size and mtime. Any change reindexes once; paths the indexer skips (including cbm's own `.codebase-memory/` output) never trigger. The first poll after the daemon starts reindexes each such project once, since nothing records which tree state the index holds. The scan walks the whole tree every poll, so it costs more than git polling on very large trees. Read once when the daemon starts, like `watcher_enabled`. |
94-
| `index_resource_profile` | `off` | Composed index resource baseline: `off`, `balanced`, or `strict`. |
94+
| `index_resource_profile` | `off` | Composed index resource baseline: `off`, `balanced`, or `strict`. 80% of detected host memory is a hard ceiling on `index_max_rss_mb`. |
9595
| `index_max_files` | `off` | Optional maximum number of accepted source files in one discovery run. |
9696
| `index_max_source_mb` | `off` | Optional maximum accepted source size in MiB in one discovery run. |
9797
| `index_max_rss_mb` | `off` | Optional maximum charged memory in MiB for the complete contained index-worker process tree (`64..1048576`). On macOS this is phys_footprint, not resident size. |

‎docs/INDEX_RESOURCE_LIMITS.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ by default so existing large-repository workloads retain their current behavior.
77
## Resource profiles
88

99
`index_resource_profile` accepts `off`, `balanced`, or `strict` and defaults to
10-
`off`. A profile supplies a baseline; each explicitly stored individual
11-
resource key then replaces its dimension. An explicit `off` disables only that
12-
dimension.
10+
`off`. A profile supplies a baseline. Each explicitly stored individual
11+
resource key then replaces its dimension, and an explicit `off` disables only
12+
that dimension. Charged memory is the exception: 80% of detected host memory
13+
is a hard ceiling. No individual key can raise `index_max_rss_mb` above it,
14+
including an explicit value set while the profile is `off`. When that ceiling
15+
lowers a configured value, the process logs `index.policy.host_rss_clamp` with
16+
`requested_bytes` and `applied_bytes`.
1317

1418
| Dimension | `balanced` | `strict` |
1519
|---|---:|---:|

‎src/daemon/application.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,8 +2368,11 @@ static bool application_index_args_add_policy(cbm_daemon_application_t *applicat
23682368
return false;
23692369
}
23702370
cbm_system_info_t system = cbm_system_info();
2371-
cbm_index_policy_finalize(&policy, (uint64_t)system.total_ram,
2372-
application ? (uint64_t)application->worker_memory_budget_bytes : 0);
2371+
/* Strict measures itself against the per-worker slice, which is what the
2372+
* daemon budgets for one index today. */
2373+
uint64_t soft_budget =
2374+
application ? (uint64_t)cbm_daemon_application_worker_memory_budget_bytes(application) : 0;
2375+
cbm_index_policy_finalize(&policy, (uint64_t)system.total_ram, soft_budget);
23732376
return doc_candidates_encoded && cbm_mcp_index_policy_add_to_args(document, root, &policy);
23742377
}
23752378

‎src/foundation/index_policy.c‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "foundation/index_policy.h"
22

3+
#include "foundation/log.h"
4+
35
#include <stddef.h>
46
#include <stdio.h>
57
#include <string.h>
@@ -197,7 +199,10 @@ void cbm_index_policy_finalize(cbm_index_resource_policy_t *policy, uint64_t hos
197199
policy->max_rss_bytes = limit_value(baseline);
198200
}
199201
}
200-
if (policy->max_rss_bytes.enabled && host_cap > 0 && policy->max_rss_bytes.value > host_cap) {
202+
uint64_t requested_rss = policy->max_rss_bytes.value;
203+
bool host_tightened =
204+
policy->max_rss_bytes.enabled && host_cap > 0 && requested_rss > host_cap;
205+
if (host_tightened) {
201206
policy->max_rss_bytes.value = host_cap;
202207
}
203208
if (policy->max_rss_bytes.enabled) {
@@ -209,6 +214,16 @@ void cbm_index_policy_finalize(cbm_index_resource_policy_t *policy, uint64_t hos
209214
policy->max_rss_bytes.value = maximum;
210215
}
211216
}
217+
if (host_tightened && policy->max_rss_bytes.value != requested_rss) {
218+
char requested_bytes[32];
219+
char applied_bytes[32];
220+
(void)snprintf(requested_bytes, sizeof(requested_bytes), "%llu",
221+
(unsigned long long)requested_rss);
222+
(void)snprintf(applied_bytes, sizeof(applied_bytes), "%llu",
223+
(unsigned long long)policy->max_rss_bytes.value);
224+
cbm_log_warn("index.policy.host_rss_clamp", "requested_bytes", requested_bytes,
225+
"applied_bytes", applied_bytes);
226+
}
212227
}
213228

214229
const char *cbm_index_policy_profile_name(const cbm_index_resource_policy_t *policy) {

‎tests/test_index_policy.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "foundation/compat.h"
66
#include "foundation/constants.h"
77
#include "foundation/index_policy.h"
8+
#include "foundation/log.h"
89
#include "mcp/index_supervisor.h"
910
#include "mcp/mcp.h"
1011
#include "mcp/mcp_internal.h"
@@ -153,22 +154,43 @@ TEST(index_policy_explicit_override_and_off_replace_profile_dimensions) {
153154
PASS();
154155
}
155156

157+
static char g_host_clamp_log[512];
158+
159+
static void capture_host_clamp_log(const char *line) {
160+
(void)snprintf(g_host_clamp_log, sizeof(g_host_clamp_log), "%s", line ? line : "");
161+
}
162+
156163
TEST(index_policy_host_clamp_only_tightens_explicit_rss) {
157164
cbm_index_resource_policy_t policy;
158165
cbm_index_policy_init(&policy);
159166
char error[256];
167+
CBMLogLevel saved_level = cbm_log_get_level();
160168

161169
ASSERT_TRUE(
162170
cbm_index_policy_set(&policy, CBM_INDEX_CONFIG_MAX_RSS_MB, "4096", error, sizeof(error)));
171+
g_host_clamp_log[0] = '\0';
172+
cbm_log_set_level(CBM_LOG_WARN);
173+
cbm_log_set_sink(capture_host_clamp_log);
163174
cbm_index_policy_finalize(&policy, UINT64_C(2048) * CBM_INDEX_MIB_BYTES, 0);
175+
cbm_log_set_sink(NULL);
176+
cbm_log_set_level(saved_level);
164177
ASSERT_EQ(policy.max_rss_bytes.value,
165178
(UINT64_C(2048) * CBM_INDEX_MIB_BYTES * UINT64_C(4)) / UINT64_C(5));
179+
ASSERT_NOT_NULL(strstr(g_host_clamp_log, "index.policy.host_rss_clamp"));
180+
ASSERT_NOT_NULL(strstr(g_host_clamp_log, "requested_bytes=4294967296"));
181+
ASSERT_NOT_NULL(strstr(g_host_clamp_log, "applied_bytes=1717986918"));
166182

167183
cbm_index_policy_init(&policy);
168184
ASSERT_TRUE(
169185
cbm_index_policy_set(&policy, CBM_INDEX_CONFIG_MAX_RSS_MB, "512", error, sizeof(error)));
186+
g_host_clamp_log[0] = '\0';
187+
cbm_log_set_level(CBM_LOG_WARN);
188+
cbm_log_set_sink(capture_host_clamp_log);
170189
cbm_index_policy_finalize(&policy, UINT64_C(2048) * CBM_INDEX_MIB_BYTES, 0);
190+
cbm_log_set_sink(NULL);
191+
cbm_log_set_level(saved_level);
171192
ASSERT_EQ(policy.max_rss_bytes.value, UINT64_C(512) * CBM_INDEX_MIB_BYTES);
193+
ASSERT_EQ(g_host_clamp_log[0], '\0');
172194
PASS();
173195
}
174196

0 commit comments

Comments
 (0)