Skip to content

std.task collection combinators: completion ordering, fallible await_all, and coverage #489

Closed

Description

@ehartford

Summary

Collection-await coverage is still incomplete, and current probes expose three
live correctness/compiler failures:

  1. await_first returns the first input, not the first completed task.
  2. await_any returns the first successful input, not the first successful
    completion.
  3. the fallible await_all overload is not usable through the specified public
    surface; the question-mark form reaches an internal compiler BUG.

Some cancellation coverage landed after this issue was filed, so the old
“no focused collection-combinator test” premise is no longer true.

Current evidence (2026-07-15)

The existing
test/spec/spec_ss14_11_await_combinator_cancel_joins.w (f915adf) verifies that
cancelling a parent while each combinator is running cancels and joins its
children. It passes on the current compiler. It does not cover normal early
return, completion ordering, empty inputs, or the public result contracts.

A single current matrix used a slow first task (three suspension ticks) and an
immediately-ready second task:

all=10,20
first=10
any_ok=10
any_errors=e10,e20
any_empty=0
settled0=ok10
settled1=e20

The expected first/any winners are 20. The other displayed ordering and
empty-input cases conform.

The implementation matches the failure:

  • await_first always awaits pending.get(0);
  • await_any awaits pending entries sequentially and returns the first indexed
    Ok;
  • neither observes which task actually completes first.

The fallible await_all surface has a separate generic-call problem. Given
Vec[Task[Result[i32, str]]]:

  • returning tasks |> await_all as Result[Vec[i32], str] selects/involves the
    wrong contract and produces return-type mismatch diagnostics;

  • using the specified tasks |> await_all? form exits 134 with:

    BUG: user generic call lacks a concrete contract
    [src/MirLower.w:13113 MirModule.validate_generic_call_contracts]
    

The empty await_first contract does work: it exits 134 with the stable
“await_first: empty input” panic.

docs/requirements.md currently marks the collection-await requirements
complete while linking this still-open tracker. Those checkmarks do not match
the observed behavior.

Work

  1. Implement completion-aware selection for await_first and await_any. Input
    ordering may determine deterministic tie-breaking, but cannot replace
    completion ordering.
  2. Make the fallible await_all signature callable exactly as specified,
    including the pipeline/question-mark form, without an internal compiler
    abort.
  3. Add one focused §14.11 suite covering:
    • infallible await_all input order despite different completion order;
    • fallible await_all fail-fast when a later input errors before an earlier
      nonterminating/cooperatively-suspending task;
    • await_first winner selection and loser cancellation/join;
    • await_any first Ok, all-fail input-order errors, and empty input;
    • await_settled input order and no early cancellation;
    • combinator-parent cancellation (retain the existing f915adf coverage).
  4. Use live-fiber/cancellation hooks to prove losers have stopped before an
    early-return combinator returns.
  5. Correct requirements traceability after the tests genuinely cover each
    checked requirement.

Do not weaken the tests to the current input-sequential implementation. The
specification leads the stdlib.

Acceptance criteria

  • A slower first input and faster second input make await_first return the
    second value.
  • await_any returns the earliest successful completion, not the lowest input
    index containing Ok.
  • A later fast Err makes fallible await_all cancel/join an earlier suspended
    task and return the Err.
  • All-fail errors and settled/all values remain in input order.
  • Empty await_first and await_any match their documented contracts.
  • Early return and parent cancellation both restore the live-fiber baseline.
  • No supported call form reaches a compiler BUG or generic-contract panic.
  • Full build, fixpoint, audit, and test gates pass.

Traceability

Activity

  1. changed the title [-]Tests: collection await coverage (§14)[/-] [+]std.task collection combinators: completion ordering, fallible await_all, and coverage[/+] on Jul 15, 2026
  2. added a commit that references this issue on Jul 15, 2026
  3. ehartford commented on Jul 15, 2026

    @ehartford
    ContributorAuthor

    Implemented in eafb4ed (§14.11.1).

    Behavior

    • await_all(Task[T]) and await_all(Task[Result[T, E]]) (same-name generic overloads, selected structurally — including the piped tasks |> await_all? spelling), await_first, await_any, await_settled.
    • Completion-first semantics use a runtime completion sequence assigned under the scheduler lock at the completion linearization point (Darwin and Windows fiber cores; layouts 280→288 / 440→448). Input-order polling and randomized select were rejected because both mis-order the "several tasks already done" case; spec_ss14_11_collection_await.w pins that case (later input completing first wins await_first/await_any).
    • Fail-fast fallible await_all returns the first completion-observed Err and cancel/joins unfinished siblings via a defer; await_first/await_any join losers before returning; all-fail await_any errors are input-ordered; await_settled never cancels.
    • Cancellation: LLDB-proven that the runtime cancellation edge unwinds the lexical frame containing the await, so the cancel point (task_cancel_point().await) sits inside each combinator's own frame; spec_ss14_11_await_combinator_cancel_joins.w cancels a parent inside all five combinators and asserts the live-fiber baseline is restored.
    • Empty inputs, harness-verified: behav_await_first_empty_panics.w (exit 134, stderr await_first: empty input); await_any([]) returns Err(Vec.new()) in the spec fixture.

    Compiler prerequisites (same commit)

    • Postfix ? on a pipeline stage reparents over the completed pipeline (Parser.build_forward_pipeline), covered by behav_pipeline.w.
    • Imported same-name generic overloads survive frontend tier-shadowing and are structurally selected in Sema (candidate registry + specificity, ambiguous/no-match diagnostics), covered by behav_generic_overload.w.
    • E0921: the prelude-merged async fn task_cancel_point exposed that concurrency evidence was declaration-based, failing every ZCU with a mutable global (offset-proven to lib/std/task.w:27; use std.time alone already reproduced the same defect on the released compiler). Evidence is now recorded at call/reference sites per §9.1's usage-based obligation — docs/decisions.md D9. Verified by matrix: uncalled decls clean; direct/generic/method/dyn/fn-value calls, async blocks, and instantiated combinator internals all still fail the proof.

    Gates (all on eafb4ed's tree)

    • with build exit 0; :fixpoint FIXPOINT; analyze src/main.w audit:all violations=0
    • :test GATES EXIT 0 (866 behavior / 703 compile-error / 210 spec / all remaining targets green, EMIT-C smoke ok); :test-green and :last-green recorded
    • Debug allocator (drop-bearing str payloads across extraction, fail-fast, completed-loser cleanup, parent cancellation): no new-slice leaks or double-frees; every reported leak is the POD Vec[i32] buffer never freed at scope exit (debug-allocator finding) #608-pinned by-design POD-Vec backing state.

    Notes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions