Book uses Metro, a compile-time dependency injection library for Kotlin. Unlike runtime DI frameworks, Metro generates standard Kotlin code to wire your dependencies, which keeps the binary small and start-up time fast.

The AppGraph

The AppGraph is the central definition of all dependencies in the system. It is located in jvm/cli/src/main/kotlin/us/nineworlds/book/cli/di/AppGraph.kt.

AppGraph is composed by inheriting four domain-aligned provider module interfaces:

  • TuiModule (us.nineworlds.book.cli.di.TuiModule): Provides TUI subsystem dependencies (Terminal, TuiRenderer, Tui, CliLogger, SlashCommandRegistry).

  • AgentLoopModule (us.nineworlds.book.cli.di.AgentLoopModule): Provides agent loop and LLM execution dependencies (AgentLoopEnvironment, AgentLoopStateMachine, AgentLoopFactory, Agent, RetryConfig, ProviderConfig, PromptExecutorFactory, LLMClientAPI, CompactionPolicy).

  • ToolsModule (us.nineworlds.book.cli.di.ToolsModule): Provides tools, skills, prompt loader, extensions, ACP, and MCP dependencies (ToolRegistry, ExtensionRegistry, SkillRegistry, ExtensionContext, SystemPromptLoader, McpClientFacade, AcpServerDependencies, AcpServer, ServeCommand, Set<Tool>).

  • InfrastructureModule (us.nineworlds.book.cli.di.InfrastructureModule): Provides core infrastructure I/O, process, and session dependencies (FileSystem, ConfigManager, SessionManager, SessionAutoNamer, DynamicLogRouter, SessionOrchestrator, DefaultSessionResolver, ProcessRunner).

Key Components Provided:

  • AgentLoop (from us.nineworlds.book.domain.agentloop): The agent orchestrator. Not a singleton โ€” one instance is created per active session via AgentLoopFactory.

  • Agent (from us.nineworlds.book.domain): Provided as a KoogAgent (us.nineworlds.book.providers.koog) via AgentLoopModule.

  • ProviderConfig (from us.nineworlds.book.infrastructure): Pure data class holding provider credentials (openAiKey, openAiBaseUrl, anthropicKey) resolved at application startup from environment variables via AgentLoopModule.provideProviderConfig(). Injected directly into KoogAgent and PromptExecutorFactory without mutating JVM system properties.

  • LLMClientAPI (from Koog prompt-executor-clients): Provided as OpenAILLMClient via AgentLoopModule.provideLlmClient(), scoped @SingleIn(AppScope::class). Injected into KoogAgent for model listing โ€” delegates to LLMClientAPI.models() and enriches results via ModelMetadataRegistry.

  • FileSystem (from us.nineworlds.book.domain.filesystem): Provided as a SandboxedOkioFileSystem (us.nineworlds.book.infrastructure.filesystem) via InfrastructureModule.

  • ToolRegistry (from us.nineworlds.book.domain.tools): Wires the built-in tools (ReadFileTool, BashTool, etc.) from us.nineworlds.book.infrastructure.tools via ToolsModule.

  • ProcessRunner (from us.nineworlds.book.domain.process): Executes external processes. Implemented by ProcessBuilderRunner (us.nineworlds.book.infrastructure.process) as a @SingleIn(AppScope::class) singleton via InfrastructureModule. Injected into KtsToolLoader and forwarded to every BookScriptReceiver so scripts can call processRunner.run(command) for non-blocking coroutine-safe process execution.

  • ExtensionScriptLoader (from us.nineworlds.book.domain.extensions): Seam for loading extension scripts asynchronously. Provided by KtsToolLoader (us.nineworlds.book.scripts in :jvm:scripting) as a @SingleIn(AppScope::class) singleton via InfrastructureModule. Exposed on AppGraph as val extensionScriptLoader: ExtensionScriptLoader.

  • SystemPromptLoader (from us.nineworlds.book.domain.systemprompt): Loads and merges system prompts via ToolsModule.

  • Session stack: SessionManager, SessionAutoNamer, and DefaultSessionResolver (all from us.nineworlds.book.infrastructure.session) are wired via InfrastructureModule for persistence, resume flows, and automatic session naming.

@SingleIn(AppScope::class)
@DependencyGraph
interface AppGraph : TuiModule, AgentLoopModule, ToolsModule, InfrastructureModule {
    val agentLoop: AgentLoop
    val tui: Tui
    // ...
}

Initialization Sequence

When you run book, the following happens in Main.kt:

  1. Graph Creation: createGraph<AppGraph>() is called. Metro generates the implementation and instantiates all the components across all feature provider modules.

  2. KTS Loading: The ExtensionScriptLoader (implemented by KtsToolLoader in us.nineworlds.book.scripts in :jvm:scripting) scans ~/.bk/tools and ./.bk/tools for .bk.kts scripts. These are compiled and their tools/commands are registered in the registries provided by AppGraph. KtsToolLoader injects ProcessRunner into each script’s BookScriptReceiver, making processRunner available as a top-level property in script scope โ€” scripts use it to launch external processes without blocking the agent UI.

  3. CLI Parsing: Clikt parses the command-line arguments.

  4. Session Resolution: Optional --session, --continue, or --resume flags resolve and load session history before TUI start.

  5. TUI Start: If no sub-command is given, the Tui is started, taking control of the terminal.

Extending the Graph

To add a new dependency:

  1. Define the interface in :core:domain.

  2. Implement it in :core:infrastructure or appropriate feature module.

  3. Add a @Provides method to the relevant feature provider module (TuiModule, AgentLoopModule, ToolsModule, or InfrastructureModule), or an abstract val on AppGraph in :jvm:cli.

  4. Rebuild the project (./gradlew assemble). Metro will regenerate the graph implementation.