feat(ops): open(), close(ok) and opened(op), setup and teardown around a run
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start with src/pipekit_ops/core/_operator.py and the container implementations, then read the P9 roadmap entry in packages/pipekit-ops/docs/design/roadmap.md and inspect Graph traversal order. Implement and export opened from src/pipekit_ops/core/_lifecycle.py; use the listed G9 tests and public operator sweep to check ordering, cleanup, and deduplication. Done means the API, tests, docs, and roadmap update meet the issue’s definition of done.
Written by the indexing model from the issue text.
Description
Problem / Request
An operator that holds a resource (a signal handler, an open file, a thread, a clock) has no hook to acquire it before a run and release it after.
This issue adds Operator.open() and Operator.close(ok), which do nothing by default, a children() method that lists an operator's direct children, and a context manager opened(op) that opens a whole pipeline in order and closes what opened in reverse.
It is phase P9 of packages/pipekit-ops/docs/design/roadmap.md.
User Story
As the author of an operator that installs a signal handler, I want the pipeline to set it up before the run and always put it back afterwards, so that I don't write a
try/finallyaround every loop that uses it.
Motivation
sesmax-experiment's monitors have on_run_start and on_run_end(ok=...) hooks, and its chunked run loop calls them by hand (run/chunks.py and RunMonitors.start / RunMonitors.end in run/stages.py @ 12f169f6bf):
ok = False
try:
watch.start(state0, spec=spec) # records each monitor whose start returned
frame, _ = Cycle(step_op=chunk, n_steps=timeline.n_chunks)(first, 0)
ok = True
finally:
watch.end(last[0].state, ok=ok) # ends only those that started, newest first
The "only those that started" rule matters: its SignalMonitor replaces the process's SIGTERM handler in on_run_start, and a later monitor whose start raises must not leave that handler installed.
pipekit-train's Callback protocol (on_train_begin, … in pipekit_train/loop/_callbacks.py @ be1b64a900) is a second copy of the same idea.
Operator has no such hook today, and no generic way to list an operator's children: each container keeps them in its own attributes (Branch.if_true, Try.primary, Retry.op, a Sequential's list, …).
Proposed API
# pipekit_ops/core/_operator.py (proposed, this issue)
class Operator(ConfigMixin):
def open(self) -> None:
"""Acquire what this operator holds for a run. Default: nothing."""
def close(self, ok: bool) -> None:
"""Release it; ok is False when the run raised. Default: nothing."""
def children(self) -> tuple[Operator, ...]:
"""The direct child operators, in the order they run.
Default: every attribute that is an Operator, or a list, tuple or dict
whose values are Operators, in attribute order.
"""
# pipekit_ops/core/_lifecycle.py (proposed, this issue)
@contextlib.contextmanager
def opened(op: Operator) -> Iterator[Operator]:
"""Open op and its descendants depth-first in run order; on exit close those
that opened, in reverse, with ok=False if the block raised."""
Design Snapshot
from pipekit_ops import Sequential, opened
pipeline = Sequential([read, StopOnSignal(), advance, write]) # StopOnSignal from #385
with opened(pipeline):
carrier, state = Cycle(step_op=pipeline, n_steps=100)(x0, 0)
# every handler StopOnSignal installed is back as it was, even if the run raised
opened walks the tree once with children(), skips an operator it has already opened (the same instance can sit in two places of a Graph), and calls open() on each in that order.
If an open() raises, it closes the ones already opened with ok=False and re-raises.
A Stop (#382) leaving the block counts as ok=True when its resumable or complete flag is set, and as ok=False otherwise.
Under Map with a thread or process executor, opened opens the operator in the calling process only; opening per worker is out of scope.
Calling an operator outside opened behaves exactly as today.
References & Existing Code
- Roadmap:
packages/pipekit-ops/docs/design/roadmap.md, phase P9 (merged in #374). - sesmax-experiment's lifecycle, linked above, and
SignalMonitorinmonitor/builtins.py@12f169f6bf. Operatorinpipekit_ops/core/_operator.py@be1b64a900.public_operators(module)inpipekit_ops/testing/__init__.pylists every exported operator class, for the sweep below.
Implementation Steps
- Add
open,closeandchildrentoOperatorinsrc/pipekit_ops/core/_operator.py; none goes intoget_config. - Override
childrenwhere the attribute order is not the run order; checkGraphfirst, whose nodes may be stored in a different order from the one they run in (unverified). - Add
openedinsrc/pipekit_ops/core/_lifecycle.pyand export it frompipekit_ops.coreandpipekit_ops. - Add the sweep test below, so a container that hides a child fails CI.
- Mark P9 as built in the roadmap.
Definition of Done
- Code lands at the paths above
- Public API exported via
pipekit_ops/__init__.py - Tests pass:
make test - Lint and typecheck pass:
make lint && make typecheck - Google-style docstrings with a doctested example of
opened
Testing
- G9-1: for
Sequential([a, b, c])underopened, the opens run asa, b, cand the closes asc, b, a. - G9-2: when
b.open()raises,a.close(ok=False)runs,cis never opened, and the error propagates. - G9-3: an exception inside the block closes every operator with
ok=Falseand propagates; a clean exit closes withok=True. - G9-4: an operator inside
When(EveryN(2), Retry(Sequential([op]), attempts=2, base_delay=0, on=(OSError,)))is opened and closed exactly once (orBranch(...)if #381 has not landed). - G9-5:
get_configandpipeline_hashof every public operator are unchanged. - Sweep: for each container in
public_operators(pipekit_ops), a recording child placed in it is opened and closed byopened. - The same instance placed twice in a
Graphis opened once.
Documentation
- The core API page describes
open,close,childrenandopened, with the ordering rules.
Relationships
- Parent (theme epic): #379
- Blocked by: #382 (the
okrule for aStopleaving the block) - Blocks: #385
- Related: #326 (
typing.get_type_hintsfails on every operator; new methods onOperatorshould not make it worse)
- Dominant language
- Python
- Stars
- 0
- Forks
- 0
- Avg merge
- 3h 50m
- Merged PRs (30d)
- 143
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from jejjohnson/pipekit
-
area:code layer:0-primitives priority:p2 type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
jejjohnson/pipekit#296 ·
Maintainers usually reply within 1 day
-
area:code type:feature
Difficulty 3/5 1-2 days Newbie friendliness 57/100
jejjohnson/pipekit#411 ·
Maintainers usually reply within 1 day
-
area:code type:feature
Difficulty 4/5 3-5 days Newbie friendliness 48/100
jejjohnson/pipekit#410 ·
Maintainers usually reply within 1 day
-
area:code type:feature
Difficulty 5/5 Over a week Newbie friendliness 38/100
jejjohnson/pipekit#409 ·
Maintainers usually reply within 1 day
-
area:code type:feature
Difficulty 3/5 1-2 days Newbie friendliness 75/100
jejjohnson/pipekit#408 ·
Maintainers usually reply within 1 day
All issues in jejjohnson/pipekit
Similar issues
-
bug(operator): VolterraOperator built inside jax.jit with a concrete decay raises TracerBoolConversionErrorPossibly taken @jejjohnson claimed this today. Opentype:bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
jejjohnson/integrix#144 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
finos/ai-governance-framework#459 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
autowarefoundation/autoware_carla_scenario#2963 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
freelawproject/litigant-portal#1307 ·
Maintainers usually reply within 2 days
-
type/bug
Difficulty 1/5 Under an hour Newbie friendliness 86/100
h2oai/h2o-llmstudio#1163 ·
Maintainers usually reply within 1 day