···11-# initial prompt
11+# c12-layer: Understanding & Extending c12's Configuration Layering
22+33+## Goals
44+55+This document guides analysis of c12's configuration layering system and proposes enhancements for more dynamic, introspectable configuration management.
66+77+## Part 1: Understanding the Current System
88+99+### Questions to Answer
1010+1111+1. **Where does layering happen?**
1212+ - How does `defu` merge configurations?
1313+ - What is the call graph from `loadConfig()` to final merged config?
1414+1515+2. **When in the lifecycle?**
1616+ - Map the complete execution pipeline from `loadConfig()` invocation to resolved config
1717+ - Identify when each source is loaded, when extends are resolved, when merging occurs
21833-we want to understand how configuration from various sources is layered together. supposedly `defu` is used to merge configs. where does this happen? when in the lifecycle of execution happens? what is the total execution pipeline for a normal run of c12? use mermaid diagrams .
1919+3. **What is the layer resolution order?**
2020+ - Document the priority stack (per README: overrides → config file → RC → global RC → package.json → defaults → extended layers)
2121+ - How does environment-specific config (`$development`, `$production`, etc.) factor in?
42255-we want to try to understand more about how we might have more dynamic sources rather than a fixed path of sources. for instance we might want to make drop in config directories, such as talked about in https://github.com/unjs/c12/issues/298 .
2323+### Deliverables
62477-talking about how configuration becomes sourced. talk about how we can create a better registry of layers we are using. a registry of config layers, that can be built, and then ran, rather than a fixed pipeline. this project needs a more structured management of configuration, rather than being merely a fixed purpose tool.
2525+- **Mermaid sequence diagram**: Show the lifecycle from `loadConfig()` call through to final config
2626+- **Mermaid flowchart**: Show decision points (does RC exist? does config extend? etc.)
2727+- **Code citations**: Link to the actual source locations where merging happens
2828+2929+---
3030+3131+## Part 2: Dynamic Configuration Sources
3232+3333+### Problem Statement
3434+3535+c12 currently has a fixed resolution pipeline. We want to explore:
3636+3737+1. **Drop-in config directories** (à la systemd's `*.conf.d/`)
3838+ - Reference: https://github.com/unjs/c12/issues/298
3939+ - Allow `<name>.config.d/` directories where multiple configs can be dropped in
4040+ - Configs sorted alphabetically (or with numeric prefixes like `00-base.ts`, `10-overrides.ts`)
4141+4242+2. **Pluggable source providers**
4343+ - Instead of hardcoded sources (file, RC, package.json), allow registering custom providers
4444+ - Examples: environment variables provider, remote config provider, vault/secrets provider
4545+4646+### Design Questions
4747+4848+- How would drop-in directories integrate with the existing layer system?
4949+- Should drop-in configs be siblings to extended layers, or a separate concept?
5050+- How do we handle ordering/priority for drop-in files?
5151+5252+---
5353+5454+## Part 3: Layer Registry Architecture
5555+5656+### Vision
5757+5858+Transform c12 from a "fixed pipeline config loader" into a "structured config layer manager" with:
5959+6060+1. **Explicit layer registry**
6161+ - Named, ordered collection of configuration sources
6262+ - Each layer has metadata: name, source type, priority, file path(s)
6363+6464+2. **Two-phase execution**
6565+ - **Build phase**: Construct the layer registry, validate sources exist, resolve extends
6666+ - **Run phase**: Execute the registry to produce final merged config
6767+6868+3. **Introspection capabilities**
6969+ - Query which layer provided a specific config key
7070+ - Trace config value provenance (like Rust's `figment` crate metadata)
7171+ - Debug mode showing layer-by-layer merge steps
7272+7373+### Inspiration
7474+7575+- **Rust's figment**: Providers with metadata, value provenance tracking
7676+- **systemd**: Drop-in directories, clear override semantics
7777+- **Kubernetes**: ConfigMap layering, strategic merge patches
7878+7979+### Proposed API Sketch
8080+8181+```typescript
8282+// Build a layer registry explicitly
8383+const registry = createLayerRegistry({
8484+ name: "myapp"
8585+})
8686+ .addSource("defaults", { type: "static", config: { ... } })
8787+ .addSource("base-file", { type: "file", path: "myapp.config.ts" })
8888+ .addSource("drop-ins", { type: "directory", path: "myapp.config.d/" })
8989+ .addSource("env-overrides", { type: "env", prefix: "MYAPP_" })
9090+ .addSource("cli-overrides", { type: "static", config: cliArgs });
9191+9292+// Inspect before running
9393+console.log(registry.layers); // See all registered sources
9494+console.log(registry.resolve("database.host")); // Which layer provides this?
9595+9696+// Execute to get final config
9797+const { config, provenance } = await registry.load();
9898+```
9999+100100+---
101101+102102+## Part 4: Implementation Considerations
103103+104104+### Backward Compatibility
105105+106106+- `loadConfig()` should continue working unchanged
107107+- New APIs are opt-in enhancements
108108+109109+### Key Extension Points to Identify
110110+111111+1. Where can we hook into layer resolution?
112112+2. Can we intercept/wrap the merge function?
113113+3. How do we inject additional sources into the pipeline?
114114+115115+### Files to Analyze
116116+117117+- `src/loader.ts` - Main loading logic
118118+- `src/config.ts` - Config resolution
119119+- Look for `defu` usage patterns
120120+- Look for `extends` resolution logic
121121+122122+---
123123+124124+## Success Criteria
125125+126126+After this analysis, we should have:
127127+128128+1. Clear understanding of c12's internals with diagrams
129129+2. Feasibility assessment for drop-in directories
130130+3. Draft design for layer registry architecture
131131+4. Identified extension points or required changes to c12