fix(import): resume fast path proves the lease row untouched too (wy-sbgucn)

Fable review wy-03ne4j (BLOCKER) on e05000d64: dropping a provably-unchanged
tie row from the write set also skips RestoreLeaseOnImportInTx, the only
caller that reconciles the ephemeral lease row on import — but the proof
covered labels, comments and dependencies only. An in_progress snapshot
carrying a lease whose local lease row was absent, or differed, could be
reported Unchanged and never restore or replace it.

The proof now includes the lease axis (importLeaseAlreadyReconciled): a tie
row leaves the write set only when the rewrite's restore would upsert nothing
new — no incoming lease, no live claim, or a local lease already equal to the
snapshot's (expiry, heartbeat, granting node at the store's second
granularity) — and its reconcile would drop nothing (no local lease row, or a
live claim that keeps it). A missing row, a differing lease, a heartbeat the
write would stamp live, or an orphaned local lease all keep the row in the
write set, i.e. the pre-fast-path behavior. The rollback-drill resume is
unaffected: rows the interrupted run already wrote carry the snapshot's own
lease values, so they still prove.

Tests: TestFilterStaleImportIssuesResumeKeepsRowsNeedingLeaseReconciliation
(nine lease shapes, five retained / four proven) and the second-granularity
comparison; both fail against the pre-fix semantics (lease axis ignored:
write set [] vs the five rows).
This commit is contained in:
plato 2026-09-01 02:31:26 -07:00
commit 37ecb53eda
2 changed files with 150 additions and 9 deletions

View file

