Skip to content

Back off idle collection ticks and keep active truncations progressing - #231

Merged
Shengyu Fu (shengyfu) merged 4 commits into
mainfrom
shengyfu/idle-pacing-collection-cursor
Nov 30, 2026
Merged

Shengyu Fu (shengyfu) merged 4 commits into
mainfrom
shengyfu/idle-pacing-collection-cursor

Conversation

@shengyfu

@shengyfu Shengyu Fu (shengyfu) commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: e90fe722594a5651a44276fe8abe11f75c724115

Dimension Assessment Rationale
Change confidence 8.5/10 ↑ The Windows regression failed before the collector fix and now deletes the full fixture without re-reading verified bytes.
↑ The exact CI dependency qualification now passes both locked graphs and all four fuzz targets after updating fuzz/Cargo.lock to 1.2.10.
↑ Collection and shared-daemon tests pass; the CLI prints tgrep 1.2.10.
↓ Workspace Clippy reported disallowed std::env::current_dir uses in unchanged files. The full workspace test command omitted managed-test-hooks; its affected test passed when rerun with the feature.
↓ CI for the current head is still running; no production-sized backlog measurement.
Diagnosis confidence 9.5/10 ↑ A waiting cursor suppressed idle backoff. The scheduler test distinguishes on-time retries from hot polling.
↑ On the previous head, a real collection pass re-read 4,096 bytes and reclaimed zero after individually short idle gaps. Credited truncations had not renewed the completed proof's deadline.
Evidence fit 7.5/10 ↑ The two defects account for unnecessary polling during waits and repeated verification of an actively shrinking checkpoint; the latter was observed on Windows.
↓ Neither the reported cache total nor the 15 s checkpoint cadence has been attributed on the reporter's machine. Branch-switch rebuild policy is separate.

Problem

Reports on Windows ARM64 and x64 describe an idle managed daemon (tgrep serve --shared) that:

  • holds a steady fraction of a core (about 0.5 core on a 16-core x64 Dev Box running a large monorepo);
  • keeps reopening owner.lock, activity.lock and catalog.sqlite, which Defender then scans.

The scheduler loop cleared quiet whenever a collection continuation was waiting:

if collection.is_some() || collection_cursor.is_some() || !failures.names().is_empty() { quiet = false; }

A cursor waits out collection_schedule, which is retry_ms × 2^exponent with the exponent capped at 6. The Copilot runtime sets retry_ms = 5000, so one wait can last up to 320 s. For that whole time every 100 ms tick was a full tick, which reads the catalog and re-verifies the anchors. The IdlePacer backoff added in #210/#211 never engaged.

There was also a distinct cause of slow reclamation. After tgrep finished verifying a large member, successive passes could safely truncate it in chunks, but only verification pages refreshed the proof's idle deadline. Once the original deadline elapsed, tgrep started verifying the remaining prefix from byte zero. The cursor stayed on that member, delaying cleanup of later objects even though every individual retry arrived on time.

Change

  • A waiting continuation now counts as quiet, so full ticks back off to SCHEDULER_IDLE_TICK as they do for any idle namespace.
  • RetrySchedule::next_due gives the continuation's due time. IdlePacer keeps it as a wake, and IdlePacer::due runs a full tick once now >= wake. The continuation therefore still starts on the first 100 ms tick after it falls due, the same start latency as before.
  • IdlePacer::completed drops a wake that is not after the tick. Work that was due but did not start (for example, when collection was disabled while a cursor waited) costs at most one extra full tick and cannot hold the scheduler at the base tick.
  • In-flight passes and phase failures still keep ticks hot. That behavior is unchanged.
  • A credited truncation renews the idle deadline only if the collector still holds the native proof. The timeout comes from the pass's version-checked policy, with no extra catalog lookup per chunk. Genuine idle expiry, policy/allocation changes and native invalidation still require fresh verification. macOS still drops the proof after every truncation.
  • SHARED_WORKTREE_INDEXES.md documents both the idle pacing and the lifetime of a proof during deletion.
  • The workspace version and both local packages are bumped from 1.2.9 to 1.2.10; fuzz/Cargo.lock is updated to match tgrep-core.

Full ticks per wait:

Wait Before After
5 s (base) about 50 6
320 s (maximum backoff) about 3,200 about 200

