forge.core¶
Core runtime — module registry, lifecycle, DI container, event bus, and context management.
Provides the ForgeModule base class, ForgeRuntime coordinator, and supporting
infrastructure for module initialization ordering, dependency injection, event
emission, and async context propagation.
forge.core ¶
Core runtime — module registry, lifecycle, DI container, event bus, and context management.
Provides the ForgeModule base class, ForgeRuntime coordinator, and supporting infrastructure for module initialization ordering, dependency injection, event emission, and async context propagation.
Classes¶
CircularDependencyError ¶
Bases: ModuleError
Raised when a circular dependency is detected between modules.
ConfigurationError ¶
Bases: ForgeError
Raised when configuration is missing, invalid, or misconfigured.
Container ¶
Lightweight explicit dependency injection container.
This container holds all registered ForgeModule instances and
provides type-safe retrieval. Registration is explicit (not
annotation-based) per ADR-001_.
.. _ADR-001: https://github.com/kaif-ai-engineer/forge-kaif/blob/main/docs/ADR-001.md
Source code in src/forge/core/container.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
Attributes¶
Methods:¶
get ¶
Retrieve a registered module by its class.
Parameters¶
module_type: The class of the registered module.
Returns¶
ForgeModule The registered module instance.
Raises¶
ModuleNotFoundError When no module of the requested type is registered.
Source code in src/forge/core/container.py
get_by_name ¶
Retrieve a registered module by its name.
Parameters¶
name: The unique module name.
Returns¶
ForgeModule The registered module instance.
Raises¶
ModuleNotFoundError When no module with the given name is registered.
Source code in src/forge/core/container.py
has ¶
has_name ¶
initialization_order ¶
Return modules sorted by dependency order (topological sort).
Modules with no dependencies appear first; modules that depend on others appear after their dependencies.
Source code in src/forge/core/container.py
register ¶
Register a module instance.
Parameters¶
module:
An instantiated ForgeModule subclass.
replace:
If True, silently replace an existing registration
with the same name. Otherwise raise
ModuleRegistrationError on conflict.
Raises¶
ModuleRegistrationError
When a module with the same name is already registered and
replace is False.
Source code in src/forge/core/container.py
EventBus ¶
In-process async event bus for decoupled module communication.
Supports subscribing with :meth:on / :meth:off and emitting
events with :meth:emit. All handlers are awaited concurrently
and exceptions are caught and logged so a single failing handler
never blocks other subscribers.
Source code in src/forge/core/events.py
Methods:¶
emit
async
¶
Emit event and await all registered handlers concurrently.
Parameters¶
event: Dot-delimited event name. **kwargs: Payload passed as keyword arguments to each handler.
Source code in src/forge/core/events.py
has_listeners ¶
off ¶
Unregister a previously registered handler.
Raises¶
EventError If the handler was not registered for event.
Source code in src/forge/core/events.py
EventError ¶
Bases: ForgeError
Raised when an event bus operation fails.
ForgeError ¶
ForgeModule ¶
Bases: ABC
Base interface for all forge modules.
Every module in the framework must implement this contract.
Subclasses must provide a unique name and may optionally declare
dependencies, setup, teardown, and health_check.
Source code in src/forge/core/module.py
Attributes¶
dependencies
property
¶
Names of modules this module depends on.
The runtime guarantees that all dependencies are initialised
before setup is called on this module.
Methods:¶
health_check ¶
Optional health check registered automatically by the runtime.
Return HealthResult.ok() by default.
setup
async
¶
Called once during runtime initialisation.
Order is guaranteed to follow a topological sort of the dependency graph declared across all registered modules.
teardown
async
¶
ForgeRuntime ¶
Central runtime coordinator for the forge framework.
Manages module lifecycle, dependency injection, event dispatch, and graceful shutdown.
Usage::
runtime = ForgeRuntime()
runtime.register(ConfigModule())
await runtime.init()
# ... application code ...
await runtime.teardown()
Source code in src/forge/core/runtime.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
Attributes¶
Methods:¶
get ¶
Retrieve a registered module by its class.
Raises ModuleNotFoundError if not registered.
get_active
classmethod
¶
Return the active initialized runtime instance.
Raises¶
RuntimeNotInitializedError When no runtime is currently active/initialized.
Source code in src/forge/core/runtime.py
init
async
¶
Initialise all registered modules in dependency order.
Parameters¶
shutdown_timeout: Seconds to wait for graceful shutdown before forcible stop.
Raises¶
ForgeError If the runtime is already initialized or a module fails to set up.
Source code in src/forge/core/runtime.py
on_ready ¶
Register a callback invoked after all modules are initialised.
The callback must be an async function.
on_shutdown ¶
Register a callback invoked before teardown begins.
The callback must be an async function.
register ¶
Register a module with the runtime.
Parameters¶
module:
An instantiated ForgeModule subclass.
replace:
If True, silently replace an existing registration.
Returns¶
Self For method chaining.
Source code in src/forge/core/runtime.py
teardown
async
¶
Shut down all modules in reverse dependency order.
Safe to call multiple times; subsequent calls are no-ops.
Source code in src/forge/core/runtime.py
use_defaults ¶
Register all default modules (config, log, retry, health, cache).
This is a convenience method for standard applications. For custom configurations, register modules individually.
Returns¶
Self For method chaining.
Source code in src/forge/core/runtime.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
HealthCheckError ¶
Bases: ForgeError
Raised when a health check fails critically.
HealthResult ¶
Result of a single health check.
Source code in src/forge/core/module.py
ModuleError ¶
Bases: ForgeError
Base exception for module-related errors.
ModuleLifecycleState ¶
Bases: Enum
Lifecycle states for a forge module.
Source code in src/forge/core/module.py
ModuleNotFoundError ¶
Bases: ModuleError
Raised when a requested module is not registered.
ModuleRegistrationError ¶
Bases: ModuleError
Raised when module registration fails (e.g., duplicate registration).
ModuleStateError ¶
Bases: ModuleError
Raised when a module operation is invalid for its current lifecycle state.
RuntimeNotInitializedError ¶
Bases: ForgeError
Raised when an operation requires an initialized runtime but it is not.
TraceContext ¶
Async context manager that sets trace ID, span ID, and baggage.
Usage::
async with TraceContext(trace_id="abc") as ctx:
print(ctx.trace_id)
Source code in src/forge/core/context.py
Functions:¶
generate_span_id ¶
generate_trace_id ¶
get_baggage ¶
get_baggage_item ¶
get_meter ¶
Return an OTel Meter, or None if OTel is not installed.
Source code in src/forge/core/otel.py
get_span_id ¶
get_trace_id ¶
get_tracer ¶
Return an OTel Tracer, or None if OTel is not installed.
Source code in src/forge/core/otel.py
in_span ¶
in_span(name: str, attributes: dict[str, Any] | None = None, tracer: Any = None) -> Generator[Any, None, None]
Context manager that creates an OTel span when OTel is available.
Yields the span (or None when OTel is not installed).
Source code in src/forge/core/otel.py
init_otel ¶
Initialise OTel providers for internal collection only (no exporters).
Source code in src/forge/core/otel.py
is_otel_available ¶
reset_baggage ¶
reset_span_id ¶
reset_trace_id ¶
set_baggage ¶
set_span_id ¶
Set the current span ID.
Parameters¶
span_id:
The span ID to use. If None, a new one is generated.
Source code in src/forge/core/context.py
set_trace_id ¶
Set the current trace ID.
Parameters¶
trace_id:
The trace ID to use. If None, a new one is generated.
Returns¶
Token[str]
A token that can be used with reset_trace_id to restore
the previous value.