Documentation / @agentick/core / KernelContext
Interface: KernelContext
Defined in: kernel/src/context.ts:172
Execution context that flows through all async operations via AsyncLocalStorage.
The KernelContext contains all state needed during execution:
- Identity and tracing (requestId, traceId, user)
- Event buses (events, executionHandle)
- Cancellation (signal)
- Communication (channels)
- Execution tracking (procedureGraph, procedurePid, origin)
- Extensible storage (metadata, metrics)
Access the current context with Context.get() from anywhere in your code - no need to pass it explicitly through function calls.
Examples
const ctx = Context.create({
user: { id: 'user-1' },
metadata: { conversationId: 'conv-123' }
});
await Context.run(ctx, async () => {
// Context is available here and in all async calls
const current = Context.get();
console.log(current.user?.id); // 'user-1'
});interface AppContext extends KernelContext {
customHandle?: MyCustomHandle;
appMetadata: AppSpecificMetadata;
}See
- Context - Static methods to create/access context
- UserContext - User information structure
Properties
[KERNEL_CONTEXT_SYMBOL]?
optional[KERNEL_CONTEXT_SYMBOL]:boolean
Defined in: kernel/src/context.ts:274
Type brand for context detection
activeSpan?
optionalactiveSpan:Span
Defined in: kernel/src/context.ts:269
Active telemetry span for the currently-executing procedure.
Set by ExecutionTracker when forking the per-procedure context. Inside a procedure body (or any middleware/hook running within it), reading Context.get().activeSpan returns the span the engine started for that procedure — letting middleware enrich it with setAttribute, addEvent, etc., without needing a parallel ALS or a getActiveSpan() accessor.
Undefined outside of any procedure execution.
channels?
optionalchannels:ChannelServiceInterface
Defined in: kernel/src/context.ts:194
Channel service for bidirectional communication (optional). Injected by Engine when channels are configured. Tools and components can access channels via this service.
events
events:
EventEmitter
Defined in: kernel/src/context.ts:184
Global request event bus for subscribing to all events
executionHandle?
optionalexecutionHandle:EventEmitter<any>
Defined in: kernel/src/context.ts:188
Operation-specific event emitter (from .withHandle())
executionId?
optionalexecutionId:string
Defined in: kernel/src/context.ts:223
Current execution ID. Set when entering an execution boundary. All procedures within this execution share this ID.
executionType?
optionalexecutionType:string
Defined in: kernel/src/context.ts:229
Type of execution at this boundary (e.g., 'engine', 'model', 'component_tool', 'fork', 'spawn'). Only meaningful at execution boundaries.
metadata
metadata:
ContextMetadata
Defined in: kernel/src/context.ts:180
Application-specific metadata
metrics
metrics:
ContextMetrics
Defined in: kernel/src/context.ts:182
Accumulated execution metrics
middleware?
optionalmiddleware:object
Defined in: kernel/src/context.ts:254
Middleware registry for procedure execution. Procedures read this at execution time to get instance-level middleware. Typically set by createApp() from the Agentick instance.
getMiddlewareFor()
getMiddlewareFor(
procedureName):Middleware[]
Parameters
procedureName
string
Returns
origin?
optionalorigin:ProcedureNode
Defined in: kernel/src/context.ts:211
Origin procedure node - the root procedure that initiated this execution chain. Undefined for the root procedure itself (since it IS the origin). Set automatically by ExecutionTracker when procedures are executed.
parentExecutionId?
optionalparentExecutionId:string
Defined in: kernel/src/context.ts:235
Parent execution ID for nested executions (e.g., component_tool called from engine). Enables DevTools to show execution hierarchy.
parentHandle?
optionalparentHandle:ExecutionHandle
Defined in: core/src/types.ts:91
Parent execution handle for child executions. Set by Engine when creating child executions.
parentPid?
optionalparentPid:string
Defined in: core/src/types.ts:86
Parent execution PID for child executions. Set by Engine when creating child executions.
procedureGraph?
optionalprocedureGraph:ProcedureGraph
Defined in: kernel/src/context.ts:199
Procedure graph for tracking procedure execution hierarchy. Automatically initialized when first procedure is executed.
procedureNode?
optionalprocedureNode:ProcedureNode
Defined in: kernel/src/context.ts:205
Current procedure node in the graph
procedurePid?
optionalprocedurePid:string
Defined in: kernel/src/context.ts:203
Current procedure PID (for tracking nested procedures).
requestId
requestId:
string
Defined in: kernel/src/context.ts:174
Unique identifier for this request/execution
sessionId?
optionalsessionId:string
Defined in: kernel/src/context.ts:241
Session ID (set by session's runWithContext). Available during all session execution — tool calls, transformCompiled, etc.
signal?
optionalsignal:AbortSignal
Defined in: kernel/src/context.ts:186
Abort signal for cooperative cancellation
tick?
optionaltick:number
Defined in: kernel/src/context.ts:247
Current tick number (set by engine during tick loop). Enables events to include tick context for correlation.
traceId
traceId:
string
Defined in: kernel/src/context.ts:176
Correlation ID for distributed tracing
user?
optionaluser:UserContext
Defined in: kernel/src/context.ts:178
User information (from auth)