Skip to main content

Programs

A Program is an immutable, content-addressed snapshot of one Policy directory.

Directory layout

A Program directory must contain policy.py:

my-policy/
├── policy.py
└── model.json

policy.py must define make_policy(context). Other files are optional and may be imported or read by the Policy.

Create a Program

from evopolicygym import Program

program = Program.from_directory("my-policy/")

print(program.digest)
print(program.files)

from_directory() reads a stable snapshot. Later changes to my-policy/ do not change program.

PropertyValue
digestSHA-256 content identity, including the fixed entry point and Policy ABI version.
entrypointpolicy.py:make_policy
policy_abiPolicy ABI version required by the snapshot.
filesRelative file paths in deterministic order.
file_countNumber of files in the snapshot.
total_bytesTotal uncompressed file size.

Snapshot limits

Default limits are 1,000 files, 64 MiB total, and 16 MiB per file. Override them with ProgramLimits:

from evopolicygym import Program
from evopolicygym.program import ProgramLimits

program = Program.from_directory(
"my-policy/",
limits=ProgramLimits(
max_files=100,
max_total_bytes=8 * 1024 * 1024,
max_file_bytes=2 * 1024 * 1024,
),
)

The source must be a real directory. Symbolic links are rejected. .git and __pycache__ directories are ignored.

Read or materialize a snapshot

source = program.read_bytes("policy.py")
program.write_to("saved-policy")

write_to() requires a destination that does not already exist.

Next