# Lifecycle

### Overview

A lifecycle defines the operational boundaries of a mode, from its activation to its completion or transition to another mode. It is responsible for initializing, managing, and safely terminating a mode's operation, ensuring predictable transitions, consistent state handling, and controlled execution of mode-specific logic.

A mode represents a functional state of the robot, such as Guard, SLAM, or Navigation, and each mode runs within a lifecycle that determines how and when it starts, executes, and ends.

![](/files/LGNPWiW14bAedV3FH7KM)

### Conceptual Model

| Concept       | Description                                                                          |
| ------------- | ------------------------------------------------------------------------------------ |
| **Lifecycle** | Defines the start, execution, and end of a mode                                      |
| **Stage**     | Represents a logical phase within the lifecycle (e.g., startup, entry, active, exit) |
| **Hook**      | A programmable event point that executes specific actions at key stages              |
| **Mode**      | The operational context governed by the lifecycle                                    |

> **In short:** Lifecycle controls the flow → Stages define phases → Hooks perform actions → Mode defines behavior.

### Stages and Hooks

Each lifecycle is composed of several stages. Hooks are executed at specific points in these stages to perform initialization, cleanup, or handling tasks.

| Stage        | Hook          | Description                                                                                   |
| ------------ | ------------- | --------------------------------------------------------------------------------------------- |
| **Startup**  | `ON_STARTUP`  | Executes system-level initialization before any mode begins                                   |
| **Entry**    | `ON_ENTRY`    | Runs when entering a mode; prepares mode-specific resources and context                       |
| **Active**   | —             | Main operational phase; mode logic runs continuously (no fixed hook)                          |
| **Exit**     | `ON_EXIT`     | Executes cleanup, saves state, and prepares for the next transition                           |
| **Timeout**  | `ON_TIMEOUT`  | Handles cases where a mode exceeds its defined duration or fails to complete expected actions |
| **Shutdown** | `ON_SHUTDOWN` | Performs final cleanup and safe termination of the lifecycle management system                |

#### Startup Stage

Triggered by `ON_STARTUP`. Executes system-level initialization before any mode begins.

#### Entry Stage

Triggered by `ON_ENTRY`. Runs when entering a mode; prepares mode-specific resources and context.

#### Active Stage

Represents the main operational phase of the mode. No fixed hook; mode logic runs continuously during this stage.

#### Exit Stage

Triggered by `ON_EXIT`. Executes cleanup, saves state, and prepares for the next transition.

#### Timeout Stage

Triggered by `ON_TIMEOUT`. Handles cases where a mode exceeds its defined duration or fails to complete expected actions.

#### Shutdown Stage

Triggered by `ON_SHUTDOWN`. Performs final cleanup and safe termination of the lifecycle management system.


---

# Agent Instructions: 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://docs.openmind.com/modes-and-lifecycle/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.
