docs(styleguide): strengthen type_aliases §1 (Metadata is boundary type, not escape hatch)
This commit is contained in:
@@ -37,17 +37,28 @@ Plus the NamedTuple:
|
||||
|
||||
## The 5 Decision Patterns
|
||||
|
||||
### 1. Use `Metadata` for any dict-shaped record
|
||||
### 1. Use `Metadata` ONLY at the wire boundary (TOML/JSON parse)
|
||||
|
||||
**UPDATED 2026-06-25 (the C11/Odin/Jai-in-Python mandate).** `Metadata` is the typed fat struct at the wire boundary. It is `@dataclass(frozen=True, slots=True)` with explicit fields covering the TOML/JSON wire schema (paths, project, discussion, role, content, ts, source_tier, model, depends_on, document, script, args, etc.).
|
||||
|
||||
```python
|
||||
def parse_metadata(raw: str) -> Metadata:
|
||||
return json.loads(raw)
|
||||
# CORRECT — at the literal wire boundary:
|
||||
def _parse_toml_config(raw: str) -> Metadata:
|
||||
return Metadata.from_dict(tomllib.loads(raw))
|
||||
|
||||
def save_metadata(name: str, data: Metadata) -> None:
|
||||
...
|
||||
# CORRECT — consumer at the boundary, converts immediately:
|
||||
def _load_project_context(raw_toml: Metadata) -> ProjectContext:
|
||||
return ProjectContext.from_dict(raw_toml)
|
||||
|
||||
# WRONG — using Metadata as a lazy-typing escape hatch:
|
||||
def process_event(self, event: Metadata) -> None:
|
||||
if hasattr(event, 'tool_calls'):
|
||||
... # ← BAD: this is the laziest possible typing
|
||||
```
|
||||
|
||||
The alias is `dict[str, Any]` at runtime; the name documents the semantic role.
|
||||
`Metadata` is **NOT** `TypeAlias = dict[str, Any]`. It is a typed fat struct. The boundary is 2-3 functions per file. Every consumer IMMEDIATELY converts to a componentized dataclass via `from_dict()`.
|
||||
|
||||
**Anti-pattern (banned):** `Metadata: TypeAlias = dict[str, Any]` (the lazy-typing escape hatch). LLMs default to this because it's idiomatic Python. This codebase does NOT do idiomatic Python. See `data_oriented_design.md` §8.5.
|
||||
|
||||
### 2. Use the more specific alias when the role is known
|
||||
|
||||
|
||||
Reference in New Issue
Block a user