Skip to content

laife.llm.mission

Mission for the player to complete.

Classes:

Attributes:

MAX_MISSION_FAILURES module-attribute

MAX_MISSION_FAILURES: int = 5

Number of consecutive build/craft failures before a mission is marked FAILED.

Mission

Bases: BaseModel

A mission composed of ordered mission steps.

Methods:

active_focus

active_focus() -> Mission

Return the deepest active sub-mission, or self if none.

Source code in src/laife/llm/mission.py
def active_focus(self) -> Mission:
    """Return the deepest active sub-mission, or self if none."""
    for step in self.steps:
        if step.status == MissionStatus.ACTIVE:
            return step.active_focus()
    return self

add_sub_mission

add_sub_mission(objective: str) -> None

Add a sub-mission to the mission.

Source code in src/laife/llm/mission.py
def add_sub_mission(self, objective: str) -> None:
    """Add a sub-mission to the mission."""
    sm = Mission(
        objective=objective,
        sub_mission_level=self.sub_mission_level + 1,
        parent_mission=self,
    )
    self.steps.append(sm)

advance

advance() -> bool

Activate the next pending step.

Returns True if a new step was activated, False if all steps are done. When all steps are done, marks self as COMPLETED and propagates upward.

Source code in src/laife/llm/mission.py
def advance(self) -> bool:
    """Activate the next pending step.

    Returns True if a new step was activated, False if all steps are done.
    When all steps are done, marks self as COMPLETED and propagates upward.
    """
    for step in self.steps:
        if step.status == MissionStatus.PENDING:
            step.status = MissionStatus.ACTIVE
            return True
    # All steps accounted for - mark self completed and ask parent to advance
    self.status = MissionStatus.COMPLETED
    if self.parent_mission is not None:
        self.parent_mission.advance()
    return False

is_terminal

is_terminal() -> bool

Return True when the mission has reached a final state (COMPLETED or FAILED).

Source code in src/laife/llm/mission.py
def is_terminal(self) -> bool:
    """Return True when the mission has reached a final state (COMPLETED or FAILED)."""
    return self.status in (MissionStatus.COMPLETED, MissionStatus.FAILED)

record_action_failure

record_action_failure() -> None

Record a failed build or craft action.

Increments the consecutive failure counter. When the counter reaches max_failures the mission is marked FAILED.

Source code in src/laife/llm/mission.py
def record_action_failure(self) -> None:
    """Record a failed build or craft action.

    Increments the consecutive failure counter.  When the counter reaches
    ``max_failures`` the mission is marked ``FAILED``.
    """
    self.consecutive_failures += 1
    if self.consecutive_failures >= self.max_failures:
        self.status = MissionStatus.FAILED

record_action_success

record_action_success() -> None

Record a successful build or craft action and mark the mission COMPLETED.

Source code in src/laife/llm/mission.py
def record_action_success(self) -> None:
    """Record a successful build or craft action and mark the mission COMPLETED."""
    self.consecutive_failures = 0
    self.status = MissionStatus.COMPLETED

to_prompt

to_prompt(*, top_prompt: bool = True, focus: Mission | None = None) -> str

Return the mission as a prompt.

Steps that match focus are prefixed with [FOCUS] so the LLM knows exactly which sub-mission to work on.

Source code in src/laife/llm/mission.py
def to_prompt(self, *, top_prompt: bool = True, focus: Mission | None = None) -> str:
    """Return the mission as a prompt.

    Steps that match *focus* are prefixed with ``[FOCUS]`` so the LLM
    knows exactly which sub-mission to work on.
    """
    # current mission objective
    p = f"[M{self.sub_mission_level}] The mission is '{self.objective}' ({self.status.value})\n"
    # context of what we plan to do later/have done before
    for step in self.steps:
        if focus is not None and step is focus:
            p += f"[FOCUS]{step.to_prompt(top_prompt=False, focus=focus)}"
        else:
            p += step.to_prompt(top_prompt=False, focus=focus)
    # context of parent mission to ground to bigger picture
    if top_prompt:
        pm = self.parent_mission
        while pm is not None:
            p += f"[PM{pm.sub_mission_level}]: {pm.objective} ({pm.status.value})\n"
            pm = pm.parent_mission
    return p

MissionHistory

Bases: BaseModel

Collection of mission history entries.

Methods:

add_history_entry

add_history_entry(mission_history_entry: MissionHistoryEntry) -> None

Add a history entry to the mission.

Source code in src/laife/llm/mission.py
def add_history_entry(
    self,
    mission_history_entry: MissionHistoryEntry,
) -> None:
    """Add a history entry to the mission."""
    self.history.append(mission_history_entry)

to_prompt

to_prompt() -> str

Return the history as a prompt.

Source code in src/laife/llm/mission.py
def to_prompt(self) -> str:
    """Return the history as a prompt."""
    p = ""
    for entry in self.history:
        p += entry.to_prompt()
    return p

MissionHistoryEntry

Bases: BaseModel

History entry of a mission.

Methods:

  • to_prompt

    Return the history entry as a prompt.

to_prompt

to_prompt() -> str

Return the history entry as a prompt.

Source code in src/laife/llm/mission.py
def to_prompt(self) -> str:
    """Return the history entry as a prompt."""
    return f"You tried to <{self.action}> and the result was {self.result}\n"

MissionStatus

Bases: Enum

Status of the mission.