Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(gitlink): bound the transaction ref cleanup by the caller's deadline (#892)

Claudeb9c6d393966c
diff --git a/cli/beacon/internal/gitlink/notes.go b/cli/beacon/internal/gitlink/notes.go
index 7e35a281..1ea49816 100644
--- a/cli/beacon/internal/gitlink/notes.go
+++ b/cli/beacon/internal/gitlink/notes.go
@@ -104,10 +104,12 @@ func updateNoteOnce(ctx context.Context, g Git, commit string, update func(strin
defer func() {
// Best effort, and on a fresh context: a transaction that timed out must still not leave
// its working ref behind. Every ref deletion takes packed-refs.lock, which concurrent
- // writers contend for, and git gives up on it after one second by default.
- cleanup, cancel := context.WithTimeout(context.Background(), 6*time.Second)
+ // writers contend for, so wait for it longer than git's default second -- but never
+ // further past the caller's deadline than that second.
+ wait := txCleanupWait(ctx)
+ cleanup, cancel := context.WithTimeout(context.Background(), wait+time.Second)
defer cancel()
- _, _ = g.Run(cleanup, nil, "-c", "core.packedRefsTimeout=5000", "update-ref", "-d", txRef)
+ _, _ = g.Run(cleanup, nil, "-c", "core.packedRefsTimeout="+strconv.FormatInt(wait.Milliseconds(), 10), "update-ref", "-d", txRef)
}()
current, err := readNote(ctx, g, txRef, commit)
@@ -143,6 +145,15 @@ func updateNoteOnce(ctx context.Context, g Git, commit string, update func(strin
func normalizeNote(note string) string { return MergeNote(note, nil) }
+// txCleanupWait is how long a transaction's ref deletion waits for packed-refs.lock.
+func txCleanupWait(ctx context.Context) time.Duration {
+ wait := 5 * time.Second
+ if deadline, ok := ctx.Deadline(); ok {
+ wait = min(wait, max(time.Until(deadline), 0)+time.Second)
+ }
+ return wait
+}
+
func refMoved(ctx context.Context, g Git, base string) (bool, error) {
now, err := resolveRef(ctx, g, NotesRef)
if err != nil {
diff --git a/cli/beacon/internal/gitlink/notes_test.go b/cli/beacon/internal/gitlink/notes_test.go
index 6e4108ce..dd1e1658 100644
--- a/cli/beacon/internal/gitlink/notes_test.go
+++ b/cli/beacon/internal/gitlink/notes_test.go
@@ -290,6 +290,25 @@ func TestAddLinksWaitsForPackedRefsLock(t *testing.T) {
}
}
+func TestAddLinksCleanupKeepsCallerDeadline(t *testing.T) {
+ r := newTestRepo(t)
+ r.write("f", "1")
+ sha := r.commitAt("c", t0)
+ if err := os.WriteFile(filepath.Join(r.root, ".git", "packed-refs.lock"), nil, 0o644); err != nil {
+ t.Fatal(err)
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second)
+ defer cancel()
+ start := time.Now()
+ if _, err := AddLinks(ctx, r.gitClient(), sha, []Link{{Harness: "codex", SessionID: "s"}}); err != nil {
+ t.Fatal(err)
+ }
+ // The deadline plus one second of lock wait, not the five a caller without a deadline gets.
+ if elapsed := time.Since(start); elapsed > 4*time.Second {
+ t.Fatalf("AddLinks took %v under a 1s deadline", elapsed)
+ }
+}
+
func TestListLinked(t *testing.T) {
r := newTestRepo(t)
ctx := context.Background()