@ -841,14 +841,18 @@ func filterStaleImportIssues(ctx context.Context, store importIssueLookup, issue
// proveUnchangedImportRows returns the positions in filtered (drawn from the
// candidate set) whose incoming row is provably a no-op write: the tie already
// proved the columns equal, and every incoming label, comment and dependency
// is already stored. The aux writes are all additive (INSERT IGNORE labels,
// existence-checked comments, dedup'd dependency rows), so incoming ⊆ stored
// is exactly the condition under which the rewrite would change nothing.
// Anything short of proof — a store without bulk loaders, a comment with no
// timestamp, an untyped dependency, an aux row not yet stored — keeps the row
// in the write set, i.e. today's behavior. Comments are keyed the way
// PersistComments checks existence (author, created_at as stored, text).
// proved the columns equal, the ephemeral lease row would be left exactly as
// it is (importLeaseAlreadyReconciled — the rewrite's RestoreLeaseOnImportInTx
// is skipped along with the row, so it must have had nothing to do), and every
// incoming label, comment and dependency is already stored. The aux writes are
// all additive (INSERT IGNORE labels, existence-checked comments, dedup'd
// dependency rows), so incoming ⊆ stored is exactly the condition under which
// the rewrite would change nothing. Anything short of proof — a store without
// bulk loaders, a comment with no timestamp, an untyped dependency, an aux row
// not yet stored, a lease row the rewrite would insert, replace or drop —
// keeps the row in the write set, i.e. today's behavior. Comments are keyed
// the way PersistComments checks existence (author, created_at as stored,
// text).
func proveUnchangedImportRows(ctx context.Context, store importIssueLookup, filtered []*types.Issue, candidates map[int]struct{}, localByID map[string]*types.Issue) (map[int]struct{}, error) {
rel, ok := store.(importRelationLookup)
if !ok {
@ -886,13 +890,58 @@ func proveUnchangedImportRows(ctx context.Context, store importIssueLookup, filt
if local == nil {
continue
}
if importAuxAlreadyStored(issue, local, comments[issue.ID], deps[issue.ID]) {
if importLeaseAlreadyReconciled(issue, local) && importAuxAlreadyStored(issue, local, comments[issue.ID], deps[issue.ID]) {
unchanged[pos] = struct{}{}
}
}
return unchanged, nil
}
// importLeaseAlreadyReconciled reports whether RestoreLeaseOnImportInTx would
// leave the local lease row untouched if it ran over this tie row — the
// condition under which skipping the rewrite (and with it the lease
// reconciliation) changes nothing. The tie already proved status and assignee
// equal, so the stored row the restore keys off is the local one. Local lease
// state is the leases.* overlay every issue read carries (sqlbuild.LeaseJoin);
// holder is not part of it, so a live claim is taken to hold its own lease
// row (the UpsertLeaseInTx invariant: lease row ⇔ live claim).
//
// - reconcile drops a lease row whose issue is no longer a live claim: a
// local lease on a row that is not in_progress+assigned is an orphan the
// rewrite would delete — not a no-op.
// - restore fires only when the snapshot carries a lease AND the stored row
// is a live claim. It inserts a missing row (not a no-op), leaves a live
// local lease alone, and replaces an expired one with the snapshot's
// values. Rather than reason about the clock, the proof requires the
// local lease to already equal the snapshot's (expiry, heartbeat, granting
// node, at the store's second granularity) — then the upsert is a no-op
// whichever branch it takes. A snapshot with no heartbeat would be
// stamped live on write, so it never proves.
func importLeaseAlreadyReconciled(incoming, local *types.Issue) bool {
liveClaim := local.Status == types.StatusInProgress && local.Assignee != ""
if local.LeaseExpiresAt != nil && !liveClaim {
return false // reconcile would drop the orphaned lease row
}
if incoming.LeaseExpiresAt == nil || !liveClaim {
return true // nothing to restore; a live claim keeps its local row
}
if local.LeaseExpiresAt == nil || incoming.HeartbeatAt == nil {
return false // restore would insert the row / stamp a live heartbeat
}
return timePtrEqualSecond(local.LeaseExpiresAt, incoming.LeaseExpiresAt) &&
timePtrEqualSecond(local.HeartbeatAt, incoming.HeartbeatAt) &&
local.LeaseGrantedNode == incoming.LeaseGrantedNode
}
// timePtrEqualSecond compares two optional timestamps at the DATETIME(0)
// granularity the leases table stores them with.
func timePtrEqualSecond(a, b *time.Time) bool {
if a == nil || b == nil {
return a == nil && b == nil
}
return a.UTC().Truncate(time.Second).Equal(b.UTC().Truncate(time.Second))
}
func importAuxAlreadyStored(incoming, local *types.Issue, storedComments []*types.Comment, storedDeps []*types.Dependency) bool {
stored := make(map[string]struct{}, len(local.Labels))
for _, l := range local.Labels {

View file

@ -786,3 +786,95 @@ func TestBulkLoadPoolReadTimeoutOnlyForImport(t *testing.T) {
t.Fatalf("nil cmd fallback = %v, want 0", got)
}
}
// wy-sbgucn (Fable review wy-03ne4j BLOCKER): dropping a tie row from the
// write set also bypasses RestoreLeaseOnImportInTx, so the proof must cover
// the ephemeral lease row too. A row is provably unchanged on the lease axis
// only when the rewrite's restore would upsert nothing new (no incoming
// lease, no live claim, or the local lease already equals the snapshot's) and
// its reconcile would drop nothing (no local lease row, or a live claim that
// keeps it). Every other shape stays in the write set.
func TestFilterStaleImportIssuesResumeKeepsRowsNeedingLeaseReconciliation(t *testing.T) {
base := time.Date(2026, 8, 31, 14, 0, 0, 0, time.UTC)
expires := base.Add(30 * time.Minute)
beat := base.Add(-time.Minute)
otherExpires := expires.Add(time.Hour)
claimed := func(id string, leaseExpires, heartbeat *time.Time, node string) *types.Issue {
return &types.Issue{
ID: id, Title: "row " + id, UpdatedAt: base,
Status: types.StatusInProgress, Assignee: "plato",
LeaseExpiresAt: leaseExpires, HeartbeatAt: heartbeat, LeaseGrantedNode: node,
}
}
open := func(id string, leaseExpires, heartbeat *time.Time) *types.Issue {
return &types.Issue{
ID: id, Title: "row " + id, UpdatedAt: base, Status: types.StatusOpen,
LeaseExpiresAt: leaseExpires, HeartbeatAt: heartbeat,
}
}
incoming := []*types.Issue{
claimed("bd-lease-missing", &expires, &beat, "studio"), // restore would INSERT the lease row
claimed("bd-lease-equal", &expires, &beat, "studio"), // lease row already identical
claimed("bd-lease-differs", &expires, &beat, "studio"), // local lease differs (expiry)
claimed("bd-lease-node", &expires, &beat, "studio"), // local lease differs (granting node)
claimed("bd-lease-nobeat", &expires, nil, "studio"), // snapshot heartbeat would be stamped live
claimed("bd-lease-local-only", nil, nil, ""), // live claim keeps its local lease
open("bd-lease-orphan", nil, nil), // reconcile would DROP the orphaned lease row
open("bd-lease-none", nil, nil), // nothing on either side
open("bd-lease-snapshot-open", &expires, &beat), // restore only fires for a live claim
}
store := &fakeImportRelationStore{
fakeImportIssueLookupStore: fakeImportIssueLookupStore{issues: []*types.Issue{
claimed("bd-lease-missing", nil, nil, ""),
claimed("bd-lease-equal", &expires, &beat, "studio"),
claimed("bd-lease-differs", &otherExpires, &beat, "studio"),
claimed("bd-lease-node", &expires, &beat, "mini"),
claimed("bd-lease-nobeat", &expires, &beat, "studio"),
claimed("bd-lease-local-only", &expires, &beat, "studio"),
open("bd-lease-orphan", &expires, &beat),
open("bd-lease-none", nil, nil),
open("bd-lease-snapshot-open", nil, nil),
}},
}
filtered, skippedIDs, plan, err := filterStaleImportIssues(context.Background(), store, incoming)
if err != nil {
t.Fatalf("filterStaleImportIssues: %v", err)
}
if len(skippedIDs) != 0 {
t.Fatalf("skippedIDs = %#v, want none (nothing is stale)", skippedIDs)
}
var kept []string
for _, issue := range filtered {
kept = append(kept, issue.ID)
}
wantKept := []string{"bd-lease-missing", "bd-lease-differs", "bd-lease-node", "bd-lease-nobeat", "bd-lease-orphan"}
if strings.Join(kept, ",") != strings.Join(wantKept, ",") {
t.Fatalf("write set = %v, want %v (every row whose lease row the rewrite would touch)", kept, wantKept)
}
wantUnchanged := []string{"bd-lease-equal", "bd-lease-local-only", "bd-lease-none", "bd-lease-snapshot-open"}
if strings.Join(plan.Unchanged, ",") != strings.Join(wantUnchanged, ",") {
t.Fatalf("plan.Unchanged = %v, want %v", plan.Unchanged, wantUnchanged)
}
if len(plan.TieKeptLocal) != 0 || len(plan.Updates) != 0 {
t.Fatalf("plan = %+v, want no tie conflicts or updates", plan)
}
}
// Lease timestamps are DATETIME(0) in the store: a sub-second component on
// the snapshot side must not defeat the equality proof.
func TestImportLeaseAlreadyReconciledComparesAtSecondGranularity(t *testing.T) {
base := time.Date(2026, 8, 31, 14, 0, 0, 0, time.UTC)
expires, beat := base.Add(30*time.Minute), base.Add(-time.Minute)
expiresSub, beatSub := expires.Add(400*time.Millisecond), beat.Add(999*time.Millisecond)
local := &types.Issue{ID: "bd-x", Status: types.StatusInProgress, Assignee: "plato", LeaseExpiresAt: &expires, HeartbeatAt: &beat, LeaseGrantedNode: "studio"}
incoming := &types.Issue{ID: "bd-x", Status: types.StatusInProgress, Assignee: "plato", LeaseExpiresAt: &expiresSub, HeartbeatAt: &beatSub, LeaseGrantedNode: "studio"}
if !importLeaseAlreadyReconciled(incoming, local) {
t.Fatalf("sub-second snapshot lease timestamps must still prove the lease row unchanged")
}
nextSecond := expires.Add(time.Second)
incoming.LeaseExpiresAt = &nextSecond
if importLeaseAlreadyReconciled(incoming, local) {
t.Fatalf("a lease expiry one second apart is a different lease row")
}
}