Skip to content

fix(storage): a throttle stamp must write a non-empty payload so its mtime advances - #2213

Open
gdamprint-cmyk wants to merge 4 commits into
1jehuang:masterfrom
gdamprint-cmyk:publish/stamp-nonempty-payload
Open

gdamprint-cmyk wants to merge 4 commits into
1jehuang:masterfrom
gdamprint-cmyk:publish/stamp-nonempty-payload

Conversation

@gdamprint-cmyk

@gdamprint-cmyk gdamprint-cmyk commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A "touch this marker to start the throttle window" helper wrote an empty payload. Upstream master, measured:

$ git grep -n "std::fs::write(&marker" upstream/master -- src/cli/hot_exec.rs crates/jcode-base
upstream/master:crates/jcode-base/src/session/maintenance.rs:66:    std::fs::write(&marker, b"").is_ok()
upstream/master:src/cli/hot_exec.rs:260:    std::fs::write(&marker, b"").is_ok()

std::fs::write(path, b"") truncates the file but issues no WriteFile call for an empty buffer, so on
Windows the mtime does not advance. Every later pass then reads the same old mtime, age < INTERVAL stays
false, and the guarded work runs again on every spawn instead of once per interval.

Fix

Add one helper in crates/jcode-storage/src/lib.rs:

pub fn touch_stamp(path: &Path) -> std::io::Result<()> {
    std::fs::write(path, b"1")
}

The marker's content is never read back, so the payload is only a carrier that forces the write to reach the
filesystem.

Call sites converted (all were writing b""):

file function
src/cli/hot_exec.rs claim_update_fetch_slot
crates/jcode-base/src/scratch_maintenance.rs mark_pruned
crates/jcode-base/src/session/maintenance.rs claim_prune_slot
crates/jcode-build-support/src/version_gc.rs mark_pruned

Tests

jcode-storage, mod stamp_tests:

  • touch_stamp_writes_a_non_empty_payload - the file must be non-empty, which is the property an empty write
    violates.
  • touch_stamp_advances_the_marker_mtime - captures the mtime after an empty write, sleeps 1.1 s, then asserts
    touch_stamp moves it forward. This is the negative control: it proves the test exercises the difference
    rather than passing on a filesystem with coarse timestamps.

jcode-build-support, mod stamp_marker_tests:

  • mark_pruned_advances_the_marker_mtime
  • prune_due_is_false_right_after_a_pass

Verification (real runs, Windows, dedicated CARGO_TARGET_DIR)

cargo test -p jcode-storage --lib touch_stamp
running 2 tests
test result: ok. 2 passed; 0 failed; 18 filtered out

cargo test -p jcode-base --lib prune
test result: ok. 4 passed; 0 failed; 2257 filtered out

cargo test -p jcode-build-support --lib stamp_marker
test result: ok. 2 passed; 0 failed; 53 filtered out

cargo check -p jcode-base -p jcode-build-support
Finished `dev` profile [unoptimized] target(s)

Branch is based on upstream/master (9de9fdcc2).

Why this is the general form

Upstream's own 2f8957ca6 ("cut idle battery drain: stamp-gated git polling") introduced a stamp in the same
shape, so a non-empty payload is the correct contract for any stamp that is supposed to move an mtime. This PR
does not change 2f8957ca6; it repairs the calls that still wrote b"".

Linked issue

Closes #2286

Reviewer notes (updated after push)

Greptile's P2 was correct about the shape and was fixed in e8f9d08ac, together with a second instance of the same defect that it did not see:

  • mark_pruned_advances_the_marker_mtime no longer sleeps 1.1 s. It rewinds the marker by a minute first and asserts that mark_pruned moves it forward, so the test cannot pass merely because a sleep crossed a timestamp tick and cannot fail on a filesystem whose granularity is coarser than the sleep.
  • make_version opened its directory read-only and then called set_modified, which is PermissionDenied (os error 5) on Windows before the test reaches any assertion. Opening a directory there needs FILE_FLAG_BACKUP_SEMANTICS plus FILE_WRITE_ATTRIBUTES; the combination is documented by std's own remove_dir_all.

Verification after the change, Windows:

cargo test -p jcode-build-support --lib version_gc
test result: ok. 4 passed; 0 failed
cargo fmt -p jcode-build-support --check   (clean)
cargo clippy -p jcode-build-support --all-targets   (clean)

The remaining failures in cargo test -p jcode-build-support --lib (paths.rs, tests.rs) and the two needless_return clippy errors in jcode-storage are pre-existing on this branch's base commit cc85b4c02: re-running the same commands with this PR's diff stashed reproduces them. They are outside this PR's scope.

…mtime advances

std::fs::write(path, b"") truncates but issues no WriteFile call for the empty buffer, so on Windows the mtime does not advance. claim_update_fetch_slot in src/cli/hot_exec.rs therefore never consumed its interval: every spawn ran git fetch again. touch_stamp writes b"1"; the content is never read back.
@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change reaches daemon cleanup and CLI update-check throttles.

The PR should not merge until the Unix cleanup test can open its directory successfully.

Findings

  1. P1 Unix cleanup test fails ▶
  2. P2 Timestamp tests can fail intermittently ▶
