# Action Lifecycle

Each action goes through these phases in order when executed:

```
┌─ PrePerform() ─────────────────────────────────────────────────────┐
│  Called once when dequeued. Validate conditions, override Target.  │
│  Return false → skip this entire goal stack.                       │
└────────────────────────────────────────────────────────────────────┘
          │ (behavior starts navigating to Target)
          ▼
┌─ Perform() ────────────────────────────────────────────────────────┐
│  Called every frame while the action is running.                   │
│  Use for animations, continuous checks, etc.                       │
└────────────────────────────────────────────────────────────────────┘
          │ (behavior arrives within _targetReachDistance)
          ▼
┌─ PerformOnTargetReached() ─────────────────────────────────────────┐
│  Called exactly once on arrival. Trigger animations, sounds, etc.  │
└────────────────────────────────────────────────────────────────────┘
          │ (timer counts down)
          ▼
┌─ PostPerform() ────────────────────────────────────────────────────┐
│  Called when timer expires.                                        │
│  Return true  → complete (effects applied, next action starts).    │
│  Return false → retry (timer resets, waits for condition).         │
└────────────────────────────────────────────────────────────────────┘
```

If the plan is cancelled mid-execution (goal is interruptible and state changed):

```
┌─ OnInterrupt() ────────────────────────────────────────────────────┐
│  Called instead of PostPerform. Release resources, cancel anims.   │
│  Base implementation automatically releases any locked TargetBinder│
│  — call base.OnInterrupt() to keep this behaviour when overriding. │
└────────────────────────────────────────────────────────────────────┘
```

***

## AIBehavior Update Loop

`AIBehavior.Update()` runs these phases in order each frame:

* **Phase 0 — Deferred re-plan** — if a relevant state change was flagged since last frame, call `Plan()` now; coalesces multiple same-frame state changes into a single plan call
* **Phase 1 — Execute current action** — if an action is running, tick `Perform()`, manage the timer, call `PostPerform()`, and clear target-reached flag
* **Phase 1.25 — Consume async plan result** — if a background planning task completed, apply the plan queue on success (releasing binders for actions not in the plan), or skip the goal and immediately retry the next goal on failure
* **Phase 2 — Start next action** — dequeue the next action, re-validate its requirements against the current state (world may have changed since planning), call `PrePerform()`
* **Phase 3 — Request new plan** — no action running and no plan queued, so call `Plan()`

Re-validation in Phase 2 means an action whose requirements were valid at plan time but are stale by execution time will cause an immediate re-plan rather than silently misfiring.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://beelabs-dev.gitbook.io/beelabs-docs/goap-engine/action-lifecycle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
