Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
34df1fd
feat(node): add per-IP write-surface rate brake and apply to write_ro…
beardthelion Jul 13, 2026
74e05be
feat(node): extend the per-IP write brake to the remaining write sinks
beardthelion Jul 13, 2026
6fded6e
test(node): adversarial TrustedProxy verification through the write b…
beardthelion Jul 13, 2026
e6324d2
feat(node): purge-spam admin subcommand for the empty-burst cleanup
beardthelion Jul 13, 2026
285d7c7
fix(review): make purge-spam empty-check reject git-discovery reads
beardthelion Jul 13, 2026
090019d
fix(review): normalization-consistent purge, brake helper, CLI + cove…
beardthelion Jul 13, 2026
dfd4f6c
test(review): close the reasoned-not-run gaps by execution
beardthelion Jul 13, 2026
0135419
fix(node): reject path-traversal repo names in purge-spam before remo…
beardthelion Jul 15, 2026
a0fcbb0
fix(node): delete child rows transactionally in delete_repo_by_id
beardthelion Jul 15, 2026
fde9cfe
fix(node): reap write_rate_limiter in the periodic cleanup loop
beardthelion Jul 15, 2026
15522d5
fix(node): hold the per-repo advisory lock across purge recheck+delete
beardthelion Jul 15, 2026
1fcca0d
fix(node): count purge-spam disk-removal failures separately from del…
beardthelion Jul 15, 2026
d9eebd2
fix(node): warn on unparseable rate-limit env vars instead of silentl…
beardthelion Jul 15, 2026
6325402
docs(node): document GITLAWB_IPFS_API/TIGRIS_BUCKET/METRICS_ADDR/SHUT…
beardthelion Jul 15, 2026
bfbe502
test(node): exercise the dry-run guard with a real on-disk candidate …
beardthelion Jul 15, 2026
cdb102e
style(node): cargo fmt for the #196 fix set
beardthelion Jul 15, 2026
4df79f2
fix(node): pin the writer's advisory-lock connection in RepoWriteGuard
beardthelion Jul 15, 2026
68c1f5c
refactor(node): introduce ObjectStore trait seam over TigrisClient
beardthelion Jul 15, 2026
3c1ceb8
fix(node): make purge-spam Tigris-authoritative and archive-deleting
beardthelion Jul 15, 2026
cf669bb
fix(review): don't hold/leak the pool connection in acquire_write
beardthelion Jul 15, 2026
dde5ea9
fix(node): make advisory-lock guards drop-safe and reorder acquire_write
beardthelion Jul 16, 2026
b94d440
fix(node): complete a fully-received push after client disconnect
beardthelion Jul 16, 2026
e4c4161
fix(node): serialize archive uploads under the per-repo advisory lock
beardthelion Jul 16, 2026
6ba698c
fix(node): let purge-spam reach repos that exist only as archives
beardthelion Jul 16, 2026
9f7757c
refactor(node): dedup limiter setup + write-brake test driver; fix .e…
beardthelion Jul 16, 2026
2a78524
docs(node): drop the .env.example shutdown-grace entry #196 added (#196)
beardthelion Jul 19, 2026
c4b337b
fix(node): give advisory-lock guards a dedicated pool and bound the r…
beardthelion Jul 19, 2026
4934646
fix(node): advertise a window-derived Retry-After on rate-limit 429s …
beardthelion Jul 19, 2026
0c279c9
fix(node): skip slug-scoped purge cascade when a mirror row shares th…
beardthelion Jul 19, 2026
86a8228
fix(node): fail closed classifying the purge target directory (#196)
beardthelion Jul 20, 2026
bf2a54e
fix(node): run the push completion tail in the disconnect-surviving t…
beardthelion Jul 20, 2026
c24b541
fix(node): tombstone bounties when their repo is purged (#196)
beardthelion Jul 20, 2026
3baa9ed
fix(node): serialize create_repo against the purge advisory lock (#196)
beardthelion Jul 20, 2026
9916bca
test(node): prove purge deletes at MAX_CONNECTIONS=1 with the split p…
beardthelion Jul 20, 2026
8f3d643
fix(node): fail the push when the durable upload times out, don't 200…
beardthelion Jul 20, 2026
d1e596e
fix(node): tombstone bounties under both owner forms on purge (#196)
beardthelion Jul 20, 2026
ce5dcc3
fix(node): fail closed on a symlink at the purge target (#196)
beardthelion Jul 20, 2026
7dc7be2
fix(node): retry-after on the push-advert 429, and create_repo locks …
beardthelion Jul 20, 2026
d2ba14e
fix(node): correct the create-pool and bounty-tombstone remediations …
beardthelion Jul 20, 2026
5a46ef6
fix(node): close the durable-upload data-loss class on every write pa…
beardthelion Jul 20, 2026
2fa9fa3
chore: merge main (iCaptcha PoW + gl retry tests) into feat/write-sin…
beardthelion Jul 21, 2026
16517f8
refactor(node): route the per-DID 429 through the shared too_many_req…
beardthelion Jul 21, 2026
ba572b3
fix(node): init's background upload waits out the creator's advisory …
beardthelion Jul 21, 2026
859240b
fix(node): roll back the local issue ref before unlock when the durab…
beardthelion Jul 21, 2026
c07435d
fix(node): fork publishes only after a durable upload, serialized on …
beardthelion Jul 21, 2026
2ad1c79
fix(node): read-path downloads publish under the purge lock, without …
beardthelion Jul 21, 2026
b2ca5a7
fix(node): bound every under-lock object-store call and dedup the upl…
beardthelion Jul 21, 2026
fa4322c
fix(node): harden the read-path download against cancellation, stale …
beardthelion Jul 21, 2026
c3d6e73
fix(node): keep the fork's unbounded work out of the target-lock span…
beardthelion Jul 21, 2026
03ef9b5
refactor(node): move read-path download coordination into a repo_stor…
beardthelion Jul 21, 2026
4408d27
fix(node): bound acquire_write's download and free the download-map e…
beardthelion Jul 21, 2026
a12f21a
fix(node): roll back the fork mirror and archive on late clone/insert…
beardthelion Jul 21, 2026
556aa31
fix(node): serialize repo purge cascade with mirror ingest via adviso…
beardthelion Jul 22, 2026
1d8c281
fix(node): roll back local ref on failed durable upload (close/merge/…
beardthelion Jul 22, 2026
1d0d3d2
fix(node): tie cancelled cold-download cleanup to blocking extraction
beardthelion Jul 22, 2026
03cd81e
chore(node): fmt + clippy cleanup for the #196 fixes
beardthelion Jul 22, 2026
9152178
fix(node): never let a failed ref snapshot become a delete-all rollba…
beardthelion Jul 22, 2026
296704f
fix(node): slug-lock every peer-ingest writer and drive the real inge…
beardthelion Jul 22, 2026
1e79ae5
fix(node): own cold-download cleanup in a spawned task so handler can…
beardthelion Jul 22, 2026
ce25bba
refactor(node): delegate issue ref helpers to the store ref primitives
beardthelion Jul 22, 2026
4329b2a
Merge origin/main into feat/write-sink-brake-coverage (reintegrate cu…
beardthelion Jul 22, 2026
d755180
fix(node): slug-lock arweave anchor writes; fail closed when the pre-…
beardthelion Jul 22, 2026
e6f4b2f
fix(node): close review residuals: coalesce cold downloads to one in-…
beardthelion Jul 22, 2026
1459cf2
Merge remote-tracking branch 'origin/main' into feat/write-sink-brake…
beardthelion Nov 11, 2026
da876e8
fix(node): re-apply create/fork purge serialization and restore brake…
beardthelion Nov 11, 2026
edfa6bb
fix(node): close review gaps in purge, locks, and fork cleanup
beardthelion Nov 11, 2026
96ed2b2
fix(node): subscriptions-only ws schema and unsettled-work purge guard
beardthelion Nov 11, 2026
082a37f
Merge remote-tracking branch 'origin/main' into feat/write-sink-brake…
beardthelion Nov 11, 2026
e9e86a1
fix(node): wire close_issue rollback, close purge symlink gap, correc…
beardthelion Nov 11, 2026
1187dca
fix(node): close purge/upload lifecycle gaps from review
beardthelion Nov 11, 2026
261f7ab
fix(node): merge rollback, honest create rollback, purge row recheck
beardthelion Nov 12, 2026
662b9a3
fix(node): probe purge slug liveness, gate issue write brake behind t…
beardthelion Nov 12, 2026
43d4f59
fix(node): close-issue rollback uses resolved ref, drop router brake …
beardthelion Nov 12, 2026
4b483a3
fix(node): close_issue spends neither bucket when either cap refuses,…
beardthelion Nov 12, 2026
11cadb9
fix(node): refund the write slot when the close cap refuses, send Ret…
beardthelion Nov 12, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(node): hold the per-repo advisory lock across purge recheck+delete
purge-spam took no write lock, so a concurrent receive-pack could land a ref
between the emptiness recheck and the delete, silently losing a live push.
Add RepoStore::try_lock_repo — a lock-only, non-blocking counterpart to
acquire_write (no Tigris I/O) whose guard holds a dedicated pooled connection so
the advisory lock and its unlock run on the same session. purge now locks each
candidate, rechecks emptiness UNDER the lock, deletes, and releases; a repo held
by a live writer is skipped, not force-deleted. RED->GREEN: a locked empty repo
survives purge, then deletes once the writer releases (neuter-check confirms the
lock is load-bearing).
  • Loading branch information
beardthelion committed Jul 15, 2026
commit 15522d56165981069fd781cef3b515cc19bacfd9
113 changes: 100 additions & 13 deletions crates/gitlawb-node/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,12 @@ where
/// applies the exclusion gate, prints one dry-run row per candidate with owner +
/// ref-count evidence, and — only when `execute` is true — deletes the DB row of
/// each candidate one at a time. Dry-run (the default) deletes nothing.
pub async fn run_purge_spam(db: &Db, repos_dir: &Path, execute: bool) -> Result<()> {
pub async fn run_purge_spam(
db: &Db,
repo_store: &crate::git::repo_store::RepoStore,
repos_dir: &Path,
execute: bool,
) -> Result<()> {
let repos_dir: PathBuf = repos_dir.to_path_buf();
Comment thread
beardthelion marked this conversation as resolved.
Resolved
let rows = db
.list_repos_by_owner_did(SPAM_BURST_TARGET_DID)
Expand Down Expand Up @@ -252,7 +257,32 @@ pub async fn run_purge_spam(db: &Db, repos_dir: &Path, execute: bool) -> Result<
// Execute: delete per-repo, never a single blanket "delete all of owner X".
// A per-repo failure warns and continues rather than aborting the batch.
let mut deleted = 0u64;
let mut skipped_locked = 0usize;
for c in &to_delete {
// Hold the per-repo advisory lock across the FINAL emptiness recheck and
// the delete, so a concurrent receive-pack cannot land a ref in the window
// between recheck and delete (M4). A repo currently locked by a live writer
// is skipped, never force-deleted out from under the push.
let guard = match repo_store.try_lock_repo(&c.owner_did, &c.name).await {
Ok(Some(g)) => g,
Ok(None) => {
warn!(repo = %c.id, "purge-spam: repo is locked by a live writer — skipped");
skipped_locked += 1;
continue;
}
Err(e) => {
warn!(repo = %c.id, err = %e, "purge-spam: could not acquire repo lock — skipped");
skipped_locked += 1;
continue;
}
};
// Authoritative recheck UNDER the lock: a ref that landed before we locked
// makes the repo non-empty, so it must not be deleted.
if ref_count_on_disk(&repos_dir, &c.owner_did, &c.name) != 0 {
warn!(repo = %c.id, "purge-spam: repo no longer empty under lock — skipped (TOCTOU)");
guard.release().await;
continue;
}
match db.delete_repo_by_id(&c.id).await {
Comment thread
beardthelion marked this conversation as resolved.
Resolved
Ok(0) => {
warn!(repo = %c.id, "purge-spam: repo row already gone — nothing to delete");
Expand All @@ -279,9 +309,10 @@ pub async fn run_purge_spam(db: &Db, repos_dir: &Path, execute: bool) -> Result<
warn!(repo = %c.id, err = %e, "purge-spam: failed to delete repo row — continuing");
}
}
guard.release().await;
}
println!(
"purge-spam: deleted {deleted} repo row(s), skipped {} (no longer empty).",
"purge-spam: deleted {deleted} repo row(s), skipped {} (no longer empty), {skipped_locked} (locked by a live writer).",
to_skip.len()
);
Ok(())
Expand Down Expand Up @@ -463,12 +494,18 @@ mod db_tests {
use chrono::Utc;
use sqlx::PgPool;

async fn db(pool: PgPool) -> Db {
let db = Db::for_testing(pool);
async fn db(pool: &PgPool) -> Db {
let db = Db::for_testing(pool.clone());
db.run_migrations().await.unwrap();
db
}

/// Lock-only RepoStore over a test pool + repos_dir (no Tigris), for the purge
/// callers that now take the advisory lock (M4).
fn test_store(repos_dir: &Path, pool: &PgPool) -> crate::git::repo_store::RepoStore {
crate::git::repo_store::RepoStore::for_testing(repos_dir.to_path_buf(), pool.clone())
}

fn rec(id: &str, owner: &str, name: &str) -> RepoRecord {
RepoRecord {
id: id.to_string(),
Expand Down Expand Up @@ -498,7 +535,7 @@ mod db_tests {
// which is fine here: the assertion is that dry-run mutates no rows regardless.
#[sqlx::test]
async fn dry_run_deletes_nothing(pool: PgPool) {
let db = db(pool).await;
let db = db(&pool).await;
for r in [
rec("t-empty", SPAM_BURST_TARGET_DID, "spam1"),
rec("t-nonempty", SPAM_BURST_TARGET_DID, "real"),
Expand All @@ -513,7 +550,8 @@ mod db_tests {

// Empty repos dir: no repo exists on disk, so nothing is provably empty.
let tmp = tempfile::TempDir::new().unwrap();
run_purge_spam(&db, tmp.path(), false).await.unwrap();
let store = test_store(tmp.path(), &pool);
run_purge_spam(&db, &store, tmp.path(), false).await.unwrap();

let after = count_rows(&db).await;
assert_eq!(after, before, "dry-run must not delete any repo rows");
Expand All @@ -524,7 +562,7 @@ mod db_tests {
// repo. This exercises the DB wiring end-to-end with a real empty repo on disk.
#[sqlx::test]
async fn execute_deletes_only_the_empty_target_repo_on_disk(pool: PgPool) {
let db = db(pool).await;
let db = db(&pool).await;
let tmp = tempfile::TempDir::new().unwrap();

// One empty target repo (real bare repo, zero refs) and one target repo
Expand All @@ -548,7 +586,8 @@ mod db_tests {
assert_eq!(store::list_refs(&empty_path).unwrap().len(), 0);
assert!(!store::list_refs(&refs_path).unwrap().is_empty());

run_purge_spam(&db, tmp.path(), true).await.unwrap();
let repo_store = test_store(tmp.path(), &pool);
run_purge_spam(&db, &repo_store, tmp.path(), true).await.unwrap();

// Only the empty target repo row is gone.
assert!(db
Expand All @@ -572,15 +611,16 @@ mod db_tests {
// consistent (no orphaned empty git dir left behind).
#[sqlx::test]
async fn execute_removes_the_on_disk_repo_dir(pool: PgPool) {
let db = db(pool).await;
let db = db(&pool).await;
let tmp = tempfile::TempDir::new().unwrap();
let empty = rec("t-empty", SPAM_BURST_TARGET_DID, "spam1");
db.create_repo(&empty).await.unwrap();
let path = store::repo_disk_path(tmp.path(), &empty.owner_did, &empty.name);
store::init_bare(&path).unwrap();
assert!(path.exists(), "precondition: on-disk repo exists");

run_purge_spam(&db, tmp.path(), true).await.unwrap();
let repo_store = test_store(tmp.path(), &pool);
run_purge_spam(&db, &repo_store, tmp.path(), true).await.unwrap();

assert!(
db.get_repo(SPAM_BURST_TARGET_DID, "spam1")
Expand All @@ -592,6 +632,52 @@ mod db_tests {
assert!(!path.exists(), "on-disk bare repo dir must be removed too");
}

// U4 (M4): a repo whose per-repo advisory lock is held by a live writer must be
// SKIPPED by purge, not deleted out from under the push. Holds the lock via the
// same RepoStore (a separate pooled connection), runs execute, and asserts the
// row + on-disk dir survive; once the writer releases, purge deletes it.
#[sqlx::test]
async fn locked_repo_is_skipped_not_deleted(pool: PgPool) {
let db = db(&pool).await;
let tmp = tempfile::TempDir::new().unwrap();
let empty = rec("t-empty", SPAM_BURST_TARGET_DID, "spam1");
db.create_repo(&empty).await.unwrap();
let path = store::repo_disk_path(tmp.path(), &empty.owner_did, &empty.name);
store::init_bare(&path).unwrap();

let repo_store = test_store(tmp.path(), &pool);
// A live writer holds the per-repo advisory lock.
let held = repo_store
.try_lock_repo(&empty.owner_did, &empty.name)
.await
.unwrap()
.expect("lock should be free initially");

run_purge_spam(&db, &repo_store, tmp.path(), true).await.unwrap();

// Locked → skipped: both the row and the on-disk dir survive.
assert!(
db.get_repo(SPAM_BURST_TARGET_DID, "spam1")
.await
.unwrap()
.is_some(),
"a repo locked by a live writer must NOT be deleted"
);
assert!(path.exists(), "on-disk dir must survive while locked");

// Once the writer releases, the empty repo is deleted (the lock was the
// only thing protecting it — baseline both ways).
held.release().await;
run_purge_spam(&db, &repo_store, tmp.path(), true).await.unwrap();
assert!(
db.get_repo(SPAM_BURST_TARGET_DID, "spam1")
.await
.unwrap()
.is_none(),
"once unlocked, the empty repo is deleted"
);
}

// U1 (M3): a burst-owned row whose NAME traverses out of repos_dir must never
// cause a delete OUTSIDE repos_dir. Adversarial must-not: a real empty bare repo
// planted as a "victim" beside repos_dir is reachable from repos_dir/<slug>/ via
Expand All @@ -600,7 +686,7 @@ mod db_tests {
// delete the victim. The name validator must reject it so the victim survives.
#[sqlx::test]
async fn traversal_name_cannot_delete_a_repo_outside_repos_dir(pool: PgPool) {
let db = db(pool).await;
let db = db(&pool).await;
let root = tempfile::TempDir::new().unwrap();
let repos_dir = root.path().join("repos");
std::fs::create_dir_all(&repos_dir).unwrap();
Expand All @@ -625,7 +711,8 @@ mod db_tests {
);

db.create_repo(&evil).await.unwrap();
run_purge_spam(&db, &repos_dir, true).await.unwrap();
let repo_store = test_store(&repos_dir, &pool);
run_purge_spam(&db, &repo_store, &repos_dir, true).await.unwrap();

assert!(
victim.join("HEAD").is_file(),
Expand All @@ -638,7 +725,7 @@ mod db_tests {
// target DID — the SQL side of the normalization fix.
#[sqlx::test]
async fn list_repos_by_owner_did_matches_short_form(pool: PgPool) {
let db = db(pool).await;
let db = db(&pool).await;
let short = crate::db::normalize_owner_key(SPAM_BURST_TARGET_DID);
assert_ne!(short, SPAM_BURST_TARGET_DID, "fixture must be short form");
let repo = rec("short-owned", short, "spam");
Expand Down
48 changes: 48 additions & 0 deletions crates/gitlawb-node/src/git/repo_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,35 @@ impl RepoStore {
})
}

/// Try to acquire ONLY the per-repo advisory lock, non-blocking, with no
/// Tigris I/O — the lightweight counterpart to [`acquire_write`](Self::acquire_write)
/// for out-of-band admin ops (purge-spam) that must mutually exclude a live
/// push during a destructive delete but never download/re-upload the repo.
///
/// Returns `Some(guard)` if the lock was free, `None` if another writer holds
/// it (so the caller can skip rather than block). The guard holds a dedicated
/// pooled connection for its whole lifetime, so the lock lives on that one
/// connection and `release()` unlocks it on the SAME connection — a plain
/// pool query could unlock on a different connection and silently fail.
pub async fn try_lock_repo(
&self,
owner_did: &str,
repo_name: &str,
) -> Result<Option<RepoLockGuard>> {
let (owner_slug, _local) = self.local_path(owner_did, repo_name)?;
let lock_key = advisory_lock_key(&owner_slug, repo_name);
let mut conn = self.pool.acquire().await.context("acquiring lock connection")?;
let row: (bool,) = sqlx::query_as("SELECT pg_try_advisory_lock($1)")
.bind(lock_key)
.fetch_one(&mut *conn)
.await
.context("try advisory lock")?;
if !row.0 {
return Ok(None);
}
Ok(Some(RepoLockGuard { conn, lock_key }))
}

/// Initialize a new bare repo on local disk and upload to Tigris.
pub async fn init(&self, owner_did: &str, repo_name: &str) -> Result<PathBuf> {
let (owner_slug, local_path) = self.local_path(owner_did, repo_name)?;
Expand Down Expand Up @@ -392,6 +421,25 @@ impl RepoWriteGuard {
}
}

/// Lock-only guard from [`RepoStore::try_lock_repo`]. Holds the Postgres advisory
/// lock (and the dedicated connection it lives on) until `release()`. No Tigris
/// I/O — unlike [`RepoWriteGuard`], releasing does NOT upload anything.
pub struct RepoLockGuard {
conn: sqlx::pool::PoolConnection<sqlx::Postgres>,
lock_key: i64,
}

impl RepoLockGuard {
/// Release the advisory lock on the same connection that took it, then return
/// the connection to the pool.
pub async fn release(mut self) {
let _ = sqlx::query("SELECT pg_advisory_unlock($1)")
.bind(self.lock_key)
.execute(&mut *self.conn)
.await;
}
}

/// Compute a stable i64 hash for a Postgres advisory lock key.
fn advisory_lock_key(owner_slug: &str, repo_name: &str) -> i64 {
use std::hash::{Hash, Hasher};
Expand Down
9 changes: 8 additions & 1 deletion crates/gitlawb-node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,14 @@ async fn run_admin_command(command: config::Command, config: &Config) -> Result<
)
.await
.context("connecting to database for purge-spam")?;
admin::run_purge_spam(&db, &config.repos_dir, execute).await
// Lock-only RepoStore (no Tigris): purge takes the per-repo advisory
// lock to exclude a live push, but never downloads/uploads archives.
let repo_store = git::repo_store::RepoStore::new(
config.repos_dir.clone(),
None,
db.pool().clone(),
);
admin::run_purge_spam(&db, &repo_store, &config.repos_dir, execute).await
}
}
}
Expand Down