Skip to content

Commit e73eb9d

Browse files
authored
Merge pull request DeusData#2777 from gaurav0107/fix/1818-daemon-autoindex-skipped-omits-the-effec
fix(daemon): log the effective auto_index_limit in daemon.autoindex.skipped
2 parents 70cd1a5 + ca41963 commit e73eb9d

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎src/daemon/application.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2581,10 +2581,12 @@ static void application_background_initialize_impl(cbm_daemon_application_sessio
25812581
cbm_mcp_auto_index_within_file_limit(root_path, auto_index_limit, &tracked_files);
25822582
if (auto_index_candidate && !within_auto_index_limit) {
25832583
char files[32];
2584+
char limit[32];
25842585
(void)snprintf(files, sizeof(files), "%d", tracked_files);
2586+
(void)snprintf(limit, sizeof(limit), "%d", auto_index_limit);
25852587
cbm_log_warn("daemon.autoindex.skipped", "project", project, "reason",
25862588
tracked_files >= 0 ? "too_many_files" : "unsafe_or_unavailable_path", "files",
2587-
files);
2589+
files, "limit", limit);
25882590
}
25892591
bool args_required = auto_index_candidate && within_auto_index_limit;
25902592
char *args = args_required ? application_auto_index_args(application, root_path) : NULL;

‎tests/test_daemon_application.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "foundation/compat.h"
1313
#include "foundation/compat_fs.h"
1414
#include "foundation/compat_thread.h"
15+
#include "foundation/log.h"
1516
#include "foundation/platform.h"
1617
#include "foundation/sha256.h"
1718
#include "foundation/subprocess.h"
@@ -2932,6 +2933,38 @@ TEST(daemon_application_programmatic_index_forwards_set_doc_candidates) {
29322933
ASSERT_EQ(garbage, -1);
29332934
PASS();
29342935
}
2936+
2937+
/* #1818: the daemon.autoindex.skipped warning must name the effective limit,
2938+
* like the MCP autoindex.skip event does (#1466). The warning is emitted on the
2939+
* background-initialize thread, before the release increment of the
2940+
* background-initialize counter that the test waits for, so the captured line
2941+
* is complete once that wait returns. */
2942+
static char app_autoindex_skipped_line[1024];
2943+
static atomic_bool app_autoindex_skipped_seen = ATOMIC_VAR_INIT(false);
2944+
2945+
static void app_autoindex_skipped_sink(const char *line) {
2946+
if (!line || !strstr(line, "msg=daemon.autoindex.skipped") ||
2947+
atomic_load_explicit(&app_autoindex_skipped_seen, memory_order_acquire)) {
2948+
return;
2949+
}
2950+
(void)snprintf(app_autoindex_skipped_line, sizeof(app_autoindex_skipped_line), "%s", line);
2951+
atomic_store_explicit(&app_autoindex_skipped_seen, true, memory_order_release);
2952+
}
2953+
2954+
/* True when `line` carries the whole key=value field `field` (not a prefix of
2955+
* a longer value such as limit=00). */
2956+
static bool app_log_line_has_field(const char *line, const char *field) {
2957+
size_t length = strlen(field);
2958+
for (const char *at = strstr(line, field); at; at = strstr(at + 1, field)) {
2959+
bool starts = at == line || at[-1] == ' ';
2960+
char next = at[length];
2961+
if (starts && (next == '\0' || next == ' ' || next == '\n' || next == '\r')) {
2962+
return true;
2963+
}
2964+
}
2965+
return false;
2966+
}
2967+
29352968
TEST(daemon_application_auto_index_honors_tracked_file_limit) {
29362969
app_env_backup_t cache_environment;
29372970
bool cache_saved = app_env_backup_capture(&cache_environment, "CBM_CACHE_DIR");
@@ -2976,13 +3009,25 @@ TEST(daemon_application_auto_index_honors_tracked_file_limit) {
29763009
cbm_daemon_application_runtime_callbacks(application);
29773010
cbm_daemon_runtime_application_session_t *session =
29783011
application ? app_test_open(&callbacks, 4190) : NULL;
3012+
app_autoindex_skipped_line[0] = '\0';
3013+
atomic_store_explicit(&app_autoindex_skipped_seen, false, memory_order_release);
3014+
CBMLogLevel previous_log_level = cbm_log_get_level();
3015+
CBMLogFormat previous_log_format = cbm_log_get_format();
3016+
cbm_log_set_level(CBM_LOG_INFO);
3017+
cbm_log_set_format(CBM_LOG_FORMAT_TEXT);
3018+
cbm_log_set_sink_ex(app_autoindex_skipped_sink, CBM_LOG_SINK_REPLACE);
29793019
int bg_baseline = cbm_daemon_application_background_initializes_for_test();
29803020
bool initialized = app_test_initialize_profile(&callbacks, session, root,
29813021
CBM_MCP_TOOL_PROFILE_ALL, NULL, NULL);
29823022
bool admission_evaluated = app_wait_for_background_initializes(bg_baseline + 1);
29833023
bool limit_prevented_admission = initialized && admission_evaluated &&
29843024
atomic_load(&fake.starts) == 0 &&
29853025
cbm_daemon_application_active_jobs(application) == 0;
3026+
cbm_log_set_sink(NULL);
3027+
cbm_log_set_format(previous_log_format);
3028+
cbm_log_set_level(previous_log_level);
3029+
bool skip_logged = admission_evaluated &&
3030+
atomic_load_explicit(&app_autoindex_skipped_seen, memory_order_acquire);
29863031

29873032
if (session) {
29883033
callbacks.session_cancel(callbacks.context, session);
@@ -3002,6 +3047,10 @@ TEST(daemon_application_auto_index_honors_tracked_file_limit) {
30023047
ASSERT_TRUE(config_ready);
30033048
ASSERT_TRUE(initialized);
30043049
ASSERT_TRUE(limit_prevented_admission);
3050+
ASSERT_TRUE(skip_logged);
3051+
ASSERT_TRUE(app_log_line_has_field(app_autoindex_skipped_line, "reason=too_many_files"));
3052+
ASSERT_TRUE(app_log_line_has_field(app_autoindex_skipped_line, "files=1"));
3053+
ASSERT_TRUE(app_log_line_has_field(app_autoindex_skipped_line, "limit=0"));
30053054
ASSERT_TRUE(stopped);
30063055
ASSERT_TRUE(cache_restored);
30073056
PASS();

0 commit comments

Comments
 (0)