Development / Contribute
Coding conventions
How AlphaAvatar's Python reads. The models are avatar-core/alphaavatar/core/perception/runtime.py and the Memory plugin (MemoryRuntime, MemoryStore, MemoryContextState, PassiveRecall).
Code order
Modules and classes read top to bottom, every name defined before it is used:
- A module starts with its license header and
from __future__ import annotations, then imports grouped standard library, third party,alphaavatarabsolute, relative; imports for typing alone go underTYPE_CHECKING. Its private helpers and private classes come before the public code that uses them. - A class is ordered: its one-line docstring, class constants (
_UPPER_CASE, grouped by purpose),__init__, properties, static methods, then its other methods. - Define a function before the code that calls it: a private method above the methods that call it. Mutually recursive functions are the one exception.
- Private methods come before public ones, unless the class is divided into functional areas by section strings such as
"""Source state operations"""or"""Runtime operations"""; within each area the order holds again. - Lifecycle comes last: the private implementation (
_start,_stop,_close) before its public entry (startandstop, oron_startandon_stop). - A method that must run with a lock held ends in
_locked; a background loop is_consume_*; a task's done callback is_*_doneor_observe.
A class divided into areas, for example:
Data types, enums and configuration
A package keeps its data types and enums apart from the code that works with them:
- A class that carries data rather than behavior goes in the package's
schemas/subpackage, one module per topic (schemas/report.py,schemas/session.py): a dataclass, aNamedTuple, a pydantic model of a record or message. A method that only reads or converts its own data, such asto_dict, does not change that. - An enum (
Enum,StrEnum,IntEnum) goes in the package'senums/subpackage the same way. A fixed set of states or kinds is an enum, not aLiteralof strings. - Each subpackage's
__init__.pyre-exports its names with__all__, and other modules import them from the subpackage:from .schemas import Report,from ..enums import CheckStatus. - A class with behavior and an exception stay in the module that owns them.
- Configuration is not a schema: configuration models, and the configuration objects built from them such as
ApplicationConfig, stay with their owner. A configuration that serves only its own directory, such as a processor's, is that directory'sconfig.py. One that other configuration composes, or that has to be told apart from others, is named for what it configures,<topic>_config.py:avatar_config.pyandruntime_config.pyinalphaavatar.agents.configs,host_config.pybeside the Host application. - A module named
config.pyor<topic>_config.pydefines configuration, and holds besides only the logic that processes what it defines, such as reading or normalizing its values. Other operations do not belong in it: a module that holds them, or defines no configuration, is named for what it does.utils/vdb/backend.py(a vector store's backend, the package it needs, its embeddings and its directory) is an example, notutils/vdb/config.py.
alphaavatar.core.session and alphaavatar.host.tools.doctor are laid out this way.
Constructor fields
An __init__ that sets more than a handful of fields groups them by purpose: borrowed services and settings, identity, budgets, the state being worked on, lifecycle. Each group is preceded by a blank line and a one-line comment naming what the group holds. The first group follows the signature directly, without a blank line. A field built from another comes after it, in the groups and within each. For example, LoopExecution in the Loop plugin:
Function bodies
Inside a function, blocks that do clearly different things, or each span several lines, are
separated by a blank line. A comment is not required: a block gets one short comment only where
it stands clearly apart and its purpose is not plain from the code; one that repeats the code or
the docstring is left out. For example, _vertex in the Provider plugin:
A guard, an if that returns, raises, continues or breaks, stays with the value it checks:
the statement that produces the value and the guards after it are one block, with no blank line
inside. The code that goes on past them begins the next block, after a blank line. Guards followed
only by the one-line return, raise, continue or break that ends the same decision stay one
block. In _vertex, the try produces vertexai and its checks follow it directly; chosen, the
next source, starts after a blank line.
Examples, each the wrong way first.
Each value with its guard, a blank line after the guards. Not:
But:
The same inside a loop. Not:
But:
A decision stays one block. Not:
But:
A comment says what the code does not. Not:
But:
The shell scripts under packaging/ and scripts/ follow the same rule.
Code being changed follows these rules; code nobody touches is not reordered for them alone.
Edits target dev/v0.7.0. Published snapshots do not change.