* fix: retry daemon spawn when CLI binary is busy CLINE-2115 Retry detached hub daemon startup on transient ETXTBSY spawn failures before polling discovery. This handles package-manager updates that replace the CLI binary during restart, with tests and architecture docs covering the behavior. * use fake timer * retry prewarm * retry * @clinebot to @cline
25 KiB
Cline SDK Architecture
This document is the architecture source of truth for the Cline SDK repository. It describes how the system is organized, how components interact, and the design principles that guide development decisions.
Who should read this?
- SDK contributors working across multiple packages
- Developers building integrations or host applications using
@cline/core - Plugin authors understanding the runtime and extension systems
What this covers:
- Package boundaries and responsibilities
- Dependency direction and layering rules
- Runtime flows (local, hub-backed, enterprise-managed)
- Design seams (repeated patterns instead of one-off integrations)
- Architectural constraints and why they exist
What this is NOT:
- An onboarding guide for new contributors (see README.md and CONTRIBUTING.md)
- A detailed API reference (see package READMEs and inline JSDoc)
- A user guide (see the main documentation)
Layered Model
The workspace is organized as a layered runtime stack.
flowchart LR
shared["@cline/shared"]
llms["@cline/llms"]
agents["@cline/agents"]
core["@cline/core"]
enterprise["@cline/enterprise (internal)"]
apps["Host Apps"]
llms --> shared
agents --> llms
agents --> shared
core --> agents
core --> llms
core --> shared
enterprise --> agents
enterprise --> core
enterprise --> shared
apps --> core
Package Responsibilities
@cline/shared
Owns reusable low-level contracts and infrastructure:
- shared types and schemas
- path resolution
- hook contracts/engine
- extension registry contracts
- prompt and parsing helpers
- storage path helpers
Design rule:
sharedshould not depend on higher-level runtime packages.
@cline/llms
Owns model/provider runtime concerns:
- provider settings/config resolution
- model catalogs and manifests
- shared gateway-style provider contracts
- handler creation via an internal gateway registry
- AI SDK-backed provider execution code
Design rule:
- provider-specific behavior should be isolated here, not spread across
coreor apps.
@cline/agents
Owns the stateless runtime loop:
- agent iteration loop
- tool orchestration
- runtime event emission
- hook/extension execution
- turn preparation before provider calls
- in-memory team/runtime primitives
Design rule:
agentsshould not own persistent storage or host lifecycle concerns.
@cline/core
Owns stateful orchestration:
- runtime composition
- session lifecycle
- storage and persistence
- config watching/loading and watcher projections
- settings listing and mutation orchestration
- default host tool assembly
- plugin discovery/loading
- default context compaction policy
- telemetry integration
- hub server and scheduled-runtime services under
src/hub/ - hub discovery, the detached hub daemon, and the
@cline/core/hub/daemon-entrysubpath - host-side hub client adapters (
NodeHubClient,HubSessionClient,HubUIClient,connectToHub) exported from@cline/core/hub
Design rules:
coreis the app-facing orchestration layer overagents.- hub-related modules live under
packages/core/src/hub/, grouped by service:client/contains host-facing hub clients and browser connection helpersdaemon/contains detached daemon startup, entrypoint, and local runtime handler wiringdiscovery/contains endpoint defaults, discovery records, and workspace owner resolutionserver/contains WebSocket server startup, native/browser socket adapters, server transport, server helpers, andhandlers/for hub command dispatch
- settings mutations belong in core services and hub commands, not in host-specific file writes. Hosts should call the core settings facade or the
settings.*hub command family and react tosettings.changed.
@cline/enterprise
Internal-only enterprise integration layer:
- enterprise identity adapters
- enterprise control-plane sync
- enterprise token/bundle storage
- managed rule/workflow/skill materialization
- claims-to-role mapping
- enterprise telemetry normalization and core bridge helpers
Design rules:
enterprisemay depend oncorecoremust not depend onenterprise- enterprise stays optional and internal to this repo
Runtime Flows
Local In-Process Runtime
- Host constructs a
RuntimeHostthrough@cline/core. @cline/coreselectsLocalRuntimeHostthroughpackages/core/src/runtime/host.ts.- Hosts normalize broad local config into
RuntimeSessionConfigpluslocalRuntimeoverrides before callingRuntimeHost.start(...). @cline/coreprepares a local bootstrap artifact fromlocalRuntime, then builds the runtime from it.@cline/corecreates anAgentfrom@cline/agents.@cline/agentsruns the loop using@cline/llmshandlers.@cline/corepersists state, artifacts, and metadata.
Completion telemetry is anchored to the assistant's explicit completion
declaration, not session shutdown. After each agent turn, the local
runtime inspects AgentResult.toolCalls and emits task.completed the
moment a successful submit_and_exit (the SDK analog of original
Cline's attempt_completion) is observed. shutdownSession(...)
retains a fallback emission for completed sessions that finished
without an explicit completion-tool observation, so non-interactive
runs not using the yolo preset still produce a task.completed signal.
Each session emits at most one task.completed. See DOC.md for the
event payload and source field.
Hub-Backed Runtime
- Host constructs a
RuntimeHostthrough@cline/core. @cline/coreselectsHubRuntimeHostorRemoteRuntimeHostthroughpackages/core/src/runtime/host.ts.- When no compatible local hub is already discovered,
@cline/corecan spawn a detached hub daemon and reconnect through discovery. - Hosts attach and detach from shared sessions without stopping the authority runtime, so another client can keep streaming or resume the same session later.
- The hub-hosted runtime executes the agent loop using
@cline/agentsand@cline/llms. @cline/corehub services broker sessions, events, approvals, schedules, and client-owned runtime capabilities such as session-local tool executors.- Hub event forwarding preserves structured streaming lifecycle boundaries: text/reasoning deltas, final text/reasoning completion, tool start/finish, and agent done events are translated across the hub transport so host UIs can reliably close loading/streaming state.
- Hub client adapters exported from
@cline/core/hub(NodeHubClient,HubSessionClient,HubUIClient,connectToHub) translate command/reply and event streams into host-facing APIs.
Detached daemon startup retries transient ETXTBSY spawn failures before
polling discovery. This covers package-manager updates that replace the CLI
binary immediately before a command restarts the shared hub.
Local hub discovery also carries the authentication contract for the shared
daemon. On startup, the hub server generates a cryptographically random
per-process auth token, stores it in the owner discovery record, and writes that
record with owner-only file permissions. Local clients resolve the token from
the discovery file at connection time rather than embedding it in endpoint URLs.
The server validates the token with a constant-time comparison before accepting
/hub WebSocket upgrades or /shutdown requests; WebSocket clients send it via
the Sec-WebSocket-Protocol header and shutdown requests use an
Authorization: Bearer header. Unauthenticated local processes can still probe
public health/build metadata, but they cannot attach to sessions, issue
commands, or stop the daemon.
Local hub rediscovery is limited to managed shared-daemon endpoints obtained
through discovery or ensure*HubServer(...) startup paths. Explicit endpoints,
including loopback URLs such as ws://127.0.0.1:<port>/hub, are sticky exact
targets: reconnects may retry the same socket URL, but command recovery and
startup-deadlock recovery must not replace them with the workspace-discovered
hub. This keeps custom local hubs and remote hubs from silently drifting to a
different process.
Interactive CLI Startup
apps/cliowns OpenTUI startup and must render the first frame without waiting for detached hub startup.- Interactive sessions use
backendMode: "auto"so an already-compatible hub can be reused immediately, while a missing hub is only prewarmed in the background and the TUI falls back to a local runtime for responsiveness. - Hub-required flows such as
clite hub, schedules, connectors, and--zenmay still call the explicit ensure path because those commands require a live hub before proceeding. - Resume hydration is deferred until after
renderOpenTui()so loading previous messages cannot block initial TUI paint. - Any future CLI/TUI startup work should follow the same rule: daemon startup, discovery polling, provider catalog refreshes, file indexing, and resume reads must be background or user-action gated unless a command explicitly requires their result before output.
Enterprise-Managed Runtime
- Enterprise bootstrap resolves identity through an
IdentityAdapter. - Enterprise fetches a normalized
EnterpriseConfigBundle. - Enterprise caches the token and bundle through enterprise stores.
- Enterprise materializes managed rules/workflows/skills under workspace-local
.cline/<plugin>/. - Enterprise optionally derives telemetry config or telemetry services.
- Hosts pass the prepared result into
@cline/corethrough the genericprepareseam. - Enterprise applies extensions and telemetry through
localRuntime.configOverrides, not the transport-safeRuntimeSessionConfig. @cline/coreconsumes the prepared local overrides during local bootstrap.
This keeps enterprise-specific behavior above the published orchestration layer.
Design Seams
The codebase relies on a few repeated seams instead of one-off integration paths.
1. Config Watchers
Core uses file-based discovery and watchers for:
- rules
- workflows
- skills
- agents
- hooks
- plugins
Design implication:
- new instruction sources should usually materialize into files and reuse watcher-based loading instead of inventing parallel in-memory execution paths.
- in
packages/core, config-facing discovery, parsing, watching, and slash-command projection live undersrc/extensions/config
2. Runtime Builder Inputs
DefaultRuntimeBuilder composes a runtime from generic inputs:
- tools
- hooks
- extensions
- user instruction watcher
- telemetry
Design implication:
- higher-level integrations should prefer feeding those seams rather than patching agent internals directly.
- the local runtime bootstrap lives in
packages/core/src/services/local-runtime-bootstrap.tsand feeds the builder rather than bypassing it
3. Runtime Host Boundary
Core exposes one shared execution boundary: RuntimeHost.
Concrete implementations:
LocalRuntimeHostfor in-process executionHubRuntimeHostfor shared local hub executionRemoteRuntimeHostfor explicit remote hub endpoints
Design implication:
- host selection happens in
packages/core/src/runtime/host.ts ClineCoredelegates uniformly toRuntimeHostand does not branch on local vs hub behavior- transport-specific translation belongs inside concrete hosts, not in top-level orchestration
RuntimeHostinputs stay transport-safe, whileClineCore.start(...)is the app-facing facade that normalizes broad local config before delegationRuntimeSessionConfigis transport-neutral across local, shared hub, and remote hub modes; host-local bootstrap concerns stay underlocalRuntime- client-local runtime behaviors that must survive hub mode, such as
defaultToolExecutors, are attached at session start and proxied through hub capability requests instead of changing host selection
4. Settings Mutation Boundary
Core owns settings snapshots and mutations through packages/core/src/settings.
The hub exposes the same path through settings.list and settings.toggle.
Design implication:
- hosts should not mutate skill, tool, MCP, provider, or other settings files directly
- domain-specific persistence helpers, such as skill markdown frontmatter writes, stay internal to the owning settings provider/service
- successful hub-backed mutations return an updated settings snapshot and publish
settings.changedwith the changed settings types - CLI settings surfaces may keep local snapshot rendering for startup responsiveness, but mutation flow must refresh the relevant watcher before reloading UI data
5. Session Startup Bootstrap
ClineCore.create(...) exposes a generic prepare(input) hook.
Design implication:
- higher-level packages can prepare workspace-scoped runtime state before a session starts
- core stays unaware of enterprise-specific contracts
- cleanup stays at the host boundary rather than inside the agent loop
6. Logging
Cross-package logging uses a small injected interface exported from @cline/shared:
BasicLogger— requireddebugandlog; optionalerror. Hosts map these to their backend (Pino, VS CodeOutputChannel, etc.). Many runtime options takelogger?: BasicLogger; when omitted, components skip logging or usenoopBasicLoggerwhere a full object is required.BasicLogMetadata— optional structured fields (sessionId,runId,providerId,toolName,durationMs, …) plusseverityonlogwhen a single method must represent both informational and warning-style messages (for example the CLI Pino bridge mapsseverity: "warn"to Pinowarn).
Naming clarity:
CliLoggerAdapter(CLI) — a host bundle: holds the rawpinologger (for file paths, rotation, and CLI-only concerns) and exposes.core: BasicLoggerfor anything that consumes the SDK contract. It is not anITelemetryAdapter.TelemetryLoggerSink(@cline/core) — anITelemetryAdapterthat mirrors telemetry events and metrics into aBasicLogger. It is a telemetry sink, not a host logging implementation.
The agent and other call sites route former info / warn semantics through log (warnings include severity: "warn" in metadata). Errors prefer error when implemented; otherwise log with severity: "error" is used as a fallback.
Design implication:
- logging is injectable and transport-agnostic, allowing host environments (CLI, VS Code, browser) to wire their own backends
- do not hardcode logging calls; accept a
logger?: BasicLoggerparameter instead
7. Storage Adapters
Stateful persistence should be isolated behind adapter/service layers.
Design implication:
- file-backed, SQLite-backed, RPC-backed, and enterprise-specific persistence should share service logic where possible and isolate backend differences in adapters.
8. Extension and Hook System
Extensibility is split deliberately:
- extensions register runtime contributions
- hooks intercept lifecycle stages
Design implication:
- additive runtime behavior should usually enter through these extension points instead of bespoke special-case host code.
9. Context Compaction
Context compaction is owned by core.
@cline/agentsowns the generic turn-preparation seam:- run normal lifecycle hooks
- allow hosts to rewrite message history or system prompt before the provider call
@cline/coreowns compaction policy:- inject a prepare-turn pipeline for root sessions
- choose between built-in strategies through a registry map
- keep compaction logic out of the low-level agent message builder
Design implications:
- compaction is a context-pipeline concern owned by
core agentsstays focused on the stateless loop and provider/tool orchestration- delegated/subagent flows should inherit compaction behavior through core session config, not through a separate agent-level compaction hook surface
10. Extension Layering Inside Core
packages/core/src/extensions is split by concern:
extensions/config: config loaders, parsers, watchers, and watcher projections such as runtime slash-command expansionextensions/plugin: runtime plugin discovery, loading, and sandboxingextensions/context: core-owned context/message pipeline concerns such as compaction
Design implications:
- avoid mixing config discovery code into runtime/plugin code
- avoid creating thin runtime wrapper files when a helper is fundamentally projecting watcher state
Architectural Constraints
Keep agents Stateless
Do not move these concerns into @cline/agents:
- session persistence
- provider settings storage
- RPC lifecycle
- host-specific approvals
- enterprise policy caching
Keep core Generic
Do not make @cline/core enterprise-specific.
If a capability is truly generic, add a generic seam to core. If it is enterprise-specific, keep it in @cline/enterprise.
Use One-Way Optional Layers
Optional higher-level integrations may depend on lower layers. Lower layers should not depend on optional feature packages.
That rule is what keeps:
enterprise -> coreacceptablecore -> enterpriseunacceptable
Current Internal Enterprise Design
@cline/enterprise currently integrates with core through three main entrypoints:
prepareEnterpriseRuntime(...)prepareEnterpriseCoreIntegration(...)createEnterprisePlugin(...)
Preferred bridge:
prepareEnterpriseCoreIntegration(...)
Why:
- it prepares and materializes enterprise-managed files under
.cline/<plugin>/ - it returns a valid
AgentPlugin - it can create a telemetry service from enterprise telemetry settings
- it lets core consume enterprise behavior through existing generic seams and normal watcher discovery
File-Based And Event-Driven Automation (ClineCore / CronService)
@cline/core ships a file-based automation subsystem under
packages/core/src/cron/. It lets operators author recurring and one-off
tasks as Markdown files under global ~/.cline/cron/ by default, and
event-driven tasks as events/*.event.md specs. All trigger kinds run
through the same durable queue and runtime handlers. ClineCore exposes the
SDK-facing cline.automation.* entry points; CronService is the internal
orchestrator used by core and hub layers.
Layers
- Spec parser (
cron/specs/cron-spec-parser.ts): parses YAML frontmatter + body into aCronSpecdiscriminated union (one_off | schedule | event). Types live in@cline/sharedundersrc/cron/cron-spec-types.tsso other packages can consume them without the YAML parser. Schedule expressions and timezones are validated before a spec can become runnable. - Store (
cron/store/sqlite-cron-store.ts): ownscron.dbatresolveCronDbPath()(default.cline/data/db/cron.db). Schema is bootstrapped fromcron/store/cron-schema.ts— sessions and cron live in separate DBs so their lifecycles stay decoupled. - Reconciler (
cron/specs/cron-reconciler.ts): scans the configured cron specs directory (global~/.cline/cron/by default, or workspace-scoped when configured), parses each file independently, and upserts spec state. Invalid specs are recorded withparse_status='invalid'so state is durable rather than silently dropped. Files that disappear between scans getremoved=1and their queued runs are cancelled. - Watcher (
cron/specs/cron-watcher.ts):node:fs watch({ recursive: true })with a ~250ms per-path debounce. Watcher events always trigger a re-reconcile — the reconciler is always the source of truth, not the watcher stream. - Materializer (
cron/runner/cron-materializer.ts): turns file-triggered specs into queuedcron_runs. One-off: at most one run record per(spec_id, revision), including failed runs so specs do not retry accidentally. Schedule: "one overdue catch-up on startup then advance" using timezone-awaregetNextCronTime. - Event ingress (
cron/events/cron-event-ingress.ts): accepts already-normalizedAutomationEventEnvelopevalues, persists them intocron_event_log, matches enabled event specs byevent_typeplus declarative filters, applies dedupe/debounce/cooldown policy, and enqueuescron_runswithtrigger_kind='event'. It never executes agents directly. Plugins can declareautomationEventsand submit normalized events throughctx.automation.ingestEvent(...); sandboxed plugins forward those events through the core plugin event bridge. - Runner (
cron/runner/cron-runner.ts): pollscron.db, atomically claims queued runs, executes them via the existingHubScheduleRuntimeHandlers(startSession→sendSession→stopSession/abortSession), renews the run claim while execution is active, writes a markdown report per run, and transactionally updates status. File specs can constrain tool availability, config extension loading (rules,skills,plugins), session source, and a notes directory that is injected into the system prompt. Event runs include the normalized trigger event context in the prompt. - Reports (
cron/reports/cron-report-writer.ts): writes.cline/cron/reports/<run-id>.mdwith run frontmatter plus## Summary,## Usage,## Tool Calls, and, for event runs,## Trigger Eventsections. - Service (
cron/service/cron-service.ts): orchestrates all of the above.ClineCore.create({ automation })owns the SDK-facing lifecycle and exposescline.automation.*methods. Hub-side callers can submit normalized events through thecron.event.ingestcommand.
The detached hub daemon passes its workspace root as cronOptions, so
normal CLI/hub startup watches ${workspaceRoot}/.cline/cron/ without a
custom host needing to opt in.
Programmatic hub schedules are stored as cron_specs with source
hub-schedule and execute through the same cron_runs
claim/requeue/report flow as file-backed one-off, recurring, and
event-driven specs. The hub schedule command surface remains a thin adapter;
there is no separate schedules table, schedule store, or schedule runner.
Navigating the Codebase
Starting Points by Task
I want to understand the agent loop and tool execution:
- Start:
packages/agents/src/agent.ts— the stateless runtime loop - Then:
packages/agents/src/agent-step.ts— individual iteration steps - Extensions:
packages/core/src/extensions/plugin/— plugin discovery and sandboxing
I want to understand session persistence and state:
- Start:
packages/core/src/runtime/host/local-runtime-host.ts— local session lifecycle - Then:
packages/core/src/runtime/orchestration/— session orchestration - Settings:
packages/core/src/settings/— settings mutation and state
I want to understand the hub system:
- Start:
packages/core/src/hub/server/— WebSocket server and hub command handlers - Clients:
packages/core/src/hub/client/— host-side hub clients - Transport:
packages/core/src/hub/runtime-host/— hub-backed runtime hosts
I want to add a new tool:
- Tools registry:
packages/core/src/extensions/tools/— built-in tool definitions - Tool execution:
packages/agents/src/tool-use.ts— how tools are called - Plugin tools:
packages/core/src/extensions/plugin/— plugin-registered tools
I want to understand settings and configuration:
- Watcher system:
packages/core/src/extensions/config/— file watching and loading - Provider config:
packages/core/src/runtime/config/— provider settings resolution - Settings services:
packages/core/src/settings/— settings state and mutation
I want to add a new runtime feature (hook/extension):
- Hook contracts:
packages/shared/src/hooks/— hook types and engine - Plugin system:
packages/core/src/extensions/plugin/— plugin discovery and execution - Runtime builder:
packages/core/src/services/local-runtime-bootstrap.ts— how runtime is composed
File Naming Conventions
*.ts— TypeScript source*.test.ts— unit tests (Vitest)*.e2e.test.ts— end-to-end tests requiring full integration*.tsin examples — runnable example files (plugins, hooks)*.mdfiles inapps/examples/— documentation and markdown-based specs (cron, events)
Key Type Locations
ClineCore—packages/core/src/index.ts— the main SDK orchestratorAgent—packages/agents/src/agent.ts— the agent loopRuntimeHost—packages/core/src/runtime/host/runtime-host.ts— execution abstractionAgentPlugin—packages/shared/src/plugin/— plugin contractCronSpec—packages/shared/src/cron/cron-spec-types.ts— automation specs
Publishability Constraint
This repo has both publishable SDK packages and internal workspace packages.
Architectural consequence:
- internal packages must not accidentally become part of the publishable SDK surface
- release automation should only target the intended published packages
- internal code may compose with published packages, but published packages should not take hard dependencies on internal-only workspace layers unless you explicitly intend to publish that integration
Published Packages
The following packages are published to npm:
@cline/shared— shared types, contracts, and low-level utilities@cline/llms— provider integrations and model manifests@cline/agents— the agent loop and tool orchestration@cline/core— the main SDK with session management, hub, and configuration
Internal Packages
The following packages are internal and not published:
@cline/enterprise— enterprise integrations (internal only)apps/cli— CLI implementationapps/webview— VS Code webviewapps/examples— example plugins and integrations