Skip to content

Commit c9b4d01

Browse files
halindromeclaude
authored andcommitted
fix: address QA round 1
F1 (major) — the failure state machine the backoff feeds had no test coverage. Deleting the streak reset, or widening the sustained-failure comparison, left all 7788 tests green: the index_backoff_* tests cover the delay arithmetic, and nothing covered the counter that selects which delay applies. A streak that never increments silently degrades the backoff to the unbounded retry it exists to prevent. Adds cbm_watcher_index_failure_count(), alongside the existing cbm_watcher_watch_count() in watcher.h's "Introspection (for testing)" section, and a test that drives a real git repo through fail/fail/succeed on the failing_index_callback seam already used by the DeusData#937 test, asserting the streak goes 0 -> 1 -> 2 -> 0. Verified by mutation: removing the reset turns it red at the reset assertion while every index_backoff_* test stays green. The delay-GATING half stays uncovered on purpose. Every integration test calls cbm_watcher_touch first, which zeroes next_poll_ns, so whether the scheduler honours the computed deadline is unobservable without an injectable clock the watcher does not have. That harness is a larger change than this fix. F3 (minor) — cbm_watcher_index_backoff_ms was non-monotonic for an interval above the ceiling: zero failures returned the interval, one failure returned the smaller ceiling. Unreachable today (POLL_MAX_MS < the ceiling), but the function is exported, so the clamp is now a floor as well as a cap and backing off can never schedule sooner than the project's own cadence. F2 (minor) needed no code change: a busy-skip following an unresolved failure streak keeps the backed-off delay, which is deliberate — only a success clears the streak, and treating rc > 0 as recovery would let a project alternating fail/busy never back off at all. The PR description's over-strong "busy-skip is behaviourally unchanged" claim is corrected there. 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 e023938 commit c9b4d01

3 files changed

Lines changed: 130 additions & 1 deletion

File tree

‎src/watcher/watcher.c‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,15 @@ int cbm_watcher_index_backoff_ms(int interval_ms, int consecutive_failures) {
171171
int shift =
172172
consecutive_failures < INDEX_FAIL_SHIFT_MAX ? consecutive_failures : INDEX_FAIL_SHIFT_MAX;
173173
int64_t delay = (int64_t)interval_ms << shift;
174-
return delay > INDEX_FAIL_CEILING_MS ? INDEX_FAIL_CEILING_MS : (int)delay;
174+
if (delay > INDEX_FAIL_CEILING_MS) {
175+
delay = INDEX_FAIL_CEILING_MS;
176+
}
177+
/* Backing off must never schedule SOONER than the project's own cadence.
178+
* Unreachable today (POLL_MAX_MS < the ceiling), but clamping here keeps
179+
* the function monotonic for every input rather than only for the inputs
180+
* the current constants can produce — the caller's contract is "a delay
181+
* that never shrinks as failures accumulate". */
182+
return (int)(delay < interval_ms ? interval_ms : delay);
175183
}
176184

