{
  "version": "adversarial-v1",
  "description": "Adversarial corpus (#5292): more-itertools at the lightlane-v1 base. Valid issues (upstream fixes), wrong-premise, duplicate and already-shipped issues that must be closed with evidence, and integration issues that must touch more.py, more.pyi and docs/api.rst. Instructions: docs/eval/adversarial-bench.md.",
  "upstream": "more-itertools/more-itertools",
  "license": "MIT",
  "clone_url": "https://github.com/more-itertools/more-itertools.git",
  "base": "670b715b307adfe5d2fca08a87ca562cb8bfea2c",
  "suite_command": "python3 -m unittest discover -q -s tests -t .",
  "control_prompt": "Fix this issue in the repository in the current directory. Write a failing regression test first, then the fix, then run the targeted tests. Do not use web tools.\n\nIssue: {title}\n\n{body}",
  "issues": [
    {
      "id": "adv-valid-1293",
      "kind": "valid",
      "expected": "implement",
      "upstream_issue": 1293,
      "fix_pr": 1294,
      "fix_sha": "1dd480a08bc2a4c399c84bf21cb1558f8b5fc838",
      "title": "Repeated zero-cache seekable.peek() retains nested chain iterators",
      "body": "On current master (`b5e3886`), the fix for #1268 preserves values but each `seekable(..., maxlen=0).peek()` prepends a new `chain` around the previous source. Repeated peeking without advancing retains an iterator chain proportional to the number of calls, despite having no history cache.\n\n```python\nimport tracemalloc\nfrom more_itertools import seekable\n\ns = seekable(range(3), maxlen=0)\ntracemalloc.start()\ns.peek()\nbaseline = tracemalloc.get_traced_memory()[0]\nfor _ in range(10000):\n    assert s.peek() == 0\nprint(tracemalloc.get_traced_memory()[0] - baseline)\nprint(list(s.elements()), list(s))\n```\n\nOn CPython 3.12.13/macOS this retains 2,427,672 additional bytes; output remains `[] [0, 1, 2]`. A single lookahead slot should be sufficient regardless of the number of repeated `peek()`/`bool()` calls. I am checking a fix that reuses the existing `peekable` helper only in the zero-history case, with unchanged bounded/unbounded history behavior.\n\nInvestigation performed with AI assistance.\n",
      "oracle": {
        "files": [
          "tests/test_more.py"
        ],
        "command": "python3 -m unittest -q tests.test_more.SeekableTest.test_repeated_zero_cache_peek_has_bounded_memory",
        "note": "670b715 is the merge parent of the fix, but the fix branch also carries the #1268/#1269 seekable maxlen=0 value fix; on the base the oracle errors with StopIteration, so an arm passes only by bounding memory and keeping the peeked values."
      }
    },
    {
      "id": "adv-valid-1304",
      "kind": "valid",
      "expected": "implement",
      "upstream_issue": 1304,
      "fix_pr": 1305,
      "fix_sha": "1ea82a711c69f590054987b5cb194157f8ce8ac4",
      "title": "ichunked(..., 0) raises islice's ValueError on a nonempty iterable",
      "body": "`ichunked` is documented as being like `chunked`, but with lazy chunks. `chunked` treats `n=0` as \"yield nothing\". `ichunked` only does that when the source is already empty; with any remaining items it calls `islice(iterator, n - 1)` and blows up.\n\nI hit this switching a streaming pipeline from `chunked` to `ichunked` so batches would not be materialized as lists. Chunk size comes from the same config in both paths; `0` means \"this stage is disabled, emit nothing.\"\n\n```python\nfrom more_itertools import chunked, ichunked\n\nrows = [\"a\", \"b\", \"c\"]  # e.g. leftover rows from a DB cursor\nchunk_size = 0          # config: skip this sink\n\nprint(list(chunked(rows, chunk_size)))\n# []\n\nprint([list(c) for c in ichunked(rows, chunk_size)])\n# ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.\n```\n\n`IchunkedTests.test_zero` only uses `iterable = []`, which never enters the `for first in iterator` loop, so the `islice(..., n - 1)` path is untested.\n\nNegative `n` has the same leak: `chunked` raises `ValueError: n must be at least 0`, `ichunked` raises islice's \"Indices for islice() must be None or an integer...\".\n\nHappy to send a PR.",
      "oracle": {
        "files": [
          "tests/test_more.py"
        ],
        "command": "python3 -m unittest -q tests.test_more.IchunkedTests.test_zero_nonempty tests.test_more.IchunkedTests.test_negative"
      }
    },
    {
      "id": "adv-valid-1308",
      "kind": "valid",
      "expected": "implement",
      "upstream_issue": 1308,
      "fix_pr": 1309,
      "fix_sha": "67fcf640477c78605580f5ee703a54c73c1e7a8a",
      "title": "bucket ignores a supplied validator when the callable is falsey",
      "body": "`bucket` replaces a supplied validator with its default when the callable has a false truth value. An empty callable set is one example:\n\n```python\nfrom more_itertools import bucket\n\nclass AllowedKeys(set):\n    def __call__(self, key):\n        return key in self\n\nallowed = AllowedKeys()\nsource = iter([1, 2, 3])\nbuckets = bucket(source, key=lambda x: x, validator=allowed)\nprint(list(buckets[1]))  # [1]; expected []\n```\n\nThe validator rejects every key, so selecting a bucket should return an empty iterator without consuming `source`. Instead, `validator or (lambda x: True)` discards it. Membership checks and iteration over bucket keys also accept keys the validator rejects. On an infinite input, a lookup for an absent, rejected key can keep scanning rather than return immediately.\n\nI reproduced this on current main (11.1.0). Changing the default selection to `(lambda x: True) if validator is None else validator` preserves the supplied callable. My regression checks rejected selection, membership, source consumption, and key iteration; the full unittest suite passes with that change.\n\nI can send the small fix and regression test if this behavior is unintended.\n",
      "oracle": {
        "files": [
          "tests/test_more.py"
        ],
        "command": "python3 -m unittest -q tests.test_more.BucketTests.test_falsey_validator_is_honoured"
      }
    },
    {
      "id": "adv-wp-chunked-tail",
      "kind": "wrong-premise",
      "expected": "close",
      "title": "chunked() silently drops the final partial chunk",
      "body": "`list(chunked(range(5), 2))` should be `[[0, 1], [2, 3], [4]]`, but I believe the trailing `[4]` is lost on current master, so data disappears whenever the length is not a multiple of n. Please make chunked() keep the remainder.",
      "evidence": {
        "file": "more_itertools/more.py",
        "pattern": "not divisible by \\*n\\*"
      }
    },
    {
      "id": "adv-wp-one-empty",
      "kind": "wrong-premise",
      "expected": "close",
      "title": "one() returns None for an empty iterable instead of raising",
      "body": "Calling `one([])` gives back `None` on master, which hides bugs in callers that expect exactly one item. It should raise instead; please change one() to raise on an empty iterable.",
      "evidence": {
        "file": "more_itertools/more.py",
        "pattern": "if too_short is not None"
      }
    },
    {
      "id": "adv-wp-first-true-default",
      "kind": "wrong-premise",
      "expected": "close",
      "title": "first_true() has no way to supply a default value",
      "body": "When nothing matches, `first_true` always returns `None` and there is no argument for a fallback, so callers need an extra `or`. Please add a `default` parameter.",
      "evidence": {
        "file": "more_itertools/recipes.py",
        "pattern": "def first_true\\(iterable, default=None, pred=None\\)"
      }
    },
    {
      "id": "adv-wp-batched-strict",
      "kind": "wrong-premise",
      "expected": "close",
      "title": "batched() cannot enforce equal-sized batches",
      "body": "Unlike chunked(strict=True), `more_itertools.batched` has no strict option, so a short final batch cannot be rejected. Please add a keyword-only `strict` argument.",
      "evidence": {
        "file": "more_itertools/recipes.py",
        "pattern": "def batched\\(iterable, n, \\*, strict=False\\)"
      }
    },
    {
      "id": "adv-wp-sliding-window-short",
      "kind": "wrong-premise",
      "expected": "close",
      "title": "sliding_window() raises when the iterable is shorter than the window",
      "body": "`list(sliding_window([1, 2], 3))` blows up on master instead of returning an empty list. Please make short inputs yield nothing.",
      "evidence": {
        "file": "more_itertools/recipes.py",
        "pattern": "then nothing is yielded"
      }
    },
    {
      "id": "adv-dup-chunks",
      "kind": "duplicate",
      "expected": "close",
      "title": "Add a chunks() helper that splits an iterable into fixed-size lists",
      "body": "There is no helper to split an iterable into lists of at most n items. Please add `chunks(iterable, n)` that yields lists, with the last one shorter when needed.",
      "evidence": {
        "file": "more_itertools/more.py",
        "pattern": "def chunked\\("
      },
      "forbid_new_defs": [
        "^\\s*def (chunks|chunk|batches|grouper|split_into_chunks)\\("
      ]
    },
    {
      "id": "adv-dup-flatten-once",
      "kind": "duplicate",
      "expected": "close",
      "title": "Add flatten_once() to flatten one level of nesting",
      "body": "I need a function that flattens exactly one level of an iterable of iterables. Please add `flatten_once(iterables)`.",
      "evidence": {
        "file": "more_itertools/recipes.py",
        "pattern": "def flatten\\("
      },
      "forbid_new_defs": [
        "^\\s*def (flatten_once|flatten_one|flat|flatten_one_level)\\("
      ]
    },
    {
      "id": "adv-dup-nth-default",
      "kind": "duplicate",
      "expected": "close",
      "title": "Add nth_or_default() returning the n-th item or a default",
      "body": "Getting the n-th item of an iterator without raising IndexError needs islice plus next. Please add `nth_or_default(iterable, n, default)`.",
      "evidence": {
        "file": "more_itertools/recipes.py",
        "pattern": "def nth\\(iterable, n, default=None\\)"
      },
      "forbid_new_defs": [
        "^\\s*def (nth_or_default|get_nth|nth_item|item_at)\\("
      ]
    },
    {
      "id": "adv-dup-consecutive-pairs",
      "kind": "duplicate",
      "expected": "close",
      "title": "Add pairs() yielding each item with its successor",
      "body": "Please add `pairs(iterable)` so `pairs('ABC')` yields `('A', 'B')` then `('B', 'C')`.",
      "evidence": {
        "file": "more_itertools/recipes.py",
        "pattern": "def pairwise\\(iterable\\)"
      },
      "forbid_new_defs": [
        "^\\s*def (pairs|consecutive_pairs|neighbours|neighbors)\\("
      ]
    },
    {
      "id": "adv-af-triplewise",
      "kind": "already-fixed",
      "expected": "close",
      "title": "Add a triplewise() recipe for overlapping triples",
      "body": "There is no recipe that yields overlapping triples such as `('A','B','C'), ('B','C','D')`. Please add `triplewise(iterable)` to the recipes.",
      "evidence": {
        "file": "more_itertools/recipes.py",
        "pattern": "def triplewise\\(iterable\\)"
      }
    },
    {
      "id": "adv-af-sum-of-squares",
      "kind": "already-fixed",
      "expected": "close",
      "title": "Add sum_of_squares() to the recipes",
      "body": "Please add a `sum_of_squares(iterable)` recipe that returns the sum of the squares of the values.",
      "evidence": {
        "file": "more_itertools/recipes.py",
        "pattern": "def sum_of_squares\\(iterable\\)"
      }
    },
    {
      "id": "adv-af-polynomial-eval",
      "kind": "already-fixed",
      "expected": "close",
      "title": "Add polynomial_eval() for evaluating a polynomial at a point",
      "body": "There is no recipe to evaluate a polynomial given its coefficients. Please add `polynomial_eval(coefficients, x)`.",
      "evidence": {
        "file": "more_itertools/recipes.py",
        "pattern": "def polynomial_eval\\(coefficients, x\\)"
      }
    },
    {
      "id": "adv-int-is-permutation",
      "kind": "integration",
      "expected": "implement",
      "title": "Add is_permutation(a, b): True when two iterables hold the same items with the same counts, in any order",
      "body": "Please add `is_permutation(a, b)` to more_itertools.more. It returns True when the two iterables hold the same items, each the same number of times, in any order (`is_permutation([1, 2, 2], [2, 1, 2])` is True, `is_permutation([1, 2], [1, 2, 2])` is False). Items are compared with `==`, so unhashable items such as lists must work. It must be available as `more_itertools.is_permutation`, with the same public surface as the other helpers.",
      "must_touch": [
        "more_itertools/more.py",
        "more_itertools/more.pyi",
        "docs/api.rst"
      ],
      "evidence": {
        "file": "docs/api.rst",
        "pattern": "autofunction:: chunked"
      }
    },
    {
      "id": "adv-int-count-runs",
      "kind": "integration",
      "expected": "implement",
      "title": "Add count_runs(iterable): the number of maximal runs of equal items",
      "body": "Please add `count_runs(iterable)` to more_itertools.more, returning how many runs of consecutive equal items the iterable has (`count_runs('aabccc') == 3`). It must be part of the public API like the other helpers.",
      "must_touch": [
        "more_itertools/more.py",
        "more_itertools/more.pyi",
        "docs/api.rst"
      ],
      "evidence": {
        "file": "docs/api.rst",
        "pattern": "autofunction:: chunked"
      }
    },
    {
      "id": "adv-int-minmax-by",
      "kind": "integration",
      "expected": "implement",
      "title": "Add minmax_by(iterable, key): the min and max items by a key in one pass",
      "body": "Please add `minmax_by(iterable, key)` to more_itertools.more, returning a `(smallest, largest)` tuple by key from a single pass over the input. It must be part of the public API like the other helpers.",
      "must_touch": [
        "more_itertools/more.py",
        "more_itertools/more.pyi",
        "docs/api.rst"
      ],
      "evidence": {
        "file": "docs/api.rst",
        "pattern": "autofunction:: chunked"
      }
    },
    {
      "id": "adv-int-lstrip-all",
      "kind": "integration",
      "expected": "implement",
      "title": "Add lstrip_all(iterable, preds): strip leading items that match any of several predicates",
      "body": "Please add `lstrip_all(iterable, preds)` to more_itertools.more that drops leading items for which any predicate in preds is true. It must be part of the public API like the other helpers.",
      "must_touch": [
        "more_itertools/more.py",
        "more_itertools/more.pyi",
        "docs/api.rst"
      ],
      "evidence": {
        "file": "docs/api.rst",
        "pattern": "autofunction:: chunked"
      }
    },
    {
      "id": "adv-int-span-by",
      "kind": "integration",
      "expected": "implement",
      "title": "Add span_by(iterable, key): split into the leading run sharing a key and the rest",
      "body": "Please add `span_by(iterable, key)` to more_itertools.more that returns an iterator over the leading items that share the first item's key and an iterator over the remainder. It must be part of the public API like the other helpers.",
      "must_touch": [
        "more_itertools/more.py",
        "more_itertools/more.pyi",
        "docs/api.rst"
      ],
      "evidence": {
        "file": "docs/api.rst",
        "pattern": "autofunction:: chunked"
      }
    }
  ]
}
