Skip to content
pleach
Build

Heartwood

A persistent agent identity — a HEARTWOOD.md persona composed into the frozen prefix of the system prompt, the same way a hosted app loads identity from a database.

Heartwood is the agent's persistent identity — a persona that leads every turn's system prompt. A standalone @pleach/core consumer gives its agent a stable voice from a HEARTWOOD.md file (the "SOUL.md" pattern); a hosted app loads the same section from a database. Either way the identity is composed into the frozen prefix — the head of the system prompt that does not change turn-to-turn — so it sits inside the cacheable region rather than rotating the cache key.

Ships from the @pleach/core/quickstart barrel: createFileHeartwoodLoader, createHeartwoodPlugin, formatHeartwoodSection, and the HeartwoodSource type.

Subpath@pleach/core/quickstart

The file loader

createFileHeartwoodLoader() resolves the identity file on a first-non-empty-wins order and formats the result into an identity section: an explicit path option first (a relative path resolves against the current working directory), then ./HEARTWOOD.md in the working directory, then ~/.pleach/heartwood.md. Cap the working size with maxChars (default 2000). The read is synchronous, so the identity is present from the very first turn.

import { createPleachAgent } from "@pleach/core";
import { createFileHeartwoodLoader } from "@pleach/core/quickstart";

const agent = createPleachAgent({
  context: process.env.OPENROUTER_API_KEY,
  heartwoodSource: createFileHeartwoodLoader(), // reads ./HEARTWOOD.md
});

Two scopes: per-project vs user-global

The two file scopes are the per-project soul (./HEARTWOOD.md in the working directory) and the user-global soul (~/.pleach/heartwood.md). With no explicit path, the loader reads both and composes them: the global body first, then the project body under a "Project heartwood" heading, so the project wins on conflict. When the shared maxChars cap overflows, the global part is truncated first and the cut is marked. An explicit path skips the merge and reads that one file. Keep a durable persona in ~/.pleach/heartwood.md, then add a ./HEARTWOOD.md to a project to refine it there.

A HEARTWOOD.md is plain markdown. An optional ## Voice & Tone heading splits persona from voice; the identity leads and the voice follows.

You are Atlas, a meticulous research companion. You cite every
claim and never guess at a number you have not seen.

## Voice & Tone
Terse, precise, no hedging.

The agent's name

The quickstart agent is named Hazel. createPleachAgent and createPleachRoute open the identity section with "Your name is Hazel." A name: line renames it. Put it first in ~/.pleach/heartwood.md or ./HEARTWOOD.md:

name: Fern

You are a meticulous research companion.

The line is removed from the persona text and becomes "Your name is Fern." Only the first line that is neither blank nor a lone HTML comment counts, so a name: further down stays prose. The project file's name beats the global one. The agentName option sets the default instead, and null names nobody.

A name is 1 to 32 letters, digits, spaces or .-_', starting with a letter or digit. you, user, agent, assistant, system and pleach are refused in any case, because a label like that would pose as you or as the client. Names made of blank Hangul filler letters are refused too. A refused name is dropped and the default is used. A file saved with a byte-order mark or CRLF line endings is read correctly.

The source contract

heartwoodSource is a HeartwoodSource — () => string | null (or its async form). Return the identity text, or null for "no identity this run" (which leaves the prompt byte-identical to having no heartwood at all). createFileHeartwoodLoader is one implementation; a hosted app passes its own loader that reads a row from a database.

import type { HeartwoodSource } from "@pleach/core/quickstart";

const fromDb: HeartwoodSource = () => loadPersonaRowSync(userId)?.identity ?? null;
FieldTypeNotes
heartwoodSourceHeartwoodSource | undefined() => string | null (sync or async). Omit ⇒ no identity section.
heartwoodMaxCharsnumber | undefinedRoute-level HARD ceiling on the resolved section, default 8000.

A sync source is resolved once so the first turn already carries the identity; an async source primes fire-and-forget (the first turn may miss it, later turns have it). The heartwoodMaxChars ceiling is the cost backstop for the never-dropped frozen-prefix slot — a loader's own maxChars below it is honored verbatim, a raw huge source is bounded here.

As a plugin

Under the façade, createHeartwoodPlugin(source) contributes the identity through contributeRuntimeAwarePrompts as a prepend, so it composes ahead of the core baseline and lands at the stable head. Use it directly when you compose the runtime yourself instead of through createPleachAgent.

import { createHeartwoodPlugin } from "@pleach/core/quickstart";

const plugin = createHeartwoodPlugin(() => "You are Atlas.");

Where to go next