Validation

  • cargo fmt --all --check: passed
  • cargo clippy --locked --workspace --all-targets --features tgrep-cli/managed-test-hooks --quiet -- -D warnings: failed on disallowed std::env::current_dir uses in unchanged files (tgrep-core/tests/case_insensitive_roundtrip.rs, tgrep-cli/tests/shared_daemon/managed.rs, tgrep-core/src/gitignore.rs, tgrep-cli/src/search.rs, and tgrep-core/src/shared.rs).
  • cargo test --locked -p tgrep-cli --bin tgrep: passed (394 passed, 2 ignored)
  • cargo test --locked -p tgrep-core --lib managed::collection::tests: passed (25 tests on Windows, including true idle expiry and policy/allocation invalidation)
  • cargo test --locked -p tgrep-cli --features managed-test-hooks --test shared_daemon collection_: passed (7 tests on Windows, including crash recovery, cancellation and replay)
  • cargo check --workspace --offline: passed with both packages at 1.2.10.
  • cargo run --locked --offline -p tgrep-cli -- --version: printed tgrep 1.2.10.
  • python -B scripts/qualification/qualify.py dependencies: passed; both locked dependency graphs and all four fuzz targets checked, with all lockfiles unchanged.
  • cargo test --locked --workspace --offline: one daemon test failed because the command omitted its required managed-test-hooks feature (unknown managed method: testing.reconcile-timer); 68 tests in that target passed and 1 was ignored. The exact test passed when rerun with --features managed-test-hooks.
  • active_truncation_outlives_the_original_verification_deadline uses a sealed four-block file, bounded passes and simulated gaps of two-thirds of an idle lifetime. On the old head, the pass after the second gap re-read 4,096 bytes and reclaimed zero. With the fix, the collector deletes the whole payload without re-reading any verified bytes after truncation begins.
  • scheduling_tests::waiting_continuations_let_ticks_back_off_and_start_on_time simulates 100 ms ticks against a 5 s retry. It checks that the full ticks fall at exactly [0, 200, 600, 1400, 3000, 4600] ms and that the continuation is due on the first tick at its due time. Removing either the wake check or the stale-wake filter makes the test fail.

The loop wiring (quiet/wake in run) is an inline closure with no unit seam, so it was checked by review rather than by a test.

Known limits and follow-ups (not changed here)

  • Policy updates don't change the pacer signature. If collection.retry_ms is lowered while a cursor waits, the earlier due time is noticed up to one idle tick (1.6 s) late. That is within the documented "noticed up to one idle tick later" lag.
  • Recovery continuations, and periodic or pressure collection starts, can still be noticed up to one idle tick late. This is existing, documented behavior.
  • A phase that fails on every tick keeps ticks at 100 ms. That is a separate potential hot loop, visible as diagnostics.consecutive_failed_ticks and failed_phases in status.
  • !failures.names().is_empty() in the quiet check is redundant with tick.is_ok(). It is left as is to keep this change small.

A collection continuation waiting out its retry interval (up to 64x the
5 s base) kept every full scheduler tick at 100 ms, so an otherwise idle
namespace re-read the catalog and reopened its anchor files ten times a
second. On Windows this showed up as steady CPU in tgrep and in Defender.

Treat the wait as quiet so ticks back off to the idle tick, and record
the continuation's due time as a pacer wake so it still starts on the
first base tick after it falls due. A wake that is not after the tick is
dropped so work that was due but did not start cannot hold the
scheduler at the base tick.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 30, 2026 07:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are blocking approval of the retry-aware idle-backoff change.

No open issues 🎉

What changed in this PR

This PR reduces idle scheduler work in managed shared daemons by letting waiting collection continuations use the existing idle backoff, while preserving timely retries.

Changes:

  • Tracks the continuation’s retry deadline so idle pacing does not delay eligible collection work.
  • Drops stale wake deadlines and adds a simulated-clock regression test for backoff and retry timing.
  • Documents that waiting collection continuations count as idle.
File Description
tgrep-cli/​src/​serve/​shared/​managed.rs Adds retry-aware idle pacing, scheduler wiring, and regression coverage.
SHARED_WORKTREE_INDEXES.md Documents idle backoff and wake timing for waiting collection continuations.

Review effort: Balanced

A verified member can take many bounded collection passes to delete. Treat each credited truncation as activity on its retained proof, using the current pass policy without rereading the catalog. Keep native invalidation and genuine idle expiry unchanged.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 30, 2026 17:55
@shengyfu Shengyu Fu (shengyfu) changed the title Let waiting collection continuations idle the scheduler Back off idle collection ticks and keep active truncations progressing Nov 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the scheduler pacing and proof-renewal changes.

No open issues 🎉

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 30, 2026 18:37
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The fuzz lockfile still records tgrep-core 1.2.9 instead of 1.2.10, causing Linux CI’s locked dependency metadata check to fail.

1 open issue

Review effort: Balanced

Comment thread Cargo.toml

[workspace.package]
version = "1.2.9"
version = "1.2.10"
Copilot AI balanced review requested due to automatic review settings November 30, 2026 18:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the collection-pacing and retained-proof renewal changes.

1 open issue

Review effort: Balanced

@shengyfu
Shengyu Fu (shengyfu) merged commit 95c6c32 into main Nov 30, 2026
23 of 25 checks passed
@shengyfu
Shengyu Fu (shengyfu) deleted the shengyfu/idle-pacing-collection-cursor branch November 30, 2026 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants