← Back to Pull requests

Detect Clear during full-buffer iteration #65

Merged
rupayon123 merged 2 files (89 lines) on Oct 31, 2026 00:02 UTC
View on GitHub
rupayon123 rupayon123 commented on Oct 29, 2026 13:22 UTC
Author

Clearing a full deque during Iter or RIter can leave both ring indices unchanged. The current mutation guard then misses the clear and yields zeroed elements from the old buffer.

Include the original element count in the existing guard for both directions. Detection still happens before the next yield; early-break and last-yield behavior stay the same.

Go 1.26.0 / macOS arm64 validation: four regressions fail before the fix; all eight new cases pass afterward. Full coverage and race suites pass all 58 leaf cases, with no skips and 100% statement coverage. Vet, build, formatting and tidy checks pass. Additional public checks cover delayed iterator use and the existing break/last-yield timing.

This closes the missing count check; it does not add a general mutation version or change value-only Set behavior. Equivalent-shape mutations retain the existing limitations. Hosted CI remains unverified locally.

Diff: 89 lines (+85 / -4) across 2 files Reviewer: gammazero Review duration: 2079m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.31) × (1 − Review Confidence (0.87)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.31 · 89 lines diff across 2 files
0.31
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.90 · 5 substantive comment(s)
0.90
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
87%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.269
Real PR Conversation Timeline
7 items
gammazero gammazero commented on code on Oct 30, 2026 23:48 UTC
Code Review
iterator_clear_test.go:1
@@ -0,0 +1,44 @@
+package deque
+
+import (
+	"fmt"
+	"testing"
+)
+
+func TestIteratorDetectsClear(t *testing.T) {
+	for _, reverse := range []bool{false, true} {
+		for _, tt := range []struct {
+			size   int
+			rotate int
+		}{
+			{15, 0},
+			{16, 0},
+			{32, 0},
+			{16, 3},
+		} {
+			t.Run(fmt.Sprintf("reverse=%v/size=%d/rotate=%d", reverse, tt.size, tt.rotate), func(t *testing.T) {
+				var q Deque[int]
+				for i := range tt.size {
+					q.PushBack(i + 1)
+				}
+				q.Rotate(tt.rotate)
+				items := q.Iter()
+				if reverse {
+					items = q.RIter()
+				}
+				seen := 0
+				assertPanics(t, "iterator must detect Clear", func() {
+					for range items {
+						seen++
+						if seen == 1 {
+							q.Clear()
+						}
+					}
+				})
+				if seen != 1 {
+					t.Fatalf("iterator yielded %d items after Clear, want one before detection", seen)
+				}
+			})
+		}
+	}
+}

}

func TestIterNil(t *testing.T) {
	var q *Deque[int]
	for range q.Iter() {
		t.Fatal("Iter yielded from nil deque")
	}
	for range q.RIter() {
		t.Fatal("RIter yielded from nil deque")
	}
}

func TestIterModifyAfterBreak(t *testing.T) {
	var q Deque[int]
	for i := range 8 {
		q.PushBack(i)
	}

	for range q.Iter() {
		q.PushBack(0)
		break
	}
	for range q.RIter() {
		q.PushBack(0)
		break
	}
	if q.Len() != 10 {
		t.Fatalf("expected length 10, got %d", q.Len())
	}
}

gammazero gammazero commented on code on Oct 30, 2026 23:50 UTC
Code Review
deque.go:1
@@ -219,9 +219,10 @@ func (q *Deque[T]) Iter() iter.Seq[T] {
 	return func(yield func(T) bool) {

	return func(yield func(T) bool) {
		if q.Len() == 0 {
			return
		}

gammazero gammazero commented on code on Oct 30, 2026 23:51 UTC
Code Review
deque.go:1
@@ -239,9 +240,10 @@ func (q *Deque[T]) RIter() iter.Seq[T] {
 	return func(yield func(T) bool) {

	return func(yield func(T) bool) {
		if q.Len() == 0 {
			return
		}

gammazero gammazero commented on Oct 30, 2026 23:52 UTC
Reviewer

Good catch, and the fix does what it intends: Clear on a full deque with head at 0 now panics during iteration instead of yielding zero values.

There are still cases where modification during iteration goes unnoticed:

  • push and then pop without growing buffer

  • Clear followed by re-pushing the same number of items

  • Clear on final iteration.
  • I consider those as acceptable, because while they do alter the _contents_ of a Deque, they do not cause a problem for iteration over legitimate values.

    Your fix corrects the case where iteration happens over values that should not be iterated over.

    There should also be a check to iterate nothing for a nil Deque. I added that in the suggestions.

    codecov-commenter codecov-commenter commented on Oct 30, 2026 23:56 UTC
    Contributor

    :warning: Please install the !'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

    Codecov Report


    :white_check_mark: All modified and coverable lines are covered by tests.
    :white_check_mark: Project coverage is 100.00%. Comparing base (7ae3ab2) to head (dfa61c0).
    :exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

    <details><summary>Additional details and impacted files</summary>

    @@            Coverage Diff            @@
    ##              main       #65   +/-   ##
    =========================================
      Coverage   100.00%   100.00%           
    =========================================
      Files            1         1           
      Lines          273       279    +6     
    =========================================
    + Hits           273       279    +6     
    

    </details>

    :umbrella: View full report in Codecov by Harness.
    :loudspeaker: Have feedback on the report? Share it here.
    <details><summary> :rocket: New features to boost your workflow: </summary>

  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

  • </details>

    rupayon123 rupayon123 commented on code on Oct 31, 2026 12:25 UTC
    Code Review
    iterator_clear_test.go:1
    @@ -0,0 +1,44 @@
    +package deque
    +
    +import (
    +	"fmt"
    +	"testing"
    +)
    +
    +func TestIteratorDetectsClear(t *testing.T) {
    +	for _, reverse := range []bool{false, true} {
    +		for _, tt := range []struct {
    +			size   int
    +			rotate int
    +		}{
    +			{15, 0},
    +			{16, 0},
    +			{32, 0},
    +			{16, 3},
    +		} {
    +			t.Run(fmt.Sprintf("reverse=%v/size=%d/rotate=%d", reverse, tt.size, tt.rotate), func(t *testing.T) {
    +				var q Deque[int]
    +				for i := range tt.size {
    +					q.PushBack(i + 1)
    +				}
    +				q.Rotate(tt.rotate)
    +				items := q.Iter()
    +				if reverse {
    +					items = q.RIter()
    +				}
    +				seen := 0
    +				assertPanics(t, "iterator must detect Clear", func() {
    +					for range items {
    +						seen++
    +						if seen == 1 {
    +							q.Clear()
    +						}
    +					}
    +				})
    +				if seen != 1 {
    +					t.Fatalf("iterator yielded %d items after Clear, want one before detection", seen)
    +				}
    +			})
    +		}
    +	}
    +}

    I checked dfa61c0 with the nil and early-break additions: the full suite and race run both pass (60 cases), as do vet and build on Go 1.26.0. The iterators now skip nil deques. The remaining content changes you listed stay outside this check.

    Reviewers
    Assignees
    rupayon123 rupayon123
    Labels
    risk: low confidence: 87.1% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 31.4% (LOW) · Higher = larger blast radius
    31.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 87.1% · Lower = rushed or shallow review
    87.1%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4% · Safe to merge
    4%