177185
int cbm_watcher_poll_interval_ms(int file_count) {
@@ -1161,6 +1169,17 @@ void cbm_watcher_touch(cbm_watcher_t *w, const char *project_name) {
11611169
cbm_mutex_unlock(&w->projects_lock);
11621170
}
11631171

1172+
int cbm_watcher_index_failure_count(cbm_watcher_t *w, const char *project_name) {
1173+
if (!w || !project_name) {
1174+
return -1;
1175+
}
1176+
cbm_mutex_lock(&w->projects_lock);
1177+
project_state_t *s = cbm_ht_get(w->projects, project_name);
1178+
int failures = s ? s->index_failures : -1;
1179+
cbm_mutex_unlock(&w->projects_lock);
1180+
return failures;
1181+
}
1182+
11641183
int cbm_watcher_watch_count(cbm_watcher_t *w) {
11651184
if (!w) {
11661185
return 0;

‎src/watcher/watcher.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ void cbm_watcher_stop(cbm_watcher_t *w);
9191
/* Return the number of projects in the watch list. */
9292
int cbm_watcher_watch_count(cbm_watcher_t *w);
9393

94+
/* 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. */
99+
int cbm_watcher_index_failure_count(cbm_watcher_t *w, const char *project_name);
100+
94101
/* Return the adaptive poll interval (ms) for a given file count. */
95102
int cbm_watcher_poll_interval_ms(int file_count);
96103

‎tests/test_watcher.c‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ TEST(index_backoff_degenerate_inputs) {
137137
PASS();
138138
}
139139

140+
TEST(index_backoff_never_schedules_sooner_than_the_interval) {
141+
/* An interval above the ceiling must not be SHORTENED by backing off:
142+
* the clamp is a floor as well as a cap. Not reachable while
143+
* POLL_MAX_MS < the ceiling, but the helper is exported, so its contract
144+
* has to hold for what a caller can pass, not only for what today's
145+
* constants produce. */
146+
int over = 400000; /* > INDEX_FAIL_CEILING_MS */
147+
ASSERT_EQ(cbm_watcher_index_backoff_ms(over, 0), over);
148+
for (int failures = 1; failures < 20; failures++) {
149+
ASSERT_TRUE(cbm_watcher_index_backoff_ms(over, failures) >= over);
150+
}
151+
PASS();
152+
}
153+
140154
/* ══════════════════════════════════════════════════════════════════
141155
* LIFECYCLE
142156
* ══════════════════════════════════════════════════════════════════ */
@@ -1788,6 +1802,92 @@ TEST(watcher_failed_reindex_retries_issue937) {
17881802
PASS();
17891803
}
17901804

1805+
/* The failure state machine the backoff feeds (#2015): the streak must count
1806+
* consecutive HARD failures and reset the moment one reindex succeeds. The
1807+
* delay it computes is covered by the index_backoff_* unit tests; what is
1808+
* covered here is the counter that selects which delay applies, because a
1809+
* streak that never increments — or never resets — silently reverts the
1810+
* backoff to the unbounded retry it exists to prevent, with every other test
1811+
* still green. Each poll is preceded by cbm_watcher_touch, which zeroes
1812+
* next_poll_ns, so this exercises the counter without depending on a clock. */
1813+
TEST(watcher_index_failure_streak_increments_and_resets_issue2015) {
1814+
char tmpdir[256];
1815+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_watcher_strk_XXXXXX");
1816+
if (!cbm_mkdtemp(tmpdir))
1817+
FAIL("cbm_mkdtemp failed");
1818+
1819+
if (wt_git(tmpdir, "init -q") != 0) {
1820+
th_rmtree(tmpdir);
1821+
FAIL("git init failed");
1822+
}
1823+
{
1824+
char p[300];
1825+
th_write_file(wt_path(p, sizeof(p), tmpdir, "file.txt"), "hello\n");
1826+
}
1827+
wt_git(tmpdir, "add file.txt");
1828+
wt_git(tmpdir, "commit -q -m init");
1829+
1830+
cbm_store_t *store = cbm_store_open_memory();
1831+
cbm_watcher_t *w = cbm_watcher_new(store, failing_index_callback, NULL);
1832+
1833+
cbm_watcher_watch(w, "strk-repo", tmpdir);
1834+
failing_index_calls = 0;
1835+
failing_index_fail_first_n = 2; /* first TWO reindex attempts fail hard */
1836+
1837+
/* Baseline (clean tree): no reindex, so no failures yet. */
1838+
cbm_watcher_poll_once(w);
1839+
ASSERT_EQ(failing_index_calls, 0);
1840+
ASSERT_EQ(cbm_watcher_index_failure_count(w, "strk-repo"), 0);
1841+
1842+
/* HEAD moves. */
1843+
{
1844+
char p[300];
1845+
th_append_file(wt_path(p, sizeof(p), tmpdir, "file.txt"), "world\n");
1846+
}
1847+
wt_git(tmpdir, "add file.txt");
1848+
wt_git(tmpdir, "commit -q -m add-world");
1849+
1850+
/* First hard failure → streak 1. */
1851+
cbm_watcher_touch(w, "strk-repo");
1852+
cbm_watcher_poll_once(w);
1853+
ASSERT_EQ(failing_index_calls, 1);
1854+
ASSERT_EQ(cbm_watcher_index_failure_count(w, "strk-repo"), 1);
1855+
1856+
/* Second consecutive hard failure → streak 2 (it accumulates; it is not
1857+
* a boolean, which is what makes the delay grow). */
1858+
cbm_watcher_touch(w, "strk-repo");
1859+
cbm_watcher_poll_once(w);
1860+
ASSERT_EQ(failing_index_calls, 2);
1861+
ASSERT_EQ(cbm_watcher_index_failure_count(w, "strk-repo"), 2);
1862+
1863+
/* Third attempt succeeds → streak resets, so a project that recovers
1864+
* returns to its normal cadence instead of staying backed off. */
1865+
cbm_watcher_touch(w, "strk-repo");
1866+
cbm_watcher_poll_once(w);
1867+
ASSERT_EQ(failing_index_calls, 3);
1868+
ASSERT_EQ(cbm_watcher_index_failure_count(w, "strk-repo"), 0);
1869+
1870+
cbm_watcher_free(w);
1871+
cbm_store_close(store);
1872+
th_rmtree(tmpdir);
1873+
PASS();
1874+
}
1875+
1876+
TEST(watcher_index_failure_count_unknown_project) {
1877+
cbm_store_t *store = cbm_store_open_memory();
1878+
cbm_watcher_t *w = cbm_watcher_new(store, index_callback, NULL);
1879+
1880+
/* Unwatched and NULL inputs report -1, distinct from a watched project
1881+
* sitting at a legitimate 0. */
1882+
ASSERT_EQ(cbm_watcher_index_failure_count(w, "never-watched"), -1);
1883+
ASSERT_EQ(cbm_watcher_index_failure_count(w, NULL), -1);
1884+
ASSERT_EQ(cbm_watcher_index_failure_count(NULL, "x"), -1);
1885+
1886+
cbm_watcher_free(w);
1887+
cbm_store_close(store);
1888+
PASS();
1889+
}
1890+
17911891
TEST(watcher_multiple_projects) {
17921892
/* Create two temporary git repos */
17931893
char tmpdirA[256];
@@ -3200,6 +3300,9 @@ SUITE(watcher) {
32003300
RUN_TEST(index_backoff_reaches_ceiling_and_stays);
32013301
RUN_TEST(index_backoff_is_monotonic_and_bounded);
32023302
RUN_TEST(index_backoff_degenerate_inputs);
3303+
RUN_TEST(index_backoff_never_schedules_sooner_than_the_interval);
3304+
RUN_TEST(watcher_index_failure_streak_increments_and_resets_issue2015);
3305+
RUN_TEST(watcher_index_failure_count_unknown_project);
32033306

32043307
/* Lifecycle */
32053308
RUN_TEST(watcher_create_free);

0 commit comments

Comments
 (0)