Fix with agent prompt
### Issue 1
crates/jcode-build-support/src/version_gc.rs:undefined-293
`make_version` passes a directory to this helper, but the non-Windows branch now opens it with `.write(true)`. Unix rejects write-only directory opens, so after fixing the missing import, `keeps_protected_recent_and_fresh_versions` still fails at the `.unwrap()`. Open the directory read-only, as the old `File::open` call did.

```suggestion
    let file = std::fs::OpenOptions::new().read(true).open(path)?;
```

### Issue 2
crates/jcode-storage/src/lib.rs:undefined-874
If the temporary directory uses a filesystem with two-second timestamp steps, such as FAT, the two writes can get the same timestamp despite the 1.1-second sleep. The strict comparison then fails even though the helper works. The new test in `version_gc.rs` has the same issue.

Set the marker's mtime well into the past with `File::set_modified`, then call the helper and compare against the saved timestamp. This also removes the waits.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

The PR adds touch_stamp and uses a non-empty payload to refresh throttle markers.

  • Daily cleanup passes now leave a marker that blocks repeat runs.
  • Source-update checks now leave a marker that limits repeat fetches.

Reviews (4) · Last reviewed commit: "fix(build-support): keep the mtime helpe..." · Reviewed by Greptile

.and_then(|meta| meta.modified())
.expect("mtime after empty write");

std::thread::sleep(std::time::Duration::from_millis(1100));

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.

P2 Timestamp tests can fail intermittently

If the temporary directory uses a filesystem with two-second timestamp steps, such as FAT, the two writes can get the same timestamp despite the 1.1-second sleep. The strict comparison then fails even though the helper works. The new test in version_gc.rs has the same issue.

Set the marker's mtime well into the past with File::set_modified, then call the helper and compare against the saved timestamp. This also removes the waits.

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-storage/src/lib.rs
Line: 874

Comment:
**Timestamp tests can fail intermittently**

If the temporary directory uses a filesystem with two-second timestamp steps, such as FAT, the two writes can get the same timestamp despite the 1.1-second sleep. The strict comparison then fails even though the helper works. The new test in `version_gc.rs` has the same issue.

Set the marker's mtime well into the past with `File::set_modified`, then call the helper and compare against the saved timestamp. This also removes the waits.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

xenking added a commit to xenking/jcode that referenced this pull request Nov 22, 2026
Use one stamp writer for session and scratch pruning, build cleanup, and update-fetch throttling. Reopening an empty stamp does not reliably advance its mtime.

Upstream: 1jehuang#2213

Co-authored-by: Guntrip <[email protected]>
xenking added a commit to xenking/jcode that referenced this pull request Nov 22, 2026
Upstream-PR: 1jehuang#1357
Upstream-PR-Commit: 1jehuang#1357 0da5e81
Upstream-PR: 1jehuang#1362
Upstream-PR-Commit: 1jehuang#1362 5962058
Upstream-PR: 1jehuang#1486
Upstream-PR-Commit: 1jehuang#1486 fadc624
Upstream-PR: 1jehuang#1733
Upstream-PR-Commit: 1jehuang#1733 01f3415
Upstream-PR: 1jehuang#1750
Upstream-PR-Commit: 1jehuang#1750 1ac0c82
Upstream-PR: 1jehuang#1822
Upstream-PR-Commit: 1jehuang#1822 73f85fc
Upstream-PR: 1jehuang#1912
Upstream-PR-Commit: 1jehuang#1912 36d7682
Upstream-PR: 1jehuang#1978
Upstream-PR-Commit: 1jehuang#1978 0f55643
Upstream-PR: 1jehuang#2007
Upstream-PR-Commit: 1jehuang#2007 638f87c
Upstream-PR: 1jehuang#2213
Upstream-PR-Commit: 1jehuang#2213 c5e1487
Upstream-PR: 1jehuang#2234
Upstream-PR-Commit: 1jehuang#2234 0d4d5d4
Upstream-PR: 1jehuang#2266
Upstream-PR-Commit: 1jehuang#2266 217ddc2
Upstream-PR: 1jehuang#2267
Upstream-PR-Commit: 1jehuang#2267 e98c40b

#[cfg(not(windows))]
fn set_mtime_on_handle(path: &Path, when: std::time::SystemTime) -> std::io::Result<()> {
let file = fs::OpenOptions::new().write(true).open(path)?;

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.

P1 Unix cleanup test fails

make_version passes a directory to this helper, but the non-Windows branch now opens it with .write(true). Unix rejects write-only directory opens, so after fixing the missing import, keeps_protected_recent_and_fresh_versions still fails at the .unwrap(). Open the directory read-only, as the old File::open call did.

Suggested change
let file = fs::OpenOptions::new().write(true).open(path)?;
let file = std::fs::OpenOptions::new().read(true).open(path)?;
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-build-support/src/version_gc.rs
Line: 293

Comment:
**Unix cleanup test fails**

`make_version` passes a directory to this helper, but the non-Windows branch now opens it with `.write(true)`. Unix rejects write-only directory opens, so after fixing the missing import, `keeps_protected_recent_and_fresh_versions` still fails at the `.unwrap()`. Open the directory read-only, as the old `File::open` call did.

```suggestion
    let file = std::fs::OpenOptions::new().read(true).open(path)?;
```

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

@greptile-apps

greptile-apps Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

This branch has not been deployed

No deployments
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.

Stamp writes with an empty payload never advance mtime, so the throttle re-runs every pass

1 participant