Skip to content

Commit 61ccd90

Browse files
halindromeclaude
andcommitted
fix: address QA round 2
Retracts an incorrect justification from the round-1 commit (4fc5999) and closes the coverage it was used to excuse. MAJOR — the delay wire-up had no coverage, and the reason given for that was wrong. Round 1 claimed the gating half was "structurally unobservable without an injectable clock". It is not. cbm_watcher_touch zeroes next_poll_ns, so a wire-up that never sets a deadline leaves it at zero and the callback fires on every poll: simply NOT calling touch between polls observes gating deterministically, no clock involved. Only the delay's MAGNITUDE needs one. watcher_index_failure_backoff_gates_repolling_issue2015 covers it — one hard failure, then five polls without touch asserting no re-fork, then a touch proving the retry still gets through (so the test cannot pass by observing a dead watcher). Mutation-verified: deleting the wire-up yields "failing_index_calls == 6, expected 1". MINOR — round 1's F1 was half-fixed. Of the two single-token mutations that finding named, only the streak reset was covered; widening the sustained-failure "==" to ">=" still passed. watcher_sustained_failure_logs_ once_issue2015 drives 14 consecutive failures through a cbm_log_set_sink and asserts exactly one emission. Mutation-verified: ">=" yields "sustained_log_hits == 5, expected 1". All three mutations F1 named are now demonstrated caught rather than argued about: reset removal (round 1), wire-up deletion, and the "==" widening. MINOR — cbm_watcher_index_failure_count reads index_failures under projects_lock while poll_project writes it lock-free from a state snapshot. That is the discipline every other per-project field here already follows, so this documents the visibility contract rather than making one field atomic in isolation: the accessor is a diagnostic and test seam, not a synchronisation point, and must not carry scheduling decisions without atomic accessors first. MINOR — watcher.h still described the pre-F3 backoff contract. It now states the clamp is a floor as well as a cap, why the floor exists for an exported function whose reachable inputs cannot currently hit it, and that negative inputs are treated as zero. Still uncovered, now stated precisely rather than hand-waved: the delay's MAGNITUDE. Nothing asserts that ten failures produce a five-minute delay rather than a five-second one. Gating is proven; the numbers are not, and that does need a controllable clock. Refs DeusData#2015 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01FbrM52pmB1m2SR1vRRoNnf Signed-off-by: Shane McCarron <[email protected]>
1 parent 4fc5999 commit 61ccd90

2 files changed

Lines changed: 164 additions & 5 deletions

File tree

‎src/watcher/watcher.h‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,19 @@ void cbm_watcher_stop(cbm_watcher_t *w);
9292
int cbm_watcher_watch_count(cbm_watcher_t *w);
9393

9494
/* Return a watched project's consecutive hard-index-failure count, or -1 when
95-
* it is not watched. Exposed because the failure state machine (increment on
96-
* a hard error, reset on success) is otherwise unobservable from a test: the
97-
* only other thing it feeds is a poll deadline, and gating on that deadline
98-
* needs a controllable clock the watcher does not have. */
95+
* it is not watched. Exposed so the failure state machine (increment on a
96+
* hard error, reset on success) can be asserted directly rather than inferred
97+
* from the poll deadline it feeds.
98+
*
99+
* Memory visibility: this reads under projects_lock, but poll_project WRITES
100+
* the counter outside that lock — it runs against a state snapshot taken
101+
* while the lock was held, which is the same discipline every other
102+
* per-project field in this struct already follows. Reading concurrently with
103+
* a live poll is therefore formally a data race and may observe a stale
104+
* value; it is a diagnostic and test accessor, not a synchronisation point.
105+
* Single-threaded callers (the tests, and any caller between poll cycles)
106+
* always see the current value. Do not build scheduling decisions on it
107+
* without first giving the counter atomic accessors. */
99108
int cbm_watcher_index_failure_count(cbm_watcher_t *w, const char *project_name);
100109

