Instruction file imported from jaelliot/keri-challenge (
.github/instructions/code-generation.instructions.md). Copyright stays with the author.
Use when
- Generating new Python code for the KERI project.
- Refactoring existing code to match KERI/ToIP standards.
- Implementing KERI specifications (KEL, CESR, ACDC).
Do
- File Management:
- Check Existence: Verify if a target module exists before creating a new one.
- Domain Structure: Place functionality in
keridomain modules (e.g.,keri/corefor primitives,keri/appfor logic,keri/dbfor storage). - Exception for Challenge: For this single-purpose challenge, a simplified structure (e.g.,
src/app.pyandtests/test_app.py) is permitted. - Path Comments: Include a single-line comment at the top of every file indicating its path (e.g.,
# src/keri/core/eventing.py).
- Patterns:
- Immutability: Use
dataclasses.dataclass(frozen=True)ortyping.NamedTuplefor data transfer objects (events, messages). - AsyncIO: Implement I/O-bound operations as
asyncfunctions/methods. Useasynciocontext managers for resources. - Generators: Use generators for processing streams of events (CESR streams) to handle backpressure and memory efficiency.
- Context Managers: Use
withstatements for resource management (DB handles, file I/O, locks).
- Immutability: Use
- Typing (Strict):
- Explicit Types: Use
bytesfor raw crypto material andstrfor CESR-encoded (Base64) text. Never mix them. - Type Hints: Fully type hint all functions (
def func(a: int) -> str:). - No Any: Avoid
typing.Anyunless interacting with untyped 3rd-party libraries (and document why).
- Explicit Types: Use
- Serialization:
- Canonical JSON: When using JSON, ensure no whitespace and sorted keys (required for KERI hashing).
- CESR: Prefer CESR encoding for internal representation of crypto primitives.
Don't
- Anti-Patterns:
- Mutable Defaults: Never use mutable default arguments (e.g.,
def foo(l=[])). - Implicit Casting: Don't rely on implicit
strtobytesconversion. - Global State: Avoid global mutable state. Use dependency injection or context objects (
Context). - Generic Utils: Don't create
utils.py. Put the function where it belongs (e.g.,hashing.py,encoding.py).
- Mutable Defaults: Never use mutable default arguments (e.g.,
- Dependencies:
- Don't introduce heavy external dependencies (e.g., Redis, Kafka) when internal KERI mechanisms (witnesses, direct mode) suffice.
- Don't use
floatfor precise calculations; usedecimal.Decimalor integer math.
Notes / Examples
- File Path Comment:
# src/keri/core/coring.py import dataclasses ... - Immutable Event Class:
@dataclasses.dataclass(frozen=True) class InceptionEvent: v: str # Version string t: str # Type (icp) d: str # Digest ... - Canonical JSON Serialization:
import json # Ensure strict ordering and no whitespace for stable hashing raw = json.dumps(data, separators=(",", ":"), sort_keys=True).encode("utf-8") - Rationale: "Used
frozen=TrueforRotationEventto ensure the event content cannot be modified after signing, preserving hash integrity."