Documentation
¶
Overview ¶
Package executor runs pipeline stages: a bounded goroutine pool with SQLite checkpointing.
Its job is to never re-spend. An interrupted run resumes rather than restarts, so Ctrl-C costs nothing but the in-flight call. Stage purity is what makes the platform's durable-workflow executor a registration exercise rather than a rewrite.
Package executor runs work over a bounded pool with checkpointing.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultConcurrency ¶
func DefaultConcurrency() int
DefaultConcurrency is what a zero Concurrency resolves to.
Exported because a caller planning against a budget has to know the number it will actually run at. Treating zero as "unset, skip the check" let core's feasibility guard be bypassed on the CLI's default path — the one almost every user takes.
Modest by default. The work is I/O against a rate-limited provider, so the useful ceiling is set by that provider, not by this machine.
Types ¶
type Options ¶
type Options struct {
// Concurrency bounds in-flight work. Zero means a conservative default.
//
// The constraint is provider rate limits, not CPU: a bigger pool buys
// nothing but a faster path to a 429.
Concurrency int
// ID identifies an item, for Skip and for error messages. Required.
//
// The executor is generic over its item type — it has no business knowing
// what a Case is — so the caller supplies the identity function.
ID func(any) string
// Skip reports whether an item is already complete and should not be
// re-executed. Nil means execute everything.
//
// This is how resume avoids paying twice. It is consulted in the producer,
// before any work is dispatched, so a skipped item costs nothing.
Skip func(caseID string) bool
// IsFatal reports whether a work error ends the whole run rather than just
// that item. Nil means every work error is per-item.
//
// Without this there is no path from a WorkFunc to shutdown, and a
// budget-exhausted run would dispatch every remaining item only to have
// each one denied — completing with thousands of failures instead of
// stopping as budget-stopped and resumable. The caller decides which
// errors qualify, so this package needs no knowledge of the budget guard.
IsFatal func(error) bool
// RecordGrace bounds how long result recording may continue after the
// caller's context is cancelled. Zero means a sensible default.
//
// Recording outlives cancellation on purpose — the money is already spent —
// but not indefinitely, or a hung sink would make Ctrl-C unbounded.
//
// It is armed BY the cancellation, not at run start. It was a
// context.WithTimeout built before the first item was dispatched, which
// made it a bound on the whole run: every result after 30 seconds was
// silently discarded, sinkBroken latched so the loss cascaded to every
// result after it, and a resumed run paid for all of them again. The
// godoc above always described this behavior; nothing implemented it until
// docs/debt.md#54.
RecordGrace time.Duration
// PerRecordTimeout bounds a SINGLE SinkFunc call. Zero means a sensible
// default.
//
// This is the bound that makes a hung sink survivable during a run, and it
// is per call because the hazard is one call that never returns — not a
// budget the whole run draws down. Bounding the sum instead is what
// docs/debt.md#54 was.
PerRecordTimeout time.Duration
// AfterRecord runs on the sink goroutine once a result is DURABLY
// recorded, and a non-nil return ends the run.
//
// It exists because IsFatal is consulted only on a work ERROR, so there is
// no path from a SUCCESSFUL item to shutdown — and some conditions are
// discovered in a successful response. The resolved model changing mid-run
// is the case that forced this: the answer is paid for and scoreable, so
// failing the item to stop the run would discard a result the caller
// already owns, and returning an error from SinkFunc would latch
// sinkBroken and discard every result after it.
//
// Ending the run here keeps the triggering result durable and counted,
// which is the whole point of the seam.
//
// result is a Result[I, T] boxed as `any`, for the same reason ID's
// argument is untyped: this package is generic over its item type and
// Options is not. Type-assert it.
//
// Boxed WHOLE rather than splintered into (item, value, err): Result
// documents that exactly one of Value and Err is meaningful, and three
// loose parameters both discard that invariant and hand the caller a
// non-nil `any` wrapping a nil *T on the failure path — a trap that needs
// a godoc paragraph to survive rather than a shape that cannot spring it.
// Done() comes along for free.
//
// The returned error is surfaced to the caller verbatim, so it must be a
// code or an identifier, never prompt or response content.
//
// It runs on the sink goroutine with no context and no timeout of its own:
// a blocking implementation stalls the run, exactly as a blocking SinkFunc
// would but without PerRecordTimeout to bound it. A panic inside it is
// recovered and ends the run rather than deadlocking it.
//
// It is not called again after returning an error, but IS called for
// results already in flight during the drain that follows — the first
// error is the one reported.
AfterRecord func(result any) error
}
Options configures a Run.
type Result ¶
type Result[I proto.Message, T any] struct { // Item is the input this result belongs to. Item I // Value is the work's output. Nil when Err is set. Value *T // Err is the terminal failure. Nil when the work succeeded. Err error }
Result is one item's terminal outcome.
Exactly one of Value or Err is meaningful, mirroring the store's Outcome and the event stream's CaseScored/CaseErrored split. An item is either done or failed; it is never both, because a shape permitting both would let one item land on both sides of the denominator.
type SinkFunc ¶
SinkFunc durably records one result.
Called from a single goroutine, so implementations need no locking of their own and results are recorded in completion order. Returning an error stops the run: if results cannot be persisted, continuing would spend money whose outcome nothing can record, and resume would pay for it again.
ctx carries a PerRecordTimeout deadline for THIS call. It does not inherit the caller's cancellation — recording outlives that on purpose — but it is cancelled once RecordGrace expires after the caller cancels, which is how a Ctrl-C eventually reaches an implementation that is still writing.
type Stats ¶
type Stats struct {
// Dispatched is the number of items handed to workers.
Dispatched int
// Skipped is the number recognized as already complete.
Skipped int
// Succeeded and Failed count DURABLY RECORDED outcomes, and partition
// Dispatched once the run is over.
//
// Counted after the sink accepts them, not when work finishes, so they
// describe outcomes that survive the process. A result the sink rejected
// is neither, and the run ends with an error saying so — reporting it as
// succeeded would tell a resumed run that an unrecorded item was done.
Succeeded int
Failed int
}
Stats reports what a run did.
func Run ¶
func Run[I proto.Message, T any]( ctx context.Context, items iter.Seq2[I, error], work WorkFunc[I, T], sink SinkFunc[I, T], opts Options, ) (Stats, error)
Run executes work over items, recording each result through sink.
The pipeline is producer, workers, sink:
producer workers (N) sink (1)
──────────────── ─────────── ────────
range items ──▶ recv case ──▶ recv result
clone each Case work(ctx, case) record durably
select{ send, done } send result
Rules that make it correct, each answering a specific failure:
The clone happens in the PRODUCER, before the send. Cloning after the pointer crosses the channel leaves a window where the producer's next iteration can reuse the backing memory while a worker still holds it. The Ring-0 contract says yielded values are borrowed for one iteration, and this is the boundary where that stops being documentation.
Every send selects on the shutdown context. Without it a producer can block forever handing off to workers that are all stuck, leaving no goroutine alive to service a cancellation.
Shutdown is bidirectional. The caller cancels top-down; a fatal iterator error, a sink failure, or a panic cancels bottom-up. Both route through one internal context, because a worker-side failure has to be able to stop the producer's range loop.
A fatal error from the item source stops iteration but DRAINS what is already in flight. Those items' work is already paid for; discarding their results would spend money and record nothing.
Worker AND sink panics are recovered. A panic in one item must not take down a run that has spent money on hundreds of others, and a panic on the sink goroutine would be worse still: no caller's recover can reach another goroutine, so it would kill the process mid-run.
A completed result is ALWAYS delivered to the sink, even during shutdown. The work is already paid for, and a result that never reaches the sink is one a resumed run pays for again.
Recording outlives the caller's cancellation, under a grace period the cancellation itself arms. On Ctrl-C the caller's context is precisely the one that died, so recording through it would drop the results it is meant to preserve — and bounding the whole run instead of the drain discards them just as thoroughly, which is docs/debt.md#54.
AfterRecord is the only path from a SUCCESSFUL item to shutdown. IsFatal answers for work errors; some conditions are only visible in an answer the caller has already paid for and wants to keep.
A sink that blocks forever stalls one result for PerRecordTimeout and is then reported as broken; a sink that blocks and ignores its context stalls the run indefinitely, which is the caller's responsibility to avoid. RecordGrace bounds the drain AFTER the caller cancels, not the run.