> ## 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.

# 2.2 Runtime View

> Runtime scenarios for Kcode Robot Suite: sign-in, opening a demo, and the plugin lifecycle.

## 2.2.1 Scenario: sign in and open a demo

```mermaid theme={"system"}
sequenceDiagram
    actor User
    participant Login as /login (server action)
    participant Auth as DemoAuthService
    participant MW as middleware
    participant Launcher as /launcher
    participant Demo as /demo/[pluginId]
    User->>Login: Submit username and password
    Login->>Auth: signIn(credentials)
    Auth-->>Login: User or AuthenticationError
    Login->>User: Set kcode-demo-session cookie, redirect to /launcher
    User->>MW: Request /launcher
    MW->>Launcher: Cookie matches, continue
    Launcher->>User: Plugin cards from the catalog
    User->>Demo: Open demo in a new tab
    Demo->>Demo: Look up plugin, render PluginRuntime with a PluginContext
```

The middleware matches `/launcher` and `/demo/:path*`. A request without the session cookie value redirects to `/login`.

## 2.2.2 Scenario: plugin lifecycle

The kernel's `PluginLifecycleManager` allows only these transitions. It serializes operations for each plugin, so hooks cannot race.

```mermaid theme={"system"}
stateDiagram-v2
    [*] --> discovered
    discovered --> registered
    registered --> initialized
    registered --> disposed
    initialized --> active
    initialized --> inactive
    initialized --> disposed
    active --> inactive
    active --> disposed
    inactive --> active
    inactive --> disposed
    disposed --> [*]
```

Every transition publishes a `plugin.lifecycle.changed` event with `pluginId`, `previousState`, and `state`. If event delivery fails, the manager raises `EVENT_DELIVERY_FAILED` after the state has already changed.

## 2.2.3 Scenario: event publishing

The `InMemoryEventBus` runs handlers in subscription order and awaits each one. A failing handler propagates the error to the publisher. `emit` is a compatibility alias for `publish`.

> [!NOTE]
>
> The web app's `PlatformProvider` builds its own per-plugin `PluginContext` and event bus in the browser. It does not run the kernel's lifecycle manager. See [Risks and technical debt](https://docs.khanhng.com/kcode-robot-suite/architecture/11-risks-and-technical-debt.md).


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