Skip to content

0.18 PR3: stop before pulling, validate take/total - #30

Merged
ildella merged 3 commits into
masterfrom
fix-pre-pull-stop
Nov 24, 2026
Merged

ildella merged 3 commits into
masterfrom
fix-pre-pull-stop

Conversation

@ildella

@ildella ildella commented Nov 24, 2026

Copy link
Copy Markdown
Owner

What

PR 3 of the 0.18 stabilization plan (fix/pre-pull-stop).

3a. Pre-loop guard + re-check after index++

runAsyncStrategy (src/functional.js) and runSyncStrategy
(src/functional-sync.js) now check the stop condition before entering the
loop and again right after index++:

  • take: 0 pulls zero items from the source.
  • An already-aborted signal pulls zero items and never starts a dying
    source (no sourceErrors).
  • An abort during pause or during the step no longer pulls the next item.
  • The outcome?.skip branch is gone: index++ is common to skip and success.
  • for await / for...of stay, so break still calls iterator.return()
    and generator finally blocks still run.

3b. take / total validation

assertTake / assertTotal in src/run.js, called from planSeries and
findSync before getPlannedTotal:

  • take must be a non-negative integer.
  • total must be a finite non-negative number (total: 0 is valid).

planFold is untouched: rejecting take/total on scan/reduce belongs to
PR 7.

Tests

  • New tests/take-limits.test.js counts actual pulls from a generator (not
    just results) for series, seriesSync, filter, filterSync,
    mapSeries, mapSeriesSync, findSync: take: 0 → pulled === 0,
    take: 1 → pulled === 1 plus cleanup, broken source never started,
    invalid take/total → TypeError, total: 0 valid.
  • tests/signal.test.js extended with pull counts for pre-abort, abort during
    pause, abort during step, dying source on pre-abort, and scan/reduce with
    a pre-aborted signal.
  • Cleanup is proven by a counting generator whose finally must run
    (cleanedUp === true).

All new tests fail on the old loop code (verified by stashing the src changes)
and pass with the fix.

Gates

  • yarn lint — clean
  • yarn vitest run — 972 passed (933 existing + 39 new), 0 failed
  • yarn check — clean (dup + knip)

No existing test had to change: none asserted the buggy pre-pull behaviour.

Plan reference: .opencode/plan/pipelean-0.18.md §PR 3 (lines 257–368), risk #2
(for await cleanup).

- Guard take/signal before the loop and re-check after `index++` in the
  series and seriesSync engines, so `take: 0` and an already-aborted
  signal pull zero items and an abort during pause or step does not pull
  the next one. `break` keeps iterator.return() and generator finally
  semantics intact.
- Validate `take` as a non-negative integer and `total` as a finite
  non-negative number in planSeries and findSync, before the planned
  total is computed.
- Count actual pulls (not just results) in take-limits and signal tests,
  including pre-abort, abort during pause, abort during step, folds with
  a pre-aborted signal and generator cleanup.
@ildella
ildella marked this pull request as ready for review November 24, 2026 14:35

@ildella ildella left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APPROVED

REVIEW: Both review passes found no medium-or-higher issues. The pre-loop guards stop take: 0 and a pre-aborted signal before the source is pulled — no next() call, no source errors — and the post-index++ guard prevents the next pull after an abort or a reached take limit while preserving skip and progress index semantics. assertTake/assertTotal validate at the single shared point (planSeries) plus directly in findSync; scan/reduce correctly stay unvalidated per plan. docs/reference.md covers the new take/total contract.

FINDINGS: No medium-or-higher findings. Low/info follow-ups:

  • LOW — invalid take/total on async filter and mapSeries throws series() take must be a non-negative integer, because planSeries defaults the label to 'series'. Trigger: filter(items, fn, {take: -1}); behavior is correct but the message names the wrong wrapper. Plan-conformant, sync twins label correctly.
  • LOW — the countingSource fixture is duplicated in tests/signal.test.js and the new tests/take-limits.test.js. Trigger: changing pull-count semantics requires two edits; test scaffolding only.
  • INFO — the async pre-loop check and the first in-loop check both evaluate index 0. Trigger: harmless for take, meaningful for signal that aborts between them.
  • INFO — curried mapSeries(fn, {take: -1}) defers validation to a rejected promise at call time, while its strategy options validate at construction. Trigger: constructing an invalid run; pre-existing split and untested here.
  • INFO — take: 0 never starts the generator, so its finally does not execute (cleanedUp stays false). Trigger: writing a cleanup assertion for that case; correct behavior since nothing was acquired.
  • INFO — tests/signal.test.js adds a blanket eslint-disable max-lines while mixing cancellation and source-pull suites in one file.

CI: green — the test check (Code Check workflow) succeeded.

@ildella
ildella merged commit 4482f24 into master Nov 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant