|
12 | 12 | #include "test_helpers.h" |
13 | 13 | #include <daemon/application.h> |
14 | 14 | #include <watcher/watcher.h> |
| 15 | +#include <pipeline/artifact.h> |
15 | 16 | #include <store/store.h> |
16 | 17 | #include <errno.h> |
17 | 18 | #include <stdatomic.h> |
@@ -1574,6 +1575,133 @@ TEST(watcher_no_change_no_reindex) { |
1574 | 1575 | PASS(); |
1575 | 1576 | } |
1576 | 1577 |
|
| 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 | + |
1577 | 1705 | /* #937: a PERSISTENTLY dirty worktree must reindex ONCE per distinct dirty |
1578 | 1706 | * state, not on every poll. The watcher used to treat "tree is dirty" as |
1579 | 1707 | * "tree changed", so an idle repo with one uncommitted file re-triggered a |
@@ -3176,6 +3304,7 @@ SUITE(watcher) { |
3176 | 3304 | RUN_TEST(watcher_identical_watch_preserves_dirty_baseline); |
3177 | 3305 | RUN_TEST(watcher_detects_new_file); |
3178 | 3306 | RUN_TEST(watcher_no_change_no_reindex); |
| 3307 | + RUN_TEST(watcher_own_artifact_export_does_not_retrigger_issue1953); |
3179 | 3308 | RUN_TEST(watcher_dirty_state_reindexes_once_issue937); |
3180 | 3309 | RUN_TEST(watcher_failed_reindex_retries_issue937); |
3181 | 3310 | RUN_TEST(watcher_multiple_projects); |
|
0 commit comments