feat(pipekit-ops): a readable recipe format with named steps

Aperta
#222 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python, yaml
Ambito
api, backend

Direzione di ricerca

Read packages/pipekit-ops/src/pipekit_ops/serial.py and _base/graph.py, focusing on dumps, loads, Graph.get_config, and Sequential serialization. Then inspect the package exports and serialization tests to see how state records and sandbox checks work. Done means the readable API round trips supported graphs, handles shared operators and nesting, and has tests and exports; YAML support and explicit step naming are design decisions still open in the issue.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

area:code layer:0-primitives priority:p2 type:feature wave:1

Problem / Request

pipekit.dumps(graph) writes a record built for machines: operators in a list, nodes that point at operators and at each other by index. It round-trips, but nobody can read a recipe from it or edit one by hand. There is no readable format with named steps, though the serial.py docstring already promises YAML front ends in an optional extra (pipekit[yaml]), which no package ships.

User Story

As someone who keeps recipes in a repository, reviews them in pull requests and edits them by hand, I want a readable form of a Graph, with each step named and its inputs named, that loads back to the same Graph.

Motivation

Found while writing the GeoStack Kinds and Roles design page (jejjohnson/geostack#127), whose plume recipe has four steps. Its dumps today:

{
  "class": "Graph",
  "config": {
    "inputs": ["scene", "wind"],
    "nodes": [
      { "operator": 0, "parents": [{ "input": "scene" }] },
      { "operator": 1, "parents": [{ "node": 0 }] },
      { "operator": 2, "parents": [{ "input": "wind" }] },
      { "operator": 3, "parents": [{ "node": 0 }, { "node": 1 }, { "node": 2 }] }
    ],
    "operators": [
      { "class": "MethaneRetrieval", "config": { "window_nm": [2122.0, 2488.0] }, "module": "plume" },
      { "class": "PlumeSegmentation", "config": { "k": 3.0, "min_pixels": 20 }, "module": "plume" },
      { "class": "WindAt", "config": { "lat": 31.98, "lon": -103.52 }, "module": "plume" },
      { "class": "EmissionRate", "config": { "kappa": 6.56e-07 }, "module": "plume" }
    ],
    "outputs": { "emission": { "node": 3 } }
  },
  "module": "pipekit_ops._base.graph"
}

To see what EmissionRate takes, you count positions in nodes, then in operators. The page now shows a readable view built by a 20-line helper. It should be pipekit's: the same view in a notebook, a PR diff, a DVC params file and a GeoApps upload.

Proposed API

# readable records: plain dicts, no new dependency, in pipekit-ops
def to_readable(op: Operator) -> dict[str, Any]: ...
def from_readable(record: dict[str, Any]) -> Operator: ...      # same checks as loads
def from_readable_sandboxed(record: dict[str, Any]) -> Operator: ...  # allowlist, as loads_sandboxed

# YAML on top, in the optional extra the serial.py docstring names
def dumps_yaml(op: Operator) -> str: ...                         # pipekit[yaml]
def loads_yaml(text: str, *, sandboxed: bool = False) -> Operator: ...

Design Snapshot

The same recipe, readable:

graph:
  inputs: [scene, wind]
  steps:
    methane_retrieval:
      op: plume.MethaneRetrieval
      in: [scene]
      config: {window_nm: [2122.0, 2488.0]}
    plume_segmentation:
      op: plume.PlumeSegmentation
      in: [methane_retrieval]
      config: {k: 3.0, min_pixels: 20}
    wind_at:
      op: plume.WindAt
      in: [wind]
      config: {lat: 31.98, lon: -103.52}
    emission_rate:
      op: plume.EmissionRate
      in: [methane_retrieval, plume_segmentation, wind_at]
      config: {kappa: 6.56e-07}
  outputs:
    emission: emission_rate

Rules the format needs (the implementer decides the details, the tests pin them):

  • Step names. By default, the operator's class in snake case, with _2, _3, … when a class repeats. Explicit names are an open question: a name= on the symbolic call, or a names= mapping on Graph.
  • in. The positional inputs, in order; each is a declared input or an earlier step, by name. The order of steps is the topological order, as nodes is now.
  • op. module.Class, so the loader resolves it exactly as from_state does.
  • config nested, not inlined. Inlining the config next to op and in would break for any operator with a parameter called op or in.
  • Shared operators. The machine record lists an operator once when several nodes use it. The readable form needs to keep that: for example op: {same_as: methane_retrieval}.
  • Nesting. A Sequential inside a step reads as a list of {op, config}; a Graph inside a step reads as a nested graph:.
  • Round trip. from_readable(to_readable(g)).state == g.state for every Graph dumps accepts. Operators that set forbid_in_yaml refuse, as loads does.

References & Existing Code

  • packages/pipekit-ops/src/pipekit_ops/serial.py: dumps, loads, from_state_sandboxed, loads_sandboxed, and the docstring that names pipekit[yaml].
  • packages/pipekit-ops/src/pipekit_ops/_base/graph.py: Graph.get_config, the record the readable form maps to and from.
  • The motivating page: jejjohnson/geostack#127, docs/design/pages/kinds-and-roles.html, section "⑥ Recipe, and orchestration".

Implementation Steps

  • Add to_readable / from_readable / from_readable_sandboxed in packages/pipekit-ops/src/pipekit_ops/serial.py, mapping to and from the Graph.get_config record (and Sequential's), recursively.
  • Default step names, deduplicated; decide explicit names (call-site name= or Graph(names=…)).
  • Shared operators and nested containers, as above.
  • dumps_yaml / loads_yaml behind a [yaml] extra (PyYAML, safe_load only), or fix the serial.py docstring if YAML stays out of scope.
  • Export the new functions from pipekit_ops/__init__.py (and the umbrella, if that is where dumps is exported).

Definition of Done

  • Code lands at the intended path
  • Public API exported via src/<package>/__init__.py
  • Tests pass: make test
  • Lint + typecheck pass: make lint && make typecheck
  • Docstrings (Google-style) on all public symbols

Testing

  • Round trip: for a linear chain, a fan-in Graph, a Graph with a shared operator and a Graph nesting a Sequential, from_readable(to_readable(g)).state == g.state.
  • Step names: deduplicated when a class repeats; stable across runs.
  • A config with keys named op or in survives the round trip.
  • from_readable_sandboxed rejects a class outside the allowlist at any depth; forbid_in_yaml operators refuse.
  • YAML: loads_yaml(dumps_yaml(g)).state == g.state, and loads_yaml never constructs arbitrary Python objects (safe_load).

Documentation

  • The serialisation docs page: the readable form next to the JSON, with the recipe above as the example.

Relationships

  • Related: jejjohnson/geostack#127 (the design page that found it); #221, the multi-input signatures filed alongside this one.

Generated by Claude Code

Lingua principale
Python
Stelle
0
Fork
0
Merge medio
3h 50m
PR unite (30g)
143

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di jejjohnson/pipekit

Tutte le issue di jejjohnson/pipekit

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.