Engine reference — generated from the canonical engineering source. Do not edit this page by hand.
Prompt Cache Layout and Prefix Stability
Luna’s system prompt is assembled in a canonical, cache-friendly layout. The goal is that the bytes a provider can cache stay identical turn-to-turn and child-to-child, while genuinely volatile context (date, cwd, git state, memory, query-specific retrieval) changes only after a literal boundary.
Canonical layout
client/core/system_prompt.lua partitions sections into two classes:
| Stable (cached) | Volatile (not cached) |
|---|---|
| intro / identity | date, platform, model, working directory |
| system rules (and desktop rules) | git context (branch, commits, dirty count) |
| mode section (build/plan/explore/review/general/coordinate) | memory context |
| instruction files (AGENTS.md/CLAUDE.md/SYSTEM.md/.cursorrules, APPEND_SYSTEM.md) | skills discovery output |
| tool usage guidelines | |
| available tool descriptions |
The stable sections are joined with \n\n, followed by the literal marker in
client/core/prompt_prefix.lua:
\n\n<!-- luna:stable-prefix-boundary -->\n\n
then the volatile sections. All existing prompt content is preserved; only
order and the boundary insertion changed. The old order is available through
the test/benchmark seam SystemPrompt.set_layout_for_test("legacy").
Deterministic tool order
Tools.specs (and Tools.specs_by_namespace) sort by advertised tool
name, and the prompt’s tool-guideline/tool-description lists sort their input
copy. The same tool set therefore produces a byte-identical tools array and
stable prompt text regardless of registry hash order.
Prefix hash and diagnostics
client/core/prompt_prefix.lua computes:
sha256( len(stable_text) .. ":" .. stable_text
.. "|" .. len(tool_bytes) .. ":" .. tool_bytes )
where tool_bytes is a canonical, key-sorted encoding of each tool’s
name/description/input_schema (sorted by tool name). Diagnostics
(hash, backend, stable_text_bytes, volatile_text_bytes,
tool_schema_bytes, cacheable_bytes, boundary) are stored on
session.prompt_prefix and on the provider instance as
provider._last_prompt_prefix (never inside the request table — that table is
JSON-encoded verbatim). Read them through PromptPrefix.diagnostics(source).
The backend field is sha256 when libcrypto (or the openssl CLI fallback)
is available; otherwise a deterministic FNV-1a digest is used and recorded
honestly.
Provider markers
For Anthropic-wire providers that support cache_control, the client splits
the system prompt at the boundary into two text blocks: the stable block
carries cache_control: {type="ephemeral"}, the volatile block carries none.
Concatenating the blocks reproduces the prompt bytes exactly. The existing
recency markers on the last three messages are unchanged (they are inherently
after the boundary). Boundary-less prompts (custom config.system_prompt)
keep the legacy single-block shape with the marker on that block.
Cache-identical forks
The local sub-agent spawn path (client/core/sub_agent.lua) gives
_shared_system_prompt and _frozen_tool_specs a real producer:
the parent’s lazily-built prompt is materialized before the spawn,
Agent._build_system_prompt snapshots the advertised tool-spec array (deep
copy) as _frozen_tool_specs,
a local child inherits the parent’s exact prompt string and a deep copy of
that array; Agent:_ensure_system_prompt serves the frozen array for the
provider wire and prompt hash while config._tool_filter continues to
enforce the child’s narrower dispatch set,
forks taken mid-tool get one canonical tool_result placeholder for the
unmatched trailing tool_use (_generate_fork_placeholders with
canonical=true, only_incomplete=true).
Divergence starts only after the boundary (memory fence, new turns).
ZMQ limitation (explicit)
ZMQ and background children cannot share the parent’s provider cache
state. The spawn crosses a process/worker boundary into a possibly
different API key, provider endpoint, or model; Anthropic/OpenAI cache lines
are per-session. Background forwarding carries the frozen prompt bytes so
the child starts from identical instructions, but no cache state crosses the
wire — this is documented, not faked. cache_context is intentionally not
forwarded on the ZMQ path.
Measurement
luajit benchmarks/prompt_prefix_stability.lua scripts the same fleet turn
twice for the canonical and legacy layouts and reports cacheable bytes, hash
stability, fork equality, and timing. The measured dev-host run lives in
benchmarks/prompt-prefix-stability.md.