Skip to content

Commit 98c652a

Browse files
merschformannclaude
andcommitted
Adding instance_id support via AppRunConfig
Allows a predecessor step to pick the instance per sub-run, alongside the run configuration. Both replace their decorator-level counterpart as a whole, rather than merging field by field. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent f0f6e68 commit 98c652a

5 files changed

Lines changed: 72 additions & 20 deletions

File tree

‎nextpipe/decorators.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,8 @@ def __init__(
798798
The ID of the Nextmv Application to run.
799799
instance_id : str, optional
800800
The ID of the instance to run. Default is defined by the app on Platform.
801+
A predecessor step may override this per run by returning an `AppRunConfig`
802+
carrying its own `instance_id`.
801803
options : dict[str, Any], optional
802804
The options to pass to the application, by default None.
803805
full_result : bool, optional
@@ -886,7 +888,9 @@ def app(
886888
app_id : str
887889
The ID of the application to run.
888890
instance_id : str
889-
The ID of the instance to run. Default is defined by the app on Platform.
891+
The ID of the instance to run. Default is defined by the app on Platform. A
892+
predecessor step may override this per run by returning an `AppRunConfig`
893+
carrying its own `instance_id`.
890894
options : dict[str, Any]
891895
The options to pass to the application. This is a dictionary of
892896
parameter names and values. The values must be JSON serializable.

‎nextpipe/flow.py‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -901,14 +901,15 @@ def __prepare_app_run_args(
901901
node: FlowNode,
902902
inputs: list[object],
903903
app_step: decorators.App,
904-
) -> tuple[dict[str, Any], nextmv.PollingOptions, bool]:
904+
) -> tuple[dict[str, Any], nextmv.PollingOptions, bool, str]:
905905
"""
906906
Prepare arguments for running an app step.
907907
908908
Returns
909909
-------
910-
tuple[dict[str, Any], nextmv.PollingOptions, bool]
911-
A tuple of run kwargs, polling options, and whether the input is dir mode.
910+
tuple[dict[str, Any], nextmv.PollingOptions, bool, str]
911+
A tuple of run kwargs, polling options, whether the input is dir mode, and
912+
the ID of the instance to run.
912913
"""
913914

914915
app_run_name, app_run_description = Runner.__determine_sub_run_info(app_step.app_id, node.id)
@@ -921,6 +922,7 @@ def __prepare_app_run_args(
921922
raise Exception(f"App steps cannot have more than one predecessor, but {node.id} has {len(inputs)}")
922923

923924
run_configuration = app_step.run_configuration
925+
instance_id = app_step.instance_id
924926

925927
if isinstance(inputs[0], schema.AppRunConfig):
926928
app_run_config: schema.AppRunConfig = inputs[0]
@@ -934,6 +936,9 @@ def __prepare_app_run_args(
934936
# An AppRunConfig run configuration replaces the decorator one as a whole.
935937
if app_run_config.run_configuration is not None:
936938
run_configuration = app_run_config.run_configuration
939+
# Same for the instance to run on.
940+
if app_run_config.instance_id:
941+
instance_id = app_run_config.instance_id
937942
elif isinstance(inputs[0], nextmv.RunResult):
938943
run_result: nextmv.RunResult = inputs[0]
939944
input_data = run_result.output
@@ -961,7 +966,7 @@ def __prepare_app_run_args(
961966
if run_configuration is not None:
962967
run_kwargs["configuration"] = run_configuration
963968

964-
return run_kwargs, polling_options, is_dir_mode
969+
return run_kwargs, polling_options, is_dir_mode, instance_id
965970

966971
@staticmethod
967972
def __execute_app_run(
@@ -974,6 +979,7 @@ def __execute_app_run(
974979
polling_options: nextmv.PollingOptions,
975980
is_dir_mode: bool,
976981
temp_dir: str,
982+
instance_id: str,
977983
) -> nextmv.RunResult:
978984
"""
979985
Execute an app run or return bypass output wrapped as a run result.
@@ -1005,7 +1011,7 @@ def __execute_app_run(
10051011
user_email="unavailable",
10061012
metadata=nextmv.Metadata(
10071013
application_id=app_step.app_id or "unavailable",
1008-
application_instance_id=app_step.instance_id or "unavailable",
1014+
application_instance_id=instance_id or "unavailable",
10091015
application_version_id="unavailable",
10101016
created_at=datetime.datetime.now(),
10111017
duration=0.0,
@@ -1072,15 +1078,17 @@ def __run_step(
10721078
if node.parent.definition.is_app():
10731079
app_step: decorators.App = node.parent.definition.app
10741080

1075-
run_kwargs, polling_options, is_dir_mode = Runner.__prepare_app_run_args(node, inputs, app_step)
1081+
run_kwargs, polling_options, is_dir_mode, instance_id = Runner.__prepare_app_run_args(
1082+
node, inputs, app_step
1083+
)
10761084

10771085
# Prepare the application itself.
10781086
app = Application(
10791087
client=client,
10801088
id=app_step.app_id,
10811089
)
1082-
if app_step.instance_id is not None and app_step.instance_id != "":
1083-
app.default_instance_id = app_step.instance_id
1090+
if instance_id is not None and instance_id != "":
1091+
app.default_instance_id = instance_id
10841092

10851093
# We always supply an output directory path in case of implicit multi-file output
10861094
temp_dir = tempfile.mkdtemp(prefix="nextpipe_output_")
@@ -1097,6 +1105,7 @@ def __run_step(
10971105
polling_options=polling_options,
10981106
is_dir_mode=is_dir_mode,
10991107
temp_dir=temp_dir,
1108+
instance_id=instance_id,
11001109
)
11011110

11021111
finally: # Make sure we clean up temp dir on failure too

‎nextpipe/schema.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ class AppRunConfig:
106106
replaces the `run_configuration` of the `app` decorator for this run. Note that
107107
it replaces it as a whole, i.e., no fields are inherited from the decorator's
108108
configuration.
109+
instance_id : str, optional
110+
The ID of the instance to run, by default None. If given, it replaces the
111+
`instance_id` of the `app` decorator for this run.
109112
110113
Examples
111114
--------
@@ -116,14 +119,15 @@ class AppRunConfig:
116119
... name="my-run"
117120
... )
118121
119-
The run configuration can be chosen per run, e.g., to pick an execution class
120-
based on the size of the scenario at hand.
122+
The run configuration and instance can be chosen per run, e.g., to pick an
123+
execution class based on the size of the scenario at hand.
121124
122125
>>> import nextmv
123126
>>> from nextpipe import AppRunConfig
124127
>>> config = AppRunConfig(
125128
... input={"data": [1, 2, 3]},
126129
... run_configuration=nextmv.RunConfiguration(execution_class="8c16gb12h"),
130+
... instance_id="tuned-for-large",
127131
... )
128132
"""
129133

@@ -146,6 +150,9 @@ class AppRunConfig:
146150
)
147151
"""The configuration to apply when running the app. Replaces the `run_configuration`
148152
of the `app` decorator as a whole, if given."""
153+
instance_id: str | None = None
154+
"""The ID of the instance to run. Replaces the `instance_id` of the `app` decorator,
155+
if given."""
149156

150157
def get_options(self) -> dict[str, Any]:
151158
"""

‎tests/test_flow.py‎

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,25 @@ def __init__(self, id: str = "solve_0"):
2020
self.cancel = False
2121

2222

23-
def _prepare(inputs: list[object], app_step: decorators.App) -> dict:
24-
"""Prepare the run kwargs for the given inputs and app step."""
23+
def _prepare(inputs: list[object], app_step: decorators.App) -> tuple[dict, str]:
24+
"""Prepare the run kwargs and instance ID for the given inputs and app step."""
2525

26-
run_kwargs, _, _ = Runner._Runner__prepare_app_run_args(_FakeNode(), inputs, app_step)
27-
return run_kwargs
26+
run_kwargs, _, _, instance_id = Runner._Runner__prepare_app_run_args(_FakeNode(), inputs, app_step)
27+
return run_kwargs, instance_id
2828

2929

3030
class TestPrepareAppRunArgs(unittest.TestCase):
3131
def test_no_run_configuration(self):
3232
app_step = decorators.App(app_id="echo")
33-
run_kwargs = _prepare([{"data": [1, 2, 3]}], app_step)
33+
run_kwargs, _ = _prepare([{"data": [1, 2, 3]}], app_step)
3434
self.assertNotIn("configuration", run_kwargs)
3535

3636
def test_decorator_run_configuration(self):
3737
app_step = decorators.App(
3838
app_id="echo",
3939
run_configuration=nextmv.RunConfiguration(execution_class="4c8gb12h"),
4040
)
41-
run_kwargs = _prepare([{"data": [1, 2, 3]}], app_step)
41+
run_kwargs, _ = _prepare([{"data": [1, 2, 3]}], app_step)
4242
self.assertEqual(run_kwargs["configuration"].execution_class, "4c8gb12h")
4343

4444
def test_app_run_config_run_configuration(self):
@@ -47,7 +47,7 @@ def test_app_run_config_run_configuration(self):
4747
input={"data": [1, 2, 3]},
4848
run_configuration=nextmv.RunConfiguration(execution_class="8c16gb12h"),
4949
)
50-
run_kwargs = _prepare([app_run_config], app_step)
50+
run_kwargs, _ = _prepare([app_run_config], app_step)
5151
self.assertEqual(run_kwargs["configuration"].execution_class, "8c16gb12h")
5252

5353
def test_app_run_config_run_configuration_replaces_decorator(self):
@@ -64,7 +64,7 @@ def test_app_run_config_run_configuration_replaces_decorator(self):
6464
input={"data": [1, 2, 3]},
6565
run_configuration=nextmv.RunConfiguration(execution_class="8c16gb12h"),
6666
)
67-
run_kwargs = _prepare([app_run_config], app_step)
67+
run_kwargs, _ = _prepare([app_run_config], app_step)
6868
self.assertEqual(run_kwargs["configuration"].execution_class, "8c16gb12h")
6969
self.assertIsNone(run_kwargs["configuration"].secrets_collection_id)
7070

@@ -74,9 +74,31 @@ def test_app_run_config_without_run_configuration_keeps_decorator(self):
7474
run_configuration=nextmv.RunConfiguration(execution_class="4c8gb12h"),
7575
)
7676
app_run_config = AppRunConfig(input={"data": [1, 2, 3]})
77-
run_kwargs = _prepare([app_run_config], app_step)
77+
run_kwargs, _ = _prepare([app_run_config], app_step)
7878
self.assertEqual(run_kwargs["configuration"].execution_class, "4c8gb12h")
7979

80+
def test_no_instance_id(self):
81+
app_step = decorators.App(app_id="echo")
82+
_, instance_id = _prepare([{"data": [1, 2, 3]}], app_step)
83+
self.assertEqual(instance_id, "")
84+
85+
def test_decorator_instance_id(self):
86+
app_step = decorators.App(app_id="echo", instance_id="decorator-instance")
87+
_, instance_id = _prepare([{"data": [1, 2, 3]}], app_step)
88+
self.assertEqual(instance_id, "decorator-instance")
89+
90+
def test_app_run_config_instance_id_replaces_decorator(self):
91+
app_step = decorators.App(app_id="echo", instance_id="decorator-instance")
92+
app_run_config = AppRunConfig(input={"data": [1, 2, 3]}, instance_id="per-run-instance")
93+
_, instance_id = _prepare([app_run_config], app_step)
94+
self.assertEqual(instance_id, "per-run-instance")
95+
96+
def test_app_run_config_without_instance_id_keeps_decorator(self):
97+
app_step = decorators.App(app_id="echo", instance_id="decorator-instance")
98+
app_run_config = AppRunConfig(input={"data": [1, 2, 3]})
99+
_, instance_id = _prepare([app_run_config], app_step)
100+
self.assertEqual(instance_id, "decorator-instance")
101+
80102

81103
if __name__ == "__main__":
82104
unittest.main()

‎tests/test_schema.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,13 @@ def test_run_configuration_json_round_trip_none(self):
5757
config = AppRunConfig(input={"data": [1, 2, 3]})
5858
restored = AppRunConfig.from_json(config.to_json())
5959
self.assertIsNone(restored.run_configuration)
60+
61+
def test_instance_id_default(self):
62+
config = AppRunConfig(input={"data": [1, 2, 3]})
63+
self.assertIsNone(config.instance_id)
64+
65+
def test_instance_id(self):
66+
config = AppRunConfig(input={"data": [1, 2, 3]}, instance_id="tuned-for-large")
67+
self.assertEqual(config.instance_id, "tuned-for-large")
68+
restored = AppRunConfig.from_json(config.to_json())
69+
self.assertEqual(restored.instance_id, "tuned-for-large")

0 commit comments

Comments
 (0)