存储模块¶
本模块提供实验数据的存储与回放功能。
ReplayWriter¶
- class agentsociety2.storage.ReplayWriter(db_path, *, enabled=True)[源代码]¶
基类:
ReplaySinkBack-compat alias over
ReplaySink.The legacy public API (
agentsociety2.storage, examples) constructed a SQLite writer with a.dbfile path and calledawait init()before use. That writer is gone; this alias keeps the symbol importable and writes the same JSONL format, mapping the legacydb_pathto a replay directory (a trailing.dbis stripped) so old call sites keep working without SQLite.
ReplaySink¶
- class agentsociety2.storage.ReplaySink(replay_dir, *, enabled=True)[源代码]¶
基类:
objectPer-process, distributed, append-only JSONL replay writer.
One instance per writer process (driver / env router actor / each agent Ray task). Holds its own open shard fds; per-shard lock files make concurrent appends to the same shard file safe for rows of any size.
ReplayDatasetSpec¶
- class agentsociety2.storage.ReplayDatasetSpec(dataset_id, table_name, module_name, kind, title='', description='', entity_key=None, step_key=None, time_key=None, default_order=<factory>, capabilities=<factory>, version=1)[源代码]¶
Semantic metadata for a replay dataset backed by a JSONL table shard set.
- __init__(dataset_id, table_name, module_name, kind, title='', description='', entity_key=None, step_key=None, time_key=None, default_order=<factory>, capabilities=<factory>, version=1)¶
ReplayReader¶
- class agentsociety2.storage.ReplayReader(replay_dir)[源代码]¶
Read replay
_schema.json+ sharded JSONL rows via DuckDB.- fetch_dataset_rows(dataset, *, order_by=None, desc=False, step=None, entity_id=None, start_step=None, end_step=None, max_step=None, columns=None, latest_per_entity=False, limit=None, offset=0)[源代码]¶
- count_dataset_rows(dataset, *, step=None, entity_id=None, start_step=None, end_step=None, max_step=None, latest_per_entity=False)[源代码]¶
ColumnDef¶
- class agentsociety2.storage.ColumnDef(name, type, nullable=True, default=None, title=None, description=None, logical_type=None, analysis_role=None, unit=None, enum_values=None, example=None, tags=<factory>)[源代码]¶
表列定义(SQLite)。
字段说明:
name:列名。type:SQLite 列类型字符串,取值见ColumnType。nullable:是否允许 NULL。default:默认值表达式,例如CURRENT_TIMESTAMP。title:可选,人类可读的列标题。description:可选,语义描述,供回放、导出与分析使用。logical_type:可选,逻辑类型,例如geo.lng、money。analysis_role:可选,分析角色,例如measure。unit:可选,单位字符串,供分析与报告使用。enum_values:可选,离散列的枚举值列表。example:可选,示例值。tags:可选,自由标签列表。
- __init__(name, type, nullable=True, default=None, title=None, description=None, logical_type=None, analysis_role=None, unit=None, enum_values=None, example=None, tags=<factory>)¶
TableSchema¶
分布式 Replay Proxy¶
大规模仿真下,ReplayProxy 只携带 replay_dir 与启用标志。各 agent / env /
society 进程收到 proxy 后在本地懒加载 ReplaySink,并直接 append sharded JSONL。
ReplayProxy¶
- class agentsociety2.storage.replay_proxy.ReplayProxy(replay_dir=None, enabled=True, _sink=None)[源代码]¶
Serializable config for distributed replay writing.
Carries only the replay output directory and an enable flag — no Ray actor handle. Methods lazily build+cache a process-local
ReplaySink(fromreplay_dir) on first use and delegate, so each agent Ray task / env actor that receives a deserialized copy gets its own sink and its own file descriptors.enabled=Falsemakes every method a no-op.- __init__(replay_dir=None, enabled=True, _sink=None)¶
- agentsociety2.storage.build_replay_sink(replay_proxy)[源代码]¶
Build a per-process
ReplaySinkfrom aReplayProxy.Returns
Nonewhen the proxy is disabled or carries noreplay_dir.
兼容数据模型¶
以下模型仅用于兼容读取历史 SQLite 数据库;新实验默认不再写入这些 agent 表。
AgentProfile¶
- class agentsociety2.storage.models.AgentProfile(*, id, name, profile={}, created_at=<factory>)[源代码]¶
agent 档案信息(框架表)。
- __init__(**data)¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config = {'from_attributes': True, 'read_from_attributes': True, 'read_with_orm_mode': True, 'registry': PydanticUndefined, 'table': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
AgentStatus¶
- class agentsociety2.storage.models.AgentStatus(*, id, step, t, action=None, status=None, created_at=<factory>)[源代码]¶
agent 在某一步的状态快照(框架表)。
- __init__(**data)¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config = {'from_attributes': True, 'read_from_attributes': True, 'read_with_orm_mode': True, 'registry': PydanticUndefined, 'table': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
AgentDialog¶
- class agentsociety2.storage.models.AgentDialog(*, id=None, agent_id, step, t, type, speaker, content, created_at=<factory>)[源代码]¶
agent 对话记录(框架表)。
- __init__(**data)¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config = {'from_attributes': True, 'read_from_attributes': True, 'read_with_orm_mode': True, 'registry': PydanticUndefined, 'table': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].