-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: pre-PR review improvements for ExecutionLoop + ReAct loop (#124) #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| """Execution loop protocol and supporting models. | ||
|
|
||
| Defines the ``ExecutionLoop`` protocol that the agent engine calls to | ||
| run a task, along with ``ExecutionResult``, ``TurnRecord``, | ||
| ``TerminationReason``, and the ``BudgetChecker`` type alias. | ||
| """ | ||
|
|
||
| from collections.abc import Callable | ||
| from enum import StrEnum | ||
| from typing import TYPE_CHECKING, Any, Protocol, Self, runtime_checkable | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, Field, computed_field, model_validator | ||
|
|
||
| from ai_company.core.types import NotBlankStr # noqa: TC001 | ||
| from ai_company.engine.context import AgentContext | ||
| from ai_company.providers.enums import FinishReason # noqa: TC001 | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ai_company.providers.models import CompletionConfig | ||
| from ai_company.providers.protocol import CompletionProvider | ||
| from ai_company.tools.invoker import ToolInvoker | ||
|
|
||
|
|
||
| class TerminationReason(StrEnum): | ||
| """Why the execution loop terminated.""" | ||
|
|
||
| COMPLETED = "completed" | ||
| MAX_TURNS = "max_turns" | ||
| BUDGET_EXHAUSTED = "budget_exhausted" | ||
| ERROR = "error" | ||
|
|
||
|
|
||
| class TurnRecord(BaseModel): | ||
| """Per-turn metadata recorded during execution. | ||
|
|
||
| Attributes: | ||
| turn_number: 1-indexed turn number. | ||
| input_tokens: Input tokens consumed this turn. | ||
| output_tokens: Output tokens generated this turn. | ||
| total_tokens: Sum of input and output tokens (computed). | ||
| cost_usd: Cost in USD for this turn. | ||
| tool_calls_made: Names of tools invoked this turn. | ||
| finish_reason: LLM finish reason for this turn. | ||
| """ | ||
|
|
||
| model_config = ConfigDict(frozen=True) | ||
|
|
||
| turn_number: int = Field(gt=0, description="1-indexed turn number") | ||
| input_tokens: int = Field(ge=0, description="Input tokens this turn") | ||
| output_tokens: int = Field(ge=0, description="Output tokens this turn") | ||
| cost_usd: float = Field(ge=0.0, description="Cost in USD this turn") | ||
| tool_calls_made: tuple[NotBlankStr, ...] = Field( | ||
| default=(), | ||
| description="Tool names invoked this turn", | ||
| ) | ||
| finish_reason: FinishReason = Field( | ||
| description="LLM finish reason this turn", | ||
| ) | ||
|
|
||
| @computed_field(description="Total token count") # type: ignore[prop-decorator] | ||
| @property | ||
| def total_tokens(self) -> int: | ||
| """Sum of input and output tokens.""" | ||
| return self.input_tokens + self.output_tokens | ||
|
|
||
|
|
||
| class ExecutionResult(BaseModel): | ||
| """Result returned by an execution loop. | ||
|
|
||
| Attributes: | ||
| context: Final agent context after execution. | ||
| termination_reason: Why the loop stopped. | ||
| turns: Per-turn metadata records. | ||
| total_tool_calls: Total tool calls across all turns (computed). | ||
| error_message: Error description when termination_reason is ERROR. | ||
| metadata: Forward-compatible dict for future loop types. | ||
| Note: ``frozen=True`` prevents field reassignment but not | ||
| in-place mutation of the dict contents; deep-copy at | ||
| system boundaries per project conventions. | ||
| """ | ||
|
|
||
| model_config = ConfigDict(frozen=True) | ||
|
|
||
| context: AgentContext = Field(description="Final agent context") | ||
| termination_reason: TerminationReason = Field( | ||
| description="Why the loop stopped", | ||
| ) | ||
| turns: tuple[TurnRecord, ...] = Field( | ||
| default=(), | ||
| description="Per-turn metadata", | ||
| ) | ||
| error_message: str | None = Field( | ||
| default=None, | ||
| description="Error description (when reason is ERROR)", | ||
| ) | ||
| metadata: dict[str, Any] = Field( | ||
| default_factory=dict, | ||
| description="Forward-compatible metadata for future loop types", | ||
| ) | ||
|
|
||
| @computed_field( # type: ignore[prop-decorator] | ||
| description="Total tool calls across all turns", | ||
| ) | ||
| @property | ||
| def total_tool_calls(self) -> int: | ||
| """Sum of tool calls from all turn records.""" | ||
| return sum(len(t.tool_calls_made) for t in self.turns) | ||
|
|
||
| @model_validator(mode="after") | ||
| def _validate_error_message(self) -> Self: | ||
| if self.termination_reason == TerminationReason.ERROR: | ||
| if self.error_message is None: | ||
| msg = "error_message is required when termination_reason is ERROR" | ||
| raise ValueError(msg) | ||
| elif self.error_message is not None: | ||
| msg = "error_message must be None when termination_reason is not ERROR" | ||
| raise ValueError(msg) | ||
| return self | ||
|
|
||
|
|
||
| BudgetChecker = Callable[[AgentContext], bool] | ||
| """Callback that returns ``True`` when the budget is exhausted.""" | ||
|
|
||
|
|
||
| @runtime_checkable | ||
| class ExecutionLoop(Protocol): | ||
| """Protocol for agent execution loops. | ||
|
|
||
| The agent engine calls ``execute`` to run a task through the loop. | ||
| Implementations decide the control flow (ReAct, Plan-and-Execute, etc.) | ||
| but all return an ``ExecutionResult`` with a ``TerminationReason``. | ||
| """ | ||
|
|
||
| async def execute( | ||
| self, | ||
| *, | ||
| context: AgentContext, | ||
| provider: CompletionProvider, | ||
| tool_invoker: ToolInvoker | None = None, | ||
| budget_checker: BudgetChecker | None = None, | ||
| completion_config: CompletionConfig | None = None, | ||
| ) -> ExecutionResult: | ||
| """Run the execution loop. | ||
|
|
||
| Args: | ||
| context: Initial agent context with conversation and identity. | ||
| provider: LLM completion provider. | ||
| tool_invoker: Optional tool invoker for tool execution. | ||
| budget_checker: Optional callback; returns ``True`` when | ||
| budget is exhausted. | ||
| completion_config: Optional per-execution override for | ||
| temperature/max_tokens (defaults to identity's model config). | ||
|
|
||
| Returns: | ||
| Execution result with final context and termination reason. | ||
| """ | ||
| ... | ||
|
|
||
| def get_loop_type(self) -> str: | ||
| """Return the loop type identifier (e.g. ``"react"``).""" | ||
| ... |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep a first-class blocked termination state.
Issue
#124still calls out blocked termination, and later sections in this spec already distinguish parked/resumable work from outright failure. Folding those cases intoERRORloses whether the loop failed versus needs external input and can be resumed; ifBLOCKEDis intentionally deferred for M3, call that out explicitly instead of redefining the protocol here.📝 Suggested spec correction
🤖 Prompt for AI Agents