Skip to content

Documentation / @agentick/kernel / KernelContext

Interface: KernelContext ​

Defined in: kernel/src/context.ts:171

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 ​

typescript
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'
});
typescript
interface AppContext extends KernelContext {
  customHandle?: MyCustomHandle;
  appMetadata: AppSpecificMetadata;
}

See ​

Properties ​

[KERNEL_CONTEXT_SYMBOL]? ​

optional [KERNEL_CONTEXT_SYMBOL]: boolean

Defined in: kernel/src/context.ts:260

Type brand for context detection


channels? ​

optional channels: ChannelServiceInterface

Defined in: kernel/src/context.ts:193

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:183

Global request event bus for subscribing to all events


executionHandle? ​

optional executionHandle: EventEmitter<any>

Defined in: kernel/src/context.ts:187

Operation-specific event emitter (from .withHandle())


executionId? ​

optional executionId: string

Defined in: kernel/src/context.ts:222

Current execution ID. Set when entering an execution boundary. All procedures within this execution share this ID.


executionType? ​

optional executionType: string

Defined in: kernel/src/context.ts:228

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:179

Application-specific metadata


metrics ​

metrics: ContextMetrics

Defined in: kernel/src/context.ts:181

Accumulated execution metrics


middleware? ​

optional middleware: object

Defined in: kernel/src/context.ts:253

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 ​

Middleware[]


origin? ​

optional origin: ProcedureNode

Defined in: kernel/src/context.ts:210

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? ​

optional parentExecutionId: string

Defined in: kernel/src/context.ts:234

Parent execution ID for nested executions (e.g., component_tool called from engine). Enables DevTools to show execution hierarchy.


procedureGraph? ​

optional procedureGraph: ProcedureGraph

Defined in: kernel/src/context.ts:198

Procedure graph for tracking procedure execution hierarchy. Automatically initialized when first procedure is executed.


procedureNode? ​

optional procedureNode: ProcedureNode

Defined in: kernel/src/context.ts:204

Current procedure node in the graph


procedurePid? ​

optional procedurePid: string

Defined in: kernel/src/context.ts:202

Current procedure PID (for tracking nested procedures).


requestId ​

requestId: string

Defined in: kernel/src/context.ts:173

Unique identifier for this request/execution


sessionId? ​

optional sessionId: string

Defined in: kernel/src/context.ts:240

Session ID (set by session's runWithContext). Available during all session execution — tool calls, transformCompiled, etc.


signal? ​

optional signal: AbortSignal

Defined in: kernel/src/context.ts:185

Abort signal for cooperative cancellation


tick? ​

optional tick: number

Defined in: kernel/src/context.ts:246

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:175

Correlation ID for distributed tracing


user? ​

optional user: UserContext

Defined in: kernel/src/context.ts:177

User information (from auth)

Released under the ISC License.