Repository navigation
Add requirements and a FAILED state to Action - #3801
Conversation
Three tests at start and three at completion drove identical setups and asserted one fact each. Merge each triple into the one scenario it exercises and regroup the rest into TestRequirements and TestActionFailure, matching how the surrounding file bundles related assertions. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Performance benchmarks:
|
I am not sure about this and I am not convinced by the grazing example. Typically, you would claim the resource at the start of the action, rather than check at the end if the resource is still available. So, if you arive at a station and there is not enough resource remaining, the action would be to claim just the remaining resource, move to another station, or wait until enough of the resource is again available. Also, requirements at start need not be the same as requirements on completion. Can this be handled? |
|
Sorry, I missed it yesterday and apologies for the wrong example. Your three responses, take what remains, go elsewhere, or wait, all need to know how much is there. A requirement returns True or False, so it cannot express "ten were wanted, three are left, take the three". In writing that example I presented What a boolean gate is good for is preconditions that are genuinely binary, where the only sensible response is not to start at all: the agent holds a permit, the vehicle has a driver, the door is unlocked. Nothing to claim, no quantity to negotiate. On your second question, yes: Action(agent, requirements=[...], completion_requirements=[...])Independent lists rather than one list checked twice, with the completion list empty by default so nothing is checked at the end unless a model asks for it. I will replace the example with a claim-based one either way. It makes the boundary obvious, and the current one teaches the wrong pattern. |
|
If we separate on_start requirements and on_completion requirements, it leaves it to the model developer to decide and gives them also a lot more flexibility. |
|
Yes and using |
Separating the two, as quaquel suggested, leaves the choice to the model developer instead of checking one list at both ends. requirements gates start(); completion_requirements gates the effect and is empty by default, so nothing is checked at completion unless a model asks for it. That makes claim-at-start the default path. Replace the contested-grass example, which taught re-checking a rival resource at the end, with one that claims the serving in on_start and decides what to do next from on_fail. Add a market-hours test for the case the completion list exists for: a condition nobody can reserve, where the start check answers the wrong question. Co-Authored-By: Claude Opus 5 <[email protected]>
quaquel
left a comment
There was a problem hiding this comment.
I have a few minor points at the code level.
Also, we now have start and completion requirements. But what about an interrupt? How would that work?
|
Renamed, they are On interrupt:
For eg. A technician on a eight hour shift. The technician has to be on shift to work on it, and the overhaul only counts if the machine is still in service when it lands: class Repair(Action):
def __init__(self, tech, machine):
super().__init__(
tech,
duration=8.0,
start_requirements=lambda a: a.on_shift,
completion_requirements=lambda a: machine.in_service,
)An emergency callout preempts it three hours in, and runs an hour past the end of the shift: And separately, the same overhaul running undisturbed while the machine is written off on the bench: One consequence worth noting from that first trace. The 38% is lost: Separately, I also have had a question. |
|
Thanks for working on this. Interesting stuff. Actions do get more powerful but also more complicated this way. A documentation/tutorial effort might be useful. |
|
Right, and that is indeed point 5 of #3798. I am a bit preoccupied currently but I will land it by the end of this week. |
Summary
Actiongains two requirement lists and a failure path.requirementsgates the start,completion_requirementsgates the effect, and a predicate that does not hold moves the action toActionState.FAILEDand fireson_fail()instead of letting it complete.Motive
An action can only succeed today.
Forageschedules its completion the moment it starts, and five time units lateron_complete()fires and the sheep gains energy whether or not the conditions still hold. The workaround is to re-check insideon_complete()and return early, which repeats the same guard in every action and still leaves the actionCOMPLETEDwith nothing accomplished.Declaring the condition moves the check into the framework and gives the failure its own state, so a model can tell "ate" from "got there and could not".
Implementation
Both lists accept a single
callable(agent) -> boolor an iterable of them, mirroring the waydurationandpriorityalready accept callables resolved against the agent, and each is normalised into a list the action owns. Subclasses can assign to either aftersuper().__init__()instead of passing them in.requirementsis checked atstart(), before duration and priority are resolved, so a failing action never fireson_start()oron_resume()and never schedules a completion event.completion_requirementsis checked in_do_complete()before the effect is applied, and is empty by default. Nothing is checked at completion unless a model asks for it, which makes claim-at-start the default path.They are separate lists rather than one list checked twice because the two questions are different, and separating them leaves the choice to the model developer. A rival resource should be claimed at the start, and the decisions that follow, take what remains, go elsewhere, or wait, belong to the agent rather than to a boolean gate. What the completion list is for is conditions nobody can reserve: a market still open when a trade lands, a counterparty still solvent at settlement.
Neither list is re-checked in between. Continuous revalidation would mean rescanning every active action whenever the world changes, which is activity scanning by another name. An action that must abort the moment a condition breaks belongs to a
Threshold(#3766) callingagent.cancel_action().Failure moves the action to
FAILEDand fireson_fail()in place ofon_start()oron_complete()._fail()clearsagent.current_actionbefore firing, so the agent is already idle when the hook runs and can start something else from inside it. On a completion failureprogressstill reads1.0, because the full duration did elapse and only the effect is withheld.interrupt_for()changes behaviour. It returnedTrueunconditionally once the interruption succeeded; it now returnsnot new_action.has_failed. It deliberately does not roll the interruption back: the old action is alreadyINTERRUPTEDand the agent is left idle, since whether to resume it is the model's decision. The twoFalsecases therefore differ in what they leave behind, a refused interruption changes nothing and a failed requirement does not.Usage Examples
Claiming a rival resource at the start, which is where contention belongs:
Two sheep starting on a patch with one serving: the first claims it and grazes unimpeded, the second never starts and decides what to do next from
on_fail().Gating an effect on something nobody can reserve:
A trade that starts at 15:58 and lands at 16:03 ends
FAILED. Checking at the start would have reported the market open, which is true and useless, and there is no claim to take at 15:58 that would keep it open.Additional Notes
A requirement that fails on resume is terminal. An action interrupted at 40% whose requirement then breaks moves to
FAILED, keeps its 0.4 progress, and cannot be restarted even once the condition is repaired; the model has to build a fresh action and lose the partial work. The alternative, sending a failed resume back toINTERRUPTEDso it stays resumable, was rejected becausestart()would then have to acceptFAILED, andresuming = self.state is ActionState.INTERRUPTEDwould misclassify it, callingon_start()instead ofon_resume()and re-resolving duration. If transient requirements turn out to be the common case, this is the piece to revisit.Requirements are callables, so a lambda makes a model unpicklable. That is not new,
duration=lambda a: ...already has the same effect; named functions and bound methods pickle fine.The third item of section 2 in #3798, giving
priorityan effect, is not here. It is #3805, which addsAgent.should_interrupt(current, incoming)and rewrites the same three lines ofinterrupt_for().