> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nika.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# L0 foundation decisions

> Q1-Q13: the early architectural decisions that shaped the L0 and L0.5 layers, and what each became at v0.123.0.

These are **engine-internal** decisions, not workflow-language rules.
Thirteen questions, locked in April 2026 (revision 3), shaped the **L0
foundation** (22 crates at v0.124.4) and the **L0.5 kernel
contract**. Some were executed as written, some differently, and some not at
all: the index records what each became at v0.124.4, and the deep
dives keep the original rationale as history.

> [!NOTE]
>
> **Canonical source:** [`docs/architecture/l0-l05-architecture-decisions.md`](https://github.com/supernovae-st/nika/blob/main/docs/architecture/l0-l05-architecture-decisions.md).
> Full rationale (3-4 agents per question) lives in the engine repo.

## Decision index

| Q | Decision (April 2026) | At v0.123.0 |
| - | - | - |
| **Q1** | No proc macros in L0 · manual impl + `macro_rules!` | **Holds.** No crate in the workspace is a proc macro; the L2 `#[builtin_tool]` escape hatch was never built |
| **Q2** | No `nika-stdx`; split `nika-error` → `nika-types` + `nika-error` | **Executed** |
| **Q3** | Extract `nika-catalog-codegen` | **Executed** (`nika-catalog-codegen`, L0); the community overlay it was meant to serve was not built |
| **Q4** | `nika-event`: L0 types + L1 store + L2 export | **Not executed as split.** `nika-event` holds the types; the journal is written by the runtime and exported by `nika trace export`; no `nika-event-store` or `nika-event-export` crate exists |
| **Q5** | Admission order: schema → codegen → event → binding → pck-manifest | **Superseded.** No `nika-binding` crate: templates live in `nika-tmpl`, expressions in `nika-cel` |
| **Q6** | `EventKind` = scoped sub-enums, about 22 categories | **Superseded.** One flat `#[non_exhaustive]` `EventKind` of 35 kinds |
| **Q7** | `nika-kernel` prelude re-export hub | **Executed.** `nika-kernel` is a facade with a `prelude` over `nika-kernel-core`, `-ai`, `-runtime` and `-plugin` |
| **Q8** | `nika-transform` as a standalone L0 crate | **Not executed.** No transform crate and no template filters: transformation is jq (`extract:`, `nika:jq`) |
| **Q9** | `Timestamp` + `WallDuration` as a module in `nika-types` | **Executed** |
| **Q10** | Canonical JSON (RFC 8785) as a module in `nika-types` | **Not executed** |
| **Q11** | Token streaming: deltas, not one event per token | **Executed in a different shape.** `InferEvent::Delta { text }`, alongside `ToolUseStart`, `ToolUseDelta`, `Thinking`, `Usage` and `Done` |
| **Q12** | Drop `ObservabilitySink`, add `AuditSink` (five sibling channels) | **Interface only.** `AuditSink` exists as a kernel trait with no production implementation |
| **Q13** | Bridge OTel GenAI conventions via typed `GenAiAttrs` | **Executed.** `GenAiAttrs` lives in `nika-kernel-ai`; the attributes ride the spans of `nika trace export` |

## Why these decisions exist

> [!WARNING]
>
> L0 is the foundation: a wrong choice there propagates through every crate
> above it. These decisions were made before any L1 or L2 admission, to avoid
> costly migrations, and each was reviewed independently several times.

## Deep dives

The deep dives keep the April 2026 rationale as it was written. Where it
names a crate or a filter that does not exist, the index above records what
was built instead.

> [accordion] Q1: No proc macros in L0
>
> **Rationale.** Proc macros add compile-time dependency on `syn` + `quote`
>
> * `proc-macro2` (\~50 MLOC compile graph). L0 crates must compile in
>   seconds for CI parity testing. Manual `impl Trait for Struct` +
>   `macro_rules!` covers every pattern we've needed at L0.
>
> **Escape hatch.** Proc macros allowed at L2+ (e.g., `#[builtin_tool]`
> attribute macro in `nika-verb-invoke`). L0 stays procmacro-free forever.
>
> **Impact.** `nika-types` compile time stays \< 2s; downstream crates
> don't pay proc-macro cost transitively.

> [accordion] Q2: nika-types is the foundation, not nika-stdx
>
> **Rationale.** A generic `nika-stdx` crate devolves into a kitchen-sink
> (anti-pattern #1 from layer-registry). Instead, foundation types live in
> `nika-types` (splitable when cohesion demands), error infra in
> `nika-error`, lookup tables in `nika-catalog`. Purpose-named, not
> kitchen-sink.
>
> **Executed.** `nika-types` admitted as L0 foundation (no I/O, no async,
> Cow-heavy). `nika-error` re-exports the subset it needs.

> [accordion] Q3: Extract nika-catalog-codegen as standalone L0
>
> **Rationale.** TOML → Rust codegen is testable logic that today lives
> in `build.rs`. Extract it so:
>
> * `build.rs` remains \<50 LOC (delegates).
> * Codegen gets its own unit tests + snapshot tests.
> * Community overlays (`nika-catalog-cn`) reuse the same codegen.
>
> Targets admission Round 3 post-nika-schema.

> [accordion] Q4: nika-event 3-layer split
>
> **Rationale.** Event emission (L0 types + scoped sub-enums) is concept
> separate from storage (L1 `nika-event-store` writes NDJSON / SQLite)
> which is separate from export (L2 `nika-event-export` pushes OTel /
> Datadog / Honeycomb). Each layer tested in isolation.
>
> **Mapping**: L0 `nika-event` (\~2.5k LOC) types + sub-enums ·
> L1 `nika-event-store` (\~3k LOC, future) · L2 `nika-event-export` (\~2k LOC, future).

> [accordion] Q5: Admission order for L0
>
> **Order.** schema → catalog-codegen → event → binding → pck-manifest.
>
> **Why this order.**
>
> * `nika-schema` unblocks the rest (it defines the AST).
> * `catalog-codegen` independent (purely build-time).
> * `nika-event` needs types (done) and schema (for correlation IDs).
> * `nika-binding` needs event + transform.
> * `nika-pck-manifest` is standalone and can run in parallel with any of the above.

> [accordion] Q6: EventKind scoped sub-enums (not mega-enum)
>
> **Rationale.** A single `EventKind` enum with 22 variants becomes a
> merge-conflict magnet and bloats every consumer's match arm. Instead,
> \~22 sub-enums grouped by emission site (`ExecEvent`, `InferEvent`,
> `ProviderEvent`, etc.), unified under an `Event` sum type via the
> `event_categories!` macro.
>
> **Impact.** Each verb crate emits its own sub-enum variants; consumers
> match on the category they care about; rest are transparent.

> [accordion] Q7: nika-kernel prelude re-export hub
>
> **Rationale.** L2+ verb crates today `use nika_types::*; use nika_error::*;
>         use nika_kernel::traits::*;` (3 imports every file). Instead,
> `nika-kernel` ships `pub mod prelude` that re-exports from `nika-types`
>
> * `nika-error` + its own traits. Verb crates: `use nika_kernel::prelude::*;`.
>
> **Cost.** 4 lines of code. Zero new deps (already transitive).

> [accordion] Q8: nika-transform = standalone L0 crate (REVERTED rev.2)
>
> **Original decision.** Inline `nika-transform` into `nika-binding` (single
> consumer).
>
> **Reversion rationale.** Swarm audit found a **second consumer**:
> `nika-builtin-*` crates need `nika-transform` filters (json, base64,
> shell\_quote) for `invoke:builtin:` parameter processing. Inlining would
> force `nika-binding` dependency on every builtin crate (wrong coupling).
>
> **Final.** `nika-transform` stays L0, 65 transforms in 7 sub-modules,
> two consumers (binding + builtin-\*).

> [accordion] Q9-Q10: Timestamp + canonical-JSON = modules in nika-types
>
> **Rationale rev.2.** Both were initially planned as standalone L0 crates
> (`nika-time`, `nika-canonical`). Audit found each has zero
> non-foundation dependencies and tight coupling to core types. They
> become modules inside `nika-types`:
>
> * `nika_types::timestamp`: `Timestamp` + `WallDuration` (monotonic + RFC3339)
> * `nika_types::hash::canonical`: RFC 8785 canonical-JSON for hash stability
>
> **Impact.** `nika-types` gains \~300 LOC, stays well under 15k crate cap.

> [accordion] Q11: Token-streaming delta-batching
>
> **Rationale.** Emitting 1 `InferEvent::Token` per LLM token would flood
> the event bus (2k tokens/s on fast providers). Instead, batch into
> deltas: emit `InferEvent::Delta { text, token_count, duration }` every
> 50ms or 32 tokens, whichever first.
>
> **Impact.** Event rate capped at \~20 Hz regardless of provider speed.

> [accordion] Q12: 5 sibling sinks (not 1 ObservabilitySink)
>
> **Rationale rev.3.** The original `ObservabilitySink` god-trait would have
> forced every sink implementation to handle 5 concerns at once. Instead,
> 5 sibling traits live alongside each other:
>
> * `EventSink`: structured events (NDJSON, OpenTelemetry)
> * `MetricsExporter`: Prometheus / StatsD / OTel metrics
> * `TracerProvider`: OTel traces (W3C TraceContext)
> * `AuditSink`: compliance audit log (tamper-evident, separate from EventSink)
> * `BillingSink`: cost accounting (\$ per provider call)
>
> **Impact.** Each sink tested independently. Implementations can mix-and-match
> (e.g., Honeycomb for trace, Prometheus for metrics, local NDJSON for audit).

> [accordion] Q13: OTel GenAI semconv via typed GenAiAttrs
>
> **Rationale rev.3.** OpenTelemetry defines [GenAI semantic conventions](https://opentelemetry.io/docs/specs/semconv/gen-ai/)
> for LLM attributes (`gen_ai.system`, `gen_ai.request.model`, etc.).
> Rather than stuff strings into a `HashMap<String, String>`, expose a
> typed `GenAiAttrs` struct on `InferRequest` / `InferResponse`.
>
> **Impact.** Consumers get IDE completion + compile-time safety.
> Non-breaking: `GenAiAttrs` is `#[non_exhaustive]`. New attrs are additive.

## See also

> [card] [Layer registry](https://docs.nika.sh/architecture/layers.md)
>
> How these L0 crates fit into the layers.

> [card] [Forward-compat invariants](https://docs.nika.sh/architecture/forward-compat-invariants.md)
>
> The patterns that make these decisions safe to evolve.

> [card] [12-gate admission](https://docs.nika.sh/architecture/admission.md)
>
> How each L0 crate earns its seat.

> [card] [Constellation](https://docs.nika.sh/reference/constellation.md)
>
> Which crates are admitted at v0.123.0.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.