SDK: sync and run import the declaring module in a child process

The command line no longer imports a research module, so import-time GPU work
ends with a short-lived child instead of holding a CUDA context for the length
of a followed run. fluksio.sdk.declare starts the child, which writes each
flow's document and shims as JSON; Declared stands in for Flow in sync().
Errors keep their type and message, a child's traceback reaches the user.

Claude-Session: https://claude.ai/code/session_01SSi5AvZ7pFmySK8zy3NPMh
This commit is contained in:
2026-10-28 08:11:14 +01:00
parent 6b8473f207
commit 361ef35d58
8 changed files with 364 additions and 24 deletions
+16 -6
View File
@@ -42,6 +42,7 @@ from fluksio.sdk.client import (
repo_root,
sync,
)
from fluksio.sdk.declare import ImportFailed, declare, declare_module
__all__ = ["add_parsers", "discover"]
@@ -199,6 +200,10 @@ def discover(targets: list[str], keep_going: bool = False) -> list[Flow]:
two directories away is not a reason to refuse to run this one. A
collision between two module names is never skipped — it would produce a
node body that imports the wrong file.
Imports in *this* process. The commands go through
:func:`fluksio.sdk.declare.declare`, which runs this in a child so the
command line never holds what the modules open.
"""
for target in targets:
path = Path(target)
@@ -336,9 +341,14 @@ def cmd_login(args: argparse.Namespace) -> int:
def cmd_sync(args: argparse.Namespace) -> int:
targets = args.targets or ["."]
try:
flows = discover(targets)
flows = declare(targets)
except (ImportError, SyncError) as exc:
return _fail(str(exc))
except ImportFailed as exc:
# What an uncaught exception from the module printed, back when the
# import happened in this process.
print(exc.trace, file=sys.stderr, end="")
return _fail(str(exc))
if not flows:
return _fail(
f"no flows declared in {', '.join(targets)} — a flow is a `Flow(...)` "
@@ -553,7 +563,8 @@ def _sync_first(
A flow with no such record — never synced, or an engine older than the
field — falls back to the whole directory, downwards, skipping what the
repository ignores; a study there that will not import is a warning.
`--sync` names the targets outright.
`--sync` names the targets outright. Either way the import happens in a
child process (see :mod:`fluksio.sdk.declare`), not in this one.
A directory that declares nothing is not an error: a flow drawn on the
canvas is run the same way, and has nothing to upload.
@@ -561,13 +572,12 @@ def _sync_first(
declared = None if targets else _declaration(client, flow)
try:
if declared is None:
flows = discover(targets or ["."], keep_going=True)
flows = declare(targets or ["."], keep_going=True)
else:
# Under the name it was synced with, so the node bodies it
# generates are the ones already stored.
_import(*declared)
flows = list(FLOWS.values())
except SyncError as exc:
flows = declare_module(*declared)
except (SyncError, ImportFailed) as exc:
# Do not fail a run for a module the run may not even need.
_say(f"warning: nothing synced — {exc}")
return