101110
/* Return the adaptive poll interval (ms) for a given file count. */
@@ -105,7 +114,17 @@ int cbm_watcher_poll_interval_ms(int file_count);
105114
* `consecutive_failures` consecutive hard index failures. Zero failures
106115
* yields `interval_ms` unchanged; each further failure doubles the delay up
107116
* to a fixed ceiling, so a permanently failing project stops re-forking a
108-
* worker at the poll cadence without ever being abandoned. */
117+
* worker at the poll cadence without ever being abandoned.
118+
*
119+
* The result is clamped at BOTH ends: never above the ceiling, and never
120+
* below `interval_ms`, so backing off can only ever delay the next attempt,
121+
* never bring it forward. That lower clamp matters only for an `interval_ms`
122+
* already above the ceiling, which the current constants cannot produce
123+
* (POLL_MAX_MS < the ceiling) — it is stated because this is an exported
124+
* function and the guarantee should hold for any argument a caller passes,
125+
* not only for the ones today's constants generate.
126+
*
127+
* Negative inputs are treated as zero. */
109128
int cbm_watcher_index_backoff_ms(int interval_ms, int consecutive_failures);
110129

111130
/* Classify a stat() errno observed on a watched project root: returns true

‎tests/test_watcher.c‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "../src/foundation/compat.h"
88
#include "../src/foundation/compat_thread.h"
99
#include "../src/foundation/constants.h"
10+
#include "../src/foundation/log.h"
1011
#include "../src/foundation/platform.h"
1112
#include "test_framework.h"
1213
#include "test_helpers.h"
@@ -1873,6 +1874,143 @@ TEST(watcher_index_failure_streak_increments_and_resets_issue2015) {
18731874
PASS();
18741875
}
18751876

1877+
/* The delay WIRE-UP (#2015): a hard failure must actually push the project's
1878+
* next poll out, so a permanently failing project stops re-forking a worker
1879+
* on every cycle. That is this PR's headline behaviour and it is observable
1880+
* without a clock, by simply NOT calling cbm_watcher_touch between polls:
1881+
* touch zeroes next_poll_ns, so a wire-up that never sets a deadline leaves
1882+
* it at 0 and the callback fires on every poll. What still needs a clock is
1883+
* only the delay's MAGNITUDE, not whether gating happens at all. */
1884+
TEST(watcher_index_failure_backoff_gates_repolling_issue2015) {
1885+
char tmpdir[256];
1886+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_watcher_gate_XXXXXX");
1887+
if (!cbm_mkdtemp(tmpdir))
1888+
FAIL("cbm_mkdtemp failed");
1889+
1890+
if (wt_git(tmpdir, "init -q") != 0) {
1891+
th_rmtree(tmpdir);
1892+
FAIL("git init failed");
1893+
}
1894+
{
1895+
char p[300];
1896+
th_write_file(wt_path(p, sizeof(p), tmpdir, "file.txt"), "hello\n");
1897+
}
1898+
wt_git(tmpdir, "add file.txt");
1899+
wt_git(tmpdir, "commit -q -m init");
1900+
1901+
cbm_store_t *store = cbm_store_open_memory();
1902+
cbm_watcher_t *w = cbm_watcher_new(store, failing_index_callback, NULL);
1903+
1904+
cbm_watcher_watch(w, "gate-repo", tmpdir);
1905+
failing_index_calls = 0;
1906+
failing_index_fail_first_n = 100; /* never succeeds */
1907+
1908+
cbm_watcher_poll_once(w); /* baseline */
1909+
1910+
{
1911+
char p[300];
1912+
th_append_file(wt_path(p, sizeof(p), tmpdir, "file.txt"), "world\n");
1913+
}
1914+
wt_git(tmpdir, "add file.txt");
1915+
wt_git(tmpdir, "commit -q -m add-world");
1916+
1917+
/* One failure, forced by touch. */
1918+
cbm_watcher_touch(w, "gate-repo");
1919+
cbm_watcher_poll_once(w);
1920+
ASSERT_EQ(failing_index_calls, 1);
1921+
ASSERT_EQ(cbm_watcher_index_failure_count(w, "gate-repo"), 1);
1922+
1923+
/* Now poll repeatedly WITHOUT touching. The change is still pending (the
1924+
* baseline stays uncommitted per #937), so the ONLY thing that can stop a
1925+
* re-fork is the deadline the failure just scheduled. Before the backoff
1926+
* wire-up existed this project was re-forked at the poll cadence; with no
1927+
* deadline set at all it would fire on every one of these iterations. */
1928+
for (int i = 0; i < 5; i++) {
1929+
cbm_watcher_poll_once(w);
1930+
}
1931+
ASSERT_EQ(failing_index_calls, 1);
1932+
ASSERT_EQ(cbm_watcher_index_failure_count(w, "gate-repo"), 1);
1933+
1934+
/* Control: the watcher is not simply dead — clearing the deadline lets the
1935+
* retry through, which is what keeps #937's at-least-once guarantee. */
1936+
cbm_watcher_touch(w, "gate-repo");
1937+
cbm_watcher_poll_once(w);
1938+
ASSERT_EQ(failing_index_calls, 2);
1939+
ASSERT_EQ(cbm_watcher_index_failure_count(w, "gate-repo"), 2);
1940+
1941+
cbm_watcher_free(w);
1942+
cbm_store_close(store);
1943+
th_rmtree(tmpdir);
1944+
PASS();
1945+
}
1946+
1947+
/* The sustained-failure line must fire EXACTLY once, at the threshold — the
1948+
* other single-token mutation round 1 named (`==` widened to `>=`) would emit
1949+
* it on every failure from the threshold onward and turn a one-shot signal
1950+
* into the log spam the whole change is meant to avoid. */
1951+
static int sustained_log_hits = 0;
1952+
static void sustained_log_sink(const char *line) {
1953+
if (line && strstr(line, "watcher.index.sustained_failure") != NULL) {
1954+
sustained_log_hits++;
1955+
}
1956+
}
1957+
1958+
TEST(watcher_sustained_failure_logs_once_issue2015) {
1959+
char tmpdir[256];
1960+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_watcher_sust_XXXXXX");
1961+
if (!cbm_mkdtemp(tmpdir))
1962+
FAIL("cbm_mkdtemp failed");
1963+
1964+
if (wt_git(tmpdir, "init -q") != 0) {
1965+
th_rmtree(tmpdir);
1966+
FAIL("git init failed");
1967+
}
1968+
{
1969+
char p[300];
1970+
th_write_file(wt_path(p, sizeof(p), tmpdir, "file.txt"), "hello\n");
1971+
}
1972+
wt_git(tmpdir, "add file.txt");
1973+
wt_git(tmpdir, "commit -q -m init");
1974+
1975+
cbm_store_t *store = cbm_store_open_memory();
1976+
cbm_watcher_t *w = cbm_watcher_new(store, failing_index_callback, NULL);
1977+
1978+
cbm_watcher_watch(w, "sust-repo", tmpdir);
1979+
failing_index_calls = 0;
1980+
failing_index_fail_first_n = 100; /* never succeeds */
1981+
1982+
cbm_watcher_poll_once(w); /* baseline */
1983+
1984+
{
1985+
char p[300];
1986+
th_append_file(wt_path(p, sizeof(p), tmpdir, "file.txt"), "world\n");
1987+
}
1988+
wt_git(tmpdir, "add file.txt");
1989+
wt_git(tmpdir, "commit -q -m add-world");
1990+
1991+
sustained_log_hits = 0;
1992+
cbm_log_set_sink(sustained_log_sink);
1993+
1994+
/* Drive well past the threshold (10). touch clears the deadline each time
1995+
* so every iteration actually attempts a reindex. */
1996+
for (int i = 0; i < 14; i++) {
1997+
cbm_watcher_touch(w, "sust-repo");
1998+
cbm_watcher_poll_once(w);
1999+
}
2000+
2001+
cbm_log_set_sink(NULL);
2002+
2003+
ASSERT_EQ(failing_index_calls, 14);
2004+
ASSERT_EQ(cbm_watcher_index_failure_count(w, "sust-repo"), 14);
2005+
/* Exactly one, not four (11..14) and not zero. */
2006+
ASSERT_EQ(sustained_log_hits, 1);
2007+
2008+
cbm_watcher_free(w);
2009+
cbm_store_close(store);
2010+
th_rmtree(tmpdir);
2011+
PASS();
2012+
}
2013+
18762014
TEST(watcher_index_failure_count_unknown_project) {
18772015
cbm_store_t *store = cbm_store_open_memory();
18782016
cbm_watcher_t *w = cbm_watcher_new(store, index_callback, NULL);
@@ -3302,6 +3440,8 @@ SUITE(watcher) {
33023440
RUN_TEST(index_backoff_degenerate_inputs);
33033441
RUN_TEST(index_backoff_never_schedules_sooner_than_the_interval);
33043442
RUN_TEST(watcher_index_failure_streak_increments_and_resets_issue2015);
3443+
RUN_TEST(watcher_index_failure_backoff_gates_repolling_issue2015);
3444+
RUN_TEST(watcher_sustained_failure_logs_once_issue2015);
33053445
RUN_TEST(watcher_index_failure_count_unknown_project);
33063446

33073447
/* Lifecycle */

0 commit comments

Comments
 (0)