Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/development.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- knowledge
last_checked: "2026-11-26T01:14:08Z"
last_checked: "2026-11-27T03:17:49Z"
-->
# Development navigation

Expand Down Expand Up @@ -85,6 +85,11 @@ public surface.
into [HumanInputNode](../src/graphon/nodes/human_input/human_input_node.py).
The host returns a pause, completed, or expired decision. See
[human-input tests](../tests/nodes/human_input/) for the event contract.
- For host-managed work, a custom node can yield `PauseRequestedEvent` with an
`ExternalWaitReason`. Pause events and runtime snapshots preserve the wait
ID and the identity of the frame-local node execution. See the
[pause reason model](../src/graphon/entities/pause_reason.py) and
[round-trip tests](../tests/runtime/test_external_wait.py).

## Validate the behavior you changed

Expand Down
20 changes: 19 additions & 1 deletion src/graphon/entities/pause_reason.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PauseReasonType(StrEnum):

HITL_REQUIRED = auto()
SCHEDULED_PAUSE = auto()
EXTERNAL_WAIT = auto()


class HitlRequired(BaseModel):
Expand All @@ -31,6 +32,23 @@ class SchedulingPause(BaseModel):
message: str


class ExternalWaitReason(BaseModel):
"""Identify a node execution waiting for host-managed work.

The host stores the work and its result and resumes the same frame-local
Comment thread
zyssyz123 marked this conversation as resolved.
Resolved
execution when the result is ready.
"""

model_config = ConfigDict(extra="forbid", frozen=True)

TYPE: Literal[PauseReasonType.EXTERNAL_WAIT] = PauseReasonType.EXTERNAL_WAIT
wait_id: str = Field(min_length=1)
node_id: str = Field(min_length=1)
node_title: str
execution_id: str = Field(min_length=1)
frame_id: str = Field(min_length=1)


class _LegacyHumanInputRequired(BaseModel):
"""
This model serves as documentation purpose. DO NOT USE.
Expand Down Expand Up @@ -71,7 +89,7 @@ def _decode_pause_reason(value: object) -> object:


type PauseReason = Annotated[
HitlRequired | SchedulingPause,
HitlRequired | SchedulingPause | ExternalWaitReason,
Field(discriminator="TYPE"),
BeforeValidator(_decode_pause_reason),
]
86 changes: 86 additions & 0 deletions tests/runtime/test_external_wait.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""Check host pause identities across engine events and runtime snapshots."""

import pytest
from pydantic import ValidationError

from graphon.engine_events.graph import GraphRunPausedEvent
from graphon.engine_events.node import NodeRunPauseRequestedEvent
from graphon.node_events.node import PauseRequestedEvent
from graphon.runtime import RuntimeState, VariablePool


def make_external_wait_payload(frame_id: str, execution_id: str) -> dict[str, str]:
return {
"TYPE": "external_wait",
"wait_id": f"external-job:{frame_id}",
"node_id": "worker",
"node_title": "External worker",
"execution_id": execution_id,
"frame_id": frame_id,
}


def test_external_wait_identity_survives_events_and_snapshot() -> None:
state = RuntimeState(
workflow_id="workflow", variable_pool=VariablePool(), start_at=0
)
expected_payloads = []
for frame_id in ("iteration:0", "iteration:1"):
execution = state.graph_execution.get_or_create_node_execution(
frame_id=frame_id, node_id="worker"
)
reason_payload = make_external_wait_payload(frame_id, execution.execution_id)
pause_request = PauseRequestedEvent.model_validate({"reason": reason_payload})
node_pause = NodeRunPauseRequestedEvent(
id=execution.execution_id,
node_id=reason_payload["node_id"],
node_type="worker",
frame_id=frame_id,
reason=pause_request.reason,
)
restored_node_pause = NodeRunPauseRequestedEvent.model_validate_json(
node_pause.model_dump_json()
)
assert restored_node_pause.reason.model_dump(mode="json") == reason_payload
state.graph_execution.pause(restored_node_pause.reason)
expected_payloads.append(reason_payload)

graph_pause = GraphRunPausedEvent(reasons=state.graph_execution.pause_reasons)
restored_graph_pause = GraphRunPausedEvent.model_validate_json(
graph_pause.model_dump_json()
)
assert restored_graph_pause.model_dump(mode="json")["reasons"] == expected_payloads

restored_state = RuntimeState.from_snapshot(state.dumps())
assert restored_state.graph_execution.paused
assert [
reason.model_dump(mode="json")
for reason in restored_state.graph_execution.pause_reasons
] == expected_payloads
for reason_payload in expected_payloads:
execution = restored_state.graph_execution.get_or_create_node_execution(
frame_id=reason_payload["frame_id"], node_id=reason_payload["node_id"]
)
assert execution.execution_id == reason_payload["execution_id"]


@pytest.mark.parametrize(
("field", "value", "error_type"),
[
("wait_id", "", "string_too_short"),
("node_id", "", "string_too_short"),
("execution_id", "", "string_too_short"),
("frame_id", "", "string_too_short"),
("deadline", "host-owned", "extra_forbidden"),
],
)
def test_external_wait_rejects_invalid_fields(
field: str, value: str, error_type: str
) -> None:
reason_payload = make_external_wait_payload("root", "execution") | {field: value}
with pytest.raises(ValidationError) as error:
PauseRequestedEvent.model_validate({"reason": reason_payload})
assert any(
item["loc"][-1] == field and item["type"] == error_type
for item in error.value.errors()
)
Loading