Skip to content

Commit 9e1faa7

Browse files
committed
fix(watcher): exclude the indexer's own artifact dir from the dirty signature
After every publish the pipeline re-exports <root>/.codebase-memory/ (graph.db.zst + artifact.json) whenever an artifact already lives there: once a session indexed with persistence, or when the team committed it and every linked worktree checks it out. The watcher's dirty signature folded those files' (size, mtime) in, so each successful reindex rewrote them, the next poll saw a new dirty state, and the daemon re-triggered itself forever: index.supervisor.reap outcome=clean immediately followed by watcher.changed strategy=git, 100+ times in 15 minutes with index workers pinned (DeusData#1953). Nothing under .git moved and no source changed, which is why the reporters blamed their long hyphenated worktree branch names (the DeusData#1254 theory its own reporter retracted); the regression test builds that exact worktree and shows the branch is irrelevant. Scope git status with the same :(exclude).codebase-memory pathspec the exporter's own clean-tree probe already uses for the same reason. Nothing under that directory is ever an index input (discovery skips it), so a change there can never require a reindex; the pathspec is CWD-relative, so a monorepo sub-package watch excludes its own artifact directory. Untracked and committed artifacts both looped before; a real edit and a real HEAD move are still detected. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent aa44c28 commit 9e1faa7

2 files changed

Lines changed: 155 additions & 6 deletions

File tree

‎src/watcher/watcher.c‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "foundation/platform.h"
3232
#include "foundation/str_util.h"
3333
#include "foundation/subprocess.h"
34+
#include "pipeline/artifact.h" /* CBM_ARTIFACT_DIR: the indexer's own output directory */
3435
#ifdef _WIN32
3536
#include "foundation/win_utf8.h"
3637
#define WIN32_LEAN_AND_MEAN
@@ -631,12 +632,31 @@ static watcher_git_status_t git_dirty_signature(cbm_watcher_t *w, project_state_
631632
* watched at a sub-package of a monorepo reindexes whenever any SIBLING
632633
* package changes, because git reports the whole repository's dirty state
633634
* regardless of -C. Paths stay repository-relative either way, which is
634-
* what repo_cdup is for. */
635-
const char *status_argv[] = {"git", "--no-optional-locks",
636-
"-C", state->root_path,
637-
"status", "--porcelain",
638-
"-uall", "-z",
639-
"--", ".",
635+
* what repo_cdup is for.
636+
*
637+
* `:(exclude).codebase-memory` drops the indexer's OWN output. After every
638+
* publish the pipeline re-exports <root>/.codebase-memory/ whenever an
639+
* artifact already lives there (once persisted, or committed by the team
640+
* and checked out into every worktree). Folding that write into the
641+
* signature made each successful reindex look like a NEW dirty state on
642+
* the next poll, and the daemon re-triggered itself forever (#1953: reap
643+
* clean -> watcher.changed, 100+ times in 15 minutes, index workers
644+
* pinned). Nothing under that directory is ever an index input (discovery
645+
* skips it), so a change there can never require a reindex. The pathspec
646+
* is CWD-relative, so a project watched at a monorepo sub-package excludes
647+
* its own artifact directory; it is the same pathspec the exporter's
648+
* clean-tree probe uses, for the same reason. */
649+
const char *status_argv[] = {"git",
650+
"--no-optional-locks",
651+
"-C",
652+
state->root_path,
653+
"status",
654+
"--porcelain",
655+
"-uall",
656+
"-z",
657+
"--",
658+
".",
659+
":(exclude)" CBM_ARTIFACT_DIR,
640660
NULL};
641661
watcher_git_output_t output;
642662
watcher_git_status_t status =

‎tests/test_watcher.c‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "test_helpers.h"
1313
#include <daemon/application.h>
1414
#include <watcher/watcher.h>
15+
#include <pipeline/artifact.h>
1516
#include <store/store.h>
1617
#include <errno.h>
1718
#include <stdatomic.h>
@@ -1574,6 +1575,133 @@ TEST(watcher_no_change_no_reindex) {
15741575
PASS();
15751576
}
15761577

1578+
/* #1953: the reindex's OWN OUTPUT must never count as a change. After every
1579+
* publish the pipeline re-exports <root>/.codebase-memory/graph.db.zst (plus
1580+
* artifact.json) whenever an artifact already lives there — a `persistence:
1581+
* true` index leaves one behind, and a linked worktree checks the team's
1582+
* committed one out. The dirty signature folded that file's (size, mtime)
1583+
* in, so each successful reindex rewrote it, the next poll saw a "new" dirty
1584+
* state, and the daemon re-triggered itself forever: `index.supervisor.reap
1585+
* outcome=clean` immediately followed by `watcher.changed strategy=git`, 100+
1586+
* times in 15 minutes with two index workers pinned. Nothing under .git moved
1587+
* and no source changed, which is why the reporters blamed their long
1588+
* hyphenated worktree branch names (#1254's retracted theory). The scenario is
1589+
* built exactly that way to show the branch is irrelevant: the artifact write
1590+
* is the trigger. Untracked (`??`) and committed (` M`) artifacts both looped. */
1591+
static const char *exporting_index_db_path = NULL;
1592+
static int exporting_index_export_rc = 0;
1593+
static int exporting_index_callback(const char *name, const char *path, void *ud) {
1594+
(void)ud;
1595+
index_call_count++;
1596+
/* What the daemon's index worker does after publish: export_after_publish
1597+
* re-exports FAST because cbm_artifact_exists(root). */
1598+
int rc = cbm_artifact_export(exporting_index_db_path, path, name, CBM_ARTIFACT_FAST);
1599+
if (rc != 0) {
1600+
exporting_index_export_rc = rc;
1601+
}
1602+
return 0;
1603+
}
1604+
1605+
TEST(watcher_own_artifact_export_does_not_retrigger_issue1953) {
1606+
char tmpdir[256];
1607+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_watcher_1953_XXXXXX");
1608+
if (!cbm_mkdtemp(tmpdir))
1609+
FAIL("cbm_mkdtemp failed");
1610+
1611+
char main_repo[400];
1612+
char worktree[400];
1613+
char db_path[400];
1614+
snprintf(main_repo, sizeof(main_repo), "%s/main", tmpdir);
1615+
snprintf(worktree, sizeof(worktree), "%s/4385-auditable-patreon-manual-grants", tmpdir);
1616+
snprintf(db_path, sizeof(db_path), "%s/graph.db", tmpdir);
1617+
if (!cbm_mkdir_p(main_repo, 0755) || wt_git(main_repo, "init -q") != 0) {
1618+
th_rmtree(tmpdir);
1619+
FAIL("git init failed");
1620+
}
1621+
{
1622+
char p[500];
1623+
th_write_file(wt_path(p, sizeof(p), main_repo, "file.txt"), "hello\n");
1624+
}
1625+
wt_git(main_repo, "add file.txt");
1626+
wt_git(main_repo, "commit -q -m init");
1627+
1628+
/* Linked worktree on the reporters' branch profile (>= 25 chars, hyphens). */
1629+
{
1630+
char args[600];
1631+
snprintf(args, sizeof(args),
1632+
"worktree add -q -b 4385-auditable-patreon-manual-grants \"%s\"", worktree);
1633+
if (wt_git(main_repo, args) != 0) {
1634+
th_rmtree(tmpdir);
1635+
FAIL("git worktree add failed");
1636+
}
1637+
}
1638+
1639+
/* A minimal but valid store standing in for the project's cache DB. */
1640+
{
1641+
cbm_store_t *db = cbm_store_open_path(db_path);
1642+
if (!db) {
1643+
th_rmtree(tmpdir);
1644+
FAIL("cbm_store_open_path failed");
1645+
}
1646+
cbm_store_exec(db, "INSERT OR IGNORE INTO projects(name, indexed_at, root_path) "
1647+
"VALUES('wt-1953', '2026-01-01', '/tmp/wt-1953');");
1648+
cbm_store_close(db);
1649+
}
1650+
1651+
/* The artifact is already there — as after any persisted index. */
1652+
ASSERT_EQ(cbm_artifact_export(db_path, worktree, "wt-1953", CBM_ARTIFACT_FAST), 0);
1653+
ASSERT_TRUE(cbm_artifact_exists(worktree));
1654+
1655+
cbm_store_t *store = cbm_store_open_memory();
1656+
exporting_index_db_path = db_path;
1657+
exporting_index_export_rc = 0;
1658+
cbm_watcher_t *w = cbm_watcher_new(store, exporting_index_callback, NULL);
1659+
cbm_watcher_watch(w, "wt-1953", worktree);
1660+
index_call_count = 0;
1661+
1662+
cbm_watcher_poll_once(w); /* baseline */
1663+
ASSERT_EQ(index_call_count, 0);
1664+
1665+
/* Idle worktree: nothing but the tool's own artifact directory differs
1666+
* from HEAD. Every poll used to reindex — and re-export, feeding the next. */
1667+
for (int i = 0; i < 4; i++) {
1668+
cbm_watcher_touch(w, "wt-1953");
1669+
cbm_watcher_poll_once(w);
1670+
}
1671+
ASSERT_EQ(exporting_index_export_rc, 0);
1672+
ASSERT_EQ(index_call_count, 0);
1673+
1674+
/* Committed artifact (team sharing): the export now MODIFIES tracked files
1675+
* instead of leaving untracked ones. The commit is a real HEAD change and
1676+
* reindexes once; the re-export that reindex performs must not. */
1677+
wt_git(worktree, "add -A");
1678+
wt_git(worktree, "commit -q -m share-artifact");
1679+
cbm_watcher_touch(w, "wt-1953");
1680+
cbm_watcher_poll_once(w);
1681+
ASSERT_EQ(index_call_count, 1);
1682+
for (int i = 0; i < 4; i++) {
1683+
cbm_watcher_touch(w, "wt-1953");
1684+
cbm_watcher_poll_once(w);
1685+
}
1686+
ASSERT_EQ(exporting_index_export_rc, 0);
1687+
ASSERT_EQ(index_call_count, 1);
1688+
1689+
/* A real edit in the worktree is still seen. */
1690+
{
1691+
char p[500];
1692+
th_append_file(wt_path(p, sizeof(p), worktree, "file.txt"), "edit\n");
1693+
}
1694+
cbm_watcher_touch(w, "wt-1953");
1695+
cbm_watcher_poll_once(w);
1696+
ASSERT_EQ(index_call_count, 2);
1697+
1698+
cbm_watcher_free(w);
1699+
cbm_store_close(store);
1700+
exporting_index_db_path = NULL;
1701+
th_rmtree(tmpdir);
1702+
PASS();
1703+
}
1704+
15771705
/* #937: a PERSISTENTLY dirty worktree must reindex ONCE per distinct dirty
15781706
* state, not on every poll. The watcher used to treat "tree is dirty" as
15791707
* "tree changed", so an idle repo with one uncommitted file re-triggered a
@@ -3176,6 +3304,7 @@ SUITE(watcher) {
31763304
RUN_TEST(watcher_identical_watch_preserves_dirty_baseline);
31773305
RUN_TEST(watcher_detects_new_file);
31783306
RUN_TEST(watcher_no_change_no_reindex);
3307+
RUN_TEST(watcher_own_artifact_export_does_not_retrigger_issue1953);
31793308
RUN_TEST(watcher_dirty_state_reindexes_once_issue937);
31803309
RUN_TEST(watcher_failed_reindex_retries_issue937);
31813310
RUN_TEST(watcher_multiple_projects);

0 commit comments

Comments
 (0)