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

# 4.4 Kernel API

> Reference for KcodeKernel, PluginRegistry, PluginLifecycleManager, PermissionService, InMemoryEventBus, and ConfigurationStore.

## 4.4.1 KcodeKernel

A domain-agnostic composition of the registry, lifecycle manager, event bus, permissions, and configuration.

```ts theme={"system"}
import { KcodeKernel } from "@kcode/kernel";

const kernel = new KcodeKernel({ logger, navigation, notifications, auth });
kernel.lifecycle.discover(plugin);
await kernel.lifecycle.register(plugin);
await kernel.lifecycle.initialize(plugin.id);
await kernel.lifecycle.activate(plugin.id);
```

| Member | Description |
| - | - |
| `new KcodeKernel(services, events?)` | `services` is `{ logger, navigation, notifications, auth }`. `events` defaults to a new `InMemoryEventBus`. |
| `registry`, `events`, `configuration`, `permissions`, `lifecycle` | The composed components. |
| `createContext(pluginId)` | Returns a frozen `PluginContext` for a registered plugin. Throws for an unregistered ID. |

## 4.4.2 PluginRegistry

| Method | Description |
| - | - |
| `register(plugin)` | Validates and stores a frozen snapshot. Throws `PluginRegistryError` on failure. |
| `unregister(pluginId)` | Removes a plugin. Throws when it is not registered. |
| `get(pluginId)` | Returns the plugin or `undefined`. |
| `getAll()`, `getRoutes()`, `getPermissions()` | Read-only collections. |
| `getNavigation()` | Navigation items sorted by `order`, then `id`. |

## 4.4.3 PluginLifecycleManager

| Method | Allowed from | Result state |
| - | - | - |
| `discover(plugin)` | None | `discovered` |
| `register(plugin)` | `discovered` | `registered` |
| `initialize(id)` | `registered` | `initialized` |
| `activate(id)` | `initialized`, `inactive` | `active` |
| `deactivate(id)` | `active` | `inactive` |
| `dispose(id)` | `registered`, `initialized`, `active`, `inactive` | `disposed`, then unregistered |

`getState(id)` and `getStates()` return the current states.

| Error code | Meaning |
| - | - |
| `ALREADY_DISCOVERED` | The plugin was discovered before. |
| `INVALID_TRANSITION` | The current state does not allow the operation. |
| `NOT_REGISTERED` | The registry has no such plugin. |
| `HOOK_FAILED` | A plugin hook threw. The original error is in `cause`. |
| `EVENT_DELIVERY_FAILED` | The state changed but a lifecycle event handler failed. |

## 4.4.4 PermissionService

| Method | Description |
| - | - |
| `grant(subjectId, permissionId)` | Adds a grant. |
| `revoke(subjectId, permissionId)` | Removes a grant. Returns whether one was removed. |
| `replace(subjectId, permissionIds)` | Replaces all grants for a subject. |
| `clear(subjectId)` | Removes all grants. |
| `has`, `hasAll`, `hasAny` | Checks. A subject with the administrator permission, `platform.admin` by default, always passes. |
| `assert(subjectId, permissionId)` | Throws `PermissionDeniedError` when missing. |
| `getGrantedPermissions(subjectId)` | Sorted list of grants. |

## 4.4.5 InMemoryEventBus and ConfigurationStore

`InMemoryEventBus` implements `publish`, `emit`, `subscribe`, and `unsubscribe`. `ConfigurationStore.createPluginConfiguration(pluginId, settings)` returns a plugin-scoped `PluginConfigurationService` and fills in defaults. `deletePluginConfiguration(pluginId)` drops a namespace.


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