tot_agent.agent¶
Core agentic loop, Observer pattern implementation, and Goal template builders.
Observer pattern classes¶
classDiagram
class AgentObserver {
<<abstract>>
+on_event(event: AgentEvent) None
}
class ConsoleObserver {
-_console: Console
+on_event(event: AgentEvent) None
}
class LoggingObserver {
-_log: Logger
+on_event(event: AgentEvent) None
}
AgentObserver <|-- ConsoleObserver
AgentObserver <|-- LoggingObserver
BrowserAgent --> AgentObserver : notifies
BrowserAgent --> AgentEvent : emits
Goal template hierarchy¶
classDiagram
class GoalTemplate {
<<abstract>>
+build(**kwargs) str
}
class CreateTestsGoal {
+count: int
+genre: str
+build() str
}
class VoteGoal {
+username: str
+password: str
+vote_count: int
+bias: str
+build() str
}
class SimulateAllUsersGoal {
+vote_count_each: int
+build() str
}
class FullSeedGoal {
+test_count: int
+vote_rounds: int
+build() str
}
GoalTemplate <|-- CreateTestsGoal
GoalTemplate <|-- VoteGoal
GoalTemplate <|-- SimulateAllUsersGoal
GoalTemplate <|-- FullSeedGoal
Module reference¶
tot_agent.agent
¶
agent.py — Core agentic loop and goal template builders.
Architecture — Observer pattern¶
The agent emits :class:AgentEvent objects at each significant step of its
execution. Any number of :class:AgentObserver instances can be attached to
receive these events. Two concrete observers are bundled:
- :class:
ConsoleObserver— renders events to the terminal via :mod:rich. - :class:
LoggingObserver— writes events to the standard :mod:logginghierarchy (useful for CI/CD pipelines and file-based audit trails).
This separation means the core loop never directly calls print or
console.print.
Usage::
from tot_agent.browser import BrowserManager
from tot_agent.agent import BrowserAgent, ConsoleObserver, LoggingObserver
async with BrowserManager() as bm:
agent = BrowserAgent(bm, observers=[ConsoleObserver(), LoggingObserver()])
summary = await agent.run("Log in as admin and take a screenshot.")
EventType
¶
Bases: Enum
Enumeration of agent lifecycle events.
Source code in src/tot_agent/agent.py
GOAL_START = auto()
class-attribute
instance-attribute
¶
The agent has received a new goal and is about to start the loop.
STEP_START = auto()
class-attribute
instance-attribute
¶
A new iteration of the tool-call loop is beginning.
AGENT_TEXT = auto()
class-attribute
instance-attribute
¶
The model produced a text block (reasoning or narration).
TOOL_CALL = auto()
class-attribute
instance-attribute
¶
The model requested a tool execution.
TOOL_RESULT = auto()
class-attribute
instance-attribute
¶
A tool call completed and returned a result.
GOAL_COMPLETE = auto()
class-attribute
instance-attribute
¶
The agent finished successfully (stop_reason == "end_turn").
STEP_LIMIT = auto()
class-attribute
instance-attribute
¶
The loop was terminated because :data:~tot_agent.config.MAX_AGENT_STEPS
was reached.
AgentEvent
dataclass
¶
Carries data from the agent loop to registered observers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_type
|
EventType
|
The kind of event that occurred. |
required |
data
|
dict[str, Any]
|
Arbitrary key-value payload; keys vary per event type. |
dict()
|
Source code in src/tot_agent/agent.py
AgentObserver
¶
Bases: ABC
Abstract base class for agent event observers (Observer pattern).
Subclass this and implement :meth:on_event to react to agent lifecycle
events without modifying the agent core.
Source code in src/tot_agent/agent.py
on_event(event)
abstractmethod
¶
Handle an agent event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
AgentEvent
|
The event to handle. |
required |
ConsoleObserver
¶
Bases: AgentObserver
Renders agent events to the terminal using :class:rich.console.Console.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
console
|
Console | None
|
Rich console instance. A new one is created if omitted. |
None
|
Source code in src/tot_agent/agent.py
on_event(event)
¶
Render event to the terminal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
AgentEvent
|
Agent event to render. |
required |
Source code in src/tot_agent/agent.py
LoggingObserver
¶
Bases: AgentObserver
Writes agent events to the Python :mod:logging hierarchy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log
|
Logger | None
|
Logger to write to. Defaults to this module's logger. |
None
|
Source code in src/tot_agent/agent.py
on_event(event)
¶
Log event at an appropriate level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
AgentEvent
|
Agent event to log. |
required |
Source code in src/tot_agent/agent.py
BrowserAgent
¶
Vision-capable agentic loop that drives a Playwright browser.
The agent calls Claude with :data:~tot_agent.tools.TOOL_DEFINITIONS and
iterates until the model reaches end_turn or the step cap is hit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bm
|
BrowserManager
|
An initialised :class: |
required |
observers
|
list[AgentObserver] | None
|
Observers to notify at each lifecycle event. Defaults
to |
None
|
model
|
str
|
Claude model identifier. Defaults to
:data: |
AGENT_MODEL
|
max_steps
|
int
|
Maximum tool-call iterations per goal. Defaults to
:data: |
MAX_AGENT_STEPS
|
api_key
|
str | None
|
Anthropic API key. Defaults to
:data: |
ANTHROPIC_API_KEY
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
At construction time if api_key is |
Source code in src/tot_agent/agent.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
add_observer(observer)
¶
Register an additional observer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observer
|
AgentObserver
|
Observer to add. |
required |
remove_observer(observer)
¶
Deregister an observer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observer
|
AgentObserver
|
Observer to remove. |
required |
run(goal)
async
¶
Execute the agent loop until the goal is achieved or the step cap is hit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
goal
|
str
|
Plain-English objective for the agent. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A human-readable summary of what was accomplished. |
Source code in src/tot_agent/agent.py
GoalTemplate
¶
Base class for structured goal strings (Template Method pattern).
Subclass and override :meth:build to create reusable, parameterised
goal strings for common test scenarios.
Source code in src/tot_agent/agent.py
build(**kwargs)
¶
Render the goal string from the provided keyword arguments.
Returns:
| Type | Description |
|---|---|
str
|
A plain-English goal string ready to pass to
:meth: |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
In the base class. |
Source code in src/tot_agent/agent.py
CreateTestsGoal
¶
Bases: GoalTemplate
Goal: create A/B tests with real book covers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of tests to create. Defaults to |
5
|
genre
|
str
|
Book genre, or |
'mixed'
|
Source code in src/tot_agent/agent.py
build(**kwargs)
¶
Render the create-tests goal string.
Returns:
| Type | Description |
|---|---|
str
|
Goal string. |
Source code in src/tot_agent/agent.py
VoteGoal
¶
Bases: GoalTemplate
Goal: have a single user vote on existing tests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
str
|
Voter's username. |
required |
password
|
str
|
Voter's password. |
required |
vote_count
|
int
|
Number of tests to vote on. Defaults to |
3
|
bias
|
str
|
Voting bias hint. Defaults to |
'random'
|
Source code in src/tot_agent/agent.py
build(**kwargs)
¶
Render the vote goal string.
Returns:
| Type | Description |
|---|---|
str
|
Goal string. |
Source code in src/tot_agent/agent.py
SimulateAllUsersGoal
¶
Bases: GoalTemplate
Goal: simulate all configured users voting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vote_count_each
|
int
|
Number of votes per user. Defaults to |
2
|
Source code in src/tot_agent/agent.py
build(**kwargs)
¶
Render the simulate-all-users goal string.
Returns:
| Type | Description |
|---|---|
str
|
Goal string. |
Source code in src/tot_agent/agent.py
FullSeedGoal
¶
Bases: GoalTemplate
Goal: create tests, run voting simulation, view results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
test_count
|
int
|
Number of A/B tests to create. Defaults to |
5
|
vote_rounds
|
int
|
Voting rounds per user. Defaults to |
1
|
Source code in src/tot_agent/agent.py
build(**kwargs)
¶
Render the full-seed goal string.
Returns:
| Type | Description |
|---|---|
str
|
Goal string. |