feat(core): combinators MVP — sequence, parallel, map#102
Merged
Conversation
Phase 3. Pure functions over ctx.run — each combinator IS an Agent,
so they recurse without limit. Runs in-process via makeCtxRun or
through the daemon via runtime.dispatch transparently.
Combinators (src/combinators/index.ts)
- sequence(...steps) — run in order; if step's result.output is an
object it becomes next step's input.config; halt on first failure
- parallel(steps) — concurrent; tuple of outputs in input order;
any child failure flips overall success=false, does NOT cancel
siblings (caller decides via result)
- map<T>(items, factory) — fan-out; each child's input.config gains
{ item }; output is array in item order
Helper (src/combinators/merge.ts)
- mergeGenerators<T, R>(gens): interleaves yielded T in arrival
order, collects R[] returns in input order. Used by parallel/map
to stream child events as they arrive while preserving result
ordering.
Accept StepRef = Agent | string; string refs resolve via ctx.run's
registry (same rules as ctx.run itself).
Tests (+16, total 123)
- merge: multi-source yield + return collection, empty, fast/slow
arrival ordering
- sequence: empty, object output threads as config, halt on failure,
string ref via registry, event forwarding across steps
- parallel: empty, outputs in input order, partial failure, event
interleaving
- map: empty items, factory per-item, { item } injection into
config, partial failure
Exports: sequence, parallel, map, StepRef, MapFactory from
@agentage/core.
Contributor
|
🎉 PR Validation ✅ PASSED Commit: Checks:
Ready to merge! ✨ 🔗 View workflow run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Phase 3 of `projects/work/specs/agent-composition.md`. Three pure functions over `ctx.run` — each combinator is itself an `Agent`, so they recurse without limit. Runs in-process (via `makeCtxRun`) or through the daemon (via `runtime.dispatch`) with no code changes.
```ts
import { sequence, parallel, map } from '@agentage/core';
// Chain
export default sequence(fetcher, enricher, summarizer);
// Fan-out with post-processing
export default sequence(
parallel([prReviewer, lintChecker, testRunner]),
reporter,
);
// Per-item
export default map(['repo-a', 'repo-b'], (r) => agentForRepo(r));
```
Combinators
`sequence(...steps: StepRef[]): Agent`
`parallel(steps: StepRef[]): Agent`
`map(items: T[], factory: (item, i) => StepRef): Agent`
`StepRef = Agent | string`
String refs resolve via `ctx.run`'s registry — same rules as `ctx.run` itself.
Helper
`mergeGenerators<T, R>(gens): AsyncGenerator<T, R[], void>` — interleaves yielded values in arrival order, collects returns into `R[]` in input order. The single primitive that makes `parallel` and `map` work without losing events.
Test coverage (+16, total 123)
merge (3): multi-source yield + return collection, empty input, fast/slow arrival ordering
sequence (5): empty, object-output threaded as config, halt-on-first-failure (confirms step B is NOT called), string ref via registry, event forwarding across steps
parallel (4): empty, outputs in input order, partial failure (flips success=false, still collects all), event interleaving
map (4): empty items, factory per item, `{ item }` injection into `config`, partial failure
Verify
`npm run verify` — 123/123 tests, type-check, lint, format, build all clean.
Decisions locked (per spec)
Next