# FEATURE: Generator Control

| Field | Value |
|-------|-------|
| Version | 20261028 V1 |
| Description | Reduce streams and control generator execution. |
| Depends On | FEATURE-Bindings.md, FEATURE-Assignment.md |
| Provides | jq reductions and generator control |
| Consumes | jq lexical bindings and functions, jq path mutation |

## Purpose

Provide reductions, intermediate accumulator streams, lexical control transfer, and bounded consumption of generators.

## Behavior

`reduce` evaluates its source stream in order, binds each result to its pattern, and applies the update to accumulator states initialized by the initializer. `foreach` follows the same accumulation model and emits the extraction stream for each intermediate state; omitted extraction is identity. Initializers and updates may produce multiple values.

`label` and `break` use lexical scope and stop the targeted stream without suppressing outputs already emitted. Implement `limit`, `skip`, `first`, `last`, and `nth` in their documented arities. Stop consuming a source when the selected result or limit is reached, so an unneeded later error is not evaluated. Preserve jq's behavior for empty streams, zero and negative counts, and multi-output arguments.

## Programmatic Acceptance

=== AC generator-control-conformance ===
Intent: Reduction, intermediate extraction, and bounded generator consumption pass their applicable corpus cases.
Suite: scoped
Requires: executable=python3; scope=test
Probe: --list
Probe Count: summary

import json
import os
import subprocess
import sys

SELECT = r"^(?:reduce \.\[\] as \$x \(0; \. \+ \$x\)|\[foreach \.\[\] as \[\$i, \$j\] \(0; \. \+ \$i - \$j\)\]|\[limit\(3; \.\[\]\)\]|\[skip\(3; \.\[\]\)\]|first\(1,error\(\"foo\"\)\)|nth\(1; 0,1,error\(\"foo\"\)\))$"
result = subprocess.run(
    [sys.executable, "sources/run_conformance.py", "--select", SELECT, "--json"],
    capture_output=True, text=True,
    env={**os.environ, "JQ": os.path.abspath("jq")},
)
print(result.stdout)
print(result.stderr, file=sys.stderr)
report = json.loads(result.stdout)
tally = report["summary"]
assert sum(tally.values()) > 0, SELECT
assert tally["fail"] == 0 and tally["error"] == 0, tally
assert result.returncode == 0, result.returncode
=== END AC generator-control-conformance ===

## User Acceptance

- None.

## Guardrails

- Do not mutate any file under `sources/`.
- Preserve lazy evaluation, generator order, multiplicity, and partial output.
- Cross-feature cases requiring later builtins remain covered by scoped integration.
