Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

allow optional inputs on factory entry states

Sebastian Müllere08c542f45e2
diff --git a/.changes/entry-inputs.md b/.changes/entry-inputs.md
new file mode 100644
index 0000000000..7cfc5c8023
--- /dev/null
+++ b/.changes/entry-inputs.md
@@ -0,0 +1 @@
+- Factory machines can loop back into an entry state: an entry state may now declare inputs as long as all of them are optional. Its first entry binds the null sentinel, and a later entry through a transition binds the real output, so an investigate/verify loop no longer needs a duplicate re-investigate state. A required input on an entry state is still rejected, with a message saying it must be optional.
diff --git a/prime-agent-runtime/src/rlm/factory.py b/prime-agent-runtime/src/rlm/factory.py
index 6afe16a7c4..85e41c404d 100644
--- a/prime-agent-runtime/src/rlm/factory.py
+++ b/prime-agent-runtime/src/rlm/factory.py
@@ -1,8 +1,8 @@
"""Validation and compilation for factory specifications.
A continual-harness ``factory`` entry stores a declarative state machine of
-subagent states in ``arguments["machine"]``: entry states (which declare
-no inputs), guarded transitions between states, and bounded re-entry
+subagent states in ``arguments["machine"]``: entry states (whose inputs
+are all optional), guarded transitions between states, and bounded re-entry
(``max_entries``). The original DAG form in ``arguments["dag"]`` stays as
sugar: it compiles to machine form (each node becomes a state entered
once; a node's full effective dependency set compiles to ONE join
@@ -492,7 +492,7 @@ def validate_factory_machine(machine: Any) -> list[str]:
Returns a list of human-readable error sentences; an empty list means
the machine is valid. Rules: states are 1..1024 with unique slug ids and
at least one entry state; every state requires a subagent and entry
- states declare no inputs; resident states declare no outputs, foreach,
+ states declare only optional inputs; resident states declare no outputs, foreach,
retries, or outgoing transitions; wait blocks are rejected (the watch host
handlers arrive with the communication series); transitions reference
existing states (self-loops are legal re-entry) and may carry one guard
@@ -597,17 +597,22 @@ def validate_factory_machine(machine: Any) -> list[str]:
_is_int(state.get("max_entries")) and state.get("max_entries") >= STATE_MAX_ENTRIES_DEFAULT
):
errors.append(f"state {state_id} max_entries must be an integer >= {STATE_MAX_ENTRIES_DEFAULT}")
- if state.get("entry") is True and _port_list(state, "inputs"):
- errors.append(f"entry state {state_id} cannot declare inputs")
- # A REQUIRED self-input can never bind: the state's first entry needs
- # its own prior settle, and no settle exists before an entry settles.
- # Optional self-inputs are the loop form (the first entry binds the
- # null sentinel, re-entries re-bind the previous settle), so only the
- # required variant is rejected -- the machine-form mirror of the dag
- # compiler's "node b cannot depend on itself" rule.
+ # A REQUIRED input can never bind on an entry state's first entry
+ # (admission has no upstream settle) nor on a self-input's first
+ # entry (no settle exists before an entry settles). Optional inputs
+ # are the loop form (the first entry binds the null sentinel, a
+ # later entry binds the real settle), so only the required variants
+ # are rejected -- the self-input check is the machine-form mirror of
+ # the dag compiler's "node b cannot depend on itself" rule.
for inp in _port_list(state, "inputs"):
if not isinstance(inp, dict) or inp.get("optional"):
continue
+ if state.get("entry") is True:
+ errors.append(
+ f"entry state {state_id} input {inp.get('name')!r} must be optional: "
+ "an entry state's first entry has no upstream settle to bind"
+ )
+ continue
source = inp.get("from")
if isinstance(source, str) and "." in source and source.partition(".")[0] == state_id:
errors.append(
@@ -5005,8 +5010,10 @@ the cap, fails to capture, and fails the producing state through its
## Authoring reference
- **States**: 1 to 1024, unique ids matching `^[a-z0-9][a-z0-9-]{0,63}$`; at
- least one state carries `"entry": true`, and entry states declare no
- inputs. A state's `subagent` is a harness subagent entry id or title (its
+ least one state carries `"entry": true`. An entry state's inputs must all
+ be optional: its first entry binds the null sentinel, and a later entry
+ through a transition (a verify loop back into it) binds the real
+ settle. A state's `subagent` is a harness subagent entry id or title (its
content is the prompt template; `metadata.model`/`metadata.thinking` are
spawn settings) or an inline `{"prompt": ...}` object with optional
`name`/`model`/`thinking`. The optional `name` labels the spawned
diff --git a/prime-agent-runtime/test/test_factory.py b/prime-agent-runtime/test/test_factory.py
index d05fcf755a..9a565ac75d 100644
--- a/prime-agent-runtime/test/test_factory.py
+++ b/prime-agent-runtime/test/test_factory.py
@@ -1173,18 +1173,28 @@ class ValidateFactoryMachineTest(unittest.TestCase):
repr(required),
)
- def test_entry_states_cannot_declare_inputs(self) -> None:
- with_inputs = {
- "states": [
- state("src", entry=True, inputs=[{"name": "i", "type": "text", "from": "peer.o"}]),
- state("peer", outputs=[{"name": "o", "type": "text"}]),
- ],
- "transitions": [{"from": "peer", "to": "src"}],
- }
- self.assertEqual(
- validate_factory_machine(with_inputs),
- ["entry state src cannot declare inputs"],
- )
+ def test_entry_state_inputs_must_be_optional(self) -> None:
+ absent = object() # sentinel: the optional key is omitted entirely
+
+ def with_inputs(optional: Any) -> dict[str, Any]:
+ inp: dict[str, Any] = {"name": "i", "type": "text", "from": "peer.o"}
+ if optional is not absent:
+ inp["optional"] = optional
+ return {
+ "states": [
+ state("src", entry=True, inputs=[inp]),
+ state("peer", outputs=[{"name": "o", "type": "text"}]),
+ ],
+ "transitions": [{"from": "src", "to": "peer"}, {"from": "peer", "to": "src"}],
+ }
+
+ self.assertEqual(validate_factory_machine(with_inputs(True)), [])
+ for required in (False, absent):
+ self.assertEqual(
+ validate_factory_machine(with_inputs(required)),
+ ["entry state src input 'i' must be optional: an entry state's first entry has no upstream settle to bind"],
+ repr(required),
+ )
entry_without_inputs = {"states": [state("a", entry=True)], "transitions": []}
self.assertEqual(validate_factory_machine(entry_without_inputs), [])
# Compiled dags only mark dep-free nodes as entry states, so the rule
@@ -5827,6 +5837,60 @@ class FactoryExecutorTest(_ExecutorTestCase):
self.assertEqual(self.state_report(status, "fixer")["entries_used"], 1)
self.assertEqual(status["usage"]["transitions_fired"], 3)
+ @async_test
+ async def test_machine_loop_back_into_entry_state_binds_null_then_the_real_settle(self) -> None:
+ # investigate -> check -> investigate: the entry state's optional
+ # input binds null on admission and the checker's objections on the
+ # loop-back entry.
+ self.host.child_outcomes["child-1"] = {"status": "done", "answer": "FINDINGS-1"}
+ self.host.child_outcomes["child-2"] = {
+ "status": "done",
+ "answer": '```json\n{"verdict": {"approved": false, "objections": ["OBJ-A1"]}}\n```',
+ }
+ self.host.child_outcomes["child-3"] = {"status": "done", "answer": "FINDINGS-2"}
+ self.host.child_outcomes["child-4"] = {
+ "status": "done",
+ "answer": '```json\n{"verdict": {"approved": true, "objections": []}}\n```',
+ }
+ self.store_machine(
+ {
+ "states": [
+ {
+ "id": "investigate",
+ "entry": True,
+ "subagent": "worker",
+ "inputs": [{"name": "verdict", "type": "json", "from": "check.verdict", "optional": True}],
+ "outputs": [{"name": "findings", "type": "text"}],
+ "max_entries": 3,
+ },
+ {
+ "id": "check",
+ "subagent": "worker",
+ "inputs": [{"name": "findings", "type": "text", "from": "investigate.findings"}],
+ "outputs": [{"name": "verdict", "type": "json"}],
+ "max_entries": 3,
+ },
+ ],
+ "transitions": [
+ {"from": "investigate", "to": "check"},
+ {
+ "from": "check",
+ "to": "investigate",
+ "when": {"output": "verdict", "path": "approved", "op": "eq", "value": False},
+ },
+ ],
+ }
+ )
+ result = await self.start()
+ status = await self.settle(result)
+ self.assertEqual(status["state"], "done")
+ prompts = [call["prompt"] for call in self.host.spawn_calls("investigate")]
+ self.assertEqual(len(prompts), 2)
+ self.assertIn("null", prompts[0])
+ self.assertNotIn("OBJ-A1", prompts[0])
+ self.assertIn("OBJ-A1", prompts[1])
+ self.assertEqual(self.state_report(status, "check")["entries_used"], 2)
+
@async_test
async def test_machine_optional_foreach_over_expands_empty_then_the_real_settle(self) -> None:
# Review finding (PR #3199): marking the foreach.over input